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 #======================================================================
 
  38 use List::Util qw(max sum);
 
  40 require "bin/mozilla/io.pl";
 
  41 require "bin/mozilla/invoice_io.pl";
 
  42 require "bin/mozilla/arap.pl";
 
  43 require "bin/mozilla/common.pl";
 
  44 require "bin/mozilla/drafts.pl";
 
  53   $main::lxdebug->enter_sub();
 
  55   my $form     = $main::form;
 
  56   my $locale   = $main::locale;
 
  58   $main::auth->assert('vendor_invoice_edit');
 
  60   return $main::lxdebug->leave_sub() if (load_draft_maybe());
 
  62   $form->{title} = $locale->text('Record Vendor Invoice');
 
  68   $main::lxdebug->leave_sub();
 
  72   $main::lxdebug->enter_sub();
 
  74   my $form     = $main::form;
 
  75   my $locale   = $main::locale;
 
  77   $main::auth->assert('vendor_invoice_edit');
 
  80   $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
 
  81   #/show hhistory button
 
  83   $form->{title} = $locale->text('Edit Vendor Invoice');
 
  89   $main::lxdebug->leave_sub();
 
  93   $main::lxdebug->enter_sub();
 
  95   my $form     = $main::form;
 
  96   my %myconfig = %main::myconfig;
 
  98   $main::auth->assert('vendor_invoice_edit');
 
 100   $form->{vc} = 'vendor';
 
 103   $form->{webdav}   = $::lx_office_conf{features}->{webdav};
 
 104   $form->{jsscript} = 1;
 
 106   $form->create_links("AP", \%myconfig, "vendor");
 
 108   #quote all_vendor Bug 133
 
 109   foreach my $ref (@{ $form->{all_vendor} }) {
 
 110     $ref->{name} = $form->quote($ref->{name});
 
 113   if ($form->{all_vendor}) {
 
 114     unless ($form->{vendor_id}) {
 
 115       $form->{vendor_id} = $form->{all_vendor}->[0]->{id};
 
 119   my ($payment_id, $language_id, $taxzone_id);
 
 120   if ($form->{payment_id}) {
 
 121     $payment_id = $form->{payment_id};
 
 123   if ($form->{language_id}) {
 
 124     $language_id = $form->{language_id};
 
 126   if ($form->{taxzone_id}) {
 
 127     $taxzone_id = $form->{taxzone_id};
 
 130   my $cp_id = $form->{cp_id};
 
 131   IR->get_vendor(\%myconfig, \%$form);
 
 132   IR->retrieve_invoice(\%myconfig, \%$form);
 
 133   $form->{cp_id} = $cp_id;
 
 136     $form->{payment_id} = $payment_id;
 
 139     $form->{language_id} = $language_id;
 
 142     $form->{taxzone_id} = $taxzone_id;
 
 145   my @curr = split(/:/, $form->{currencies}); #seems to be missing
 
 146   map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
 
 148   $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
 
 150   # build vendor/customer drop down comatibility... don't ask
 
 151   if (@{ $form->{"all_vendor"} || [] }) {
 
 152     $form->{"selectvendor"} = 1;
 
 153     $form->{vendor}         = qq|$form->{vendor}--$form->{vendor_id}|;
 
 157   if ($form->{all_departments}) {
 
 158     $form->{selectdepartment} = "<option>\n";
 
 159     $form->{department}       = "$form->{department}--$form->{department_id}";
 
 162       $form->{selectdepartment} .=
 
 163         "<option>$_->{description}--$_->{id}\n"
 
 164     } (@{ $form->{all_departments} || [] });
 
 168   $form->{forex} = $form->{exchangerate};
 
 169   my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
 
 171   foreach my $key (keys %{ $form->{AP_links} }) {
 
 173     foreach my $ref (@{ $form->{AP_links}{$key} }) {
 
 174       $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}</option>";
 
 177     next unless $form->{acc_trans}{$key};
 
 179     if ($key eq "AP_paid") {
 
 180       for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
 
 181         $form->{"AP_paid_$i"} =
 
 182           "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
 
 185         $form->{"paid_$i"}     = $form->{acc_trans}{$key}->[$i - 1]->{amount};
 
 186         $form->{"datepaid_$i"} =
 
 187           $form->{acc_trans}{$key}->[$i - 1]->{transdate};
 
 188         $form->{"forex_$i"} = $form->{"exchangerate_$i"} =
 
 189           $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
 
 190         $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
 
 191         $form->{"memo_$i"}   = $form->{acc_trans}{$key}->[$i - 1]->{memo};
 
 193         $form->{paidaccounts} = $i;
 
 197         "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
 
 202   $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
 
 204   $form->{AP} = $form->{AP_1} unless $form->{id};
 
 207     ($form->datetonum($form->{invdate}, \%myconfig) <=
 
 208      $form->datetonum($form->{closedto}, \%myconfig));
 
 210   $main::lxdebug->leave_sub();
 
 213 sub prepare_invoice {
 
 214   $main::lxdebug->enter_sub();
 
 216   my $form     = $main::form;
 
 217   my %myconfig = %main::myconfig;
 
 219   $main::auth->assert('vendor_invoice_edit');
 
 223     map { $form->{$_} =~ s/\"/"/g } qw(invnumber ordnumber quonumber);
 
 226     foreach my $ref (@{ $form->{invoice_details} }) {
 
 228       map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
 
 229       # übernommen aus is.pl Fix für Bug 1642. Nebenwirkungen? jb 12.5.2011
 
 230       # getestet: Lieferantenauftrag -> Rechnung i.O.
 
 231       #           Lieferantenauftrag -> Lieferschein -> Rechnung i.O.
 
 232       # Werte: 20% (Lieferantenrabatt), 12,4% individuell und 0,4 individuell s.a. 
 
 233       # Screenshot zu Bug 1642
 
 234       $form->{"discount_$i"}   = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
 
 236       my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
 
 238       my $decimalplaces = ($dec > 2) ? $dec : 2;
 
 240       $form->{"sellprice_$i"} =
 
 241         $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
 
 244       (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
 
 245       $dec_qty = length $dec_qty;
 
 248         $form->format_amount(\%myconfig, ($form->{"qty_$i"} * -1), $dec_qty);
 
 250       $form->{rowcount} = $i;
 
 254   $main::lxdebug->leave_sub();
 
 258   $main::lxdebug->enter_sub();
 
 260   my $form     = $main::form;
 
 261   my %myconfig = %main::myconfig;
 
 262   my $locale   = $main::locale;
 
 263   my $cgi      = $main::cgi;
 
 265   $main::auth->assert('vendor_invoice_edit');
 
 270   $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
 
 271   $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
 
 273   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 275   my $set_duedate_url = "$form->{script}?action=set_duedate";
 
 277   push @ { $form->{AJAX} }, new CGI::Ajax( 'set_duedate' => $set_duedate_url );
 
 279   my @old_project_ids = ($form->{"globalproject_id"});
 
 280   map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
 
 282   $form->get_lists("contacts"      => "ALL_CONTACTS",
 
 283                    "shipto"        => "ALL_SHIPTO",
 
 284                    "projects"      => { "key"    => "ALL_PROJECTS",
 
 286                                         "old_id" => \@old_project_ids },
 
 287                    "employees"     => "ALL_EMPLOYEES",
 
 288                    "taxzones"      => "ALL_TAXZONES",
 
 289                    "currencies"    => "ALL_CURRENCIES",
 
 290                    "vendors"       => "ALL_VENDORS",
 
 291                    "departments"   => "all_departments",
 
 292                    "price_factors" => "ALL_PRICE_FACTORS");
 
 294   $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
 
 295 #  $TMPL_VAR{shipto_labels}         = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
 
 296   $TMPL_VAR{contact_labels}        = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
 
 297   $TMPL_VAR{department_labels}     = sub { "$_[0]->{description}--$_[0]->{id}" };
 
 300   $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
 
 301   $TMPL_VAR{vclimit} = $myconfig{vclimit};
 
 302   $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('vendor', '', 1, 0)";
 
 303   push @custom_hiddens, "vendor_id";
 
 304   push @custom_hiddens, "oldvendor";
 
 305   push @custom_hiddens, "selectvendor";
 
 307   # currencies and exchangerate
 
 308   my @values = map { $_       } @{ $form->{ALL_CURRENCIES} };
 
 309   my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
 
 310   $form->{currency}            = $form->{defaultcurrency} unless $form->{currency};
 
 311   $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
 
 312   $TMPL_VAR{currencies}        = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
 
 313                                                       '-values' => \@values, '-labels' => \%labels)) if scalar @values;
 
 314   push @custom_hiddens, "forex";
 
 315   push @custom_hiddens, "exchangerate" if $form->{forex};
 
 317   $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
 
 318   $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
 
 320   $form->{fokus} = "invoice.vendor";
 
 322   my $follow_up_vc         =  $form->{vendor};
 
 323   $follow_up_vc            =~ s/--\d*\s*$//;
 
 324   $TMPL_VAR{vendor_name} = $follow_up_vc;
 
 326 # set option selected
 
 327   foreach my $item (qw(AP)) {
 
 328     $form->{"select$item"} =~ s/ selected//;
 
 329     $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
 
 332   $TMPL_VAR{is_type_credit_note} = $form->{type}   eq "credit_note";
 
 333   $TMPL_VAR{is_format_html}      = $form->{format} eq 'html';
 
 334   $TMPL_VAR{dateformat}          = $myconfig{dateformat};
 
 335   $TMPL_VAR{numberformat}        = $myconfig{numberformat};
 
 338   $TMPL_VAR{HIDDENS} = [qw(
 
 339     id action type media format queued printed emailed title vc discount
 
 340     title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
 
 341     max_dunning_level dunning_amount
 
 342     shiptoname shiptostreet shiptozipcode shiptocity shiptocountry  shiptocontact shiptophone shiptofax
 
 343     shiptoemail shiptodepartment_1 shiptodepartment_2 message email subject cc bcc taxaccounts cursor_fokus
 
 344     convert_from_do_ids convert_from_oe_ids
 
 346   map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
 
 348   $form->{jsscript} = 1;
 
 351   print $form->parse_html_template("ir/form_header", \%TMPL_VAR);
 
 353   $main::lxdebug->leave_sub();
 
 357   $main::lxdebug->enter_sub();
 
 359   my $form     = $main::form;
 
 360   my %myconfig = %main::myconfig;
 
 361   my $locale   = $main::locale;
 
 363   $main::auth->assert('vendor_invoice_edit');
 
 365   $form->{invtotal}    = $form->{invsubtotal};
 
 366   $form->{oldinvtotal} = $form->{invtotal};
 
 369   $form->{rows} = max 2,
 
 370     $form->numtextrows($form->{notes},    26, 8),
 
 371     $form->numtextrows($form->{intnotes}, 35, 8);
 
 374   # tax, total and subtotal calculations
 
 375   my ($tax, $subtotal);
 
 376   $form->{taxaccounts_array} = [ split / /, $form->{taxaccounts} ];
 
 378   foreach my $item (@{ $form->{taxaccounts_array} }) {
 
 379     if ($form->{"${item}_base"}) {
 
 380       if ($form->{taxincluded}) {
 
 381         $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"}
 
 382                                                                                  / (1 + $form->{"${item}_rate"})), 2);
 
 383         $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
 
 385         $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
 
 386         $form->{invtotal} += $form->{"${item}_total"};
 
 393     $form->{follow_ups}            = FU->follow_ups('trans_id' => $form->{id}) || [];
 
 394     $form->{follow_ups_unfinished} = ( sum map { $_->{due} * 1 } @{ $form->{follow_ups} } ) || 0;
 
 399   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
 
 400   $form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];
 
 402   # Standard Konto für Umlaufvermögen
 
 403   my $accno_arap = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
 
 405   for my $i (1 .. $form->{paidaccounts}) {
 
 406     $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
 
 407     if (!$form->{"AP_paid_$i"}) {
 
 408       $form->{"selectAP_paid_$i"} =~ s/option>$accno_arap--(.*?)>/option selected>$accno_arap--$1>/;
 
 410       $form->{"selectAP_paid_$i"} =~ s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
 
 413     $totalpaid += $form->{"paid_$i"};
 
 416   print $form->parse_html_template('ir/form_footer', {
 
 417     is_type_credit_note => ($form->{type} eq "credit_note"),
 
 418     totalpaid           => $totalpaid,
 
 419     paid_missing        => $form->{invtotal} - $totalpaid,
 
 420     show_storno         => $form->{id} && !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ap") && !$totalpaid,
 
 421     show_delete         => ($form->current_date(\%myconfig) eq $form->{gldate}),
 
 423 ##print $form->parse_html_template('ir/_payments'); # parser
 
 424 ##print $form->parse_html_template('webdav/_list'); # parser
 
 426   $main::lxdebug->leave_sub();
 
 430   $main::lxdebug->enter_sub();
 
 432   my $form     = $main::form;
 
 433   my %myconfig = %main::myconfig;
 
 435   $main::auth->assert('vendor_invoice_edit');
 
 437   &mark_as_paid_common(\%myconfig,"ap");
 
 439   $main::lxdebug->leave_sub();
 
 443   $main::lxdebug->enter_sub();
 
 445   my $form     = $main::form;
 
 446   my %myconfig = %main::myconfig;
 
 448   $main::auth->assert('vendor_invoice_edit');
 
 450 #  map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining);
 
 452   &check_name('vendor');
 
 454   $form->{forex}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'sell');
 
 455   $form->{exchangerate} = $form->{forex} if $form->{forex};
 
 457   for my $i (1 .. $form->{paidaccounts}) {
 
 458     next unless $form->{"paid_$i"};
 
 459     map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
 
 460     $form->{"forex_$i"}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
 
 461     $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
 
 464   my $i            = $form->{rowcount};
 
 465   my $exchangerate = ($form->{exchangerate} * 1) || 1;
 
 467   if (   ($form->{"partnumber_$i"} eq "")
 
 468       && ($form->{"description_$i"} eq "")
 
 469       && ($form->{"partsgroup_$i"} eq "")) {
 
 470     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
 
 475     IR->retrieve_item(\%myconfig, \%$form);
 
 477     my $rows = scalar @{ $form->{item_list} };
 
 480       $form->{"qty_$i"} = 1 unless $form->parse_amount(\%myconfig, $form->{"qty_$i"});
 
 489         # override sellprice if there is one entered
 
 490         my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
 
 492         # ergaenzung fuer bug 736 Lieferanten-Rabatt auch in Einkaufsrechnungen vorbelegen jb
 
 493         $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{vendor_discount} * 100 );
 
 494         map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
 
 495         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
 
 497         $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
 
 499         ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
 
 500         my $dec_qty       = length $1;
 
 501         my $decimalplaces = max 2, $dec_qty;
 
 504           $form->{"sellprice_$i"} = $sellprice;
 
 506           # if there is an exchange rate adjust sellprice
 
 507           $form->{"sellprice_$i"} /= $exchangerate;
 
 510         my $amount                   = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
 
 511         $form->{creditremaining} -= $amount;
 
 512         $form->{"sellprice_$i"}   = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
 
 513         $form->{"qty_$i"}         = $form->format_amount(\%myconfig, $form->{"qty_$i"},       $dec_qty);
 
 520       # ok, so this is a new part
 
 521       # ask if it is a part or service item
 
 523       if (   $form->{"partsgroup_$i"}
 
 524           && ($form->{"partsnumber_$i"} eq "")
 
 525           && ($form->{"description_$i"} eq "")) {
 
 527         $form->{"discount_$i"} = "";
 
 531         $form->{"id_$i"}   = 0;
 
 536   $main::lxdebug->leave_sub();
 
 540   $main::lxdebug->enter_sub();
 
 542   my $form     = $main::form;
 
 543   my %myconfig = %main::myconfig;
 
 544   my $locale   = $main::locale;
 
 546   $main::auth->assert('vendor_invoice_edit');
 
 548   if ($form->{storno}) {
 
 549     $form->error($locale->text('Cannot storno storno invoice!'));
 
 552   if (IS->has_storno(\%myconfig, $form, "ap")) {
 
 553     $form->error($locale->text("Invoice has already been storno'd!"));
 
 556   my $employee_id = $form->{employee_id};
 
 561   # Payments must not be recorded for the new storno invoice.
 
 562   $form->{paidaccounts} = 0;
 
 563   map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
 
 566   if(!exists $form->{addition} && $form->{id} ne "") {
 
 567     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
 568     $form->{addition} = "CANCELED";
 
 571   # /saving the history
 
 573   $form->{storno_id} = $form->{id};
 
 576   $form->{invnumber} = "Storno zu " . $form->{invnumber};
 
 578   $form->{employee_id} = $employee_id;
 
 580   $main::lxdebug->leave_sub();
 
 584 sub use_as_template {
 
 585   $main::lxdebug->enter_sub();
 
 587   my $form     = $main::form;
 
 588   my %myconfig = %main::myconfig;
 
 590   $main::auth->assert('vendor_invoice_edit');
 
 592   map { delete $form->{$_} } qw(printed emailed queued invnumber invdate deliverydate id datepaid_1 source_1 memo_1 paid_1 exchangerate_1 AP_paid_1 storno);
 
 593   $form->{paidaccounts} = 1;
 
 595   $form->{invdate} = $form->current_date(\%myconfig);
 
 598   $main::lxdebug->leave_sub();
 
 602   $main::lxdebug->enter_sub();
 
 604   my $form     = $main::form;
 
 605   my %myconfig = %main::myconfig;
 
 606   my $locale   = $main::locale;
 
 608   $main::auth->assert('vendor_invoice_edit');
 
 610   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 611   for my $i (1 .. $form->{paidaccounts}) {
 
 612     if ($form->{"paid_$i"}) {
 
 613       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
 
 615       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
 617       $form->error($locale->text('Cannot post payment for a closed period!'))
 
 618         if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
 
 620       if ($form->{currency} ne $form->{defaultcurrency}) {
 
 621 #        $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid); # invdate isn't set here
 
 622         $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
 
 627   ($form->{AP})      = split /--/, $form->{AP};
 
 628   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
 
 629   if (IR->post_payment(\%myconfig, \%$form)){
 
 630     if (!exists $form->{addition} && $form->{id} ne "") {
 
 632       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
 633       $form->{addition} = "PAYMENT POSTED";
 
 634       $form->{what_done} = $form->{currency} . qq| | . $form->{paid} . qq| | . $locale->text("POSTED");
 
 636       # /saving the history
 
 639     $form->redirect($locale->text('Payment posted!'));
 
 642   $form->error($locale->text('Cannot post payment!'));
 
 644   $main::lxdebug->leave_sub();
 
 648   my $form  =  $main::form;
 
 650   my @dates = sort { $b->[1] cmp $a->[1] }
 
 651               map  { [ $_, $main::locale->reformat_date(\%main::myconfig, $_, 'yyyy-mm-dd') ] }
 
 653               map  { $form->{"datepaid_${_}"} }
 
 654               (1..$form->{rowcount});
 
 656   return @dates ? $dates[0]->[0] : undef;
 
 661   $main::lxdebug->enter_sub();
 
 663   my $form     = $main::form;
 
 664   my %myconfig = %main::myconfig;
 
 665   my $locale   = $main::locale;
 
 667   $main::auth->assert('vendor_invoice_edit');
 
 669   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 671   $form->isblank("invdate",   $locale->text('Invoice Date missing!'));
 
 672   $form->isblank("vendor",    $locale->text('Vendor missing!'));
 
 673   $form->isblank("invnumber", $locale->text('Invnumber missing!'));
 
 675   $form->{invnumber} =~ s/^\s*//g;
 
 676   $form->{invnumber} =~ s/\s*$//g;
 
 678   # if the vendor changed get new values
 
 679   if (&check_name('vendor')) {
 
 684   if ($myconfig{mandatory_departments} && !$form->{department_id}) {
 
 685     $form->{saved_message} = $::locale->text('You have to specify a department.');
 
 690   remove_emptied_rows();
 
 693   my $closedto     = $form->datetonum($form->{closedto}, \%myconfig);
 
 694   my $invdate      = $form->datetonum($form->{invdate},  \%myconfig);
 
 695   my $max_datepaid = _max_datepaid();
 
 697   $form->error($locale->text('Cannot post invoice for a closed period!')) if $max_datepaid && $form->date_closed($max_datepaid, \%myconfig);
 
 699   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
 
 700     if ($form->{currency} ne $form->{defaultcurrency});
 
 703   for $i (1 .. $form->{paidaccounts}) {
 
 704     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
 
 705       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
 
 707       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
 709       $form->error($locale->text('Cannot post payment for a closed period!'))
 
 710         if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
 
 712       if ($form->{currency} ne $form->{defaultcurrency}) {
 
 713         $form->{"exchangerate_$i"} = $form->{exchangerate}
 
 714           if ($invdate == $datepaid);
 
 715         $form->isblank("exchangerate_$i",
 
 716                        $locale->text('Exchangerate for payment missing!'));
 
 721   ($form->{AP})      = split /--/, $form->{AP};
 
 722   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
 
 723   $form->{storno}  ||= 0;
 
 725   $form->{id} = 0 if $form->{postasnew};
 
 729   if (IR->post_invoice(\%myconfig, \%$form)){
 
 731     if(!exists $form->{addition} && $form->{id} ne "") {
 
 732       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
 733       $form->{addition} = "POSTED";
 
 734       #$form->{what_done} = $locale->text("Rechnungsnummer") . qq| | . $form->{invnumber};
 
 737     # /saving the history
 
 738     remove_draft() if $form->{remove_draft};
 
 739     $form->redirect(  $locale->text('Invoice')
 
 740                   . " $form->{invnumber} "
 
 741                   . $locale->text('posted!'));
 
 743   $form->error($locale->text('Cannot post invoice!'));
 
 745   $main::lxdebug->leave_sub();
 
 749   $main::lxdebug->enter_sub();
 
 751   my $form     = $main::form;
 
 752   my $locale   = $main::locale;
 
 754   $main::auth->assert('vendor_invoice_edit');
 
 760 <form method=post action=$form->{script}>
 
 763   # delete action variable
 
 764   map { delete $form->{$_} } qw(action header);
 
 766   foreach my $key (keys %$form) {
 
 767     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
 
 768     $form->{$key} =~ s/\"/"/g;
 
 769     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
 
 773 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
 
 776     . $locale->text('Are you sure you want to delete Invoice Number')
 
 777     . qq| $form->{invnumber}</h4>
 
 779 <input name=action class=submit type=submit value="|
 
 780     . $locale->text('Yes') . qq|">
 
 784   $main::lxdebug->leave_sub();
 
 788   $main::lxdebug->enter_sub();
 
 790   my $form     = $main::form;
 
 791   my %myconfig = %main::myconfig;
 
 792   my $locale   = $main::locale;
 
 794   $main::auth->assert('vendor_invoice_edit');
 
 796   if (IR->delete_invoice(\%myconfig, \%$form)) {
 
 798     if(!exists $form->{addition}) {
 
 799       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
 800       $form->{addition} = "DELETED";
 
 803     # /saving the history
 
 804     $form->redirect($locale->text('Invoice deleted!'));
 
 806   $form->error($locale->text('Cannot delete invoice!'));
 
 808   $main::lxdebug->leave_sub();
 
 811 sub set_duedate_vendor {
 
 812   $main::lxdebug->enter_sub();
 
 814   my $form     = $main::form;
 
 816   print $form->ajax_response_header(), IR->get_duedate('vendor_id' => $form->{vendor_id},
 
 817                                                        'invdate'   => $form->{invdate},
 
 818                                                        'default'   => $form->{old_duedate});
 
 820   $main::lxdebug->leave_sub();