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);
44 require "bin/mozilla/io.pl";
45 require "bin/mozilla/invoice_io.pl";
46 require "bin/mozilla/arap.pl";
47 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');
96 $form->{taxincluded_changed_by_user} = 1;
99 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
100 #/show hhistory button
102 my ($language_id, $printer_id);
103 if ($form->{print_and_post}) {
104 $form->{action} = "print";
105 $form->{resubmit} = 1;
106 $language_id = $form->{language_id};
107 $printer_id = $form->{printer_id};
111 if ($form->{type} eq "credit_note") {
112 $form->{title} = $locale->text('Edit Credit Note');
113 $form->{title} = $locale->text('Edit Storno Credit Note') if $form->{storno};
115 $form->{title} = $locale->text('Edit Sales Invoice');
116 $form->{title} = $locale->text('Edit Storno Invoice') if $form->{storno};
120 if ($form->{print_and_post}) {
121 $form->{language_id} = $language_id;
122 $form->{printer_id} = $printer_id;
127 $main::lxdebug->leave_sub();
131 $main::lxdebug->enter_sub();
133 my $form = $main::form;
134 my %myconfig = %main::myconfig;
136 $main::auth->assert('invoice_edit');
138 $form->{vc} = 'customer';
141 $form->{webdav} = $::lx_office_conf{features}->{webdav};
143 $form->create_links("AR", \%myconfig, "customer");
145 if ($form->{all_customer}) {
146 unless ($form->{customer_id}) {
147 $form->{customer_id} = $form->{all_customer}->[0]->{id};
148 $form->{salesman_id} = $form->{all_customer}->[0]->{salesman_id};
152 my $editing = $form->{id};
154 $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded currency cp_id intnotes id shipto_id));
156 IS->get_customer(\%myconfig, \%$form);
158 #quote all_customer Bug 133
159 foreach my $ref (@{ $form->{all_customer} }) {
160 $ref->{name} = $form->quote($ref->{name});
163 $form->restore_vars(qw(id));
165 IS->retrieve_invoice(\%myconfig, \%$form);
166 $form->restore_vars(qw(payment_id language_id taxzone_id currency intnotes cp_id shipto_id));
167 $form->restore_vars(qw(taxincluded)) if $form->{id};
168 $form->restore_vars(qw(salesman_id)) if $editing;
171 # build vendor/customer drop down comatibility... don't ask
172 if (@{ $form->{"all_customer"} }) {
173 $form->{"selectcustomer"} = 1;
174 $form->{customer} = qq|$form->{customer}--$form->{"customer_id"}|;
177 $form->{"oldcustomer"} = $form->{customer};
179 if ($form->{"oldcustomer"} !~ m/--\d+$/ && $form->{"customer_id"}) {
180 $form->{"oldcustomer"} .= qq|--$form->{"customer_id"}|
184 # $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
185 # $form->{selectcustomer} = 1;
187 $form->{employee} = "$form->{employee}--$form->{employee_id}";
190 $form->{forex} = $form->{exchangerate};
191 my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
193 foreach my $key (keys %{ $form->{AR_links} }) {
194 foreach my $ref (@{ $form->{AR_links}{$key} }) {
195 $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}</option>\n";
198 if ($key eq "AR_paid") {
199 next unless $form->{acc_trans}{$key};
200 for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
201 $form->{"AR_paid_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
203 $form->{"acc_trans_id_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{acc_trans_id};
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->{"gldate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{gldate};
208 $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
209 $form->{"forex_$i"} = $form->{"exchangerate_$i"};
210 $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
211 $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
213 $form->{paidaccounts} = $i;
216 $form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
220 $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
222 $form->{AR} = $form->{AR_1} unless $form->{id};
224 $form->{locked} = ($form->datetonum($form->{invdate}, \%myconfig)
225 <= $form->datetonum($form->{closedto}, \%myconfig));
227 $main::lxdebug->leave_sub();
230 sub prepare_invoice {
231 $main::lxdebug->enter_sub();
233 my $form = $main::form;
234 my %myconfig = %main::myconfig;
236 $main::auth->assert('invoice_edit');
238 if ($form->{type} eq "credit_note") {
239 $form->{type} = "credit_note";
240 $form->{formname} = "credit_note";
242 $form->{type} = "invoice";
243 $form->{formname} = "invoice";
250 foreach my $ref (@{ $form->{invoice_details} }) {
253 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
255 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
256 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
258 my $decimalplaces = ($dec > 2) ? $dec : 2;
260 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
261 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
262 $dec_qty = length $dec_qty;
264 $form->{"lastcost_$i"} = $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, $decimalplaces);
266 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
268 $form->{"sellprice_pg_$i"} = join ('--', $form->{"sellprice_$i"}, $form->{"pricegroup_id_$i"});
270 $form->{rowcount} = $i;
274 # get pricegroups for parts
275 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
277 # Problem: set_pricegroup resets the sellprice of old invoices to the price
278 # currently defined in the pricegroup, which is a problem if the price has
279 # changed, as the old invoice gets the new price
280 # set_pricegroup must never be called, when an old invoice is initially loaded
282 # set_pricegroup($_) for 1 .. $form->{rowcount};
284 $main::lxdebug->leave_sub();
288 $main::lxdebug->enter_sub();
290 my $form = $main::form;
291 my %myconfig = %main::myconfig;
292 my $locale = $main::locale;
293 my $cgi = $::request->{cgi};
295 $main::auth->assert('invoice_edit');
300 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
301 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
303 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
305 my @old_project_ids = ($form->{"globalproject_id"});
306 map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
308 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
310 "old_id" => \@old_project_ids },
311 "taxzones" => "ALL_TAXZONES",
312 "currencies" => "ALL_CURRENCIES",
313 "customers" => "ALL_CUSTOMERS",
314 "departments" => "all_departments",
315 "price_factors" => "ALL_PRICE_FACTORS");
317 $TMPL_VAR{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $::form->{employee_id}, deleted => 0 ] ]);
318 $TMPL_VAR{ALL_SALESMEN} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $::form->{salesman_id}, deleted => 0 ] ]);
319 $TMPL_VAR{ALL_SHIPTO} = SL::DB::Manager::Shipto->get_all(query => [
320 or => [ trans_id => $::form->{"$::form->{vc}_id"} * 1, and => [ shipto_id => $::form->{shipto_id} * 1, trans_id => undef ] ]
322 $TMPL_VAR{ALL_CONTACTS} = SL::DB::Manager::Contact->get_all(query => [
324 cp_cv_id => $::form->{"$::form->{vc}_id"} * 1,
327 cp_id => $::form->{cp_id} * 1
331 $TMPL_VAR{department_labels} = sub { "$_[0]->{description}--$_[0]->{id}" };
334 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
335 $TMPL_VAR{vclimit} = $myconfig{vclimit};
336 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('customer', '', 0, 0)";
337 push @custom_hiddens, "customer_id";
338 push @custom_hiddens, "oldcustomer";
339 push @custom_hiddens, "selectcustomer";
341 # currencies and exchangerate
342 my @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
343 my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
344 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
345 $form->{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
346 $TMPL_VAR{currencies} = NTI($::request->{cgi}->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
347 '-values' => \@values, '-labels' => \%labels,
348 '-onchange' => "document.getElementById('update_button').click();"
349 )) if scalar @values;
350 push @custom_hiddens, "forex";
351 push @custom_hiddens, "exchangerate" if $form->{forex};
353 $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
354 $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
356 $::request->{layout}->focus('#customer');
358 my $follow_up_vc = $form->{customer};
359 $follow_up_vc =~ s/--\d*\s*$//;
360 $TMPL_VAR{customer_name} = $follow_up_vc;
362 # set option selected
363 foreach my $item (qw(AR)) {
364 $form->{"select$item"} =~ s/ selected//;
365 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
368 $TMPL_VAR{is_type_credit_note} = $form->{type} eq "credit_note";
369 $TMPL_VAR{is_format_html} = $form->{format} eq 'html';
370 $TMPL_VAR{dateformat} = $myconfig{dateformat};
371 $TMPL_VAR{numberformat} = $myconfig{numberformat};
374 $TMPL_VAR{HIDDENS} = [qw(
375 id action type media format queued printed emailed title vc discount
376 title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
377 max_dunning_level dunning_amount
378 shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax
379 shiptoemail shiptodepartment_1 shiptodepartment_2 shiptocp_gender message email subject cc bcc taxaccounts cursor_fokus
380 convert_from_do_ids convert_from_oe_ids
382 map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
384 $form->{jsscript} = 1;
385 $::request->layout->use_stylesheet('presenter/record/record_list.css');
388 print $form->parse_html_template("is/form_header", \%TMPL_VAR);
390 $main::lxdebug->leave_sub();
394 $main::lxdebug->enter_sub();
396 my $form = $main::form;
397 my %myconfig = %main::myconfig;
398 my $locale = $main::locale;
400 $main::auth->assert('invoice_edit');
402 $form->{invtotal} = $form->{invsubtotal};
405 $form->{rows} = max 2,
406 $form->numtextrows($form->{notes}, 26, 8),
407 $form->numtextrows($form->{intnotes}, 35, 8);
410 # tax, total and subtotal calculations
411 my ($tax, $subtotal);
412 $form->{taxaccounts_array} = [ split(/ /, $form->{taxaccounts}) ];
414 if( $form->{customer_id} && !$form->{taxincluded_changed_by_user} ) {
415 my $customer = SL::DB::Customer->new(id => $form->{customer_id})->load();
416 $form->{taxincluded} = defined($customer->taxincluded_checked) ? $customer->taxincluded_checked : $myconfig{taxincluded_checked};
419 foreach my $item (@{ $form->{taxaccounts_array} }) {
420 if ($form->{"${item}_base"}) {
421 if ($form->{taxincluded}) {
422 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"}
423 / (1 + $form->{"${item}_rate"})), 2);
424 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
426 $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
427 $form->{invtotal} += $form->{"${item}_total"};
434 $form->{follow_ups} = FU->follow_ups('trans_id' => $form->{id}) || [];
435 $form->{follow_ups_unfinished} = ( sum map { $_->{due} * 1 } @{ $form->{follow_ups} } ) || 0;
440 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
441 $form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];
443 # Standard Konto für Umlaufvermögen
444 my $accno_arap = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
446 for my $i (1 .. $form->{paidaccounts}) {
447 $form->{"changeable_$i"} = 1;
448 if (SL::DB::Default->get->payments_changeable == 0) {
450 $form->{"changeable_$i"} = ($form->{"acc_trans_id_$i"})? 0 : 1;
451 } elsif (SL::DB::Default->get->payments_changeable == 2) {
453 $form->{"changeable_$i"} = (($form->{"gldate_$i"} eq '') ||
454 ($form->current_date(\%myconfig) eq $form->{"gldate_$i"}));
457 $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
458 if (!$form->{"AR_paid_$i"}) {
459 $form->{"selectAR_paid_$i"} =~ s/option>$accno_arap--(.*?)</option selected>$accno_arap--$1</;
461 $form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
464 $totalpaid += $form->{"paid_$i"};
467 $form->{oldinvtotal} = $form->{invtotal};
469 print $form->parse_html_template('is/form_footer', {
470 is_type_credit_note => ($form->{type} eq "credit_note"),
471 totalpaid => $totalpaid,
472 paid_missing => $form->{invtotal} - $totalpaid,
473 print_options => print_options(inline => 1),
474 show_storno => $form->{id} && !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar") && !$totalpaid,
475 show_delete => ($::instance_conf->get_is_changeable == 2)
476 ? ($form->current_date(\%myconfig) eq $form->{gldate})
477 : ($::instance_conf->get_is_changeable == 1),
479 ##print $form->parse_html_template('is/_payments'); # parser
480 ##print $form->parse_html_template('webdav/_list'); # parser
482 $main::lxdebug->leave_sub();
486 $main::lxdebug->enter_sub();
488 my $form = $main::form;
489 my %myconfig = %main::myconfig;
491 $main::auth->assert('invoice_edit');
493 &mark_as_paid_common(\%myconfig,"ar");
495 $main::lxdebug->leave_sub();
499 $main::lxdebug->enter_sub();
501 my $form = $main::form;
502 my %myconfig = %main::myconfig;
504 $main::auth->assert('invoice_edit');
506 my ($recursive_call) = @_;
508 $form->{print_and_post} = 0 if $form->{second_run};
509 my $taxincluded = $form->{taxincluded} ? "checked" : '';
512 &check_name("customer");
514 $form->{taxincluded} ||= $taxincluded;
516 if (!$form->{forex}) { # read exchangerate from input field (not hidden)
517 $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate}) unless $recursive_call;
519 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
520 $form->{exchangerate} = $form->{forex} if $form->{forex};
522 for my $i (1 .. $form->{paidaccounts}) {
523 next unless $form->{"paid_$i"};
524 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
525 if (!$form->{"forex_$i"}) { #read exchangerate from input field (not hidden)
526 $form->{exchangerate} = $form->{"exchangerate_$i"};
528 $form->{"forex_$i"} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
529 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
532 my $i = $form->{rowcount};
533 my $exchangerate = $form->{exchangerate} || 1;
535 # if last row empty, check the form otherwise retrieve new item
536 if ( ($form->{"partnumber_$i"} eq "")
537 && ($form->{"description_$i"} eq "")
538 && ($form->{"partsgroup_$i"} eq "")) {
540 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
545 IS->retrieve_item(\%myconfig, \%$form);
547 my $rows = scalar @{ $form->{item_list} };
549 # Falls kein Kundenrabatt vorhanden ist, den aktuellen Rabatt nicht mit 0% überschreiben,
550 # da hier der Anwender schon manual einen Wert eingetragen haben könnte (analog zu qty) Bugfix: 1412
551 if ($form->{customer_discount}){
552 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{customer_discount} * 100);
556 $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
560 select_item(mode => 'IS');
565 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
567 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
568 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
570 $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
571 $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
573 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
575 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
576 my $decimalplaces = max 2, length $1;
579 $form->{"sellprice_$i"} = $sellprice;
581 # if there is an exchange rate adjust sellprice
582 $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
583 $form->{"sellprice_$i"} /= $exchangerate;
586 $form->{"listprice_$i"} /= $exchangerate;
588 my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
589 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
590 map { $form->{"${_}_base"} += $amount } split / /, $form->{"taxaccounts_$i"};
591 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
593 $form->{creditremaining} -= $amount;
595 map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice lastcost);
597 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
599 # get pricegroups for parts
600 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
602 # build up html code for prices_$i
610 # ok, so this is a new part
611 # ask if it is a part or service item
613 if ( $form->{"partsgroup_$i"}
614 && ($form->{"partsnumber_$i"} eq "")
615 && ($form->{"description_$i"} eq "")) {
617 $form->{"discount_$i"} = "";
621 $form->{"id_$i"} = 0;
626 $main::lxdebug->leave_sub();
630 $main::lxdebug->enter_sub();
632 my $form = $main::form;
633 my %myconfig = %main::myconfig;
634 my $locale = $main::locale;
636 $main::auth->assert('invoice_edit');
638 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
640 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
641 for my $i (1 .. $form->{paidaccounts}) {
642 if ($form->{"paid_$i"}) {
643 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
645 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
648 if ($form->{currency} ne $form->{defaultcurrency}) {
649 $form->{"exchangerate_$i"} = $form->{exchangerate}
650 if ($invdate == $datepaid);
651 $form->isblank("exchangerate_$i",
652 $locale->text('Exchangerate for payment missing!'));
656 # Abgeschlossene Zeiträume nur für den letzten (aktuellen) Zahlungseingang prüfen
657 # Details s.a. Bug 1502
658 # Das Problem ist jetzt, dass man Zahlungseingänge nachträglich ändern kann
659 # Wobei dies für Installationen die sowieso nicht mit Bücherkontrolle arbeiten keinen
660 # keinen Unterschied macht.
661 # Optimal wäre, wenn gegen einen Zeitstempel des Zahlungsfelds geprüft würde ...
662 # Das Problem hierbei ist, dass in IS.pm post_invoice IMMER alle Zahlungseingänge aus $form
663 # erneut gespeichert werden. Prinzipiell wäre es besser NUR die Änderungen des Rechnungs-
664 # belegs (neue Zahlung aber nichts anderes) zu speichern ...
665 # Vielleicht könnte man ähnlich wie bei Rechnung löschen verfahren
666 $form->error($locale->text('Cannot post payment for a closed period!'))
667 if ($form->date_closed($form->{"datepaid_$form->{paidaccounts}"}, \%myconfig));
669 ($form->{AR}) = split /--/, $form->{AR};
670 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
672 $form->redirect($locale->text('Payment posted!'))
673 if (IS->post_payment(\%myconfig, \%$form));
674 $form->error($locale->text('Cannot post payment!'));
677 $main::lxdebug->leave_sub();
681 $main::lxdebug->enter_sub();
683 my $form = $main::form;
684 my %myconfig = %main::myconfig;
685 my $locale = $main::locale;
687 $main::auth->assert('invoice_edit');
689 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
690 $form->isblank("invdate", $locale->text('Invoice Date missing!'));
691 $form->isblank("customer", $locale->text('Customer missing!'));
692 $form->error($locale->text('Cannot post invoice for a closed period!'))
693 if ($form->date_closed($form->{"invdate"}, \%myconfig));
695 $form->{invnumber} =~ s/^\s*//g;
696 $form->{invnumber} =~ s/\s*$//g;
698 # if oldcustomer ne customer redo form
699 if (&check_name('customer')) {
704 if ($myconfig{mandatory_departments} && !$form->{department_id}) {
705 $form->{saved_message} = $::locale->text('You have to specify a department.');
710 if ($form->{second_run}) {
711 $form->{print_and_post} = 0;
714 remove_emptied_rows();
717 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
718 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
720 $form->error($locale->text('Cannot post invoice for a closed period!'))
721 if ($invdate <= $closedto);
723 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
724 if ($form->{currency} ne $form->{defaultcurrency});
726 for my $i (1 .. $form->{paidaccounts}) {
727 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
728 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
730 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
732 $form->error($locale->text('Cannot post payment for a closed period!'))
733 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
735 if ($form->{currency} ne $form->{defaultcurrency}) {
736 $form->{"exchangerate_$i"} = $form->{exchangerate}
737 if ($invdate == $datepaid);
738 $form->isblank("exchangerate_$i",
739 $locale->text('Exchangerate for payment missing!'));
744 ($form->{AR}) = split /--/, $form->{AR};
745 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
746 $form->{storno} ||= 0;
748 $form->{label} = $form->{type} eq 'credit_note' ? $locale->text('Credit Note') : $locale->text('Invoice');
750 $form->{id} = 0 if $form->{postasnew};
752 # get new invnumber in sequence if no invnumber is given or if posasnew was requested
753 if ($form->{postasnew}) {
754 if ($form->{type} eq "credit_note") {
755 undef($form->{cnnumber});
757 undef($form->{invnumber});
762 $form->error($locale->text('Cannot post invoice!'))
763 unless IS->post_invoice(\%myconfig, \%$form);
764 remove_draft() if $form->{remove_draft};
766 if(!exists $form->{addition}) {
767 $form->{snumbers} = 'invnumber' .'_'. $form->{invnumber}; # ($form->{type} eq 'credit_note' ? 'cnnumber' : 'invnumber') .'_'. $form->{invnumber};
768 $form->{addition} = $form->{print_and_post} ? "PRINTED AND POSTED" :
769 $form->{storno} ? "STORNO" :
774 if (!$form->{no_redirect_after_post}) {
775 $form->{action} = 'edit';
776 $form->{script} = 'is.pl';
777 $form->{saved_message} = $form->{label} . " $form->{invnumber} " . $locale->text('posted!');
778 $form->{callback} = build_std_url(qw(action edit id saved_message));
782 $main::lxdebug->leave_sub();
786 $main::lxdebug->enter_sub();
788 my $form = $main::form;
790 $main::auth->assert('invoice_edit');
792 my $old_form = new Form;
793 $form->{no_redirect_after_post} = 1;
794 $form->{print_and_post} = 1;
798 $main::lxdebug->leave_sub();
803 $main::lxdebug->enter_sub();
805 my $form = $main::form;
806 my %myconfig = %main::myconfig;
808 $main::auth->assert('invoice_edit');
810 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 locked)};
811 $form->{paidaccounts} = 1;
813 $form->{invdate} = $form->current_date(\%myconfig);
814 $form->{duedate} = $form->get_duedate(\%myconfig, $form->{invdate}) || $form->{invdate};
816 # remember pricegroups for "use as new"
817 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
818 set_pricegroup($_) for 1 .. $form->{rowcount};
822 $main::lxdebug->leave_sub();
826 $main::lxdebug->enter_sub();
828 my $form = $main::form;
829 my %myconfig = %main::myconfig;
830 my $locale = $main::locale;
832 $main::auth->assert('invoice_edit');
834 if ($form->{storno}) {
835 $form->error($locale->text('Cannot storno storno invoice!'));
838 if (IS->has_storno(\%myconfig, $form, "ar")) {
839 $form->error($locale->text("Invoice has already been storno'd!"));
842 map({ my $key = $_; delete($form->{$key}) unless (grep({ $key eq $_ } qw(id login password type))); } keys(%{ $form }));
848 # Payments must not be recorded for the new storno invoice.
849 $form->{paidaccounts} = 0;
850 map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ gldate_ acc_trans_id_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
852 $form->{storno_id} = $form->{id};
855 $form->{invnumber} = "Storno zu " . $form->{invnumber};
859 $main::lxdebug->leave_sub();
863 $main::lxdebug->enter_sub();
865 my $form = $main::form;
867 $main::auth->assert('invoice_edit');
869 $form->{preview} = 1;
870 my $old_form = new Form;
871 for (keys %$form) { $old_form->{$_} = $form->{$_} }
873 &print_form($old_form);
874 $main::lxdebug->leave_sub();
879 $main::lxdebug->enter_sub();
881 my $form = $main::form;
882 my $locale = $main::locale;
884 $main::auth->assert('invoice_edit');
886 if ($form->{second_run}) {
887 $form->{print_and_post} = 0;
892 <form method="post" action="$form->{script}">
895 # delete action variable
896 map { delete $form->{$_} } qw(action header);
898 foreach my $key (keys %$form) {
899 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
900 $form->{$key} =~ s/\"/"/g;
901 print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
905 <h2 class="confirm">| . $locale->text('Confirm!') . qq|</h2>
908 . $locale->text('Are you sure you want to delete Invoice Number')
909 . qq| $form->{invnumber}
913 <input name="action" class="submit" type="submit" value="|
914 . $locale->text('Yes') . qq|">
918 $main::lxdebug->leave_sub();
922 $main::lxdebug->enter_sub();
924 my $form = $main::form;
925 my %myconfig = %main::myconfig;
926 my $locale = $main::locale;
928 $main::auth->assert('invoice_edit');
930 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
932 $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
939 $form->{title} = $locale->text('Add Credit Note');
940 $form->{script} = 'is.pl';
942 # Bei Gutschriften bezug zur Rechnungsnummer
943 $form->{invnumber_for_credit_note} = $form->{invnumber};
944 # bo creates the id, reset it
945 map { delete $form->{$_} }
946 qw(id invnumber subject message cc bcc printed emailed queued);
947 $form->{ $form->{vc} } =~ s/--.*//g;
948 $form->{type} = "credit_note";
951 map { $form->{"select$_"} = "" } ($form->{vc}, 'currency');
953 # map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
954 # qw(creditlimit creditremaining);
956 for my $i (1 .. $form->{rowcount}) {
957 for (qw(listprice)) {
958 $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
962 my $currency = $form->{currency};
965 $form->{currency} = $currency;
966 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, 'buy');
967 $form->{exchangerate} = $form->{forex} || '';
969 $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
971 # bei Gutschriften werden Zahlungseingänge aus Rechnung nicht übernommen
972 for my $i (1 .. $form->{paidaccounts}) {
973 delete $form->{"paid_$i"};
974 delete $form->{"source_$i"};
975 delete $form->{"memo_$i"};
976 delete $form->{"datepaid_$i"};
977 delete $form->{"gldate_$i"};
978 delete $form->{"acc_trans_id_$i"};
979 delete $form->{"AR_paid_$i"};
981 $form->{paidaccounts} = 1;
988 $main::lxdebug->leave_sub();
992 $main::lxdebug->enter_sub();
994 my $form = $main::form;
995 my %myconfig = %main::myconfig;
996 my $locale = $main::locale;
998 $main::auth->assert('invoice_edit');
1000 if (IS->delete_invoice(\%myconfig, \%$form)) {
1001 # saving the history
1002 if(!exists $form->{addition}) {
1003 $form->{snumbers} = 'invnumber' .'_'. $form->{invnumber}; # ($form->{type} eq 'credit_note' ? 'cnnumber' : 'invnumber') .'_'. $form->{invnumber};
1004 $form->{addition} = "DELETED";
1005 $form->save_history;
1007 # /saving the history
1008 $form->redirect($locale->text('Invoice deleted!'));
1010 $form->error($locale->text('Cannot delete invoice!'));
1012 $main::lxdebug->leave_sub();
1016 $main::lxdebug->enter_sub();
1018 my $form = $main::form;
1020 $main::auth->assert('invoice_edit');
1023 $form->{no_redirect_after_post} = 1;
1025 my $saved_form = save_form();
1029 restore_form($saved_form, 0, qw(id invnumber));
1034 $main::lxdebug->leave_sub();