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 #======================================================================
40 use List::MoreUtils qw(uniq);
41 use List::Util qw(max sum);
46 require "bin/mozilla/io.pl";
47 require "bin/mozilla/invoice_io.pl";
48 require "bin/mozilla/arap.pl";
49 require "bin/mozilla/drafts.pl";
58 $main::lxdebug->enter_sub();
60 my $form = $main::form;
61 my $locale = $main::locale;
63 $main::auth->assert('invoice_edit');
65 return $main::lxdebug->leave_sub() if (load_draft_maybe());
67 if ($form->{type} eq "credit_note") {
68 $form->{title} = $locale->text('Add Credit Note');
70 if ($form->{storno}) {
71 $form->{title} = $locale->text('Add Storno Credit Note');
74 $form->{title} = $locale->text('Add Sales Invoice');
79 $form->{callback} = "$form->{script}?action=add&type=$form->{type}" unless $form->{callback};
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} = $::instance_conf->get_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
155 taxincluded currency cp_id intnotes id shipto_id
158 IS->get_customer(\%myconfig, \%$form);
160 #quote all_customer Bug 133
161 foreach my $ref (@{ $form->{all_customer} }) {
162 $ref->{name} = $form->quote($ref->{name});
165 $form->restore_vars(qw(id));
167 IS->retrieve_invoice(\%myconfig, \%$form);
168 $form->restore_vars(qw(payment_id language_id taxzone_id currency intnotes
169 cp_id shipto_id delivery_term_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}";
206 $form->{"acc_trans_id_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{acc_trans_id};
208 $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
209 $form->{"datepaid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{transdate};
210 $form->{"gldate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{gldate};
211 $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
212 $form->{"forex_$i"} = $form->{"exchangerate_$i"};
213 $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
214 $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
216 $form->{paidaccounts} = $i;
219 $form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
223 $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
225 $form->{AR} = $form->{AR_1} unless $form->{id};
227 $form->{locked} = ($form->datetonum($form->{invdate}, \%myconfig)
228 <= $form->datetonum($form->{closedto}, \%myconfig));
230 $main::lxdebug->leave_sub();
233 sub prepare_invoice {
234 $main::lxdebug->enter_sub();
236 my $form = $main::form;
237 my %myconfig = %main::myconfig;
239 $main::auth->assert('invoice_edit');
241 if ($form->{type} eq "credit_note") {
242 $form->{type} = "credit_note";
243 $form->{formname} = "credit_note";
245 $form->{type} = "invoice";
246 $form->{formname} = "invoice";
253 foreach my $ref (@{ $form->{invoice_details} }) {
256 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
258 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
259 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
261 my $decimalplaces = ($dec > 2) ? $dec : 2;
263 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
264 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
265 $dec_qty = length $dec_qty;
267 $form->{"lastcost_$i"} = $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, $decimalplaces);
269 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
271 $form->{"sellprice_pg_$i"} = join ('--', $form->{"sellprice_$i"}, $form->{"pricegroup_id_$i"});
273 $form->{rowcount} = $i;
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 = $::request->{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 $form->get_lists("taxzones" => ($form->{id} ? "ALL_TAXZONES" : "ALL_ACTIVE_TAXZONES"),
299 "currencies" => "ALL_CURRENCIES",
300 "customers" => "ALL_CUSTOMERS",
301 "departments" => "all_departments",
302 "price_factors" => "ALL_PRICE_FACTORS");
305 my @old_project_ids = uniq grep { $_ } map { $_ * 1 } ($form->{"globalproject_id"}, map { $form->{"project_id_$_"} } 1..$form->{"rowcount"});
306 my @old_ids_cond = @old_project_ids ? (id => \@old_project_ids) : ();
308 if ($::instance_conf->get_customer_projects_only_in_sales) {
311 customer_id => $::form->{customer_id},
312 billable_customer_id => $::form->{customer_id},
317 and => [ active => 1, @customer_cond ],
321 $TMPL_VAR{ALL_PROJECTS} = SL::DB::Manager::Project->get_all_sorted(query => \@conditions);
322 $TMPL_VAR{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ or => [ id => $::form->{employee_id}, deleted => 0 ] ]);
323 $TMPL_VAR{ALL_SALESMEN} = SL::DB::Manager::Employee->get_all_sorted(query => [ or => [ id => $::form->{salesman_id}, deleted => 0 ] ]);
324 $TMPL_VAR{ALL_SHIPTO} = SL::DB::Manager::Shipto->get_all_sorted(query => [
325 or => [ trans_id => $::form->{"$::form->{vc}_id"} * 1, and => [ shipto_id => $::form->{shipto_id} * 1, trans_id => undef ] ]
327 $TMPL_VAR{ALL_CONTACTS} = SL::DB::Manager::Contact->get_all_sorted(query => [
329 cp_cv_id => $::form->{"$::form->{vc}_id"} * 1,
332 cp_id => $::form->{cp_id} * 1
336 $TMPL_VAR{department_labels} = sub { "$_[0]->{description}--$_[0]->{id}" };
339 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
340 $TMPL_VAR{vclimit} = $myconfig{vclimit};
341 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('customer', '', 0, 0)";
342 push @custom_hiddens, "customer_id";
343 push @custom_hiddens, "oldcustomer";
344 push @custom_hiddens, "selectcustomer";
346 # currencies and exchangerate
347 my @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
348 my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
349 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
350 $form->{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
351 $TMPL_VAR{currencies} = NTI($::request->{cgi}->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
352 '-values' => \@values, '-labels' => \%labels,
353 '-onchange' => "document.getElementById('update_button').click();"
354 )) if scalar @values;
355 push @custom_hiddens, "forex";
356 push @custom_hiddens, "exchangerate" if $form->{forex};
358 $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
359 $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
361 my $follow_up_vc = $form->{customer};
362 $follow_up_vc =~ s/--\d*\s*$//;
363 $TMPL_VAR{customer_name} = $follow_up_vc;
365 # set option selected
366 foreach my $item (qw(AR)) {
367 $form->{"select$item"} =~ s/ selected//;
368 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
371 $TMPL_VAR{is_type_credit_note} = $form->{type} eq "credit_note";
372 $TMPL_VAR{is_format_html} = $form->{format} eq 'html';
373 $TMPL_VAR{dateformat} = $myconfig{dateformat};
374 $TMPL_VAR{numberformat} = $myconfig{numberformat};
377 $TMPL_VAR{HIDDENS} = [qw(
378 id action type media format queued printed emailed title vc discount
379 title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
380 max_dunning_level dunning_amount
381 shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax
382 shiptoemail shiptodepartment_1 shiptodepartment_2 shiptocp_gender message email subject cc bcc taxaccounts cursor_fokus
383 convert_from_do_ids convert_from_oe_ids convert_from_ar_ids
386 map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
388 $::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.SalesPurchase ckeditor/ckeditor ckeditor/adapters/jquery kivi.io));
392 print $form->parse_html_template("is/form_header", \%TMPL_VAR);
394 $main::lxdebug->leave_sub();
398 $main::lxdebug->enter_sub();
400 my $form = $main::form;
401 my %myconfig = %main::myconfig;
402 my $locale = $main::locale;
404 $main::auth->assert('invoice_edit');
406 $form->{invtotal} = $form->{invsubtotal};
409 $form->{rows} = max 2,
410 $form->numtextrows($form->{notes}, 26, 8),
411 $form->numtextrows($form->{intnotes}, 35, 8);
414 # tax, total and subtotal calculations
415 my ($tax, $subtotal);
416 $form->{taxaccounts_array} = [ split(/ /, $form->{taxaccounts}) ];
418 if( $form->{customer_id} && !$form->{taxincluded_changed_by_user} ) {
419 my $customer = SL::DB::Customer->new(id => $form->{customer_id})->load();
420 $form->{taxincluded} = defined($customer->taxincluded_checked) ? $customer->taxincluded_checked : $myconfig{taxincluded_checked};
423 foreach my $item (@{ $form->{taxaccounts_array} }) {
424 if ($form->{"${item}_base"}) {
425 if ($form->{taxincluded}) {
426 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"}
427 / (1 + $form->{"${item}_rate"})), 2);
428 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
430 $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
431 $form->{invtotal} += $form->{"${item}_total"};
438 $form->{follow_ups} = FU->follow_ups('trans_id' => $form->{id}) || [];
439 $form->{follow_ups_unfinished} = ( sum map { $_->{due} * 1 } @{ $form->{follow_ups} } ) || 0;
444 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
445 $form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];
447 # Standard Konto für Umlaufvermögen
448 my $accno_arap = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
450 for my $i (1 .. $form->{paidaccounts}) {
451 $form->{"changeable_$i"} = 1;
452 if (SL::DB::Default->get->payments_changeable == 0) {
454 $form->{"changeable_$i"} = ($form->{"acc_trans_id_$i"})? 0 : 1;
455 } elsif (SL::DB::Default->get->payments_changeable == 2) {
457 $form->{"changeable_$i"} = (($form->{"gldate_$i"} eq '') ||
458 ($form->current_date(\%myconfig) eq $form->{"gldate_$i"}));
461 $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
462 if (!$form->{"AR_paid_$i"}) {
463 $form->{"selectAR_paid_$i"} =~ s/option>$accno_arap--(.*?)</option selected>$accno_arap--$1</;
465 $form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
468 $totalpaid += $form->{"paid_$i"};
471 $form->{oldinvtotal} = $form->{invtotal};
473 $form->{ALL_DELIVERY_TERMS} = SL::DB::Manager::DeliveryTerm->get_all_sorted();
475 print $form->parse_html_template('is/form_footer', {
476 is_type_credit_note => ($form->{type} eq "credit_note"),
477 totalpaid => $totalpaid,
478 paid_missing => $form->{invtotal} - $totalpaid,
479 print_options => print_options(inline => 1),
480 show_storno => $form->{id} && !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar") && !$totalpaid,
481 show_delete => ($::instance_conf->get_is_changeable == 2)
482 ? ($form->current_date(\%myconfig) eq $form->{gldate})
483 : ($::instance_conf->get_is_changeable == 1),
485 ##print $form->parse_html_template('is/_payments'); # parser
486 ##print $form->parse_html_template('webdav/_list'); # parser
488 $main::lxdebug->leave_sub();
492 $main::lxdebug->enter_sub();
494 my $form = $main::form;
495 my %myconfig = %main::myconfig;
497 $main::auth->assert('invoice_edit');
499 &mark_as_paid_common(\%myconfig,"ar");
501 $main::lxdebug->leave_sub();
505 $main::lxdebug->enter_sub();
507 my $form = $main::form;
508 my %myconfig = %main::myconfig;
510 $main::auth->assert('invoice_edit');
512 my ($recursive_call) = @_;
514 $form->{print_and_post} = 0 if $form->{second_run};
515 my $taxincluded = $form->{taxincluded} ? "checked" : '';
518 &check_name("customer");
520 $form->{taxincluded} ||= $taxincluded;
522 if (!$form->{forex}) { # read exchangerate from input field (not hidden)
523 $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate}) unless $recursive_call;
525 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
526 $form->{exchangerate} = $form->{forex} if $form->{forex};
528 for my $i (1 .. $form->{paidaccounts}) {
529 next unless $form->{"paid_$i"};
530 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
531 if (!$form->{"forex_$i"}) { #read exchangerate from input field (not hidden)
532 $form->{exchangerate} = $form->{"exchangerate_$i"};
534 $form->{"forex_$i"} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
535 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
538 my $i = $form->{rowcount};
539 my $exchangerate = $form->{exchangerate} || 1;
541 # if last row empty, check the form otherwise retrieve new item
542 if ( ($form->{"partnumber_$i"} eq "")
543 && ($form->{"description_$i"} eq "")
544 && ($form->{"partsgroup_$i"} eq "")) {
546 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
551 IS->retrieve_item(\%myconfig, \%$form);
553 my $rows = scalar @{ $form->{item_list} };
555 # Falls kein Kundenrabatt vorhanden ist, den aktuellen Rabatt nicht mit 0% überschreiben,
556 # da hier der Anwender schon manual einen Wert eingetragen haben könnte (analog zu qty) Bugfix: 1412
557 if ($form->{customer_discount}){
558 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{customer_discount} * 100);
562 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
563 if( !$form->{"qty_$i"} ) {
564 $form->{"qty_$i"} = 1;
569 select_item(mode => 'IS');
574 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
576 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
577 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
579 $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
580 $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
582 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
584 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
585 my $decimalplaces = max 2, length $1;
588 $form->{"sellprice_$i"} = $sellprice;
590 my $record = _make_record();
591 my $price_source = SL::PriceSource->new(record_item => $record->items->[$i-1], record => $record);
592 my $best_price = $price_source->best_price;
593 my $best_discount = $price_source->best_discount;
596 $::form->{"sellprice_$i"} = $best_price->price;
597 $::form->{"active_price_source_$i"} = $best_price->source;
599 if ($best_discount) {
600 $::form->{"discount_$i"} = $best_discount->discount;
601 $::form->{"active_discount_source_$i"} = $best_discount->source;
604 # if there is an exchange rate adjust sellprice
605 $form->{"sellprice_$i"} /= $exchangerate;
608 $form->{"listprice_$i"} /= $exchangerate;
610 my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
611 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
612 map { $form->{"${_}_base"} += $amount } split / /, $form->{"taxaccounts_$i"};
613 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
615 $form->{creditremaining} -= $amount;
617 map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice lastcost);
619 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
626 # ok, so this is a new part
627 # ask if it is a part or service item
629 if ( $form->{"partsgroup_$i"}
630 && ($form->{"partsnumber_$i"} eq "")
631 && ($form->{"description_$i"} eq "")) {
633 $form->{"discount_$i"} = "";
637 $form->{"id_$i"} = 0;
642 $main::lxdebug->leave_sub();
646 $main::lxdebug->enter_sub();
648 my $form = $main::form;
649 my %myconfig = %main::myconfig;
650 my $locale = $main::locale;
652 $main::auth->assert('invoice_edit');
654 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
656 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
657 for my $i (1 .. $form->{paidaccounts}) {
658 if ($form->{"paid_$i"}) {
659 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
661 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
664 if ($form->{currency} ne $form->{defaultcurrency}) {
665 $form->{"exchangerate_$i"} = $form->{exchangerate}
666 if ($invdate == $datepaid);
667 $form->isblank("exchangerate_$i",
668 $locale->text('Exchangerate for payment missing!'));
672 # Abgeschlossene Zeiträume nur für den letzten (aktuellen) Zahlungseingang prüfen
673 # Details s.a. Bug 1502
674 # Das Problem ist jetzt, dass man Zahlungseingänge nachträglich ändern kann
675 # Wobei dies für Installationen die sowieso nicht mit Bücherkontrolle arbeiten keinen
676 # keinen Unterschied macht.
677 # Optimal wäre, wenn gegen einen Zeitstempel des Zahlungsfelds geprüft würde ...
678 # Das Problem hierbei ist, dass in IS.pm post_invoice IMMER alle Zahlungseingänge aus $form
679 # erneut gespeichert werden. Prinzipiell wäre es besser NUR die Änderungen des Rechnungs-
680 # belegs (neue Zahlung aber nichts anderes) zu speichern ...
681 # Vielleicht könnte man ähnlich wie bei Rechnung löschen verfahren
682 $form->error($locale->text('Cannot post payment for a closed period!'))
683 if ($form->date_closed($form->{"datepaid_$form->{paidaccounts}"}, \%myconfig));
685 ($form->{AR}) = split /--/, $form->{AR};
686 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
688 $form->redirect($locale->text('Payment posted!'))
689 if (IS->post_payment(\%myconfig, \%$form));
690 $form->error($locale->text('Cannot post payment!'));
693 $main::lxdebug->leave_sub();
697 $main::lxdebug->enter_sub();
699 my $form = $main::form;
700 my %myconfig = %main::myconfig;
701 my $locale = $main::locale;
703 $main::auth->assert('invoice_edit');
705 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
706 $form->isblank("invdate", $locale->text('Invoice Date missing!'));
707 $form->isblank("customer", $locale->text('Customer missing!'));
708 $form->error($locale->text('Cannot post invoice for a closed period!'))
709 if ($form->date_closed($form->{"invdate"}, \%myconfig));
711 $form->{invnumber} =~ s/^\s*//g;
712 $form->{invnumber} =~ s/\s*$//g;
714 # if oldcustomer ne customer redo form
715 if (&check_name('customer')) {
720 if ($myconfig{mandatory_departments} && !$form->{department_id}) {
721 $form->{saved_message} = $::locale->text('You have to specify a department.');
726 if ($form->{second_run}) {
727 $form->{print_and_post} = 0;
730 remove_emptied_rows();
733 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
734 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
736 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
737 if ($form->date_max_future($invdate, \%myconfig));
738 $form->error($locale->text('Cannot post invoice for a closed period!'))
739 if ($invdate <= $closedto);
741 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
742 if ($form->{currency} ne $form->{defaultcurrency});
744 for my $i (1 .. $form->{paidaccounts}) {
745 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
746 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
748 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
750 $form->error($locale->text('Cannot post payment for a closed period!'))
751 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
753 if ($form->{currency} ne $form->{defaultcurrency}) {
754 $form->{"exchangerate_$i"} = $form->{exchangerate}
755 if ($invdate == $datepaid);
756 $form->isblank("exchangerate_$i",
757 $locale->text('Exchangerate for payment missing!'));
762 ($form->{AR}) = split /--/, $form->{AR};
763 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
764 $form->{storno} ||= 0;
766 $form->{label} = $form->{type} eq 'credit_note' ? $locale->text('Credit Note') : $locale->text('Invoice');
768 $form->{id} = 0 if $form->{postasnew};
770 # get new invnumber in sequence if no invnumber is given or if posasnew was requested
771 if ($form->{postasnew}) {
772 if ($form->{type} eq "credit_note") {
773 undef($form->{cnnumber});
775 undef($form->{invnumber});
780 $form->error($locale->text('Cannot post invoice!'))
781 unless IS->post_invoice(\%myconfig, \%$form);
782 remove_draft() if $form->{remove_draft};
784 if(!exists $form->{addition}) {
785 $form->{snumbers} = 'invnumber' .'_'. $form->{invnumber}; # ($form->{type} eq 'credit_note' ? 'cnnumber' : 'invnumber') .'_'. $form->{invnumber};
786 $form->{addition} = $form->{print_and_post} ? "PRINTED AND POSTED" :
787 $form->{storno} ? "STORNO" :
792 if (!$form->{no_redirect_after_post}) {
793 $form->{action} = 'edit';
794 $form->{script} = 'is.pl';
795 $form->{saved_message} = $form->{label} . " $form->{invnumber} " . $locale->text('posted!');
796 $form->{callback} = build_std_url(qw(action edit id saved_message));
800 $main::lxdebug->leave_sub();
804 $main::lxdebug->enter_sub();
806 my $form = $main::form;
808 $main::auth->assert('invoice_edit');
810 my $old_form = new Form;
811 $form->{no_redirect_after_post} = 1;
812 $form->{print_and_post} = 1;
816 $main::lxdebug->leave_sub();
821 $main::lxdebug->enter_sub();
823 my $form = $main::form;
824 my %myconfig = %main::myconfig;
826 $main::auth->assert('invoice_edit');
828 delete @{ $form }{qw(printed emailed queued invnumber invdate exchangerate forex deliverydate id datepaid_1 gldate_1 acc_trans_id_1 source_1 memo_1 paid_1 exchangerate_1 AP_paid_1 storno locked)};
830 $form->{paidaccounts} = 1;
831 $form->{invdate} = $form->current_date(\%myconfig);
832 $form->{duedate} = $form->get_duedate(\%myconfig, $form->{invdate}) || $form->{invdate};
833 $form->{employee_id} = SL::DB::Manager::Employee->current->id;
834 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
835 $form->{exchangerate} = $form->{forex} if $form->{forex};
839 $main::lxdebug->leave_sub();
843 $main::lxdebug->enter_sub();
845 my $form = $main::form;
846 my %myconfig = %main::myconfig;
847 my $locale = $main::locale;
849 $main::auth->assert('invoice_edit');
851 if ($form->{storno}) {
852 $form->error($locale->text('Cannot storno storno invoice!'));
855 if (IS->has_storno(\%myconfig, $form, "ar")) {
856 $form->error($locale->text("Invoice has already been storno'd!"));
858 if ($form->datetonum($form->{invdate}, \%myconfig) <= $form->datetonum($form->{closedto}, \%myconfig)) {
859 $form->error($locale->text('Cannot storno invoice for a closed period!'));
862 map({ my $key = $_; delete($form->{$key}) unless (grep({ $key eq $_ } qw(id login password type))); } keys(%{ $form }));
868 # Payments must not be recorded for the new storno invoice.
869 $form->{paidaccounts} = 0;
870 map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ gldate_ acc_trans_id_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
872 $form->{storno_id} = $form->{id};
875 $form->{invnumber} = "Storno zu " . $form->{invnumber};
876 $form->{invdate} = DateTime->today->to_lxoffice;
880 $main::lxdebug->leave_sub();
884 $main::lxdebug->enter_sub();
886 my $form = $main::form;
888 $main::auth->assert('invoice_edit');
890 $form->{preview} = 1;
891 my $old_form = new Form;
892 for (keys %$form) { $old_form->{$_} = $form->{$_} }
894 &print_form($old_form);
895 $main::lxdebug->leave_sub();
900 $main::lxdebug->enter_sub();
902 my $form = $main::form;
903 my $locale = $main::locale;
905 $main::auth->assert('invoice_edit');
907 if ($form->{second_run}) {
908 $form->{print_and_post} = 0;
913 <form method="post" action="$form->{script}">
916 # delete action variable
917 map { delete $form->{$_} } qw(action header);
919 foreach my $key (keys %$form) {
920 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
921 $form->{$key} =~ s/\"/"/g;
922 print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
926 <h2 class="confirm">| . $locale->text('Confirm!') . qq|</h2>
929 . $locale->text('Are you sure you want to delete Invoice Number')
930 . qq| $form->{invnumber}
934 <input name="action" class="submit" type="submit" value="|
935 . $locale->text('Yes') . qq|">
939 $main::lxdebug->leave_sub();
943 $main::lxdebug->enter_sub();
945 my $form = $main::form;
946 my %myconfig = %main::myconfig;
947 my $locale = $main::locale;
949 $main::auth->assert('invoice_edit');
951 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
953 $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
955 $form->{convert_from_ar_ids} = $form->{id};
960 $form->{title} = $locale->text('Add Credit Note');
961 $form->{script} = 'is.pl';
963 # Bei Gutschriften bezug zur Rechnungsnummer
964 $form->{invnumber_for_credit_note} = $form->{invnumber};
965 # bo creates the id, reset it
966 map { delete $form->{$_} }
967 qw(id invnumber subject message cc bcc printed emailed queued);
968 $form->{ $form->{vc} } =~ s/--.*//g;
969 $form->{type} = "credit_note";
972 map { $form->{"select$_"} = "" } ($form->{vc}, 'currency');
974 # map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
975 # qw(creditlimit creditremaining);
977 for my $i (1 .. $form->{rowcount}) {
978 for (qw(listprice)) {
979 $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
983 my $currency = $form->{currency};
986 $form->{currency} = $currency;
987 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, 'buy');
988 $form->{exchangerate} = $form->{forex} || '';
990 $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
992 # bei Gutschriften werden Zahlungseingänge aus Rechnung nicht übernommen
993 for my $i (1 .. $form->{paidaccounts}) {
994 delete $form->{"paid_$i"};
995 delete $form->{"source_$i"};
996 delete $form->{"memo_$i"};
997 delete $form->{"datepaid_$i"};
998 delete $form->{"gldate_$i"};
999 delete $form->{"acc_trans_id_$i"};
1000 delete $form->{"AR_paid_$i"};
1002 $form->{paidaccounts} = 1;
1009 $main::lxdebug->leave_sub();
1013 $main::lxdebug->enter_sub();
1015 my $form = $main::form;
1016 my %myconfig = %main::myconfig;
1017 my $locale = $main::locale;
1019 $main::auth->assert('invoice_edit');
1021 if (IS->delete_invoice(\%myconfig, \%$form)) {
1022 # saving the history
1023 if(!exists $form->{addition}) {
1024 $form->{snumbers} = 'invnumber' .'_'. $form->{invnumber}; # ($form->{type} eq 'credit_note' ? 'cnnumber' : 'invnumber') .'_'. $form->{invnumber};
1025 $form->{addition} = "DELETED";
1026 $form->save_history;
1028 # /saving the history
1029 $form->redirect($locale->text('Invoice deleted!'));
1031 $form->error($locale->text('Cannot delete invoice!'));
1033 $main::lxdebug->leave_sub();
1036 sub post_and_e_mail {
1041 $main::lxdebug->enter_sub();
1043 my $form = $main::form;
1045 $main::auth->assert('invoice_edit');
1048 $form->{no_redirect_after_post} = 1;
1050 my $saved_form = save_form();
1054 restore_form($saved_form, 0, qw(id invnumber));
1059 $main::lxdebug->leave_sub();