]> wagnertech.de Git - kivitendo-erp.git/blob - bin/mozilla/ir.pl
Zusätzliche Auftragsfelder beim Laden von Rechnungen holen.
[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::FU;
35 use SL::IR;
36 use SL::IS;
37 use SL::PE;
38 use List::Util qw(max sum);
39
40 require "bin/mozilla/io.pl";
41 require "bin/mozilla/invoice_io.pl";
42 require "bin/mozilla/arap.pl";
43 require "bin/mozilla/common.pl";
44 require "bin/mozilla/drafts.pl";
45
46 use strict;
47
48 1;
49
50 # end of main
51
52 sub add {
53   $main::lxdebug->enter_sub();
54
55   my $form     = $main::form;
56   my $locale   = $main::locale;
57
58   $main::auth->assert('vendor_invoice_edit');
59
60   return $main::lxdebug->leave_sub() if (load_draft_maybe());
61
62   $form->{title} = $locale->text('Add Vendor Invoice');
63
64   &invoice_links;
65   &prepare_invoice;
66   &display_form;
67
68   $main::lxdebug->leave_sub();
69 }
70
71 sub edit {
72   $main::lxdebug->enter_sub();
73
74   my $form     = $main::form;
75   my $locale   = $main::locale;
76
77   $main::auth->assert('vendor_invoice_edit');
78
79   # show history button
80   $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
81   #/show hhistory button
82
83   $form->{title} = $locale->text('Edit Vendor Invoice');
84
85   &invoice_links;
86   &prepare_invoice;
87   &display_form;
88
89   $main::lxdebug->leave_sub();
90 }
91
92 sub invoice_links {
93   $main::lxdebug->enter_sub();
94
95   my $form     = $main::form;
96   my %myconfig = %main::myconfig;
97
98   $main::auth->assert('vendor_invoice_edit');
99
100   # create links
101   $form->{webdav}   = $main::webdav;
102   $form->{jsscript} = 1;
103
104   $form->create_links("AP", \%myconfig, "vendor");
105
106   #quote all_vendor Bug 133
107   foreach my $ref (@{ $form->{all_vendor} }) {
108     $ref->{name} = $form->quote($ref->{name});
109   }
110
111   if ($form->{all_vendor}) {
112     unless ($form->{vendor_id}) {
113       $form->{vendor_id} = $form->{all_vendor}->[0]->{id};
114     }
115   }
116
117   my ($payment_id, $language_id, $taxzone_id);
118   if ($form->{payment_id}) {
119     $payment_id = $form->{payment_id};
120   }
121   if ($form->{language_id}) {
122     $language_id = $form->{language_id};
123   }
124   if ($form->{taxzone_id}) {
125     $taxzone_id = $form->{taxzone_id};
126   }
127
128   my $cp_id = $form->{cp_id};
129   IR->get_vendor(\%myconfig, \%$form);
130   IR->retrieve_invoice(\%myconfig, \%$form);
131   $form->{cp_id} = $cp_id;
132
133   if ($payment_id) {
134     $form->{payment_id} = $payment_id;
135   }
136   if ($language_id) {
137     $form->{language_id} = $language_id;
138   }
139   if ($taxzone_id) {
140     $form->{taxzone_id} = $taxzone_id;
141   }
142
143   my @curr = split(/:/, $form->{currencies}); #seems to be missing
144   map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
145
146   $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
147
148   # build vendor/customer drop down comatibility... don't ask
149   if (@{ $form->{"all_vendor"} || [] }) {
150     $form->{"selectvendor"} = 1;
151     $form->{vendor}         = qq|$form->{vendor}--$form->{vendor_id}|;
152   }
153
154   # departments
155   if ($form->{all_departments}) {
156     $form->{selectdepartment} = "<option>\n";
157     $form->{department}       = "$form->{department}--$form->{department_id}";
158
159     map {
160       $form->{selectdepartment} .=
161         "<option>$_->{description}--$_->{id}\n"
162     } (@{ $form->{all_departments} || [] });
163   }
164
165   # forex
166   $form->{forex} = $form->{exchangerate};
167   my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
168
169   foreach my $key (keys %{ $form->{AP_links} }) {
170
171     foreach my $ref (@{ $form->{AP_links}{$key} }) {
172       $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}\n";
173     }
174
175     next unless $form->{acc_trans}{$key};
176
177     if ($key eq "AP_paid") {
178       for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
179         $form->{"AP_paid_$i"} =
180           "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
181
182         # reverse paid
183         $form->{"paid_$i"}     = $form->{acc_trans}{$key}->[$i - 1]->{amount};
184         $form->{"datepaid_$i"} =
185           $form->{acc_trans}{$key}->[$i - 1]->{transdate};
186         $form->{"forex_$i"} = $form->{"exchangerate_$i"} =
187           $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
188         $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
189         $form->{"memo_$i"}   = $form->{acc_trans}{$key}->[$i - 1]->{memo};
190
191         $form->{paidaccounts} = $i;
192       }
193     } else {
194       $form->{$key} =
195         "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
196     }
197
198   }
199
200   $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
201
202   $form->{AP} = $form->{AP_1} unless $form->{id};
203
204   $form->{locked} =
205     ($form->datetonum($form->{invdate}, \%myconfig) <=
206      $form->datetonum($form->{closedto}, \%myconfig));
207
208   $main::lxdebug->leave_sub();
209 }
210
211 sub prepare_invoice {
212   $main::lxdebug->enter_sub();
213
214   my $form     = $main::form;
215   my %myconfig = %main::myconfig;
216
217   $main::auth->assert('vendor_invoice_edit');
218
219   if ($form->{id}) {
220
221     map { $form->{$_} =~ s/\"/&quot;/g } qw(invnumber ordnumber quonumber);
222
223     my $i = 0;
224     foreach my $ref (@{ $form->{invoice_details} }) {
225       $i++;
226       map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
227
228       my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
229       $dec           = length $dec;
230       my $decimalplaces = ($dec > 2) ? $dec : 2;
231
232       $form->{"sellprice_$i"} =
233         $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
234                              $decimalplaces);
235
236       (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
237       $dec_qty = length $dec_qty;
238
239       $form->{"qty_$i"} =
240         $form->format_amount(\%myconfig, ($form->{"qty_$i"} * -1), $dec_qty);
241
242       $form->{rowcount} = $i;
243     }
244   }
245
246   $main::lxdebug->leave_sub();
247 }
248
249 sub form_header {
250   $main::lxdebug->enter_sub();
251
252   my $form     = $main::form;
253   my %myconfig = %main::myconfig;
254   my $locale   = $main::locale;
255   my $cgi      = $main::cgi;
256
257   $main::auth->assert('vendor_invoice_edit');
258
259   push @{ $form->{AJAX} }, CGI::Ajax->new('set_duedate_vendor' => "$form->{script}?action=set_duedate_vendor");
260
261   # set option selected
262   foreach my $item (qw(AP vendor currency department)) {
263     $form->{"select$item"} =~ s/ selected//;
264     $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
265   }
266
267   $form->{employee_id}     = $form->{old_employee_id} if $form->{old_employee_id};
268   $form->{salesman_id}     = $form->{old_salesman_id} if $form->{old_salesman_id};
269   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
270   $form->{radier}          = ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
271   $form->{exchangerate}    = $form->format_amount(\%myconfig, $form->{exchangerate});
272   $form->{creditlimit}     = $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
273   $form->{creditremaining} = $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
274
275   my $exchangerate = "";
276   if ($form->{currency} ne $form->{defaultcurrency}) {
277     if ($form->{forex}) {
278       $exchangerate .= qq| <th align=right nowrap>| . $locale->text('Exchangerate') . qq|</th>
279                            <td>$form->{exchangerate}<input type=hidden name=exchangerate value=$form->{exchangerate}></td>\n|;
280     } else {
281       $exchangerate .= qq| <th align=right nowrap>| . $locale->text('Exchangerate') . qq|</th>
282                            <td><input name=exchangerate size=10 value=$form->{exchangerate}></td>\n|;
283     }
284   }
285   $exchangerate .= qq| <input type=hidden name=forex value=$form->{forex}>\n|;
286
287   my @old_project_ids = ($form->{"globalproject_id"});
288   map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
289
290   $form->get_lists("contacts"      => "ALL_CONTACTS",
291                    "projects"      => { "key"  => "ALL_PROJECTS",
292                                       "all"    => 0,
293                                       "old_id" => \@old_project_ids },
294                    "taxzones"      => "ALL_TAXZONES",
295                    "employees"     => "ALL_SALESMEN",
296                    "currencies"    => "ALL_CURRENCIES",
297                    "vendors"       => "ALL_VENDORS",
298                    "price_factors" => "ALL_PRICE_FACTORS");
299
300   my %labels;
301   my @values = (undef);
302   foreach my $item (@{ $form->{"ALL_CONTACTS"} }) {
303     push(@values, $item->{"cp_id"});
304     $labels{$item->{"cp_id"}} = $item->{"cp_name"} . ($item->{"cp_abteilung"} ? " ($item->{cp_abteilung})" : "");
305   }
306
307   my $contact;
308   if (scalar @values > 1) {
309     $contact = qq|
310     <tr>
311       <th align="right">| . $locale->text('Contact Person') . qq|</th>
312       <td>| .  NTI($cgi->popup_menu('-name' => 'cp_id', '-values' => \@values, '-style' => 'width: 250px',
313                                     '-labels' => \%labels, '-default' => $form->{"cp_id"})) . qq|
314       </td>
315     </tr>|;
316   }
317
318   %labels = ();
319   @values = ("");
320   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
321     push(@values, $item->{"id"});
322     $labels{$item->{"id"}} = $item->{"projectnumber"};
323   }
324   my $globalprojectnumber = NTI($cgi->popup_menu('-name' => 'globalproject_id', '-values' => \@values, '-labels' => \%labels,
325                                                  '-default' => $form->{"globalproject_id"}));
326
327   %labels = ();
328   @values = ();
329   my $i = 0;
330   foreach my $item (@{ $form->{"ALL_CURRENCIES"} }) {
331     push(@values, $item);
332     $labels{$item} = $item;
333   }
334
335   $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
336   my $currencies;
337   if (scalar @values) {
338     $currencies = qq|
339     <tr>
340       <th align="right">| . $locale->text('Currency') . qq|</th>
341       <td>| .  NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
342                                     '-values' => \@values, '-labels' => \%labels)) . qq|
343       </td>
344     </tr>|;
345   }
346
347   %labels = ();
348   @values = ();
349   foreach my $item (@{ $form->{"ALL_SALESMEN"} }) {
350     push(@values, $item->{"id"});
351     $labels{$item->{"id"}} = $item->{"name"};
352   }
353   my $employees = qq|
354     <tr>
355       <th align="right">| . $locale->text('Employee') . qq|</th>
356       <td>| .  NTI($cgi->popup_menu('-name' => 'employee_id', '-default' => $form->{"employee_id"},
357                                     '-values' => \@values, '-labels' => \%labels)) . qq|
358       </td>
359     </tr>|;
360
361   %labels = ();
362   @values = ();
363   foreach my $item (@{ $form->{"ALL_VENDORS"} }) {
364     push(@values, $item->{name}.qq|--|.$item->{"id"});
365     $labels{$item->{name}.qq|--|.$item->{"id"}} = $item->{"name"};
366   }
367
368   $form->{selectvendor} = ($myconfig{vclimit} > scalar(@values));
369
370   my $vendors = qq|
371       <th align="right">| . $locale->text('Vendor') . qq|</th>
372       <td>| .
373         (($myconfig{vclimit} <=  scalar(@values))
374               ? qq|<input type="text" value="| . H($form->{vendor}) . qq|" name="vendor">|
375               : (NTI($cgi->popup_menu('-name' => 'vendor', '-default' => $form->{oldvendor},
376                                       '-onChange' => 'document.getElementById(\'update_button\').click();',
377                                       '-values' => \@values, '-labels' => \%labels, '-style' => 'width: 250px')))) . qq|
378         <input type="button" value="| . $locale->text('Details (one letter abbreviation)') . qq|" onclick="show_vc_details('vendor')">
379       </td>|;
380
381   %labels = ();
382   @values = ();
383   foreach my $item (@{ $form->{"ALL_TAXZONES"} }) {
384     push(@values, $item->{"id"});
385     $labels{$item->{"id"}} = $item->{"description"};
386   }
387
388   my $taxzone;
389   if (!$form->{"id"}) {
390     $taxzone = qq|
391     <tr>
392       <th align="right">| . $locale->text('Steuersatz') . qq|</th>
393       <td>| .  NTI($cgi->popup_menu('-name' => 'taxzone_id', '-default' => $form->{"taxzone_id"},
394                                     '-values' => \@values, '-labels' => \%labels, '-style' => 'width: 250px')) . qq|
395       </td>
396     </tr>|;
397   } else {
398     $taxzone = qq|
399     <tr>
400       <th align="right">| . $locale->text('Steuersatz') . qq|</th>
401       <td>
402         <input type="hidden" name="taxzone_id" value="| . H($form->{"taxzone_id"}) . qq|">
403         | . H($labels{$form->{"taxzone_id"}}) . qq|
404       </td>
405     </tr>|;
406   }
407
408   my $department = qq|
409            <tr>
410               <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
411               <td colspan="3"><select name="department" style="width: 250px">$form->{selectdepartment}</select>
412               <input type="hidden" name="selectdepartment" value="$form->{selectdepartment}">
413               </td>
414             </tr>\n| if $form->{selectdepartment};
415
416   my $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
417
418   # use JavaScript Calendar or not
419   $form->{jsscript} = 1;
420   my $jsscript = "";
421
422   my $button1 = qq|
423      <td nowrap>
424          <input name=invdate id=invdate size=11 title="$myconfig{dateformat}" value="$form->{invdate}" onBlur=\"check_right_date_format(this)\"
425                 onChange="if (this.value) set_duedate_vendor(['invdate__' + this.value, 'old_duedate__' + document.getElementsByName('duedate')[0].value, 'vendor_id__' + document.getElementsByName('vendor_id')[0].value],['duedate'])">
426          <input type=button name=invdate id="trigger1" value="?">
427      </td>\n|;
428
429 #, 'old_duedate__'' + document.getElementsByName('duedate')[0].value, 'vendor_id__' + document.getElementsByName('vendor_id')[0].value],['duedate'])">
430   my $button2 = qq|
431      <td width="13" nowrap>
432           <input name=duedate id=duedate size=11 title="$myconfig{dateformat}" value="$form->{duedate}"  onBlur=\"check_right_date_format(this)\">
433           <input type=button name=duedate id="trigger2" value=| . $locale->text('button') . qq|>
434      </td>\n|;
435
436   #write Trigger
437   $jsscript =
438     Form->write_trigger(\%myconfig, "2",
439                         "invdate", "BL", "trigger1",
440                         "duedate", "BL", "trigger2");
441
442   my $follow_up_vc         =  $form->{vendor};
443   $follow_up_vc            =~ s/--\d*\s*$//;
444   my $follow_up_trans_info =  "$form->{invnumber} ($follow_up_vc)";
445
446   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
447   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
448   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
449   $form->{javascript} .= qq|<script type="text/javascript" src="js/follow_up.js"></script>|;
450
451   $jsscript .= $form->write_trigger(\%myconfig, 2, "orddate", "BL", "trigger_orddate", "quodate", "BL", "trigger_quodate");
452
453   $form->header;
454   my $onload  = qq|focus()|;
455   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
456   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
457   print qq|
458 <body onLoad="$onload">
459 <script type="text/javascript" src="js/common.js"></script>
460 <form method="post" action="ir.pl" name="Form">
461 |;
462
463   $form->hide_form(qw(id title vc type level creditlimit creditremaining closedto locked shippted storno storno_id
464                       max_dunning_level dunning_amount vendor_id oldvendor selectvendor taxaccounts
465                       fxgain_accno fxloss_accno taxpart taxservice cursor_fokus
466                       convert_from_oe_ids convert_from_do_ids),
467                       map { $_.'_rate', $_.'_description' } split / /, $form->{taxaccounts} );
468
469   print qq|<p>$form->{saved_message}</p>| if $form->{saved_message};
470
471   print qq|
472
473 <div class="listtop" width="100%">$form->{title}</div>
474
475 <table width=100%>
476   <tr>
477     <td valign="top">
478             <table>
479         $vendors
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" style="width: 250px">$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=webdav value=$main::webdav>
542 |;
543
544   foreach my $item (split / /, $form->{taxaccounts}) {
545     print qq|
546 <input type=hidden name="${item}_rate" value=$form->{"${item}_rate"}>
547 <input type=hidden name="${item}_description" value="$form->{"${item}_description"}">
548 |;
549   }
550
551   $main::lxdebug->leave_sub();
552 }
553
554 sub form_footer {
555   $main::lxdebug->enter_sub();
556
557   my $form     = $main::form;
558   my %myconfig = %main::myconfig;
559   my $locale   = $main::locale;
560   my $cgi      = $main::cgi;
561
562   $main::auth->assert('vendor_invoice_edit');
563
564   $form->{invtotal} = $form->{invsubtotal};
565
566   my ($rows, $introws);
567   if (($rows = $form->numtextrows($form->{notes}, 25, 8)) < 2) {
568     $rows = 2;
569   }
570   if (($introws = $form->numtextrows($form->{intnotes}, 35, 8)) < 2) {
571     $introws = 2;
572   }
573   $rows = ($rows > $introws) ? $rows : $introws;
574   my $notes =
575     qq|<textarea name=notes rows=$rows cols=25 wrap=soft>$form->{notes}</textarea>|;
576   my $intnotes =
577     qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</textarea>|;
578
579   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
580
581   my $taxincluded = "";
582   if ($form->{taxaccounts}) {
583     $taxincluded = qq|
584                 <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}> <b>|
585       . $locale->text('Tax Included') . qq|</b>
586 |;
587   }
588
589   my ($tax, $subtotal);
590   if (!$form->{taxincluded}) {
591
592     foreach my $item (split / /, $form->{taxaccounts}) {
593       if ($form->{"${item}_base"}) {
594         $form->{invtotal} += $form->{"${item}_total"} =
595           $form->round_amount(
596                              $form->{"${item}_base"} * $form->{"${item}_rate"},
597                              2);
598         $form->{"${item}_total"} =
599           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
600
601         $tax .= qq|
602                 <tr>
603                   <th align=right>$form->{"${item}_description"}&nbsp;|
604                     . $form->{"${item}_rate"} * 100 .qq|%</th>
605                   <td align=right>$form->{"${item}_total"}</td>
606                 </tr>
607 |;
608       }
609     }
610
611     $form->{invsubtotal} =
612       $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
613
614     $subtotal = qq|
615               <tr>
616                 <th align=right>| . $locale->text('Subtotal') . qq|</th>
617                 <td align=right>$form->{invsubtotal}</td>
618               </tr>
619 |;
620
621   }
622
623   if ($form->{taxincluded}) {
624     foreach my $item (split / /, $form->{taxaccounts}) {
625       if ($form->{"${item}_base"}) {
626         $form->{"${item}_total"} =
627           $form->round_amount(
628                            ($form->{"${item}_base"} * $form->{"${item}_rate"} /
629                               (1 + $form->{"${item}_rate"})
630                            ),
631                            2);
632         $form->{"${item}_base"} =
633           $form->round_amount($form->{"${item}_base"}, 2);
634         $form->{"${item}_netto"} =
635           $form->round_amount(
636                           ($form->{"${item}_base"} - $form->{"${item}_total"}),
637                           2);
638         $form->{"${item}_netto"} =
639           $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
640         $form->{"${item}_total"} =
641           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
642
643         $tax .= qq|
644               <tr>
645                 <th align=right>Enthaltene $form->{"${item}_description"}&nbsp;|
646                                     . $form->{"${item}_rate"} * 100 .qq|%</th>
647                 <td align=right>$form->{"${item}_total"}</td>
648               </tr>
649               <tr>
650                 <th align=right>Nettobetrag</th>
651                 <td align=right>$form->{"${item}_netto"}</td>
652               </tr>
653 |;
654       }
655     }
656
657   }
658
659   $form->{oldinvtotal} = $form->{invtotal};
660   $form->{invtotal}    =
661     $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
662
663   my $follow_ups_block;
664   if ($form->{id}) {
665     my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
666
667     if (@{ $follow_ups} ) {
668       my $num_due       = sum map { $_->{due} * 1 } @{ $follow_ups };
669       $follow_ups_block = qq|
670       <tr>
671         <td colspan="2">| . $locale->text("There are #1 unfinished follow-ups of which #2 are due.", scalar @{ $follow_ups }, $num_due) . qq|</td>
672       </tr>
673 |;
674     }
675   }
676
677   our $colspan;
678   print qq|
679   <tr>
680     <td colspan=$colspan>
681       <table cellspacing="0">
682         <tr valign=bottom>
683           <td>
684             <table>
685               <tr>
686                 <th align=left>| . $locale->text('Notes') . qq|</th>
687                 <th align=left>| . $locale->text('Internal Notes') . qq|</th>
688               </tr>
689               <tr valign=top>
690                 <td>$notes</td>
691                 <td>$intnotes</td>
692               </tr>
693         $follow_ups_block
694             </table>
695           </td>
696           <td colspan=2 align=right width=100%>
697             $taxincluded
698             <br>
699             <table width=100%>
700               $subtotal
701               $tax
702               <tr>
703                 <th align=right>| . $locale->text('Total') . qq|</th>
704                 <td align=right>$form->{invtotal}</td>
705               </tr>
706             </table>
707           </td>
708         </tr>
709       </table>
710     </td>
711   </tr>
712 |;
713   my $webdav_list;
714   if ($main::webdav) {
715     $webdav_list = qq|
716   <tr>
717     <td><hr size=3 noshade></td>
718   </tr>
719   <tr>
720     <th class=listtop align=left>Dokumente im Webdav-Repository</th>
721   </tr>
722     <table width=100%>
723       <td align=left width=30%><b>Dateiname</b></td>
724       <td align=left width=70%><b>Webdavlink</b></td>
725 |;
726     foreach my $file (@{ $form->{WEBDAV} }) {
727       $webdav_list .= qq|
728       <tr>
729         <td align="left">$file->{name}</td>
730         <td align="left"><a href="$file->{link}">$file->{type}</a></td>
731       </tr>
732 |;
733     }
734     $webdav_list .= qq|
735     </table>
736   </tr>
737 |;
738
739     print $webdav_list;
740   }
741   print qq|
742   <tr>
743     <td colspan=$colspan>
744       <table width=100%>
745         <tr>
746           <th colspan=6 class=listheading>| . $locale->text('Payments') . qq|</th>
747         </tr>
748 |;
749
750   my @column_index;
751   if ($form->{currency} eq $form->{defaultcurrency}) {
752     @column_index = qw(datepaid source memo paid AP_paid);
753   } else {
754     @column_index = qw(datepaid source memo paid exchangerate AP_paid);
755   }
756
757   my %column_data;
758   $column_data{datepaid}     = "<th>" . $locale->text('Date') . "</th>";
759   $column_data{paid}         = "<th>" . $locale->text('Amount') . "</th>";
760   $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
761   $column_data{AP_paid}      = "<th>" . $locale->text('Account') . "</th>";
762   $column_data{source}       = "<th>" . $locale->text('Source') . "</th>";
763   $column_data{memo}         = "<th>" . $locale->text('Memo') . "</th>";
764
765   print qq|
766         <tr>
767 |;
768   map { print "$column_data{$_}\n" } @column_index;
769   print qq|
770         </tr>
771 |;
772
773   my @triggers  = ();
774   my $totalpaid = 0;
775
776   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
777   for my $i (1 .. $form->{paidaccounts}) {
778
779     print qq|
780         <tr>
781 |;
782
783     $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
784     $form->{"selectAP_paid_$i"} =~
785       s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
786
787     $totalpaid += $form->{"paid_$i"};
788
789     # format amounts
790     if ($form->{"paid_$i"}) {
791       $form->{"paid_$i"} =
792         $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
793     }
794     $form->{"exchangerate_$i"} =
795       $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
796
797     my $exchangerate = qq|&nbsp;|;
798     if ($form->{currency} ne $form->{defaultcurrency}) {
799       if ($form->{"forex_$i"}) {
800         $exchangerate =
801           qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
802       } else {
803         $exchangerate =
804           qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
805       }
806     }
807     $exchangerate .= qq|
808 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
809 |;
810
811     $column_data{"paid_$i"} =
812       qq|<td align=center><input name="paid_$i" size=11 value="$form->{"paid_$i"}" onBlur=\"check_right_number_format(this)\"></td>|;
813     $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
814     $column_data{"AP_paid_$i"}      =
815       qq|<td align=center><select name="AP_paid_$i">$form->{"selectAP_paid_$i"}</select></td>|;
816     $column_data{"datepaid_$i"} =
817       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)\">
818          <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
819     $column_data{"source_$i"} =
820       qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
821     $column_data{"memo_$i"} =
822       qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
823
824     map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
825
826     print qq|
827         </tr>
828 |;
829     push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
830   }
831
832   my $paid_missing = $form->{oldinvtotal} - $totalpaid;
833
834   print qq|
835         <tr>
836           <td></td>
837           <td></td>
838           <td align="center">| . $locale->text('Total') . qq|</td>
839           <td align="center">| . H($form->format_amount(\%myconfig, $totalpaid, 2)) . qq|</td>
840         </tr>
841         <tr>
842           <td></td>
843           <td></td>
844           <td align="center">| . $locale->text('Missing amount') . qq|</td>
845           <td align="center">| . H($form->format_amount(\%myconfig, $paid_missing, 2)) . qq|</td>
846         </tr>
847
848             <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
849             <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
850             <input type=hidden name=selectAP_paid value="$form->{selectAP_paid}">
851       </table>
852     </td>
853   </tr>
854   <tr>
855     <td><hr size=3 noshade></td>
856   </tr>
857 </table>
858 <br>
859 |;
860
861   my $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
862   my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
863
864   print qq|<input class=submit type=submit name=action id=update_button value="|
865     . $locale->text('Update') . qq|">
866 |;
867
868   if ($form->{id}) {
869     my $show_storno = !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ap") && (($totalpaid == 0) || ($totalpaid eq ""));
870
871     print qq|<input class=submit type=submit name=action value="|
872       . $locale->text('Post Payment') . qq|">
873 |;
874     print qq|<input class=submit type=submit name=action value="|
875       . $locale->text('Storno') . qq|">
876 | if ($show_storno);
877     if ($form->{radier}) {
878     print qq|
879     <input class=submit type=submit name=action value="|
880       . $locale->text('Delete') . qq|">
881 |;
882   }
883     print qq|<input class=submit type=submit name=action value="|
884       . $locale->text('Use As Template') . qq|">
885         <input type="button" class="submit" onclick="follow_up_window()" value="|
886       . $locale->text('Follow-Up')
887       . qq|">
888 |;
889
890   }
891
892   if (!$form->{id} && ($invdate > $closedto)) {
893     print qq| <input class=submit type=submit name=action value="|
894       . $locale->text('Post') . qq|"> | .
895       NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'),
896                        '-class' => 'submit'));
897   }
898
899   print $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers);
900   $form->hide_form(qw(rowcount callback draft_id draft_description vendor_discount));
901
902   # button for saving history
903   if($form->{id} ne "") {
904     print qq|
905           <input type="button" class="submit" onclick="set_history_window(|
906           . Q($form->{id})
907           . qq|);" name="history" id="history" value="|
908           . $locale->text('history')
909           . qq|">|;
910   }
911   # /button for saving history
912   # mark_as_paid button
913   if($form->{id} ne "") {
914     print qq| <input type="submit" class="submit" name="action" value="|
915           . $locale->text('mark as paid') . qq|">|;
916   }
917   # /mark_as_paid button
918 print qq|</form>
919 </body>
920 </html>
921 |;
922
923   $main::lxdebug->leave_sub();
924 }
925
926 sub mark_as_paid {
927   $main::lxdebug->enter_sub();
928
929   my $form     = $main::form;
930   my %myconfig = %main::myconfig;
931
932   $main::auth->assert('vendor_invoice_edit');
933
934   &mark_as_paid_common(\%myconfig,"ap");
935
936   $main::lxdebug->leave_sub();
937 }
938
939 sub update {
940   $main::lxdebug->enter_sub();
941
942   my $form     = $main::form;
943   my %myconfig = %main::myconfig;
944
945   $main::auth->assert('vendor_invoice_edit');
946
947   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining);
948
949   &check_name('vendor');
950
951   $form->{forex}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'sell');
952   $form->{exchangerate} = $form->{forex} if $form->{forex};
953
954   for my $i (1 .. $form->{paidaccounts}) {
955     next unless $form->{"paid_$i"};
956     map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
957     $form->{"forex_$i"}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
958     $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
959   }
960
961   my $i            = $form->{rowcount};
962   my $exchangerate = ($form->{exchangerate} * 1) || 1;
963
964   if (   ($form->{"partnumber_$i"} eq "")
965       && ($form->{"description_$i"} eq "")
966       && ($form->{"partsgroup_$i"} eq "")) {
967     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
968     &check_form;
969
970   } else {
971
972     IR->retrieve_item(\%myconfig, \%$form);
973
974     my $rows = scalar @{ $form->{item_list} };
975
976     if ($rows) {
977       $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
978
979       if ($rows > 1) {
980
981         &select_item;
982         exit;
983
984       } else {
985
986         # override sellprice if there is one entered
987         my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
988
989         # ergaenzung fuer bug 736 Lieferanten-Rabatt auch in Einkaufsrechnungen vorbelegen jb
990         $form->{"discount_$i"} = $form->format_amount(\%myconfig,
991                                                       $form->{vendor_discount} * 100 );
992         map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g } qw(partnumber description unit);
993         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
994
995         $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
996
997         ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
998         my $dec_qty       = length $1;
999         my $decimalplaces = max 2, $dec_qty;
1000
1001         if ($sellprice) {
1002           $form->{"sellprice_$i"} = $sellprice;
1003         } else {
1004           # if there is an exchange rate adjust sellprice
1005           $form->{"sellprice_$i"} /= $exchangerate;
1006         }
1007
1008         my $amount                   = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
1009         $form->{creditremaining} -= $amount;
1010         $form->{"sellprice_$i"}   = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
1011         $form->{"qty_$i"}         = $form->format_amount(\%myconfig, $form->{"qty_$i"},       $dec_qty);
1012       }
1013
1014       &display_form;
1015
1016     } else {
1017
1018       # ok, so this is a new part
1019       # ask if it is a part or service item
1020
1021       if (   $form->{"partsgroup_$i"}
1022           && ($form->{"partsnumber_$i"} eq "")
1023           && ($form->{"description_$i"} eq "")) {
1024         $form->{rowcount}--;
1025         $form->{"discount_$i"} = "";
1026         display_form();
1027
1028       } else {
1029         $form->{"id_$i"}   = 0;
1030         new_item();
1031       }
1032     }
1033   }
1034   $main::lxdebug->leave_sub();
1035 }
1036
1037 sub storno {
1038   $main::lxdebug->enter_sub();
1039
1040   my $form     = $main::form;
1041   my %myconfig = %main::myconfig;
1042   my $locale   = $main::locale;
1043
1044   $main::auth->assert('vendor_invoice_edit');
1045
1046   if ($form->{storno}) {
1047     $form->error($locale->text('Cannot storno storno invoice!'));
1048   }
1049
1050   if (IS->has_storno(\%myconfig, $form, "ap")) {
1051     $form->error($locale->text("Invoice has already been storno'd!"));
1052   }
1053
1054   my $employee_id = $form->{employee_id};
1055   invoice_links();
1056   prepare_invoice();
1057   relink_accounts();
1058
1059   # Payments must not be recorded for the new storno invoice.
1060   $form->{paidaccounts} = 0;
1061   map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
1062
1063   # saving the history
1064   if(!exists $form->{addition} && $form->{id} ne "") {
1065     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1066     $form->{addition} = "CANCELED";
1067     $form->save_history($form->dbconnect(\%myconfig));
1068   }
1069   # /saving the history
1070
1071   $form->{storno_id} = $form->{id};
1072   $form->{storno} = 1;
1073   $form->{id} = "";
1074   $form->{invnumber} = "Storno zu " . $form->{invnumber};
1075   $form->{rowcount}++;
1076   $form->{employee_id} = $employee_id;
1077   post();
1078   $main::lxdebug->leave_sub();
1079
1080 }
1081
1082 sub use_as_template {
1083   $main::lxdebug->enter_sub();
1084
1085   my $form     = $main::form;
1086   my %myconfig = %main::myconfig;
1087
1088   $main::auth->assert('vendor_invoice_edit');
1089
1090   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);
1091   $form->{paidaccounts} = 1;
1092   $form->{rowcount}--;
1093   $form->{invdate} = $form->current_date(\%myconfig);
1094   &display_form;
1095
1096   $main::lxdebug->leave_sub();
1097 }
1098
1099 sub post_payment {
1100   $main::lxdebug->enter_sub();
1101
1102   my $form     = $main::form;
1103   my %myconfig = %main::myconfig;
1104   my $locale   = $main::locale;
1105
1106   $main::auth->assert('vendor_invoice_edit');
1107
1108   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1109   for my $i (1 .. $form->{paidaccounts}) {
1110     if ($form->{"paid_$i"}) {
1111       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1112
1113       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1114
1115       $form->error($locale->text('Cannot post payment for a closed period!'))
1116         if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
1117
1118       if ($form->{currency} ne $form->{defaultcurrency}) {
1119 #        $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid); # invdate isn't set here
1120         $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
1121       }
1122     }
1123   }
1124
1125   ($form->{AP})      = split /--/, $form->{AP};
1126   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
1127   if (IR->post_payment(\%myconfig, \%$form)){
1128         if (!exists $form->{addition} && $form->{id} ne "") {
1129                 # saving the history
1130       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1131                 $form->{addition} = "PAYMENT POSTED";
1132       $form->{what_done} = $form->{currency} . qq| | . $form->{paid} . qq| | . $locale->text("POSTED");
1133                 $form->save_history($form->dbconnect(\%myconfig));
1134                 # /saving the history
1135         }
1136
1137     $form->redirect($locale->text('Payment posted!'));
1138   }
1139
1140   $form->error($locale->text('Cannot post payment!'));
1141
1142   $main::lxdebug->leave_sub();
1143 }
1144
1145 sub _max_datepaid {
1146   my $form  =  $main::form;
1147
1148   my @dates = sort { $b->[1] cmp $a->[1] }
1149               map  { [ $_, $main::locale->reformat_date(\%main::myconfig, $_, 'yyyy-mm-dd') ] }
1150               grep { $_ }
1151               map  { $form->{"datepaid_${_}"} }
1152               (1..$form->{rowcount});
1153
1154   return @dates ? $dates[0]->[0] : undef;
1155 }
1156
1157
1158 sub post {
1159   $main::lxdebug->enter_sub();
1160
1161   my $form     = $main::form;
1162   my %myconfig = %main::myconfig;
1163   my $locale   = $main::locale;
1164
1165   $main::auth->assert('vendor_invoice_edit');
1166
1167   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1168
1169   $form->isblank("invdate",   $locale->text('Invoice Date missing!'));
1170   $form->isblank("vendor",    $locale->text('Vendor missing!'));
1171   $form->isblank("invnumber", $locale->text('Invnumber missing!'));
1172
1173   $form->{invnumber} =~ s/^\s*//g;
1174   $form->{invnumber} =~ s/\s*$//g;
1175
1176   # if the vendor changed get new values
1177   if (&check_name('vendor')) {
1178     &update;
1179     exit;
1180   }
1181
1182   &validate_items;
1183
1184   my $closedto     = $form->datetonum($form->{closedto}, \%myconfig);
1185   my $invdate      = $form->datetonum($form->{invdate},  \%myconfig);
1186   my $max_datepaid = _max_datepaid();
1187
1188   $form->error($locale->text('Cannot post invoice for a closed period!')) if $max_datepaid && $form->date_closed($max_datepaid, \%myconfig);
1189
1190   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1191     if ($form->{currency} ne $form->{defaultcurrency});
1192
1193   my $i;
1194   for $i (1 .. $form->{paidaccounts}) {
1195     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
1196       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1197
1198       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1199
1200       $form->error($locale->text('Cannot post payment for a closed period!'))
1201         if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
1202
1203       if ($form->{currency} ne $form->{defaultcurrency}) {
1204         $form->{"exchangerate_$i"} = $form->{exchangerate}
1205           if ($invdate == $datepaid);
1206         $form->isblank("exchangerate_$i",
1207                        $locale->text('Exchangerate for payment missing!'));
1208       }
1209     }
1210   }
1211
1212   ($form->{AP})      = split /--/, $form->{AP};
1213   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
1214   $form->{storno}  ||= 0;
1215
1216   $form->{id} = 0 if $form->{postasnew};
1217
1218
1219   relink_accounts();
1220   if (IR->post_invoice(\%myconfig, \%$form)){
1221         # saving the history
1222         if(!exists $form->{addition} && $form->{id} ne "") {
1223       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1224       $form->{addition} = "POSTED";
1225                 #$form->{what_done} = $locale->text("Rechnungsnummer") . qq| | . $form->{invnumber};
1226                 $form->save_history($form->dbconnect(\%myconfig));
1227         }
1228         # /saving the history
1229     remove_draft() if $form->{remove_draft};
1230         $form->redirect(  $locale->text('Invoice')
1231                   . " $form->{invnumber} "
1232                   . $locale->text('posted!'));
1233   }
1234   $form->error($locale->text('Cannot post invoice!'));
1235
1236   $main::lxdebug->leave_sub();
1237 }
1238
1239 sub delete {
1240   $main::lxdebug->enter_sub();
1241
1242   my $form     = $main::form;
1243   my $locale   = $main::locale;
1244
1245   $main::auth->assert('vendor_invoice_edit');
1246
1247   $form->header;
1248   print qq|
1249 <body>
1250
1251 <form method=post action=$form->{script}>
1252 |;
1253
1254   # delete action variable
1255   map { delete $form->{$_} } qw(action header);
1256
1257   foreach my $key (keys %$form) {
1258     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1259     $form->{$key} =~ s/\"/&quot;/g;
1260     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1261   }
1262
1263   print qq|
1264 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1265
1266 <h4>|
1267     . $locale->text('Are you sure you want to delete Invoice Number')
1268     . qq| $form->{invnumber}</h4>
1269 <p>
1270 <input name=action class=submit type=submit value="|
1271     . $locale->text('Yes') . qq|">
1272 </form>
1273 |;
1274
1275   $main::lxdebug->leave_sub();
1276 }
1277
1278 sub yes {
1279   $main::lxdebug->enter_sub();
1280
1281   my $form     = $main::form;
1282   my %myconfig = %main::myconfig;
1283   my $locale   = $main::locale;
1284
1285   $main::auth->assert('vendor_invoice_edit');
1286
1287   if (IR->delete_invoice(\%myconfig, \%$form)) {
1288     # saving the history
1289     if(!exists $form->{addition}) {
1290       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1291           $form->{addition} = "DELETED";
1292           $form->save_history($form->dbconnect(\%myconfig));
1293     }
1294     # /saving the history
1295     $form->redirect($locale->text('Invoice deleted!'));
1296   }
1297   $form->error($locale->text('Cannot delete invoice!'));
1298
1299   $main::lxdebug->leave_sub();
1300 }
1301
1302 sub set_duedate_vendor {
1303   $main::lxdebug->enter_sub();
1304
1305   my $form     = $main::form;
1306
1307   print $form->ajax_response_header(), IR->get_duedate('vendor_id' => $form->{vendor_id},
1308                                                        'invdate'   => $form->{invdate},
1309                                                        'default'   => $form->{old_duedate});
1310
1311   $main::lxdebug->leave_sub();
1312 }