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 => 1 } ],
229 id => 'update_button',
230 accesskey => 'enter',
236 submit => [ '#form', { action_post => 1 } ],
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 => 1 } ],
246 disabled => !$form->{id} ? t8('This invoice has not been posted yet.') : undef,
250 submit => [ '#form', { action_mark_as_paid => 1 } ],
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 ]) x !!$::instance_conf->get_ir_show_mark_as_paid,
254 ], # end of combobox "Post"
257 action => [ t8('Storno'),
258 submit => [ '#form', { action_storno => 1 } ],
259 confirm => t8('Do you really want to cancel this invoice?'),
260 disabled => !$form->{id} ? t8('This invoice has not been posted yet.') : undef,
262 action => [ t8('Delete'),
263 submit => [ '#form', { action_delete => 1 } ],
264 confirm => t8('Do you really want to delete this object?'),
265 disabled => !$form->{id} ? t8('This invoice has not been posted yet.')
266 : $form->{locked} ? t8('The billing period has already been locked.')
267 : $change_never ? t8('Changing invoices has been disabled in the configuration.')
268 : $change_on_same_day_only ? t8('Invoices can only be changed on the day they are posted.')
271 ], # end of combobox "Storno"
276 action => [ t8('more') ],
279 call => [ 'set_history_window', $::form->{id} * 1, 'id', 'glid' ],
280 disabled => !$form->{id} ? t8('This invoice has not been posted yet.') : undef,
284 call => [ 'follow_up_window' ],
285 disabled => !$form->{id} ? t8('This invoice has not been posted yet.') : undef,
289 call => [ 'kivi.Draft.popup', 'ir', 'invoice', $::form->{draft_id}, $::form->{draft_description} ],
290 disabled => $form->{id} ? t8('This invoice has already been posted.')
291 : $form->{locked} ? t8('The billing period has already been locked.')
294 ], # end of combobox "more"
300 $main::lxdebug->enter_sub();
302 my $form = $main::form;
303 my %myconfig = %main::myconfig;
304 my $locale = $main::locale;
305 my $cgi = $::request->{cgi};
307 $main::auth->assert('vendor_invoice_edit');
312 $TMPL_VAR{invoice_obj} = SL::DB::PurchaseInvoice->load_cached($form->{id}) if $form->{id};
313 $TMPL_VAR{vendor_obj} = SL::DB::Vendor->load_cached($form->{vendor_id}) if $form->{vendor_id};
314 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
315 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
317 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
319 my @old_project_ids = ($form->{"globalproject_id"});
320 map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
322 $form->get_lists("taxzones" => ($form->{id} ? "ALL_TAXZONES" : "ALL_ACTIVE_TAXZONES"),
323 "currencies" => "ALL_CURRENCIES",
324 "price_factors" => "ALL_PRICE_FACTORS");
326 $TMPL_VAR{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
327 $TMPL_VAR{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ or => [ id => $::form->{employee_id}, deleted => 0 ] ]);
328 $TMPL_VAR{ALL_CONTACTS} = SL::DB::Manager::Contact->get_all_sorted(query => [
330 cp_cv_id => $::form->{"$::form->{vc}_id"} * 1,
333 cp_id => $::form->{cp_id} * 1
338 # currencies and exchangerate
339 my @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
340 my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
341 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
342 # show_exchangerate is also later needed in another template
343 $form->{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
344 $TMPL_VAR{currencies} = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
345 '-values' => \@values, '-labels' => \%labels,
346 '-onchange' => "document.getElementById('update_button').click();"
347 )) if scalar @values;
348 push @custom_hiddens, "forex";
349 push @custom_hiddens, "exchangerate" if $form->{forex};
351 $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
352 $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
354 # set option selected
355 foreach my $item (qw(AP)) {
356 $form->{"select$item"} =~ s/ selected//;
357 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
360 $TMPL_VAR{is_type_credit_note} = $form->{type} eq "credit_note";
361 $TMPL_VAR{is_format_html} = $form->{format} eq 'html';
362 $TMPL_VAR{dateformat} = $myconfig{dateformat};
363 $TMPL_VAR{numberformat} = $myconfig{numberformat};
366 $TMPL_VAR{HIDDENS} = [qw(
367 id action type media format queued printed emailed title vc discount
368 title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
369 max_dunning_level dunning_amount
370 shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptogln shiptocontact shiptophone shiptofax
371 shiptoemail shiptodepartment_1 shiptodepartment_2 message email subject cc bcc taxaccounts cursor_fokus
372 convert_from_do_ids convert_from_oe_ids convert_from_ap_ids show_details gldate useasnew
374 map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
376 $TMPL_VAR{payment_terms_obj} = get_payment_terms_for_invoice();
377 $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};
379 $::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));
381 setup_ir_action_bar();
385 print $form->parse_html_template("ir/form_header", \%TMPL_VAR);
387 $main::lxdebug->leave_sub();
391 my @fields = qw(acc_trans_id gldate datepaid source memo paid AP_paid);
393 grep { $_->{paid} != 0 }
396 +{ map { ($_ => delete($::form->{"${_}_${idx}"})) } @fields }
397 } (1..$::form->{paidaccounts});
399 @payments = sort_by { DateTime->from_kivitendo($_->{datepaid}) } @payments;
401 $::form->{paidaccounts} = max scalar(@payments), 1;
403 foreach my $idx (1 .. scalar(@payments)) {
404 my $payment = $payments[$idx - 1];
405 $::form->{"${_}_${idx}"} = $payment->{$_} for @fields;
410 $main::lxdebug->enter_sub();
412 my $form = $main::form;
413 my %myconfig = %main::myconfig;
414 my $locale = $main::locale;
416 $main::auth->assert('vendor_invoice_edit');
418 $form->{invtotal} = $form->{invsubtotal};
419 $form->{oldinvtotal} = $form->{invtotal};
422 $form->{rows} = max 2,
423 $form->numtextrows($form->{notes}, 26, 8),
424 $form->numtextrows($form->{intnotes}, 35, 8);
427 # tax, total and subtotal calculations
428 my ($tax, $subtotal);
429 $form->{taxaccounts_array} = [ split / /, $form->{taxaccounts} ];
431 foreach my $item (@{ $form->{taxaccounts_array} }) {
432 if ($form->{"${item}_base"}) {
433 if ($form->{taxincluded}) {
434 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"}
435 / (1 + $form->{"${item}_rate"})), 2);
436 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
438 $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
439 $form->{invtotal} += $form->{"${item}_total"};
446 $form->{follow_ups} = FU->follow_ups('trans_id' => $form->{id}, 'not_done' => 1) || [];
447 $form->{follow_ups_unfinished} = ( sum map { $_->{due} * 1 } @{ $form->{follow_ups} } ) || 0;
454 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
455 $form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];
457 # Standard Konto für Umlaufvermögen
458 my $accno_arap = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
460 for my $i (1 .. $form->{paidaccounts}) {
461 $form->{"changeable_$i"} = 1;
462 if (SL::DB::Default->get->payments_changeable == 0) {
464 $form->{"changeable_$i"} = ($form->{"acc_trans_id_$i"})? 0 : 1;
465 } elsif (SL::DB::Default->get->payments_changeable == 2) {
467 $form->{"changeable_$i"} = (($form->{"gldate_$i"} eq '') ||
468 ($form->current_date(\%myconfig) eq $form->{"gldate_$i"}));
471 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
472 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
474 #deaktivieren von Zahlungen ausserhalb der Bücherkontrolle
475 if ($form->date_closed($form->{"gldate_$i"})) {
476 $form->{"changeable_$i"} = 0;
479 $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
480 if (!$form->{"AP_paid_$i"}) {
481 $form->{"selectAP_paid_$i"} =~ s/option>$accno_arap--(.*?)>/option selected>$accno_arap--$1>/;
483 $form->{"selectAP_paid_$i"} =~ s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
486 $totalpaid += $form->{"paid_$i"};
489 $form->{ALL_DELIVERY_TERMS} = SL::DB::Manager::DeliveryTerm->get_all_sorted();
491 print $form->parse_html_template('ir/form_footer', {
492 is_type_credit_note => ($form->{type} eq "credit_note"),
493 totalpaid => $totalpaid,
494 paid_missing => $form->{invtotal} - $totalpaid,
495 show_storno => $form->{id} && !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ap") && !$totalpaid,
496 show_delete => ($::instance_conf->get_ir_changeable == 2)
497 ? ($form->current_date(\%myconfig) eq $form->{gldate})
498 : ($::instance_conf->get_ir_changeable == 1),
499 today => DateTime->today,
501 ##print $form->parse_html_template('ir/_payments'); # parser
502 ##print $form->parse_html_template('webdav/_list'); # parser
504 $main::lxdebug->leave_sub();
508 $::auth->assert('vendor_invoice_edit');
510 SL::DB::PurchaseInvoice->new(id => $::form->{id})->load->mark_as_paid;
512 $::form->redirect($::locale->text("Marked as paid"));
520 $main::lxdebug->enter_sub();
522 my $form = $main::form;
523 my %myconfig = %main::myconfig;
525 $main::auth->assert('vendor_invoice_edit');
527 if (($form->{previous_vendor_id} || $form->{vendor_id}) != $form->{vendor_id}) {
528 IR->get_vendor(\%myconfig, $form);
531 if (!$form->{forex}) { # read exchangerate from input field (not hidden)
532 $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate});
534 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'sell');
535 $form->{exchangerate} = $form->{forex} if $form->{forex};
537 for my $i (1 .. $form->{paidaccounts}) {
538 next unless $form->{"paid_$i"};
539 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
540 $form->{"forex_$i"} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
541 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
544 my $i = $form->{rowcount};
545 my $exchangerate = ($form->{exchangerate} * 1) || 1;
547 if ( ($form->{"partnumber_$i"} eq "")
548 && ($form->{"description_$i"} eq "")
549 && ($form->{"partsgroup_$i"} eq "")) {
550 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
555 IR->retrieve_item(\%myconfig, \%$form);
557 my $rows = scalar @{ $form->{item_list} };
559 $form->{"discount_$i"} = $form->parse_amount(\%myconfig, $form->{"discount_$i"}) / 100.0;
560 $form->{"discount_$i"} ||= $form->{vendor_discount};
563 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
564 if( !$form->{"qty_$i"} ) {
565 $form->{"qty_$i"} = 1;
570 select_item(mode => 'IR', pre_entered_qty => $form->{"qty_$i"});
571 $::dispatcher->end_request;
575 # override sellprice if there is one entered
576 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
578 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
579 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
581 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
583 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
584 my $dec_qty = length $1;
585 my $decimalplaces = max 2, $dec_qty;
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 my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"});
609 $form->{creditremaining} -= $amount;
610 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
611 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
612 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100.0);
619 # ok, so this is a new part
620 # ask if it is a part or service item
622 if ( $form->{"partsgroup_$i"}
623 && ($form->{"partsnumber_$i"} eq "")
624 && ($form->{"description_$i"} eq "")) {
626 $form->{"discount_$i"} = "";
630 $form->{"id_$i"} = 0;
635 $main::lxdebug->leave_sub();
639 $main::lxdebug->enter_sub();
641 my $form = $main::form;
642 my %myconfig = %main::myconfig;
643 my $locale = $main::locale;
645 $main::auth->assert('vendor_invoice_edit');
647 if ($form->{storno}) {
648 $form->error($locale->text('Cannot storno storno invoice!'));
651 if (IS->has_storno(\%myconfig, $form, "ap")) {
652 $form->error($locale->text("Invoice has already been storno'd!"));
655 $form->error($locale->text('Cannot post storno for a closed period!'))
656 if ( $form->date_closed($form->{invdate}, \%myconfig));
658 my $employee_id = $form->{employee_id};
663 # Payments must not be recorded for the new storno invoice.
664 $form->{paidaccounts} = 0;
665 map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ gldate_ acc_trans_id_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
666 # set new ids for storno invoice
667 # set new persistent ids for storno invoice items
668 $form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};
671 if(!exists $form->{addition} && $form->{id} ne "") {
672 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
673 $form->{what_done} = "invoice";
674 $form->{addition} = "CANCELED";
677 # /saving the history
679 # record link invoice to storno
680 $form->{convert_from_ap_ids} = $form->{id};
681 $form->{storno_id} = $form->{id};
684 $form->{invnumber} = "Storno zu " . $form->{invnumber};
686 $form->{employee_id} = $employee_id;
688 $main::lxdebug->leave_sub();
693 $main::lxdebug->enter_sub();
695 my $form = $main::form;
696 my %myconfig = %main::myconfig;
698 $main::auth->assert('vendor_invoice_edit');
700 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);
701 $form->{paidaccounts} = 1;
703 $form->{invdate} = $form->current_date(\%myconfig);
705 $form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};
707 $form->{useasnew} = 1;
710 $main::lxdebug->leave_sub();
714 $main::lxdebug->enter_sub();
716 my $form = $main::form;
717 my %myconfig = %main::myconfig;
718 my $locale = $main::locale;
720 $main::auth->assert('vendor_invoice_edit');
722 $form->mtime_ischanged('ap') ;
723 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
724 for my $i (1 .. $form->{paidaccounts}) {
725 if ($form->{"paid_$i"}) {
726 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
728 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
730 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
731 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
733 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
734 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
735 $form->error($locale->text('Cannot post payment for a closed period!'))
736 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
738 if ($form->{currency} ne $form->{defaultcurrency}) {
739 # $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid); # invdate isn't set here
740 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
745 ($form->{AP}) = split /--/, $form->{AP};
746 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
747 if (IR->post_payment(\%myconfig, \%$form)){
748 if (!exists $form->{addition} && $form->{id} ne "") {
750 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
751 $form->{addition} = "PAYMENT POSTED";
752 $form->{what_done} = "invoice";
754 # /saving the history
757 $form->redirect($locale->text('Payment posted!'));
760 $form->error($locale->text('Cannot post payment!'));
762 $main::lxdebug->leave_sub();
766 my $form = $main::form;
768 my @dates = sort { $b->[1] cmp $a->[1] }
769 map { [ $_, $main::locale->reformat_date(\%main::myconfig, $_, 'yyyy-mm-dd') ] }
771 map { $form->{"datepaid_${_}"} }
772 (1..$form->{rowcount});
774 return @dates ? $dates[0]->[0] : undef;
779 $main::lxdebug->enter_sub();
781 my $form = $main::form;
782 my %myconfig = %main::myconfig;
783 my $locale = $main::locale;
785 $main::auth->assert('vendor_invoice_edit');
787 $form->mtime_ischanged('ap');
788 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
790 $form->isblank("invdate", $locale->text('Invoice Date missing!'));
791 $form->isblank("vendor_id", $locale->text('Vendor missing!'));
792 $form->isblank("invnumber", $locale->text('Invnumber missing!'));
794 $form->{invnumber} =~ s/^\s*//g;
795 $form->{invnumber} =~ s/\s*$//g;
797 # if the vendor changed get new values
798 if (($form->{previous_vendor_id} || $form->{vendor_id}) != $form->{vendor_id}) {
800 $::dispatcher->end_request;
803 if ($myconfig{mandatory_departments} && !$form->{department_id}) {
804 $form->{saved_message} = $::locale->text('You have to specify a department.');
809 remove_emptied_rows();
812 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
813 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
814 my $max_datepaid = _max_datepaid();
816 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
817 if ($form->date_max_future($invdate, \%myconfig));
818 $form->error($locale->text('Cannot post invoice for a closed period!'))
819 if ($invdate <= $closedto);
821 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
822 if ($form->{currency} ne $form->{defaultcurrency});
825 for $i (1 .. $form->{paidaccounts}) {
826 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
827 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
829 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
831 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
832 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
834 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
835 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
836 $form->error($locale->text('Cannot post payment for a closed period!'))
837 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
839 if ($form->{currency} ne $form->{defaultcurrency}) {
840 $form->{"exchangerate_$i"} = $form->{exchangerate}
841 if ($invdate == $datepaid);
842 $form->isblank("exchangerate_$i",
843 $locale->text('Exchangerate for payment missing!'));
848 ($form->{AP}) = split /--/, $form->{AP};
849 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
850 $form->{storno} ||= 0;
852 $form->{id} = 0 if $form->{postasnew};
856 if (IR->post_invoice(\%myconfig, \%$form)){
858 if(!exists $form->{addition} && $form->{id} ne "") {
859 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
860 $form->{addition} = "POSTED";
861 $form->{what_done} = 'invoice';
864 # /saving the history
865 $form->{callback} = 'ir.pl?action=add';
866 $form->redirect( $locale->text('Invoice')
867 . " $form->{invnumber} "
868 . $locale->text('posted!'));
870 $form->error($locale->text('Cannot post invoice!'));
872 $main::lxdebug->leave_sub();
876 $main::lxdebug->enter_sub();
878 my $form = $main::form;
879 my $locale = $main::locale;
881 $main::auth->assert('vendor_invoice_edit');
885 <form method=post action=$form->{script}>
888 # delete action variable
889 map { delete $form->{$_} } qw(action header);
891 foreach my $key (keys %$form) {
892 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
893 $form->{$key} =~ s/\"/"/g;
894 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
898 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
901 . $locale->text('Are you sure you want to delete Invoice Number')
902 . qq| $form->{invnumber}</h4>
904 <input name=action class=submit type=submit value="|
905 . $locale->text('Yes') . qq|">
909 $main::lxdebug->leave_sub();
913 $::lxdebug->enter_sub;
915 $::auth->assert('vendor_invoice_edit');
919 my $new_rowcount = $::form->{"rowcount"} * 1 + 1;
920 $::form->{"project_id_${new_rowcount}"} = $::form->{"globalproject_id"};
922 $::form->language_payment(\%::myconfig);
924 Common::webdav_folder($::form);
927 display_row(++$::form->{rowcount});
930 $::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('vendor_invoice_edit');
942 if (IR->delete_invoice(\%myconfig, \%$form)) {
944 if(!exists $form->{addition}) {
945 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
946 $form->{addition} = "DELETED";
949 # /saving the history
950 $form->redirect($locale->text('Invoice deleted!'));
952 $form->error($locale->text('Cannot delete invoice!'));
954 $main::lxdebug->leave_sub();
957 sub get_duedate_vendor {
958 $::lxdebug->enter_sub;
960 my $result = IR->get_duedate(
961 vendor_id => $::form->{vendor_id},
962 invdate => $::form->{invdate},
963 default => $::form->{old_duedate},
966 print $::form->ajax_response_header, $result;
967 $::lxdebug->leave_sub;