11bb82c4b83bf2112c1bb2b883e516caa7e02008
[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"}&nbsp;|
557                                   . $form->{"${item}_rate"} * 100 .qq|%</th>
558                   <td align=right>$form->{"${item}_total"}</td>
559                 </tr>
560 |;
561       }
562     }
563
564     $form->{invsubtotal} =
565       $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
566
567     $subtotal = qq|
568               <tr>
569                 <th align=right>| . $locale->text('Subtotal') . qq|</th>
570                 <td align=right>$form->{invsubtotal}</td>
571               </tr>
572 |;
573
574   }
575
576   if ($form->{taxincluded}) {
577     foreach $item (split / /, $form->{taxaccounts}) {
578       if ($form->{"${item}_base"}) {
579         $form->{"${item}_total"} =
580           $form->round_amount(
581                            ($form->{"${item}_base"} * $form->{"${item}_rate"} /
582                               (1 + $form->{"${item}_rate"})
583                            ),
584                            2);
585         $form->{"${item}_base"} =
586           $form->round_amount($form->{"${item}_base"}, 2);
587         $form->{"${item}_netto"} =
588           $form->round_amount(
589                           ($form->{"${item}_base"} - $form->{"${item}_total"}),
590                           2);
591         $form->{"${item}_netto"} =
592           $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
593         $form->{"${item}_total"} =
594           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
595
596         $tax .= qq|
597               <tr>
598                 <th align=right>Enthaltene $form->{"${item}_description"}&nbsp;|
599                                 . $form->{"${item}_rate"} * 100 .qq|%</th>
600                 <td align=right>$form->{"${item}_total"}</td>
601               </tr>
602               <tr>
603                 <th align=right>Nettobetrag</th>
604                 <td align=right>$form->{"${item}_netto"}</td>
605               </tr>
606 |;
607       }
608     }
609
610   }
611
612   $form->{oldinvtotal} = $form->{invtotal};
613   $form->{invtotal}    =
614     $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
615
616   print qq|
617   <tr>
618     <td colspan=$colspan>
619       <table cellspacing="0">
620         <tr valign=bottom>
621           <td>
622             <table>
623               <tr>
624                 <th align=left>| . $locale->text('Notes') . qq|</th>
625                 <th align=left>| . $locale->text('Internal Notes') . qq|</th>
626               </tr>
627               <tr valign=top>
628                 <td>$notes</td>
629                 <td>$intnotes</td>
630               </tr>
631             </table>
632           </td>
633           <td colspan=2 align=right width=100%>
634             $taxincluded
635             <br>
636             <table width=100%>
637               $subtotal
638               $tax
639               <tr>0
640                 <th align=right>| . $locale->text('Total') . qq|</th>
641                 <td align=right>$form->{invtotal}</td>
642               </tr>
643             </table>
644           </td>
645         </tr>
646       </table>
647     </td>
648   </tr>
649 |;
650   if ($webdav) {
651     $webdav_list = qq|
652   <tr>
653     <td><hr size=3 noshade></td>
654   </tr>
655   <tr>
656     <th class=listtop align=left>Dokumente im Webdav-Repository</th>
657   </tr>
658     <table width=100%>
659       <td align=left width=30%><b>Dateiname</b></td>
660       <td align=left width=70%><b>Webdavlink</b></td>
661 |;
662     foreach $file (keys %{ $form->{WEBDAV} }) {
663       $webdav_list .= qq|
664       <tr>
665         <td align=left>$file</td>
666         <td align=left><a href="$form->{WEBDAV}{$file}">$form->{WEBDAV}{$file}</a></td>
667       </tr>
668 |;
669     }
670     $webdav_list .= qq|
671     </table>
672   </tr>
673 |;
674
675     print $webdav_list;
676   }
677   print qq|
678 <input type=hidden name=jscalendar value=$form->{jscalendar}>
679 |;
680   print qq|
681   <tr>
682     <td colspan=$colspan>
683       <table width=100%>
684         <tr>
685           <th colspan=6 class=listheading>| . $locale->text('Payments') . qq|</th>
686         </tr>
687 |;
688
689   if ($form->{currency} eq $form->{defaultcurrency}) {
690     @column_index = qw(datepaid source memo paid AP_paid);
691   } else {
692     @column_index = qw(datepaid source memo paid exchangerate AP_paid);
693   }
694
695   $column_data{datepaid}     = "<th>" . $locale->text('Date') . "</th>";
696   $column_data{paid}         = "<th>" . $locale->text('Amount') . "</th>";
697   $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
698   $column_data{AP_paid}      = "<th>" . $locale->text('Account') . "</th>";
699   $column_data{source}       = "<th>" . $locale->text('Source') . "</th>";
700   $column_data{memo}         = "<th>" . $locale->text('Memo') . "</th>";
701
702   print qq|
703         <tr>
704 |;
705   map { print "$column_data{$_}\n" } @column_index;
706   print qq|
707         </tr>
708 |;
709
710   my @triggers = ();
711   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
712   for $i (1 .. $form->{paidaccounts}) {
713
714     print qq|
715         <tr>
716 |;
717
718     $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
719     $form->{"selectAP_paid_$i"} =~
720       s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
721
722     # format amounts
723     if ($form->{"paid_$i"}) {
724       $form->{"paid_$i"} =
725         $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
726     }
727     $form->{"exchangerate_$i"} =
728       $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
729
730     $exchangerate = qq|&nbsp;|;
731     if ($form->{currency} ne $form->{defaultcurrency}) {
732       if ($form->{"forex_$i"}) {
733         $exchangerate =
734           qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
735       } else {
736         $exchangerate =
737           qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
738       }
739     }
740     $exchangerate .= qq|
741 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
742 |;
743
744     $column_data{"paid_$i"} =
745       qq|<td align=center><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
746     $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
747     $column_data{"AP_paid_$i"}      =
748       qq|<td align=center><select name="AP_paid_$i">$form->{"selectAP_paid_$i"}</select></td>|;
749     $column_data{"datepaid_$i"} =
750       qq|<td align=center><input name="datepaid_$i" id="datepaid_$i" size=11 title="$myconfig{dateformat}" value=$form->{"datepaid_$i"}>
751          <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
752     $column_data{"source_$i"} =
753       qq|<td align=center><input name="source_$i" size=11 value=$form->{"source_$i"}></td>|;
754     $column_data{"memo_$i"} =
755       qq|<td align=center><input name="memo_$i" size=11 value=$form->{"memo_$i"}></td>|;
756
757     map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
758
759     print qq|
760         </tr>
761 |;
762     push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
763   }
764
765   print qq|
766             <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
767             <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
768             <input type=hidden name=selectAP_paid value="$form->{selectAP_paid}">
769       </table>
770     </td>
771   </tr>
772   <tr>
773     <td><hr size=3 noshade></td>
774   </tr>
775 </table>
776 <br>
777 |;
778
779   $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
780   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
781
782   if ($form->{id}) {
783     print qq|<input class=submit type=submit name=action value="|
784       . $locale->text('Post Payment') . qq|">
785 |;
786     print qq|<input class=submit type=submit name=action value="|
787       . $locale->text('Storno') . qq|">
788 | unless ($form->{storno});
789     if ($form->{radier}) {
790     print qq|
791     <input class=submit type=submit name=action value="|
792       . $locale->text('Delete') . qq|">
793 |;
794   }
795     print qq|<input class=submit type=submit name=action value="|
796       . $locale->text('Use As Template') . qq|">
797 |;
798
799   }
800
801   print qq|<input class=submit type=submit name=action id=update_button value="|
802     . $locale->text('Update') . qq|">|;
803
804   if (!$form->{id} && ($invdate > $closedto)) {
805     print qq| <input class=submit type=submit name=action value="|
806       . $locale->text('Post') . qq|">|;
807   }
808
809   print $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
810     qq|
811
812 <input type=hidden name=rowcount value=$form->{rowcount}>
813
814 <input name=callback type=hidden value="$form->{callback}">
815
816 <input type=hidden name=path value=$form->{path}>
817 <input type=hidden name=login value=$form->{login}>
818 <input type=hidden name=password value=$form->{password}>
819 |;
820   # button for saving history
821   if($form->{id} ne "") {
822     print qq|
823           <input type="button" class="submit" onclick="set_history_window(|
824           . Q($form->{id})
825           . qq|);" name="history" id="history" value="|
826           . $locale->text('history')
827           . qq|">|;
828   }
829   # /button for saving history
830
831 print qq|</form>
832
833 </body>
834 </html>
835 |;
836
837   $lxdebug->leave_sub();
838 }
839
840 sub update {
841   $lxdebug->enter_sub();
842
843   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
844     qw(exchangerate creditlimit creditremaining);
845
846   &check_name(vendor);
847
848   $form->{exchangerate} = $exchangerate
849     if (
850         $form->{forex} = (
851                       $exchangerate =
852                         $form->check_exchangerate(
853                         \%myconfig, $form->{currency}, $form->{invdate}, 'sell'
854                         )));
855
856   for $i (1 .. $form->{paidaccounts}) {
857     if ($form->{"paid_$i"}) {
858       map {
859         $form->{"${_}_$i"} =
860           $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
861       } qw(paid exchangerate);
862
863       $form->{"exchangerate_$i"} = $exchangerate
864         if (
865             $form->{"forex_$i"} = (
866                 $exchangerate =
867                   $form->check_exchangerate(
868                   \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell'
869                   )));
870     }
871   }
872
873   $i            = $form->{rowcount};
874   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
875
876   if (   ($form->{"partnumber_$i"} eq "")
877       && ($form->{"description_$i"} eq "")
878       && ($form->{"partsgroup_$i"} eq "")) {
879     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
880     &check_form;
881
882   } else {
883
884     IR->retrieve_item(\%myconfig, \%$form);
885
886     my $rows = scalar @{ $form->{item_list} };
887
888     if ($rows) {
889       $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
890
891       if ($rows > 1) {
892
893         &select_item;
894         exit;
895
896       } else {
897
898         # override sellprice if there is one entered
899         $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
900
901         map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g }
902           qw(partnumber description unit);
903
904         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
905           keys %{ $form->{item_list}[0] };
906
907         $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
908
909         ($dec) = ($s =~ /\.(\d+)/);
910         $dec           = length $dec;
911         $decimalplaces = ($dec > 2) ? $dec : 2;
912
913         if ($sellprice) {
914           $form->{"sellprice_$i"} = $sellprice;
915         } else {
916
917           # if there is an exchange rate adjust sellprice
918           $form->{"sellprice_$i"} /= $exchangerate;
919         }
920
921         $amount =
922           $form->{"sellprice_$i"} * $form->{"qty_$i"} *
923           (1 - $form->{"discount_$i"} / 100);
924         $form->{creditremaining} -= $amount;
925         $form->{"sellprice_$i"} =
926           $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
927                                $decimalplaces);
928         $form->{"qty_$i"} =
929           $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
930       }
931
932       &display_form;
933
934     } else {
935
936       # ok, so this is a new part
937       # ask if it is a part or service item
938
939       if (   $form->{"partsgroup_$i"}
940           && ($form->{"partsnumber_$i"} eq "")
941           && ($form->{"description_$i"} eq "")) {
942         $form->{rowcount}--;
943         $form->{"discount_$i"} = "";
944         &display_form;
945           } else {
946
947         $form->{"id_$i"}   = 0;
948         $form->{"unit_$i"} = $locale->text('ea');
949
950         &new_item;
951
952       }
953     }
954   }
955   $lxdebug->leave_sub();
956 }
957
958 sub storno {
959   $lxdebug->enter_sub();
960
961   if ($form->{storno}) {
962     $form->error($locale->text('Cannot storno storno invoice!'));
963   }
964
965   $form->{storno_id} = $form->{id};
966   $form->{storno} = 1;
967   $form->{id} = "";
968   $form->{invnumber} = "Storno zu " . $form->{invnumber};
969
970   &post();
971   $lxdebug->leave_sub();
972
973 }
974
975 sub use_as_template {
976   $lxdebug->enter_sub();
977
978   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);
979   $form->{paidaccounts} = 1;
980   $form->{rowcount}--;
981   $form->{invdate} = $form->current_date(\%myconfig);
982   &display_form;
983
984   $lxdebug->leave_sub();
985 }
986
987 sub post_payment {
988   $lxdebug->enter_sub();
989   for $i (1 .. $form->{paidaccounts}) {
990     if ($form->{"paid_$i"}) {
991       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
992
993       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
994
995       $form->error($locale->text('Cannot post payment for a closed period!'))
996         if ($datepaid <= $closedto);
997
998       if ($form->{currency} ne $form->{defaultcurrency}) {
999         $form->{"exchangerate_$i"} = $form->{exchangerate}
1000           if ($invdate == $datepaid);
1001         $form->isblank("exchangerate_$i",
1002                        $locale->text('Exchangerate for payment missing!'));
1003       }
1004     }
1005   }
1006
1007   ($form->{AP})      = split /--/, $form->{AP};
1008   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
1009   if (IR->post_payment(\%myconfig, \%$form)){
1010         
1011         if(!exists $form->{addition} && $form->{id} ne "") {
1012                 # saving the history
1013                 $form->{addition} = "PAYMENT POSTED";
1014                 $form->save_history($form->dbconnect(\%myconfig));
1015                 # /saving the history 
1016                 $form->redirect($locale->text(' Payment posted!'));
1017         }
1018   }
1019     $form->error($locale->text('Cannot post payment!'));
1020
1021
1022   $lxdebug->leave_sub();
1023 }
1024
1025 sub post {
1026   $lxdebug->enter_sub();
1027
1028   $form->isblank("invdate", $locale->text('Invoice Date missing!'));
1029   $form->isblank("vendor",  $locale->text('Vendor missing!'));
1030
1031   # if the vendor changed get new values
1032   if (&check_name(vendor)) {
1033     &update;
1034     exit;
1035   }
1036
1037   &validate_items;
1038
1039   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1040   $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
1041
1042   $form->error($locale->text('Cannot post invoice for a closed period!'))
1043     if ($invdate <= $closedto);
1044
1045   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1046     if ($form->{currency} ne $form->{defaultcurrency});
1047
1048   for $i (1 .. $form->{paidaccounts}) {
1049     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
1050       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1051
1052       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1053
1054       $form->error($locale->text('Cannot post payment for a closed period!'))
1055         if ($datepaid <= $closedto);
1056
1057       if ($form->{currency} ne $form->{defaultcurrency}) {
1058         $form->{"exchangerate_$i"} = $form->{exchangerate}
1059           if ($invdate == $datepaid);
1060         $form->isblank("exchangerate_$i",
1061                        $locale->text('Exchangerate for payment missing!'));
1062       }
1063     }
1064   }
1065
1066   ($form->{AP})      = split /--/, $form->{AP};
1067   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
1068
1069   $form->{id} = 0 if $form->{postasnew};
1070
1071
1072   relink_accounts();
1073   if (IR->post_invoice(\%myconfig, \%$form)){ 
1074         # saving the history
1075         if(!exists $form->{addition} && $form->{id} ne "") {
1076                 $form->{addition} = "POSTED";
1077                 #$form->{what_done} = $locale->text("Rechnungsnummer") . qq| | . $form->{invnumber};
1078                 $form->save_history($form->dbconnect(\%myconfig));
1079         }
1080         # /saving the history
1081         $form->redirect(  $locale->text('Invoice')
1082                   . " $form->{invnumber} "
1083                   . $locale->text('posted!'));
1084   }
1085   $form->error($locale->text('Cannot post invoice!'));
1086
1087   $lxdebug->leave_sub();
1088 }
1089
1090 sub delete {
1091   $lxdebug->enter_sub();
1092
1093   $form->header;
1094   print qq|
1095 <body>
1096
1097 <form method=post action=$form->{script}>
1098 |;
1099
1100   # delete action variable
1101   map { delete $form->{$_} } qw(action header);
1102
1103   foreach $key (keys %$form) {
1104     $form->{$key} =~ s/\"/&quot;/g;
1105     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1106   }
1107
1108   print qq|
1109 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1110
1111 <h4>|
1112     . $locale->text('Are you sure you want to delete Invoice Number')
1113     . qq| $form->{invnumber}</h4>
1114 <p>
1115 <input name=action class=submit type=submit value="|
1116     . $locale->text('Yes') . qq|">
1117 </form>
1118 |;
1119
1120   $lxdebug->leave_sub();
1121 }
1122
1123 sub yes {
1124   $lxdebug->enter_sub();
1125   if (IR->delete_invoice(\%myconfig, \%$form)) {
1126     # saving the history
1127     if(!exists $form->{addition}) {
1128           $form->{addition} = "DELETED";
1129           $form->save_history($form->dbconnect(\%myconfig));
1130     }
1131     # /saving the history 
1132     $form->redirect($locale->text('Invoice deleted!'));
1133   }
1134   $form->error($locale->text('Cannot delete invoice!'));
1135
1136   $lxdebug->leave_sub();
1137 }