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