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 #======================================================================
38 use SL::MoreCommon qw(restore_form save_form);
41 use List::MoreUtils qw(uniq);
42 use List::Util qw(max sum);
43 use List::UtilsBy qw(sort_by);
44 use English qw(-no_match_vars);
48 use SL::DB::PaymentTerm;
50 require "bin/mozilla/io.pl";
51 require "bin/mozilla/arap.pl";
60 $main::lxdebug->enter_sub();
62 my $form = $main::form;
63 my $locale = $main::locale;
65 $main::auth->assert('invoice_edit');
67 $form->{show_details} = $::myconfig{show_form_details};
69 if ($form->{type} eq "credit_note") {
70 $form->{title} = $locale->text('Add Credit Note');
72 if ($form->{storno}) {
73 $form->{title} = $locale->text('Add Storno Credit Note');
76 $form->{title} = $locale->text('Add Sales Invoice');
81 $form->{callback} = "$form->{script}?action=add&type=$form->{type}" unless $form->{callback};
87 $main::lxdebug->leave_sub();
91 $main::lxdebug->enter_sub();
93 my $form = $main::form;
94 my $locale = $main::locale;
96 $main::auth->assert('invoice_edit');
98 $form->{show_details} = $::myconfig{show_form_details};
99 $form->{taxincluded_changed_by_user} = 1;
101 # show history button
102 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
104 my ($language_id, $printer_id);
105 if ($form->{print_and_post}) {
106 $form->{action} = "print";
107 $form->{resubmit} = 1;
108 $language_id = $form->{language_id};
109 $printer_id = $form->{printer_id};
113 if ($form->{type} eq "credit_note") {
114 $form->{title} = $locale->text('Edit Credit Note');
115 $form->{title} = $locale->text('Edit Storno Credit Note') if $form->{storno};
117 $form->{title} = $locale->text('Edit Sales Invoice');
118 $form->{title} = $locale->text('Edit Storno Invoice') if $form->{storno};
122 if ($form->{print_and_post}) {
123 $form->{language_id} = $language_id;
124 $form->{printer_id} = $printer_id;
129 $main::lxdebug->leave_sub();
133 $main::lxdebug->enter_sub();
135 my $form = $main::form;
136 my %myconfig = %main::myconfig;
138 $main::auth->assert('invoice_edit');
140 $form->{vc} = 'customer';
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 compatibility... 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');
294 require SL::DB::Invoice;
295 $TMPL_VAR{invoice_obj} = SL::DB::Invoice->new(id => $form->{id})->load;
297 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
298 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
300 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
302 $form->get_lists("taxzones" => ($form->{id} ? "ALL_TAXZONES" : "ALL_ACTIVE_TAXZONES"),
303 "currencies" => "ALL_CURRENCIES",
304 "customers" => "ALL_CUSTOMERS",
305 "departments" => "all_departments",
306 "price_factors" => "ALL_PRICE_FACTORS");
309 my @old_project_ids = uniq grep { $_ } map { $_ * 1 } ($form->{"globalproject_id"}, map { $form->{"project_id_$_"} } 1..$form->{"rowcount"});
310 my @old_ids_cond = @old_project_ids ? (id => \@old_project_ids) : ();
312 if ($::instance_conf->get_customer_projects_only_in_sales) {
315 customer_id => $::form->{customer_id},
316 billable_customer_id => $::form->{customer_id},
321 and => [ active => 1, @customer_cond ],
325 $TMPL_VAR{ALL_PROJECTS} = SL::DB::Manager::Project->get_all_sorted(query => \@conditions);
326 $form->{ALL_PROJECTS} = $TMPL_VAR{ALL_PROJECTS}; # make projects available for second row drop-down in io.pl
327 $TMPL_VAR{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ or => [ id => $::form->{employee_id}, deleted => 0 ] ]);
328 $TMPL_VAR{ALL_SALESMEN} = SL::DB::Manager::Employee->get_all_sorted(query => [ or => [ id => $::form->{salesman_id}, deleted => 0 ] ]);
329 $TMPL_VAR{ALL_SHIPTO} = SL::DB::Manager::Shipto->get_all_sorted(query => [
330 or => [ trans_id => $::form->{"$::form->{vc}_id"} * 1, and => [ shipto_id => $::form->{shipto_id} * 1, trans_id => undef ] ]
332 $TMPL_VAR{ALL_CONTACTS} = SL::DB::Manager::Contact->get_all_sorted(query => [
334 cp_cv_id => $::form->{"$::form->{vc}_id"} * 1,
337 cp_id => $::form->{cp_id} * 1
341 $TMPL_VAR{department_labels} = sub { "$_[0]->{description}--$_[0]->{id}" };
344 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
345 $TMPL_VAR{vclimit} = $myconfig{vclimit};
346 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('customer', '', 0, 0)";
347 push @custom_hiddens, "customer_id";
348 push @custom_hiddens, "oldcustomer";
349 push @custom_hiddens, "selectcustomer";
351 # currencies and exchangerate
352 my @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
353 my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
354 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
355 $form->{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
356 $TMPL_VAR{currencies} = NTI($::request->{cgi}->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
357 '-values' => \@values, '-labels' => \%labels,
358 '-onchange' => "document.getElementById('update_button').click();"
359 )) if scalar @values;
360 push @custom_hiddens, "forex";
361 push @custom_hiddens, "exchangerate" if $form->{forex};
363 $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
364 $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
366 my $follow_up_vc = $form->{customer};
367 $follow_up_vc =~ s/--\d*\s*$//;
368 $TMPL_VAR{customer_name} = $follow_up_vc;
370 # set option selected
371 foreach my $item (qw(AR)) {
372 $form->{"select$item"} =~ s/ selected//;
373 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
376 $TMPL_VAR{is_type_credit_note} = $form->{type} eq "credit_note";
377 $TMPL_VAR{is_format_html} = $form->{format} eq 'html';
378 $TMPL_VAR{dateformat} = $myconfig{dateformat};
379 $TMPL_VAR{numberformat} = $myconfig{numberformat};
381 push @custom_hiddens, map { "shiptocvar_" . $_->name } @{ SL::DB::Manager::CustomVariableConfig->get_all(where => [ module => 'ShipTo' ]) };
384 $TMPL_VAR{HIDDENS} = [qw(
385 id type media format queued printed emailed title vc discount
386 title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
387 max_dunning_level dunning_amount dunning_description
388 shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptogln shiptocontact shiptophone shiptofax
389 shiptoemail shiptodepartment_1 shiptodepartment_2 shiptocp_gender message email subject cc bcc taxaccounts cursor_fokus
390 convert_from_do_ids convert_from_oe_ids convert_from_ar_ids useasnew
394 map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
396 $::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.Draft kivi.SalesPurchase ckeditor/ckeditor ckeditor/adapters/jquery kivi.io autocomplete_customer autocomplete_part client_js));
398 $TMPL_VAR{payment_terms_obj} = get_payment_terms_for_invoice();
399 $form->{duedate} = $TMPL_VAR{payment_terms_obj}->calc_date(reference_date => $form->{invdate}, due_date => $form->{duedate})->to_kivitendo if $TMPL_VAR{payment_terms_obj};
403 print $form->parse_html_template("is/form_header", \%TMPL_VAR);
405 $main::lxdebug->leave_sub();
409 my @fields = qw(acc_trans_id gldate datepaid source memo paid AR_paid);
411 grep { $_->{paid} != 0 }
414 +{ map { ($_ => delete($::form->{"${_}_${idx}"})) } @fields }
415 } (1..$::form->{paidaccounts});
417 @payments = sort_by { DateTime->from_kivitendo($_->{datepaid}) } @payments;
419 $::form->{paidaccounts} = max scalar(@payments), 1;
421 foreach my $idx (1 .. scalar(@payments)) {
422 my $payment = $payments[$idx - 1];
423 $::form->{"${_}_${idx}"} = $payment->{$_} for @fields;
428 $main::lxdebug->enter_sub();
430 my $form = $main::form;
431 my %myconfig = %main::myconfig;
432 my $locale = $main::locale;
434 $main::auth->assert('invoice_edit');
436 $form->{invtotal} = $form->{invsubtotal};
439 $form->{rows} = max 2,
440 $form->numtextrows($form->{notes}, 26, 8),
441 $form->numtextrows($form->{intnotes}, 35, 8);
444 # tax, total and subtotal calculations
445 my ($tax, $subtotal);
446 $form->{taxaccounts_array} = [ split(/ /, $form->{taxaccounts}) ];
448 if( $form->{customer_id} && !$form->{taxincluded_changed_by_user} ) {
449 my $customer = SL::DB::Customer->new(id => $form->{customer_id})->load();
450 $form->{taxincluded} = defined($customer->taxincluded_checked) ? $customer->taxincluded_checked : $myconfig{taxincluded_checked};
453 foreach my $item (@{ $form->{taxaccounts_array} }) {
454 if ($form->{"${item}_base"}) {
455 if ($form->{taxincluded}) {
456 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"}
457 / (1 + $form->{"${item}_rate"})), 2);
458 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
460 $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
461 $form->{invtotal} += $form->{"${item}_total"};
466 my $grossamount = $form->{invtotal};
467 $form->{invtotal} = $form->round_amount( $form->{invtotal}, 2, 1 );
468 $form->{rounding} = $form->round_amount(
469 $form->{invtotal} - $form->round_amount($grossamount, 2),
475 $form->{follow_ups} = FU->follow_ups('trans_id' => $form->{id}, 'not_done' => 1) || [];
476 $form->{follow_ups_unfinished} = ( sum map { $_->{due} * 1 } @{ $form->{follow_ups} } ) || 0;
483 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
484 $form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];
486 # Standard Konto für Umlaufvermögen
487 my $accno_arap = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
489 for my $i (1 .. $form->{paidaccounts}) {
490 $form->{"changeable_$i"} = 1;
491 if (SL::DB::Default->get->payments_changeable == 0) {
493 $form->{"changeable_$i"} = ($form->{"acc_trans_id_$i"})? 0 : 1;
494 } elsif (SL::DB::Default->get->payments_changeable == 2) {
496 $form->{"changeable_$i"} = (($form->{"gldate_$i"} eq '') ||
497 ($form->current_date(\%myconfig) eq $form->{"gldate_$i"}));
500 #deaktivieren von gebuchten Zahlungen ausserhalb der Bücherkontrolle, vorher prüfen ob heute eingegeben
501 if ($form->date_closed($form->{"gldate_$i"})) {
502 $form->{"changeable_$i"} = 0;
505 $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
506 if (!$form->{"AR_paid_$i"}) {
507 $form->{"selectAR_paid_$i"} =~ s/option>$accno_arap--(.*?)</option selected>$accno_arap--$1</;
509 $form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
512 $totalpaid += $form->{"paid_$i"};
515 $form->{oldinvtotal} = $form->{invtotal};
517 $form->{ALL_DELIVERY_TERMS} = SL::DB::Manager::DeliveryTerm->get_all_sorted();
519 print $form->parse_html_template('is/form_footer', {
520 is_type_credit_note => ($form->{type} eq "credit_note"),
521 totalpaid => $totalpaid,
522 paid_missing => $form->{invtotal} - $totalpaid,
523 print_options => print_options(inline => 1),
524 show_storno => $form->{id} && !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar") && !$totalpaid,
525 show_delete => ($::instance_conf->get_is_changeable == 2)
526 ? ($form->current_date(\%myconfig) eq $form->{gldate})
527 : ($::instance_conf->get_is_changeable == 1),
528 today => DateTime->today,
530 ##print $form->parse_html_template('is/_payments'); # parser
531 ##print $form->parse_html_template('webdav/_list'); # parser
533 $main::lxdebug->leave_sub();
537 $main::lxdebug->enter_sub();
539 my $form = $main::form;
540 my %myconfig = %main::myconfig;
542 $main::auth->assert('invoice_edit');
544 &mark_as_paid_common(\%myconfig,"ar");
546 $main::lxdebug->leave_sub();
550 $main::lxdebug->enter_sub();
552 my $form = $main::form;
553 my %myconfig = %main::myconfig;
555 $main::auth->assert('invoice_edit');
557 my ($recursive_call) = @_;
559 $form->{print_and_post} = 0 if $form->{second_run};
560 my $taxincluded = $form->{taxincluded} ? "checked" : '';
563 &check_name("customer");
565 $form->{taxincluded} ||= $taxincluded;
567 if (!$form->{forex}) { # read exchangerate from input field (not hidden)
568 $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate}) unless $recursive_call;
570 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
571 $form->{exchangerate} = $form->{forex} if $form->{forex};
573 for my $i (1 .. $form->{paidaccounts}) {
574 next unless $form->{"paid_$i"};
575 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
576 if (!$form->{"forex_$i"}) { #read exchangerate from input field (not hidden)
577 $form->{exchangerate} = $form->{"exchangerate_$i"};
579 $form->{"forex_$i"} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
580 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
583 my $i = $form->{rowcount};
584 my $exchangerate = $form->{exchangerate} || 1;
586 # if last row empty, check the form otherwise retrieve new item
587 if ( ($form->{"partnumber_$i"} eq "")
588 && ($form->{"description_$i"} eq "")
589 && ($form->{"partsgroup_$i"} eq "")) {
591 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
596 IS->retrieve_item(\%myconfig, \%$form);
598 my $rows = scalar @{ $form->{item_list} };
600 $form->{"discount_$i"} = $form->parse_amount(\%myconfig, $form->{"discount_$i"}) / 100.0;
601 $form->{"discount_$i"} ||= $form->{customer_discount};
604 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
605 if( !$form->{"qty_$i"} ) {
606 $form->{"qty_$i"} = 1;
611 select_item(mode => 'IS', pre_entered_qty => $form->{"qty_$i"});
612 $::dispatcher->end_request;
616 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
618 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
619 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
621 $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
622 $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
624 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
626 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
627 my $decimalplaces = max 2, length $1;
630 $form->{"sellprice_$i"} = $sellprice;
632 my $record = _make_record();
633 my $price_source = SL::PriceSource->new(record_item => $record->items->[$i-1], record => $record);
634 my $best_price = $price_source->best_price;
635 my $best_discount = $price_source->best_discount;
638 $::form->{"sellprice_$i"} = $best_price->price;
639 $::form->{"active_price_source_$i"} = $best_price->source;
641 if ($best_discount) {
642 $::form->{"discount_$i"} = $best_discount->discount;
643 $::form->{"active_discount_source_$i"} = $best_discount->source;
646 # if there is an exchange rate adjust sellprice
647 $form->{"sellprice_$i"} /= $exchangerate;
650 $form->{"listprice_$i"} /= $exchangerate;
652 my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"});
653 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
654 map { $form->{"${_}_base"} += $amount } split / /, $form->{"taxaccounts_$i"};
655 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
657 $form->{creditremaining} -= $amount;
659 map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice lastcost);
661 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
662 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100.0);
669 # ok, so this is a new part
670 # ask if it is a part or service item
672 if ( $form->{"partsgroup_$i"}
673 && ($form->{"partsnumber_$i"} eq "")
674 && ($form->{"description_$i"} eq "")) {
676 $form->{"discount_$i"} = "";
680 $form->{"id_$i"} = 0;
685 $main::lxdebug->leave_sub();
689 $main::lxdebug->enter_sub();
691 my $form = $main::form;
692 my %myconfig = %main::myconfig;
693 my $locale = $main::locale;
695 $main::auth->assert('invoice_edit');
697 $form->mtime_ischanged('ar') ;
698 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
700 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
701 for my $i (1 .. $form->{paidaccounts}) {
702 if ($form->{"paid_$i"}) {
703 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
705 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
708 if ($form->{currency} ne $form->{defaultcurrency}) {
709 $form->{"exchangerate_$i"} = $form->{exchangerate}
710 if ($invdate == $datepaid);
711 $form->isblank("exchangerate_$i",
712 $locale->text('Exchangerate for payment missing!'));
714 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
715 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
717 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
718 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
719 $form->error($locale->text('Cannot post payment for a closed period!'))
720 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
724 ($form->{AR}) = split /--/, $form->{AR};
725 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
727 if ( IS->post_payment(\%myconfig, \%$form) ) {
728 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
729 $form->{what_done} = 'invoice';
730 $form->{addition} = "PAYMENT POSTED";
732 $form->redirect($locale->text('Payment posted!'))
734 $form->error($locale->text('Cannot post payment!'));
737 $main::lxdebug->leave_sub();
741 $main::lxdebug->enter_sub();
743 my $form = $main::form;
744 my %myconfig = %main::myconfig;
745 my $locale = $main::locale;
747 $main::auth->assert('invoice_edit');
748 $form->mtime_ischanged('ar');
750 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
751 $form->isblank("invdate", $locale->text('Invoice Date missing!'));
752 $form->isblank("customer", $locale->text('Customer missing!'));
753 $form->error($locale->text('Cannot post invoice for a closed period!'))
754 if ($form->date_closed($form->{"invdate"}, \%myconfig));
756 $form->{invnumber} =~ s/^\s*//g;
757 $form->{invnumber} =~ s/\s*$//g;
759 # if oldcustomer ne customer redo form
760 if (&check_name('customer')) {
762 $::dispatcher->end_request;
765 if ($myconfig{mandatory_departments} && !$form->{department_id}) {
766 $form->{saved_message} = $::locale->text('You have to specify a department.');
771 if ($form->{second_run}) {
772 $form->{print_and_post} = 0;
775 remove_emptied_rows();
778 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
779 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
781 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
782 if ($form->date_max_future($invdate, \%myconfig));
783 $form->error($locale->text('Cannot post invoice for a closed period!'))
784 if ($invdate <= $closedto);
786 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
787 if ($form->{currency} ne $form->{defaultcurrency});
789 for my $i (1 .. $form->{paidaccounts}) {
790 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
791 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
793 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
795 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
796 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
798 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
799 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
800 $form->error($locale->text('Cannot post payment for a closed period!'))
801 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
803 if ($form->{currency} ne $form->{defaultcurrency}) {
804 $form->{"exchangerate_$i"} = $form->{exchangerate}
805 if ($invdate == $datepaid);
806 $form->isblank("exchangerate_$i",
807 $locale->text('Exchangerate for payment missing!'));
812 ($form->{AR}) = split /--/, $form->{AR};
813 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
814 $form->{storno} ||= 0;
816 $form->{label} = $form->{type} eq 'credit_note' ? $locale->text('Credit Note') : $locale->text('Invoice');
818 $form->{id} = 0 if $form->{postasnew};
820 # get new invnumber in sequence if no invnumber is given or if posasnew was requested
821 if ($form->{postasnew}) {
822 if ($form->{type} eq "credit_note") {
823 undef($form->{cnnumber});
825 undef($form->{invnumber});
831 my $terms = get_payment_terms_for_invoice();
832 $form->{duedate} = $terms->calc_date(reference_date => $form->{invdate}, due_date => $form->{due_due})->to_kivitendo if $terms;
834 # If transfer_out is requested, get rose db handle and do post and
835 # transfer out in one transaction. Otherwise just post the invoice.
836 if ($::instance_conf->get_is_transfer_out && $form->{type} ne 'credit_note' && !$form->{storno}) {
837 require SL::DB::Inventory;
838 my $rose_db = SL::DB::Inventory->new->db;
841 if (!$rose_db->with_transaction(sub {
843 if (!IS->post_invoice(\%myconfig, \%$form, $rose_db->dbh)) {
844 push @errors, $locale->text('Cannot post invoice!');
847 my $err = IS->transfer_out(\%$form, $rose_db->dbh);
849 push @errors, @{ $err };
850 die 'transfer error';
855 push @errors, $EVAL_ERROR;
856 die 'transaction error';
861 push @errors, $rose_db->error;
862 $form->error($locale->text('Cannot post invoice and/or transfer out! Error message:') . "\n" . join("\n", @errors));
865 if (!IS->post_invoice(\%myconfig, \%$form)) {
866 $form->error($locale->text('Cannot post invoice!'));
870 if(!exists $form->{addition}) {
871 $form->{snumbers} = 'invnumber' .'_'. $form->{invnumber}; # ($form->{type} eq 'credit_note' ? 'cnnumber' : 'invnumber') .'_'. $form->{invnumber};
872 $form->{what_done} = 'invoice';
873 $form->{addition} = $form->{print_and_post} ? "PRINTED AND POSTED" :
874 $form->{storno} ? "STORNO" :
879 if (!$form->{no_redirect_after_post}) {
880 $form->{action} = 'edit';
881 $form->{script} = 'is.pl';
882 $form->{saved_message} = $form->{label} . " $form->{invnumber} " . $locale->text('posted!');
883 $form->{callback} = build_std_url(qw(action edit id callback saved_message));
887 $main::lxdebug->leave_sub();
891 $main::lxdebug->enter_sub();
893 my $form = $main::form;
895 $main::auth->assert('invoice_edit');
897 my $old_form = Form->new;
898 $form->{no_redirect_after_post} = 1;
899 $form->{print_and_post} = 1;
903 $main::lxdebug->leave_sub();
908 $main::lxdebug->enter_sub();
910 my $form = $main::form;
911 my %myconfig = %main::myconfig;
913 $main::auth->assert('invoice_edit');
915 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)};
917 $form->{paidaccounts} = 1;
918 $form->{invdate} = $form->current_date(\%myconfig);
919 my $terms = get_payment_terms_for_invoice();
920 $form->{duedate} = $terms ? $terms->calc_date(reference_date => $form->{invdate})->to_kivitendo : $form->{invdate};
921 $form->{employee_id} = SL::DB::Manager::Employee->current->id;
922 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
923 $form->{exchangerate} = $form->{forex} if $form->{forex};
925 $form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};
927 $form->{useasnew} = 1;
930 $main::lxdebug->leave_sub();
934 $main::lxdebug->enter_sub();
936 my $form = $main::form;
937 my %myconfig = %main::myconfig;
938 my $locale = $main::locale;
940 $main::auth->assert('invoice_edit');
942 if ($form->{storno}) {
943 $form->error($locale->text('Cannot storno storno invoice!'));
946 if (IS->has_storno(\%myconfig, $form, "ar")) {
947 $form->error($locale->text("Invoice has already been storno'd!"));
949 if ($form->datetonum($form->{invdate}, \%myconfig) <= $form->datetonum($form->{closedto}, \%myconfig)) {
950 $form->error($locale->text('Cannot storno invoice for a closed period!'));
953 # save the history of invoice being stornoed
954 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
955 $form->{what_done} = 'invoice';
956 $form->{addition} = "STORNO";
959 map({ my $key = $_; delete($form->{$key}) unless (grep({ $key eq $_ } qw(id login password type))); } keys(%{ $form }));
965 # Payments must not be recorded for the new storno invoice.
966 $form->{paidaccounts} = 0;
967 map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ gldate_ acc_trans_id_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
969 # record link invoice to storno
970 $form->{convert_from_ar_ids} = $form->{id};
971 $form->{storno_id} = $form->{id};
974 $form->{invnumber} = "Storno zu " . $form->{invnumber};
975 $form->{invdate} = DateTime->today->to_lxoffice;
977 # set new ids for storno invoice
978 # set new persistent ids for storno invoice items
979 $form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};
982 $main::lxdebug->leave_sub();
986 $main::lxdebug->enter_sub();
988 my $form = $main::form;
990 $main::auth->assert('invoice_edit');
992 $form->{preview} = 1;
993 my $old_form = Form->new;
994 for (keys %$form) { $old_form->{$_} = $form->{$_} }
996 &print_form($old_form);
997 $main::lxdebug->leave_sub();
1002 $main::lxdebug->enter_sub();
1004 my $form = $main::form;
1005 my %myconfig = %main::myconfig;
1006 my $locale = $main::locale;
1008 $main::auth->assert('invoice_edit');
1010 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1012 $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1014 $form->{convert_from_ar_ids} = $form->{id};
1016 $form->{rowcount}--;
1019 $form->{title} = $locale->text('Add Credit Note');
1020 $form->{script} = 'is.pl';
1022 # Bei Gutschriften bezug zur Rechnungsnummer
1023 $form->{invnumber_for_credit_note} = $form->{invnumber};
1024 # bo creates the id, reset it
1025 map { delete $form->{$_} }
1026 qw(id invnumber subject message cc bcc printed emailed queued);
1027 $form->{ $form->{vc} } =~ s/--.*//g;
1028 $form->{type} = "credit_note";
1031 map { $form->{"select$_"} = "" } ($form->{vc}, 'currency');
1033 # map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1034 # qw(creditlimit creditremaining);
1036 # set new persistent ids for credit note and link previous invoice id
1037 $form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};
1039 my $currency = $form->{currency};
1042 $form->{currency} = $currency;
1043 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, 'buy');
1044 $form->{exchangerate} = $form->{forex} || '';
1046 $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1048 # bei Gutschriften werden Zahlungseingänge aus Rechnung nicht übernommen
1049 for my $i (1 .. $form->{paidaccounts}) {
1050 delete $form->{"paid_$i"};
1051 delete $form->{"source_$i"};
1052 delete $form->{"memo_$i"};
1053 delete $form->{"datepaid_$i"};
1054 delete $form->{"gldate_$i"};
1055 delete $form->{"acc_trans_id_$i"};
1056 delete $form->{"AR_paid_$i"};
1058 $form->{paidaccounts} = 1;
1065 $main::lxdebug->leave_sub();
1069 $::lxdebug->enter_sub;
1071 $::auth->assert('invoice_edit');
1075 my $new_rowcount = $::form->{"rowcount"} * 1 + 1;
1076 $::form->{"project_id_${new_rowcount}"} = $::form->{"globalproject_id"};
1078 $::form->language_payment(\%::myconfig);
1080 Common::webdav_folder($::form);
1083 display_row(++$::form->{rowcount});
1086 $::lxdebug->leave_sub;
1090 $::auth->assert('invoice_edit');
1092 if (IS->delete_invoice(\%::myconfig, $::form)) {
1093 # saving the history
1094 if(!exists $::form->{addition}) {
1095 $::form->{snumbers} = 'invnumber' .'_'. $::form->{invnumber};
1096 $::form->{what_done} = 'invoice';
1097 $::form->{addition} = "DELETED";
1098 $::form->save_history;
1100 # /saving the history
1101 $::form->redirect($::locale->text('Invoice deleted!'));
1103 $::form->error($::locale->text('Cannot delete invoice!'));
1106 sub post_and_e_mail {
1111 $main::lxdebug->enter_sub();
1113 my $form = $main::form;
1115 $main::auth->assert('invoice_edit');
1118 $form->{no_redirect_after_post} = 1;
1120 my $saved_form = save_form();
1124 restore_form($saved_form, 0, qw(id invnumber));
1129 $main::lxdebug->leave_sub();
1134 print update ship_to e_mail storno post_payment use_as_new credit_note
1135 delete post order preview post_and_e_mail print_and_post
1138 if ($::form->{"action_$action"}) {
1144 $::form->error($::locale->text('No action defined.'));