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";
53 $main::lxdebug->enter_sub();
55 my $form = $main::form;
56 my $locale = $main::locale;
58 $main::auth->assert('invoice_edit');
60 return $main::lxdebug->leave_sub() if (load_draft_maybe());
62 if ($form->{type} eq "credit_note") {
63 $form->{title} = $locale->text('Add Credit Note');
65 if ($form->{storno}) {
66 $form->{title} = $locale->text('Add Storno Credit Note');
69 $form->{title} = $locale->text('Add Sales Invoice');
74 $form->{callback} = "$form->{script}?action=add&type=$form->{type}" unless $form->{callback};
76 $form->{jsscript} = "date";
82 $main::lxdebug->leave_sub();
86 $main::lxdebug->enter_sub();
88 my $form = $main::form;
89 my $locale = $main::locale;
91 $main::auth->assert('invoice_edit');
94 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
95 #/show hhistory button
97 if ($form->{type} eq "credit_note") {
98 $form->{title} = $locale->text('Edit Credit Note');
99 $form->{title} = $locale->text('Edit Storno Credit Note') if $form->{storno};
101 $form->{title} = $locale->text('Edit Sales Invoice');
102 $form->{title} = $locale->text('Edit Storno Invoice') if $form->{storno};
105 my ($language_id, $printer_id);
106 if ($form->{print_and_post}) {
107 $form->{action} = "print";
108 $form->{resubmit} = 1;
109 $language_id = $form->{language_id};
110 $printer_id = $form->{printer_id};
114 if ($form->{print_and_post}) {
115 $form->{language_id} = $language_id;
116 $form->{printer_id} = $printer_id;
121 $main::lxdebug->leave_sub();
125 $main::lxdebug->enter_sub();
127 my $form = $main::form;
128 my %myconfig = %main::myconfig;
130 $main::auth->assert('invoice_edit');
132 $form->{vc} = 'customer';
135 $form->{webdav} = $main::webdav;
136 $form->{lizenzen} = $main::lizenzen;
138 $form->create_links("AR", \%myconfig, "customer");
140 if ($form->{all_customer}) {
141 unless ($form->{customer_id}) {
142 $form->{customer_id} = $form->{all_customer}->[0]->{id};
143 $form->{salesman_id} = $form->{all_customer}->[0]->{salesman_id};
147 $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded cp_id intnotes id shipto_id));
149 IS->get_customer(\%myconfig, \%$form);
151 #quote all_customer Bug 133
152 foreach my $ref (@{ $form->{all_customer} }) {
153 $ref->{name} = $form->quote($ref->{name});
156 # Load data for a specific order and update form fields
157 my $order_data = OE->get_order_data_by_ordnumber(%$form) if $form->{ordnumber};
159 # Copy the fields we need to %form
160 for my $key (qw(payment_id salesman_id orddate taxzone_id quonumber)) {
161 $form->{$key} = $order_data->{$key};
164 $form->restore_vars(qw(id));
166 IS->retrieve_invoice(\%myconfig, \%$form);
167 $form->restore_vars(qw(payment_id language_id taxzone_id intnotes cp_id shipto_id));
168 $form->restore_vars(qw(taxincluded)) if $form->{id};
169 $form->restore_vars(qw(salesman_id)) if $main::editing;
172 # build vendor/customer drop down comatibility... don't ask
173 if (@{ $form->{"all_customer"} }) {
174 $form->{"selectcustomer"} = 1;
175 $form->{customer} = qq|$form->{customer}--$form->{"customer_id"}|;
178 $form->{"oldcustomer"} = $form->{customer};
180 if ($form->{"oldcustomer"} !~ m/--\d+$/ && $form->{"customer_id"}) {
181 $form->{"oldcustomer"} .= qq|--$form->{"customer_id"}|
185 # $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
186 # $form->{selectcustomer} = 1;
188 $form->{employee} = "$form->{employee}--$form->{employee_id}";
191 $form->{forex} = $form->{exchangerate};
192 my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
194 foreach my $key (keys %{ $form->{AR_links} }) {
195 foreach my $ref (@{ $form->{AR_links}{$key} }) {
196 $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}</option>\n";
199 if ($key eq "AR_paid") {
200 next unless $form->{acc_trans}{$key};
201 for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
202 $form->{"AR_paid_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
205 $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
206 $form->{"datepaid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{transdate};
207 $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
208 $form->{"forex_$i"} = $form->{"exchangerate_$i"};
209 $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
210 $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
212 $form->{paidaccounts} = $i;
215 $form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
219 $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
221 $form->{AR} = $form->{AR_1} unless $form->{id};
223 $form->{locked} = ($form->datetonum($form->{invdate}, \%myconfig)
224 <= $form->datetonum($form->{closedto}, \%myconfig));
226 $main::lxdebug->leave_sub();
229 sub prepare_invoice {
230 $main::lxdebug->enter_sub();
232 my $form = $main::form;
233 my %myconfig = %main::myconfig;
235 $main::auth->assert('invoice_edit');
237 if ($form->{type} eq "credit_note") {
238 $form->{type} = "credit_note";
239 $form->{formname} = "credit_note";
241 $form->{type} = "invoice";
242 $form->{formname} = "invoice";
249 foreach my $ref (@{ $form->{invoice_details} }) {
252 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
254 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
255 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
257 my $decimalplaces = ($dec > 2) ? $dec : 2;
259 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
260 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
261 $dec_qty = length $dec_qty;
263 $form->{"lastcost_$i"} = $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, $decimalplaces);
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);
296 my $set_duedate_url = "$form->{script}?action=set_duedate";
298 push @ { $form->{AJAX} }, new CGI::Ajax( 'set_duedate' => $set_duedate_url );
300 my @old_project_ids = ($form->{"globalproject_id"});
301 map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
303 $form->get_lists("contacts" => "ALL_CONTACTS",
304 "shipto" => "ALL_SHIPTO",
305 "projects" => { "key" => "ALL_PROJECTS",
307 "old_id" => \@old_project_ids },
308 "employees" => "ALL_EMPLOYEES",
309 "salesmen" => "ALL_SALESMEN",
310 "taxzones" => "ALL_TAXZONES",
311 "currencies" => "ALL_CURRENCIES",
312 "customers" => "ALL_CUSTOMERS",
313 "departments" => "all_departments",
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 lastcost);
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');
599 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
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')) {
655 if ($myconfig{mandatory_departments} && !$form->{department_id}) {
656 $form->{saved_message} = $::locale->text('You have to specify a department.');
661 if ($form->{second_run}) {
662 $form->{print_and_post} = 0;
665 remove_emptied_rows();
668 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
669 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
671 $form->error($locale->text('Cannot post invoice for a closed period!'))
672 if ($invdate <= $closedto);
674 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
675 if ($form->{currency} ne $form->{defaultcurrency});
677 for my $i (1 .. $form->{paidaccounts}) {
678 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
679 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
681 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
683 $form->error($locale->text('Cannot post payment for a closed period!'))
684 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
686 if ($form->{currency} ne $form->{defaultcurrency}) {
687 $form->{"exchangerate_$i"} = $form->{exchangerate}
688 if ($invdate == $datepaid);
689 $form->isblank("exchangerate_$i",
690 $locale->text('Exchangerate for payment missing!'));
695 ($form->{AR}) = split /--/, $form->{AR};
696 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
697 $form->{storno} ||= 0;
699 $form->{label} = $locale->text('Invoice');
701 $form->{id} = 0 if $form->{postasnew};
703 # get new invnumber in sequence if no invnumber is given or if posasnew was requested
704 if ($form->{postasnew}) {
705 if ($form->{type} eq "credit_note") {
706 undef($form->{cnnumber});
708 undef($form->{invnumber});
713 $form->error($locale->text('Cannot post invoice!'))
714 unless IS->post_invoice(\%myconfig, \%$form);
715 remove_draft() if $form->{remove_draft};
717 if(!exists $form->{addition}) {
718 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
719 $form->{addition} = $form->{print_and_post} ? "PRINTED AND POSTED" :
720 $form->{storno} ? "STORNO" :
722 $form->save_history($form->dbconnect(\%myconfig));
725 # macnetix: after invoice->Post, redirect to edit.
726 if (!$form->{no_redirect_after_post}) {
727 $form->{action} = 'edit';
728 $form->{script} = 'is.pl';
729 $form->{saved_message} = $form->{label} . " $form->{invnumber} " . $locale->text('posted!');
730 $form->{callback} = build_std_url(qw(action edit id saved_message));
734 $main::lxdebug->leave_sub();
738 $main::lxdebug->enter_sub();
740 my $form = $main::form;
742 $main::auth->assert('invoice_edit');
744 my $old_form = new Form;
745 $form->{no_redirect_after_post} = 1;
746 $form->{print_and_post} = 1;
750 $main::lxdebug->leave_sub();
754 sub use_as_template {
755 $main::lxdebug->enter_sub();
757 my $form = $main::form;
758 my %myconfig = %main::myconfig;
760 $main::auth->assert('invoice_edit');
762 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);
763 $form->{paidaccounts} = 1;
765 $form->{invdate} = $form->current_date(\%myconfig);
768 $main::lxdebug->leave_sub();
772 $main::lxdebug->enter_sub();
774 my $form = $main::form;
775 my %myconfig = %main::myconfig;
776 my $locale = $main::locale;
778 $main::auth->assert('invoice_edit');
780 if ($form->{storno}) {
781 $form->error($locale->text('Cannot storno storno invoice!'));
784 if (IS->has_storno(\%myconfig, $form, "ar")) {
785 $form->error($locale->text("Invoice has already been storno'd!"));
788 map({ my $key = $_; delete($form->{$key}) unless (grep({ $key eq $_ } qw(id login password stylesheet type))); } keys(%{ $form }));
794 # Payments must not be recorded for the new storno invoice.
795 $form->{paidaccounts} = 0;
796 map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
798 $form->{storno_id} = $form->{id};
801 $form->{invnumber} = "Storno zu " . $form->{invnumber};
805 $main::lxdebug->leave_sub();
809 $main::lxdebug->enter_sub();
811 my $form = $main::form;
813 $main::auth->assert('invoice_edit');
815 $form->{preview} = 1;
816 my $old_form = new Form;
817 for (keys %$form) { $old_form->{$_} = $form->{$_} }
819 &print_form($old_form);
820 $main::lxdebug->leave_sub();
825 $main::lxdebug->enter_sub();
827 my $form = $main::form;
828 my $locale = $main::locale;
830 $main::auth->assert('invoice_edit');
832 if ($form->{second_run}) {
833 $form->{print_and_post} = 0;
840 <form method="post" action="$form->{script}">
843 # delete action variable
844 map { delete $form->{$_} } qw(action header);
846 foreach my $key (keys %$form) {
847 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
848 $form->{$key} =~ s/\"/"/g;
849 print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
853 <h2 class="confirm">| . $locale->text('Confirm!') . qq|</h2>
856 . $locale->text('Are you sure you want to delete Invoice Number')
857 . qq| $form->{invnumber}
861 <input name="action" class="submit" type="submit" value="|
862 . $locale->text('Yes') . qq|">
866 $main::lxdebug->leave_sub();
870 $main::lxdebug->enter_sub();
872 my $form = $main::form;
873 my %myconfig = %main::myconfig;
874 my $locale = $main::locale;
876 $main::auth->assert('invoice_edit');
878 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
880 $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
887 $form->{title} = $locale->text('Add Credit Note');
888 $form->{script} = 'is.pl';
891 # bo creates the id, reset it
892 map { delete $form->{$_} }
893 qw(id invnumber subject message cc bcc printed emailed queued);
894 $form->{ $form->{vc} } =~ s/--.*//g;
895 $form->{type} = "credit_note";
898 map { $form->{"select$_"} = "" } ($form->{vc}, 'currency');
900 # map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
901 # qw(creditlimit creditremaining);
903 my $currency = $form->{currency};
906 $form->{currency} = $currency;
907 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, 'buy');
908 $form->{exchangerate} = $form->{forex} || '';
910 $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
912 # bei Gutschriften werden Zahlungseingänge aus Rechnung nicht übernommen
913 for my $i (1 .. $form->{paidaccounts}) {
914 delete $form->{"paid_$i"};
915 delete $form->{"source_$i"};
916 delete $form->{"memo_$i"};
917 delete $form->{"datepaid_$i"};
918 delete $form->{"AR_paid_$i"};
920 $form->{paidaccounts} = 1;
927 $main::lxdebug->leave_sub();
931 $main::lxdebug->enter_sub();
933 my $form = $main::form;
934 my %myconfig = %main::myconfig;
935 my $locale = $main::locale;
937 $main::auth->assert('invoice_edit');
939 if (IS->delete_invoice(\%myconfig, \%$form, $main::spool)) {
941 if(!exists $form->{addition}) {
942 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
943 $form->{addition} = "DELETED";
944 $form->save_history($form->dbconnect(\%myconfig));
946 # /saving the history
947 $form->redirect($locale->text('Invoice deleted!'));
949 $form->error($locale->text('Cannot delete invoice!'));
951 $main::lxdebug->leave_sub();
955 $main::lxdebug->enter_sub();
957 my $form = $main::form;
959 $main::auth->assert('invoice_edit');
962 $form->{no_redirect_after_post} = 1;
964 my $saved_form = save_form();
968 restore_form($saved_form, 0, qw(id invnumber));
973 $main::lxdebug->leave_sub();