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;
46 use SL::DB::PurchaseInvoice;
48 use SL::Locale::String qw(t8);
50 require "bin/mozilla/arap.pl";
51 require "bin/mozilla/common.pl";
52 require "bin/mozilla/reportgenerator.pl";
60 # this is for our long dates
61 # $locale->text('January')
62 # $locale->text('February')
63 # $locale->text('March')
64 # $locale->text('April')
65 # $locale->text('May ')
66 # $locale->text('June')
67 # $locale->text('July')
68 # $locale->text('August')
69 # $locale->text('September')
70 # $locale->text('October')
71 # $locale->text('November')
72 # $locale->text('December')
74 # this is for our short month
75 # $locale->text('Jan')
76 # $locale->text('Feb')
77 # $locale->text('Mar')
78 # $locale->text('Apr')
79 # $locale->text('May')
80 # $locale->text('Jun')
81 # $locale->text('Jul')
82 # $locale->text('Aug')
83 # $locale->text('Sep')
84 # $locale->text('Oct')
85 # $locale->text('Nov')
86 # $locale->text('Dec')
89 $main::lxdebug->enter_sub();
91 my $form = $main::form;
92 my %myconfig = %main::myconfig;
94 $main::auth->assert('ap_transactions');
96 $form->{title} = "Add";
98 $form->{callback} = "ap.pl?action=add" unless $form->{callback};
100 AP->get_transdate(\%myconfig, $form);
101 $form->{initial_transdate} = $form->{transdate};
102 create_links(dont_save => 1);
103 $form->{transdate} = $form->{initial_transdate};
105 if ($form->{vendor_id}) {
106 my $last_used_ap_chart = SL::DB::Vendor->load_cached($form->{vendor_id})->last_used_ap_chart;
107 $form->{"AP_amount_chart_id_1"} = $last_used_ap_chart->id if $last_used_ap_chart;
112 $main::lxdebug->leave_sub();
116 $main::lxdebug->enter_sub();
118 my $form = $main::form;
120 $main::auth->assert('ap_transactions');
122 $form->{title} = "Edit";
127 $main::lxdebug->leave_sub();
131 $main::lxdebug->enter_sub();
133 my $form = $main::form;
135 $main::auth->assert('ap_transactions');
137 # get all files stored in the webdav folder
138 if ($form->{invnumber} && $::instance_conf->get_webdav) {
139 my $webdav = SL::Webdav->new(
140 type => 'accounts_payable',
141 number => $form->{invnumber},
143 my $webdav_path = $webdav->webdav_path;
144 my @all_objects = $webdav->get_all_objects;
145 @{ $form->{WEBDAV} } = map { { name => $_->filename,
147 link => File::Spec->catfile($_->full_filedescriptor),
153 $main::lxdebug->leave_sub();
157 $main::lxdebug->enter_sub();
161 my $form = $main::form;
162 my %myconfig = %main::myconfig;
164 $main::auth->assert('ap_transactions');
166 $form->create_links("AP", \%myconfig, "vendor");
168 if (!$params{dont_save}) {
169 %saved = map { ($_ => $form->{$_}) } qw(direct_debit taxincluded);
170 $saved{duedate} = $form->{duedate} if $form->{duedate};
171 $saved{currency} = $form->{currency} if $form->{currency};
172 $saved{taxincluded} = $form->{taxincluded} if $form->{taxincluded};
175 IR->get_vendor(\%myconfig, \%$form);
177 $form->{$_} = $saved{$_} for keys %saved;
178 $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
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->{selectcurrency} = "";
189 map { my $quoted = H($_); $form->{selectcurrency} .= "<option value=\"${quoted}\">${quoted}\n" } $form->get_all_currencies(\%myconfig);
192 if (@{ $form->{all_vendor} || [] }) {
193 $form->{vendor} = qq|$form->{vendor}--$form->{vendor_id}|;
194 map { my $quoted = H($_->{name} . "--" . $_->{id}); $form->{selectvendor} .= "<option value=\"${quoted}\">${quoted}\n" }
195 (@{ $form->{all_vendor} });
198 $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
200 $form->{employee} = "$form->{employee}--$form->{employee_id}";
202 AP->setup_form($form);
205 ($form->datetonum($form->{transdate}, \%myconfig) <=
206 $form->datetonum($form->{closedto}, \%myconfig));
208 $main::lxdebug->leave_sub();
212 my @fields = qw(acc_trans_id gldate datepaid source memo paid AP_paid paid_project_id);
214 grep { $_->{paid} != 0 }
217 +{ map { ($_ => delete($::form->{"${_}_${idx}"})) } @fields }
218 } (1..$::form->{paidaccounts});
220 @payments = sort_by { DateTime->from_kivitendo($_->{datepaid}) } @payments;
222 $::form->{paidaccounts} = max scalar(@payments), 1;
224 foreach my $idx (1 .. scalar(@payments)) {
225 my $payment = $payments[$idx - 1];
226 $::form->{"${_}_${idx}"} = $payment->{$_} for @fields;
231 $main::lxdebug->enter_sub();
233 my $form = $main::form;
234 my %myconfig = %main::myconfig;
235 my $locale = $main::locale;
236 my $cgi = $::request->{cgi};
238 $main::auth->assert('ap_transactions');
240 $::form->{invoice_obj} = SL::DB::PurchaseInvoice->new(id => $::form->{id})->load if $::form->{id};
242 $form->{initial_focus} = !($form->{amount_1} * 1) ? 'vendor' : 'row_' . $form->{rowcount};
244 $form->{title_} = $form->{title};
245 $form->{title} = $form->{title} eq 'Add' ? $locale->text('Add Accounts Payables Transaction') : $locale->text('Edit Accounts Payables Transaction');
247 # type=submit $locale->text('Add Accounts Payables Transaction')
248 # type=submit $locale->text('Edit Accounts Payables Transaction')
250 # set option selected
251 foreach my $item (qw(vendor currency)) {
252 my $to_replace = H($form->{$item});
253 $form->{"select$item"} =~ s/ selected//;
254 $form->{"select$item"} =~ s/>\Q${to_replace}\E/ selected>${to_replace}/;
256 my $readonly = $form->{id} ? "readonly" : "";
258 $form->{radier} = ($::instance_conf->get_ap_changeable == 2)
259 ? ($form->current_date(\%myconfig) eq $form->{gldate})
260 : ($::instance_conf->get_ap_changeable == 1);
261 $readonly = $form->{radier} ? "" : $readonly;
263 $form->{readonly} = $readonly;
265 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
266 if ( $form->{forex} ) {
267 $form->{exchangerate} = $form->{forex};
271 $form->{exchangerate} = $form->{exchangerate} ? $form->format_amount(\%myconfig, $form->{exchangerate}) : '';
272 $form->{creditlimit} = $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
273 $form->{creditremaining} = $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
276 if (($rows = $form->numtextrows($form->{notes}, 50)) < 2) {
279 $form->{textarea_rows} = $rows;
281 $form->{creditremaining_plus} = ($form->{creditremaining} =~ /-/) ? "0" : "1";
283 my @old_project_ids = ();
286 if ($form->{"project_id_$_"}) {
287 push(@old_project_ids, $form->{"project_id_$_"});
290 (1..$form->{"rowcount"})
293 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
295 "old_id" => \@old_project_ids },
296 "charts" => { "key" => "ALL_CHARTS",
297 "transdate" => $form->{transdate} },
298 "taxcharts" => { "key" => "ALL_TAXCHARTS",
299 "module" => "AP" },);
302 { $_->{link_split} = [ split(/:/, $_->{link}) ]; }
303 @{ $form->{ALL_CHARTS} }
306 $form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
308 my %project_labels = ();
309 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
310 $project_labels{$item->{id}} = $item->{projectnumber};
314 my $default_ap_amount_chart_id;
316 foreach my $item (@{ $form->{ALL_CHARTS} }) {
317 if ( grep({ $_ eq 'AP_amount' } @{ $item->{link_split} }) ) {
318 $default_ap_amount_chart_id //= $item->{id};
320 } elsif ( grep({ $_ eq 'AP_paid' } @{ $item->{link_split} }) ) {
321 push(@{ $form->{ALL_CHARTS_AP_paid} }, $item);
324 $charts{$item->{accno}} = $item;
328 foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
329 $taxcharts{$item->{id}} = $item;
332 my $follow_up_vc = $form->{vendor};
333 $follow_up_vc =~ s/--.*?//;
334 my $follow_up_trans_info = "$form->{invnumber} ($follow_up_vc)";
336 $::request->layout->add_javascripts("autocomplete_chart.js", "show_vc_details.js", "show_history.js", "follow_up.js", "kivi.Draft.js", "kivi.GL.js");
337 my $transdate = $::form->{transdate} ? DateTime->from_kivitendo($::form->{transdate}) : DateTime->today_local;
342 for my $i (1 .. $form->{rowcount}) {
345 $form->{"amount_$i"} = $form->format_amount(\%myconfig, $form->{"amount_$i"}, 2);
346 $form->{"tax_$i"} = $form->format_amount(\%myconfig, $form->{"tax_$i"}, 2);
348 my ($default_taxchart, $taxchart_to_use);
349 my $amount_chart_id = $form->{"AP_amount_chart_id_$i"} || $default_ap_amount_chart_id;
350 my $chart_has_changed = $::form->{"previous_AP_amount_chart_id_$i"} && ($amount_chart_id != $::form->{"previous_AP_amount_chart_id_$i"});
352 foreach my $item ( GL->get_active_taxes_for_chart($amount_chart_id, $transdate) ) {
353 my $key = $item->id . "--" . $item->rate;
354 $first_taxchart //= $item;
355 $default_taxchart = $item if $item->{is_default};
356 $taxchart_to_use = $item if $key eq $form->{"taxchart_$i"};
359 $taxchart_to_use = $default_taxchart // $first_taxchart if $chart_has_changed || !$taxchart_to_use;
360 my $selected_taxchart = $taxchart_to_use->id . '--' . $taxchart_to_use->rate;
361 $form->{"selected_taxchart_$i"} = $selected_taxchart;
362 $form->{"AP_amount_chart_id_$i"} = $amount_chart_id;
365 $form->{taxchart_value_title_sub} = sub {
368 $item->{id} .'--'. $item->{rate},
369 $item->{taxdescription} .' '. ($item->{rate} * 100) .' %',
373 $form->{AP_paid_value_title_sub} = sub {
377 $item->{accno} .'--'. $item->{description}
381 $form->{invtotal_unformatted} = $form->{invtotal};
382 $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
384 $form->{totalpaid} = 0;
388 if ( $form->{'paid_'. $form->{paidaccounts}} ) {
389 $form->{paidaccounts}++;
392 # default account for current assets (i.e. 1801 - SKR04)
393 $form->{accno_arap} = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
395 for my $i (1 .. $form->{paidaccounts}) {
396 $form->{totalpaid} += $form->{"paid_$i"};
399 if ($form->{"paid_$i"}) {
400 $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
402 if ($form->{"exchangerate_$i"} == 0) {
403 $form->{"exchangerate_$i"} = "";
405 $form->{"exchangerate_$i"} =
406 $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
410 if (SL::DB::Default->get->payments_changeable == 0) {
412 $changeable = ($form->{"acc_trans_id_$i"})? 0 : 1;
414 if (SL::DB::Default->get->payments_changeable == 2) {
416 $changeable = (($form->{"gldate_$i"} eq '') || $form->current_date(\%myconfig) eq $form->{"gldate_$i"});
419 #deaktivieren von gebuchten Zahlungen ausserhalb der Bücherkontrolle, vorher prüfen ob heute eingegeben
420 if ($form->date_closed($form->{"gldate_$i"})) {
424 $form->{'paidaccount_changeable_'. $i} = $changeable;
426 $form->{'labelpaid_project_id_'. $i} = $project_labels{$form->{'paid_project_id_'. $i}};
429 $form->{paid_missing} = $form->{invtotal_unformatted} - $form->{totalpaid};
431 print $form->parse_html_template('ap/form_header', {
432 today => DateTime->today,
435 $main::lxdebug->leave_sub();
439 $::lxdebug->enter_sub;
440 $::auth->assert('ap_transactions');
445 my $follow_ups = FU->follow_ups('trans_id' => $::form->{id}, 'not_done' => 1);
447 if (@{ $follow_ups }) {
448 $num_due = sum map { $_->{due} * 1 } @{ $follow_ups };
449 $num_follow_ups = scalar @{ $follow_ups }
453 my $transdate = $::form->datetonum($::form->{transdate}, \%::myconfig);
454 my $closedto = $::form->datetonum($::form->{closedto}, \%::myconfig);
456 my $storno = $::form->{id}
457 && !IS->has_storno(\%::myconfig, $::form, 'ap')
458 && !IS->is_storno( \%::myconfig, $::form, 'ap', $::form->{id})
459 && ($::form->{totalpaid} == 0 || $::form->{totalpaid} eq '');
462 print $::form->parse_html_template('ap/form_footer', {
464 num_follow_ups => $num_follow_ups,
465 show_post_draft => ($transdate > $closedto) && !$::form->{id},
466 show_storno => $storno,
469 $::lxdebug->leave_sub;
473 $::auth->assert('ap_transactions');
475 SL::DB::PurchaseInvoice->new(id => $::form->{id})->load->mark_as_paid;
477 $::form->redirect($::locale->text("Marked as paid"));
481 $::form->{transdate} = DateTime->today_local->to_kivitendo if !$::form->{transdate};
482 $::form->{gldate} = $::form->{transdate} if !$::form->{gldate};
487 $main::lxdebug->enter_sub();
489 my $form = $main::form;
490 my %myconfig = %main::myconfig;
492 $main::auth->assert('ap_transactions');
496 $form->{invtotal} = 0;
498 delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
500 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
501 qw(exchangerate creditlimit creditremaining);
503 my @flds = qw(amount AP_amount projectnumber oldprojectnumber project_id taxchart);
505 my (@a, $j, $totaltax);
506 for my $i (1 .. $form->{rowcount}) {
507 $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
508 if ($form->{"amount_$i"}) {
511 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
513 # calculate tax exactly the same way as AP in post_transaction via form->calculate_tax
515 ($tmpnetamount,$form->{"tax_$i"}) = $form->calculate_tax($form->{"amount_$i"},$rate,$form->{taxincluded},2);
517 $totaltax += $form->{"tax_$i"};
518 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
522 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
524 map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
526 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
527 $form->{exchangerate} = $form->{forex} if $form->{forex};
529 $form->{invdate} = $form->{transdate};
531 my $vendor_changed = &check_name("vendor");
533 $form->{rowcount} = $count + 1;
536 ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
539 for my $i (1 .. $form->{paidaccounts}) {
540 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
543 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
544 } qw(paid exchangerate);
546 $totalpaid += $form->{"paid_$i"};
548 $form->{"forex_$i"} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
549 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
553 $form->{creditremaining} -=
554 ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} -
555 $form->{oldinvtotal});
556 $form->{oldinvtotal} = $form->{invtotal};
557 $form->{oldtotalpaid} = $totalpaid;
561 $main::lxdebug->leave_sub();
566 $main::lxdebug->enter_sub();
568 my $form = $main::form;
569 my %myconfig = %main::myconfig;
570 my $locale = $main::locale;
572 $main::auth->assert('ap_transactions');
573 $form->mtime_ischanged('ap');
575 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
577 my $invdate = $form->datetonum($form->{transdate}, \%myconfig);
579 for my $i (1 .. $form->{paidaccounts}) {
580 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
581 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
583 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
585 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
586 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
588 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
589 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
590 $form->error($locale->text('Cannot post payment for a closed period!'))
591 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
593 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
594 $form->{"exchangerate_$i"} = $form->{exchangerate}
595 if ($invdate == $datepaid);
596 $form->isblank("exchangerate_$i",
597 $locale->text('Exchangerate for payment missing!'));
602 ($form->{AP}) = split /--/, $form->{AP};
603 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
604 if (AP->post_payment(\%myconfig, \%$form)) {
605 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
606 $form->{what_done} = 'invoice';
607 $form->{addition} = "PAYMENT POSTED";
609 $form->redirect($locale->text('Payment posted!'))
611 $form->error($locale->text('Cannot post payment!'));
615 $main::lxdebug->leave_sub();
620 $main::lxdebug->enter_sub();
622 my $form = $main::form;
623 my %myconfig = %main::myconfig;
624 my $locale = $main::locale;
626 $main::auth->assert('ap_transactions');
627 $form->mtime_ischanged('ap');
631 # check if there is a vendor, invoice, due date and invnumber
632 $form->isblank("transdate", $locale->text("Invoice Date missing!"));
633 $form->isblank("duedate", $locale->text("Due Date missing!"));
634 $form->isblank("vendor", $locale->text('Vendor missing!'));
635 $form->isblank("invnumber", $locale->text('Invoice Number missing!'));
637 if ($myconfig{mandatory_departments} && !$form->{department_id}) {
638 $form->{saved_message} = $::locale->text('You have to specify a department.');
643 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
644 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
646 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
647 if ($form->date_max_future($form->{"transdate"}, \%myconfig));
648 $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
650 my $zero_amount_posting = 1;
651 for my $i (1 .. $form->{rowcount}) {
652 if ($form->parse_amount(\%myconfig, $form->{"amount_$i"})) {
653 $zero_amount_posting = 0;
658 $form->error($locale->text('Zero amount posting!')) if $zero_amount_posting;
660 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
661 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
664 for my $i (1 .. $form->{paidaccounts}) {
665 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
666 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
668 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
670 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
671 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
673 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
674 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
675 $form->error($locale->text('Cannot post payment for a closed period!'))
676 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
678 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
679 $form->{"exchangerate_$i"} = $form->{exchangerate}
680 if ($transdate == $datepaid);
681 $form->isblank("exchangerate_$i",
682 $locale->text('Exchangerate for payment missing!'));
688 # if old vendor ne vendor redo form
689 my ($vendor) = split /--/, $form->{vendor};
690 if ($form->{oldvendor} ne "$vendor--$form->{vendor_id}") {
692 $::dispatcher->end_request;
696 $form->{id} = 0 if $form->{postasnew};
698 if (AP->post_transaction(\%myconfig, \%$form)) {
699 # create webdav folder
700 if ($::instance_conf->get_webdav) {
701 SL::Webdav->new(type => 'accounts_payable',
702 number => $form->{invnumber},
706 if(!exists $form->{addition} && $form->{id} ne "") {
707 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
708 $form->{addition} = "POSTED";
709 $form->{what_done} = "invoice";
712 # /saving the history
713 # Dieser Text wird niemals ausgegeben: Probleme beim redirect?
714 $form->redirect($locale->text('Transaction posted!')) unless $inline;
716 $form->error($locale->text('Cannot post transaction!'));
719 $main::lxdebug->leave_sub();
723 $main::lxdebug->enter_sub();
725 my $form = $main::form;
726 my %myconfig = %main::myconfig;
728 $main::auth->assert('ap_transactions');
730 $form->{postasnew} = 1;
732 if(!exists $form->{addition} && $form->{id} ne "") {
733 # does this work? post_as_new for ap doesn't immediately save the
734 # invoice, because the invnumber has to be entered by hand.
735 # And the value of $form->{postasnew} isn't checked when calling post
736 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
737 $form->{addition} = "POSTED AS NEW";
738 $form->{what_done} = "invoice";
741 # /saving the history
744 $main::lxdebug->leave_sub();
748 $main::lxdebug->enter_sub();
750 my $form = $main::form;
751 my %myconfig = %main::myconfig;
753 $main::auth->assert('ap_transactions');
755 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);
756 $form->{paidaccounts} = 1;
758 $form->{invdate} = $form->current_date(\%myconfig);
761 $main::lxdebug->leave_sub();
765 $main::lxdebug->enter_sub();
767 my $form = $main::form;
768 my $locale = $main::locale;
770 $main::auth->assert('ap_transactions');
772 $form->{title} = $locale->text('Confirm!');
776 delete $form->{header};
779 <form method=post action=$form->{script}>
782 foreach my $key (keys %$form) {
783 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
784 $form->{$key} =~ s/\"/"/g;
785 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
789 <h2 class=confirm>$form->{title}</h2>
792 . $locale->text('Are you sure you want to delete Transaction')
793 . qq| $form->{invnumber}</h4>
795 <input name=action class=submit type=submit value="|
796 . $locale->text('Yes') . qq|">
800 $main::lxdebug->leave_sub();
804 $main::lxdebug->enter_sub();
806 my $form = $main::form;
807 my %myconfig = %main::myconfig;
808 my $locale = $main::locale;
810 $main::auth->assert('ap_transactions');
812 if (AP->delete_transaction(\%myconfig, \%$form)) {
814 if(!exists $form->{addition}) {
815 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
816 $form->{addition} = "DELETED";
817 $form->{what_done} = "invoice";
820 # /saving the history
821 $form->redirect($locale->text('Transaction deleted!'));
823 $form->error($locale->text('Cannot delete transaction!'));
825 $main::lxdebug->leave_sub();
829 $main::lxdebug->enter_sub();
831 $main::auth->assert('vendor_invoice_edit');
833 my $form = $main::form;
834 my %myconfig = %main::myconfig;
835 my $locale = $main::locale;
837 # setup customer selection
838 $form->all_vc(\%myconfig, "vendor", "AP");
840 $form->{title} = $locale->text('AP Transactions');
842 $form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 },
843 "vendors" => "ALL_VC");
845 $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
846 # constants and subs for template
847 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
850 print $form->parse_html_template('ap/search', { %myconfig });
852 $main::lxdebug->leave_sub();
855 sub create_subtotal_row {
856 $main::lxdebug->enter_sub();
858 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
860 my $form = $main::form;
861 my %myconfig = %main::myconfig;
863 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
865 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
867 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
869 map { $totals->{$_} = 0 } @{ $subtotal_columns };
871 $main::lxdebug->leave_sub();
876 sub ap_transactions {
877 $main::lxdebug->enter_sub();
879 my $form = $main::form;
880 my %myconfig = %main::myconfig;
881 my $locale = $main::locale;
883 $main::auth->assert('vendor_invoice_edit');
885 ($form->{vendor}, $form->{vendor_id}) = split(/--/, $form->{vendor});
887 report_generator_set_default_sort('transdate', 1);
889 AP->ap_transactions(\%myconfig, \%$form);
891 $form->{title} = $locale->text('AP Transactions');
893 my $report = SL::ReportGenerator->new(\%myconfig, $form);
896 qw(transdate id type invnumber ordnumber name netamount tax amount paid datepaid
897 due duedate transaction_description notes employee globalprojectnumber
898 vendornumber country ustid taxzone payment_terms charts direct_debit);
900 my @hidden_variables = map { "l_${_}" } @columns;
901 push @hidden_variables, "l_subtotal", qw(open closed vendor invnumber ordnumber transaction_description notes project_id transdatefrom transdateto
902 parts_partnumber parts_description);
904 my $href = build_std_url('action=ap_transactions', grep { $form->{$_} } @hidden_variables);
907 'transdate' => { 'text' => $locale->text('Date'), },
908 'id' => { 'text' => $locale->text('ID'), },
909 'type' => { 'text' => $locale->text('Type'), },
910 'invnumber' => { 'text' => $locale->text('Invoice'), },
911 'ordnumber' => { 'text' => $locale->text('Order'), },
912 'name' => { 'text' => $locale->text('Vendor'), },
913 'netamount' => { 'text' => $locale->text('Amount'), },
914 'tax' => { 'text' => $locale->text('Tax'), },
915 'amount' => { 'text' => $locale->text('Total'), },
916 'paid' => { 'text' => $locale->text('Paid'), },
917 'datepaid' => { 'text' => $locale->text('Date Paid'), },
918 'due' => { 'text' => $locale->text('Amount Due'), },
919 'duedate' => { 'text' => $locale->text('Due Date'), },
920 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
921 'notes' => { 'text' => $locale->text('Notes'), },
922 'employee' => { 'text' => $locale->text('Employee'), },
923 'globalprojectnumber' => { 'text' => $locale->text('Document Project Number'), },
924 'vendornumber' => { 'text' => $locale->text('Vendor Number'), },
925 'country' => { 'text' => $locale->text('Country'), },
926 'ustid' => { 'text' => $locale->text('USt-IdNr.'), },
927 'taxzone' => { 'text' => $locale->text('Tax rate'), },
928 'payment_terms' => { 'text' => $locale->text('Payment Terms'), },
929 'charts' => { 'text' => $locale->text('Buchungskonto'), },
930 'direct_debit' => { 'text' => $locale->text('direct debit'), },
933 foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid employee shippingpoint shipvia transaction_description direct_debit)) {
934 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
935 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
938 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
940 $form->{"l_type"} = "Y";
941 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
943 $report->set_columns(%column_defs);
944 $report->set_column_order(@columns);
946 $report->set_export_options('ap_transactions', @hidden_variables, qw(sort sortdir));
948 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
951 push @options, $locale->text('Vendor') . " : $form->{vendor}" if ($form->{vendor});
952 push @options, $locale->text('Contact Person') . " : $form->{cp_name}" if ($form->{cp_name});
953 push @options, $locale->text('Department') . " : $form->{department}" if ($form->{department});
954 push @options, $locale->text('Invoice Number') . " : $form->{invnumber}" if ($form->{invnumber});
955 push @options, $locale->text('Order Number') . " : $form->{ordnumber}" if ($form->{ordnumber});
956 push @options, $locale->text('Notes') . " : $form->{notes}" if ($form->{notes});
957 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}" if ($form->{transaction_description});
958 push @options, $locale->text('Part Description') . " : $form->{parts_description}" if $form->{parts_description};
959 push @options, $locale->text('Part Number') . " : $form->{parts_partnumber}" if $form->{parts_partnumber};
960 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1) if ($form->{transdatefrom});
961 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1) if ($form->{transdateto});
962 push @options, $locale->text('Open') if ($form->{open});
963 push @options, $locale->text('Closed') if ($form->{closed});
965 $report->set_options('top_info_text' => join("\n", @options),
966 'raw_bottom_info_text' => $form->parse_html_template('ap/ap_transactions_bottom'),
967 'output_format' => 'HTML',
968 'title' => $form->{title},
969 'attachment_basename' => $locale->text('vendor_invoice_list') . strftime('_%Y%m%d', localtime time),
971 $report->set_options_from_form();
972 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
974 # add sort and escape callback, this one we use for the add sub
975 $form->{callback} = $href .= "&sort=$form->{sort}";
977 # escape callback for href
978 my $callback = $form->escape($href);
980 my @subtotal_columns = qw(netamount amount paid due);
982 my %totals = map { $_ => 0 } @subtotal_columns;
983 my %subtotals = map { $_ => 0 } @subtotal_columns;
987 foreach my $ap (@{ $form->{AP} }) {
988 $ap->{tax} = $ap->{amount} - $ap->{netamount};
989 $ap->{due} = $ap->{amount} - $ap->{paid};
991 map { $subtotals{$_} += $ap->{$_};
992 $totals{$_} += $ap->{$_} } @subtotal_columns;
994 map { $ap->{$_} = $form->format_amount(\%myconfig, $ap->{$_}, 2) } qw(netamount tax amount paid due);
996 my $is_storno = $ap->{storno} && $ap->{storno_id};
997 my $has_storno = $ap->{storno} && !$ap->{storno_id};
999 if ($ap->{invoice}) {
1001 $has_storno ? $locale->text("Invoice with Storno (abbreviation)")
1002 : $is_storno ? $locale->text("Storno (one letter abbreviation)")
1003 : $locale->text("Invoice (one letter abbreviation)");
1006 $has_storno ? $locale->text("AP Transaction with Storno (abbreviation)")
1007 : $is_storno ? $locale->text("AP Transaction Storno (one letter abbreviation)")
1008 : $locale->text("AP Transaction (abbreviation)");
1011 $ap->{direct_debit} = $ap->{direct_debit} ? $::locale->text('yes') : $::locale->text('no');
1015 foreach my $column (@columns) {
1017 'data' => $ap->{$column},
1018 'align' => $column_alignment{$column},
1022 $row->{invnumber}->{link} = build_std_url("script=" . ($ap->{invoice} ? 'ir.pl' : 'ap.pl'), 'action=edit')
1023 . "&id=" . E($ap->{id}) . "&callback=${callback}";
1025 my $row_set = [ $row ];
1027 if (($form->{l_subtotal} eq 'Y')
1028 && (($idx == (scalar @{ $form->{AP} } - 1))
1029 || ($ap->{ $form->{sort} } ne $form->{AP}->[$idx + 1]->{ $form->{sort} }))) {
1030 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1033 $report->add_data($row_set);
1038 $report->add_separator();
1039 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1041 $report->generate_with_headers();
1043 $main::lxdebug->leave_sub();
1047 $main::lxdebug->enter_sub();
1049 my $form = $main::form;
1050 my %myconfig = %main::myconfig;
1051 my $locale = $main::locale;
1053 $main::auth->assert('ap_transactions');
1055 if (IS->has_storno(\%myconfig, $form, 'ap')) {
1056 $form->{title} = $locale->text("Cancel Accounts Payables Transaction");
1057 $form->error($locale->text("Transaction has already been cancelled!"));
1060 $form->error($locale->text('Cannot post storno for a closed period!'))
1061 if ( $form->date_closed($form->{transdate}, \%myconfig));
1063 AP->storno($form, \%myconfig, $form->{id});
1065 # saving the history
1066 if(!exists $form->{addition} && $form->{id} ne "") {
1067 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1068 $form->{addition} = "STORNO";
1069 $form->{what_done} = "invoice";
1070 $form->save_history;
1072 # /saving the history
1074 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1076 $main::lxdebug->leave_sub();