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);
44 use SL::ReportGenerator;
47 use SL::DB::PurchaseInvoice;
49 use SL::Locale::String qw(t8);
51 require "bin/mozilla/arap.pl";
52 require "bin/mozilla/common.pl";
53 require "bin/mozilla/reportgenerator.pl";
61 # this is for our long dates
62 # $locale->text('January')
63 # $locale->text('February')
64 # $locale->text('March')
65 # $locale->text('April')
66 # $locale->text('May ')
67 # $locale->text('June')
68 # $locale->text('July')
69 # $locale->text('August')
70 # $locale->text('September')
71 # $locale->text('October')
72 # $locale->text('November')
73 # $locale->text('December')
75 # this is for our short month
76 # $locale->text('Jan')
77 # $locale->text('Feb')
78 # $locale->text('Mar')
79 # $locale->text('Apr')
80 # $locale->text('May')
81 # $locale->text('Jun')
82 # $locale->text('Jul')
83 # $locale->text('Aug')
84 # $locale->text('Sep')
85 # $locale->text('Oct')
86 # $locale->text('Nov')
87 # $locale->text('Dec')
90 $main::lxdebug->enter_sub();
92 my $form = $main::form;
93 my %myconfig = %main::myconfig;
95 $main::auth->assert('ap_transactions');
97 $form->{title} = "Add";
99 $form->{callback} = "ap.pl?action=add" unless $form->{callback};
101 AP->get_transdate(\%myconfig, $form);
102 $form->{initial_transdate} = $form->{transdate};
103 create_links(dont_save => 1);
104 $form->{transdate} = $form->{initial_transdate};
106 if ($form->{vendor_id}) {
107 my $last_used_ap_chart = SL::DB::Vendor->load_cached($form->{vendor_id})->last_used_ap_chart;
108 $form->{"AP_amount_chart_id_1"} = $last_used_ap_chart->id if $last_used_ap_chart;
113 $main::lxdebug->leave_sub();
117 $main::lxdebug->enter_sub();
119 my $form = $main::form;
121 $main::auth->assert('ap_transactions');
123 $form->{title} = "Edit";
128 $main::lxdebug->leave_sub();
132 $main::lxdebug->enter_sub();
134 my $form = $main::form;
136 $main::auth->assert('ap_transactions');
138 # get all files stored in the webdav folder
139 if ($form->{invnumber} && $::instance_conf->get_webdav) {
140 my $webdav = SL::Webdav->new(
141 type => 'accounts_payable',
142 number => $form->{invnumber},
144 my $webdav_path = $webdav->webdav_path;
145 my @all_objects = $webdav->get_all_objects;
146 @{ $form->{WEBDAV} } = map { { name => $_->filename,
148 link => File::Spec->catfile($_->full_filedescriptor),
154 $main::lxdebug->leave_sub();
158 $main::lxdebug->enter_sub();
162 my $form = $main::form;
163 my %myconfig = %main::myconfig;
165 $main::auth->assert('ap_transactions');
167 $form->create_links("AP", \%myconfig, "vendor");
169 if (!$params{dont_save}) {
170 %saved = map { ($_ => $form->{$_}) } qw(direct_debit taxincluded);
171 $saved{duedate} = $form->{duedate} if $form->{duedate};
172 $saved{currency} = $form->{currency} if $form->{currency};
173 $saved{taxincluded} = $form->{taxincluded} if $form->{taxincluded};
176 IR->get_vendor(\%myconfig, \%$form);
178 $form->{$_} = $saved{$_} for keys %saved;
179 $form->{rowcount} = 1;
180 $form->{AP_chart_id} = $form->{acc_trans} && $form->{acc_trans}->{AP} ? $form->{acc_trans}->{AP}->[0]->{chart_id} : $form->{AP_links}->{AP}->[0]->{chart_id};
182 # build the popup menus
183 $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
186 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
188 $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
190 $form->{employee} = "$form->{employee}--$form->{employee_id}";
192 AP->setup_form($form);
195 ($form->datetonum($form->{transdate}, \%myconfig) <=
196 $form->datetonum($form->{closedto}, \%myconfig));
198 $main::lxdebug->leave_sub();
202 my @fields = qw(acc_trans_id gldate datepaid source memo paid AP_paid paid_project_id);
204 grep { $_->{paid} != 0 }
207 +{ map { ($_ => delete($::form->{"${_}_${idx}"})) } @fields }
208 } (1..$::form->{paidaccounts});
210 @payments = sort_by { DateTime->from_kivitendo($_->{datepaid}) } @payments;
212 $::form->{paidaccounts} = max scalar(@payments), 1;
214 foreach my $idx (1 .. scalar(@payments)) {
215 my $payment = $payments[$idx - 1];
216 $::form->{"${_}_${idx}"} = $payment->{$_} for @fields;
221 $main::lxdebug->enter_sub();
223 my $form = $main::form;
224 my %myconfig = %main::myconfig;
225 my $locale = $main::locale;
226 my $cgi = $::request->{cgi};
228 $main::auth->assert('ap_transactions');
230 $::form->{invoice_obj} = SL::DB::PurchaseInvoice->new(id => $::form->{id})->load if $::form->{id};
232 $form->{initial_focus} = !($form->{amount_1} * 1) ? 'vendor_id' : 'row_' . $form->{rowcount};
234 $form->{title_} = $form->{title};
235 $form->{title} = $form->{title} eq 'Add' ? $locale->text('Add Accounts Payables Transaction') : $locale->text('Edit Accounts Payables Transaction');
237 # type=submit $locale->text('Add Accounts Payables Transaction')
238 # type=submit $locale->text('Edit Accounts Payables Transaction')
240 my $readonly = $form->{id} ? "readonly" : "";
242 $form->{radier} = ($::instance_conf->get_ap_changeable == 2)
243 ? ($form->current_date(\%myconfig) eq $form->{gldate})
244 : ($::instance_conf->get_ap_changeable == 1);
245 $readonly = $form->{radier} ? "" : $readonly;
247 $form->{readonly} = $readonly;
249 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
250 if ( $form->{forex} ) {
251 $form->{exchangerate} = $form->{forex};
255 $form->{exchangerate} = $form->{exchangerate} ? $form->format_amount(\%myconfig, $form->{exchangerate}) : '';
256 $form->{creditlimit} = $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
257 $form->{creditremaining} = $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
260 if (($rows = $form->numtextrows($form->{notes}, 50)) < 2) {
263 $form->{textarea_rows} = $rows;
265 $form->{creditremaining_plus} = ($form->{creditremaining} =~ /-/) ? "0" : "1";
267 my @old_project_ids = ();
270 if ($form->{"project_id_$_"}) {
271 push(@old_project_ids, $form->{"project_id_$_"});
274 (1..$form->{"rowcount"})
277 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
279 "old_id" => \@old_project_ids },
280 "charts" => { "key" => "ALL_CHARTS",
281 "transdate" => $form->{transdate} },
282 "taxcharts" => { "key" => "ALL_TAXCHARTS",
283 "module" => "AP" },);
286 { $_->{link_split} = [ split(/:/, $_->{link}) ]; }
287 @{ $form->{ALL_CHARTS} }
290 $form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
292 my %project_labels = ();
293 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
294 $project_labels{$item->{id}} = $item->{projectnumber};
298 my $default_ap_amount_chart_id;
300 foreach my $item (@{ $form->{ALL_CHARTS} }) {
301 if ( grep({ $_ eq 'AP_amount' } @{ $item->{link_split} }) ) {
302 $default_ap_amount_chart_id //= $item->{id};
304 } elsif ( grep({ $_ eq 'AP_paid' } @{ $item->{link_split} }) ) {
305 push(@{ $form->{ALL_CHARTS_AP_paid} }, $item);
308 $charts{$item->{accno}} = $item;
311 my $follow_up_vc = $form->{vendor_id} ? SL::DB::Vendor->load_cached($form->{vendor_id})->name : '';
312 my $follow_up_trans_info = "$form->{invnumber} ($follow_up_vc)";
314 $::request->layout->add_javascripts("autocomplete_chart.js", "autocomplete_customer.js", "show_vc_details.js", "show_history.js", "follow_up.js", "kivi.Draft.js", "kivi.GL.js");
315 my $transdate = $::form->{transdate} ? DateTime->from_kivitendo($::form->{transdate}) : DateTime->today_local;
320 for my $i (1 .. $form->{rowcount}) {
323 $form->{"amount_$i"} = $form->format_amount(\%myconfig, $form->{"amount_$i"}, 2);
324 $form->{"tax_$i"} = $form->format_amount(\%myconfig, $form->{"tax_$i"}, 2);
326 my ($default_taxchart, $taxchart_to_use);
327 my $amount_chart_id = $form->{"AP_amount_chart_id_$i"} || $default_ap_amount_chart_id;
328 my $chart_has_changed = $::form->{"previous_AP_amount_chart_id_$i"} && ($amount_chart_id != $::form->{"previous_AP_amount_chart_id_$i"});
329 my @taxcharts = GL->get_active_taxes_for_chart($amount_chart_id, $transdate);
331 foreach my $item (@taxcharts) {
332 my $key = $item->id . "--" . $item->rate;
333 $first_taxchart //= $item;
334 $default_taxchart = $item if $item->{is_default};
335 $taxchart_to_use = $item if $key eq $form->{"taxchart_$i"};
338 $taxchart_to_use = $default_taxchart // $first_taxchart if $chart_has_changed || !$taxchart_to_use;
339 my $selected_taxchart = $taxchart_to_use->id . '--' . $taxchart_to_use->rate;
340 $form->{"selected_taxchart_$i"} = $selected_taxchart;
341 $form->{"AP_amount_chart_id_$i"} = $amount_chart_id;
342 $form->{"taxcharts_$i"} = \@taxcharts;
345 $form->{taxchart_value_title_sub} = sub {
348 $item->{id} .'--'. $item->{rate},
349 $item->{taxdescription} .' '. ($item->{rate} * 100) .' %',
353 $form->{AP_paid_value_title_sub} = sub {
357 $item->{accno} .'--'. $item->{description}
361 $form->{invtotal_unformatted} = $form->{invtotal};
362 $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
364 $form->{totalpaid} = 0;
368 if ( $form->{'paid_'. $form->{paidaccounts}} ) {
369 $form->{paidaccounts}++;
372 # default account for current assets (i.e. 1801 - SKR04)
373 $form->{accno_arap} = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
375 for my $i (1 .. $form->{paidaccounts}) {
376 $form->{totalpaid} += $form->{"paid_$i"};
379 if ($form->{"paid_$i"}) {
380 $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
382 if ($form->{"exchangerate_$i"} == 0) {
383 $form->{"exchangerate_$i"} = "";
385 $form->{"exchangerate_$i"} =
386 $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
390 if (SL::DB::Default->get->payments_changeable == 0) {
392 $changeable = ($form->{"acc_trans_id_$i"})? 0 : 1;
394 if (SL::DB::Default->get->payments_changeable == 2) {
396 $changeable = (($form->{"gldate_$i"} eq '') || $form->current_date(\%myconfig) eq $form->{"gldate_$i"});
399 #deaktivieren von gebuchten Zahlungen ausserhalb der Bücherkontrolle, vorher prüfen ob heute eingegeben
400 if ($form->date_closed($form->{"gldate_$i"})) {
404 $form->{'paidaccount_changeable_'. $i} = $changeable;
406 $form->{'labelpaid_project_id_'. $i} = $project_labels{$form->{'paid_project_id_'. $i}};
409 $form->{paid_missing} = $form->{invtotal_unformatted} - $form->{totalpaid};
411 print $form->parse_html_template('ap/form_header', {
412 today => DateTime->today,
413 currencies => SL::DB::Manager::Currency->get_all_sorted,
416 $main::lxdebug->leave_sub();
420 $::lxdebug->enter_sub;
421 $::auth->assert('ap_transactions');
426 my $follow_ups = FU->follow_ups('trans_id' => $::form->{id}, 'not_done' => 1);
428 if (@{ $follow_ups }) {
429 $num_due = sum map { $_->{due} * 1 } @{ $follow_ups };
430 $num_follow_ups = scalar @{ $follow_ups }
434 my $transdate = $::form->datetonum($::form->{transdate}, \%::myconfig);
435 my $closedto = $::form->datetonum($::form->{closedto}, \%::myconfig);
437 my $storno = $::form->{id}
438 && !IS->has_storno(\%::myconfig, $::form, 'ap')
439 && !IS->is_storno( \%::myconfig, $::form, 'ap', $::form->{id})
440 && ($::form->{totalpaid} == 0 || $::form->{totalpaid} eq '');
443 print $::form->parse_html_template('ap/form_footer', {
445 num_follow_ups => $num_follow_ups,
446 show_post_draft => ($transdate > $closedto) && !$::form->{id},
447 show_storno => $storno,
450 $::lxdebug->leave_sub;
454 $::auth->assert('ap_transactions');
456 SL::DB::PurchaseInvoice->new(id => $::form->{id})->load->mark_as_paid;
458 $::form->redirect($::locale->text("Marked as paid"));
462 $::form->{transdate} = DateTime->today_local->to_kivitendo if !$::form->{transdate};
463 $::form->{gldate} = $::form->{transdate} if !$::form->{gldate};
468 $main::lxdebug->enter_sub();
470 my $form = $main::form;
471 my %myconfig = %main::myconfig;
473 $main::auth->assert('ap_transactions');
477 $form->{invtotal} = 0;
479 delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
481 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
482 qw(exchangerate creditlimit creditremaining);
484 my @flds = qw(amount AP_amount projectnumber oldprojectnumber project_id taxchart);
486 my (@a, $j, $totaltax);
487 for my $i (1 .. $form->{rowcount}) {
488 $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
489 if ($form->{"amount_$i"}) {
492 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
494 # calculate tax exactly the same way as AP in post_transaction via form->calculate_tax
496 ($tmpnetamount,$form->{"tax_$i"}) = $form->calculate_tax($form->{"amount_$i"},$rate,$form->{taxincluded},2);
498 $totaltax += $form->{"tax_$i"};
499 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
503 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
505 map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
507 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
508 $form->{exchangerate} = $form->{forex} if $form->{forex};
510 $form->{invdate} = $form->{transdate};
512 if (($form->{previous_vendor_id} || $form->{vendor_id}) != $form->{vendor_id}) {
513 IR->get_vendor(\%::myconfig, $form);
516 $form->{rowcount} = $count + 1;
519 ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
522 for my $i (1 .. $form->{paidaccounts}) {
523 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
526 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
527 } qw(paid exchangerate);
529 $totalpaid += $form->{"paid_$i"};
531 $form->{"forex_$i"} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
532 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
536 $form->{creditremaining} -=
537 ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} -
538 $form->{oldinvtotal});
539 $form->{oldinvtotal} = $form->{invtotal};
540 $form->{oldtotalpaid} = $totalpaid;
544 $main::lxdebug->leave_sub();
549 $main::lxdebug->enter_sub();
551 my $form = $main::form;
552 my %myconfig = %main::myconfig;
553 my $locale = $main::locale;
555 $main::auth->assert('ap_transactions');
556 $form->mtime_ischanged('ap');
558 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
560 my $invdate = $form->datetonum($form->{transdate}, \%myconfig);
562 for my $i (1 .. $form->{paidaccounts}) {
563 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
564 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
566 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
568 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
569 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
571 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
572 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
573 $form->error($locale->text('Cannot post payment for a closed period!'))
574 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
576 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
577 $form->{"exchangerate_$i"} = $form->{exchangerate}
578 if ($invdate == $datepaid);
579 $form->isblank("exchangerate_$i",
580 $locale->text('Exchangerate for payment missing!'));
585 ($form->{AP}) = split /--/, $form->{AP};
586 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
587 if (AP->post_payment(\%myconfig, \%$form)) {
588 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
589 $form->{what_done} = 'invoice';
590 $form->{addition} = "PAYMENT POSTED";
592 $form->redirect($locale->text('Payment posted!'))
594 $form->error($locale->text('Cannot post payment!'));
598 $main::lxdebug->leave_sub();
603 $main::lxdebug->enter_sub();
605 my $form = $main::form;
606 my %myconfig = %main::myconfig;
607 my $locale = $main::locale;
609 $main::auth->assert('ap_transactions');
610 $form->mtime_ischanged('ap');
614 # check if there is a vendor, invoice, due date and invnumber
615 $form->isblank("transdate", $locale->text("Invoice Date missing!"));
616 $form->isblank("duedate", $locale->text("Due Date missing!"));
617 $form->isblank("vendor_id", $locale->text('Vendor missing!'));
618 $form->isblank("invnumber", $locale->text('Invoice Number missing!'));
620 if ($myconfig{mandatory_departments} && !$form->{department_id}) {
621 $form->{saved_message} = $::locale->text('You have to specify a department.');
626 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
627 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
629 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
630 if ($form->date_max_future($form->{"transdate"}, \%myconfig));
631 $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
633 my $zero_amount_posting = 1;
634 for my $i (1 .. $form->{rowcount}) {
635 if ($form->parse_amount(\%myconfig, $form->{"amount_$i"})) {
636 $zero_amount_posting = 0;
641 $form->error($locale->text('Zero amount posting!')) if $zero_amount_posting;
643 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
644 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
647 for my $i (1 .. $form->{paidaccounts}) {
648 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
649 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
651 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
653 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
654 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
656 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
657 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
658 $form->error($locale->text('Cannot post payment for a closed period!'))
659 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
661 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
662 $form->{"exchangerate_$i"} = $form->{exchangerate}
663 if ($transdate == $datepaid);
664 $form->isblank("exchangerate_$i",
665 $locale->text('Exchangerate for payment missing!'));
671 # if old vendor ne vendor redo form
672 if (($form->{previous_customer_id} || $form->{customer_id}) != $form->{customer_id}) {
674 $::dispatcher->end_request;
678 $form->{id} = 0 if $form->{postasnew};
680 if (AP->post_transaction(\%myconfig, \%$form)) {
681 # create webdav folder
682 if ($::instance_conf->get_webdav) {
683 SL::Webdav->new(type => 'accounts_payable',
684 number => $form->{invnumber},
688 if(!exists $form->{addition} && $form->{id} ne "") {
689 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
690 $form->{addition} = "POSTED";
691 $form->{what_done} = "invoice";
694 # /saving the history
695 # Dieser Text wird niemals ausgegeben: Probleme beim redirect?
696 $form->redirect($locale->text('Transaction posted!')) unless $inline;
698 $form->error($locale->text('Cannot post transaction!'));
701 $main::lxdebug->leave_sub();
705 $main::lxdebug->enter_sub();
707 my $form = $main::form;
708 my %myconfig = %main::myconfig;
710 $main::auth->assert('ap_transactions');
712 $form->{postasnew} = 1;
714 if(!exists $form->{addition} && $form->{id} ne "") {
715 # does this work? post_as_new for ap doesn't immediately save the
716 # invoice, because the invnumber has to be entered by hand.
717 # And the value of $form->{postasnew} isn't checked when calling post
718 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
719 $form->{addition} = "POSTED AS NEW";
720 $form->{what_done} = "invoice";
723 # /saving the history
726 $main::lxdebug->leave_sub();
730 $main::lxdebug->enter_sub();
732 my $form = $main::form;
733 my %myconfig = %main::myconfig;
735 $main::auth->assert('ap_transactions');
737 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);
738 $form->{paidaccounts} = 1;
740 $form->{invdate} = $form->current_date(\%myconfig);
743 $main::lxdebug->leave_sub();
747 $main::lxdebug->enter_sub();
749 my $form = $main::form;
750 my $locale = $main::locale;
752 $main::auth->assert('ap_transactions');
754 $form->{title} = $locale->text('Confirm!');
758 delete $form->{header};
761 <form method=post action=$form->{script}>
764 foreach my $key (keys %$form) {
765 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
766 $form->{$key} =~ s/\"/"/g;
767 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
771 <h2 class=confirm>$form->{title}</h2>
774 . $locale->text('Are you sure you want to delete Transaction')
775 . qq| $form->{invnumber}</h4>
777 <input name=action class=submit type=submit value="|
778 . $locale->text('Yes') . qq|">
782 $main::lxdebug->leave_sub();
786 $main::lxdebug->enter_sub();
788 my $form = $main::form;
789 my %myconfig = %main::myconfig;
790 my $locale = $main::locale;
792 $main::auth->assert('ap_transactions');
794 if (AP->delete_transaction(\%myconfig, \%$form)) {
796 if(!exists $form->{addition}) {
797 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
798 $form->{addition} = "DELETED";
799 $form->{what_done} = "invoice";
802 # /saving the history
803 $form->redirect($locale->text('Transaction deleted!'));
805 $form->error($locale->text('Cannot delete transaction!'));
807 $main::lxdebug->leave_sub();
811 $main::lxdebug->enter_sub();
813 $main::auth->assert('vendor_invoice_edit');
815 my $form = $main::form;
816 my %myconfig = %main::myconfig;
817 my $locale = $main::locale;
819 $form->{title} = $locale->text('AP Transactions');
821 $form->get_lists(projects => { "key" => "ALL_PROJECTS", "all" => 1 });
823 $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
824 # constants and subs for template
825 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
827 $::request->layout->add_javascripts("autocomplete_project.js");
830 print $form->parse_html_template('ap/search', { %myconfig });
832 $main::lxdebug->leave_sub();
835 sub create_subtotal_row {
836 $main::lxdebug->enter_sub();
838 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
840 my $form = $main::form;
841 my %myconfig = %main::myconfig;
843 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
845 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
847 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
849 map { $totals->{$_} = 0 } @{ $subtotal_columns };
851 $main::lxdebug->leave_sub();
856 sub ap_transactions {
857 $main::lxdebug->enter_sub();
859 my $form = $main::form;
860 my %myconfig = %main::myconfig;
861 my $locale = $main::locale;
863 $main::auth->assert('vendor_invoice_edit');
865 report_generator_set_default_sort('transdate', 1);
867 AP->ap_transactions(\%myconfig, \%$form);
869 $form->{title} = $locale->text('AP Transactions');
871 my $report = SL::ReportGenerator->new(\%myconfig, $form);
874 qw(transdate id type invnumber ordnumber name netamount tax amount paid datepaid
875 due duedate transaction_description notes employee globalprojectnumber
876 vendornumber country ustid taxzone payment_terms charts direct_debit);
878 my @hidden_variables = map { "l_${_}" } @columns;
879 push @hidden_variables, "l_subtotal", qw(open closed vendor invnumber ordnumber transaction_description notes project_id transdatefrom transdateto
880 parts_partnumber parts_description);
882 my $href = build_std_url('action=ap_transactions', grep { $form->{$_} } @hidden_variables);
885 'transdate' => { 'text' => $locale->text('Date'), },
886 'id' => { 'text' => $locale->text('ID'), },
887 'type' => { 'text' => $locale->text('Type'), },
888 'invnumber' => { 'text' => $locale->text('Invoice'), },
889 'ordnumber' => { 'text' => $locale->text('Order'), },
890 'name' => { 'text' => $locale->text('Vendor'), },
891 'netamount' => { 'text' => $locale->text('Amount'), },
892 'tax' => { 'text' => $locale->text('Tax'), },
893 'amount' => { 'text' => $locale->text('Total'), },
894 'paid' => { 'text' => $locale->text('Paid'), },
895 'datepaid' => { 'text' => $locale->text('Date Paid'), },
896 'due' => { 'text' => $locale->text('Amount Due'), },
897 'duedate' => { 'text' => $locale->text('Due Date'), },
898 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
899 'notes' => { 'text' => $locale->text('Notes'), },
900 'employee' => { 'text' => $locale->text('Employee'), },
901 'globalprojectnumber' => { 'text' => $locale->text('Document Project Number'), },
902 'vendornumber' => { 'text' => $locale->text('Vendor Number'), },
903 'country' => { 'text' => $locale->text('Country'), },
904 'ustid' => { 'text' => $locale->text('USt-IdNr.'), },
905 'taxzone' => { 'text' => $locale->text('Tax rate'), },
906 'payment_terms' => { 'text' => $locale->text('Payment Terms'), },
907 'charts' => { 'text' => $locale->text('Buchungskonto'), },
908 'direct_debit' => { 'text' => $locale->text('direct debit'), },
911 foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid employee shippingpoint shipvia transaction_description direct_debit)) {
912 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
913 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
916 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
918 $form->{"l_type"} = "Y";
919 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
921 $report->set_columns(%column_defs);
922 $report->set_column_order(@columns);
924 $report->set_export_options('ap_transactions', @hidden_variables, qw(sort sortdir));
926 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
929 push @options, $locale->text('Vendor') . " : $form->{vendor}" if ($form->{vendor});
930 push @options, $locale->text('Contact Person') . " : $form->{cp_name}" if ($form->{cp_name});
931 push @options, $locale->text('Department') . " : $form->{department}" if ($form->{department});
932 push @options, $locale->text('Invoice Number') . " : $form->{invnumber}" if ($form->{invnumber});
933 push @options, $locale->text('Order Number') . " : $form->{ordnumber}" if ($form->{ordnumber});
934 push @options, $locale->text('Notes') . " : $form->{notes}" if ($form->{notes});
935 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}" if ($form->{transaction_description});
936 push @options, $locale->text('Part Description') . " : $form->{parts_description}" if $form->{parts_description};
937 push @options, $locale->text('Part Number') . " : $form->{parts_partnumber}" if $form->{parts_partnumber};
938 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1) if ($form->{transdatefrom});
939 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1) if ($form->{transdateto});
940 push @options, $locale->text('Open') if ($form->{open});
941 push @options, $locale->text('Closed') if ($form->{closed});
943 $report->set_options('top_info_text' => join("\n", @options),
944 'raw_bottom_info_text' => $form->parse_html_template('ap/ap_transactions_bottom'),
945 'output_format' => 'HTML',
946 'title' => $form->{title},
947 'attachment_basename' => $locale->text('vendor_invoice_list') . strftime('_%Y%m%d', localtime time),
949 $report->set_options_from_form();
950 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
952 # add sort and escape callback, this one we use for the add sub
953 $form->{callback} = $href .= "&sort=$form->{sort}";
955 # escape callback for href
956 my $callback = $form->escape($href);
958 my @subtotal_columns = qw(netamount amount paid due);
960 my %totals = map { $_ => 0 } @subtotal_columns;
961 my %subtotals = map { $_ => 0 } @subtotal_columns;
965 foreach my $ap (@{ $form->{AP} }) {
966 $ap->{tax} = $ap->{amount} - $ap->{netamount};
967 $ap->{due} = $ap->{amount} - $ap->{paid};
969 map { $subtotals{$_} += $ap->{$_};
970 $totals{$_} += $ap->{$_} } @subtotal_columns;
972 map { $ap->{$_} = $form->format_amount(\%myconfig, $ap->{$_}, 2) } qw(netamount tax amount paid due);
974 my $is_storno = $ap->{storno} && $ap->{storno_id};
975 my $has_storno = $ap->{storno} && !$ap->{storno_id};
977 if ($ap->{invoice}) {
979 $has_storno ? $locale->text("Invoice with Storno (abbreviation)")
980 : $is_storno ? $locale->text("Storno (one letter abbreviation)")
981 : $locale->text("Invoice (one letter abbreviation)");
984 $has_storno ? $locale->text("AP Transaction with Storno (abbreviation)")
985 : $is_storno ? $locale->text("AP Transaction Storno (one letter abbreviation)")
986 : $locale->text("AP Transaction (abbreviation)");
989 $ap->{direct_debit} = $ap->{direct_debit} ? $::locale->text('yes') : $::locale->text('no');
993 foreach my $column (@columns) {
995 'data' => $ap->{$column},
996 'align' => $column_alignment{$column},
1000 $row->{invnumber}->{link} = build_std_url("script=" . ($ap->{invoice} ? 'ir.pl' : 'ap.pl'), 'action=edit')
1001 . "&id=" . E($ap->{id}) . "&callback=${callback}";
1003 my $row_set = [ $row ];
1005 if (($form->{l_subtotal} eq 'Y')
1006 && (($idx == (scalar @{ $form->{AP} } - 1))
1007 || ($ap->{ $form->{sort} } ne $form->{AP}->[$idx + 1]->{ $form->{sort} }))) {
1008 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1011 $report->add_data($row_set);
1016 $report->add_separator();
1017 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1019 $report->generate_with_headers();
1021 $main::lxdebug->leave_sub();
1025 $main::lxdebug->enter_sub();
1027 my $form = $main::form;
1028 my %myconfig = %main::myconfig;
1029 my $locale = $main::locale;
1031 $main::auth->assert('ap_transactions');
1033 if (IS->has_storno(\%myconfig, $form, 'ap')) {
1034 $form->{title} = $locale->text("Cancel Accounts Payables Transaction");
1035 $form->error($locale->text("Transaction has already been cancelled!"));
1038 $form->error($locale->text('Cannot post storno for a closed period!'))
1039 if ( $form->date_closed($form->{transdate}, \%myconfig));
1041 AP->storno($form, \%myconfig, $form->{id});
1043 # saving the history
1044 if(!exists $form->{addition} && $form->{id} ne "") {
1045 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1046 $form->{addition} = "STORNO";
1047 $form->{what_done} = "invoice";
1048 $form->save_history;
1050 # /saving the history
1052 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1054 $main::lxdebug->leave_sub();