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