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 List::Util qw(max sum);
 
  40 use List::UtilsBy qw(sort_by);
 
  42 require "bin/mozilla/io.pl";
 
  43 require "bin/mozilla/invoice_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->{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');
 
  86   $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
 
  87   #/show hhistory button
 
  89   $form->{title} = $locale->text('Edit Vendor Invoice');
 
  95   $main::lxdebug->leave_sub();
 
  99   $main::lxdebug->enter_sub();
 
 101   my $form     = $main::form;
 
 102   my %myconfig = %main::myconfig;
 
 104   $main::auth->assert('vendor_invoice_edit');
 
 106   $form->{vc} = 'vendor';
 
 109   $form->{webdav}   = $::instance_conf->get_webdav;
 
 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   $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  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 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.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 AR_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}) || [];
 
 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->{"selectAP_paid_$i"} = $form->{selectAP_paid};
 
 435     if (!$form->{"AP_paid_$i"}) {
 
 436       $form->{"selectAP_paid_$i"} =~ s/option>$accno_arap--(.*?)>/option selected>$accno_arap--$1>/;
 
 438       $form->{"selectAP_paid_$i"} =~ s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
 
 441     $totalpaid += $form->{"paid_$i"};
 
 444   $form->{ALL_DELIVERY_TERMS} = SL::DB::Manager::DeliveryTerm->get_all_sorted();
 
 446   print $form->parse_html_template('ir/form_footer', {
 
 447     is_type_credit_note => ($form->{type} eq "credit_note"),
 
 448     totalpaid           => $totalpaid,
 
 449     paid_missing        => $form->{invtotal} - $totalpaid,
 
 450     show_storno         => $form->{id} && !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ap") && !$totalpaid,
 
 451     show_delete         => ($::instance_conf->get_ir_changeable == 2)
 
 452                              ? ($form->current_date(\%myconfig) eq $form->{gldate})
 
 453                              : ($::instance_conf->get_ir_changeable == 1),
 
 454     today               => DateTime->today,
 
 456 ##print $form->parse_html_template('ir/_payments'); # parser
 
 457 ##print $form->parse_html_template('webdav/_list'); # parser
 
 459   $main::lxdebug->leave_sub();
 
 463   $main::lxdebug->enter_sub();
 
 465   my $form     = $main::form;
 
 466   my %myconfig = %main::myconfig;
 
 468   $main::auth->assert('vendor_invoice_edit');
 
 470   &mark_as_paid_common(\%myconfig,"ap");
 
 472   $main::lxdebug->leave_sub();
 
 476   $main::lxdebug->enter_sub();
 
 478   my $form     = $main::form;
 
 479   my %myconfig = %main::myconfig;
 
 481   $main::auth->assert('vendor_invoice_edit');
 
 483   &check_name('vendor');
 
 485   if (!$form->{forex}) {        # read exchangerate from input field (not hidden)
 
 486     $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate});
 
 488   $form->{forex}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'sell');
 
 489   $form->{exchangerate} = $form->{forex} if $form->{forex};
 
 491   for my $i (1 .. $form->{paidaccounts}) {
 
 492     next unless $form->{"paid_$i"};
 
 493     map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
 
 494     $form->{"forex_$i"}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
 
 495     $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
 
 498   my $i            = $form->{rowcount};
 
 499   my $exchangerate = ($form->{exchangerate} * 1) || 1;
 
 501   if (   ($form->{"partnumber_$i"} eq "")
 
 502       && ($form->{"description_$i"} eq "")
 
 503       && ($form->{"partsgroup_$i"} eq "")) {
 
 504     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
 
 509     IR->retrieve_item(\%myconfig, \%$form);
 
 511     my $rows = scalar @{ $form->{item_list} };
 
 513     $form->{"discount_$i"}   = $form->parse_amount(\%myconfig, $form->{"discount_$i"}) / 100.0;
 
 514     $form->{"discount_$i"} ||= $form->{vendor_discount};
 
 517       $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
 
 518       if( !$form->{"qty_$i"} ) {
 
 519         $form->{"qty_$i"} = 1;
 
 524         select_item(mode => 'IR', pre_entered_qty => $form->{"qty_$i"});
 
 529         # override sellprice if there is one entered
 
 530         my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
 
 532         map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
 
 533         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
 
 535         $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
 
 537         ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
 
 538         my $dec_qty       = length $1;
 
 539         my $decimalplaces = max 2, $dec_qty;
 
 542           $form->{"sellprice_$i"} = $sellprice;
 
 544           my $record        = _make_record();
 
 545           my $price_source  = SL::PriceSource->new(record_item => $record->items->[$i-1], record => $record);
 
 546           my $best_price    = $price_source->best_price;
 
 547           my $best_discount = $price_source->best_discount;
 
 550             $::form->{"sellprice_$i"}           = $best_price->price;
 
 551             $::form->{"active_price_source_$i"} = $best_price->source;
 
 553           if ($best_discount) {
 
 554             $::form->{"discount_$i"}               = $best_discount->discount;
 
 555             $::form->{"active_discount_source_$i"} = $best_discount->source;
 
 558           # if there is an exchange rate adjust sellprice
 
 559           $form->{"sellprice_$i"} /= $exchangerate;
 
 562         my $amount                = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"});
 
 563         $form->{creditremaining} -= $amount;
 
 564         $form->{"sellprice_$i"}   = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
 
 565         $form->{"qty_$i"}         = $form->format_amount(\%myconfig, $form->{"qty_$i"},       $dec_qty);
 
 566         $form->{"discount_$i"}    = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100.0);
 
 573       # ok, so this is a new part
 
 574       # ask if it is a part or service item
 
 576       if (   $form->{"partsgroup_$i"}
 
 577           && ($form->{"partsnumber_$i"} eq "")
 
 578           && ($form->{"description_$i"} eq "")) {
 
 580         $form->{"discount_$i"} = "";
 
 584         $form->{"id_$i"}   = 0;
 
 589   $main::lxdebug->leave_sub();
 
 593   $main::lxdebug->enter_sub();
 
 595   my $form     = $main::form;
 
 596   my %myconfig = %main::myconfig;
 
 597   my $locale   = $main::locale;
 
 599   $main::auth->assert('vendor_invoice_edit');
 
 601   if ($form->{storno}) {
 
 602     $form->error($locale->text('Cannot storno storno invoice!'));
 
 605   if (IS->has_storno(\%myconfig, $form, "ap")) {
 
 606     $form->error($locale->text("Invoice has already been storno'd!"));
 
 609   $form->error($locale->text('Cannot post storno for a closed period!'))
 
 610     if ( $form->date_closed($form->{invdate}, \%myconfig));
 
 612   my $employee_id = $form->{employee_id};
 
 617   # Payments must not be recorded for the new storno invoice.
 
 618   $form->{paidaccounts} = 0;
 
 619   map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ gldate_ acc_trans_id_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
 
 620   # set new ids for storno invoice
 
 621   delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};
 
 624   if(!exists $form->{addition} && $form->{id} ne "") {
 
 625     $form->{snumbers}  = qq|invnumber_| . $form->{invnumber};
 
 626     $form->{what_done} = "invoice";
 
 627     $form->{addition}  = "CANCELED";
 
 630   # /saving the history
 
 632   $form->{storno_id} = $form->{id};
 
 635   $form->{invnumber} = "Storno zu " . $form->{invnumber};
 
 637   $form->{employee_id} = $employee_id;
 
 639   $main::lxdebug->leave_sub();
 
 644   $main::lxdebug->enter_sub();
 
 646   my $form     = $main::form;
 
 647   my %myconfig = %main::myconfig;
 
 649   $main::auth->assert('vendor_invoice_edit');
 
 651   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);
 
 652   $form->{paidaccounts} = 1;
 
 654   $form->{invdate} = $form->current_date(\%myconfig);
 
 656   $form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};
 
 658   $form->{useasnew} = 1;
 
 661   $main::lxdebug->leave_sub();
 
 665   $main::lxdebug->enter_sub();
 
 667   my $form     = $main::form;
 
 668   my %myconfig = %main::myconfig;
 
 669   my $locale   = $main::locale;
 
 671   $main::auth->assert('vendor_invoice_edit');
 
 673   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 674   for my $i (1 .. $form->{paidaccounts}) {
 
 675     if ($form->{"paid_$i"}) {
 
 676       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
 
 678       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
 680       $form->error($locale->text('Cannot post payment for a closed period!'))
 
 681         if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
 
 683       if ($form->{currency} ne $form->{defaultcurrency}) {
 
 684 #        $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid); # invdate isn't set here
 
 685         $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
 
 690   ($form->{AP})      = split /--/, $form->{AP};
 
 691   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
 
 692   if (IR->post_payment(\%myconfig, \%$form)){
 
 693     if (!exists $form->{addition} && $form->{id} ne "") {
 
 695       $form->{snumbers}  = qq|invnumber_| . $form->{invnumber};
 
 696       $form->{addition}  = "PAYMENT POSTED";
 
 697       $form->{what_done} = "invoice";
 
 699       # /saving the history
 
 702     $form->redirect($locale->text('Payment posted!'));
 
 705   $form->error($locale->text('Cannot post payment!'));
 
 707   $main::lxdebug->leave_sub();
 
 711   my $form  =  $main::form;
 
 713   my @dates = sort { $b->[1] cmp $a->[1] }
 
 714               map  { [ $_, $main::locale->reformat_date(\%main::myconfig, $_, 'yyyy-mm-dd') ] }
 
 716               map  { $form->{"datepaid_${_}"} }
 
 717               (1..$form->{rowcount});
 
 719   return @dates ? $dates[0]->[0] : undef;
 
 724   $main::lxdebug->enter_sub();
 
 726   my $form     = $main::form;
 
 727   my %myconfig = %main::myconfig;
 
 728   my $locale   = $main::locale;
 
 730   $main::auth->assert('vendor_invoice_edit');
 
 732   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 734   $form->isblank("invdate",   $locale->text('Invoice Date missing!'));
 
 735   $form->isblank("vendor",    $locale->text('Vendor missing!'));
 
 736   $form->isblank("invnumber", $locale->text('Invnumber missing!'));
 
 738   $form->{invnumber} =~ s/^\s*//g;
 
 739   $form->{invnumber} =~ s/\s*$//g;
 
 741   # if the vendor changed get new values
 
 742   if (&check_name('vendor')) {
 
 747   if ($myconfig{mandatory_departments} && !$form->{department_id}) {
 
 748     $form->{saved_message} = $::locale->text('You have to specify a department.');
 
 753   remove_emptied_rows();
 
 756   my $closedto     = $form->datetonum($form->{closedto}, \%myconfig);
 
 757   my $invdate      = $form->datetonum($form->{invdate},  \%myconfig);
 
 758   my $max_datepaid = _max_datepaid();
 
 760   $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
 
 761     if ($form->date_max_future($invdate, \%myconfig));
 
 762   $form->error($locale->text('Cannot post invoice for a closed period!'))
 
 763     if ($invdate <= $closedto);
 
 765   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
 
 766     if ($form->{currency} ne $form->{defaultcurrency});
 
 769   for $i (1 .. $form->{paidaccounts}) {
 
 770     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
 
 771       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
 
 773       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
 775       $form->error($locale->text('Cannot post payment for a closed period!'))
 
 776         if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
 
 778       if ($form->{currency} ne $form->{defaultcurrency}) {
 
 779         $form->{"exchangerate_$i"} = $form->{exchangerate}
 
 780           if ($invdate == $datepaid);
 
 781         $form->isblank("exchangerate_$i",
 
 782                        $locale->text('Exchangerate for payment missing!'));
 
 787   ($form->{AP})      = split /--/, $form->{AP};
 
 788   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
 
 789   $form->{storno}  ||= 0;
 
 791   $form->{id} = 0 if $form->{postasnew};
 
 795   if (IR->post_invoice(\%myconfig, \%$form)){
 
 797     if(!exists $form->{addition} && $form->{id} ne "") {
 
 798       $form->{snumbers}  = qq|invnumber_| . $form->{invnumber};
 
 799       $form->{addition}  = "POSTED";
 
 800       $form->{what_done} = 'invoice';
 
 803     # /saving the history
 
 804     remove_draft() if $form->{remove_draft};
 
 805     $form->redirect(  $locale->text('Invoice')
 
 806                   . " $form->{invnumber} "
 
 807                   . $locale->text('posted!'));
 
 809   $form->error($locale->text('Cannot post invoice!'));
 
 811   $main::lxdebug->leave_sub();
 
 815   $main::lxdebug->enter_sub();
 
 817   my $form     = $main::form;
 
 818   my $locale   = $main::locale;
 
 820   $main::auth->assert('vendor_invoice_edit');
 
 824 <form method=post action=$form->{script}>
 
 827   # delete action variable
 
 828   map { delete $form->{$_} } qw(action header);
 
 830   foreach my $key (keys %$form) {
 
 831     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
 
 832     $form->{$key} =~ s/\"/"/g;
 
 833     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
 
 837 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
 
 840     . $locale->text('Are you sure you want to delete Invoice Number')
 
 841     . qq| $form->{invnumber}</h4>
 
 843 <input name=action class=submit type=submit value="|
 
 844     . $locale->text('Yes') . qq|">
 
 848   $main::lxdebug->leave_sub();
 
 852   $main::lxdebug->enter_sub();
 
 854   my $form     = $main::form;
 
 855   my %myconfig = %main::myconfig;
 
 856   my $locale   = $main::locale;
 
 858   $main::auth->assert('vendor_invoice_edit');
 
 860   if (IR->delete_invoice(\%myconfig, \%$form)) {
 
 862     if(!exists $form->{addition}) {
 
 863       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
 864       $form->{addition} = "DELETED";
 
 867     # /saving the history
 
 868     $form->redirect($locale->text('Invoice deleted!'));
 
 870   $form->error($locale->text('Cannot delete invoice!'));
 
 872   $main::lxdebug->leave_sub();
 
 875 sub get_duedate_vendor {
 
 876   $::lxdebug->enter_sub;
 
 878   my $result = IR->get_duedate(
 
 879     vendor_id => $::form->{vendor_id},
 
 880     invdate   => $::form->{invdate},
 
 881     default   => $::form->{old_duedate},
 
 884   print $::form->ajax_response_header, $result;
 
 885   $::lxdebug->leave_sub;