1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger, Accounting
9 # Copyright (c) 1998-2002
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
30 # Inventory received module
32 #======================================================================
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";
46 require "bin/mozilla/drafts.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 return $main::lxdebug->leave_sub() if (load_draft_maybe());
68 $form->{show_details} = $::myconfig{show_form_details};
70 $form->{title} = $locale->text('Record Vendor Invoice');
76 $main::lxdebug->leave_sub();
80 $main::lxdebug->enter_sub();
82 my $form = $main::form;
83 my $locale = $main::locale;
85 $main::auth->assert('vendor_invoice_edit');
87 $form->{show_details} = $::myconfig{show_form_details};
90 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
91 #/show hhistory button
93 $form->{title} = $locale->text('Edit Vendor Invoice');
99 $main::lxdebug->leave_sub();
103 $main::lxdebug->enter_sub();
105 my $form = $main::form;
106 my %myconfig = %main::myconfig;
108 $main::auth->assert('vendor_invoice_edit');
110 $form->{vc} = 'vendor';
113 $form->create_links("AP", \%myconfig, "vendor");
115 #quote all_vendor Bug 133
116 foreach my $ref (@{ $form->{all_vendor} }) {
117 $ref->{name} = $form->quote($ref->{name});
120 if ($form->{all_vendor}) {
121 unless ($form->{vendor_id}) {
122 $form->{vendor_id} = $form->{all_vendor}->[0]->{id};
126 $form->backup_vars(qw(payment_id language_id taxzone_id
127 currency delivery_term_id intnotes cp_id));
129 IR->get_vendor(\%myconfig, \%$form);
130 IR->retrieve_invoice(\%myconfig, \%$form);
132 $form->restore_vars(qw(payment_id language_id taxzone_id
133 currency delivery_term_id intnotes cp_id));
135 my @curr = $form->get_all_currencies();
136 map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
138 $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
140 # build vendor/customer drop down comatibility... don't ask
141 if (@{ $form->{"all_vendor"} || [] }) {
142 $form->{"selectvendor"} = 1;
143 $form->{vendor} = qq|$form->{vendor}--$form->{vendor_id}|;
147 if ($form->{all_departments}) {
148 $form->{selectdepartment} = "<option>\n";
149 $form->{department} = "$form->{department}--$form->{department_id}";
152 $form->{selectdepartment} .=
153 "<option>$_->{description}--$_->{id}\n"
154 } (@{ $form->{all_departments} || [] });
158 $form->{forex} = $form->{exchangerate};
159 my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
161 foreach my $key (keys %{ $form->{AP_links} }) {
163 foreach my $ref (@{ $form->{AP_links}{$key} }) {
164 $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}</option>";
167 next unless $form->{acc_trans}{$key};
169 if ($key eq "AP_paid") {
170 for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
171 $form->{"AP_paid_$i"} =
172 "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
174 $form->{"acc_trans_id_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{acc_trans_id};
176 $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount};
177 $form->{"datepaid_$i"} =
178 $form->{acc_trans}{$key}->[$i - 1]->{transdate};
179 $form->{"gldate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{gldate};
180 $form->{"forex_$i"} = $form->{"exchangerate_$i"} =
181 $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
182 $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
183 $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
185 $form->{paidaccounts} = $i;
189 "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
194 $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
196 $form->{AP} = $form->{AP_1} unless $form->{id};
199 ($form->datetonum($form->{invdate}, \%myconfig) <=
200 $form->datetonum($form->{closedto}, \%myconfig));
202 $main::lxdebug->leave_sub();
205 sub prepare_invoice {
206 $main::lxdebug->enter_sub();
208 my $form = $main::form;
209 my %myconfig = %main::myconfig;
211 $main::auth->assert('vendor_invoice_edit');
213 $form->{type} = "purchase_invoice";
217 map { $form->{$_} =~ s/\"/"/g } qw(invnumber ordnumber quonumber);
220 foreach my $ref (@{ $form->{invoice_details} }) {
222 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
223 # übernommen aus is.pl Fix für Bug 1642. Nebenwirkungen? jb 12.5.2011
224 # getestet: Lieferantenauftrag -> Rechnung i.O.
225 # Lieferantenauftrag -> Lieferschein -> Rechnung i.O.
226 # Werte: 20% (Lieferantenrabatt), 12,4% individuell und 0,4 individuell s.a.
227 # Screenshot zu Bug 1642
228 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
230 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
232 my $decimalplaces = ($dec > 2) ? $dec : 2;
234 $form->{"sellprice_$i"} =
235 $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
238 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
239 $dec_qty = length $dec_qty;
242 $form->format_amount(\%myconfig, ($form->{"qty_$i"} * -1), $dec_qty);
244 $form->{rowcount} = $i;
248 $main::lxdebug->leave_sub();
252 $main::lxdebug->enter_sub();
254 my $form = $main::form;
255 my %myconfig = %main::myconfig;
256 my $locale = $main::locale;
257 my $cgi = $::request->{cgi};
259 $main::auth->assert('vendor_invoice_edit');
264 $TMPL_VAR{invoice_obj} = SL::DB::PurchaseInvoice->new(id => $form->{id})->load if $form->{id};
265 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
266 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
268 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
270 my @old_project_ids = ($form->{"globalproject_id"});
271 map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
273 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
275 "old_id" => \@old_project_ids },
276 "taxzones" => "ALL_TAXZONES",
277 "currencies" => "ALL_CURRENCIES",
278 "vendors" => "ALL_VENDORS",
279 "departments" => "all_departments",
280 "price_factors" => "ALL_PRICE_FACTORS");
282 $TMPL_VAR{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ or => [ id => $::form->{employee_id}, deleted => 0 ] ]);
283 $TMPL_VAR{ALL_CONTACTS} = SL::DB::Manager::Contact->get_all_sorted(query => [
285 cp_cv_id => $::form->{"$::form->{vc}_id"} * 1,
288 cp_id => $::form->{cp_id} * 1
292 $TMPL_VAR{department_labels} = sub { "$_[0]->{description}--$_[0]->{id}" };
295 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
296 $TMPL_VAR{vclimit} = $myconfig{vclimit};
297 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('vendor', '', 1, 0)";
298 push @custom_hiddens, "vendor_id";
299 push @custom_hiddens, "oldvendor";
300 push @custom_hiddens, "selectvendor";
302 # currencies and exchangerate
303 my @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
304 my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
305 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
306 # show_exchangerate is also later needed in another template
307 $form->{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
308 $TMPL_VAR{currencies} = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
309 '-values' => \@values, '-labels' => \%labels,
310 '-onchange' => "document.getElementById('update_button').click();"
311 )) if scalar @values;
312 push @custom_hiddens, "forex";
313 push @custom_hiddens, "exchangerate" if $form->{forex};
315 $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
316 $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
318 my $follow_up_vc = $form->{vendor};
319 $follow_up_vc =~ s/--\d*\s*$//;
320 $TMPL_VAR{vendor_name} = $follow_up_vc;
322 # set option selected
323 foreach my $item (qw(AP)) {
324 $form->{"select$item"} =~ s/ selected//;
325 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
328 $TMPL_VAR{is_type_credit_note} = $form->{type} eq "credit_note";
329 $TMPL_VAR{is_format_html} = $form->{format} eq 'html';
330 $TMPL_VAR{dateformat} = $myconfig{dateformat};
331 $TMPL_VAR{numberformat} = $myconfig{numberformat};
334 $TMPL_VAR{HIDDENS} = [qw(
335 id action type media format queued printed emailed title vc discount
336 title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
337 max_dunning_level dunning_amount
338 shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptogln shiptocontact shiptophone shiptofax
339 shiptoemail shiptodepartment_1 shiptodepartment_2 message email subject cc bcc taxaccounts cursor_fokus
340 convert_from_do_ids convert_from_oe_ids show_details gldate useasnew
342 map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
344 $TMPL_VAR{payment_terms_obj} = get_payment_terms_for_invoice();
345 $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};
347 $::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.SalesPurchase ckeditor/ckeditor ckeditor/adapters/jquery kivi.io autocomplete_customer autocomplete_part client_js));
351 print $form->parse_html_template("ir/form_header", \%TMPL_VAR);
353 $main::lxdebug->leave_sub();
357 my @fields = qw(acc_trans_id gldate datepaid source memo paid AR_paid);
359 grep { $_->{paid} != 0 }
362 +{ map { ($_ => delete($::form->{"${_}_${idx}"})) } @fields }
363 } (1..$::form->{paidaccounts});
365 @payments = sort_by { DateTime->from_kivitendo($_->{datepaid}) } @payments;
367 $::form->{paidaccounts} = max scalar(@payments), 1;
369 foreach my $idx (1 .. scalar(@payments)) {
370 my $payment = $payments[$idx - 1];
371 $::form->{"${_}_${idx}"} = $payment->{$_} for @fields;
376 $main::lxdebug->enter_sub();
378 my $form = $main::form;
379 my %myconfig = %main::myconfig;
380 my $locale = $main::locale;
382 $main::auth->assert('vendor_invoice_edit');
384 $form->{invtotal} = $form->{invsubtotal};
385 $form->{oldinvtotal} = $form->{invtotal};
388 $form->{rows} = max 2,
389 $form->numtextrows($form->{notes}, 26, 8),
390 $form->numtextrows($form->{intnotes}, 35, 8);
393 # tax, total and subtotal calculations
394 my ($tax, $subtotal);
395 $form->{taxaccounts_array} = [ split / /, $form->{taxaccounts} ];
397 foreach my $item (@{ $form->{taxaccounts_array} }) {
398 if ($form->{"${item}_base"}) {
399 if ($form->{taxincluded}) {
400 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"}
401 / (1 + $form->{"${item}_rate"})), 2);
402 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
404 $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
405 $form->{invtotal} += $form->{"${item}_total"};
412 $form->{follow_ups} = FU->follow_ups('trans_id' => $form->{id}) || [];
413 $form->{follow_ups_unfinished} = ( sum map { $_->{due} * 1 } @{ $form->{follow_ups} } ) || 0;
420 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
421 $form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];
423 # Standard Konto für Umlaufvermögen
424 my $accno_arap = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
426 for my $i (1 .. $form->{paidaccounts}) {
427 $form->{"changeable_$i"} = 1;
428 if (SL::DB::Default->get->payments_changeable == 0) {
430 $form->{"changeable_$i"} = ($form->{"acc_trans_id_$i"})? 0 : 1;
431 } elsif (SL::DB::Default->get->payments_changeable == 2) {
433 $form->{"changeable_$i"} = (($form->{"gldate_$i"} eq '') ||
434 ($form->current_date(\%myconfig) eq $form->{"gldate_$i"}));
437 $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
438 if (!$form->{"AP_paid_$i"}) {
439 $form->{"selectAP_paid_$i"} =~ s/option>$accno_arap--(.*?)>/option selected>$accno_arap--$1>/;
441 $form->{"selectAP_paid_$i"} =~ s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
444 $totalpaid += $form->{"paid_$i"};
447 $form->{ALL_DELIVERY_TERMS} = SL::DB::Manager::DeliveryTerm->get_all_sorted();
449 print $form->parse_html_template('ir/form_footer', {
450 is_type_credit_note => ($form->{type} eq "credit_note"),
451 totalpaid => $totalpaid,
452 paid_missing => $form->{invtotal} - $totalpaid,
453 show_storno => $form->{id} && !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ap") && !$totalpaid,
454 show_delete => ($::instance_conf->get_ir_changeable == 2)
455 ? ($form->current_date(\%myconfig) eq $form->{gldate})
456 : ($::instance_conf->get_ir_changeable == 1),
457 today => DateTime->today,
459 ##print $form->parse_html_template('ir/_payments'); # parser
460 ##print $form->parse_html_template('webdav/_list'); # parser
462 $main::lxdebug->leave_sub();
466 $main::lxdebug->enter_sub();
468 my $form = $main::form;
469 my %myconfig = %main::myconfig;
471 $main::auth->assert('vendor_invoice_edit');
473 &mark_as_paid_common(\%myconfig,"ap");
475 $main::lxdebug->leave_sub();
479 $main::lxdebug->enter_sub();
481 my $form = $main::form;
482 my %myconfig = %main::myconfig;
484 $main::auth->assert('vendor_invoice_edit');
486 &check_name('vendor');
488 if (!$form->{forex}) { # read exchangerate from input field (not hidden)
489 $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate});
491 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'sell');
492 $form->{exchangerate} = $form->{forex} if $form->{forex};
494 for my $i (1 .. $form->{paidaccounts}) {
495 next unless $form->{"paid_$i"};
496 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
497 $form->{"forex_$i"} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
498 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
501 my $i = $form->{rowcount};
502 my $exchangerate = ($form->{exchangerate} * 1) || 1;
504 if ( ($form->{"partnumber_$i"} eq "")
505 && ($form->{"description_$i"} eq "")
506 && ($form->{"partsgroup_$i"} eq "")) {
507 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
512 IR->retrieve_item(\%myconfig, \%$form);
514 my $rows = scalar @{ $form->{item_list} };
516 $form->{"discount_$i"} = $form->parse_amount(\%myconfig, $form->{"discount_$i"}) / 100.0;
517 $form->{"discount_$i"} ||= $form->{vendor_discount};
520 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
521 if( !$form->{"qty_$i"} ) {
522 $form->{"qty_$i"} = 1;
527 select_item(mode => 'IR', pre_entered_qty => $form->{"qty_$i"});
532 # override sellprice if there is one entered
533 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
535 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
536 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
538 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
540 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
541 my $dec_qty = length $1;
542 my $decimalplaces = max 2, $dec_qty;
545 $form->{"sellprice_$i"} = $sellprice;
547 my $record = _make_record();
548 my $price_source = SL::PriceSource->new(record_item => $record->items->[$i-1], record => $record);
549 my $best_price = $price_source->best_price;
550 my $best_discount = $price_source->best_discount;
553 $::form->{"sellprice_$i"} = $best_price->price;
554 $::form->{"active_price_source_$i"} = $best_price->source;
556 if ($best_discount) {
557 $::form->{"discount_$i"} = $best_discount->discount;
558 $::form->{"active_discount_source_$i"} = $best_discount->source;
561 # if there is an exchange rate adjust sellprice
562 $form->{"sellprice_$i"} /= $exchangerate;
565 my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"});
566 $form->{creditremaining} -= $amount;
567 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
568 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
569 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100.0);
576 # ok, so this is a new part
577 # ask if it is a part or service item
579 if ( $form->{"partsgroup_$i"}
580 && ($form->{"partsnumber_$i"} eq "")
581 && ($form->{"description_$i"} eq "")) {
583 $form->{"discount_$i"} = "";
587 $form->{"id_$i"} = 0;
592 $main::lxdebug->leave_sub();
596 $main::lxdebug->enter_sub();
598 my $form = $main::form;
599 my %myconfig = %main::myconfig;
600 my $locale = $main::locale;
602 $main::auth->assert('vendor_invoice_edit');
604 if ($form->{storno}) {
605 $form->error($locale->text('Cannot storno storno invoice!'));
608 if (IS->has_storno(\%myconfig, $form, "ap")) {
609 $form->error($locale->text("Invoice has already been storno'd!"));
612 $form->error($locale->text('Cannot post storno for a closed period!'))
613 if ( $form->date_closed($form->{invdate}, \%myconfig));
615 my $employee_id = $form->{employee_id};
620 # Payments must not be recorded for the new storno invoice.
621 $form->{paidaccounts} = 0;
622 map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ gldate_ acc_trans_id_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
623 # set new ids for storno invoice
624 delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};
627 if(!exists $form->{addition} && $form->{id} ne "") {
628 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
629 $form->{what_done} = "invoice";
630 $form->{addition} = "CANCELED";
633 # /saving the history
635 $form->{storno_id} = $form->{id};
638 $form->{invnumber} = "Storno zu " . $form->{invnumber};
640 $form->{employee_id} = $employee_id;
642 $main::lxdebug->leave_sub();
647 $main::lxdebug->enter_sub();
649 my $form = $main::form;
650 my %myconfig = %main::myconfig;
652 $main::auth->assert('vendor_invoice_edit');
654 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);
655 $form->{paidaccounts} = 1;
657 $form->{invdate} = $form->current_date(\%myconfig);
659 $form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};
661 $form->{useasnew} = 1;
664 $main::lxdebug->leave_sub();
668 $main::lxdebug->enter_sub();
670 my $form = $main::form;
671 my %myconfig = %main::myconfig;
672 my $locale = $main::locale;
674 $main::auth->assert('vendor_invoice_edit');
676 $form->mtime_ischanged('ap') ;
677 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
678 for my $i (1 .. $form->{paidaccounts}) {
679 if ($form->{"paid_$i"}) {
680 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
682 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
684 $form->error($locale->text('Cannot post payment for a closed period!'))
685 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
687 if ($form->{currency} ne $form->{defaultcurrency}) {
688 # $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid); # invdate isn't set here
689 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
694 ($form->{AP}) = split /--/, $form->{AP};
695 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
696 if (IR->post_payment(\%myconfig, \%$form)){
697 if (!exists $form->{addition} && $form->{id} ne "") {
699 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
700 $form->{addition} = "PAYMENT POSTED";
701 $form->{what_done} = "invoice";
703 # /saving the history
706 $form->redirect($locale->text('Payment posted!'));
709 $form->error($locale->text('Cannot post payment!'));
711 $main::lxdebug->leave_sub();
715 my $form = $main::form;
717 my @dates = sort { $b->[1] cmp $a->[1] }
718 map { [ $_, $main::locale->reformat_date(\%main::myconfig, $_, 'yyyy-mm-dd') ] }
720 map { $form->{"datepaid_${_}"} }
721 (1..$form->{rowcount});
723 return @dates ? $dates[0]->[0] : undef;
728 $main::lxdebug->enter_sub();
730 my $form = $main::form;
731 my %myconfig = %main::myconfig;
732 my $locale = $main::locale;
734 $main::auth->assert('vendor_invoice_edit');
736 $form->mtime_ischanged('ap');
737 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
739 $form->isblank("invdate", $locale->text('Invoice Date missing!'));
740 $form->isblank("vendor", $locale->text('Vendor missing!'));
741 $form->isblank("invnumber", $locale->text('Invnumber missing!'));
743 $form->{invnumber} =~ s/^\s*//g;
744 $form->{invnumber} =~ s/\s*$//g;
746 # if the vendor changed get new values
747 if (&check_name('vendor')) {
752 if ($myconfig{mandatory_departments} && !$form->{department_id}) {
753 $form->{saved_message} = $::locale->text('You have to specify a department.');
758 remove_emptied_rows();
761 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
762 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
763 my $max_datepaid = _max_datepaid();
765 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
766 if ($form->date_max_future($invdate, \%myconfig));
767 $form->error($locale->text('Cannot post invoice for a closed period!'))
768 if ($invdate <= $closedto);
770 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
771 if ($form->{currency} ne $form->{defaultcurrency});
774 for $i (1 .. $form->{paidaccounts}) {
775 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
776 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
778 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
780 $form->error($locale->text('Cannot post payment for a closed period!'))
781 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
783 if ($form->{currency} ne $form->{defaultcurrency}) {
784 $form->{"exchangerate_$i"} = $form->{exchangerate}
785 if ($invdate == $datepaid);
786 $form->isblank("exchangerate_$i",
787 $locale->text('Exchangerate for payment missing!'));
792 ($form->{AP}) = split /--/, $form->{AP};
793 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
794 $form->{storno} ||= 0;
796 $form->{id} = 0 if $form->{postasnew};
800 if (IR->post_invoice(\%myconfig, \%$form)){
802 if(!exists $form->{addition} && $form->{id} ne "") {
803 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
804 $form->{addition} = "POSTED";
805 $form->{what_done} = 'invoice';
808 # /saving the history
809 remove_draft() if $form->{remove_draft};
810 $form->redirect( $locale->text('Invoice')
811 . " $form->{invnumber} "
812 . $locale->text('posted!'));
814 $form->error($locale->text('Cannot post invoice!'));
816 $main::lxdebug->leave_sub();
820 $main::lxdebug->enter_sub();
822 my $form = $main::form;
823 my $locale = $main::locale;
825 $main::auth->assert('vendor_invoice_edit');
829 <form method=post action=$form->{script}>
832 # delete action variable
833 map { delete $form->{$_} } qw(action header);
835 foreach my $key (keys %$form) {
836 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
837 $form->{$key} =~ s/\"/"/g;
838 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
842 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
845 . $locale->text('Are you sure you want to delete Invoice Number')
846 . qq| $form->{invnumber}</h4>
848 <input name=action class=submit type=submit value="|
849 . $locale->text('Yes') . qq|">
853 $main::lxdebug->leave_sub();
857 $::lxdebug->enter_sub;
859 $::auth->assert('vendor_invoice_edit');
863 my $new_rowcount = $::form->{"rowcount"} * 1 + 1;
864 $::form->{"project_id_${new_rowcount}"} = $::form->{"globalproject_id"};
866 $::form->language_payment(\%::myconfig);
868 Common::webdav_folder($::form);
871 display_row(++$::form->{rowcount});
874 $::lxdebug->leave_sub;
878 $main::lxdebug->enter_sub();
880 my $form = $main::form;
881 my %myconfig = %main::myconfig;
882 my $locale = $main::locale;
884 $main::auth->assert('vendor_invoice_edit');
886 if (IR->delete_invoice(\%myconfig, \%$form)) {
888 if(!exists $form->{addition}) {
889 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
890 $form->{addition} = "DELETED";
893 # /saving the history
894 $form->redirect($locale->text('Invoice deleted!'));
896 $form->error($locale->text('Cannot delete invoice!'));
898 $main::lxdebug->leave_sub();
901 sub get_duedate_vendor {
902 $::lxdebug->enter_sub;
904 my $result = IR->get_duedate(
905 vendor_id => $::form->{vendor_id},
906 invdate => $::form->{invdate},
907 default => $::form->{old_duedate},
910 print $::form->ajax_response_header, $result;
911 $::lxdebug->leave_sub;