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