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