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