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 my ($language_id, $printer_id);
98 if ($form->{print_and_post}) {
99 $form->{action} = "print";
100 $form->{resubmit} = 1;
101 $language_id = $form->{language_id};
102 $printer_id = $form->{printer_id};
106 if ($form->{type} eq "credit_note") {
107 $form->{title} = $locale->text('Edit Credit Note');
108 $form->{title} = $locale->text('Edit Storno Credit Note') if $form->{storno};
110 $form->{title} = $locale->text('Edit Sales Invoice');
111 $form->{title} = $locale->text('Edit Storno Invoice') if $form->{storno};
115 if ($form->{print_and_post}) {
116 $form->{language_id} = $language_id;
117 $form->{printer_id} = $printer_id;
122 $main::lxdebug->leave_sub();
126 $main::lxdebug->enter_sub();
128 my $form = $main::form;
129 my %myconfig = %main::myconfig;
131 $main::auth->assert('invoice_edit');
133 $form->{vc} = 'customer';
136 $form->{webdav} = $::lx_office_conf{features}->{webdav};
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 currency 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 currency intnotes cp_id shipto_id));
162 $form->restore_vars(qw(taxincluded)) if $form->{id};
163 $form->restore_vars(qw(salesman_id)) if $editing;
166 # build vendor/customer drop down comatibility... don't ask
167 if (@{ $form->{"all_customer"} }) {
168 $form->{"selectcustomer"} = 1;
169 $form->{customer} = qq|$form->{customer}--$form->{"customer_id"}|;
172 $form->{"oldcustomer"} = $form->{customer};
174 if ($form->{"oldcustomer"} !~ m/--\d+$/ && $form->{"customer_id"}) {
175 $form->{"oldcustomer"} .= qq|--$form->{"customer_id"}|
179 # $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
180 # $form->{selectcustomer} = 1;
182 $form->{employee} = "$form->{employee}--$form->{employee_id}";
185 $form->{forex} = $form->{exchangerate};
186 my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
188 foreach my $key (keys %{ $form->{AR_links} }) {
189 foreach my $ref (@{ $form->{AR_links}{$key} }) {
190 $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}</option>\n";
193 if ($key eq "AR_paid") {
194 next unless $form->{acc_trans}{$key};
195 for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
196 $form->{"AR_paid_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
198 $form->{"acc_trans_id_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{acc_trans_id};
200 $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
201 $form->{"datepaid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{transdate};
202 $form->{"gldate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{gldate};
203 $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
204 $form->{"forex_$i"} = $form->{"exchangerate_$i"};
205 $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
206 $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
208 $form->{paidaccounts} = $i;
211 $form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
215 $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
217 $form->{AR} = $form->{AR_1} unless $form->{id};
219 $form->{locked} = ($form->datetonum($form->{invdate}, \%myconfig)
220 <= $form->datetonum($form->{closedto}, \%myconfig));
222 $main::lxdebug->leave_sub();
225 sub prepare_invoice {
226 $main::lxdebug->enter_sub();
228 my $form = $main::form;
229 my %myconfig = %main::myconfig;
231 $main::auth->assert('invoice_edit');
233 if ($form->{type} eq "credit_note") {
234 $form->{type} = "credit_note";
235 $form->{formname} = "credit_note";
237 $form->{type} = "invoice";
238 $form->{formname} = "invoice";
245 foreach my $ref (@{ $form->{invoice_details} }) {
248 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
250 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
251 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
253 my $decimalplaces = ($dec > 2) ? $dec : 2;
255 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
256 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
257 $dec_qty = length $dec_qty;
259 $form->{"lastcost_$i"} = $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, $decimalplaces);
261 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
263 $form->{"sellprice_pg_$i"} = join ('--', $form->{"sellprice_$i"}, $form->{"pricegroup_id_$i"});
265 $form->{rowcount} = $i;
269 # get pricegroups for parts
270 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
272 # Problem: set_pricegroup resets the sellprice of old invoices to the price
273 # currently defined in the pricegroup, which is a problem if the price has
274 # changed, as the old invoice gets the new price
275 # set_pricegroup must never be called, when an old invoice is initially loaded
277 # set_pricegroup($_) for 1 .. $form->{rowcount};
279 $main::lxdebug->leave_sub();
283 $main::lxdebug->enter_sub();
285 my $form = $main::form;
286 my %myconfig = %main::myconfig;
287 my $locale = $main::locale;
288 my $cgi = $::request->{cgi};
290 $main::auth->assert('invoice_edit');
295 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
296 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
298 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
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("projects" => { "key" => "ALL_PROJECTS",
305 "old_id" => \@old_project_ids },
306 "taxzones" => "ALL_TAXZONES",
307 "currencies" => "ALL_CURRENCIES",
308 "customers" => "ALL_CUSTOMERS",
309 "departments" => "all_departments",
310 "price_factors" => "ALL_PRICE_FACTORS");
312 $TMPL_VAR{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $::form->{employee_id}, deleted => 0 ] ]);
313 $TMPL_VAR{ALL_SALESMEN} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $::form->{salesman_id}, deleted => 0 ] ]);
314 $TMPL_VAR{ALL_SHIPTO} = SL::DB::Manager::Shipto->get_all(query => [
315 or => [ trans_id => $::form->{"$::form->{vc}_id"} * 1, and => [ shipto_id => $::form->{shipto_id} * 1, trans_id => undef ] ]
317 $TMPL_VAR{ALL_CONTACTS} = SL::DB::Manager::Contact->get_all(query => [
319 cp_cv_id => $::form->{"$::form->{vc}_id"} * 1,
322 cp_id => $::form->{cp_id} * 1
326 $TMPL_VAR{department_labels} = sub { "$_[0]->{description}--$_[0]->{id}" };
329 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
330 $TMPL_VAR{vclimit} = $myconfig{vclimit};
331 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('customer', '', 0, 0)";
332 push @custom_hiddens, "customer_id";
333 push @custom_hiddens, "oldcustomer";
334 push @custom_hiddens, "selectcustomer";
336 # currencies and exchangerate
337 my @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
338 my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
339 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
340 $form->{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
341 $TMPL_VAR{currencies} = NTI($::request->{cgi}->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
342 '-values' => \@values, '-labels' => \%labels,
343 '-onchange' => "document.getElementById('update_button').click();"
344 )) if scalar @values;
345 push @custom_hiddens, "forex";
346 push @custom_hiddens, "exchangerate" if $form->{forex};
348 $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
349 $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
351 $form->{fokus} = "invoice.customer";
353 my $follow_up_vc = $form->{customer};
354 $follow_up_vc =~ s/--\d*\s*$//;
355 $TMPL_VAR{customer_name} = $follow_up_vc;
357 # set option selected
358 foreach my $item (qw(AR)) {
359 $form->{"select$item"} =~ s/ selected//;
360 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
363 $TMPL_VAR{is_type_credit_note} = $form->{type} eq "credit_note";
364 $TMPL_VAR{is_format_html} = $form->{format} eq 'html';
365 $TMPL_VAR{dateformat} = $myconfig{dateformat};
366 $TMPL_VAR{numberformat} = $myconfig{numberformat};
369 $TMPL_VAR{HIDDENS} = [qw(
370 id action type media format queued printed emailed title vc discount
371 title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
372 max_dunning_level dunning_amount
373 shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax
374 shiptoemail shiptodepartment_1 shiptodepartment_2 shiptocp_gender message email subject cc bcc taxaccounts cursor_fokus
375 convert_from_do_ids convert_from_oe_ids
377 map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
379 $form->{jsscript} = 1;
382 print $form->parse_html_template("is/form_header", \%TMPL_VAR);
384 $main::lxdebug->leave_sub();
388 $main::lxdebug->enter_sub();
390 my $form = $main::form;
391 my %myconfig = %main::myconfig;
392 my $locale = $main::locale;
394 $main::auth->assert('invoice_edit');
396 $form->{invtotal} = $form->{invsubtotal};
399 $form->{rows} = max 2,
400 $form->numtextrows($form->{notes}, 26, 8),
401 $form->numtextrows($form->{intnotes}, 35, 8);
404 # tax, total and subtotal calculations
405 my ($tax, $subtotal);
406 $form->{taxaccounts_array} = [ split / /, $form->{taxaccounts} ];
408 IS->get_customer(\%myconfig, \%$form) if $form->{type} =~ /sales_(order|quotation)/;
410 if ( $form->{vc} eq 'customer' && !$form->{taxincluded_changed_by_user} ) {
411 if ( $form->{taxincluded_checked} eq 'y' ) {
412 $form->{taxincluded} = 1;
413 } elsif ( $form->{taxincluded_checked} eq 'n' ) {
414 $form->{taxincluded} = 0;
416 $form->{taxincluded} = $myconfig{taxincluded_checked};
420 foreach my $item (@{ $form->{taxaccounts_array} }) {
421 if ($form->{"${item}_base"}) {
422 if ($form->{taxincluded}) {
423 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"}
424 / (1 + $form->{"${item}_rate"})), 2);
425 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
427 $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
428 $form->{invtotal} += $form->{"${item}_total"};
435 $form->{follow_ups} = FU->follow_ups('trans_id' => $form->{id}) || [];
436 $form->{follow_ups_unfinished} = ( sum map { $_->{due} * 1 } @{ $form->{follow_ups} } ) || 0;
441 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
442 $form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];
444 # Standard Konto für Umlaufvermögen
445 my $accno_arap = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
447 for my $i (1 .. $form->{paidaccounts}) {
448 $form->{"changeable_$i"} = 1;
449 if ($::lx_office_conf{features}->{payments_changeable} == 0) {
451 $form->{"changeable_$i"} = ($form->{"acc_trans_id_$i"})? 0 : 1;
452 } elsif ($::lx_office_conf{features}->{payments_changeable} == 2) {
454 $form->{"changeable_$i"} = (($form->{"gldate_$i"} eq '') ||
455 ($form->current_date(\%myconfig) eq $form->{"gldate_$i"}));
458 $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
459 if (!$form->{"AR_paid_$i"}) {
460 $form->{"selectAR_paid_$i"} =~ s/option>$accno_arap--(.*?)</option selected>$accno_arap--$1</;
462 $form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
465 $totalpaid += $form->{"paid_$i"};
468 $form->{oldinvtotal} = $form->{invtotal};
470 print $form->parse_html_template('is/form_footer', {
471 is_type_credit_note => ($form->{type} eq "credit_note"),
472 totalpaid => $totalpaid,
473 paid_missing => $form->{invtotal} - $totalpaid,
474 print_options => print_options(inline => 1),
475 show_storno => $form->{id} && !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar") && !$totalpaid,
476 show_delete => ($form->current_date(\%myconfig) eq $form->{gldate}),
478 ##print $form->parse_html_template('is/_payments'); # parser
479 ##print $form->parse_html_template('webdav/_list'); # parser
481 $main::lxdebug->leave_sub();
485 $main::lxdebug->enter_sub();
487 my $form = $main::form;
488 my %myconfig = %main::myconfig;
490 $main::auth->assert('invoice_edit');
492 &mark_as_paid_common(\%myconfig,"ar");
494 $main::lxdebug->leave_sub();
498 $main::lxdebug->enter_sub();
500 my $form = $main::form;
501 my %myconfig = %main::myconfig;
503 $main::auth->assert('invoice_edit');
505 my ($recursive_call) = @_;
507 $form->{print_and_post} = 0 if $form->{second_run};
508 my $taxincluded = $form->{taxincluded} ? "checked" : '';
511 &check_name("customer");
513 $form->{taxincluded} ||= $taxincluded;
515 if (!$form->{forex}) { # read exchangerate from input field (not hidden)
516 $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate}) unless $recursive_call;
518 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
519 $form->{exchangerate} = $form->{forex} if $form->{forex};
521 for my $i (1 .. $form->{paidaccounts}) {
522 next unless $form->{"paid_$i"};
523 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
524 $form->{"forex_$i"} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
525 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
528 my $i = $form->{rowcount};
529 my $exchangerate = $form->{exchangerate} || 1;
531 # if last row empty, check the form otherwise retrieve new item
532 if ( ($form->{"partnumber_$i"} eq "")
533 && ($form->{"description_$i"} eq "")
534 && ($form->{"partsgroup_$i"} eq "")) {
536 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
541 IS->retrieve_item(\%myconfig, \%$form);
543 my $rows = scalar @{ $form->{item_list} };
545 # Falls kein Kundenrabatt vorhanden ist, den aktuellen Rabatt nicht mit 0% überschreiben,
546 # da hier der Anwender schon manual einen Wert eingetragen haben könnte (analog zu qty) Bugfix: 1412
547 if ($form->{customer_discount}){
548 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{customer_discount} * 100);
552 $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
556 select_item(mode => 'IS');
561 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
563 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
564 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
566 $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
567 $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
569 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
571 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
572 my $decimalplaces = max 2, length $1;
575 $form->{"sellprice_$i"} = $sellprice;
577 # if there is an exchange rate adjust sellprice
578 $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
579 $form->{"sellprice_$i"} /= $exchangerate;
582 $form->{"listprice_$i"} /= $exchangerate;
584 my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
585 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
586 map { $form->{"${_}_base"} += $amount } split / /, $form->{"taxaccounts_$i"};
587 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
589 $form->{creditremaining} -= $amount;
591 map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice lastcost);
593 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
595 # get pricegroups for parts
596 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
598 # build up html code for prices_$i
606 # ok, so this is a new part
607 # ask if it is a part or service item
609 if ( $form->{"partsgroup_$i"}
610 && ($form->{"partsnumber_$i"} eq "")
611 && ($form->{"description_$i"} eq "")) {
613 $form->{"discount_$i"} = "";
617 $form->{"id_$i"} = 0;
622 $main::lxdebug->leave_sub();
626 $main::lxdebug->enter_sub();
628 my $form = $main::form;
629 my %myconfig = %main::myconfig;
630 my $locale = $main::locale;
632 $main::auth->assert('invoice_edit');
634 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
636 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
637 for my $i (1 .. $form->{paidaccounts}) {
638 if ($form->{"paid_$i"}) {
639 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
641 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
644 if ($form->{currency} ne $form->{defaultcurrency}) {
645 $form->{"exchangerate_$i"} = $form->{exchangerate}
646 if ($invdate == $datepaid);
647 $form->isblank("exchangerate_$i",
648 $locale->text('Exchangerate for payment missing!'));
652 # Abgeschlossene Zeiträume nur für den letzten (aktuellen) Zahlungseingang prüfen
653 # Details s.a. Bug 1502
654 # Das Problem ist jetzt, dass man Zahlungseingänge nachträglich ändern kann
655 # Wobei dies für Installationen die sowieso nicht mit Bücherkontrolle arbeiten keinen
656 # keinen Unterschied macht.
657 # Optimal wäre, wenn gegen einen Zeitstempel des Zahlungsfelds geprüft würde ...
658 # Das Problem hierbei ist, dass in IS.pm post_invoice IMMER alle Zahlungseingänge aus $form
659 # erneut gespeichert werden. Prinzipiell wäre es besser NUR die Änderungen des Rechnungs-
660 # belegs (neue Zahlung aber nichts anderes) zu speichern ...
661 # Vielleicht könnte man ähnlich wie bei Rechnung löschen verfahren
662 $form->error($locale->text('Cannot post payment for a closed period!'))
663 if ($form->date_closed($form->{"datepaid_$form->{paidaccounts}"}, \%myconfig));
665 ($form->{AR}) = split /--/, $form->{AR};
666 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
668 $form->redirect($locale->text('Payment posted!'))
669 if (IS->post_payment(\%myconfig, \%$form));
670 $form->error($locale->text('Cannot post payment!'));
673 $main::lxdebug->leave_sub();
677 $main::lxdebug->enter_sub();
679 my $form = $main::form;
680 my %myconfig = %main::myconfig;
681 my $locale = $main::locale;
683 $main::auth->assert('invoice_edit');
685 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
686 $form->isblank("invdate", $locale->text('Invoice Date missing!'));
687 $form->isblank("customer", $locale->text('Customer missing!'));
688 $form->error($locale->text('Cannot post invoice for a closed period!'))
689 if ($form->date_closed($form->{"invdate"}, \%myconfig));
691 $form->{invnumber} =~ s/^\s*//g;
692 $form->{invnumber} =~ s/\s*$//g;
694 # if oldcustomer ne customer redo form
695 if (&check_name('customer')) {
700 if ($myconfig{mandatory_departments} && !$form->{department_id}) {
701 $form->{saved_message} = $::locale->text('You have to specify a department.');
706 if ($form->{second_run}) {
707 $form->{print_and_post} = 0;
710 remove_emptied_rows();
713 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
714 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
716 $form->error($locale->text('Cannot post invoice for a closed period!'))
717 if ($invdate <= $closedto);
719 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
720 if ($form->{currency} ne $form->{defaultcurrency});
722 for my $i (1 .. $form->{paidaccounts}) {
723 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
724 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
726 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
728 $form->error($locale->text('Cannot post payment for a closed period!'))
729 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
731 if ($form->{currency} ne $form->{defaultcurrency}) {
732 $form->{"exchangerate_$i"} = $form->{exchangerate}
733 if ($invdate == $datepaid);
734 $form->isblank("exchangerate_$i",
735 $locale->text('Exchangerate for payment missing!'));
740 ($form->{AR}) = split /--/, $form->{AR};
741 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
742 $form->{storno} ||= 0;
744 $form->{label} = $form->{type} eq 'credit_note' ? $locale->text('Credit Note') : $locale->text('Invoice');
746 $form->{id} = 0 if $form->{postasnew};
748 # get new invnumber in sequence if no invnumber is given or if posasnew was requested
749 if ($form->{postasnew}) {
750 if ($form->{type} eq "credit_note") {
751 undef($form->{cnnumber});
753 undef($form->{invnumber});
758 $form->error($locale->text('Cannot post invoice!'))
759 unless IS->post_invoice(\%myconfig, \%$form);
760 remove_draft() if $form->{remove_draft};
762 if(!exists $form->{addition}) {
763 $form->{snumbers} = 'invnumber' .'_'. $form->{invnumber}; # ($form->{type} eq 'credit_note' ? 'cnnumber' : 'invnumber') .'_'. $form->{invnumber};
764 $form->{addition} = $form->{print_and_post} ? "PRINTED AND POSTED" :
765 $form->{storno} ? "STORNO" :
770 if (!$form->{no_redirect_after_post}) {
771 $form->{action} = 'edit';
772 $form->{script} = 'is.pl';
773 $form->{saved_message} = $form->{label} . " $form->{invnumber} " . $locale->text('posted!');
774 $form->{callback} = build_std_url(qw(action edit id saved_message));
778 $main::lxdebug->leave_sub();
782 $main::lxdebug->enter_sub();
784 my $form = $main::form;
786 $main::auth->assert('invoice_edit');
788 my $old_form = new Form;
789 $form->{no_redirect_after_post} = 1;
790 $form->{print_and_post} = 1;
794 $main::lxdebug->leave_sub();
798 sub use_as_template {
799 $main::lxdebug->enter_sub();
801 my $form = $main::form;
802 my %myconfig = %main::myconfig;
804 $main::auth->assert('invoice_edit');
806 map { delete $form->{$_} } qw(printed emailed queued invnumber invdate deliverydate id datepaid_1 gldate_1 acc_trans_id_1 source_1 memo_1 paid_1 exchangerate_1 AP_paid_1 storno);
807 $form->{paidaccounts} = 1;
809 $form->{invdate} = $form->current_date(\%myconfig);
811 # remember pricegroups for "use as template"
812 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
813 set_pricegroup($_) for 1 .. $form->{rowcount};
817 $main::lxdebug->leave_sub();
821 $main::lxdebug->enter_sub();
823 my $form = $main::form;
824 my %myconfig = %main::myconfig;
825 my $locale = $main::locale;
827 $main::auth->assert('invoice_edit');
829 if ($form->{storno}) {
830 $form->error($locale->text('Cannot storno storno invoice!'));
833 if (IS->has_storno(\%myconfig, $form, "ar")) {
834 $form->error($locale->text("Invoice has already been storno'd!"));
837 map({ my $key = $_; delete($form->{$key}) unless (grep({ $key eq $_ } qw(id login password stylesheet type))); } keys(%{ $form }));
843 # Payments must not be recorded for the new storno invoice.
844 $form->{paidaccounts} = 0;
845 map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ gldate_ acc_trans_id_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
847 $form->{storno_id} = $form->{id};
850 $form->{invnumber} = "Storno zu " . $form->{invnumber};
854 $main::lxdebug->leave_sub();
858 $main::lxdebug->enter_sub();
860 my $form = $main::form;
862 $main::auth->assert('invoice_edit');
864 $form->{preview} = 1;
865 my $old_form = new Form;
866 for (keys %$form) { $old_form->{$_} = $form->{$_} }
868 &print_form($old_form);
869 $main::lxdebug->leave_sub();
874 $main::lxdebug->enter_sub();
876 my $form = $main::form;
877 my $locale = $main::locale;
879 $main::auth->assert('invoice_edit');
881 if ($form->{second_run}) {
882 $form->{print_and_post} = 0;
889 <form method="post" action="$form->{script}">
892 # delete action variable
893 map { delete $form->{$_} } qw(action header);
895 foreach my $key (keys %$form) {
896 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
897 $form->{$key} =~ s/\"/"/g;
898 print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
902 <h2 class="confirm">| . $locale->text('Confirm!') . qq|</h2>
905 . $locale->text('Are you sure you want to delete Invoice Number')
906 . qq| $form->{invnumber}
910 <input name="action" class="submit" type="submit" value="|
911 . $locale->text('Yes') . qq|">
915 $main::lxdebug->leave_sub();
919 $main::lxdebug->enter_sub();
921 my $form = $main::form;
922 my %myconfig = %main::myconfig;
923 my $locale = $main::locale;
925 $main::auth->assert('invoice_edit');
927 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
929 $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
936 $form->{title} = $locale->text('Add Credit Note');
937 $form->{script} = 'is.pl';
939 # Bei Gutschriften bezug zur Rechnungsnummer
940 $form->{invnumber_for_credit_note} = $form->{invnumber};
941 # bo creates the id, reset it
942 map { delete $form->{$_} }
943 qw(id invnumber subject message cc bcc printed emailed queued);
944 $form->{ $form->{vc} } =~ s/--.*//g;
945 $form->{type} = "credit_note";
948 map { $form->{"select$_"} = "" } ($form->{vc}, 'currency');
950 # map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
951 # qw(creditlimit creditremaining);
953 for my $i (1 .. $form->{rowcount}) {
954 for (qw(listprice)) {
955 $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
959 my $currency = $form->{currency};
962 $form->{currency} = $currency;
963 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, 'buy');
964 $form->{exchangerate} = $form->{forex} || '';
966 $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
968 # bei Gutschriften werden Zahlungseingänge aus Rechnung nicht übernommen
969 for my $i (1 .. $form->{paidaccounts}) {
970 delete $form->{"paid_$i"};
971 delete $form->{"source_$i"};
972 delete $form->{"memo_$i"};
973 delete $form->{"datepaid_$i"};
974 delete $form->{"gldate_$i"};
975 delete $form->{"acc_trans_id_$i"};
976 delete $form->{"AR_paid_$i"};
978 $form->{paidaccounts} = 1;
985 $main::lxdebug->leave_sub();
989 $main::lxdebug->enter_sub();
991 my $form = $main::form;
992 my %myconfig = %main::myconfig;
993 my $locale = $main::locale;
995 $main::auth->assert('invoice_edit');
997 if (IS->delete_invoice(\%myconfig, \%$form)) {
999 if(!exists $form->{addition}) {
1000 $form->{snumbers} = 'invnumber' .'_'. $form->{invnumber}; # ($form->{type} eq 'credit_note' ? 'cnnumber' : 'invnumber') .'_'. $form->{invnumber};
1001 $form->{addition} = "DELETED";
1002 $form->save_history;
1004 # /saving the history
1005 $form->redirect($locale->text('Invoice deleted!'));
1007 $form->error($locale->text('Cannot delete invoice!'));
1009 $main::lxdebug->leave_sub();
1013 $main::lxdebug->enter_sub();
1015 my $form = $main::form;
1017 $main::auth->assert('invoice_edit');
1020 $form->{no_redirect_after_post} = 1;
1022 my $saved_form = save_form();
1026 restore_form($saved_form, 0, qw(id invnumber));
1031 $main::lxdebug->leave_sub();