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