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