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 #======================================================================
 
  32 #======================================================================
 
  34 use POSIX qw(strftime);
 
  35 use List::Util qw(sum);
 
  42 use SL::ReportGenerator;
 
  44 require "bin/mozilla/arap.pl";
 
  45 require "bin/mozilla/common.pl";
 
  46 require "bin/mozilla/drafts.pl";
 
  47 require "bin/mozilla/reportgenerator.pl";
 
  55 # this is for our long dates
 
  56 # $locale->text('January')
 
  57 # $locale->text('February')
 
  58 # $locale->text('March')
 
  59 # $locale->text('April')
 
  60 # $locale->text('May ')
 
  61 # $locale->text('June')
 
  62 # $locale->text('July')
 
  63 # $locale->text('August')
 
  64 # $locale->text('September')
 
  65 # $locale->text('October')
 
  66 # $locale->text('November')
 
  67 # $locale->text('December')
 
  69 # this is for our short month
 
  70 # $locale->text('Jan')
 
  71 # $locale->text('Feb')
 
  72 # $locale->text('Mar')
 
  73 # $locale->text('Apr')
 
  74 # $locale->text('May')
 
  75 # $locale->text('Jun')
 
  76 # $locale->text('Jul')
 
  77 # $locale->text('Aug')
 
  78 # $locale->text('Sep')
 
  79 # $locale->text('Oct')
 
  80 # $locale->text('Nov')
 
  81 # $locale->text('Dec')
 
  84   $main::lxdebug->enter_sub();
 
  86   my $form     = $main::form;
 
  87   my %myconfig = %main::myconfig;
 
  89   $main::auth->assert('general_ledger');
 
  91   return $main::lxdebug->leave_sub() if (load_draft_maybe());
 
  93   $form->{title} = "Add";
 
  95   $form->{callback} = "ap.pl?action=add&DONT_LOAD_DRAFT=1" unless $form->{callback};
 
  97   AP->get_transdate(\%myconfig, $form);
 
  98   $form->{initial_transdate} = $form->{transdate};
 
 100   $form->{transdate} = $form->{initial_transdate};
 
 103   $main::lxdebug->leave_sub();
 
 107   $main::lxdebug->enter_sub();
 
 109   my $form     = $main::form;
 
 111   $main::auth->assert('general_ledger');
 
 113   $form->{title} = "Edit";
 
 118   $main::lxdebug->leave_sub();
 
 122   $main::lxdebug->enter_sub();
 
 124   my $form     = $main::form;
 
 126   $main::auth->assert('general_ledger');
 
 131   $main::lxdebug->leave_sub();
 
 135   $main::lxdebug->enter_sub();
 
 137   my $form     = $main::form;
 
 138   my %myconfig = %main::myconfig;
 
 140   $main::auth->assert('general_ledger');
 
 142   $form->create_links("AP", \%myconfig, "vendor");
 
 143   my $taxincluded = $form->{taxincluded};
 
 144   my $duedate     = $form->{duedate};
 
 146   IR->get_vendor(\%myconfig, \%$form);
 
 147   $form->{taxincluded} = $taxincluded;
 
 148   $form->{duedate}   = $duedate if $duedate;
 
 149   $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
 
 150   $form->{rowcount}  = 1;
 
 152   # build the popup menus
 
 153   $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
 
 156   $form->{notes} = $form->{intnotes} unless $form->{notes};
 
 159   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 161   map { $form->{selectcurrency} .= "<option>$_\n" } $form->get_all_currencies(\%myconfig);
 
 164   if (@{ $form->{all_vendor} || [] }) {
 
 165     $form->{vendor} = qq|$form->{vendor}--$form->{vendor_id}|;
 
 166     map { $form->{selectvendor} .= "<option>$_->{name}--$_->{id}\n" }
 
 167       (@{ $form->{all_vendor} });
 
 171   if (@{ $form->{all_departments} || [] }) {
 
 172     $form->{selectdepartment} = "<option>\n";
 
 173     $form->{department}       = "$form->{department}--$form->{department_id}";
 
 176       $form->{selectdepartment} .=
 
 177         "<option>$_->{description}--$_->{id}\n"
 
 178     } (@{ $form->{all_departments} || [] });
 
 181   $form->{employee} = "$form->{employee}--$form->{employee_id}";
 
 183   AP->setup_form($form);
 
 186     ($form->datetonum($form->{transdate}, \%myconfig) <=
 
 187      $form->datetonum($form->{closedto}, \%myconfig));
 
 189   $main::lxdebug->leave_sub();
 
 193   $main::lxdebug->enter_sub();
 
 195   my $form     = $main::form;
 
 196   my %myconfig = %main::myconfig;
 
 197   my $locale   = $main::locale;
 
 198   my $cgi      = $::request->{cgi};
 
 200   $main::auth->assert('general_ledger');
 
 202   my $title = $form->{title};
 
 203   $form->{title} = $locale->text("$title Accounts Payables Transaction");
 
 205   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
 
 207   # type=submit $locale->text('Add Accounts Payables Transaction')
 
 208   # type=submit $locale->text('Edit Accounts Payables Transaction')
 
 210   $form->{javascript} = qq|<script type="text/javascript">
 
 212   function setTaxkey(accno, row) {
 
 213     var taxkey = accno.options[accno.selectedIndex].value;
 
 214     var reg = /--([0-9]*)/;
 
 215     var found = reg.exec(taxkey);
 
 216     var index = found[1];
 
 217     index = parseInt(index);
 
 218     var tax = 'taxchart_' + row;
 
 219     for (var i = 0; i < document.getElementById(tax).options.length; ++i) {
 
 220       var reg2 = new RegExp("^"+ index, "");
 
 221       if (reg2.exec(document.getElementById(tax).options[i].value)) {
 
 222         document.getElementById(tax).options[i].selected = true;
 
 229   # show history button
 
 230   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
 
 231   #/show hhistory button
 
 233   # set option selected
 
 234   foreach my $item (qw(vendor currency department)) {
 
 235     $form->{"select$item"} =~ s/ selected//;
 
 236     $form->{"select$item"} =~
 
 237       s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
 
 239   my $readonly = ($form->{id}) ? "readonly" : "";
 
 241   $form->{radier} = ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
 
 242   $readonly       = ($form->{radier}) ? "" : $readonly;
 
 244   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
 
 245   $form->{exchangerate} = $form->{forex} if $form->{forex};
 
 248   $form->{exchangerate}    = $form->{exchangerate} ? $form->format_amount(\%myconfig, $form->{exchangerate}) : '';
 
 249   $form->{creditlimit}     = $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
 
 250   $form->{creditremaining} = $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
 
 252   my $exchangerate = qq|
 
 253 <input type=hidden name=forex value=$form->{forex}>
 
 255   if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
 
 256     if ($form->{forex}) {
 
 259               <th align=right>| . $locale->text('Exchangerate') . qq|</th>
 
 260               <td><input type=hidden name=exchangerate value=$form->{exchangerate}>$form->{exchangerate}</td>
 
 265              <th align=right>| . $locale->text('Exchangerate') . qq|</th>
 
 266              <td><input name=exchangerate size=10 value=$form->{exchangerate}></td>
 
 271   my $taxincluded = "";
 
 275               <td align=right><input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}></td>
 
 276               <th align=left nowrap>|
 
 277     . $locale->text('Tax Included') . qq|</th>
 
 282   if (($rows = $form->numtextrows($form->{notes}, 50)) < 2) {
 
 286     qq|<textarea name=notes rows=$rows cols=50 wrap=soft $readonly>$form->{notes}</textarea>|;
 
 291                 <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
 
 292                 <td colspan=3><select name=department>$form->{selectdepartment}</select>
 
 293                 <input type=hidden name=selectdepartment value="$form->{selectdepartment}">
 
 296 | if $form->{selectdepartment};
 
 298   my $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
 
 301     ($form->{selectvendor})
 
 302     ? qq|<select name="vendor" onchange="document.getElementById('update_button').click();">$form->{selectvendor} </select>|
 
 303     : qq|<input name=vendor value="$form->{vendor}" size=35>|;
 
 305   my @old_project_ids = ();
 
 306   map({ push(@old_project_ids, $form->{"project_id_$_"})
 
 307           if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
 
 309   $form->get_lists("projects"  => { "key"       => "ALL_PROJECTS",
 
 311                                     "old_id"    => \@old_project_ids },
 
 312                    "charts"    => { "key"       => "ALL_CHARTS",
 
 313                                     "transdate" => $form->{transdate} },
 
 314                    "taxcharts" => { "key"       => "ALL_TAXCHARTS",
 
 315                                     "module"    => "AP" },);
 
 317   map({ $_->{link_split} = [ split(/:/, $_->{link}) ]; }
 
 318       @{ $form->{ALL_CHARTS} });
 
 320   my %project_labels = ();
 
 321   my @project_values = ("");
 
 322   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
 
 323     push(@project_values, $item->{"id"});
 
 324     $project_labels{$item->{"id"}} = $item->{"projectnumber"};
 
 327   my (%AP_amount_labels, @AP_amount_values);
 
 328   my (%AP_labels, @AP_values);
 
 329   my (%AP_paid_labels, @AP_paid_values);
 
 333   foreach my $item (@{ $form->{ALL_CHARTS} }) {
 
 334     if (grep({ $_ eq "AP_amount" } @{ $item->{link_split} })) {
 
 335       $taxchart_init = $item->{tax_id} if ($taxchart_init eq "");
 
 336       my $key = "$item->{accno}--$item->{tax_id}";
 
 337       push(@AP_amount_values, $key);
 
 338       $AP_amount_labels{$key} =
 
 339         "$item->{accno}--$item->{description}";
 
 341     } elsif (grep({ $_ eq "AP" } @{ $item->{link_split} })) {
 
 342       push(@AP_values, $item->{accno});
 
 343       $AP_labels{$item->{accno}} = "$item->{accno}--$item->{description}";
 
 345     } elsif (grep({ $_ eq "AP_paid" } @{ $item->{link_split} })) {
 
 346       push(@AP_paid_values, $item->{accno});
 
 347       $AP_paid_labels{$item->{accno}} =
 
 348         "$item->{accno}--$item->{description}";
 
 351     $charts{$item->{accno}} = $item;
 
 354   my %taxchart_labels = ();
 
 355   my @taxchart_values = ();
 
 357   foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
 
 358     my $key = "$item->{id}--$item->{rate}";
 
 359     $taxchart_init = $key if ($taxchart_init eq $item->{id});
 
 360     push(@taxchart_values, $key);
 
 361     $taxchart_labels{$key} =
 
 362       "$item->{taxdescription} " . ($item->{rate} * 100) . ' %';
 
 363     $taxcharts{$item->{id}} = $item;
 
 366   # use JavaScript Calendar or not
 
 367   $form->{jsscript} = 1;
 
 369   my ($button1, $button2);
 
 370   if ($form->{jsscript}) {
 
 372     # with JavaScript Calendar
 
 374        <td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\" $readonly></td>
 
 375        <td><input type=button name=transdate id="trigger1" value=|
 
 376       . $locale->text('button') . qq|></td>
 
 379        <td><input name=duedate id=duedate size=11 title="$myconfig{dateformat}" value="$form->{duedate}" onBlur=\"check_right_date_format(this)\" $readonly></td>
 
 380        <td><input type=button name=duedate id="trigger2" value=|
 
 381       . $locale->text('button') . qq|></td></td>
 
 386       Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1",
 
 387                           "duedate", "BL", "trigger2");
 
 390     # without JavaScript Calendar
 
 392       qq|<td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\" $readonly></td>|;
 
 394       qq|<td><input name=duedate id=duedate size=11 title="$myconfig{dateformat}" value="$form->{duedate}" onBlur=\"check_right_date_format(this)\" $readonly></td>|;
 
 397   my $follow_up_vc         =  $form->{vendor};
 
 398   $follow_up_vc            =~ s/--.*?//;
 
 399   my $follow_up_trans_info =  "$form->{invnumber} ($follow_up_vc)";
 
 401   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
 
 402   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
 
 403   $form->{javascript} .= qq|<script type="text/javascript" src="js/follow_up.js"></script>|;
 
 405   my $globalprojectnumber =
 
 406     NTI($cgi->popup_menu('-name' => "globalproject_id",
 
 407                          '-values' => \@project_values,
 
 408                          '-labels' => \%project_labels,
 
 409                          '-default' => $form->{"globalproject_id"} ));
 
 412   my $onload = qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
 
 413   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
 
 415 <body onLoad="$onload">
 
 417 <form method=post action=$form->{script}>
 
 419 <input type=hidden name=id value=$form->{id}>
 
 420 <input type=hidden name=sort value=$form->{sort}>
 
 421 <input type=hidden name=closedto value=$form->{closedto}>
 
 422 <input type=hidden name=locked value=$form->{locked}>
 
 423 <input type=hidden name=title value="$title">
 
 425 <input type="hidden" name="follow_up_trans_id_1" value="| . H($form->{id}) . qq|">
 
 426 <input type="hidden" name="follow_up_trans_type_1" value="ap_transaction">
 
 427 <input type="hidden" name="follow_up_trans_info_1" value="| . H($follow_up_trans_info) . qq|">
 
 428 <input type="hidden" name="follow_up_rowcount" value="1">
 
 430 | . ($form->{saved_message} ? qq|<p>$form->{saved_message}</p>| : "") . qq|
 
 434     <th class=listtop>$form->{title}</th>
 
 444                 <th align=right nowrap>| . $locale->text('Vendor') . qq|</th>
 
 445                 <td colspan=3>$vendor <input type="button" value="D" onclick="show_vc_details('vendor')"></td>
 
 446                 <input type=hidden name=selectvendor value="| . H($form->{selectvendor}) . qq|">
 
 447                 <input type=hidden name=oldvendor value="| . H($form->{oldvendor}) . qq|">
 
 448                 <input type=hidden name=vendor_id value="| . H($form->{vendor_id}) . qq|">
 
 449                 <input type=hidden name=terms value="| . H($form->{terms}) . qq|">
 
 456                       <th align=left nowrap>| . $locale->text('Credit Limit') . qq|</th>
 
 457                       <td>$form->{creditlimit}</td>
 
 458                       <th align=left nowrap>| . $locale->text('Remaining') . qq|</th>
 
 459                       <td class="plus$n">$form->{creditremaining}</td>
 
 460                       <input type=hidden name=creditlimit value=$form->{creditlimit}>
 
 461                       <input type=hidden name=creditremaining value=$form->{creditremaining}>
 
 466                 <th align=right nowrap>| . $locale->text('Currency') . qq|</th>
 
 467                 <td><select name=currency>$form->{selectcurrency}</select></td>
 
 468                 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
 
 469                 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
 
 470                 <input type=hidden name=fxgain_accno value=$form->{fxgain_accno}>
 
 471                 <input type=hidden name=fxloss_accno value=$form->{fxloss_accno}>
 
 481                 <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
 
 482                 <td><input name=invnumber size=11 value="$form->{invnumber}" $readonly></td>
 
 485                 <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
 
 486                 <td><input name=ordnumber size=11 value="$form->{ordnumber}" $readonly></td>
 
 489                 <th align=right nowrap>| . $locale->text('Invoice Date') . qq|</th>
 
 493                 <th align=right nowrap>| . $locale->text('Due Date') . qq|</th>
 
 497                 <th align=right nowrap>| . $locale->text('Project Number') . qq|</th>
 
 498                 <td>$globalprojectnumber</td>
 
 510   <input type=hidden name=rowcount value=$form->{rowcount}>
 
 514         <tr class=listheading>
 
 515           <th class=listheading style="width:15%">| . $locale->text('Account') . qq|</th>
 
 516           <th class=listheading style="width:10%">| . $locale->text('Amount') . qq|</th>
 
 517           <th class=listheading style="width:10%">| . $locale->text('Tax') . qq|</th>
 
 518           <th class=listheading style="width:10%">| . $locale->text('Taxkey') . qq|</th>
 
 519           <th class=listheading style="width:10%">| . $locale->text('Project') . qq|</th>
 
 523   my $amount  = $locale->text('Amount');
 
 524   my $project = $locale->text('Project');
 
 526   for my $i (1 .. $form->{rowcount}) {
 
 529     $form->{"amount_$i"} =
 
 530       $form->format_amount(\%myconfig, $form->{"amount_$i"}, 2);
 
 531     $form->{"tax_$i"} = $form->format_amount(\%myconfig, $form->{"tax_$i"}, 2);
 
 533     my $selected_accno_full;
 
 534     my ($accno_row) = split(/--/, $form->{"AP_amount_$i"});
 
 535     my $item = $charts{$accno_row};
 
 536     $selected_accno_full = "$item->{accno}--$item->{tax_id}";
 
 538     my $selected_taxchart = $form->{"taxchart_$i"};
 
 539     my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full);
 
 540     my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_AP_amount_$i"});
 
 542     if ($previous_accno &&
 
 543         ($previous_accno eq $selected_accno) &&
 
 544         ($previous_tax_id ne $selected_tax_id)) {
 
 545       my $item = $taxcharts{$selected_tax_id};
 
 546       $selected_taxchart = "$item->{id}--$item->{rate}";
 
 549     $selected_taxchart = $taxchart_init unless ($form->{"taxchart_$i"});
 
 551     my $selectAP_amount =
 
 552       NTI($cgi->popup_menu('-name' => "AP_amount_$i",
 
 553                            '-id' => "AP_amount_$i",
 
 554                            '-style' => 'width:400px',
 
 555                            '-onChange' => "setTaxkey(this, $i)",
 
 556                            '-values' => \@AP_amount_values,
 
 557                            '-labels' => \%AP_amount_labels,
 
 558                            '-default' => $selected_accno_full))
 
 559       . $cgi->hidden('-name' => "previous_AP_amount_$i",
 
 560                      '-default' => $selected_accno_full);
 
 563       NTI($cgi->popup_menu('-name' => "taxchart_$i",
 
 564                            '-id' => "taxchart_$i",
 
 565                            '-style' => 'width:200px',
 
 566                            '-values' => \@taxchart_values,
 
 567                            '-labels' => \%taxchart_labels,
 
 568                            '-default' => $selected_taxchart))
 
 572       NTI($cgi->popup_menu('-name' => "project_id_$i",
 
 573                            '-values' => \@project_values,
 
 574                            '-labels' => \%project_labels,
 
 575                            '-default' => $form->{"project_id_$i"} ));
 
 579           <td>$selectAP_amount</td>
 
 580           <td><input name="amount_$i" size=10 value=$form->{"amount_$i"}></td>
 
 581           <td><input type="hidden" name="tax_$i" value="$form->{"tax_$i"}">$form->{"tax_$i"}</td>
 
 583           <td>$projectnumber</td>
 
 591     ($form->{taxincluded})
 
 592     ? $locale->text('Tax Included')
 
 593     : $locale->text('Tax');
 
 595   $form->{invtotal_unformatted} = $form->{invtotal};
 
 596   $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
 
 599     NTI($cgi->popup_menu('-name' => "APselected", '-id' => "APselected",
 
 600                          '-style' => 'width:400px',
 
 601                          '-values' => \@AP_values, '-labels' => \%AP_labels,
 
 602                          '-default' => $form->{APselected}));
 
 610           <td>${APselected}</td>
 
 611           <th align=left>$form->{invtotal}</th>
 
 613           <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
 
 614           <input type=hidden name=oldtotalpaid value=$form->{oldtotalpaid}>
 
 616           <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
 
 629           <th align=left width=1%>| . $locale->text('Notes') . qq|</th>
 
 630           <td align=left>$notes</td>
 
 638         <tr class=listheading>
 
 639           <th class=listheading colspan=7>| . $locale->text('Payments') . qq|</th>
 
 644   if ($form->{defaultcurrency} && ($form->{currency} eq $form->{defaultcurrency})) {
 
 645     @column_index = qw(datepaid source memo paid AP_paid paid_project_id);
 
 647     @column_index = qw(datepaid source memo paid exchangerate AP_paid paid_project_id);
 
 651   $column_data{datepaid}     = "<th>" . $locale->text('Date') . "</th>";
 
 652   $column_data{paid}         = "<th>" . $locale->text('Amount') . "</th>";
 
 653   $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
 
 654   $column_data{AP_paid}      = "<th>" . $locale->text('Account') . "</th>";
 
 655   $column_data{source}       = "<th>" . $locale->text('Source') . "</th>";
 
 656   $column_data{memo}         = "<th>" . $locale->text('Memo') . "</th>";
 
 657   $column_data{paid_project_id} = "<th>" . $locale->text('Project Number') . "</th>";
 
 662   map { print "$column_data{$_}\n" } @column_index;
 
 668   $form->{totalpaid} = 0;
 
 670   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
 
 671   for my $i (1 .. $form->{paidaccounts}) {
 
 677       NTI($cgi->popup_menu('-name' => "AP_paid_$i",
 
 678                            '-id' => "AP_paid_$i",
 
 679                            '-values' => \@AP_paid_values,
 
 680                            '-labels' => \%AP_paid_labels,
 
 681                            '-default' => $form->{"AP_paid_$i"}));
 
 683     $form->{totalpaid} += $form->{"paid_$i"};
 
 686     if ($form->{"paid_$i"}) {
 
 688         $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
 
 690     if ($form->{"exchangerate_$i"} == 0) {
 
 691       $form->{"exchangerate_$i"} = "";
 
 693       $form->{"exchangerate_$i"} =
 
 694         $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
 
 697     print qq|<input type=hidden name="acc_trans_id_$i" value=$form->{"acc_trans_id_$i"}>\n|;
 
 698     print qq|<input type=hidden name="gldate_$i" value=$form->{"gldate_$i"}>\n|;
 
 700     if ($::lx_office_conf{features}->{payments_changeable} == 0) {
 
 702       $changeable = ($form->{"acc_trans_id_$i"})? 0 : 1;
 
 704     if ($::lx_office_conf{features}->{payments_changeable} == 2) {
 
 706       $changeable = (($form->{"gldate_$i"} eq '') || $form->current_date(\%myconfig) eq $form->{"gldate_$i"});
 
 709     $exchangerate = qq| |;
 
 710     if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
 
 711       if ($form->{"forex_$i"}) {
 
 713           qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
 
 717             qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
 
 720             qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
 
 726 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
 
 731       $datepaid = qq|<td align=center><input name="datepaid_$i" id="datepaid_$i" size=11 title="($myconfig{'dateformat'})" value="$form->{"datepaid_$i"}" onBlur=\"check_right_date_format(this)\">
 
 732          <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="| . $locale->text('Details (one letter abbreviation)') . qq|"></td>|;
 
 734       $datepaid = qq|<td align=center>$form->{"datepaid_$i"}</td>|.
 
 735         qq|<input type=hidden name="datepaid_$i" value=$form->{"datepaid_$i"}>|;
 
 740       $paid = qq|<td align=center><input name="paid_$i" size=11 value="$form->{"paid_$i"}" onBlur=\"check_right_number_format(this)\"></td>|;
 
 742       $paid = qq|<td align=center>$form->{"paid_$i"}</td>|.
 
 743         qq|<input type=hidden name="paid_$i" value=$form->{"paid_$i"}>|;
 
 748       $source = qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
 
 750       $source = qq|<td align=center>$form->{"source_$i"}</td>|.
 
 751         qq|<input type=hidden name="source_$i" value=$form->{"source_$i"}>|;
 
 756       $memo = qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
 
 758       $memo = qq|<td align=center>$form->{"memo_$i"}</td>|.
 
 759         qq|<input type=hidden name="memo_$i" value=$form->{"memo_$i"}>|;
 
 764       $AP_paid = qq|<td align=center>${selectAP_paid}</td>|;
 
 766       $AP_paid = qq|<td align=center>$form->{"AP_paid_$i"}</td>|.
 
 767         qq|<input type=hidden name="AP_paid_$i" value=$form->{"AP_paid_$i"}>|;
 
 774       . NTI($cgi->popup_menu('-name' => "paid_project_id_$i",
 
 775                              '-values' => \@project_values,
 
 776                              '-labels' => \%project_labels,
 
 777                              '-default' => $form->{"paid_project_id_$i"} ))
 
 780       my $projectnumber = $project_labels{$form->{"paid_project_id_$i"}};
 
 781       $paid_project_id = qq|<td>$projectnumber</td>|.
 
 782         qq|<input type=hidden name="paid_project_id_$i" value=$form->{"paid_project_id_$i"}>|;
 
 785     $column_data{"paid_$i"}            = $paid;
 
 786     $column_data{"AP_paid_$i"}         = $AP_paid;
 
 787     $column_data{"exchangerate_$i"}    = qq|<td align=center>$exchangerate</td>|;
 
 788     $column_data{"datepaid_$i"}        = $datepaid;
 
 789     $column_data{"source_$i"}          = $source;
 
 790     $column_data{"memo_$i"}            = $memo;
 
 791     $column_data{"paid_project_id_$i"} = $paid_project_id;
 
 793     map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
 
 799       push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
 
 803   my $paid_missing = $form->{invtotal_unformatted} - $form->{totalpaid};
 
 809           <td align="center">| . $locale->text('Total') . qq|</td>
 
 810           <td align="center">| . H($form->format_amount(\%myconfig, $form->{totalpaid}, 2)) . qq|</td>
 
 815           <td align="center">| . $locale->text('Missing amount') . qq|</td>
 
 816           <td align="center">| . H($form->format_amount(\%myconfig, $paid_missing, 2)) . qq|</td>
 
 818 | . $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
 
 820     <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
 
 826     <td><hr size=3 noshade></td>
 
 831   $main::lxdebug->leave_sub();
 
 835   $main::lxdebug->enter_sub();
 
 837   my $form     = $main::form;
 
 838   my %myconfig = %main::myconfig;
 
 839   my $locale   = $main::locale;
 
 840   my $cgi      = $::request->{cgi};
 
 842   $main::auth->assert('general_ledger');
 
 844   my $follow_ups_block;
 
 846     my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
 
 848     if (@{ $follow_ups} ) {
 
 849       my $num_due       = sum map { $_->{due} * 1 } @{ $follow_ups };
 
 850       $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>|;
 
 858 <input name=callback type=hidden value="$form->{callback}">
 
 859 <input name="gldate" type="hidden" value="| . Q($form->{gldate}) . qq|">
 
 861 . $cgi->hidden('-name' => 'draft_id', '-default' => [$form->{draft_id}])
 
 862 . $cgi->hidden('-name' => 'draft_description', '-default' => [$form->{draft_description}])
 
 868   if (!$form->{id} && $form->{draft_id}) {
 
 869     print(NTI($cgi->checkbox('-name' => 'remove_draft', '-id' => 'remove_draft',
 
 870                              '-value' => 1, '-checked' => $form->{remove_draft},
 
 872           qq| <label for="remove_draft">| .
 
 873           $locale->text("Remove draft when posting") .
 
 877   my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
 
 878   my $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
 
 880   print qq|<input class="submit" type="submit" name="action" id="update_button" value="| . $locale->text('Update') . qq|">|;
 
 883     if ($form->{radier}) {
 
 884       print qq| <input class=submit type=submit name=action value="| . $locale->text('Post') . qq|">
 
 885                 <input class=submit type=submit name=action value="| . $locale->text('Delete') . qq|">
 
 888     # ToDO: - insert a global check for stornos, so that a storno is only possible a limited time after saving it
 
 889     print qq| <input class=submit type=submit name=action value="| . $locale->text('Storno') . qq|"> |
 
 890       if ($form->{id} && !IS->has_storno(\%myconfig, $form, 'ap') && !IS->is_storno(\%myconfig, $form, 'ap', $form->{id}) && (($form->{totalpaid} == 0) || ($form->{totalpaid} eq "")));
 
 892     print qq| <input class=submit type=submit name=action value="| . $locale->text('Post Payment') . qq|">
 
 893               <input class=submit type=submit name=action value="| . $locale->text('Use As Template') . qq|">
 
 894               <input type="button" class="submit" onclick="follow_up_window()" value="| . $locale->text('Follow-Up') . qq|">
 
 896   } elsif (($transdate > $closedto) && !$form->{id}) {
 
 898       <input class=submit type=submit name=action value="| . $locale->text('Post') . qq|"> | .
 
 899       NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'), '-class' => 'submit'));
 
 901   # button for saving history
 
 902   if($form->{id} ne "") {
 
 903     print qq| <input type="button" class="submit" onclick="set_history_window($form->{id});" name="history" id="history" value="| . $locale->text('history') . qq|"> |;
 
 905   # /button for saving history
 
 906   # mark_as_paid button
 
 907   if($form->{id} ne "") {
 
 908     print qq| <input type="submit" class="submit" name="action" value="| . $locale->text('mark as paid') . qq|"> |;
 
 910   # /mark_as_paid button
 
 918   $main::lxdebug->leave_sub();
 
 922   $main::lxdebug->enter_sub();
 
 924   my $form     = $main::form;
 
 925   my %myconfig = %main::myconfig;
 
 927   $main::auth->assert('general_ledger');
 
 929   &mark_as_paid_common(\%myconfig,"ap");
 
 931   $main::lxdebug->leave_sub();
 
 935   $main::lxdebug->enter_sub();
 
 937   my $form     = $main::form;
 
 938   my %myconfig = %main::myconfig;
 
 940   $main::auth->assert('general_ledger');
 
 944   $form->{invtotal} = 0;
 
 946   delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
 
 948   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
 
 949     qw(exchangerate creditlimit creditremaining);
 
 951   my @flds  = qw(amount AP_amount projectnumber oldprojectnumber project_id taxchart);
 
 953   my (@a, $j, $totaltax);
 
 954   for my $i (1 .. $form->{rowcount}) {
 
 955     $form->{"amount_$i"} =
 
 956       $form->parse_amount(\%myconfig, $form->{"amount_$i"});
 
 957     $form->{"tax_$i"} = $form->parse_amount(\%myconfig, $form->{"tax_$i"});
 
 958     if ($form->{"amount_$i"}) {
 
 961       my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
 
 963         if ($form->{taxincluded}) {
 
 964           $form->{"tax_$i"} = $form->{"amount_$i"} / ($rate + 1) * $rate;
 
 966           $form->{"tax_$i"} = $form->{"amount_$i"} * $rate;
 
 969         $form->{"tax_$i"} = 0;
 
 971       $form->{"tax_$i"} = $form->round_amount($form->{"tax_$i"}, 2);
 
 973       $totaltax += $form->{"tax_$i"};
 
 974       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
 
 978   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
 
 980   map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
 
 982   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
 
 983   $form->{exchangerate} = $form->{forex} if $form->{forex};
 
 985   $form->{invdate} = $form->{transdate};
 
 986   my %saved_variables = map +( $_ => $form->{$_} ), qw(AP AP_amount_1 taxchart_1);
 
 988   my $vendor_changed = &check_name("vendor");
 
 990   $form->{AP} = $saved_variables{AP};
 
 991   if ($saved_variables{AP_amount_1} =~ m/.--./) {
 
 992     map { $form->{$_} = $saved_variables{$_} } qw(AP_amount_1 taxchart_1);
 
 994     delete $form->{taxchart_1};
 
 997   $form->{rowcount} = $count + 1;
 
1000     ($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"}, 'sell');
 
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;
 
1024   $form->{notes} = $form->{intnotes} if $vendor_changed;
 
1028   $main::lxdebug->leave_sub();
 
1033   $main::lxdebug->enter_sub();
 
1035   my $form     = $main::form;
 
1036   my %myconfig = %main::myconfig;
 
1037   my $locale   = $main::locale;
 
1039   $main::auth->assert('general_ledger');
 
1041   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
1043   my $invdate = $form->datetonum($form->{transdate}, \%myconfig);
 
1045   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!'))
 
1052         if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
 
1054       if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
 
1055         $form->{"exchangerate_$i"} = $form->{exchangerate}
 
1056           if ($invdate == $datepaid);
 
1057         $form->isblank("exchangerate_$i",
 
1058                        $locale->text('Exchangerate for payment missing!'));
 
1063   ($form->{AP})      = split /--/, $form->{AP};
 
1064   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
 
1065   $form->redirect($locale->text('Payment posted!'))
 
1066       if (AP->post_payment(\%myconfig, \%$form));
 
1067     $form->error($locale->text('Cannot post payment!'));
 
1070   $main::lxdebug->leave_sub();
 
1075   $main::lxdebug->enter_sub();
 
1077   my $form     = $main::form;
 
1078   my %myconfig = %main::myconfig;
 
1079   my $locale   = $main::locale;
 
1081   $main::auth->assert('general_ledger');
 
1085   # check if there is a vendor, invoice and due date
 
1086   $form->isblank("transdate", $locale->text("Invoice Date missing!"));
 
1087   $form->isblank("duedate",   $locale->text("Due Date missing!"));
 
1088   $form->isblank("vendor",    $locale->text('Vendor missing!'));
 
1090   if ($myconfig{mandatory_departments} && !$form->{department}) {
 
1091     $form->{saved_message} = $::locale->text('You have to specify a department.');
 
1096   my $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
 
1097   my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
 
1098   $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
 
1100   my $zero_amount_posting = 1;
 
1101   for my $i (1 .. $form->{rowcount}) {
 
1102     if ($form->parse_amount(\%myconfig, $form->{"amount_$i"})) {
 
1103       $zero_amount_posting = 0;
 
1108   $form->error($locale->text('Zero amount posting!')) if $zero_amount_posting;
 
1110   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
 
1111     if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
 
1112   delete($form->{AP});
 
1114   for my $i (1 .. $form->{paidaccounts}) {
 
1115     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
 
1116       my $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}
 
1125           if ($transdate == $datepaid);
 
1126         $form->isblank("exchangerate_$i",
 
1127                        $locale->text('Exchangerate for payment missing!'));
 
1133   # if old vendor ne vendor redo form
 
1134   my ($vendor) = split /--/, $form->{vendor};
 
1135   if ($form->{oldvendor} ne "$vendor--$form->{vendor_id}") {
 
1139   my ($debitaccno,    $debittaxkey)    = split /--/, $form->{AP_amountselected};
 
1140   my ($taxkey,        $NULL)           = split /--/, $form->{taxchartselected};
 
1141   my ($payablesaccno, $payablestaxkey) = split /--/, $form->{APselected};
 
1142 #  $form->{AP_amount_1}  = $debitaccno;
 
1143   $form->{AP_payables}  = $payablesaccno;
 
1144   $form->{taxkey}       = $taxkey;
 
1145   $form->{storno}       = 0;
 
1147   $form->{id} = 0 if $form->{postasnew};
 
1149   if (AP->post_transaction(\%myconfig, \%$form)) {
 
1150     # saving the history
 
1151     if(!exists $form->{addition} && $form->{id} ne "") {
 
1152       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
1153       $form->{addition} = "POSTED";
 
1154       $form->save_history;
 
1156     # /saving the history
 
1157     remove_draft() if $form->{remove_draft};
 
1158     # Dieser Text wird niemals ausgegeben: Probleme beim redirect?
 
1159     $form->redirect($locale->text('Transaction posted!')) unless $inline;
 
1161     $form->error($locale->text('Cannot post transaction!'));
 
1164   $main::lxdebug->leave_sub();
 
1168   $main::lxdebug->enter_sub();
 
1170   my $form     = $main::form;
 
1171   my %myconfig = %main::myconfig;
 
1173   $main::auth->assert('general_ledger');
 
1175   $form->{postasnew} = 1;
 
1176   # saving the history
 
1177   if(!exists $form->{addition} && $form->{id} ne "") {
 
1178     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
1179     $form->{addition} = "POSTED AS NEW";
 
1180     $form->save_history;
 
1182   # /saving the history
 
1185   $main::lxdebug->leave_sub();
 
1188 sub use_as_template {
 
1189   $main::lxdebug->enter_sub();
 
1191   my $form     = $main::form;
 
1192   my %myconfig = %main::myconfig;
 
1194   $main::auth->assert('general_ledger');
 
1196   map { delete $form->{$_} } qw(printed emailed queued invnumber invdate deliverydate id datepaid_1 gldate_1 acc_trans_id_1 source_1 memo_1 paid_1 exchangerate_1 AP_paid_1 storno);
 
1197   $form->{paidaccounts} = 1;
 
1198   $form->{rowcount}--;
 
1199   $form->{invdate} = $form->current_date(\%myconfig);
 
1202   $main::lxdebug->leave_sub();
 
1206   $main::lxdebug->enter_sub();
 
1208   my $form     = $main::form;
 
1209   my $locale   = $main::locale;
 
1211   $main::auth->assert('general_ledger');
 
1213   $form->{title} = $locale->text('Confirm!');
 
1217   delete $form->{header};
 
1222 <form method=post action=$form->{script}>
 
1225   foreach my $key (keys %$form) {
 
1226     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
 
1227     $form->{$key} =~ s/\"/"/g;
 
1228     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
 
1232 <h2 class=confirm>$form->{title}</h2>
 
1235     . $locale->text('Are you sure you want to delete Transaction')
 
1236     . qq| $form->{invnumber}</h4>
 
1238 <input name=action class=submit type=submit value="|
 
1239     . $locale->text('Yes') . qq|">
 
1246   $main::lxdebug->leave_sub();
 
1250   $main::lxdebug->enter_sub();
 
1252   my $form     = $main::form;
 
1253   my %myconfig = %main::myconfig;
 
1254   my $locale   = $main::locale;
 
1256   $main::auth->assert('general_ledger');
 
1258   if (AP->delete_transaction(\%myconfig, \%$form)) {
 
1259     # saving the history
 
1260     if(!exists $form->{addition}) {
 
1261       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
1262       $form->{addition} = "DELETED";
 
1263       $form->save_history;
 
1265     # /saving the history
 
1266     $form->redirect($locale->text('Transaction deleted!'));
 
1268   $form->error($locale->text('Cannot delete transaction!'));
 
1270   $main::lxdebug->leave_sub();
 
1274   $main::lxdebug->enter_sub();
 
1276   $main::auth->assert('general_ledger | vendor_invoice_edit');
 
1278   my $form     = $main::form;
 
1279   my %myconfig = %main::myconfig;
 
1280   my $locale   = $main::locale;
 
1282   # setup customer selection
 
1283   $form->all_vc(\%myconfig, "vendor", "AP");
 
1285   $form->{title}    = $locale->text('AP Transactions');
 
1286   $form->{fokus}    = "search.vendor";
 
1287   $form->{jsscript} = 1;
 
1289   $form->get_lists("projects"     => { "key" => "ALL_PROJECTS", "all" => 1 },
 
1290                    "departments"  => "ALL_DEPARTMENTS",
 
1291                    "vendors"      => "ALL_VC");
 
1293   # constants and subs for template
 
1294   $form->{jsscript}  = 1;
 
1295   $form->{vc_keys}   = sub { "$_[0]->{name}--$_[0]->{id}" };
 
1298   print $form->parse_html_template('ap/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 ap_transactions {
 
1325   $main::lxdebug->enter_sub();
 
1327   my $form     = $main::form;
 
1328   my %myconfig = %main::myconfig;
 
1329   my $locale   = $main::locale;
 
1331   $main::auth->assert('general_ledger | vendor_invoice_edit');
 
1333   ($form->{vendor}, $form->{vendor_id}) = split(/--/, $form->{vendor});
 
1335   report_generator_set_default_sort('transdate', 1);
 
1337   AP->ap_transactions(\%myconfig, \%$form);
 
1339   $form->{title} = $locale->text('AP Transactions');
 
1341   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
1344     qw(transdate id type invnumber ordnumber name netamount tax amount paid datepaid
 
1345        due duedate transaction_description notes employee globalprojectnumber
 
1346        vendornumber country ustid taxzone payment_terms charts);
 
1348   my @hidden_variables = map { "l_${_}" } @columns;
 
1349   push @hidden_variables, "l_subtotal", qw(open closed vendor invnumber ordnumber transaction_description notes project_id transdatefrom transdateto department);
 
1351   my $href = build_std_url('action=ap_transactions', grep { $form->{$_} } @hidden_variables);
 
1354     'transdate'               => { 'text' => $locale->text('Date'), },
 
1355     'id'                      => { 'text' => $locale->text('ID'), },
 
1356     'type'                    => { 'text' => $locale->text('Type'), },
 
1357     'invnumber'               => { 'text' => $locale->text('Invoice'), },
 
1358     'ordnumber'               => { 'text' => $locale->text('Order'), },
 
1359     'name'                    => { 'text' => $locale->text('Vendor'), },
 
1360     'netamount'               => { 'text' => $locale->text('Amount'), },
 
1361     'tax'                     => { 'text' => $locale->text('Tax'), },
 
1362     'amount'                  => { 'text' => $locale->text('Total'), },
 
1363     'paid'                    => { 'text' => $locale->text('Paid'), },
 
1364     'datepaid'                => { 'text' => $locale->text('Date Paid'), },
 
1365     'due'                     => { 'text' => $locale->text('Amount Due'), },
 
1366     'duedate'                 => { 'text' => $locale->text('Due Date'), },
 
1367     'transaction_description' => { 'text' => $locale->text('Transaction description'), },
 
1368     'notes'                   => { 'text' => $locale->text('Notes'), },
 
1369     'employee'                => { 'text' => $locale->text('Employee'), },
 
1370     'globalprojectnumber'     => { 'text' => $locale->text('Document Project Number'), },
 
1371     'vendornumber'            => { 'text' => $locale->text('Vendor Number'), },
 
1372     'country'                 => { 'text' => $locale->text('Country'), },
 
1373     'ustid'                   => { 'text' => $locale->text('USt-IdNr.'), },
 
1374     'taxzone'                 => { 'text' => $locale->text('Steuersatz'), },
 
1375     'payment_terms'           => { 'text' => $locale->text('Payment Terms'), },
 
1376     'charts'                  => { 'text' => $locale->text('Buchungskonto'), },
 
1379   foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid employee shippingpoint shipvia transaction_description)) {
 
1380     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
 
1381     $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
 
1384   my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
 
1386   $form->{"l_type"} = "Y";
 
1387   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
 
1389   $report->set_columns(%column_defs);
 
1390   $report->set_column_order(@columns);
 
1392   $report->set_export_options('ap_transactions', @hidden_variables, qw(sort sortdir));
 
1394   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
 
1397   push @options, $locale->text('Vendor')                  . " : $form->{vendor}"                         if ($form->{vendor});
 
1398   push @options, $locale->text('Department')              . " : " . (split /--/, $form->{department})[0] if ($form->{department});
 
1399   push @options, $locale->text('Invoice Number')          . " : $form->{invnumber}"                      if ($form->{invnumber});
 
1400   push @options, $locale->text('Order Number')            . " : $form->{ordnumber}"                      if ($form->{ordnumber});
 
1401   push @options, $locale->text('Notes')                   . " : $form->{notes}"                          if ($form->{notes});
 
1402   push @options, $locale->text('Transaction description') . " : $form->{transaction_description}"        if ($form->{transaction_description});
 
1403   push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1)      if ($form->{transdatefrom});
 
1404   push @options, $locale->text('Bis')  . " " . $locale->date(\%myconfig, $form->{transdateto},   1)      if ($form->{transdateto});
 
1405   push @options, $locale->text('Open')                                                                   if ($form->{open});
 
1406   push @options, $locale->text('Closed')                                                                 if ($form->{closed});
 
1408   $report->set_options('top_info_text'        => join("\n", @options),
 
1409                        'raw_bottom_info_text' => $form->parse_html_template('ap/ap_transactions_bottom'),
 
1410                        'output_format'        => 'HTML',
 
1411                        'title'                => $form->{title},
 
1412                        'attachment_basename'  => $locale->text('vendor_invoice_list') . strftime('_%Y%m%d', localtime time),
 
1414   $report->set_options_from_form();
 
1415   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
1417   # add sort and escape callback, this one we use for the add sub
 
1418   $form->{callback} = $href .= "&sort=$form->{sort}";
 
1420   # escape callback for href
 
1421   my $callback = $form->escape($href);
 
1423   my @subtotal_columns = qw(netamount amount paid due);
 
1425   my %totals    = map { $_ => 0 } @subtotal_columns;
 
1426   my %subtotals = map { $_ => 0 } @subtotal_columns;
 
1430   foreach my $ap (@{ $form->{AP} }) {
 
1431     $ap->{tax} = $ap->{amount} - $ap->{netamount};
 
1432     $ap->{due} = $ap->{amount} - $ap->{paid};
 
1434     map { $subtotals{$_} += $ap->{$_};
 
1435           $totals{$_}    += $ap->{$_} } @subtotal_columns;
 
1437     map { $ap->{$_} = $form->format_amount(\%myconfig, $ap->{$_}, 2) } qw(netamount tax amount paid due);
 
1439     my $is_storno  = $ap->{storno} &&  $ap->{storno_id};
 
1440     my $has_storno = $ap->{storno} && !$ap->{storno_id};
 
1442     if ($ap->{invoice}) {
 
1444           $has_storno       ? $locale->text("Invoice with Storno (abbreviation)")
 
1445         : $is_storno        ? $locale->text("Storno (one letter abbreviation)")
 
1446         :                     $locale->text("Invoice (one letter abbreviation)");
 
1449           $has_storno       ? $locale->text("AP Transaction with Storno (abbreviation)")
 
1450         : $is_storno        ? $locale->text("AP Transaction Storno (one letter abbreviation)")
 
1451         :                     $locale->text("AP Transaction (abbreviation)");
 
1456     foreach my $column (@columns) {
 
1458         'data'  => $ap->{$column},
 
1459         'align' => $column_alignment{$column},
 
1463     $row->{invnumber}->{link} = build_std_url("script=" . ($ap->{invoice} ? 'ir.pl' : 'ap.pl'), 'action=edit')
 
1464       . "&id=" . E($ap->{id}) . "&callback=${callback}";
 
1466     my $row_set = [ $row ];
 
1468     if (($form->{l_subtotal} eq 'Y')
 
1469         && (($idx == (scalar @{ $form->{AP} } - 1))
 
1470             || ($ap->{ $form->{sort} } ne $form->{AP}->[$idx + 1]->{ $form->{sort} }))) {
 
1471       push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
 
1474     $report->add_data($row_set);
 
1479   $report->add_separator();
 
1480   $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
 
1482   $report->generate_with_headers();
 
1484   $main::lxdebug->leave_sub();
 
1488   $main::lxdebug->enter_sub();
 
1490   my $form     = $main::form;
 
1491   my %myconfig = %main::myconfig;
 
1492   my $locale   = $main::locale;
 
1494   $main::auth->assert('general_ledger');
 
1496   if (IS->has_storno(\%myconfig, $form, 'ap')) {
 
1497     $form->{title} = $locale->text("Cancel Accounts Payables Transaction");
 
1498     $form->error($locale->text("Transaction has already been cancelled!"));
 
1501   AP->storno($form, \%myconfig, $form->{id});
 
1503   # saving the history
 
1504   if(!exists $form->{addition} && $form->{id} ne "") {
 
1505     $form->{snumbers} = "ordnumber_$form->{ordnumber}";
 
1506     $form->{addition} = "STORNO";
 
1507     $form->save_history;
 
1509   # /saving the history
 
1511   $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
 
1513   $main::lxdebug->leave_sub();