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 use SL::Locale::String qw(t8);
49 require "bin/mozilla/arap.pl";
50 require "bin/mozilla/common.pl";
51 require "bin/mozilla/reportgenerator.pl";
59 # this is for our long dates
60 # $locale->text('January')
61 # $locale->text('February')
62 # $locale->text('March')
63 # $locale->text('April')
64 # $locale->text('May ')
65 # $locale->text('June')
66 # $locale->text('July')
67 # $locale->text('August')
68 # $locale->text('September')
69 # $locale->text('October')
70 # $locale->text('November')
71 # $locale->text('December')
73 # this is for our short month
74 # $locale->text('Jan')
75 # $locale->text('Feb')
76 # $locale->text('Mar')
77 # $locale->text('Apr')
78 # $locale->text('May')
79 # $locale->text('Jun')
80 # $locale->text('Jul')
81 # $locale->text('Aug')
82 # $locale->text('Sep')
83 # $locale->text('Oct')
84 # $locale->text('Nov')
85 # $locale->text('Dec')
88 $main::lxdebug->enter_sub();
90 my $form = $main::form;
91 my %myconfig = %main::myconfig;
93 $main::auth->assert('ap_transactions');
95 $form->{title} = "Add";
97 $form->{callback} = "ap.pl?action=add" unless $form->{callback};
99 AP->get_transdate(\%myconfig, $form);
100 $form->{initial_transdate} = $form->{transdate};
101 create_links(dont_save => 1);
102 $form->{transdate} = $form->{initial_transdate};
106 $main::lxdebug->leave_sub();
110 $main::lxdebug->enter_sub();
112 my $form = $main::form;
114 $main::auth->assert('ap_transactions');
116 $form->{title} = "Edit";
121 $main::lxdebug->leave_sub();
125 $main::lxdebug->enter_sub();
127 my $form = $main::form;
129 $main::auth->assert('ap_transactions');
131 # get all files stored in the webdav folder
132 if ($form->{invnumber} && $::instance_conf->get_webdav) {
133 my $webdav = SL::Webdav->new(
134 type => 'accounts_payable',
135 number => $form->{invnumber},
137 my $webdav_path = $webdav->webdav_path;
138 my @all_objects = $webdav->get_all_objects;
139 @{ $form->{WEBDAV} } = map { { name => $_->filename,
141 link => File::Spec->catfile($_->full_filedescriptor),
147 $main::lxdebug->leave_sub();
151 $main::lxdebug->enter_sub();
155 my $form = $main::form;
156 my %myconfig = %main::myconfig;
158 $main::auth->assert('ap_transactions');
160 $form->create_links("AP", \%myconfig, "vendor");
162 if (!$params{dont_save}) {
163 %saved = map { ($_ => $form->{$_}) } qw(direct_debit taxincluded);
164 $saved{duedate} = $form->{duedate} if $form->{duedate};
165 $saved{currency} = $form->{currency} if $form->{currency};
166 $saved{taxincluded} = $form->{taxincluded} if $form->{taxincluded};
169 IR->get_vendor(\%myconfig, \%$form);
171 $form->{$_} = $saved{$_} for keys %saved;
172 $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
173 $form->{rowcount} = 1;
175 # build the popup menus
176 $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
179 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
181 $form->{selectcurrency} = "";
182 map { my $quoted = H($_); $form->{selectcurrency} .= "<option value=\"${quoted}\">${quoted}\n" } $form->get_all_currencies(\%myconfig);
185 if (@{ $form->{all_vendor} || [] }) {
186 $form->{vendor} = qq|$form->{vendor}--$form->{vendor_id}|;
187 map { my $quoted = H($_->{name} . "--" . $_->{id}); $form->{selectvendor} .= "<option value=\"${quoted}\">${quoted}\n" }
188 (@{ $form->{all_vendor} });
191 $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
193 $form->{employee} = "$form->{employee}--$form->{employee_id}";
195 AP->setup_form($form);
198 ($form->datetonum($form->{transdate}, \%myconfig) <=
199 $form->datetonum($form->{closedto}, \%myconfig));
201 $main::lxdebug->leave_sub();
205 my @fields = qw(acc_trans_id gldate datepaid source memo paid AP_paid paid_project_id);
207 grep { $_->{paid} != 0 }
210 +{ map { ($_ => delete($::form->{"${_}_${idx}"})) } @fields }
211 } (1..$::form->{paidaccounts});
213 @payments = sort_by { DateTime->from_kivitendo($_->{datepaid}) } @payments;
215 $::form->{paidaccounts} = max scalar(@payments), 1;
217 foreach my $idx (1 .. scalar(@payments)) {
218 my $payment = $payments[$idx - 1];
219 $::form->{"${_}_${idx}"} = $payment->{$_} for @fields;
224 $main::lxdebug->enter_sub();
226 my $form = $main::form;
227 my %myconfig = %main::myconfig;
228 my $locale = $main::locale;
229 my $cgi = $::request->{cgi};
231 $main::auth->assert('ap_transactions');
233 $::form->{invoice_obj} = SL::DB::PurchaseInvoice->new(id => $::form->{id})->load if $::form->{id};
235 $form->{title_} = $form->{title};
236 $form->{title} = $form->{title} eq 'Add' ? $locale->text('Add Accounts Payables Transaction') : $locale->text('Edit Accounts Payables Transaction');
238 # type=submit $locale->text('Add Accounts Payables Transaction')
239 # type=submit $locale->text('Edit Accounts Payables Transaction')
241 # set option selected
242 foreach my $item (qw(vendor currency)) {
243 my $to_replace = H($form->{$item});
244 $form->{"select$item"} =~ s/ selected//;
245 $form->{"select$item"} =~ s/>\Q${to_replace}\E/ selected>${to_replace}/;
247 my $readonly = $form->{id} ? "readonly" : "";
249 $form->{radier} = ($::instance_conf->get_ap_changeable == 2)
250 ? ($form->current_date(\%myconfig) eq $form->{gldate})
251 : ($::instance_conf->get_ap_changeable == 1);
252 $readonly = $form->{radier} ? "" : $readonly;
254 $form->{readonly} = $readonly;
256 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
257 if ( $form->{forex} ) {
258 $form->{exchangerate} = $form->{forex};
262 $form->{exchangerate} = $form->{exchangerate} ? $form->format_amount(\%myconfig, $form->{exchangerate}) : '';
263 $form->{creditlimit} = $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
264 $form->{creditremaining} = $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
267 if (($rows = $form->numtextrows($form->{notes}, 50)) < 2) {
270 $form->{textarea_rows} = $rows;
272 $form->{creditremaining_plus} = ($form->{creditremaining} =~ /-/) ? "0" : "1";
274 my @old_project_ids = ();
277 if ($form->{"project_id_$_"}) {
278 push(@old_project_ids, $form->{"project_id_$_"});
281 (1..$form->{"rowcount"})
284 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
286 "old_id" => \@old_project_ids },
287 "charts" => { "key" => "ALL_CHARTS",
288 "transdate" => $form->{transdate} },
289 "taxcharts" => { "key" => "ALL_TAXCHARTS",
290 "module" => "AP" },);
293 { $_->{link_split} = [ split(/:/, $_->{link}) ]; }
294 @{ $form->{ALL_CHARTS} }
297 $form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
299 my %project_labels = ();
300 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
301 $project_labels{$item->{id}} = $item->{projectnumber};
307 foreach my $item (@{ $form->{ALL_CHARTS} }) {
308 if ( grep({ $_ eq 'AP_amount' } @{ $item->{link_split} }) ) {
309 if ( $taxchart_init eq '' ) {
310 $taxchart_init = $item->{tax_id};
313 push(@{ $form->{ALL_CHARTS_AP_amount} }, $item);
315 elsif ( grep({ $_ eq 'AP' } @{ $item->{link_split} }) ) {
316 push(@{ $form->{ALL_CHARTS_AP} }, $item);
318 elsif ( grep({ $_ eq 'AP_paid' } @{ $item->{link_split} }) ) {
319 push(@{ $form->{ALL_CHARTS_AP_paid} }, $item);
322 $charts{$item->{accno}} = $item;
326 foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
327 my $key = $item->{id} .'--'. $item->{rate};
329 if ( $taxchart_init eq $item->{id} ) {
330 $taxchart_init = $key;
333 $taxcharts{$item->{id}} = $item;
336 my $follow_up_vc = $form->{vendor};
337 $follow_up_vc =~ s/--.*?//;
338 my $follow_up_trans_info = "$form->{invnumber} ($follow_up_vc)";
340 $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
341 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
342 $form->{javascript} .= qq|<script type="text/javascript" src="js/follow_up.js"></script>|;
343 $form->{javascript} .= qq|<script type="text/javascript" src="js/kivi.Draft.js"></script>|;
347 for my $i (1 .. $form->{rowcount}) {
350 $form->{"amount_$i"} = $form->format_amount(\%myconfig, $form->{"amount_$i"}, 2);
351 $form->{"tax_$i"} = $form->format_amount(\%myconfig, $form->{"tax_$i"}, 2);
353 my $selected_accno_full;
354 my ($accno_row) = split(/--/, $form->{"AP_amount_$i"});
355 my $item = $charts{$accno_row};
356 $selected_accno_full = "$item->{accno}--$item->{tax_id}";
358 my $selected_taxchart = $form->{"taxchart_$i"};
359 my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full);
360 my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_AP_amount_$i"});
362 if ($previous_accno &&
363 ($previous_accno eq $selected_accno) &&
364 ($previous_tax_id ne $selected_tax_id)) {
365 my $item = $taxcharts{$selected_tax_id};
366 $selected_taxchart = "$item->{id}--$item->{rate}";
369 $selected_taxchart = $taxchart_init unless ($form->{"taxchart_$i"});
371 $form->{'selected_accno_full_'. $i} = $selected_accno_full;
373 $form->{'selected_taxchart_'. $i} = $selected_taxchart;
376 $form->{AP_amount_value_title_sub} = sub {
379 $item->{accno} .'--'. $item->{tax_id},
380 $item->{accno} .'--'. $item->{description},
384 $form->{taxchart_value_title_sub} = sub {
387 $item->{id} .'--'. $item->{rate},
388 $item->{taxdescription} .' '. ($item->{rate} * 100) .' %',
392 $form->{AP_paid_value_title_sub} = sub {
396 $item->{accno} .'--'. $item->{description}
400 $form->{APselected_value_title_sub} = sub {
404 $item->{accno} .'--'. $item->{description}
408 $form->{invtotal_unformatted} = $form->{invtotal};
409 $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
411 $form->{totalpaid} = 0;
415 if ( $form->{'paid_'. $form->{paidaccounts}} ) {
416 $form->{paidaccounts}++;
419 # default account for current assets (i.e. 1801 - SKR04)
420 $form->{accno_arap} = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
422 for my $i (1 .. $form->{paidaccounts}) {
423 $form->{totalpaid} += $form->{"paid_$i"};
426 if ($form->{"paid_$i"}) {
427 $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
429 if ($form->{"exchangerate_$i"} == 0) {
430 $form->{"exchangerate_$i"} = "";
432 $form->{"exchangerate_$i"} =
433 $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
437 if (SL::DB::Default->get->payments_changeable == 0) {
439 $changeable = ($form->{"acc_trans_id_$i"})? 0 : 1;
441 if (SL::DB::Default->get->payments_changeable == 2) {
443 $changeable = (($form->{"gldate_$i"} eq '') || $form->current_date(\%myconfig) eq $form->{"gldate_$i"});
446 #deaktivieren von gebuchten Zahlungen ausserhalb der Bücherkontrolle, vorher prüfen ob heute eingegeben
447 if ($form->date_closed($form->{"gldate_$i"})) {
451 $form->{'paidaccount_changeable_'. $i} = $changeable;
453 $form->{'labelpaid_project_id_'. $i} = $project_labels{$form->{'paid_project_id_'. $i}};
456 $form->{paid_missing} = $form->{invtotal_unformatted} - $form->{totalpaid};
458 print $form->parse_html_template('ap/form_header', {
459 today => DateTime->today,
462 $main::lxdebug->leave_sub();
466 $::lxdebug->enter_sub;
467 $::auth->assert('ap_transactions');
472 my $follow_ups = FU->follow_ups('trans_id' => $::form->{id}, 'not_done' => 1);
474 if (@{ $follow_ups }) {
475 $num_due = sum map { $_->{due} * 1 } @{ $follow_ups };
476 $num_follow_ups = scalar @{ $follow_ups }
480 my $transdate = $::form->datetonum($::form->{transdate}, \%::myconfig);
481 my $closedto = $::form->datetonum($::form->{closedto}, \%::myconfig);
483 my $storno = $::form->{id}
484 && !IS->has_storno(\%::myconfig, $::form, 'ap')
485 && !IS->is_storno( \%::myconfig, $::form, 'ap', $::form->{id})
486 && ($::form->{totalpaid} == 0 || $::form->{totalpaid} eq '');
489 print $::form->parse_html_template('ap/form_footer', {
491 num_follow_ups => $num_follow_ups,
492 show_post_draft => ($transdate > $closedto) && !$::form->{id},
493 show_storno => $storno,
496 $::lxdebug->leave_sub;
500 $main::lxdebug->enter_sub();
502 my $form = $main::form;
503 my %myconfig = %main::myconfig;
505 $main::auth->assert('ap_transactions');
507 &mark_as_paid_common(\%myconfig,"ap");
509 $main::lxdebug->leave_sub();
513 $::form->{transdate} = DateTime->today_local->to_kivitendo if !$::form->{transdate};
514 $::form->{gldate} = $::form->{transdate} if !$::form->{gldate};
519 $main::lxdebug->enter_sub();
521 my $form = $main::form;
522 my %myconfig = %main::myconfig;
524 $main::auth->assert('ap_transactions');
528 $form->{invtotal} = 0;
530 delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
532 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
533 qw(exchangerate creditlimit creditremaining);
535 my @flds = qw(amount AP_amount projectnumber oldprojectnumber project_id taxchart);
537 my (@a, $j, $totaltax);
538 for my $i (1 .. $form->{rowcount}) {
539 $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
540 if ($form->{"amount_$i"}) {
543 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
545 # calculate tax exactly the same way as AP in post_transaction via form->calculate_tax
547 ($tmpnetamount,$form->{"tax_$i"}) = $form->calculate_tax($form->{"amount_$i"},$rate,$form->{taxincluded},2);
549 $totaltax += $form->{"tax_$i"};
550 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
554 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
556 map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
558 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
559 $form->{exchangerate} = $form->{forex} if $form->{forex};
561 $form->{invdate} = $form->{transdate};
562 my %saved_variables = map +( $_ => $form->{$_} ), qw(AP AP_amount_1 taxchart_1 notes);
564 my $vendor_changed = &check_name("vendor");
566 $form->{AP} = $saved_variables{AP};
567 if ($saved_variables{AP_amount_1} =~ m/.--./) {
568 map { $form->{$_} = $saved_variables{$_} } qw(AP_amount_1 taxchart_1);
570 delete $form->{taxchart_1};
573 $form->{rowcount} = $count + 1;
576 ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
579 for my $i (1 .. $form->{paidaccounts}) {
580 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
583 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
584 } qw(paid exchangerate);
586 $totalpaid += $form->{"paid_$i"};
588 $form->{"forex_$i"} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
589 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
593 $form->{creditremaining} -=
594 ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} -
595 $form->{oldinvtotal});
596 $form->{oldinvtotal} = $form->{invtotal};
597 $form->{oldtotalpaid} = $totalpaid;
601 $main::lxdebug->leave_sub();
606 $main::lxdebug->enter_sub();
608 my $form = $main::form;
609 my %myconfig = %main::myconfig;
610 my $locale = $main::locale;
612 $main::auth->assert('ap_transactions');
613 $form->mtime_ischanged('ap');
615 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
617 my $invdate = $form->datetonum($form->{transdate}, \%myconfig);
619 for my $i (1 .. $form->{paidaccounts}) {
620 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
621 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
623 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
625 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
626 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
628 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
629 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
630 $form->error($locale->text('Cannot post payment for a closed period!'))
631 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
633 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
634 $form->{"exchangerate_$i"} = $form->{exchangerate}
635 if ($invdate == $datepaid);
636 $form->isblank("exchangerate_$i",
637 $locale->text('Exchangerate for payment missing!'));
642 ($form->{AP}) = split /--/, $form->{AP};
643 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
644 if (AP->post_payment(\%myconfig, \%$form)) {
645 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
646 $form->{what_done} = 'invoice';
647 $form->{addition} = "PAYMENT POSTED";
649 $form->redirect($locale->text('Payment posted!'))
651 $form->error($locale->text('Cannot post payment!'));
655 $main::lxdebug->leave_sub();
660 $main::lxdebug->enter_sub();
662 my $form = $main::form;
663 my %myconfig = %main::myconfig;
664 my $locale = $main::locale;
666 $main::auth->assert('ap_transactions');
667 $form->mtime_ischanged('ap');
671 # check if there is a vendor, invoice, due date and invnumber
672 $form->isblank("transdate", $locale->text("Invoice Date missing!"));
673 $form->isblank("duedate", $locale->text("Due Date missing!"));
674 $form->isblank("vendor", $locale->text('Vendor missing!'));
675 $form->isblank("invnumber", $locale->text('Invoice Number missing!'));
677 if ($myconfig{mandatory_departments} && !$form->{department_id}) {
678 $form->{saved_message} = $::locale->text('You have to specify a department.');
683 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
684 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
686 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
687 if ($form->date_max_future($form->{"transdate"}, \%myconfig));
688 $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
690 my $zero_amount_posting = 1;
691 for my $i (1 .. $form->{rowcount}) {
692 if ($form->parse_amount(\%myconfig, $form->{"amount_$i"})) {
693 $zero_amount_posting = 0;
698 $form->error($locale->text('Zero amount posting!')) if $zero_amount_posting;
700 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
701 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
704 for my $i (1 .. $form->{paidaccounts}) {
705 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
706 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
708 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
710 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
711 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
713 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
714 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
715 $form->error($locale->text('Cannot post payment for a closed period!'))
716 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
718 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
719 $form->{"exchangerate_$i"} = $form->{exchangerate}
720 if ($transdate == $datepaid);
721 $form->isblank("exchangerate_$i",
722 $locale->text('Exchangerate for payment missing!'));
728 # if old vendor ne vendor redo form
729 my ($vendor) = split /--/, $form->{vendor};
730 if ($form->{oldvendor} ne "$vendor--$form->{vendor_id}") {
732 $::dispatcher->end_request;
734 my ($debitaccno, $debittaxkey) = split /--/, $form->{AP_amountselected};
735 my ($taxkey, $NULL) = split /--/, $form->{taxchartselected};
736 my ($payablesaccno, $payablestaxkey) = split /--/, $form->{APselected};
737 # $form->{AP_amount_1} = $debitaccno;
738 $form->{AP_payables} = $payablesaccno;
739 $form->{taxkey} = $taxkey;
742 $form->{id} = 0 if $form->{postasnew};
744 if (AP->post_transaction(\%myconfig, \%$form)) {
745 # create webdav folder
746 if ($::instance_conf->get_webdav) {
747 SL::Webdav->new(type => 'accounts_payable',
748 number => $form->{invnumber},
752 if(!exists $form->{addition} && $form->{id} ne "") {
753 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
754 $form->{addition} = "POSTED";
755 $form->{what_done} = "invoice";
758 # /saving the history
759 # Dieser Text wird niemals ausgegeben: Probleme beim redirect?
760 $form->redirect($locale->text('Transaction posted!')) unless $inline;
762 $form->error($locale->text('Cannot post transaction!'));
765 $main::lxdebug->leave_sub();
769 $main::lxdebug->enter_sub();
771 my $form = $main::form;
772 my %myconfig = %main::myconfig;
774 $main::auth->assert('ap_transactions');
776 $form->{postasnew} = 1;
778 if(!exists $form->{addition} && $form->{id} ne "") {
779 # does this work? post_as_new for ap doesn't immediately save the
780 # invoice, because the invnumber has to be entered by hand.
781 # And the value of $form->{postasnew} isn't checked when calling post
782 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
783 $form->{addition} = "POSTED AS NEW";
784 $form->{what_done} = "invoice";
787 # /saving the history
790 $main::lxdebug->leave_sub();
794 $main::lxdebug->enter_sub();
796 my $form = $main::form;
797 my %myconfig = %main::myconfig;
799 $main::auth->assert('ap_transactions');
801 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);
802 $form->{paidaccounts} = 1;
804 $form->{invdate} = $form->current_date(\%myconfig);
807 $main::lxdebug->leave_sub();
811 $main::lxdebug->enter_sub();
813 my $form = $main::form;
814 my $locale = $main::locale;
816 $main::auth->assert('ap_transactions');
818 $form->{title} = $locale->text('Confirm!');
822 delete $form->{header};
825 <form method=post action=$form->{script}>
828 foreach my $key (keys %$form) {
829 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
830 $form->{$key} =~ s/\"/"/g;
831 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
835 <h2 class=confirm>$form->{title}</h2>
838 . $locale->text('Are you sure you want to delete Transaction')
839 . qq| $form->{invnumber}</h4>
841 <input name=action class=submit type=submit value="|
842 . $locale->text('Yes') . qq|">
846 $main::lxdebug->leave_sub();
850 $main::lxdebug->enter_sub();
852 my $form = $main::form;
853 my %myconfig = %main::myconfig;
854 my $locale = $main::locale;
856 $main::auth->assert('ap_transactions');
858 if (AP->delete_transaction(\%myconfig, \%$form)) {
860 if(!exists $form->{addition}) {
861 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
862 $form->{addition} = "DELETED";
863 $form->{what_done} = "invoice";
866 # /saving the history
867 $form->redirect($locale->text('Transaction deleted!'));
869 $form->error($locale->text('Cannot delete transaction!'));
871 $main::lxdebug->leave_sub();
875 $main::lxdebug->enter_sub();
877 $main::auth->assert('vendor_invoice_edit');
879 my $form = $main::form;
880 my %myconfig = %main::myconfig;
881 my $locale = $main::locale;
883 # setup customer selection
884 $form->all_vc(\%myconfig, "vendor", "AP");
886 $form->{title} = $locale->text('AP Transactions');
888 $form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 },
889 "vendors" => "ALL_VC");
891 $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
892 # constants and subs for template
893 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
896 print $form->parse_html_template('ap/search', { %myconfig });
898 $main::lxdebug->leave_sub();
901 sub create_subtotal_row {
902 $main::lxdebug->enter_sub();
904 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
906 my $form = $main::form;
907 my %myconfig = %main::myconfig;
909 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
911 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
913 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
915 map { $totals->{$_} = 0 } @{ $subtotal_columns };
917 $main::lxdebug->leave_sub();
922 sub ap_transactions {
923 $main::lxdebug->enter_sub();
925 my $form = $main::form;
926 my %myconfig = %main::myconfig;
927 my $locale = $main::locale;
929 $main::auth->assert('vendor_invoice_edit');
931 ($form->{vendor}, $form->{vendor_id}) = split(/--/, $form->{vendor});
933 report_generator_set_default_sort('transdate', 1);
935 AP->ap_transactions(\%myconfig, \%$form);
937 $form->{title} = $locale->text('AP Transactions');
939 my $report = SL::ReportGenerator->new(\%myconfig, $form);
942 qw(transdate id type invnumber ordnumber name netamount tax amount paid datepaid
943 due duedate transaction_description notes employee globalprojectnumber
944 vendornumber country ustid taxzone payment_terms charts direct_debit);
946 my @hidden_variables = map { "l_${_}" } @columns;
947 push @hidden_variables, "l_subtotal", qw(open closed vendor invnumber ordnumber transaction_description notes project_id transdatefrom transdateto
948 parts_partnumber parts_description);
950 my $href = build_std_url('action=ap_transactions', grep { $form->{$_} } @hidden_variables);
953 'transdate' => { 'text' => $locale->text('Date'), },
954 'id' => { 'text' => $locale->text('ID'), },
955 'type' => { 'text' => $locale->text('Type'), },
956 'invnumber' => { 'text' => $locale->text('Invoice'), },
957 'ordnumber' => { 'text' => $locale->text('Order'), },
958 'name' => { 'text' => $locale->text('Vendor'), },
959 'netamount' => { 'text' => $locale->text('Amount'), },
960 'tax' => { 'text' => $locale->text('Tax'), },
961 'amount' => { 'text' => $locale->text('Total'), },
962 'paid' => { 'text' => $locale->text('Paid'), },
963 'datepaid' => { 'text' => $locale->text('Date Paid'), },
964 'due' => { 'text' => $locale->text('Amount Due'), },
965 'duedate' => { 'text' => $locale->text('Due Date'), },
966 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
967 'notes' => { 'text' => $locale->text('Notes'), },
968 'employee' => { 'text' => $locale->text('Employee'), },
969 'globalprojectnumber' => { 'text' => $locale->text('Document Project Number'), },
970 'vendornumber' => { 'text' => $locale->text('Vendor Number'), },
971 'country' => { 'text' => $locale->text('Country'), },
972 'ustid' => { 'text' => $locale->text('USt-IdNr.'), },
973 'taxzone' => { 'text' => $locale->text('Tax rate'), },
974 'payment_terms' => { 'text' => $locale->text('Payment Terms'), },
975 'charts' => { 'text' => $locale->text('Buchungskonto'), },
976 'direct_debit' => { 'text' => $locale->text('direct debit'), },
979 foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid employee shippingpoint shipvia transaction_description direct_debit)) {
980 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
981 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
984 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
986 $form->{"l_type"} = "Y";
987 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
989 $report->set_columns(%column_defs);
990 $report->set_column_order(@columns);
992 $report->set_export_options('ap_transactions', @hidden_variables, qw(sort sortdir));
994 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
997 push @options, $locale->text('Vendor') . " : $form->{vendor}" if ($form->{vendor});
998 push @options, $locale->text('Contact Person') . " : $form->{cp_name}" if ($form->{cp_name});
999 push @options, $locale->text('Department') . " : $form->{department}" if ($form->{department});
1000 push @options, $locale->text('Invoice Number') . " : $form->{invnumber}" if ($form->{invnumber});
1001 push @options, $locale->text('Order Number') . " : $form->{ordnumber}" if ($form->{ordnumber});
1002 push @options, $locale->text('Notes') . " : $form->{notes}" if ($form->{notes});
1003 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}" if ($form->{transaction_description});
1004 push @options, $locale->text('Part Description') . " : $form->{parts_description}" if $form->{parts_description};
1005 push @options, $locale->text('Part Number') . " : $form->{parts_partnumber}" if $form->{parts_partnumber};
1006 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1) if ($form->{transdatefrom});
1007 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1) if ($form->{transdateto});
1008 push @options, $locale->text('Open') if ($form->{open});
1009 push @options, $locale->text('Closed') if ($form->{closed});
1011 $report->set_options('top_info_text' => join("\n", @options),
1012 'raw_bottom_info_text' => $form->parse_html_template('ap/ap_transactions_bottom'),
1013 'output_format' => 'HTML',
1014 'title' => $form->{title},
1015 'attachment_basename' => $locale->text('vendor_invoice_list') . strftime('_%Y%m%d', localtime time),
1017 $report->set_options_from_form();
1018 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1020 # add sort and escape callback, this one we use for the add sub
1021 $form->{callback} = $href .= "&sort=$form->{sort}";
1023 # escape callback for href
1024 my $callback = $form->escape($href);
1026 my @subtotal_columns = qw(netamount amount paid due);
1028 my %totals = map { $_ => 0 } @subtotal_columns;
1029 my %subtotals = map { $_ => 0 } @subtotal_columns;
1033 foreach my $ap (@{ $form->{AP} }) {
1034 $ap->{tax} = $ap->{amount} - $ap->{netamount};
1035 $ap->{due} = $ap->{amount} - $ap->{paid};
1037 map { $subtotals{$_} += $ap->{$_};
1038 $totals{$_} += $ap->{$_} } @subtotal_columns;
1040 map { $ap->{$_} = $form->format_amount(\%myconfig, $ap->{$_}, 2) } qw(netamount tax amount paid due);
1042 my $is_storno = $ap->{storno} && $ap->{storno_id};
1043 my $has_storno = $ap->{storno} && !$ap->{storno_id};
1045 if ($ap->{invoice}) {
1047 $has_storno ? $locale->text("Invoice with Storno (abbreviation)")
1048 : $is_storno ? $locale->text("Storno (one letter abbreviation)")
1049 : $locale->text("Invoice (one letter abbreviation)");
1052 $has_storno ? $locale->text("AP Transaction with Storno (abbreviation)")
1053 : $is_storno ? $locale->text("AP Transaction Storno (one letter abbreviation)")
1054 : $locale->text("AP Transaction (abbreviation)");
1057 $ap->{direct_debit} = $ap->{direct_debit} ? $::locale->text('yes') : $::locale->text('no');
1061 foreach my $column (@columns) {
1063 'data' => $ap->{$column},
1064 'align' => $column_alignment{$column},
1068 $row->{invnumber}->{link} = build_std_url("script=" . ($ap->{invoice} ? 'ir.pl' : 'ap.pl'), 'action=edit')
1069 . "&id=" . E($ap->{id}) . "&callback=${callback}";
1071 my $row_set = [ $row ];
1073 if (($form->{l_subtotal} eq 'Y')
1074 && (($idx == (scalar @{ $form->{AP} } - 1))
1075 || ($ap->{ $form->{sort} } ne $form->{AP}->[$idx + 1]->{ $form->{sort} }))) {
1076 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1079 $report->add_data($row_set);
1084 $report->add_separator();
1085 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1087 $report->generate_with_headers();
1089 $main::lxdebug->leave_sub();
1093 $main::lxdebug->enter_sub();
1095 my $form = $main::form;
1096 my %myconfig = %main::myconfig;
1097 my $locale = $main::locale;
1099 $main::auth->assert('ap_transactions');
1101 if (IS->has_storno(\%myconfig, $form, 'ap')) {
1102 $form->{title} = $locale->text("Cancel Accounts Payables Transaction");
1103 $form->error($locale->text("Transaction has already been cancelled!"));
1106 $form->error($locale->text('Cannot post storno for a closed period!'))
1107 if ( $form->date_closed($form->{transdate}, \%myconfig));
1109 AP->storno($form, \%myconfig, $form->{id});
1111 # saving the history
1112 if(!exists $form->{addition} && $form->{id} ne "") {
1113 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1114 $form->{addition} = "STORNO";
1115 $form->{what_done} = "invoice";
1116 $form->save_history;
1118 # /saving the history
1120 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1122 $main::lxdebug->leave_sub();