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 #======================================================================
31 # Accounts Receivables
33 #======================================================================
35 use POSIX qw(strftime);
36 use List::Util qw(sum first max);
37 use List::UtilsBy qw(sort_by);
40 use SL::Controller::Base;
50 use SL::DB::RecordTemplate;
52 use SL::Helper::Flash qw(flash);
53 use SL::ReportGenerator;
55 require "bin/mozilla/common.pl";
56 require "bin/mozilla/reportgenerator.pl";
61 # this is for our long dates
62 # $locale->text('January')
63 # $locale->text('February')
64 # $locale->text('March')
65 # $locale->text('April')
66 # $locale->text('May ')
67 # $locale->text('June')
68 # $locale->text('July')
69 # $locale->text('August')
70 # $locale->text('September')
71 # $locale->text('October')
72 # $locale->text('November')
73 # $locale->text('December')
75 # this is for our short month
76 # $locale->text('Jan')
77 # $locale->text('Feb')
78 # $locale->text('Mar')
79 # $locale->text('Apr')
80 # $locale->text('May')
81 # $locale->text('Jun')
82 # $locale->text('Jul')
83 # $locale->text('Aug')
84 # $locale->text('Sep')
85 # $locale->text('Oct')
86 # $locale->text('Nov')
87 # $locale->text('Dec')
89 sub load_record_template {
90 $::auth->assert('ar_transactions');
92 # Load existing template and verify that its one for this module.
93 my $template = SL::DB::RecordTemplate
94 ->new(id => $::form->{id})
96 with_object => [ qw(customer payment currency record_items record_items.chart) ],
99 die "invalid template type" unless $template->template_type eq 'ar_transaction';
101 $template->substitute_variables;
103 # Clean the current $::form before rebuilding it from the template.
104 delete @{ $::form }{ grep { !m{^(?:script|login)$}i } keys %{ $::form } };
106 # Fill $::form from the template.
107 my $today = DateTime->today_local;
108 $::form->{title} = "Add";
109 $::form->{currency} = $template->currency->name;
110 $::form->{direct_debit} = $template->direct_debit;
111 $::form->{globalproject_id} = $template->project_id;
112 $::form->{AR_chart_id} = $template->ar_ap_chart_id;
113 $::form->{transdate} = $today->to_kivitendo;
114 $::form->{duedate} = $today->to_kivitendo;
115 $::form->{rowcount} = @{ $template->items };
116 $::form->{paidaccounts} = 1;
117 $::form->{$_} = $template->$_ for qw(department_id ordnumber taxincluded employee_id notes);
119 if ($template->customer) {
120 $::form->{customer_id} = $template->customer_id;
121 $::form->{customer} = $template->customer->name;
122 $::form->{duedate} = $template->customer->payment->calc_date(reference_date => $today)->to_kivitendo if $template->customer->payment;
126 foreach my $item (@{ $template->items }) {
129 my $active_taxkey = $item->chart->get_active_taxkey;
130 my $taxes = SL::DB::Manager::Tax->get_all(
131 where => [ chart_categories => { like => '%' . $item->chart->category . '%' }],
132 sort_by => 'taxkey, rate',
135 my $tax = first { $item->tax_id == $_->id } @{ $taxes };
136 $tax //= first { $active_taxkey->tax_id == $_->id } @{ $taxes };
137 $tax //= $taxes->[0];
144 $::form->{"AR_amount_chart_id_${row}"} = $item->chart_id;
145 $::form->{"previous_AR_amount_chart_id_${row}"} = $item->chart_id;
146 $::form->{"amount_${row}"} = $::form->format_amount(\%::myconfig, $item->amount1, 2);
147 $::form->{"taxchart_${row}"} = $item->tax_id . '--' . $tax->rate;
148 $::form->{"project_id_${row}"} = $item->project_id;
151 flash('info', $::locale->text("The record template '#1' has been loaded.", $template->template_name));
154 keep_rows_without_amount => 1,
155 dont_add_new_row => 1,
159 sub save_record_template {
160 $::auth->assert('ar_transactions');
162 my $template = $::form->{record_template_id} ? SL::DB::RecordTemplate->new(id => $::form->{record_template_id})->load : SL::DB::RecordTemplate->new;
163 my $js = SL::ClientJS->new(controller => SL::Controller::Base->new);
164 my $new_name = $template->template_name_to_use($::form->{record_template_new_template_name});
166 $js->dialog->close('#record_template_dialog');
169 $_->{chart_id} && (($_->{tax_id} // '') ne '')
171 +{ chart_id => $::form->{"AR_amount_chart_id_${_}"},
172 amount1 => $::form->parse_amount(\%::myconfig, $::form->{"amount_${_}"}),
173 tax_id => (split m{--}, $::form->{"taxchart_${_}"})[0],
174 project_id => $::form->{"project_id_${_}"} || undef,
176 } (1..($::form->{rowcount} || 1));
178 $template->assign_attributes(
179 template_type => 'ar_transaction',
180 template_name => $new_name,
182 currency_id => SL::DB::Manager::Currency->find_by(name => $::form->{currency})->id,
183 ar_ap_chart_id => $::form->{AR_chart_id} || undef,
184 customer_id => $::form->{customer_id} || undef,
185 department_id => $::form->{department_id} || undef,
186 project_id => $::form->{globalproject_id} || undef,
187 employee_id => $::form->{employee_id} || undef,
188 taxincluded => $::form->{taxincluded} ? 1 : 0,
189 direct_debit => $::form->{direct_debit} ? 1 : 0,
190 ordnumber => $::form->{ordnumber},
191 notes => $::form->{notes},
201 ->flash('error', $::locale->text("Saving the record template '#1' failed.", $new_name))
206 ->flash('info', $::locale->text("The record template '#1' has been saved.", $new_name))
211 $main::lxdebug->enter_sub();
213 $main::auth->assert('ar_transactions');
215 my $form = $main::form;
216 my %myconfig = %main::myconfig;
219 if(!exists $form->{addition} && ($form->{id} ne "")) {
220 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
221 $form->{addition} = "ADDED";
224 # /saving the history
226 $form->{title} = "Add";
227 $form->{callback} = "ar.pl?action=add" unless $form->{callback};
229 AR->get_transdate(\%myconfig, $form);
230 $form->{initial_transdate} = $form->{transdate};
231 create_links(dont_save => 1);
232 $form->{transdate} = $form->{initial_transdate};
234 if ($form->{customer_id}) {
235 my $last_used_ar_chart = SL::DB::Customer->load_cached($form->{customer_id})->last_used_ar_chart;
236 $form->{"AR_amount_chart_id_1"} = $last_used_ar_chart->id if $last_used_ar_chart;
240 $main::lxdebug->leave_sub();
244 $main::lxdebug->enter_sub();
246 $main::auth->assert('ar_transactions');
248 my $form = $main::form;
250 # show history button
251 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
252 #/show hhistory button
253 $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
254 $form->{title} = "Edit";
259 $main::lxdebug->leave_sub();
263 $main::lxdebug->enter_sub();
265 $main::auth->assert('ar_transactions');
267 my $form = $main::form;
272 $main::lxdebug->leave_sub();
275 sub _retrieve_invoice_object {
276 return undef if !$::form->{id};
277 return $::form->{invoice_obj} if $::form->{invoice_obj} && $::form->{invoice_obj}->id == $::form->{id};
278 return SL::DB::Invoice->new(id => $::form->{id})->load;
282 $main::lxdebug->enter_sub();
284 $main::auth->assert('ar_transactions');
287 my $form = $main::form;
288 my %myconfig = %main::myconfig;
290 $form->create_links("AR", \%myconfig, "customer");
291 $form->{invoice_obj} = _retrieve_invoice_object();
294 if (!$params{dont_save}) {
295 %saved = map { ($_ => $form->{$_}) } qw(direct_debit id taxincluded);
296 $saved{duedate} = $form->{duedate} if $form->{duedate};
297 $saved{currency} = $form->{currency} if $form->{currency};
300 IS->get_customer(\%myconfig, \%$form);
302 $form->{$_} = $saved{$_} for keys %saved;
303 $form->{rowcount} = 1;
304 $form->{AR_chart_id} = $form->{acc_trans} && $form->{acc_trans}->{AR} ? $form->{acc_trans}->{AR}->[0]->{chart_id} : $form->{AR_links}->{AR}->[0]->{chart_id};
307 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
309 $form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
311 # build the popup menus
312 $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
314 AR->setup_form($form);
317 ($form->datetonum($form->{transdate}, \%myconfig) <=
318 $form->datetonum($form->{closedto}, \%myconfig));
320 $main::lxdebug->leave_sub();
324 $main::lxdebug->enter_sub();
326 $main::auth->assert('ar_transactions');
328 my $form = $main::form;
329 my %myconfig = %main::myconfig;
330 my $locale = $main::locale;
331 my $cgi = $::request->{cgi};
333 $form->{invoice_obj} = _retrieve_invoice_object();
335 my ($title, $readonly, $exchangerate, $rows);
336 my ($notes, $amount, $project);
338 $form->{initial_focus} = !($form->{amount_1} * 1) ? 'customer_id' : 'row_' . $form->{rowcount};
340 $title = $form->{title};
341 # $locale->text('Add Accounts Receivables Transaction')
342 # $locale->text('Edit Accounts Receivables Transaction')
343 $form->{title} = $locale->text("$title Accounts Receivables Transaction");
345 $readonly = ($form->{id}) ? "readonly" : "";
347 $form->{radier} = ($::instance_conf->get_ar_changeable == 2)
348 ? ($form->current_date(\%myconfig) eq $form->{gldate})
349 : ($::instance_conf->get_ar_changeable == 1);
350 $readonly = ($form->{radier}) ? "" : $readonly;
352 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'buy');
353 $form->{exchangerate} = $form->{forex} if $form->{forex};
355 # format exchangerate
356 $form->{exchangerate} = $form->{exchangerate} ? $form->format_amount(\%myconfig, $form->{exchangerate}) : '';
358 $rows = max 2, $form->numtextrows($form->{notes}, 50);
360 my @old_project_ids = grep { $_ } map { $form->{"project_id_$_"} } 1..$form->{rowcount};
362 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
364 "old_id" => \@old_project_ids },
365 "charts" => { "key" => "ALL_CHARTS",
366 "transdate" => $form->{transdate} },
367 "taxcharts" => { "key" => "ALL_TAXCHARTS",
368 "module" => "AR" },);
370 $form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
372 $_->{link_split} = { map { $_ => 1 } split/:/, $_->{link} } for @{ $form->{ALL_CHARTS} };
374 my %project_labels = map { $_->{id} => $_->{projectnumber} } @{ $form->{"ALL_PROJECTS"} };
376 my (@AR_paid_values, %AR_paid_labels);
377 my $default_ar_amount_chart_id;
379 foreach my $item (@{ $form->{ALL_CHARTS} }) {
380 if ($item->{link_split}{AR_amount}) {
381 $default_ar_amount_chart_id //= $item->{id};
383 } elsif ($item->{link_split}{AR_paid}) {
384 push(@AR_paid_values, $item->{accno});
385 $AR_paid_labels{$item->{accno}} = "$item->{accno}--$item->{description}";
389 my $follow_up_vc = $form->{customer_id} ? SL::DB::Customer->load_cached($form->{customer_id})->name : '';
390 my $follow_up_trans_info = "$form->{invnumber} ($follow_up_vc)";
392 $::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.File.js", "kivi.RecordTemplate.js");
394 my $transdate = $::form->{transdate} ? DateTime->from_kivitendo($::form->{transdate}) : DateTime->today_local;
398 for my $i (1 .. $form->{rowcount}) {
400 amount => $form->{"amount_$i"},
401 tax => $form->{"tax_$i"},
402 project_id => ($i==$form->{rowcount}) ? $form->{globalproject_id} : $form->{"project_id_$i"},
405 my (%taxchart_labels, @taxchart_values, $default_taxchart, $taxchart_to_use);
406 my $amount_chart_id = $form->{"AR_amount_chart_id_$i"} // $default_ar_amount_chart_id;
407 my $chart_has_changed = $::form->{"previous_AR_amount_chart_id_$i"} && ($amount_chart_id != $::form->{"previous_AR_amount_chart_id_$i"});
409 foreach my $item ( GL->get_active_taxes_for_chart($amount_chart_id, $transdate) ) {
410 my $key = $item->id . "--" . $item->rate;
411 $first_taxchart //= $item;
412 $default_taxchart = $item if $item->{is_default};
413 $taxchart_to_use = $item if $key eq $form->{"taxchart_$i"};
415 push(@taxchart_values, $key);
416 $taxchart_labels{$key} = $item->taxdescription . " " . $item->rate * 100 . ' %';
419 $taxchart_to_use = $default_taxchart // $first_taxchart if $chart_has_changed || !$taxchart_to_use;
420 my $selected_taxchart = $taxchart_to_use->id . '--' . $taxchart_to_use->rate;
422 $transaction->{selectAR_amount} =
423 $::request->presenter->chart_picker("AR_amount_chart_id_$i", $amount_chart_id, style => "width: 400px", type => "AR_amount", class => ($form->{initial_focus} eq "row_$i" ? "initial_focus" : ""))
424 . $::request->presenter->hidden_tag("previous_AR_amount_chart_id_$i", $amount_chart_id);
426 $transaction->{taxchart} =
427 NTI($cgi->popup_menu('-name' => "taxchart_$i",
428 '-id' => "taxchart_$i",
429 '-style' => 'width:200px',
430 '-values' => \@taxchart_values,
431 '-labels' => \%taxchart_labels,
432 '-default' => $selected_taxchart));
434 push @transactions, $transaction;
437 $form->{invtotal_unformatted} = $form->{invtotal};
439 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
441 my $now = $form->current_date(\%myconfig);
444 for my $i (1 .. $form->{paidaccounts}) {
446 paid => $form->{"paid_$i"},
447 exchangerate => $form->{"exchangerate_$i"} || '',
448 gldate => $form->{"gldate_$i"},
449 acc_trans_id => $form->{"acc_trans_id_$i"},
450 source => $form->{"source_$i"},
451 memo => $form->{"memo_$i"},
452 AR_paid => $form->{"AR_paid_$i"},
453 forex => $form->{"forex_$i"},
454 datepaid => $form->{"datepaid_$i"},
455 paid_project_id => $form->{"paid_project_id_$i"},
456 gldate => $form->{"gldate_$i"},
459 # default account for current assets (i.e. 1801 - SKR04) if no account is selected
460 $form->{accno_arap} = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
462 $payment->{selectAR_paid} =
463 NTI($cgi->popup_menu('-name' => "AR_paid_$i",
464 '-id' => "AR_paid_$i",
465 '-values' => \@AR_paid_values,
466 '-labels' => \%AR_paid_labels,
467 '-default' => $payment->{AR_paid} || $form->{accno_arap}));
471 $payment->{changeable} =
472 SL::DB::Default->get->payments_changeable == 0 ? !$payment->{acc_trans_id} # never
473 : SL::DB::Default->get->payments_changeable == 2 ? $payment->{gldate} eq '' || $payment->{gldate} eq $now
476 #deaktivieren von gebuchten Zahlungen ausserhalb der Bücherkontrolle, vorher prüfen ob heute eingegeben
477 if ($form->date_closed($payment->{"gldate_$i"})) {
478 $payment->{changeable} = 0;
481 push @payments, $payment;
484 my @empty = grep { $_->{paid} eq '' } @payments;
486 (sort_by { DateTime->from_kivitendo($_->{datepaid}) } grep { $_->{paid} ne '' } @payments),
490 $form->{totalpaid} = sum map { $_->{paid} } @payments;
492 my $employees = SL::DB::Manager::Employee->get_all_sorted(
495 (id => $::form->{employee_id}) x !!$::form->{employee_id},
503 print $::form->parse_html_template('ar/form_header', {
504 paid_missing => $::form->{invtotal} - $::form->{totalpaid},
505 show_exch => ($::form->{defaultcurrency} && ($::form->{currency} ne $::form->{defaultcurrency})),
506 payments => \@payments,
507 transactions => \@transactions,
508 project_labels => \%project_labels,
510 AR_chart_id => $form->{AR_chart_id},
512 follow_up_trans_info => $follow_up_trans_info,
513 today => DateTime->today,
514 currencies => scalar(SL::DB::Manager::Currency->get_all_sorted),
515 employees => $employees,
518 $main::lxdebug->leave_sub();
522 $main::lxdebug->enter_sub();
524 $main::auth->assert('ar_transactions');
526 my $form = $main::form;
527 my %myconfig = %main::myconfig;
528 my $locale = $main::locale;
529 my $cgi = $::request->{cgi};
532 my $follow_ups = FU->follow_ups('trans_id' => $form->{id}, 'not_done' => 1);
533 if ( @{ $follow_ups} ) {
534 $form->{follow_up_length} = scalar(@{$follow_ups});
535 $form->{follow_up_due_length} = sum(map({ $_->{due} * 1 } @{ $follow_ups }));
539 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
540 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
542 $form->{is_closed} = $transdate <= $closedto;
544 # ToDO: - insert a global check for stornos, so that a storno is only possible a limited time after saving it
545 $form->{show_storno_button} =
547 !IS->has_storno(\%myconfig, $form, 'ar') &&
548 !IS->is_storno(\%myconfig, $form, 'ar') &&
549 ($form->{totalpaid} == 0 || $form->{totalpaid} eq "");
551 $form->{show_mark_as_paid_button} = $form->{id} && $::instance_conf->get_ar_show_mark_as_paid();
553 print $::form->parse_html_template('ar/form_footer');
555 $main::lxdebug->leave_sub();
559 $::auth->assert('ar_transactions');
561 SL::DB::Invoice->new(id => $::form->{id})->load->mark_as_paid;
563 $::form->redirect($::locale->text("Marked as paid"));
567 $::form->{transdate} = DateTime->today_local->to_kivitendo if !$::form->{transdate};
568 $::form->{gldate} = $::form->{transdate} if !$::form->{gldate};
574 $main::lxdebug->enter_sub();
576 $main::auth->assert('ar_transactions');
578 my $form = $main::form;
579 my %myconfig = %main::myconfig;
583 my ($totaltax, $exchangerate);
585 $form->{invtotal} = 0;
587 delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
589 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
590 qw(exchangerate creditlimit creditremaining);
592 my @flds = qw(amount AR_amount projectnumber oldprojectnumber project_id);
596 for my $i (1 .. $form->{rowcount}) {
597 $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
598 if ($form->{"amount_$i"} || $params{keep_rows_without_amount}) {
601 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
604 ($tmpnetamount,$form->{"tax_$i"}) = $form->calculate_tax($form->{"amount_$i"},$rate,$form->{taxincluded},2);
606 $totaltax += $form->{"tax_$i"};
607 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
612 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
613 $form->{rowcount} = $count + ($params{dont_add_new_row} ? 0 : 1);
614 map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
616 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'buy');
617 $form->{exchangerate} = $form->{forex} if $form->{forex};
619 $form->{invdate} = $form->{transdate};
621 if (($form->{previous_customer_id} || $form->{customer_id}) != $form->{customer_id}) {
622 IS->get_customer(\%myconfig, $form);
626 ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
628 for my $i (1 .. $form->{paidaccounts}) {
629 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
632 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
633 } qw(paid exchangerate);
635 $form->{totalpaid} += $form->{"paid_$i"};
637 $form->{"forex_$i"} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
638 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
642 $form->{creditremaining} -=
643 ($form->{invtotal} - $form->{totalpaid} + $form->{oldtotalpaid} -
644 $form->{oldinvtotal});
645 $form->{oldinvtotal} = $form->{invtotal};
646 $form->{oldtotalpaid} = $form->{totalpaid};
650 $main::lxdebug->leave_sub();
654 # ToDO: fix $closedto and $invdate
657 $main::lxdebug->enter_sub();
659 $main::auth->assert('ar_transactions');
661 my $form = $main::form;
662 my %myconfig = %main::myconfig;
663 my $locale = $main::locale;
665 $form->mtime_ischanged('ar');
666 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
668 my $invdate = $form->datetonum($form->{transdate}, \%myconfig);
670 for my $i (1 .. $form->{paidaccounts}) {
672 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
673 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
675 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
677 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
678 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
680 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
681 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
682 $form->error($locale->text('Cannot post payment for a closed period!'))
683 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
685 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
686 # $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid);
687 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
692 ($form->{AR}) = split /--/, $form->{AR};
693 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
694 if (AR->post_payment(\%myconfig, \%$form)) {
695 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
696 $form->{what_done} = 'invoice';
697 $form->{addition} = "PAYMENT POSTED";
699 $form->redirect($locale->text('Payment posted!'))
701 $form->error($locale->text('Cannot post payment!'));
704 $main::lxdebug->leave_sub();
709 $main::auth->assert('ar_transactions');
711 my $form = $main::form;
718 $main::lxdebug->enter_sub();
720 $main::auth->assert('ar_transactions');
722 my $form = $main::form;
723 my %myconfig = %main::myconfig;
724 my $locale = $main::locale;
728 $form->mtime_ischanged('ar');
732 # check if there is an invoice number, invoice and due date
733 $form->isblank("transdate", $locale->text('Invoice Date missing!'));
734 $form->isblank("duedate", $locale->text('Due Date missing!'));
735 $form->isblank("customer_id", $locale->text('Customer missing!'));
737 if ($myconfig{mandatory_departments} && !$form->{department_id}) {
738 $form->{saved_message} = $::locale->text('You have to specify a department.');
743 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
744 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
746 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
747 if ($form->date_max_future($transdate, \%myconfig));
749 $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
751 $form->error($locale->text('Zero amount posting!'))
752 unless grep $_*1, map $form->parse_amount(\%myconfig, $form->{"amount_$_"}), 1..$form->{rowcount};
754 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
755 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
759 for my $i (1 .. $form->{paidaccounts}) {
760 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
761 $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
763 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
765 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
766 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
768 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
769 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
770 $form->error($locale->text('Cannot post payment for a closed period!'))
771 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
773 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
774 $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
775 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
780 # if oldcustomer ne customer redo form
781 if (($form->{previous_customer_id} || $form->{customer_id}) != $form->{customer_id}) {
783 $::dispatcher->end_request;
786 $form->{AR}{receivables} = $form->{ARselected};
789 $form->{id} = 0 if $form->{postasnew};
790 $form->error($locale->text('Cannot post transaction!')) unless AR->post_transaction(\%myconfig, \%$form);
793 if(!exists $form->{addition} && $form->{id} ne "") {
794 $form->{snumbers} = "invnumber_$form->{invnumber}";
795 $form->{what_done} = "invoice";
796 $form->{addition} = "POSTED";
799 # /saving the history
801 $form->redirect($locale->text("AR transaction posted.")) unless $inline;
803 $main::lxdebug->leave_sub();
807 $main::lxdebug->enter_sub();
809 $main::auth->assert('ar_transactions');
811 my $form = $main::form;
812 my %myconfig = %main::myconfig;
814 $form->{postasnew} = 1;
816 if(!exists $form->{addition} && $form->{id} ne "") {
817 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
818 $form->{what_done} = "invoice";
819 $form->{addition} = "POSTED AS NEW";
822 # /saving the history
825 $main::lxdebug->leave_sub();
829 $main::lxdebug->enter_sub();
831 $main::auth->assert('ar_transactions');
833 my $form = $main::form;
834 my %myconfig = %main::myconfig;
836 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);
837 $form->{paidaccounts} = 1;
839 $form->{invdate} = $form->current_date(\%myconfig);
842 $main::lxdebug->leave_sub();
846 $main::lxdebug->enter_sub();
848 $main::auth->assert('ar_transactions');
850 my $form = $main::form;
851 my $locale = $main::locale;
853 $form->{title} = $locale->text('Confirm!');
857 delete $form->{header};
860 <form method=post action=$form->{script}>
863 foreach my $key (keys %$form) {
864 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
865 $form->{$key} =~ s/\"/"/g;
866 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
870 <h2 class=confirm>$form->{title}</h2>
873 . $locale->text('Are you sure you want to delete Transaction')
874 . qq| $form->{invnumber}</h4>
876 <input name=action class=submit type=submit value="|
877 . $locale->text('Yes') . qq|">
881 $main::lxdebug->leave_sub();
885 $main::lxdebug->enter_sub();
887 $main::auth->assert('ar_transactions');
889 my $form = $main::form;
890 my %myconfig = %main::myconfig;
891 my $locale = $main::locale;
893 if (AR->delete_transaction(\%myconfig, \%$form)) {
895 if(!exists $form->{addition}) {
896 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
897 $form->{what_done} = "invoice";
898 $form->{addition} = "DELETED";
901 # /saving the history
902 $form->redirect($locale->text('Transaction deleted!'));
904 $form->error($locale->text('Cannot delete transaction!'));
906 $main::lxdebug->leave_sub();
910 $main::lxdebug->enter_sub();
912 $main::auth->assert('invoice_edit');
914 my $form = $main::form;
915 my %myconfig = %main::myconfig;
916 my $locale = $main::locale;
917 my $cgi = $::request->{cgi};
919 $form->{title} = $locale->text('AR Transactions');
921 $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
922 $form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
923 $form->{ALL_BUSINESS_TYPES} = SL::DB::Manager::Business->get_all_sorted;
925 $form->{CT_CUSTOM_VARIABLES} = CVar->get_configs('module' => 'CT');
926 ($form->{CT_CUSTOM_VARIABLES_FILTER_CODE},
927 $form->{CT_CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $form->{CT_CUSTOM_VARIABLES},
928 'include_prefix' => 'l_',
929 'include_value' => 'Y');
931 # constants and subs for template
932 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
934 $::request->layout->add_javascripts("autocomplete_project.js");
937 print $form->parse_html_template('ar/search', { %myconfig });
939 $main::lxdebug->leave_sub();
942 sub create_subtotal_row {
943 $main::lxdebug->enter_sub();
945 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
947 my $form = $main::form;
948 my %myconfig = %main::myconfig;
950 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
952 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
954 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
956 map { $totals->{$_} = 0 } @{ $subtotal_columns };
958 $main::lxdebug->leave_sub();
963 sub ar_transactions {
964 $main::lxdebug->enter_sub();
966 $main::auth->assert('invoice_edit');
968 my $form = $main::form;
969 my %myconfig = %main::myconfig;
970 my $locale = $main::locale;
972 my ($callback, $href, @columns);
974 report_generator_set_default_sort('transdate', 1);
976 AR->ar_transactions(\%myconfig, \%$form);
978 $form->{title} = $locale->text('AR Transactions');
980 my $report = SL::ReportGenerator->new(\%myconfig, $form);
983 qw(ids transdate id type invnumber ordnumber cusordnumber name netamount tax amount paid
984 datepaid due duedate transaction_description notes salesman employee shippingpoint shipvia
985 marge_total marge_percent globalprojectnumber customernumber country ustid taxzone
986 payment_terms charts customertype direct_debit dunning_description department);
988 my $ct_cvar_configs = CVar->get_configs('module' => 'CT');
989 my @ct_includeable_custom_variables = grep { $_->{includeable} } @{ $ct_cvar_configs };
990 my @ct_searchable_custom_variables = grep { $_->{searchable} } @{ $ct_cvar_configs };
992 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @ct_includeable_custom_variables;
993 push @columns, map { "cvar_$_->{name}" } @ct_includeable_custom_variables;
995 my @hidden_variables = map { "l_${_}" } @columns;
996 push @hidden_variables, "l_subtotal", qw(open closed customer invnumber ordnumber cusordnumber transaction_description notes project_id transdatefrom transdateto duedatefrom duedateto
997 employee_id salesman_id business_id parts_partnumber parts_description department_id);
998 push @hidden_variables, map { "cvar_$_->{name}" } @ct_searchable_custom_variables;
1000 $href = build_std_url('action=ar_transactions', grep { $form->{$_} } @hidden_variables);
1003 'ids' => { raw_header_data => $::request->presenter->checkbox_tag("", id => "check_all", checkall => "[data-checkall=1]"), align => 'center' },
1004 'transdate' => { 'text' => $locale->text('Date'), },
1005 'id' => { 'text' => $locale->text('ID'), },
1006 'type' => { 'text' => $locale->text('Type'), },
1007 'invnumber' => { 'text' => $locale->text('Invoice'), },
1008 'ordnumber' => { 'text' => $locale->text('Order'), },
1009 'cusordnumber' => { 'text' => $locale->text('Customer Order Number'), },
1010 'name' => { 'text' => $locale->text('Customer'), },
1011 'netamount' => { 'text' => $locale->text('Amount'), },
1012 'tax' => { 'text' => $locale->text('Tax'), },
1013 'amount' => { 'text' => $locale->text('Total'), },
1014 'paid' => { 'text' => $locale->text('Paid'), },
1015 'datepaid' => { 'text' => $locale->text('Date Paid'), },
1016 'due' => { 'text' => $locale->text('Amount Due'), },
1017 'duedate' => { 'text' => $locale->text('Due Date'), },
1018 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
1019 'notes' => { 'text' => $locale->text('Notes'), },
1020 'salesman' => { 'text' => $locale->text('Salesperson'), },
1021 'employee' => { 'text' => $locale->text('Employee'), },
1022 'shippingpoint' => { 'text' => $locale->text('Shipping Point'), },
1023 'shipvia' => { 'text' => $locale->text('Ship via'), },
1024 'globalprojectnumber' => { 'text' => $locale->text('Document Project Number'), },
1025 'marge_total' => { 'text' => $locale->text('Ertrag'), },
1026 'marge_percent' => { 'text' => $locale->text('Ertrag prozentual'), },
1027 'customernumber' => { 'text' => $locale->text('Customer Number'), },
1028 'country' => { 'text' => $locale->text('Country'), },
1029 'ustid' => { 'text' => $locale->text('USt-IdNr.'), },
1030 'taxzone' => { 'text' => $locale->text('Steuersatz'), },
1031 'payment_terms' => { 'text' => $locale->text('Payment Terms'), },
1032 'charts' => { 'text' => $locale->text('Buchungskonto'), },
1033 'customertype' => { 'text' => $locale->text('Customer type'), },
1034 'direct_debit' => { 'text' => $locale->text('direct debit'), },
1035 'department' => { 'text' => $locale->text('Department'), },
1036 dunning_description => { 'text' => $locale->text('Dunning level'), },
1040 foreach my $name (qw(id transdate duedate invnumber ordnumber cusordnumber name datepaid employee shippingpoint shipvia transaction_description direct_debit)) {
1041 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
1042 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
1045 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
1047 $form->{"l_type"} = "Y";
1048 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
1050 $column_defs{ids}->{visible} = 'HTML';
1052 $report->set_columns(%column_defs);
1053 $report->set_column_order(@columns);
1055 $report->set_export_options('ar_transactions', @hidden_variables, qw(sort sortdir));
1057 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
1059 CVar->add_custom_variables_to_report('module' => 'CT',
1060 'trans_id_field' => 'customer_id',
1061 'configs' => $ct_cvar_configs,
1062 'column_defs' => \%column_defs,
1063 'data' => $form->{AR});
1066 if ($form->{customer}) {
1067 push @options, $locale->text('Customer') . " : $form->{customer}";
1069 if ($form->{cp_name}) {
1070 push @options, $locale->text('Contact Person') . " : $form->{cp_name}";
1073 if ($form->{department_id}) {
1074 my $department = SL::DB::Manager::Department->find_by( id => $form->{department_id} );
1075 push @options, $locale->text('Department') . " : " . $department->description;
1077 if ($form->{invnumber}) {
1078 push @options, $locale->text('Invoice Number') . " : $form->{invnumber}";
1080 if ($form->{ordnumber}) {
1081 push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
1083 if ($form->{cusordnumber}) {
1084 push @options, $locale->text('Customer Order Number') . " : $form->{cusordnumber}";
1086 if ($form->{notes}) {
1087 push @options, $locale->text('Notes') . " : $form->{notes}";
1089 if ($form->{transaction_description}) {
1090 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
1092 if ($form->{parts_partnumber}) {
1093 push @options, $locale->text('Part Number') . " : $form->{parts_partnumber}";
1095 if ($form->{parts_description}) {
1096 push @options, $locale->text('Part Description') . " : $form->{parts_description}";
1098 if ($form->{transdatefrom}) {
1099 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1101 if ($form->{transdateto}) {
1102 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
1104 if ($form->{open}) {
1105 push @options, $locale->text('Open');
1107 if ($form->{employee_id}) {
1108 my $employee = SL::DB::Employee->new(id => $form->{employee_id})->load;
1109 push @options, $locale->text('Employee') . ' : ' . $employee->name;
1111 if ($form->{salesman_id}) {
1112 my $salesman = SL::DB::Employee->new(id => $form->{salesman_id})->load;
1113 push @options, $locale->text('Salesman') . ' : ' . $salesman->name;
1115 if ($form->{closed}) {
1116 push @options, $locale->text('Closed');
1119 $form->{ALL_PRINTERS} = SL::DB::Manager::Printer->get_all_sorted;
1121 $report->set_options('top_info_text' => join("\n", @options),
1122 'raw_top_info_text' => $form->parse_html_template('ar/ar_transactions_header'),
1123 'raw_bottom_info_text' => $form->parse_html_template('ar/ar_transactions_bottom'),
1124 'output_format' => 'HTML',
1125 'title' => $form->{title},
1126 'attachment_basename' => $locale->text('invoice_list') . strftime('_%Y%m%d', localtime time),
1128 $report->set_options_from_form();
1129 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1131 # add sort and escape callback, this one we use for the add sub
1132 $form->{callback} = $href .= "&sort=$form->{sort}";
1134 # escape callback for href
1135 $callback = $form->escape($href);
1137 my @subtotal_columns = qw(netamount amount paid due marge_total marge_percent);
1139 my %totals = map { $_ => 0 } @subtotal_columns;
1140 my %subtotals = map { $_ => 0 } @subtotal_columns;
1144 foreach my $ar (@{ $form->{AR} }) {
1145 $ar->{tax} = $ar->{amount} - $ar->{netamount};
1146 $ar->{due} = $ar->{amount} - $ar->{paid};
1148 map { $subtotals{$_} += $ar->{$_};
1149 $totals{$_} += $ar->{$_} } @subtotal_columns;
1151 $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
1152 $totals{marge_percent} = $totals{netamount} ? ($totals{marge_total} * 100 / $totals{netamount} ) : 0;
1154 my $is_storno = $ar->{storno} && $ar->{storno_id};
1155 my $has_storno = $ar->{storno} && !$ar->{storno_id};
1158 $has_storno ? $locale->text("Invoice with Storno (abbreviation)") :
1159 $is_storno ? $locale->text("Storno (one letter abbreviation)") :
1160 $ar->{amount} < 0 ? $locale->text("Credit note (one letter abbreviation)") :
1161 $ar->{invoice} ? $locale->text("Invoice (one letter abbreviation)") :
1162 $locale->text("AR Transaction (abbreviation)");
1164 map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(netamount tax amount paid due marge_total marge_percent);
1166 $ar->{direct_debit} = $ar->{direct_debit} ? $::locale->text('yes') : $::locale->text('no');
1170 foreach my $column (@columns) {
1172 'data' => $ar->{$column},
1173 'align' => $column_alignment{$column},
1177 $row->{invnumber}->{link} = build_std_url("script=" . ($ar->{invoice} ? 'is.pl' : 'ar.pl'), 'action=edit')
1178 . "&id=" . E($ar->{id}) . "&callback=${callback}";
1181 raw_data => $::request->presenter->checkbox_tag("id[]", value => $ar->{id}, "data-checkall" => 1),
1186 my $row_set = [ $row ];
1188 if (($form->{l_subtotal} eq 'Y')
1189 && (($idx == (scalar @{ $form->{AR} } - 1))
1190 || ($ar->{ $form->{sort} } ne $form->{AR}->[$idx + 1]->{ $form->{sort} }))) {
1191 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1194 $report->add_data($row_set);
1199 $report->add_separator();
1200 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1202 $report->generate_with_headers();
1204 $main::lxdebug->leave_sub();
1208 $main::lxdebug->enter_sub();
1210 $main::auth->assert('ar_transactions');
1212 my $form = $main::form;
1213 my %myconfig = %main::myconfig;
1214 my $locale = $main::locale;
1216 # don't cancel cancelled transactions
1217 if (IS->has_storno(\%myconfig, $form, 'ar')) {
1218 $form->{title} = $locale->text("Cancel Accounts Receivables Transaction");
1219 $form->error($locale->text("Transaction has already been cancelled!"));
1222 AR->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();