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;
 
  45 require "bin/mozilla/arap.pl";
 
  46 require "bin/mozilla/common.pl";
 
  47 require "bin/mozilla/drafts.pl";
 
  48 require "bin/mozilla/reportgenerator.pl";
 
  56 # this is for our long dates
 
  57 # $locale->text('January')
 
  58 # $locale->text('February')
 
  59 # $locale->text('March')
 
  60 # $locale->text('April')
 
  61 # $locale->text('May ')
 
  62 # $locale->text('June')
 
  63 # $locale->text('July')
 
  64 # $locale->text('August')
 
  65 # $locale->text('September')
 
  66 # $locale->text('October')
 
  67 # $locale->text('November')
 
  68 # $locale->text('December')
 
  70 # this is for our short month
 
  71 # $locale->text('Jan')
 
  72 # $locale->text('Feb')
 
  73 # $locale->text('Mar')
 
  74 # $locale->text('Apr')
 
  75 # $locale->text('May')
 
  76 # $locale->text('Jun')
 
  77 # $locale->text('Jul')
 
  78 # $locale->text('Aug')
 
  79 # $locale->text('Sep')
 
  80 # $locale->text('Oct')
 
  81 # $locale->text('Nov')
 
  82 # $locale->text('Dec')
 
  85   $main::lxdebug->enter_sub();
 
  87   my $form     = $main::form;
 
  88   my %myconfig = %main::myconfig;
 
  90   $main::auth->assert('general_ledger');
 
  92   return $main::lxdebug->leave_sub() if (load_draft_maybe());
 
  94   $form->{title} = "Add";
 
  96   $form->{callback} = "ap.pl?action=add&DONT_LOAD_DRAFT=1" unless $form->{callback};
 
  98   AP->get_transdate(\%myconfig, $form);
 
  99   $form->{initial_transdate} = $form->{transdate};
 
 101   $form->{transdate} = $form->{initial_transdate};
 
 104   $main::lxdebug->leave_sub();
 
 108   $main::lxdebug->enter_sub();
 
 110   my $form     = $main::form;
 
 112   $main::auth->assert('general_ledger');
 
 114   $form->{title} = "Edit";
 
 119   $main::lxdebug->leave_sub();
 
 123   $main::lxdebug->enter_sub();
 
 125   my $form     = $main::form;
 
 127   $main::auth->assert('general_ledger');
 
 132   $main::lxdebug->leave_sub();
 
 136   $main::lxdebug->enter_sub();
 
 138   my $form     = $main::form;
 
 139   my %myconfig = %main::myconfig;
 
 141   $main::auth->assert('general_ledger');
 
 143   $form->create_links("AP", \%myconfig, "vendor");
 
 144   my $taxincluded = $form->{taxincluded};
 
 145   my $duedate     = $form->{duedate};
 
 147   IR->get_vendor(\%myconfig, \%$form);
 
 148   $form->{taxincluded} = $taxincluded;
 
 149   $form->{duedate}   = $duedate if $duedate;
 
 150   $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
 
 151   $form->{rowcount}  = 1;
 
 153   # build the popup menus
 
 154   $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
 
 157   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 159   map { my $quoted = H($_); $form->{selectcurrency} .= "<option value=\"${quoted}\">${quoted}\n" } $form->get_all_currencies(\%myconfig);
 
 162   if (@{ $form->{all_vendor} || [] }) {
 
 163     $form->{vendor} = qq|$form->{vendor}--$form->{vendor_id}|;
 
 164     map { my $quoted = H($_->{name} . "--" . $_->{id}); $form->{selectvendor} .= "<option value=\"${quoted}\">${quoted}\n" }
 
 165       (@{ $form->{all_vendor} });
 
 169   if (@{ $form->{all_departments} || [] }) {
 
 170     $form->{department}       = "$form->{department}--$form->{department_id}";
 
 171     $form->{selectdepartment} = "<option>\n" . join('', map { my $quoted = H("$_->{description}--$_->{id}"); "<option value=\"${quoted}\">${quoted}\n"} @{ $form->{all_departments} || [] });
 
 174   $form->{employee} = "$form->{employee}--$form->{employee_id}";
 
 176   AP->setup_form($form);
 
 179     ($form->datetonum($form->{transdate}, \%myconfig) <=
 
 180      $form->datetonum($form->{closedto}, \%myconfig));
 
 182   $main::lxdebug->leave_sub();
 
 186   $main::lxdebug->enter_sub();
 
 188   my $form     = $main::form;
 
 189   my %myconfig = %main::myconfig;
 
 190   my $locale   = $main::locale;
 
 191   my $cgi      = $::request->{cgi};
 
 193   $main::auth->assert('general_ledger');
 
 195   $form->{title_} = $form->{title};
 
 196   $form->{title} = $locale->text($form->{title} .' Accounts Payables Transaction');
 
 198   # type=submit $locale->text('Add Accounts Payables Transaction')
 
 199   # type=submit $locale->text('Edit Accounts Payables Transaction')
 
 201   # set option selected
 
 202   foreach my $item (qw(vendor currency department)) {
 
 203     my $to_replace         =  H($form->{$item});
 
 204     $form->{"select$item"} =~ s/ selected//;
 
 205     $form->{"select$item"} =~ s/>\Q${to_replace}\E/ selected>${to_replace}/;
 
 207   my $readonly = $form->{id} ? "readonly" : "";
 
 209   $form->{radier} = ($::instance_conf->get_ap_changeable == 2)
 
 210                       ? ($form->current_date(\%myconfig) eq $form->{gldate})
 
 211                       : ($::instance_conf->get_ap_changeable == 1);
 
 212   $readonly       = $form->{radier} ? "" : $readonly;
 
 214   $form->{readonly} = $readonly;
 
 216   $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
 
 217   if ( $form->{forex} ) {
 
 218     $form->{exchangerate} = $form->{forex};
 
 222   $form->{exchangerate}    = $form->{exchangerate} ? $form->format_amount(\%myconfig, $form->{exchangerate}) : '';
 
 223   $form->{creditlimit}     = $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
 
 224   $form->{creditremaining} = $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
 
 227   if (($rows = $form->numtextrows($form->{notes}, 50)) < 2) {
 
 230   $form->{textarea_rows} = $rows;
 
 232   $form->{creditremaining_plus} = ($form->{creditremaining} =~ /-/) ? "0" : "1";
 
 234   my @old_project_ids = ();
 
 237       if ($form->{"project_id_$_"}) {
 
 238         push(@old_project_ids, $form->{"project_id_$_"});
 
 241     (1..$form->{"rowcount"})
 
 244   $form->get_lists("projects"  => { "key"       => "ALL_PROJECTS",
 
 246                                     "old_id"    => \@old_project_ids },
 
 247                    "charts"    => { "key"       => "ALL_CHARTS",
 
 248                                     "transdate" => $form->{transdate} },
 
 249                    "taxcharts" => { "key"       => "ALL_TAXCHARTS",
 
 250                                     "module"    => "AP" },);
 
 253     { $_->{link_split} = [ split(/:/, $_->{link}) ]; }
 
 254     @{ $form->{ALL_CHARTS} }
 
 257   my %project_labels = ();
 
 258   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
 
 259     $project_labels{$item->{id}} = $item->{projectnumber};
 
 265   foreach my $item (@{ $form->{ALL_CHARTS} }) {
 
 266     if ( grep({ $_ eq 'AP_amount' } @{ $item->{link_split} }) ) {
 
 267       if ( $taxchart_init eq '' ) {
 
 268         $taxchart_init = $item->{tax_id};
 
 271       push(@{ $form->{ALL_CHARTS_AP_amount} }, $item);
 
 273     elsif ( grep({ $_ eq 'AP' } @{ $item->{link_split} }) ) {
 
 274       push(@{ $form->{ALL_CHARTS_AP} }, $item);
 
 276     elsif ( grep({ $_ eq 'AP_paid' } @{ $item->{link_split} }) ) {
 
 277       push(@{ $form->{ALL_CHARTS_AP_paid} }, $item);
 
 280     $charts{$item->{accno}} = $item;
 
 284   foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
 
 285     my $key = $item->{id} .'--'. $item->{rate};
 
 287     if ( $taxchart_init eq $item->{id} ) {
 
 288       $taxchart_init = $key;
 
 291     $taxcharts{$item->{id}} = $item;
 
 294   $form->{jsscript} = 1;
 
 296   my $follow_up_vc         =  $form->{vendor};
 
 297   $follow_up_vc            =~ s/--.*?//;
 
 298   my $follow_up_trans_info =  "$form->{invnumber} ($follow_up_vc)";
 
 300   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
 
 301   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
 
 302   $form->{javascript} .= qq|<script type="text/javascript" src="js/follow_up.js"></script>|;
 
 306   for my $i (1 .. $form->{rowcount}) {
 
 309     $form->{"amount_$i"} = $form->format_amount(\%myconfig, $form->{"amount_$i"}, 2);
 
 310     $form->{"tax_$i"} = $form->format_amount(\%myconfig, $form->{"tax_$i"}, 2);
 
 312     my $selected_accno_full;
 
 313     my ($accno_row) = split(/--/, $form->{"AP_amount_$i"});
 
 314     my $item = $charts{$accno_row};
 
 315     $selected_accno_full = "$item->{accno}--$item->{tax_id}";
 
 317     my $selected_taxchart = $form->{"taxchart_$i"};
 
 318     my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full);
 
 319     my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_AP_amount_$i"});
 
 321     if ($previous_accno &&
 
 322         ($previous_accno eq $selected_accno) &&
 
 323         ($previous_tax_id ne $selected_tax_id)) {
 
 324       my $item = $taxcharts{$selected_tax_id};
 
 325       $selected_taxchart = "$item->{id}--$item->{rate}";
 
 328     $selected_taxchart = $taxchart_init unless ($form->{"taxchart_$i"});
 
 330     $form->{'selected_accno_full_'. $i} = $selected_accno_full;
 
 332     $form->{'selected_taxchart_'. $i} = $selected_taxchart;
 
 335   $form->{AP_amount_value_title_sub} = sub {
 
 338       $item->{accno} .'--'. $item->{tax_id},
 
 339       $item->{accno} .'--'. $item->{description},
 
 343   $form->{taxchart_value_title_sub} = sub {
 
 346       $item->{id} .'--'. $item->{rate},
 
 347       $item->{taxdescription} .' '. ($item->{rate} * 100) .' %',
 
 351   $form->{AP_paid_value_title_sub} = sub {
 
 355       $item->{accno} .'--'. $item->{description}
 
 359   $form->{APselected_value_title_sub} = sub {
 
 363       $item->{accno} .'--'. $item->{description}
 
 367   $form->{invtotal_unformatted} = $form->{invtotal};
 
 368   $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
 
 370   $form->{totalpaid} = 0;
 
 372   if ( $form->{'paid_'. $form->{paidaccounts}} ) {
 
 373     $form->{paidaccounts}++;
 
 375   for my $i (1 .. $form->{paidaccounts}) {
 
 376     $form->{totalpaid} += $form->{"paid_$i"};
 
 379     if ($form->{"paid_$i"}) {
 
 380       $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
 
 382     if ($form->{"exchangerate_$i"} == 0) {
 
 383       $form->{"exchangerate_$i"} = "";
 
 385       $form->{"exchangerate_$i"} =
 
 386         $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
 
 390     if (SL::DB::Default->get->payments_changeable == 0) {
 
 392       $changeable = ($form->{"acc_trans_id_$i"})? 0 : 1;
 
 394     if (SL::DB::Default->get->payments_changeable == 2) {
 
 396       $changeable = (($form->{"gldate_$i"} eq '') || $form->current_date(\%myconfig) eq $form->{"gldate_$i"});
 
 399     $form->{'paidaccount_changeable_'. $i} = $changeable;
 
 401     $form->{'labelpaid_project_id_'. $i} = $project_labels{$form->{'paid_project_id_'. $i}};
 
 404   $form->{paid_missing} = $form->{invtotal_unformatted} - $form->{totalpaid};
 
 406   print $form->parse_html_template('ap/form_header');
 
 408   $main::lxdebug->leave_sub();
 
 412   $::lxdebug->enter_sub;
 
 413   $::auth->assert('general_ledger');
 
 418     my $follow_ups = FU->follow_ups('trans_id' => $::form->{id});
 
 420     if (@{ $follow_ups }) {
 
 421       $num_due        = sum map { $_->{due} * 1 } @{ $follow_ups };
 
 422       $num_follow_ups = scalar @{ $follow_ups }
 
 426   my $transdate = $::form->datetonum($::form->{transdate}, \%::myconfig);
 
 427   my $closedto  = $::form->datetonum($::form->{closedto},  \%::myconfig);
 
 429   my $storno = $::form->{id}
 
 430             && !IS->has_storno(\%::myconfig, $::form, 'ap')
 
 431             && !IS->is_storno( \%::myconfig, $::form, 'ap', $::form->{id})
 
 432             && ($::form->{totalpaid} == 0 || $::form->{totalpaid} eq '');
 
 435   print $::form->parse_html_template('ap/form_footer', {
 
 437     num_follow_ups    => $num_follow_ups,
 
 438     show_post_draft   => ($transdate > $closedto) && !$::form->{id},
 
 439     show_storno       => $storno,
 
 442   $::lxdebug->leave_sub;
 
 446   $main::lxdebug->enter_sub();
 
 448   my $form     = $main::form;
 
 449   my %myconfig = %main::myconfig;
 
 451   $main::auth->assert('general_ledger');
 
 453   &mark_as_paid_common(\%myconfig,"ap");
 
 455   $main::lxdebug->leave_sub();
 
 459   $main::lxdebug->enter_sub();
 
 461   my $form     = $main::form;
 
 462   my %myconfig = %main::myconfig;
 
 464   $main::auth->assert('general_ledger');
 
 468   $form->{invtotal} = 0;
 
 470   delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
 
 472   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
 
 473     qw(exchangerate creditlimit creditremaining);
 
 475   my @flds  = qw(amount AP_amount projectnumber oldprojectnumber project_id taxchart);
 
 477   my (@a, $j, $totaltax);
 
 478   for my $i (1 .. $form->{rowcount}) {
 
 479     $form->{"amount_$i"} =
 
 480       $form->parse_amount(\%myconfig, $form->{"amount_$i"});
 
 481     $form->{"tax_$i"} = $form->parse_amount(\%myconfig, $form->{"tax_$i"});
 
 482     if ($form->{"amount_$i"}) {
 
 485       my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
 
 487         if ($form->{taxincluded}) {
 
 488           $form->{"tax_$i"} = $form->{"amount_$i"} / ($rate + 1) * $rate;
 
 490           $form->{"tax_$i"} = $form->{"amount_$i"} * $rate;
 
 493         $form->{"tax_$i"} = 0;
 
 495       $form->{"tax_$i"} = $form->round_amount($form->{"tax_$i"}, 2);
 
 497       $totaltax += $form->{"tax_$i"};
 
 498       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
 
 502   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
 
 504   map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
 
 506   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
 
 507   $form->{exchangerate} = $form->{forex} if $form->{forex};
 
 509   $form->{invdate} = $form->{transdate};
 
 510   my %saved_variables = map +( $_ => $form->{$_} ), qw(AP AP_amount_1 taxchart_1 notes);
 
 512   my $vendor_changed = &check_name("vendor");
 
 514   $form->{AP} = $saved_variables{AP};
 
 515   if ($saved_variables{AP_amount_1} =~ m/.--./) {
 
 516     map { $form->{$_} = $saved_variables{$_} } qw(AP_amount_1 taxchart_1);
 
 518     delete $form->{taxchart_1};
 
 521   $form->{rowcount} = $count + 1;
 
 524     ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
 
 527   for my $i (1 .. $form->{paidaccounts}) {
 
 528     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
 
 531           $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
 
 532       } qw(paid exchangerate);
 
 534       $totalpaid += $form->{"paid_$i"};
 
 536       $form->{"forex_$i"}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
 
 537       $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
 
 541   $form->{creditremaining} -=
 
 542     ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} -
 
 543      $form->{oldinvtotal});
 
 544   $form->{oldinvtotal}  = $form->{invtotal};
 
 545   $form->{oldtotalpaid} = $totalpaid;
 
 549   $main::lxdebug->leave_sub();
 
 554   $main::lxdebug->enter_sub();
 
 556   my $form     = $main::form;
 
 557   my %myconfig = %main::myconfig;
 
 558   my $locale   = $main::locale;
 
 560   $main::auth->assert('general_ledger');
 
 562   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 564   my $invdate = $form->datetonum($form->{transdate}, \%myconfig);
 
 566   for my $i (1 .. $form->{paidaccounts}) {
 
 567     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
 
 568       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
 
 570       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
 572       $form->error($locale->text('Cannot post payment for a closed period!'))
 
 573         if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
 
 575       if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
 
 576         $form->{"exchangerate_$i"} = $form->{exchangerate}
 
 577           if ($invdate == $datepaid);
 
 578         $form->isblank("exchangerate_$i",
 
 579                        $locale->text('Exchangerate for payment missing!'));
 
 584   ($form->{AP})      = split /--/, $form->{AP};
 
 585   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
 
 586   $form->redirect($locale->text('Payment posted!'))
 
 587       if (AP->post_payment(\%myconfig, \%$form));
 
 588     $form->error($locale->text('Cannot post payment!'));
 
 591   $main::lxdebug->leave_sub();
 
 596   $main::lxdebug->enter_sub();
 
 598   my $form     = $main::form;
 
 599   my %myconfig = %main::myconfig;
 
 600   my $locale   = $main::locale;
 
 602   $main::auth->assert('general_ledger');
 
 606   # check if there is a vendor, invoice and due date
 
 607   $form->isblank("transdate", $locale->text("Invoice Date missing!"));
 
 608   $form->isblank("duedate",   $locale->text("Due Date missing!"));
 
 609   $form->isblank("vendor",    $locale->text('Vendor missing!'));
 
 611   if ($myconfig{mandatory_departments} && !$form->{department}) {
 
 612     $form->{saved_message} = $::locale->text('You have to specify a department.');
 
 617   my $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
 
 618   my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
 
 619   $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
 
 621   my $zero_amount_posting = 1;
 
 622   for my $i (1 .. $form->{rowcount}) {
 
 623     if ($form->parse_amount(\%myconfig, $form->{"amount_$i"})) {
 
 624       $zero_amount_posting = 0;
 
 629   $form->error($locale->text('Zero amount posting!')) if $zero_amount_posting;
 
 631   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
 
 632     if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
 
 635   for my $i (1 .. $form->{paidaccounts}) {
 
 636     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
 
 637       my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
 
 639       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
 641       $form->error($locale->text('Cannot post payment for a closed period!'))
 
 642         if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
 
 644       if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
 
 645         $form->{"exchangerate_$i"} = $form->{exchangerate}
 
 646           if ($transdate == $datepaid);
 
 647         $form->isblank("exchangerate_$i",
 
 648                        $locale->text('Exchangerate for payment missing!'));
 
 654   # if old vendor ne vendor redo form
 
 655   my ($vendor) = split /--/, $form->{vendor};
 
 656   if ($form->{oldvendor} ne "$vendor--$form->{vendor_id}") {
 
 660   my ($debitaccno,    $debittaxkey)    = split /--/, $form->{AP_amountselected};
 
 661   my ($taxkey,        $NULL)           = split /--/, $form->{taxchartselected};
 
 662   my ($payablesaccno, $payablestaxkey) = split /--/, $form->{APselected};
 
 663 #  $form->{AP_amount_1}  = $debitaccno;
 
 664   $form->{AP_payables}  = $payablesaccno;
 
 665   $form->{taxkey}       = $taxkey;
 
 668   $form->{id} = 0 if $form->{postasnew};
 
 670   if (AP->post_transaction(\%myconfig, \%$form)) {
 
 672     if(!exists $form->{addition} && $form->{id} ne "") {
 
 673       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
 674       $form->{addition} = "POSTED";
 
 677     # /saving the history
 
 678     remove_draft() if $form->{remove_draft};
 
 679     # Dieser Text wird niemals ausgegeben: Probleme beim redirect?
 
 680     $form->redirect($locale->text('Transaction posted!')) unless $inline;
 
 682     $form->error($locale->text('Cannot post transaction!'));
 
 685   $main::lxdebug->leave_sub();
 
 689   $main::lxdebug->enter_sub();
 
 691   my $form     = $main::form;
 
 692   my %myconfig = %main::myconfig;
 
 694   $main::auth->assert('general_ledger');
 
 696   $form->{postasnew} = 1;
 
 698   if(!exists $form->{addition} && $form->{id} ne "") {
 
 699     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
 700     $form->{addition} = "POSTED AS NEW";
 
 703   # /saving the history
 
 706   $main::lxdebug->leave_sub();
 
 710   $main::lxdebug->enter_sub();
 
 712   my $form     = $main::form;
 
 713   my %myconfig = %main::myconfig;
 
 715   $main::auth->assert('general_ledger');
 
 717   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);
 
 718   $form->{paidaccounts} = 1;
 
 720   $form->{invdate} = $form->current_date(\%myconfig);
 
 723   $main::lxdebug->leave_sub();
 
 727   $main::lxdebug->enter_sub();
 
 729   my $form     = $main::form;
 
 730   my $locale   = $main::locale;
 
 732   $main::auth->assert('general_ledger');
 
 734   $form->{title} = $locale->text('Confirm!');
 
 738   delete $form->{header};
 
 741 <form method=post action=$form->{script}>
 
 744   foreach my $key (keys %$form) {
 
 745     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
 
 746     $form->{$key} =~ s/\"/"/g;
 
 747     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
 
 751 <h2 class=confirm>$form->{title}</h2>
 
 754     . $locale->text('Are you sure you want to delete Transaction')
 
 755     . qq| $form->{invnumber}</h4>
 
 757 <input name=action class=submit type=submit value="|
 
 758     . $locale->text('Yes') . qq|">
 
 762   $main::lxdebug->leave_sub();
 
 766   $main::lxdebug->enter_sub();
 
 768   my $form     = $main::form;
 
 769   my %myconfig = %main::myconfig;
 
 770   my $locale   = $main::locale;
 
 772   $main::auth->assert('general_ledger');
 
 774   if (AP->delete_transaction(\%myconfig, \%$form)) {
 
 776     if(!exists $form->{addition}) {
 
 777       $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
 778       $form->{addition} = "DELETED";
 
 781     # /saving the history
 
 782     $form->redirect($locale->text('Transaction deleted!'));
 
 784   $form->error($locale->text('Cannot delete transaction!'));
 
 786   $main::lxdebug->leave_sub();
 
 790   $main::lxdebug->enter_sub();
 
 792   $main::auth->assert('general_ledger | vendor_invoice_edit');
 
 794   my $form     = $main::form;
 
 795   my %myconfig = %main::myconfig;
 
 796   my $locale   = $main::locale;
 
 798   # setup customer selection
 
 799   $form->all_vc(\%myconfig, "vendor", "AP");
 
 801   $form->{title}    = $locale->text('AP Transactions');
 
 802   $::request->{layout}->focus('#vendor');
 
 803   $form->{jsscript} = 1;
 
 805   $form->get_lists("projects"     => { "key" => "ALL_PROJECTS", "all" => 1 },
 
 806                    "departments"  => "ALL_DEPARTMENTS",
 
 807                    "vendors"      => "ALL_VC");
 
 809   # constants and subs for template
 
 810   $form->{jsscript}  = 1;
 
 811   $form->{vc_keys}   = sub { "$_[0]->{name}--$_[0]->{id}" };
 
 814   print $form->parse_html_template('ap/search', { %myconfig });
 
 816   $main::lxdebug->leave_sub();
 
 819 sub create_subtotal_row {
 
 820   $main::lxdebug->enter_sub();
 
 822   my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
 
 824   my $form     = $main::form;
 
 825   my %myconfig = %main::myconfig;
 
 827   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
 
 829   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
 
 831   $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
 
 833   map { $totals->{$_} = 0 } @{ $subtotal_columns };
 
 835   $main::lxdebug->leave_sub();
 
 840 sub ap_transactions {
 
 841   $main::lxdebug->enter_sub();
 
 843   my $form     = $main::form;
 
 844   my %myconfig = %main::myconfig;
 
 845   my $locale   = $main::locale;
 
 847   $main::auth->assert('general_ledger | vendor_invoice_edit');
 
 849   ($form->{vendor}, $form->{vendor_id}) = split(/--/, $form->{vendor});
 
 851   report_generator_set_default_sort('transdate', 1);
 
 853   AP->ap_transactions(\%myconfig, \%$form);
 
 855   $form->{title} = $locale->text('AP Transactions');
 
 857   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 860     qw(transdate id type invnumber ordnumber name netamount tax amount paid datepaid
 
 861        due duedate transaction_description notes employee globalprojectnumber
 
 862        vendornumber country ustid taxzone payment_terms charts);
 
 864   my @hidden_variables = map { "l_${_}" } @columns;
 
 865   push @hidden_variables, "l_subtotal", qw(open closed vendor invnumber ordnumber transaction_description notes project_id transdatefrom transdateto department);
 
 867   my $href = build_std_url('action=ap_transactions', grep { $form->{$_} } @hidden_variables);
 
 870     'transdate'               => { 'text' => $locale->text('Date'), },
 
 871     'id'                      => { 'text' => $locale->text('ID'), },
 
 872     'type'                    => { 'text' => $locale->text('Type'), },
 
 873     'invnumber'               => { 'text' => $locale->text('Invoice'), },
 
 874     'ordnumber'               => { 'text' => $locale->text('Order'), },
 
 875     'name'                    => { 'text' => $locale->text('Vendor'), },
 
 876     'netamount'               => { 'text' => $locale->text('Amount'), },
 
 877     'tax'                     => { 'text' => $locale->text('Tax'), },
 
 878     'amount'                  => { 'text' => $locale->text('Total'), },
 
 879     'paid'                    => { 'text' => $locale->text('Paid'), },
 
 880     'datepaid'                => { 'text' => $locale->text('Date Paid'), },
 
 881     'due'                     => { 'text' => $locale->text('Amount Due'), },
 
 882     'duedate'                 => { 'text' => $locale->text('Due Date'), },
 
 883     'transaction_description' => { 'text' => $locale->text('Transaction description'), },
 
 884     'notes'                   => { 'text' => $locale->text('Notes'), },
 
 885     'employee'                => { 'text' => $locale->text('Employee'), },
 
 886     'globalprojectnumber'     => { 'text' => $locale->text('Document Project Number'), },
 
 887     'vendornumber'            => { 'text' => $locale->text('Vendor Number'), },
 
 888     'country'                 => { 'text' => $locale->text('Country'), },
 
 889     'ustid'                   => { 'text' => $locale->text('USt-IdNr.'), },
 
 890     'taxzone'                 => { 'text' => $locale->text('Steuersatz'), },
 
 891     'payment_terms'           => { 'text' => $locale->text('Payment Terms'), },
 
 892     'charts'                  => { 'text' => $locale->text('Buchungskonto'), },
 
 895   foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid employee shippingpoint shipvia transaction_description)) {
 
 896     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
 
 897     $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
 
 900   my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
 
 902   $form->{"l_type"} = "Y";
 
 903   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
 
 905   $report->set_columns(%column_defs);
 
 906   $report->set_column_order(@columns);
 
 908   $report->set_export_options('ap_transactions', @hidden_variables, qw(sort sortdir));
 
 910   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
 
 913   push @options, $locale->text('Vendor')                  . " : $form->{vendor}"                         if ($form->{vendor});
 
 914   push @options, $locale->text('Department')              . " : " . (split /--/, $form->{department})[0] if ($form->{department});
 
 915   push @options, $locale->text('Invoice Number')          . " : $form->{invnumber}"                      if ($form->{invnumber});
 
 916   push @options, $locale->text('Order Number')            . " : $form->{ordnumber}"                      if ($form->{ordnumber});
 
 917   push @options, $locale->text('Notes')                   . " : $form->{notes}"                          if ($form->{notes});
 
 918   push @options, $locale->text('Transaction description') . " : $form->{transaction_description}"        if ($form->{transaction_description});
 
 919   push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1)      if ($form->{transdatefrom});
 
 920   push @options, $locale->text('Bis')  . " " . $locale->date(\%myconfig, $form->{transdateto},   1)      if ($form->{transdateto});
 
 921   push @options, $locale->text('Open')                                                                   if ($form->{open});
 
 922   push @options, $locale->text('Closed')                                                                 if ($form->{closed});
 
 924   $report->set_options('top_info_text'        => join("\n", @options),
 
 925                        'raw_bottom_info_text' => $form->parse_html_template('ap/ap_transactions_bottom'),
 
 926                        'output_format'        => 'HTML',
 
 927                        'title'                => $form->{title},
 
 928                        'attachment_basename'  => $locale->text('vendor_invoice_list') . strftime('_%Y%m%d', localtime time),
 
 930   $report->set_options_from_form();
 
 931   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 933   # add sort and escape callback, this one we use for the add sub
 
 934   $form->{callback} = $href .= "&sort=$form->{sort}";
 
 936   # escape callback for href
 
 937   my $callback = $form->escape($href);
 
 939   my @subtotal_columns = qw(netamount amount paid due);
 
 941   my %totals    = map { $_ => 0 } @subtotal_columns;
 
 942   my %subtotals = map { $_ => 0 } @subtotal_columns;
 
 946   foreach my $ap (@{ $form->{AP} }) {
 
 947     $ap->{tax} = $ap->{amount} - $ap->{netamount};
 
 948     $ap->{due} = $ap->{amount} - $ap->{paid};
 
 950     map { $subtotals{$_} += $ap->{$_};
 
 951           $totals{$_}    += $ap->{$_} } @subtotal_columns;
 
 953     map { $ap->{$_} = $form->format_amount(\%myconfig, $ap->{$_}, 2) } qw(netamount tax amount paid due);
 
 955     my $is_storno  = $ap->{storno} &&  $ap->{storno_id};
 
 956     my $has_storno = $ap->{storno} && !$ap->{storno_id};
 
 958     if ($ap->{invoice}) {
 
 960           $has_storno       ? $locale->text("Invoice with Storno (abbreviation)")
 
 961         : $is_storno        ? $locale->text("Storno (one letter abbreviation)")
 
 962         :                     $locale->text("Invoice (one letter abbreviation)");
 
 965           $has_storno       ? $locale->text("AP Transaction with Storno (abbreviation)")
 
 966         : $is_storno        ? $locale->text("AP Transaction Storno (one letter abbreviation)")
 
 967         :                     $locale->text("AP Transaction (abbreviation)");
 
 972     foreach my $column (@columns) {
 
 974         'data'  => $ap->{$column},
 
 975         'align' => $column_alignment{$column},
 
 979     $row->{invnumber}->{link} = build_std_url("script=" . ($ap->{invoice} ? 'ir.pl' : 'ap.pl'), 'action=edit')
 
 980       . "&id=" . E($ap->{id}) . "&callback=${callback}";
 
 982     my $row_set = [ $row ];
 
 984     if (($form->{l_subtotal} eq 'Y')
 
 985         && (($idx == (scalar @{ $form->{AP} } - 1))
 
 986             || ($ap->{ $form->{sort} } ne $form->{AP}->[$idx + 1]->{ $form->{sort} }))) {
 
 987       push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
 
 990     $report->add_data($row_set);
 
 995   $report->add_separator();
 
 996   $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
 
 998   $report->generate_with_headers();
 
1000   $main::lxdebug->leave_sub();
 
1004   $main::lxdebug->enter_sub();
 
1006   my $form     = $main::form;
 
1007   my %myconfig = %main::myconfig;
 
1008   my $locale   = $main::locale;
 
1010   $main::auth->assert('general_ledger');
 
1012   if (IS->has_storno(\%myconfig, $form, 'ap')) {
 
1013     $form->{title} = $locale->text("Cancel Accounts Payables Transaction");
 
1014     $form->error($locale->text("Transaction has already been cancelled!"));
 
1017   $form->error($locale->text('Cannot post storno for a closed period!'))
 
1018     if ( $form->date_closed($form->{transdate}, \%myconfig));
 
1020   AP->storno($form, \%myconfig, $form->{id});
 
1022   # saving the history
 
1023   if(!exists $form->{addition} && $form->{id} ne "") {
 
1024     $form->{snumbers} = "ordnumber_$form->{ordnumber}";
 
1025     $form->{addition} = "STORNO";
 
1026     $form->save_history;
 
1028   # /saving the history
 
1030   $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
 
1032   $main::lxdebug->leave_sub();