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