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";
 
  53   $main::lxdebug->enter_sub();
 
  55   my $form     = $main::form;
 
  56   my $locale   = $main::locale;
 
  58   $main::auth->assert('invoice_edit');
 
  60   return $main::lxdebug->leave_sub() if (load_draft_maybe());
 
  62   if ($form->{type} eq "credit_note") {
 
  63     $form->{title} = $locale->text('Add Credit Note');
 
  65     if ($form->{storno}) {
 
  66       $form->{title} = $locale->text('Add Storno Credit Note');
 
  69     $form->{title} = $locale->text('Add Sales Invoice');
 
  74   $form->{callback} = "$form->{script}?action=add&type=$form->{type}" unless $form->{callback};
 
  76   $form->{jsscript} = "date";
 
  82   $main::lxdebug->leave_sub();
 
  86   $main::lxdebug->enter_sub();
 
  88   my $form     = $main::form;
 
  89   my $locale   = $main::locale;
 
  91   $main::auth->assert('invoice_edit');
 
  93   $form->{taxincluded_changed_by_user} = 1;
 
  96   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
 
  97   #/show hhistory button
 
  99   my ($language_id, $printer_id);
 
 100   if ($form->{print_and_post}) {
 
 101     $form->{action}   = "print";
 
 102     $form->{resubmit} = 1;
 
 103     $language_id = $form->{language_id};
 
 104     $printer_id = $form->{printer_id};
 
 108   if ($form->{type} eq "credit_note") {
 
 109     $form->{title} = $locale->text('Edit Credit Note');
 
 110     $form->{title} = $locale->text('Edit Storno Credit Note') if $form->{storno};
 
 112     $form->{title} = $locale->text('Edit Sales Invoice');
 
 113     $form->{title} = $locale->text('Edit Storno Invoice')     if $form->{storno};
 
 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}   = $::lx_office_conf{features}->{webdav};
 
 140   $form->create_links("AR", \%myconfig, "customer");
 
 142   if ($form->{all_customer}) {
 
 143     unless ($form->{customer_id}) {
 
 144       $form->{customer_id} = $form->{all_customer}->[0]->{id};
 
 145       $form->{salesman_id} = $form->{all_customer}->[0]->{salesman_id};
 
 149   my $editing = $form->{id};
 
 151   $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded currency cp_id intnotes id shipto_id));
 
 153   IS->get_customer(\%myconfig, \%$form);
 
 155   #quote all_customer Bug 133
 
 156   foreach my $ref (@{ $form->{all_customer} }) {
 
 157     $ref->{name} = $form->quote($ref->{name});
 
 160   $form->restore_vars(qw(id));
 
 162   IS->retrieve_invoice(\%myconfig, \%$form);
 
 163   $form->restore_vars(qw(payment_id language_id taxzone_id currency intnotes cp_id shipto_id));
 
 164   $form->restore_vars(qw(taxincluded)) if $form->{id};
 
 165   $form->restore_vars(qw(salesman_id)) if $editing;
 
 168   # build vendor/customer drop down comatibility... don't ask
 
 169   if (@{ $form->{"all_customer"} }) {
 
 170     $form->{"selectcustomer"} = 1;
 
 171     $form->{customer}         = qq|$form->{customer}--$form->{"customer_id"}|;
 
 174   $form->{"oldcustomer"}  = $form->{customer};
 
 176   if ($form->{"oldcustomer"} !~ m/--\d+$/ && $form->{"customer_id"}) {
 
 177     $form->{"oldcustomer"} .= qq|--$form->{"customer_id"}|
 
 181 #  $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
 
 182 #  $form->{selectcustomer} = 1;
 
 184   $form->{employee} = "$form->{employee}--$form->{employee_id}";
 
 187   $form->{forex} = $form->{exchangerate};
 
 188   my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
 
 190   foreach my $key (keys %{ $form->{AR_links} }) {
 
 191     foreach my $ref (@{ $form->{AR_links}{$key} }) {
 
 192       $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}</option>\n";
 
 195     if ($key eq "AR_paid") {
 
 196       next unless $form->{acc_trans}{$key};
 
 197       for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
 
 198         $form->{"AR_paid_$i"}      = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
 
 200         $form->{"acc_trans_id_$i"}    = $form->{acc_trans}{$key}->[$i - 1]->{acc_trans_id};
 
 202         $form->{"paid_$i"}         = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
 
 203         $form->{"datepaid_$i"}     = $form->{acc_trans}{$key}->[$i - 1]->{transdate};
 
 204         $form->{"gldate_$i"}       = $form->{acc_trans}{$key}->[$i - 1]->{gldate};
 
 205         $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
 
 206         $form->{"forex_$i"}        = $form->{"exchangerate_$i"};
 
 207         $form->{"source_$i"}       = $form->{acc_trans}{$key}->[$i - 1]->{source};
 
 208         $form->{"memo_$i"}         = $form->{acc_trans}{$key}->[$i - 1]->{memo};
 
 210         $form->{paidaccounts} = $i;
 
 213       $form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
 
 217   $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
 
 219   $form->{AR} = $form->{AR_1} unless $form->{id};
 
 221   $form->{locked} = ($form->datetonum($form->{invdate},  \%myconfig)
 
 222                   <= $form->datetonum($form->{closedto}, \%myconfig));
 
 224   $main::lxdebug->leave_sub();
 
 227 sub prepare_invoice {
 
 228   $main::lxdebug->enter_sub();
 
 230   my $form     = $main::form;
 
 231   my %myconfig = %main::myconfig;
 
 233   $main::auth->assert('invoice_edit');
 
 235   if ($form->{type} eq "credit_note") {
 
 236     $form->{type}     = "credit_note";
 
 237     $form->{formname} = "credit_note";
 
 239     $form->{type}     = "invoice";
 
 240     $form->{formname} = "invoice";
 
 247     foreach my $ref (@{ $form->{invoice_details} }) {
 
 250       map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
 
 252       $form->{"discount_$i"}   = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
 
 253       my ($dec)                = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
 
 255       my $decimalplaces        = ($dec > 2) ? $dec : 2;
 
 257       $form->{"sellprice_$i"}  = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
 
 258       (my $dec_qty)            = ($form->{"qty_$i"} =~ /\.(\d+)/);
 
 259       $dec_qty                 = length $dec_qty;
 
 261       $form->{"lastcost_$i"}  = $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, $decimalplaces);
 
 263       $form->{"qty_$i"}        = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
 
 265       $form->{"sellprice_pg_$i"} = join ('--', $form->{"sellprice_$i"}, $form->{"pricegroup_id_$i"});
 
 267       $form->{rowcount}        = $i;
 
 271     # get pricegroups for parts
 
 272     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
 
 274     # Problem: set_pricegroup resets the sellprice of old invoices to the price
 
 275     # currently defined in the pricegroup, which is a problem if the price has
 
 276     # changed, as the old invoice gets the new price
 
 277     # set_pricegroup must never be called, when an old invoice is initially loaded
 
 279     # set_pricegroup($_) for 1 .. $form->{rowcount};
 
 281   $main::lxdebug->leave_sub();
 
 285   $main::lxdebug->enter_sub();
 
 287   my $form     = $main::form;
 
 288   my %myconfig = %main::myconfig;
 
 289   my $locale   = $main::locale;
 
 290   my $cgi      = $::request->{cgi};
 
 292   $main::auth->assert('invoice_edit');
 
 297   $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
 
 298   $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
 
 300   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 302   my @old_project_ids = ($form->{"globalproject_id"});
 
 303   map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
 
 305   $form->get_lists("projects"      => { "key"    => "ALL_PROJECTS",
 
 307                                         "old_id" => \@old_project_ids },
 
 308                    "taxzones"      => "ALL_TAXZONES",
 
 309                    "currencies"    => "ALL_CURRENCIES",
 
 310                    "customers"     => "ALL_CUSTOMERS",
 
 311                    "departments"   => "all_departments",
 
 312                    "price_factors" => "ALL_PRICE_FACTORS");
 
 314   $TMPL_VAR{ALL_EMPLOYEES}         = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $::form->{employee_id},  deleted => 0 ] ]);
 
 315   $TMPL_VAR{ALL_SALESMEN}          = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $::form->{salesman_id},  deleted => 0 ] ]);
 
 316   $TMPL_VAR{ALL_SHIPTO}            = SL::DB::Manager::Shipto->get_all(query => [
 
 317     or => [ trans_id  => $::form->{"$::form->{vc}_id"} * 1, and => [ shipto_id => $::form->{shipto_id} * 1, trans_id => undef ] ]
 
 319   $TMPL_VAR{ALL_CONTACTS}          = SL::DB::Manager::Contact->get_all(query => [
 
 321       cp_cv_id => $::form->{"$::form->{vc}_id"} * 1,
 
 324         cp_id    => $::form->{cp_id} * 1
 
 328   $TMPL_VAR{department_labels}     = sub { "$_[0]->{description}--$_[0]->{id}" };
 
 331   $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
 
 332   $TMPL_VAR{vclimit} = $myconfig{vclimit};
 
 333   $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('customer', '', 0, 0)";
 
 334   push @custom_hiddens, "customer_id";
 
 335   push @custom_hiddens, "oldcustomer";
 
 336   push @custom_hiddens, "selectcustomer";
 
 338   # currencies and exchangerate
 
 339   my @values = map { $_       } @{ $form->{ALL_CURRENCIES} };
 
 340   my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
 
 341   $form->{currency}            = $form->{defaultcurrency} unless $form->{currency};
 
 342   $form->{show_exchangerate}   = $form->{currency} ne $form->{defaultcurrency};
 
 343   $TMPL_VAR{currencies}        = NTI($::request->{cgi}->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
 
 344                                                       '-values' => \@values, '-labels' => \%labels,
 
 345                                                       '-onchange' => "document.getElementById('update_button').click();"
 
 346                                      )) if scalar @values;
 
 347   push @custom_hiddens, "forex";
 
 348   push @custom_hiddens, "exchangerate" if $form->{forex};
 
 350   $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
 
 351   $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
 
 353   $form->{fokus} = "invoice.customer";
 
 355   my $follow_up_vc         =  $form->{customer};
 
 356   $follow_up_vc            =~ s/--\d*\s*$//;
 
 357   $TMPL_VAR{customer_name} = $follow_up_vc;
 
 359 # set option selected
 
 360   foreach my $item (qw(AR)) {
 
 361     $form->{"select$item"} =~ s/ selected//;
 
 362     $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
 
 365   $TMPL_VAR{is_type_credit_note} = $form->{type}   eq "credit_note";
 
 366   $TMPL_VAR{is_format_html}      = $form->{format} eq 'html';
 
 367   $TMPL_VAR{dateformat}          = $myconfig{dateformat};
 
 368   $TMPL_VAR{numberformat}        = $myconfig{numberformat};
 
 371   $TMPL_VAR{HIDDENS} = [qw(
 
 372     id action type media format queued printed emailed title vc discount
 
 373     title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
 
 374     max_dunning_level dunning_amount
 
 375     shiptoname shiptostreet shiptozipcode shiptocity shiptocountry  shiptocontact shiptophone shiptofax
 
 376     shiptoemail shiptodepartment_1 shiptodepartment_2  shiptocp_gender message email subject cc bcc taxaccounts cursor_fokus
 
 377     convert_from_do_ids convert_from_oe_ids
 
 379   map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
 
 381   $form->{jsscript} = 1;
 
 384   print $form->parse_html_template("is/form_header", \%TMPL_VAR);
 
 386   $main::lxdebug->leave_sub();
 
 390   $main::lxdebug->enter_sub();
 
 392   my $form     = $main::form;
 
 393   my %myconfig = %main::myconfig;
 
 394   my $locale   = $main::locale;
 
 396   $main::auth->assert('invoice_edit');
 
 398   $form->{invtotal}    = $form->{invsubtotal};
 
 401   $form->{rows} = max 2,
 
 402     $form->numtextrows($form->{notes},    26, 8),
 
 403     $form->numtextrows($form->{intnotes}, 35, 8);
 
 406   # tax, total and subtotal calculations
 
 407   my ($tax, $subtotal);
 
 408   $form->{taxaccounts_array} = [ split / /, $form->{taxaccounts} ];
 
 410   IS->get_customer(\%myconfig, \%$form) if $form->{type} =~ /sales_(order|quotation)/;
 
 412   if ( $form->{vc} eq 'customer' && !$form->{taxincluded_changed_by_user} ) {
 
 413     $form->{taxincluded} = defined($form->{taxincluded_checked}) ? $form->{taxincluded_checked} : $myconfig{taxincluded_checked};
 
 416   foreach my $item (@{ $form->{taxaccounts_array} }) {
 
 417     if ($form->{"${item}_base"}) {
 
 418       if ($form->{taxincluded}) {
 
 419         $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"}
 
 420                                                                                  / (1 + $form->{"${item}_rate"})), 2);
 
 421         $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
 
 423         $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
 
 424         $form->{invtotal} += $form->{"${item}_total"};
 
 431     $form->{follow_ups}            = FU->follow_ups('trans_id' => $form->{id}) || [];
 
 432     $form->{follow_ups_unfinished} = ( sum map { $_->{due} * 1 } @{ $form->{follow_ups} } ) || 0;
 
 437   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
 
 438   $form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];
 
 440   # Standard Konto für Umlaufvermögen
 
 441   my $accno_arap = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
 
 443   for my $i (1 .. $form->{paidaccounts}) {
 
 444     $form->{"changeable_$i"} = 1;
 
 445     if ($::lx_office_conf{features}->{payments_changeable} == 0) {
 
 447       $form->{"changeable_$i"} = ($form->{"acc_trans_id_$i"})? 0 : 1;
 
 448     } elsif ($::lx_office_conf{features}->{payments_changeable} == 2) {
 
 450       $form->{"changeable_$i"} = (($form->{"gldate_$i"} eq '') ||
 
 451                                   ($form->current_date(\%myconfig) eq $form->{"gldate_$i"}));
 
 454     $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
 
 455     if (!$form->{"AR_paid_$i"}) {
 
 456       $form->{"selectAR_paid_$i"} =~ s/option>$accno_arap--(.*?)</option selected>$accno_arap--$1</;
 
 458       $form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
 
 461     $totalpaid += $form->{"paid_$i"};
 
 464   $form->{oldinvtotal} = $form->{invtotal};
 
 466   print $form->parse_html_template('is/form_footer', {
 
 467     is_type_credit_note => ($form->{type} eq "credit_note"),
 
 468     totalpaid           => $totalpaid,
 
 469     paid_missing        => $form->{invtotal} - $totalpaid,
 
 470     print_options       => print_options(inline => 1),
 
 471     show_storno         => $form->{id} && !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar") && !$totalpaid,
 
 472     show_delete         => ($form->current_date(\%myconfig) eq $form->{gldate}),
 
 474 ##print $form->parse_html_template('is/_payments'); # parser
 
 475 ##print $form->parse_html_template('webdav/_list'); # parser
 
 477   $main::lxdebug->leave_sub();
 
 481   $main::lxdebug->enter_sub();
 
 483   my $form     = $main::form;
 
 484   my %myconfig = %main::myconfig;
 
 486   $main::auth->assert('invoice_edit');
 
 488   &mark_as_paid_common(\%myconfig,"ar");
 
 490   $main::lxdebug->leave_sub();
 
 494   $main::lxdebug->enter_sub();
 
 496   my $form     = $main::form;
 
 497   my %myconfig = %main::myconfig;
 
 499   $main::auth->assert('invoice_edit');
 
 501   my ($recursive_call) = @_;
 
 503   $form->{print_and_post} = 0         if $form->{second_run};
 
 504   my $taxincluded         = $form->{taxincluded} ? "checked" : '';
 
 507   &check_name("customer");
 
 509   $form->{taxincluded} ||= $taxincluded;
 
 511   if (!$form->{forex}) {        # read exchangerate from input field (not hidden)
 
 512     $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate}) unless $recursive_call;
 
 514   $form->{forex}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
 
 515   $form->{exchangerate} = $form->{forex} if $form->{forex};
 
 517   for my $i (1 .. $form->{paidaccounts}) {
 
 518     next unless $form->{"paid_$i"};
 
 519     map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
 
 520     $form->{"forex_$i"}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
 
 521     $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
 
 524   my $i            = $form->{rowcount};
 
 525   my $exchangerate = $form->{exchangerate} || 1;
 
 527   # if last row empty, check the form otherwise retrieve new item
 
 528   if (   ($form->{"partnumber_$i"} eq "")
 
 529       && ($form->{"description_$i"} eq "")
 
 530       && ($form->{"partsgroup_$i"}  eq "")) {
 
 532     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
 
 537     IS->retrieve_item(\%myconfig, \%$form);
 
 539     my $rows = scalar @{ $form->{item_list} };
 
 541     # Falls kein Kundenrabatt vorhanden ist, den aktuellen Rabatt nicht mit 0% überschreiben,
 
 542     # da hier der Anwender schon manual einen Wert eingetragen haben könnte (analog zu qty) Bugfix: 1412
 
 543     if ($form->{customer_discount}){
 
 544       $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{customer_discount} * 100);
 
 548       $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
 
 552         select_item(mode => 'IS');
 
 557         my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
 
 559         map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
 
 560         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
 
 562         $form->{payment_id}    = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
 
 563         $form->{"discount_$i"} = 0                             if $form->{"not_discountable_$i"};
 
 565         $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
 
 567         ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
 
 568         my $decimalplaces = max 2, length $1;
 
 571           $form->{"sellprice_$i"} = $sellprice;
 
 573           # if there is an exchange rate adjust sellprice
 
 574           $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
 
 575           $form->{"sellprice_$i"} /= $exchangerate;
 
 578         $form->{"listprice_$i"} /= $exchangerate;
 
 580         my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
 
 581         map { $form->{"${_}_base"} = 0 }                                 split / /, $form->{taxaccounts};
 
 582         map { $form->{"${_}_base"} += $amount }                          split / /, $form->{"taxaccounts_$i"};
 
 583         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
 
 585         $form->{creditremaining} -= $amount;
 
 587         map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice lastcost);
 
 589         $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
 
 591         # get pricegroups for parts
 
 592         IS->get_pricegroups_for_parts(\%myconfig, \%$form);
 
 594         # build up html code for prices_$i
 
 602       # ok, so this is a new part
 
 603       # ask if it is a part or service item
 
 605       if (   $form->{"partsgroup_$i"}
 
 606           && ($form->{"partsnumber_$i"} eq "")
 
 607           && ($form->{"description_$i"} eq "")) {
 
 609         $form->{"discount_$i"} = "";
 
 613         $form->{"id_$i"}   = 0;
 
 618   $main::lxdebug->leave_sub();
 
 622   $main::lxdebug->enter_sub();
 
 624   my $form     = $main::form;
 
 625   my %myconfig = %main::myconfig;
 
 626   my $locale   = $main::locale;
 
 628   $main::auth->assert('invoice_edit');
 
 630   my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
 
 632   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 633   for my $i (1 .. $form->{paidaccounts}) {
 
 634     if ($form->{"paid_$i"}) {
 
 635       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
 
 637       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
 640       if ($form->{currency} ne $form->{defaultcurrency}) {
 
 641         $form->{"exchangerate_$i"} = $form->{exchangerate}
 
 642           if ($invdate == $datepaid);
 
 643         $form->isblank("exchangerate_$i",
 
 644                        $locale->text('Exchangerate for payment missing!'));
 
 648   # Abgeschlossene Zeiträume nur für den letzten (aktuellen) Zahlungseingang prüfen
 
 649   # Details s.a. Bug 1502
 
 650   # Das Problem ist jetzt, dass man Zahlungseingänge nachträglich ändern kann
 
 651   # Wobei dies für Installationen die sowieso nicht mit Bücherkontrolle arbeiten keinen
 
 652   # keinen Unterschied macht.
 
 653   # Optimal wäre, wenn gegen einen Zeitstempel des Zahlungsfelds geprüft würde ...
 
 654   # Das Problem hierbei ist, dass in IS.pm post_invoice IMMER alle Zahlungseingänge aus $form
 
 655   # erneut gespeichert werden. Prinzipiell wäre es besser NUR die Änderungen des Rechnungs-
 
 656   # belegs (neue Zahlung aber nichts anderes) zu speichern ...
 
 657   # Vielleicht könnte man ähnlich wie bei Rechnung löschen verfahren
 
 658   $form->error($locale->text('Cannot post payment for a closed period!'))
 
 659     if ($form->date_closed($form->{"datepaid_$form->{paidaccounts}"}, \%myconfig));
 
 661   ($form->{AR})      = split /--/, $form->{AR};
 
 662   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
 
 664   $form->redirect($locale->text('Payment posted!'))
 
 665       if (IS->post_payment(\%myconfig, \%$form));
 
 666     $form->error($locale->text('Cannot post payment!'));
 
 669   $main::lxdebug->leave_sub();
 
 673   $main::lxdebug->enter_sub();
 
 675   my $form     = $main::form;
 
 676   my %myconfig = %main::myconfig;
 
 677   my $locale   = $main::locale;
 
 679   $main::auth->assert('invoice_edit');
 
 681   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 682   $form->isblank("invdate",  $locale->text('Invoice Date missing!'));
 
 683   $form->isblank("customer", $locale->text('Customer missing!'));
 
 684   $form->error($locale->text('Cannot post invoice for a closed period!'))
 
 685         if ($form->date_closed($form->{"invdate"}, \%myconfig));
 
 687   $form->{invnumber} =~ s/^\s*//g;
 
 688   $form->{invnumber} =~ s/\s*$//g;
 
 690   # if oldcustomer ne customer redo form
 
 691   if (&check_name('customer')) {
 
 696   if ($myconfig{mandatory_departments} && !$form->{department_id}) {
 
 697     $form->{saved_message} = $::locale->text('You have to specify a department.');
 
 702   if ($form->{second_run}) {
 
 703     $form->{print_and_post} = 0;
 
 706   remove_emptied_rows();
 
 709   my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
 
 710   my $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
 
 712   $form->error($locale->text('Cannot post invoice for a closed period!'))
 
 713     if ($invdate <= $closedto);
 
 715   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
 
 716     if ($form->{currency} ne $form->{defaultcurrency});
 
 718   for my $i (1 .. $form->{paidaccounts}) {
 
 719     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
 
 720       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
 
 722       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
 724       $form->error($locale->text('Cannot post payment for a closed period!'))
 
 725         if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
 
 727       if ($form->{currency} ne $form->{defaultcurrency}) {
 
 728         $form->{"exchangerate_$i"} = $form->{exchangerate}
 
 729           if ($invdate == $datepaid);
 
 730         $form->isblank("exchangerate_$i",
 
 731                        $locale->text('Exchangerate for payment missing!'));
 
 736   ($form->{AR})        = split /--/, $form->{AR};
 
 737   ($form->{AR_paid})   = split /--/, $form->{AR_paid};
 
 738   $form->{storno}    ||= 0;
 
 740   $form->{label} = $form->{type} eq 'credit_note' ? $locale->text('Credit Note') : $locale->text('Invoice');
 
 742   $form->{id} = 0 if $form->{postasnew};
 
 744   # get new invnumber in sequence if no invnumber is given or if posasnew was requested
 
 745   if ($form->{postasnew}) {
 
 746     if ($form->{type} eq "credit_note") {
 
 747       undef($form->{cnnumber});
 
 749       undef($form->{invnumber});
 
 754   $form->error($locale->text('Cannot post invoice!'))
 
 755     unless IS->post_invoice(\%myconfig, \%$form);
 
 756   remove_draft() if $form->{remove_draft};
 
 758   if(!exists $form->{addition}) {
 
 759     $form->{snumbers} =  'invnumber' .'_'. $form->{invnumber}; # ($form->{type} eq 'credit_note' ? 'cnnumber' : 'invnumber') .'_'. $form->{invnumber};
 
 760     $form->{addition} = $form->{print_and_post} ? "PRINTED AND POSTED" :
 
 761                         $form->{storno}         ? "STORNO"             :
 
 766   if (!$form->{no_redirect_after_post}) {
 
 767     $form->{action} = 'edit';
 
 768     $form->{script} = 'is.pl';
 
 769     $form->{saved_message} = $form->{label} . " $form->{invnumber} " . $locale->text('posted!');
 
 770     $form->{callback} = build_std_url(qw(action edit id saved_message));
 
 774   $main::lxdebug->leave_sub();
 
 778   $main::lxdebug->enter_sub();
 
 780   my $form     = $main::form;
 
 782   $main::auth->assert('invoice_edit');
 
 784   my $old_form                    = new Form;
 
 785   $form->{no_redirect_after_post} = 1;
 
 786   $form->{print_and_post}         = 1;
 
 790   $main::lxdebug->leave_sub();
 
 794 sub use_as_template {
 
 795   $main::lxdebug->enter_sub();
 
 797   my $form     = $main::form;
 
 798   my %myconfig = %main::myconfig;
 
 800   $main::auth->assert('invoice_edit');
 
 802   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);
 
 803   $form->{paidaccounts} = 1;
 
 805   $form->{invdate} = $form->current_date(\%myconfig);
 
 807   # remember pricegroups for "use as template"
 
 808   IS->get_pricegroups_for_parts(\%myconfig, \%$form);
 
 809   set_pricegroup($_) for 1 .. $form->{rowcount};
 
 813   $main::lxdebug->leave_sub();
 
 817   $main::lxdebug->enter_sub();
 
 819   my $form     = $main::form;
 
 820   my %myconfig = %main::myconfig;
 
 821   my $locale   = $main::locale;
 
 823   $main::auth->assert('invoice_edit');
 
 825   if ($form->{storno}) {
 
 826     $form->error($locale->text('Cannot storno storno invoice!'));
 
 829   if (IS->has_storno(\%myconfig, $form, "ar")) {
 
 830     $form->error($locale->text("Invoice has already been storno'd!"));
 
 833   map({ my $key = $_; delete($form->{$key}) unless (grep({ $key eq $_ } qw(id login password stylesheet type))); } keys(%{ $form }));
 
 839   # Payments must not be recorded for the new storno invoice.
 
 840   $form->{paidaccounts} = 0;
 
 841   map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ gldate_ acc_trans_id_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
 
 843   $form->{storno_id} = $form->{id};
 
 846   $form->{invnumber} = "Storno zu " . $form->{invnumber};
 
 850   $main::lxdebug->leave_sub();
 
 854   $main::lxdebug->enter_sub();
 
 856   my $form     = $main::form;
 
 858   $main::auth->assert('invoice_edit');
 
 860   $form->{preview} = 1;
 
 861   my $old_form = new Form;
 
 862   for (keys %$form) { $old_form->{$_} = $form->{$_} }
 
 864   &print_form($old_form);
 
 865   $main::lxdebug->leave_sub();
 
 870   $main::lxdebug->enter_sub();
 
 872   my $form     = $main::form;
 
 873   my $locale   = $main::locale;
 
 875   $main::auth->assert('invoice_edit');
 
 877   if ($form->{second_run}) {
 
 878     $form->{print_and_post} = 0;
 
 885 <form method="post" action="$form->{script}">
 
 888   # delete action variable
 
 889   map { delete $form->{$_} } qw(action header);
 
 891   foreach my $key (keys %$form) {
 
 892     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
 
 893     $form->{$key} =~ s/\"/"/g;
 
 894     print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
 
 898 <h2 class="confirm">| . $locale->text('Confirm!') . qq|</h2>
 
 901     . $locale->text('Are you sure you want to delete Invoice Number')
 
 902     . qq| $form->{invnumber}
 
 906 <input name="action" class="submit" type="submit" value="|
 
 907     . $locale->text('Yes') . qq|">
 
 911   $main::lxdebug->leave_sub();
 
 915   $main::lxdebug->enter_sub();
 
 917   my $form     = $main::form;
 
 918   my %myconfig = %main::myconfig;
 
 919   my $locale   = $main::locale;
 
 921   $main::auth->assert('invoice_edit');
 
 923   $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
 
 925     $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
 
 932   $form->{title}  = $locale->text('Add Credit Note');
 
 933   $form->{script} = 'is.pl';
 
 935   # Bei Gutschriften bezug zur Rechnungsnummer
 
 936   $form->{invnumber_for_credit_note} = $form->{invnumber};
 
 937   # bo creates the id, reset it
 
 938   map { delete $form->{$_} }
 
 939     qw(id invnumber subject message cc bcc printed emailed queued);
 
 940   $form->{ $form->{vc} } =~ s/--.*//g;
 
 941   $form->{type} = "credit_note";
 
 944   map { $form->{"select$_"} = "" } ($form->{vc}, 'currency');
 
 946 #  map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
 
 947 #    qw(creditlimit creditremaining);
 
 949   for my $i (1 .. $form->{rowcount}) {
 
 950     for (qw(listprice)) {
 
 951       $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
 
 955   my $currency = $form->{currency};
 
 958   $form->{currency}     = $currency;
 
 959   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, 'buy');
 
 960   $form->{exchangerate} = $form->{forex} || '';
 
 962   $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
 
 964   # bei Gutschriften werden Zahlungseingänge aus Rechnung nicht übernommen
 
 965   for my $i (1 .. $form->{paidaccounts}) {
 
 966     delete $form->{"paid_$i"};
 
 967     delete $form->{"source_$i"};
 
 968     delete $form->{"memo_$i"};
 
 969     delete $form->{"datepaid_$i"};
 
 970     delete $form->{"gldate_$i"};
 
 971     delete $form->{"acc_trans_id_$i"};
 
 972     delete $form->{"AR_paid_$i"};
 
 974   $form->{paidaccounts} = 1;
 
 981   $main::lxdebug->leave_sub();
 
 985   $main::lxdebug->enter_sub();
 
 987   my $form     = $main::form;
 
 988   my %myconfig = %main::myconfig;
 
 989   my $locale   = $main::locale;
 
 991   $main::auth->assert('invoice_edit');
 
 993   if (IS->delete_invoice(\%myconfig, \%$form)) {
 
 995     if(!exists $form->{addition}) {
 
 996       $form->{snumbers} = 'invnumber' .'_'. $form->{invnumber}; # ($form->{type} eq 'credit_note' ? 'cnnumber' : 'invnumber') .'_'. $form->{invnumber};
 
 997       $form->{addition} = "DELETED";
 
1000     # /saving the history
 
1001     $form->redirect($locale->text('Invoice deleted!'));
 
1003   $form->error($locale->text('Cannot delete invoice!'));
 
1005   $main::lxdebug->leave_sub();
 
1009   $main::lxdebug->enter_sub();
 
1011   my $form     = $main::form;
 
1013   $main::auth->assert('invoice_edit');
 
1016     $form->{no_redirect_after_post} = 1;
 
1018     my $saved_form = save_form();
 
1022     restore_form($saved_form, 0, qw(id invnumber));
 
1027   $main::lxdebug->leave_sub();