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 #======================================================================
 
  38 use List::Util qw(max sum);
 
  40 require "bin/mozilla/io.pl";
 
  41 require "bin/mozilla/invoice_io.pl";
 
  42 require "bin/mozilla/arap.pl";
 
  43 require "bin/mozilla/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};
 
 151   if ($form->{payment_id}) {
 
 152     $payment_id = $form->{payment_id};
 
 155   if ($form->{language_id}) {
 
 156     $language_id = $form->{language_id};
 
 159   if ($form->{taxzone_id}) {
 
 160     $taxzone_id = $form->{taxzone_id};
 
 167   if ($form->{shipto_id}) {
 
 168     $shipto_id = $form->{shipto_id};
 
 171   my $cp_id = $form->{cp_id};
 
 172   IS->get_customer(\%myconfig, \%$form);
 
 174   #quote all_customer Bug 133
 
 175   foreach my $ref (@{ $form->{all_customer} }) {
 
 176     $ref->{name} = $form->quote($ref->{name});
 
 181   IS->retrieve_invoice(\%myconfig, \%$form);
 
 182   $form->{cp_id} = $cp_id;
 
 185     $form->{payment_id} = $payment_id;
 
 188     $form->{language_id} = $language_id;
 
 191     $form->{taxzone_id} = $taxzone_id;
 
 194     $form->{shipto_id} = $shipto_id;
 
 197   $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
 
 198   $form->{selectcustomer} = 1;
 
 201   if ($form->{all_departments}) {
 
 202     $form->{selectdepartment} = "<option>\n";
 
 203     $form->{department}       = "$form->{department}--$form->{department_id}";
 
 206       $form->{selectdepartment} .=
 
 207         "<option>$_->{description}--$_->{id}</option>\n"
 
 208     } (@{ $form->{all_departments} });
 
 211   $form->{employee} = "$form->{employee}--$form->{employee_id}";
 
 214   $form->{forex} = $form->{exchangerate};
 
 215   my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
 
 217   foreach my $key (keys %{ $form->{AR_links} }) {
 
 218     foreach my $ref (@{ $form->{AR_links}{$key} }) {
 
 219       $form->{"select$key"} .=
 
 220 "<option>$ref->{accno}--$ref->{description}</option>\n";
 
 223     if ($key eq "AR_paid") {
 
 224       next unless $form->{acc_trans}{$key};
 
 225       for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
 
 226         $form->{"AR_paid_$i"} =
 
 227           "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
 
 230         $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
 
 231         $form->{"datepaid_$i"} =
 
 232           $form->{acc_trans}{$key}->[$i - 1]->{transdate};
 
 233         $form->{"forex_$i"} = $form->{"exchangerate_$i"} =
 
 234           $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
 
 235         $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
 
 236         $form->{"memo_$i"}   = $form->{acc_trans}{$key}->[$i - 1]->{memo};
 
 238         $form->{paidaccounts} = $i;
 
 242         "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
 
 247   $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
 
 249   $form->{AR} = $form->{AR_1} unless $form->{id};
 
 252     ($form->datetonum($form->{invdate}, \%myconfig) <=
 
 253      $form->datetonum($form->{closedto}, \%myconfig));
 
 255   $main::lxdebug->leave_sub();
 
 258 sub prepare_invoice {
 
 259   $main::lxdebug->enter_sub();
 
 261   my $form     = $main::form;
 
 262   my %myconfig = %main::myconfig;
 
 264   $main::auth->assert('invoice_edit');
 
 266   if ($form->{type} eq "credit_note") {
 
 267     $form->{type}     = "credit_note";
 
 268     $form->{formname} = "credit_note";
 
 270     $form->{type}     = "invoice";
 
 271     $form->{formname} = "invoice";
 
 278     foreach my $ref (@{ $form->{invoice_details} }) {
 
 281       map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
 
 283       $form->{"discount_$i"}   = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
 
 284       my ($dec)                = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
 
 286       my $decimalplaces        = ($dec > 2) ? $dec : 2;
 
 288       $form->{"sellprice_$i"}  = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
 
 289       (my $dec_qty)            = ($form->{"qty_$i"} =~ /\.(\d+)/);
 
 290       $dec_qty                 = length $dec_qty;
 
 292       $form->{"qty_$i"}        = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
 
 294       $form->{rowcount}        = $i;
 
 298     # get pricegroups for parts
 
 299     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
 
 300     set_pricegroup($_) for 1 .. $form->{rowcount};
 
 302   $main::lxdebug->leave_sub();
 
 306   $main::lxdebug->enter_sub();
 
 308   my $form     = $main::form;
 
 309   my %myconfig = %main::myconfig;
 
 310   my $locale   = $main::locale;
 
 311   my $cgi      = $main::cgi;
 
 313   $main::auth->assert('invoice_edit');
 
 318   $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
 
 319   $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
 
 321   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 322   $form->{radier}          = ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
 
 324   $payment = qq|<option value=""></option>|;
 
 325   foreach my $item (@{ $form->{payment_terms} }) {
 
 326     if ($form->{payment_id} eq $item->{id}) {
 
 327       $payment .= qq|<option value="$item->{id}" selected>$item->{description}</option>|;
 
 329       $payment .= qq|<option value="$item->{id}">$item->{description}</option>|;
 
 333   my $set_duedate_url = "$form->{script}?action=set_duedate";
 
 335   push @ { $form->{AJAX} }, new CGI::Ajax( 'set_duedate' => $set_duedate_url );
 
 337   my @old_project_ids = ($form->{"globalproject_id"});
 
 338   map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
 
 340   $form->get_lists("contacts"      => "ALL_CONTACTS",
 
 341                    "shipto"        => "ALL_SHIPTO",
 
 342                    "projects"      => { "key"    => "ALL_PROJECTS",
 
 344                                         "old_id" => \@old_project_ids },
 
 345                    "employees"     => "ALL_EMPLOYEES",
 
 346                    "salesmen"      => "ALL_SALESMEN",
 
 347                    "taxzones"      => "ALL_TAXZONES",
 
 348                    "currencies"    => "ALL_CURRENCIES",
 
 349                    "customers"     => "ALL_CUSTOMERS",
 
 350                    "price_factors" => "ALL_PRICE_FACTORS");
 
 352   $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
 
 353   $TMPL_VAR{shipto_labels}         = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
 
 354   $TMPL_VAR{contact_labels}        = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
 
 355   $TMPL_VAR{department_labels}     = sub { "$_[0]->{description}--$_[0]->{id}" };
 
 358   $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
 
 359   $TMPL_VAR{vclimit} = $myconfig{vclimit};
 
 360   $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('customer', '', 0, 0)";
 
 361   push @custom_hiddens, "customer_id";
 
 362   push @custom_hiddens, "oldcustomer";
 
 363   push @custom_hiddens, "selectcustomer";
 
 365   # currencies and exchangerate
 
 366   my @values = map { $_       } @{ $form->{ALL_CURRENCIES} };
 
 367   my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
 
 368   $form->{currency}            = $form->{defaultcurrency} unless $form->{currency};
 
 369   $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
 
 370   $TMPL_VAR{currencies}        = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
 
 371                                                       '-values' => \@values, '-labels' => \%labels)) if scalar @values;
 
 372   push @custom_hiddens, "forex";
 
 373   push @custom_hiddens, "exchangerate" if $form->{forex};
 
 375   $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
 
 376   $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
 
 378   $form->{fokus} = "invoice.customer";
 
 380   my $follow_up_vc         =  $form->{customer};
 
 381   $follow_up_vc            =~ s/--\d*\s*$//;
 
 382   $TMPL_VAR{customer_name} = $follow_up_vc;
 
 384 # set option selected
 
 385   foreach my $item (qw(AR)) {
 
 386     $form->{"select$item"} =~ s/ selected//;
 
 387     $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
 
 390   $TMPL_VAR{is_type_credit_note} = $form->{type}   eq "credit_note";
 
 391   $TMPL_VAR{is_format_html}      = $form->{format} eq 'html';
 
 392   $TMPL_VAR{dateformat}          = $myconfig{dateformat};
 
 393   $TMPL_VAR{numberformat}        = $myconfig{numberformat};
 
 396   $TMPL_VAR{HIDDEN} = [qw(
 
 397     id action type media format queued printed emailed title vc discount
 
 398     creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
 
 399     max_dunning_level dunning_amount
 
 400     shiptoname shiptostreet shiptozipcode shiptocity shiptocountry  shiptocontact shiptophone shiptofax
 
 401     shiptoemail shiptodepartment_1 shiptodepartment_2 message email subject cc bcc taxaccounts cursor_fokus
 
 402     convert_from_do_ids convert_from_oe_ids
 
 404   map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
 
 406   $form->{jsscript} = 1;
 
 409   print $form->parse_html_template("is/form_header", \%TMPL_VAR);
 
 411   $main::lxdebug->leave_sub();
 
 415   $main::lxdebug->enter_sub();
 
 417   my $form     = $main::form;
 
 418   my %myconfig = %main::myconfig;
 
 419   my $locale   = $main::locale;
 
 420   my $cgi      = $main::cgi;
 
 422   $main::auth->assert('invoice_edit');
 
 424   $form->{invtotal} = $form->{invsubtotal};
 
 426   my ($rows, $introws);
 
 427   if (($rows = $form->numtextrows($form->{notes}, 26, 8)) < 2) {
 
 430   if (($introws = $form->numtextrows($form->{intnotes}, 35, 8)) < 2) {
 
 433   $rows = ($rows > $introws) ? $rows : $introws;
 
 434   my $notes = qq|<textarea name="notes" rows="$rows" cols="26" wrap="soft">$form->{notes}</textarea>|;
 
 435   my $intnotes = qq|<textarea name="intnotes" rows="$rows" cols="35" wrap="soft">$form->{intnotes}</textarea>|;
 
 437   $form->{taxincluded} = ($form->{taxincluded} ? "checked" : "");
 
 439   my $taxincluded = "";
 
 440   if ($form->{taxaccounts}) {
 
 441     $taxincluded = qq| <input name="taxincluded" class="checkbox" type="checkbox" $form->{taxincluded}> <b>| . $locale->text('Tax Included') . qq|</b><br><br>|;
 
 444   my ($tax, $subtotal);
 
 445   if (!$form->{taxincluded}) {
 
 447     foreach my $item (split / /, $form->{taxaccounts}) {
 
 448       if ($form->{"${item}_base"}) {
 
 449         $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
 
 450         $form->{invtotal} += $form->{"${item}_total"};
 
 451         $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
 
 455                 <th align="right">$form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
 
 456                 <td align="right">$form->{"${item}_total"}</td>
 
 462     $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
 
 466                 <th align="right">| . $locale->text('Subtotal') . qq|</th>
 
 467                 <td align="right">$form->{invsubtotal}</td>
 
 473   if ($form->{taxincluded}) {
 
 474     foreach my $item (split / /, $form->{taxaccounts}) {
 
 475       if ($form->{"${item}_base"}) {
 
 476         $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"} / (1 + $form->{"${item}_rate"})), 2);
 
 477         $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
 
 478         $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
 
 479         $form->{"${item}_netto"} = $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
 
 483                 <th align="right">Enthaltene $form->{"${item}_description"} |
 
 484                                     . $form->{"${item}_rate"} * 100 .qq|%</th>
 
 485                 <td align="right">$form->{"${item}_total"}</td>
 
 488                 <th align="right">Nettobetrag</th>
 
 489                 <td align="right">$form->{"${item}_netto"}</td>
 
 497   $form->{oldinvtotal} = $form->{invtotal};
 
 498   $form->{invtotal}    = $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
 
 500   my $follow_ups_block;
 
 502     my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
 
 504     if (@{ $follow_ups} ) {
 
 505       my $num_due       = sum map { $_->{due} * 1 } @{ $follow_ups };
 
 506       $follow_ups_block = qq|
 
 508         <td colspan="2">| . $locale->text("There are #1 unfinished follow-ups of which #2 are due.", scalar @{ $follow_ups }, $num_due) . qq|</td>
 
 522                 <th align="left">| . $locale->text('Notes') . qq|</th>
 
 523                 <th align="left">| . $locale->text('Internal Notes') . qq|</th>
 
 524                 <th align="right">| . $locale->text('Payment Terms') . qq|</th>
 
 529                 <td><select name="payment_id" onChange="if (this.value) set_duedate(['payment_id__' + this.value, 'invdate__' + invdate.value],['duedate'])">$payment
 
 538               <th  align=left>| . $locale->text('Ertrag') . qq|</th>
 
 539               <td>| .  $form->format_amount(\%myconfig, $form->{marge_total}, 2, 0) . qq|</td>
 
 542               <th  align=left>| . $locale->text('Ertrag prozentual') . qq|</th>
 
 543               <td>| .  $form->format_amount(\%myconfig, $form->{marge_percent}, 2, 0) . qq| %</td>
 
 545             <input type=hidden name="marge_total" value="$form->{"marge_total"}">
 
 546             <input type=hidden name="marge_percent" value="$form->{"marge_percent"}">
 
 555                 <th align="right">| . $locale->text('Total') . qq|</th>
 
 556                 <td align="right">$form->{invtotal}</td>
 
 569     <td><hr size="3" noshade></td>
 
 572     <th class="listtop" align="left">Dokumente im Webdav-Repository</th>
 
 575       <td align="left" width="30%"><b>Dateiname</b></td>
 
 576       <td align="left" width="70%"><b>Webdavlink</b></td>
 
 578     foreach my $file (@{ $form->{WEBDAV} }) {
 
 581         <td align="left">$file->{name}</td>
 
 582         <td align="left"><a href="$file->{link}">$file->{type}</a></td>
 
 593 if ($form->{type} eq "credit_note") {
 
 598         <tr class="listheading">
 
 599           <th colspan="6" class="listheading">|
 
 600     . $locale->text('Payments') . qq|</th>
 
 608         <tr class="listheading">
 
 609           <th colspan="6" class="listheading">|
 
 610     . $locale->text('Incoming Payments') . qq|</th>
 
 616   if ($form->{currency} eq $form->{defaultcurrency}) {
 
 617     @column_index = qw(datepaid source memo paid AR_paid);
 
 619     @column_index = qw(datepaid source memo paid exchangerate AR_paid);
 
 623   $column_data{datepaid}     = "<th>" . $locale->text('Date') . "</th>";
 
 624   $column_data{paid}         = "<th>" . $locale->text('Amount') . "</th>";
 
 625   $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
 
 626   $column_data{AR_paid}      = "<th>" . $locale->text('Account') . "</th>";
 
 627   $column_data{source}       = "<th>" . $locale->text('Source') . "</th>";
 
 628   $column_data{memo}         = "<th>" . $locale->text('Memo') . "</th>";
 
 633   map { print "$column_data{$_}\n" } @column_index;
 
 641   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
 
 642   for my $i (1 .. $form->{paidaccounts}) {
 
 647     $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
 
 648     $form->{"selectAR_paid_$i"} =~
 
 649       s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
 
 652     $totalpaid += $form->{"paid_$i"};
 
 653     if ($form->{"paid_$i"}) {
 
 654       $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
 
 656     $form->{"exchangerate_$i"} = $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
 
 658     if ($form->{"exchangerate_$i"} == 0) {
 
 659       $form->{"exchangerate_$i"} = "";
 
 661     my $exchangerate = qq| |;
 
 662     if ($form->{currency} ne $form->{defaultcurrency}) {
 
 663       if ($form->{"forex_$i"}) {
 
 664         $exchangerate = qq|<input type="hidden" name="exchangerate_$i" value="$form->{"exchangerate_$i"}">$form->{"exchangerate_$i"}|;
 
 666         $exchangerate = qq|<input name="exchangerate_$i" size="10" value="$form->{"exchangerate_$i"}">|;
 
 670     $exchangerate .= qq|<input type="hidden" name="forex_$i" value="$form->{"forex_$i"}">|;
 
 672     $column_data{"paid_$i"} =
 
 673       qq|<td align="center"><input name="paid_$i" size="11" value="$form->{"paid_$i"}" onBlur=\"check_right_number_format(this)\"></td>|;
 
 674     $column_data{"exchangerate_$i"} = qq|<td align="center">$exchangerate</td>|;
 
 675     $column_data{"AR_paid_$i"}      =
 
 676       qq|<td align="center"><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
 
 677     $column_data{"datepaid_$i"} =
 
 678       qq|<td align="center"><input id="datepaid_$i" name="datepaid_$i"  size="11" title="$myconfig{dateformat}" value="$form->{"datepaid_$i"}" onBlur=\"check_right_date_format(this)\">
 
 679          <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
 
 680     $column_data{"source_$i"} =
 
 681       qq|<td align=center><input name="source_$i" size="11" value="$form->{"source_$i"}"></td>|;
 
 682     $column_data{"memo_$i"} =
 
 683       qq|<td align="center"><input name="memo_$i" size="11" value="$form->{"memo_$i"}"></td>|;
 
 685     map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
 
 688     push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
 
 691   my $paid_missing = $form->{oldinvtotal} - $totalpaid;
 
 697       <td align="center">| . $locale->text('Total') . qq|</td>
 
 698       <td align="center">| . H($form->format_amount(\%myconfig, $totalpaid, 2)) . qq|</td>
 
 703       <td align="center">| . $locale->text('Missing amount') . qq|</td>
 
 704       <td align="center">| . H($form->format_amount(\%myconfig, $paid_missing, 2)) . qq|</td>
 
 708   map({ print($cgi->hidden("-name" => $_, "-value" => $form->{$_})); } qw(paidaccounts selectAR_paid oldinvtotal));
 
 709   print qq|<input type="hidden" name="oldtotalpaid" value="$totalpaid">
 
 714     <td><hr size="3" noshade></td>
 
 728   my $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
 
 729   my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
 
 732     my $show_storno = !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar") && (($totalpaid == 0) || ($totalpaid eq ""));
 
 735     <input class="submit" type="submit" accesskey="u" name="action" id="update_button" value="| . $locale->text('Update') . qq|">
 
 736     <input class="submit" type="submit" name="action" value="| . $locale->text('Ship to') . qq|">
 
 737     <input class="submit" type="submit" name="action" value="| . $locale->text('Print') . qq|">
 
 738     <input class="submit" type="submit" name="action" value="| . $locale->text('E-mail') . qq|"> |;
 
 739     print qq|<input class="submit" type="submit" name="action" value="| . $locale->text('Storno') . qq|"> | if ($show_storno);
 
 740     print qq|<input class="submit" type="submit" name="action" value="| . $locale->text('Post Payment') . qq|"> |;
 
 741     print qq|<input class="submit" type="submit" name="action" value="| . $locale->text('Use As Template') . qq|"> |;
 
 742     if ($form->{id} && !($form->{type} eq "credit_note")) {
 
 743       print qq| <input class="submit" type="submit" name="action" value="| . $locale->text('Credit Note') . qq|"> |;
 
 745     if ($form->{radier}) {
 
 746       print qq| <input class="submit" type="submit" name="action" value="| . $locale->text('Delete') . qq|"> |;
 
 750     if ($invdate > $closedto) {
 
 751       print qq| <input class="submit" type="submit" name="action" value="| . $locale->text('Order') . qq|"> |;
 
 754     print qq| <input type="button" class="submit" onclick="follow_up_window()" value="| . $locale->text('Follow-Up') . qq|">|;
 
 757     if ($invdate > $closedto) {
 
 759       <input class="submit" type="submit" name="action" id="update_button" value="| . $locale->text('Update') . qq|">
 
 760       <input class="submit" type="submit" name="action" value="| . $locale->text('Ship to') . qq|">
 
 761       <input class="submit" type="submit" name="action" value="| . $locale->text('Preview') . qq|">
 
 762       <input class="submit" type="submit" name="action" value="| . $locale->text('E-mail') . qq|">
 
 763       <input class="submit" type="submit" name="action" value="| . $locale->text('Print and Post') . qq|">
 
 764       <input class="submit" type="submit" name="action" value="| . $locale->text('Post') . qq|"> | .
 
 765         NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'), '-class' => 'submit'));
 
 769   # button for saving history
 
 770   if($form->{id} ne "") {
 
 772           <input type="button" class="submit" onclick="set_history_window(|
 
 774           . qq|);" name="history" id="history" value="|
 
 775           . $locale->text('history')
 
 778   # /button for saving history
 
 780   # mark_as_paid button
 
 781   if($form->{id} ne "") {
 
 782     print qq|<input type="submit" class="submit" name="action" value="| . $locale->text('mark as paid') . qq|">|;
 
 784   # /mark_as_paid button
 
 785   print $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
 
 788 <input type="hidden" name="rowcount" value="$form->{rowcount}">
 
 790 $cgi->hidden("-name" => "callback", "-value" => $form->{callback})
 
 791 . $cgi->hidden('-name' => 'draft_id', '-default' => [$form->{draft_id}])
 
 792 . $cgi->hidden('-name' => 'draft_description', '-default' => [$form->{draft_description}])
 
 793 . $cgi->hidden('-name' => 'customer_discount', '-value' => [$form->{customer_discount}])
 
 802   $main::lxdebug->leave_sub();
 
 806   $main::lxdebug->enter_sub();
 
 808   my $form     = $main::form;
 
 809   my %myconfig = %main::myconfig;
 
 811   $main::auth->assert('invoice_edit');
 
 813   &mark_as_paid_common(\%myconfig,"ar");
 
 815   $main::lxdebug->leave_sub();
 
 819   $main::lxdebug->enter_sub();
 
 821   my $form     = $main::form;
 
 822   my %myconfig = %main::myconfig;
 
 824   $main::auth->assert('invoice_edit');
 
 826   my ($recursive_call) = shift;
 
 828   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining) unless $recursive_call;
 
 830   $form->{print_and_post} = 0         if $form->{second_run};
 
 831   my $taxincluded            = "checked" if $form->{taxincluded};
 
 834   &check_name("customer");
 
 836   $form->{taxincluded} ||= $taxincluded;
 
 838   $form->{forex}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
 
 839   $form->{exchangerate} = $form->{forex} if $form->{forex};
 
 841   for my $i (1 .. $form->{paidaccounts}) {
 
 842     next unless $form->{"paid_$i"};
 
 843     map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
 
 844     $form->{"forex_$i"}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
 
 845     $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
 
 848   my $i            = $form->{rowcount};
 
 849   my $exchangerate = $form->{exchangerate} || 1;
 
 851   # if last row empty, check the form otherwise retrieve new item
 
 852   if (   ($form->{"partnumber_$i"} eq "")
 
 853       && ($form->{"description_$i"} eq "")
 
 854       && ($form->{"partsgroup_$i"}  eq "")) {
 
 856     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
 
 861     IS->retrieve_item(\%myconfig, \%$form);
 
 863     my $rows = scalar @{ $form->{item_list} };
 
 865     $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{customer_discount} * 100);
 
 868       $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
 
 877         my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
 
 879         map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
 
 880         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
 
 882         $form->{payment_id}    = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
 
 883         $form->{"discount_$i"} = 0                             if $form->{"not_discountable_$i"};
 
 885         $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
 
 887         ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
 
 888         my $decimalplaces = max 2, length $1;
 
 891           $form->{"sellprice_$i"} = $sellprice;
 
 893           # if there is an exchange rate adjust sellprice
 
 894           $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
 
 895           $form->{"sellprice_$i"} /= $exchangerate;
 
 898         $form->{"listprice_$i"} /= $exchangerate;
 
 900         my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
 
 901         map { $form->{"${_}_base"} = 0 }                                 split / /, $form->{taxaccounts};
 
 902         map { $form->{"${_}_base"} += $amount }                          split / /, $form->{"taxaccounts_$i"};
 
 903         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
 
 905         $form->{creditremaining} -= $amount;
 
 907         map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice listprice);
 
 909         $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
 
 911         if ($main::lizenzen) {
 
 912           if ($form->{"inventory_accno_$i"} ne "") {
 
 913             $form->{"lizenzen_$i"} = qq|<option></option>|;
 
 914             foreach my $item (@{ $form->{LIZENZEN}{ $form->{"id_$i"} } }) {
 
 915               $form->{"lizenzen_$i"} .= qq|<option value="$item->{"id"}">$item->{"licensenumber"}</option>|;
 
 917             $form->{"lizenzen_$i"} .= qq|<option value=-1>Neue Lizenz</option>|;
 
 921         # get pricegroups for parts
 
 922         IS->get_pricegroups_for_parts(\%myconfig, \%$form);
 
 924         # build up html code for prices_$i
 
 932       # ok, so this is a new part
 
 933       # ask if it is a part or service item
 
 935       if (   $form->{"partsgroup_$i"}
 
 936           && ($form->{"partsnumber_$i"} eq "")
 
 937           && ($form->{"description_$i"} eq "")) {
 
 939         $form->{"discount_$i"} = "";
 
 943         $form->{"id_$i"}   = 0;
 
 948   $main::lxdebug->leave_sub();
 
 952   $main::lxdebug->enter_sub();
 
 954   my $form     = $main::form;
 
 955   my %myconfig = %main::myconfig;
 
 956   my $locale   = $main::locale;
 
 958   $main::auth->assert('invoice_edit');
 
 962   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 963   for my $i (1 .. $form->{paidaccounts}) {
 
 964     if ($form->{"paid_$i"}) {
 
 965       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
 
 967       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
 969       $form->error($locale->text('Cannot post payment for a closed period!'))
 
 970         if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
 
 972       if ($form->{currency} ne $form->{defaultcurrency}) {
 
 973         $form->{"exchangerate_$i"} = $form->{exchangerate}
 
 974           if ($invdate == $datepaid);
 
 975         $form->isblank("exchangerate_$i",
 
 976                        $locale->text('Exchangerate for payment missing!'));
 
 981   ($form->{AR})      = split /--/, $form->{AR};
 
 982   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
 
 984   $form->redirect($locale->text('Payment posted!'))
 
 985       if (IS->post_payment(\%myconfig, \%$form));
 
 986     $form->error($locale->text('Cannot post payment!'));
 
 989   $main::lxdebug->leave_sub();
 
 993   $main::lxdebug->enter_sub();
 
 995   my $form     = $main::form;
 
 996   my %myconfig = %main::myconfig;
 
 997   my $locale   = $main::locale;
 
 999   $main::auth->assert('invoice_edit');
 
1001   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
1002   $form->isblank("invdate",  $locale->text('Invoice Date missing!'));
 
1003   $form->isblank("customer", $locale->text('Customer missing!'));
 
1004   $form->error($locale->text('Cannot post invoice for a closed period!'))
 
1005         if ($form->date_closed($form->{"invdate"}, \%myconfig));
 
1007   $form->{invnumber} =~ s/^\s*//g;
 
1008   $form->{invnumber} =~ s/\s*$//g;
 
1010   # if oldcustomer ne customer redo form
 
1011   if (&check_name('customer')) {
 
1015   if ($form->{second_run}) {
 
1016     $form->{print_and_post} = 0;
 
1021   my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
 
1022   my $invdate  = $form->datetonum($form->{invdate},  \%myconfig);
 
1024   $form->error($locale->text('Cannot post invoice for a closed period!'))
 
1025     if ($invdate <= $closedto);
 
1027   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
 
1028     if ($form->{currency} ne $form->{defaultcurrency});
 
1030   for my $i (1 .. $form->{paidaccounts}) {
 
1031     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
 
1032       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
 
1034       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
1036       $form->error($locale->text('Cannot post payment for a closed period!'))
 
1037         if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
 
1039       if ($form->{currency} ne $form->{defaultcurrency}) {
 
1040         $form->{"exchangerate_$i"} = $form->{exchangerate}
 
1041           if ($invdate == $datepaid);
 
1042         $form->isblank("exchangerate_$i",
 
1043                        $locale->text('Exchangerate for payment missing!'));
 
1048   ($form->{AR})        = split /--/, $form->{AR};
 
1049   ($form->{AR_paid})   = split /--/, $form->{AR_paid};
 
1050   $form->{storno}    ||= 0;
 
1052   $form->{label} = $locale->text('Invoice');
 
1054   $form->{id} = 0 if $form->{postasnew};
 
1056   # get new invnumber in sequence if no invnumber is given or if posasnew was requested
 
1057   if ($form->{postasnew}) {
 
1058     if ($form->{type} eq "credit_note") {
 
1059       undef($form->{cnnumber});
 
1061       undef($form->{invnumber});
 
1066   $form->error($locale->text('Cannot post invoice!'))
 
1067     unless IS->post_invoice(\%myconfig, \%$form);
 
1068   remove_draft() if $form->{remove_draft};
 
1070   if(!exists $form->{addition}) {
 
1071     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
1072     $form->{addition} = $print_post     ? "PRINTED AND POSTED" :
 
1073                         $form->{storno} ? "STORNO"             :
 
1075     $form->save_history($form->dbconnect(\%myconfig));
 
1078   $form->redirect( $form->{label} . " $form->{invnumber} " . $locale->text('posted!'))
 
1081   $main::lxdebug->leave_sub();
 
1084 sub print_and_post {
 
1085   $main::lxdebug->enter_sub();
 
1087   my $form     = $main::form;
 
1089   $main::auth->assert('invoice_edit');
 
1091   my $old_form               = new Form;
 
1093   $form->{print_and_post} = 1;
 
1097   $main::lxdebug->leave_sub();
 
1101 sub use_as_template {
 
1102   $main::lxdebug->enter_sub();
 
1104   my $form     = $main::form;
 
1105   my %myconfig = %main::myconfig;
 
1107   $main::auth->assert('invoice_edit');
 
1109   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);
 
1110   $form->{paidaccounts} = 1;
 
1111   $form->{rowcount}--;
 
1112   $form->{invdate} = $form->current_date(\%myconfig);
 
1115   $main::lxdebug->leave_sub();
 
1119   $main::lxdebug->enter_sub();
 
1121   my $form     = $main::form;
 
1122   my %myconfig = %main::myconfig;
 
1123   my $locale   = $main::locale;
 
1125   $main::auth->assert('invoice_edit');
 
1127   if ($form->{storno}) {
 
1128     $form->error($locale->text('Cannot storno storno invoice!'));
 
1131   if (IS->has_storno(\%myconfig, $form, "ar")) {
 
1132     $form->error($locale->text("Invoice has already been storno'd!"));
 
1135   map({ my $key = $_; delete($form->{$key}) unless (grep({ $key eq $_ } qw(id login password stylesheet type))); } keys(%{ $form }));
 
1141   # Payments must not be recorded for the new storno invoice.
 
1142   $form->{paidaccounts} = 0;
 
1143   map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
 
1145   $form->{storno_id} = $form->{id};
 
1146   $form->{storno} = 1;
 
1148   $form->{invnumber} = "Storno zu " . $form->{invnumber};
 
1149   $form->{rowcount}++;
 
1152   $main::lxdebug->leave_sub();
 
1156   $main::lxdebug->enter_sub();
 
1158   my $form     = $main::form;
 
1160   $main::auth->assert('invoice_edit');
 
1162   $form->{preview} = 1;
 
1163   my $old_form = new Form;
 
1164   for (keys %$form) { $old_form->{$_} = $form->{$_} }
 
1166   &print_form($old_form);
 
1167   $main::lxdebug->leave_sub();
 
1172   $main::lxdebug->enter_sub();
 
1174   my $form     = $main::form;
 
1175   my $locale   = $main::locale;
 
1177   $main::auth->assert('invoice_edit');
 
1179   if ($form->{second_run}) {
 
1180     $form->{print_and_post} = 0;
 
1187 <form method="post" action="$form->{script}">
 
1190   # delete action variable
 
1191   map { delete $form->{$_} } qw(action header);
 
1193   foreach my $key (keys %$form) {
 
1194     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
 
1195     $form->{$key} =~ s/\"/"/g;
 
1196     print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
 
1200 <h2 class="confirm">| . $locale->text('Confirm!') . qq|</h2>
 
1203     . $locale->text('Are you sure you want to delete Invoice Number')
 
1204     . qq| $form->{invnumber}
 
1208 <input name="action" class="submit" type="submit" value="|
 
1209     . $locale->text('Yes') . qq|">
 
1213   $main::lxdebug->leave_sub();
 
1217   $main::lxdebug->enter_sub();
 
1219   my $form     = $main::form;
 
1220   my %myconfig = %main::myconfig;
 
1221   my $locale   = $main::locale;
 
1223   $main::auth->assert('invoice_edit');
 
1225   $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
 
1227     $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
 
1230   $form->{rowcount}--;
 
1231   $form->{shipto} = 1;
 
1234   $form->{title}  = $locale->text('Add Credit Note');
 
1235   $form->{script} = 'is.pl';
 
1237   our $buysell        = 'buy';
 
1240   # bo creates the id, reset it
 
1241   map { delete $form->{$_} }
 
1242     qw(id invnumber subject message cc bcc printed emailed queued);
 
1243   $form->{ $form->{vc} } =~ s/--.*//g;
 
1244   $form->{type} = "credit_note";
 
1247   map { $form->{"select$_"} = "" } ($form->{vc}, 'currency');
 
1249   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
 
1250     qw(creditlimit creditremaining);
 
1252   my $currency = $form->{currency};
 
1255   $form->{currency}     = $currency;
 
1256   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, $buysell);
 
1257   $form->{exchangerate} = $form->{forex} || '';
 
1259   $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
 
1266   $main::lxdebug->leave_sub();
 
1270   $main::lxdebug->enter_sub();
 
1272   my $form     = $main::form;
 
1273   my %myconfig = %main::myconfig;
 
1274   my $locale   = $main::locale;
 
1276   $main::auth->assert('invoice_edit');
 
1278   if (IS->delete_invoice(\%myconfig, \%$form, $main::spool)) {
 
1279     # saving the history
 
1280         if(!exists $form->{addition}) {
 
1281     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
1282           $form->{addition} = "DELETED";
 
1283           $form->save_history($form->dbconnect(\%myconfig));
 
1285     # /saving the history
 
1286     $form->redirect($locale->text('Invoice deleted!'));
 
1288   $form->error($locale->text('Cannot delete invoice!'));
 
1290   $main::lxdebug->leave_sub();
 
1294   $main::lxdebug->enter_sub();
 
1296   my $form     = $main::form;
 
1298   $main::auth->assert('invoice_edit');
 
1303     my $saved_form = save_form();
 
1307     restore_form($saved_form, 0, qw(id invnumber));
 
1312   $main::lxdebug->leave_sub();