1b99f5fb53b8ff45b29141d50d9493624d6924a5
[kivitendo-erp.git] / bin / mozilla / ir.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 received module
31 #
32 #======================================================================
33
34 use SL::IR;
35 use SL::PE;
36
37 require "$form->{path}/io.pl";
38 require "$form->{path}/arap.pl";
39
40 1;
41
42 # end of main
43
44 sub add {
45   $lxdebug->enter_sub();
46
47   $form->{title} = $locale->text('Add Vendor Invoice');
48
49   &invoice_links;
50   &prepare_invoice;
51   &display_form;
52
53   $lxdebug->leave_sub();
54 }
55
56 sub edit {
57   $lxdebug->enter_sub();
58
59   $form->{title} = $locale->text('Edit Vendor Invoice');
60
61   &invoice_links;
62   &prepare_invoice;
63   &display_form;
64
65   $lxdebug->leave_sub();
66 }
67
68 sub invoice_links {
69   $lxdebug->enter_sub();
70
71   # create links
72   $form->{webdav} = $webdav;
73
74   # set jscalendar
75   $form->{jscalendar} = $jscalendar;
76
77   $form->create_links("AP", \%myconfig, "vendor");
78
79   #quote all_vendor Bug 133
80   foreach $ref (@{ $form->{all_vendor} }) {
81     $ref->{name} = $form->quote($ref->{name});
82   }
83
84   if ($form->{all_vendor}) {
85     unless ($form->{vendor_id}) {
86       $form->{vendor_id} = $form->{all_vendor}->[0]->{id};
87     }
88   }
89   if ($form->{payment_id}) {
90     $payment_id = $form->{payment_id};
91   }
92   if ($form->{language_id}) {
93     $language_id = $form->{language_id};
94   }
95   if ($form->{taxzone_id}) {
96     $taxzone_id = $form->{taxzone_id};
97   }
98
99   $cp_id = $form->{cp_id};
100   IR->get_vendor(\%myconfig, \%$form);
101   IR->retrieve_invoice(\%myconfig, \%$form);
102   $form->{cp_id} = $cp_id;
103  
104   if ($payment_id) {
105     $form->{payment_id} = $payment_id;
106   }
107   if ($language_id) {
108     $form->{language_id} = $language_id;
109   }
110   if ($taxzone_id) {
111     $form->{taxzone_id} = $taxzone_id;
112   }
113
114   # currencies
115   @curr = split(/:/, $form->{currencies});
116   chomp $curr[0];
117   $form->{defaultcurrency} = $curr[0];
118
119   map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
120
121   $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
122
123   # vendors
124   if (@{ $form->{all_vendor} }) {
125     $form->{vendor} = "$form->{vendor}--$form->{vendor_id}";
126     map { $form->{selectvendor} .= "<option>$_->{name}--$_->{id}\n" }
127       (@{ $form->{all_vendor} });
128   }
129
130   # departments
131   if ($form->{all_departments}) {
132     $form->{selectdepartment} = "<option>\n";
133     $form->{department}       = "$form->{department}--$form->{department_id}";
134
135     map {
136       $form->{selectdepartment} .=
137         "<option>$_->{description}--$_->{id}\n"
138     } (@{ $form->{all_departments} });
139   }
140
141   # forex
142   $form->{forex} = $form->{exchangerate};
143   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
144
145   foreach $key (keys %{ $form->{AP_links} }) {
146
147     foreach $ref (@{ $form->{AP_links}{$key} }) {
148       $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}\n";
149     }
150
151     if ($key eq "AP_paid") {
152       for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
153         $form->{"AP_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};
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->{AP} = $form->{AP_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
188   if ($form->{id}) {
189
190     map { $form->{$_} =~ s/\"/&quot;/g } qw(invnumber ordnumber quonumber);
191
192     my $i = 0;
193     foreach $ref (@{ $form->{invoice_details} }) {
194       $i++;
195       map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
196
197       ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
198       $dec           = length $dec;
199       $decimalplaces = ($dec > 2) ? $dec : 2;
200
201       $form->{"sellprice_$i"} =
202         $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
203                              $decimalplaces);
204
205       (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
206       $dec_qty = length $dec_qty;
207
208       $form->{"qty_$i"} =
209         $form->format_amount(\%myconfig, ($form->{"qty_$i"} * -1), $dec_qty);
210
211       $form->{rowcount} = $i;
212     }
213   }
214
215   $lxdebug->leave_sub();
216 }
217
218 sub form_header {
219   $lxdebug->enter_sub();
220
221   # set option selected
222   foreach $item (qw(AP vendor currency department)) {
223     $form->{"select$item"} =~ s/ selected//;
224     $form->{"select$item"} =~
225       s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
226   }
227
228   $form->{radier} =
229     ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
230
231   #quote selectvendor Bug 133
232   $form->{"selectvendor"} = $form->quote($form->{"selectvendor"});
233
234   $form->{exchangerate} =
235     $form->format_amount(\%myconfig, $form->{exchangerate});
236
237   $form->{creditlimit} =
238     $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
239   $form->{creditremaining} =
240     $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
241
242   $exchangerate = "";
243   if ($form->{currency} ne $form->{defaultcurrency}) {
244     if ($form->{forex}) {
245       $exchangerate .= qq|
246                 <th align=right nowrap>|
247         . $locale->text('Exchangerate') . qq|</th>
248                 <td>$form->{exchangerate}<input type=hidden name=exchangerate value=$form->{exchangerate}></td>
249 |;
250     } else {
251       $exchangerate .= qq|
252                 <th align=right nowrap>|
253         . $locale->text('Exchangerate') . qq|</th>
254                 <td><input name=exchangerate size=10 value=$form->{exchangerate}></td>
255 |;
256     }
257   }
258   $exchangerate .= qq|
259 <input type=hidden name=forex value=$form->{forex}>
260 |;
261
262   $form->get_lists("contacts" => "ALL_CONTACTS");
263
264   my (%labels, @values);
265   foreach my $item (@{ $form->{"ALL_CONTACTS"} }) {
266     push(@values, $item->{"cp_id"});
267     $labels{$item->{"cp_id"}} = $item->{"cp_name"} .
268       ($item->{"cp_abteilung"} ? " ($item->{cp_abteilung})" : "");
269   }
270   my $contact =
271     $cgi->popup_menu('-name' => 'cp_id', '-values' => \@values,
272                      '-labels' => \%labels, '-default' => $form->{"cp_id"});
273
274   if (@{ $form->{TAXZONE} }) {
275     $form->{selecttaxzone} = "";
276     foreach $item (@{ $form->{TAXZONE} }) {
277       if ($item->{id} == $form->{taxzone_id}) {
278         $form->{selecttaxzone} .=
279           "<option value=$item->{id} selected>" . H($item->{description}) .
280           "</option>";
281       } else {
282         $form->{selecttaxzone} .=
283           "<option value=$item->{id}>" . H($item->{description}) . "</option>";
284       }
285
286     }
287   } else {
288     $form->{selecttaxzone} =~ s/ selected//g;
289     if ($form->{taxzone_id} ne "") {
290       $form->{selecttaxzone} =~ s/value=$form->{taxzone_id}/value=$form->{taxzone_id} selected/;
291     }
292   }
293
294   $taxzone = qq|
295               <tr>
296                 <th align=right>| . $locale->text('Steuersatz') . qq|</th>
297                 <td><select name=taxzone_id>$form->{selecttaxzone}</select></td>
298                 <input type=hidden name=selecttaxzone value="$form->{selecttaxzone}">
299               </tr>|;
300
301
302
303   $vendor =
304     ($form->{selectvendor})
305     ? qq|<select name=vendor>$form->{selectvendor}</select>\n<input type=hidden name="selectvendor" value="$form->{selectvendor}">|
306     : qq|<input name=vendor value="$form->{vendor}" size=35>|;
307
308   $department = qq|
309               <tr>
310               <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
311               <td colspan=3><select name=department>$form->{selectdepartment}</select>
312               <input type=hidden name=selectdepartment value="$form->{selectdepartment}">
313               </td>
314             </tr>
315 | if $form->{selectdepartment};
316
317   $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
318
319   # use JavaScript Calendar or not
320   $form->{jsscript} = $form->{jscalendar};
321   $jsscript = "";
322   if ($form->{jsscript}) {
323
324     # with JavaScript Calendar
325     $button1 = qq|
326        <td><input name=invdate id=invdate size=11 title="$myconfig{dateformat}" value=$form->{invdate}></td>
327        <td><input type=button name=invdate id="trigger1" value=|
328       . $locale->text('button') . qq|></td>
329        |;
330     $button2 = qq|
331        <td width="13"><input name=duedate id=duedate size=11 title="$myconfig{dateformat}" value=$form->{duedate}></td>
332        <td width="4"><input type=button name=duedate id="trigger2" value=|
333       . $locale->text('button') . qq|></td></td>
334      |;
335
336     #write Trigger
337     $jsscript =
338       Form->write_trigger(\%myconfig, "2", "invdate", "BL", "trigger1",
339                           "duedate", "BL", "trigger2");
340   } else {
341
342     # without JavaScript Calendar
343     $button1 =
344       qq|<td><input name=invdate size=11 title="$myconfig{dateformat}" value=$form->{invdate}></td>|;
345     $button2 =
346       qq|<td width="13"><input name=duedate size=11 title="$myconfig{dateformat}" value=$form->{duedate}></td>|;
347   }
348
349   $form->{"javascript"} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
350
351   $jsscript .=
352     $form->write_trigger(\%myconfig, 2,
353                          "orddate", "BL", "trigger_orddate",
354                          "quodate", "BL", "trigger_quodate");
355
356   $form->header;
357
358   print qq|
359 <body>
360
361 <form method=post action=$form->{script}>
362
363 <input type=hidden name=id value=$form->{id}>
364 <input type=hidden name=title value="$form->{title}">
365 <input type=hidden name=vc value="vendor">
366 <input type=hidden name=type value=$form->{type}>
367 <input type=hidden name=level value=$form->{level}>
368
369 <input type=hidden name=creditlimit value=$form->{creditlimit}>
370 <input type=hidden name=creditremaining value=$form->{creditremaining}>
371
372 <input type=hidden name=closedto value=$form->{closedto}>
373 <input type=hidden name=locked value=$form->{locked}>
374
375 <input type=hidden name=shipped value=$form->{shipped}>
376 <input type=hidden name=storno value=$form->{storno}>
377 <input type=hidden name=storno_id value=$form->{storno_id}>
378
379 <table width=100%>
380   <tr class=listtop>
381     <th class=listtop>$form->{title}</th>
382   </tr>
383   <tr height="5"></tr>
384   <tr>
385     <td>
386       <table width=100%>
387         <tr valign=top>
388           <td>
389             <table>
390               <tr>
391                 <th align=right nowrap>| . $locale->text('Vendor') . qq|</th>
392                 <td colspan=3>$vendor</td>
393
394                 <th align=richt nowrap>|
395     . $locale->text('Contact Person') . qq|</th>
396                 <td colspan=3>$contact</td>
397
398                 <input type=hidden name=vendor_id value=$form->{vendor_id}>
399                 <input type=hidden name=oldvendor value="$form->{oldvendor}">
400
401               </tr>
402               <tr>
403                 <td></td>
404                 <td colspan=3>
405                   <table>
406                     <tr>
407                       <th nowrap>| . $locale->text('Credit Limit') . qq|</th>
408                       <td>$form->{creditlimit}</td>
409                       <td width=20%></td>
410                       <th nowrap>| . $locale->text('Remaining') . qq|</th>
411                       <td class="plus$n">$form->{creditremaining}</td>
412                     </tr>
413                   </table>
414                 </td>
415               <tr>
416                 <th align=right>| . $locale->text('Record in') . qq|</th>
417                 <td colspan=3><select name=AP>$form->{selectAP}</select></td>
418                 <input type=hidden name=selectAP value="$form->{selectAP}">
419               </tr>
420               $taxzone
421               $department
422               <tr>
423                 <th align=right nowrap>| . $locale->text('Currency') . qq|</th>
424                 <td><select name=currency>$form->{selectcurrency}</select></td>
425                 $exchangerate
426               </tr>
427             </table>
428           </td>
429           <td align=right>
430             <table>
431               <tr>
432                 <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
433                 <td><input name=invnumber size=11 value="$form->{invnumber}"></td>
434               </tr>
435               <tr>
436                 <th align=right nowrap>| . $locale->text('Invoice Date') . qq|</th>
437                 $button1
438               </tr>
439               <tr>
440                 <th align=right nowrap>| . $locale->text('Due Date') . qq|</th>
441                 $button2
442               </tr>
443               <tr>
444                 <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
445                 <td><input name=ordnumber size=11 value="$form->{ordnumber}"></td>
446 <input type=hidden name=quonumber value="$form->{quonumber}">
447               </tr>
448         <tr>
449           <th align="right" nowrap>| . $locale->text('Order Date') . qq|</th>
450           <td><input name="orddate" id="orddate" size="11" title="$myconfig{dateformat}" value="| . Q($form->{orddate}) . qq|"></td>
451           <td><input type="button" name="b_orddate" id="trigger_orddate" value="?"></td>
452         </tr>
453         <tr>
454           <th align="right" nowrap>| . $locale->text('Quotation Date') . qq|</th>
455           <td><input name="quodate" id="quodate" size="11" title="$myconfig{dateformat}" value="| . Q($form->{quodate}) . qq|"></td>
456           <td><input type="button" name="b_quodate" id="trigger_quodate" value="?"></td>
457         </tr>
458               <tr>
459           <th align="right" nowrap>| . $locale->text('Project Number') . qq|</th>
460           <td>
461             <input name="globalprojectnumber" size="11" value="| . Q($form->{globalprojectnumber}) . qq|">
462             <input type="hidden" name="oldglobalprojectnumber" value="| . Q($form->{globalprojectnumber}) . qq|">
463             <input type="hidden" name="globalproject_id" value="| . Q($form->{globalproject_id}) . qq|">
464           </td:>
465               </tr>
466      </table>
467           </td>
468         </tr>
469       </table>
470     </td>
471   </tr>
472
473 $jsscript
474
475 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
476 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
477 <input type=hidden name=fxgain_accno value=$form->{fxgain_accno}>
478 <input type=hidden name=fxloss_accno value=$form->{fxloss_accno}>
479 <input type=hidden name=webdav value=$webdav>
480
481 <input type=hidden name=taxpart value="$form->{taxpart}">
482 <input type=hidden name=taxservice value="$form->{taxservice}">
483
484 <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
485 |;
486
487   foreach $item (split / /, $form->{taxaccounts}) {
488     print qq|
489 <input type=hidden name="${item}_rate" value=$form->{"${item}_rate"}>
490 <input type=hidden name="${item}_description" value="$form->{"${item}_description"}">
491 |;
492   }
493
494   $lxdebug->leave_sub();
495 }
496
497 sub form_footer {
498   $lxdebug->enter_sub();
499
500   $form->{invtotal} = $form->{invsubtotal};
501
502   if (($rows = $form->numtextrows($form->{notes}, 25, 8)) < 2) {
503     $rows = 2;
504   }
505   if (($introws = $form->numtextrows($form->{intnotes}, 35, 8)) < 2) {
506     $introws = 2;
507   }
508   $rows = ($rows > $introws) ? $rows : $introws;
509   $notes =
510     qq|<textarea name=notes rows=$rows cols=25 wrap=soft>$form->{notes}</textarea>|;
511   $intnotes =
512     qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</textarea>|;
513
514   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
515
516   $taxincluded = "";
517   if ($form->{taxaccounts}) {
518     $taxincluded = qq|
519                 <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}> <b>|
520       . $locale->text('Tax Included') . qq|</b>
521 |;
522   }
523
524   if (!$form->{taxincluded}) {
525
526     foreach $item (split / /, $form->{taxaccounts}) {
527       if ($form->{"${item}_base"}) {
528         $form->{invtotal} += $form->{"${item}_total"} =
529           $form->round_amount(
530                              $form->{"${item}_base"} * $form->{"${item}_rate"},
531                              2);
532         $form->{"${item}_total"} =
533           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
534
535         $tax .= qq|
536                 <tr>
537                   <th align=right>$form->{"${item}_description"}</th>
538                   <td align=right>$form->{"${item}_total"}</td>
539                 </tr>
540 |;
541       }
542     }
543
544     $form->{invsubtotal} =
545       $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
546
547     $subtotal = qq|
548               <tr>
549                 <th align=right>| . $locale->text('Subtotal') . qq|</th>
550                 <td align=right>$form->{invsubtotal}</td>
551               </tr>
552 |;
553
554   }
555
556   if ($form->{taxincluded}) {
557     foreach $item (split / /, $form->{taxaccounts}) {
558       if ($form->{"${item}_base"}) {
559         $form->{"${item}_total"} =
560           $form->round_amount(
561                            ($form->{"${item}_base"} * $form->{"${item}_rate"} /
562                               (1 + $form->{"${item}_rate"})
563                            ),
564                            2);
565         $form->{"${item}_base"} =
566           $form->round_amount($form->{"${item}_base"}, 2);
567         $form->{"${item}_netto"} =
568           $form->round_amount(
569                           ($form->{"${item}_base"} - $form->{"${item}_total"}),
570                           2);
571         $form->{"${item}_netto"} =
572           $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
573         $form->{"${item}_total"} =
574           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
575
576         $tax .= qq|
577               <tr>
578                 <th align=right>Enthaltene $form->{"${item}_description"}</th>
579                 <td align=right>$form->{"${item}_total"}</td>
580               </tr>
581               <tr>
582                 <th align=right>Nettobetrag</th>
583                 <td align=right>$form->{"${item}_netto"}</td>
584               </tr>
585 |;
586       }
587     }
588
589   }
590
591   $form->{oldinvtotal} = $form->{invtotal};
592   $form->{invtotal}    =
593     $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
594
595   print qq|
596   <tr>
597     <td colspan=$colspan>
598       <table cellspacing="0">
599         <tr valign=bottom>
600           <td>
601             <table>
602               <tr>
603                 <th align=left>| . $locale->text('Notes') . qq|</th>
604                 <th align=left>| . $locale->text('Internal Notes') . qq|</th>
605               </tr>
606               <tr valign=top>
607                 <td>$notes</td>
608                 <td>$intnotes</td>
609               </tr>
610             </table>
611           </td>
612           <td colspan=2 align=right width=100%>
613             $taxincluded
614             <br>
615             <table width=100%>
616               $subtotal
617               $tax
618               <tr>0
619                 <th align=right>| . $locale->text('Total') . qq|</th>
620                 <td align=right>$form->{invtotal}</td>
621               </tr>
622             </table>
623           </td>
624         </tr>
625       </table>
626     </td>
627   </tr>
628 |;
629   if ($webdav) {
630     $webdav_list = qq|
631   <tr>
632     <td><hr size=3 noshade></td>
633   </tr>
634   <tr>
635     <th class=listtop align=left>Dokumente im Webdav-Repository</th>
636   </tr>
637     <table width=100%>
638       <td align=left width=30%><b>Dateiname</b></td>
639       <td align=left width=70%><b>Webdavlink</b></td>
640 |;
641     foreach $file (keys %{ $form->{WEBDAV} }) {
642       $webdav_list .= qq|
643       <tr>
644         <td align=left>$file</td>
645         <td align=left><a href="$form->{WEBDAV}{$file}">$form->{WEBDAV}{$file}</a></td>
646       </tr>
647 |;
648     }
649     $webdav_list .= qq|
650     </table>
651   </tr>
652 |;
653
654     print $webdav_list;
655   }
656   print qq|
657 <input type=hidden name=jscalendar value=$form->{jscalendar}>
658 |;
659   print qq|
660   <tr>
661     <td colspan=$colspan>
662       <table width=100%>
663         <tr>
664           <th colspan=6 class=listheading>| . $locale->text('Payments') . qq|</th>
665         </tr>
666 |;
667
668   if ($form->{currency} eq $form->{defaultcurrency}) {
669     @column_index = qw(datepaid source memo paid AP_paid);
670   } else {
671     @column_index = qw(datepaid source memo paid exchangerate AP_paid);
672   }
673
674   $column_data{datepaid}     = "<th>" . $locale->text('Date') . "</th>";
675   $column_data{paid}         = "<th>" . $locale->text('Amount') . "</th>";
676   $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
677   $column_data{AP_paid}      = "<th>" . $locale->text('Account') . "</th>";
678   $column_data{source}       = "<th>" . $locale->text('Source') . "</th>";
679   $column_data{memo}         = "<th>" . $locale->text('Memo') . "</th>";
680
681   print qq|
682         <tr>
683 |;
684   map { print "$column_data{$_}\n" } @column_index;
685   print qq|
686         </tr>
687 |;
688
689   my @triggers = ();
690   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
691   for $i (1 .. $form->{paidaccounts}) {
692
693     print qq|
694         <tr>
695 |;
696
697     $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
698     $form->{"selectAP_paid_$i"} =~
699       s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
700
701     # format amounts
702     if ($form->{"paid_$i"}) {
703       $form->{"paid_$i"} =
704         $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
705     }
706     $form->{"exchangerate_$i"} =
707       $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
708
709     $exchangerate = qq|&nbsp;|;
710     if ($form->{currency} ne $form->{defaultcurrency}) {
711       if ($form->{"forex_$i"}) {
712         $exchangerate =
713           qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
714       } else {
715         $exchangerate =
716           qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
717       }
718     }
719     $exchangerate .= qq|
720 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
721 |;
722
723     $column_data{"paid_$i"} =
724       qq|<td align=center><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
725     $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
726     $column_data{"AP_paid_$i"}      =
727       qq|<td align=center><select name="AP_paid_$i">$form->{"selectAP_paid_$i"}</select></td>|;
728     $column_data{"datepaid_$i"} =
729       qq|<td align=center><input name="datepaid_$i" id="datepaid_$i" size=11 title="$myconfig{dateformat}" value=$form->{"datepaid_$i"}>
730          <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
731     $column_data{"source_$i"} =
732       qq|<td align=center><input name="source_$i" size=11 value=$form->{"source_$i"}></td>|;
733     $column_data{"memo_$i"} =
734       qq|<td align=center><input name="memo_$i" size=11 value=$form->{"memo_$i"}></td>|;
735
736     map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
737
738     print qq|
739         </tr>
740 |;
741     push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
742   }
743
744   print qq|
745             <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
746             <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
747             <input type=hidden name=selectAP_paid value="$form->{selectAP_paid}">
748       </table>
749     </td>
750   </tr>
751   <tr>
752     <td><hr size=3 noshade></td>
753   </tr>
754 </table>
755 <br>
756 |;
757
758   $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
759   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
760
761   if ($form->{id}) {
762     print qq|<input class=submit type=submit name=action value="|
763       . $locale->text('Post Payment') . qq|">
764 |;
765     print qq|<input class=submit type=submit name=action value="|
766       . $locale->text('Storno') . qq|">
767 | unless ($form->{storno});
768     if ($form->{radier}) {
769     print qq|
770     <input class=submit type=submit name=action value="|
771       . $locale->text('Delete') . qq|">
772 |;
773   }
774     print qq|<input class=submit type=submit name=action value="|
775       . $locale->text('Use As Template') . qq|">
776 |;
777
778   }
779
780   print qq|<input class=submit type=submit name=action id=update_button value="|
781     . $locale->text('Update') . qq|">|;
782
783   if (!$form->{id} && ($invdate > $closedto)) {
784     print qq| <input class=submit type=submit name=action value="|
785       . $locale->text('Post') . qq|">|;
786   }
787
788   print $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
789     qq|
790
791 <input type=hidden name=rowcount value=$form->{rowcount}>
792
793 <input name=callback type=hidden value="$form->{callback}">
794
795 <input type=hidden name=path value=$form->{path}>
796 <input type=hidden name=login value=$form->{login}>
797 <input type=hidden name=password value=$form->{password}>
798
799 </form>
800
801 </body>
802 </html>
803 |;
804
805   $lxdebug->leave_sub();
806 }
807
808 sub update {
809   $lxdebug->enter_sub();
810
811   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
812     qw(exchangerate creditlimit creditremaining);
813
814   &check_name(vendor);
815
816   &check_project;
817
818   $form->{exchangerate} = $exchangerate
819     if (
820         $form->{forex} = (
821                       $exchangerate =
822                         $form->check_exchangerate(
823                         \%myconfig, $form->{currency}, $form->{invdate}, 'sell'
824                         )));
825
826   for $i (1 .. $form->{paidaccounts}) {
827     if ($form->{"paid_$i"}) {
828       map {
829         $form->{"${_}_$i"} =
830           $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
831       } qw(paid exchangerate);
832
833       $form->{"exchangerate_$i"} = $exchangerate
834         if (
835             $form->{"forex_$i"} = (
836                 $exchangerate =
837                   $form->check_exchangerate(
838                   \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell'
839                   )));
840     }
841   }
842
843   $i            = $form->{rowcount};
844   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
845
846   if (   ($form->{"partnumber_$i"} eq "")
847       && ($form->{"description_$i"} eq "")
848       && ($form->{"partsgroup_$i"} eq "")) {
849     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
850     &check_form;
851
852   } else {
853
854     IR->retrieve_item(\%myconfig, \%$form);
855
856     my $rows = scalar @{ $form->{item_list} };
857
858     if ($rows) {
859       $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
860
861       if ($rows > 1) {
862
863         &select_item;
864         exit;
865
866       } else {
867
868         # override sellprice if there is one entered
869         $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
870
871         map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g }
872           qw(partnumber description unit);
873
874         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
875           keys %{ $form->{item_list}[0] };
876
877         $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
878
879         ($dec) = ($s =~ /\.(\d+)/);
880         $dec           = length $dec;
881         $decimalplaces = ($dec > 2) ? $dec : 2;
882
883         if ($sellprice) {
884           $form->{"sellprice_$i"} = $sellprice;
885         } else {
886
887           # if there is an exchange rate adjust sellprice
888           $form->{"sellprice_$i"} /= $exchangerate;
889         }
890
891         $amount =
892           $form->{"sellprice_$i"} * $form->{"qty_$i"} *
893           (1 - $form->{"discount_$i"} / 100);
894         $form->{creditremaining} -= $amount;
895         $form->{"sellprice_$i"} =
896           $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
897                                $decimalplaces);
898         $form->{"qty_$i"} =
899           $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
900       }
901
902       &display_form;
903
904     } else {
905
906       # ok, so this is a new part
907       # ask if it is a part or service item
908
909       if (   $form->{"partsgroup_$i"}
910           && ($form->{"partsnumber_$i"} eq "")
911           && ($form->{"description_$i"} eq "")) {
912         $form->{rowcount}--;
913         $form->{"discount_$i"} = "";
914         &display_form;
915           } else {
916
917         $form->{"id_$i"}   = 0;
918         $form->{"unit_$i"} = $locale->text('ea');
919
920         &new_item;
921
922       }
923     }
924   }
925   $lxdebug->leave_sub();
926 }
927
928 sub storno {
929   $lxdebug->enter_sub();
930
931   if ($form->{storno}) {
932     $form->error($locale->text('Cannot storno storno invoice!'));
933   }
934
935   $form->{storno_id} = $form->{id};
936   $form->{storno} = 1;
937   $form->{id} = "";
938   $form->{invnumber} = "Storno zu " . $form->{invnumber};
939
940   &post();
941   $lxdebug->leave_sub();
942
943 }
944
945 sub use_as_template {
946   $lxdebug->enter_sub();
947
948   map { delete $form->{$_} } qw(printed emailed queued invnumber invdate deliverydate id datepaid_1 source_1 memo_1 paid_1 exchangerate_1 AP_paid_1 storno);
949   $form->{paidaccounts} = 1;
950   $form->{rowcount}--;
951   $form->{invdate} = $form->current_date(\%myconfig);
952   &display_form;
953
954   $lxdebug->leave_sub();
955 }
956
957 sub post_payment {
958   $lxdebug->enter_sub();
959   for $i (1 .. $form->{paidaccounts}) {
960     if ($form->{"paid_$i"}) {
961       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
962
963       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
964
965       $form->error($locale->text('Cannot post payment for a closed period!'))
966         if ($datepaid <= $closedto);
967
968       if ($form->{currency} ne $form->{defaultcurrency}) {
969         $form->{"exchangerate_$i"} = $form->{exchangerate}
970           if ($invdate == $datepaid);
971         $form->isblank("exchangerate_$i",
972                        $locale->text('Exchangerate for payment missing!'));
973       }
974     }
975   }
976
977   ($form->{AP})      = split /--/, $form->{AP};
978   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
979   $form->redirect($locale->text(' Payment posted!'))
980       if (IR->post_payment(\%myconfig, \%$form));
981     $form->error($locale->text('Cannot post payment!'));
982
983
984   $lxdebug->leave_sub();
985 }
986
987 sub post {
988   $lxdebug->enter_sub();
989
990   $form->isblank("invdate", $locale->text('Invoice Date missing!'));
991   $form->isblank("vendor",  $locale->text('Vendor missing!'));
992
993   # if the vendor changed get new values
994   if (&check_name(vendor)) {
995     &update;
996     exit;
997   }
998
999   &validate_items;
1000
1001   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1002   $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
1003
1004   $form->error($locale->text('Cannot post invoice for a closed period!'))
1005     if ($invdate <= $closedto);
1006
1007   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1008     if ($form->{currency} ne $form->{defaultcurrency});
1009
1010   for $i (1 .. $form->{paidaccounts}) {
1011     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
1012       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1013
1014       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1015
1016       $form->error($locale->text('Cannot post payment for a closed period!'))
1017         if ($datepaid <= $closedto);
1018
1019       if ($form->{currency} ne $form->{defaultcurrency}) {
1020         $form->{"exchangerate_$i"} = $form->{exchangerate}
1021           if ($invdate == $datepaid);
1022         $form->isblank("exchangerate_$i",
1023                        $locale->text('Exchangerate for payment missing!'));
1024       }
1025     }
1026   }
1027
1028   ($form->{AP})      = split /--/, $form->{AP};
1029   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
1030
1031   $form->{id} = 0 if $form->{postasnew};
1032
1033
1034   relink_accounts();
1035   $form->redirect(  $locale->text('Invoice')
1036                   . " $form->{invnumber} "
1037                   . $locale->text('posted!'))
1038     if (IR->post_invoice(\%myconfig, \%$form));
1039   $form->error($locale->text('Cannot post invoice!'));
1040
1041   $lxdebug->leave_sub();
1042 }
1043
1044 sub delete {
1045   $lxdebug->enter_sub();
1046
1047   $form->header;
1048
1049   print qq|
1050 <body>
1051
1052 <form method=post action=$form->{script}>
1053 |;
1054
1055   # delete action variable
1056   map { delete $form->{$_} } qw(action header);
1057
1058   foreach $key (keys %$form) {
1059     $form->{$key} =~ s/\"/&quot;/g;
1060     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1061   }
1062
1063   print qq|
1064 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1065
1066 <h4>|
1067     . $locale->text('Are you sure you want to delete Invoice Number')
1068     . qq| $form->{invnumber}</h4>
1069 <p>
1070 <input name=action class=submit type=submit value="|
1071     . $locale->text('Yes') . qq|">
1072 </form>
1073 |;
1074
1075   $lxdebug->leave_sub();
1076 }
1077
1078 sub yes {
1079   $lxdebug->enter_sub();
1080
1081   $form->redirect($locale->text('Invoice deleted!'))
1082     if (IR->delete_invoice(\%myconfig, \%$form));
1083   $form->error($locale->text('Cannot delete invoice!'));
1084
1085   $lxdebug->leave_sub();
1086 }