Preisgruppenerweiterung auf Basis von Andres Patch - Thanks
[kivitendo-erp.git] / bin / mozilla / is.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
29 #
30 # Inventory invoicing module
31 #
32 #======================================================================
33
34 use SL::IS;
35 use SL::PE;
36 use Data::Dumper;
37
38 require "$form->{path}/io.pl";
39 require "$form->{path}/arap.pl";
40
41 1;
42
43 # end of main
44
45 sub add {
46   $lxdebug->enter_sub();
47
48   $form->{title} = $locale->text('Add Sales Invoice');
49
50   $form->{callback} =
51     "$form->{script}?action=add&type=$form->{type}&login=$form->{login}&path=$form->{path}&password=$form->{password}"
52     unless $form->{callback};
53
54   $form{jsscript} = "date";
55
56   if ($myconfig{acs} =~ "AR--Add Sales Invoice" || $myconfig{acs} =~ "AR--AR")
57   {
58     $form->error("Access Denied");
59   }
60
61   &invoice_links;
62   &prepare_invoice;
63   &display_form;
64
65   $lxdebug->leave_sub();
66 }
67
68 sub edit {
69   $lxdebug->enter_sub();
70 print STDERR "is.pl-edit\n";
71   $form->{title} = $locale->text('Edit Sales Invoice');
72
73   if ($myconfig{acs} =~ "AR--Add Sales Invoice" || $myconfig{acs} =~ "AR--AR")
74   {
75     $form->error("Access Denied");
76   }
77
78   &invoice_links;
79   &prepare_invoice;
80   &display_form;
81
82   $lxdebug->leave_sub();
83 }
84
85 sub invoice_links {
86   $lxdebug->enter_sub();
87 print STDERR "is.pl-invoice_links\n";
88   $form->{vc} = 'customer';
89
90   # create links
91   $form->{webdav}   = $webdav;
92   $form->{lizenzen} = $lizenzen;
93
94   $form->create_links("AR", \%myconfig, "customer");
95
96   if ($form->{all_customer}) {
97     unless ($form->{customer_id}) {
98       $form->{customer_id} = $form->{all_customer}->[0]->{id};
99     }
100   }
101   $cp_id = $form->{cp_id};
102   IS->get_customer(\%myconfig, \%$form);
103
104   IS->retrieve_invoice(\%myconfig, \%$form);
105   $form->{cp_id} = $cp_id;
106
107   # currencies
108   @curr = split /:/, $form->{currencies};
109   chomp $curr[0];
110   $form->{defaultcurrency} = $curr[0];
111
112   map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
113
114   $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
115
116   if ($form->{all_customer}) {
117     $form->{customer} = "$form->{customer}--$form->{customer_id}";
118     map { $form->{selectcustomer} .= "<option>$_->{name}--$_->{id}\n" }
119       (@{ $form->{all_customer} });
120   }
121
122   # departments
123   if ($form->{all_departments}) {
124     $form->{selectdepartment} = "<option>\n";
125     $form->{department}       = "$form->{department}--$form->{department_id}";
126
127     map {
128       $form->{selectdepartment} .=
129         "<option>$_->{description}--$_->{id}\n"
130     } (@{ $form->{all_departments} });
131   }
132
133   $form->{employee} = "$form->{employee}--$form->{employee_id}";
134
135   # sales staff
136   if ($form->{all_employees}) {
137     $form->{selectemployee} = "";
138     map { $form->{selectemployee} .= "<option>$_->{name}--$_->{id}\n" }
139       (@{ $form->{all_employees} });
140   }
141
142   # forex
143   $form->{forex} = $form->{exchangerate};
144   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
145
146   foreach $key (keys %{ $form->{AR_links} }) {
147     foreach $ref (@{ $form->{AR_links}{$key} }) {
148       $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}\n";
149     }
150
151     if ($key eq "AR_paid") {
152       for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
153         $form->{"AR_paid_$i"} =
154           "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
155
156         # reverse paid
157         $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
158         $form->{"datepaid_$i"} =
159           $form->{acc_trans}{$key}->[$i - 1]->{transdate};
160         $form->{"forex_$i"} = $form->{"exchangerate_$i"} =
161           $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
162         $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
163         $form->{"memo_$i"}   = $form->{acc_trans}{$key}->[$i - 1]->{memo};
164
165         $form->{paidaccounts} = $i;
166       }
167     } else {
168       $form->{$key} =
169         "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
170     }
171
172   }
173
174   $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
175
176   $form->{AR} = $form->{AR_1} unless $form->{id};
177
178   $form->{locked} =
179     ($form->datetonum($form->{invdate}, \%myconfig) <=
180      $form->datetonum($form->{closedto}, \%myconfig));
181
182   $lxdebug->leave_sub();
183 }
184
185 sub prepare_invoice {
186   $lxdebug->enter_sub();
187 print STDERR "is.pl-prepare_invoice\n";
188   $form->{type}     = "invoice";
189   $form->{formname} = "invoice";
190   $form->{format}   = "html";
191   $form->{media}    = "screen";
192
193   if ($form->{id}) {
194
195     map { $form->{$_} =~ s/\"/&quot;/g }
196       qw(invnumber ordnumber quonumber shippingpoint shipvia notes intnotes);
197
198 #     # get pricegroups for parts
199 #     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
200
201     foreach $ref (@{ $form->{invoice_details} }) {
202       $i++;
203       map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
204       $form->{"discount_$i"} =
205         $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
206       ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
207       $dec           = length $dec;
208       $decimalplaces = ($dec > 2) ? $dec : 2;
209
210       $form->{"sellprice_$i"} =
211         $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
212                              $decimalplaces);
213       $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
214
215       map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
216         qw(partnumber description unit partnotes);
217       $form->{rowcount} = $i;
218
219 #      # build up html code for prices_$i
220 # print STDERR "set_pricegroup aus is.pl-prepare_invoice\n";
221 #      set_pricegroup();
222     }
223   }
224   $lxdebug->leave_sub();
225 }
226
227 sub form_header {
228   $lxdebug->enter_sub();
229 print STDERR "is.pl-form_header\n";
230   # set option selected
231   foreach $item (qw(AR customer currency department employee contact)) {
232     $form->{"select$item"} =~ s/ selected//;
233     $form->{"select$item"} =~
234       s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
235   }
236
237   #build contacts
238   if ($form->{all_contacts}) {
239
240     $form->{selectcontact} = "";
241     foreach $item (@{ $form->{all_contacts} }) {
242       if ($form->{cp_id} == $item->{cp_id}) {
243         $form->{selectcontact} .=
244           "<option selected>$item->{cp_name}--$item->{cp_id}";
245       } else {
246         $form->{selectcontact} .= "<option>$item->{cp_name}--$item->{cp_id}";
247       }
248     }
249   }
250
251   #else {$form->{all_contacts} = 0;}
252
253   $form->{exchangerate} =
254     $form->format_amount(\%myconfig, $form->{exchangerate});
255
256   $form->{creditlimit} =
257     $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
258   $form->{creditremaining} =
259     $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
260
261   $exchangerate = "";
262   if ($form->{currency} ne $form->{defaultcurrency}) {
263     if ($form->{forex}) {
264       $exchangerate .=
265           qq|<th align=right>|
266         . $locale->text('Exchangerate')
267         . qq|</th><td>$form->{exchangerate}<input type=hidden name=exchangerate value=$form->{exchangerate}></td>|;
268     } else {
269       $exchangerate .=
270           qq|<th align=right>|
271         . $locale->text('Exchangerate')
272         . qq|</th><td><input name=exchangerate size=10 value=$form->{exchangerate}></td>|;
273     }
274   }
275   $exchangerate .= qq|
276 <input type=hidden name=forex value=$form->{forex}>
277 |;
278
279   $customer =
280     ($form->{selectcustomer})
281     ? qq|<select name=customer>$form->{selectcustomer}</select>\n<input type=hidden name="selectcustomer" value="$form->{selectcustomer}">|
282     : qq|<input name=customer value="$form->{customer}" size=35>|;
283
284   #sk
285   $contact =
286     ($form->{selectcontact})
287     ? qq|<select name=contact>$form->{selectcontact}</select>\n<input type=hidden name="selectcontact" value="$form->{selectcontact}">|
288     : qq|<input name=contact value="$form->{contact}" size=35>|;
289
290   $department = qq|
291               <tr>
292                 <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
293                 <td colspan=3><select name=department>$form->{selectdepartment}</select>
294                 <input type=hidden name=selectdepartment value="$form->{selectdepartment}">
295                 </td>
296               </tr>
297 | if $form->{selectdepartment};
298
299   $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
300
301   if ($form->{business}) {
302     $business = qq|
303               <tr>
304                 <th align=right>| . $locale->text('Business') . qq|</th>
305                 <td>$form->{business}</td>
306                 <th align=right>| . $locale->text('Trade Discount') . qq|</th>
307                 <td>|
308       . $form->format_amount(\%myconfig, $form->{tradediscount} * 100)
309       . qq| %</td>
310               </tr>
311 |;
312   }
313
314   $form->{fokus} = "invoice.customer";
315
316   # use JavaScript Calendar or not
317   $form->{jsscript} = $jscalendar;
318   $jsscript = "";
319   if ($form->{jsscript}) {
320
321     # with JavaScript Calendar
322     $button1 = qq|
323        <td><input name=invdate id=invdate size=11 title="$myconfig{dateformat}" value=$form->{invdate}></td>
324        <td><input type=button name=invdate id="trigger1" value=|
325       . $locale->text('button') . qq|></td>
326        |;
327     $button2 = qq|
328        <td width="13"><input name=duedate id=duedate size=11 title="$myconfig{dateformat}" value=$form->{duedate}></td>
329        <td width="4"><input type=button name=duedate id="trigger2" value=|
330       . $locale->text('button') . qq|></td></td>
331      |;
332
333     #write Trigger
334     $jsscript =
335       Form->write_trigger(\%myconfig, "2", "invdate", "BL", "trigger1",
336                           "duedate", "BL", "trigger2");
337   } else {
338
339     # without JavaScript Calendar
340     $button1 =
341       qq|<td><input name=invdate size=11 title="$myconfig{dateformat}" value=$form->{invdate}></td>|;
342     $button2 =
343       qq|<td width="13"><input name=duedate size=11 title="$myconfig{dateformat}" value=$form->{duedate}></td>|;
344   }
345   if ($form->{resubmit}) {
346     $onload = "document.invoice.submit()";
347   } else {
348     $onload = "fokus()";
349   }
350   $form->header;
351
352   print qq|
353 <body onLoad="$onload">
354
355 <form method=post name="invoice" action=$form->{script}>
356
357
358 <input type=hidden name=id value=$form->{id}>
359 <input type=hidden name=action value=$form->{action}>
360
361 <input type=hidden name=type value=$form->{type}>
362 <input type=hidden name=media value=$form->{media}>
363 <input type=hidden name=format value=$form->{format}>
364
365 <input type=hidden name=queued value="$form->{queued}">
366 <input type=hidden name=printed value="$form->{printed}">
367 <input type=hidden name=emailed value="$form->{emailed}">
368
369 <input type=hidden name=title value="$form->{title}">
370 <input type=hidden name=vc value=$form->{vc}>
371
372 <input type=hidden name=discount value=$form->{discount}>
373 <input type=hidden name=creditlimit value=$form->{creditlimit}>
374 <input type=hidden name=creditremaining value=$form->{creditremaining}>
375
376 <input type=hidden name=tradediscount value=$form->{tradediscount}>
377 <input type=hidden name=business value=$form->{business}>
378
379 <input type=hidden name=closedto value=$form->{closedto}>
380 <input type=hidden name=locked value=$form->{locked}>
381
382 <input type=hidden name=shipped value=$form->{shipped}>
383 <input type=hidden name=lizenzen value=$lizenzen>
384
385 <table width=100%>
386   <tr class=listtop>
387     <th class=listtop>$form->{title}</th>
388   </tr>
389   <tr height="5"></tr>
390   <tr>
391     <td>
392       <table width=100%>
393         <tr valign=top>
394           <td>
395             <table>
396               <tr>
397                 <th align=right nowrap>| . $locale->text('Customer') . qq|</th>
398                 <td colspan=3>$customer</td>
399     <input type=hidden name=customer_klass value=$form->{customer_klass}>
400                 <input type=hidden name=customer_id value=$form->{customer_id}>
401                 <input type=hidden name=oldcustomer value="$form->{oldcustomer}">
402                 <th align=richt nowrap>|
403     . $locale->text('Contact Person') . qq|</th>
404                 <td colspan=3>$contact</td>
405               </tr>
406               <tr>
407                 <td></td>
408                 <td colspan=3>
409                   <table>
410                     <tr>
411                       <th nowrap>| . $locale->text('Credit Limit') . qq|</th>
412                       <td>$form->{creditlimit}</td>
413                       <td width=20%></td>
414                       <th nowrap>| . $locale->text('Remaining') . qq|</th>
415                       <td class="plus$n">$form->{creditremaining}</td>
416                     </tr>
417                   </table>
418                 </td>
419               </tr>
420               $business
421               <tr>
422                 <th align=right nowrap>| . $locale->text('Record in') . qq|</th>
423                 <td colspan=3><select name=AR>$form->{selectAR}</select></td>
424                 <input type=hidden name=selectAR value="$form->{selectAR}">
425               </tr>
426               $department
427               <tr>
428                 <th align=right nowrap>| . $locale->text('Currency') . qq|</th>
429                 <td><select name=currency>$form->{selectcurrency}</select></td>
430                 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
431                 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
432                 <input type=hidden name=fxgain_accno value=$form->{fxgain_accno}>
433                 <input type=hidden name=fxloss_accno value=$form->{fxloss_accno}>
434                 $exchangerate
435               </tr>
436               <tr>
437                 <th align=right nowrap>| . $locale->text('Shipping Point') . qq|</th>
438                 <td colspan=3><input name=shippingpoint size=35 value="$form->{shippingpoint}"></td>
439               </tr>
440               <tr>
441                 <th align=right nowrap>| . $locale->text('Ship via') . qq|</th>
442                 <td colspan=3><input name=shipvia size=35 value="$form->{shipvia}"></td>
443               </tr>
444             </table>
445           </td>
446           <td align=right>
447             <table>
448               <tr>
449                 <th align=right nowrap>| . $locale->text('Salesperson') . qq|</th>
450                 <td colspan=2><select name=employee>$form->{selectemployee}</select></td>
451                 <input type=hidden name=selectemployee value="$form->{selectemployee}">
452                 <td></td>
453               </tr>
454               <tr>
455                 <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
456                 <td><input name=invnumber size=11 value="$form->{invnumber}"></td>
457               </tr>
458               <tr>
459                 <th align=right>| . $locale->text('Invoice Date') . qq|</th>
460                 $button1
461               </tr>
462               <tr>
463                 <th align=right>| . $locale->text('Due Date') . qq|</th>
464                 $button2
465               </tr>
466               <tr>
467                 <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
468                 <td><input name=ordnumber size=11 value="$form->{ordnumber}"></td>
469               </tr>
470               <tr>
471                 <th align=right nowrap>| . $locale->text('Quotation Number') . qq|</th>
472                 <td><input name=quonumber size=11 value="$form->{quonumber}"></td>
473               </tr>
474               <tr>
475                 <th align=right nowrap>| . $locale->text('Customer Order Number') . qq|</th>
476                 <td><input name=cusordnumber size=11 value="$form->{cusordnumber}"></td>
477               </tr>
478             </table>
479           </td>
480         </tr>
481       </table>
482     </td>
483   </tr>
484   <tr>
485     <td>
486     </td>
487   </tr>
488
489 $jsscript
490
491 <!-- shipto are in hidden variables -->
492
493 <input type=hidden name=shiptoname value="$form->{shiptoname}">
494 <input type=hidden name=shiptostreet value="$form->{shiptostreet}">
495 <input type=hidden name=shiptozipcode value="$form->{shiptozipcode}">
496 <input type=hidden name=shiptocity value="$form->{shiptocity}">
497 <input type=hidden name=shiptocountry value="$form->{shiptocountry}">
498 <input type=hidden name=shiptocontact value="$form->{shiptocontact}">
499 <input type=hidden name=shiptophone value="$form->{shiptophone}">
500 <input type=hidden name=shiptofax value="$form->{shiptofax}">
501 <input type=hidden name=shiptoemail value="$form->{shiptoemail}">
502
503 <!-- email variables -->
504 <input type=hidden name=message value="$form->{message}">
505 <input type=hidden name=email value="$form->{email}">
506 <input type=hidden name=subject value="$form->{subject}">
507 <input type=hidden name=cc value="$form->{cc}">
508 <input type=hidden name=bcc value="$form->{bcc}">
509 <input type=hidden name=webdav value=$webdav>
510 <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
511 |;
512
513   foreach $item (split / /, $form->{taxaccounts}) {
514     print qq|
515 <input type=hidden name="${item}_rate" value="$form->{"${item}_rate"}">
516 <input type=hidden name="${item}_description" value="$form->{"${item}_description"}">
517 <input type=hidden name="${item}_taxnumber" value="$form->{"${item}_taxnumber"}">
518 |;
519   }
520   $lxdebug->leave_sub();
521 }
522
523 sub form_footer {
524   $lxdebug->enter_sub();
525 print STDERR "is.pl-form_footer\n";
526   $form->{invtotal} = $form->{invsubtotal};
527
528   if (($rows = $form->numtextrows($form->{notes}, 26, 8)) < 2) {
529     $rows = 2;
530   }
531   if (($introws = $form->numtextrows($form->{intnotes}, 35, 8)) < 2) {
532     $introws = 2;
533   }
534   $rows = ($rows > $introws) ? $rows : $introws;
535   $notes =
536     qq|<textarea name=notes rows=$rows cols=26 wrap=soft>$form->{notes}</textarea>|;
537   $intnotes =
538     qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</textarea>|;
539
540   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
541
542   $taxincluded = "";
543   if ($form->{taxaccounts}) {
544     $taxincluded = qq|
545                 <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}> <b>|
546       . $locale->text('Tax Included') . qq|</b><br><br>|;
547   }
548
549   if (!$form->{taxincluded}) {
550
551     foreach $item (split / /, $form->{taxaccounts}) {
552       if ($form->{"${item}_base"}) {
553         $form->{"${item}_total"} =
554           $form->round_amount(
555                              $form->{"${item}_base"} * $form->{"${item}_rate"},
556                              2);
557         $form->{invtotal} += $form->{"${item}_total"};
558         $form->{"${item}_total"} =
559           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
560
561         $tax .= qq|
562               <tr>
563                 <th align=right>$form->{"${item}_description"}</th>
564                 <td align=right>$form->{"${item}_total"}</td>
565               </tr>
566 |;
567       }
568     }
569
570     $form->{invsubtotal} =
571       $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
572
573     $subtotal = qq|
574               <tr>
575                 <th align=right>| . $locale->text('Subtotal') . qq|</th>
576                 <td align=right>$form->{invsubtotal}</td>
577               </tr>
578 |;
579
580   }
581
582   if ($form->{taxincluded}) {
583     foreach $item (split / /, $form->{taxaccounts}) {
584       if ($form->{"${item}_base"}) {
585         $form->{"${item}_total"} =
586           $form->round_amount(
587                            ($form->{"${item}_base"} * $form->{"${item}_rate"} /
588                               (1 + $form->{"${item}_rate"})
589                            ),
590                            2);
591         $form->{"${item}_netto"} =
592           $form->round_amount(
593                           ($form->{"${item}_base"} - $form->{"${item}_total"}),
594                           2);
595         $form->{"${item}_total"} =
596           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
597         $form->{"${item}_netto"} =
598           $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
599
600         $tax .= qq|
601               <tr>
602                 <th align=right>Enthaltene $form->{"${item}_description"}</th>
603                 <td align=right>$form->{"${item}_total"}</td>
604               </tr>
605               <tr>
606                 <th align=right>Nettobetrag</th>
607                 <td align=right>$form->{"${item}_netto"}</td>
608               </tr>
609 |;
610       }
611     }
612
613   }
614
615   $form->{oldinvtotal} = $form->{invtotal};
616   $form->{invtotal}    =
617     $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
618
619   print qq|
620   <tr>
621     <td>
622       <table width=100%>
623         <tr valign=bottom>
624           <td>
625             <table>
626               <tr>
627                 <th align=left>| . $locale->text('Notes') . qq|</th>
628                 <th align=left>| . $locale->text('Internal Notes') . qq|</th>
629               </tr>
630               <tr valign=top>
631                 <td>$notes</td>
632                 <td>$intnotes</td>
633               </tr>
634             </table>
635           </td>
636           <td align=right width=100%>
637             $taxincluded
638             <table width=100%>
639               $subtotal
640               $tax
641               <tr>
642                 <th align=right>| . $locale->text('Total') . qq|</th>
643                 <td align=right>$form->{invtotal}</td>
644               </tr>
645             </table>
646           </td>
647         </tr>
648       </table>
649     </td>
650   </tr>
651 |;
652   if ($webdav) {
653     $webdav_list = qq|
654   <tr>
655     <td><hr size=3 noshade></td>
656   </tr>
657   <tr>
658     <th class=listtop align=left>Dokumente im Webdav-Repository</th>
659   </tr>
660     <table width=100%>
661       <td align=left width=30%><b>Dateiname</b></td>
662       <td align=left width=70%><b>Webdavlink</b></td>
663 |;
664     foreach $file (keys %{ $form->{WEBDAV} }) {
665       $webdav_list .= qq|
666       <tr>
667         <td align=left>$file</td>
668         <td align=left><a href="$form->{WEBDAV}{$file}">$form->{WEBDAV}{$file}</a></td>
669       </tr>
670 |;
671     }
672     $webdav_list .= qq|
673     </table>
674   </tr>
675 |;
676
677     print $webdav_list;
678   }
679   print qq|
680   <tr>
681     <td>
682       <table width=100%>
683         <tr class=listheading>
684           <th colspan=6 class=listheading>|
685     . $locale->text('Incoming Payments') . qq|</th>
686         </tr>
687 |;
688
689   if ($form->{currency} eq $form->{defaultcurrency}) {
690     @column_index = qw(datepaid source memo paid AR_paid);
691   } else {
692     @column_index = qw(datepaid source memo paid exchangerate AR_paid);
693   }
694
695   $column_data{datepaid}     = "<th>" . $locale->text('Date') . "</th>";
696   $column_data{paid}         = "<th>" . $locale->text('Amount') . "</th>";
697   $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
698   $column_data{AR_paid}      = "<th>" . $locale->text('Account') . "</th>";
699   $column_data{source}       = "<th>" . $locale->text('Source') . "</th>";
700   $column_data{memo}         = "<th>" . $locale->text('Memo') . "</th>";
701
702   print "
703         <tr>
704 ";
705   map { print "$column_data{$_}\n" } @column_index;
706   print "
707         </tr>
708 ";
709
710   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
711   for $i (1 .. $form->{paidaccounts}) {
712
713     print "
714         <tr>\n";
715
716     $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
717     $form->{"selectAR_paid_$i"} =~
718       s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
719
720     # format amounts
721     $totalpaid += $form->{"paid_$i"};
722     $form->{"paid_$i"} =
723       $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
724     $form->{"exchangerate_$i"} =
725       $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
726
727     $exchangerate = qq|&nbsp;|;
728     if ($form->{currency} ne $form->{defaultcurrency}) {
729       if ($form->{"forex_$i"}) {
730         $exchangerate =
731           qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
732       } else {
733         $exchangerate =
734           qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
735       }
736     }
737
738     $exchangerate .= qq|
739 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
740 |;
741
742     $column_data{"paid_$i"} =
743       qq|<td align=center><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
744     $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
745     $column_data{"AR_paid_$i"}      =
746       qq|<td align=center><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
747     $column_data{"datepaid_$i"} =
748       qq|<td align=center><input name="datepaid_$i"  size=11 title="$myconfig{dateformat}" value=$form->{"datepaid_$i"}></td>|;
749     $column_data{"source_$i"} =
750       qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
751     $column_data{"memo_$i"} =
752       qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
753
754     map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
755     print "
756         </tr>\n";
757   }
758
759   print qq|
760 <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
761 <input type=hidden name=selectAR_paid value="$form->{selectAR_paid}">
762 <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
763 <input type=hidden name=oldtotalpaid value=$totalpaid>
764 <input type=hidden name=print_and_post value=$form->{print_and_post}>
765 <input type=hidden name=second_run value=$form->{second_run}>
766     </table>
767     </td>
768   </tr>
769   <tr>
770     <td><hr size=3 noshade></td>
771   </tr>
772   <tr>
773     <td>
774 |;
775
776   &print_options;
777
778   print qq|
779     </td>
780   </tr>
781 </table>
782 |;
783
784   $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
785   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
786
787   if ($form->{id}) {
788     print qq|
789     <input class=submit type=submit name=action value="|
790       . $locale->text('Update') . qq|">
791     <input class=submit type=submit name=action value="|
792       . $locale->text('Ship to') . qq|">
793     <input class=submit type=submit name=action value="|
794       . $locale->text('Print') . qq|">
795     <input class=submit type=submit name=action value="|
796       . $locale->text('E-mail') . qq|">
797 |;
798
799     if (!$form->{revtrans}) {
800       if (!$form->{locked}) {
801         print qq|
802         <input class=submit type=submit name=action value="|
803           . $locale->text('Post') . qq|">
804         <input class=submit type=submit name=action value="|
805           . $locale->text('Delete') . qq|">
806 |;
807       }
808     }
809
810     if ($invdate > $closedto) {
811       print qq|
812       <input class=submit type=submit name=action value="|
813         . $locale->text('Post as new') . qq|">
814       <input class=submit type=submit name=action value="|
815         . $locale->text('Order') . qq|">
816 |;
817     }
818
819   } else {
820     if ($invdate > $closedto) {
821       print qq|<input class=submit type=submit name=action value="|
822         . $locale->text('Update') . qq|">
823       <input class=submit type=submit name=action value="|
824         . $locale->text('Ship to') . qq|">
825       <input class=submit type=submit name=action value="|
826         . $locale->text('Preview') . qq|">
827       <input class=submit type=submit name=action value="|
828         . $locale->text('E-mail') . qq|">
829       <input class=submit type=submit name=action value="|
830         . $locale->text('Print and Post') . qq|">
831       <input class=submit type=submit name=action value="|
832         . $locale->text('Post') . qq|">|;
833     }
834   }
835
836   if ($form->{menubar}) {
837     require "$form->{path}/menu.pl";
838     &menubar;
839   }
840
841   print qq|
842
843 <input type=hidden name=rowcount value=$form->{rowcount}>
844
845 <input name=callback type=hidden value="$form->{callback}">
846
847 <input type=hidden name=path value=$form->{path}>
848 <input type=hidden name=login value=$form->{login}>
849 <input type=hidden name=password value=$form->{password}>
850
851 </form>
852
853 </body>
854
855  </html>
856 |;
857
858   $lxdebug->leave_sub();
859 }
860
861 sub update {
862   $lxdebug->enter_sub();
863 print STDERR "is.pl-update\n";
864   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
865     qw(exchangerate creditlimit creditremaining);
866
867   &check_name(customer);
868
869   &check_project;
870
871   $form->{exchangerate} = $exchangerate
872     if (
873         $form->{forex} = (
874                        $exchangerate =
875                          $form->check_exchangerate(
876                          \%myconfig, $form->{currency}, $form->{invdate}, 'buy'
877                          )));
878
879   for $i (1 .. $form->{paidaccounts}) {
880     if ($form->{"paid_$i"}) {
881       map {
882         $form->{"${_}_$i"} =
883           $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
884       } qw(paid exchangerate);
885
886       $form->{"exchangerate_$i"} = $exchangerate
887         if (
888             $form->{"forex_$i"} = (
889                  $exchangerate =
890                    $form->check_exchangerate(
891                    \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy'
892                    )));
893     }
894   }
895
896   $i            = $form->{rowcount};
897   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
898
899   # if last row empty, check the form otherwise retrieve new item
900   if (   ($form->{"partnumber_$i"} eq "")
901       && ($form->{"description_$i"} eq "")
902       && ($form->{"partsgroup_$i"}  eq "")) {
903
904     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
905     &check_form;
906
907       } else {
908
909     IS->retrieve_item(\%myconfig, \%$form);
910
911     $rows = scalar @{ $form->{item_list} };
912
913     $form->{"discount_$i"} =
914       $form->format_amount(\%myconfig, $form->{discount} * 100);
915
916     if ($rows) {
917       $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
918
919       if ($rows > 1) {
920
921         &select_item;
922         exit;
923
924       } else {
925
926         $sellprice = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
927 #print STDERR " SELLPRICE-111- $sellprice\n";
928         map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g }
929           qw(partnumber description unit);
930         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
931           keys %{ $form->{item_list}[0] };
932
933         $form->{"discount_$i"} = $form->{discount} * 100;
934
935         $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
936         ($dec) = ($s =~ /\.(\d+)/);
937         $dec           = length $dec;
938         $decimalplaces = ($dec > 2) ? $dec : 2;
939
940         if ($sellprice) {
941           $form->{"sellprice_$i"} = $sellprice;
942         } else {
943
944           # if there is an exchange rate adjust sellprice
945           $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
946           $form->{"sellprice_$i"} /= $exchangerate;
947         }
948
949         $form->{"listprice_$i"} /= $exchangerate;
950
951         $amount =
952           $form->{"sellprice_$i"} * $form->{"qty_$i"} *
953           (1 - $form->{"discount_$i"} / 100);
954         map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
955         map { $form->{"${_}_base"} += $amount }
956           (split / /, $form->{"taxaccounts_$i"});
957         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
958           split / /, $form->{"taxaccounts_$i"}
959           if !$form->{taxincluded};
960
961         $form->{creditremaining} -= $amount;
962
963         map {
964           $form->{"${_}_$i"} =
965             $form->format_amount(\%myconfig, $form->{"${_}_$i"},
966                                  $decimalplaces)
967         } qw(sellprice listprice);
968
969         $form->{"qty_$i"} =
970           $form->format_amount(\%myconfig, $form->{"qty_$i"});
971
972         if ($lizenzen) {
973           if ($form->{"inventory_accno_$i"} ne "") {
974             $form->{"lizenzen_$i"} = qq|<option></option>|;
975             foreach $item (@{ $form->{LIZENZEN}{ $form->{"id_$i"} } }) {
976               $form->{"lizenzen_$i"} .=
977                 qq|<option value="$item->{"id"}">$item->{"licensenumber"}</option>|;
978             }
979             $form->{"lizenzen_$i"} .=
980               qq|<option value=-1>Neue Lizenz</option>|;
981           }
982         }
983
984         # get pricegroups for parts
985         IS->get_pricegroups_for_parts(\%myconfig, \%$form, "new");
986
987         # build up html code for prices_$i
988         set_pricegroup();
989       }
990
991       &display_form;
992
993     } else {
994
995       # ok, so this is a new part
996       # ask if it is a part or service item
997
998       if (   $form->{"partsgroup_$i"}
999           && ($form->{"partsnumber_$i"} eq "")
1000           && ($form->{"description_$i"} eq "")) {
1001         $form->{rowcount}--;
1002         $form->{"discount_$i"} = "";
1003         &display_form;
1004           } else {
1005
1006         $form->{"id_$i"}   = 0;
1007         $form->{"unit_$i"} = $locale->text('ea');
1008
1009         &new_item;
1010
1011       }
1012     }
1013   }
1014   $lxdebug->leave_sub();
1015 }
1016
1017 sub post {
1018   $lxdebug->enter_sub();
1019 print STDERR "is.pl-post\n";
1020   $form->isblank("invdate",  $locale->text('Invoice Date missing!'));
1021   $form->isblank("customer", $locale->text('Customer missing!'));
1022
1023   # if oldcustomer ne customer redo form
1024   if (&check_name(customer)) {
1025     &update;
1026     exit;
1027   }
1028
1029   &validate_items;
1030
1031   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1032   $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
1033
1034   $form->error($locale->text('Cannot post invoice for a closed period!'))
1035     if ($invdate <= $closedto);
1036
1037   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1038     if ($form->{currency} ne $form->{defaultcurrency});
1039
1040   for $i (1 .. $form->{paidaccounts}) {
1041     if ($form->{"paid_$i"}) {
1042       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1043
1044       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1045
1046       $form->error($locale->text('Cannot post payment for a closed period!'))
1047         if ($datepaid <= $closedto);
1048
1049       if ($form->{currency} ne $form->{defaultcurrency}) {
1050         $form->{"exchangerate_$i"} = $form->{exchangerate}
1051           if ($invdate == $datepaid);
1052         $form->isblank("exchangerate_$i",
1053                        $locale->text('Exchangerate for payment missing!'));
1054       }
1055     }
1056   }
1057
1058   ($form->{AR})      = split /--/, $form->{AR};
1059   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
1060
1061   $form->{label} = $locale->text('Invoice');
1062
1063   $form->{id} = 0 if $form->{postasnew};
1064
1065   $form->{invnumber} = $form->update_defaults(\%myconfig, "invnumber")
1066     unless $form->{invnumber};
1067   if ($print_post) {
1068     if (!(IS->post_invoice(\%myconfig, \%$form))) {
1069       $form->error($locale->text('Cannot post invoice!'));
1070     }
1071   } else {
1072     $form->redirect(
1073             $form->{label} . " $form->{invnumber} " . $locale->text('posted!'))
1074       if (IS->post_invoice(\%myconfig, \%$form));
1075     $form->error($locale->text('Cannot post invoice!'));
1076   }
1077
1078   $lxdebug->leave_sub();
1079 }
1080
1081 sub print_and_post {
1082   $lxdebug->enter_sub();
1083
1084   $old_form               = new Form;
1085   $print_post             = 1;
1086   $form->{print_and_post} = 1;
1087   &post();
1088   &display_form();
1089   $lxdebug->leave_sub();
1090
1091 }
1092
1093 sub preview {
1094   $lxdebug->enter_sub();
1095
1096   $form->{preview} = 1;
1097   $old_form = new Form;
1098   for (keys %$form) { $old_form->{$_} = $form->{$_} }
1099   $old_form->{rowcount}++;
1100
1101   &print_form($old_form);
1102   $lxdebug->leave_sub();
1103
1104 }
1105
1106 sub delete {
1107   $lxdebug->enter_sub();
1108
1109   $form->header;
1110
1111   print qq|
1112 <body>
1113
1114 <form method=post action=$form->{script}>
1115 |;
1116
1117   # delete action variable
1118   map { delete $form->{$_} } qw(action header);
1119
1120   foreach $key (keys %$form) {
1121     $form->{$key} =~ s/\"/&quot;/g;
1122     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1123   }
1124
1125   print qq|
1126 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1127
1128 <h4>|
1129     . $locale->text('Are you sure you want to delete Invoice Number')
1130     . qq| $form->{invnumber}
1131 </h4>
1132
1133 <p>
1134 <input name=action class=submit type=submit value="|
1135     . $locale->text('Yes') . qq|">
1136 </form>
1137 |;
1138
1139   $lxdebug->leave_sub();
1140 }
1141
1142 sub yes {
1143   $lxdebug->enter_sub();
1144
1145   $form->redirect($locale->text('Invoice deleted!'))
1146     if (IS->delete_invoice(\%myconfig, \%$form, $spool));
1147   $form->error($locale->text('Cannot delete invoice!'));
1148
1149   $lxdebug->leave_sub();
1150 }