is form_footer: weitere exportierte variablen
[kivitendo-erp.git] / bin / mozilla / is.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 invoicing module
31 #
32 #======================================================================
33
34 use SL::FU;
35 use SL::IS;
36 use SL::PE;
37 use Data::Dumper;
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/drafts.pl";
44
45 use strict;
46
47 my $edit;
48 my $payment;
49 my $print_post;
50
51 1;
52
53 # end of main
54
55 sub add {
56   $main::lxdebug->enter_sub();
57
58   my $form     = $main::form;
59   my $locale   = $main::locale;
60
61   $main::auth->assert('invoice_edit');
62
63   return $main::lxdebug->leave_sub() if (load_draft_maybe());
64
65   if ($form->{type} eq "credit_note") {
66     $form->{title} = $locale->text('Add Credit Note');
67
68     if ($form->{storno}) {
69       $form->{title} = $locale->text('Add Storno Credit Note');
70     }
71   } else {
72     $form->{title} = $locale->text('Add Sales Invoice');
73
74   }
75
76
77   $form->{callback} = "$form->{script}?action=add&type=$form->{type}" unless $form->{callback};
78
79   $form->{jsscript} = "date";
80
81   &invoice_links;
82   &prepare_invoice;
83   &display_form;
84
85   $main::lxdebug->leave_sub();
86 }
87
88 sub edit {
89   $main::lxdebug->enter_sub();
90
91   my $form     = $main::form;
92   my $locale   = $main::locale;
93
94   $main::auth->assert('invoice_edit');
95
96   # show history button
97   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
98   #/show hhistory button
99
100   if ($form->{type} eq "credit_note") {
101     $form->{title} = $locale->text('Edit Credit Note');
102     $form->{title} = $locale->text('Edit Storno Credit Note') if $form->{storno};
103   } else {
104     $form->{title} = $locale->text('Edit Sales Invoice');
105     $form->{title} = $locale->text('Edit Storno Invoice')     if $form->{storno};
106   }
107
108   my ($language_id, $printer_id);
109   if ($form->{print_and_post}) {
110     $form->{action}   = "print";
111     $form->{resubmit} = 1;
112     $language_id = $form->{language_id};
113     $printer_id = $form->{printer_id};
114   }
115   &invoice_links;
116   &prepare_invoice;
117   if ($form->{print_and_post}) {
118     $form->{language_id} = $language_id;
119     $form->{printer_id} = $printer_id;
120   }
121
122   &display_form;
123
124   $main::lxdebug->leave_sub();
125 }
126
127 sub invoice_links {
128   $main::lxdebug->enter_sub();
129
130   my $form     = $main::form;
131   my %myconfig = %main::myconfig;
132
133   $main::auth->assert('invoice_edit');
134
135   $form->{vc} = 'customer';
136
137   # create links
138   $form->{webdav}   = $main::webdav;
139   $form->{lizenzen} = $main::lizenzen;
140
141   $form->create_links("AR", \%myconfig, "customer");
142
143   if ($form->{all_customer}) {
144     unless ($form->{customer_id}) {
145       $form->{customer_id} = $form->{all_customer}->[0]->{id};
146       $form->{salesman_id} = $form->{all_customer}->[0]->{salesman_id};
147     }
148   }
149
150   $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded cp_id intnotes id shipto_id));
151
152   IS->get_customer(\%myconfig, \%$form);
153
154   #quote all_customer Bug 133
155   foreach my $ref (@{ $form->{all_customer} }) {
156     $ref->{name} = $form->quote($ref->{name});
157   }
158
159   $form->restore_vars(qw(id));
160
161   IS->retrieve_invoice(\%myconfig, \%$form);
162   $form->restore_vars(qw(payment_id language_id taxzone_id intnotes cp_id shipto_id));
163   $form->restore_vars(qw(taxincluded)) if $form->{id};
164   $form->restore_vars(qw(salesman_id)) if $main::editing;
165
166   # build vendor/customer drop down comatibility... don't ask
167   if (@{ $form->{"all_customer"} }) {
168     $form->{"selectcustomer"} = 1;
169     $form->{customer}         = qq|$form->{customer}--$form->{"customer_id"}|;
170   }
171
172   $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
173   $form->{selectcustomer} = 1;
174
175   # departments
176 #  if ($form->{all_departments}) {
177 #    $form->{selectdepartment} = "<option>\n";
178 #    $form->{department}       = "$form->{department}--$form->{department_id}";
179 #
180 #    map {
181 #      $form->{selectdepartment} .= "<option>$_->{description}--$_->{id}</option>\n"
182 #    } @{ $form->{all_departments} };
183 #  }
184
185   $form->{employee} = "$form->{employee}--$form->{employee_id}";
186
187   # forex
188   $form->{forex} = $form->{exchangerate};
189   my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
190
191   foreach my $key (keys %{ $form->{AR_links} }) {
192     foreach my $ref (@{ $form->{AR_links}{$key} }) {
193       $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}</option>\n";
194     }
195
196     if ($key eq "AR_paid") {
197       next unless $form->{acc_trans}{$key};
198       for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
199         $form->{"AR_paid_$i"}      = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
200
201         # reverse paid
202         $form->{"paid_$i"}         = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
203         $form->{"datepaid_$i"}     = $form->{acc_trans}{$key}->[$i - 1]->{transdate};
204         $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
205         $form->{"forex_$i"}        = $form->{"exchangerate_$i"};
206         $form->{"source_$i"}       = $form->{acc_trans}{$key}->[$i - 1]->{source};
207         $form->{"memo_$i"}         = $form->{acc_trans}{$key}->[$i - 1]->{memo};
208
209         $form->{paidaccounts} = $i;
210       }
211     } else {
212       $form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
213     }
214   }
215
216   $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
217
218   $form->{AR} = $form->{AR_1} unless $form->{id};
219
220   $form->{locked} = ($form->datetonum($form->{invdate},  \%myconfig)
221                   <= $form->datetonum($form->{closedto}, \%myconfig));
222
223   $main::lxdebug->leave_sub();
224 }
225
226 sub prepare_invoice {
227   $main::lxdebug->enter_sub();
228
229   my $form     = $main::form;
230   my %myconfig = %main::myconfig;
231
232   $main::auth->assert('invoice_edit');
233
234   if ($form->{type} eq "credit_note") {
235     $form->{type}     = "credit_note";
236     $form->{formname} = "credit_note";
237   } else {
238     $form->{type}     = "invoice";
239     $form->{formname} = "invoice";
240   }
241
242   if ($form->{id}) {
243
244     my $i = 0;
245
246     foreach my $ref (@{ $form->{invoice_details} }) {
247       $i++;
248
249       map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
250
251       $form->{"discount_$i"}   = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
252       my ($dec)                = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
253       $dec                     = length $dec;
254       my $decimalplaces        = ($dec > 2) ? $dec : 2;
255
256       $form->{"sellprice_$i"}  = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
257       (my $dec_qty)            = ($form->{"qty_$i"} =~ /\.(\d+)/);
258       $dec_qty                 = length $dec_qty;
259
260       $form->{"qty_$i"}        = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
261
262       $form->{rowcount}        = $i;
263
264     }
265
266     # get pricegroups for parts
267     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
268     set_pricegroup($_) for 1 .. $form->{rowcount};
269   }
270   $main::lxdebug->leave_sub();
271 }
272
273 sub form_header {
274   $main::lxdebug->enter_sub();
275
276   my $form     = $main::form;
277   my %myconfig = %main::myconfig;
278   my $locale   = $main::locale;
279   my $cgi      = $main::cgi;
280
281   $main::auth->assert('invoice_edit');
282
283   our %TMPL_VAR = ();
284   my @custom_hiddens;
285
286   $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
287   $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
288
289   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
290   $form->{radier}          = ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
291
292   $payment = qq|<option value=""></option>|;
293   foreach my $item (@{ $form->{payment_terms} }) {
294     if ($form->{payment_id} eq $item->{id}) {
295       $payment .= qq|<option value="$item->{id}" selected>$item->{description}</option>|;
296     } else {
297       $payment .= qq|<option value="$item->{id}">$item->{description}</option>|;
298     }
299   }
300
301   my $set_duedate_url = "$form->{script}?action=set_duedate";
302
303   push @ { $form->{AJAX} }, new CGI::Ajax( 'set_duedate' => $set_duedate_url );
304
305   my @old_project_ids = ($form->{"globalproject_id"});
306   map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
307
308   $form->get_lists("contacts"      => "ALL_CONTACTS",
309                    "shipto"        => "ALL_SHIPTO",
310                    "projects"      => { "key"    => "ALL_PROJECTS",
311                                         "all"    => 0,
312                                         "old_id" => \@old_project_ids },
313                    "employees"     => "ALL_EMPLOYEES",
314                    "salesmen"      => "ALL_SALESMEN",
315                    "taxzones"      => "ALL_TAXZONES",
316                    "currencies"    => "ALL_CURRENCIES",
317                    "customers"     => "ALL_CUSTOMERS",
318                    "price_factors" => "ALL_PRICE_FACTORS");
319
320   $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
321   $TMPL_VAR{shipto_labels}         = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
322   $TMPL_VAR{contact_labels}        = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
323   $TMPL_VAR{department_labels}     = sub { "$_[0]->{description}--$_[0]->{id}" };
324
325   # customer
326   $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
327   $TMPL_VAR{vclimit} = $myconfig{vclimit};
328   $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('customer', '', 0, 0)";
329   push @custom_hiddens, "customer_id";
330   push @custom_hiddens, "oldcustomer";
331   push @custom_hiddens, "selectcustomer";
332
333   # currencies and exchangerate
334   my @values = map { $_       } @{ $form->{ALL_CURRENCIES} };
335   my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
336   $form->{currency}            = $form->{defaultcurrency} unless $form->{currency};
337   $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
338   $TMPL_VAR{currencies}        = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
339                                                       '-values' => \@values, '-labels' => \%labels)) if scalar @values;
340   push @custom_hiddens, "forex";
341   push @custom_hiddens, "exchangerate" if $form->{forex};
342
343   $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
344   $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
345
346   $form->{fokus} = "invoice.customer";
347
348   my $follow_up_vc         =  $form->{customer};
349   $follow_up_vc            =~ s/--\d*\s*$//;
350   $TMPL_VAR{customer_name} = $follow_up_vc;
351
352 # set option selected
353   foreach my $item (qw(AR)) {
354     $form->{"select$item"} =~ s/ selected//;
355     $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
356   }
357
358   $TMPL_VAR{is_type_credit_note} = $form->{type}   eq "credit_note";
359   $TMPL_VAR{is_format_html}      = $form->{format} eq 'html';
360   $TMPL_VAR{dateformat}          = $myconfig{dateformat};
361   $TMPL_VAR{numberformat}        = $myconfig{numberformat};
362
363   # hiddens
364   $TMPL_VAR{HIDDENS} = [qw(
365     id action type media format queued printed emailed title vc discount
366     title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
367     max_dunning_level dunning_amount
368     shiptoname shiptostreet shiptozipcode shiptocity shiptocountry  shiptocontact shiptophone shiptofax
369     shiptoemail shiptodepartment_1 shiptodepartment_2 message email subject cc bcc taxaccounts cursor_fokus
370     convert_from_do_ids convert_from_oe_ids
371   ), @custom_hiddens,
372   map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
373
374   $form->{jsscript} = 1;
375   $form->header();
376
377   print $form->parse_html_template("is/form_header", \%TMPL_VAR);
378
379   $main::lxdebug->leave_sub();
380 }
381
382 sub form_footer {
383   $main::lxdebug->enter_sub();
384
385   my $form     = $main::form;
386   my %myconfig = %main::myconfig;
387   my $locale   = $main::locale;
388
389   $main::auth->assert('invoice_edit');
390
391   $form->{invtotal}    = $form->{invsubtotal};
392   $form->{oldinvtotal} = $form->{invtotal};
393
394   # note rows
395   $form->{rows} = max 2,
396     $form->numtextrows($form->{notes},    26, 8),
397     $form->numtextrows($form->{intnotes}, 35, 8);
398
399
400   # tax, total and subtotal calculations
401   my ($tax, $subtotal);
402   $form->{taxaccounts_array} = [ split / /, $form->{taxaccounts} ];
403
404   foreach my $item (@{ $form->{taxaccounts_array} }) {
405     if ($form->{"${item}_base"}) {
406       if ($form->{taxincluded}) {
407         $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"}
408                                                                                  / (1 + $form->{"${item}_rate"})), 2);
409         $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
410       } else {
411         $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
412         $form->{invtotal} += $form->{"${item}_total"};
413       }
414     }
415   }
416
417   # unfortunately locales doesn't support extended syntax
418   if ($form->{id}) {
419     my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
420     if (@{ $follow_ups} ) {
421       $form->{follow_up_text} = $locale->text("There are #1 unfinished follow-ups of which #2 are due.",
422                                                scalar(@{ $follow_ups }),
423                                                sum map { $_->{due} * 1 } @{ $follow_ups });
424     }
425   }
426
427   # payments
428   my $totalpaid = 0;
429   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
430   $form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];
431
432   for my $i (1 .. $form->{paidaccounts}) {
433     $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
434     $form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
435     $totalpaid += $form->{"paid_$i"};
436   }
437
438   print $form->parse_html_template('is/form_footer', {
439     is_type_credit_note => ($form->{type} eq "credit_note"),
440     totalpaid           => $totalpaid,
441     paid_missing        => $form->{invtotal} - $totalpaid,
442     print_options       => print_options(inline => 1),
443     show_storno         => $form->{id} && !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar") && !$totalpaid,
444     show_delete         => ($form->current_date(\%myconfig) eq $form->{gldate}),
445   });
446 ##print $form->parse_html_template('is/_payments'); # parser
447
448
449
450 #  print qq|
451 #  <tr>
452 #    <td>
453 #      <table width="100%">
454 #       <tr valign="bottom">
455 #         <td>
456 #           <table>
457 #             <tr>
458 #               <th align="left">| . $locale->text('Notes') . qq|</th>
459 #               <th align="left">| . $locale->text('Internal Notes') . qq|</th>
460 #                <th align="right">| . $locale->text('Payment Terms') . qq|</th>
461 #             </tr>
462 #             <tr valign="top">
463 #               <td>$notes</td>
464 #               <td>$intnotes</td>
465 #                <td><select name="payment_id" onChange="if (this.value) set_duedate(['payment_id__' + this.value, 'invdate__' + invdate.value],['duedate'])">$payment
466 #                </select></td>
467 #             </tr>
468 #        $follow_ups_block
469 #           </table>
470 #         </td>
471 #          <td>
472 #            <table>
473 #            <tr>
474 #              <th  align=left>| . $locale->text('Ertrag') . qq|</th>
475 #              <td>| .  $form->format_amount(\%myconfig, $form->{marge_total}, 2, 0) . qq|</td>
476 #            </tr>
477 #            <tr>
478 #              <th  align=left>| . $locale->text('Ertrag prozentual') . qq|</th>
479 #              <td>| .  $form->format_amount(\%myconfig, $form->{marge_percent}, 2, 0) . qq| %</td>
480 #            </tr>
481 #            <input type=hidden name="marge_total" value="$form->{"marge_total"}">
482 #            <input type=hidden name="marge_percent" value="$form->{"marge_percent"}">
483 #            </table>
484 #          </td>
485 #         <td align="right">
486 #           $taxincluded
487 #           <table>
488 #             $subtotal
489 #             $tax
490 #             <tr>
491 #               <th align="right">| . $locale->text('Total') . qq|</th>
492 #               <td align="right">$form->{invtotal}</td>
493 #             </tr>
494 #           </table>
495 #         </td>
496 #       </tr>
497 #      </table>
498 #    </td>
499 #  </tr>
500 #|;
501 #  my $webdav_list;
502 #  if ($main::webdav) {
503 #    $webdav_list = qq|
504 #  <tr>
505 #    <td><hr size="3" noshade></td>
506 #  </tr>
507 #  <tr>
508 #    <th class="listtop" align="left">Dokumente im Webdav-Repository</th>
509 #  </tr>
510 #    <table width="100%">
511 #      <td align="left" width="30%"><b>Dateiname</b></td>
512 #      <td align="left" width="70%"><b>Webdavlink</b></td>
513 #|;
514 #    foreach my $file (@{ $form->{WEBDAV} }) {
515 #      $webdav_list .= qq|
516 #      <tr>
517 #        <td align="left">$file->{name}</td>
518 #        <td align="left"><a href="$file->{link}">$file->{type}</a></td>
519 #      </tr>
520 #|;
521 #    }
522 #    $webdav_list .= qq|
523 #    </table>
524 #  </tr>
525 #|;
526 #
527 #    print $webdav_list;
528 #  }
529 #if ($form->{type} eq "credit_note") {
530 #  print qq|
531 #  <tr>
532 #    <td>
533 #      <table width="100%">
534 #       <tr class="listheading">
535 #         <th colspan="6" class="listheading">|
536 #    . $locale->text('Payments') . qq|</th>
537 #       </tr>
538 #|;
539 #} else {
540 #  print qq|
541 #  <tr>
542 #    <td>
543 #      <table width="100%">
544 #       <tr class="listheading">
545 #         <th colspan="6" class="listheading">|
546 #    . $locale->text('Incoming Payments') . qq|</th>
547 #       </tr>
548 #|;
549 #}
550 #
551 #  my @column_index;
552 #  if ($form->{currency} eq $form->{defaultcurrency}) {
553 #    @column_index = qw(datepaid source memo paid AR_paid);
554 #  } else {
555 #    @column_index = qw(datepaid source memo paid exchangerate AR_paid);
556 #  }
557 #
558 #  my %column_data;
559 #  $column_data{datepaid}     = "<th>" . $locale->text('Date') . "</th>";
560 #  $column_data{paid}         = "<th>" . $locale->text('Amount') . "</th>";
561 #  $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
562 #  $column_data{AR_paid}      = "<th>" . $locale->text('Account') . "</th>";
563 #  $column_data{source}       = "<th>" . $locale->text('Source') . "</th>";
564 #  $column_data{memo}         = "<th>" . $locale->text('Memo') . "</th>";
565 #
566 #  print "
567 #       <tr>
568 #";
569 #  map { print "$column_data{$_}\n" } @column_index;
570 #  print "
571 #        </tr>
572 #";
573 #
574 #  my @triggers  = ();
575 #  my $totalpaid = 0;
576 #
577 #  $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
578 #  for my $i (1 .. $form->{paidaccounts}) {
579 #
580 #    print "
581 #        <tr>\n";
582 #
583 #    $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
584 #    $form->{"selectAR_paid_$i"} =~
585 #      s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
586 #
587 #    # format amounts
588 #    $totalpaid += $form->{"paid_$i"};
589 #    if ($form->{"paid_$i"}) {
590 #      $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
591 #    }
592 #    $form->{"exchangerate_$i"} = $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
593 #
594 #    if ($form->{"exchangerate_$i"} == 0) {
595 #      $form->{"exchangerate_$i"} = "";
596 #    }
597 #    my $exchangerate = qq|&nbsp;|;
598 #    if ($form->{currency} ne $form->{defaultcurrency}) {
599 #      if ($form->{"forex_$i"}) {
600 #        $exchangerate = qq|<input type="hidden" name="exchangerate_$i" value="$form->{"exchangerate_$i"}">$form->{"exchangerate_$i"}|;
601 #      } else {
602 #        $exchangerate = qq|<input name="exchangerate_$i" size="10" value="$form->{"exchangerate_$i"}">|;
603 #      }
604 #    }
605 #
606 #    $exchangerate .= qq|<input type="hidden" name="forex_$i" value="$form->{"forex_$i"}">|;
607 #
608 #    $column_data{"paid_$i"} =
609 #      qq|<td align="center"><input name="paid_$i" size="11" value="$form->{"paid_$i"}" onBlur=\"check_right_number_format(this)\"></td>|;
610 #    $column_data{"exchangerate_$i"} = qq|<td align="center">$exchangerate</td>|;
611 #    $column_data{"AR_paid_$i"}      =
612 #      qq|<td align="center"><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
613 #    $column_data{"datepaid_$i"} =
614 #      qq|<td align="center"><input id="datepaid_$i" name="datepaid_$i"  size="11" title="$myconfig{dateformat}" value="$form->{"datepaid_$i"}" onBlur=\"check_right_date_format(this)\">
615 #         <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
616 #    $column_data{"source_$i"} =
617 #      qq|<td align=center><input name="source_$i" size="11" value="$form->{"source_$i"}"></td>|;
618 #    $column_data{"memo_$i"} =
619 #      qq|<td align="center"><input name="memo_$i" size="11" value="$form->{"memo_$i"}"></td>|;
620 #
621 #    map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
622 #    print "
623 #        </tr>\n";
624 #    push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
625 #  }
626 #
627 #  my $paid_missing = $form->{oldinvtotal} - $totalpaid;
628 #
629 #  print qq|
630 #    <tr>
631 #      <td></td>
632 #      <td></td>
633 #      <td align="center">| . $locale->text('Total') . qq|</td>
634 #      <td align="center">| . H($form->format_amount(\%myconfig, $totalpaid, 2)) . qq|</td>
635 #    </tr>
636 #    <tr>
637 #      <td></td>
638 #      <td></td>
639 #      <td align="center">| . $locale->text('Missing amount') . qq|</td>
640 #      <td align="center">| . H($form->format_amount(\%myconfig, $paid_missing, 2)) . qq|</td>
641 #    </tr>
642 #|;
643 #
644 #  map({ print($cgi->hidden("-name" => $_, "-value" => $form->{$_})); } qw(paidaccounts selectAR_paid oldinvtotal));
645 #  print qq|<input type="hidden" name="oldtotalpaid" value="$totalpaid">
646 #    </table>
647 #    </td>
648 #  </tr>
649 #  <tr>
650 #    <td><hr size="3" noshade></td>
651 #  </tr>
652 #  <tr>
653 #    <td>
654 #|;
655 #
656 #  print_options();
657 #
658 #  print qq|
659 #    </td>
660 #  </tr>
661 #</table>
662 #|;
663 #
664 #  my $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
665 #  my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
666 #
667 #  if ($form->{id}) {
668 #    my $show_storno = !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar") && (($totalpaid == 0) || ($totalpaid eq ""));
669 #
670 #    print qq|
671 #    <input class="submit" type="submit" accesskey="u" name="action" id="update_button" value="| . $locale->text('Update') . qq|">
672 #    <input class="submit" type="submit" name="action" value="| . $locale->text('Ship to') . qq|">
673 #    <input class="submit" type="submit" name="action" value="| . $locale->text('Print') . qq|">
674 #    <input class="submit" type="submit" name="action" value="| . $locale->text('E-mail') . qq|"> |;
675 #    print qq|<input class="submit" type="submit" name="action" value="| . $locale->text('Storno') . qq|"> | if ($show_storno);
676 #    print qq|<input class="submit" type="submit" name="action" value="| . $locale->text('Post Payment') . qq|"> |;
677 #    print qq|<input class="submit" type="submit" name="action" value="| . $locale->text('Use As Template') . qq|"> |;
678 #    if ($form->{id} && !($form->{type} eq "credit_note")) {
679 #      print qq| <input class="submit" type="submit" name="action" value="| . $locale->text('Credit Note') . qq|"> |;
680 #    }
681 #    if ($form->{radier}) {
682 #      print qq| <input class="submit" type="submit" name="action" value="| . $locale->text('Delete') . qq|"> |;
683 #    }
684 #
685 #
686 #    if ($invdate > $closedto) {
687 #      print qq| <input class="submit" type="submit" name="action" value="| . $locale->text('Order') . qq|"> |;
688 #    }
689 #
690 #    print qq| <input type="button" class="submit" onclick="follow_up_window()" value="| . $locale->text('Follow-Up') . qq|">|;
691 #
692 #  } else {
693 #    if ($invdate > $closedto) {
694 #      print qq|
695 #      <input class="submit" type="submit" name="action" id="update_button" value="| . $locale->text('Update') . qq|">
696 #      <input class="submit" type="submit" name="action" value="| . $locale->text('Ship to') . qq|">
697 #      <input class="submit" type="submit" name="action" value="| . $locale->text('Preview') . qq|">
698 #      <input class="submit" type="submit" name="action" value="| . $locale->text('E-mail') . qq|">
699 #      <input class="submit" type="submit" name="action" value="| . $locale->text('Print and Post') . qq|">
700 #      <input class="submit" type="submit" name="action" value="| . $locale->text('Post') . qq|"> | .
701 #        NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'), '-class' => 'submit'));
702 #    }
703 #  }
704 #
705 #  # button for saving history
706 #  if($form->{id} ne "") {
707 #    print qq|
708 #         <input type="button" class="submit" onclick="set_history_window(|
709 #         . Q($form->{id})
710 #         . qq|);" name="history" id="history" value="|
711 #         . $locale->text('history')
712 #         . qq|"> |;
713 #  }
714 #  # /button for saving history
715 #
716 #  # mark_as_paid button
717 #  if($form->{id} ne "") {
718 #    print qq|<input type="submit" class="submit" name="action" value="| . $locale->text('mark as paid') . qq|">|;
719 #  }
720 #  # /mark_as_paid button
721 #  print $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
722 #    qq|
723 #
724 #<input type="hidden" name="rowcount" value="$form->{rowcount}">
725 #| .
726 #$cgi->hidden("-name" => "callback", "-value" => $form->{callback})
727 #. $cgi->hidden('-name' => 'draft_id', '-default' => [$form->{draft_id}])
728 #. $cgi->hidden('-name' => 'draft_description', '-default' => [$form->{draft_description}])
729 #. $cgi->hidden('-name' => 'customer_discount', '-value' => [$form->{customer_discount}])
730 #. qq|
731 #</form>
732 #
733 #</body>
734 #
735 # </html>
736 #|;
737
738   $main::lxdebug->leave_sub();
739 }
740
741 sub mark_as_paid {
742   $main::lxdebug->enter_sub();
743
744   my $form     = $main::form;
745   my %myconfig = %main::myconfig;
746
747   $main::auth->assert('invoice_edit');
748
749   &mark_as_paid_common(\%myconfig,"ar");
750
751   $main::lxdebug->leave_sub();
752 }
753
754 sub update {
755   $main::lxdebug->enter_sub();
756
757   my $form     = $main::form;
758   my %myconfig = %main::myconfig;
759
760   $main::auth->assert('invoice_edit');
761
762   my ($recursive_call) = shift;
763
764   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining) unless $recursive_call;
765
766   $form->{print_and_post} = 0         if $form->{second_run};
767   my $taxincluded            = "checked" if $form->{taxincluded};
768   $form->{update} = 1;
769
770   &check_name("customer");
771
772   $form->{taxincluded} ||= $taxincluded;
773
774   $form->{forex}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
775   $form->{exchangerate} = $form->{forex} if $form->{forex};
776
777   for my $i (1 .. $form->{paidaccounts}) {
778     next unless $form->{"paid_$i"};
779     map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
780     $form->{"forex_$i"}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
781     $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
782   }
783
784   my $i            = $form->{rowcount};
785   my $exchangerate = $form->{exchangerate} || 1;
786
787   # if last row empty, check the form otherwise retrieve new item
788   if (   ($form->{"partnumber_$i"} eq "")
789       && ($form->{"description_$i"} eq "")
790       && ($form->{"partsgroup_$i"}  eq "")) {
791
792     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
793     &check_form;
794
795   } else {
796
797     IS->retrieve_item(\%myconfig, \%$form);
798
799     my $rows = scalar @{ $form->{item_list} };
800
801     $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{customer_discount} * 100);
802
803     if ($rows) {
804       $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
805
806       if ($rows > 1) {
807
808         &select_item;
809         exit;
810
811       } else {
812
813         my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
814
815         map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g } qw(partnumber description unit);
816         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
817
818         $form->{payment_id}    = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
819         $form->{"discount_$i"} = 0                             if $form->{"not_discountable_$i"};
820
821         $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
822
823         ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
824         my $decimalplaces = max 2, length $1;
825
826         if ($sellprice) {
827           $form->{"sellprice_$i"} = $sellprice;
828         } else {
829           # if there is an exchange rate adjust sellprice
830           $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
831           $form->{"sellprice_$i"} /= $exchangerate;
832         }
833
834         $form->{"listprice_$i"} /= $exchangerate;
835
836         my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
837         map { $form->{"${_}_base"} = 0 }                                 split / /, $form->{taxaccounts};
838         map { $form->{"${_}_base"} += $amount }                          split / /, $form->{"taxaccounts_$i"};
839         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
840
841         $form->{creditremaining} -= $amount;
842
843         map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice listprice);
844
845         $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
846
847         if ($main::lizenzen) {
848           if ($form->{"inventory_accno_$i"} ne "") {
849             $form->{"lizenzen_$i"} = qq|<option></option>|;
850             foreach my $item (@{ $form->{LIZENZEN}{ $form->{"id_$i"} } }) {
851               $form->{"lizenzen_$i"} .= qq|<option value="$item->{"id"}">$item->{"licensenumber"}</option>|;
852             }
853             $form->{"lizenzen_$i"} .= qq|<option value=-1>Neue Lizenz</option>|;
854           }
855         }
856
857         # get pricegroups for parts
858         IS->get_pricegroups_for_parts(\%myconfig, \%$form);
859
860         # build up html code for prices_$i
861         &set_pricegroup($i);
862       }
863
864       &display_form;
865
866     } else {
867
868       # ok, so this is a new part
869       # ask if it is a part or service item
870
871       if (   $form->{"partsgroup_$i"}
872           && ($form->{"partsnumber_$i"} eq "")
873           && ($form->{"description_$i"} eq "")) {
874         $form->{rowcount}--;
875         $form->{"discount_$i"} = "";
876         display_form();
877
878       } else {
879         $form->{"id_$i"}   = 0;
880         new_item();
881       }
882     }
883   }
884   $main::lxdebug->leave_sub();
885 }
886
887 sub post_payment {
888   $main::lxdebug->enter_sub();
889
890   my $form     = $main::form;
891   my %myconfig = %main::myconfig;
892   my $locale   = $main::locale;
893
894   $main::auth->assert('invoice_edit');
895
896   our $invdate;
897
898   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
899   for my $i (1 .. $form->{paidaccounts}) {
900     if ($form->{"paid_$i"}) {
901       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
902
903       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
904
905       $form->error($locale->text('Cannot post payment for a closed period!'))
906         if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
907
908       if ($form->{currency} ne $form->{defaultcurrency}) {
909         $form->{"exchangerate_$i"} = $form->{exchangerate}
910           if ($invdate == $datepaid);
911         $form->isblank("exchangerate_$i",
912                        $locale->text('Exchangerate for payment missing!'));
913       }
914     }
915   }
916
917   ($form->{AR})      = split /--/, $form->{AR};
918   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
919   relink_accounts();
920   $form->redirect($locale->text('Payment posted!'))
921       if (IS->post_payment(\%myconfig, \%$form));
922     $form->error($locale->text('Cannot post payment!'));
923
924
925   $main::lxdebug->leave_sub();
926 }
927
928 sub post {
929   $main::lxdebug->enter_sub();
930
931   my $form     = $main::form;
932   my %myconfig = %main::myconfig;
933   my $locale   = $main::locale;
934
935   $main::auth->assert('invoice_edit');
936
937   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
938   $form->isblank("invdate",  $locale->text('Invoice Date missing!'));
939   $form->isblank("customer", $locale->text('Customer missing!'));
940   $form->error($locale->text('Cannot post invoice for a closed period!'))
941         if ($form->date_closed($form->{"invdate"}, \%myconfig));
942
943   $form->{invnumber} =~ s/^\s*//g;
944   $form->{invnumber} =~ s/\s*$//g;
945
946   # if oldcustomer ne customer redo form
947   if (&check_name('customer')) {
948     &update;
949     exit;
950   }
951   if ($form->{second_run}) {
952     $form->{print_and_post} = 0;
953   }
954
955   &validate_items;
956
957   my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
958   my $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
959
960   $form->error($locale->text('Cannot post invoice for a closed period!'))
961     if ($invdate <= $closedto);
962
963   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
964     if ($form->{currency} ne $form->{defaultcurrency});
965
966   for my $i (1 .. $form->{paidaccounts}) {
967     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
968       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
969
970       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
971
972       $form->error($locale->text('Cannot post payment for a closed period!'))
973         if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
974
975       if ($form->{currency} ne $form->{defaultcurrency}) {
976         $form->{"exchangerate_$i"} = $form->{exchangerate}
977           if ($invdate == $datepaid);
978         $form->isblank("exchangerate_$i",
979                        $locale->text('Exchangerate for payment missing!'));
980       }
981     }
982   }
983
984   ($form->{AR})        = split /--/, $form->{AR};
985   ($form->{AR_paid})   = split /--/, $form->{AR_paid};
986   $form->{storno}    ||= 0;
987
988   $form->{label} = $locale->text('Invoice');
989
990   $form->{id} = 0 if $form->{postasnew};
991
992   # get new invnumber in sequence if no invnumber is given or if posasnew was requested
993   if ($form->{postasnew}) {
994     if ($form->{type} eq "credit_note") {
995       undef($form->{cnnumber});
996     } else {
997       undef($form->{invnumber});
998     }
999   }
1000
1001   relink_accounts();
1002   $form->error($locale->text('Cannot post invoice!'))
1003     unless IS->post_invoice(\%myconfig, \%$form);
1004   remove_draft() if $form->{remove_draft};
1005
1006   if(!exists $form->{addition}) {
1007     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1008     $form->{addition} = $print_post     ? "PRINTED AND POSTED" :
1009                         $form->{storno} ? "STORNO"             :
1010                                           "POSTED";
1011     $form->save_history($form->dbconnect(\%myconfig));
1012   }
1013
1014   $form->redirect( $form->{label} . " $form->{invnumber} " . $locale->text('posted!'))
1015     unless $print_post;
1016
1017   $main::lxdebug->leave_sub();
1018 }
1019
1020 sub print_and_post {
1021   $main::lxdebug->enter_sub();
1022
1023   my $form     = $main::form;
1024
1025   $main::auth->assert('invoice_edit');
1026
1027   my $old_form               = new Form;
1028   $print_post             = 1;
1029   $form->{print_and_post} = 1;
1030   &post();
1031
1032   &edit();
1033   $main::lxdebug->leave_sub();
1034
1035 }
1036
1037 sub use_as_template {
1038   $main::lxdebug->enter_sub();
1039
1040   my $form     = $main::form;
1041   my %myconfig = %main::myconfig;
1042
1043   $main::auth->assert('invoice_edit');
1044
1045   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);
1046   $form->{paidaccounts} = 1;
1047   $form->{rowcount}--;
1048   $form->{invdate} = $form->current_date(\%myconfig);
1049   &display_form;
1050
1051   $main::lxdebug->leave_sub();
1052 }
1053
1054 sub storno {
1055   $main::lxdebug->enter_sub();
1056
1057   my $form     = $main::form;
1058   my %myconfig = %main::myconfig;
1059   my $locale   = $main::locale;
1060
1061   $main::auth->assert('invoice_edit');
1062
1063   if ($form->{storno}) {
1064     $form->error($locale->text('Cannot storno storno invoice!'));
1065   }
1066
1067   if (IS->has_storno(\%myconfig, $form, "ar")) {
1068     $form->error($locale->text("Invoice has already been storno'd!"));
1069   }
1070
1071   map({ my $key = $_; delete($form->{$key}) unless (grep({ $key eq $_ } qw(id login password stylesheet type))); } keys(%{ $form }));
1072
1073   invoice_links();
1074   prepare_invoice();
1075   relink_accounts();
1076
1077   # Payments must not be recorded for the new storno invoice.
1078   $form->{paidaccounts} = 0;
1079   map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
1080
1081   $form->{storno_id} = $form->{id};
1082   $form->{storno} = 1;
1083   $form->{id} = "";
1084   $form->{invnumber} = "Storno zu " . $form->{invnumber};
1085   $form->{rowcount}++;
1086
1087   post();
1088   $main::lxdebug->leave_sub();
1089 }
1090
1091 sub preview {
1092   $main::lxdebug->enter_sub();
1093
1094   my $form     = $main::form;
1095
1096   $main::auth->assert('invoice_edit');
1097
1098   $form->{preview} = 1;
1099   my $old_form = new Form;
1100   for (keys %$form) { $old_form->{$_} = $form->{$_} }
1101
1102   &print_form($old_form);
1103   $main::lxdebug->leave_sub();
1104
1105 }
1106
1107 sub delete {
1108   $main::lxdebug->enter_sub();
1109
1110   my $form     = $main::form;
1111   my $locale   = $main::locale;
1112
1113   $main::auth->assert('invoice_edit');
1114
1115   if ($form->{second_run}) {
1116     $form->{print_and_post} = 0;
1117   }
1118   $form->header;
1119
1120   print qq|
1121 <body>
1122
1123 <form method="post" action="$form->{script}">
1124 |;
1125
1126   # delete action variable
1127   map { delete $form->{$_} } qw(action header);
1128
1129   foreach my $key (keys %$form) {
1130     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1131     $form->{$key} =~ s/\"/&quot;/g;
1132     print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
1133   }
1134
1135   print qq|
1136 <h2 class="confirm">| . $locale->text('Confirm!') . qq|</h2>
1137
1138 <h4>|
1139     . $locale->text('Are you sure you want to delete Invoice Number')
1140     . qq| $form->{invnumber}
1141 </h4>
1142
1143 <p>
1144 <input name="action" class="submit" type="submit" value="|
1145     . $locale->text('Yes') . qq|">
1146 </form>
1147 |;
1148
1149   $main::lxdebug->leave_sub();
1150 }
1151
1152 sub credit_note {
1153   $main::lxdebug->enter_sub();
1154
1155   my $form     = $main::form;
1156   my %myconfig = %main::myconfig;
1157   my $locale   = $main::locale;
1158
1159   $main::auth->assert('invoice_edit');
1160
1161   $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1162   $form->{duedate} =
1163     $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1164
1165   $form->{id}     = '';
1166   $form->{rowcount}--;
1167   $form->{shipto} = 1;
1168
1169
1170   $form->{title}  = $locale->text('Add Credit Note');
1171   $form->{script} = 'is.pl';
1172   our $script         = "is";
1173   our $buysell        = 'buy';
1174
1175
1176   # bo creates the id, reset it
1177   map { delete $form->{$_} }
1178     qw(id invnumber subject message cc bcc printed emailed queued);
1179   $form->{ $form->{vc} } =~ s/--.*//g;
1180   $form->{type} = "credit_note";
1181
1182
1183   map { $form->{"select$_"} = "" } ($form->{vc}, 'currency');
1184
1185   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1186     qw(creditlimit creditremaining);
1187
1188   my $currency = $form->{currency};
1189   &invoice_links;
1190
1191   $form->{currency}     = $currency;
1192   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, $buysell);
1193   $form->{exchangerate} = $form->{forex} || '';
1194
1195   $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1196
1197   &prepare_invoice;
1198
1199
1200   &display_form;
1201
1202   $main::lxdebug->leave_sub();
1203 }
1204
1205 sub yes {
1206   $main::lxdebug->enter_sub();
1207
1208   my $form     = $main::form;
1209   my %myconfig = %main::myconfig;
1210   my $locale   = $main::locale;
1211
1212   $main::auth->assert('invoice_edit');
1213
1214   if (IS->delete_invoice(\%myconfig, \%$form, $main::spool)) {
1215     # saving the history
1216         if(!exists $form->{addition}) {
1217     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1218           $form->{addition} = "DELETED";
1219           $form->save_history($form->dbconnect(\%myconfig));
1220     }
1221     # /saving the history
1222     $form->redirect($locale->text('Invoice deleted!'));
1223   }
1224   $form->error($locale->text('Cannot delete invoice!'));
1225
1226   $main::lxdebug->leave_sub();
1227 }
1228
1229 sub e_mail {
1230   $main::lxdebug->enter_sub();
1231
1232   my $form     = $main::form;
1233
1234   $main::auth->assert('invoice_edit');
1235
1236   if (!$form->{id}) {
1237     $print_post = 1;
1238
1239     my $saved_form = save_form();
1240
1241     post();
1242
1243     restore_form($saved_form, 0, qw(id invnumber));
1244   }
1245
1246   edit_e_mail();
1247
1248   $main::lxdebug->leave_sub();
1249 }