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 flash_later);
45 use SL::ReportGenerator;
46 use SL::DB::BankTransactionAccTrans;
51 use SL::DB::PaymentTerm;
52 use SL::DB::PurchaseInvoice;
53 use SL::DB::RecordTemplate;
56 use SL::Locale::String qw(t8);
58 require "bin/mozilla/common.pl";
59 require "bin/mozilla/reportgenerator.pl";
67 # this is for our long dates
68 # $locale->text('January')
69 # $locale->text('February')
70 # $locale->text('March')
71 # $locale->text('April')
72 # $locale->text('May ')
73 # $locale->text('June')
74 # $locale->text('July')
75 # $locale->text('August')
76 # $locale->text('September')
77 # $locale->text('October')
78 # $locale->text('November')
79 # $locale->text('December')
81 # this is for our short month
82 # $locale->text('Jan')
83 # $locale->text('Feb')
84 # $locale->text('Mar')
85 # $locale->text('Apr')
86 # $locale->text('May')
87 # $locale->text('Jun')
88 # $locale->text('Jul')
89 # $locale->text('Aug')
90 # $locale->text('Sep')
91 # $locale->text('Oct')
92 # $locale->text('Nov')
93 # $locale->text('Dec')
95 sub _may_view_or_edit_this_invoice {
96 return 1 if $::auth->assert('ap_transactions', 1); # may edit all invoices
97 return 0 if !$::form->{id}; # creating new invoices isn't allowed without invoice_edit
98 return 0 if !$::form->{globalproject_id}; # existing records without a project ID are not allowed
99 return SL::DB::Project->new(id => $::form->{globalproject_id})->load->may_employee_view_project_invoices(SL::DB::Manager::Employee->current);
103 my $cache = $::request->cache('ap.pl::_assert_access');
105 $cache->{_may_view_or_edit_this_invoice} = _may_view_or_edit_this_invoice() if !exists $cache->{_may_view_or_edit_this_invoice};
106 $::form->show_generic_error($::locale->text("You do not have the permissions to access this function.")) if ! $cache->{_may_view_or_edit_this_invoice};
109 sub load_record_template {
110 $::auth->assert('ap_transactions');
112 # Load existing template and verify that its one for this module.
113 my $template = SL::DB::RecordTemplate
114 ->new(id => $::form->{id})
116 with_object => [ qw(customer payment currency record_items record_items.chart) ],
119 die "invalid template type" unless $template->template_type eq 'ap_transaction';
121 $template->substitute_variables;
123 # Clean the current $::form before rebuilding it from the template.
124 my $form_defaults = delete $::form->{form_defaults};
125 delete @{ $::form }{ grep { !m{^(?:script|login)$}i } keys %{ $::form } };
127 # Fill $::form from the template.
128 my $today = DateTime->today_local;
129 $::form->{title} = "Add";
130 $::form->{currency} = $template->currency->name;
131 $::form->{direct_debit} = $template->direct_debit;
132 $::form->{globalproject_id} = $template->project_id;
133 $::form->{payment_id} = $template->payment_id;
134 $::form->{AP_chart_id} = $template->ar_ap_chart_id;
135 $::form->{transdate} = $today->to_kivitendo;
136 $::form->{duedate} = $today->to_kivitendo;
137 $::form->{rowcount} = @{ $template->items };
138 $::form->{paidaccounts} = 1;
139 $::form->{$_} = $template->$_ for qw(department_id ordnumber taxincluded notes transaction_description);
141 if ($template->vendor) {
142 $::form->{vendor_id} = $template->vendor_id;
143 $::form->{vendor} = $template->vendor->name;
144 $::form->{duedate} = $template->vendor->payment->calc_date(reference_date => $today)->to_kivitendo if $template->vendor->payment;
148 foreach my $item (@{ $template->items }) {
151 my $active_taxkey = $item->chart->get_active_taxkey;
152 my $taxes = SL::DB::Manager::Tax->get_all(
153 where => [ chart_categories => { like => '%' . $item->chart->category . '%' }],
154 sort_by => 'taxkey, rate',
157 my $tax = first { $item->tax_id == $_->id } @{ $taxes };
158 $tax //= first { $active_taxkey->tax_id == $_->id } @{ $taxes };
159 $tax //= $taxes->[0];
166 $::form->{"AP_amount_chart_id_${row}"} = $item->chart_id;
167 $::form->{"previous_AP_amount_chart_id_${row}"} = $item->chart_id;
168 $::form->{"amount_${row}"} = $::form->format_amount(\%::myconfig, $item->amount1, 2);
169 $::form->{"taxchart_${row}"} = $item->tax_id . '--' . $tax->rate;
170 $::form->{"project_id_${row}"} = $item->project_id;
173 $::form->{$_} = $form_defaults->{$_} for keys %{ $form_defaults // {} };
175 flash('info', $::locale->text("The record template '#1' has been loaded.", $template->template_name));
176 flash('info', $::locale->text("Payment bookings disallowed. After the booking this record may be " .
177 "suggested with the amount of '#1' or otherwise has to be choosen manually." .
178 " No automatic payment booking will be done to chart '#2'.",
179 $form_defaults->{paid_1_suggestion},
180 $form_defaults->{AP_paid_1_suggestion},
181 )) if $::form->{no_payment_bookings};
184 keep_rows_without_amount => 1,
185 dont_add_new_row => 1,
189 sub save_record_template {
190 $::auth->assert('ap_transactions');
192 my $template = $::form->{record_template_id} ? SL::DB::RecordTemplate->new(id => $::form->{record_template_id})->load : SL::DB::RecordTemplate->new;
193 my $js = SL::ClientJS->new(controller => SL::Controller::Base->new);
194 my $new_name = $template->template_name_to_use($::form->{record_template_new_template_name});
196 $js->dialog->close('#record_template_dialog');
199 $_->{chart_id} && (($_->{tax_id} // '') ne '')
201 +{ chart_id => $::form->{"AP_amount_chart_id_${_}"},
202 amount1 => $::form->parse_amount(\%::myconfig, $::form->{"amount_${_}"}),
203 tax_id => (split m{--}, $::form->{"taxchart_${_}"})[0],
204 project_id => $::form->{"project_id_${_}"} || undef,
206 } (1..($::form->{rowcount} || 1));
208 $template->assign_attributes(
209 template_type => 'ap_transaction',
210 template_name => $new_name,
212 currency_id => SL::DB::Manager::Currency->find_by(name => $::form->{currency})->id,
213 ar_ap_chart_id => $::form->{AP_chart_id} || undef,
214 vendor_id => $::form->{vendor_id} || undef,
215 department_id => $::form->{department_id} || undef,
216 project_id => $::form->{globalproject_id} || undef,
217 payment_id => $::form->{payment_id} || undef,
218 taxincluded => $::form->{taxincluded} ? 1 : 0,
219 direct_debit => $::form->{direct_debit} ? 1 : 0,
220 ordnumber => $::form->{ordnumber},
221 notes => $::form->{notes},
222 transaction_description => $::form->{transaction_description},
232 ->flash('error', $::locale->text("Saving the record template '#1' failed.", $new_name))
237 ->flash('info', $::locale->text("The record template '#1' has been saved.", $new_name))
242 $main::lxdebug->enter_sub();
244 my $form = $main::form;
245 my %myconfig = %main::myconfig;
247 $main::auth->assert('ap_transactions');
249 $form->{title} = "Add";
251 $form->{callback} = "ap.pl?action=add" unless $form->{callback};
253 AP->get_transdate(\%myconfig, $form);
254 $form->{initial_transdate} = $form->{transdate};
255 create_links(dont_save => 1);
256 $form->{transdate} = $form->{initial_transdate};
258 if ($form->{vendor_id}) {
259 my $vendor = SL::DB::Vendor->load_cached($form->{vendor_id});
261 # set initial payment terms
262 $form->{payment_id} = $vendor->payment_id;
264 my $last_used_ap_chart = $vendor->last_used_ap_chart;
265 $form->{"AP_amount_chart_id_1"} = $last_used_ap_chart->id if $last_used_ap_chart;
270 $main::lxdebug->leave_sub();
274 $main::lxdebug->enter_sub();
276 # Delay access check to after the invoice's been loaded in
277 # "create_links" so that project-specific invoice rights can be
280 my $form = $main::form;
282 $form->{title} = "Edit";
287 $main::lxdebug->leave_sub();
291 $main::lxdebug->enter_sub();
295 my $form = $main::form;
297 # get all files stored in the webdav folder
298 if ($form->{invnumber} && $::instance_conf->get_webdav) {
299 my $webdav = SL::Webdav->new(
300 type => 'accounts_payable',
301 number => $form->{invnumber},
303 my @all_objects = $webdav->get_all_objects;
304 @{ $form->{WEBDAV} } = map { { name => $_->filename,
306 link => File::Spec->catfile($_->full_filedescriptor),
312 $main::lxdebug->leave_sub();
316 $main::lxdebug->enter_sub();
318 # Delay access check to after the invoice's been loaded so that
319 # project-specific invoice rights can be evaluated.
323 my $form = $main::form;
324 my %myconfig = %main::myconfig;
326 $form->create_links("AP", \%myconfig, "vendor");
331 if (!$params{dont_save}) {
332 %saved = map { ($_ => $form->{$_}) } qw(direct_debit taxincluded);
333 $saved{duedate} = $form->{duedate} if $form->{duedate};
334 $saved{currency} = $form->{currency} if $form->{currency};
335 $saved{taxincluded} = $form->{taxincluded} if $form->{taxincluded};
338 IR->get_vendor(\%myconfig, \%$form);
340 $form->{$_} = $saved{$_} for keys %saved;
341 $form->{rowcount} = 1;
342 $form->{AP_chart_id} = $form->{acc_trans} && $form->{acc_trans}->{AP} ? $form->{acc_trans}->{AP}->[0]->{chart_id} : $::instance_conf->get_ap_chart_id || $form->{AP_links}->{AP}->[0]->{chart_id};
344 # build the popup menus
345 $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
348 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
350 $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
352 $form->{employee} = "$form->{employee}--$form->{employee_id}";
354 AP->setup_form($form);
356 $main::lxdebug->leave_sub();
360 my @fields = qw(acc_trans_id gldate datepaid source memo paid AP_paid paid_project_id);
362 grep { $_->{paid} != 0 }
365 +{ map { ($_ => delete($::form->{"${_}_${idx}"})) } @fields }
366 } (1..$::form->{paidaccounts});
368 @payments = sort_by { DateTime->from_kivitendo($_->{datepaid}) } @payments;
370 $::form->{paidaccounts} = max scalar(@payments), 1;
372 foreach my $idx (1 .. scalar(@payments)) {
373 my $payment = $payments[$idx - 1];
374 $::form->{"${_}_${idx}"} = $payment->{$_} for @fields;
379 $main::lxdebug->enter_sub();
383 my $form = $main::form;
384 my %myconfig = %main::myconfig;
385 my $locale = $main::locale;
386 my $cgi = $::request->{cgi};
388 $::form->{invoice_obj} = SL::DB::PurchaseInvoice->new(id => $::form->{id})->load if $::form->{id};
390 $form->{initial_focus} = !($form->{amount_1} * 1) ? 'vendor_id' : 'row_' . $form->{rowcount};
392 $form->{title_} = $form->{title};
393 $form->{title} = $form->{title} eq 'Add' ? $locale->text('Add Accounts Payables Transaction') : $locale->text('Edit Accounts Payables Transaction');
395 # type=submit $locale->text('Add Accounts Payables Transaction')
396 # type=submit $locale->text('Edit Accounts Payables Transaction')
398 my $readonly = $form->{id} ? "readonly" : "";
400 $form->{radier} = ($::instance_conf->get_ap_changeable == 2)
401 ? ($form->current_date(\%myconfig) eq $form->{gldate})
402 : ($::instance_conf->get_ap_changeable == 1);
403 $readonly = $form->{radier} ? "" : $readonly;
405 $form->{readonly} = $readonly;
407 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
408 if ( $form->{forex} ) {
409 $form->{exchangerate} = $form->{forex};
413 $form->{exchangerate} = $form->{exchangerate} ? $form->format_amount(\%myconfig, $form->{exchangerate}) : '';
414 $form->{creditlimit} = $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
415 $form->{creditremaining} = $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
418 if (($rows = $form->numtextrows($form->{notes}, 50)) < 2) {
421 $form->{textarea_rows} = $rows;
423 $form->{creditremaining_plus} = ($form->{creditremaining} =~ /-/) ? "0" : "1";
425 $form->get_lists("charts" => { "key" => "ALL_CHARTS",
426 "transdate" => $form->{transdate} },
430 { $_->{link_split} = [ split(/:/, $_->{link}) ]; }
431 @{ $form->{ALL_CHARTS} }
434 $form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
436 my %project_labels = map { $_->id => $_->projectnumber } @{ SL::DB::Manager::Project->get_all };
439 my $default_ap_amount_chart_id;
441 foreach my $item (@{ $form->{ALL_CHARTS} }) {
442 if ( grep({ $_ eq 'AP_amount' } @{ $item->{link_split} }) ) {
443 $default_ap_amount_chart_id //= $item->{id};
445 } elsif ( grep({ $_ eq 'AP_paid' } @{ $item->{link_split} }) ) {
446 push(@{ $form->{ALL_CHARTS_AP_paid} }, $item);
449 $charts{$item->{accno}} = $item;
452 my $follow_up_vc = $form->{vendor_id} ? SL::DB::Vendor->load_cached($form->{vendor_id})->name : '';
453 my $follow_up_trans_info = "$form->{invnumber} ($follow_up_vc)";
455 $::request->layout->add_javascripts("autocomplete_chart.js", "show_vc_details.js", "show_history.js", "follow_up.js", "kivi.Draft.js", "kivi.SalesPurchase.js", "kivi.GL.js", "kivi.RecordTemplate.js", "kivi.File.js", "kivi.AP.js", "kivi.CustomerVendor.js", "kivi.Validator.js", "autocomplete_project.js");
456 # $form->{totalpaid} is used by the action bar setup to determine
457 # whether or not canceling is allowed. Therefore it must be
458 # calculated prior to the action bar setup.
459 $form->{totalpaid} = sum map { $form->{"paid_${_}"} } (1..$form->{paidaccounts});
461 setup_ap_display_form_action_bar();
464 # get the correct date for tax
465 my $transdate = $::form->{transdate} ? DateTime->from_kivitendo($::form->{transdate}) : DateTime->today_local;
466 my $deliverydate = $::form->{deliverydate} ? DateTime->from_kivitendo($::form->{deliverydate}) : undef;
467 my $taxdate = $deliverydate ? $deliverydate : $transdate;
471 for my $i (1 .. $form->{rowcount}) {
474 $form->{"amount_$i"} = $form->format_amount(\%myconfig, $form->{"amount_$i"}, 2);
475 $form->{"tax_$i"} = $form->format_amount(\%myconfig, $form->{"tax_$i"}, 2);
477 my ($default_taxchart, $taxchart_to_use);
479 if ( $form->{"taxchart_$i"} ) {
480 ($used_tax_id) = split(/--/, $form->{"taxchart_$i"});
482 my $amount_chart_id = $form->{"AP_amount_chart_id_$i"} || $default_ap_amount_chart_id;
484 my @taxcharts = GL->get_active_taxes_for_chart($amount_chart_id, $taxdate, $used_tax_id);
485 foreach my $item (@taxcharts) {
486 my $key = $item->id . "--" . $item->rate;
487 $first_taxchart //= $item;
488 $default_taxchart = $item if $item->{is_default};
489 $taxchart_to_use = $item if $key eq $form->{"taxchart_$i"};
492 $taxchart_to_use //= $default_taxchart // $first_taxchart;
493 my $selected_taxchart = $taxchart_to_use->id . '--' . $taxchart_to_use->rate;
494 $form->{"selected_taxchart_$i"} = $selected_taxchart;
495 $form->{"AP_amount_chart_id_$i"} = $amount_chart_id;
496 $form->{"taxcharts_$i"} = \@taxcharts;
498 # reverse charge hack for template, display two taxes
499 if ($taxchart_to_use->reverse_charge_chart_id) {
501 ($tmpnetamount, $form->{"tax_reverse_$i"}) = $form->calculate_tax($form->parse_amount(\%myconfig, $form->{"amount_$i"}),
502 $taxchart_to_use->rate, $form->{taxincluded}, 2 );
504 $form->{"tax_charge_$i"} = $form->{"tax_reverse_$i"} * -1;
505 $form->{"tax_reverse_$i"} = $form->format_amount(\%myconfig, $form->{"tax_reverse_$i"}, 2);
506 $form->{"tax_charge_$i"} = $form->format_amount(\%myconfig, $form->{"tax_charge_$i"}, 2);
510 $form->{taxchart_value_title_sub} = sub {
513 $item->{id} .'--'. $item->{rate},
514 $item->{taxkey} . ' - ' . $item->{taxdescription} .' '. ($item->{rate} * 100) .' %',
518 $form->{AP_paid_value_title_sub} = sub {
522 $item->{accno} .'--'. $item->{description}
526 $form->{invtotal_unformatted} = $form->{invtotal};
527 $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
531 if ( $form->{'paid_'. $form->{paidaccounts}} ) {
532 $form->{paidaccounts}++;
535 # default account for current assets (i.e. 1801 - SKR04)
536 $form->{accno_arap} = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
538 for my $i (1 .. $form->{paidaccounts}) {
540 if ($form->{"paid_$i"}) {
541 $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
543 if ($form->{"exchangerate_$i"} == 0) {
544 $form->{"exchangerate_$i"} = "";
546 $form->{"exchangerate_$i"} =
547 $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
551 if (SL::DB::Default->get->payments_changeable == 0) {
553 $changeable = ($form->{"acc_trans_id_$i"})? 0 : 1;
555 if (SL::DB::Default->get->payments_changeable == 2) {
557 $changeable = (($form->{"gldate_$i"} eq '') || $form->current_date(\%myconfig) eq $form->{"gldate_$i"});
560 #deaktivieren von gebuchten Zahlungen ausserhalb der Bücherkontrolle, vorher prüfen ob heute eingegeben
561 if ($form->date_closed($form->{"gldate_$i"})) {
565 $form->{'paidaccount_changeable_'. $i} = $changeable;
567 $form->{'labelpaid_project_id_'. $i} = $project_labels{$form->{'paid_project_id_'. $i}};
568 # accno and description as info text
569 $form->{'AP_paid_readonly_desc_' . $i} = $form->{'AP_paid_' . $i} ?
570 $form->{'AP_paid_' . $i} . " " . SL::DB::Manager::Chart->find_by(accno => $form->{'AP_paid_' . $i})->description
574 $form->{paid_missing} = $form->{invtotal_unformatted} - $form->{totalpaid};
576 $form->{payment_id} = $form->{invoice_obj}->{payment_id} // $form->{payment_id};
577 print $form->parse_html_template('ap/form_header', {
578 today => DateTime->today,
579 currencies => SL::DB::Manager::Currency->get_all_sorted,
580 payment_terms => SL::DB::Manager::PaymentTerm->get_all_sorted(query => [ or => [ obsolete => 0, id => $form->{payment_id}*1 ]]),
583 $main::lxdebug->leave_sub();
587 $::lxdebug->enter_sub;
594 my $follow_ups = FU->follow_ups('trans_id' => $::form->{id}, 'not_done' => 1);
596 if (@{ $follow_ups }) {
597 $num_due = sum map { $_->{due} * 1 } @{ $follow_ups };
598 $num_follow_ups = scalar @{ $follow_ups }
602 my $transdate = $::form->datetonum($::form->{transdate}, \%::myconfig);
603 my $closedto = $::form->datetonum($::form->{closedto}, \%::myconfig);
605 my $storno = $::form->{id}
606 && !IS->has_storno(\%::myconfig, $::form, 'ap')
607 && !IS->is_storno( \%::myconfig, $::form, 'ap', $::form->{id})
608 && ($::form->{totalpaid} == 0 || $::form->{totalpaid} eq '');
611 print $::form->parse_html_template('ap/form_footer', {
613 num_follow_ups => $num_follow_ups,
616 $::lxdebug->leave_sub;
620 $::auth->assert('ap_transactions');
622 SL::DB::PurchaseInvoice->new(id => $::form->{id})->load->mark_as_paid;
624 $::form->redirect($::locale->text("Marked as paid"));
628 $::form->{transdate} = DateTime->today_local->to_kivitendo if !$::form->{transdate};
629 $::form->{gldate} = $::form->{transdate} if !$::form->{gldate};
636 $main::lxdebug->enter_sub();
638 my $form = $main::form;
639 my %myconfig = %main::myconfig;
641 $main::auth->assert('ap_transactions');
645 $form->{invtotal} = 0;
647 delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
649 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
650 qw(exchangerate creditlimit creditremaining);
652 my @flds = qw(amount AP_amount_chart_id projectnumber oldprojectnumber project_id taxchart tax);
654 my (@a, $j, $totaltax);
655 for my $i (1 .. $form->{rowcount}) {
656 $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
657 if ($form->{"amount_$i"} || $params{keep_rows_without_amount}) {
660 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
662 # calculate tax exactly the same way as AP in post_transaction via form->calculate_tax
664 ($tmpnetamount,$form->{"tax_$i"}) = $form->calculate_tax($form->{"amount_$i"},$rate,$form->{taxincluded},2);
665 $totaltax += $form->{"tax_$i"};
666 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
670 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
672 map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
674 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
675 $form->{exchangerate} = $form->{forex} if $form->{forex};
677 $form->{invdate} = $form->{transdate};
679 if (($form->{previous_vendor_id} || $form->{vendor_id}) != $form->{vendor_id}) {
680 IR->get_vendor(\%::myconfig, $form);
682 my $vendor = SL::DB::Vendor->load_cached($form->{vendor_id});
684 # reset payment to new vendor
685 $form->{payment_id} = $vendor->payment_id;
687 if (($form->{rowcount} == 1) && ($form->{amount_1} == 0)) {
688 my $last_used_ap_chart = $vendor->last_used_ap_chart;
689 $form->{"AP_amount_chart_id_1"} = $last_used_ap_chart->id if $last_used_ap_chart;
693 $form->{rowcount} = $count + ($params{dont_add_new_row} ? 0 : 1);
696 ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
699 for my $i (1 .. $form->{paidaccounts}) {
700 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
703 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
704 } qw(paid exchangerate);
706 $totalpaid += $form->{"paid_$i"};
708 $form->{"forex_$i"} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
709 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
713 $form->{creditremaining} -=
714 ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} -
715 $form->{oldinvtotal});
716 $form->{oldinvtotal} = $form->{invtotal};
717 $form->{oldtotalpaid} = $totalpaid;
721 $main::lxdebug->leave_sub();
726 $main::lxdebug->enter_sub();
728 my $form = $main::form;
729 my %myconfig = %main::myconfig;
730 my $locale = $main::locale;
732 $main::auth->assert('ap_transactions');
733 $form->mtime_ischanged('ap');
735 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
737 my $invdate = $form->datetonum($form->{transdate}, \%myconfig);
739 for my $i (1 .. $form->{paidaccounts}) {
740 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
741 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
743 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
745 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
746 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
748 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
749 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
750 $form->error($locale->text('Cannot post payment for a closed period!'))
751 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
753 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
754 $form->{"exchangerate_$i"} = $form->{exchangerate}
755 if ($invdate == $datepaid);
756 $form->isblank("exchangerate_$i",
757 $locale->text('Exchangerate for payment missing!'));
762 ($form->{AP}) = split /--/, $form->{AP};
763 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
764 if (AP->post_payment(\%myconfig, \%$form)) {
765 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
766 $form->{what_done} = 'invoice';
767 $form->{addition} = "PAYMENT POSTED";
769 $form->redirect($locale->text('Payment posted!'))
771 $form->error($locale->text('Cannot post payment!'));
775 $main::lxdebug->leave_sub();
780 $main::lxdebug->enter_sub();
782 my $form = $main::form;
783 my %myconfig = %main::myconfig;
784 my $locale = $main::locale;
786 $main::auth->assert('ap_transactions');
787 $form->mtime_ischanged('ap');
791 # check if there is a vendor, invoice, due date and invnumber
792 $form->isblank("transdate", $locale->text("Invoice Date missing!"));
793 $form->isblank("duedate", $locale->text("Due Date missing!"));
794 $form->isblank("vendor_id", $locale->text('Vendor missing!'));
795 $form->isblank("invnumber", $locale->text('Invoice Number missing!'));
796 $form->isblank("AP_chart_id", $locale->text('No contra account selected!'));
798 if ($myconfig{mandatory_departments} && !$form->{department_id}) {
799 $form->{saved_message} = $::locale->text('You have to specify a department.');
804 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
805 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
807 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
808 if ($form->date_max_future($form->{"transdate"}, \%myconfig));
809 $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
811 my $zero_amount_posting = 1;
812 for my $i (1 .. $form->{rowcount}) {
814 # no taxincluded for reverse charge
815 my ($used_tax_id) = split(/--/, $form->{"taxchart_$i"});
816 my $tax = SL::DB::Manager::Tax->find_by(id => $used_tax_id);
817 if ($tax->reverse_charge_chart_id && $form->{taxincluded}) {
818 $form->error($locale->text('Cannot Post AP transaction with tax included!'));
821 if ($form->parse_amount(\%myconfig, $form->{"amount_$i"})) {
822 $zero_amount_posting = 0;
826 $form->error($locale->text('Zero amount posting!')) if $zero_amount_posting;
828 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
829 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
832 for my $i (1 .. $form->{paidaccounts}) {
833 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
834 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
836 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
838 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
839 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
841 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
842 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
843 $form->error($locale->text('Cannot post payment for a closed period!'))
844 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
846 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
847 $form->{"exchangerate_$i"} = $form->{exchangerate}
848 if ($transdate == $datepaid);
849 $form->isblank("exchangerate_$i",
850 $locale->text('Exchangerate for payment missing!'));
856 # if old vendor ne vendor redo form
857 if (($form->{previous_customer_id} || $form->{customer_id}) != $form->{customer_id}) {
859 $::dispatcher->end_request;
863 $form->{id} = 0 if $form->{postasnew};
865 if (AP->post_transaction(\%myconfig, \%$form)) {
866 # create webdav folder
867 if ($::instance_conf->get_webdav) {
868 SL::Webdav->new(type => 'accounts_payable',
869 number => $form->{invnumber},
873 if(!exists $form->{addition} && $form->{id} ne "") {
874 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
875 $form->{addition} = "POSTED";
876 $form->{what_done} = "invoice";
881 my $msg = $locale->text("AP transaction '#1' posted (ID: #2)", $form->{invnumber}, $form->{id});
882 if ($form->{callback} =~ /BankTransaction/) {
883 # no restore_from_session_id needed. we like to have a newly generated
884 # list of invoices for bank transactions
885 SL::Helper::Flash::flash_later('info', $msg);
886 print $form->redirect_header($form->{callback});
887 $::dispatcher->end_request;
889 } elsif ('doc-tab' eq $form->{after_action}) {
890 # Redirect with callback containing a fragment does not work (by now)
891 # because the callback info is stored in the session an parsing the
892 # callback parameters does not support fragments (see SL::Form::redirect).
893 # So use flash_later for the message and redirect_headers for redirecting.
894 my $add_doc_url = build_std_url("script=ap.pl", 'action=edit', 'id=' . E($form->{id}), 'fragment=ui-tabs-docs');
895 SL::Helper::Flash::flash_later('info', $msg);
896 print $form->redirect_header($add_doc_url);
897 $::dispatcher->end_request;
900 $form->redirect($msg);
905 $form->error($locale->text('Cannot post transaction!'));
908 $main::lxdebug->leave_sub();
912 $main::lxdebug->enter_sub();
914 my $form = $main::form;
915 my %myconfig = %main::myconfig;
917 $main::auth->assert('ap_transactions');
919 $form->{postasnew} = 1;
921 if(!exists $form->{addition} && $form->{id} ne "") {
922 # does this work? post_as_new for ap doesn't immediately save the
923 # invoice, because the invnumber has to be entered by hand.
924 # And the value of $form->{postasnew} isn't checked when calling post
925 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
926 $form->{addition} = "POSTED AS NEW";
927 $form->{what_done} = "invoice";
930 # /saving the history
933 $main::lxdebug->leave_sub();
937 $main::lxdebug->enter_sub();
939 my $form = $main::form;
940 my %myconfig = %main::myconfig;
942 $main::auth->assert('ap_transactions');
944 map { delete $form->{$_} } qw(printed emailed queued invnumber deliverydate id datepaid_1 gldate_1 acc_trans_id_1 source_1 memo_1 paid_1 exchangerate_1 AP_paid_1 storno convert_from_oe_id);
945 $form->{paidaccounts} = 1;
948 my $today = DateTime->today_local;
949 $form->{transdate} = $today->to_kivitendo;
950 $form->{duedate} = $form->{transdate};
952 if ($form->{vendor_id}) {
953 my $payment_terms = SL::DB::Vendor->load_cached($form->{vendor_id})->payment;
954 $form->{duedate} = $payment_terms->calc_date(reference_date => $today)->to_kivitendo if $payment_terms;
959 $main::lxdebug->leave_sub();
963 my $form = $main::form;
964 my %myconfig = %main::myconfig;
965 my $locale = $main::locale;
967 $main::auth->assert('ap_transactions');
969 if (AP->delete_transaction(\%myconfig, \%$form)) {
971 if(!exists $form->{addition}) {
972 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
973 $form->{addition} = "DELETED";
974 $form->{what_done} = "invoice";
977 # /saving the history
978 $form->redirect($locale->text('Transaction deleted!'));
980 $form->error($locale->text('Cannot delete transaction!'));
984 $main::lxdebug->enter_sub();
986 my $form = $main::form;
987 my %myconfig = %main::myconfig;
988 my $locale = $main::locale;
990 $form->{title} = $locale->text('Vendor Invoices & AP Transactions');
992 $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
993 $::form->{ALL_TAXZONES} = SL::DB::Manager::TaxZone ->get_all_sorted;
995 # constants and subs for template
996 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
998 $::request->layout->add_javascripts("autocomplete_project.js");
1000 setup_ap_search_action_bar();
1003 print $form->parse_html_template('ap/search', { %myconfig });
1005 $main::lxdebug->leave_sub();
1008 sub create_subtotal_row {
1009 $main::lxdebug->enter_sub();
1011 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
1013 my $form = $main::form;
1014 my %myconfig = %main::myconfig;
1016 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
1018 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
1020 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
1022 map { $totals->{$_} = 0 } @{ $subtotal_columns };
1024 $main::lxdebug->leave_sub();
1029 sub ap_transactions {
1030 $main::lxdebug->enter_sub();
1032 my $form = $main::form;
1033 my %myconfig = %main::myconfig;
1034 my $locale = $main::locale;
1036 report_generator_set_default_sort('transdate', 1);
1038 AP->ap_transactions(\%myconfig, \%$form);
1040 $form->{title} = $locale->text('Vendor Invoices & AP Transactions');
1042 my $report = SL::ReportGenerator->new(\%myconfig, $form);
1045 qw(transdate id type invnumber ordnumber name netamount tax amount paid datepaid
1046 due duedate transaction_description notes employee globalprojectnumber department
1047 vendornumber country ustid taxzone payment_terms charts debit_chart direct_debit
1050 my @hidden_variables = map { "l_${_}" } @columns;
1051 push @hidden_variables, "l_subtotal", qw(open closed vendor invnumber ordnumber transaction_description notes project_id transdatefrom transdateto
1052 parts_partnumber parts_description department_id taxzone_id);
1054 my $href = build_std_url('action=ap_transactions', grep { $form->{$_} } @hidden_variables);
1057 'transdate' => { 'text' => $locale->text('Date'), },
1058 'id' => { 'text' => $locale->text('ID'), },
1059 'type' => { 'text' => $locale->text('Type'), },
1060 'invnumber' => { 'text' => $locale->text('Invoice'), },
1061 'ordnumber' => { 'text' => $locale->text('Order'), },
1062 'name' => { 'text' => $locale->text('Vendor'), },
1063 'netamount' => { 'text' => $locale->text('Amount'), },
1064 'tax' => { 'text' => $locale->text('Tax'), },
1065 'amount' => { 'text' => $locale->text('Total'), },
1066 'paid' => { 'text' => $locale->text('Paid'), },
1067 'datepaid' => { 'text' => $locale->text('Date Paid'), },
1068 'due' => { 'text' => $locale->text('Amount Due'), },
1069 'duedate' => { 'text' => $locale->text('Due Date'), },
1070 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
1071 'notes' => { 'text' => $locale->text('Notes'), },
1072 'employee' => { 'text' => $locale->text('Employee'), },
1073 'globalprojectnumber' => { 'text' => $locale->text('Document Project Number'), },
1074 'department' => { 'text' => $locale->text('Department'), },
1075 'vendornumber' => { 'text' => $locale->text('Vendor Number'), },
1076 'country' => { 'text' => $locale->text('Country'), },
1077 'ustid' => { 'text' => $locale->text('USt-IdNr.'), },
1078 'taxzone' => { 'text' => $locale->text('Tax rate'), },
1079 'payment_terms' => { 'text' => $locale->text('Payment Terms'), },
1080 'charts' => { 'text' => $locale->text('Chart'), },
1081 'debit_chart' => { 'text' => $locale->text('Debit Account'), },
1082 'direct_debit' => { 'text' => $locale->text('direct debit'), },
1083 'insertdate' => { 'text' => $locale->text('Insert Date'), },
1086 foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid employee shippingpoint shipvia transaction_description direct_debit department taxzone)) {
1087 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
1088 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
1091 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
1093 $form->{"l_type"} = "Y";
1094 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
1096 $report->set_columns(%column_defs);
1097 $report->set_column_order(@columns);
1099 $report->set_export_options('ap_transactions', @hidden_variables, qw(sort sortdir));
1101 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
1103 my $department_description;
1104 $department_description = SL::DB::Manager::Department->find_by(id => $form->{department_id})->description if $form->{department_id};
1105 my $project_description;
1106 $project_description = SL::DB::Manager::Project->find_by(id => $form->{project_id})->description if $form->{project_id};
1109 push @options, $locale->text('Vendor') . " : $form->{vendor}" if ($form->{vendor});
1110 push @options, $locale->text('Contact Person') . " : $form->{cp_name}" if ($form->{cp_name});
1111 push @options, $locale->text('Department') . " : $department_description" if ($form->{department_id});
1112 push @options, $locale->text('Project') . " : $project_description" if ($project_description);
1113 push @options, $locale->text('Invoice Number') . " : $form->{invnumber}" if ($form->{invnumber});
1114 push @options, $locale->text('Order Number') . " : $form->{ordnumber}" if ($form->{ordnumber});
1115 push @options, $locale->text('Notes') . " : $form->{notes}" if ($form->{notes});
1116 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}" if ($form->{transaction_description});
1117 push @options, $locale->text('Part Description') . " : $form->{parts_description}" if $form->{parts_description};
1118 push @options, $locale->text('Part Number') . " : $form->{parts_partnumber}" if $form->{parts_partnumber};
1119 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1) if ($form->{transdatefrom});
1120 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1) if ($form->{transdateto});
1121 push @options, $locale->text('Open') if ($form->{open});
1122 push @options, $locale->text('Closed') if ($form->{closed});
1124 $report->set_options('top_info_text' => join("\n", @options),
1125 'output_format' => 'HTML',
1126 'title' => $form->{title},
1127 'attachment_basename' => $locale->text('vendor_invoice_list') . strftime('_%Y%m%d', localtime time),
1129 $report->set_options_from_form();
1130 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1132 # add sort and escape callback, this one we use for the add sub
1133 $form->{callback} = $href .= "&sort=$form->{sort}";
1135 # escape callback for href
1136 my $callback = $form->escape($href);
1138 my @subtotal_columns = qw(netamount amount paid due);
1140 my %totals = map { $_ => 0 } @subtotal_columns;
1141 my %subtotals = map { $_ => 0 } @subtotal_columns;
1145 foreach my $ap (@{ $form->{AP} }) {
1146 $ap->{tax} = $ap->{amount} - $ap->{netamount};
1147 $ap->{due} = $ap->{amount} - $ap->{paid};
1149 map { $subtotals{$_} += $ap->{$_};
1150 $totals{$_} += $ap->{$_} } @subtotal_columns;
1152 map { $ap->{$_} = $form->format_amount(\%myconfig, $ap->{$_}, 2) } qw(netamount tax amount paid due);
1154 my $is_storno = $ap->{storno} && $ap->{storno_id};
1155 my $has_storno = $ap->{storno} && !$ap->{storno_id};
1157 if ($ap->{invoice}) {
1159 $has_storno ? $locale->text("Invoice with Storno (abbreviation)")
1160 : $is_storno ? $locale->text("Storno (one letter abbreviation)")
1161 : $locale->text("Invoice (one letter abbreviation)");
1164 $has_storno ? $locale->text("AP Transaction with Storno (abbreviation)")
1165 : $is_storno ? $locale->text("AP Transaction Storno (one letter abbreviation)")
1166 : $locale->text("AP Transaction (abbreviation)");
1169 $ap->{direct_debit} = $ap->{direct_debit} ? $::locale->text('yes') : $::locale->text('no');
1173 foreach my $column (@columns) {
1175 'data' => $ap->{$column},
1176 'align' => $column_alignment{$column},
1180 $row->{invnumber}->{link} = build_std_url("script=" . ($ap->{invoice} ? 'ir.pl' : 'ap.pl'), 'action=edit')
1181 . "&id=" . E($ap->{id}) . "&callback=${callback}";
1183 my $row_set = [ $row ];
1185 if (($form->{l_subtotal} eq 'Y')
1186 && (($idx == (scalar @{ $form->{AP} } - 1))
1187 || ($ap->{ $form->{sort} } ne $form->{AP}->[$idx + 1]->{ $form->{sort} }))) {
1188 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1191 $report->add_data($row_set);
1196 $report->add_separator();
1197 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1199 setup_ap_transactions_action_bar();
1200 $report->generate_with_headers();
1202 $main::lxdebug->leave_sub();
1206 $main::lxdebug->enter_sub();
1208 my $form = $main::form;
1209 my %myconfig = %main::myconfig;
1210 my $locale = $main::locale;
1212 $main::auth->assert('ap_transactions');
1214 if (IS->has_storno(\%myconfig, $form, 'ap')) {
1215 $form->{title} = $locale->text("Cancel Accounts Payables Transaction");
1216 $form->error($locale->text("Transaction has already been cancelled!"));
1219 $form->error($locale->text('Cannot post storno for a closed period!'))
1220 if ( $form->date_closed($form->{transdate}, \%myconfig));
1222 AP->storno($form, \%myconfig, $form->{id});
1224 # saving the history
1225 if(!exists $form->{addition} && $form->{id} ne "") {
1226 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1227 $form->{addition} = "STORNO";
1228 $form->{what_done} = "invoice";
1229 $form->save_history;
1231 # /saving the history
1233 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1235 $main::lxdebug->leave_sub();
1238 sub add_from_purchase_order {
1239 $main::auth->assert('ap_transactions');
1241 return if !$::form->{id};
1243 my $order_id = delete $::form->{id};
1244 my $order = SL::DB::Order->new(id => $order_id)->load(with => [ 'vendor', 'currency', 'payment_terms' ]);
1246 return if $order->type ne 'purchase_order';
1248 my $today = DateTime->today_local;
1249 $::form->{title} = "Add";
1250 $::form->{vc} = 'vendor';
1251 $::form->{vendor_id} = $order->customervendor->id;
1252 $::form->{vendor} = $order->vendor->name;
1253 $::form->{convert_from_oe_id} = $order->id;
1254 $::form->{globalproject_id} = $order->globalproject_id;
1255 $::form->{ordnumber} = $order->number;
1256 $::form->{department_id} = $order->department_id;
1257 $::form->{transaction_description} = $order->transaction_description;
1258 $::form->{currency} = $order->currency->name;
1259 $::form->{taxincluded} = 1; # we use amount below, so tax is included
1260 $::form->{transdate} = $today->to_kivitendo;
1261 $::form->{duedate} = $today->to_kivitendo;
1262 $::form->{duedate} = $order->payment_terms->calc_date(reference_date => $today)->to_kivitendo if $order->payment_terms;
1263 $::form->{deliverydate} = $order->reqdate->to_kivitendo if $order->reqdate;
1266 my $config_po_ap_workflow_chart_id = $::instance_conf->get_workflow_po_ap_chart_id;
1268 my ($first_taxchart, $default_taxchart, $taxchart_to_use);
1270 @taxcharts = GL->get_active_taxes_for_chart($config_po_ap_workflow_chart_id, $::form->{transdate}) if (defined $config_po_ap_workflow_chart_id);
1271 foreach my $item (@taxcharts) {
1272 $first_taxchart //= $item;
1273 $default_taxchart = $item if $item->{is_default};
1275 $taxchart_to_use = $default_taxchart // $first_taxchart;
1277 my %pat = $order->calculate_prices_and_taxes;
1279 foreach my $amount_chart (keys %{$pat{amounts}}) {
1280 my $tax = SL::DB::Manager::Tax->find_by(id => $pat{amounts}->{$amount_chart}->{tax_id});
1281 # If tax chart from order for this amount is active, use it. Use default or first tax chart for selected chart else.
1282 if (defined $config_po_ap_workflow_chart_id) {
1283 $taxchart_to_use = (first {$_->{id} == $tax->id} @taxcharts) // $taxchart_to_use;
1285 $taxchart_to_use = $tax;
1288 $::form->{"AP_amount_chart_id_$row"} = $config_po_ap_workflow_chart_id // $amount_chart;
1289 $::form->{"previous_AP_amount_chart_id_$row"} = $::form->{"AP_amount_chart_id_$row"};
1290 $::form->{"amount_$row"} = $::form->format_amount(\%::myconfig, $pat{amounts}->{$amount_chart}->{amount} * (1 + $tax->rate), 2);
1291 $::form->{"taxchart_$row"} = $taxchart_to_use->id . '--' . $taxchart_to_use->rate;
1292 $::form->{"project_id_$row"} = $order->globalproject_id;
1297 my $last_used_ap_chart = SL::DB::Vendor->load_cached($::form->{vendor_id})->last_used_ap_chart;
1298 $::form->{"AP_amount_chart_id_$row"} = $last_used_ap_chart->id if $last_used_ap_chart;
1299 $::form->{rowcount} = $row;
1302 keep_rows_without_amount => 1,
1303 dont_add_new_row => 1,
1307 sub setup_ap_search_action_bar {
1310 for my $bar ($::request->layout->get('actionbar')) {
1313 $::locale->text('Search'),
1314 submit => [ '#form', { action => "ap_transactions" } ],
1315 checks => [ 'kivi.validate_form' ],
1316 accesskey => 'enter',
1320 $::request->layout->add_javascripts('kivi.Validator.js');
1323 sub setup_ap_transactions_action_bar {
1325 my $may_edit_create = $::auth->assert('ap_transactions', 1);
1327 for my $bar ($::request->layout->get('actionbar')) {
1330 action => [ t8('Add') ],
1332 t8('Purchase Invoice'),
1333 link => [ 'ir.pl?action=add' ],
1334 disabled => !$may_edit_create ? t8('You do not have the permissions to access this function.') : undef,
1338 t8('AP Transaction'),
1339 link => [ 'ap.pl?action=add' ],
1340 disabled => !$may_edit_create ? t8('You do not have the permissions to access this function.') : undef,
1342 ], # end of combobox "Add"
1347 sub setup_ap_display_form_action_bar {
1348 my $transdate = $::form->datetonum($::form->{transdate}, \%::myconfig);
1349 my $closedto = $::form->datetonum($::form->{closedto}, \%::myconfig);
1350 my $is_closed = $transdate <= $closedto;
1352 my $change_never = $::instance_conf->get_ap_changeable == 0;
1353 my $change_on_same_day_only = $::instance_conf->get_ap_changeable == 2 && ($::form->current_date(\%::myconfig) ne $::form->{gldate});
1355 my $is_storno = IS->is_storno(\%::myconfig, $::form, 'ap', $::form->{id});
1356 my $has_storno = IS->has_storno(\%::myconfig, $::form, 'ap');
1358 my $may_edit_create = $::auth->assert('ap_transactions', 1);
1360 my $has_sepa_exports;
1361 if ($::form->{id}) {
1362 my $invoice = SL::DB::Manager::PurchaseInvoice->find_by(id => $::form->{id});
1363 $has_sepa_exports = 1 if ($invoice->find_sepa_export_items()->[0]);
1366 my $is_linked_bank_transaction;
1368 && SL::DB::Default->get->payments_changeable != 0
1369 && SL::DB::Manager::BankTransactionAccTrans->find_by(ap_id => $::form->{id})) {
1371 $is_linked_bank_transaction = 1;
1373 my $is_linked_gl_transaction;
1374 if ($::form->{id} && SL::DB::Manager::ApGl->find_by(ap_id => $::form->{id})) {
1375 $is_linked_gl_transaction = 1;
1378 my $create_post_action = sub {
1379 # $_[0]: description
1380 # $_[1]: after_action
1383 submit => [ '#form', { action => "post", after_action => $_[1] } ],
1384 checks => [ 'kivi.validate_form', 'kivi.AP.check_fields_before_posting', 'kivi.AP.check_duplicate_invnumber' ],
1385 disabled => !$may_edit_create ? t8('You must not change this AP transaction.')
1386 : $is_closed ? t8('The billing period has already been locked.')
1387 : $is_storno ? t8('A canceled invoice cannot be posted.')
1388 : ($::form->{id} && $change_never) ? t8('Changing invoices has been disabled in the configuration.')
1389 : ($::form->{id} && $change_on_same_day_only) ? t8('Invoices can only be changed on the day they are posted.')
1390 : $is_linked_bank_transaction ? t8('This transaction is linked with a bank transaction. Please undo and redo the bank transaction booking if needed.')
1396 if ($::instance_conf->get_ap_add_doc && $::instance_conf->get_doc_storage) {
1397 @post_entries = ( $create_post_action->(t8('Post'), 'doc-tab'),
1398 $create_post_action->(t8('Post and new booking')) );
1399 } elsif ($::instance_conf->get_doc_storage) {
1400 @post_entries = ( $create_post_action->(t8('Post')),
1401 $create_post_action->(t8('Post and upload document'), 'doc-tab') );
1403 @post_entries = ( $create_post_action->(t8('Post')) );
1406 for my $bar ($::request->layout->get('actionbar')) {
1410 submit => [ '#form', { action => "update" } ],
1411 id => 'update_button',
1412 checks => [ 'kivi.validate_form' ],
1413 accesskey => 'enter',
1414 disabled => !$may_edit_create ? t8('You must not change this AP transaction.') : undef,
1420 submit => [ '#form', { action => "post_payment" } ],
1421 checks => [ 'kivi.validate_form' ],
1422 disabled => !$may_edit_create ? t8('You must not change this AP transaction.')
1423 : !$::form->{id} ? t8('This invoice has not been posted yet.')
1424 : $is_linked_bank_transaction ? t8('This transaction is linked with a bank transaction. Please undo and redo the bank transaction booking if needed.')
1427 action => [ t8('Mark as paid'),
1428 submit => [ '#form', { action => "mark_as_paid" } ],
1429 confirm => t8('This will remove the invoice from showing as unpaid even if the unpaid amount does not match the amount. Proceed?'),
1430 disabled => !$may_edit_create ? t8('You must not change this AP transaction.')
1431 : !$::form->{id} ? t8('This invoice has not been posted yet.')
1433 only_if => $::instance_conf->get_is_show_mark_as_paid,
1435 ], # end of combobox "Post"
1438 action => [ t8('Storno'),
1439 submit => [ '#form', { action => "storno" } ],
1440 checks => [ 'kivi.validate_form', 'kivi.AP.check_fields_before_posting' ],
1441 confirm => t8('Do you really want to cancel this invoice?'),
1442 disabled => !$may_edit_create ? t8('You must not change this AP transaction.')
1443 : !$::form->{id} ? t8('This invoice has not been posted yet.')
1444 : $has_storno ? t8('This invoice has been canceled already.')
1445 : $is_storno ? t8('Reversal invoices cannot be canceled.')
1446 : $::form->{totalpaid} ? t8('Invoices with payments cannot be canceled.')
1447 : $has_sepa_exports ? t8('This invoice has been linked with a sepa export, undo this first.')
1448 : $is_linked_gl_transaction ? t8('This transaction is linked with a gl transaction. Please delete the ap transaction booking if needed.')
1451 action => [ t8('Delete'),
1452 submit => [ '#form', { action => "delete" } ],
1453 confirm => t8('Do you really want to delete this object?'),
1454 disabled => !$may_edit_create ? t8('You must not change this AP transaction.')
1455 : !$::form->{id} ? t8('This invoice has not been posted yet.')
1456 : $is_closed ? t8('The billing period has already been locked.')
1457 : $has_sepa_exports ? t8('This invoice has been linked with a sepa export, undo this first.')
1458 : $is_linked_bank_transaction ? t8('This transaction is linked with a bank transaction. Please undo and redo the bank transaction booking if needed.')
1459 : $is_linked_gl_transaction ? undef # linked transactions can be deleted, if period is not closed
1460 : $change_never ? t8('Changing invoices has been disabled in the configuration.')
1461 : $change_on_same_day_only ? t8('Invoices can only be changed on the day they are posted.')
1462 : $has_storno ? t8('This invoice has been canceled already.')
1465 ], # end of combobox "Storno"
1470 action => [ t8('Workflow') ],
1473 submit => [ '#form', { action => "use_as_new" } ],
1474 checks => [ 'kivi.validate_form' ],
1475 disabled => !$may_edit_create ? t8('You must not change this AP transaction.')
1476 : !$::form->{id} ? t8('This invoice has not been posted yet.')
1479 ], # end of combobox "Workflow"
1482 action => [ t8('more') ],
1485 call => [ 'set_history_window', $::form->{id} * 1, 'glid' ],
1486 disabled => !$::form->{id} ? t8('This invoice has not been posted yet.') : undef,
1490 call => [ 'follow_up_window' ],
1491 disabled => !$::form->{id} ? t8('This invoice has not been posted yet.') : undef,
1494 t8('Record templates'),
1495 call => [ 'kivi.RecordTemplate.popup', 'ap_transaction' ],
1496 disabled => !$may_edit_create ? t8('You must not change this AP transaction.') : undef,
1500 call => [ 'kivi.Draft.popup', 'ap', 'invoice', $::form->{draft_id}, $::form->{draft_description} ],
1501 disabled => !$may_edit_create ? t8('You must not change this AP transaction.')
1502 : $::form->{id} ? t8('This invoice has already been posted.')
1503 : $is_closed ? t8('The billing period has already been locked.')
1506 ], # end of combobox "more"
1509 $::request->layout->add_javascripts('kivi.Validator.js');