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