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., 51 Franklin Street, Fifth Floor, Boston,
29 #======================================================================
31 # Inventory received module
33 #======================================================================
39 use SL::DB::Department;
40 use SL::DB::PurchaseInvoice;
42 use List::Util qw(max sum);
43 use List::UtilsBy qw(sort_by);
45 require "bin/mozilla/io.pl";
46 require "bin/mozilla/common.pl";
55 $main::lxdebug->enter_sub();
57 my $form = $main::form;
58 my $locale = $main::locale;
60 $main::auth->assert('vendor_invoice_edit');
62 if (!$::instance_conf->get_allow_new_purchase_invoice) {
63 $::form->show_generic_error($::locale->text("You do not have the permissions to access this function."));
66 $form->{show_details} = $::myconfig{show_form_details};
68 $form->{title} = $locale->text('Record Vendor Invoice');
74 $main::lxdebug->leave_sub();
78 $main::lxdebug->enter_sub();
80 my $form = $main::form;
81 my $locale = $main::locale;
83 $main::auth->assert('vendor_invoice_edit');
85 $form->{show_details} = $::myconfig{show_form_details};
88 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
89 #/show hhistory button
91 $form->{title} = $locale->text('Edit Vendor Invoice');
97 $main::lxdebug->leave_sub();
101 $main::lxdebug->enter_sub();
103 my $form = $main::form;
104 my %myconfig = %main::myconfig;
106 $main::auth->assert('vendor_invoice_edit');
108 $form->{vc} = 'vendor';
111 $form->create_links("AP", \%myconfig, "vendor");
113 $form->backup_vars(qw(payment_id language_id taxzone_id
114 currency delivery_term_id intnotes cp_id));
116 IR->get_vendor(\%myconfig, \%$form);
117 IR->retrieve_invoice(\%myconfig, \%$form);
119 $form->restore_vars(qw(payment_id language_id taxzone_id
120 currency delivery_term_id intnotes cp_id));
122 my @curr = $form->get_all_currencies();
123 map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
126 $form->{forex} = $form->{exchangerate};
127 my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
129 foreach my $key (keys %{ $form->{AP_links} }) {
131 foreach my $ref (@{ $form->{AP_links}{$key} }) {
132 $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}</option>";
135 next unless $form->{acc_trans}{$key};
137 if ($key eq "AP_paid") {
138 for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
139 $form->{"AP_paid_$i"} =
140 "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
142 $form->{"acc_trans_id_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{acc_trans_id};
144 $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount};
145 $form->{"datepaid_$i"} =
146 $form->{acc_trans}{$key}->[$i - 1]->{transdate};
147 $form->{"gldate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{gldate};
148 $form->{"forex_$i"} = $form->{"exchangerate_$i"} =
149 $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
150 $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
151 $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
153 $form->{paidaccounts} = $i;
157 "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
162 $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
164 $form->{AP} = $form->{AP_1} unless $form->{id};
167 ($form->datetonum($form->{invdate}, \%myconfig) <=
168 $form->datetonum($form->{closedto}, \%myconfig));
170 $main::lxdebug->leave_sub();
173 sub prepare_invoice {
174 $main::lxdebug->enter_sub();
176 my $form = $main::form;
177 my %myconfig = %main::myconfig;
179 $main::auth->assert('vendor_invoice_edit');
181 $form->{type} = "purchase_invoice";
185 map { $form->{$_} =~ s/\"/"/g } qw(invnumber ordnumber quonumber);
188 foreach my $ref (@{ $form->{invoice_details} }) {
190 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
191 # übernommen aus is.pl Fix für Bug 1642. Nebenwirkungen? jb 12.5.2011
192 # getestet: Lieferantenauftrag -> Rechnung i.O.
193 # Lieferantenauftrag -> Lieferschein -> Rechnung i.O.
194 # Werte: 20% (Lieferantenrabatt), 12,4% individuell und 0,4 individuell s.a.
195 # Screenshot zu Bug 1642
196 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
198 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
200 my $decimalplaces = ($dec > 2) ? $dec : 2;
202 $form->{"sellprice_$i"} =
203 $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
206 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
207 $dec_qty = length $dec_qty;
210 $form->format_amount(\%myconfig, ($form->{"qty_$i"} * -1), $dec_qty);
212 $form->{rowcount} = $i;
216 $main::lxdebug->leave_sub();
219 sub setup_ir_action_bar {
221 my $change_never = $::instance_conf->get_ir_changeable == 0;
222 my $change_on_same_day_only = $::instance_conf->get_ir_changeable == 2 && ($form->current_date(\%::myconfig) ne $form->{gldate});
224 for my $bar ($::request->layout->get('actionbar')) {
228 submit => [ '#form', { action => "update" } ],
229 id => 'update_button',
230 accesskey => 'enter',
236 submit => [ '#form', { action => "post" } ],
237 disabled => $form->{locked} ? t8('The billing period has already been locked.')
238 : $form->{storno} ? t8('A canceled invoice cannot be posted.')
239 : ($form->{id} && $change_never) ? t8('Changing invoices has been disabled in the configuration.')
240 : ($form->{id} && $change_on_same_day_only) ? t8('Invoices can only be changed on the day they are posted.')
245 submit => [ '#form', { action => "post_payment" } ],
246 disabled => !$form->{id} ? t8('This invoice has not been posted yet.') : undef,
250 submit => [ '#form', { action => "mark_as_paid" } ],
251 confirm => t8('This will remove the invoice from showing as unpaid even if the unpaid amount does not match the amount. Proceed?'),
252 disabled => !$form->{id} ? t8('This invoice has not been posted yet.') : undef,
253 only_if => $::instance_conf->get_ir_show_mark_as_paid,
255 ], # end of combobox "Post"
258 action => [ t8('Storno'),
259 submit => [ '#form', { action => "storno" } ],
260 confirm => t8('Do you really want to cancel this invoice?'),
261 disabled => !$form->{id} ? t8('This invoice has not been posted yet.') : undef,
263 action => [ t8('Delete'),
264 submit => [ '#form', { action => "delete" } ],
265 confirm => t8('Do you really want to delete this object?'),
266 disabled => !$form->{id} ? t8('This invoice has not been posted yet.')
267 : $form->{locked} ? t8('The billing period has already been locked.')
268 : $change_never ? t8('Changing invoices has been disabled in the configuration.')
269 : $change_on_same_day_only ? t8('Invoices can only be changed on the day they are posted.')
272 ], # end of combobox "Storno"
277 action => [ t8('Workflow') ],
280 submit => [ '#form', { action => "use_as_new" } ],
281 disabled => !$form->{id} ? t8('This invoice has not been posted yet.') : undef,
283 ], # end of combobox "Workflow"
286 action => [ t8('more') ],
289 call => [ 'set_history_window', $::form->{id} * 1, 'id', 'glid' ],
290 disabled => !$form->{id} ? t8('This invoice has not been posted yet.') : undef,
294 call => [ 'follow_up_window' ],
295 disabled => !$form->{id} ? t8('This invoice has not been posted yet.') : undef,
299 call => [ 'kivi.Draft.popup', 'ir', 'invoice', $::form->{draft_id}, $::form->{draft_description} ],
300 disabled => $form->{id} ? t8('This invoice has already been posted.')
301 : $form->{locked} ? t8('The billing period has already been locked.')
304 ], # end of combobox "more"
310 $main::lxdebug->enter_sub();
312 my $form = $main::form;
313 my %myconfig = %main::myconfig;
314 my $locale = $main::locale;
315 my $cgi = $::request->{cgi};
317 $main::auth->assert('vendor_invoice_edit');
322 $TMPL_VAR{invoice_obj} = SL::DB::PurchaseInvoice->load_cached($form->{id}) if $form->{id};
323 $TMPL_VAR{vendor_obj} = SL::DB::Vendor->load_cached($form->{vendor_id}) if $form->{vendor_id};
324 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
325 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
327 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
329 my @old_project_ids = ($form->{"globalproject_id"});
330 map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
332 $form->get_lists("taxzones" => ($form->{id} ? "ALL_TAXZONES" : "ALL_ACTIVE_TAXZONES"),
333 "currencies" => "ALL_CURRENCIES",
334 "price_factors" => "ALL_PRICE_FACTORS");
336 $TMPL_VAR{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
337 $TMPL_VAR{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ or => [ id => $::form->{employee_id}, deleted => 0 ] ]);
338 $TMPL_VAR{ALL_CONTACTS} = SL::DB::Manager::Contact->get_all_sorted(query => [
340 cp_cv_id => $::form->{"$::form->{vc}_id"} * 1,
343 cp_id => $::form->{cp_id} * 1
348 # currencies and exchangerate
349 my @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
350 my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
351 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
352 # show_exchangerate is also later needed in another template
353 $form->{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
354 $TMPL_VAR{currencies} = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
355 '-values' => \@values, '-labels' => \%labels,
356 '-onchange' => "document.getElementById('update_button').click();"
357 )) if scalar @values;
358 push @custom_hiddens, "forex";
359 push @custom_hiddens, "exchangerate" if $form->{forex};
361 $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
362 $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
364 # set option selected
365 foreach my $item (qw(AP)) {
366 $form->{"select$item"} =~ s/ selected//;
367 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
370 $TMPL_VAR{is_type_credit_note} = $form->{type} eq "credit_note";
371 $TMPL_VAR{is_format_html} = $form->{format} eq 'html';
372 $TMPL_VAR{dateformat} = $myconfig{dateformat};
373 $TMPL_VAR{numberformat} = $myconfig{numberformat};
376 $TMPL_VAR{HIDDENS} = [qw(
377 id type queued printed emailed title vc discount
378 title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
379 max_dunning_level dunning_amount
380 shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptogln shiptocontact shiptophone shiptofax
381 shiptoemail shiptodepartment_1 shiptodepartment_2 message email subject cc bcc taxaccounts cursor_fokus
382 convert_from_do_ids convert_from_oe_ids convert_from_ap_ids show_details gldate useasnew
384 map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
386 $TMPL_VAR{payment_terms_obj} = get_payment_terms_for_invoice();
387 $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};
389 $::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.Draft kivi.File kivi.SalesPurchase kivi.Part ckeditor/ckeditor ckeditor/adapters/jquery kivi.io autocomplete_customer autocomplete_project client_js));
391 setup_ir_action_bar();
395 print $form->parse_html_template("ir/form_header", \%TMPL_VAR);
397 $main::lxdebug->leave_sub();
401 my @fields = qw(acc_trans_id gldate datepaid source memo paid AP_paid);
403 grep { $_->{paid} != 0 }
406 +{ map { ($_ => delete($::form->{"${_}_${idx}"})) } @fields }
407 } (1..$::form->{paidaccounts});
409 @payments = sort_by { DateTime->from_kivitendo($_->{datepaid}) } @payments;
411 $::form->{paidaccounts} = max scalar(@payments), 1;
413 foreach my $idx (1 .. scalar(@payments)) {
414 my $payment = $payments[$idx - 1];
415 $::form->{"${_}_${idx}"} = $payment->{$_} for @fields;
420 $main::lxdebug->enter_sub();
422 my $form = $main::form;
423 my %myconfig = %main::myconfig;
424 my $locale = $main::locale;
426 $main::auth->assert('vendor_invoice_edit');
428 $form->{invtotal} = $form->{invsubtotal};
429 $form->{oldinvtotal} = $form->{invtotal};
432 $form->{rows} = max 2,
433 $form->numtextrows($form->{notes}, 26, 8),
434 $form->numtextrows($form->{intnotes}, 35, 8);
437 # tax, total and subtotal calculations
438 my ($tax, $subtotal);
439 $form->{taxaccounts_array} = [ split / /, $form->{taxaccounts} ];
441 foreach my $item (@{ $form->{taxaccounts_array} }) {
442 if ($form->{"${item}_base"}) {
443 if ($form->{taxincluded}) {
444 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"}
445 / (1 + $form->{"${item}_rate"})), 2);
446 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
448 $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
449 $form->{invtotal} += $form->{"${item}_total"};
456 $form->{follow_ups} = FU->follow_ups('trans_id' => $form->{id}, 'not_done' => 1) || [];
457 $form->{follow_ups_unfinished} = ( sum map { $_->{due} * 1 } @{ $form->{follow_ups} } ) || 0;
464 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
465 $form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];
467 # Standard Konto für Umlaufvermögen
468 my $accno_arap = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
470 for my $i (1 .. $form->{paidaccounts}) {
471 $form->{"changeable_$i"} = 1;
472 if (SL::DB::Default->get->payments_changeable == 0) {
474 $form->{"changeable_$i"} = ($form->{"acc_trans_id_$i"})? 0 : 1;
475 } elsif (SL::DB::Default->get->payments_changeable == 2) {
477 $form->{"changeable_$i"} = (($form->{"gldate_$i"} eq '') ||
478 ($form->current_date(\%myconfig) eq $form->{"gldate_$i"}));
481 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
482 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
484 #deaktivieren von Zahlungen ausserhalb der Bücherkontrolle
485 if ($form->date_closed($form->{"gldate_$i"})) {
486 $form->{"changeable_$i"} = 0;
489 $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
490 if (!$form->{"AP_paid_$i"}) {
491 $form->{"selectAP_paid_$i"} =~ s/option>$accno_arap--(.*?)>/option selected>$accno_arap--$1>/;
493 $form->{"selectAP_paid_$i"} =~ s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
496 $totalpaid += $form->{"paid_$i"};
499 $form->{ALL_DELIVERY_TERMS} = SL::DB::Manager::DeliveryTerm->get_all_sorted();
501 print $form->parse_html_template('ir/form_footer', {
502 is_type_credit_note => ($form->{type} eq "credit_note"),
503 totalpaid => $totalpaid,
504 paid_missing => $form->{invtotal} - $totalpaid,
505 show_storno => $form->{id} && !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ap") && !$totalpaid,
506 show_delete => ($::instance_conf->get_ir_changeable == 2)
507 ? ($form->current_date(\%myconfig) eq $form->{gldate})
508 : ($::instance_conf->get_ir_changeable == 1),
509 today => DateTime->today,
511 ##print $form->parse_html_template('ir/_payments'); # parser
512 ##print $form->parse_html_template('webdav/_list'); # parser
514 $main::lxdebug->leave_sub();
518 $::auth->assert('vendor_invoice_edit');
520 SL::DB::PurchaseInvoice->new(id => $::form->{id})->load->mark_as_paid;
522 $::form->redirect($::locale->text("Marked as paid"));
530 $main::lxdebug->enter_sub();
532 my $form = $main::form;
533 my %myconfig = %main::myconfig;
535 $main::auth->assert('vendor_invoice_edit');
537 if (($form->{previous_vendor_id} || $form->{vendor_id}) != $form->{vendor_id}) {
538 IR->get_vendor(\%myconfig, $form);
541 if (!$form->{forex}) { # read exchangerate from input field (not hidden)
542 $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate});
544 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'sell');
545 $form->{exchangerate} = $form->{forex} if $form->{forex};
547 for my $i (1 .. $form->{paidaccounts}) {
548 next unless $form->{"paid_$i"};
549 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
550 $form->{"forex_$i"} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
551 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
554 my $i = $form->{rowcount};
555 my $exchangerate = ($form->{exchangerate} * 1) || 1;
557 if ( ($form->{"partnumber_$i"} eq "")
558 && ($form->{"description_$i"} eq "")
559 && ($form->{"partsgroup_$i"} eq "")) {
560 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
565 IR->retrieve_item(\%myconfig, \%$form);
567 my $rows = scalar @{ $form->{item_list} };
569 $form->{"discount_$i"} = $form->parse_amount(\%myconfig, $form->{"discount_$i"}) / 100.0;
570 $form->{"discount_$i"} ||= $form->{vendor_discount};
573 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
574 if( !$form->{"qty_$i"} ) {
575 $form->{"qty_$i"} = 1;
580 select_item(mode => 'IR', pre_entered_qty => $form->{"qty_$i"});
581 $::dispatcher->end_request;
585 # override sellprice if there is one entered
586 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
588 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
589 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
591 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
593 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
594 my $dec_qty = length $1;
595 my $decimalplaces = max 2, $dec_qty;
598 $form->{"sellprice_$i"} = $sellprice;
600 my $record = _make_record();
601 my $price_source = SL::PriceSource->new(record_item => $record->items->[$i-1], record => $record);
602 my $best_price = $price_source->best_price;
603 my $best_discount = $price_source->best_discount;
606 $::form->{"sellprice_$i"} = $best_price->price;
607 $::form->{"active_price_source_$i"} = $best_price->source;
609 if ($best_discount) {
610 $::form->{"discount_$i"} = $best_discount->discount;
611 $::form->{"active_discount_source_$i"} = $best_discount->source;
614 # if there is an exchange rate adjust sellprice
615 $form->{"sellprice_$i"} /= $exchangerate;
618 my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"});
619 $form->{creditremaining} -= $amount;
620 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
621 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
622 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100.0);
629 # ok, so this is a new part
630 # ask if it is a part or service item
632 if ( $form->{"partsgroup_$i"}
633 && ($form->{"partsnumber_$i"} eq "")
634 && ($form->{"description_$i"} eq "")) {
636 $form->{"discount_$i"} = "";
640 $form->{"id_$i"} = 0;
645 $main::lxdebug->leave_sub();
649 $main::lxdebug->enter_sub();
651 my $form = $main::form;
652 my %myconfig = %main::myconfig;
653 my $locale = $main::locale;
655 $main::auth->assert('vendor_invoice_edit');
657 if ($form->{storno}) {
658 $form->error($locale->text('Cannot storno storno invoice!'));
661 if (IS->has_storno(\%myconfig, $form, "ap")) {
662 $form->error($locale->text("Invoice has already been storno'd!"));
665 $form->error($locale->text('Cannot post storno for a closed period!'))
666 if ( $form->date_closed($form->{invdate}, \%myconfig));
668 my $employee_id = $form->{employee_id};
673 # Payments must not be recorded for the new storno invoice.
674 $form->{paidaccounts} = 0;
675 map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ gldate_ acc_trans_id_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
676 # set new ids for storno invoice
677 # set new persistent ids for storno invoice items
678 $form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};
681 if(!exists $form->{addition} && $form->{id} ne "") {
682 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
683 $form->{what_done} = "invoice";
684 $form->{addition} = "CANCELED";
687 # /saving the history
689 # record link invoice to storno
690 $form->{convert_from_ap_ids} = $form->{id};
691 $form->{storno_id} = $form->{id};
694 $form->{invnumber} = "Storno zu " . $form->{invnumber};
696 $form->{employee_id} = $employee_id;
698 $main::lxdebug->leave_sub();
703 $main::lxdebug->enter_sub();
705 my $form = $main::form;
706 my %myconfig = %main::myconfig;
708 $main::auth->assert('vendor_invoice_edit');
710 map { delete $form->{$_} } qw(printed emailed queued invnumber invdate deliverydate id datepaid_1 gldate_1 acc_trans_id_1 source_1 memo_1 paid_1 exchangerate_1 AP_paid_1 storno);
711 $form->{paidaccounts} = 1;
713 $form->{invdate} = $form->current_date(\%myconfig);
715 $form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};
717 $form->{useasnew} = 1;
720 $main::lxdebug->leave_sub();
724 $main::lxdebug->enter_sub();
726 my $form = $main::form;
727 my %myconfig = %main::myconfig;
728 my $locale = $main::locale;
730 $main::auth->assert('vendor_invoice_edit');
732 $form->mtime_ischanged('ap') ;
733 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
734 for my $i (1 .. $form->{paidaccounts}) {
735 if ($form->{"paid_$i"}) {
736 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
738 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
740 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
741 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
743 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
744 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
745 $form->error($locale->text('Cannot post payment for a closed period!'))
746 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
748 if ($form->{currency} ne $form->{defaultcurrency}) {
749 # $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid); # invdate isn't set here
750 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
755 ($form->{AP}) = split /--/, $form->{AP};
756 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
757 if (IR->post_payment(\%myconfig, \%$form)){
758 if (!exists $form->{addition} && $form->{id} ne "") {
760 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
761 $form->{addition} = "PAYMENT POSTED";
762 $form->{what_done} = "invoice";
764 # /saving the history
767 $form->redirect($locale->text('Payment posted!'));
770 $form->error($locale->text('Cannot post payment!'));
772 $main::lxdebug->leave_sub();
776 my $form = $main::form;
778 my @dates = sort { $b->[1] cmp $a->[1] }
779 map { [ $_, $main::locale->reformat_date(\%main::myconfig, $_, 'yyyy-mm-dd') ] }
781 map { $form->{"datepaid_${_}"} }
782 (1..$form->{rowcount});
784 return @dates ? $dates[0]->[0] : undef;
789 $main::lxdebug->enter_sub();
791 my $form = $main::form;
792 my %myconfig = %main::myconfig;
793 my $locale = $main::locale;
795 $main::auth->assert('vendor_invoice_edit');
797 $form->mtime_ischanged('ap');
798 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
800 $form->isblank("invdate", $locale->text('Invoice Date missing!'));
801 $form->isblank("vendor_id", $locale->text('Vendor missing!'));
802 $form->isblank("invnumber", $locale->text('Invnumber missing!'));
804 $form->{invnumber} =~ s/^\s*//g;
805 $form->{invnumber} =~ s/\s*$//g;
807 # if the vendor changed get new values
808 if (($form->{previous_vendor_id} || $form->{vendor_id}) != $form->{vendor_id}) {
810 $::dispatcher->end_request;
813 if ($myconfig{mandatory_departments} && !$form->{department_id}) {
814 $form->{saved_message} = $::locale->text('You have to specify a department.');
819 remove_emptied_rows();
822 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
823 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
824 my $max_datepaid = _max_datepaid();
826 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
827 if ($form->date_max_future($invdate, \%myconfig));
828 $form->error($locale->text('Cannot post invoice for a closed period!'))
829 if ($invdate <= $closedto);
831 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
832 if ($form->{currency} ne $form->{defaultcurrency});
835 for $i (1 .. $form->{paidaccounts}) {
836 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
837 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
839 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
841 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
842 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
844 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
845 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
846 $form->error($locale->text('Cannot post payment for a closed period!'))
847 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
849 if ($form->{currency} ne $form->{defaultcurrency}) {
850 $form->{"exchangerate_$i"} = $form->{exchangerate}
851 if ($invdate == $datepaid);
852 $form->isblank("exchangerate_$i",
853 $locale->text('Exchangerate for payment missing!'));
858 ($form->{AP}) = split /--/, $form->{AP};
859 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
860 $form->{storno} ||= 0;
862 $form->{id} = 0 if $form->{postasnew};
866 if (IR->post_invoice(\%myconfig, \%$form)){
868 if(!exists $form->{addition} && $form->{id} ne "") {
869 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
870 $form->{addition} = "POSTED";
871 $form->{what_done} = 'invoice';
874 # /saving the history
875 $form->{callback} = 'ir.pl?action=add';
876 $form->redirect( $locale->text('Invoice')
877 . " $form->{invnumber} "
878 . $locale->text('posted!'));
880 $form->error($locale->text('Cannot post invoice!'));
882 $main::lxdebug->leave_sub();
886 $main::lxdebug->enter_sub();
888 my $form = $main::form;
889 my $locale = $main::locale;
891 $main::auth->assert('vendor_invoice_edit');
895 <form method=post action=$form->{script}>
898 # delete action variable
899 map { delete $form->{$_} } qw(action header);
901 foreach my $key (keys %$form) {
902 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
903 $form->{$key} =~ s/\"/"/g;
904 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
908 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
911 . $locale->text('Are you sure you want to delete Invoice Number')
912 . qq| $form->{invnumber}</h4>
914 <input name=action class=submit type=submit value="|
915 . $locale->text('Yes') . qq|">
919 $main::lxdebug->leave_sub();
923 $::lxdebug->enter_sub;
925 $::auth->assert('vendor_invoice_edit');
929 my $new_rowcount = $::form->{"rowcount"} * 1 + 1;
930 $::form->{"project_id_${new_rowcount}"} = $::form->{"globalproject_id"};
932 $::form->language_payment(\%::myconfig);
934 Common::webdav_folder($::form);
937 display_row(++$::form->{rowcount});
940 $::lxdebug->leave_sub;
944 $main::lxdebug->enter_sub();
946 my $form = $main::form;
947 my %myconfig = %main::myconfig;
948 my $locale = $main::locale;
950 $main::auth->assert('vendor_invoice_edit');
952 if (IR->delete_invoice(\%myconfig, \%$form)) {
954 if(!exists $form->{addition}) {
955 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
956 $form->{addition} = "DELETED";
959 # /saving the history
960 $form->redirect($locale->text('Invoice deleted!'));
962 $form->error($locale->text('Cannot delete invoice!'));
964 $main::lxdebug->leave_sub();
967 sub get_duedate_vendor {
968 $::lxdebug->enter_sub;
970 my $result = IR->get_duedate(
971 vendor_id => $::form->{vendor_id},
972 invdate => $::form->{invdate},
973 default => $::form->{old_duedate},
976 print $::form->ajax_response_header, $result;
977 $::lxdebug->leave_sub;