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