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