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();
 
 220   $main::lxdebug->enter_sub();
 
 222   my $form     = $main::form;
 
 223   my %myconfig = %main::myconfig;
 
 224   my $locale   = $main::locale;
 
 225   my $cgi      = $::request->{cgi};
 
 227   $main::auth->assert('vendor_invoice_edit');
 
 232   $TMPL_VAR{invoice_obj} = SL::DB::PurchaseInvoice->load_cached($form->{id}) if $form->{id};
 
 233   $TMPL_VAR{vendor_obj}  = SL::DB::Vendor->load_cached($form->{vendor_id})   if $form->{vendor_id};
 
 234   $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
 
 235   $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
 
 237   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 239   my @old_project_ids = ($form->{"globalproject_id"});
 
 240   map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
 
 242   $form->get_lists("taxzones"      => ($form->{id} ? "ALL_TAXZONES" : "ALL_ACTIVE_TAXZONES"),
 
 243                    "currencies"    => "ALL_CURRENCIES",
 
 244                    "price_factors" => "ALL_PRICE_FACTORS");
 
 246   $TMPL_VAR{ALL_DEPARTMENTS}       = SL::DB::Manager::Department->get_all_sorted;
 
 247   $TMPL_VAR{ALL_EMPLOYEES}         = SL::DB::Manager::Employee->get_all_sorted(query => [ or => [ id => $::form->{employee_id},  deleted => 0 ] ]);
 
 248   $TMPL_VAR{ALL_CONTACTS}          = SL::DB::Manager::Contact->get_all_sorted(query => [
 
 250       cp_cv_id => $::form->{"$::form->{vc}_id"} * 1,
 
 253         cp_id    => $::form->{cp_id} * 1
 
 258   # currencies and exchangerate
 
 259   my @values = map { $_       } @{ $form->{ALL_CURRENCIES} };
 
 260   my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
 
 261   $form->{currency}            = $form->{defaultcurrency} unless $form->{currency};
 
 262   # show_exchangerate is also later needed in another template
 
 263   $form->{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
 
 264   $TMPL_VAR{currencies}        = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
 
 265                                                       '-values' => \@values, '-labels' => \%labels,
 
 266                                                       '-onchange' => "document.getElementById('update_button').click();"
 
 267                                      )) if scalar @values;
 
 268   push @custom_hiddens, "forex";
 
 269   push @custom_hiddens, "exchangerate" if $form->{forex};
 
 271   $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
 
 272   $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
 
 274 # set option selected
 
 275   foreach my $item (qw(AP)) {
 
 276     $form->{"select$item"} =~ s/ selected//;
 
 277     $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
 
 280   $TMPL_VAR{is_type_credit_note} = $form->{type}   eq "credit_note";
 
 281   $TMPL_VAR{is_format_html}      = $form->{format} eq 'html';
 
 282   $TMPL_VAR{dateformat}          = $myconfig{dateformat};
 
 283   $TMPL_VAR{numberformat}        = $myconfig{numberformat};
 
 286   $TMPL_VAR{HIDDENS} = [qw(
 
 287     id action type media format queued printed emailed title vc discount
 
 288     title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
 
 289     max_dunning_level dunning_amount
 
 290     shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptogln shiptocontact shiptophone shiptofax
 
 291     shiptoemail shiptodepartment_1 shiptodepartment_2 message email subject cc bcc taxaccounts cursor_fokus
 
 292     convert_from_do_ids convert_from_oe_ids convert_from_ap_ids show_details gldate useasnew
 
 294   map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
 
 296   $TMPL_VAR{payment_terms_obj} = get_payment_terms_for_invoice();
 
 297   $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};
 
 299   $::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.Draft kivi.SalesPurchase ckeditor/ckeditor ckeditor/adapters/jquery kivi.io autocomplete_customer autocomplete_part autocomplete_project client_js));
 
 303   print $form->parse_html_template("ir/form_header", \%TMPL_VAR);
 
 305   $main::lxdebug->leave_sub();
 
 309   my @fields   = qw(acc_trans_id gldate datepaid source memo paid AP_paid);
 
 311     grep { $_->{paid} != 0 }
 
 314       +{ map { ($_ => delete($::form->{"${_}_${idx}"})) } @fields }
 
 315     } (1..$::form->{paidaccounts});
 
 317   @payments = sort_by { DateTime->from_kivitendo($_->{datepaid}) } @payments;
 
 319   $::form->{paidaccounts} = max scalar(@payments), 1;
 
 321   foreach my $idx (1 .. scalar(@payments)) {
 
 322     my $payment = $payments[$idx - 1];
 
 323     $::form->{"${_}_${idx}"} = $payment->{$_} for @fields;
 
 328   $main::lxdebug->enter_sub();
 
 330   my $form     = $main::form;
 
 331   my %myconfig = %main::myconfig;
 
 332   my $locale   = $main::locale;
 
 334   $main::auth->assert('vendor_invoice_edit');
 
 336   $form->{invtotal}    = $form->{invsubtotal};
 
 337   $form->{oldinvtotal} = $form->{invtotal};
 
 340   $form->{rows} = max 2,
 
 341     $form->numtextrows($form->{notes},    26, 8),
 
 342     $form->numtextrows($form->{intnotes}, 35, 8);
 
 345   # tax, total and subtotal calculations
 
 346   my ($tax, $subtotal);
 
 347   $form->{taxaccounts_array} = [ split / /, $form->{taxaccounts} ];
 
 349   foreach my $item (@{ $form->{taxaccounts_array} }) {
 
 350     if ($form->{"${item}_base"}) {
 
 351       if ($form->{taxincluded}) {
 
 352         $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"}
 
 353                                                                                  / (1 + $form->{"${item}_rate"})), 2);
 
 354         $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
 
 356         $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
 
 357         $form->{invtotal} += $form->{"${item}_total"};
 
 364     $form->{follow_ups}            = FU->follow_ups('trans_id' => $form->{id}, 'not_done' => 1) || [];
 
 365     $form->{follow_ups_unfinished} = ( sum map { $_->{due} * 1 } @{ $form->{follow_ups} } ) || 0;
 
 372   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
 
 373   $form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];
 
 375   # Standard Konto für Umlaufvermögen
 
 376   my $accno_arap = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
 
 378   for my $i (1 .. $form->{paidaccounts}) {
 
 379     $form->{"changeable_$i"} = 1;
 
 380     if (SL::DB::Default->get->payments_changeable == 0) {
 
 382       $form->{"changeable_$i"} = ($form->{"acc_trans_id_$i"})? 0 : 1;
 
 383     } elsif (SL::DB::Default->get->payments_changeable == 2) {
 
 385       $form->{"changeable_$i"} = (($form->{"gldate_$i"} eq '') ||
 
 386                                   ($form->current_date(\%myconfig) eq $form->{"gldate_$i"}));
 
 389     $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
 
 390       if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
 
 392     #deaktivieren von Zahlungen ausserhalb der Bücherkontrolle
 
 393     if ($form->date_closed($form->{"gldate_$i"})) {
 
 394       $form->{"changeable_$i"} = 0;
 
 397     $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
 
 398     if (!$form->{"AP_paid_$i"}) {
 
 399       $form->{"selectAP_paid_$i"} =~ s/option>$accno_arap--(.*?)>/option selected>$accno_arap--$1>/;
 
 401       $form->{"selectAP_paid_$i"} =~ s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
 
 404     $totalpaid += $form->{"paid_$i"};
 
 407   $form->{ALL_DELIVERY_TERMS} = SL::DB::Manager::DeliveryTerm->get_all_sorted();
 
 409   print $form->parse_html_template('ir/form_footer', {
 
 410     is_type_credit_note => ($form->{type} eq "credit_note"),
 
 411     totalpaid           => $totalpaid,
 
 412     paid_missing        => $form->{invtotal} - $totalpaid,
 
 413     show_storno         => $form->{id} && !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ap") && !$totalpaid,
 
 414     show_delete         => ($::instance_conf->get_ir_changeable == 2)
 
 415                              ? ($form->current_date(\%myconfig) eq $form->{gldate})
 
 416                              : ($::instance_conf->get_ir_changeable == 1),
 
 417     today               => DateTime->today,
 
 419 ##print $form->parse_html_template('ir/_payments'); # parser
 
 420 ##print $form->parse_html_template('webdav/_list'); # parser
 
 422   $main::lxdebug->leave_sub();
 
 426   $::auth->assert('vendor_invoice_edit');
 
 428   SL::DB::PurchaseInvoice->new(id => $::form->{id})->load->mark_as_paid;
 
 430   $::form->redirect($::locale->text("Marked as paid"));
 
 438   $main::lxdebug->enter_sub();
 
 440   my $form     = $main::form;
 
 441   my %myconfig = %main::myconfig;
 
 443   $main::auth->assert('vendor_invoice_edit');
 
 445   if (($form->{previous_vendor_id} || $form->{vendor_id}) != $form->{vendor_id}) {
 
 446     IR->get_vendor(\%myconfig, $form);
 
 449   if (!$form->{forex}) {        # read exchangerate from input field (not hidden)
 
 450     $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate});
 
 452   $form->{forex}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'sell');
 
 453   $form->{exchangerate} = $form->{forex} if $form->{forex};
 
 455   for my $i (1 .. $form->{paidaccounts}) {
 
 456     next unless $form->{"paid_$i"};
 
 457     map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
 
 458     $form->{"forex_$i"}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
 
 459     $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
 
 462   my $i            = $form->{rowcount};
 
 463   my $exchangerate = ($form->{exchangerate} * 1) || 1;
 
 465   if (   ($form->{"partnumber_$i"} eq "")
 
 466       && ($form->{"description_$i"} eq "")
 
 467       && ($form->{"partsgroup_$i"} eq "")) {
 
 468     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
 
 473     IR->retrieve_item(\%myconfig, \%$form);
 
 475     my $rows = scalar @{ $form->{item_list} };
 
 477     $form->{"discount_$i"}   = $form->parse_amount(\%myconfig, $form->{"discount_$i"}) / 100.0;
 
 478     $form->{"discount_$i"} ||= $form->{vendor_discount};
 
 481       $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
 
 482       if( !$form->{"qty_$i"} ) {
 
 483         $form->{"qty_$i"} = 1;
 
 488         select_item(mode => 'IR', pre_entered_qty => $form->{"qty_$i"});
 
 489         $::dispatcher->end_request;
 
 493         # override sellprice if there is one entered
 
 494         my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
 
 496         map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
 
 497         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
 
 499         $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
 
 501         ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
 
 502         my $dec_qty       = length $1;
 
 503         my $decimalplaces = max 2, $dec_qty;
 
 506           $form->{"sellprice_$i"} = $sellprice;
 
 508           my $record        = _make_record();
 
 509           my $price_source  = SL::PriceSource->new(record_item => $record->items->[$i-1], record => $record);
 
 510           my $best_price    = $price_source->best_price;
 
 511           my $best_discount = $price_source->best_discount;
 
 514             $::form->{"sellprice_$i"}           = $best_price->price;
 
 515             $::form->{"active_price_source_$i"} = $best_price->source;
 
 517           if ($best_discount) {
 
 518             $::form->{"discount_$i"}               = $best_discount->discount;
 
 519             $::form->{"active_discount_source_$i"} = $best_discount->source;
 
 522           # if there is an exchange rate adjust sellprice
 
 523           $form->{"sellprice_$i"} /= $exchangerate;
 
 526         my $amount                = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"});
 
 527         $form->{creditremaining} -= $amount;
 
 528         $form->{"sellprice_$i"}   = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
 
 529         $form->{"qty_$i"}         = $form->format_amount(\%myconfig, $form->{"qty_$i"},       $dec_qty);
 
 530         $form->{"discount_$i"}    = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100.0);
 
 537       # ok, so this is a new part
 
 538       # ask if it is a part or service item
 
 540       if (   $form->{"partsgroup_$i"}
 
 541           && ($form->{"partsnumber_$i"} eq "")
 
 542           && ($form->{"description_$i"} eq "")) {
 
 544         $form->{"discount_$i"} = "";
 
 548         $form->{"id_$i"}   = 0;
 
 553   $main::lxdebug->leave_sub();
 
 557   $main::lxdebug->enter_sub();
 
 559   my $form     = $main::form;
 
 560   my %myconfig = %main::myconfig;
 
 561   my $locale   = $main::locale;
 
 563   $main::auth->assert('vendor_invoice_edit');
 
 565   if ($form->{storno}) {
 
 566     $form->error($locale->text('Cannot storno storno invoice!'));
 
 569   if (IS->has_storno(\%myconfig, $form, "ap")) {
 
 570     $form->error($locale->text("Invoice has already been storno'd!"));
 
 573   $form->error($locale->text('Cannot post storno for a closed period!'))
 
 574     if ( $form->date_closed($form->{invdate}, \%myconfig));
 
 576   my $employee_id = $form->{employee_id};
 
 581   # Payments must not be recorded for the new storno invoice.
 
 582   $form->{paidaccounts} = 0;
 
 583   map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ gldate_ acc_trans_id_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
 
 584   # set new ids for storno invoice
 
 585   # set new persistent ids for storno invoice items
 
 586   $form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};
 
 589   if(!exists $form->{addition} && $form->{id} ne "") {
 
 590     $form->{snumbers}  = qq|invnumber_| . $form->{invnumber};
 
 591     $form->{what_done} = "invoice";
 
 592     $form->{addition}  = "CANCELED";
 
 595   # /saving the history
 
 597   # record link invoice to storno
 
 598   $form->{convert_from_ap_ids} = $form->{id};
 
 599   $form->{storno_id} = $form->{id};
 
 602   $form->{invnumber} = "Storno zu " . $form->{invnumber};
 
 604   $form->{employee_id} = $employee_id;
 
 606   $main::lxdebug->leave_sub();
 
 611   $main::lxdebug->enter_sub();
 
 613   my $form     = $main::form;
 
 614   my %myconfig = %main::myconfig;
 
 616   $main::auth->assert('vendor_invoice_edit');
 
 618   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);
 
 619   $form->{paidaccounts} = 1;
 
 621   $form->{invdate} = $form->current_date(\%myconfig);
 
 623   $form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};
 
 625   $form->{useasnew} = 1;
 
 628   $main::lxdebug->leave_sub();
 
 632   $main::lxdebug->enter_sub();
 
 634   my $form     = $main::form;
 
 635   my %myconfig = %main::myconfig;
 
 636   my $locale   = $main::locale;
 
 638   $main::auth->assert('vendor_invoice_edit');
 
 640   $form->mtime_ischanged('ap') ;
 
 641   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 642   for my $i (1 .. $form->{paidaccounts}) {
 
 643     if ($form->{"paid_$i"}) {
 
 644       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
 
 646       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
 648       $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
 
 649         if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
 
 651       #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
 
 652       # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
 
 653       $form->error($locale->text('Cannot post payment for a closed period!'))
 
 654         if ($form->date_closed($form->{"datepaid_$i"})  && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
 
 656       if ($form->{currency} ne $form->{defaultcurrency}) {
 
 657 #        $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid); # invdate isn't set here
 
 658         $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
 
 663   ($form->{AP})      = split /--/, $form->{AP};
 
 664   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
 
 665   if (IR->post_payment(\%myconfig, \%$form)){
 
 666     if (!exists $form->{addition} && $form->{id} ne "") {
 
 668       $form->{snumbers}  = qq|invnumber_| . $form->{invnumber};
 
 669       $form->{addition}  = "PAYMENT POSTED";
 
 670       $form->{what_done} = "invoice";
 
 672       # /saving the history
 
 675     $form->redirect($locale->text('Payment posted!'));
 
 678   $form->error($locale->text('Cannot post payment!'));
 
 680   $main::lxdebug->leave_sub();
 
 684   my $form  =  $main::form;
 
 686   my @dates = sort { $b->[1] cmp $a->[1] }
 
 687               map  { [ $_, $main::locale->reformat_date(\%main::myconfig, $_, 'yyyy-mm-dd') ] }
 
 689               map  { $form->{"datepaid_${_}"} }
 
 690               (1..$form->{rowcount});
 
 692   return @dates ? $dates[0]->[0] : undef;
 
 697   $main::lxdebug->enter_sub();
 
 699   my $form     = $main::form;
 
 700   my %myconfig = %main::myconfig;
 
 701   my $locale   = $main::locale;
 
 703   $main::auth->assert('vendor_invoice_edit');
 
 705   $form->mtime_ischanged('ap');
 
 706   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 708   $form->isblank("invdate",   $locale->text('Invoice Date missing!'));
 
 709   $form->isblank("vendor_id", $locale->text('Vendor missing!'));
 
 710   $form->isblank("invnumber", $locale->text('Invnumber missing!'));
 
 712   $form->{invnumber} =~ s/^\s*//g;
 
 713   $form->{invnumber} =~ s/\s*$//g;
 
 715   # if the vendor changed get new values
 
 716   if (($form->{previous_vendor_id} || $form->{vendor_id}) != $form->{vendor_id}) {
 
 718     $::dispatcher->end_request;
 
 721   if ($myconfig{mandatory_departments} && !$form->{department_id}) {
 
 722     $form->{saved_message} = $::locale->text('You have to specify a department.');
 
 727   remove_emptied_rows();
 
 730   my $closedto     = $form->datetonum($form->{closedto}, \%myconfig);
 
 731   my $invdate      = $form->datetonum($form->{invdate},  \%myconfig);
 
 732   my $max_datepaid = _max_datepaid();
 
 734   $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
 
 735     if ($form->date_max_future($invdate, \%myconfig));
 
 736   $form->error($locale->text('Cannot post invoice for a closed period!'))
 
 737     if ($invdate <= $closedto);
 
 739   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
 
 740     if ($form->{currency} ne $form->{defaultcurrency});
 
 743   for $i (1 .. $form->{paidaccounts}) {
 
 744     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
 
 745       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
 
 747       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
 749       $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
 
 750         if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
 
 752       #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
 
 753       # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
 
 754       $form->error($locale->text('Cannot post payment for a closed period!'))
 
 755         if ($form->date_closed($form->{"datepaid_$i"})  && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
 
 757       if ($form->{currency} ne $form->{defaultcurrency}) {
 
 758         $form->{"exchangerate_$i"} = $form->{exchangerate}
 
 759           if ($invdate == $datepaid);
 
 760         $form->isblank("exchangerate_$i",
 
 761                        $locale->text('Exchangerate for payment missing!'));
 
 766   ($form->{AP})      = split /--/, $form->{AP};
 
 767   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
 
 768   $form->{storno}  ||= 0;
 
 770   $form->{id} = 0 if $form->{postasnew};
 
 774   if (IR->post_invoice(\%myconfig, \%$form)){
 
 776     if(!exists $form->{addition} && $form->{id} ne "") {
 
 777       $form->{snumbers}  = qq|invnumber_| . $form->{invnumber};
 
 778       $form->{addition}  = "POSTED";
 
 779       $form->{what_done} = 'invoice';
 
 782     # /saving the history
 
 783     $form->redirect(  $locale->text('Invoice')
 
 784                   . " $form->{invnumber} "
 
 785                   . $locale->text('posted!'));
 
 787   $form->error($locale->text('Cannot post invoice!'));
 
 789   $main::lxdebug->leave_sub();
 
 793   $main::lxdebug->enter_sub();
 
 795   my $form     = $main::form;
 
 796   my $locale   = $main::locale;
 
 798   $main::auth->assert('vendor_invoice_edit');
 
 802 <form method=post action=$form->{script}>
 
 805   # delete action variable
 
 806   map { delete $form->{$_} } qw(action header);
 
 808   foreach my $key (keys %$form) {
 
 809     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
 
 810     $form->{$key} =~ s/\"/"/g;
 
 811     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
 
 815 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
 
 818     . $locale->text('Are you sure you want to delete Invoice Number')
 
 819     . qq| $form->{invnumber}</h4>
 
 821 <input name=action class=submit type=submit value="|
 
 822     . $locale->text('Yes') . qq|">
 
 826   $main::lxdebug->leave_sub();
 
 830   $::lxdebug->enter_sub;
 
 832   $::auth->assert('vendor_invoice_edit');
 
 836   my $new_rowcount = $::form->{"rowcount"} * 1 + 1;
 
 837   $::form->{"project_id_${new_rowcount}"} = $::form->{"globalproject_id"};
 
 839   $::form->language_payment(\%::myconfig);
 
 841   Common::webdav_folder($::form);
 
 844   display_row(++$::form->{rowcount});
 
 847   $::lxdebug->leave_sub;
 
 851   $main::lxdebug->enter_sub();
 
 853   my $form     = $main::form;
 
 854   my %myconfig = %main::myconfig;
 
 855   my $locale   = $main::locale;
 
 857   $main::auth->assert('vendor_invoice_edit');
 
 859   if (IR->delete_invoice(\%myconfig, \%$form)) {
 
 861     if(!exists $form->{addition}) {
 
 862       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
 863       $form->{addition} = "DELETED";
 
 866     # /saving the history
 
 867     $form->redirect($locale->text('Invoice deleted!'));
 
 869   $form->error($locale->text('Cannot delete invoice!'));
 
 871   $main::lxdebug->leave_sub();
 
 874 sub get_duedate_vendor {
 
 875   $::lxdebug->enter_sub;
 
 877   my $result = IR->get_duedate(
 
 878     vendor_id => $::form->{vendor_id},
 
 879     invdate   => $::form->{invdate},
 
 880     default   => $::form->{old_duedate},
 
 883   print $::form->ajax_response_header, $result;
 
 884   $::lxdebug->leave_sub;