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 #deaktivieren von gebuchten Zahlungen ausserhalb der Bücherkontrolle, vorher prüfen ob heute eingegeben
430 if ($form->date_closed($form->{"gldate_$i"})) {
434 $form->{'paidaccount_changeable_'. $i} = $changeable;
436 $form->{'labelpaid_project_id_'. $i} = $project_labels{$form->{'paid_project_id_'. $i}};
439 $form->{paid_missing} = $form->{invtotal_unformatted} - $form->{totalpaid};
441 print $form->parse_html_template('ap/form_header', {
442 today => DateTime->today,
445 $main::lxdebug->leave_sub();
449 $::lxdebug->enter_sub;
450 $::auth->assert('general_ledger');
455 my $follow_ups = FU->follow_ups('trans_id' => $::form->{id});
457 if (@{ $follow_ups }) {
458 $num_due = sum map { $_->{due} * 1 } @{ $follow_ups };
459 $num_follow_ups = scalar @{ $follow_ups }
463 my $transdate = $::form->datetonum($::form->{transdate}, \%::myconfig);
464 my $closedto = $::form->datetonum($::form->{closedto}, \%::myconfig);
466 my $storno = $::form->{id}
467 && !IS->has_storno(\%::myconfig, $::form, 'ap')
468 && !IS->is_storno( \%::myconfig, $::form, 'ap', $::form->{id})
469 && ($::form->{totalpaid} == 0 || $::form->{totalpaid} eq '');
472 print $::form->parse_html_template('ap/form_footer', {
474 num_follow_ups => $num_follow_ups,
475 show_post_draft => ($transdate > $closedto) && !$::form->{id},
476 show_storno => $storno,
479 $::lxdebug->leave_sub;
483 $main::lxdebug->enter_sub();
485 my $form = $main::form;
486 my %myconfig = %main::myconfig;
488 $main::auth->assert('general_ledger');
490 &mark_as_paid_common(\%myconfig,"ap");
492 $main::lxdebug->leave_sub();
496 $main::lxdebug->enter_sub();
498 my $form = $main::form;
499 my %myconfig = %main::myconfig;
501 $main::auth->assert('general_ledger');
505 $form->{invtotal} = 0;
507 delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
509 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
510 qw(exchangerate creditlimit creditremaining);
512 my @flds = qw(amount AP_amount projectnumber oldprojectnumber project_id taxchart);
514 my (@a, $j, $totaltax);
515 for my $i (1 .. $form->{rowcount}) {
516 $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
517 if ($form->{"amount_$i"}) {
520 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
522 # calculate tax exactly the same way as AP in post_transaction via form->calculate_tax
524 ($tmpnetamount,$form->{"tax_$i"}) = $form->calculate_tax($form->{"amount_$i"},$rate,$form->{taxincluded},2);
526 $totaltax += $form->{"tax_$i"};
527 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
531 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
533 map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
535 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
536 $form->{exchangerate} = $form->{forex} if $form->{forex};
538 $form->{invdate} = $form->{transdate};
539 my %saved_variables = map +( $_ => $form->{$_} ), qw(AP AP_amount_1 taxchart_1 notes);
541 my $vendor_changed = &check_name("vendor");
543 $form->{AP} = $saved_variables{AP};
544 if ($saved_variables{AP_amount_1} =~ m/.--./) {
545 map { $form->{$_} = $saved_variables{$_} } qw(AP_amount_1 taxchart_1);
547 delete $form->{taxchart_1};
550 $form->{rowcount} = $count + 1;
553 ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
556 for my $i (1 .. $form->{paidaccounts}) {
557 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
560 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
561 } qw(paid exchangerate);
563 $totalpaid += $form->{"paid_$i"};
565 $form->{"forex_$i"} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
566 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
570 $form->{creditremaining} -=
571 ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} -
572 $form->{oldinvtotal});
573 $form->{oldinvtotal} = $form->{invtotal};
574 $form->{oldtotalpaid} = $totalpaid;
578 $main::lxdebug->leave_sub();
583 $main::lxdebug->enter_sub();
585 my $form = $main::form;
586 my %myconfig = %main::myconfig;
587 my $locale = $main::locale;
589 $main::auth->assert('general_ledger');
590 $form->mtime_ischanged('ap');
592 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
594 my $invdate = $form->datetonum($form->{transdate}, \%myconfig);
596 for my $i (1 .. $form->{paidaccounts}) {
597 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
598 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
600 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
602 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
603 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
605 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
606 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
607 $form->error($locale->text('Cannot post payment for a closed period!'))
608 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
610 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
611 $form->{"exchangerate_$i"} = $form->{exchangerate}
612 if ($invdate == $datepaid);
613 $form->isblank("exchangerate_$i",
614 $locale->text('Exchangerate for payment missing!'));
619 ($form->{AP}) = split /--/, $form->{AP};
620 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
621 if (AP->post_payment(\%myconfig, \%$form)) {
622 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
623 $form->{what_done} = 'invoice';
624 $form->{addition} = "PAYMENT POSTED";
626 $form->redirect($locale->text('Payment posted!'))
628 $form->error($locale->text('Cannot post payment!'));
632 $main::lxdebug->leave_sub();
637 $main::lxdebug->enter_sub();
639 my $form = $main::form;
640 my %myconfig = %main::myconfig;
641 my $locale = $main::locale;
643 $main::auth->assert('general_ledger');
644 $form->mtime_ischanged('ap');
648 # check if there is a vendor, invoice, due date and invnumber
649 $form->isblank("transdate", $locale->text("Invoice Date missing!"));
650 $form->isblank("duedate", $locale->text("Due Date missing!"));
651 $form->isblank("vendor", $locale->text('Vendor missing!'));
652 $form->isblank("invnumber", $locale->text('Invoice Number missing!'));
654 if ($myconfig{mandatory_departments} && !$form->{department}) {
655 $form->{saved_message} = $::locale->text('You have to specify a department.');
660 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
661 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
663 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
664 if ($form->date_max_future($form->{"transdate"}, \%myconfig));
665 $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
667 my $zero_amount_posting = 1;
668 for my $i (1 .. $form->{rowcount}) {
669 if ($form->parse_amount(\%myconfig, $form->{"amount_$i"})) {
670 $zero_amount_posting = 0;
675 $form->error($locale->text('Zero amount posting!')) if $zero_amount_posting;
677 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
678 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
681 for my $i (1 .. $form->{paidaccounts}) {
682 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
683 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
685 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
687 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
688 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
690 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
691 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
692 $form->error($locale->text('Cannot post payment for a closed period!'))
693 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
695 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
696 $form->{"exchangerate_$i"} = $form->{exchangerate}
697 if ($transdate == $datepaid);
698 $form->isblank("exchangerate_$i",
699 $locale->text('Exchangerate for payment missing!'));
705 # if old vendor ne vendor redo form
706 my ($vendor) = split /--/, $form->{vendor};
707 if ($form->{oldvendor} ne "$vendor--$form->{vendor_id}") {
709 $::dispatcher->end_request;
711 my ($debitaccno, $debittaxkey) = split /--/, $form->{AP_amountselected};
712 my ($taxkey, $NULL) = split /--/, $form->{taxchartselected};
713 my ($payablesaccno, $payablestaxkey) = split /--/, $form->{APselected};
714 # $form->{AP_amount_1} = $debitaccno;
715 $form->{AP_payables} = $payablesaccno;
716 $form->{taxkey} = $taxkey;
719 $form->{id} = 0 if $form->{postasnew};
721 if (AP->post_transaction(\%myconfig, \%$form)) {
723 if(!exists $form->{addition} && $form->{id} ne "") {
724 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
725 $form->{addition} = "POSTED";
726 $form->{what_done} = "invoice";
729 # /saving the history
730 # Dieser Text wird niemals ausgegeben: Probleme beim redirect?
731 $form->redirect($locale->text('Transaction posted!')) unless $inline;
733 $form->error($locale->text('Cannot post transaction!'));
736 $main::lxdebug->leave_sub();
740 $main::lxdebug->enter_sub();
742 my $form = $main::form;
743 my %myconfig = %main::myconfig;
745 $main::auth->assert('general_ledger');
747 $form->{postasnew} = 1;
749 if(!exists $form->{addition} && $form->{id} ne "") {
750 # does this work? post_as_new for ap doesn't immediately save the
751 # invoice, because the invnumber has to be entered by hand.
752 # And the value of $form->{postasnew} isn't checked when calling post
753 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
754 $form->{addition} = "POSTED AS NEW";
755 $form->{what_done} = "invoice";
758 # /saving the history
761 $main::lxdebug->leave_sub();
765 $main::lxdebug->enter_sub();
767 my $form = $main::form;
768 my %myconfig = %main::myconfig;
770 $main::auth->assert('general_ledger');
772 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);
773 $form->{paidaccounts} = 1;
775 $form->{invdate} = $form->current_date(\%myconfig);
778 $main::lxdebug->leave_sub();
782 $main::lxdebug->enter_sub();
784 my $form = $main::form;
785 my $locale = $main::locale;
787 $main::auth->assert('general_ledger');
789 $form->{title} = $locale->text('Confirm!');
793 delete $form->{header};
796 <form method=post action=$form->{script}>
799 foreach my $key (keys %$form) {
800 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
801 $form->{$key} =~ s/\"/"/g;
802 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
806 <h2 class=confirm>$form->{title}</h2>
809 . $locale->text('Are you sure you want to delete Transaction')
810 . qq| $form->{invnumber}</h4>
812 <input name=action class=submit type=submit value="|
813 . $locale->text('Yes') . qq|">
817 $main::lxdebug->leave_sub();
821 $main::lxdebug->enter_sub();
823 my $form = $main::form;
824 my %myconfig = %main::myconfig;
825 my $locale = $main::locale;
827 $main::auth->assert('general_ledger');
829 if (AP->delete_transaction(\%myconfig, \%$form)) {
831 if(!exists $form->{addition}) {
832 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
833 $form->{addition} = "DELETED";
834 $form->{what_done} = "invoice";
837 # /saving the history
838 $form->redirect($locale->text('Transaction deleted!'));
840 $form->error($locale->text('Cannot delete transaction!'));
842 $main::lxdebug->leave_sub();
846 $main::lxdebug->enter_sub();
848 $main::auth->assert('vendor_invoice_edit');
850 my $form = $main::form;
851 my %myconfig = %main::myconfig;
852 my $locale = $main::locale;
854 # setup customer selection
855 $form->all_vc(\%myconfig, "vendor", "AP");
857 $form->{title} = $locale->text('AP Transactions');
859 $form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 },
860 "departments" => "ALL_DEPARTMENTS",
861 "vendors" => "ALL_VC");
863 # constants and subs for template
864 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
867 print $form->parse_html_template('ap/search', { %myconfig });
869 $main::lxdebug->leave_sub();
872 sub create_subtotal_row {
873 $main::lxdebug->enter_sub();
875 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
877 my $form = $main::form;
878 my %myconfig = %main::myconfig;
880 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
882 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
884 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
886 map { $totals->{$_} = 0 } @{ $subtotal_columns };
888 $main::lxdebug->leave_sub();
893 sub ap_transactions {
894 $main::lxdebug->enter_sub();
896 my $form = $main::form;
897 my %myconfig = %main::myconfig;
898 my $locale = $main::locale;
900 $main::auth->assert('vendor_invoice_edit');
902 ($form->{vendor}, $form->{vendor_id}) = split(/--/, $form->{vendor});
904 report_generator_set_default_sort('transdate', 1);
906 AP->ap_transactions(\%myconfig, \%$form);
908 $form->{title} = $locale->text('AP Transactions');
910 my $report = SL::ReportGenerator->new(\%myconfig, $form);
913 qw(transdate id type invnumber ordnumber name netamount tax amount paid datepaid
914 due duedate transaction_description notes employee globalprojectnumber
915 vendornumber country ustid taxzone payment_terms charts direct_debit);
917 my @hidden_variables = map { "l_${_}" } @columns;
918 push @hidden_variables, "l_subtotal", qw(open closed vendor invnumber ordnumber transaction_description notes project_id transdatefrom transdateto department
919 parts_partnumber parts_description);
921 my $href = build_std_url('action=ap_transactions', grep { $form->{$_} } @hidden_variables);
924 'transdate' => { 'text' => $locale->text('Date'), },
925 'id' => { 'text' => $locale->text('ID'), },
926 'type' => { 'text' => $locale->text('Type'), },
927 'invnumber' => { 'text' => $locale->text('Invoice'), },
928 'ordnumber' => { 'text' => $locale->text('Order'), },
929 'name' => { 'text' => $locale->text('Vendor'), },
930 'netamount' => { 'text' => $locale->text('Amount'), },
931 'tax' => { 'text' => $locale->text('Tax'), },
932 'amount' => { 'text' => $locale->text('Total'), },
933 'paid' => { 'text' => $locale->text('Paid'), },
934 'datepaid' => { 'text' => $locale->text('Date Paid'), },
935 'due' => { 'text' => $locale->text('Amount Due'), },
936 'duedate' => { 'text' => $locale->text('Due Date'), },
937 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
938 'notes' => { 'text' => $locale->text('Notes'), },
939 'employee' => { 'text' => $locale->text('Employee'), },
940 'globalprojectnumber' => { 'text' => $locale->text('Document Project Number'), },
941 'vendornumber' => { 'text' => $locale->text('Vendor Number'), },
942 'country' => { 'text' => $locale->text('Country'), },
943 'ustid' => { 'text' => $locale->text('USt-IdNr.'), },
944 'taxzone' => { 'text' => $locale->text('Tax rate'), },
945 'payment_terms' => { 'text' => $locale->text('Payment Terms'), },
946 'charts' => { 'text' => $locale->text('Buchungskonto'), },
947 'direct_debit' => { 'text' => $locale->text('direct debit'), },
950 foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid employee shippingpoint shipvia transaction_description direct_debit)) {
951 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
952 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
955 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
957 $form->{"l_type"} = "Y";
958 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
960 $report->set_columns(%column_defs);
961 $report->set_column_order(@columns);
963 $report->set_export_options('ap_transactions', @hidden_variables, qw(sort sortdir));
965 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
968 push @options, $locale->text('Vendor') . " : $form->{vendor}" if ($form->{vendor});
969 push @options, $locale->text('Contact Person') . " : $form->{cp_name}" if ($form->{cp_name});
970 push @options, $locale->text('Department') . " : " . (split /--/, $form->{department})[0] if ($form->{department});
971 push @options, $locale->text('Invoice Number') . " : $form->{invnumber}" if ($form->{invnumber});
972 push @options, $locale->text('Order Number') . " : $form->{ordnumber}" if ($form->{ordnumber});
973 push @options, $locale->text('Notes') . " : $form->{notes}" if ($form->{notes});
974 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}" if ($form->{transaction_description});
975 push @options, $locale->text('Part Description') . " : $form->{parts_description}" if $form->{parts_description};
976 push @options, $locale->text('Part Number') . " : $form->{parts_partnumber}" if $form->{parts_partnumber};
977 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1) if ($form->{transdatefrom});
978 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1) if ($form->{transdateto});
979 push @options, $locale->text('Open') if ($form->{open});
980 push @options, $locale->text('Closed') if ($form->{closed});
982 $report->set_options('top_info_text' => join("\n", @options),
983 'raw_bottom_info_text' => $form->parse_html_template('ap/ap_transactions_bottom'),
984 'output_format' => 'HTML',
985 'title' => $form->{title},
986 'attachment_basename' => $locale->text('vendor_invoice_list') . strftime('_%Y%m%d', localtime time),
988 $report->set_options_from_form();
989 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
991 # add sort and escape callback, this one we use for the add sub
992 $form->{callback} = $href .= "&sort=$form->{sort}";
994 # escape callback for href
995 my $callback = $form->escape($href);
997 my @subtotal_columns = qw(netamount amount paid due);
999 my %totals = map { $_ => 0 } @subtotal_columns;
1000 my %subtotals = map { $_ => 0 } @subtotal_columns;
1004 foreach my $ap (@{ $form->{AP} }) {
1005 $ap->{tax} = $ap->{amount} - $ap->{netamount};
1006 $ap->{due} = $ap->{amount} - $ap->{paid};
1008 map { $subtotals{$_} += $ap->{$_};
1009 $totals{$_} += $ap->{$_} } @subtotal_columns;
1011 map { $ap->{$_} = $form->format_amount(\%myconfig, $ap->{$_}, 2) } qw(netamount tax amount paid due);
1013 my $is_storno = $ap->{storno} && $ap->{storno_id};
1014 my $has_storno = $ap->{storno} && !$ap->{storno_id};
1016 if ($ap->{invoice}) {
1018 $has_storno ? $locale->text("Invoice with Storno (abbreviation)")
1019 : $is_storno ? $locale->text("Storno (one letter abbreviation)")
1020 : $locale->text("Invoice (one letter abbreviation)");
1023 $has_storno ? $locale->text("AP Transaction with Storno (abbreviation)")
1024 : $is_storno ? $locale->text("AP Transaction Storno (one letter abbreviation)")
1025 : $locale->text("AP Transaction (abbreviation)");
1028 $ap->{direct_debit} = $ap->{direct_debit} ? $::locale->text('yes') : $::locale->text('no');
1032 foreach my $column (@columns) {
1034 'data' => $ap->{$column},
1035 'align' => $column_alignment{$column},
1039 $row->{invnumber}->{link} = build_std_url("script=" . ($ap->{invoice} ? 'ir.pl' : 'ap.pl'), 'action=edit')
1040 . "&id=" . E($ap->{id}) . "&callback=${callback}";
1042 my $row_set = [ $row ];
1044 if (($form->{l_subtotal} eq 'Y')
1045 && (($idx == (scalar @{ $form->{AP} } - 1))
1046 || ($ap->{ $form->{sort} } ne $form->{AP}->[$idx + 1]->{ $form->{sort} }))) {
1047 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1050 $report->add_data($row_set);
1055 $report->add_separator();
1056 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1058 $report->generate_with_headers();
1060 $main::lxdebug->leave_sub();
1064 $main::lxdebug->enter_sub();
1066 my $form = $main::form;
1067 my %myconfig = %main::myconfig;
1068 my $locale = $main::locale;
1070 $main::auth->assert('general_ledger');
1072 if (IS->has_storno(\%myconfig, $form, 'ap')) {
1073 $form->{title} = $locale->text("Cancel Accounts Payables Transaction");
1074 $form->error($locale->text("Transaction has already been cancelled!"));
1077 $form->error($locale->text('Cannot post storno for a closed period!'))
1078 if ( $form->date_closed($form->{transdate}, \%myconfig));
1080 AP->storno($form, \%myconfig, $form->{id});
1082 # saving the history
1083 if(!exists $form->{addition} && $form->{id} ne "") {
1084 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1085 $form->{addition} = "STORNO";
1086 $form->{what_done} = "invoice";
1087 $form->save_history;
1089 # /saving the history
1091 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1093 $main::lxdebug->leave_sub();