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