1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
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.
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 #======================================================================
30 # Inventory invoicing module
32 #======================================================================
38 use List::Util qw(max sum);
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";
55 $main::lxdebug->enter_sub();
57 my $form = $main::form;
58 my $locale = $main::locale;
60 $main::auth->assert('invoice_edit');
62 return $main::lxdebug->leave_sub() if (load_draft_maybe());
64 if ($form->{type} eq "credit_note") {
65 $form->{title} = $locale->text('Add Credit Note');
67 if ($form->{storno}) {
68 $form->{title} = $locale->text('Add Storno Credit Note');
71 $form->{title} = $locale->text('Add Sales Invoice');
76 $form->{callback} = "$form->{script}?action=add&type=$form->{type}" unless $form->{callback};
78 $form->{jsscript} = "date";
84 $main::lxdebug->leave_sub();
88 $main::lxdebug->enter_sub();
90 my $form = $main::form;
91 my $locale = $main::locale;
93 $main::auth->assert('invoice_edit');
96 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
97 #/show hhistory button
99 if ($form->{type} eq "credit_note") {
100 $form->{title} = $locale->text('Edit Credit Note');
101 $form->{title} = $locale->text('Edit Storno Credit Note') if $form->{storno};
103 $form->{title} = $locale->text('Edit Sales Invoice');
104 $form->{title} = $locale->text('Edit Storno Invoice') if $form->{storno};
107 my ($language_id, $printer_id);
108 if ($form->{print_and_post}) {
109 $form->{action} = "print";
110 $form->{resubmit} = 1;
111 $language_id = $form->{language_id};
112 $printer_id = $form->{printer_id};
116 if ($form->{print_and_post}) {
117 $form->{language_id} = $language_id;
118 $form->{printer_id} = $printer_id;
123 $main::lxdebug->leave_sub();
127 $main::lxdebug->enter_sub();
129 my $form = $main::form;
130 my %myconfig = %main::myconfig;
132 $main::auth->assert('invoice_edit');
134 $form->{vc} = 'customer';
137 $form->{webdav} = $main::webdav;
138 $form->{lizenzen} = $main::lizenzen;
140 $form->create_links("AR", \%myconfig, "customer");
142 if ($form->{all_customer}) {
143 unless ($form->{customer_id}) {
144 $form->{customer_id} = $form->{all_customer}->[0]->{id};
145 $form->{salesman_id} = $form->{all_customer}->[0]->{salesman_id};
149 $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded cp_id intnotes id shipto_id));
151 IS->get_customer(\%myconfig, \%$form);
153 #quote all_customer Bug 133
154 foreach my $ref (@{ $form->{all_customer} }) {
155 $ref->{name} = $form->quote($ref->{name});
158 # Load data for a specific order and update form fields
159 my $order_data = IS->get_order_data();
161 # Copy the fields we need to %form
162 for my $key (qw(payment_id salesman_id orddate taxzone_id quonumber)) {
163 $form->{$key} = $order_data->{$key};
166 $form->restore_vars(qw(id));
168 IS->retrieve_invoice(\%myconfig, \%$form);
169 $form->restore_vars(qw(payment_id language_id taxzone_id intnotes cp_id shipto_id));
170 $form->restore_vars(qw(taxincluded)) if $form->{id};
171 $form->restore_vars(qw(salesman_id)) if $main::editing;
174 # build vendor/customer drop down comatibility... don't ask
175 if (@{ $form->{"all_customer"} }) {
176 $form->{"selectcustomer"} = 1;
177 $form->{customer} = qq|$form->{customer}--$form->{"customer_id"}|;
180 $form->{"oldcustomer"} = $form->{customer};
182 if ($form->{"oldcustomer"} !~ m/--\d+$/ && $form->{"customer_id"}) {
183 $form->{"oldcustomer"} .= qq|--$form->{"customer_id"}|
187 # $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
188 # $form->{selectcustomer} = 1;
190 $form->{employee} = "$form->{employee}--$form->{employee_id}";
193 $form->{forex} = $form->{exchangerate};
194 my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
196 foreach my $key (keys %{ $form->{AR_links} }) {
197 foreach my $ref (@{ $form->{AR_links}{$key} }) {
198 $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}</option>\n";
201 if ($key eq "AR_paid") {
202 next unless $form->{acc_trans}{$key};
203 for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
204 $form->{"AR_paid_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
207 $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
208 $form->{"datepaid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{transdate};
209 $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
210 $form->{"forex_$i"} = $form->{"exchangerate_$i"};
211 $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
212 $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
214 $form->{paidaccounts} = $i;
217 $form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
221 $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
223 $form->{AR} = $form->{AR_1} unless $form->{id};
225 $form->{locked} = ($form->datetonum($form->{invdate}, \%myconfig)
226 <= $form->datetonum($form->{closedto}, \%myconfig));
228 $main::lxdebug->leave_sub();
231 sub prepare_invoice {
232 $main::lxdebug->enter_sub();
234 my $form = $main::form;
235 my %myconfig = %main::myconfig;
237 $main::auth->assert('invoice_edit');
239 if ($form->{type} eq "credit_note") {
240 $form->{type} = "credit_note";
241 $form->{formname} = "credit_note";
243 $form->{type} = "invoice";
244 $form->{formname} = "invoice";
251 foreach my $ref (@{ $form->{invoice_details} }) {
254 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
256 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
257 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
259 my $decimalplaces = ($dec > 2) ? $dec : 2;
261 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
262 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
263 $dec_qty = length $dec_qty;
265 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
267 $form->{rowcount} = $i;
271 # get pricegroups for parts
272 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
273 set_pricegroup($_) for 1 .. $form->{rowcount};
275 $main::lxdebug->leave_sub();
279 $main::lxdebug->enter_sub();
281 my $form = $main::form;
282 my %myconfig = %main::myconfig;
283 my $locale = $main::locale;
284 my $cgi = $main::cgi;
286 $main::auth->assert('invoice_edit');
291 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
292 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
294 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
295 $form->{radier} = ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
297 my $set_duedate_url = "$form->{script}?action=set_duedate";
299 push @ { $form->{AJAX} }, new CGI::Ajax( 'set_duedate' => $set_duedate_url );
301 my @old_project_ids = ($form->{"globalproject_id"});
302 map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
304 $form->get_lists("contacts" => "ALL_CONTACTS",
305 "shipto" => "ALL_SHIPTO",
306 "projects" => { "key" => "ALL_PROJECTS",
308 "old_id" => \@old_project_ids },
309 "employees" => "ALL_EMPLOYEES",
310 "salesmen" => "ALL_SALESMEN",
311 "taxzones" => "ALL_TAXZONES",
312 "currencies" => "ALL_CURRENCIES",
313 "customers" => "ALL_CUSTOMERS",
314 "price_factors" => "ALL_PRICE_FACTORS");
316 $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
317 $TMPL_VAR{shipto_labels} = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
318 $TMPL_VAR{contact_labels} = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
319 $TMPL_VAR{department_labels} = sub { "$_[0]->{description}--$_[0]->{id}" };
322 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
323 $TMPL_VAR{vclimit} = $myconfig{vclimit};
324 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('customer', '', 0, 0)";
325 push @custom_hiddens, "customer_id";
326 push @custom_hiddens, "oldcustomer";
327 push @custom_hiddens, "selectcustomer";
329 # currencies and exchangerate
330 my @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
331 my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
332 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
333 $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
334 $TMPL_VAR{currencies} = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
335 '-values' => \@values, '-labels' => \%labels)) if scalar @values;
336 push @custom_hiddens, "forex";
337 push @custom_hiddens, "exchangerate" if $form->{forex};
339 $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
340 $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
342 $form->{fokus} = "invoice.customer";
344 my $follow_up_vc = $form->{customer};
345 $follow_up_vc =~ s/--\d*\s*$//;
346 $TMPL_VAR{customer_name} = $follow_up_vc;
348 # set option selected
349 foreach my $item (qw(AR)) {
350 $form->{"select$item"} =~ s/ selected//;
351 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
354 $TMPL_VAR{is_type_credit_note} = $form->{type} eq "credit_note";
355 $TMPL_VAR{is_format_html} = $form->{format} eq 'html';
356 $TMPL_VAR{dateformat} = $myconfig{dateformat};
357 $TMPL_VAR{numberformat} = $myconfig{numberformat};
360 $TMPL_VAR{HIDDENS} = [qw(
361 id action type media format queued printed emailed title vc discount
362 title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
363 max_dunning_level dunning_amount
364 shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax
365 shiptoemail shiptodepartment_1 shiptodepartment_2 message email subject cc bcc taxaccounts cursor_fokus
366 convert_from_do_ids convert_from_oe_ids
368 map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
370 $form->{jsscript} = 1;
373 print $form->parse_html_template("is/form_header", \%TMPL_VAR);
375 $main::lxdebug->leave_sub();
379 $main::lxdebug->enter_sub();
381 my $form = $main::form;
382 my %myconfig = %main::myconfig;
383 my $locale = $main::locale;
385 $main::auth->assert('invoice_edit');
387 $form->{invtotal} = $form->{invsubtotal};
388 $form->{oldinvtotal} = $form->{invtotal};
391 $form->{rows} = max 2,
392 $form->numtextrows($form->{notes}, 26, 8),
393 $form->numtextrows($form->{intnotes}, 35, 8);
396 # tax, total and subtotal calculations
397 my ($tax, $subtotal);
398 $form->{taxaccounts_array} = [ split / /, $form->{taxaccounts} ];
400 foreach my $item (@{ $form->{taxaccounts_array} }) {
401 if ($form->{"${item}_base"}) {
402 if ($form->{taxincluded}) {
403 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"}
404 / (1 + $form->{"${item}_rate"})), 2);
405 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
407 $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
408 $form->{invtotal} += $form->{"${item}_total"};
415 $form->{follow_ups} = FU->follow_ups('trans_id' => $form->{id}) || [];
416 $form->{follow_ups_unfinished} = ( sum map { $_->{due} * 1 } @{ $form->{follow_ups} } ) || 0;
421 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
422 $form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];
424 for my $i (1 .. $form->{paidaccounts}) {
425 $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
426 $form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
427 $totalpaid += $form->{"paid_$i"};
430 print $form->parse_html_template('is/form_footer', {
431 is_type_credit_note => ($form->{type} eq "credit_note"),
432 totalpaid => $totalpaid,
433 paid_missing => $form->{invtotal} - $totalpaid,
434 print_options => print_options(inline => 1),
435 show_storno => $form->{id} && !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar") && !$totalpaid,
436 show_delete => ($form->current_date(\%myconfig) eq $form->{gldate}),
438 ##print $form->parse_html_template('is/_payments'); # parser
439 ##print $form->parse_html_template('webdav/_list'); # parser
441 $main::lxdebug->leave_sub();
445 $main::lxdebug->enter_sub();
447 my $form = $main::form;
448 my %myconfig = %main::myconfig;
450 $main::auth->assert('invoice_edit');
452 &mark_as_paid_common(\%myconfig,"ar");
454 $main::lxdebug->leave_sub();
458 $main::lxdebug->enter_sub();
460 my $form = $main::form;
461 my %myconfig = %main::myconfig;
463 $main::auth->assert('invoice_edit');
465 my ($recursive_call) = shift;
467 # map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining) unless $recursive_call;
469 $form->{print_and_post} = 0 if $form->{second_run};
470 my $taxincluded = "checked" if $form->{taxincluded};
473 &check_name("customer");
475 $form->{taxincluded} ||= $taxincluded;
477 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
478 $form->{exchangerate} = $form->{forex} if $form->{forex};
480 for my $i (1 .. $form->{paidaccounts}) {
481 next unless $form->{"paid_$i"};
482 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
483 $form->{"forex_$i"} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
484 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
487 my $i = $form->{rowcount};
488 my $exchangerate = $form->{exchangerate} || 1;
490 # if last row empty, check the form otherwise retrieve new item
491 if ( ($form->{"partnumber_$i"} eq "")
492 && ($form->{"description_$i"} eq "")
493 && ($form->{"partsgroup_$i"} eq "")) {
495 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
500 IS->retrieve_item(\%myconfig, \%$form);
502 my $rows = scalar @{ $form->{item_list} };
504 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{customer_discount} * 100);
507 $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
516 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
518 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
519 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
521 $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
522 $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
524 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
526 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
527 my $decimalplaces = max 2, length $1;
530 $form->{"sellprice_$i"} = $sellprice;
532 # if there is an exchange rate adjust sellprice
533 $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
534 $form->{"sellprice_$i"} /= $exchangerate;
537 $form->{"listprice_$i"} /= $exchangerate;
539 my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
540 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
541 map { $form->{"${_}_base"} += $amount } split / /, $form->{"taxaccounts_$i"};
542 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
544 $form->{creditremaining} -= $amount;
546 map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice listprice);
548 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
550 if ($main::lizenzen) {
551 if ($form->{"inventory_accno_$i"} ne "") {
552 $form->{"lizenzen_$i"} = qq|<option></option>|;
553 foreach my $item (@{ $form->{LIZENZEN}{ $form->{"id_$i"} } }) {
554 $form->{"lizenzen_$i"} .= qq|<option value="$item->{"id"}">$item->{"licensenumber"}</option>|;
556 $form->{"lizenzen_$i"} .= qq|<option value=-1>Neue Lizenz</option>|;
560 # get pricegroups for parts
561 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
563 # build up html code for prices_$i
571 # ok, so this is a new part
572 # ask if it is a part or service item
574 if ( $form->{"partsgroup_$i"}
575 && ($form->{"partsnumber_$i"} eq "")
576 && ($form->{"description_$i"} eq "")) {
578 $form->{"discount_$i"} = "";
582 $form->{"id_$i"} = 0;
587 $main::lxdebug->leave_sub();
591 $main::lxdebug->enter_sub();
593 my $form = $main::form;
594 my %myconfig = %main::myconfig;
595 my $locale = $main::locale;
597 $main::auth->assert('invoice_edit');
601 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
602 for my $i (1 .. $form->{paidaccounts}) {
603 if ($form->{"paid_$i"}) {
604 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
606 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
608 $form->error($locale->text('Cannot post payment for a closed period!'))
609 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
611 if ($form->{currency} ne $form->{defaultcurrency}) {
612 $form->{"exchangerate_$i"} = $form->{exchangerate}
613 if ($invdate == $datepaid);
614 $form->isblank("exchangerate_$i",
615 $locale->text('Exchangerate for payment missing!'));
620 ($form->{AR}) = split /--/, $form->{AR};
621 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
623 $form->redirect($locale->text('Payment posted!'))
624 if (IS->post_payment(\%myconfig, \%$form));
625 $form->error($locale->text('Cannot post payment!'));
628 $main::lxdebug->leave_sub();
632 $main::lxdebug->enter_sub();
634 my $form = $main::form;
635 my %myconfig = %main::myconfig;
636 my $locale = $main::locale;
638 $main::auth->assert('invoice_edit');
640 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
641 $form->isblank("invdate", $locale->text('Invoice Date missing!'));
642 $form->isblank("customer", $locale->text('Customer missing!'));
643 $form->error($locale->text('Cannot post invoice for a closed period!'))
644 if ($form->date_closed($form->{"invdate"}, \%myconfig));
646 $form->{invnumber} =~ s/^\s*//g;
647 $form->{invnumber} =~ s/\s*$//g;
649 # if oldcustomer ne customer redo form
650 if (&check_name('customer')) {
654 if ($form->{second_run}) {
655 $form->{print_and_post} = 0;
660 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
661 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
663 $form->error($locale->text('Cannot post invoice for a closed period!'))
664 if ($invdate <= $closedto);
666 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
667 if ($form->{currency} ne $form->{defaultcurrency});
669 for my $i (1 .. $form->{paidaccounts}) {
670 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
671 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
673 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
675 $form->error($locale->text('Cannot post payment for a closed period!'))
676 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
678 if ($form->{currency} ne $form->{defaultcurrency}) {
679 $form->{"exchangerate_$i"} = $form->{exchangerate}
680 if ($invdate == $datepaid);
681 $form->isblank("exchangerate_$i",
682 $locale->text('Exchangerate for payment missing!'));
687 ($form->{AR}) = split /--/, $form->{AR};
688 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
689 $form->{storno} ||= 0;
691 $form->{label} = $locale->text('Invoice');
693 $form->{id} = 0 if $form->{postasnew};
695 # get new invnumber in sequence if no invnumber is given or if posasnew was requested
696 if ($form->{postasnew}) {
697 if ($form->{type} eq "credit_note") {
698 undef($form->{cnnumber});
700 undef($form->{invnumber});
705 $form->error($locale->text('Cannot post invoice!'))
706 unless IS->post_invoice(\%myconfig, \%$form);
707 remove_draft() if $form->{remove_draft};
709 if(!exists $form->{addition}) {
710 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
711 $form->{addition} = $print_post ? "PRINTED AND POSTED" :
712 $form->{storno} ? "STORNO" :
714 $form->save_history($form->dbconnect(\%myconfig));
717 $form->redirect( $form->{label} . " $form->{invnumber} " . $locale->text('posted!'))
720 $main::lxdebug->leave_sub();
724 $main::lxdebug->enter_sub();
726 my $form = $main::form;
728 $main::auth->assert('invoice_edit');
730 my $old_form = new Form;
732 $form->{print_and_post} = 1;
736 $main::lxdebug->leave_sub();
740 sub use_as_template {
741 $main::lxdebug->enter_sub();
743 my $form = $main::form;
744 my %myconfig = %main::myconfig;
746 $main::auth->assert('invoice_edit');
748 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);
749 $form->{paidaccounts} = 1;
751 $form->{invdate} = $form->current_date(\%myconfig);
754 $main::lxdebug->leave_sub();
758 $main::lxdebug->enter_sub();
760 my $form = $main::form;
761 my %myconfig = %main::myconfig;
762 my $locale = $main::locale;
764 $main::auth->assert('invoice_edit');
766 if ($form->{storno}) {
767 $form->error($locale->text('Cannot storno storno invoice!'));
770 if (IS->has_storno(\%myconfig, $form, "ar")) {
771 $form->error($locale->text("Invoice has already been storno'd!"));
774 map({ my $key = $_; delete($form->{$key}) unless (grep({ $key eq $_ } qw(id login password stylesheet type))); } keys(%{ $form }));
780 # Payments must not be recorded for the new storno invoice.
781 $form->{paidaccounts} = 0;
782 map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
784 $form->{storno_id} = $form->{id};
787 $form->{invnumber} = "Storno zu " . $form->{invnumber};
791 $main::lxdebug->leave_sub();
795 $main::lxdebug->enter_sub();
797 my $form = $main::form;
799 $main::auth->assert('invoice_edit');
801 $form->{preview} = 1;
802 my $old_form = new Form;
803 for (keys %$form) { $old_form->{$_} = $form->{$_} }
805 &print_form($old_form);
806 $main::lxdebug->leave_sub();
811 $main::lxdebug->enter_sub();
813 my $form = $main::form;
814 my $locale = $main::locale;
816 $main::auth->assert('invoice_edit');
818 if ($form->{second_run}) {
819 $form->{print_and_post} = 0;
826 <form method="post" action="$form->{script}">
829 # delete action variable
830 map { delete $form->{$_} } qw(action header);
832 foreach my $key (keys %$form) {
833 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
834 $form->{$key} =~ s/\"/"/g;
835 print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
839 <h2 class="confirm">| . $locale->text('Confirm!') . qq|</h2>
842 . $locale->text('Are you sure you want to delete Invoice Number')
843 . qq| $form->{invnumber}
847 <input name="action" class="submit" type="submit" value="|
848 . $locale->text('Yes') . qq|">
852 $main::lxdebug->leave_sub();
856 $main::lxdebug->enter_sub();
858 my $form = $main::form;
859 my %myconfig = %main::myconfig;
860 my $locale = $main::locale;
862 $main::auth->assert('invoice_edit');
864 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
866 $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
873 $form->{title} = $locale->text('Add Credit Note');
874 $form->{script} = 'is.pl';
876 our $buysell = 'buy';
879 # bo creates the id, reset it
880 map { delete $form->{$_} }
881 qw(id invnumber subject message cc bcc printed emailed queued);
882 $form->{ $form->{vc} } =~ s/--.*//g;
883 $form->{type} = "credit_note";
886 map { $form->{"select$_"} = "" } ($form->{vc}, 'currency');
888 # map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
889 # qw(creditlimit creditremaining);
891 my $currency = $form->{currency};
894 $form->{currency} = $currency;
895 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, $buysell);
896 $form->{exchangerate} = $form->{forex} || '';
898 $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
905 $main::lxdebug->leave_sub();
909 $main::lxdebug->enter_sub();
911 my $form = $main::form;
912 my %myconfig = %main::myconfig;
913 my $locale = $main::locale;
915 $main::auth->assert('invoice_edit');
917 if (IS->delete_invoice(\%myconfig, \%$form, $main::spool)) {
919 if(!exists $form->{addition}) {
920 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
921 $form->{addition} = "DELETED";
922 $form->save_history($form->dbconnect(\%myconfig));
924 # /saving the history
925 $form->redirect($locale->text('Invoice deleted!'));
927 $form->error($locale->text('Cannot delete invoice!'));
929 $main::lxdebug->leave_sub();
933 $main::lxdebug->enter_sub();
935 my $form = $main::form;
937 $main::auth->assert('invoice_edit');
942 my $saved_form = save_form();
946 restore_form($saved_form, 0, qw(id invnumber));
951 $main::lxdebug->leave_sub();