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 #======================================================================
40 use SL::DB::PurchaseInvoice;
41 use List::Util qw(max sum);
42 use List::UtilsBy qw(sort_by);
44 require "bin/mozilla/io.pl";
45 require "bin/mozilla/arap.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 #quote all_vendor Bug 133
114 foreach my $ref (@{ $form->{all_vendor} }) {
115 $ref->{name} = $form->quote($ref->{name});
118 if ($form->{all_vendor}) {
119 unless ($form->{vendor_id}) {
120 $form->{vendor_id} = $form->{all_vendor}->[0]->{id};
124 $form->backup_vars(qw(payment_id language_id taxzone_id
125 currency delivery_term_id intnotes cp_id));
127 IR->get_vendor(\%myconfig, \%$form);
128 IR->retrieve_invoice(\%myconfig, \%$form);
130 $form->restore_vars(qw(payment_id language_id taxzone_id
131 currency delivery_term_id intnotes cp_id));
133 my @curr = $form->get_all_currencies();
134 map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
136 $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
138 # build vendor/customer drop down comatibility... don't ask
139 if (@{ $form->{"all_vendor"} || [] }) {
140 $form->{"selectvendor"} = 1;
141 $form->{vendor} = qq|$form->{vendor}--$form->{vendor_id}|;
145 if ($form->{all_departments}) {
146 $form->{selectdepartment} = "<option>\n";
147 $form->{department} = "$form->{department}--$form->{department_id}";
150 $form->{selectdepartment} .=
151 "<option>$_->{description}--$_->{id}\n"
152 } (@{ $form->{all_departments} || [] });
156 $form->{forex} = $form->{exchangerate};
157 my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
159 foreach my $key (keys %{ $form->{AP_links} }) {
161 foreach my $ref (@{ $form->{AP_links}{$key} }) {
162 $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}</option>";
165 next unless $form->{acc_trans}{$key};
167 if ($key eq "AP_paid") {
168 for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
169 $form->{"AP_paid_$i"} =
170 "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
172 $form->{"acc_trans_id_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{acc_trans_id};
174 $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount};
175 $form->{"datepaid_$i"} =
176 $form->{acc_trans}{$key}->[$i - 1]->{transdate};
177 $form->{"gldate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{gldate};
178 $form->{"forex_$i"} = $form->{"exchangerate_$i"} =
179 $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
180 $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
181 $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
183 $form->{paidaccounts} = $i;
187 "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
192 $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
194 $form->{AP} = $form->{AP_1} unless $form->{id};
197 ($form->datetonum($form->{invdate}, \%myconfig) <=
198 $form->datetonum($form->{closedto}, \%myconfig));
200 $main::lxdebug->leave_sub();
203 sub prepare_invoice {
204 $main::lxdebug->enter_sub();
206 my $form = $main::form;
207 my %myconfig = %main::myconfig;
209 $main::auth->assert('vendor_invoice_edit');
211 $form->{type} = "purchase_invoice";
215 map { $form->{$_} =~ s/\"/"/g } qw(invnumber ordnumber quonumber);
218 foreach my $ref (@{ $form->{invoice_details} }) {
220 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
221 # übernommen aus is.pl Fix für Bug 1642. Nebenwirkungen? jb 12.5.2011
222 # getestet: Lieferantenauftrag -> Rechnung i.O.
223 # Lieferantenauftrag -> Lieferschein -> Rechnung i.O.
224 # Werte: 20% (Lieferantenrabatt), 12,4% individuell und 0,4 individuell s.a.
225 # Screenshot zu Bug 1642
226 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
228 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
230 my $decimalplaces = ($dec > 2) ? $dec : 2;
232 $form->{"sellprice_$i"} =
233 $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
236 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
237 $dec_qty = length $dec_qty;
240 $form->format_amount(\%myconfig, ($form->{"qty_$i"} * -1), $dec_qty);
242 $form->{rowcount} = $i;
246 $main::lxdebug->leave_sub();
250 $main::lxdebug->enter_sub();
252 my $form = $main::form;
253 my %myconfig = %main::myconfig;
254 my $locale = $main::locale;
255 my $cgi = $::request->{cgi};
257 $main::auth->assert('vendor_invoice_edit');
262 $TMPL_VAR{invoice_obj} = SL::DB::PurchaseInvoice->new(id => $form->{id})->load if $form->{id};
263 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
264 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
266 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
268 my @old_project_ids = ($form->{"globalproject_id"});
269 map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
271 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
273 "old_id" => \@old_project_ids },
274 "taxzones" => "ALL_TAXZONES",
275 "currencies" => "ALL_CURRENCIES",
276 "vendors" => "ALL_VENDORS",
277 "departments" => "all_departments",
278 "price_factors" => "ALL_PRICE_FACTORS");
280 $TMPL_VAR{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ or => [ id => $::form->{employee_id}, deleted => 0 ] ]);
281 $TMPL_VAR{ALL_CONTACTS} = SL::DB::Manager::Contact->get_all_sorted(query => [
283 cp_cv_id => $::form->{"$::form->{vc}_id"} * 1,
286 cp_id => $::form->{cp_id} * 1
290 $TMPL_VAR{department_labels} = sub { "$_[0]->{description}--$_[0]->{id}" };
293 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
294 $TMPL_VAR{vclimit} = $myconfig{vclimit};
295 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('vendor', '', 1, 0)";
296 push @custom_hiddens, "vendor_id";
297 push @custom_hiddens, "oldvendor";
298 push @custom_hiddens, "selectvendor";
300 # currencies and exchangerate
301 my @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
302 my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
303 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
304 # show_exchangerate is also later needed in another template
305 $form->{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
306 $TMPL_VAR{currencies} = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
307 '-values' => \@values, '-labels' => \%labels,
308 '-onchange' => "document.getElementById('update_button').click();"
309 )) if scalar @values;
310 push @custom_hiddens, "forex";
311 push @custom_hiddens, "exchangerate" if $form->{forex};
313 $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
314 $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
316 my $follow_up_vc = $form->{vendor};
317 $follow_up_vc =~ s/--\d*\s*$//;
318 $TMPL_VAR{vendor_name} = $follow_up_vc;
320 # set option selected
321 foreach my $item (qw(AP)) {
322 $form->{"select$item"} =~ s/ selected//;
323 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
326 $TMPL_VAR{is_type_credit_note} = $form->{type} eq "credit_note";
327 $TMPL_VAR{is_format_html} = $form->{format} eq 'html';
328 $TMPL_VAR{dateformat} = $myconfig{dateformat};
329 $TMPL_VAR{numberformat} = $myconfig{numberformat};
332 $TMPL_VAR{HIDDENS} = [qw(
333 id action type media format queued printed emailed title vc discount
334 title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
335 max_dunning_level dunning_amount
336 shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptogln shiptocontact shiptophone shiptofax
337 shiptoemail shiptodepartment_1 shiptodepartment_2 message email subject cc bcc taxaccounts cursor_fokus
338 convert_from_do_ids convert_from_oe_ids convert_from_ap_ids show_details gldate useasnew
340 map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
342 $TMPL_VAR{payment_terms_obj} = get_payment_terms_for_invoice();
343 $form->{duedate} = $TMPL_VAR{payment_terms_obj}->calc_date(reference_date => $form->{invdate}, due_date => $form->{due_due})->to_kivitendo if $TMPL_VAR{payment_terms_obj};
345 $::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.Draft kivi.SalesPurchase ckeditor/ckeditor ckeditor/adapters/jquery kivi.io autocomplete_customer autocomplete_part client_js));
349 print $form->parse_html_template("ir/form_header", \%TMPL_VAR);
351 $main::lxdebug->leave_sub();
355 my @fields = qw(acc_trans_id gldate datepaid source memo paid AP_paid);
357 grep { $_->{paid} != 0 }
360 +{ map { ($_ => delete($::form->{"${_}_${idx}"})) } @fields }
361 } (1..$::form->{paidaccounts});
363 @payments = sort_by { DateTime->from_kivitendo($_->{datepaid}) } @payments;
365 $::form->{paidaccounts} = max scalar(@payments), 1;
367 foreach my $idx (1 .. scalar(@payments)) {
368 my $payment = $payments[$idx - 1];
369 $::form->{"${_}_${idx}"} = $payment->{$_} for @fields;
374 $main::lxdebug->enter_sub();
376 my $form = $main::form;
377 my %myconfig = %main::myconfig;
378 my $locale = $main::locale;
380 $main::auth->assert('vendor_invoice_edit');
382 $form->{invtotal} = $form->{invsubtotal};
383 $form->{oldinvtotal} = $form->{invtotal};
386 $form->{rows} = max 2,
387 $form->numtextrows($form->{notes}, 26, 8),
388 $form->numtextrows($form->{intnotes}, 35, 8);
391 # tax, total and subtotal calculations
392 my ($tax, $subtotal);
393 $form->{taxaccounts_array} = [ split / /, $form->{taxaccounts} ];
395 foreach my $item (@{ $form->{taxaccounts_array} }) {
396 if ($form->{"${item}_base"}) {
397 if ($form->{taxincluded}) {
398 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"}
399 / (1 + $form->{"${item}_rate"})), 2);
400 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
402 $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
403 $form->{invtotal} += $form->{"${item}_total"};
410 $form->{follow_ups} = FU->follow_ups('trans_id' => $form->{id}, 'not_done' => 1) || [];
411 $form->{follow_ups_unfinished} = ( sum map { $_->{due} * 1 } @{ $form->{follow_ups} } ) || 0;
418 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
419 $form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];
421 # Standard Konto für Umlaufvermögen
422 my $accno_arap = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
424 for my $i (1 .. $form->{paidaccounts}) {
425 $form->{"changeable_$i"} = 1;
426 if (SL::DB::Default->get->payments_changeable == 0) {
428 $form->{"changeable_$i"} = ($form->{"acc_trans_id_$i"})? 0 : 1;
429 } elsif (SL::DB::Default->get->payments_changeable == 2) {
431 $form->{"changeable_$i"} = (($form->{"gldate_$i"} eq '') ||
432 ($form->current_date(\%myconfig) eq $form->{"gldate_$i"}));
435 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
436 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
438 #deaktivieren von Zahlungen ausserhalb der Bücherkontrolle
439 if ($form->date_closed($form->{"gldate_$i"})) {
440 $form->{"changeable_$i"} = 0;
443 $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
444 if (!$form->{"AP_paid_$i"}) {
445 $form->{"selectAP_paid_$i"} =~ s/option>$accno_arap--(.*?)>/option selected>$accno_arap--$1>/;
447 $form->{"selectAP_paid_$i"} =~ s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
450 $totalpaid += $form->{"paid_$i"};
453 $form->{ALL_DELIVERY_TERMS} = SL::DB::Manager::DeliveryTerm->get_all_sorted();
455 print $form->parse_html_template('ir/form_footer', {
456 is_type_credit_note => ($form->{type} eq "credit_note"),
457 totalpaid => $totalpaid,
458 paid_missing => $form->{invtotal} - $totalpaid,
459 show_storno => $form->{id} && !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ap") && !$totalpaid,
460 show_delete => ($::instance_conf->get_ir_changeable == 2)
461 ? ($form->current_date(\%myconfig) eq $form->{gldate})
462 : ($::instance_conf->get_ir_changeable == 1),
463 today => DateTime->today,
465 ##print $form->parse_html_template('ir/_payments'); # parser
466 ##print $form->parse_html_template('webdav/_list'); # parser
468 $main::lxdebug->leave_sub();
472 $main::lxdebug->enter_sub();
474 my $form = $main::form;
475 my %myconfig = %main::myconfig;
477 $main::auth->assert('vendor_invoice_edit');
479 &mark_as_paid_common(\%myconfig,"ap");
481 $main::lxdebug->leave_sub();
485 $main::lxdebug->enter_sub();
487 my $form = $main::form;
488 my %myconfig = %main::myconfig;
490 $main::auth->assert('vendor_invoice_edit');
492 &check_name('vendor');
494 if (!$form->{forex}) { # read exchangerate from input field (not hidden)
495 $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate});
497 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'sell');
498 $form->{exchangerate} = $form->{forex} if $form->{forex};
500 for my $i (1 .. $form->{paidaccounts}) {
501 next unless $form->{"paid_$i"};
502 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
503 $form->{"forex_$i"} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
504 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
507 my $i = $form->{rowcount};
508 my $exchangerate = ($form->{exchangerate} * 1) || 1;
510 if ( ($form->{"partnumber_$i"} eq "")
511 && ($form->{"description_$i"} eq "")
512 && ($form->{"partsgroup_$i"} eq "")) {
513 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
518 IR->retrieve_item(\%myconfig, \%$form);
520 my $rows = scalar @{ $form->{item_list} };
522 $form->{"discount_$i"} = $form->parse_amount(\%myconfig, $form->{"discount_$i"}) / 100.0;
523 $form->{"discount_$i"} ||= $form->{vendor_discount};
526 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
527 if( !$form->{"qty_$i"} ) {
528 $form->{"qty_$i"} = 1;
533 select_item(mode => 'IR', pre_entered_qty => $form->{"qty_$i"});
534 $::dispatcher->end_request;
538 # override sellprice if there is one entered
539 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
541 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
542 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
544 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
546 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
547 my $dec_qty = length $1;
548 my $decimalplaces = max 2, $dec_qty;
551 $form->{"sellprice_$i"} = $sellprice;
553 my $record = _make_record();
554 my $price_source = SL::PriceSource->new(record_item => $record->items->[$i-1], record => $record);
555 my $best_price = $price_source->best_price;
556 my $best_discount = $price_source->best_discount;
559 $::form->{"sellprice_$i"} = $best_price->price;
560 $::form->{"active_price_source_$i"} = $best_price->source;
562 if ($best_discount) {
563 $::form->{"discount_$i"} = $best_discount->discount;
564 $::form->{"active_discount_source_$i"} = $best_discount->source;
567 # if there is an exchange rate adjust sellprice
568 $form->{"sellprice_$i"} /= $exchangerate;
571 my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"});
572 $form->{creditremaining} -= $amount;
573 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
574 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
575 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100.0);
582 # ok, so this is a new part
583 # ask if it is a part or service item
585 if ( $form->{"partsgroup_$i"}
586 && ($form->{"partsnumber_$i"} eq "")
587 && ($form->{"description_$i"} eq "")) {
589 $form->{"discount_$i"} = "";
593 $form->{"id_$i"} = 0;
598 $main::lxdebug->leave_sub();
602 $main::lxdebug->enter_sub();
604 my $form = $main::form;
605 my %myconfig = %main::myconfig;
606 my $locale = $main::locale;
608 $main::auth->assert('vendor_invoice_edit');
610 if ($form->{storno}) {
611 $form->error($locale->text('Cannot storno storno invoice!'));
614 if (IS->has_storno(\%myconfig, $form, "ap")) {
615 $form->error($locale->text("Invoice has already been storno'd!"));
618 $form->error($locale->text('Cannot post storno for a closed period!'))
619 if ( $form->date_closed($form->{invdate}, \%myconfig));
621 my $employee_id = $form->{employee_id};
626 # Payments must not be recorded for the new storno invoice.
627 $form->{paidaccounts} = 0;
628 map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ gldate_ acc_trans_id_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
629 # set new ids for storno invoice
630 # set new persistent ids for storno invoice items
631 $form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};
634 if(!exists $form->{addition} && $form->{id} ne "") {
635 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
636 $form->{what_done} = "invoice";
637 $form->{addition} = "CANCELED";
640 # /saving the history
642 # record link invoice to storno
643 $form->{convert_from_ap_ids} = $form->{id};
644 $form->{storno_id} = $form->{id};
647 $form->{invnumber} = "Storno zu " . $form->{invnumber};
649 $form->{employee_id} = $employee_id;
651 $main::lxdebug->leave_sub();
656 $main::lxdebug->enter_sub();
658 my $form = $main::form;
659 my %myconfig = %main::myconfig;
661 $main::auth->assert('vendor_invoice_edit');
663 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);
664 $form->{paidaccounts} = 1;
666 $form->{invdate} = $form->current_date(\%myconfig);
668 $form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};
670 $form->{useasnew} = 1;
673 $main::lxdebug->leave_sub();
677 $main::lxdebug->enter_sub();
679 my $form = $main::form;
680 my %myconfig = %main::myconfig;
681 my $locale = $main::locale;
683 $main::auth->assert('vendor_invoice_edit');
685 $form->mtime_ischanged('ap') ;
686 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
687 for my $i (1 .. $form->{paidaccounts}) {
688 if ($form->{"paid_$i"}) {
689 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
691 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
693 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
694 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
696 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
697 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
698 $form->error($locale->text('Cannot post payment for a closed period!'))
699 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
701 if ($form->{currency} ne $form->{defaultcurrency}) {
702 # $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid); # invdate isn't set here
703 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
708 ($form->{AP}) = split /--/, $form->{AP};
709 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
710 if (IR->post_payment(\%myconfig, \%$form)){
711 if (!exists $form->{addition} && $form->{id} ne "") {
713 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
714 $form->{addition} = "PAYMENT POSTED";
715 $form->{what_done} = "invoice";
717 # /saving the history
720 $form->redirect($locale->text('Payment posted!'));
723 $form->error($locale->text('Cannot post payment!'));
725 $main::lxdebug->leave_sub();
729 my $form = $main::form;
731 my @dates = sort { $b->[1] cmp $a->[1] }
732 map { [ $_, $main::locale->reformat_date(\%main::myconfig, $_, 'yyyy-mm-dd') ] }
734 map { $form->{"datepaid_${_}"} }
735 (1..$form->{rowcount});
737 return @dates ? $dates[0]->[0] : undef;
742 $main::lxdebug->enter_sub();
744 my $form = $main::form;
745 my %myconfig = %main::myconfig;
746 my $locale = $main::locale;
748 $main::auth->assert('vendor_invoice_edit');
750 $form->mtime_ischanged('ap');
751 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
753 $form->isblank("invdate", $locale->text('Invoice Date missing!'));
754 $form->isblank("vendor", $locale->text('Vendor missing!'));
755 $form->isblank("invnumber", $locale->text('Invnumber missing!'));
757 $form->{invnumber} =~ s/^\s*//g;
758 $form->{invnumber} =~ s/\s*$//g;
760 # if the vendor changed get new values
761 if (&check_name('vendor')) {
763 $::dispatcher->end_request;
766 if ($myconfig{mandatory_departments} && !$form->{department_id}) {
767 $form->{saved_message} = $::locale->text('You have to specify a department.');
772 remove_emptied_rows();
775 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
776 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
777 my $max_datepaid = _max_datepaid();
779 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
780 if ($form->date_max_future($invdate, \%myconfig));
781 $form->error($locale->text('Cannot post invoice for a closed period!'))
782 if ($invdate <= $closedto);
784 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
785 if ($form->{currency} ne $form->{defaultcurrency});
788 for $i (1 .. $form->{paidaccounts}) {
789 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
790 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
792 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
794 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
795 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
797 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
798 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
799 $form->error($locale->text('Cannot post payment for a closed period!'))
800 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
802 if ($form->{currency} ne $form->{defaultcurrency}) {
803 $form->{"exchangerate_$i"} = $form->{exchangerate}
804 if ($invdate == $datepaid);
805 $form->isblank("exchangerate_$i",
806 $locale->text('Exchangerate for payment missing!'));
811 ($form->{AP}) = split /--/, $form->{AP};
812 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
813 $form->{storno} ||= 0;
815 $form->{id} = 0 if $form->{postasnew};
819 if (IR->post_invoice(\%myconfig, \%$form)){
821 if(!exists $form->{addition} && $form->{id} ne "") {
822 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
823 $form->{addition} = "POSTED";
824 $form->{what_done} = 'invoice';
827 # /saving the history
828 $form->redirect( $locale->text('Invoice')
829 . " $form->{invnumber} "
830 . $locale->text('posted!'));
832 $form->error($locale->text('Cannot post invoice!'));
834 $main::lxdebug->leave_sub();
838 $main::lxdebug->enter_sub();
840 my $form = $main::form;
841 my $locale = $main::locale;
843 $main::auth->assert('vendor_invoice_edit');
847 <form method=post action=$form->{script}>
850 # delete action variable
851 map { delete $form->{$_} } qw(action header);
853 foreach my $key (keys %$form) {
854 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
855 $form->{$key} =~ s/\"/"/g;
856 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
860 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
863 . $locale->text('Are you sure you want to delete Invoice Number')
864 . qq| $form->{invnumber}</h4>
866 <input name=action class=submit type=submit value="|
867 . $locale->text('Yes') . qq|">
871 $main::lxdebug->leave_sub();
875 $::lxdebug->enter_sub;
877 $::auth->assert('vendor_invoice_edit');
881 my $new_rowcount = $::form->{"rowcount"} * 1 + 1;
882 $::form->{"project_id_${new_rowcount}"} = $::form->{"globalproject_id"};
884 $::form->language_payment(\%::myconfig);
886 Common::webdav_folder($::form);
889 display_row(++$::form->{rowcount});
892 $::lxdebug->leave_sub;
896 $main::lxdebug->enter_sub();
898 my $form = $main::form;
899 my %myconfig = %main::myconfig;
900 my $locale = $main::locale;
902 $main::auth->assert('vendor_invoice_edit');
904 if (IR->delete_invoice(\%myconfig, \%$form)) {
906 if(!exists $form->{addition}) {
907 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
908 $form->{addition} = "DELETED";
911 # /saving the history
912 $form->redirect($locale->text('Invoice deleted!'));
914 $form->error($locale->text('Cannot delete invoice!'));
916 $main::lxdebug->leave_sub();
919 sub get_duedate_vendor {
920 $::lxdebug->enter_sub;
922 my $result = IR->get_duedate(
923 vendor_id => $::form->{vendor_id},
924 invdate => $::form->{invdate},
925 default => $::form->{old_duedate},
928 print $::form->ajax_response_header, $result;
929 $::lxdebug->leave_sub;