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