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 my $editing = $form->{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 = OE->get_order_data_by_ordnumber(%$form) if $form->{ordnumber};
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 $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->{"lastcost_$i"} = $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, $decimalplaces);
267 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
269 $form->{rowcount} = $i;
273 # get pricegroups for parts
274 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
275 set_pricegroup($_) for 1 .. $form->{rowcount};
277 $main::lxdebug->leave_sub();
281 $main::lxdebug->enter_sub();
283 my $form = $main::form;
284 my %myconfig = %main::myconfig;
285 my $locale = $main::locale;
286 my $cgi = $main::cgi;
288 $main::auth->assert('invoice_edit');
293 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
294 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
296 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
298 my $set_duedate_url = "$form->{script}?action=set_duedate";
300 push @ { $form->{AJAX} }, new CGI::Ajax( 'set_duedate' => $set_duedate_url );
302 my @old_project_ids = ($form->{"globalproject_id"});
303 map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
305 $form->get_lists("contacts" => "ALL_CONTACTS",
306 "shipto" => "ALL_SHIPTO",
307 "projects" => { "key" => "ALL_PROJECTS",
309 "old_id" => \@old_project_ids },
310 "employees" => "ALL_EMPLOYEES",
311 "salesmen" => "ALL_SALESMEN",
312 "taxzones" => "ALL_TAXZONES",
313 "currencies" => "ALL_CURRENCIES",
314 "customers" => "ALL_CUSTOMERS",
315 "departments" => "all_departments",
316 "price_factors" => "ALL_PRICE_FACTORS");
318 $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
319 $TMPL_VAR{shipto_labels} = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
320 $TMPL_VAR{contact_labels} = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
321 $TMPL_VAR{department_labels} = sub { "$_[0]->{description}--$_[0]->{id}" };
324 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
325 $TMPL_VAR{vclimit} = $myconfig{vclimit};
326 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('customer', '', 0, 0)";
327 push @custom_hiddens, "customer_id";
328 push @custom_hiddens, "oldcustomer";
329 push @custom_hiddens, "selectcustomer";
331 # currencies and exchangerate
332 my @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
333 my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
334 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
335 $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
336 $TMPL_VAR{currencies} = NTI($::cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
337 '-values' => \@values, '-labels' => \%labels)) if scalar @values;
338 push @custom_hiddens, "forex";
339 push @custom_hiddens, "exchangerate" if $form->{forex};
341 $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
342 $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
344 $form->{fokus} = "invoice.customer";
346 my $follow_up_vc = $form->{customer};
347 $follow_up_vc =~ s/--\d*\s*$//;
348 $TMPL_VAR{customer_name} = $follow_up_vc;
350 # set option selected
351 foreach my $item (qw(AR)) {
352 $form->{"select$item"} =~ s/ selected//;
353 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
356 $TMPL_VAR{is_type_credit_note} = $form->{type} eq "credit_note";
357 $TMPL_VAR{is_format_html} = $form->{format} eq 'html';
358 $TMPL_VAR{dateformat} = $myconfig{dateformat};
359 $TMPL_VAR{numberformat} = $myconfig{numberformat};
362 $TMPL_VAR{HIDDENS} = [qw(
363 id action type media format queued printed emailed title vc discount
364 title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
365 max_dunning_level dunning_amount
366 shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax
367 shiptoemail shiptodepartment_1 shiptodepartment_2 message email subject cc bcc taxaccounts cursor_fokus
368 convert_from_do_ids convert_from_oe_ids
370 map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
372 $form->{jsscript} = 1;
375 print $form->parse_html_template("is/form_header", \%TMPL_VAR);
377 $main::lxdebug->leave_sub();
381 $main::lxdebug->enter_sub();
383 my $form = $main::form;
384 my %myconfig = %main::myconfig;
385 my $locale = $main::locale;
387 $main::auth->assert('invoice_edit');
389 $form->{invtotal} = $form->{invsubtotal};
390 $form->{oldinvtotal} = $form->{invtotal};
393 $form->{rows} = max 2,
394 $form->numtextrows($form->{notes}, 26, 8),
395 $form->numtextrows($form->{intnotes}, 35, 8);
398 # tax, total and subtotal calculations
399 my ($tax, $subtotal);
400 $form->{taxaccounts_array} = [ split / /, $form->{taxaccounts} ];
402 foreach my $item (@{ $form->{taxaccounts_array} }) {
403 if ($form->{"${item}_base"}) {
404 if ($form->{taxincluded}) {
405 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"}
406 / (1 + $form->{"${item}_rate"})), 2);
407 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
409 $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
410 $form->{invtotal} += $form->{"${item}_total"};
417 $form->{follow_ups} = FU->follow_ups('trans_id' => $form->{id}) || [];
418 $form->{follow_ups_unfinished} = ( sum map { $_->{due} * 1 } @{ $form->{follow_ups} } ) || 0;
423 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
424 $form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];
426 # Standard Konto für Umlaufvermögen
427 my $accno_arap = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
429 for my $i (1 .. $form->{paidaccounts}) {
430 $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
431 if (!$form->{"AR_paid_$i"}) {
432 $form->{"selectAR_paid_$i"} =~ s/option>$accno_arap--(.*?)</option selected>$accno_arap--$1</;
434 $form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
437 $totalpaid += $form->{"paid_$i"};
440 print $form->parse_html_template('is/form_footer', {
441 is_type_credit_note => ($form->{type} eq "credit_note"),
442 totalpaid => $totalpaid,
443 paid_missing => $form->{invtotal} - $totalpaid,
444 print_options => print_options(inline => 1),
445 show_storno => $form->{id} && !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar") && !$totalpaid,
446 show_delete => ($form->current_date(\%myconfig) eq $form->{gldate}),
448 ##print $form->parse_html_template('is/_payments'); # parser
449 ##print $form->parse_html_template('webdav/_list'); # parser
451 $main::lxdebug->leave_sub();
455 $main::lxdebug->enter_sub();
457 my $form = $main::form;
458 my %myconfig = %main::myconfig;
460 $main::auth->assert('invoice_edit');
462 &mark_as_paid_common(\%myconfig,"ar");
464 $main::lxdebug->leave_sub();
468 $main::lxdebug->enter_sub();
470 my $form = $main::form;
471 my %myconfig = %main::myconfig;
473 $main::auth->assert('invoice_edit');
475 my ($recursive_call) = shift;
477 # map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining) unless $recursive_call;
479 $form->{print_and_post} = 0 if $form->{second_run};
480 my $taxincluded = "checked" if $form->{taxincluded};
483 &check_name("customer");
485 $form->{taxincluded} ||= $taxincluded;
487 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
488 $form->{exchangerate} = $form->{forex} if $form->{forex};
490 for my $i (1 .. $form->{paidaccounts}) {
491 next unless $form->{"paid_$i"};
492 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
493 $form->{"forex_$i"} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
494 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
497 my $i = $form->{rowcount};
498 my $exchangerate = $form->{exchangerate} || 1;
500 # if last row empty, check the form otherwise retrieve new item
501 if ( ($form->{"partnumber_$i"} eq "")
502 && ($form->{"description_$i"} eq "")
503 && ($form->{"partsgroup_$i"} eq "")) {
505 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
510 IS->retrieve_item(\%myconfig, \%$form);
512 my $rows = scalar @{ $form->{item_list} };
514 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{customer_discount} * 100);
517 $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
526 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
528 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
529 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
531 $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
532 $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
534 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
536 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
537 my $decimalplaces = max 2, length $1;
540 $form->{"sellprice_$i"} = $sellprice;
542 # if there is an exchange rate adjust sellprice
543 $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
544 $form->{"sellprice_$i"} /= $exchangerate;
547 $form->{"listprice_$i"} /= $exchangerate;
549 my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
550 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
551 map { $form->{"${_}_base"} += $amount } split / /, $form->{"taxaccounts_$i"};
552 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
554 $form->{creditremaining} -= $amount;
556 map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice listprice lastcost);
558 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
560 if ($main::lizenzen) {
561 if ($form->{"inventory_accno_$i"} ne "") {
562 $form->{"lizenzen_$i"} = qq|<option></option>|;
563 foreach my $item (@{ $form->{LIZENZEN}{ $form->{"id_$i"} } }) {
564 $form->{"lizenzen_$i"} .= qq|<option value="$item->{"id"}">$item->{"licensenumber"}</option>|;
566 $form->{"lizenzen_$i"} .= qq|<option value=-1>Neue Lizenz</option>|;
570 # get pricegroups for parts
571 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
573 # build up html code for prices_$i
581 # ok, so this is a new part
582 # ask if it is a part or service item
584 if ( $form->{"partsgroup_$i"}
585 && ($form->{"partsnumber_$i"} eq "")
586 && ($form->{"description_$i"} eq "")) {
588 $form->{"discount_$i"} = "";
592 $form->{"id_$i"} = 0;
597 $main::lxdebug->leave_sub();
601 $main::lxdebug->enter_sub();
603 my $form = $main::form;
604 my %myconfig = %main::myconfig;
605 my $locale = $main::locale;
607 $main::auth->assert('invoice_edit');
609 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
611 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
612 for my $i (1 .. $form->{paidaccounts}) {
613 if ($form->{"paid_$i"}) {
614 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
616 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
619 if ($form->{currency} ne $form->{defaultcurrency}) {
620 $form->{"exchangerate_$i"} = $form->{exchangerate}
621 if ($invdate == $datepaid);
622 $form->isblank("exchangerate_$i",
623 $locale->text('Exchangerate for payment missing!'));
627 # Abgeschlossene Zeiträume nur für den letzten (aktuellen) Zahlungseingang prüfen
628 # Details s.a. Bug 1502
629 # Das Problem ist jetzt, dass man Zahlungseingänge nachträglich ändern kann
630 # Wobei dies für Installationen die sowieso nicht mit Bücherkontrolle arbeiten keinen
631 # keinen Unterschied macht.
632 # Optimal wäre, wenn gegen einen Zeitstempel des Zahlungsfelds geprüft würde ...
633 # Das Problem hierbei ist, dass in IS.pm post_invoice IMMER alle Zahlungseingänge aus $form
634 # erneut gespeichert werden. Prinzipiell wäre es besser NUR die Änderungen des Rechnungs-
635 # belegs (neue Zahlung aber nichts anderes) zu speichern ...
636 # Vielleicht könnte man ähnlich wie bei Rechnung löschen verfahren
637 $form->error($locale->text('Cannot post payment for a closed period!'))
638 if ($form->date_closed($form->{"datepaid_$form->{paidaccounts}"}, \%myconfig));
640 ($form->{AR}) = split /--/, $form->{AR};
641 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
643 $form->redirect($locale->text('Payment posted!'))
644 if (IS->post_payment(\%myconfig, \%$form));
645 $form->error($locale->text('Cannot post payment!'));
648 $main::lxdebug->leave_sub();
652 $main::lxdebug->enter_sub();
654 my $form = $main::form;
655 my %myconfig = %main::myconfig;
656 my $locale = $main::locale;
658 $main::auth->assert('invoice_edit');
660 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
661 $form->isblank("invdate", $locale->text('Invoice Date missing!'));
662 $form->isblank("customer", $locale->text('Customer missing!'));
663 $form->error($locale->text('Cannot post invoice for a closed period!'))
664 if ($form->date_closed($form->{"invdate"}, \%myconfig));
666 $form->{invnumber} =~ s/^\s*//g;
667 $form->{invnumber} =~ s/\s*$//g;
669 # if oldcustomer ne customer redo form
670 if (&check_name('customer')) {
675 if ($myconfig{mandatory_departments} && !$form->{department_id}) {
676 $form->{saved_message} = $::locale->text('You have to specify a department.');
681 if ($form->{second_run}) {
682 $form->{print_and_post} = 0;
685 remove_emptied_rows();
688 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
689 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
691 $form->error($locale->text('Cannot post invoice for a closed period!'))
692 if ($invdate <= $closedto);
694 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
695 if ($form->{currency} ne $form->{defaultcurrency});
697 for my $i (1 .. $form->{paidaccounts}) {
698 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
699 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
701 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
703 $form->error($locale->text('Cannot post payment for a closed period!'))
704 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
706 if ($form->{currency} ne $form->{defaultcurrency}) {
707 $form->{"exchangerate_$i"} = $form->{exchangerate}
708 if ($invdate == $datepaid);
709 $form->isblank("exchangerate_$i",
710 $locale->text('Exchangerate for payment missing!'));
715 ($form->{AR}) = split /--/, $form->{AR};
716 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
717 $form->{storno} ||= 0;
719 $form->{label} = $locale->text('Invoice');
721 $form->{id} = 0 if $form->{postasnew};
723 # get new invnumber in sequence if no invnumber is given or if posasnew was requested
724 if ($form->{postasnew}) {
725 if ($form->{type} eq "credit_note") {
726 undef($form->{cnnumber});
728 undef($form->{invnumber});
733 $form->error($locale->text('Cannot post invoice!'))
734 unless IS->post_invoice(\%myconfig, \%$form);
735 remove_draft() if $form->{remove_draft};
737 if(!exists $form->{addition}) {
738 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
739 $form->{addition} = $form->{print_and_post} ? "PRINTED AND POSTED" :
740 $form->{storno} ? "STORNO" :
745 if (!$form->{no_redirect_after_post}) {
746 $form->{action} = 'edit';
747 $form->{script} = 'is.pl';
748 $form->{saved_message} = $form->{label} . " $form->{invnumber} " . $locale->text('posted!');
749 $form->{callback} = build_std_url(qw(action edit id saved_message));
753 $main::lxdebug->leave_sub();
757 $main::lxdebug->enter_sub();
759 my $form = $main::form;
761 $main::auth->assert('invoice_edit');
763 my $old_form = new Form;
764 $form->{no_redirect_after_post} = 1;
765 $form->{print_and_post} = 1;
769 $main::lxdebug->leave_sub();
773 sub use_as_template {
774 $main::lxdebug->enter_sub();
776 my $form = $main::form;
777 my %myconfig = %main::myconfig;
779 $main::auth->assert('invoice_edit');
781 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);
782 $form->{paidaccounts} = 1;
784 $form->{invdate} = $form->current_date(\%myconfig);
787 $main::lxdebug->leave_sub();
791 $main::lxdebug->enter_sub();
793 my $form = $main::form;
794 my %myconfig = %main::myconfig;
795 my $locale = $main::locale;
797 $main::auth->assert('invoice_edit');
799 if ($form->{storno}) {
800 $form->error($locale->text('Cannot storno storno invoice!'));
803 if (IS->has_storno(\%myconfig, $form, "ar")) {
804 $form->error($locale->text("Invoice has already been storno'd!"));
807 map({ my $key = $_; delete($form->{$key}) unless (grep({ $key eq $_ } qw(id login password stylesheet type))); } keys(%{ $form }));
813 # Payments must not be recorded for the new storno invoice.
814 $form->{paidaccounts} = 0;
815 map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
817 $form->{storno_id} = $form->{id};
820 $form->{invnumber} = "Storno zu " . $form->{invnumber};
824 $main::lxdebug->leave_sub();
828 $main::lxdebug->enter_sub();
830 my $form = $main::form;
832 $main::auth->assert('invoice_edit');
834 $form->{preview} = 1;
835 my $old_form = new Form;
836 for (keys %$form) { $old_form->{$_} = $form->{$_} }
838 &print_form($old_form);
839 $main::lxdebug->leave_sub();
844 $main::lxdebug->enter_sub();
846 my $form = $main::form;
847 my $locale = $main::locale;
849 $main::auth->assert('invoice_edit');
851 if ($form->{second_run}) {
852 $form->{print_and_post} = 0;
859 <form method="post" action="$form->{script}">
862 # delete action variable
863 map { delete $form->{$_} } qw(action header);
865 foreach my $key (keys %$form) {
866 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
867 $form->{$key} =~ s/\"/"/g;
868 print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
872 <h2 class="confirm">| . $locale->text('Confirm!') . qq|</h2>
875 . $locale->text('Are you sure you want to delete Invoice Number')
876 . qq| $form->{invnumber}
880 <input name="action" class="submit" type="submit" value="|
881 . $locale->text('Yes') . qq|">
885 $main::lxdebug->leave_sub();
889 $main::lxdebug->enter_sub();
891 my $form = $main::form;
892 my %myconfig = %main::myconfig;
893 my $locale = $main::locale;
895 $main::auth->assert('invoice_edit');
897 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
899 $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
906 $form->{title} = $locale->text('Add Credit Note');
907 $form->{script} = 'is.pl';
910 # bo creates the id, reset it
911 map { delete $form->{$_} }
912 qw(id invnumber subject message cc bcc printed emailed queued);
913 $form->{ $form->{vc} } =~ s/--.*//g;
914 $form->{type} = "credit_note";
917 map { $form->{"select$_"} = "" } ($form->{vc}, 'currency');
919 # map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
920 # qw(creditlimit creditremaining);
922 my $currency = $form->{currency};
925 $form->{currency} = $currency;
926 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, 'buy');
927 $form->{exchangerate} = $form->{forex} || '';
929 $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
931 # bei Gutschriften werden Zahlungseingänge aus Rechnung nicht übernommen
932 for my $i (1 .. $form->{paidaccounts}) {
933 delete $form->{"paid_$i"};
934 delete $form->{"source_$i"};
935 delete $form->{"memo_$i"};
936 delete $form->{"datepaid_$i"};
937 delete $form->{"AR_paid_$i"};
939 $form->{paidaccounts} = 1;
946 $main::lxdebug->leave_sub();
950 $main::lxdebug->enter_sub();
952 my $form = $main::form;
953 my %myconfig = %main::myconfig;
954 my $locale = $main::locale;
956 $main::auth->assert('invoice_edit');
958 if (IS->delete_invoice(\%myconfig, \%$form, $main::spool)) {
960 if(!exists $form->{addition}) {
961 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
962 $form->{addition} = "DELETED";
965 # /saving the history
966 $form->redirect($locale->text('Invoice deleted!'));
968 $form->error($locale->text('Cannot delete invoice!'));
970 $main::lxdebug->leave_sub();
974 $main::lxdebug->enter_sub();
976 my $form = $main::form;
978 $main::auth->assert('invoice_edit');
981 $form->{no_redirect_after_post} = 1;
983 my $saved_form = save_form();
987 restore_form($saved_form, 0, qw(id invnumber));
992 $main::lxdebug->leave_sub();