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