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