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 #======================================================================
39 use List::Util qw(max sum);
41 require "bin/mozilla/io.pl";
42 require "bin/mozilla/invoice_io.pl";
43 require "bin/mozilla/arap.pl";
44 require "bin/mozilla/drafts.pl";
56 $main::lxdebug->enter_sub();
58 my $form = $main::form;
59 my $locale = $main::locale;
61 $main::auth->assert('invoice_edit');
63 return $main::lxdebug->leave_sub() if (load_draft_maybe());
65 if ($form->{type} eq "credit_note") {
66 $form->{title} = $locale->text('Add Credit Note');
68 if ($form->{storno}) {
69 $form->{title} = $locale->text('Add Storno Credit Note');
72 $form->{title} = $locale->text('Add Sales Invoice');
77 $form->{callback} = "$form->{script}?action=add&type=$form->{type}" unless $form->{callback};
79 $form->{jsscript} = "date";
85 $main::lxdebug->leave_sub();
89 $main::lxdebug->enter_sub();
91 my $form = $main::form;
92 my $locale = $main::locale;
94 $main::auth->assert('invoice_edit');
97 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
98 #/show hhistory button
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};
104 $form->{title} = $locale->text('Edit Sales Invoice');
105 $form->{title} = $locale->text('Edit Storno Invoice') if $form->{storno};
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};
117 if ($form->{print_and_post}) {
118 $form->{language_id} = $language_id;
119 $form->{printer_id} = $printer_id;
124 $main::lxdebug->leave_sub();
128 $main::lxdebug->enter_sub();
130 my $form = $main::form;
131 my %myconfig = %main::myconfig;
133 $main::auth->assert('invoice_edit');
135 $form->{vc} = 'customer';
138 $form->{webdav} = $main::webdav;
139 $form->{lizenzen} = $main::lizenzen;
141 $form->create_links("AR", \%myconfig, "customer");
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};
150 $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded cp_id intnotes id shipto_id));
152 IS->get_customer(\%myconfig, \%$form);
154 #quote all_customer Bug 133
155 foreach my $ref (@{ $form->{all_customer} }) {
156 $ref->{name} = $form->quote($ref->{name});
159 # Load data for a specific order and update form fields
160 my $order_data = OE->get_order_data_by_ordnumber(%$form) if $form->{ordnumber};
162 # Copy the fields we need to %form
163 for my $key (qw(payment_id salesman_id orddate taxzone_id quonumber)) {
164 $form->{$key} = $order_data->{$key};
167 $form->restore_vars(qw(id));
169 IS->retrieve_invoice(\%myconfig, \%$form);
170 $form->restore_vars(qw(payment_id language_id taxzone_id intnotes cp_id shipto_id));
171 $form->restore_vars(qw(taxincluded)) if $form->{id};
172 $form->restore_vars(qw(salesman_id)) if $main::editing;
175 # build vendor/customer drop down comatibility... don't ask
176 if (@{ $form->{"all_customer"} }) {
177 $form->{"selectcustomer"} = 1;
178 $form->{customer} = qq|$form->{customer}--$form->{"customer_id"}|;
181 $form->{"oldcustomer"} = $form->{customer};
183 if ($form->{"oldcustomer"} !~ m/--\d+$/ && $form->{"customer_id"}) {
184 $form->{"oldcustomer"} .= qq|--$form->{"customer_id"}|
188 # $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
189 # $form->{selectcustomer} = 1;
191 $form->{employee} = "$form->{employee}--$form->{employee_id}";
194 $form->{forex} = $form->{exchangerate};
195 my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
197 foreach my $key (keys %{ $form->{AR_links} }) {
198 foreach my $ref (@{ $form->{AR_links}{$key} }) {
199 $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}</option>\n";
202 if ($key eq "AR_paid") {
203 next unless $form->{acc_trans}{$key};
204 for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
205 $form->{"AR_paid_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
208 $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
209 $form->{"datepaid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{transdate};
210 $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
211 $form->{"forex_$i"} = $form->{"exchangerate_$i"};
212 $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
213 $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
215 $form->{paidaccounts} = $i;
218 $form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
222 $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
224 $form->{AR} = $form->{AR_1} unless $form->{id};
226 $form->{locked} = ($form->datetonum($form->{invdate}, \%myconfig)
227 <= $form->datetonum($form->{closedto}, \%myconfig));
229 $main::lxdebug->leave_sub();
232 sub prepare_invoice {
233 $main::lxdebug->enter_sub();
235 my $form = $main::form;
236 my %myconfig = %main::myconfig;
238 $main::auth->assert('invoice_edit');
240 if ($form->{type} eq "credit_note") {
241 $form->{type} = "credit_note";
242 $form->{formname} = "credit_note";
244 $form->{type} = "invoice";
245 $form->{formname} = "invoice";
252 foreach my $ref (@{ $form->{invoice_details} }) {
255 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
257 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
258 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
260 my $decimalplaces = ($dec > 2) ? $dec : 2;
262 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
263 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
264 $dec_qty = length $dec_qty;
266 $form->{"lastcost_$i"} = $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, $decimalplaces);
268 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
270 $form->{rowcount} = $i;
274 # get pricegroups for parts
275 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
276 set_pricegroup($_) for 1 .. $form->{rowcount};
278 $main::lxdebug->leave_sub();
282 $main::lxdebug->enter_sub();
284 my $form = $main::form;
285 my %myconfig = %main::myconfig;
286 my $locale = $main::locale;
287 my $cgi = $main::cgi;
289 $main::auth->assert('invoice_edit');
294 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
295 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
297 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
299 my $set_duedate_url = "$form->{script}?action=set_duedate";
301 push @ { $form->{AJAX} }, new CGI::Ajax( 'set_duedate' => $set_duedate_url );
303 my @old_project_ids = ($form->{"globalproject_id"});
304 map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
306 $form->get_lists("contacts" => "ALL_CONTACTS",
307 "shipto" => "ALL_SHIPTO",
308 "projects" => { "key" => "ALL_PROJECTS",
310 "old_id" => \@old_project_ids },
311 "employees" => "ALL_EMPLOYEES",
312 "salesmen" => "ALL_SALESMEN",
313 "taxzones" => "ALL_TAXZONES",
314 "currencies" => "ALL_CURRENCIES",
315 "customers" => "ALL_CUSTOMERS",
316 "departments" => "all_departments",
317 "price_factors" => "ALL_PRICE_FACTORS");
319 $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
320 $TMPL_VAR{shipto_labels} = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
321 $TMPL_VAR{contact_labels} = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
322 $TMPL_VAR{department_labels} = sub { "$_[0]->{description}--$_[0]->{id}" };
325 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
326 $TMPL_VAR{vclimit} = $myconfig{vclimit};
327 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('customer', '', 0, 0)";
328 push @custom_hiddens, "customer_id";
329 push @custom_hiddens, "oldcustomer";
330 push @custom_hiddens, "selectcustomer";
332 # currencies and exchangerate
333 my @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
334 my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
335 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
336 $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
337 $TMPL_VAR{currencies} = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
338 '-values' => \@values, '-labels' => \%labels)) if scalar @values;
339 push @custom_hiddens, "forex";
340 push @custom_hiddens, "exchangerate" if $form->{forex};
342 $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
343 $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
345 $form->{fokus} = "invoice.customer";
347 my $follow_up_vc = $form->{customer};
348 $follow_up_vc =~ s/--\d*\s*$//;
349 $TMPL_VAR{customer_name} = $follow_up_vc;
351 # set option selected
352 foreach my $item (qw(AR)) {
353 $form->{"select$item"} =~ s/ selected//;
354 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
357 $TMPL_VAR{is_type_credit_note} = $form->{type} eq "credit_note";
358 $TMPL_VAR{is_format_html} = $form->{format} eq 'html';
359 $TMPL_VAR{dateformat} = $myconfig{dateformat};
360 $TMPL_VAR{numberformat} = $myconfig{numberformat};
363 $TMPL_VAR{HIDDENS} = [qw(
364 id action type media format queued printed emailed title vc discount
365 title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
366 max_dunning_level dunning_amount
367 shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax
368 shiptoemail shiptodepartment_1 shiptodepartment_2 message email subject cc bcc taxaccounts cursor_fokus
369 convert_from_do_ids convert_from_oe_ids
371 map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
373 $form->{jsscript} = 1;
376 print $form->parse_html_template("is/form_header", \%TMPL_VAR);
378 $main::lxdebug->leave_sub();
382 $main::lxdebug->enter_sub();
384 my $form = $main::form;
385 my %myconfig = %main::myconfig;
386 my $locale = $main::locale;
388 $main::auth->assert('invoice_edit');
390 $form->{invtotal} = $form->{invsubtotal};
391 $form->{oldinvtotal} = $form->{invtotal};
394 $form->{rows} = max 2,
395 $form->numtextrows($form->{notes}, 26, 8),
396 $form->numtextrows($form->{intnotes}, 35, 8);
399 # tax, total and subtotal calculations
400 my ($tax, $subtotal);
401 $form->{taxaccounts_array} = [ split / /, $form->{taxaccounts} ];
403 foreach my $item (@{ $form->{taxaccounts_array} }) {
404 if ($form->{"${item}_base"}) {
405 if ($form->{taxincluded}) {
406 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"}
407 / (1 + $form->{"${item}_rate"})), 2);
408 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
410 $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
411 $form->{invtotal} += $form->{"${item}_total"};
418 $form->{follow_ups} = FU->follow_ups('trans_id' => $form->{id}) || [];
419 $form->{follow_ups_unfinished} = ( sum map { $_->{due} * 1 } @{ $form->{follow_ups} } ) || 0;
424 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
425 $form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];
427 for my $i (1 .. $form->{paidaccounts}) {
428 $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
429 $form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
430 $totalpaid += $form->{"paid_$i"};
433 print $form->parse_html_template('is/form_footer', {
434 is_type_credit_note => ($form->{type} eq "credit_note"),
435 totalpaid => $totalpaid,
436 paid_missing => $form->{invtotal} - $totalpaid,
437 print_options => print_options(inline => 1),
438 show_storno => $form->{id} && !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar") && !$totalpaid,
439 show_delete => ($form->current_date(\%myconfig) eq $form->{gldate}),
441 ##print $form->parse_html_template('is/_payments'); # parser
442 ##print $form->parse_html_template('webdav/_list'); # parser
444 $main::lxdebug->leave_sub();
448 $main::lxdebug->enter_sub();
450 my $form = $main::form;
451 my %myconfig = %main::myconfig;
453 $main::auth->assert('invoice_edit');
455 &mark_as_paid_common(\%myconfig,"ar");
457 $main::lxdebug->leave_sub();
461 $main::lxdebug->enter_sub();
463 my $form = $main::form;
464 my %myconfig = %main::myconfig;
466 $main::auth->assert('invoice_edit');
468 my ($recursive_call) = shift;
470 # map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining) unless $recursive_call;
472 $form->{print_and_post} = 0 if $form->{second_run};
473 my $taxincluded = "checked" if $form->{taxincluded};
476 &check_name("customer");
478 $form->{taxincluded} ||= $taxincluded;
480 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
481 $form->{exchangerate} = $form->{forex} if $form->{forex};
483 for my $i (1 .. $form->{paidaccounts}) {
484 next unless $form->{"paid_$i"};
485 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
486 $form->{"forex_$i"} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
487 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
490 my $i = $form->{rowcount};
491 my $exchangerate = $form->{exchangerate} || 1;
493 # if last row empty, check the form otherwise retrieve new item
494 if ( ($form->{"partnumber_$i"} eq "")
495 && ($form->{"description_$i"} eq "")
496 && ($form->{"partsgroup_$i"} eq "")) {
498 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
503 IS->retrieve_item(\%myconfig, \%$form);
505 my $rows = scalar @{ $form->{item_list} };
507 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{customer_discount} * 100);
510 $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
519 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
521 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
522 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
524 $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
525 $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
527 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
529 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
530 my $decimalplaces = max 2, length $1;
533 $form->{"sellprice_$i"} = $sellprice;
535 # if there is an exchange rate adjust sellprice
536 $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
537 $form->{"sellprice_$i"} /= $exchangerate;
540 $form->{"listprice_$i"} /= $exchangerate;
542 my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
543 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
544 map { $form->{"${_}_base"} += $amount } split / /, $form->{"taxaccounts_$i"};
545 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
547 $form->{creditremaining} -= $amount;
549 map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice listprice lastcost);
551 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
553 if ($main::lizenzen) {
554 if ($form->{"inventory_accno_$i"} ne "") {
555 $form->{"lizenzen_$i"} = qq|<option></option>|;
556 foreach my $item (@{ $form->{LIZENZEN}{ $form->{"id_$i"} } }) {
557 $form->{"lizenzen_$i"} .= qq|<option value="$item->{"id"}">$item->{"licensenumber"}</option>|;
559 $form->{"lizenzen_$i"} .= qq|<option value=-1>Neue Lizenz</option>|;
563 # get pricegroups for parts
564 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
566 # build up html code for prices_$i
574 # ok, so this is a new part
575 # ask if it is a part or service item
577 if ( $form->{"partsgroup_$i"}
578 && ($form->{"partsnumber_$i"} eq "")
579 && ($form->{"description_$i"} eq "")) {
581 $form->{"discount_$i"} = "";
585 $form->{"id_$i"} = 0;
590 $main::lxdebug->leave_sub();
594 $main::lxdebug->enter_sub();
596 my $form = $main::form;
597 my %myconfig = %main::myconfig;
598 my $locale = $main::locale;
600 $main::auth->assert('invoice_edit');
604 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
605 for my $i (1 .. $form->{paidaccounts}) {
606 if ($form->{"paid_$i"}) {
607 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
609 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
611 $form->error($locale->text('Cannot post payment for a closed period!'))
612 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
614 if ($form->{currency} ne $form->{defaultcurrency}) {
615 $form->{"exchangerate_$i"} = $form->{exchangerate}
616 if ($invdate == $datepaid);
617 $form->isblank("exchangerate_$i",
618 $locale->text('Exchangerate for payment missing!'));
623 ($form->{AR}) = split /--/, $form->{AR};
624 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
626 $form->redirect($locale->text('Payment posted!'))
627 if (IS->post_payment(\%myconfig, \%$form));
628 $form->error($locale->text('Cannot post payment!'));
631 $main::lxdebug->leave_sub();
635 $main::lxdebug->enter_sub();
637 my $form = $main::form;
638 my %myconfig = %main::myconfig;
639 my $locale = $main::locale;
641 $main::auth->assert('invoice_edit');
643 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
644 $form->isblank("invdate", $locale->text('Invoice Date missing!'));
645 $form->isblank("customer", $locale->text('Customer missing!'));
646 $form->error($locale->text('Cannot post invoice for a closed period!'))
647 if ($form->date_closed($form->{"invdate"}, \%myconfig));
649 $form->{invnumber} =~ s/^\s*//g;
650 $form->{invnumber} =~ s/\s*$//g;
652 # if oldcustomer ne customer redo form
653 if (&check_name('customer')) {
657 if ($form->{second_run}) {
658 $form->{print_and_post} = 0;
661 remove_emptied_rows();
664 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
665 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
667 $form->error($locale->text('Cannot post invoice for a closed period!'))
668 if ($invdate <= $closedto);
670 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
671 if ($form->{currency} ne $form->{defaultcurrency});
673 for my $i (1 .. $form->{paidaccounts}) {
674 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
675 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
677 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
679 $form->error($locale->text('Cannot post payment for a closed period!'))
680 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
682 if ($form->{currency} ne $form->{defaultcurrency}) {
683 $form->{"exchangerate_$i"} = $form->{exchangerate}
684 if ($invdate == $datepaid);
685 $form->isblank("exchangerate_$i",
686 $locale->text('Exchangerate for payment missing!'));
691 ($form->{AR}) = split /--/, $form->{AR};
692 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
693 $form->{storno} ||= 0;
695 $form->{label} = $locale->text('Invoice');
697 $form->{id} = 0 if $form->{postasnew};
699 # get new invnumber in sequence if no invnumber is given or if posasnew was requested
700 if ($form->{postasnew}) {
701 if ($form->{type} eq "credit_note") {
702 undef($form->{cnnumber});
704 undef($form->{invnumber});
709 $form->error($locale->text('Cannot post invoice!'))
710 unless IS->post_invoice(\%myconfig, \%$form);
711 remove_draft() if $form->{remove_draft};
713 if(!exists $form->{addition}) {
714 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
715 $form->{addition} = $print_post ? "PRINTED AND POSTED" :
716 $form->{storno} ? "STORNO" :
718 $form->save_history($form->dbconnect(\%myconfig));
721 $form->redirect( $form->{label} . " $form->{invnumber} " . $locale->text('posted!'))
724 $main::lxdebug->leave_sub();
728 $main::lxdebug->enter_sub();
730 my $form = $main::form;
732 $main::auth->assert('invoice_edit');
734 my $old_form = new Form;
736 $form->{print_and_post} = 1;
740 $main::lxdebug->leave_sub();
744 sub use_as_template {
745 $main::lxdebug->enter_sub();
747 my $form = $main::form;
748 my %myconfig = %main::myconfig;
750 $main::auth->assert('invoice_edit');
752 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);
753 $form->{paidaccounts} = 1;
755 $form->{invdate} = $form->current_date(\%myconfig);
758 $main::lxdebug->leave_sub();
762 $main::lxdebug->enter_sub();
764 my $form = $main::form;
765 my %myconfig = %main::myconfig;
766 my $locale = $main::locale;
768 $main::auth->assert('invoice_edit');
770 if ($form->{storno}) {
771 $form->error($locale->text('Cannot storno storno invoice!'));
774 if (IS->has_storno(\%myconfig, $form, "ar")) {
775 $form->error($locale->text("Invoice has already been storno'd!"));
778 map({ my $key = $_; delete($form->{$key}) unless (grep({ $key eq $_ } qw(id login password stylesheet type))); } keys(%{ $form }));
784 # Payments must not be recorded for the new storno invoice.
785 $form->{paidaccounts} = 0;
786 map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
788 $form->{storno_id} = $form->{id};
791 $form->{invnumber} = "Storno zu " . $form->{invnumber};
795 $main::lxdebug->leave_sub();
799 $main::lxdebug->enter_sub();
801 my $form = $main::form;
803 $main::auth->assert('invoice_edit');
805 $form->{preview} = 1;
806 my $old_form = new Form;
807 for (keys %$form) { $old_form->{$_} = $form->{$_} }
809 &print_form($old_form);
810 $main::lxdebug->leave_sub();
815 $main::lxdebug->enter_sub();
817 my $form = $main::form;
818 my $locale = $main::locale;
820 $main::auth->assert('invoice_edit');
822 if ($form->{second_run}) {
823 $form->{print_and_post} = 0;
830 <form method="post" action="$form->{script}">
833 # delete action variable
834 map { delete $form->{$_} } qw(action header);
836 foreach my $key (keys %$form) {
837 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
838 $form->{$key} =~ s/\"/"/g;
839 print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
843 <h2 class="confirm">| . $locale->text('Confirm!') . qq|</h2>
846 . $locale->text('Are you sure you want to delete Invoice Number')
847 . qq| $form->{invnumber}
851 <input name="action" class="submit" type="submit" value="|
852 . $locale->text('Yes') . qq|">
856 $main::lxdebug->leave_sub();
860 $main::lxdebug->enter_sub();
862 my $form = $main::form;
863 my %myconfig = %main::myconfig;
864 my $locale = $main::locale;
866 $main::auth->assert('invoice_edit');
868 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
870 $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
877 $form->{title} = $locale->text('Add Credit Note');
878 $form->{script} = 'is.pl';
880 our $buysell = 'buy';
883 # bo creates the id, reset it
884 map { delete $form->{$_} }
885 qw(id invnumber subject message cc bcc printed emailed queued);
886 $form->{ $form->{vc} } =~ s/--.*//g;
887 $form->{type} = "credit_note";
890 map { $form->{"select$_"} = "" } ($form->{vc}, 'currency');
892 # map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
893 # qw(creditlimit creditremaining);
895 my $currency = $form->{currency};
898 $form->{currency} = $currency;
899 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, $buysell);
900 $form->{exchangerate} = $form->{forex} || '';
902 $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
904 # bei Gutschriften werden Zahlungseingänge aus Rechnung nicht übernommen
905 for my $i (1 .. $form->{paidaccounts}) {
906 delete $form->{"paid_$i"};
907 delete $form->{"source_$i"};
908 delete $form->{"memo_$i"};
909 delete $form->{"datepaid_$i"};
910 delete $form->{"AR_paid_$i"};
912 $form->{paidaccounts} = 1;
919 $main::lxdebug->leave_sub();
923 $main::lxdebug->enter_sub();
925 my $form = $main::form;
926 my %myconfig = %main::myconfig;
927 my $locale = $main::locale;
929 $main::auth->assert('invoice_edit');
931 if (IS->delete_invoice(\%myconfig, \%$form, $main::spool)) {
933 if(!exists $form->{addition}) {
934 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
935 $form->{addition} = "DELETED";
936 $form->save_history($form->dbconnect(\%myconfig));
938 # /saving the history
939 $form->redirect($locale->text('Invoice deleted!'));
941 $form->error($locale->text('Cannot delete invoice!'));
943 $main::lxdebug->leave_sub();
947 $main::lxdebug->enter_sub();
949 my $form = $main::form;
951 $main::auth->assert('invoice_edit');
956 my $saved_form = save_form();
960 restore_form($saved_form, 0, qw(id invnumber));
965 $main::lxdebug->leave_sub();