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(max sum);
36 use List::UtilsBy qw(sort_by);
43 use SL::ReportGenerator;
45 use SL::DB::PurchaseInvoice;
47 require "bin/mozilla/arap.pl";
48 require "bin/mozilla/common.pl";
49 require "bin/mozilla/reportgenerator.pl";
57 # this is for our long dates
58 # $locale->text('January')
59 # $locale->text('February')
60 # $locale->text('March')
61 # $locale->text('April')
62 # $locale->text('May ')
63 # $locale->text('June')
64 # $locale->text('July')
65 # $locale->text('August')
66 # $locale->text('September')
67 # $locale->text('October')
68 # $locale->text('November')
69 # $locale->text('December')
71 # this is for our short month
72 # $locale->text('Jan')
73 # $locale->text('Feb')
74 # $locale->text('Mar')
75 # $locale->text('Apr')
76 # $locale->text('May')
77 # $locale->text('Jun')
78 # $locale->text('Jul')
79 # $locale->text('Aug')
80 # $locale->text('Sep')
81 # $locale->text('Oct')
82 # $locale->text('Nov')
83 # $locale->text('Dec')
86 $main::lxdebug->enter_sub();
88 my $form = $main::form;
89 my %myconfig = %main::myconfig;
91 $main::auth->assert('general_ledger');
93 $form->{title} = "Add";
95 $form->{callback} = "ap.pl?action=add" unless $form->{callback};
97 AP->get_transdate(\%myconfig, $form);
98 $form->{initial_transdate} = $form->{transdate};
99 create_links(dont_save => 1);
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();
139 my $form = $main::form;
140 my %myconfig = %main::myconfig;
142 $main::auth->assert('general_ledger');
144 $form->create_links("AP", \%myconfig, "vendor");
146 if (!$params{dont_save}) {
147 %saved = map { ($_ => $form->{$_}) } qw(direct_debit taxincluded);
148 $saved{duedate} = $form->{duedate} if $form->{duedate};
151 IR->get_vendor(\%myconfig, \%$form);
153 $form->{$_} = $saved{$_} for keys %saved;
154 $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
155 $form->{rowcount} = 1;
157 # build the popup menus
158 $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
161 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
163 map { my $quoted = H($_); $form->{selectcurrency} .= "<option value=\"${quoted}\">${quoted}\n" } $form->get_all_currencies(\%myconfig);
166 if (@{ $form->{all_vendor} || [] }) {
167 $form->{vendor} = qq|$form->{vendor}--$form->{vendor_id}|;
168 map { my $quoted = H($_->{name} . "--" . $_->{id}); $form->{selectvendor} .= "<option value=\"${quoted}\">${quoted}\n" }
169 (@{ $form->{all_vendor} });
173 if (@{ $form->{all_departments} || [] }) {
174 $form->{department} = "$form->{department}--$form->{department_id}";
175 $form->{selectdepartment} = "<option>\n" . join('', map { my $quoted = H("$_->{description}--$_->{id}"); "<option value=\"${quoted}\">${quoted}\n"} @{ $form->{all_departments} || [] });
178 $form->{employee} = "$form->{employee}--$form->{employee_id}";
180 AP->setup_form($form);
183 ($form->datetonum($form->{transdate}, \%myconfig) <=
184 $form->datetonum($form->{closedto}, \%myconfig));
186 $main::lxdebug->leave_sub();
190 my @fields = qw(acc_trans_id gldate datepaid source memo paid AP_paid paid_project_id);
192 grep { $_->{paid} != 0 }
195 +{ map { ($_ => delete($::form->{"${_}_${idx}"})) } @fields }
196 } (1..$::form->{paidaccounts});
198 @payments = sort_by { DateTime->from_kivitendo($_->{datepaid}) } @payments;
200 $::form->{paidaccounts} = max scalar(@payments), 1;
202 foreach my $idx (1 .. scalar(@payments)) {
203 my $payment = $payments[$idx - 1];
204 $::form->{"${_}_${idx}"} = $payment->{$_} for @fields;
209 $main::lxdebug->enter_sub();
211 my $form = $main::form;
212 my %myconfig = %main::myconfig;
213 my $locale = $main::locale;
214 my $cgi = $::request->{cgi};
216 $main::auth->assert('general_ledger');
218 $::form->{invoice_obj} = SL::DB::PurchaseInvoice->new(id => $::form->{id})->load if $::form->{id};
220 $form->{title_} = $form->{title};
221 $form->{title} = $form->{title} eq 'Add' ? $locale->text('Add Accounts Payables Transaction') : $locale->text('Edit Accounts Payables Transaction');
223 # type=submit $locale->text('Add Accounts Payables Transaction')
224 # type=submit $locale->text('Edit Accounts Payables Transaction')
226 # set option selected
227 foreach my $item (qw(vendor currency department)) {
228 my $to_replace = H($form->{$item});
229 $form->{"select$item"} =~ s/ selected//;
230 $form->{"select$item"} =~ s/>\Q${to_replace}\E/ selected>${to_replace}/;
232 my $readonly = $form->{id} ? "readonly" : "";
234 $form->{radier} = ($::instance_conf->get_ap_changeable == 2)
235 ? ($form->current_date(\%myconfig) eq $form->{gldate})
236 : ($::instance_conf->get_ap_changeable == 1);
237 $readonly = $form->{radier} ? "" : $readonly;
239 $form->{readonly} = $readonly;
241 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
242 if ( $form->{forex} ) {
243 $form->{exchangerate} = $form->{forex};
247 $form->{exchangerate} = $form->{exchangerate} ? $form->format_amount(\%myconfig, $form->{exchangerate}) : '';
248 $form->{creditlimit} = $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
249 $form->{creditremaining} = $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
252 if (($rows = $form->numtextrows($form->{notes}, 50)) < 2) {
255 $form->{textarea_rows} = $rows;
257 $form->{creditremaining_plus} = ($form->{creditremaining} =~ /-/) ? "0" : "1";
259 my @old_project_ids = ();
262 if ($form->{"project_id_$_"}) {
263 push(@old_project_ids, $form->{"project_id_$_"});
266 (1..$form->{"rowcount"})
269 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
271 "old_id" => \@old_project_ids },
272 "charts" => { "key" => "ALL_CHARTS",
273 "transdate" => $form->{transdate} },
274 "taxcharts" => { "key" => "ALL_TAXCHARTS",
275 "module" => "AP" },);
278 { $_->{link_split} = [ split(/:/, $_->{link}) ]; }
279 @{ $form->{ALL_CHARTS} }
282 my %project_labels = ();
283 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
284 $project_labels{$item->{id}} = $item->{projectnumber};
290 foreach my $item (@{ $form->{ALL_CHARTS} }) {
291 if ( grep({ $_ eq 'AP_amount' } @{ $item->{link_split} }) ) {
292 if ( $taxchart_init eq '' ) {
293 $taxchart_init = $item->{tax_id};
296 push(@{ $form->{ALL_CHARTS_AP_amount} }, $item);
298 elsif ( grep({ $_ eq 'AP' } @{ $item->{link_split} }) ) {
299 push(@{ $form->{ALL_CHARTS_AP} }, $item);
301 elsif ( grep({ $_ eq 'AP_paid' } @{ $item->{link_split} }) ) {
302 push(@{ $form->{ALL_CHARTS_AP_paid} }, $item);
305 $charts{$item->{accno}} = $item;
309 foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
310 my $key = $item->{id} .'--'. $item->{rate};
312 if ( $taxchart_init eq $item->{id} ) {
313 $taxchart_init = $key;
316 $taxcharts{$item->{id}} = $item;
319 my $follow_up_vc = $form->{vendor};
320 $follow_up_vc =~ s/--.*?//;
321 my $follow_up_trans_info = "$form->{invnumber} ($follow_up_vc)";
323 $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
324 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
325 $form->{javascript} .= qq|<script type="text/javascript" src="js/follow_up.js"></script>|;
326 $form->{javascript} .= qq|<script type="text/javascript" src="js/kivi.Draft.js"></script>|;
330 for my $i (1 .. $form->{rowcount}) {
333 $form->{"amount_$i"} = $form->format_amount(\%myconfig, $form->{"amount_$i"}, 2);
334 $form->{"tax_$i"} = $form->format_amount(\%myconfig, $form->{"tax_$i"}, 2);
336 my $selected_accno_full;
337 my ($accno_row) = split(/--/, $form->{"AP_amount_$i"});
338 my $item = $charts{$accno_row};
339 $selected_accno_full = "$item->{accno}--$item->{tax_id}";
341 my $selected_taxchart = $form->{"taxchart_$i"};
342 my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full);
343 my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_AP_amount_$i"});
345 if ($previous_accno &&
346 ($previous_accno eq $selected_accno) &&
347 ($previous_tax_id ne $selected_tax_id)) {
348 my $item = $taxcharts{$selected_tax_id};
349 $selected_taxchart = "$item->{id}--$item->{rate}";
352 $selected_taxchart = $taxchart_init unless ($form->{"taxchart_$i"});
354 $form->{'selected_accno_full_'. $i} = $selected_accno_full;
356 $form->{'selected_taxchart_'. $i} = $selected_taxchart;
359 $form->{AP_amount_value_title_sub} = sub {
362 $item->{accno} .'--'. $item->{tax_id},
363 $item->{accno} .'--'. $item->{description},
367 $form->{taxchart_value_title_sub} = sub {
370 $item->{id} .'--'. $item->{rate},
371 $item->{taxdescription} .' '. ($item->{rate} * 100) .' %',
375 $form->{AP_paid_value_title_sub} = sub {
379 $item->{accno} .'--'. $item->{description}
383 $form->{APselected_value_title_sub} = sub {
387 $item->{accno} .'--'. $item->{description}
391 $form->{invtotal_unformatted} = $form->{invtotal};
392 $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
394 $form->{totalpaid} = 0;
398 if ( $form->{'paid_'. $form->{paidaccounts}} ) {
399 $form->{paidaccounts}++;
402 # default account for current assets (i.e. 1801 - SKR04)
403 $form->{accno_arap} = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
405 for my $i (1 .. $form->{paidaccounts}) {
406 $form->{totalpaid} += $form->{"paid_$i"};
409 if ($form->{"paid_$i"}) {
410 $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
412 if ($form->{"exchangerate_$i"} == 0) {
413 $form->{"exchangerate_$i"} = "";
415 $form->{"exchangerate_$i"} =
416 $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
420 if (SL::DB::Default->get->payments_changeable == 0) {
422 $changeable = ($form->{"acc_trans_id_$i"})? 0 : 1;
424 if (SL::DB::Default->get->payments_changeable == 2) {
426 $changeable = (($form->{"gldate_$i"} eq '') || $form->current_date(\%myconfig) eq $form->{"gldate_$i"});
429 $form->{'paidaccount_changeable_'. $i} = $changeable;
431 $form->{'labelpaid_project_id_'. $i} = $project_labels{$form->{'paid_project_id_'. $i}};
434 $form->{paid_missing} = $form->{invtotal_unformatted} - $form->{totalpaid};
436 print $form->parse_html_template('ap/form_header', {
437 today => DateTime->today,
440 $main::lxdebug->leave_sub();
444 $::lxdebug->enter_sub;
445 $::auth->assert('general_ledger');
450 my $follow_ups = FU->follow_ups('trans_id' => $::form->{id});
452 if (@{ $follow_ups }) {
453 $num_due = sum map { $_->{due} * 1 } @{ $follow_ups };
454 $num_follow_ups = scalar @{ $follow_ups }
458 my $transdate = $::form->datetonum($::form->{transdate}, \%::myconfig);
459 my $closedto = $::form->datetonum($::form->{closedto}, \%::myconfig);
461 my $storno = $::form->{id}
462 && !IS->has_storno(\%::myconfig, $::form, 'ap')
463 && !IS->is_storno( \%::myconfig, $::form, 'ap', $::form->{id})
464 && ($::form->{totalpaid} == 0 || $::form->{totalpaid} eq '');
467 print $::form->parse_html_template('ap/form_footer', {
469 num_follow_ups => $num_follow_ups,
470 show_post_draft => ($transdate > $closedto) && !$::form->{id},
471 show_storno => $storno,
474 $::lxdebug->leave_sub;
478 $main::lxdebug->enter_sub();
480 my $form = $main::form;
481 my %myconfig = %main::myconfig;
483 $main::auth->assert('general_ledger');
485 &mark_as_paid_common(\%myconfig,"ap");
487 $main::lxdebug->leave_sub();
491 $main::lxdebug->enter_sub();
493 my $form = $main::form;
494 my %myconfig = %main::myconfig;
496 $main::auth->assert('general_ledger');
500 $form->{invtotal} = 0;
502 delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
504 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
505 qw(exchangerate creditlimit creditremaining);
507 my @flds = qw(amount AP_amount projectnumber oldprojectnumber project_id taxchart);
509 my (@a, $j, $totaltax);
510 for my $i (1 .. $form->{rowcount}) {
511 $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
512 if ($form->{"amount_$i"}) {
515 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
517 # calculate tax exactly the same way as AP in post_transaction via form->calculate_tax
519 ($tmpnetamount,$form->{"tax_$i"}) = $form->calculate_tax($form->{"amount_$i"},$rate,$form->{taxincluded},2);
521 $totaltax += $form->{"tax_$i"};
522 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
526 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
528 map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
530 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
531 $form->{exchangerate} = $form->{forex} if $form->{forex};
533 $form->{invdate} = $form->{transdate};
534 my %saved_variables = map +( $_ => $form->{$_} ), qw(AP AP_amount_1 taxchart_1 notes);
536 my $vendor_changed = &check_name("vendor");
538 $form->{AP} = $saved_variables{AP};
539 if ($saved_variables{AP_amount_1} =~ m/.--./) {
540 map { $form->{$_} = $saved_variables{$_} } qw(AP_amount_1 taxchart_1);
542 delete $form->{taxchart_1};
545 $form->{rowcount} = $count + 1;
548 ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
551 for my $i (1 .. $form->{paidaccounts}) {
552 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
555 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
556 } qw(paid exchangerate);
558 $totalpaid += $form->{"paid_$i"};
560 $form->{"forex_$i"} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
561 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
565 $form->{creditremaining} -=
566 ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} -
567 $form->{oldinvtotal});
568 $form->{oldinvtotal} = $form->{invtotal};
569 $form->{oldtotalpaid} = $totalpaid;
573 $main::lxdebug->leave_sub();
578 $main::lxdebug->enter_sub();
580 my $form = $main::form;
581 my %myconfig = %main::myconfig;
582 my $locale = $main::locale;
584 $main::auth->assert('general_ledger');
585 $form->mtime_ischanged('ap');
587 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
589 my $invdate = $form->datetonum($form->{transdate}, \%myconfig);
591 for my $i (1 .. $form->{paidaccounts}) {
592 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
593 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
595 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
597 $form->error($locale->text('Cannot post payment for a closed period!'))
598 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
600 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
601 $form->{"exchangerate_$i"} = $form->{exchangerate}
602 if ($invdate == $datepaid);
603 $form->isblank("exchangerate_$i",
604 $locale->text('Exchangerate for payment missing!'));
609 ($form->{AP}) = split /--/, $form->{AP};
610 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
611 if (AP->post_payment(\%myconfig, \%$form)) {
612 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
613 $form->{what_done} = 'invoice';
614 $form->{addition} = "PAYMENT POSTED";
616 $form->redirect($locale->text('Payment posted!'))
618 $form->error($locale->text('Cannot post payment!'));
622 $main::lxdebug->leave_sub();
627 $main::lxdebug->enter_sub();
629 my $form = $main::form;
630 my %myconfig = %main::myconfig;
631 my $locale = $main::locale;
633 $main::auth->assert('general_ledger');
634 $form->mtime_ischanged('ap');
638 # check if there is a vendor, invoice, due date and invnumber
639 $form->isblank("transdate", $locale->text("Invoice Date missing!"));
640 $form->isblank("duedate", $locale->text("Due Date missing!"));
641 $form->isblank("vendor", $locale->text('Vendor missing!'));
642 $form->isblank("invnumber", $locale->text('Invoice Number missing!'));
644 if ($myconfig{mandatory_departments} && !$form->{department}) {
645 $form->{saved_message} = $::locale->text('You have to specify a department.');
650 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
651 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
653 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
654 if ($form->date_max_future($form->{"transdate"}, \%myconfig));
655 $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
657 my $zero_amount_posting = 1;
658 for my $i (1 .. $form->{rowcount}) {
659 if ($form->parse_amount(\%myconfig, $form->{"amount_$i"})) {
660 $zero_amount_posting = 0;
665 $form->error($locale->text('Zero amount posting!')) if $zero_amount_posting;
667 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
668 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
671 for my $i (1 .. $form->{paidaccounts}) {
672 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
673 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
675 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
677 $form->error($locale->text('Cannot post payment for a closed period!'))
678 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
680 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
681 $form->{"exchangerate_$i"} = $form->{exchangerate}
682 if ($transdate == $datepaid);
683 $form->isblank("exchangerate_$i",
684 $locale->text('Exchangerate for payment missing!'));
690 # if old vendor ne vendor redo form
691 my ($vendor) = split /--/, $form->{vendor};
692 if ($form->{oldvendor} ne "$vendor--$form->{vendor_id}") {
694 $::dispatcher->end_request;
696 my ($debitaccno, $debittaxkey) = split /--/, $form->{AP_amountselected};
697 my ($taxkey, $NULL) = split /--/, $form->{taxchartselected};
698 my ($payablesaccno, $payablestaxkey) = split /--/, $form->{APselected};
699 # $form->{AP_amount_1} = $debitaccno;
700 $form->{AP_payables} = $payablesaccno;
701 $form->{taxkey} = $taxkey;
704 $form->{id} = 0 if $form->{postasnew};
706 if (AP->post_transaction(\%myconfig, \%$form)) {
708 if(!exists $form->{addition} && $form->{id} ne "") {
709 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
710 $form->{addition} = "POSTED";
711 $form->{what_done} = "invoice";
714 # /saving the history
715 # Dieser Text wird niemals ausgegeben: Probleme beim redirect?
716 $form->redirect($locale->text('Transaction posted!')) unless $inline;
718 $form->error($locale->text('Cannot post transaction!'));
721 $main::lxdebug->leave_sub();
725 $main::lxdebug->enter_sub();
727 my $form = $main::form;
728 my %myconfig = %main::myconfig;
730 $main::auth->assert('general_ledger');
732 $form->{postasnew} = 1;
734 if(!exists $form->{addition} && $form->{id} ne "") {
735 # does this work? post_as_new for ap doesn't immediately save the
736 # invoice, because the invnumber has to be entered by hand.
737 # And the value of $form->{postasnew} isn't checked when calling post
738 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
739 $form->{addition} = "POSTED AS NEW";
740 $form->{what_done} = "invoice";
743 # /saving the history
746 $main::lxdebug->leave_sub();
750 $main::lxdebug->enter_sub();
752 my $form = $main::form;
753 my %myconfig = %main::myconfig;
755 $main::auth->assert('general_ledger');
757 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);
758 $form->{paidaccounts} = 1;
760 $form->{invdate} = $form->current_date(\%myconfig);
763 $main::lxdebug->leave_sub();
767 $main::lxdebug->enter_sub();
769 my $form = $main::form;
770 my $locale = $main::locale;
772 $main::auth->assert('general_ledger');
774 $form->{title} = $locale->text('Confirm!');
778 delete $form->{header};
781 <form method=post action=$form->{script}>
784 foreach my $key (keys %$form) {
785 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
786 $form->{$key} =~ s/\"/"/g;
787 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
791 <h2 class=confirm>$form->{title}</h2>
794 . $locale->text('Are you sure you want to delete Transaction')
795 . qq| $form->{invnumber}</h4>
797 <input name=action class=submit type=submit value="|
798 . $locale->text('Yes') . qq|">
802 $main::lxdebug->leave_sub();
806 $main::lxdebug->enter_sub();
808 my $form = $main::form;
809 my %myconfig = %main::myconfig;
810 my $locale = $main::locale;
812 $main::auth->assert('general_ledger');
814 if (AP->delete_transaction(\%myconfig, \%$form)) {
816 if(!exists $form->{addition}) {
817 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
818 $form->{addition} = "DELETED";
819 $form->{what_done} = "invoice";
822 # /saving the history
823 $form->redirect($locale->text('Transaction deleted!'));
825 $form->error($locale->text('Cannot delete transaction!'));
827 $main::lxdebug->leave_sub();
831 $main::lxdebug->enter_sub();
833 $main::auth->assert('vendor_invoice_edit');
835 my $form = $main::form;
836 my %myconfig = %main::myconfig;
837 my $locale = $main::locale;
839 # setup customer selection
840 $form->all_vc(\%myconfig, "vendor", "AP");
842 $form->{title} = $locale->text('AP Transactions');
844 $form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 },
845 "departments" => "ALL_DEPARTMENTS",
846 "vendors" => "ALL_VC");
848 # constants and subs for template
849 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
852 print $form->parse_html_template('ap/search', { %myconfig });
854 $main::lxdebug->leave_sub();
857 sub create_subtotal_row {
858 $main::lxdebug->enter_sub();
860 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
862 my $form = $main::form;
863 my %myconfig = %main::myconfig;
865 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
867 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
869 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
871 map { $totals->{$_} = 0 } @{ $subtotal_columns };
873 $main::lxdebug->leave_sub();
878 sub ap_transactions {
879 $main::lxdebug->enter_sub();
881 my $form = $main::form;
882 my %myconfig = %main::myconfig;
883 my $locale = $main::locale;
885 $main::auth->assert('vendor_invoice_edit');
887 ($form->{vendor}, $form->{vendor_id}) = split(/--/, $form->{vendor});
889 report_generator_set_default_sort('transdate', 1);
891 AP->ap_transactions(\%myconfig, \%$form);
893 $form->{title} = $locale->text('AP Transactions');
895 my $report = SL::ReportGenerator->new(\%myconfig, $form);
898 qw(transdate id type invnumber ordnumber name netamount tax amount paid datepaid
899 due duedate transaction_description notes employee globalprojectnumber
900 vendornumber country ustid taxzone payment_terms charts direct_debit);
902 my @hidden_variables = map { "l_${_}" } @columns;
903 push @hidden_variables, "l_subtotal", qw(open closed vendor invnumber ordnumber transaction_description notes project_id transdatefrom transdateto department
904 parts_partnumber parts_description);
906 my $href = build_std_url('action=ap_transactions', grep { $form->{$_} } @hidden_variables);
909 'transdate' => { 'text' => $locale->text('Date'), },
910 'id' => { 'text' => $locale->text('ID'), },
911 'type' => { 'text' => $locale->text('Type'), },
912 'invnumber' => { 'text' => $locale->text('Invoice'), },
913 'ordnumber' => { 'text' => $locale->text('Order'), },
914 'name' => { 'text' => $locale->text('Vendor'), },
915 'netamount' => { 'text' => $locale->text('Amount'), },
916 'tax' => { 'text' => $locale->text('Tax'), },
917 'amount' => { 'text' => $locale->text('Total'), },
918 'paid' => { 'text' => $locale->text('Paid'), },
919 'datepaid' => { 'text' => $locale->text('Date Paid'), },
920 'due' => { 'text' => $locale->text('Amount Due'), },
921 'duedate' => { 'text' => $locale->text('Due Date'), },
922 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
923 'notes' => { 'text' => $locale->text('Notes'), },
924 'employee' => { 'text' => $locale->text('Employee'), },
925 'globalprojectnumber' => { 'text' => $locale->text('Document Project Number'), },
926 'vendornumber' => { 'text' => $locale->text('Vendor Number'), },
927 'country' => { 'text' => $locale->text('Country'), },
928 'ustid' => { 'text' => $locale->text('USt-IdNr.'), },
929 'taxzone' => { 'text' => $locale->text('Steuersatz'), },
930 'payment_terms' => { 'text' => $locale->text('Payment Terms'), },
931 'charts' => { 'text' => $locale->text('Buchungskonto'), },
932 'direct_debit' => { 'text' => $locale->text('direct debit'), },
935 foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid employee shippingpoint shipvia transaction_description direct_debit)) {
936 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
937 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
940 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
942 $form->{"l_type"} = "Y";
943 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
945 $report->set_columns(%column_defs);
946 $report->set_column_order(@columns);
948 $report->set_export_options('ap_transactions', @hidden_variables, qw(sort sortdir));
950 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
953 push @options, $locale->text('Vendor') . " : $form->{vendor}" if ($form->{vendor});
954 push @options, $locale->text('Contact Person') . " : $form->{cp_name}" if ($form->{cp_name});
955 push @options, $locale->text('Department') . " : " . (split /--/, $form->{department})[0] if ($form->{department});
956 push @options, $locale->text('Invoice Number') . " : $form->{invnumber}" if ($form->{invnumber});
957 push @options, $locale->text('Order Number') . " : $form->{ordnumber}" if ($form->{ordnumber});
958 push @options, $locale->text('Notes') . " : $form->{notes}" if ($form->{notes});
959 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}" if ($form->{transaction_description});
960 push @options, $locale->text('Part Description') . " : $form->{parts_description}" if $form->{parts_description};
961 push @options, $locale->text('Part Number') . " : $form->{parts_partnumber}" if $form->{parts_partnumber};
962 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1) if ($form->{transdatefrom});
963 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1) if ($form->{transdateto});
964 push @options, $locale->text('Open') if ($form->{open});
965 push @options, $locale->text('Closed') if ($form->{closed});
967 $report->set_options('top_info_text' => join("\n", @options),
968 'raw_bottom_info_text' => $form->parse_html_template('ap/ap_transactions_bottom'),
969 'output_format' => 'HTML',
970 'title' => $form->{title},
971 'attachment_basename' => $locale->text('vendor_invoice_list') . strftime('_%Y%m%d', localtime time),
973 $report->set_options_from_form();
974 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
976 # add sort and escape callback, this one we use for the add sub
977 $form->{callback} = $href .= "&sort=$form->{sort}";
979 # escape callback for href
980 my $callback = $form->escape($href);
982 my @subtotal_columns = qw(netamount amount paid due);
984 my %totals = map { $_ => 0 } @subtotal_columns;
985 my %subtotals = map { $_ => 0 } @subtotal_columns;
989 foreach my $ap (@{ $form->{AP} }) {
990 $ap->{tax} = $ap->{amount} - $ap->{netamount};
991 $ap->{due} = $ap->{amount} - $ap->{paid};
993 map { $subtotals{$_} += $ap->{$_};
994 $totals{$_} += $ap->{$_} } @subtotal_columns;
996 map { $ap->{$_} = $form->format_amount(\%myconfig, $ap->{$_}, 2) } qw(netamount tax amount paid due);
998 my $is_storno = $ap->{storno} && $ap->{storno_id};
999 my $has_storno = $ap->{storno} && !$ap->{storno_id};
1001 if ($ap->{invoice}) {
1003 $has_storno ? $locale->text("Invoice with Storno (abbreviation)")
1004 : $is_storno ? $locale->text("Storno (one letter abbreviation)")
1005 : $locale->text("Invoice (one letter abbreviation)");
1008 $has_storno ? $locale->text("AP Transaction with Storno (abbreviation)")
1009 : $is_storno ? $locale->text("AP Transaction Storno (one letter abbreviation)")
1010 : $locale->text("AP Transaction (abbreviation)");
1013 $ap->{direct_debit} = $ap->{direct_debit} ? $::locale->text('yes') : $::locale->text('no');
1017 foreach my $column (@columns) {
1019 'data' => $ap->{$column},
1020 'align' => $column_alignment{$column},
1024 $row->{invnumber}->{link} = build_std_url("script=" . ($ap->{invoice} ? 'ir.pl' : 'ap.pl'), 'action=edit')
1025 . "&id=" . E($ap->{id}) . "&callback=${callback}";
1027 my $row_set = [ $row ];
1029 if (($form->{l_subtotal} eq 'Y')
1030 && (($idx == (scalar @{ $form->{AP} } - 1))
1031 || ($ap->{ $form->{sort} } ne $form->{AP}->[$idx + 1]->{ $form->{sort} }))) {
1032 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1035 $report->add_data($row_set);
1040 $report->add_separator();
1041 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1043 $report->generate_with_headers();
1045 $main::lxdebug->leave_sub();
1049 $main::lxdebug->enter_sub();
1051 my $form = $main::form;
1052 my %myconfig = %main::myconfig;
1053 my $locale = $main::locale;
1055 $main::auth->assert('general_ledger');
1057 if (IS->has_storno(\%myconfig, $form, 'ap')) {
1058 $form->{title} = $locale->text("Cancel Accounts Payables Transaction");
1059 $form->error($locale->text("Transaction has already been cancelled!"));
1062 $form->error($locale->text('Cannot post storno for a closed period!'))
1063 if ( $form->date_closed($form->{transdate}, \%myconfig));
1065 AP->storno($form, \%myconfig, $form->{id});
1067 # saving the history
1068 if(!exists $form->{addition} && $form->{id} ne "") {
1069 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1070 $form->{addition} = "STORNO";
1071 $form->{what_done} = "invoice";
1072 $form->save_history;
1074 # /saving the history
1076 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1078 $main::lxdebug->leave_sub();