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