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