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')
 
  80   $main::lxdebug->enter_sub();
 
  82   $main::auth->assert('general_ledger');
 
  84   my $form     = $main::form;
 
  85   my %myconfig = %main::myconfig;
 
  87   return $main::lxdebug->leave_sub() if (load_draft_maybe());
 
  90   if(!exists $form->{addition} && ($form->{id} ne "")) {
 
  91     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
  92     $form->{addition} = "ADDED";
 
  97   $form->{title}    = "Add";
 
  98   $form->{callback} = "ar.pl?action=add" unless $form->{callback};
 
 100   AR->get_transdate(\%myconfig, $form);
 
 101   $form->{initial_transdate} = $form->{transdate};
 
 103   $form->{transdate} = $form->{initial_transdate};
 
 105   $main::lxdebug->leave_sub();
 
 109   $main::lxdebug->enter_sub();
 
 111   $main::auth->assert('general_ledger');
 
 113   my $form     = $main::form;
 
 115   # show history button
 
 116   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
 
 117   #/show hhistory button
 
 118   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
 
 119   $form->{title} = "Edit";
 
 124   $main::lxdebug->leave_sub();
 
 128   $main::lxdebug->enter_sub();
 
 130   $main::auth->assert('general_ledger');
 
 132   my $form     = $main::form;
 
 137   $main::lxdebug->leave_sub();
 
 141   $main::lxdebug->enter_sub();
 
 143   $main::auth->assert('general_ledger');
 
 145   my $form     = $main::form;
 
 146   my %myconfig = %main::myconfig;
 
 148   my ($duedate, $taxincluded);
 
 150   $form->create_links("AR", \%myconfig, "customer");
 
 151   $duedate = $form->{duedate};
 
 153   $taxincluded = $form->{taxincluded};
 
 154   my $id = $form->{id};
 
 155   IS->get_customer(\%myconfig, \%$form);
 
 156   $form->{taxincluded} = $taxincluded;
 
 159   $form->{duedate}     = $duedate if $duedate;
 
 160   $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
 
 161   $form->{rowcount}    = 1;
 
 164   $form->{notes} = $form->{intnotes} unless $form->{notes};
 
 167   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 169   map { $form->{selectcurrency} .= "<option>$_\n" } $form->get_all_currencies(\%myconfig);
 
 172   if (@{ $form->{all_customer} || [] }) {
 
 173     $form->{customer} = "$form->{customer}--$form->{customer_id}";
 
 174     map { $form->{selectcustomer} .= "<option>$_->{name}--$_->{id}\n" }
 
 175       (@{ $form->{all_customer} });
 
 179   if (@{ $form->{all_departments} || [] }) {
 
 180     $form->{selectdepartment} = "<option>\n";
 
 181     $form->{department}       = "$form->{department}--$form->{department_id}";
 
 184       $form->{selectdepartment} .=
 
 185         "<option>$_->{description}--$_->{id}\n"
 
 186     } (@{ $form->{all_departments} || [] });
 
 189   $form->{employee} = "$form->{employee}--$form->{employee_id}";
 
 192   if (@{ $form->{all_employees} || [] }) {
 
 193     $form->{selectemployee} = "";
 
 194     map { $form->{selectemployee} .= "<option>$_->{name}--$_->{id}\n" }
 
 195       (@{ $form->{all_employees} || [] });
 
 198   # build the popup menus
 
 199   $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
 
 201   AR->setup_form($form);
 
 204     ($form->datetonum($form->{transdate}, \%myconfig) <=
 
 205      $form->datetonum($form->{closedto}, \%myconfig));
 
 207   $main::lxdebug->leave_sub();
 
 211   $main::lxdebug->enter_sub();
 
 213   $main::auth->assert('general_ledger');
 
 215   my $form     = $main::form;
 
 216   my %myconfig = %main::myconfig;
 
 217   my $locale   = $main::locale;
 
 218   my $cgi      = $main::cgi;
 
 220   my ($title, $readonly, $exchangerate, $rows);
 
 221   my ($taxincluded, $notes, $department, $customer, $employee, $amount, $project);
 
 222   my ($jsscript, $button1, $button2, $onload);
 
 223   my ($selectAR_amount, $selectAR_paid, $ARselected, $tax);
 
 224   my (@column_index, %column_data);
 
 227   $title = $form->{title};
 
 228   $form->{title} = $locale->text("$title Accounts Receivables Transaction");
 
 230   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
 
 232   # $locale->text('Add Accounts Receivables Transaction')
 
 233   # $locale->text('Edit Accounts Receivables Transaction')
 
 234   $form->{javascript} = qq|<script type="text/javascript">
 
 236   function setTaxkey(accno, row) {
 
 237     var taxkey = accno.options[accno.selectedIndex].value;
 
 238     var reg = /--([0-9]*)/;
 
 239     var found = reg.exec(taxkey);
 
 240     var index = found[1];
 
 241     index = parseInt(index);
 
 242     var tax = 'taxchart_' + row;
 
 243     for (var i = 0; i < document.getElementById(tax).options.length; ++i) {
 
 244       var reg2 = new RegExp("^"+ index, "");
 
 245       if (reg2.exec(document.getElementById(tax).options[i].value)) {
 
 246         document.getElementById(tax).options[i].selected = true;
 
 253   # show history button js
 
 254   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
 
 255   #/show history button js
 
 256   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
 
 257   $readonly = ($form->{id}) ? "readonly" : "";
 
 260     ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
 
 261   $readonly = ($form->{radier}) ? "" : $readonly;
 
 263   # set option selected
 
 264   foreach my $item (qw(customer currency department employee)) {
 
 265     $form->{"select$item"} =~ s/ selected//;
 
 266     $form->{"select$item"} =~
 
 267       s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
 
 270   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'buy');
 
 271   $form->{exchangerate} = $form->{forex} if $form->{forex};
 
 274   $form->{exchangerate} =
 
 275     $form->format_amount(\%myconfig, $form->{exchangerate});
 
 277   if ($form->{exchangerate} == 0) {
 
 278     $form->{exchangerate} = "";
 
 281   $form->{creditlimit} =
 
 282     $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
 
 283   $form->{creditremaining} =
 
 284     $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
 
 287 <input type=hidden name=forex value=$form->{forex}>
 
 289   if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
 
 290     if ($form->{forex}) {
 
 292         <th align=right>| . $locale->text('Exchangerate') . qq|</th>
 
 293         <td><input type=hidden name=exchangerate value=$form->{exchangerate}>$form->{exchangerate}</td>
 
 297         <th align=right>| . $locale->text('Exchangerate') . qq|</th>
 
 298         <td><input name=exchangerate size=10 value=$form->{exchangerate}></td>
 
 305                 <td align=right><input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}></td>
 
 306                 <th align=left nowrap>| . $locale->text('Tax Included') . qq|</th>
 
 310   if (($rows = $form->numtextrows($form->{notes}, 50)) < 2) {
 
 314     qq|<textarea name=notes rows=$rows cols=50 wrap=soft>$form->{notes}</textarea>|;
 
 318                 <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
 
 319                 <td colspan=3><select name=department>$form->{selectdepartment}</select>
 
 320                 <input type=hidden name=selectdepartment value="$form->{selectdepartment}">
 
 323 | if $form->{selectdepartment};
 
 325   my $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
 
 327   $customer = ($form->{selectcustomer})
 
 328     ? qq|<select name="customer" onchange="document.getElementById('update_button').click();">$form->{selectcustomer}</select>|
 
 329     : qq|<input name=customer value="$form->{customer}" size=35>|;
 
 332                 <input type=hidden name=employee value="$form->{employee}">
 
 335   if ($form->{selectemployee}) {
 
 338                 <th align=right nowrap>| . $locale->text('Salesperson') . qq|</th>
 
 339                 <td  colspan=2><select name=employee>$form->{selectemployee}</select></td>
 
 340                 <input type=hidden name=selectemployee value="$form->{selectemployee}">
 
 345   my @old_project_ids = ();
 
 346   map({ push(@old_project_ids, $form->{"project_id_$_"})
 
 347           if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
 
 349   $form->get_lists("projects"  => { "key"       => "ALL_PROJECTS",
 
 351                                     "old_id"    => \@old_project_ids },
 
 352                    "charts"    => { "key"       => "ALL_CHARTS",
 
 353                                     "transdate" => $form->{transdate} },
 
 354                    "taxcharts" => { "key"       => "ALL_TAXCHARTS",
 
 355                                     "module"    => "AR" },);
 
 357   map({ $_->{link_split} = [ split(/:/, $_->{link}) ]; }
 
 358       @{ $form->{ALL_CHARTS} });
 
 360   my %project_labels = ();
 
 361   my @project_values = ("");
 
 362   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
 
 363     push(@project_values, $item->{"id"});
 
 364     $project_labels{$item->{"id"}} = $item->{"projectnumber"};
 
 367   my (%AR_amount_labels, @AR_amount_values);
 
 368   my (%AR_labels, @AR_values);
 
 369   my (%AR_paid_labels, @AR_paid_values);
 
 373   foreach my $item (@{ $form->{ALL_CHARTS} }) {
 
 374     if (grep({ $_ eq "AR_amount" } @{ $item->{link_split} })) {
 
 375       $taxchart_init = $item->{tax_id} if ($taxchart_init eq "");
 
 376       my $key = "$item->{accno}--$item->{tax_id}";
 
 377       push(@AR_amount_values, $key);
 
 378       $AR_amount_labels{$key} =
 
 379         "$item->{accno}--$item->{description}";
 
 381     } elsif (grep({ $_ eq "AR" } @{ $item->{link_split} })) {
 
 382       push(@AR_values, $item->{accno});
 
 383       $AR_labels{$item->{accno}} = "$item->{accno}--$item->{description}";
 
 385     } elsif (grep({ $_ eq "AR_paid" } @{ $item->{link_split} })) {
 
 386       push(@AR_paid_values, $item->{accno});
 
 387       $AR_paid_labels{$item->{accno}} =
 
 388         "$item->{accno}--$item->{description}";
 
 391     $charts{$item->{accno}} = $item;
 
 394   my %taxchart_labels = ();
 
 395   my @taxchart_values = ();
 
 397   foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
 
 398     my $key = "$item->{id}--$item->{rate}";
 
 399     $taxchart_init = $key if ($taxchart_init eq $item->{id});
 
 400     push(@taxchart_values, $key);
 
 401     $taxchart_labels{$key} =
 
 402       "$item->{taxdescription} " . ($item->{rate} * 100) . ' %';
 
 403     $taxcharts{$item->{id}} = $item;
 
 406   $form->{fokus} = "arledger.customer";
 
 408   # use JavaScript Calendar or not
 
 409   $form->{jsscript} = 1;
 
 411   if ($form->{jsscript}) {
 
 413     # with JavaScript Calendar
 
 415        <td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\"></td>
 
 416        <td><input type=button name=transdate id="trigger1" value=|
 
 417       . $locale->text('button') . qq|></td>
 
 420        <td><input name=duedate id=duedate size=11 title="$myconfig{dateformat}" value="$form->{duedate}" onBlur=\"check_right_date_format(this)\"></td>
 
 421        <td><input type=button name=duedate id="trigger2" value=|
 
 422       . $locale->text('button') . qq|></td></td>
 
 427       Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1",
 
 428                           "duedate", "BL", "trigger2");
 
 431     # without JavaScript Calendar
 
 433       qq|<td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\"></td>|;
 
 435       qq|<td><input name=duedate id=duedate size=11 title="$myconfig{dateformat}" value="$form->{duedate}" onBlur=\"check_right_date_format(this)\"></td>|;
 
 438   my $follow_up_vc         =  $form->{customer};
 
 439   $follow_up_vc            =~ s/--.*?//;
 
 440   my $follow_up_trans_info =  "$form->{invnumber} ($follow_up_vc)";
 
 442   $form->{javascript} .=
 
 443     qq|<script type="text/javascript" src="js/common.js"></script>| .
 
 444     qq|<script type="text/javascript" src="js/show_vc_details.js"></script>| .
 
 445     qq|<script type="text/javascript" src="js/follow_up.js"></script>|;
 
 448   $onload = qq|focus()|;
 
 449   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
 
 450   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
 
 452 <body onLoad="$onload">
 
 454 <form method=post name="arledger" action=$form->{script}>
 
 456 <input type=hidden name=id value=$form->{id}>
 
 457 <input type=hidden name=sort value=$form->{sort}>
 
 458 <input type=hidden name=closedto value=$form->{closedto}>
 
 459 <input type=hidden name=locked value=$form->{locked}>
 
 460 <input type=hidden name=title value="$title">
 
 461 <input type="hidden" name="follow_up_trans_id_1" value="| . H($form->{id}) . qq|">
 
 462 <input type="hidden" name="follow_up_trans_type_1" value="ar_transaction">
 
 463 <input type="hidden" name="follow_up_trans_info_1" value="| . H($follow_up_trans_info) . qq|">
 
 464 <input type="hidden" name="follow_up_rowcount" value="1">
 
 466 | . ($form->{saved_message} ? qq|<p>$form->{saved_message}</p>| : "") . qq|
 
 470     <th class=listtop>$form->{title}</th>
 
 480                 <th align="right" nowrap>| . $locale->text('Customer') . qq|</th>
 
 481                 <td colspan=3>$customer <input type="button" value="| . $locale->text('Details (one letter abbreviation)') . qq|" onclick="show_vc_details('customer')"></td>
 
 482                 <input type=hidden name=selectcustomer value="$form->{selectcustomer}">
 
 483                 <input type=hidden name=oldcustomer value="$form->{oldcustomer}">
 
 484                 <input type=hidden name=customer_id value="$form->{customer_id}">
 
 485                 <input type=hidden name=terms value=$form->{terms}>
 
 492                       <th align=left nowrap>| . $locale->text('Credit Limit') . qq|</th>
 
 493                       <td>$form->{creditlimit}</td>
 
 494                       <th align=left nowrap>| . $locale->text('Remaining') . qq|</th>
 
 495                       <td class="plus$n">$form->{creditremaining}</td>
 
 496                       <input type=hidden name=creditlimit value=$form->{creditlimit}>
 
 497                       <input type=hidden name=creditremaining value=$form->{creditremaining}>
 
 503                 <th align=right>| . $locale->text('Currency') . qq|</th>
 
 504                 <td><select name=currency>$form->{selectcurrency}</select></td>
 
 505                 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
 
 506                 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
 
 507                 <input type=hidden name=fxgain_accno value=$form->{fxgain_accno}>
 
 508                 <input type=hidden name=fxloss_accno value=$form->{fxloss_accno}>
 
 519                 <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
 
 520                 <td><input name=invnumber size=11 value="$form->{invnumber}"></td>
 
 523                 <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
 
 524                 <td><input name=ordnumber size=11 value="$form->{ordnumber}"></td>
 
 527                 <th align=right nowrap>| . $locale->text('Invoice Date') . qq|</th>
 
 531                 <th align=right nowrap>| . $locale->text('Due Date') . qq|</th>
 
 542   <input type=hidden name=rowcount value=$form->{rowcount}>
 
 546            <tr class=listheading>
 
 547           <th class=listheading style="width:15%">|
 
 548     . $locale->text('Account') . qq|</th>
 
 549           <th class=listheading style="width:10%">|
 
 550     . $locale->text('Amount') . qq|</th>
 
 551           <th class=listheading style="width:10%">|
 
 552     . $locale->text('Tax') . qq|</th>
 
 553           <th class=listheading style="width:5%">|
 
 554     . $locale->text('Taxkey') . qq|</th>
 
 555           <th class=listheading style="width:10%">|
 
 556     . $locale->text('Project') . qq|</th>
 
 560   $amount  = $locale->text('Amount');
 
 561   $project = $locale->text('Project');
 
 563   for my $i (1 .. $form->{rowcount}) {
 
 566     $form->{"amount_$i"} =
 
 567       $form->format_amount(\%myconfig, $form->{"amount_$i"}, 2);
 
 568     $form->{"tax_$i"} = $form->format_amount(\%myconfig, $form->{"tax_$i"}, 2);
 
 570     my $selected_accno_full;
 
 571     my ($accno_row) = split(/--/, $form->{"AR_amount_$i"});
 
 572     my $item = $charts{$accno_row};
 
 573     $selected_accno_full = "$item->{accno}--$item->{tax_id}";
 
 575     my $selected_taxchart = $form->{"taxchart_$i"};
 
 576     my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full);
 
 577     my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_AR_amount_$i"});
 
 579     if ($previous_accno &&
 
 580         ($previous_accno eq $selected_accno) &&
 
 581         ($previous_tax_id ne $selected_tax_id)) {
 
 582       my $item = $taxcharts{$selected_tax_id};
 
 583       $selected_taxchart = "$item->{id}--$item->{rate}";
 
 586     if (!$form->{"taxchart_$i"}) {
 
 587       if ($form->{"AR_amount_$i"} =~ m/.--./) {
 
 588         $selected_taxchart = join '--', map { ($_->{id}, $_->{rate}) } first { $_->{id} == $item->{tax_id} } @{ $form->{ALL_TAXCHARTS} };
 
 590         $selected_taxchart = $taxchart_init;
 
 595       NTI($cgi->popup_menu('-name' => "AR_amount_$i",
 
 596                            '-id' => "AR_amount_$i",
 
 597                            '-style' => 'width:400px',
 
 598                            '-onChange' => "setTaxkey(this, $i)",
 
 599                            '-values' => \@AR_amount_values,
 
 600                            '-labels' => \%AR_amount_labels,
 
 601                            '-default' => $selected_accno_full))
 
 602       . $cgi->hidden('-name' => "previous_AR_amount_$i",
 
 603                      '-default' => $selected_accno_full);
 
 606       NTI($cgi->popup_menu('-name' => "taxchart_$i",
 
 607                            '-id' => "taxchart_$i",
 
 608                            '-style' => 'width:200px',
 
 609                            '-values' => \@taxchart_values,
 
 610                            '-labels' => \%taxchart_labels,
 
 611                            '-default' => $selected_taxchart))
 
 615       NTI($cgi->popup_menu('-name' => "project_id_$i",
 
 616                            '-values' => \@project_values,
 
 617                            '-labels' => \%project_labels,
 
 618                            '-default' => $form->{"project_id_$i"} ));
 
 622           <td>$selectAR_amount</td>
 
 623           <td><input name="amount_$i" size=10 value=$form->{"amount_$i"}></td>
 
 624           <td><input type="hidden" name="tax_$i" value="$form->{"tax_$i"}">$form->{"tax_$i"}</td>
 
 626           <td>$projectnumber</td>
 
 633   $form->{invtotal_unformatted} = $form->{invtotal};
 
 634   $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
 
 637     NTI($cgi->popup_menu('-name' => "ARselected", '-id' => "ARselected",
 
 638                          '-style' => 'width:400px',
 
 639                          '-values' => \@AR_values, '-labels' => \%AR_labels,
 
 640                          '-default' => $form->{ARselected}));
 
 649           <td>${ARselected}</td>
 
 650           <th align=left>$form->{invtotal}</th>
 
 652           <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
 
 653           <input type=hidden name=oldtotalpaid value=$form->{oldtotalpaid}>
 
 655           <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
 
 668           <th align=left width=1%>| . $locale->text('Notes') . qq|</th>
 
 669           <td align=left>$notes</td>
 
 677         <tr class=listheading>
 
 678           <th colspan=7 class=listheading>|
 
 679     . $locale->text('Incoming Payments') . qq|</th>
 
 683   if ($form->{defaultcurrency} && ($form->{currency} eq $form->{defaultcurrency})) {
 
 684     @column_index = qw(datepaid source memo paid AR_paid paid_project_id);
 
 686     @column_index = qw(datepaid source memo paid exchangerate AR_paid paid_project_id);
 
 689   $column_data{datepaid}     = "<th>" . $locale->text('Date') . "</th>";
 
 690   $column_data{paid}         = "<th>" . $locale->text('Amount') . "</th>";
 
 691   $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
 
 692   $column_data{AR_paid}      = "<th>" . $locale->text('Account') . "</th>";
 
 693   $column_data{source}       = "<th>" . $locale->text('Source') . "</th>";
 
 694   $column_data{memo}         = "<th>" . $locale->text('Memo') . "</th>";
 
 695   $column_data{paid_project_id} = "<th>" . $locale->text('Project Number') . "</th>";
 
 700   map { print "$column_data{$_}\n" } @column_index;
 
 706   $form->{totalpaid} = 0;
 
 708   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
 
 709   for my $i (1 .. $form->{paidaccounts}) {
 
 715       NTI($cgi->popup_menu('-name' => "AR_paid_$i",
 
 716                            '-id' => "AR_paid_$i",
 
 717                            '-values' => \@AR_paid_values,
 
 718                            '-labels' => \%AR_paid_labels,
 
 719                            '-default' => $form->{"AR_paid_$i"}));
 
 721     $form->{totalpaid} += $form->{"paid_$i"};
 
 724     if ($form->{"paid_$i"}) {
 
 726         $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
 
 728     $form->{"exchangerate_$i"} =
 
 729       $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
 
 731     if ($form->{"exchangerate_$i"} == 0) {
 
 732       $form->{"exchangerate_$i"} = "";
 
 735     $exchangerate = qq| |;
 
 736     if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
 
 737       if ($form->{"forex_$i"}) {
 
 739           qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
 
 742           qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
 
 747 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
 
 751       qq|<td align=center><input name="paid_$i" size=11 value="$form->{"paid_$i"}" onBlur=\"check_right_number_format(this)\"></td>|;
 
 752     $column_data{AR_paid} =
 
 753       qq|<td align=center>${selectAR_paid}</td>|;
 
 754     $column_data{exchangerate} = qq|<td align=center>$exchangerate</td>|;
 
 755     $column_data{datepaid}     =
 
 756       qq|<td align=center><input name="datepaid_$i" id="datepaid_$i" size=11 value="$form->{"datepaid_$i"}" onBlur=\"check_right_date_format(this)\">
 
 757          <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
 
 758     $column_data{source} =
 
 759       qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
 
 761       qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
 
 763     $column_data{paid_project_id} =
 
 765       . NTI($cgi->popup_menu('-name' => "paid_project_id_$i",
 
 766                              '-values' => \@project_values,
 
 767                              '-labels' => \%project_labels,
 
 768                              '-default' => $form->{"paid_project_id_$i"} ))
 
 771     map { print qq|$column_data{$_}\n| } @column_index;
 
 776     push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
 
 779   my $paid_missing = $form->{invtotal_unformatted} - $form->{totalpaid};
 
 785           <td align="center">| . $locale->text('Total') . qq|</td>
 
 786           <td align="center">| . H($form->format_amount(\%myconfig, $form->{totalpaid}, 2)) . qq|</td>
 
 791           <td align="center">| . $locale->text('Missing amount') . qq|</td>
 
 792           <td align="center">| . H($form->format_amount(\%myconfig, $paid_missing, 2)) . qq|</td>
 
 794 | . $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
 
 796 <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
 
 802     <td><hr size=3 noshade></td>
 
 807   $main::lxdebug->leave_sub();
 
 811   $main::lxdebug->enter_sub();
 
 813   $main::auth->assert('general_ledger');
 
 815   my $form     = $main::form;
 
 816   my %myconfig = %main::myconfig;
 
 817   my $locale   = $main::locale;
 
 818   my $cgi      = $main::cgi;
 
 820   my ($transdate, $closedto);
 
 822   my $follow_ups_block;
 
 824     my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
 
 826     if (@{ $follow_ups} ) {
 
 827       my $num_due       = sum map { $_->{due} * 1 } @{ $follow_ups };
 
 828       $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>|;
 
 836 <input name=gldate type=hidden value="| . Q($form->{gldate}) . qq|">
 
 838 <input name=callback type=hidden value="$form->{callback}">
 
 840 . $cgi->hidden('-name' => 'draft_id', '-default' => [$form->{draft_id}])
 
 841 . $cgi->hidden('-name' => 'draft_description', '-default' => [$form->{draft_description}])
 
 847   if (!$form->{id} && $form->{draft_id}) {
 
 848     print(NTI($cgi->checkbox('-name' => 'remove_draft', '-id' => 'remove_draft',
 
 849                              '-value' => 1, '-checked' => $form->{remove_draft},
 
 851           qq| <label for="remove_draft">| .
 
 852           $locale->text("Remove draft when posting") .
 
 856   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
 
 857   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
 
 859   print qq|<input class="submit" type="submit" name="action" id="update_button" value="| . $locale->text('Update') . qq|">\n|;
 
 861   # ToDO: - insert a global check for stornos, so that a storno is only possible a limited time after saving it
 
 862   print qq| <input class=submit type=submit name=action value="| . $locale->text('Storno') . qq|"> |
 
 863     if ($form->{id} && !IS->has_storno(\%myconfig, $form, 'ar') && !IS->is_storno(\%myconfig, $form, 'ar') && (($form->{totalpaid} == 0) || ($form->{totalpaid} eq "")));
 
 866     if ($form->{radier}) {
 
 868         <input class=submit type=submit name=action value="| . $locale->text('Post') .            qq|">
 
 869         <input class=submit type=submit name=action value="| . $locale->text('Delete') .          qq|"> |;
 
 871     if ($transdate > $closedto) {
 
 873         <input class=submit type=submit name=action value="| . $locale->text('Use As Template') . qq|"> |;
 
 876         <input class=submit type=submit name=action value="| . $locale->text('Post Payment') .    qq|">
 
 877         <input type="button" class="submit" onclick="follow_up_window()" value="|
 
 878       . $locale->text('Follow-Up')
 
 882     if ($transdate > $closedto) {
 
 883       print qq| <input class=submit type=submit name=action value="| . $locale->text('Post') .     qq|"> | .
 
 884         NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'), '-class' => 'submit'));
 
 888   if ($form->{menubar}) {
 
 889     require "bin/mozilla/menu.pl";
 
 892   # button for saving history
 
 893   if($form->{id} ne "") {
 
 894     print qq| <input type=button class=submit onclick=set_history_window($form->{id}); name=history id=history value=| . $locale->text('history') . qq|> |;
 
 896   # /button for saving history
 
 897   # mark_as_paid button
 
 898   if($form->{id} ne "") {
 
 899     print qq|<input type="submit" class="submit" name="action" value="|
 
 900           . $locale->text('mark as paid') . qq|">|;
 
 902   # /mark_as_paid button
 
 911   $main::lxdebug->leave_sub();
 
 915   $main::lxdebug->enter_sub();
 
 917   $main::auth->assert('general_ledger');
 
 919   my $form     = $main::form;
 
 920   my %myconfig = %main::myconfig;
 
 922   &mark_as_paid_common(\%myconfig,"ar");
 
 924   $main::lxdebug->leave_sub();
 
 928   $main::lxdebug->enter_sub();
 
 930   $main::auth->assert('general_ledger');
 
 932   my $form     = $main::form;
 
 933   my %myconfig = %main::myconfig;
 
 937   my ($totaltax, $exchangerate);
 
 939   $form->{invtotal} = 0;
 
 941   delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
 
 943   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
 
 944     qw(exchangerate creditlimit creditremaining);
 
 946   my @flds  = qw(amount AR_amount projectnumber oldprojectnumber project_id);
 
 950   for my $i (1 .. $form->{rowcount}) {
 
 951     $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
 
 952     $form->{"tax_$i"} = $form->parse_amount(\%myconfig, $form->{"tax_$i"});
 
 953     if ($form->{"amount_$i"}) {
 
 956       my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
 
 958         if ($form->{taxincluded}) {
 
 959           $form->{"tax_$i"} = $form->{"amount_$i"} / ($rate + 1) * $rate;
 
 961           $form->{"tax_$i"} = $form->{"amount_$i"} * $rate;
 
 964         $form->{"tax_$i"} = 0;
 
 966       $form->{"tax_$i"} = $form->round_amount($form->{"tax_$i"}, 2);
 
 968       $totaltax += $form->{"tax_$i"};
 
 969       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
 
 974   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
 
 975   $form->{rowcount} = $count + 1;
 
 976   map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
 
 978   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'buy');
 
 979   $form->{exchangerate} = $form->{forex} if $form->{forex};
 
 981   $form->{invdate} = $form->{transdate};
 
 983   $form->{invdate} = $form->{transdate};
 
 985   my %saved_variables = map +( $_ => $form->{$_} ), qw(AR AR_amount_1 taxchart_1 oldcustomer);
 
 987   &check_name("customer");
 
 989   # check_name ruft get_customer auf, oldcustomer wird Ã¼berschrieben, daher wird dies vorher gemerkt
 
 990   # get_customer holt Bemerkungen als intnotes, für Debitorenbuchungen gibt es aber nur das Feld notes
 
 991   $form->{notes} = $form->{intnotes} if $saved_variables{oldcustomer} ne $form->{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       $form->{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} - $form->{totalpaid} + $form->{oldtotalpaid} -
 
1019      $form->{oldinvtotal});
 
1020   $form->{oldinvtotal}  = $form->{invtotal};
 
1021   $form->{oldtotalpaid} = $form->{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   my $invdate = $form->datetonum($form->{transdate}, \%myconfig);
 
1044   for my $i (1 .. $form->{paidaccounts}) {
 
1046     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
 
1047       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
 
1049       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
1051       $form->error($locale->text('Cannot post payment for a closed period!')) if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
 
1053       if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
 
1054 #        $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid);
 
1055         $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
 
1060   ($form->{AR})      = split /--/, $form->{AR};
 
1061   ($form->{AR_paid}) = split /--/, $form->{AR_paid};
 
1062   $form->redirect($locale->text('Payment posted!')) if (AR->post_payment(\%myconfig, \%$form));
 
1063   $form->error($locale->text('Cannot post payment!'));
 
1065   $main::lxdebug->leave_sub();
 
1070   $main::auth->assert('general_ledger');
 
1072   my $form     = $main::form;
 
1079   $main::lxdebug->enter_sub();
 
1081   $main::auth->assert('general_ledger');
 
1083   my $form     = $main::form;
 
1084   my %myconfig = %main::myconfig;
 
1085   my $locale   = $main::locale;
 
1091   # check if there is an invoice number, invoice and due date
 
1092   $form->isblank("transdate", $locale->text('Invoice Date missing!'));
 
1093   $form->isblank("duedate",   $locale->text('Due Date missing!'));
 
1094   $form->isblank("customer",  $locale->text('Customer missing!'));
 
1096   if ($myconfig{mandatory_departments} && !$form->{department}) {
 
1097     $form->{saved_message} = $::locale->text('You have to specify a department.');
 
1102   my $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
 
1103   my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
 
1104   $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
 
1106   $form->error($locale->text('Zero amount posting!'))
 
1107     unless grep $_*1, map $form->parse_amount(\%myconfig, $form->{"amount_$_"}), 1..$form->{rowcount};
 
1109   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
 
1110     if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
 
1112   delete($form->{AR});
 
1114   for my $i (1 .. $form->{paidaccounts}) {
 
1115     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
 
1116       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
 
1118       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
1120       $form->error($locale->text('Cannot post payment for a closed period!'))
 
1121         if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
 
1123       if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
 
1124         $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
 
1125         $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
 
1130   # if oldcustomer ne customer redo form
 
1131   my ($customer) = split /--/, $form->{customer};
 
1132   if ($form->{oldcustomer} ne "$customer--$form->{customer_id}") {
 
1137   $form->{AR}{receivables} = $form->{ARselected};
 
1138   $form->{storno}          = 0;
 
1140   $form->{id} = 0 if $form->{postasnew};
 
1141   $form->error($locale->text('Cannot post transaction!')) unless AR->post_transaction(\%myconfig, \%$form);
 
1143   # saving the history
 
1144   if(!exists $form->{addition} && $form->{id} ne "") {
 
1145     $form->{snumbers} = "invnumber_$form->{invnumber}";
 
1146     $form->{addition} = "POSTED";
 
1147     $form->save_history;
 
1149   # /saving the history
 
1150   remove_draft() if $form->{remove_draft};
 
1152   $form->redirect($locale->text('Transaction posted!')) unless $inline;
 
1154   $main::lxdebug->leave_sub();
 
1158   $main::lxdebug->enter_sub();
 
1160   $main::auth->assert('general_ledger');
 
1162   my $form     = $main::form;
 
1163   my %myconfig = %main::myconfig;
 
1165   $form->{postasnew} = 1;
 
1166   # saving the history
 
1167   if(!exists $form->{addition} && $form->{id} ne "") {
 
1168     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
1169     $form->{addition} = "POSTED AS NEW";
 
1170     $form->save_history;
 
1172   # /saving the history
 
1175   $main::lxdebug->leave_sub();
 
1178 sub use_as_template {
 
1179   $main::lxdebug->enter_sub();
 
1181   $main::auth->assert('general_ledger');
 
1183   my $form     = $main::form;
 
1184   my %myconfig = %main::myconfig;
 
1186   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);
 
1187   $form->{paidaccounts} = 1;
 
1188   $form->{rowcount}--;
 
1189   $form->{invdate} = $form->current_date(\%myconfig);
 
1192   $main::lxdebug->leave_sub();
 
1196   $main::lxdebug->enter_sub();
 
1198   $main::auth->assert('general_ledger');
 
1200   my $form     = $main::form;
 
1201   my $locale   = $main::locale;
 
1203   $form->{title} = $locale->text('Confirm!');
 
1207   delete $form->{header};
 
1212 <form method=post action=$form->{script}>
 
1215   foreach my $key (keys %$form) {
 
1216     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
 
1217     $form->{$key} =~ s/\"/"/g;
 
1218     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
 
1222 <h2 class=confirm>$form->{title}</h2>
 
1225     . $locale->text('Are you sure you want to delete Transaction')
 
1226     . qq| $form->{invnumber}</h4>
 
1228 <input name=action class=submit type=submit value="|
 
1229     . $locale->text('Yes') . qq|">
 
1236   $main::lxdebug->leave_sub();
 
1240   $main::lxdebug->enter_sub();
 
1242   $main::auth->assert('general_ledger');
 
1244   my $form     = $main::form;
 
1245   my %myconfig = %main::myconfig;
 
1246   my $locale   = $main::locale;
 
1248   if (AR->delete_transaction(\%myconfig, \%$form)) {
 
1249     # saving the history
 
1250     if(!exists $form->{addition}) {
 
1251       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
1252       $form->{addition} = "DELETED";
 
1253       $form->save_history;
 
1255     # /saving the history
 
1256     $form->redirect($locale->text('Transaction deleted!'));
 
1258   $form->error($locale->text('Cannot delete transaction!'));
 
1260   $main::lxdebug->leave_sub();
 
1264   $main::lxdebug->enter_sub();
 
1266   $main::auth->assert('general_ledger | invoice_edit');
 
1268   my $form     = $main::form;
 
1269   my %myconfig = %main::myconfig;
 
1270   my $locale   = $main::locale;
 
1271   my $cgi      = $main::cgi;
 
1273   my ($customer, $department);
 
1274   my ($jsscript, $button1, $button2, $onload);
 
1276   # setup customer selection
 
1277   $form->all_vc(\%myconfig, "customer", "AR");
 
1279   $form->{title}    = $locale->text('AR Transactions');
 
1280   $form->{jsscript} = 1;
 
1282   # Auch in Rechnungsübersicht nach Kundentyp filtern - jan
 
1283   $form->get_lists("projects"       => { "key" => "ALL_PROJECTS", "all" => 1 },
 
1284                    "departments"    => "ALL_DEPARTMENTS",
 
1285                    "customers"      => "ALL_VC",
 
1286                    "employees"    => "ALL_EMPLOYEES",
 
1287                    "salesmen"     => "ALL_SALESMEN",
 
1288                    "business_types" => "ALL_BUSINESS_TYPES");
 
1289   $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
 
1291   # constants and subs for template
 
1292   $form->{jsscript}  = 1;
 
1293   $form->{vc_keys}   = sub { "$_[0]->{name}--$_[0]->{id}" };
 
1294   $form->{employee_labels} = sub { $_[0]->{"name"} || $_[0]->{"login"} };
 
1295   $form->{salesman_labels} = $form->{employee_labels};
 
1298   print $form->parse_html_template('ar/search', { %myconfig });
 
1300   $main::lxdebug->leave_sub();
 
1303 sub create_subtotal_row {
 
1304   $main::lxdebug->enter_sub();
 
1306   my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
 
1308   my $form     = $main::form;
 
1309   my %myconfig = %main::myconfig;
 
1311   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
 
1313   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
 
1315   $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
 
1317   map { $totals->{$_} = 0 } @{ $subtotal_columns };
 
1319   $main::lxdebug->leave_sub();
 
1324 sub ar_transactions {
 
1325   $main::lxdebug->enter_sub();
 
1327   $main::auth->assert('general_ledger | invoice_edit');
 
1329   my $form     = $main::form;
 
1330   my %myconfig = %main::myconfig;
 
1331   my $locale   = $main::locale;
 
1333   my ($callback, $href, @columns);
 
1335   $form->{customer} = $form->unescape($form->{customer});
 
1336   ($form->{customer}, $form->{customer_id}) = split(/--/, $form->{customer});
 
1338   report_generator_set_default_sort('transdate', 1);
 
1340   AR->ar_transactions(\%myconfig, \%$form);
 
1342   $form->{title} = $locale->text('AR Transactions');
 
1344   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
1347     qw(transdate id type invnumber ordnumber name netamount tax amount paid
 
1348        datepaid due duedate transaction_description notes salesman employee shippingpoint shipvia
 
1349        marge_total marge_percent globalprojectnumber customernumber country ustid taxzone payment_terms charts);
 
1351   my @hidden_variables = map { "l_${_}" } @columns;
 
1352   push @hidden_variables, "l_subtotal", qw(open closed customer invnumber ordnumber transaction_description notes project_id transdatefrom transdateto);
 
1354   $href = build_std_url('action=ar_transactions', grep { $form->{$_} } @hidden_variables);
 
1357     'transdate'               => { 'text' => $locale->text('Date'), },
 
1358     'id'                      => { 'text' => $locale->text('ID'), },
 
1359     'type'                    => { 'text' => $locale->text('Type'), },
 
1360     'invnumber'               => { 'text' => $locale->text('Invoice'), },
 
1361     'ordnumber'               => { 'text' => $locale->text('Order'), },
 
1362     'name'                    => { 'text' => $locale->text('Customer'), },
 
1363     'netamount'               => { 'text' => $locale->text('Amount'), },
 
1364     'tax'                     => { 'text' => $locale->text('Tax'), },
 
1365     'amount'                  => { 'text' => $locale->text('Total'), },
 
1366     'paid'                    => { 'text' => $locale->text('Paid'), },
 
1367     'datepaid'                => { 'text' => $locale->text('Date Paid'), },
 
1368     'due'                     => { 'text' => $locale->text('Amount Due'), },
 
1369     'duedate'                 => { 'text' => $locale->text('Due Date'), },
 
1370     'transaction_description' => { 'text' => $locale->text('Transaction description'), },
 
1371     'notes'                   => { 'text' => $locale->text('Notes'), },
 
1372     'salesman'                => { 'text' => $locale->text('Salesperson'), },
 
1373     'employee'                => { 'text' => $locale->text('Employee'), },
 
1374     'shippingpoint'           => { 'text' => $locale->text('Shipping Point'), },
 
1375     'shipvia'                 => { 'text' => $locale->text('Ship via'), },
 
1376     'globalprojectnumber'     => { 'text' => $locale->text('Project Number'), },
 
1377     'marge_total'             => { 'text' => $locale->text('Ertrag'), },
 
1378     'marge_percent'           => { 'text' => $locale->text('Ertrag prozentual'), },
 
1379     'customernumber'          => { 'text' => $locale->text('Customer Number'), },
 
1380     'country'                 => { 'text' => $locale->text('Country'), },
 
1381     'ustid'                   => { 'text' => $locale->text('USt-IdNr.'), },
 
1382     'taxzone'                 => { 'text' => $locale->text('Steuersatz'), },
 
1383     'payment_terms'           => { 'text' => $locale->text('Payment Terms'), },
 
1384     'charts'                  => { 'text' => $locale->text('Buchungskonto'), },
 
1387   foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid employee shippingpoint shipvia transaction_description)) {
 
1388     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
 
1389     $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
 
1392   my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
 
1394   $form->{"l_type"} = "Y";
 
1395   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
 
1397   $report->set_columns(%column_defs);
 
1398   $report->set_column_order(@columns);
 
1400   $report->set_export_options('ar_transactions', @hidden_variables, qw(sort sortdir));
 
1402   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
 
1405   if ($form->{customer}) {
 
1406     push @options, $locale->text('Customer') . " : $form->{customer}";
 
1408   if ($form->{department}) {
 
1409     my ($department) = split /--/, $form->{department};
 
1410     push @options, $locale->text('Department') . " : $department";
 
1412   if ($form->{department_id}) {
 
1413     push @options, $locale->text('Department Id') . " : $form->{department_id}";
 
1415   if ($form->{invnumber}) {
 
1416     push @options, $locale->text('Invoice Number') . " : $form->{invnumber}";
 
1418   if ($form->{ordnumber}) {
 
1419     push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
 
1421   if ($form->{notes}) {
 
1422     push @options, $locale->text('Notes') . " : $form->{notes}";
 
1424   if ($form->{transaction_description}) {
 
1425     push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
 
1427   if ($form->{transdatefrom}) {
 
1428     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
 
1430   if ($form->{transdateto}) {
 
1431     push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
 
1433   if ($form->{open}) {
 
1434     push @options, $locale->text('Open');
 
1436   if ($form->{closed}) {
 
1437     push @options, $locale->text('Closed');
 
1440   $report->set_options('top_info_text'        => join("\n", @options),
 
1441                        'raw_bottom_info_text' => $form->parse_html_template('ar/ar_transactions_bottom'),
 
1442                        'output_format'        => 'HTML',
 
1443                        'title'                => $form->{title},
 
1444                        'attachment_basename'  => $locale->text('invoice_list') . strftime('_%Y%m%d', localtime time),
 
1446   $report->set_options_from_form();
 
1447   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
1449   # add sort and escape callback, this one we use for the add sub
 
1450   $form->{callback} = $href .= "&sort=$form->{sort}";
 
1452   # escape callback for href
 
1453   $callback = $form->escape($href);
 
1455   my @subtotal_columns = qw(netamount amount paid due marge_total marge_percent);
 
1457   my %totals    = map { $_ => 0 } @subtotal_columns;
 
1458   my %subtotals = map { $_ => 0 } @subtotal_columns;
 
1462   foreach my $ar (@{ $form->{AR} }) {
 
1463     $ar->{tax} = $ar->{amount} - $ar->{netamount};
 
1464     $ar->{due} = $ar->{amount} - $ar->{paid};
 
1466     map { $subtotals{$_} += $ar->{$_};
 
1467           $totals{$_}    += $ar->{$_} } @subtotal_columns;
 
1469     $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
 
1470     $totals{marge_percent}    = $totals{netamount}    ? ($totals{marge_total}    * 100 / $totals{netamount}   ) : 0;
 
1472     map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(netamount tax amount paid due marge_total marge_percent);
 
1474     my $is_storno  = $ar->{storno} &&  $ar->{storno_id};
 
1475     my $has_storno = $ar->{storno} && !$ar->{storno_id};
 
1478       $has_storno       ? $locale->text("Invoice with Storno (abbreviation)") :
 
1479       $is_storno        ? $locale->text("Storno (one letter abbreviation)") :
 
1480       $ar->{amount} < 0 ? $locale->text("Credit note (one letter abbreviation)") :
 
1481       $ar->{invoice}    ? $locale->text("Invoice (one letter abbreviation)") :
 
1482                           $locale->text("AR Transaction (abbreviation)");
 
1486     foreach my $column (@columns) {
 
1488         'data'  => $ar->{$column},
 
1489         'align' => $column_alignment{$column},
 
1493     $row->{invnumber}->{link} = build_std_url("script=" . ($ar->{invoice} ? 'is.pl' : 'ar.pl'), 'action=edit')
 
1494       . "&id=" . E($ar->{id}) . "&callback=${callback}";
 
1496     my $row_set = [ $row ];
 
1498     if (($form->{l_subtotal} eq 'Y')
 
1499         && (($idx == (scalar @{ $form->{AR} } - 1))
 
1500             || ($ar->{ $form->{sort} } ne $form->{AR}->[$idx + 1]->{ $form->{sort} }))) {
 
1501       push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
 
1504     $report->add_data($row_set);
 
1509   $report->add_separator();
 
1510   $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
 
1512   $report->generate_with_headers();
 
1514   $main::lxdebug->leave_sub();
 
1518   $main::lxdebug->enter_sub();
 
1520   $main::auth->assert('general_ledger');
 
1522   my $form     = $main::form;
 
1523   my %myconfig = %main::myconfig;
 
1524   my $locale   = $main::locale;
 
1526   # don't cancel cancelled transactions
 
1527   if (IS->has_storno(\%myconfig, $form, 'ar')) {
 
1528     $form->{title} = $locale->text("Cancel Accounts Receivables Transaction");
 
1529     $form->error($locale->text("Transaction has already been cancelled!"));
 
1532   AR->storno($form, \%myconfig, $form->{id});
 
1534   # saving the history
 
1535   if(!exists $form->{addition} && $form->{id} ne "") {
 
1536     $form->{snumbers} = "ordnumber_$form->{ordnumber}";
 
1537     $form->{addition} = "STORNO";
 
1538     $form->save_history;
 
1540   # /saving the history
 
1542   $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
 
1544   $main::lxdebug->leave_sub();