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