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 invoicing module
 
  32 #======================================================================
 
  39 use List::Util qw(max sum);
 
  41 require "bin/mozilla/io.pl";
 
  42 require "bin/mozilla/invoice_io.pl";
 
  43 require "bin/mozilla/arap.pl";
 
  44 require "bin/mozilla/drafts.pl";
 
  56   $main::lxdebug->enter_sub();
 
  58   my $form     = $main::form;
 
  59   my $locale   = $main::locale;
 
  61   $main::auth->assert('invoice_edit');
 
  63   return $main::lxdebug->leave_sub() if (load_draft_maybe());
 
  65   if ($form->{type} eq "credit_note") {
 
  66     $form->{title} = $locale->text('Add Credit Note');
 
  68     if ($form->{storno}) {
 
  69       $form->{title} = $locale->text('Add Storno Credit Note');
 
  72     $form->{title} = $locale->text('Add Sales Invoice');
 
  77   $form->{callback} = "$form->{script}?action=add&type=$form->{type}" unless $form->{callback};
 
  79   $form->{jsscript} = "date";
 
  85   $main::lxdebug->leave_sub();
 
  89   $main::lxdebug->enter_sub();
 
  91   my $form     = $main::form;
 
  92   my $locale   = $main::locale;
 
  94   $main::auth->assert('invoice_edit');
 
  97   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
 
  98   #/show hhistory button
 
 100   if ($form->{type} eq "credit_note") {
 
 101     $form->{title} = $locale->text('Edit Credit Note');
 
 102     $form->{title} = $locale->text('Edit Storno Credit Note') if $form->{storno};
 
 104     $form->{title} = $locale->text('Edit Sales Invoice');
 
 105     $form->{title} = $locale->text('Edit Storno Invoice')     if $form->{storno};
 
 108   my ($language_id, $printer_id);
 
 109   if ($form->{print_and_post}) {
 
 110     $form->{action}   = "print";
 
 111     $form->{resubmit} = 1;
 
 112     $language_id = $form->{language_id};
 
 113     $printer_id = $form->{printer_id};
 
 117   if ($form->{print_and_post}) {
 
 118     $form->{language_id} = $language_id;
 
 119     $form->{printer_id} = $printer_id;
 
 124   $main::lxdebug->leave_sub();
 
 128   $main::lxdebug->enter_sub();
 
 130   my $form     = $main::form;
 
 131   my %myconfig = %main::myconfig;
 
 133   $main::auth->assert('invoice_edit');
 
 135   $form->{vc} = 'customer';
 
 138   $form->{webdav}   = $main::webdav;
 
 139   $form->{lizenzen} = $main::lizenzen;
 
 141   $form->create_links("AR", \%myconfig, "customer");
 
 143   if ($form->{all_customer}) {
 
 144     unless ($form->{customer_id}) {
 
 145       $form->{customer_id} = $form->{all_customer}->[0]->{id};
 
 146       $form->{salesman_id} = $form->{all_customer}->[0]->{salesman_id};
 
 150   $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded cp_id intnotes id shipto_id));
 
 152   IS->get_customer(\%myconfig, \%$form);
 
 154   #quote all_customer Bug 133
 
 155   foreach my $ref (@{ $form->{all_customer} }) {
 
 156     $ref->{name} = $form->quote($ref->{name});
 
 159   # Load data for a specific order and update form fields
 
 160   my $order_data = OE->get_order_data_by_ordnumber(%$form) if $form->{ordnumber};
 
 162   # Copy the fields we need to %form
 
 163   for my $key (qw(payment_id salesman_id orddate taxzone_id quonumber)) {
 
 164     $form->{$key} = $order_data->{$key};
 
 167   $form->restore_vars(qw(id));
 
 169   IS->retrieve_invoice(\%myconfig, \%$form);
 
 170   $form->restore_vars(qw(payment_id language_id taxzone_id intnotes cp_id shipto_id));
 
 171   $form->restore_vars(qw(taxincluded)) if $form->{id};
 
 172   $form->restore_vars(qw(salesman_id)) if $main::editing;
 
 175   # build vendor/customer drop down comatibility... don't ask
 
 176   if (@{ $form->{"all_customer"} }) {
 
 177     $form->{"selectcustomer"} = 1;
 
 178     $form->{customer}         = qq|$form->{customer}--$form->{"customer_id"}|;
 
 181   $form->{"oldcustomer"}  = $form->{customer};
 
 183   if ($form->{"oldcustomer"} !~ m/--\d+$/ && $form->{"customer_id"}) {
 
 184     $form->{"oldcustomer"} .= qq|--$form->{"customer_id"}|
 
 188 #  $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
 
 189 #  $form->{selectcustomer} = 1;
 
 191   $form->{employee} = "$form->{employee}--$form->{employee_id}";
 
 194   $form->{forex} = $form->{exchangerate};
 
 195   my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
 
 197   foreach my $key (keys %{ $form->{AR_links} }) {
 
 198     foreach my $ref (@{ $form->{AR_links}{$key} }) {
 
 199       $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}</option>\n";
 
 202     if ($key eq "AR_paid") {
 
 203       next unless $form->{acc_trans}{$key};
 
 204       for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
 
 205         $form->{"AR_paid_$i"}      = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
 
 208         $form->{"paid_$i"}         = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
 
 209         $form->{"datepaid_$i"}     = $form->{acc_trans}{$key}->[$i - 1]->{transdate};
 
 210         $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
 
 211         $form->{"forex_$i"}        = $form->{"exchangerate_$i"};
 
 212         $form->{"source_$i"}       = $form->{acc_trans}{$key}->[$i - 1]->{source};
 
 213         $form->{"memo_$i"}         = $form->{acc_trans}{$key}->[$i - 1]->{memo};
 
 215         $form->{paidaccounts} = $i;
 
 218       $form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
 
 222   $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
 
 224   $form->{AR} = $form->{AR_1} unless $form->{id};
 
 226   $form->{locked} = ($form->datetonum($form->{invdate},  \%myconfig)
 
 227                   <= $form->datetonum($form->{closedto}, \%myconfig));
 
 229   $main::lxdebug->leave_sub();
 
 232 sub prepare_invoice {
 
 233   $main::lxdebug->enter_sub();
 
 235   my $form     = $main::form;
 
 236   my %myconfig = %main::myconfig;
 
 238   $main::auth->assert('invoice_edit');
 
 240   if ($form->{type} eq "credit_note") {
 
 241     $form->{type}     = "credit_note";
 
 242     $form->{formname} = "credit_note";
 
 244     $form->{type}     = "invoice";
 
 245     $form->{formname} = "invoice";
 
 252     foreach my $ref (@{ $form->{invoice_details} }) {
 
 255       map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
 
 257       $form->{"discount_$i"}   = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
 
 258       my ($dec)                = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
 
 260       my $decimalplaces        = ($dec > 2) ? $dec : 2;
 
 262       $form->{"sellprice_$i"}  = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
 
 263       (my $dec_qty)            = ($form->{"qty_$i"} =~ /\.(\d+)/);
 
 264       $dec_qty                 = length $dec_qty;
 
 266       $form->{"qty_$i"}        = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
 
 268       $form->{rowcount}        = $i;
 
 272     # get pricegroups for parts
 
 273     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
 
 274     set_pricegroup($_) for 1 .. $form->{rowcount};
 
 276   $main::lxdebug->leave_sub();
 
 280   $main::lxdebug->enter_sub();
 
 282   my $form     = $main::form;
 
 283   my %myconfig = %main::myconfig;
 
 284   my $locale   = $main::locale;
 
 285   my $cgi      = $main::cgi;
 
 287   $main::auth->assert('invoice_edit');
 
 292   $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
 
 293   $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
 
 295   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 297   my $set_duedate_url = "$form->{script}?action=set_duedate";
 
 299   push @ { $form->{AJAX} }, new CGI::Ajax( 'set_duedate' => $set_duedate_url );
 
 301   my @old_project_ids = ($form->{"globalproject_id"});
 
 302   map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
 
 304   $form->get_lists("contacts"      => "ALL_CONTACTS",
 
 305                    "shipto"        => "ALL_SHIPTO",
 
 306                    "projects"      => { "key"    => "ALL_PROJECTS",
 
 308                                         "old_id" => \@old_project_ids },
 
 309                    "employees"     => "ALL_EMPLOYEES",
 
 310                    "salesmen"      => "ALL_SALESMEN",
 
 311                    "taxzones"      => "ALL_TAXZONES",
 
 312                    "currencies"    => "ALL_CURRENCIES",
 
 313                    "customers"     => "ALL_CUSTOMERS",
 
 314                    "departments"   => "all_departments",
 
 315                    "price_factors" => "ALL_PRICE_FACTORS");
 
 317   $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
 
 318   $TMPL_VAR{shipto_labels}         = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
 
 319   $TMPL_VAR{contact_labels}        = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
 
 320   $TMPL_VAR{department_labels}     = sub { "$_[0]->{description}--$_[0]->{id}" };
 
 323   $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
 
 324   $TMPL_VAR{vclimit} = $myconfig{vclimit};
 
 325   $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('customer', '', 0, 0)";
 
 326   push @custom_hiddens, "customer_id";
 
 327   push @custom_hiddens, "oldcustomer";
 
 328   push @custom_hiddens, "selectcustomer";
 
 330   # currencies and exchangerate
 
 331   my @values = map { $_       } @{ $form->{ALL_CURRENCIES} };
 
 332   my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
 
 333   $form->{currency}            = $form->{defaultcurrency} unless $form->{currency};
 
 334   $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
 
 335   $TMPL_VAR{currencies}        = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
 
 336                                                       '-values' => \@values, '-labels' => \%labels)) if scalar @values;
 
 337   push @custom_hiddens, "forex";
 
 338   push @custom_hiddens, "exchangerate" if $form->{forex};
 
 340   $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
 
 341   $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
 
 343   $form->{fokus} = "invoice.customer";
 
 345   my $follow_up_vc         =  $form->{customer};
 
 346   $follow_up_vc            =~ s/--\d*\s*$//;
 
 347   $TMPL_VAR{customer_name} = $follow_up_vc;
 
 349 # set option selected
 
 350   foreach my $item (qw(AR)) {
 
 351     $form->{"select$item"} =~ s/ selected//;
 
 352     $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
 
 355   $TMPL_VAR{is_type_credit_note} = $form->{type}   eq "credit_note";
 
 356   $TMPL_VAR{is_format_html}      = $form->{format} eq 'html';
 
 357   $TMPL_VAR{dateformat}          = $myconfig{dateformat};
 
 358   $TMPL_VAR{numberformat}        = $myconfig{numberformat};
 
 361   $TMPL_VAR{HIDDENS} = [qw(
 
 362     id action type media format queued printed emailed title vc discount
 
 363     title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
 
 364     max_dunning_level dunning_amount
 
 365     shiptoname shiptostreet shiptozipcode shiptocity shiptocountry  shiptocontact shiptophone shiptofax
 
 366     shiptoemail shiptodepartment_1 shiptodepartment_2 message email subject cc bcc taxaccounts cursor_fokus
 
 367     convert_from_do_ids convert_from_oe_ids
 
 369   map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
 
 371   $form->{jsscript} = 1;
 
 374   print $form->parse_html_template("is/form_header", \%TMPL_VAR);
 
 376   $main::lxdebug->leave_sub();
 
 380   $main::lxdebug->enter_sub();
 
 382   my $form     = $main::form;
 
 383   my %myconfig = %main::myconfig;
 
 384   my $locale   = $main::locale;
 
 386   $main::auth->assert('invoice_edit');
 
 388   $form->{invtotal}    = $form->{invsubtotal};
 
 389   $form->{oldinvtotal} = $form->{invtotal};
 
 392   $form->{rows} = max 2,
 
 393     $form->numtextrows($form->{notes},    26, 8),
 
 394     $form->numtextrows($form->{intnotes}, 35, 8);
 
 397   # tax, total and subtotal calculations
 
 398   my ($tax, $subtotal);
 
 399   $form->{taxaccounts_array} = [ split / /, $form->{taxaccounts} ];
 
 401   foreach my $item (@{ $form->{taxaccounts_array} }) {
 
 402     if ($form->{"${item}_base"}) {
 
 403       if ($form->{taxincluded}) {
 
 404         $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"}
 
 405                                                                                  / (1 + $form->{"${item}_rate"})), 2);
 
 406         $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
 
 408         $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
 
 409         $form->{invtotal} += $form->{"${item}_total"};
 
 416     $form->{follow_ups}            = FU->follow_ups('trans_id' => $form->{id}) || [];
 
 417     $form->{follow_ups_unfinished} = ( sum map { $_->{due} * 1 } @{ $form->{follow_ups} } ) || 0;
 
 422   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
 
 423   $form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];
 
 425   for my $i (1 .. $form->{paidaccounts}) {
 
 426     $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
 
 427     $form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
 
 428     $totalpaid += $form->{"paid_$i"};
 
 431   print $form->parse_html_template('is/form_footer', {
 
 432     is_type_credit_note => ($form->{type} eq "credit_note"),
 
 433     totalpaid           => $totalpaid,
 
 434     paid_missing        => $form->{invtotal} - $totalpaid,
 
 435     print_options       => print_options(inline => 1),
 
 436     show_storno         => $form->{id} && !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar") && !$totalpaid,
 
 437     show_delete         => ($form->current_date(\%myconfig) eq $form->{gldate}),
 
 439 ##print $form->parse_html_template('is/_payments'); # parser
 
 440 ##print $form->parse_html_template('webdav/_list'); # parser
 
 442   $main::lxdebug->leave_sub();
 
 446   $main::lxdebug->enter_sub();
 
 448   my $form     = $main::form;
 
 449   my %myconfig = %main::myconfig;
 
 451   $main::auth->assert('invoice_edit');
 
 453   &mark_as_paid_common(\%myconfig,"ar");
 
 455   $main::lxdebug->leave_sub();
 
 459   $main::lxdebug->enter_sub();
 
 461   my $form     = $main::form;
 
 462   my %myconfig = %main::myconfig;
 
 464   $main::auth->assert('invoice_edit');
 
 466   my ($recursive_call) = shift;
 
 468 #  map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining) unless $recursive_call;
 
 470   $form->{print_and_post} = 0         if $form->{second_run};
 
 471   my $taxincluded            = "checked" if $form->{taxincluded};
 
 474   &check_name("customer");
 
 476   $form->{taxincluded} ||= $taxincluded;
 
 478   $form->{forex}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
 
 479   $form->{exchangerate} = $form->{forex} if $form->{forex};
 
 481   for my $i (1 .. $form->{paidaccounts}) {
 
 482     next unless $form->{"paid_$i"};
 
 483     map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
 
 484     $form->{"forex_$i"}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
 
 485     $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
 
 488   my $i            = $form->{rowcount};
 
 489   my $exchangerate = $form->{exchangerate} || 1;
 
 491   # if last row empty, check the form otherwise retrieve new item
 
 492   if (   ($form->{"partnumber_$i"} eq "")
 
 493       && ($form->{"description_$i"} eq "")
 
 494       && ($form->{"partsgroup_$i"}  eq "")) {
 
 496     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
 
 501     IS->retrieve_item(\%myconfig, \%$form);
 
 503     my $rows = scalar @{ $form->{item_list} };
 
 505     $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{customer_discount} * 100);
 
 508       $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
 
 517         my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
 
 519         map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
 
 520         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
 
 522         $form->{payment_id}    = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
 
 523         $form->{"discount_$i"} = 0                             if $form->{"not_discountable_$i"};
 
 525         $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
 
 527         ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
 
 528         my $decimalplaces = max 2, length $1;
 
 531           $form->{"sellprice_$i"} = $sellprice;
 
 533           # if there is an exchange rate adjust sellprice
 
 534           $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
 
 535           $form->{"sellprice_$i"} /= $exchangerate;
 
 538         $form->{"listprice_$i"} /= $exchangerate;
 
 540         my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
 
 541         map { $form->{"${_}_base"} = 0 }                                 split / /, $form->{taxaccounts};
 
 542         map { $form->{"${_}_base"} += $amount }                          split / /, $form->{"taxaccounts_$i"};
 
 543         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
 
 545         $form->{creditremaining} -= $amount;
 
 547         map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice listprice);
 
 549         $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
 
 551         if ($main::lizenzen) {
 
 552           if ($form->{"inventory_accno_$i"} ne "") {
 
 553             $form->{"lizenzen_$i"} = qq|<option></option>|;
 
 554             foreach my $item (@{ $form->{LIZENZEN}{ $form->{"id_$i"} } }) {
 
 555               $form->{"lizenzen_$i"} .= qq|<option value="$item->{"id"}">$item->{"licensenumber"}</option>|;
 
 557             $form->{"lizenzen_$i"} .= qq|<option value=-1>Neue Lizenz</option>|;
 
 561         # get pricegroups for parts
 
 562         IS->get_pricegroups_for_parts(\%myconfig, \%$form);
 
 564         # build up html code for prices_$i
 
 572       # ok, so this is a new part
 
 573       # ask if it is a part or service item
 
 575       if (   $form->{"partsgroup_$i"}
 
 576           && ($form->{"partsnumber_$i"} eq "")
 
 577           && ($form->{"description_$i"} eq "")) {
 
 579         $form->{"discount_$i"} = "";
 
 583         $form->{"id_$i"}   = 0;
 
 588   $main::lxdebug->leave_sub();
 
 592   $main::lxdebug->enter_sub();
 
 594   my $form     = $main::form;
 
 595   my %myconfig = %main::myconfig;
 
 596   my $locale   = $main::locale;
 
 598   $main::auth->assert('invoice_edit');
 
 602   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 603   for my $i (1 .. $form->{paidaccounts}) {
 
 604     if ($form->{"paid_$i"}) {
 
 605       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
 
 607       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
 609       $form->error($locale->text('Cannot post payment for a closed period!'))
 
 610         if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
 
 612       if ($form->{currency} ne $form->{defaultcurrency}) {
 
 613         $form->{"exchangerate_$i"} = $form->{exchangerate}
 
 614           if ($invdate == $datepaid);
 
 615         $form->isblank("exchangerate_$i",
 
 616                        $locale->text('Exchangerate for payment missing!'));
 
 621   ($form->{AR})      = split /--/, $form->{AR};
 
 622   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
 
 624   $form->redirect($locale->text('Payment posted!'))
 
 625       if (IS->post_payment(\%myconfig, \%$form));
 
 626     $form->error($locale->text('Cannot post payment!'));
 
 629   $main::lxdebug->leave_sub();
 
 633   $main::lxdebug->enter_sub();
 
 635   my $form     = $main::form;
 
 636   my %myconfig = %main::myconfig;
 
 637   my $locale   = $main::locale;
 
 639   $main::auth->assert('invoice_edit');
 
 641   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 642   $form->isblank("invdate",  $locale->text('Invoice Date missing!'));
 
 643   $form->isblank("customer", $locale->text('Customer missing!'));
 
 644   $form->error($locale->text('Cannot post invoice for a closed period!'))
 
 645         if ($form->date_closed($form->{"invdate"}, \%myconfig));
 
 647   $form->{invnumber} =~ s/^\s*//g;
 
 648   $form->{invnumber} =~ s/\s*$//g;
 
 650   # if oldcustomer ne customer redo form
 
 651   if (&check_name('customer')) {
 
 655   if ($form->{second_run}) {
 
 656     $form->{print_and_post} = 0;
 
 659   remove_emptied_rows();
 
 662   my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
 
 663   my $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
 
 665   $form->error($locale->text('Cannot post invoice for a closed period!'))
 
 666     if ($invdate <= $closedto);
 
 668   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
 
 669     if ($form->{currency} ne $form->{defaultcurrency});
 
 671   for my $i (1 .. $form->{paidaccounts}) {
 
 672     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
 
 673       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
 
 675       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
 677       $form->error($locale->text('Cannot post payment for a closed period!'))
 
 678         if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
 
 680       if ($form->{currency} ne $form->{defaultcurrency}) {
 
 681         $form->{"exchangerate_$i"} = $form->{exchangerate}
 
 682           if ($invdate == $datepaid);
 
 683         $form->isblank("exchangerate_$i",
 
 684                        $locale->text('Exchangerate for payment missing!'));
 
 689   ($form->{AR})        = split /--/, $form->{AR};
 
 690   ($form->{AR_paid})   = split /--/, $form->{AR_paid};
 
 691   $form->{storno}    ||= 0;
 
 693   $form->{label} = $locale->text('Invoice');
 
 695   $form->{id} = 0 if $form->{postasnew};
 
 697   # get new invnumber in sequence if no invnumber is given or if posasnew was requested
 
 698   if ($form->{postasnew}) {
 
 699     if ($form->{type} eq "credit_note") {
 
 700       undef($form->{cnnumber});
 
 702       undef($form->{invnumber});
 
 707   $form->error($locale->text('Cannot post invoice!'))
 
 708     unless IS->post_invoice(\%myconfig, \%$form);
 
 709   remove_draft() if $form->{remove_draft};
 
 711   if(!exists $form->{addition}) {
 
 712     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
 713     $form->{addition} = $print_post     ? "PRINTED AND POSTED" :
 
 714                         $form->{storno} ? "STORNO"             :
 
 716     $form->save_history($form->dbconnect(\%myconfig));
 
 719   $form->redirect( $form->{label} . " $form->{invnumber} " . $locale->text('posted!'))
 
 722   $main::lxdebug->leave_sub();
 
 726   $main::lxdebug->enter_sub();
 
 728   my $form     = $main::form;
 
 730   $main::auth->assert('invoice_edit');
 
 732   my $old_form               = new Form;
 
 734   $form->{print_and_post} = 1;
 
 738   $main::lxdebug->leave_sub();
 
 742 sub use_as_template {
 
 743   $main::lxdebug->enter_sub();
 
 745   my $form     = $main::form;
 
 746   my %myconfig = %main::myconfig;
 
 748   $main::auth->assert('invoice_edit');
 
 750   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);
 
 751   $form->{paidaccounts} = 1;
 
 753   $form->{invdate} = $form->current_date(\%myconfig);
 
 756   $main::lxdebug->leave_sub();
 
 760   $main::lxdebug->enter_sub();
 
 762   my $form     = $main::form;
 
 763   my %myconfig = %main::myconfig;
 
 764   my $locale   = $main::locale;
 
 766   $main::auth->assert('invoice_edit');
 
 768   if ($form->{storno}) {
 
 769     $form->error($locale->text('Cannot storno storno invoice!'));
 
 772   if (IS->has_storno(\%myconfig, $form, "ar")) {
 
 773     $form->error($locale->text("Invoice has already been storno'd!"));
 
 776   map({ my $key = $_; delete($form->{$key}) unless (grep({ $key eq $_ } qw(id login password stylesheet type))); } keys(%{ $form }));
 
 782   # Payments must not be recorded for the new storno invoice.
 
 783   $form->{paidaccounts} = 0;
 
 784   map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
 
 786   $form->{storno_id} = $form->{id};
 
 789   $form->{invnumber} = "Storno zu " . $form->{invnumber};
 
 793   $main::lxdebug->leave_sub();
 
 797   $main::lxdebug->enter_sub();
 
 799   my $form     = $main::form;
 
 801   $main::auth->assert('invoice_edit');
 
 803   $form->{preview} = 1;
 
 804   my $old_form = new Form;
 
 805   for (keys %$form) { $old_form->{$_} = $form->{$_} }
 
 807   &print_form($old_form);
 
 808   $main::lxdebug->leave_sub();
 
 813   $main::lxdebug->enter_sub();
 
 815   my $form     = $main::form;
 
 816   my $locale   = $main::locale;
 
 818   $main::auth->assert('invoice_edit');
 
 820   if ($form->{second_run}) {
 
 821     $form->{print_and_post} = 0;
 
 828 <form method="post" action="$form->{script}">
 
 831   # delete action variable
 
 832   map { delete $form->{$_} } qw(action header);
 
 834   foreach my $key (keys %$form) {
 
 835     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
 
 836     $form->{$key} =~ s/\"/"/g;
 
 837     print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
 
 841 <h2 class="confirm">| . $locale->text('Confirm!') . qq|</h2>
 
 844     . $locale->text('Are you sure you want to delete Invoice Number')
 
 845     . qq| $form->{invnumber}
 
 849 <input name="action" class="submit" type="submit" value="|
 
 850     . $locale->text('Yes') . qq|">
 
 854   $main::lxdebug->leave_sub();
 
 858   $main::lxdebug->enter_sub();
 
 860   my $form     = $main::form;
 
 861   my %myconfig = %main::myconfig;
 
 862   my $locale   = $main::locale;
 
 864   $main::auth->assert('invoice_edit');
 
 866   $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
 
 868     $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
 
 875   $form->{title}  = $locale->text('Add Credit Note');
 
 876   $form->{script} = 'is.pl';
 
 878   our $buysell        = 'buy';
 
 881   # bo creates the id, reset it
 
 882   map { delete $form->{$_} }
 
 883     qw(id invnumber subject message cc bcc printed emailed queued);
 
 884   $form->{ $form->{vc} } =~ s/--.*//g;
 
 885   $form->{type} = "credit_note";
 
 888   map { $form->{"select$_"} = "" } ($form->{vc}, 'currency');
 
 890 #  map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
 
 891 #    qw(creditlimit creditremaining);
 
 893   my $currency = $form->{currency};
 
 896   $form->{currency}     = $currency;
 
 897   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, $buysell);
 
 898   $form->{exchangerate} = $form->{forex} || '';
 
 900   $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
 
 902   # bei Gutschriften werden Zahlungseingänge aus Rechnung nicht übernommen
 
 903   for my $i (1 .. $form->{paidaccounts}) {
 
 904     delete $form->{"paid_$i"};
 
 905     delete $form->{"source_$i"};
 
 906     delete $form->{"memo_$i"};
 
 907     delete $form->{"datepaid_$i"};
 
 908     delete $form->{"AR_paid_$i"};
 
 910   $form->{paidaccounts} = 1;
 
 917   $main::lxdebug->leave_sub();
 
 921   $main::lxdebug->enter_sub();
 
 923   my $form     = $main::form;
 
 924   my %myconfig = %main::myconfig;
 
 925   my $locale   = $main::locale;
 
 927   $main::auth->assert('invoice_edit');
 
 929   if (IS->delete_invoice(\%myconfig, \%$form, $main::spool)) {
 
 931     if(!exists $form->{addition}) {
 
 932     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
 933       $form->{addition} = "DELETED";
 
 934       $form->save_history($form->dbconnect(\%myconfig));
 
 936     # /saving the history
 
 937     $form->redirect($locale->text('Invoice deleted!'));
 
 939   $form->error($locale->text('Cannot delete invoice!'));
 
 941   $main::lxdebug->leave_sub();
 
 945   $main::lxdebug->enter_sub();
 
 947   my $form     = $main::form;
 
 949   $main::auth->assert('invoice_edit');
 
 954     my $saved_form = save_form();
 
 958     restore_form($saved_form, 0, qw(id invnumber));
 
 963   $main::lxdebug->leave_sub();