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};
100 create_links(dont_save => 1);
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();
140 my $form = $main::form;
141 my %myconfig = %main::myconfig;
143 $main::auth->assert('general_ledger');
145 $form->create_links("AP", \%myconfig, "vendor");
147 if (!$params{dont_save}) {
148 %saved = map { ($_ => $form->{$_}) } qw(direct_debit taxincluded);
149 $saved{duedate} = $form->{duedate} if $form->{duedate};
152 IR->get_vendor(\%myconfig, \%$form);
154 $form->{$_} = $saved{$_} for keys %saved;
155 $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
156 $form->{rowcount} = 1;
158 # build the popup menus
159 $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
162 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
164 map { my $quoted = H($_); $form->{selectcurrency} .= "<option value=\"${quoted}\">${quoted}\n" } $form->get_all_currencies(\%myconfig);
167 if (@{ $form->{all_vendor} || [] }) {
168 $form->{vendor} = qq|$form->{vendor}--$form->{vendor_id}|;
169 map { my $quoted = H($_->{name} . "--" . $_->{id}); $form->{selectvendor} .= "<option value=\"${quoted}\">${quoted}\n" }
170 (@{ $form->{all_vendor} });
174 if (@{ $form->{all_departments} || [] }) {
175 $form->{department} = "$form->{department}--$form->{department_id}";
176 $form->{selectdepartment} = "<option>\n" . join('', map { my $quoted = H("$_->{description}--$_->{id}"); "<option value=\"${quoted}\">${quoted}\n"} @{ $form->{all_departments} || [] });
179 $form->{employee} = "$form->{employee}--$form->{employee_id}";
181 AP->setup_form($form);
184 ($form->datetonum($form->{transdate}, \%myconfig) <=
185 $form->datetonum($form->{closedto}, \%myconfig));
187 $main::lxdebug->leave_sub();
191 $main::lxdebug->enter_sub();
193 my $form = $main::form;
194 my %myconfig = %main::myconfig;
195 my $locale = $main::locale;
196 my $cgi = $::request->{cgi};
198 $main::auth->assert('general_ledger');
200 $form->{title_} = $form->{title};
201 $form->{title} = $form->{title} eq 'Add' ? $locale->text('Add Accounts Payables Transaction') : $locale->text('Edit Accounts Payables Transaction');
203 # type=submit $locale->text('Add Accounts Payables Transaction')
204 # type=submit $locale->text('Edit Accounts Payables Transaction')
206 # set option selected
207 foreach my $item (qw(vendor currency department)) {
208 my $to_replace = H($form->{$item});
209 $form->{"select$item"} =~ s/ selected//;
210 $form->{"select$item"} =~ s/>\Q${to_replace}\E/ selected>${to_replace}/;
212 my $readonly = $form->{id} ? "readonly" : "";
214 $form->{radier} = ($::instance_conf->get_ap_changeable == 2)
215 ? ($form->current_date(\%myconfig) eq $form->{gldate})
216 : ($::instance_conf->get_ap_changeable == 1);
217 $readonly = $form->{radier} ? "" : $readonly;
219 $form->{readonly} = $readonly;
221 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
222 if ( $form->{forex} ) {
223 $form->{exchangerate} = $form->{forex};
227 $form->{exchangerate} = $form->{exchangerate} ? $form->format_amount(\%myconfig, $form->{exchangerate}) : '';
228 $form->{creditlimit} = $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
229 $form->{creditremaining} = $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
232 if (($rows = $form->numtextrows($form->{notes}, 50)) < 2) {
235 $form->{textarea_rows} = $rows;
237 $form->{creditremaining_plus} = ($form->{creditremaining} =~ /-/) ? "0" : "1";
239 my @old_project_ids = ();
242 if ($form->{"project_id_$_"}) {
243 push(@old_project_ids, $form->{"project_id_$_"});
246 (1..$form->{"rowcount"})
249 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
251 "old_id" => \@old_project_ids },
252 "charts" => { "key" => "ALL_CHARTS",
253 "transdate" => $form->{transdate} },
254 "taxcharts" => { "key" => "ALL_TAXCHARTS",
255 "module" => "AP" },);
258 { $_->{link_split} = [ split(/:/, $_->{link}) ]; }
259 @{ $form->{ALL_CHARTS} }
262 my %project_labels = ();
263 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
264 $project_labels{$item->{id}} = $item->{projectnumber};
270 foreach my $item (@{ $form->{ALL_CHARTS} }) {
271 if ( grep({ $_ eq 'AP_amount' } @{ $item->{link_split} }) ) {
272 if ( $taxchart_init eq '' ) {
273 $taxchart_init = $item->{tax_id};
276 push(@{ $form->{ALL_CHARTS_AP_amount} }, $item);
278 elsif ( grep({ $_ eq 'AP' } @{ $item->{link_split} }) ) {
279 push(@{ $form->{ALL_CHARTS_AP} }, $item);
281 elsif ( grep({ $_ eq 'AP_paid' } @{ $item->{link_split} }) ) {
282 push(@{ $form->{ALL_CHARTS_AP_paid} }, $item);
285 $charts{$item->{accno}} = $item;
289 foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
290 my $key = $item->{id} .'--'. $item->{rate};
292 if ( $taxchart_init eq $item->{id} ) {
293 $taxchart_init = $key;
296 $taxcharts{$item->{id}} = $item;
299 my $follow_up_vc = $form->{vendor};
300 $follow_up_vc =~ s/--.*?//;
301 my $follow_up_trans_info = "$form->{invnumber} ($follow_up_vc)";
303 $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
304 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
305 $form->{javascript} .= qq|<script type="text/javascript" src="js/follow_up.js"></script>|;
309 for my $i (1 .. $form->{rowcount}) {
312 $form->{"amount_$i"} = $form->format_amount(\%myconfig, $form->{"amount_$i"}, 2);
313 $form->{"tax_$i"} = $form->format_amount(\%myconfig, $form->{"tax_$i"}, 2);
315 my $selected_accno_full;
316 my ($accno_row) = split(/--/, $form->{"AP_amount_$i"});
317 my $item = $charts{$accno_row};
318 $selected_accno_full = "$item->{accno}--$item->{tax_id}";
320 my $selected_taxchart = $form->{"taxchart_$i"};
321 my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full);
322 my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_AP_amount_$i"});
324 if ($previous_accno &&
325 ($previous_accno eq $selected_accno) &&
326 ($previous_tax_id ne $selected_tax_id)) {
327 my $item = $taxcharts{$selected_tax_id};
328 $selected_taxchart = "$item->{id}--$item->{rate}";
331 $selected_taxchart = $taxchart_init unless ($form->{"taxchart_$i"});
333 $form->{'selected_accno_full_'. $i} = $selected_accno_full;
335 $form->{'selected_taxchart_'. $i} = $selected_taxchart;
338 $form->{AP_amount_value_title_sub} = sub {
341 $item->{accno} .'--'. $item->{tax_id},
342 $item->{accno} .'--'. $item->{description},
346 $form->{taxchart_value_title_sub} = sub {
349 $item->{id} .'--'. $item->{rate},
350 $item->{taxdescription} .' '. ($item->{rate} * 100) .' %',
354 $form->{AP_paid_value_title_sub} = sub {
358 $item->{accno} .'--'. $item->{description}
362 $form->{APselected_value_title_sub} = sub {
366 $item->{accno} .'--'. $item->{description}
370 $form->{invtotal_unformatted} = $form->{invtotal};
371 $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
373 $form->{totalpaid} = 0;
375 if ( $form->{'paid_'. $form->{paidaccounts}} ) {
376 $form->{paidaccounts}++;
378 for my $i (1 .. $form->{paidaccounts}) {
379 $form->{totalpaid} += $form->{"paid_$i"};
382 if ($form->{"paid_$i"}) {
383 $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
385 if ($form->{"exchangerate_$i"} == 0) {
386 $form->{"exchangerate_$i"} = "";
388 $form->{"exchangerate_$i"} =
389 $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
393 if (SL::DB::Default->get->payments_changeable == 0) {
395 $changeable = ($form->{"acc_trans_id_$i"})? 0 : 1;
397 if (SL::DB::Default->get->payments_changeable == 2) {
399 $changeable = (($form->{"gldate_$i"} eq '') || $form->current_date(\%myconfig) eq $form->{"gldate_$i"});
402 $form->{'paidaccount_changeable_'. $i} = $changeable;
404 $form->{'labelpaid_project_id_'. $i} = $project_labels{$form->{'paid_project_id_'. $i}};
407 $form->{paid_missing} = $form->{invtotal_unformatted} - $form->{totalpaid};
409 print $form->parse_html_template('ap/form_header');
411 $main::lxdebug->leave_sub();
415 $::lxdebug->enter_sub;
416 $::auth->assert('general_ledger');
421 my $follow_ups = FU->follow_ups('trans_id' => $::form->{id});
423 if (@{ $follow_ups }) {
424 $num_due = sum map { $_->{due} * 1 } @{ $follow_ups };
425 $num_follow_ups = scalar @{ $follow_ups }
429 my $transdate = $::form->datetonum($::form->{transdate}, \%::myconfig);
430 my $closedto = $::form->datetonum($::form->{closedto}, \%::myconfig);
432 my $storno = $::form->{id}
433 && !IS->has_storno(\%::myconfig, $::form, 'ap')
434 && !IS->is_storno( \%::myconfig, $::form, 'ap', $::form->{id})
435 && ($::form->{totalpaid} == 0 || $::form->{totalpaid} eq '');
438 print $::form->parse_html_template('ap/form_footer', {
440 num_follow_ups => $num_follow_ups,
441 show_post_draft => ($transdate > $closedto) && !$::form->{id},
442 show_storno => $storno,
445 $::lxdebug->leave_sub;
449 $main::lxdebug->enter_sub();
451 my $form = $main::form;
452 my %myconfig = %main::myconfig;
454 $main::auth->assert('general_ledger');
456 &mark_as_paid_common(\%myconfig,"ap");
458 $main::lxdebug->leave_sub();
462 $main::lxdebug->enter_sub();
464 my $form = $main::form;
465 my %myconfig = %main::myconfig;
467 $main::auth->assert('general_ledger');
471 $form->{invtotal} = 0;
473 delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
475 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
476 qw(exchangerate creditlimit creditremaining);
478 my @flds = qw(amount AP_amount projectnumber oldprojectnumber project_id taxchart);
480 my (@a, $j, $totaltax);
481 for my $i (1 .. $form->{rowcount}) {
482 $form->{"amount_$i"} =
483 $form->parse_amount(\%myconfig, $form->{"amount_$i"});
484 $form->{"tax_$i"} = $form->parse_amount(\%myconfig, $form->{"tax_$i"});
485 if ($form->{"amount_$i"}) {
488 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
490 if ($form->{taxincluded}) {
491 $form->{"tax_$i"} = $form->{"amount_$i"} / ($rate + 1) * $rate;
493 $form->{"tax_$i"} = $form->{"amount_$i"} * $rate;
496 $form->{"tax_$i"} = 0;
498 $form->{"tax_$i"} = $form->round_amount($form->{"tax_$i"}, 2);
500 $totaltax += $form->{"tax_$i"};
501 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
505 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
507 map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
509 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
510 $form->{exchangerate} = $form->{forex} if $form->{forex};
512 $form->{invdate} = $form->{transdate};
513 my %saved_variables = map +( $_ => $form->{$_} ), qw(AP AP_amount_1 taxchart_1 notes);
515 my $vendor_changed = &check_name("vendor");
517 $form->{AP} = $saved_variables{AP};
518 if ($saved_variables{AP_amount_1} =~ m/.--./) {
519 map { $form->{$_} = $saved_variables{$_} } qw(AP_amount_1 taxchart_1);
521 delete $form->{taxchart_1};
524 $form->{rowcount} = $count + 1;
527 ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
530 for my $i (1 .. $form->{paidaccounts}) {
531 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
534 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
535 } qw(paid exchangerate);
537 $totalpaid += $form->{"paid_$i"};
539 $form->{"forex_$i"} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
540 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
544 $form->{creditremaining} -=
545 ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} -
546 $form->{oldinvtotal});
547 $form->{oldinvtotal} = $form->{invtotal};
548 $form->{oldtotalpaid} = $totalpaid;
552 $main::lxdebug->leave_sub();
557 $main::lxdebug->enter_sub();
559 my $form = $main::form;
560 my %myconfig = %main::myconfig;
561 my $locale = $main::locale;
563 $main::auth->assert('general_ledger');
565 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
567 my $invdate = $form->datetonum($form->{transdate}, \%myconfig);
569 for my $i (1 .. $form->{paidaccounts}) {
570 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
571 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
573 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
575 $form->error($locale->text('Cannot post payment for a closed period!'))
576 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
578 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
579 $form->{"exchangerate_$i"} = $form->{exchangerate}
580 if ($invdate == $datepaid);
581 $form->isblank("exchangerate_$i",
582 $locale->text('Exchangerate for payment missing!'));
587 ($form->{AP}) = split /--/, $form->{AP};
588 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
589 $form->redirect($locale->text('Payment posted!'))
590 if (AP->post_payment(\%myconfig, \%$form));
591 $form->error($locale->text('Cannot post payment!'));
594 $main::lxdebug->leave_sub();
599 $main::lxdebug->enter_sub();
601 my $form = $main::form;
602 my %myconfig = %main::myconfig;
603 my $locale = $main::locale;
605 $main::auth->assert('general_ledger');
609 # check if there is a vendor, invoice, due date and invnumber
610 $form->isblank("transdate", $locale->text("Invoice Date missing!"));
611 $form->isblank("duedate", $locale->text("Due Date missing!"));
612 $form->isblank("vendor", $locale->text('Vendor missing!'));
613 $form->isblank("invnumber", $locale->text('Invoice Number missing!'));
615 if ($myconfig{mandatory_departments} && !$form->{department}) {
616 $form->{saved_message} = $::locale->text('You have to specify a department.');
621 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
622 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
624 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
625 if ($form->date_max_future($form->{"transdate"}, \%myconfig));
626 $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
628 my $zero_amount_posting = 1;
629 for my $i (1 .. $form->{rowcount}) {
630 if ($form->parse_amount(\%myconfig, $form->{"amount_$i"})) {
631 $zero_amount_posting = 0;
636 $form->error($locale->text('Zero amount posting!')) if $zero_amount_posting;
638 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
639 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
642 for my $i (1 .. $form->{paidaccounts}) {
643 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
644 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
646 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
648 $form->error($locale->text('Cannot post payment for a closed period!'))
649 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
651 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
652 $form->{"exchangerate_$i"} = $form->{exchangerate}
653 if ($transdate == $datepaid);
654 $form->isblank("exchangerate_$i",
655 $locale->text('Exchangerate for payment missing!'));
661 # if old vendor ne vendor redo form
662 my ($vendor) = split /--/, $form->{vendor};
663 if ($form->{oldvendor} ne "$vendor--$form->{vendor_id}") {
667 my ($debitaccno, $debittaxkey) = split /--/, $form->{AP_amountselected};
668 my ($taxkey, $NULL) = split /--/, $form->{taxchartselected};
669 my ($payablesaccno, $payablestaxkey) = split /--/, $form->{APselected};
670 # $form->{AP_amount_1} = $debitaccno;
671 $form->{AP_payables} = $payablesaccno;
672 $form->{taxkey} = $taxkey;
675 $form->{id} = 0 if $form->{postasnew};
677 if (AP->post_transaction(\%myconfig, \%$form)) {
679 if(!exists $form->{addition} && $form->{id} ne "") {
680 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
681 $form->{addition} = "POSTED";
684 # /saving the history
685 remove_draft() if $form->{remove_draft};
686 # Dieser Text wird niemals ausgegeben: Probleme beim redirect?
687 $form->redirect($locale->text('Transaction posted!')) unless $inline;
689 $form->error($locale->text('Cannot post transaction!'));
692 $main::lxdebug->leave_sub();
696 $main::lxdebug->enter_sub();
698 my $form = $main::form;
699 my %myconfig = %main::myconfig;
701 $main::auth->assert('general_ledger');
703 $form->{postasnew} = 1;
705 if(!exists $form->{addition} && $form->{id} ne "") {
706 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
707 $form->{addition} = "POSTED AS NEW";
710 # /saving the history
713 $main::lxdebug->leave_sub();
717 $main::lxdebug->enter_sub();
719 my $form = $main::form;
720 my %myconfig = %main::myconfig;
722 $main::auth->assert('general_ledger');
724 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);
725 $form->{paidaccounts} = 1;
727 $form->{invdate} = $form->current_date(\%myconfig);
730 $main::lxdebug->leave_sub();
734 $main::lxdebug->enter_sub();
736 my $form = $main::form;
737 my $locale = $main::locale;
739 $main::auth->assert('general_ledger');
741 $form->{title} = $locale->text('Confirm!');
745 delete $form->{header};
748 <form method=post action=$form->{script}>
751 foreach my $key (keys %$form) {
752 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
753 $form->{$key} =~ s/\"/"/g;
754 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
758 <h2 class=confirm>$form->{title}</h2>
761 . $locale->text('Are you sure you want to delete Transaction')
762 . qq| $form->{invnumber}</h4>
764 <input name=action class=submit type=submit value="|
765 . $locale->text('Yes') . qq|">
769 $main::lxdebug->leave_sub();
773 $main::lxdebug->enter_sub();
775 my $form = $main::form;
776 my %myconfig = %main::myconfig;
777 my $locale = $main::locale;
779 $main::auth->assert('general_ledger');
781 if (AP->delete_transaction(\%myconfig, \%$form)) {
783 if(!exists $form->{addition}) {
784 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
785 $form->{addition} = "DELETED";
788 # /saving the history
789 $form->redirect($locale->text('Transaction deleted!'));
791 $form->error($locale->text('Cannot delete transaction!'));
793 $main::lxdebug->leave_sub();
797 $main::lxdebug->enter_sub();
799 $main::auth->assert('vendor_invoice_edit');
801 my $form = $main::form;
802 my %myconfig = %main::myconfig;
803 my $locale = $main::locale;
805 # setup customer selection
806 $form->all_vc(\%myconfig, "vendor", "AP");
808 $form->{title} = $locale->text('AP Transactions');
810 $form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 },
811 "departments" => "ALL_DEPARTMENTS",
812 "vendors" => "ALL_VC");
814 # constants and subs for template
815 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
818 print $form->parse_html_template('ap/search', { %myconfig });
820 $main::lxdebug->leave_sub();
823 sub create_subtotal_row {
824 $main::lxdebug->enter_sub();
826 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
828 my $form = $main::form;
829 my %myconfig = %main::myconfig;
831 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
833 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
835 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
837 map { $totals->{$_} = 0 } @{ $subtotal_columns };
839 $main::lxdebug->leave_sub();
844 sub ap_transactions {
845 $main::lxdebug->enter_sub();
847 my $form = $main::form;
848 my %myconfig = %main::myconfig;
849 my $locale = $main::locale;
851 $main::auth->assert('vendor_invoice_edit');
853 ($form->{vendor}, $form->{vendor_id}) = split(/--/, $form->{vendor});
855 report_generator_set_default_sort('transdate', 1);
857 AP->ap_transactions(\%myconfig, \%$form);
859 $form->{title} = $locale->text('AP Transactions');
861 my $report = SL::ReportGenerator->new(\%myconfig, $form);
864 qw(transdate id type invnumber ordnumber name netamount tax amount paid datepaid
865 due duedate transaction_description notes employee globalprojectnumber
866 vendornumber country ustid taxzone payment_terms charts);
868 my @hidden_variables = map { "l_${_}" } @columns;
869 push @hidden_variables, "l_subtotal", qw(open closed vendor invnumber ordnumber transaction_description notes project_id transdatefrom transdateto department);
871 my $href = build_std_url('action=ap_transactions', grep { $form->{$_} } @hidden_variables);
874 'transdate' => { 'text' => $locale->text('Date'), },
875 'id' => { 'text' => $locale->text('ID'), },
876 'type' => { 'text' => $locale->text('Type'), },
877 'invnumber' => { 'text' => $locale->text('Invoice'), },
878 'ordnumber' => { 'text' => $locale->text('Order'), },
879 'name' => { 'text' => $locale->text('Vendor'), },
880 'netamount' => { 'text' => $locale->text('Amount'), },
881 'tax' => { 'text' => $locale->text('Tax'), },
882 'amount' => { 'text' => $locale->text('Total'), },
883 'paid' => { 'text' => $locale->text('Paid'), },
884 'datepaid' => { 'text' => $locale->text('Date Paid'), },
885 'due' => { 'text' => $locale->text('Amount Due'), },
886 'duedate' => { 'text' => $locale->text('Due Date'), },
887 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
888 'notes' => { 'text' => $locale->text('Notes'), },
889 'employee' => { 'text' => $locale->text('Employee'), },
890 'globalprojectnumber' => { 'text' => $locale->text('Document Project Number'), },
891 'vendornumber' => { 'text' => $locale->text('Vendor Number'), },
892 'country' => { 'text' => $locale->text('Country'), },
893 'ustid' => { 'text' => $locale->text('USt-IdNr.'), },
894 'taxzone' => { 'text' => $locale->text('Steuersatz'), },
895 'payment_terms' => { 'text' => $locale->text('Payment Terms'), },
896 'charts' => { 'text' => $locale->text('Buchungskonto'), },
899 foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid employee shippingpoint shipvia transaction_description)) {
900 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
901 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
904 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
906 $form->{"l_type"} = "Y";
907 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
909 $report->set_columns(%column_defs);
910 $report->set_column_order(@columns);
912 $report->set_export_options('ap_transactions', @hidden_variables, qw(sort sortdir));
914 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
917 push @options, $locale->text('Vendor') . " : $form->{vendor}" if ($form->{vendor});
918 push @options, $locale->text('Department') . " : " . (split /--/, $form->{department})[0] if ($form->{department});
919 push @options, $locale->text('Invoice Number') . " : $form->{invnumber}" if ($form->{invnumber});
920 push @options, $locale->text('Order Number') . " : $form->{ordnumber}" if ($form->{ordnumber});
921 push @options, $locale->text('Notes') . " : $form->{notes}" if ($form->{notes});
922 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}" if ($form->{transaction_description});
923 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1) if ($form->{transdatefrom});
924 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1) if ($form->{transdateto});
925 push @options, $locale->text('Open') if ($form->{open});
926 push @options, $locale->text('Closed') if ($form->{closed});
928 $report->set_options('top_info_text' => join("\n", @options),
929 'raw_bottom_info_text' => $form->parse_html_template('ap/ap_transactions_bottom'),
930 'output_format' => 'HTML',
931 'title' => $form->{title},
932 'attachment_basename' => $locale->text('vendor_invoice_list') . strftime('_%Y%m%d', localtime time),
934 $report->set_options_from_form();
935 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
937 # add sort and escape callback, this one we use for the add sub
938 $form->{callback} = $href .= "&sort=$form->{sort}";
940 # escape callback for href
941 my $callback = $form->escape($href);
943 my @subtotal_columns = qw(netamount amount paid due);
945 my %totals = map { $_ => 0 } @subtotal_columns;
946 my %subtotals = map { $_ => 0 } @subtotal_columns;
950 foreach my $ap (@{ $form->{AP} }) {
951 $ap->{tax} = $ap->{amount} - $ap->{netamount};
952 $ap->{due} = $ap->{amount} - $ap->{paid};
954 map { $subtotals{$_} += $ap->{$_};
955 $totals{$_} += $ap->{$_} } @subtotal_columns;
957 map { $ap->{$_} = $form->format_amount(\%myconfig, $ap->{$_}, 2) } qw(netamount tax amount paid due);
959 my $is_storno = $ap->{storno} && $ap->{storno_id};
960 my $has_storno = $ap->{storno} && !$ap->{storno_id};
962 if ($ap->{invoice}) {
964 $has_storno ? $locale->text("Invoice with Storno (abbreviation)")
965 : $is_storno ? $locale->text("Storno (one letter abbreviation)")
966 : $locale->text("Invoice (one letter abbreviation)");
969 $has_storno ? $locale->text("AP Transaction with Storno (abbreviation)")
970 : $is_storno ? $locale->text("AP Transaction Storno (one letter abbreviation)")
971 : $locale->text("AP Transaction (abbreviation)");
976 foreach my $column (@columns) {
978 'data' => $ap->{$column},
979 'align' => $column_alignment{$column},
983 $row->{invnumber}->{link} = build_std_url("script=" . ($ap->{invoice} ? 'ir.pl' : 'ap.pl'), 'action=edit')
984 . "&id=" . E($ap->{id}) . "&callback=${callback}";
986 my $row_set = [ $row ];
988 if (($form->{l_subtotal} eq 'Y')
989 && (($idx == (scalar @{ $form->{AP} } - 1))
990 || ($ap->{ $form->{sort} } ne $form->{AP}->[$idx + 1]->{ $form->{sort} }))) {
991 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
994 $report->add_data($row_set);
999 $report->add_separator();
1000 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1002 $report->generate_with_headers();
1004 $main::lxdebug->leave_sub();
1008 $main::lxdebug->enter_sub();
1010 my $form = $main::form;
1011 my %myconfig = %main::myconfig;
1012 my $locale = $main::locale;
1014 $main::auth->assert('general_ledger');
1016 if (IS->has_storno(\%myconfig, $form, 'ap')) {
1017 $form->{title} = $locale->text("Cancel Accounts Payables Transaction");
1018 $form->error($locale->text("Transaction has already been cancelled!"));
1021 $form->error($locale->text('Cannot post storno for a closed period!'))
1022 if ( $form->date_closed($form->{transdate}, \%myconfig));
1024 AP->storno($form, \%myconfig, $form->{id});
1026 # saving the history
1027 if(!exists $form->{addition} && $form->{id} ne "") {
1028 $form->{snumbers} = "ordnumber_$form->{ordnumber}";
1029 $form->{addition} = "STORNO";
1030 $form->save_history;
1032 # /saving the history
1034 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1036 $main::lxdebug->leave_sub();