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