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