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);
48 use SL::ReportGenerator;
50 require "bin/mozilla/common.pl";
51 require "bin/mozilla/reportgenerator.pl";
56 # this is for our long dates
57 # $locale->text('January')
58 # $locale->text('February')
59 # $locale->text('March')
60 # $locale->text('April')
61 # $locale->text('May ')
62 # $locale->text('June')
63 # $locale->text('July')
64 # $locale->text('August')
65 # $locale->text('September')
66 # $locale->text('October')
67 # $locale->text('November')
68 # $locale->text('December')
70 # this is for our short month
71 # $locale->text('Jan')
72 # $locale->text('Feb')
73 # $locale->text('Mar')
74 # $locale->text('Apr')
75 # $locale->text('May')
76 # $locale->text('Jun')
77 # $locale->text('Jul')
78 # $locale->text('Aug')
79 # $locale->text('Sep')
80 # $locale->text('Oct')
81 # $locale->text('Nov')
82 # $locale->text('Dec')
85 $main::lxdebug->enter_sub();
87 $main::auth->assert('ar_transactions');
89 my $form = $main::form;
90 my %myconfig = %main::myconfig;
93 if(!exists $form->{addition} && ($form->{id} ne "")) {
94 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
95 $form->{addition} = "ADDED";
100 $form->{title} = "Add";
101 $form->{callback} = "ar.pl?action=add" unless $form->{callback};
103 AR->get_transdate(\%myconfig, $form);
104 $form->{initial_transdate} = $form->{transdate};
105 create_links(dont_save => 1);
106 $form->{transdate} = $form->{initial_transdate};
108 if ($form->{customer_id}) {
109 my $last_used_ar_chart = SL::DB::Customer->load_cached($form->{customer_id})->last_used_ar_chart;
110 $form->{"AR_amount_chart_id_1"} = $last_used_ar_chart->id if $last_used_ar_chart;
114 $main::lxdebug->leave_sub();
118 $main::lxdebug->enter_sub();
120 $main::auth->assert('ar_transactions');
122 my $form = $main::form;
124 # show history button
125 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
126 #/show hhistory button
127 $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
128 $form->{title} = "Edit";
133 $main::lxdebug->leave_sub();
137 $main::lxdebug->enter_sub();
139 $main::auth->assert('ar_transactions');
141 my $form = $main::form;
146 $main::lxdebug->leave_sub();
149 sub _retrieve_invoice_object {
150 return undef if !$::form->{id};
151 return $::form->{invoice_obj} if $::form->{invoice_obj} && $::form->{invoice_obj}->id == $::form->{id};
152 return SL::DB::Invoice->new(id => $::form->{id})->load;
156 $main::lxdebug->enter_sub();
158 $main::auth->assert('ar_transactions');
161 my $form = $main::form;
162 my %myconfig = %main::myconfig;
164 $form->create_links("AR", \%myconfig, "customer");
165 $form->{invoice_obj} = _retrieve_invoice_object();
168 if (!$params{dont_save}) {
169 %saved = map { ($_ => $form->{$_}) } qw(direct_debit id taxincluded);
170 $saved{duedate} = $form->{duedate} if $form->{duedate};
171 $saved{currency} = $form->{currency} if $form->{currency};
174 IS->get_customer(\%myconfig, \%$form);
176 $form->{$_} = $saved{$_} for keys %saved;
177 $form->{rowcount} = 1;
178 $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};
181 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
183 $form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
185 # build the popup menus
186 $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
188 AR->setup_form($form);
191 ($form->datetonum($form->{transdate}, \%myconfig) <=
192 $form->datetonum($form->{closedto}, \%myconfig));
194 $main::lxdebug->leave_sub();
198 $main::lxdebug->enter_sub();
200 $main::auth->assert('ar_transactions');
202 my $form = $main::form;
203 my %myconfig = %main::myconfig;
204 my $locale = $main::locale;
205 my $cgi = $::request->{cgi};
207 $form->{invoice_obj} = _retrieve_invoice_object();
209 my ($title, $readonly, $exchangerate, $rows);
210 my ($notes, $amount, $project);
212 $form->{initial_focus} = !($form->{amount_1} * 1) ? 'customer_id' : 'row_' . $form->{rowcount};
214 $title = $form->{title};
215 # $locale->text('Add Accounts Receivables Transaction')
216 # $locale->text('Edit Accounts Receivables Transaction')
217 $form->{title} = $locale->text("$title Accounts Receivables Transaction");
219 $readonly = ($form->{id}) ? "readonly" : "";
221 $form->{radier} = ($::instance_conf->get_ar_changeable == 2)
222 ? ($form->current_date(\%myconfig) eq $form->{gldate})
223 : ($::instance_conf->get_ar_changeable == 1);
224 $readonly = ($form->{radier}) ? "" : $readonly;
226 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'buy');
227 $form->{exchangerate} = $form->{forex} if $form->{forex};
229 # format exchangerate
230 $form->{exchangerate} = $form->{exchangerate} ? $form->format_amount(\%myconfig, $form->{exchangerate}) : '';
232 $rows = max 2, $form->numtextrows($form->{notes}, 50);
234 my @old_project_ids = grep { $_ } map { $form->{"project_id_$_"} } 1..$form->{rowcount};
236 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
238 "old_id" => \@old_project_ids },
239 "charts" => { "key" => "ALL_CHARTS",
240 "transdate" => $form->{transdate} },
241 "taxcharts" => { "key" => "ALL_TAXCHARTS",
242 "module" => "AR" },);
244 $form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
246 $_->{link_split} = { map { $_ => 1 } split/:/, $_->{link} } for @{ $form->{ALL_CHARTS} };
248 my %project_labels = map { $_->{id} => $_->{projectnumber} } @{ $form->{"ALL_PROJECTS"} };
250 my (@AR_paid_values, %AR_paid_labels);
251 my $default_ar_amount_chart_id;
253 foreach my $item (@{ $form->{ALL_CHARTS} }) {
254 if ($item->{link_split}{AR_amount}) {
255 $default_ar_amount_chart_id //= $item->{id};
257 } elsif ($item->{link_split}{AR_paid}) {
258 push(@AR_paid_values, $item->{accno});
259 $AR_paid_labels{$item->{accno}} = "$item->{accno}--$item->{description}";
263 my $follow_up_vc = $form->{customer_id} ? SL::DB::Customer->load_cached($form->{customer_id})->name : '';
264 my $follow_up_trans_info = "$form->{invnumber} ($follow_up_vc)";
266 $::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");
268 my $transdate = $::form->{transdate} ? DateTime->from_kivitendo($::form->{transdate}) : DateTime->today_local;
272 for my $i (1 .. $form->{rowcount}) {
274 amount => $form->{"amount_$i"},
275 tax => $form->{"tax_$i"},
276 project_id => ($i==$form->{rowcount}) ? $form->{globalproject_id} : $form->{"project_id_$i"},
279 my (%taxchart_labels, @taxchart_values, $default_taxchart, $taxchart_to_use);
280 my $amount_chart_id = $form->{"AR_amount_chart_id_$i"} // $default_ar_amount_chart_id;
281 my $chart_has_changed = $::form->{"previous_AR_amount_chart_id_$i"} && ($amount_chart_id != $::form->{"previous_AR_amount_chart_id_$i"});
283 foreach my $item ( GL->get_active_taxes_for_chart($amount_chart_id, $transdate) ) {
284 my $key = $item->id . "--" . $item->rate;
285 $first_taxchart //= $item;
286 $default_taxchart = $item if $item->{is_default};
287 $taxchart_to_use = $item if $key eq $form->{"taxchart_$i"};
289 push(@taxchart_values, $key);
290 $taxchart_labels{$key} = $item->taxdescription . " " . $item->rate * 100 . ' %';
293 $taxchart_to_use = $default_taxchart // $first_taxchart if $chart_has_changed || !$taxchart_to_use;
294 my $selected_taxchart = $taxchart_to_use->id . '--' . $taxchart_to_use->rate;
296 $transaction->{selectAR_amount} =
297 $::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" : ""))
298 . $::request->presenter->hidden_tag("previous_AR_amount_chart_id_$i", $amount_chart_id);
300 $transaction->{taxchart} =
301 NTI($cgi->popup_menu('-name' => "taxchart_$i",
302 '-id' => "taxchart_$i",
303 '-style' => 'width:200px',
304 '-values' => \@taxchart_values,
305 '-labels' => \%taxchart_labels,
306 '-default' => $selected_taxchart));
308 push @transactions, $transaction;
311 $form->{invtotal_unformatted} = $form->{invtotal};
313 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
315 my $now = $form->current_date(\%myconfig);
318 for my $i (1 .. $form->{paidaccounts}) {
320 paid => $form->{"paid_$i"},
321 exchangerate => $form->{"exchangerate_$i"} || '',
322 gldate => $form->{"gldate_$i"},
323 acc_trans_id => $form->{"acc_trans_id_$i"},
324 source => $form->{"source_$i"},
325 memo => $form->{"memo_$i"},
326 AR_paid => $form->{"AR_paid_$i"},
327 forex => $form->{"forex_$i"},
328 datepaid => $form->{"datepaid_$i"},
329 paid_project_id => $form->{"paid_project_id_$i"},
330 gldate => $form->{"gldate_$i"},
333 # default account for current assets (i.e. 1801 - SKR04) if no account is selected
334 $form->{accno_arap} = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
336 $payment->{selectAR_paid} =
337 NTI($cgi->popup_menu('-name' => "AR_paid_$i",
338 '-id' => "AR_paid_$i",
339 '-values' => \@AR_paid_values,
340 '-labels' => \%AR_paid_labels,
341 '-default' => $payment->{AR_paid} || $form->{accno_arap}));
345 $payment->{changeable} =
346 SL::DB::Default->get->payments_changeable == 0 ? !$payment->{acc_trans_id} # never
347 : SL::DB::Default->get->payments_changeable == 2 ? $payment->{gldate} eq '' || $payment->{gldate} eq $now
350 #deaktivieren von gebuchten Zahlungen ausserhalb der Bücherkontrolle, vorher prüfen ob heute eingegeben
351 if ($form->date_closed($payment->{"gldate_$i"})) {
352 $payment->{changeable} = 0;
355 push @payments, $payment;
358 my @empty = grep { $_->{paid} eq '' } @payments;
360 (sort_by { DateTime->from_kivitendo($_->{datepaid}) } grep { $_->{paid} ne '' } @payments),
364 $form->{totalpaid} = sum map { $_->{paid} } @payments;
366 my $employees = SL::DB::Manager::Employee->get_all_sorted(
369 (id => $::form->{employee_id}) x !!$::form->{employee_id},
377 print $::form->parse_html_template('ar/form_header', {
378 paid_missing => $::form->{invtotal} - $::form->{totalpaid},
379 show_exch => ($::form->{defaultcurrency} && ($::form->{currency} ne $::form->{defaultcurrency})),
380 payments => \@payments,
381 transactions => \@transactions,
382 project_labels => \%project_labels,
384 AR_chart_id => $form->{AR_chart_id},
386 follow_up_trans_info => $follow_up_trans_info,
387 today => DateTime->today,
388 currencies => scalar(SL::DB::Manager::Currency->get_all_sorted),
389 employees => $employees,
392 $main::lxdebug->leave_sub();
396 $main::lxdebug->enter_sub();
398 $main::auth->assert('ar_transactions');
400 my $form = $main::form;
401 my %myconfig = %main::myconfig;
402 my $locale = $main::locale;
403 my $cgi = $::request->{cgi};
406 my $follow_ups = FU->follow_ups('trans_id' => $form->{id}, 'not_done' => 1);
407 if ( @{ $follow_ups} ) {
408 $form->{follow_up_length} = scalar(@{$follow_ups});
409 $form->{follow_up_due_length} = sum(map({ $_->{due} * 1 } @{ $follow_ups }));
413 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
414 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
416 $form->{is_closed} = $transdate <= $closedto;
418 # ToDO: - insert a global check for stornos, so that a storno is only possible a limited time after saving it
419 $form->{show_storno_button} =
421 !IS->has_storno(\%myconfig, $form, 'ar') &&
422 !IS->is_storno(\%myconfig, $form, 'ar') &&
423 ($form->{totalpaid} == 0 || $form->{totalpaid} eq "");
425 $form->{show_mark_as_paid_button} = $form->{id} && $::instance_conf->get_ar_show_mark_as_paid();
427 print $::form->parse_html_template('ar/form_footer');
429 $main::lxdebug->leave_sub();
433 $::auth->assert('ar_transactions');
435 SL::DB::Invoice->new(id => $::form->{id})->load->mark_as_paid;
437 $::form->redirect($::locale->text("Marked as paid"));
441 $::form->{transdate} = DateTime->today_local->to_kivitendo if !$::form->{transdate};
442 $::form->{gldate} = $::form->{transdate} if !$::form->{gldate};
447 $main::lxdebug->enter_sub();
449 $main::auth->assert('ar_transactions');
451 my $form = $main::form;
452 my %myconfig = %main::myconfig;
456 my ($totaltax, $exchangerate);
458 $form->{invtotal} = 0;
460 delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
462 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
463 qw(exchangerate creditlimit creditremaining);
465 my @flds = qw(amount AR_amount projectnumber oldprojectnumber project_id);
469 for my $i (1 .. $form->{rowcount}) {
470 $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
471 if ($form->{"amount_$i"}) {
474 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
477 ($tmpnetamount,$form->{"tax_$i"}) = $form->calculate_tax($form->{"amount_$i"},$rate,$form->{taxincluded},2);
479 $totaltax += $form->{"tax_$i"};
480 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
485 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
486 $form->{rowcount} = $count + 1;
487 map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
489 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'buy');
490 $form->{exchangerate} = $form->{forex} if $form->{forex};
492 $form->{invdate} = $form->{transdate};
494 if (($form->{previous_customer_id} || $form->{customer_id}) != $form->{customer_id}) {
495 IS->get_customer(\%myconfig, $form);
499 ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
501 for my $i (1 .. $form->{paidaccounts}) {
502 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
505 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
506 } qw(paid exchangerate);
508 $form->{totalpaid} += $form->{"paid_$i"};
510 $form->{"forex_$i"} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
511 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
515 $form->{creditremaining} -=
516 ($form->{invtotal} - $form->{totalpaid} + $form->{oldtotalpaid} -
517 $form->{oldinvtotal});
518 $form->{oldinvtotal} = $form->{invtotal};
519 $form->{oldtotalpaid} = $form->{totalpaid};
523 $main::lxdebug->leave_sub();
527 # ToDO: fix $closedto and $invdate
530 $main::lxdebug->enter_sub();
532 $main::auth->assert('ar_transactions');
534 my $form = $main::form;
535 my %myconfig = %main::myconfig;
536 my $locale = $main::locale;
538 $form->mtime_ischanged('ar');
539 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
541 my $invdate = $form->datetonum($form->{transdate}, \%myconfig);
543 for my $i (1 .. $form->{paidaccounts}) {
545 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
546 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
548 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
550 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
551 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
553 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
554 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
555 $form->error($locale->text('Cannot post payment for a closed period!'))
556 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
558 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
559 # $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid);
560 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
565 ($form->{AR}) = split /--/, $form->{AR};
566 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
567 if (AR->post_payment(\%myconfig, \%$form)) {
568 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
569 $form->{what_done} = 'invoice';
570 $form->{addition} = "PAYMENT POSTED";
572 $form->redirect($locale->text('Payment posted!'))
574 $form->error($locale->text('Cannot post payment!'));
577 $main::lxdebug->leave_sub();
582 $main::auth->assert('ar_transactions');
584 my $form = $main::form;
591 $main::lxdebug->enter_sub();
593 $main::auth->assert('ar_transactions');
595 my $form = $main::form;
596 my %myconfig = %main::myconfig;
597 my $locale = $main::locale;
601 $form->mtime_ischanged('ar');
605 # check if there is an invoice number, invoice and due date
606 $form->isblank("transdate", $locale->text('Invoice Date missing!'));
607 $form->isblank("duedate", $locale->text('Due Date missing!'));
608 $form->isblank("customer_id", $locale->text('Customer missing!'));
610 if ($myconfig{mandatory_departments} && !$form->{department_id}) {
611 $form->{saved_message} = $::locale->text('You have to specify a department.');
616 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
617 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
619 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
620 if ($form->date_max_future($transdate, \%myconfig));
622 $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
624 $form->error($locale->text('Zero amount posting!'))
625 unless grep $_*1, map $form->parse_amount(\%myconfig, $form->{"amount_$_"}), 1..$form->{rowcount};
627 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
628 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
632 for my $i (1 .. $form->{paidaccounts}) {
633 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
634 $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
636 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
638 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
639 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
641 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
642 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
643 $form->error($locale->text('Cannot post payment for a closed period!'))
644 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
646 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
647 $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
648 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
653 # if oldcustomer ne customer redo form
654 if (($form->{previous_customer_id} || $form->{customer_id}) != $form->{customer_id}) {
656 $::dispatcher->end_request;
659 $form->{AR}{receivables} = $form->{ARselected};
662 $form->{id} = 0 if $form->{postasnew};
663 $form->error($locale->text('Cannot post transaction!')) unless AR->post_transaction(\%myconfig, \%$form);
666 if(!exists $form->{addition} && $form->{id} ne "") {
667 $form->{snumbers} = "invnumber_$form->{invnumber}";
668 $form->{what_done} = "invoice";
669 $form->{addition} = "POSTED";
672 # /saving the history
674 $form->redirect($locale->text("AR transaction posted.")) unless $inline;
676 $main::lxdebug->leave_sub();
680 $main::lxdebug->enter_sub();
682 $main::auth->assert('ar_transactions');
684 my $form = $main::form;
685 my %myconfig = %main::myconfig;
687 $form->{postasnew} = 1;
689 if(!exists $form->{addition} && $form->{id} ne "") {
690 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
691 $form->{what_done} = "invoice";
692 $form->{addition} = "POSTED AS NEW";
695 # /saving the history
698 $main::lxdebug->leave_sub();
702 $main::lxdebug->enter_sub();
704 $main::auth->assert('ar_transactions');
706 my $form = $main::form;
707 my %myconfig = %main::myconfig;
709 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);
710 $form->{paidaccounts} = 1;
712 $form->{invdate} = $form->current_date(\%myconfig);
715 $main::lxdebug->leave_sub();
719 $main::lxdebug->enter_sub();
721 $main::auth->assert('ar_transactions');
723 my $form = $main::form;
724 my $locale = $main::locale;
726 $form->{title} = $locale->text('Confirm!');
730 delete $form->{header};
733 <form method=post action=$form->{script}>
736 foreach my $key (keys %$form) {
737 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
738 $form->{$key} =~ s/\"/"/g;
739 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
743 <h2 class=confirm>$form->{title}</h2>
746 . $locale->text('Are you sure you want to delete Transaction')
747 . qq| $form->{invnumber}</h4>
749 <input name=action class=submit type=submit value="|
750 . $locale->text('Yes') . qq|">
754 $main::lxdebug->leave_sub();
758 $main::lxdebug->enter_sub();
760 $main::auth->assert('ar_transactions');
762 my $form = $main::form;
763 my %myconfig = %main::myconfig;
764 my $locale = $main::locale;
766 if (AR->delete_transaction(\%myconfig, \%$form)) {
768 if(!exists $form->{addition}) {
769 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
770 $form->{what_done} = "invoice";
771 $form->{addition} = "DELETED";
774 # /saving the history
775 $form->redirect($locale->text('Transaction deleted!'));
777 $form->error($locale->text('Cannot delete transaction!'));
779 $main::lxdebug->leave_sub();
783 $main::lxdebug->enter_sub();
785 $main::auth->assert('invoice_edit');
787 my $form = $main::form;
788 my %myconfig = %main::myconfig;
789 my $locale = $main::locale;
790 my $cgi = $::request->{cgi};
792 $form->{title} = $locale->text('AR Transactions');
794 $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
795 $form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
796 $form->{ALL_BUSINESS_TYPES} = SL::DB::Manager::Business->get_all_sorted;
798 $form->{CT_CUSTOM_VARIABLES} = CVar->get_configs('module' => 'CT');
799 ($form->{CT_CUSTOM_VARIABLES_FILTER_CODE},
800 $form->{CT_CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $form->{CT_CUSTOM_VARIABLES},
801 'include_prefix' => 'l_',
802 'include_value' => 'Y');
804 # constants and subs for template
805 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
807 $::request->layout->add_javascripts("autocomplete_project.js");
810 print $form->parse_html_template('ar/search', { %myconfig });
812 $main::lxdebug->leave_sub();
815 sub create_subtotal_row {
816 $main::lxdebug->enter_sub();
818 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
820 my $form = $main::form;
821 my %myconfig = %main::myconfig;
823 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
825 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
827 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
829 map { $totals->{$_} = 0 } @{ $subtotal_columns };
831 $main::lxdebug->leave_sub();
836 sub ar_transactions {
837 $main::lxdebug->enter_sub();
839 $main::auth->assert('invoice_edit');
841 my $form = $main::form;
842 my %myconfig = %main::myconfig;
843 my $locale = $main::locale;
845 my ($callback, $href, @columns);
847 report_generator_set_default_sort('transdate', 1);
849 AR->ar_transactions(\%myconfig, \%$form);
851 $form->{title} = $locale->text('AR Transactions');
853 my $report = SL::ReportGenerator->new(\%myconfig, $form);
856 qw(ids transdate id type invnumber ordnumber cusordnumber name netamount tax amount paid
857 datepaid due duedate transaction_description notes salesman employee shippingpoint shipvia
858 marge_total marge_percent globalprojectnumber customernumber country ustid taxzone
859 payment_terms charts customertype direct_debit dunning_description department);
861 my $ct_cvar_configs = CVar->get_configs('module' => 'CT');
862 my @ct_includeable_custom_variables = grep { $_->{includeable} } @{ $ct_cvar_configs };
863 my @ct_searchable_custom_variables = grep { $_->{searchable} } @{ $ct_cvar_configs };
865 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @ct_includeable_custom_variables;
866 push @columns, map { "cvar_$_->{name}" } @ct_includeable_custom_variables;
868 my @hidden_variables = map { "l_${_}" } @columns;
869 push @hidden_variables, "l_subtotal", qw(open closed customer invnumber ordnumber cusordnumber transaction_description notes project_id transdatefrom transdateto duedatefrom duedateto
870 employee_id salesman_id business_id parts_partnumber parts_description department_id);
871 push @hidden_variables, map { "cvar_$_->{name}" } @ct_searchable_custom_variables;
873 $href = build_std_url('action=ar_transactions', grep { $form->{$_} } @hidden_variables);
876 'ids' => { raw_header_data => $::request->presenter->checkbox_tag("", id => "check_all", checkall => "[data-checkall=1]"), align => 'center' },
877 'transdate' => { 'text' => $locale->text('Date'), },
878 'id' => { 'text' => $locale->text('ID'), },
879 'type' => { 'text' => $locale->text('Type'), },
880 'invnumber' => { 'text' => $locale->text('Invoice'), },
881 'ordnumber' => { 'text' => $locale->text('Order'), },
882 'cusordnumber' => { 'text' => $locale->text('Customer Order Number'), },
883 'name' => { 'text' => $locale->text('Customer'), },
884 'netamount' => { 'text' => $locale->text('Amount'), },
885 'tax' => { 'text' => $locale->text('Tax'), },
886 'amount' => { 'text' => $locale->text('Total'), },
887 'paid' => { 'text' => $locale->text('Paid'), },
888 'datepaid' => { 'text' => $locale->text('Date Paid'), },
889 'due' => { 'text' => $locale->text('Amount Due'), },
890 'duedate' => { 'text' => $locale->text('Due Date'), },
891 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
892 'notes' => { 'text' => $locale->text('Notes'), },
893 'salesman' => { 'text' => $locale->text('Salesperson'), },
894 'employee' => { 'text' => $locale->text('Employee'), },
895 'shippingpoint' => { 'text' => $locale->text('Shipping Point'), },
896 'shipvia' => { 'text' => $locale->text('Ship via'), },
897 'globalprojectnumber' => { 'text' => $locale->text('Document Project Number'), },
898 'marge_total' => { 'text' => $locale->text('Ertrag'), },
899 'marge_percent' => { 'text' => $locale->text('Ertrag prozentual'), },
900 'customernumber' => { 'text' => $locale->text('Customer Number'), },
901 'country' => { 'text' => $locale->text('Country'), },
902 'ustid' => { 'text' => $locale->text('USt-IdNr.'), },
903 'taxzone' => { 'text' => $locale->text('Steuersatz'), },
904 'payment_terms' => { 'text' => $locale->text('Payment Terms'), },
905 'charts' => { 'text' => $locale->text('Buchungskonto'), },
906 'customertype' => { 'text' => $locale->text('Customer type'), },
907 'direct_debit' => { 'text' => $locale->text('direct debit'), },
908 'department' => { 'text' => $locale->text('Department'), },
909 dunning_description => { 'text' => $locale->text('Dunning level'), },
913 foreach my $name (qw(id transdate duedate invnumber ordnumber cusordnumber name datepaid employee shippingpoint shipvia transaction_description direct_debit)) {
914 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
915 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
918 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
920 $form->{"l_type"} = "Y";
921 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
923 $column_defs{ids}->{visible} = 'HTML';
925 $report->set_columns(%column_defs);
926 $report->set_column_order(@columns);
928 $report->set_export_options('ar_transactions', @hidden_variables, qw(sort sortdir));
930 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
932 CVar->add_custom_variables_to_report('module' => 'CT',
933 'trans_id_field' => 'customer_id',
934 'configs' => $ct_cvar_configs,
935 'column_defs' => \%column_defs,
936 'data' => $form->{AR});
939 if ($form->{customer}) {
940 push @options, $locale->text('Customer') . " : $form->{customer}";
942 if ($form->{cp_name}) {
943 push @options, $locale->text('Contact Person') . " : $form->{cp_name}";
946 if ($form->{department_id}) {
947 my $department = SL::DB::Manager::Department->find_by( id => $form->{department_id} );
948 push @options, $locale->text('Department') . " : " . $department->description;
950 if ($form->{invnumber}) {
951 push @options, $locale->text('Invoice Number') . " : $form->{invnumber}";
953 if ($form->{ordnumber}) {
954 push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
956 if ($form->{cusordnumber}) {
957 push @options, $locale->text('Customer Order Number') . " : $form->{cusordnumber}";
959 if ($form->{notes}) {
960 push @options, $locale->text('Notes') . " : $form->{notes}";
962 if ($form->{transaction_description}) {
963 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
965 if ($form->{parts_partnumber}) {
966 push @options, $locale->text('Part Number') . " : $form->{parts_partnumber}";
968 if ($form->{parts_description}) {
969 push @options, $locale->text('Part Description') . " : $form->{parts_description}";
971 if ($form->{transdatefrom}) {
972 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
974 if ($form->{transdateto}) {
975 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
978 push @options, $locale->text('Open');
980 if ($form->{employee_id}) {
981 my $employee = SL::DB::Employee->new(id => $form->{employee_id})->load;
982 push @options, $locale->text('Employee') . ' : ' . $employee->name;
984 if ($form->{salesman_id}) {
985 my $salesman = SL::DB::Employee->new(id => $form->{salesman_id})->load;
986 push @options, $locale->text('Salesman') . ' : ' . $salesman->name;
988 if ($form->{closed}) {
989 push @options, $locale->text('Closed');
992 $form->{ALL_PRINTERS} = SL::DB::Manager::Printer->get_all_sorted;
994 $report->set_options('top_info_text' => join("\n", @options),
995 'raw_top_info_text' => $form->parse_html_template('ar/ar_transactions_header'),
996 'raw_bottom_info_text' => $form->parse_html_template('ar/ar_transactions_bottom'),
997 'output_format' => 'HTML',
998 'title' => $form->{title},
999 'attachment_basename' => $locale->text('invoice_list') . strftime('_%Y%m%d', localtime time),
1001 $report->set_options_from_form();
1002 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1004 # add sort and escape callback, this one we use for the add sub
1005 $form->{callback} = $href .= "&sort=$form->{sort}";
1007 # escape callback for href
1008 $callback = $form->escape($href);
1010 my @subtotal_columns = qw(netamount amount paid due marge_total marge_percent);
1012 my %totals = map { $_ => 0 } @subtotal_columns;
1013 my %subtotals = map { $_ => 0 } @subtotal_columns;
1017 foreach my $ar (@{ $form->{AR} }) {
1018 $ar->{tax} = $ar->{amount} - $ar->{netamount};
1019 $ar->{due} = $ar->{amount} - $ar->{paid};
1021 map { $subtotals{$_} += $ar->{$_};
1022 $totals{$_} += $ar->{$_} } @subtotal_columns;
1024 $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
1025 $totals{marge_percent} = $totals{netamount} ? ($totals{marge_total} * 100 / $totals{netamount} ) : 0;
1027 my $is_storno = $ar->{storno} && $ar->{storno_id};
1028 my $has_storno = $ar->{storno} && !$ar->{storno_id};
1031 $has_storno ? $locale->text("Invoice with Storno (abbreviation)") :
1032 $is_storno ? $locale->text("Storno (one letter abbreviation)") :
1033 $ar->{amount} < 0 ? $locale->text("Credit note (one letter abbreviation)") :
1034 $ar->{invoice} ? $locale->text("Invoice (one letter abbreviation)") :
1035 $locale->text("AR Transaction (abbreviation)");
1037 map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(netamount tax amount paid due marge_total marge_percent);
1039 $ar->{direct_debit} = $ar->{direct_debit} ? $::locale->text('yes') : $::locale->text('no');
1043 foreach my $column (@columns) {
1045 'data' => $ar->{$column},
1046 'align' => $column_alignment{$column},
1050 $row->{invnumber}->{link} = build_std_url("script=" . ($ar->{invoice} ? 'is.pl' : 'ar.pl'), 'action=edit')
1051 . "&id=" . E($ar->{id}) . "&callback=${callback}";
1054 raw_data => $::request->presenter->checkbox_tag("id[]", value => $ar->{id}, "data-checkall" => 1),
1059 my $row_set = [ $row ];
1061 if (($form->{l_subtotal} eq 'Y')
1062 && (($idx == (scalar @{ $form->{AR} } - 1))
1063 || ($ar->{ $form->{sort} } ne $form->{AR}->[$idx + 1]->{ $form->{sort} }))) {
1064 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1067 $report->add_data($row_set);
1072 $report->add_separator();
1073 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1075 $report->generate_with_headers();
1077 $main::lxdebug->leave_sub();
1081 $main::lxdebug->enter_sub();
1083 $main::auth->assert('ar_transactions');
1085 my $form = $main::form;
1086 my %myconfig = %main::myconfig;
1087 my $locale = $main::locale;
1089 # don't cancel cancelled transactions
1090 if (IS->has_storno(\%myconfig, $form, 'ar')) {
1091 $form->{title} = $locale->text("Cancel Accounts Receivables Transaction");
1092 $form->error($locale->text("Transaction has already been cancelled!"));
1095 AR->storno($form, \%myconfig, $form->{id});
1097 # saving the history
1098 if(!exists $form->{addition} && $form->{id} ne "") {
1099 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1100 $form->{addition} = "STORNO";
1101 $form->{what_done} = "invoice";
1102 $form->save_history;
1104 # /saving the history
1106 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1108 $main::lxdebug->leave_sub();