1 #=====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #=====================================================================
 
   8 # SQL-Ledger Accounting
 
  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 # Accounts Receivables
 
  32 #======================================================================
 
  34 use POSIX qw(strftime);
 
  35 use List::Util qw(sum first);
 
  41 use SL::ReportGenerator;
 
  43 require "bin/mozilla/arap.pl";
 
  44 require "bin/mozilla/common.pl";
 
  45 require "bin/mozilla/drafts.pl";
 
  46 require "bin/mozilla/reportgenerator.pl";
 
  51 # this is for our long dates
 
  52 # $locale->text('January')
 
  53 # $locale->text('February')
 
  54 # $locale->text('March')
 
  55 # $locale->text('April')
 
  56 # $locale->text('May ')
 
  57 # $locale->text('June')
 
  58 # $locale->text('July')
 
  59 # $locale->text('August')
 
  60 # $locale->text('September')
 
  61 # $locale->text('October')
 
  62 # $locale->text('November')
 
  63 # $locale->text('December')
 
  65 # this is for our short month
 
  66 # $locale->text('Jan')
 
  67 # $locale->text('Feb')
 
  68 # $locale->text('Mar')
 
  69 # $locale->text('Apr')
 
  70 # $locale->text('May')
 
  71 # $locale->text('Jun')
 
  72 # $locale->text('Jul')
 
  73 # $locale->text('Aug')
 
  74 # $locale->text('Sep')
 
  75 # $locale->text('Oct')
 
  76 # $locale->text('Nov')
 
  77 # $locale->text('Dec')
 
  82   $main::lxdebug->enter_sub();
 
  84   $main::auth->assert('general_ledger');
 
  86   my $form     = $main::form;
 
  87   my %myconfig = %main::myconfig;
 
  89   return $main::lxdebug->leave_sub() if (load_draft_maybe());
 
  92   if(!exists $form->{addition} && ($form->{id} ne "")) {
 
  93     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
  94     $form->{addition} = "ADDED";
 
  95     $form->save_history($form->dbconnect(\%myconfig));
 
  99   $form->{title}    = "Add";
 
 100   $form->{callback} = "ar.pl?action=add" unless $form->{callback};
 
 102   AR->get_transdate(\%myconfig, $form);
 
 103   $form->{initial_transdate} = $form->{transdate};
 
 105   $form->{transdate} = $form->{initial_transdate};
 
 107   $main::lxdebug->leave_sub();
 
 111   $main::lxdebug->enter_sub();
 
 113   $main::auth->assert('general_ledger');
 
 115   my $form     = $main::form;
 
 117   # show history button
 
 118   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
 
 119   #/show hhistory button
 
 120   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
 
 121   $form->{title} = "Edit";
 
 126   $main::lxdebug->leave_sub();
 
 130   $main::lxdebug->enter_sub();
 
 132   $main::auth->assert('general_ledger');
 
 134   my $form     = $main::form;
 
 139   $main::lxdebug->leave_sub();
 
 143   $main::lxdebug->enter_sub();
 
 145   $main::auth->assert('general_ledger');
 
 147   my $form     = $main::form;
 
 148   my %myconfig = %main::myconfig;
 
 150   my ($duedate, $taxincluded, @curr);
 
 152   $form->create_links("AR", \%myconfig, "customer");
 
 153   $duedate = $form->{duedate};
 
 155   $taxincluded = $form->{taxincluded};
 
 156   my $id = $form->{id};
 
 157   IS->get_customer(\%myconfig, \%$form);
 
 158   $form->{taxincluded} = $taxincluded;
 
 161   $form->{duedate}     = $duedate if $duedate;
 
 162   $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
 
 163   $form->{rowcount}    = 1;
 
 166   $form->{notes} = $form->{intnotes} unless $form->{notes};
 
 169   @curr = split(/:/, $form->{currencies});
 
 171   $form->{defaultcurrency} = $curr[0];
 
 173   map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
 
 176   if (@{ $form->{all_customer} || [] }) {
 
 177     $form->{customer} = "$form->{customer}--$form->{customer_id}";
 
 178     map { $form->{selectcustomer} .= "<option>$_->{name}--$_->{id}\n" }
 
 179       (@{ $form->{all_customer} });
 
 183   if (@{ $form->{all_departments} || [] }) {
 
 184     $form->{selectdepartment} = "<option>\n";
 
 185     $form->{department}       = "$form->{department}--$form->{department_id}";
 
 188       $form->{selectdepartment} .=
 
 189         "<option>$_->{description}--$_->{id}\n"
 
 190     } (@{ $form->{all_departments} || [] });
 
 193   $form->{employee} = "$form->{employee}--$form->{employee_id}";
 
 196   if (@{ $form->{all_employees} || [] }) {
 
 197     $form->{selectemployee} = "";
 
 198     map { $form->{selectemployee} .= "<option>$_->{name}--$_->{id}\n" }
 
 199       (@{ $form->{all_employees} || [] });
 
 202   # build the popup menus
 
 203   $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
 
 205   AR->setup_form($form);
 
 208     ($form->datetonum($form->{transdate}, \%myconfig) <=
 
 209      $form->datetonum($form->{closedto}, \%myconfig));
 
 211   $main::lxdebug->leave_sub();
 
 215   $main::lxdebug->enter_sub();
 
 217   $main::auth->assert('general_ledger');
 
 219   my $form     = $main::form;
 
 220   my %myconfig = %main::myconfig;
 
 221   my $locale   = $main::locale;
 
 222   my $cgi      = $main::cgi;
 
 224   my ($title, $readonly, $exchangerate, $rows);
 
 225   my ($taxincluded, $notes, $department, $customer, $employee, $amount, $project);
 
 226   my ($jsscript, $button1, $button2, $onload);
 
 227   my ($selectAR_amount, $selectAR_paid, $ARselected, $tax);
 
 228   my (@column_index, %column_data);
 
 231   $title = $form->{title};
 
 232   $form->{title} = $locale->text("$title Accounts Receivables Transaction");
 
 234   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
 
 236   # $locale->text('Add Accounts Receivables Transaction')
 
 237   # $locale->text('Edit Accounts Receivables Transaction')
 
 238   $form->{javascript} = qq|<script type="text/javascript">
 
 240   function setTaxkey(accno, row) {
 
 241     var taxkey = accno.options[accno.selectedIndex].value;
 
 242     var reg = /--([0-9]*)/;
 
 243     var found = reg.exec(taxkey);
 
 244     var index = found[1];
 
 245     index = parseInt(index);
 
 246     var tax = 'taxchart_' + row;
 
 247     for (var i = 0; i < document.getElementById(tax).options.length; ++i) {
 
 248       var reg2 = new RegExp("^"+ index, "");
 
 249       if (reg2.exec(document.getElementById(tax).options[i].value)) {
 
 250         document.getElementById(tax).options[i].selected = true;
 
 257   # show history button js
 
 258   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
 
 259   #/show history button js
 
 260   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
 
 261   $readonly = ($form->{id}) ? "readonly" : "";
 
 264     ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
 
 265   $readonly = ($form->{radier}) ? "" : $readonly;
 
 267   # set option selected
 
 268   foreach my $item (qw(customer currency department employee)) {
 
 269     $form->{"select$item"} =~ s/ selected//;
 
 270     $form->{"select$item"} =~
 
 271       s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
 
 274   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'buy');
 
 275   $form->{exchangerate} = $form->{forex} if $form->{forex};
 
 278   $form->{exchangerate} =
 
 279     $form->format_amount(\%myconfig, $form->{exchangerate});
 
 281   if ($form->{exchangerate} == 0) {
 
 282     $form->{exchangerate} = "";
 
 285   $form->{creditlimit} =
 
 286     $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
 
 287   $form->{creditremaining} =
 
 288     $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
 
 291 <input type=hidden name=forex value=$form->{forex}>
 
 293   if ($form->{currency} ne $form->{defaultcurrency}) {
 
 294     if ($form->{forex}) {
 
 296         <th align=right>| . $locale->text('Exchangerate') . qq|</th>
 
 297         <td><input type=hidden name=exchangerate value=$form->{exchangerate}>$form->{exchangerate}</td>
 
 301         <th align=right>| . $locale->text('Exchangerate') . qq|</th>
 
 302         <td><input name=exchangerate size=10 value=$form->{exchangerate}></td>
 
 309                 <td align=right><input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}></td>
 
 310                 <th align=left nowrap>| . $locale->text('Tax Included') . qq|</th>
 
 314   if (($rows = $form->numtextrows($form->{notes}, 50)) < 2) {
 
 318     qq|<textarea name=notes rows=$rows cols=50 wrap=soft>$form->{notes}</textarea>|;
 
 322                 <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
 
 323                 <td colspan=3><select name=department>$form->{selectdepartment}</select>
 
 324                 <input type=hidden name=selectdepartment value="$form->{selectdepartment}">
 
 327 | if $form->{selectdepartment};
 
 329   my $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
 
 331   $customer = ($form->{selectcustomer})
 
 332     ? qq|<select name="customer" onchange="document.getElementById('update_button').click();">$form->{selectcustomer}</select>|
 
 333     : qq|<input name=customer value="$form->{customer}" size=35>|;
 
 336                 <input type=hidden name=employee value="$form->{employee}">
 
 339   if ($form->{selectemployee}) {
 
 342                 <th align=right nowrap>| . $locale->text('Salesperson') . qq|</th>
 
 343                 <td  colspan=2><select name=employee>$form->{selectemployee}</select></td>
 
 344                 <input type=hidden name=selectemployee value="$form->{selectemployee}">
 
 349   my @old_project_ids = ();
 
 350   map({ push(@old_project_ids, $form->{"project_id_$_"})
 
 351           if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
 
 353   $form->get_lists("projects"  => { "key"       => "ALL_PROJECTS",
 
 355                                     "old_id"    => \@old_project_ids },
 
 356                    "charts"    => { "key"       => "ALL_CHARTS",
 
 357                                     "transdate" => $form->{transdate} },
 
 358                    "taxcharts" => { "key"       => "ALL_TAXCHARTS",
 
 359                                     "module"    => "AR" },);
 
 361   map({ $_->{link_split} = [ split(/:/, $_->{link}) ]; }
 
 362       @{ $form->{ALL_CHARTS} });
 
 364   my %project_labels = ();
 
 365   my @project_values = ("");
 
 366   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
 
 367     push(@project_values, $item->{"id"});
 
 368     $project_labels{$item->{"id"}} = $item->{"projectnumber"};
 
 371   my (%AR_amount_labels, @AR_amount_values);
 
 372   my (%AR_labels, @AR_values);
 
 373   my (%AR_paid_labels, @AR_paid_values);
 
 377   foreach my $item (@{ $form->{ALL_CHARTS} }) {
 
 378     if (grep({ $_ eq "AR_amount" } @{ $item->{link_split} })) {
 
 379       $taxchart_init = $item->{tax_id} if ($taxchart_init eq "");
 
 380       my $key = "$item->{accno}--$item->{tax_id}";
 
 381       push(@AR_amount_values, $key);
 
 382       $AR_amount_labels{$key} =
 
 383         "$item->{accno}--$item->{description}";
 
 385     } elsif (grep({ $_ eq "AR" } @{ $item->{link_split} })) {
 
 386       push(@AR_values, $item->{accno});
 
 387       $AR_labels{$item->{accno}} = "$item->{accno}--$item->{description}";
 
 389     } elsif (grep({ $_ eq "AR_paid" } @{ $item->{link_split} })) {
 
 390       push(@AR_paid_values, $item->{accno});
 
 391       $AR_paid_labels{$item->{accno}} =
 
 392         "$item->{accno}--$item->{description}";
 
 395     $charts{$item->{accno}} = $item;
 
 398   my %taxchart_labels = ();
 
 399   my @taxchart_values = ();
 
 401   foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
 
 402     my $key = "$item->{id}--$item->{rate}";
 
 403     $taxchart_init = $key if ($taxchart_init eq $item->{id});
 
 404     push(@taxchart_values, $key);
 
 405     $taxchart_labels{$key} =
 
 406       "$item->{taxdescription} " . ($item->{rate} * 100) . ' %';
 
 407     $taxcharts{$item->{id}} = $item;
 
 410   $form->{fokus} = "arledger.customer";
 
 412   # use JavaScript Calendar or not
 
 413   $form->{jsscript} = 1;
 
 415   if ($form->{jsscript}) {
 
 417     # with JavaScript Calendar
 
 419        <td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\"></td>
 
 420        <td><input type=button name=transdate id="trigger1" value=|
 
 421       . $locale->text('button') . qq|></td>
 
 424        <td><input name=duedate id=duedate size=11 title="$myconfig{dateformat}" value="$form->{duedate}" onBlur=\"check_right_date_format(this)\"></td>
 
 425        <td><input type=button name=duedate id="trigger2" value=|
 
 426       . $locale->text('button') . qq|></td></td>
 
 431       Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1",
 
 432                           "duedate", "BL", "trigger2");
 
 435     # without JavaScript Calendar
 
 437       qq|<td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\"></td>|;
 
 439       qq|<td><input name=duedate id=duedate size=11 title="$myconfig{dateformat}" value="$form->{duedate}" onBlur=\"check_right_date_format(this)\"></td>|;
 
 442   my $follow_up_vc         =  $form->{customer};
 
 443   $follow_up_vc            =~ s/--.*?//;
 
 444   my $follow_up_trans_info =  "$form->{invnumber} ($follow_up_vc)";
 
 446   $form->{javascript} .=
 
 447     qq|<script type="text/javascript" src="js/common.js"></script>| .
 
 448     qq|<script type="text/javascript" src="js/show_vc_details.js"></script>| .
 
 449     qq|<script type="text/javascript" src="js/follow_up.js"></script>|;
 
 452   $onload = qq|focus()|;
 
 453   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
 
 454   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
 
 456 <body onLoad="$onload">
 
 458 <form method=post name="arledger" action=$form->{script}>
 
 460 <input type=hidden name=id value=$form->{id}>
 
 461 <input type=hidden name=sort value=$form->{sort}>
 
 462 <input type=hidden name=closedto value=$form->{closedto}>
 
 463 <input type=hidden name=locked value=$form->{locked}>
 
 464 <input type=hidden name=title value="$title">
 
 465 <input type="hidden" name="follow_up_trans_id_1" value="| . H($form->{id}) . qq|">
 
 466 <input type="hidden" name="follow_up_trans_type_1" value="ar_transaction">
 
 467 <input type="hidden" name="follow_up_trans_info_1" value="| . H($follow_up_trans_info) . qq|">
 
 468 <input type="hidden" name="follow_up_rowcount" value="1">
 
 470 | . ($form->{saved_message} ? qq|<p>$form->{saved_message}</p>| : "") . qq|
 
 474     <th class=listtop>$form->{title}</th>
 
 484                 <th align="right" nowrap>| . $locale->text('Customer') . qq|</th>
 
 485                 <td colspan=3>$customer <input type="button" value="| . $locale->text('Details (one letter abbreviation)') . qq|" onclick="show_vc_details('customer')"></td>
 
 486                 <input type=hidden name=selectcustomer value="$form->{selectcustomer}">
 
 487                 <input type=hidden name=oldcustomer value="$form->{oldcustomer}">
 
 488                 <input type=hidden name=customer_id value="$form->{customer_id}">
 
 489                 <input type=hidden name=terms value=$form->{terms}>
 
 496                       <th align=left nowrap>| . $locale->text('Credit Limit') . qq|</th>
 
 497                       <td>$form->{creditlimit}</td>
 
 498                       <th align=left nowrap>| . $locale->text('Remaining') . qq|</th>
 
 499                       <td class="plus$n">$form->{creditremaining}</td>
 
 500                       <input type=hidden name=creditlimit value=$form->{creditlimit}>
 
 501                       <input type=hidden name=creditremaining value=$form->{creditremaining}>
 
 507                 <th align=right>| . $locale->text('Currency') . qq|</th>
 
 508                 <td><select name=currency>$form->{selectcurrency}</select></td>
 
 509                 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
 
 510                 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
 
 511                 <input type=hidden name=fxgain_accno value=$form->{fxgain_accno}>
 
 512                 <input type=hidden name=fxloss_accno value=$form->{fxloss_accno}>
 
 523                 <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
 
 524                 <td><input name=invnumber size=11 value="$form->{invnumber}"></td>
 
 527                 <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
 
 528                 <td><input name=ordnumber size=11 value="$form->{ordnumber}"></td>
 
 531                 <th align=right nowrap>| . $locale->text('Invoice Date') . qq|</th>
 
 535                 <th align=right nowrap>| . $locale->text('Due Date') . qq|</th>
 
 546   <input type=hidden name=rowcount value=$form->{rowcount}>
 
 550            <tr class=listheading>
 
 551           <th class=listheading style="width:15%">|
 
 552     . $locale->text('Account') . qq|</th>
 
 553           <th class=listheading style="width:10%">|
 
 554     . $locale->text('Amount') . qq|</th>
 
 555           <th class=listheading style="width:10%">|
 
 556     . $locale->text('Tax') . qq|</th>
 
 557           <th class=listheading style="width:5%">|
 
 558     . $locale->text('Taxkey') . qq|</th>
 
 559           <th class=listheading style="width:10%">|
 
 560     . $locale->text('Project') . qq|</th>
 
 564   $amount  = $locale->text('Amount');
 
 565   $project = $locale->text('Project');
 
 567   for my $i (1 .. $form->{rowcount}) {
 
 570     $form->{"amount_$i"} =
 
 571       $form->format_amount(\%myconfig, $form->{"amount_$i"}, 2);
 
 572     $form->{"tax_$i"} = $form->format_amount(\%myconfig, $form->{"tax_$i"}, 2);
 
 574     my $selected_accno_full;
 
 575     my ($accno_row) = split(/--/, $form->{"AR_amount_$i"});
 
 576     my $item = $charts{$accno_row};
 
 577     $selected_accno_full = "$item->{accno}--$item->{tax_id}";
 
 579     my $selected_taxchart = $form->{"taxchart_$i"};
 
 580     my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full);
 
 581     my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_AR_amount_$i"});
 
 583     if ($previous_accno &&
 
 584         ($previous_accno eq $selected_accno) &&
 
 585         ($previous_tax_id ne $selected_tax_id)) {
 
 586       my $item = $taxcharts{$selected_tax_id};
 
 587       $selected_taxchart = "$item->{id}--$item->{rate}";
 
 590     if (!$form->{"taxchart_$i"}) {
 
 591       if ($form->{"AR_amount_$i"} =~ m/.--./) {
 
 592         $selected_taxchart = join '--', map { ($_->{id}, $_->{rate}) } first { $_->{id} == $item->{tax_id} } @{ $form->{ALL_TAXCHARTS} };
 
 594         $selected_taxchart = $taxchart_init;
 
 599       NTI($cgi->popup_menu('-name' => "AR_amount_$i",
 
 600                            '-id' => "AR_amount_$i",
 
 601                            '-style' => 'width:400px',
 
 602                            '-onChange' => "setTaxkey(this, $i)",
 
 603                            '-values' => \@AR_amount_values,
 
 604                            '-labels' => \%AR_amount_labels,
 
 605                            '-default' => $selected_accno_full))
 
 606       . $cgi->hidden('-name' => "previous_AR_amount_$i",
 
 607                      '-default' => $selected_accno_full);
 
 610       NTI($cgi->popup_menu('-name' => "taxchart_$i",
 
 611                            '-id' => "taxchart_$i",
 
 612                            '-style' => 'width:200px',
 
 613                            '-values' => \@taxchart_values,
 
 614                            '-labels' => \%taxchart_labels,
 
 615                            '-default' => $selected_taxchart))
 
 619       NTI($cgi->popup_menu('-name' => "project_id_$i",
 
 620                            '-values' => \@project_values,
 
 621                            '-labels' => \%project_labels,
 
 622                            '-default' => $form->{"project_id_$i"} ));
 
 626           <td>$selectAR_amount</td>
 
 627           <td><input name="amount_$i" size=10 value=$form->{"amount_$i"}></td>
 
 628           <td><input type="hidden" name="tax_$i" value="$form->{"tax_$i"}">$form->{"tax_$i"}</td>
 
 630           <td>$projectnumber</td>
 
 637   $form->{invtotal_unformatted} = $form->{invtotal};
 
 638   $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
 
 641     NTI($cgi->popup_menu('-name' => "ARselected", '-id' => "ARselected",
 
 642                          '-style' => 'width:400px',
 
 643                          '-values' => \@AR_values, '-labels' => \%AR_labels,
 
 644                          '-default' => $form->{ARselected}));
 
 653           <td>${ARselected}</td>
 
 654           <th align=left>$form->{invtotal}</th>
 
 656           <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
 
 657           <input type=hidden name=oldtotalpaid value=$form->{oldtotalpaid}>
 
 659           <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
 
 672           <th align=left width=1%>| . $locale->text('Notes') . qq|</th>
 
 673           <td align=left>$notes</td>
 
 681         <tr class=listheading>
 
 682           <th colspan=7 class=listheading>|
 
 683     . $locale->text('Incoming Payments') . qq|</th>
 
 687   if ($form->{currency} eq $form->{defaultcurrency}) {
 
 688     @column_index = qw(datepaid source memo paid AR_paid paid_project_id);
 
 690     @column_index = qw(datepaid source memo paid exchangerate AR_paid paid_project_id);
 
 693   $column_data{datepaid}     = "<th>" . $locale->text('Date') . "</th>";
 
 694   $column_data{paid}         = "<th>" . $locale->text('Amount') . "</th>";
 
 695   $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
 
 696   $column_data{AR_paid}      = "<th>" . $locale->text('Account') . "</th>";
 
 697   $column_data{source}       = "<th>" . $locale->text('Source') . "</th>";
 
 698   $column_data{memo}         = "<th>" . $locale->text('Memo') . "</th>";
 
 699   $column_data{paid_project_id} = "<th>" . $locale->text('Project Number') . "</th>";
 
 704   map { print "$column_data{$_}\n" } @column_index;
 
 712   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
 
 713   for my $i (1 .. $form->{paidaccounts}) {
 
 719       NTI($cgi->popup_menu('-name' => "AR_paid_$i",
 
 720                            '-id' => "AR_paid_$i",
 
 721                            '-values' => \@AR_paid_values,
 
 722                            '-labels' => \%AR_paid_labels,
 
 723                            '-default' => $form->{"AR_paid_$i"}));
 
 725     $totalpaid += $form->{"paid_$i"};
 
 728     if ($form->{"paid_$i"}) {
 
 730         $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
 
 732     $form->{"exchangerate_$i"} =
 
 733       $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
 
 735     if ($form->{"exchangerate_$i"} == 0) {
 
 736       $form->{"exchangerate_$i"} = "";
 
 739     $exchangerate = qq| |;
 
 740     if ($form->{currency} ne $form->{defaultcurrency}) {
 
 741       if ($form->{"forex_$i"}) {
 
 743           qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
 
 746           qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
 
 751 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
 
 755       qq|<td align=center><input name="paid_$i" size=11 value="$form->{"paid_$i"}" onBlur=\"check_right_number_format(this)\"></td>|;
 
 756     $column_data{AR_paid} =
 
 757       qq|<td align=center>${selectAR_paid}</td>|;
 
 758     $column_data{exchangerate} = qq|<td align=center>$exchangerate</td>|;
 
 759     $column_data{datepaid}     =
 
 760       qq|<td align=center><input name="datepaid_$i" id="datepaid_$i" size=11 value="$form->{"datepaid_$i"}" onBlur=\"check_right_date_format(this)\">
 
 761          <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
 
 762     $column_data{source} =
 
 763       qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
 
 765       qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
 
 767     $column_data{paid_project_id} =
 
 769       . NTI($cgi->popup_menu('-name' => "paid_project_id_$i",
 
 770                              '-values' => \@project_values,
 
 771                              '-labels' => \%project_labels,
 
 772                              '-default' => $form->{"paid_project_id_$i"} ))
 
 775     map { print qq|$column_data{$_}\n| } @column_index;
 
 780     push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
 
 783   my $paid_missing = $form->{invtotal_unformatted} - $totalpaid;
 
 789           <td align="center">| . $locale->text('Total') . qq|</td>
 
 790           <td align="center">| . H($form->format_amount(\%myconfig, $totalpaid, 2)) . qq|</td>
 
 795           <td align="center">| . $locale->text('Missing amount') . qq|</td>
 
 796           <td align="center">| . H($form->format_amount(\%myconfig, $paid_missing, 2)) . qq|</td>
 
 798 | . $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
 
 800 <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
 
 806     <td><hr size=3 noshade></td>
 
 811   $main::lxdebug->leave_sub();
 
 815   $main::lxdebug->enter_sub();
 
 817   $main::auth->assert('general_ledger');
 
 819   my $form     = $main::form;
 
 820   my %myconfig = %main::myconfig;
 
 821   my $locale   = $main::locale;
 
 822   my $cgi      = $main::cgi;
 
 824   my ($transdate, $closedto);
 
 826   my $follow_ups_block;
 
 828     my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
 
 830     if (@{ $follow_ups} ) {
 
 831       my $num_due       = sum map { $_->{due} * 1 } @{ $follow_ups };
 
 832       $follow_ups_block = qq|<p>| . $locale->text("There are #1 unfinished follow-ups of which #2 are due.", scalar @{ $follow_ups }, $num_due) . qq|</p>|;
 
 840 <input name=gldate type=hidden value="| . Q($form->{gldate}) . qq|">
 
 842 <input name=callback type=hidden value="$form->{callback}">
 
 844 . $cgi->hidden('-name' => 'draft_id', '-default' => [$form->{draft_id}])
 
 845 . $cgi->hidden('-name' => 'draft_description', '-default' => [$form->{draft_description}])
 
 851   if (!$form->{id} && $form->{draft_id}) {
 
 852     print(NTI($cgi->checkbox('-name' => 'remove_draft', '-id' => 'remove_draft',
 
 853                              '-value' => 1, '-checked' => $form->{remove_draft},
 
 855           qq| <label for="remove_draft">| .
 
 856           $locale->text("Remove draft when posting") .
 
 860   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
 
 861   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
 
 863   print qq|<input class="submit" type="submit" name="action" id="update_button" value="| . $locale->text('Update') . qq|">\n|;
 
 865   # ToDO: - insert a global check for stornos, so that a storno is only possible a limited time after saving it
 
 866   print qq| <input class=submit type=submit name=action value="| . $locale->text('Storno') . qq|"> |
 
 867     if ($form->{id} && !IS->has_storno(\%myconfig, $form, 'ar') && !IS->is_storno(\%myconfig, $form, 'ar') && (($totalpaid == 0) || ($totalpaid eq "")));
 
 870     if ($form->{radier}) {
 
 872         <input class=submit type=submit name=action value="| . $locale->text('Post') .            qq|">
 
 873         <input class=submit type=submit name=action value="| . $locale->text('Delete') .          qq|"> |;
 
 875     if ($transdate > $closedto) {
 
 877         <input class=submit type=submit name=action value="| . $locale->text('Use As Template') . qq|"> |;
 
 880         <input class=submit type=submit name=action value="| . $locale->text('Post Payment') .    qq|">
 
 881         <input type="button" class="submit" onclick="follow_up_window()" value="|
 
 882       . $locale->text('Follow-Up')
 
 886     if ($transdate > $closedto) {
 
 887       print qq| <input class=submit type=submit name=action value="| . $locale->text('Post') .     qq|"> | .
 
 888         NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'), '-class' => 'submit'));
 
 892   if ($form->{menubar}) {
 
 893     require "bin/mozilla/menu.pl";
 
 896   # button for saving history
 
 897   if($form->{id} ne "") {
 
 898     print qq| <input type=button class=submit onclick=set_history_window($form->{id}); name=history id=history value=| . $locale->text('history') . qq|> |;
 
 900   # /button for saving history
 
 901   # mark_as_paid button
 
 902   if($form->{id} ne "") {
 
 903     print qq|<input type="submit" class="submit" name="action" value="|
 
 904           . $locale->text('mark as paid') . qq|">|;
 
 906   # /mark_as_paid button
 
 915   $main::lxdebug->leave_sub();
 
 919   $main::lxdebug->enter_sub();
 
 921   $main::auth->assert('general_ledger');
 
 923   my $form     = $main::form;
 
 924   my %myconfig = %main::myconfig;
 
 926   &mark_as_paid_common(\%myconfig,"ar");
 
 928   $main::lxdebug->leave_sub();
 
 932   $main::lxdebug->enter_sub();
 
 934   $main::auth->assert('general_ledger');
 
 936   my $form     = $main::form;
 
 937   my %myconfig = %main::myconfig;
 
 941   my ($totaltax, $exchangerate);
 
 943   $form->{invtotal} = 0;
 
 945   delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
 
 947   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
 
 948     qw(exchangerate creditlimit creditremaining);
 
 950   my @flds  = qw(amount AR_amount projectnumber oldprojectnumber project_id);
 
 954   for my $i (1 .. $form->{rowcount}) {
 
 955     $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
 
 956     $form->{"tax_$i"} = $form->parse_amount(\%myconfig, $form->{"tax_$i"});
 
 957     if ($form->{"amount_$i"}) {
 
 960       my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
 
 962         if ($form->{taxincluded}) {
 
 963           $form->{"tax_$i"} = $form->{"amount_$i"} / ($rate + 1) * $rate;
 
 965           $form->{"tax_$i"} = $form->{"amount_$i"} * $rate;
 
 968         $form->{"tax_$i"} = 0;
 
 970       $form->{"tax_$i"} = $form->round_amount($form->{"tax_$i"}, 2);
 
 972       $totaltax += $form->{"tax_$i"};
 
 973       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
 
 978   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
 
 979   $form->{rowcount} = $count + 1;
 
 980   map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
 
 982   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'buy');
 
 983   $form->{exchangerate} = $form->{forex} if $form->{forex};
 
 985   $form->{invdate} = $form->{transdate};
 
 987   $form->{invdate} = $form->{transdate};
 
 989   my %saved_variables = map +( $_ => $form->{$_} ), qw(AR AR_amount_1 taxchart_1);
 
 991   &check_name("customer");
 
 993   $form->{AR} = $saved_variables{AR};
 
 994   if ($saved_variables{AR_amount_1} =~ m/.--./) {
 
 995     map { $form->{$_} = $saved_variables{$_} } qw(AR_amount_1 taxchart_1);
 
 997     delete $form->{taxchart_1};
 
1001     ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
 
1003   for my $i (1 .. $form->{paidaccounts}) {
 
1004     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
 
1006         $form->{"${_}_$i"} =
 
1007           $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
 
1008       } qw(paid exchangerate);
 
1010       $totalpaid += $form->{"paid_$i"};
 
1012       $form->{"forex_$i"}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
 
1013       $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
 
1017   $form->{creditremaining} -=
 
1018     ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} -
 
1019      $form->{oldinvtotal});
 
1020   $form->{oldinvtotal}  = $form->{invtotal};
 
1021   $form->{oldtotalpaid} = $totalpaid;
 
1025   $main::lxdebug->leave_sub();
 
1029 # ToDO: fix $closedto and $invdate
 
1032   $main::lxdebug->enter_sub();
 
1034   $main::auth->assert('general_ledger');
 
1036   my $form     = $main::form;
 
1037   my %myconfig = %main::myconfig;
 
1038   my $locale   = $main::locale;
 
1040   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
1042   for my $i (1 .. $form->{paidaccounts}) {
 
1044     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
 
1045       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
 
1047       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
1049       $form->error($locale->text('Cannot post payment for a closed period!')) if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
 
1051       if ($form->{currency} ne $form->{defaultcurrency}) {
 
1052 #        $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid);
 
1053         $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
 
1058   ($form->{AR})      = split /--/, $form->{AR};
 
1059   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
 
1060   $form->redirect($locale->text('Payment posted!')) if (AR->post_payment(\%myconfig, \%$form));
 
1061   $form->error($locale->text('Cannot post payment!'));
 
1063   $main::lxdebug->leave_sub();
 
1068   $main::auth->assert('general_ledger');
 
1070   my $form     = $main::form;
 
1077   $main::lxdebug->enter_sub();
 
1079   $main::auth->assert('general_ledger');
 
1081   my $form     = $main::form;
 
1082   my %myconfig = %main::myconfig;
 
1083   my $locale   = $main::locale;
 
1089   # check if there is an invoice number, invoice and due date
 
1090   $form->isblank("transdate", $locale->text('Invoice Date missing!'));
 
1091   $form->isblank("duedate",   $locale->text('Due Date missing!'));
 
1092   $form->isblank("customer",  $locale->text('Customer missing!'));
 
1094   my $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
 
1095   my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
 
1096   $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
 
1098   $form->error($locale->text('Zero amount posting!'))
 
1099     unless grep $_*1, map $form->parse_amount(\%myconfig, $form->{"amount_$_"}), 1..$form->{rowcount};
 
1101   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
 
1102     if ($form->{currency} ne $form->{defaultcurrency});
 
1104   delete($form->{AR});
 
1106   for my $i (1 .. $form->{paidaccounts}) {
 
1107     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
 
1108       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
 
1110       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
1112       $form->error($locale->text('Cannot post payment for a closed period!'))
 
1113         if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
 
1115       if ($form->{currency} ne $form->{defaultcurrency}) {
 
1116         $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
 
1117         $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
 
1122   # if oldcustomer ne customer redo form
 
1123   my ($customer) = split /--/, $form->{customer};
 
1124   if ($form->{oldcustomer} ne "$customer--$form->{customer_id}") {
 
1129   $form->{AR}{receivables} = $form->{ARselected};
 
1130   $form->{storno}          = 0;
 
1132   $main::lxdebug->message(0, $form->{amount});
 
1133   $form->{id} = 0 if $form->{postasnew};
 
1134   $form->error($locale->text('Cannot post transaction!')) unless AR->post_transaction(\%myconfig, \%$form);
 
1136   # saving the history
 
1137   if(!exists $form->{addition} && $form->{id} ne "") {
 
1138     $form->{snumbers} = "invnumber_$form->{invnumber}";
 
1139     $form->{addition} = "POSTED";
 
1140     $form->save_history($form->dbconnect(\%myconfig));
 
1142   # /saving the history
 
1143   remove_draft() if $form->{remove_draft};
 
1145   $form->redirect($locale->text('Transaction posted!')) unless $inline;
 
1147   $main::lxdebug->leave_sub();
 
1151   $main::lxdebug->enter_sub();
 
1153   $main::auth->assert('general_ledger');
 
1155   my $form     = $main::form;
 
1156   my %myconfig = %main::myconfig;
 
1158   $form->{postasnew} = 1;
 
1159   # saving the history
 
1160   if(!exists $form->{addition} && $form->{id} ne "") {
 
1161     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
1162     $form->{addition} = "POSTED AS NEW";
 
1163     $form->save_history($form->dbconnect(\%myconfig));
 
1165   # /saving the history
 
1168   $main::lxdebug->leave_sub();
 
1171 sub use_as_template {
 
1172   $main::lxdebug->enter_sub();
 
1174   $main::auth->assert('general_ledger');
 
1176   my $form     = $main::form;
 
1177   my %myconfig = %main::myconfig;
 
1179   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);
 
1180   $form->{paidaccounts} = 1;
 
1181   $form->{rowcount}--;
 
1182   $form->{invdate} = $form->current_date(\%myconfig);
 
1185   $main::lxdebug->leave_sub();
 
1189   $main::lxdebug->enter_sub();
 
1191   $main::auth->assert('general_ledger');
 
1193   my $form     = $main::form;
 
1194   my $locale   = $main::locale;
 
1196   $form->{title} = $locale->text('Confirm!');
 
1200   delete $form->{header};
 
1205 <form method=post action=$form->{script}>
 
1208   foreach my $key (keys %$form) {
 
1209     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
 
1210     $form->{$key} =~ s/\"/"/g;
 
1211     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
 
1215 <h2 class=confirm>$form->{title}</h2>
 
1218     . $locale->text('Are you sure you want to delete Transaction')
 
1219     . qq| $form->{invnumber}</h4>
 
1221 <input name=action class=submit type=submit value="|
 
1222     . $locale->text('Yes') . qq|">
 
1229   $main::lxdebug->leave_sub();
 
1233   $main::lxdebug->enter_sub();
 
1235   $main::auth->assert('general_ledger');
 
1237   my $form     = $main::form;
 
1238   my %myconfig = %main::myconfig;
 
1239   my $locale   = $main::locale;
 
1241   if (AR->delete_transaction(\%myconfig, \%$form)) {
 
1242     # saving the history
 
1243     if(!exists $form->{addition}) {
 
1244       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
1245       $form->{addition} = "DELETED";
 
1246       $form->save_history($form->dbconnect(\%myconfig));
 
1248     # /saving the history
 
1249     $form->redirect($locale->text('Transaction deleted!'));
 
1251   $form->error($locale->text('Cannot delete transaction!'));
 
1253   $main::lxdebug->leave_sub();
 
1257   $main::lxdebug->enter_sub();
 
1259   $main::auth->assert('general_ledger | invoice_edit');
 
1261   my $form     = $main::form;
 
1262   my %myconfig = %main::myconfig;
 
1263   my $locale   = $main::locale;
 
1264   my $cgi      = $main::cgi;
 
1266   my ($customer, $department);
 
1267   my ($jsscript, $button1, $button2, $onload);
 
1269   # setup customer selection
 
1270   $form->all_vc(\%myconfig, "customer", "AR");
 
1272   $form->{title}    = $locale->text('AR Transactions');
 
1273   $form->{fokus}    = "search.customer";
 
1274   $form->{jsscript} = 1;
 
1276   $form->get_lists("projects"     => { "key" => "ALL_PROJECTS", "all" => 1 },
 
1277                    "departments"  => "ALL_DEPARTMENTS",
 
1278                    "customers"    => "ALL_VC");
 
1280   # constants and subs for template
 
1281   $form->{jsscript}  = 1;
 
1282   $form->{vc_keys}   = sub { "$_[0]->{name}--$_[0]->{id}" };
 
1285   print $form->parse_html_template('ar/search', { %myconfig });
 
1287   $main::lxdebug->leave_sub();
 
1290 sub create_subtotal_row {
 
1291   $main::lxdebug->enter_sub();
 
1293   my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
 
1295   my $form     = $main::form;
 
1296   my %myconfig = %main::myconfig;
 
1298   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
 
1300   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
 
1302   $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
 
1304   map { $totals->{$_} = 0 } @{ $subtotal_columns };
 
1306   $main::lxdebug->leave_sub();
 
1311 sub ar_transactions {
 
1312   $main::lxdebug->enter_sub();
 
1314   $main::auth->assert('general_ledger | invoice_edit');
 
1316   my $form     = $main::form;
 
1317   my %myconfig = %main::myconfig;
 
1318   my $locale   = $main::locale;
 
1320   my ($callback, $href, @columns);
 
1322   $form->{customer} = $form->unescape($form->{customer});
 
1323   ($form->{customer}, $form->{customer_id}) = split(/--/, $form->{customer});
 
1325   report_generator_set_default_sort('transdate', 1);
 
1327   AR->ar_transactions(\%myconfig, \%$form);
 
1329   $form->{title} = $locale->text('AR Transactions');
 
1331   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
1334     qw(transdate id type invnumber ordnumber name netamount tax amount paid
 
1335        datepaid due duedate transaction_description notes salesman employee shippingpoint shipvia
 
1336        marge_total marge_percent globalprojectnumber customernumber country ustid taxzone payment_terms charts);
 
1338   my @hidden_variables = map { "l_${_}" } @columns;
 
1339   push @hidden_variables, "l_subtotal", qw(open closed customer invnumber ordnumber transaction_description notes project_id transdatefrom transdateto);
 
1341   $href = build_std_url('action=ar_transactions', grep { $form->{$_} } @hidden_variables);
 
1344     'transdate'               => { 'text' => $locale->text('Date'), },
 
1345     'id'                      => { 'text' => $locale->text('ID'), },
 
1346     'type'                    => { 'text' => $locale->text('Type'), },
 
1347     'invnumber'               => { 'text' => $locale->text('Invoice'), },
 
1348     'ordnumber'               => { 'text' => $locale->text('Order'), },
 
1349     'name'                    => { 'text' => $locale->text('Customer'), },
 
1350     'netamount'               => { 'text' => $locale->text('Amount'), },
 
1351     'tax'                     => { 'text' => $locale->text('Tax'), },
 
1352     'amount'                  => { 'text' => $locale->text('Total'), },
 
1353     'paid'                    => { 'text' => $locale->text('Paid'), },
 
1354     'datepaid'                => { 'text' => $locale->text('Date Paid'), },
 
1355     'due'                     => { 'text' => $locale->text('Amount Due'), },
 
1356     'duedate'                 => { 'text' => $locale->text('Due Date'), },
 
1357     'transaction_description' => { 'text' => $locale->text('Transaction description'), },
 
1358     'notes'                   => { 'text' => $locale->text('Notes'), },
 
1359     'salesman'                => { 'text' => $locale->text('Salesperson'), },
 
1360     'employee'                => { 'text' => $locale->text('Employee'), },
 
1361     'shippingpoint'           => { 'text' => $locale->text('Shipping Point'), },
 
1362     'shipvia'                 => { 'text' => $locale->text('Ship via'), },
 
1363     'globalprojectnumber'     => { 'text' => $locale->text('Project Number'), },
 
1364     'marge_total'             => { 'text' => $locale->text('Ertrag'), },
 
1365     'marge_percent'           => { 'text' => $locale->text('Ertrag prozentual'), },
 
1366     'customernumber'          => { 'text' => $locale->text('Customer Number'), },
 
1367     'country'                 => { 'text' => $locale->text('Country'), },
 
1368     'ustid'                   => { 'text' => $locale->text('USt-IdNr.'), },
 
1369     'taxzone'                 => { 'text' => $locale->text('Steuersatz'), },
 
1370     'payment_terms'           => { 'text' => $locale->text('Payment Terms'), },
 
1371     'charts'                  => { 'text' => $locale->text('Buchungskonto'), },
 
1374   foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid employee shippingpoint shipvia transaction_description)) {
 
1375     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
 
1376     $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
 
1379   my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
 
1381   $form->{"l_type"} = "Y";
 
1382   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
 
1384   $report->set_columns(%column_defs);
 
1385   $report->set_column_order(@columns);
 
1387   $report->set_export_options('ar_transactions', @hidden_variables, qw(sort sortdir));
 
1389   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
 
1392   if ($form->{customer}) {
 
1393     push @options, $locale->text('Customer') . " : $form->{customer}";
 
1395   if ($form->{department}) {
 
1396     my ($department) = split /--/, $form->{department};
 
1397     push @options, $locale->text('Department') . " : $department";
 
1399   if ($form->{invnumber}) {
 
1400     push @options, $locale->text('Invoice Number') . " : $form->{invnumber}";
 
1402   if ($form->{ordnumber}) {
 
1403     push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
 
1405   if ($form->{notes}) {
 
1406     push @options, $locale->text('Notes') . " : $form->{notes}";
 
1408   if ($form->{transaction_description}) {
 
1409     push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
 
1411   if ($form->{transdatefrom}) {
 
1412     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
 
1414   if ($form->{transdateto}) {
 
1415     push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
 
1417   if ($form->{open}) {
 
1418     push @options, $locale->text('Open');
 
1420   if ($form->{closed}) {
 
1421     push @options, $locale->text('Closed');
 
1424   $report->set_options('top_info_text'        => join("\n", @options),
 
1425                        'raw_bottom_info_text' => $form->parse_html_template('ar/ar_transactions_bottom'),
 
1426                        'output_format'        => 'HTML',
 
1427                        'title'                => $form->{title},
 
1428                        'attachment_basename'  => $locale->text('invoice_list') . strftime('_%Y%m%d', localtime time),
 
1430   $report->set_options_from_form();
 
1432   # add sort and escape callback, this one we use for the add sub
 
1433   $form->{callback} = $href .= "&sort=$form->{sort}";
 
1435   # escape callback for href
 
1436   $callback = $form->escape($href);
 
1438   my @subtotal_columns = qw(netamount amount paid due marge_total marge_percent);
 
1440   my %totals    = map { $_ => 0 } @subtotal_columns;
 
1441   my %subtotals = map { $_ => 0 } @subtotal_columns;
 
1445   foreach my $ar (@{ $form->{AR} }) {
 
1446     $ar->{tax} = $ar->{amount} - $ar->{netamount};
 
1447     $ar->{due} = $ar->{amount} - $ar->{paid};
 
1449     map { $subtotals{$_} += $ar->{$_};
 
1450           $totals{$_}    += $ar->{$_} } @subtotal_columns;
 
1452     $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
 
1453     $totals{marge_percent}    = $totals{netamount}    ? ($totals{marge_total}    * 100 / $totals{netamount}   ) : 0;
 
1455     map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(netamount tax amount paid due marge_total marge_percent);
 
1457     my $is_storno  = $ar->{storno} &&  $ar->{storno_id};
 
1458     my $has_storno = $ar->{storno} && !$ar->{storno_id};
 
1461       $has_storno       ? $locale->text("Invoice with Storno (abbreviation)") :
 
1462       $is_storno        ? $locale->text("Storno (one letter abbreviation)") :
 
1463       $ar->{amount} < 0 ? $locale->text("Credit note (one letter abbreviation)") :
 
1464       $ar->{invoice}    ? $locale->text("Invoice (one letter abbreviation)") :
 
1465                           $locale->text("AR Transaction (abbreviation)");
 
1469     foreach my $column (@columns) {
 
1471         'data'  => $ar->{$column},
 
1472         'align' => $column_alignment{$column},
 
1476     $row->{invnumber}->{link} = build_std_url("script=" . ($ar->{invoice} ? 'is.pl' : 'ar.pl'), 'action=edit')
 
1477       . "&id=" . E($ar->{id}) . "&callback=${callback}";
 
1479     my $row_set = [ $row ];
 
1481     if (($form->{l_subtotal} eq 'Y')
 
1482         && (($idx == (scalar @{ $form->{AR} } - 1))
 
1483             || ($ar->{ $form->{sort} } ne $form->{AR}->[$idx + 1]->{ $form->{sort} }))) {
 
1484       push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
 
1487     $report->add_data($row_set);
 
1492   $report->add_separator();
 
1493   $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
 
1495   $report->generate_with_headers();
 
1497   $main::lxdebug->leave_sub();
 
1501   $main::lxdebug->enter_sub();
 
1503   $main::auth->assert('general_ledger');
 
1505   my $form     = $main::form;
 
1506   my %myconfig = %main::myconfig;
 
1507   my $locale   = $main::locale;
 
1509   # don't cancel cancelled transactions
 
1510   if (IS->has_storno(\%myconfig, $form, 'ar')) {
 
1511     $form->{title} = $locale->text("Cancel Accounts Receivables Transaction");
 
1512     $form->error($locale->text("Transaction has already been cancelled!"));
 
1515   AR->storno($form, \%myconfig, $form->{id});
 
1517   # saving the history
 
1518   if(!exists $form->{addition} && $form->{id} ne "") {
 
1519     $form->{snumbers} = "ordnumber_$form->{ordnumber}";
 
1520     $form->{addition} = "STORNO";
 
1521     $form->save_history($form->dbconnect(\%myconfig));
 
1523   # /saving the history
 
1525   $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
 
1527   $main::lxdebug->leave_sub();