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 $form->restore_vars(qw(id));
160 IS->retrieve_invoice(\%myconfig, \%$form);
161 $form->restore_vars(qw(payment_id language_id taxzone_id intnotes cp_id shipto_id));
162 $form->restore_vars(qw(taxincluded)) if $form->{id};
163 $form->restore_vars(qw(salesman_id)) if $main::editing;
165 # build vendor/customer drop down comatibility... don't ask
166 if (@{ $form->{"all_customer"} }) {
167 $form->{"selectcustomer"} = 1;
168 $form->{customer} = qq|$form->{customer}--$form->{"customer_id"}|;
171 $form->{"oldcustomer"} = $form->{customer};
173 if ($form->{"oldcustomer"} !~ m/--\d+$/ && $form->{"customer_id"}) {
174 $form->{"oldcustomer"} .= qq|--$form->{"customer_id"}|
178 # $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
179 # $form->{selectcustomer} = 1;
181 $form->{employee} = "$form->{employee}--$form->{employee_id}";
184 $form->{forex} = $form->{exchangerate};
185 my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
187 foreach my $key (keys %{ $form->{AR_links} }) {
188 foreach my $ref (@{ $form->{AR_links}{$key} }) {
189 $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}</option>\n";
192 if ($key eq "AR_paid") {
193 next unless $form->{acc_trans}{$key};
194 for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
195 $form->{"AR_paid_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
198 $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
199 $form->{"datepaid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{transdate};
200 $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
201 $form->{"forex_$i"} = $form->{"exchangerate_$i"};
202 $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
203 $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
205 $form->{paidaccounts} = $i;
208 $form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
212 $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
214 $form->{AR} = $form->{AR_1} unless $form->{id};
216 $form->{locked} = ($form->datetonum($form->{invdate}, \%myconfig)
217 <= $form->datetonum($form->{closedto}, \%myconfig));
219 $main::lxdebug->leave_sub();
222 sub prepare_invoice {
223 $main::lxdebug->enter_sub();
225 my $form = $main::form;
226 my %myconfig = %main::myconfig;
228 $main::auth->assert('invoice_edit');
230 if ($form->{type} eq "credit_note") {
231 $form->{type} = "credit_note";
232 $form->{formname} = "credit_note";
234 $form->{type} = "invoice";
235 $form->{formname} = "invoice";
242 foreach my $ref (@{ $form->{invoice_details} }) {
245 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
247 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
248 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
250 my $decimalplaces = ($dec > 2) ? $dec : 2;
252 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
253 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
254 $dec_qty = length $dec_qty;
256 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
258 $form->{rowcount} = $i;
262 # get pricegroups for parts
263 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
264 set_pricegroup($_) for 1 .. $form->{rowcount};
266 $main::lxdebug->leave_sub();
270 $main::lxdebug->enter_sub();
272 my $form = $main::form;
273 my %myconfig = %main::myconfig;
274 my $locale = $main::locale;
275 my $cgi = $main::cgi;
277 $main::auth->assert('invoice_edit');
282 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
283 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
285 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
286 $form->{radier} = ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
288 my $set_duedate_url = "$form->{script}?action=set_duedate";
290 push @ { $form->{AJAX} }, new CGI::Ajax( 'set_duedate' => $set_duedate_url );
292 my @old_project_ids = ($form->{"globalproject_id"});
293 map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
295 $form->get_lists("contacts" => "ALL_CONTACTS",
296 "shipto" => "ALL_SHIPTO",
297 "projects" => { "key" => "ALL_PROJECTS",
299 "old_id" => \@old_project_ids },
300 "employees" => "ALL_EMPLOYEES",
301 "salesmen" => "ALL_SALESMEN",
302 "taxzones" => "ALL_TAXZONES",
303 "currencies" => "ALL_CURRENCIES",
304 "customers" => "ALL_CUSTOMERS",
305 "departments" => "all_departments",
306 "price_factors" => "ALL_PRICE_FACTORS");
308 $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
309 $TMPL_VAR{shipto_labels} = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
310 $TMPL_VAR{contact_labels} = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
311 $TMPL_VAR{department_labels} = sub { "$_[0]->{description}--$_[0]->{id}" };
314 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
315 $TMPL_VAR{vclimit} = $myconfig{vclimit};
316 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('customer', '', 0, 0)";
317 push @custom_hiddens, "customer_id";
318 push @custom_hiddens, "oldcustomer";
319 push @custom_hiddens, "selectcustomer";
321 # currencies and exchangerate
322 my @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
323 my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
324 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
325 $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
326 $TMPL_VAR{currencies} = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
327 '-values' => \@values, '-labels' => \%labels)) if scalar @values;
328 push @custom_hiddens, "forex";
329 push @custom_hiddens, "exchangerate" if $form->{forex};
331 $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
332 $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
334 $form->{fokus} = "invoice.customer";
336 my $follow_up_vc = $form->{customer};
337 $follow_up_vc =~ s/--\d*\s*$//;
338 $TMPL_VAR{customer_name} = $follow_up_vc;
340 # set option selected
341 foreach my $item (qw(AR)) {
342 $form->{"select$item"} =~ s/ selected//;
343 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
346 $TMPL_VAR{is_type_credit_note} = $form->{type} eq "credit_note";
347 $TMPL_VAR{is_format_html} = $form->{format} eq 'html';
348 $TMPL_VAR{dateformat} = $myconfig{dateformat};
349 $TMPL_VAR{numberformat} = $myconfig{numberformat};
352 $TMPL_VAR{HIDDENS} = [qw(
353 id action type media format queued printed emailed title vc discount
354 title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
355 max_dunning_level dunning_amount
356 shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax
357 shiptoemail shiptodepartment_1 shiptodepartment_2 message email subject cc bcc taxaccounts cursor_fokus
358 convert_from_do_ids convert_from_oe_ids
360 map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
362 $form->{jsscript} = 1;
365 print $form->parse_html_template("is/form_header", \%TMPL_VAR);
367 $main::lxdebug->leave_sub();
371 $main::lxdebug->enter_sub();
373 my $form = $main::form;
374 my %myconfig = %main::myconfig;
375 my $locale = $main::locale;
377 $main::auth->assert('invoice_edit');
379 $form->{invtotal} = $form->{invsubtotal};
380 $form->{oldinvtotal} = $form->{invtotal};
383 $form->{rows} = max 2,
384 $form->numtextrows($form->{notes}, 26, 8),
385 $form->numtextrows($form->{intnotes}, 35, 8);
388 # tax, total and subtotal calculations
389 my ($tax, $subtotal);
390 $form->{taxaccounts_array} = [ split / /, $form->{taxaccounts} ];
392 foreach my $item (@{ $form->{taxaccounts_array} }) {
393 if ($form->{"${item}_base"}) {
394 if ($form->{taxincluded}) {
395 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"}
396 / (1 + $form->{"${item}_rate"})), 2);
397 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
399 $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
400 $form->{invtotal} += $form->{"${item}_total"};
407 $form->{follow_ups} = FU->follow_ups('trans_id' => $form->{id}) || [];
408 $form->{follow_ups_unfinished} = ( sum map { $_->{due} * 1 } @{ $form->{follow_ups} } ) || 0;
413 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
414 $form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];
416 for my $i (1 .. $form->{paidaccounts}) {
417 $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
418 $form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
419 $totalpaid += $form->{"paid_$i"};
422 print $form->parse_html_template('is/form_footer', {
423 is_type_credit_note => ($form->{type} eq "credit_note"),
424 totalpaid => $totalpaid,
425 paid_missing => $form->{invtotal} - $totalpaid,
426 print_options => print_options(inline => 1),
427 show_storno => $form->{id} && !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar") && !$totalpaid,
428 show_delete => ($form->current_date(\%myconfig) eq $form->{gldate}),
430 ##print $form->parse_html_template('is/_payments'); # parser
431 ##print $form->parse_html_template('webdav/_list'); # parser
433 $main::lxdebug->leave_sub();
437 $main::lxdebug->enter_sub();
439 my $form = $main::form;
440 my %myconfig = %main::myconfig;
442 $main::auth->assert('invoice_edit');
444 &mark_as_paid_common(\%myconfig,"ar");
446 $main::lxdebug->leave_sub();
450 $main::lxdebug->enter_sub();
452 my $form = $main::form;
453 my %myconfig = %main::myconfig;
455 $main::auth->assert('invoice_edit');
457 my ($recursive_call) = shift;
459 # map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining) unless $recursive_call;
461 $form->{print_and_post} = 0 if $form->{second_run};
462 my $taxincluded = "checked" if $form->{taxincluded};
465 &check_name("customer");
467 $form->{taxincluded} ||= $taxincluded;
469 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
470 $form->{exchangerate} = $form->{forex} if $form->{forex};
472 for my $i (1 .. $form->{paidaccounts}) {
473 next unless $form->{"paid_$i"};
474 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
475 $form->{"forex_$i"} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
476 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
479 my $i = $form->{rowcount};
480 my $exchangerate = $form->{exchangerate} || 1;
482 # if last row empty, check the form otherwise retrieve new item
483 if ( ($form->{"partnumber_$i"} eq "")
484 && ($form->{"description_$i"} eq "")
485 && ($form->{"partsgroup_$i"} eq "")) {
487 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
492 IS->retrieve_item(\%myconfig, \%$form);
494 my $rows = scalar @{ $form->{item_list} };
496 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{customer_discount} * 100);
499 $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
508 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
510 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
511 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
513 $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
514 $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
516 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
518 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
519 my $decimalplaces = max 2, length $1;
522 $form->{"sellprice_$i"} = $sellprice;
524 # if there is an exchange rate adjust sellprice
525 $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
526 $form->{"sellprice_$i"} /= $exchangerate;
529 $form->{"listprice_$i"} /= $exchangerate;
531 my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
532 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
533 map { $form->{"${_}_base"} += $amount } split / /, $form->{"taxaccounts_$i"};
534 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
536 $form->{creditremaining} -= $amount;
538 map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice listprice);
540 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
542 if ($main::lizenzen) {
543 if ($form->{"inventory_accno_$i"} ne "") {
544 $form->{"lizenzen_$i"} = qq|<option></option>|;
545 foreach my $item (@{ $form->{LIZENZEN}{ $form->{"id_$i"} } }) {
546 $form->{"lizenzen_$i"} .= qq|<option value="$item->{"id"}">$item->{"licensenumber"}</option>|;
548 $form->{"lizenzen_$i"} .= qq|<option value=-1>Neue Lizenz</option>|;
552 # get pricegroups for parts
553 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
555 # build up html code for prices_$i
563 # ok, so this is a new part
564 # ask if it is a part or service item
566 if ( $form->{"partsgroup_$i"}
567 && ($form->{"partsnumber_$i"} eq "")
568 && ($form->{"description_$i"} eq "")) {
570 $form->{"discount_$i"} = "";
574 $form->{"id_$i"} = 0;
579 $main::lxdebug->leave_sub();
583 $main::lxdebug->enter_sub();
585 my $form = $main::form;
586 my %myconfig = %main::myconfig;
587 my $locale = $main::locale;
589 $main::auth->assert('invoice_edit');
593 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
594 for my $i (1 .. $form->{paidaccounts}) {
595 if ($form->{"paid_$i"}) {
596 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
598 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
600 $form->error($locale->text('Cannot post payment for a closed period!'))
601 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
603 if ($form->{currency} ne $form->{defaultcurrency}) {
604 $form->{"exchangerate_$i"} = $form->{exchangerate}
605 if ($invdate == $datepaid);
606 $form->isblank("exchangerate_$i",
607 $locale->text('Exchangerate for payment missing!'));
612 ($form->{AR}) = split /--/, $form->{AR};
613 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
615 $form->redirect($locale->text('Payment posted!'))
616 if (IS->post_payment(\%myconfig, \%$form));
617 $form->error($locale->text('Cannot post payment!'));
620 $main::lxdebug->leave_sub();
624 $main::lxdebug->enter_sub();
626 my $form = $main::form;
627 my %myconfig = %main::myconfig;
628 my $locale = $main::locale;
630 $main::auth->assert('invoice_edit');
632 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
633 $form->isblank("invdate", $locale->text('Invoice Date missing!'));
634 $form->isblank("customer", $locale->text('Customer missing!'));
635 $form->error($locale->text('Cannot post invoice for a closed period!'))
636 if ($form->date_closed($form->{"invdate"}, \%myconfig));
638 $form->{invnumber} =~ s/^\s*//g;
639 $form->{invnumber} =~ s/\s*$//g;
641 # if oldcustomer ne customer redo form
642 if (&check_name('customer')) {
646 if ($form->{second_run}) {
647 $form->{print_and_post} = 0;
652 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
653 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
655 $form->error($locale->text('Cannot post invoice for a closed period!'))
656 if ($invdate <= $closedto);
658 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
659 if ($form->{currency} ne $form->{defaultcurrency});
661 for my $i (1 .. $form->{paidaccounts}) {
662 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
663 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
665 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
667 $form->error($locale->text('Cannot post payment for a closed period!'))
668 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
670 if ($form->{currency} ne $form->{defaultcurrency}) {
671 $form->{"exchangerate_$i"} = $form->{exchangerate}
672 if ($invdate == $datepaid);
673 $form->isblank("exchangerate_$i",
674 $locale->text('Exchangerate for payment missing!'));
679 ($form->{AR}) = split /--/, $form->{AR};
680 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
681 $form->{storno} ||= 0;
683 $form->{label} = $locale->text('Invoice');
685 $form->{id} = 0 if $form->{postasnew};
687 # get new invnumber in sequence if no invnumber is given or if posasnew was requested
688 if ($form->{postasnew}) {
689 if ($form->{type} eq "credit_note") {
690 undef($form->{cnnumber});
692 undef($form->{invnumber});
697 $form->error($locale->text('Cannot post invoice!'))
698 unless IS->post_invoice(\%myconfig, \%$form);
699 remove_draft() if $form->{remove_draft};
701 if(!exists $form->{addition}) {
702 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
703 $form->{addition} = $print_post ? "PRINTED AND POSTED" :
704 $form->{storno} ? "STORNO" :
706 $form->save_history($form->dbconnect(\%myconfig));
709 $form->redirect( $form->{label} . " $form->{invnumber} " . $locale->text('posted!'))
712 $main::lxdebug->leave_sub();
716 $main::lxdebug->enter_sub();
718 my $form = $main::form;
720 $main::auth->assert('invoice_edit');
722 my $old_form = new Form;
724 $form->{print_and_post} = 1;
728 $main::lxdebug->leave_sub();
732 sub use_as_template {
733 $main::lxdebug->enter_sub();
735 my $form = $main::form;
736 my %myconfig = %main::myconfig;
738 $main::auth->assert('invoice_edit');
740 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);
741 $form->{paidaccounts} = 1;
743 $form->{invdate} = $form->current_date(\%myconfig);
746 $main::lxdebug->leave_sub();
750 $main::lxdebug->enter_sub();
752 my $form = $main::form;
753 my %myconfig = %main::myconfig;
754 my $locale = $main::locale;
756 $main::auth->assert('invoice_edit');
758 if ($form->{storno}) {
759 $form->error($locale->text('Cannot storno storno invoice!'));
762 if (IS->has_storno(\%myconfig, $form, "ar")) {
763 $form->error($locale->text("Invoice has already been storno'd!"));
766 map({ my $key = $_; delete($form->{$key}) unless (grep({ $key eq $_ } qw(id login password stylesheet type))); } keys(%{ $form }));
772 # Payments must not be recorded for the new storno invoice.
773 $form->{paidaccounts} = 0;
774 map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
776 $form->{storno_id} = $form->{id};
779 $form->{invnumber} = "Storno zu " . $form->{invnumber};
783 $main::lxdebug->leave_sub();
787 $main::lxdebug->enter_sub();
789 my $form = $main::form;
791 $main::auth->assert('invoice_edit');
793 $form->{preview} = 1;
794 my $old_form = new Form;
795 for (keys %$form) { $old_form->{$_} = $form->{$_} }
797 &print_form($old_form);
798 $main::lxdebug->leave_sub();
803 $main::lxdebug->enter_sub();
805 my $form = $main::form;
806 my $locale = $main::locale;
808 $main::auth->assert('invoice_edit');
810 if ($form->{second_run}) {
811 $form->{print_and_post} = 0;
818 <form method="post" action="$form->{script}">
821 # delete action variable
822 map { delete $form->{$_} } qw(action header);
824 foreach my $key (keys %$form) {
825 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
826 $form->{$key} =~ s/\"/"/g;
827 print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
831 <h2 class="confirm">| . $locale->text('Confirm!') . qq|</h2>
834 . $locale->text('Are you sure you want to delete Invoice Number')
835 . qq| $form->{invnumber}
839 <input name="action" class="submit" type="submit" value="|
840 . $locale->text('Yes') . qq|">
844 $main::lxdebug->leave_sub();
848 $main::lxdebug->enter_sub();
850 my $form = $main::form;
851 my %myconfig = %main::myconfig;
852 my $locale = $main::locale;
854 $main::auth->assert('invoice_edit');
856 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
858 $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
865 $form->{title} = $locale->text('Add Credit Note');
866 $form->{script} = 'is.pl';
868 our $buysell = 'buy';
871 # bo creates the id, reset it
872 map { delete $form->{$_} }
873 qw(id invnumber subject message cc bcc printed emailed queued);
874 $form->{ $form->{vc} } =~ s/--.*//g;
875 $form->{type} = "credit_note";
878 map { $form->{"select$_"} = "" } ($form->{vc}, 'currency');
880 # map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
881 # qw(creditlimit creditremaining);
883 my $currency = $form->{currency};
886 $form->{currency} = $currency;
887 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, $buysell);
888 $form->{exchangerate} = $form->{forex} || '';
890 $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
897 $main::lxdebug->leave_sub();
901 $main::lxdebug->enter_sub();
903 my $form = $main::form;
904 my %myconfig = %main::myconfig;
905 my $locale = $main::locale;
907 $main::auth->assert('invoice_edit');
909 if (IS->delete_invoice(\%myconfig, \%$form, $main::spool)) {
911 if(!exists $form->{addition}) {
912 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
913 $form->{addition} = "DELETED";
914 $form->save_history($form->dbconnect(\%myconfig));
916 # /saving the history
917 $form->redirect($locale->text('Invoice deleted!'));
919 $form->error($locale->text('Cannot delete invoice!'));
921 $main::lxdebug->leave_sub();
925 $main::lxdebug->enter_sub();
927 my $form = $main::form;
929 $main::auth->assert('invoice_edit');
934 my $saved_form = save_form();
938 restore_form($saved_form, 0, qw(id invnumber));
943 $main::lxdebug->leave_sub();