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('ap_transactions');
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('ap_transactions');
113 $form->{title} = "Edit";
118 $main::lxdebug->leave_sub();
122 $main::lxdebug->enter_sub();
124 my $form = $main::form;
126 $main::auth->assert('ap_transactions');
131 $main::lxdebug->leave_sub();
135 $main::lxdebug->enter_sub();
139 my $form = $main::form;
140 my %myconfig = %main::myconfig;
142 $main::auth->assert('ap_transactions');
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};
149 $saved{currency} = $form->{currency} if $form->{currency};
150 $saved{taxincluded} = $form->{taxincluded} if $form->{taxincluded};
153 IR->get_vendor(\%myconfig, \%$form);
155 $form->{$_} = $saved{$_} for keys %saved;
156 $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
157 $form->{rowcount} = 1;
159 # build the popup menus
160 $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
163 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
165 $form->{selectcurrency} = "";
166 map { my $quoted = H($_); $form->{selectcurrency} .= "<option value=\"${quoted}\">${quoted}\n" } $form->get_all_currencies(\%myconfig);
169 if (@{ $form->{all_vendor} || [] }) {
170 $form->{vendor} = qq|$form->{vendor}--$form->{vendor_id}|;
171 map { my $quoted = H($_->{name} . "--" . $_->{id}); $form->{selectvendor} .= "<option value=\"${quoted}\">${quoted}\n" }
172 (@{ $form->{all_vendor} });
176 if (@{ $form->{all_departments} || [] }) {
177 $form->{department} = "$form->{department}--$form->{department_id}";
178 $form->{selectdepartment} = "<option>\n" . join('', map { my $quoted = H("$_->{description}--$_->{id}"); "<option value=\"${quoted}\">${quoted}\n"} @{ $form->{all_departments} || [] });
181 $form->{employee} = "$form->{employee}--$form->{employee_id}";
183 AP->setup_form($form);
186 ($form->datetonum($form->{transdate}, \%myconfig) <=
187 $form->datetonum($form->{closedto}, \%myconfig));
189 $main::lxdebug->leave_sub();
193 my @fields = qw(acc_trans_id gldate datepaid source memo paid AP_paid paid_project_id);
195 grep { $_->{paid} != 0 }
198 +{ map { ($_ => delete($::form->{"${_}_${idx}"})) } @fields }
199 } (1..$::form->{paidaccounts});
201 @payments = sort_by { DateTime->from_kivitendo($_->{datepaid}) } @payments;
203 $::form->{paidaccounts} = max scalar(@payments), 1;
205 foreach my $idx (1 .. scalar(@payments)) {
206 my $payment = $payments[$idx - 1];
207 $::form->{"${_}_${idx}"} = $payment->{$_} for @fields;
212 $main::lxdebug->enter_sub();
214 my $form = $main::form;
215 my %myconfig = %main::myconfig;
216 my $locale = $main::locale;
217 my $cgi = $::request->{cgi};
219 $main::auth->assert('ap_transactions');
221 $::form->{invoice_obj} = SL::DB::PurchaseInvoice->new(id => $::form->{id})->load if $::form->{id};
223 $form->{title_} = $form->{title};
224 $form->{title} = $form->{title} eq 'Add' ? $locale->text('Add Accounts Payables Transaction') : $locale->text('Edit Accounts Payables Transaction');
226 # type=submit $locale->text('Add Accounts Payables Transaction')
227 # type=submit $locale->text('Edit Accounts Payables Transaction')
229 # set option selected
230 foreach my $item (qw(vendor currency department)) {
231 my $to_replace = H($form->{$item});
232 $form->{"select$item"} =~ s/ selected//;
233 $form->{"select$item"} =~ s/>\Q${to_replace}\E/ selected>${to_replace}/;
235 my $readonly = $form->{id} ? "readonly" : "";
237 $form->{radier} = ($::instance_conf->get_ap_changeable == 2)
238 ? ($form->current_date(\%myconfig) eq $form->{gldate})
239 : ($::instance_conf->get_ap_changeable == 1);
240 $readonly = $form->{radier} ? "" : $readonly;
242 $form->{readonly} = $readonly;
244 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
245 if ( $form->{forex} ) {
246 $form->{exchangerate} = $form->{forex};
250 $form->{exchangerate} = $form->{exchangerate} ? $form->format_amount(\%myconfig, $form->{exchangerate}) : '';
251 $form->{creditlimit} = $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
252 $form->{creditremaining} = $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
255 if (($rows = $form->numtextrows($form->{notes}, 50)) < 2) {
258 $form->{textarea_rows} = $rows;
260 $form->{creditremaining_plus} = ($form->{creditremaining} =~ /-/) ? "0" : "1";
262 my @old_project_ids = ();
265 if ($form->{"project_id_$_"}) {
266 push(@old_project_ids, $form->{"project_id_$_"});
269 (1..$form->{"rowcount"})
272 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
274 "old_id" => \@old_project_ids },
275 "charts" => { "key" => "ALL_CHARTS",
276 "transdate" => $form->{transdate} },
277 "taxcharts" => { "key" => "ALL_TAXCHARTS",
278 "module" => "AP" },);
281 { $_->{link_split} = [ split(/:/, $_->{link}) ]; }
282 @{ $form->{ALL_CHARTS} }
285 my %project_labels = ();
286 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
287 $project_labels{$item->{id}} = $item->{projectnumber};
293 foreach my $item (@{ $form->{ALL_CHARTS} }) {
294 if ( grep({ $_ eq 'AP_amount' } @{ $item->{link_split} }) ) {
295 if ( $taxchart_init eq '' ) {
296 $taxchart_init = $item->{tax_id};
299 push(@{ $form->{ALL_CHARTS_AP_amount} }, $item);
301 elsif ( grep({ $_ eq 'AP' } @{ $item->{link_split} }) ) {
302 push(@{ $form->{ALL_CHARTS_AP} }, $item);
304 elsif ( grep({ $_ eq 'AP_paid' } @{ $item->{link_split} }) ) {
305 push(@{ $form->{ALL_CHARTS_AP_paid} }, $item);
308 $charts{$item->{accno}} = $item;
312 foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
313 my $key = $item->{id} .'--'. $item->{rate};
315 if ( $taxchart_init eq $item->{id} ) {
316 $taxchart_init = $key;
319 $taxcharts{$item->{id}} = $item;
322 my $follow_up_vc = $form->{vendor};
323 $follow_up_vc =~ s/--.*?//;
324 my $follow_up_trans_info = "$form->{invnumber} ($follow_up_vc)";
326 $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
327 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
328 $form->{javascript} .= qq|<script type="text/javascript" src="js/follow_up.js"></script>|;
329 $form->{javascript} .= qq|<script type="text/javascript" src="js/kivi.Draft.js"></script>|;
333 for my $i (1 .. $form->{rowcount}) {
336 $form->{"amount_$i"} = $form->format_amount(\%myconfig, $form->{"amount_$i"}, 2);
337 $form->{"tax_$i"} = $form->format_amount(\%myconfig, $form->{"tax_$i"}, 2);
339 my $selected_accno_full;
340 my ($accno_row) = split(/--/, $form->{"AP_amount_$i"});
341 my $item = $charts{$accno_row};
342 $selected_accno_full = "$item->{accno}--$item->{tax_id}";
344 my $selected_taxchart = $form->{"taxchart_$i"};
345 my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full);
346 my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_AP_amount_$i"});
348 if ($previous_accno &&
349 ($previous_accno eq $selected_accno) &&
350 ($previous_tax_id ne $selected_tax_id)) {
351 my $item = $taxcharts{$selected_tax_id};
352 $selected_taxchart = "$item->{id}--$item->{rate}";
355 $selected_taxchart = $taxchart_init unless ($form->{"taxchart_$i"});
357 $form->{'selected_accno_full_'. $i} = $selected_accno_full;
359 $form->{'selected_taxchart_'. $i} = $selected_taxchart;
362 $form->{AP_amount_value_title_sub} = sub {
365 $item->{accno} .'--'. $item->{tax_id},
366 $item->{accno} .'--'. $item->{description},
370 $form->{taxchart_value_title_sub} = sub {
373 $item->{id} .'--'. $item->{rate},
374 $item->{taxdescription} .' '. ($item->{rate} * 100) .' %',
378 $form->{AP_paid_value_title_sub} = sub {
382 $item->{accno} .'--'. $item->{description}
386 $form->{APselected_value_title_sub} = sub {
390 $item->{accno} .'--'. $item->{description}
394 $form->{invtotal_unformatted} = $form->{invtotal};
395 $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
397 $form->{totalpaid} = 0;
401 if ( $form->{'paid_'. $form->{paidaccounts}} ) {
402 $form->{paidaccounts}++;
405 # default account for current assets (i.e. 1801 - SKR04)
406 $form->{accno_arap} = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
408 for my $i (1 .. $form->{paidaccounts}) {
409 $form->{totalpaid} += $form->{"paid_$i"};
412 if ($form->{"paid_$i"}) {
413 $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
415 if ($form->{"exchangerate_$i"} == 0) {
416 $form->{"exchangerate_$i"} = "";
418 $form->{"exchangerate_$i"} =
419 $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
423 if (SL::DB::Default->get->payments_changeable == 0) {
425 $changeable = ($form->{"acc_trans_id_$i"})? 0 : 1;
427 if (SL::DB::Default->get->payments_changeable == 2) {
429 $changeable = (($form->{"gldate_$i"} eq '') || $form->current_date(\%myconfig) eq $form->{"gldate_$i"});
432 #deaktivieren von gebuchten Zahlungen ausserhalb der Bücherkontrolle, vorher prüfen ob heute eingegeben
433 if ($form->date_closed($form->{"gldate_$i"})) {
437 $form->{'paidaccount_changeable_'. $i} = $changeable;
439 $form->{'labelpaid_project_id_'. $i} = $project_labels{$form->{'paid_project_id_'. $i}};
442 $form->{paid_missing} = $form->{invtotal_unformatted} - $form->{totalpaid};
444 print $form->parse_html_template('ap/form_header', {
445 today => DateTime->today,
448 $main::lxdebug->leave_sub();
452 $::lxdebug->enter_sub;
453 $::auth->assert('ap_transactions');
458 my $follow_ups = FU->follow_ups('trans_id' => $::form->{id}, 'not_done' => 1);
460 if (@{ $follow_ups }) {
461 $num_due = sum map { $_->{due} * 1 } @{ $follow_ups };
462 $num_follow_ups = scalar @{ $follow_ups }
466 my $transdate = $::form->datetonum($::form->{transdate}, \%::myconfig);
467 my $closedto = $::form->datetonum($::form->{closedto}, \%::myconfig);
469 my $storno = $::form->{id}
470 && !IS->has_storno(\%::myconfig, $::form, 'ap')
471 && !IS->is_storno( \%::myconfig, $::form, 'ap', $::form->{id})
472 && ($::form->{totalpaid} == 0 || $::form->{totalpaid} eq '');
475 print $::form->parse_html_template('ap/form_footer', {
477 num_follow_ups => $num_follow_ups,
478 show_post_draft => ($transdate > $closedto) && !$::form->{id},
479 show_storno => $storno,
482 $::lxdebug->leave_sub;
486 $main::lxdebug->enter_sub();
488 my $form = $main::form;
489 my %myconfig = %main::myconfig;
491 $main::auth->assert('ap_transactions');
493 &mark_as_paid_common(\%myconfig,"ap");
495 $main::lxdebug->leave_sub();
499 $main::lxdebug->enter_sub();
501 my $form = $main::form;
502 my %myconfig = %main::myconfig;
504 $main::auth->assert('ap_transactions');
508 $form->{invtotal} = 0;
510 delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
512 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
513 qw(exchangerate creditlimit creditremaining);
515 my @flds = qw(amount AP_amount projectnumber oldprojectnumber project_id taxchart);
517 my (@a, $j, $totaltax);
518 for my $i (1 .. $form->{rowcount}) {
519 $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
520 if ($form->{"amount_$i"}) {
523 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
525 # calculate tax exactly the same way as AP in post_transaction via form->calculate_tax
527 ($tmpnetamount,$form->{"tax_$i"}) = $form->calculate_tax($form->{"amount_$i"},$rate,$form->{taxincluded},2);
529 $totaltax += $form->{"tax_$i"};
530 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
534 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
536 map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
538 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
539 $form->{exchangerate} = $form->{forex} if $form->{forex};
541 $form->{invdate} = $form->{transdate};
542 my %saved_variables = map +( $_ => $form->{$_} ), qw(AP AP_amount_1 taxchart_1 notes);
544 my $vendor_changed = &check_name("vendor");
546 $form->{AP} = $saved_variables{AP};
547 if ($saved_variables{AP_amount_1} =~ m/.--./) {
548 map { $form->{$_} = $saved_variables{$_} } qw(AP_amount_1 taxchart_1);
550 delete $form->{taxchart_1};
553 $form->{rowcount} = $count + 1;
556 ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
559 for my $i (1 .. $form->{paidaccounts}) {
560 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
563 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
564 } qw(paid exchangerate);
566 $totalpaid += $form->{"paid_$i"};
568 $form->{"forex_$i"} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
569 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
573 $form->{creditremaining} -=
574 ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} -
575 $form->{oldinvtotal});
576 $form->{oldinvtotal} = $form->{invtotal};
577 $form->{oldtotalpaid} = $totalpaid;
581 $main::lxdebug->leave_sub();
586 $main::lxdebug->enter_sub();
588 my $form = $main::form;
589 my %myconfig = %main::myconfig;
590 my $locale = $main::locale;
592 $main::auth->assert('ap_transactions');
593 $form->mtime_ischanged('ap');
595 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
597 my $invdate = $form->datetonum($form->{transdate}, \%myconfig);
599 for my $i (1 .. $form->{paidaccounts}) {
600 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
601 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
603 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
605 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
606 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
608 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
609 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
610 $form->error($locale->text('Cannot post payment for a closed period!'))
611 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
613 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
614 $form->{"exchangerate_$i"} = $form->{exchangerate}
615 if ($invdate == $datepaid);
616 $form->isblank("exchangerate_$i",
617 $locale->text('Exchangerate for payment missing!'));
622 ($form->{AP}) = split /--/, $form->{AP};
623 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
624 if (AP->post_payment(\%myconfig, \%$form)) {
625 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
626 $form->{what_done} = 'invoice';
627 $form->{addition} = "PAYMENT POSTED";
629 $form->redirect($locale->text('Payment posted!'))
631 $form->error($locale->text('Cannot post payment!'));
635 $main::lxdebug->leave_sub();
640 $main::lxdebug->enter_sub();
642 my $form = $main::form;
643 my %myconfig = %main::myconfig;
644 my $locale = $main::locale;
646 $main::auth->assert('ap_transactions');
647 $form->mtime_ischanged('ap');
651 # check if there is a vendor, invoice, due date and invnumber
652 $form->isblank("transdate", $locale->text("Invoice Date missing!"));
653 $form->isblank("duedate", $locale->text("Due Date missing!"));
654 $form->isblank("vendor", $locale->text('Vendor missing!'));
655 $form->isblank("invnumber", $locale->text('Invoice Number missing!'));
657 if ($myconfig{mandatory_departments} && !$form->{department}) {
658 $form->{saved_message} = $::locale->text('You have to specify a department.');
663 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
664 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
666 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
667 if ($form->date_max_future($form->{"transdate"}, \%myconfig));
668 $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
670 my $zero_amount_posting = 1;
671 for my $i (1 .. $form->{rowcount}) {
672 if ($form->parse_amount(\%myconfig, $form->{"amount_$i"})) {
673 $zero_amount_posting = 0;
678 $form->error($locale->text('Zero amount posting!')) if $zero_amount_posting;
680 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
681 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
684 for my $i (1 .. $form->{paidaccounts}) {
685 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
686 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
688 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
690 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
691 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
693 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
694 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
695 $form->error($locale->text('Cannot post payment for a closed period!'))
696 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
698 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
699 $form->{"exchangerate_$i"} = $form->{exchangerate}
700 if ($transdate == $datepaid);
701 $form->isblank("exchangerate_$i",
702 $locale->text('Exchangerate for payment missing!'));
708 # if old vendor ne vendor redo form
709 my ($vendor) = split /--/, $form->{vendor};
710 if ($form->{oldvendor} ne "$vendor--$form->{vendor_id}") {
712 $::dispatcher->end_request;
714 my ($debitaccno, $debittaxkey) = split /--/, $form->{AP_amountselected};
715 my ($taxkey, $NULL) = split /--/, $form->{taxchartselected};
716 my ($payablesaccno, $payablestaxkey) = split /--/, $form->{APselected};
717 # $form->{AP_amount_1} = $debitaccno;
718 $form->{AP_payables} = $payablesaccno;
719 $form->{taxkey} = $taxkey;
722 $form->{id} = 0 if $form->{postasnew};
724 if (AP->post_transaction(\%myconfig, \%$form)) {
726 if(!exists $form->{addition} && $form->{id} ne "") {
727 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
728 $form->{addition} = "POSTED";
729 $form->{what_done} = "invoice";
732 # /saving the history
733 # Dieser Text wird niemals ausgegeben: Probleme beim redirect?
734 $form->redirect($locale->text('Transaction posted!')) unless $inline;
736 $form->error($locale->text('Cannot post transaction!'));
739 $main::lxdebug->leave_sub();
743 $main::lxdebug->enter_sub();
745 my $form = $main::form;
746 my %myconfig = %main::myconfig;
748 $main::auth->assert('ap_transactions');
750 $form->{postasnew} = 1;
752 if(!exists $form->{addition} && $form->{id} ne "") {
753 # does this work? post_as_new for ap doesn't immediately save the
754 # invoice, because the invnumber has to be entered by hand.
755 # And the value of $form->{postasnew} isn't checked when calling post
756 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
757 $form->{addition} = "POSTED AS NEW";
758 $form->{what_done} = "invoice";
761 # /saving the history
764 $main::lxdebug->leave_sub();
768 $main::lxdebug->enter_sub();
770 my $form = $main::form;
771 my %myconfig = %main::myconfig;
773 $main::auth->assert('ap_transactions');
775 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);
776 $form->{paidaccounts} = 1;
778 $form->{invdate} = $form->current_date(\%myconfig);
781 $main::lxdebug->leave_sub();
785 $main::lxdebug->enter_sub();
787 my $form = $main::form;
788 my $locale = $main::locale;
790 $main::auth->assert('ap_transactions');
792 $form->{title} = $locale->text('Confirm!');
796 delete $form->{header};
799 <form method=post action=$form->{script}>
802 foreach my $key (keys %$form) {
803 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
804 $form->{$key} =~ s/\"/"/g;
805 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
809 <h2 class=confirm>$form->{title}</h2>
812 . $locale->text('Are you sure you want to delete Transaction')
813 . qq| $form->{invnumber}</h4>
815 <input name=action class=submit type=submit value="|
816 . $locale->text('Yes') . qq|">
820 $main::lxdebug->leave_sub();
824 $main::lxdebug->enter_sub();
826 my $form = $main::form;
827 my %myconfig = %main::myconfig;
828 my $locale = $main::locale;
830 $main::auth->assert('ap_transactions');
832 if (AP->delete_transaction(\%myconfig, \%$form)) {
834 if(!exists $form->{addition}) {
835 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
836 $form->{addition} = "DELETED";
837 $form->{what_done} = "invoice";
840 # /saving the history
841 $form->redirect($locale->text('Transaction deleted!'));
843 $form->error($locale->text('Cannot delete transaction!'));
845 $main::lxdebug->leave_sub();
849 $main::lxdebug->enter_sub();
851 $main::auth->assert('vendor_invoice_edit');
853 my $form = $main::form;
854 my %myconfig = %main::myconfig;
855 my $locale = $main::locale;
857 # setup customer selection
858 $form->all_vc(\%myconfig, "vendor", "AP");
860 $form->{title} = $locale->text('AP Transactions');
862 $form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 },
863 "departments" => "ALL_DEPARTMENTS",
864 "vendors" => "ALL_VC");
866 # constants and subs for template
867 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
870 print $form->parse_html_template('ap/search', { %myconfig });
872 $main::lxdebug->leave_sub();
875 sub create_subtotal_row {
876 $main::lxdebug->enter_sub();
878 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
880 my $form = $main::form;
881 my %myconfig = %main::myconfig;
883 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
885 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
887 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
889 map { $totals->{$_} = 0 } @{ $subtotal_columns };
891 $main::lxdebug->leave_sub();
896 sub ap_transactions {
897 $main::lxdebug->enter_sub();
899 my $form = $main::form;
900 my %myconfig = %main::myconfig;
901 my $locale = $main::locale;
903 $main::auth->assert('vendor_invoice_edit');
905 ($form->{vendor}, $form->{vendor_id}) = split(/--/, $form->{vendor});
907 report_generator_set_default_sort('transdate', 1);
909 AP->ap_transactions(\%myconfig, \%$form);
911 $form->{title} = $locale->text('AP Transactions');
913 my $report = SL::ReportGenerator->new(\%myconfig, $form);
916 qw(transdate id type invnumber ordnumber name netamount tax amount paid datepaid
917 due duedate transaction_description notes employee globalprojectnumber
918 vendornumber country ustid taxzone payment_terms charts direct_debit);
920 my @hidden_variables = map { "l_${_}" } @columns;
921 push @hidden_variables, "l_subtotal", qw(open closed vendor invnumber ordnumber transaction_description notes project_id transdatefrom transdateto department
922 parts_partnumber parts_description);
924 my $href = build_std_url('action=ap_transactions', grep { $form->{$_} } @hidden_variables);
927 'transdate' => { 'text' => $locale->text('Date'), },
928 'id' => { 'text' => $locale->text('ID'), },
929 'type' => { 'text' => $locale->text('Type'), },
930 'invnumber' => { 'text' => $locale->text('Invoice'), },
931 'ordnumber' => { 'text' => $locale->text('Order'), },
932 'name' => { 'text' => $locale->text('Vendor'), },
933 'netamount' => { 'text' => $locale->text('Amount'), },
934 'tax' => { 'text' => $locale->text('Tax'), },
935 'amount' => { 'text' => $locale->text('Total'), },
936 'paid' => { 'text' => $locale->text('Paid'), },
937 'datepaid' => { 'text' => $locale->text('Date Paid'), },
938 'due' => { 'text' => $locale->text('Amount Due'), },
939 'duedate' => { 'text' => $locale->text('Due Date'), },
940 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
941 'notes' => { 'text' => $locale->text('Notes'), },
942 'employee' => { 'text' => $locale->text('Employee'), },
943 'globalprojectnumber' => { 'text' => $locale->text('Document Project Number'), },
944 'vendornumber' => { 'text' => $locale->text('Vendor Number'), },
945 'country' => { 'text' => $locale->text('Country'), },
946 'ustid' => { 'text' => $locale->text('USt-IdNr.'), },
947 'taxzone' => { 'text' => $locale->text('Tax rate'), },
948 'payment_terms' => { 'text' => $locale->text('Payment Terms'), },
949 'charts' => { 'text' => $locale->text('Buchungskonto'), },
950 'direct_debit' => { 'text' => $locale->text('direct debit'), },
953 foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid employee shippingpoint shipvia transaction_description direct_debit)) {
954 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
955 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
958 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
960 $form->{"l_type"} = "Y";
961 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
963 $report->set_columns(%column_defs);
964 $report->set_column_order(@columns);
966 $report->set_export_options('ap_transactions', @hidden_variables, qw(sort sortdir));
968 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
971 push @options, $locale->text('Vendor') . " : $form->{vendor}" if ($form->{vendor});
972 push @options, $locale->text('Contact Person') . " : $form->{cp_name}" if ($form->{cp_name});
973 push @options, $locale->text('Department') . " : " . (split /--/, $form->{department})[0] if ($form->{department});
974 push @options, $locale->text('Invoice Number') . " : $form->{invnumber}" if ($form->{invnumber});
975 push @options, $locale->text('Order Number') . " : $form->{ordnumber}" if ($form->{ordnumber});
976 push @options, $locale->text('Notes') . " : $form->{notes}" if ($form->{notes});
977 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}" if ($form->{transaction_description});
978 push @options, $locale->text('Part Description') . " : $form->{parts_description}" if $form->{parts_description};
979 push @options, $locale->text('Part Number') . " : $form->{parts_partnumber}" if $form->{parts_partnumber};
980 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1) if ($form->{transdatefrom});
981 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1) if ($form->{transdateto});
982 push @options, $locale->text('Open') if ($form->{open});
983 push @options, $locale->text('Closed') if ($form->{closed});
985 $report->set_options('top_info_text' => join("\n", @options),
986 'raw_bottom_info_text' => $form->parse_html_template('ap/ap_transactions_bottom'),
987 'output_format' => 'HTML',
988 'title' => $form->{title},
989 'attachment_basename' => $locale->text('vendor_invoice_list') . strftime('_%Y%m%d', localtime time),
991 $report->set_options_from_form();
992 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
994 # add sort and escape callback, this one we use for the add sub
995 $form->{callback} = $href .= "&sort=$form->{sort}";
997 # escape callback for href
998 my $callback = $form->escape($href);
1000 my @subtotal_columns = qw(netamount amount paid due);
1002 my %totals = map { $_ => 0 } @subtotal_columns;
1003 my %subtotals = map { $_ => 0 } @subtotal_columns;
1007 foreach my $ap (@{ $form->{AP} }) {
1008 $ap->{tax} = $ap->{amount} - $ap->{netamount};
1009 $ap->{due} = $ap->{amount} - $ap->{paid};
1011 map { $subtotals{$_} += $ap->{$_};
1012 $totals{$_} += $ap->{$_} } @subtotal_columns;
1014 map { $ap->{$_} = $form->format_amount(\%myconfig, $ap->{$_}, 2) } qw(netamount tax amount paid due);
1016 my $is_storno = $ap->{storno} && $ap->{storno_id};
1017 my $has_storno = $ap->{storno} && !$ap->{storno_id};
1019 if ($ap->{invoice}) {
1021 $has_storno ? $locale->text("Invoice with Storno (abbreviation)")
1022 : $is_storno ? $locale->text("Storno (one letter abbreviation)")
1023 : $locale->text("Invoice (one letter abbreviation)");
1026 $has_storno ? $locale->text("AP Transaction with Storno (abbreviation)")
1027 : $is_storno ? $locale->text("AP Transaction Storno (one letter abbreviation)")
1028 : $locale->text("AP Transaction (abbreviation)");
1031 $ap->{direct_debit} = $ap->{direct_debit} ? $::locale->text('yes') : $::locale->text('no');
1035 foreach my $column (@columns) {
1037 'data' => $ap->{$column},
1038 'align' => $column_alignment{$column},
1042 $row->{invnumber}->{link} = build_std_url("script=" . ($ap->{invoice} ? 'ir.pl' : 'ap.pl'), 'action=edit')
1043 . "&id=" . E($ap->{id}) . "&callback=${callback}";
1045 my $row_set = [ $row ];
1047 if (($form->{l_subtotal} eq 'Y')
1048 && (($idx == (scalar @{ $form->{AP} } - 1))
1049 || ($ap->{ $form->{sort} } ne $form->{AP}->[$idx + 1]->{ $form->{sort} }))) {
1050 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1053 $report->add_data($row_set);
1058 $report->add_separator();
1059 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1061 $report->generate_with_headers();
1063 $main::lxdebug->leave_sub();
1067 $main::lxdebug->enter_sub();
1069 my $form = $main::form;
1070 my %myconfig = %main::myconfig;
1071 my $locale = $main::locale;
1073 $main::auth->assert('ap_transactions');
1075 if (IS->has_storno(\%myconfig, $form, 'ap')) {
1076 $form->{title} = $locale->text("Cancel Accounts Payables Transaction");
1077 $form->error($locale->text("Transaction has already been cancelled!"));
1080 $form->error($locale->text('Cannot post storno for a closed period!'))
1081 if ( $form->date_closed($form->{transdate}, \%myconfig));
1083 AP->storno($form, \%myconfig, $form->{id});
1085 # saving the history
1086 if(!exists $form->{addition} && $form->{id} ne "") {
1087 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1088 $form->{addition} = "STORNO";
1089 $form->{what_done} = "invoice";
1090 $form->save_history;
1092 # /saving the history
1094 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1096 $main::lxdebug->leave_sub();