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(first max sum);
37 use List::UtilsBy qw(sort_by);
42 use SL::Helper::Flash qw(flash);
45 use SL::ReportGenerator;
48 use SL::DB::PurchaseInvoice;
49 use SL::DB::RecordTemplate;
52 use SL::Locale::String qw(t8);
54 require "bin/mozilla/common.pl";
55 require "bin/mozilla/reportgenerator.pl";
63 # this is for our long dates
64 # $locale->text('January')
65 # $locale->text('February')
66 # $locale->text('March')
67 # $locale->text('April')
68 # $locale->text('May ')
69 # $locale->text('June')
70 # $locale->text('July')
71 # $locale->text('August')
72 # $locale->text('September')
73 # $locale->text('October')
74 # $locale->text('November')
75 # $locale->text('December')
77 # this is for our short month
78 # $locale->text('Jan')
79 # $locale->text('Feb')
80 # $locale->text('Mar')
81 # $locale->text('Apr')
82 # $locale->text('May')
83 # $locale->text('Jun')
84 # $locale->text('Jul')
85 # $locale->text('Aug')
86 # $locale->text('Sep')
87 # $locale->text('Oct')
88 # $locale->text('Nov')
89 # $locale->text('Dec')
91 sub load_record_template {
92 $::auth->assert('ap_transactions');
94 # Load existing template and verify that its one for this module.
95 my $template = SL::DB::RecordTemplate
96 ->new(id => $::form->{id})
98 with_object => [ qw(customer payment currency record_items record_items.chart) ],
101 die "invalid template type" unless $template->template_type eq 'ap_transaction';
103 $template->substitute_variables;
105 # Clean the current $::form before rebuilding it from the template.
106 delete @{ $::form }{ grep { !m{^(?:script|login)$}i } keys %{ $::form } };
108 # Fill $::form from the template.
109 my $today = DateTime->today_local;
110 $::form->{title} = "Add";
111 $::form->{currency} = $template->currency->name;
112 $::form->{direct_debit} = $template->direct_debit;
113 $::form->{globalproject_id} = $template->project_id;
114 $::form->{AP_chart_id} = $template->ar_ap_chart_id;
115 $::form->{transdate} = $today->to_kivitendo;
116 $::form->{duedate} = $today->to_kivitendo;
117 $::form->{rowcount} = @{ $template->items };
118 $::form->{paidaccounts} = 1;
119 $::form->{$_} = $template->$_ for qw(department_id ordnumber taxincluded notes);
121 if ($template->vendor) {
122 $::form->{vendor_id} = $template->vendor_id;
123 $::form->{vendor} = $template->vendor->name;
124 $::form->{duedate} = $template->vendor->payment->calc_date(reference_date => $today)->to_kivitendo if $template->vendor->payment;
128 foreach my $item (@{ $template->items }) {
131 my $active_taxkey = $item->chart->get_active_taxkey;
132 my $taxes = SL::DB::Manager::Tax->get_all(
133 where => [ chart_categories => { like => '%' . $item->chart->category . '%' }],
134 sort_by => 'taxkey, rate',
137 my $tax = first { $item->tax_id == $_->id } @{ $taxes };
138 $tax //= first { $active_taxkey->tax_id == $_->id } @{ $taxes };
139 $tax //= $taxes->[0];
146 $::form->{"AP_amount_chart_id_${row}"} = $item->chart_id;
147 $::form->{"previous_AP_amount_chart_id_${row}"} = $item->chart_id;
148 $::form->{"amount_${row}"} = $::form->format_amount(\%::myconfig, $item->amount1, 2);
149 $::form->{"taxchart_${row}"} = $item->tax_id . '--' . $tax->rate;
150 $::form->{"project_id_${row}"} = $item->project_id;
153 flash('info', $::locale->text("The record template '#1' has been loaded.", $template->template_name));
156 keep_rows_without_amount => 1,
157 dont_add_new_row => 1,
161 sub save_record_template {
162 $::auth->assert('ap_transactions');
164 my $template = $::form->{record_template_id} ? SL::DB::RecordTemplate->new(id => $::form->{record_template_id})->load : SL::DB::RecordTemplate->new;
165 my $js = SL::ClientJS->new(controller => SL::Controller::Base->new);
166 my $new_name = $template->template_name_to_use($::form->{record_template_new_template_name});
168 $js->dialog->close('#record_template_dialog');
171 $_->{chart_id} && (($_->{tax_id} // '') ne '')
173 +{ chart_id => $::form->{"AP_amount_chart_id_${_}"},
174 amount1 => $::form->parse_amount(\%::myconfig, $::form->{"amount_${_}"}),
175 tax_id => (split m{--}, $::form->{"taxchart_${_}"})[0],
176 project_id => $::form->{"project_id_${_}"} || undef,
178 } (1..($::form->{rowcount} || 1));
180 $template->assign_attributes(
181 template_type => 'ap_transaction',
182 template_name => $new_name,
184 currency_id => SL::DB::Manager::Currency->find_by(name => $::form->{currency})->id,
185 ar_ap_chart_id => $::form->{AP_chart_id} || undef,
186 vendor_id => $::form->{vendor_id} || undef,
187 department_id => $::form->{department_id} || undef,
188 project_id => $::form->{globalproject_id} || undef,
189 taxincluded => $::form->{taxincluded} ? 1 : 0,
190 direct_debit => $::form->{direct_debit} ? 1 : 0,
191 ordnumber => $::form->{ordnumber},
192 notes => $::form->{notes},
202 ->flash('error', $::locale->text("Saving the record template '#1' failed.", $new_name))
207 ->flash('info', $::locale->text("The record template '#1' has been saved.", $new_name))
212 $main::lxdebug->enter_sub();
214 my $form = $main::form;
215 my %myconfig = %main::myconfig;
217 $main::auth->assert('ap_transactions');
219 $form->{title} = "Add";
221 $form->{callback} = "ap.pl?action=add" unless $form->{callback};
223 AP->get_transdate(\%myconfig, $form);
224 $form->{initial_transdate} = $form->{transdate};
225 create_links(dont_save => 1);
226 $form->{transdate} = $form->{initial_transdate};
228 if ($form->{vendor_id}) {
229 my $last_used_ap_chart = SL::DB::Vendor->load_cached($form->{vendor_id})->last_used_ap_chart;
230 $form->{"AP_amount_chart_id_1"} = $last_used_ap_chart->id if $last_used_ap_chart;
235 $main::lxdebug->leave_sub();
239 $main::lxdebug->enter_sub();
241 my $form = $main::form;
243 $main::auth->assert('ap_transactions');
245 $form->{title} = "Edit";
250 $main::lxdebug->leave_sub();
254 $main::lxdebug->enter_sub();
256 my $form = $main::form;
258 $main::auth->assert('ap_transactions');
260 # get all files stored in the webdav folder
261 if ($form->{invnumber} && $::instance_conf->get_webdav) {
262 my $webdav = SL::Webdav->new(
263 type => 'accounts_payable',
264 number => $form->{invnumber},
266 my $webdav_path = $webdav->webdav_path;
267 my @all_objects = $webdav->get_all_objects;
268 @{ $form->{WEBDAV} } = map { { name => $_->filename,
270 link => File::Spec->catfile($_->full_filedescriptor),
276 $main::lxdebug->leave_sub();
280 $main::lxdebug->enter_sub();
284 my $form = $main::form;
285 my %myconfig = %main::myconfig;
287 $main::auth->assert('ap_transactions');
289 $form->create_links("AP", \%myconfig, "vendor");
291 if (!$params{dont_save}) {
292 %saved = map { ($_ => $form->{$_}) } qw(direct_debit taxincluded);
293 $saved{duedate} = $form->{duedate} if $form->{duedate};
294 $saved{currency} = $form->{currency} if $form->{currency};
295 $saved{taxincluded} = $form->{taxincluded} if $form->{taxincluded};
298 IR->get_vendor(\%myconfig, \%$form);
300 $form->{$_} = $saved{$_} for keys %saved;
301 $form->{rowcount} = 1;
302 $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};
304 # build the popup menus
305 $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
308 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
310 $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
312 $form->{employee} = "$form->{employee}--$form->{employee_id}";
314 AP->setup_form($form);
317 ($form->datetonum($form->{transdate}, \%myconfig) <=
318 $form->datetonum($form->{closedto}, \%myconfig));
320 $main::lxdebug->leave_sub();
324 my @fields = qw(acc_trans_id gldate datepaid source memo paid AP_paid paid_project_id);
326 grep { $_->{paid} != 0 }
329 +{ map { ($_ => delete($::form->{"${_}_${idx}"})) } @fields }
330 } (1..$::form->{paidaccounts});
332 @payments = sort_by { DateTime->from_kivitendo($_->{datepaid}) } @payments;
334 $::form->{paidaccounts} = max scalar(@payments), 1;
336 foreach my $idx (1 .. scalar(@payments)) {
337 my $payment = $payments[$idx - 1];
338 $::form->{"${_}_${idx}"} = $payment->{$_} for @fields;
343 $main::lxdebug->enter_sub();
345 my $form = $main::form;
346 my %myconfig = %main::myconfig;
347 my $locale = $main::locale;
348 my $cgi = $::request->{cgi};
350 $main::auth->assert('ap_transactions');
352 $::form->{invoice_obj} = SL::DB::PurchaseInvoice->new(id => $::form->{id})->load if $::form->{id};
354 $form->{initial_focus} = !($form->{amount_1} * 1) ? 'vendor_id' : 'row_' . $form->{rowcount};
356 $form->{title_} = $form->{title};
357 $form->{title} = $form->{title} eq 'Add' ? $locale->text('Add Accounts Payables Transaction') : $locale->text('Edit Accounts Payables Transaction');
359 # type=submit $locale->text('Add Accounts Payables Transaction')
360 # type=submit $locale->text('Edit Accounts Payables Transaction')
362 my $readonly = $form->{id} ? "readonly" : "";
364 $form->{radier} = ($::instance_conf->get_ap_changeable == 2)
365 ? ($form->current_date(\%myconfig) eq $form->{gldate})
366 : ($::instance_conf->get_ap_changeable == 1);
367 $readonly = $form->{radier} ? "" : $readonly;
369 $form->{readonly} = $readonly;
371 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
372 if ( $form->{forex} ) {
373 $form->{exchangerate} = $form->{forex};
377 $form->{exchangerate} = $form->{exchangerate} ? $form->format_amount(\%myconfig, $form->{exchangerate}) : '';
378 $form->{creditlimit} = $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
379 $form->{creditremaining} = $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
382 if (($rows = $form->numtextrows($form->{notes}, 50)) < 2) {
385 $form->{textarea_rows} = $rows;
387 $form->{creditremaining_plus} = ($form->{creditremaining} =~ /-/) ? "0" : "1";
389 my @old_project_ids = ();
392 if ($form->{"project_id_$_"}) {
393 push(@old_project_ids, $form->{"project_id_$_"});
396 (1..$form->{"rowcount"})
399 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
401 "old_id" => \@old_project_ids },
402 "charts" => { "key" => "ALL_CHARTS",
403 "transdate" => $form->{transdate} },
404 "taxcharts" => { "key" => "ALL_TAXCHARTS",
405 "module" => "AP" },);
408 { $_->{link_split} = [ split(/:/, $_->{link}) ]; }
409 @{ $form->{ALL_CHARTS} }
412 $form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
414 my %project_labels = ();
415 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
416 $project_labels{$item->{id}} = $item->{projectnumber};
420 my $default_ap_amount_chart_id;
422 foreach my $item (@{ $form->{ALL_CHARTS} }) {
423 if ( grep({ $_ eq 'AP_amount' } @{ $item->{link_split} }) ) {
424 $default_ap_amount_chart_id //= $item->{id};
426 } elsif ( grep({ $_ eq 'AP_paid' } @{ $item->{link_split} }) ) {
427 push(@{ $form->{ALL_CHARTS_AP_paid} }, $item);
430 $charts{$item->{accno}} = $item;
433 my $follow_up_vc = $form->{vendor_id} ? SL::DB::Vendor->load_cached($form->{vendor_id})->name : '';
434 my $follow_up_trans_info = "$form->{invnumber} ($follow_up_vc)";
436 $::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", "kivi.RecordTemplate.js", "kivi.File.js");
437 my $transdate = $::form->{transdate} ? DateTime->from_kivitendo($::form->{transdate}) : DateTime->today_local;
442 for my $i (1 .. $form->{rowcount}) {
445 $form->{"amount_$i"} = $form->format_amount(\%myconfig, $form->{"amount_$i"}, 2);
446 $form->{"tax_$i"} = $form->format_amount(\%myconfig, $form->{"tax_$i"}, 2);
448 my ($default_taxchart, $taxchart_to_use);
449 my $amount_chart_id = $form->{"AP_amount_chart_id_$i"} || $default_ap_amount_chart_id;
450 my $chart_has_changed = $::form->{"previous_AP_amount_chart_id_$i"} && ($amount_chart_id != $::form->{"previous_AP_amount_chart_id_$i"});
451 my @taxcharts = GL->get_active_taxes_for_chart($amount_chart_id, $transdate);
453 foreach my $item (@taxcharts) {
454 my $key = $item->id . "--" . $item->rate;
455 $first_taxchart //= $item;
456 $default_taxchart = $item if $item->{is_default};
457 $taxchart_to_use = $item if $key eq $form->{"taxchart_$i"};
460 $taxchart_to_use = $default_taxchart // $first_taxchart if $chart_has_changed || !$taxchart_to_use;
461 my $selected_taxchart = $taxchart_to_use->id . '--' . $taxchart_to_use->rate;
462 $form->{"selected_taxchart_$i"} = $selected_taxchart;
463 $form->{"AP_amount_chart_id_$i"} = $amount_chart_id;
464 $form->{"taxcharts_$i"} = \@taxcharts;
467 $form->{taxchart_value_title_sub} = sub {
470 $item->{id} .'--'. $item->{rate},
471 $item->{taxdescription} .' '. ($item->{rate} * 100) .' %',
475 $form->{AP_paid_value_title_sub} = sub {
479 $item->{accno} .'--'. $item->{description}
483 $form->{invtotal_unformatted} = $form->{invtotal};
484 $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
486 $form->{totalpaid} = 0;
490 if ( $form->{'paid_'. $form->{paidaccounts}} ) {
491 $form->{paidaccounts}++;
494 # default account for current assets (i.e. 1801 - SKR04)
495 $form->{accno_arap} = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
497 for my $i (1 .. $form->{paidaccounts}) {
498 $form->{totalpaid} += $form->{"paid_$i"};
501 if ($form->{"paid_$i"}) {
502 $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
504 if ($form->{"exchangerate_$i"} == 0) {
505 $form->{"exchangerate_$i"} = "";
507 $form->{"exchangerate_$i"} =
508 $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
512 if (SL::DB::Default->get->payments_changeable == 0) {
514 $changeable = ($form->{"acc_trans_id_$i"})? 0 : 1;
516 if (SL::DB::Default->get->payments_changeable == 2) {
518 $changeable = (($form->{"gldate_$i"} eq '') || $form->current_date(\%myconfig) eq $form->{"gldate_$i"});
521 #deaktivieren von gebuchten Zahlungen ausserhalb der Bücherkontrolle, vorher prüfen ob heute eingegeben
522 if ($form->date_closed($form->{"gldate_$i"})) {
526 $form->{'paidaccount_changeable_'. $i} = $changeable;
528 $form->{'labelpaid_project_id_'. $i} = $project_labels{$form->{'paid_project_id_'. $i}};
531 $form->{paid_missing} = $form->{invtotal_unformatted} - $form->{totalpaid};
533 print $form->parse_html_template('ap/form_header', {
534 today => DateTime->today,
535 currencies => SL::DB::Manager::Currency->get_all_sorted,
538 $main::lxdebug->leave_sub();
542 $::lxdebug->enter_sub;
543 $::auth->assert('ap_transactions');
548 my $follow_ups = FU->follow_ups('trans_id' => $::form->{id}, 'not_done' => 1);
550 if (@{ $follow_ups }) {
551 $num_due = sum map { $_->{due} * 1 } @{ $follow_ups };
552 $num_follow_ups = scalar @{ $follow_ups }
556 my $transdate = $::form->datetonum($::form->{transdate}, \%::myconfig);
557 my $closedto = $::form->datetonum($::form->{closedto}, \%::myconfig);
559 my $storno = $::form->{id}
560 && !IS->has_storno(\%::myconfig, $::form, 'ap')
561 && !IS->is_storno( \%::myconfig, $::form, 'ap', $::form->{id})
562 && ($::form->{totalpaid} == 0 || $::form->{totalpaid} eq '');
565 print $::form->parse_html_template('ap/form_footer', {
567 num_follow_ups => $num_follow_ups,
568 show_post_draft => ($transdate > $closedto) && !$::form->{id},
569 show_storno => $storno,
572 $::lxdebug->leave_sub;
576 $::auth->assert('ap_transactions');
578 SL::DB::PurchaseInvoice->new(id => $::form->{id})->load->mark_as_paid;
580 $::form->redirect($::locale->text("Marked as paid"));
584 $::form->{transdate} = DateTime->today_local->to_kivitendo if !$::form->{transdate};
585 $::form->{gldate} = $::form->{transdate} if !$::form->{gldate};
592 $main::lxdebug->enter_sub();
594 my $form = $main::form;
595 my %myconfig = %main::myconfig;
597 $main::auth->assert('ap_transactions');
601 $form->{invtotal} = 0;
603 delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
605 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
606 qw(exchangerate creditlimit creditremaining);
608 my @flds = qw(amount AP_amount projectnumber oldprojectnumber project_id taxchart);
610 my (@a, $j, $totaltax);
611 for my $i (1 .. $form->{rowcount}) {
612 $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
613 if ($form->{"amount_$i"} || $params{keep_rows_without_amount}) {
616 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
618 # calculate tax exactly the same way as AP in post_transaction via form->calculate_tax
620 ($tmpnetamount,$form->{"tax_$i"}) = $form->calculate_tax($form->{"amount_$i"},$rate,$form->{taxincluded},2);
622 $totaltax += $form->{"tax_$i"};
623 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
627 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
629 map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
631 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
632 $form->{exchangerate} = $form->{forex} if $form->{forex};
634 $form->{invdate} = $form->{transdate};
636 if (($form->{previous_vendor_id} || $form->{vendor_id}) != $form->{vendor_id}) {
637 IR->get_vendor(\%::myconfig, $form);
640 $form->{rowcount} = $count + ($params{dont_add_new_row} ? 0 : 1);
643 ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
646 for my $i (1 .. $form->{paidaccounts}) {
647 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
650 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
651 } qw(paid exchangerate);
653 $totalpaid += $form->{"paid_$i"};
655 $form->{"forex_$i"} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
656 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
660 $form->{creditremaining} -=
661 ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} -
662 $form->{oldinvtotal});
663 $form->{oldinvtotal} = $form->{invtotal};
664 $form->{oldtotalpaid} = $totalpaid;
668 $main::lxdebug->leave_sub();
673 $main::lxdebug->enter_sub();
675 my $form = $main::form;
676 my %myconfig = %main::myconfig;
677 my $locale = $main::locale;
679 $main::auth->assert('ap_transactions');
680 $form->mtime_ischanged('ap');
682 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
684 my $invdate = $form->datetonum($form->{transdate}, \%myconfig);
686 for my $i (1 .. $form->{paidaccounts}) {
687 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
688 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
690 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
692 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
693 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
695 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
696 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
697 $form->error($locale->text('Cannot post payment for a closed period!'))
698 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
700 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
701 $form->{"exchangerate_$i"} = $form->{exchangerate}
702 if ($invdate == $datepaid);
703 $form->isblank("exchangerate_$i",
704 $locale->text('Exchangerate for payment missing!'));
709 ($form->{AP}) = split /--/, $form->{AP};
710 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
711 if (AP->post_payment(\%myconfig, \%$form)) {
712 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
713 $form->{what_done} = 'invoice';
714 $form->{addition} = "PAYMENT POSTED";
716 $form->redirect($locale->text('Payment posted!'))
718 $form->error($locale->text('Cannot post payment!'));
722 $main::lxdebug->leave_sub();
727 $main::lxdebug->enter_sub();
729 my $form = $main::form;
730 my %myconfig = %main::myconfig;
731 my $locale = $main::locale;
733 $main::auth->assert('ap_transactions');
734 $form->mtime_ischanged('ap');
738 # check if there is a vendor, invoice, due date and invnumber
739 $form->isblank("transdate", $locale->text("Invoice Date missing!"));
740 $form->isblank("duedate", $locale->text("Due Date missing!"));
741 $form->isblank("vendor_id", $locale->text('Vendor missing!'));
742 $form->isblank("invnumber", $locale->text('Invoice Number missing!'));
744 if ($myconfig{mandatory_departments} && !$form->{department_id}) {
745 $form->{saved_message} = $::locale->text('You have to specify a department.');
750 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
751 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
753 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
754 if ($form->date_max_future($form->{"transdate"}, \%myconfig));
755 $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
757 my $zero_amount_posting = 1;
758 for my $i (1 .. $form->{rowcount}) {
759 if ($form->parse_amount(\%myconfig, $form->{"amount_$i"})) {
760 $zero_amount_posting = 0;
765 $form->error($locale->text('Zero amount posting!')) if $zero_amount_posting;
767 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
768 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
771 for my $i (1 .. $form->{paidaccounts}) {
772 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
773 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
775 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
777 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
778 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
780 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
781 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
782 $form->error($locale->text('Cannot post payment for a closed period!'))
783 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
785 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
786 $form->{"exchangerate_$i"} = $form->{exchangerate}
787 if ($transdate == $datepaid);
788 $form->isblank("exchangerate_$i",
789 $locale->text('Exchangerate for payment missing!'));
795 # if old vendor ne vendor redo form
796 if (($form->{previous_customer_id} || $form->{customer_id}) != $form->{customer_id}) {
798 $::dispatcher->end_request;
802 $form->{id} = 0 if $form->{postasnew};
804 if (AP->post_transaction(\%myconfig, \%$form)) {
805 # create webdav folder
806 if ($::instance_conf->get_webdav) {
807 SL::Webdav->new(type => 'accounts_payable',
808 number => $form->{invnumber},
812 if(!exists $form->{addition} && $form->{id} ne "") {
813 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
814 $form->{addition} = "POSTED";
815 $form->{what_done} = "invoice";
818 # /saving the history
819 # Dieser Text wird niemals ausgegeben: Probleme beim redirect?
820 $form->redirect($locale->text('AP transaction posted.')) unless $inline;
822 $form->error($locale->text('Cannot post transaction!'));
825 $main::lxdebug->leave_sub();
829 $main::lxdebug->enter_sub();
831 my $form = $main::form;
832 my %myconfig = %main::myconfig;
834 $main::auth->assert('ap_transactions');
836 $form->{postasnew} = 1;
838 if(!exists $form->{addition} && $form->{id} ne "") {
839 # does this work? post_as_new for ap doesn't immediately save the
840 # invoice, because the invnumber has to be entered by hand.
841 # And the value of $form->{postasnew} isn't checked when calling post
842 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
843 $form->{addition} = "POSTED AS NEW";
844 $form->{what_done} = "invoice";
847 # /saving the history
850 $main::lxdebug->leave_sub();
854 $main::lxdebug->enter_sub();
856 my $form = $main::form;
857 my %myconfig = %main::myconfig;
859 $main::auth->assert('ap_transactions');
861 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);
862 $form->{paidaccounts} = 1;
864 $form->{invdate} = $form->current_date(\%myconfig);
867 $main::lxdebug->leave_sub();
871 $main::lxdebug->enter_sub();
873 my $form = $main::form;
874 my $locale = $main::locale;
876 $main::auth->assert('ap_transactions');
878 $form->{title} = $locale->text('Confirm!');
882 delete $form->{header};
885 <form method=post action=$form->{script}>
888 foreach my $key (keys %$form) {
889 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
890 $form->{$key} =~ s/\"/"/g;
891 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
895 <h2 class=confirm>$form->{title}</h2>
898 . $locale->text('Are you sure you want to delete Transaction')
899 . qq| $form->{invnumber}</h4>
901 <input name=action class=submit type=submit value="|
902 . $locale->text('Yes') . qq|">
906 $main::lxdebug->leave_sub();
910 $main::lxdebug->enter_sub();
912 my $form = $main::form;
913 my %myconfig = %main::myconfig;
914 my $locale = $main::locale;
916 $main::auth->assert('ap_transactions');
918 if (AP->delete_transaction(\%myconfig, \%$form)) {
920 if(!exists $form->{addition}) {
921 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
922 $form->{addition} = "DELETED";
923 $form->{what_done} = "invoice";
926 # /saving the history
927 $form->redirect($locale->text('Transaction deleted!'));
929 $form->error($locale->text('Cannot delete transaction!'));
931 $main::lxdebug->leave_sub();
935 $main::lxdebug->enter_sub();
937 $main::auth->assert('vendor_invoice_edit');
939 my $form = $main::form;
940 my %myconfig = %main::myconfig;
941 my $locale = $main::locale;
943 $form->{title} = $locale->text('AP Transactions');
945 $form->get_lists(projects => { "key" => "ALL_PROJECTS", "all" => 1 });
947 $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
948 # constants and subs for template
949 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
951 $::request->layout->add_javascripts("autocomplete_project.js");
954 print $form->parse_html_template('ap/search', { %myconfig });
956 $main::lxdebug->leave_sub();
959 sub create_subtotal_row {
960 $main::lxdebug->enter_sub();
962 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
964 my $form = $main::form;
965 my %myconfig = %main::myconfig;
967 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
969 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
971 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
973 map { $totals->{$_} = 0 } @{ $subtotal_columns };
975 $main::lxdebug->leave_sub();
980 sub ap_transactions {
981 $main::lxdebug->enter_sub();
983 my $form = $main::form;
984 my %myconfig = %main::myconfig;
985 my $locale = $main::locale;
987 $main::auth->assert('vendor_invoice_edit');
989 report_generator_set_default_sort('transdate', 1);
991 AP->ap_transactions(\%myconfig, \%$form);
993 $form->{title} = $locale->text('AP Transactions');
995 my $report = SL::ReportGenerator->new(\%myconfig, $form);
998 qw(transdate id type invnumber ordnumber name netamount tax amount paid datepaid
999 due duedate transaction_description notes employee globalprojectnumber
1000 vendornumber country ustid taxzone payment_terms charts direct_debit);
1002 my @hidden_variables = map { "l_${_}" } @columns;
1003 push @hidden_variables, "l_subtotal", qw(open closed vendor invnumber ordnumber transaction_description notes project_id transdatefrom transdateto
1004 parts_partnumber parts_description);
1006 my $href = build_std_url('action=ap_transactions', grep { $form->{$_} } @hidden_variables);
1009 'transdate' => { 'text' => $locale->text('Date'), },
1010 'id' => { 'text' => $locale->text('ID'), },
1011 'type' => { 'text' => $locale->text('Type'), },
1012 'invnumber' => { 'text' => $locale->text('Invoice'), },
1013 'ordnumber' => { 'text' => $locale->text('Order'), },
1014 'name' => { 'text' => $locale->text('Vendor'), },
1015 'netamount' => { 'text' => $locale->text('Amount'), },
1016 'tax' => { 'text' => $locale->text('Tax'), },
1017 'amount' => { 'text' => $locale->text('Total'), },
1018 'paid' => { 'text' => $locale->text('Paid'), },
1019 'datepaid' => { 'text' => $locale->text('Date Paid'), },
1020 'due' => { 'text' => $locale->text('Amount Due'), },
1021 'duedate' => { 'text' => $locale->text('Due Date'), },
1022 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
1023 'notes' => { 'text' => $locale->text('Notes'), },
1024 'employee' => { 'text' => $locale->text('Employee'), },
1025 'globalprojectnumber' => { 'text' => $locale->text('Document Project Number'), },
1026 'vendornumber' => { 'text' => $locale->text('Vendor Number'), },
1027 'country' => { 'text' => $locale->text('Country'), },
1028 'ustid' => { 'text' => $locale->text('USt-IdNr.'), },
1029 'taxzone' => { 'text' => $locale->text('Tax rate'), },
1030 'payment_terms' => { 'text' => $locale->text('Payment Terms'), },
1031 'charts' => { 'text' => $locale->text('Buchungskonto'), },
1032 'direct_debit' => { 'text' => $locale->text('direct debit'), },
1035 foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid employee shippingpoint shipvia transaction_description direct_debit)) {
1036 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
1037 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
1040 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
1042 $form->{"l_type"} = "Y";
1043 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
1045 $report->set_columns(%column_defs);
1046 $report->set_column_order(@columns);
1048 $report->set_export_options('ap_transactions', @hidden_variables, qw(sort sortdir));
1050 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
1053 push @options, $locale->text('Vendor') . " : $form->{vendor}" if ($form->{vendor});
1054 push @options, $locale->text('Contact Person') . " : $form->{cp_name}" if ($form->{cp_name});
1055 push @options, $locale->text('Department') . " : $form->{department}" if ($form->{department});
1056 push @options, $locale->text('Invoice Number') . " : $form->{invnumber}" if ($form->{invnumber});
1057 push @options, $locale->text('Order Number') . " : $form->{ordnumber}" if ($form->{ordnumber});
1058 push @options, $locale->text('Notes') . " : $form->{notes}" if ($form->{notes});
1059 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}" if ($form->{transaction_description});
1060 push @options, $locale->text('Part Description') . " : $form->{parts_description}" if $form->{parts_description};
1061 push @options, $locale->text('Part Number') . " : $form->{parts_partnumber}" if $form->{parts_partnumber};
1062 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1) if ($form->{transdatefrom});
1063 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1) if ($form->{transdateto});
1064 push @options, $locale->text('Open') if ($form->{open});
1065 push @options, $locale->text('Closed') if ($form->{closed});
1067 $report->set_options('top_info_text' => join("\n", @options),
1068 'raw_bottom_info_text' => $form->parse_html_template('ap/ap_transactions_bottom'),
1069 'output_format' => 'HTML',
1070 'title' => $form->{title},
1071 'attachment_basename' => $locale->text('vendor_invoice_list') . strftime('_%Y%m%d', localtime time),
1073 $report->set_options_from_form();
1074 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1076 # add sort and escape callback, this one we use for the add sub
1077 $form->{callback} = $href .= "&sort=$form->{sort}";
1079 # escape callback for href
1080 my $callback = $form->escape($href);
1082 my @subtotal_columns = qw(netamount amount paid due);
1084 my %totals = map { $_ => 0 } @subtotal_columns;
1085 my %subtotals = map { $_ => 0 } @subtotal_columns;
1089 foreach my $ap (@{ $form->{AP} }) {
1090 $ap->{tax} = $ap->{amount} - $ap->{netamount};
1091 $ap->{due} = $ap->{amount} - $ap->{paid};
1093 map { $subtotals{$_} += $ap->{$_};
1094 $totals{$_} += $ap->{$_} } @subtotal_columns;
1096 map { $ap->{$_} = $form->format_amount(\%myconfig, $ap->{$_}, 2) } qw(netamount tax amount paid due);
1098 my $is_storno = $ap->{storno} && $ap->{storno_id};
1099 my $has_storno = $ap->{storno} && !$ap->{storno_id};
1101 if ($ap->{invoice}) {
1103 $has_storno ? $locale->text("Invoice with Storno (abbreviation)")
1104 : $is_storno ? $locale->text("Storno (one letter abbreviation)")
1105 : $locale->text("Invoice (one letter abbreviation)");
1108 $has_storno ? $locale->text("AP Transaction with Storno (abbreviation)")
1109 : $is_storno ? $locale->text("AP Transaction Storno (one letter abbreviation)")
1110 : $locale->text("AP Transaction (abbreviation)");
1113 $ap->{direct_debit} = $ap->{direct_debit} ? $::locale->text('yes') : $::locale->text('no');
1117 foreach my $column (@columns) {
1119 'data' => $ap->{$column},
1120 'align' => $column_alignment{$column},
1124 $row->{invnumber}->{link} = build_std_url("script=" . ($ap->{invoice} ? 'ir.pl' : 'ap.pl'), 'action=edit')
1125 . "&id=" . E($ap->{id}) . "&callback=${callback}";
1127 my $row_set = [ $row ];
1129 if (($form->{l_subtotal} eq 'Y')
1130 && (($idx == (scalar @{ $form->{AP} } - 1))
1131 || ($ap->{ $form->{sort} } ne $form->{AP}->[$idx + 1]->{ $form->{sort} }))) {
1132 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1135 $report->add_data($row_set);
1140 $report->add_separator();
1141 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1143 $report->generate_with_headers();
1145 $main::lxdebug->leave_sub();
1149 $main::lxdebug->enter_sub();
1151 my $form = $main::form;
1152 my %myconfig = %main::myconfig;
1153 my $locale = $main::locale;
1155 $main::auth->assert('ap_transactions');
1157 if (IS->has_storno(\%myconfig, $form, 'ap')) {
1158 $form->{title} = $locale->text("Cancel Accounts Payables Transaction");
1159 $form->error($locale->text("Transaction has already been cancelled!"));
1162 $form->error($locale->text('Cannot post storno for a closed period!'))
1163 if ( $form->date_closed($form->{transdate}, \%myconfig));
1165 AP->storno($form, \%myconfig, $form->{id});
1167 # saving the history
1168 if(!exists $form->{addition} && $form->{id} ne "") {
1169 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1170 $form->{addition} = "STORNO";
1171 $form->{what_done} = "invoice";
1172 $form->save_history;
1174 # /saving the history
1176 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1178 $main::lxdebug->leave_sub();