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 #======================================================================
 
  41 use List::Util qw(max sum);
 
  43 require "bin/mozilla/io.pl";
 
  44 require "bin/mozilla/invoice_io.pl";
 
  45 require "bin/mozilla/arap.pl";
 
  46 require "bin/mozilla/drafts.pl";
 
  55   $main::lxdebug->enter_sub();
 
  57   my $form     = $main::form;
 
  58   my $locale   = $main::locale;
 
  60   $main::auth->assert('invoice_edit');
 
  62   return $main::lxdebug->leave_sub() if (load_draft_maybe());
 
  64   if ($form->{type} eq "credit_note") {
 
  65     $form->{title} = $locale->text('Add Credit Note');
 
  67     if ($form->{storno}) {
 
  68       $form->{title} = $locale->text('Add Storno Credit Note');
 
  71     $form->{title} = $locale->text('Add Sales Invoice');
 
  76   $form->{callback} = "$form->{script}?action=add&type=$form->{type}" unless $form->{callback};
 
  78   $form->{jsscript} = "date";
 
  84   $main::lxdebug->leave_sub();
 
  88   $main::lxdebug->enter_sub();
 
  90   my $form     = $main::form;
 
  91   my $locale   = $main::locale;
 
  93   $main::auth->assert('invoice_edit');
 
  95   $form->{taxincluded_changed_by_user} = 1;
 
  98   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
 
  99   #/show hhistory button
 
 101   my ($language_id, $printer_id);
 
 102   if ($form->{print_and_post}) {
 
 103     $form->{action}   = "print";
 
 104     $form->{resubmit} = 1;
 
 105     $language_id = $form->{language_id};
 
 106     $printer_id = $form->{printer_id};
 
 110   if ($form->{type} eq "credit_note") {
 
 111     $form->{title} = $locale->text('Edit Credit Note');
 
 112     $form->{title} = $locale->text('Edit Storno Credit Note') if $form->{storno};
 
 114     $form->{title} = $locale->text('Edit Sales Invoice');
 
 115     $form->{title} = $locale->text('Edit Storno Invoice')     if $form->{storno};
 
 119   if ($form->{print_and_post}) {
 
 120     $form->{language_id} = $language_id;
 
 121     $form->{printer_id} = $printer_id;
 
 126   $main::lxdebug->leave_sub();
 
 130   $main::lxdebug->enter_sub();
 
 132   my $form     = $main::form;
 
 133   my %myconfig = %main::myconfig;
 
 135   $main::auth->assert('invoice_edit');
 
 137   $form->{vc} = 'customer';
 
 140   $form->{webdav}   = $::lx_office_conf{features}->{webdav};
 
 142   $form->create_links("AR", \%myconfig, "customer");
 
 144   if ($form->{all_customer}) {
 
 145     unless ($form->{customer_id}) {
 
 146       $form->{customer_id} = $form->{all_customer}->[0]->{id};
 
 147       $form->{salesman_id} = $form->{all_customer}->[0]->{salesman_id};
 
 151   my $editing = $form->{id};
 
 153   $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded currency cp_id intnotes id shipto_id));
 
 155   IS->get_customer(\%myconfig, \%$form);
 
 157   #quote all_customer Bug 133
 
 158   foreach my $ref (@{ $form->{all_customer} }) {
 
 159     $ref->{name} = $form->quote($ref->{name});
 
 162   $form->restore_vars(qw(id));
 
 164   IS->retrieve_invoice(\%myconfig, \%$form);
 
 165   $form->restore_vars(qw(payment_id language_id taxzone_id currency intnotes cp_id shipto_id));
 
 166   $form->restore_vars(qw(taxincluded)) if $form->{id};
 
 167   $form->restore_vars(qw(salesman_id)) if $editing;
 
 170   # build vendor/customer drop down comatibility... don't ask
 
 171   if (@{ $form->{"all_customer"} }) {
 
 172     $form->{"selectcustomer"} = 1;
 
 173     $form->{customer}         = qq|$form->{customer}--$form->{"customer_id"}|;
 
 176   $form->{"oldcustomer"}  = $form->{customer};
 
 178   if ($form->{"oldcustomer"} !~ m/--\d+$/ && $form->{"customer_id"}) {
 
 179     $form->{"oldcustomer"} .= qq|--$form->{"customer_id"}|
 
 183 #  $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
 
 184 #  $form->{selectcustomer} = 1;
 
 186   $form->{employee} = "$form->{employee}--$form->{employee_id}";
 
 189   $form->{forex} = $form->{exchangerate};
 
 190   my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
 
 192   foreach my $key (keys %{ $form->{AR_links} }) {
 
 193     foreach my $ref (@{ $form->{AR_links}{$key} }) {
 
 194       $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}</option>\n";
 
 197     if ($key eq "AR_paid") {
 
 198       next unless $form->{acc_trans}{$key};
 
 199       for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
 
 200         $form->{"AR_paid_$i"}      = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
 
 202         $form->{"acc_trans_id_$i"}    = $form->{acc_trans}{$key}->[$i - 1]->{acc_trans_id};
 
 204         $form->{"paid_$i"}         = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
 
 205         $form->{"datepaid_$i"}     = $form->{acc_trans}{$key}->[$i - 1]->{transdate};
 
 206         $form->{"gldate_$i"}       = $form->{acc_trans}{$key}->[$i - 1]->{gldate};
 
 207         $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
 
 208         $form->{"forex_$i"}        = $form->{"exchangerate_$i"};
 
 209         $form->{"source_$i"}       = $form->{acc_trans}{$key}->[$i - 1]->{source};
 
 210         $form->{"memo_$i"}         = $form->{acc_trans}{$key}->[$i - 1]->{memo};
 
 212         $form->{paidaccounts} = $i;
 
 215       $form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
 
 219   $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
 
 221   $form->{AR} = $form->{AR_1} unless $form->{id};
 
 223   $form->{locked} = ($form->datetonum($form->{invdate},  \%myconfig)
 
 224                   <= $form->datetonum($form->{closedto}, \%myconfig));
 
 226   $main::lxdebug->leave_sub();
 
 229 sub prepare_invoice {
 
 230   $main::lxdebug->enter_sub();
 
 232   my $form     = $main::form;
 
 233   my %myconfig = %main::myconfig;
 
 235   $main::auth->assert('invoice_edit');
 
 237   if ($form->{type} eq "credit_note") {
 
 238     $form->{type}     = "credit_note";
 
 239     $form->{formname} = "credit_note";
 
 241     $form->{type}     = "invoice";
 
 242     $form->{formname} = "invoice";
 
 249     foreach my $ref (@{ $form->{invoice_details} }) {
 
 252       map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
 
 254       $form->{"discount_$i"}   = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
 
 255       my ($dec)                = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
 
 257       my $decimalplaces        = ($dec > 2) ? $dec : 2;
 
 259       $form->{"sellprice_$i"}  = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
 
 260       (my $dec_qty)            = ($form->{"qty_$i"} =~ /\.(\d+)/);
 
 261       $dec_qty                 = length $dec_qty;
 
 263       $form->{"lastcost_$i"}  = $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, $decimalplaces);
 
 265       $form->{"qty_$i"}        = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
 
 267       $form->{"sellprice_pg_$i"} = join ('--', $form->{"sellprice_$i"}, $form->{"pricegroup_id_$i"});
 
 269       $form->{rowcount}        = $i;
 
 273     # get pricegroups for parts
 
 274     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
 
 276     # Problem: set_pricegroup resets the sellprice of old invoices to the price
 
 277     # currently defined in the pricegroup, which is a problem if the price has
 
 278     # changed, as the old invoice gets the new price
 
 279     # set_pricegroup must never be called, when an old invoice is initially loaded
 
 281     # set_pricegroup($_) for 1 .. $form->{rowcount};
 
 283   $main::lxdebug->leave_sub();
 
 287   $main::lxdebug->enter_sub();
 
 289   my $form     = $main::form;
 
 290   my %myconfig = %main::myconfig;
 
 291   my $locale   = $main::locale;
 
 292   my $cgi      = $::request->{cgi};
 
 294   $main::auth->assert('invoice_edit');
 
 299   $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
 
 300   $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
 
 302   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 304   my @old_project_ids = ($form->{"globalproject_id"});
 
 305   map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
 
 307   $form->get_lists("projects"      => { "key"    => "ALL_PROJECTS",
 
 309                                         "old_id" => \@old_project_ids },
 
 310                    "taxzones"      => "ALL_TAXZONES",
 
 311                    "currencies"    => "ALL_CURRENCIES",
 
 312                    "customers"     => "ALL_CUSTOMERS",
 
 313                    "departments"   => "all_departments",
 
 314                    "price_factors" => "ALL_PRICE_FACTORS");
 
 316   $TMPL_VAR{ALL_EMPLOYEES}         = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $::form->{employee_id},  deleted => 0 ] ]);
 
 317   $TMPL_VAR{ALL_SALESMEN}          = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $::form->{salesman_id},  deleted => 0 ] ]);
 
 318   $TMPL_VAR{ALL_SHIPTO}            = SL::DB::Manager::Shipto->get_all(query => [
 
 319     or => [ trans_id  => $::form->{"$::form->{vc}_id"} * 1, and => [ shipto_id => $::form->{shipto_id} * 1, trans_id => undef ] ]
 
 321   $TMPL_VAR{ALL_CONTACTS}          = SL::DB::Manager::Contact->get_all(query => [
 
 323       cp_cv_id => $::form->{"$::form->{vc}_id"} * 1,
 
 326         cp_id    => $::form->{cp_id} * 1
 
 330   $TMPL_VAR{department_labels}     = sub { "$_[0]->{description}--$_[0]->{id}" };
 
 333   $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
 
 334   $TMPL_VAR{vclimit} = $myconfig{vclimit};
 
 335   $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('customer', '', 0, 0)";
 
 336   push @custom_hiddens, "customer_id";
 
 337   push @custom_hiddens, "oldcustomer";
 
 338   push @custom_hiddens, "selectcustomer";
 
 340   # currencies and exchangerate
 
 341   my @values = map { $_       } @{ $form->{ALL_CURRENCIES} };
 
 342   my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
 
 343   $form->{currency}            = $form->{defaultcurrency} unless $form->{currency};
 
 344   $form->{show_exchangerate}   = $form->{currency} ne $form->{defaultcurrency};
 
 345   $TMPL_VAR{currencies}        = NTI($::request->{cgi}->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
 
 346                                                       '-values' => \@values, '-labels' => \%labels,
 
 347                                                       '-onchange' => "document.getElementById('update_button').click();"
 
 348                                      )) if scalar @values;
 
 349   push @custom_hiddens, "forex";
 
 350   push @custom_hiddens, "exchangerate" if $form->{forex};
 
 352   $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
 
 353   $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
 
 355   $::request->{layout}->focus('#customer');
 
 357   my $follow_up_vc         =  $form->{customer};
 
 358   $follow_up_vc            =~ s/--\d*\s*$//;
 
 359   $TMPL_VAR{customer_name} = $follow_up_vc;
 
 361 # set option selected
 
 362   foreach my $item (qw(AR)) {
 
 363     $form->{"select$item"} =~ s/ selected//;
 
 364     $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
 
 367   $TMPL_VAR{is_type_credit_note} = $form->{type}   eq "credit_note";
 
 368   $TMPL_VAR{is_format_html}      = $form->{format} eq 'html';
 
 369   $TMPL_VAR{dateformat}          = $myconfig{dateformat};
 
 370   $TMPL_VAR{numberformat}        = $myconfig{numberformat};
 
 373   $TMPL_VAR{HIDDENS} = [qw(
 
 374     id action type media format queued printed emailed title vc discount
 
 375     title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
 
 376     max_dunning_level dunning_amount
 
 377     shiptoname shiptostreet shiptozipcode shiptocity shiptocountry  shiptocontact shiptophone shiptofax
 
 378     shiptoemail shiptodepartment_1 shiptodepartment_2  shiptocp_gender message email subject cc bcc taxaccounts cursor_fokus
 
 379     convert_from_do_ids convert_from_oe_ids
 
 381   map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
 
 383   $form->{jsscript} = 1;
 
 384   $::request->layout->use_stylesheet('presenter/record/record_list.css');
 
 387   print $form->parse_html_template("is/form_header", \%TMPL_VAR);
 
 389   $main::lxdebug->leave_sub();
 
 393   $main::lxdebug->enter_sub();
 
 395   my $form     = $main::form;
 
 396   my %myconfig = %main::myconfig;
 
 397   my $locale   = $main::locale;
 
 399   $main::auth->assert('invoice_edit');
 
 401   $form->{invtotal}    = $form->{invsubtotal};
 
 404   $form->{rows} = max 2,
 
 405     $form->numtextrows($form->{notes},    26, 8),
 
 406     $form->numtextrows($form->{intnotes}, 35, 8);
 
 409   # tax, total and subtotal calculations
 
 410   my ($tax, $subtotal);
 
 411   $form->{taxaccounts_array} = [ split(/ /, $form->{taxaccounts}) ];
 
 413   if ( $form->{type} =~ /sales_(order|quotation)/ && $form->{vc} eq 'customer' && !$form->{taxincluded_changed_by_user} ) {
 
 421     my $res = selectfirst_hashref_query($::form, $::form->get_standard_dbh(), $query, conv_i($::form->{customer_id}));
 
 423     $form->{taxincluded} = ($res && defined($res->{taxincluded_checked})) ? $res->{taxincluded_checked} : $myconfig{taxincluded_checked};
 
 426   foreach my $item (@{ $form->{taxaccounts_array} }) {
 
 427     if ($form->{"${item}_base"}) {
 
 428       if ($form->{taxincluded}) {
 
 429         $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"}
 
 430                                                                                  / (1 + $form->{"${item}_rate"})), 2);
 
 431         $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
 
 433         $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
 
 434         $form->{invtotal} += $form->{"${item}_total"};
 
 441     $form->{follow_ups}            = FU->follow_ups('trans_id' => $form->{id}) || [];
 
 442     $form->{follow_ups_unfinished} = ( sum map { $_->{due} * 1 } @{ $form->{follow_ups} } ) || 0;
 
 447   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
 
 448   $form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];
 
 450   # Standard Konto für Umlaufvermögen
 
 451   my $accno_arap = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
 
 453   for my $i (1 .. $form->{paidaccounts}) {
 
 454     $form->{"changeable_$i"} = 1;
 
 455     if (SL::DB::Default->get->payments_changeable == 0) {
 
 457       $form->{"changeable_$i"} = ($form->{"acc_trans_id_$i"})? 0 : 1;
 
 458     } elsif (SL::DB::Default->get->payments_changeable == 2) {
 
 460       $form->{"changeable_$i"} = (($form->{"gldate_$i"} eq '') ||
 
 461                                   ($form->current_date(\%myconfig) eq $form->{"gldate_$i"}));
 
 464     $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
 
 465     if (!$form->{"AR_paid_$i"}) {
 
 466       $form->{"selectAR_paid_$i"} =~ s/option>$accno_arap--(.*?)</option selected>$accno_arap--$1</;
 
 468       $form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
 
 471     $totalpaid += $form->{"paid_$i"};
 
 474   $form->{oldinvtotal} = $form->{invtotal};
 
 476   print $form->parse_html_template('is/form_footer', {
 
 477     is_type_credit_note => ($form->{type} eq "credit_note"),
 
 478     totalpaid           => $totalpaid,
 
 479     paid_missing        => $form->{invtotal} - $totalpaid,
 
 480     print_options       => print_options(inline => 1),
 
 481     show_storno         => $form->{id} && !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar") && !$totalpaid,
 
 482     show_delete         => ($::instance_conf->get_is_changeable == 2)
 
 483                              ? ($form->current_date(\%myconfig) eq $form->{gldate})
 
 484                              : ($::instance_conf->get_is_changeable == 1),
 
 486 ##print $form->parse_html_template('is/_payments'); # parser
 
 487 ##print $form->parse_html_template('webdav/_list'); # parser
 
 489   $main::lxdebug->leave_sub();
 
 493   $main::lxdebug->enter_sub();
 
 495   my $form     = $main::form;
 
 496   my %myconfig = %main::myconfig;
 
 498   $main::auth->assert('invoice_edit');
 
 500   &mark_as_paid_common(\%myconfig,"ar");
 
 502   $main::lxdebug->leave_sub();
 
 506   $main::lxdebug->enter_sub();
 
 508   my $form     = $main::form;
 
 509   my %myconfig = %main::myconfig;
 
 511   $main::auth->assert('invoice_edit');
 
 513   my ($recursive_call) = @_;
 
 515   $form->{print_and_post} = 0         if $form->{second_run};
 
 516   my $taxincluded         = $form->{taxincluded} ? "checked" : '';
 
 519   &check_name("customer");
 
 521   $form->{taxincluded} ||= $taxincluded;
 
 523   if (!$form->{forex}) {        # read exchangerate from input field (not hidden)
 
 524     $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate}) unless $recursive_call;
 
 526   $form->{forex}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
 
 527   $form->{exchangerate} = $form->{forex} if $form->{forex};
 
 529   for my $i (1 .. $form->{paidaccounts}) {
 
 530     next unless $form->{"paid_$i"};
 
 531     map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
 
 532     if (!$form->{"forex_$i"}) {   #read exchangerate from input field (not hidden)
 
 533       $form->{exchangerate} = $form->{"exchangerate_$i"};
 
 535     $form->{"forex_$i"}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
 
 536     $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
 
 539   my $i            = $form->{rowcount};
 
 540   my $exchangerate = $form->{exchangerate} || 1;
 
 542   # if last row empty, check the form otherwise retrieve new item
 
 543   if (   ($form->{"partnumber_$i"} eq "")
 
 544       && ($form->{"description_$i"} eq "")
 
 545       && ($form->{"partsgroup_$i"}  eq "")) {
 
 547     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
 
 552     IS->retrieve_item(\%myconfig, \%$form);
 
 554     my $rows = scalar @{ $form->{item_list} };
 
 556     # Falls kein Kundenrabatt vorhanden ist, den aktuellen Rabatt nicht mit 0% überschreiben,
 
 557     # da hier der Anwender schon manual einen Wert eingetragen haben könnte (analog zu qty) Bugfix: 1412
 
 558     if ($form->{customer_discount}){
 
 559       $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{customer_discount} * 100);
 
 563       $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
 
 567         select_item(mode => 'IS');
 
 572         my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
 
 574         map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
 
 575         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
 
 577         $form->{payment_id}    = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
 
 578         $form->{"discount_$i"} = 0                             if $form->{"not_discountable_$i"};
 
 580         $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
 
 582         ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
 
 583         my $decimalplaces = max 2, length $1;
 
 586           $form->{"sellprice_$i"} = $sellprice;
 
 588           # if there is an exchange rate adjust sellprice
 
 589           $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
 
 590           $form->{"sellprice_$i"} /= $exchangerate;
 
 593         $form->{"listprice_$i"} /= $exchangerate;
 
 595         my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
 
 596         map { $form->{"${_}_base"} = 0 }                                 split / /, $form->{taxaccounts};
 
 597         map { $form->{"${_}_base"} += $amount }                          split / /, $form->{"taxaccounts_$i"};
 
 598         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
 
 600         $form->{creditremaining} -= $amount;
 
 602         map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice lastcost);
 
 604         $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
 
 606         # get pricegroups for parts
 
 607         IS->get_pricegroups_for_parts(\%myconfig, \%$form);
 
 609         # build up html code for prices_$i
 
 617       # ok, so this is a new part
 
 618       # ask if it is a part or service item
 
 620       if (   $form->{"partsgroup_$i"}
 
 621           && ($form->{"partsnumber_$i"} eq "")
 
 622           && ($form->{"description_$i"} eq "")) {
 
 624         $form->{"discount_$i"} = "";
 
 628         $form->{"id_$i"}   = 0;
 
 633   $main::lxdebug->leave_sub();
 
 637   $main::lxdebug->enter_sub();
 
 639   my $form     = $main::form;
 
 640   my %myconfig = %main::myconfig;
 
 641   my $locale   = $main::locale;
 
 643   $main::auth->assert('invoice_edit');
 
 645   my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
 
 647   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 648   for my $i (1 .. $form->{paidaccounts}) {
 
 649     if ($form->{"paid_$i"}) {
 
 650       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
 
 652       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
 655       if ($form->{currency} ne $form->{defaultcurrency}) {
 
 656         $form->{"exchangerate_$i"} = $form->{exchangerate}
 
 657           if ($invdate == $datepaid);
 
 658         $form->isblank("exchangerate_$i",
 
 659                        $locale->text('Exchangerate for payment missing!'));
 
 663   # Abgeschlossene Zeiträume nur für den letzten (aktuellen) Zahlungseingang prüfen
 
 664   # Details s.a. Bug 1502
 
 665   # Das Problem ist jetzt, dass man Zahlungseingänge nachträglich ändern kann
 
 666   # Wobei dies für Installationen die sowieso nicht mit Bücherkontrolle arbeiten keinen
 
 667   # keinen Unterschied macht.
 
 668   # Optimal wäre, wenn gegen einen Zeitstempel des Zahlungsfelds geprüft würde ...
 
 669   # Das Problem hierbei ist, dass in IS.pm post_invoice IMMER alle Zahlungseingänge aus $form
 
 670   # erneut gespeichert werden. Prinzipiell wäre es besser NUR die Änderungen des Rechnungs-
 
 671   # belegs (neue Zahlung aber nichts anderes) zu speichern ...
 
 672   # Vielleicht könnte man ähnlich wie bei Rechnung löschen verfahren
 
 673   $form->error($locale->text('Cannot post payment for a closed period!'))
 
 674     if ($form->date_closed($form->{"datepaid_$form->{paidaccounts}"}, \%myconfig));
 
 676   ($form->{AR})      = split /--/, $form->{AR};
 
 677   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
 
 679   $form->redirect($locale->text('Payment posted!'))
 
 680       if (IS->post_payment(\%myconfig, \%$form));
 
 681     $form->error($locale->text('Cannot post payment!'));
 
 684   $main::lxdebug->leave_sub();
 
 688   $main::lxdebug->enter_sub();
 
 690   my $form     = $main::form;
 
 691   my %myconfig = %main::myconfig;
 
 692   my $locale   = $main::locale;
 
 694   $main::auth->assert('invoice_edit');
 
 696   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 697   $form->isblank("invdate",  $locale->text('Invoice Date missing!'));
 
 698   $form->isblank("customer", $locale->text('Customer missing!'));
 
 699   $form->error($locale->text('Cannot post invoice for a closed period!'))
 
 700         if ($form->date_closed($form->{"invdate"}, \%myconfig));
 
 702   $form->{invnumber} =~ s/^\s*//g;
 
 703   $form->{invnumber} =~ s/\s*$//g;
 
 705   # if oldcustomer ne customer redo form
 
 706   if (&check_name('customer')) {
 
 711   if ($myconfig{mandatory_departments} && !$form->{department_id}) {
 
 712     $form->{saved_message} = $::locale->text('You have to specify a department.');
 
 717   if ($form->{second_run}) {
 
 718     $form->{print_and_post} = 0;
 
 721   remove_emptied_rows();
 
 724   my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
 
 725   my $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
 
 727   $form->error($locale->text('Cannot post invoice for a closed period!'))
 
 728     if ($invdate <= $closedto);
 
 730   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
 
 731     if ($form->{currency} ne $form->{defaultcurrency});
 
 733   for my $i (1 .. $form->{paidaccounts}) {
 
 734     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
 
 735       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
 
 737       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
 739       $form->error($locale->text('Cannot post payment for a closed period!'))
 
 740         if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
 
 742       if ($form->{currency} ne $form->{defaultcurrency}) {
 
 743         $form->{"exchangerate_$i"} = $form->{exchangerate}
 
 744           if ($invdate == $datepaid);
 
 745         $form->isblank("exchangerate_$i",
 
 746                        $locale->text('Exchangerate for payment missing!'));
 
 751   ($form->{AR})        = split /--/, $form->{AR};
 
 752   ($form->{AR_paid})   = split /--/, $form->{AR_paid};
 
 753   $form->{storno}    ||= 0;
 
 755   $form->{label} = $form->{type} eq 'credit_note' ? $locale->text('Credit Note') : $locale->text('Invoice');
 
 757   $form->{id} = 0 if $form->{postasnew};
 
 759   # get new invnumber in sequence if no invnumber is given or if posasnew was requested
 
 760   if ($form->{postasnew}) {
 
 761     if ($form->{type} eq "credit_note") {
 
 762       undef($form->{cnnumber});
 
 764       undef($form->{invnumber});
 
 769   $form->error($locale->text('Cannot post invoice!'))
 
 770     unless IS->post_invoice(\%myconfig, \%$form);
 
 771   remove_draft() if $form->{remove_draft};
 
 773   if(!exists $form->{addition}) {
 
 774     $form->{snumbers} =  'invnumber' .'_'. $form->{invnumber}; # ($form->{type} eq 'credit_note' ? 'cnnumber' : 'invnumber') .'_'. $form->{invnumber};
 
 775     $form->{addition} = $form->{print_and_post} ? "PRINTED AND POSTED" :
 
 776                         $form->{storno}         ? "STORNO"             :
 
 781   if (!$form->{no_redirect_after_post}) {
 
 782     $form->{action} = 'edit';
 
 783     $form->{script} = 'is.pl';
 
 784     $form->{saved_message} = $form->{label} . " $form->{invnumber} " . $locale->text('posted!');
 
 785     $form->{callback} = build_std_url(qw(action edit id saved_message));
 
 789   $main::lxdebug->leave_sub();
 
 793   $main::lxdebug->enter_sub();
 
 795   my $form     = $main::form;
 
 797   $main::auth->assert('invoice_edit');
 
 799   my $old_form                    = new Form;
 
 800   $form->{no_redirect_after_post} = 1;
 
 801   $form->{print_and_post}         = 1;
 
 805   $main::lxdebug->leave_sub();
 
 810   $main::lxdebug->enter_sub();
 
 812   my $form     = $main::form;
 
 813   my %myconfig = %main::myconfig;
 
 815   $main::auth->assert('invoice_edit');
 
 817   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 locked)};
 
 818   $form->{paidaccounts} = 1;
 
 820   $form->{invdate} = $form->current_date(\%myconfig);
 
 821   $form->{duedate} = $form->get_duedate(\%myconfig, $form->{invdate}) || $form->{invdate};
 
 823   # remember pricegroups for "use as new"
 
 824   IS->get_pricegroups_for_parts(\%myconfig, \%$form);
 
 825   set_pricegroup($_) for 1 .. $form->{rowcount};
 
 829   $main::lxdebug->leave_sub();
 
 833   $main::lxdebug->enter_sub();
 
 835   my $form     = $main::form;
 
 836   my %myconfig = %main::myconfig;
 
 837   my $locale   = $main::locale;
 
 839   $main::auth->assert('invoice_edit');
 
 841   if ($form->{storno}) {
 
 842     $form->error($locale->text('Cannot storno storno invoice!'));
 
 845   if (IS->has_storno(\%myconfig, $form, "ar")) {
 
 846     $form->error($locale->text("Invoice has already been storno'd!"));
 
 849   map({ my $key = $_; delete($form->{$key}) unless (grep({ $key eq $_ } qw(id login password type))); } keys(%{ $form }));
 
 855   # Payments must not be recorded for the new storno invoice.
 
 856   $form->{paidaccounts} = 0;
 
 857   map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ gldate_ acc_trans_id_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
 
 859   $form->{storno_id} = $form->{id};
 
 862   $form->{invnumber} = "Storno zu " . $form->{invnumber};
 
 866   $main::lxdebug->leave_sub();
 
 870   $main::lxdebug->enter_sub();
 
 872   my $form     = $main::form;
 
 874   $main::auth->assert('invoice_edit');
 
 876   $form->{preview} = 1;
 
 877   my $old_form = new Form;
 
 878   for (keys %$form) { $old_form->{$_} = $form->{$_} }
 
 880   &print_form($old_form);
 
 881   $main::lxdebug->leave_sub();
 
 886   $main::lxdebug->enter_sub();
 
 888   my $form     = $main::form;
 
 889   my $locale   = $main::locale;
 
 891   $main::auth->assert('invoice_edit');
 
 893   if ($form->{second_run}) {
 
 894     $form->{print_and_post} = 0;
 
 899 <form method="post" action="$form->{script}">
 
 902   # delete action variable
 
 903   map { delete $form->{$_} } qw(action header);
 
 905   foreach my $key (keys %$form) {
 
 906     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
 
 907     $form->{$key} =~ s/\"/"/g;
 
 908     print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
 
 912 <h2 class="confirm">| . $locale->text('Confirm!') . qq|</h2>
 
 915     . $locale->text('Are you sure you want to delete Invoice Number')
 
 916     . qq| $form->{invnumber}
 
 920 <input name="action" class="submit" type="submit" value="|
 
 921     . $locale->text('Yes') . qq|">
 
 925   $main::lxdebug->leave_sub();
 
 929   $main::lxdebug->enter_sub();
 
 931   my $form     = $main::form;
 
 932   my %myconfig = %main::myconfig;
 
 933   my $locale   = $main::locale;
 
 935   $main::auth->assert('invoice_edit');
 
 937   $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
 
 939     $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
 
 946   $form->{title}  = $locale->text('Add Credit Note');
 
 947   $form->{script} = 'is.pl';
 
 949   # Bei Gutschriften bezug zur Rechnungsnummer
 
 950   $form->{invnumber_for_credit_note} = $form->{invnumber};
 
 951   # bo creates the id, reset it
 
 952   map { delete $form->{$_} }
 
 953     qw(id invnumber subject message cc bcc printed emailed queued);
 
 954   $form->{ $form->{vc} } =~ s/--.*//g;
 
 955   $form->{type} = "credit_note";
 
 958   map { $form->{"select$_"} = "" } ($form->{vc}, 'currency');
 
 960 #  map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
 
 961 #    qw(creditlimit creditremaining);
 
 963   for my $i (1 .. $form->{rowcount}) {
 
 964     for (qw(listprice)) {
 
 965       $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
 
 969   my $currency = $form->{currency};
 
 972   $form->{currency}     = $currency;
 
 973   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, 'buy');
 
 974   $form->{exchangerate} = $form->{forex} || '';
 
 976   $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
 
 978   # bei Gutschriften werden Zahlungseingänge aus Rechnung nicht übernommen
 
 979   for my $i (1 .. $form->{paidaccounts}) {
 
 980     delete $form->{"paid_$i"};
 
 981     delete $form->{"source_$i"};
 
 982     delete $form->{"memo_$i"};
 
 983     delete $form->{"datepaid_$i"};
 
 984     delete $form->{"gldate_$i"};
 
 985     delete $form->{"acc_trans_id_$i"};
 
 986     delete $form->{"AR_paid_$i"};
 
 988   $form->{paidaccounts} = 1;
 
 995   $main::lxdebug->leave_sub();
 
 999   $main::lxdebug->enter_sub();
 
1001   my $form     = $main::form;
 
1002   my %myconfig = %main::myconfig;
 
1003   my $locale   = $main::locale;
 
1005   $main::auth->assert('invoice_edit');
 
1007   if (IS->delete_invoice(\%myconfig, \%$form)) {
 
1008     # saving the history
 
1009     if(!exists $form->{addition}) {
 
1010       $form->{snumbers} = 'invnumber' .'_'. $form->{invnumber}; # ($form->{type} eq 'credit_note' ? 'cnnumber' : 'invnumber') .'_'. $form->{invnumber};
 
1011       $form->{addition} = "DELETED";
 
1012       $form->save_history;
 
1014     # /saving the history
 
1015     $form->redirect($locale->text('Invoice deleted!'));
 
1017   $form->error($locale->text('Cannot delete invoice!'));
 
1019   $main::lxdebug->leave_sub();
 
1023   $main::lxdebug->enter_sub();
 
1025   my $form     = $main::form;
 
1027   $main::auth->assert('invoice_edit');
 
1030     $form->{no_redirect_after_post} = 1;
 
1032     my $saved_form = save_form();
 
1036     restore_form($saved_form, 0, qw(id invnumber));
 
1041   $main::lxdebug->leave_sub();