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