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