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