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