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