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., 51 Franklin Street, Fifth Floor, Boston,
29 #======================================================================
33 #======================================================================
35 use POSIX qw(strftime);
36 use List::Util qw(max sum);
37 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 $::form->{transdate} = DateTime->today_local->to_kivitendo if !$::form->{transdate};
500 $::form->{gldate} = $::form->{transdate} if !$::form->{gldate};
505 $main::lxdebug->enter_sub();
507 my $form = $main::form;
508 my %myconfig = %main::myconfig;
510 $main::auth->assert('ap_transactions');
514 $form->{invtotal} = 0;
516 delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
518 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
519 qw(exchangerate creditlimit creditremaining);
521 my @flds = qw(amount AP_amount projectnumber oldprojectnumber project_id taxchart);
523 my (@a, $j, $totaltax);
524 for my $i (1 .. $form->{rowcount}) {
525 $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
526 if ($form->{"amount_$i"}) {
529 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
531 # calculate tax exactly the same way as AP in post_transaction via form->calculate_tax
533 ($tmpnetamount,$form->{"tax_$i"}) = $form->calculate_tax($form->{"amount_$i"},$rate,$form->{taxincluded},2);
535 $totaltax += $form->{"tax_$i"};
536 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
540 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
542 map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
544 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
545 $form->{exchangerate} = $form->{forex} if $form->{forex};
547 $form->{invdate} = $form->{transdate};
548 my %saved_variables = map +( $_ => $form->{$_} ), qw(AP AP_amount_1 taxchart_1 notes);
550 my $vendor_changed = &check_name("vendor");
552 $form->{AP} = $saved_variables{AP};
553 if ($saved_variables{AP_amount_1} =~ m/.--./) {
554 map { $form->{$_} = $saved_variables{$_} } qw(AP_amount_1 taxchart_1);
556 delete $form->{taxchart_1};
559 $form->{rowcount} = $count + 1;
562 ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
565 for my $i (1 .. $form->{paidaccounts}) {
566 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
569 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
570 } qw(paid exchangerate);
572 $totalpaid += $form->{"paid_$i"};
574 $form->{"forex_$i"} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
575 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
579 $form->{creditremaining} -=
580 ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} -
581 $form->{oldinvtotal});
582 $form->{oldinvtotal} = $form->{invtotal};
583 $form->{oldtotalpaid} = $totalpaid;
587 $main::lxdebug->leave_sub();
592 $main::lxdebug->enter_sub();
594 my $form = $main::form;
595 my %myconfig = %main::myconfig;
596 my $locale = $main::locale;
598 $main::auth->assert('ap_transactions');
599 $form->mtime_ischanged('ap');
601 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
603 my $invdate = $form->datetonum($form->{transdate}, \%myconfig);
605 for my $i (1 .. $form->{paidaccounts}) {
606 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
607 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
609 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
611 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
612 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
614 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
615 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
616 $form->error($locale->text('Cannot post payment for a closed period!'))
617 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
619 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
620 $form->{"exchangerate_$i"} = $form->{exchangerate}
621 if ($invdate == $datepaid);
622 $form->isblank("exchangerate_$i",
623 $locale->text('Exchangerate for payment missing!'));
628 ($form->{AP}) = split /--/, $form->{AP};
629 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
630 if (AP->post_payment(\%myconfig, \%$form)) {
631 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
632 $form->{what_done} = 'invoice';
633 $form->{addition} = "PAYMENT POSTED";
635 $form->redirect($locale->text('Payment posted!'))
637 $form->error($locale->text('Cannot post payment!'));
641 $main::lxdebug->leave_sub();
646 $main::lxdebug->enter_sub();
648 my $form = $main::form;
649 my %myconfig = %main::myconfig;
650 my $locale = $main::locale;
652 $main::auth->assert('ap_transactions');
653 $form->mtime_ischanged('ap');
657 # check if there is a vendor, invoice, due date and invnumber
658 $form->isblank("transdate", $locale->text("Invoice Date missing!"));
659 $form->isblank("duedate", $locale->text("Due Date missing!"));
660 $form->isblank("vendor", $locale->text('Vendor missing!'));
661 $form->isblank("invnumber", $locale->text('Invoice Number missing!'));
663 if ($myconfig{mandatory_departments} && !$form->{department}) {
664 $form->{saved_message} = $::locale->text('You have to specify a department.');
669 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
670 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
672 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
673 if ($form->date_max_future($form->{"transdate"}, \%myconfig));
674 $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
676 my $zero_amount_posting = 1;
677 for my $i (1 .. $form->{rowcount}) {
678 if ($form->parse_amount(\%myconfig, $form->{"amount_$i"})) {
679 $zero_amount_posting = 0;
684 $form->error($locale->text('Zero amount posting!')) if $zero_amount_posting;
686 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
687 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
690 for my $i (1 .. $form->{paidaccounts}) {
691 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
692 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
694 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
696 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
697 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
699 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
700 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
701 $form->error($locale->text('Cannot post payment for a closed period!'))
702 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
704 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
705 $form->{"exchangerate_$i"} = $form->{exchangerate}
706 if ($transdate == $datepaid);
707 $form->isblank("exchangerate_$i",
708 $locale->text('Exchangerate for payment missing!'));
714 # if old vendor ne vendor redo form
715 my ($vendor) = split /--/, $form->{vendor};
716 if ($form->{oldvendor} ne "$vendor--$form->{vendor_id}") {
718 $::dispatcher->end_request;
720 my ($debitaccno, $debittaxkey) = split /--/, $form->{AP_amountselected};
721 my ($taxkey, $NULL) = split /--/, $form->{taxchartselected};
722 my ($payablesaccno, $payablestaxkey) = split /--/, $form->{APselected};
723 # $form->{AP_amount_1} = $debitaccno;
724 $form->{AP_payables} = $payablesaccno;
725 $form->{taxkey} = $taxkey;
728 $form->{id} = 0 if $form->{postasnew};
730 if (AP->post_transaction(\%myconfig, \%$form)) {
732 if(!exists $form->{addition} && $form->{id} ne "") {
733 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
734 $form->{addition} = "POSTED";
735 $form->{what_done} = "invoice";
738 # /saving the history
739 # Dieser Text wird niemals ausgegeben: Probleme beim redirect?
740 $form->redirect($locale->text('Transaction posted!')) unless $inline;
742 $form->error($locale->text('Cannot post transaction!'));
745 $main::lxdebug->leave_sub();
749 $main::lxdebug->enter_sub();
751 my $form = $main::form;
752 my %myconfig = %main::myconfig;
754 $main::auth->assert('ap_transactions');
756 $form->{postasnew} = 1;
758 if(!exists $form->{addition} && $form->{id} ne "") {
759 # does this work? post_as_new for ap doesn't immediately save the
760 # invoice, because the invnumber has to be entered by hand.
761 # And the value of $form->{postasnew} isn't checked when calling post
762 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
763 $form->{addition} = "POSTED AS NEW";
764 $form->{what_done} = "invoice";
767 # /saving the history
770 $main::lxdebug->leave_sub();
774 $main::lxdebug->enter_sub();
776 my $form = $main::form;
777 my %myconfig = %main::myconfig;
779 $main::auth->assert('ap_transactions');
781 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);
782 $form->{paidaccounts} = 1;
784 $form->{invdate} = $form->current_date(\%myconfig);
787 $main::lxdebug->leave_sub();
791 $main::lxdebug->enter_sub();
793 my $form = $main::form;
794 my $locale = $main::locale;
796 $main::auth->assert('ap_transactions');
798 $form->{title} = $locale->text('Confirm!');
802 delete $form->{header};
805 <form method=post action=$form->{script}>
808 foreach my $key (keys %$form) {
809 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
810 $form->{$key} =~ s/\"/"/g;
811 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
815 <h2 class=confirm>$form->{title}</h2>
818 . $locale->text('Are you sure you want to delete Transaction')
819 . qq| $form->{invnumber}</h4>
821 <input name=action class=submit type=submit value="|
822 . $locale->text('Yes') . qq|">
826 $main::lxdebug->leave_sub();
830 $main::lxdebug->enter_sub();
832 my $form = $main::form;
833 my %myconfig = %main::myconfig;
834 my $locale = $main::locale;
836 $main::auth->assert('ap_transactions');
838 if (AP->delete_transaction(\%myconfig, \%$form)) {
840 if(!exists $form->{addition}) {
841 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
842 $form->{addition} = "DELETED";
843 $form->{what_done} = "invoice";
846 # /saving the history
847 $form->redirect($locale->text('Transaction deleted!'));
849 $form->error($locale->text('Cannot delete transaction!'));
851 $main::lxdebug->leave_sub();
855 $main::lxdebug->enter_sub();
857 $main::auth->assert('vendor_invoice_edit');
859 my $form = $main::form;
860 my %myconfig = %main::myconfig;
861 my $locale = $main::locale;
863 # setup customer selection
864 $form->all_vc(\%myconfig, "vendor", "AP");
866 $form->{title} = $locale->text('AP Transactions');
868 $form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 },
869 "departments" => "ALL_DEPARTMENTS",
870 "vendors" => "ALL_VC");
872 # constants and subs for template
873 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
876 print $form->parse_html_template('ap/search', { %myconfig });
878 $main::lxdebug->leave_sub();
881 sub create_subtotal_row {
882 $main::lxdebug->enter_sub();
884 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
886 my $form = $main::form;
887 my %myconfig = %main::myconfig;
889 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
891 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
893 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
895 map { $totals->{$_} = 0 } @{ $subtotal_columns };
897 $main::lxdebug->leave_sub();
902 sub ap_transactions {
903 $main::lxdebug->enter_sub();
905 my $form = $main::form;
906 my %myconfig = %main::myconfig;
907 my $locale = $main::locale;
909 $main::auth->assert('vendor_invoice_edit');
911 ($form->{vendor}, $form->{vendor_id}) = split(/--/, $form->{vendor});
913 report_generator_set_default_sort('transdate', 1);
915 AP->ap_transactions(\%myconfig, \%$form);
917 $form->{title} = $locale->text('AP Transactions');
919 my $report = SL::ReportGenerator->new(\%myconfig, $form);
922 qw(transdate id type invnumber ordnumber name netamount tax amount paid datepaid
923 due duedate transaction_description notes employee globalprojectnumber
924 vendornumber country ustid taxzone payment_terms charts direct_debit);
926 my @hidden_variables = map { "l_${_}" } @columns;
927 push @hidden_variables, "l_subtotal", qw(open closed vendor invnumber ordnumber transaction_description notes project_id transdatefrom transdateto department
928 parts_partnumber parts_description);
930 my $href = build_std_url('action=ap_transactions', grep { $form->{$_} } @hidden_variables);
933 'transdate' => { 'text' => $locale->text('Date'), },
934 'id' => { 'text' => $locale->text('ID'), },
935 'type' => { 'text' => $locale->text('Type'), },
936 'invnumber' => { 'text' => $locale->text('Invoice'), },
937 'ordnumber' => { 'text' => $locale->text('Order'), },
938 'name' => { 'text' => $locale->text('Vendor'), },
939 'netamount' => { 'text' => $locale->text('Amount'), },
940 'tax' => { 'text' => $locale->text('Tax'), },
941 'amount' => { 'text' => $locale->text('Total'), },
942 'paid' => { 'text' => $locale->text('Paid'), },
943 'datepaid' => { 'text' => $locale->text('Date Paid'), },
944 'due' => { 'text' => $locale->text('Amount Due'), },
945 'duedate' => { 'text' => $locale->text('Due Date'), },
946 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
947 'notes' => { 'text' => $locale->text('Notes'), },
948 'employee' => { 'text' => $locale->text('Employee'), },
949 'globalprojectnumber' => { 'text' => $locale->text('Document Project Number'), },
950 'vendornumber' => { 'text' => $locale->text('Vendor Number'), },
951 'country' => { 'text' => $locale->text('Country'), },
952 'ustid' => { 'text' => $locale->text('USt-IdNr.'), },
953 'taxzone' => { 'text' => $locale->text('Tax rate'), },
954 'payment_terms' => { 'text' => $locale->text('Payment Terms'), },
955 'charts' => { 'text' => $locale->text('Buchungskonto'), },
956 'direct_debit' => { 'text' => $locale->text('direct debit'), },
959 foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid employee shippingpoint shipvia transaction_description direct_debit)) {
960 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
961 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
964 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
966 $form->{"l_type"} = "Y";
967 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
969 $report->set_columns(%column_defs);
970 $report->set_column_order(@columns);
972 $report->set_export_options('ap_transactions', @hidden_variables, qw(sort sortdir));
974 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
977 push @options, $locale->text('Vendor') . " : $form->{vendor}" if ($form->{vendor});
978 push @options, $locale->text('Contact Person') . " : $form->{cp_name}" if ($form->{cp_name});
979 push @options, $locale->text('Department') . " : " . (split /--/, $form->{department})[0] if ($form->{department});
980 push @options, $locale->text('Invoice Number') . " : $form->{invnumber}" if ($form->{invnumber});
981 push @options, $locale->text('Order Number') . " : $form->{ordnumber}" if ($form->{ordnumber});
982 push @options, $locale->text('Notes') . " : $form->{notes}" if ($form->{notes});
983 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}" if ($form->{transaction_description});
984 push @options, $locale->text('Part Description') . " : $form->{parts_description}" if $form->{parts_description};
985 push @options, $locale->text('Part Number') . " : $form->{parts_partnumber}" if $form->{parts_partnumber};
986 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1) if ($form->{transdatefrom});
987 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1) if ($form->{transdateto});
988 push @options, $locale->text('Open') if ($form->{open});
989 push @options, $locale->text('Closed') if ($form->{closed});
991 $report->set_options('top_info_text' => join("\n", @options),
992 'raw_bottom_info_text' => $form->parse_html_template('ap/ap_transactions_bottom'),
993 'output_format' => 'HTML',
994 'title' => $form->{title},
995 'attachment_basename' => $locale->text('vendor_invoice_list') . strftime('_%Y%m%d', localtime time),
997 $report->set_options_from_form();
998 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1000 # add sort and escape callback, this one we use for the add sub
1001 $form->{callback} = $href .= "&sort=$form->{sort}";
1003 # escape callback for href
1004 my $callback = $form->escape($href);
1006 my @subtotal_columns = qw(netamount amount paid due);
1008 my %totals = map { $_ => 0 } @subtotal_columns;
1009 my %subtotals = map { $_ => 0 } @subtotal_columns;
1013 foreach my $ap (@{ $form->{AP} }) {
1014 $ap->{tax} = $ap->{amount} - $ap->{netamount};
1015 $ap->{due} = $ap->{amount} - $ap->{paid};
1017 map { $subtotals{$_} += $ap->{$_};
1018 $totals{$_} += $ap->{$_} } @subtotal_columns;
1020 map { $ap->{$_} = $form->format_amount(\%myconfig, $ap->{$_}, 2) } qw(netamount tax amount paid due);
1022 my $is_storno = $ap->{storno} && $ap->{storno_id};
1023 my $has_storno = $ap->{storno} && !$ap->{storno_id};
1025 if ($ap->{invoice}) {
1027 $has_storno ? $locale->text("Invoice with Storno (abbreviation)")
1028 : $is_storno ? $locale->text("Storno (one letter abbreviation)")
1029 : $locale->text("Invoice (one letter abbreviation)");
1032 $has_storno ? $locale->text("AP Transaction with Storno (abbreviation)")
1033 : $is_storno ? $locale->text("AP Transaction Storno (one letter abbreviation)")
1034 : $locale->text("AP Transaction (abbreviation)");
1037 $ap->{direct_debit} = $ap->{direct_debit} ? $::locale->text('yes') : $::locale->text('no');
1041 foreach my $column (@columns) {
1043 'data' => $ap->{$column},
1044 'align' => $column_alignment{$column},
1048 $row->{invnumber}->{link} = build_std_url("script=" . ($ap->{invoice} ? 'ir.pl' : 'ap.pl'), 'action=edit')
1049 . "&id=" . E($ap->{id}) . "&callback=${callback}";
1051 my $row_set = [ $row ];
1053 if (($form->{l_subtotal} eq 'Y')
1054 && (($idx == (scalar @{ $form->{AP} } - 1))
1055 || ($ap->{ $form->{sort} } ne $form->{AP}->[$idx + 1]->{ $form->{sort} }))) {
1056 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1059 $report->add_data($row_set);
1064 $report->add_separator();
1065 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1067 $report->generate_with_headers();
1069 $main::lxdebug->leave_sub();
1073 $main::lxdebug->enter_sub();
1075 my $form = $main::form;
1076 my %myconfig = %main::myconfig;
1077 my $locale = $main::locale;
1079 $main::auth->assert('ap_transactions');
1081 if (IS->has_storno(\%myconfig, $form, 'ap')) {
1082 $form->{title} = $locale->text("Cancel Accounts Payables Transaction");
1083 $form->error($locale->text("Transaction has already been cancelled!"));
1086 $form->error($locale->text('Cannot post storno for a closed period!'))
1087 if ( $form->date_closed($form->{transdate}, \%myconfig));
1089 AP->storno($form, \%myconfig, $form->{id});
1091 # saving the history
1092 if(!exists $form->{addition} && $form->{id} ne "") {
1093 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1094 $form->{addition} = "STORNO";
1095 $form->{what_done} = "invoice";
1096 $form->save_history;
1098 # /saving the history
1100 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1102 $main::lxdebug->leave_sub();