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::PurchaseInvoice;
40 use List::Util qw(max sum);
41 use List::UtilsBy qw(sort_by);
43 require "bin/mozilla/io.pl";
44 require "bin/mozilla/arap.pl";
45 require "bin/mozilla/common.pl";
54 $main::lxdebug->enter_sub();
56 my $form = $main::form;
57 my $locale = $main::locale;
59 $main::auth->assert('vendor_invoice_edit');
61 if (!$::instance_conf->get_allow_new_purchase_invoice) {
62 $::form->show_generic_error($::locale->text("You do not have the permissions to access this function."));
65 $form->{show_details} = $::myconfig{show_form_details};
67 $form->{title} = $locale->text('Record Vendor Invoice');
73 $main::lxdebug->leave_sub();
77 $main::lxdebug->enter_sub();
79 my $form = $main::form;
80 my $locale = $main::locale;
82 $main::auth->assert('vendor_invoice_edit');
84 $form->{show_details} = $::myconfig{show_form_details};
87 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
88 #/show hhistory button
90 $form->{title} = $locale->text('Edit Vendor Invoice');
96 $main::lxdebug->leave_sub();
100 $main::lxdebug->enter_sub();
102 my $form = $main::form;
103 my %myconfig = %main::myconfig;
105 $main::auth->assert('vendor_invoice_edit');
107 $form->{vc} = 'vendor';
110 $form->create_links("AP", \%myconfig, "vendor");
112 #quote all_vendor Bug 133
113 foreach my $ref (@{ $form->{all_vendor} }) {
114 $ref->{name} = $form->quote($ref->{name});
117 if ($form->{all_vendor}) {
118 unless ($form->{vendor_id}) {
119 $form->{vendor_id} = $form->{all_vendor}->[0]->{id};
123 $form->backup_vars(qw(payment_id language_id taxzone_id
124 currency delivery_term_id intnotes cp_id));
126 IR->get_vendor(\%myconfig, \%$form);
127 IR->retrieve_invoice(\%myconfig, \%$form);
129 $form->restore_vars(qw(payment_id language_id taxzone_id
130 currency delivery_term_id intnotes cp_id));
132 my @curr = $form->get_all_currencies();
133 map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
135 $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
137 # build vendor/customer drop down comatibility... don't ask
138 if (@{ $form->{"all_vendor"} || [] }) {
139 $form->{"selectvendor"} = 1;
140 $form->{vendor} = qq|$form->{vendor}--$form->{vendor_id}|;
144 if ($form->{all_departments}) {
145 $form->{selectdepartment} = "<option>\n";
146 $form->{department} = "$form->{department}--$form->{department_id}";
149 $form->{selectdepartment} .=
150 "<option>$_->{description}--$_->{id}\n"
151 } (@{ $form->{all_departments} || [] });
155 $form->{forex} = $form->{exchangerate};
156 my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
158 foreach my $key (keys %{ $form->{AP_links} }) {
160 foreach my $ref (@{ $form->{AP_links}{$key} }) {
161 $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}</option>";
164 next unless $form->{acc_trans}{$key};
166 if ($key eq "AP_paid") {
167 for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
168 $form->{"AP_paid_$i"} =
169 "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
171 $form->{"acc_trans_id_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{acc_trans_id};
173 $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount};
174 $form->{"datepaid_$i"} =
175 $form->{acc_trans}{$key}->[$i - 1]->{transdate};
176 $form->{"gldate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{gldate};
177 $form->{"forex_$i"} = $form->{"exchangerate_$i"} =
178 $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
179 $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
180 $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
182 $form->{paidaccounts} = $i;
186 "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
191 $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
193 $form->{AP} = $form->{AP_1} unless $form->{id};
196 ($form->datetonum($form->{invdate}, \%myconfig) <=
197 $form->datetonum($form->{closedto}, \%myconfig));
199 $main::lxdebug->leave_sub();
202 sub prepare_invoice {
203 $main::lxdebug->enter_sub();
205 my $form = $main::form;
206 my %myconfig = %main::myconfig;
208 $main::auth->assert('vendor_invoice_edit');
210 $form->{type} = "purchase_invoice";
214 map { $form->{$_} =~ s/\"/"/g } qw(invnumber ordnumber quonumber);
217 foreach my $ref (@{ $form->{invoice_details} }) {
219 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
220 # übernommen aus is.pl Fix für Bug 1642. Nebenwirkungen? jb 12.5.2011
221 # getestet: Lieferantenauftrag -> Rechnung i.O.
222 # Lieferantenauftrag -> Lieferschein -> Rechnung i.O.
223 # Werte: 20% (Lieferantenrabatt), 12,4% individuell und 0,4 individuell s.a.
224 # Screenshot zu Bug 1642
225 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
227 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
229 my $decimalplaces = ($dec > 2) ? $dec : 2;
231 $form->{"sellprice_$i"} =
232 $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
235 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
236 $dec_qty = length $dec_qty;
239 $form->format_amount(\%myconfig, ($form->{"qty_$i"} * -1), $dec_qty);
241 $form->{rowcount} = $i;
245 $main::lxdebug->leave_sub();
249 $main::lxdebug->enter_sub();
251 my $form = $main::form;
252 my %myconfig = %main::myconfig;
253 my $locale = $main::locale;
254 my $cgi = $::request->{cgi};
256 $main::auth->assert('vendor_invoice_edit');
261 $TMPL_VAR{invoice_obj} = SL::DB::PurchaseInvoice->new(id => $form->{id})->load if $form->{id};
262 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
263 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
265 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
267 my @old_project_ids = ($form->{"globalproject_id"});
268 map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
270 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
272 "old_id" => \@old_project_ids },
273 "taxzones" => "ALL_TAXZONES",
274 "currencies" => "ALL_CURRENCIES",
275 "vendors" => "ALL_VENDORS",
276 "departments" => "all_departments",
277 "price_factors" => "ALL_PRICE_FACTORS");
279 $TMPL_VAR{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ or => [ id => $::form->{employee_id}, deleted => 0 ] ]);
280 $TMPL_VAR{ALL_CONTACTS} = SL::DB::Manager::Contact->get_all_sorted(query => [
282 cp_cv_id => $::form->{"$::form->{vc}_id"} * 1,
285 cp_id => $::form->{cp_id} * 1
289 $TMPL_VAR{department_labels} = sub { "$_[0]->{description}--$_[0]->{id}" };
292 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
293 $TMPL_VAR{vclimit} = $myconfig{vclimit};
294 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('vendor', '', 1, 0)";
295 push @custom_hiddens, "vendor_id";
296 push @custom_hiddens, "oldvendor";
297 push @custom_hiddens, "selectvendor";
299 # currencies and exchangerate
300 my @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
301 my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
302 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
303 # show_exchangerate is also later needed in another template
304 $form->{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
305 $TMPL_VAR{currencies} = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
306 '-values' => \@values, '-labels' => \%labels,
307 '-onchange' => "document.getElementById('update_button').click();"
308 )) if scalar @values;
309 push @custom_hiddens, "forex";
310 push @custom_hiddens, "exchangerate" if $form->{forex};
312 $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
313 $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
315 my $follow_up_vc = $form->{vendor};
316 $follow_up_vc =~ s/--\d*\s*$//;
317 $TMPL_VAR{vendor_name} = $follow_up_vc;
319 # set option selected
320 foreach my $item (qw(AP)) {
321 $form->{"select$item"} =~ s/ selected//;
322 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
325 $TMPL_VAR{is_type_credit_note} = $form->{type} eq "credit_note";
326 $TMPL_VAR{is_format_html} = $form->{format} eq 'html';
327 $TMPL_VAR{dateformat} = $myconfig{dateformat};
328 $TMPL_VAR{numberformat} = $myconfig{numberformat};
331 $TMPL_VAR{HIDDENS} = [qw(
332 id action type media format queued printed emailed title vc discount
333 title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
334 max_dunning_level dunning_amount
335 shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptogln shiptocontact shiptophone shiptofax
336 shiptoemail shiptodepartment_1 shiptodepartment_2 message email subject cc bcc taxaccounts cursor_fokus
337 convert_from_do_ids convert_from_oe_ids convert_from_ap_ids show_details gldate useasnew
339 map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
341 $TMPL_VAR{payment_terms_obj} = get_payment_terms_for_invoice();
342 $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};
344 $::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.Draft kivi.SalesPurchase ckeditor/ckeditor ckeditor/adapters/jquery kivi.io autocomplete_customer autocomplete_part client_js));
348 print $form->parse_html_template("ir/form_header", \%TMPL_VAR);
350 $main::lxdebug->leave_sub();
354 my @fields = qw(acc_trans_id gldate datepaid source memo paid AP_paid);
356 grep { $_->{paid} != 0 }
359 +{ map { ($_ => delete($::form->{"${_}_${idx}"})) } @fields }
360 } (1..$::form->{paidaccounts});
362 @payments = sort_by { DateTime->from_kivitendo($_->{datepaid}) } @payments;
364 $::form->{paidaccounts} = max scalar(@payments), 1;
366 foreach my $idx (1 .. scalar(@payments)) {
367 my $payment = $payments[$idx - 1];
368 $::form->{"${_}_${idx}"} = $payment->{$_} for @fields;
373 $main::lxdebug->enter_sub();
375 my $form = $main::form;
376 my %myconfig = %main::myconfig;
377 my $locale = $main::locale;
379 $main::auth->assert('vendor_invoice_edit');
381 $form->{invtotal} = $form->{invsubtotal};
382 $form->{oldinvtotal} = $form->{invtotal};
385 $form->{rows} = max 2,
386 $form->numtextrows($form->{notes}, 26, 8),
387 $form->numtextrows($form->{intnotes}, 35, 8);
390 # tax, total and subtotal calculations
391 my ($tax, $subtotal);
392 $form->{taxaccounts_array} = [ split / /, $form->{taxaccounts} ];
394 foreach my $item (@{ $form->{taxaccounts_array} }) {
395 if ($form->{"${item}_base"}) {
396 if ($form->{taxincluded}) {
397 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"}
398 / (1 + $form->{"${item}_rate"})), 2);
399 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
401 $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
402 $form->{invtotal} += $form->{"${item}_total"};
409 $form->{follow_ups} = FU->follow_ups('trans_id' => $form->{id}, 'not_done' => 1) || [];
410 $form->{follow_ups_unfinished} = ( sum map { $_->{due} * 1 } @{ $form->{follow_ups} } ) || 0;
417 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
418 $form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];
420 # Standard Konto für Umlaufvermögen
421 my $accno_arap = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
423 for my $i (1 .. $form->{paidaccounts}) {
424 $form->{"changeable_$i"} = 1;
425 if (SL::DB::Default->get->payments_changeable == 0) {
427 $form->{"changeable_$i"} = ($form->{"acc_trans_id_$i"})? 0 : 1;
428 } elsif (SL::DB::Default->get->payments_changeable == 2) {
430 $form->{"changeable_$i"} = (($form->{"gldate_$i"} eq '') ||
431 ($form->current_date(\%myconfig) eq $form->{"gldate_$i"}));
434 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
435 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
437 #deaktivieren von Zahlungen ausserhalb der Bücherkontrolle
438 if ($form->date_closed($form->{"gldate_$i"})) {
439 $form->{"changeable_$i"} = 0;
442 $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
443 if (!$form->{"AP_paid_$i"}) {
444 $form->{"selectAP_paid_$i"} =~ s/option>$accno_arap--(.*?)>/option selected>$accno_arap--$1>/;
446 $form->{"selectAP_paid_$i"} =~ s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
449 $totalpaid += $form->{"paid_$i"};
452 $form->{ALL_DELIVERY_TERMS} = SL::DB::Manager::DeliveryTerm->get_all_sorted();
454 print $form->parse_html_template('ir/form_footer', {
455 is_type_credit_note => ($form->{type} eq "credit_note"),
456 totalpaid => $totalpaid,
457 paid_missing => $form->{invtotal} - $totalpaid,
458 show_storno => $form->{id} && !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ap") && !$totalpaid,
459 show_delete => ($::instance_conf->get_ir_changeable == 2)
460 ? ($form->current_date(\%myconfig) eq $form->{gldate})
461 : ($::instance_conf->get_ir_changeable == 1),
462 today => DateTime->today,
464 ##print $form->parse_html_template('ir/_payments'); # parser
465 ##print $form->parse_html_template('webdav/_list'); # parser
467 $main::lxdebug->leave_sub();
471 $main::lxdebug->enter_sub();
473 my $form = $main::form;
474 my %myconfig = %main::myconfig;
476 $main::auth->assert('vendor_invoice_edit');
478 &mark_as_paid_common(\%myconfig,"ap");
480 $main::lxdebug->leave_sub();
488 $main::lxdebug->enter_sub();
490 my $form = $main::form;
491 my %myconfig = %main::myconfig;
493 $main::auth->assert('vendor_invoice_edit');
495 &check_name('vendor');
497 if (!$form->{forex}) { # read exchangerate from input field (not hidden)
498 $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate});
500 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'sell');
501 $form->{exchangerate} = $form->{forex} if $form->{forex};
503 for my $i (1 .. $form->{paidaccounts}) {
504 next unless $form->{"paid_$i"};
505 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
506 $form->{"forex_$i"} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
507 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
510 my $i = $form->{rowcount};
511 my $exchangerate = ($form->{exchangerate} * 1) || 1;
513 if ( ($form->{"partnumber_$i"} eq "")
514 && ($form->{"description_$i"} eq "")
515 && ($form->{"partsgroup_$i"} eq "")) {
516 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
521 IR->retrieve_item(\%myconfig, \%$form);
523 my $rows = scalar @{ $form->{item_list} };
525 $form->{"discount_$i"} = $form->parse_amount(\%myconfig, $form->{"discount_$i"}) / 100.0;
526 $form->{"discount_$i"} ||= $form->{vendor_discount};
529 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
530 if( !$form->{"qty_$i"} ) {
531 $form->{"qty_$i"} = 1;
536 select_item(mode => 'IR', pre_entered_qty => $form->{"qty_$i"});
537 $::dispatcher->end_request;
541 # override sellprice if there is one entered
542 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
544 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
545 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
547 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
549 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
550 my $dec_qty = length $1;
551 my $decimalplaces = max 2, $dec_qty;
554 $form->{"sellprice_$i"} = $sellprice;
556 my $record = _make_record();
557 my $price_source = SL::PriceSource->new(record_item => $record->items->[$i-1], record => $record);
558 my $best_price = $price_source->best_price;
559 my $best_discount = $price_source->best_discount;
562 $::form->{"sellprice_$i"} = $best_price->price;
563 $::form->{"active_price_source_$i"} = $best_price->source;
565 if ($best_discount) {
566 $::form->{"discount_$i"} = $best_discount->discount;
567 $::form->{"active_discount_source_$i"} = $best_discount->source;
570 # if there is an exchange rate adjust sellprice
571 $form->{"sellprice_$i"} /= $exchangerate;
574 my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"});
575 $form->{creditremaining} -= $amount;
576 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
577 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
578 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100.0);
585 # ok, so this is a new part
586 # ask if it is a part or service item
588 if ( $form->{"partsgroup_$i"}
589 && ($form->{"partsnumber_$i"} eq "")
590 && ($form->{"description_$i"} eq "")) {
592 $form->{"discount_$i"} = "";
596 $form->{"id_$i"} = 0;
601 $main::lxdebug->leave_sub();
605 $main::lxdebug->enter_sub();
607 my $form = $main::form;
608 my %myconfig = %main::myconfig;
609 my $locale = $main::locale;
611 $main::auth->assert('vendor_invoice_edit');
613 if ($form->{storno}) {
614 $form->error($locale->text('Cannot storno storno invoice!'));
617 if (IS->has_storno(\%myconfig, $form, "ap")) {
618 $form->error($locale->text("Invoice has already been storno'd!"));
621 $form->error($locale->text('Cannot post storno for a closed period!'))
622 if ( $form->date_closed($form->{invdate}, \%myconfig));
624 my $employee_id = $form->{employee_id};
629 # Payments must not be recorded for the new storno invoice.
630 $form->{paidaccounts} = 0;
631 map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ gldate_ acc_trans_id_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
632 # set new ids for storno invoice
633 # set new persistent ids for storno invoice items
634 $form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};
637 if(!exists $form->{addition} && $form->{id} ne "") {
638 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
639 $form->{what_done} = "invoice";
640 $form->{addition} = "CANCELED";
643 # /saving the history
645 # record link invoice to storno
646 $form->{convert_from_ap_ids} = $form->{id};
647 $form->{storno_id} = $form->{id};
650 $form->{invnumber} = "Storno zu " . $form->{invnumber};
652 $form->{employee_id} = $employee_id;
654 $main::lxdebug->leave_sub();
659 $main::lxdebug->enter_sub();
661 my $form = $main::form;
662 my %myconfig = %main::myconfig;
664 $main::auth->assert('vendor_invoice_edit');
666 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);
667 $form->{paidaccounts} = 1;
669 $form->{invdate} = $form->current_date(\%myconfig);
671 $form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};
673 $form->{useasnew} = 1;
676 $main::lxdebug->leave_sub();
680 $main::lxdebug->enter_sub();
682 my $form = $main::form;
683 my %myconfig = %main::myconfig;
684 my $locale = $main::locale;
686 $main::auth->assert('vendor_invoice_edit');
688 $form->mtime_ischanged('ap') ;
689 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
690 for my $i (1 .. $form->{paidaccounts}) {
691 if ($form->{"paid_$i"}) {
692 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
694 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
696 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
697 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
699 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
700 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
701 $form->error($locale->text('Cannot post payment for a closed period!'))
702 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
704 if ($form->{currency} ne $form->{defaultcurrency}) {
705 # $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid); # invdate isn't set here
706 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
711 ($form->{AP}) = split /--/, $form->{AP};
712 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
713 if (IR->post_payment(\%myconfig, \%$form)){
714 if (!exists $form->{addition} && $form->{id} ne "") {
716 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
717 $form->{addition} = "PAYMENT POSTED";
718 $form->{what_done} = "invoice";
720 # /saving the history
723 $form->redirect($locale->text('Payment posted!'));
726 $form->error($locale->text('Cannot post payment!'));
728 $main::lxdebug->leave_sub();
732 my $form = $main::form;
734 my @dates = sort { $b->[1] cmp $a->[1] }
735 map { [ $_, $main::locale->reformat_date(\%main::myconfig, $_, 'yyyy-mm-dd') ] }
737 map { $form->{"datepaid_${_}"} }
738 (1..$form->{rowcount});
740 return @dates ? $dates[0]->[0] : undef;
745 $main::lxdebug->enter_sub();
747 my $form = $main::form;
748 my %myconfig = %main::myconfig;
749 my $locale = $main::locale;
751 $main::auth->assert('vendor_invoice_edit');
753 $form->mtime_ischanged('ap');
754 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
756 $form->isblank("invdate", $locale->text('Invoice Date missing!'));
757 $form->isblank("vendor", $locale->text('Vendor missing!'));
758 $form->isblank("invnumber", $locale->text('Invnumber missing!'));
760 $form->{invnumber} =~ s/^\s*//g;
761 $form->{invnumber} =~ s/\s*$//g;
763 # if the vendor changed get new values
764 if (&check_name('vendor')) {
766 $::dispatcher->end_request;
769 if ($myconfig{mandatory_departments} && !$form->{department_id}) {
770 $form->{saved_message} = $::locale->text('You have to specify a department.');
775 remove_emptied_rows();
778 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
779 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
780 my $max_datepaid = _max_datepaid();
782 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
783 if ($form->date_max_future($invdate, \%myconfig));
784 $form->error($locale->text('Cannot post invoice for a closed period!'))
785 if ($invdate <= $closedto);
787 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
788 if ($form->{currency} ne $form->{defaultcurrency});
791 for $i (1 .. $form->{paidaccounts}) {
792 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
793 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
795 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
797 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
798 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
800 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
801 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
802 $form->error($locale->text('Cannot post payment for a closed period!'))
803 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
805 if ($form->{currency} ne $form->{defaultcurrency}) {
806 $form->{"exchangerate_$i"} = $form->{exchangerate}
807 if ($invdate == $datepaid);
808 $form->isblank("exchangerate_$i",
809 $locale->text('Exchangerate for payment missing!'));
814 ($form->{AP}) = split /--/, $form->{AP};
815 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
816 $form->{storno} ||= 0;
818 $form->{id} = 0 if $form->{postasnew};
822 if (IR->post_invoice(\%myconfig, \%$form)){
824 if(!exists $form->{addition} && $form->{id} ne "") {
825 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
826 $form->{addition} = "POSTED";
827 $form->{what_done} = 'invoice';
830 # /saving the history
831 $form->redirect( $locale->text('Invoice')
832 . " $form->{invnumber} "
833 . $locale->text('posted!'));
835 $form->error($locale->text('Cannot post invoice!'));
837 $main::lxdebug->leave_sub();
841 $main::lxdebug->enter_sub();
843 my $form = $main::form;
844 my $locale = $main::locale;
846 $main::auth->assert('vendor_invoice_edit');
850 <form method=post action=$form->{script}>
853 # delete action variable
854 map { delete $form->{$_} } qw(action header);
856 foreach my $key (keys %$form) {
857 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
858 $form->{$key} =~ s/\"/"/g;
859 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
863 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
866 . $locale->text('Are you sure you want to delete Invoice Number')
867 . qq| $form->{invnumber}</h4>
869 <input name=action class=submit type=submit value="|
870 . $locale->text('Yes') . qq|">
874 $main::lxdebug->leave_sub();
878 $::lxdebug->enter_sub;
880 $::auth->assert('vendor_invoice_edit');
884 my $new_rowcount = $::form->{"rowcount"} * 1 + 1;
885 $::form->{"project_id_${new_rowcount}"} = $::form->{"globalproject_id"};
887 $::form->language_payment(\%::myconfig);
889 Common::webdav_folder($::form);
892 display_row(++$::form->{rowcount});
895 $::lxdebug->leave_sub;
899 $main::lxdebug->enter_sub();
901 my $form = $main::form;
902 my %myconfig = %main::myconfig;
903 my $locale = $main::locale;
905 $main::auth->assert('vendor_invoice_edit');
907 if (IR->delete_invoice(\%myconfig, \%$form)) {
909 if(!exists $form->{addition}) {
910 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
911 $form->{addition} = "DELETED";
914 # /saving the history
915 $form->redirect($locale->text('Invoice deleted!'));
917 $form->error($locale->text('Cannot delete invoice!'));
919 $main::lxdebug->leave_sub();
922 sub get_duedate_vendor {
923 $::lxdebug->enter_sub;
925 my $result = IR->get_duedate(
926 vendor_id => $::form->{vendor_id},
927 invdate => $::form->{invdate},
928 default => $::form->{old_duedate},
931 print $::form->ajax_response_header, $result;
932 $::lxdebug->leave_sub;