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);
45 use SL::ReportGenerator;
47 require "bin/mozilla/arap.pl";
48 require "bin/mozilla/common.pl";
49 require "bin/mozilla/reportgenerator.pl";
54 # this is for our long dates
55 # $locale->text('January')
56 # $locale->text('February')
57 # $locale->text('March')
58 # $locale->text('April')
59 # $locale->text('May ')
60 # $locale->text('June')
61 # $locale->text('July')
62 # $locale->text('August')
63 # $locale->text('September')
64 # $locale->text('October')
65 # $locale->text('November')
66 # $locale->text('December')
68 # this is for our short month
69 # $locale->text('Jan')
70 # $locale->text('Feb')
71 # $locale->text('Mar')
72 # $locale->text('Apr')
73 # $locale->text('May')
74 # $locale->text('Jun')
75 # $locale->text('Jul')
76 # $locale->text('Aug')
77 # $locale->text('Sep')
78 # $locale->text('Oct')
79 # $locale->text('Nov')
80 # $locale->text('Dec')
83 $main::lxdebug->enter_sub();
85 $main::auth->assert('ar_transactions');
87 my $form = $main::form;
88 my %myconfig = %main::myconfig;
91 if(!exists $form->{addition} && ($form->{id} ne "")) {
92 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
93 $form->{addition} = "ADDED";
98 $form->{title} = "Add";
99 $form->{callback} = "ar.pl?action=add" unless $form->{callback};
101 AR->get_transdate(\%myconfig, $form);
102 $form->{initial_transdate} = $form->{transdate};
103 create_links(dont_save => 1);
104 $form->{transdate} = $form->{initial_transdate};
106 if ($form->{customer_id}) {
107 my $last_used_ar_chart = SL::DB::Customer->load_cached($form->{customer_id})->last_used_ar_chart;
108 $form->{"AR_amount_chart_id_1"} = $last_used_ar_chart->id if $last_used_ar_chart;
112 $main::lxdebug->leave_sub();
116 $main::lxdebug->enter_sub();
118 $main::auth->assert('ar_transactions');
120 my $form = $main::form;
122 # show history button
123 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
124 #/show hhistory button
125 $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
126 $form->{title} = "Edit";
131 $main::lxdebug->leave_sub();
135 $main::lxdebug->enter_sub();
137 $main::auth->assert('ar_transactions');
139 my $form = $main::form;
144 $main::lxdebug->leave_sub();
147 sub _retrieve_invoice_object {
148 return undef if !$::form->{id};
149 return $::form->{invoice_obj} if $::form->{invoice_obj} && $::form->{invoice_obj}->id == $::form->{id};
150 return SL::DB::Invoice->new(id => $::form->{id})->load;
154 $main::lxdebug->enter_sub();
156 $main::auth->assert('ar_transactions');
159 my $form = $main::form;
160 my %myconfig = %main::myconfig;
162 $form->create_links("AR", \%myconfig, "customer");
163 $form->{invoice_obj} = _retrieve_invoice_object();
166 if (!$params{dont_save}) {
167 %saved = map { ($_ => $form->{$_}) } qw(direct_debit id taxincluded);
168 $saved{duedate} = $form->{duedate} if $form->{duedate};
169 $saved{currency} = $form->{currency} if $form->{currency};
172 IS->get_customer(\%myconfig, \%$form);
174 $form->{$_} = $saved{$_} for keys %saved;
175 $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
176 $form->{rowcount} = 1;
177 $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};
180 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
182 $form->{selectcurrency} = "";
183 map { $form->{selectcurrency} .= "<option>$_\n" } $form->get_all_currencies(\%myconfig);
186 if (@{ $form->{all_customer} || [] }) {
187 $form->{customer} = "$form->{customer}--$form->{customer_id}";
188 map { $form->{selectcustomer} .= "<option>$_->{name}--$_->{id}\n" }
189 (@{ $form->{all_customer} });
192 $form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
194 $form->{employee} = "$form->{employee}--$form->{employee_id}";
197 if (@{ $form->{all_employees} || [] }) {
198 $form->{selectemployee} = "";
199 map { $form->{selectemployee} .= "<option>$_->{name}--$_->{id}\n" }
200 (@{ $form->{all_employees} || [] });
203 # build the popup menus
204 $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
206 AR->setup_form($form);
209 ($form->datetonum($form->{transdate}, \%myconfig) <=
210 $form->datetonum($form->{closedto}, \%myconfig));
212 $main::lxdebug->leave_sub();
216 $main::lxdebug->enter_sub();
218 $main::auth->assert('ar_transactions');
220 my $form = $main::form;
221 my %myconfig = %main::myconfig;
222 my $locale = $main::locale;
223 my $cgi = $::request->{cgi};
225 $form->{invoice_obj} = _retrieve_invoice_object();
227 my ($title, $readonly, $exchangerate, $rows);
228 my ($notes, $customer, $employee, $amount, $project);
230 $form->{initial_focus} = !($form->{amount_1} * 1) ? 'customer' : 'row_' . $form->{rowcount};
232 $title = $form->{title};
233 # $locale->text('Add Accounts Receivables Transaction')
234 # $locale->text('Edit Accounts Receivables Transaction')
235 $form->{title} = $locale->text("$title Accounts Receivables Transaction");
237 $readonly = ($form->{id}) ? "readonly" : "";
239 $form->{radier} = ($::instance_conf->get_ar_changeable == 2)
240 ? ($form->current_date(\%myconfig) eq $form->{gldate})
241 : ($::instance_conf->get_ar_changeable == 1);
242 $readonly = ($form->{radier}) ? "" : $readonly;
244 # set option selected
245 foreach my $item (qw(customer currency employee)) {
246 $form->{"select$item"} =~ s/ selected//;
247 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
250 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'buy');
251 $form->{exchangerate} = $form->{forex} if $form->{forex};
253 # format exchangerate
254 $form->{exchangerate} = $form->{exchangerate} ? $form->format_amount(\%myconfig, $form->{exchangerate}) : '';
256 $rows = max 2, $form->numtextrows($form->{notes}, 50);
258 my @old_project_ids = grep { $_ } map { $form->{"project_id_$_"} } 1..$form->{rowcount};
260 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
262 "old_id" => \@old_project_ids },
263 "charts" => { "key" => "ALL_CHARTS",
264 "transdate" => $form->{transdate} },
265 "taxcharts" => { "key" => "ALL_TAXCHARTS",
266 "module" => "AR" },);
268 $form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
270 $_->{link_split} = { map { $_ => 1 } split/:/, $_->{link} } for @{ $form->{ALL_CHARTS} };
272 my %project_labels = map { $_->{id} => $_->{projectnumber} } @{ $form->{"ALL_PROJECTS"} };
274 my (@AR_paid_values, %AR_paid_labels);
275 my $default_ar_amount_chart_id;
277 foreach my $item (@{ $form->{ALL_CHARTS} }) {
278 if ($item->{link_split}{AR_amount}) {
279 $default_ar_amount_chart_id //= $item->{id};
281 } elsif ($item->{link_split}{AR_paid}) {
282 push(@AR_paid_values, $item->{accno});
283 $AR_paid_labels{$item->{accno}} = "$item->{accno}--$item->{description}";
287 my $follow_up_vc = $form->{customer};
288 $follow_up_vc =~ s/--.*?//;
289 my $follow_up_trans_info = "$form->{invnumber} ($follow_up_vc)";
291 $::request->layout->add_javascripts("autocomplete_chart.js", "show_vc_details.js", "show_history.js", "follow_up.js", "kivi.Draft.js", "kivi.GL.js");
293 my $transdate = $::form->{transdate} ? DateTime->from_kivitendo($::form->{transdate}) : DateTime->today_local;
297 for my $i (1 .. $form->{rowcount}) {
299 amount => $form->{"amount_$i"},
300 tax => $form->{"tax_$i"},
301 project_id => ($i==$form->{rowcount}) ? $form->{globalproject_id} : $form->{"project_id_$i"},
304 my (%taxchart_labels, @taxchart_values, $default_taxchart, $taxchart_to_use);
305 my $amount_chart_id = $form->{"AR_amount_chart_id_$i"} // $default_ar_amount_chart_id;
306 my $chart_has_changed = $::form->{"previous_AR_amount_chart_id_$i"} && ($amount_chart_id != $::form->{"previous_AR_amount_chart_id_$i"});
308 foreach my $item ( GL->get_active_taxes_for_chart($amount_chart_id, $transdate) ) {
309 my $key = $item->id . "--" . $item->rate;
310 $first_taxchart //= $item;
311 $default_taxchart = $item if $item->{is_default};
312 $taxchart_to_use = $item if $key eq $form->{"taxchart_$i"};
314 push(@taxchart_values, $key);
315 $taxchart_labels{$key} = $item->taxdescription . " " . $item->rate * 100 . ' %';
318 $taxchart_to_use = $default_taxchart // $first_taxchart if $chart_has_changed || !$taxchart_to_use;
319 my $selected_taxchart = $taxchart_to_use->id . '--' . $taxchart_to_use->rate;
321 $transaction->{selectAR_amount} =
322 $::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" : ""))
323 . $::request->presenter->hidden_tag("previous_AR_amount_chart_id_$i", $amount_chart_id);
325 $transaction->{taxchart} =
326 NTI($cgi->popup_menu('-name' => "taxchart_$i",
327 '-id' => "taxchart_$i",
328 '-style' => 'width:200px',
329 '-values' => \@taxchart_values,
330 '-labels' => \%taxchart_labels,
331 '-default' => $selected_taxchart));
333 push @transactions, $transaction;
336 $form->{invtotal_unformatted} = $form->{invtotal};
338 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
340 my $now = $form->current_date(\%myconfig);
343 for my $i (1 .. $form->{paidaccounts}) {
345 paid => $form->{"paid_$i"},
346 exchangerate => $form->{"exchangerate_$i"} || '',
347 gldate => $form->{"gldate_$i"},
348 acc_trans_id => $form->{"acc_trans_id_$i"},
349 source => $form->{"source_$i"},
350 memo => $form->{"memo_$i"},
351 AR_paid => $form->{"AR_paid_$i"},
352 forex => $form->{"forex_$i"},
353 datepaid => $form->{"datepaid_$i"},
354 paid_project_id => $form->{"paid_project_id_$i"},
355 gldate => $form->{"gldate_$i"},
358 # default account for current assets (i.e. 1801 - SKR04) if no account is selected
359 $form->{accno_arap} = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
361 $payment->{selectAR_paid} =
362 NTI($cgi->popup_menu('-name' => "AR_paid_$i",
363 '-id' => "AR_paid_$i",
364 '-values' => \@AR_paid_values,
365 '-labels' => \%AR_paid_labels,
366 '-default' => $payment->{AR_paid} || $form->{accno_arap}));
370 $payment->{changeable} =
371 SL::DB::Default->get->payments_changeable == 0 ? !$payment->{acc_trans_id} # never
372 : SL::DB::Default->get->payments_changeable == 2 ? $payment->{gldate} eq '' || $payment->{gldate} eq $now
375 #deaktivieren von gebuchten Zahlungen ausserhalb der Bücherkontrolle, vorher prüfen ob heute eingegeben
376 if ($form->date_closed($payment->{"gldate_$i"})) {
377 $payment->{changeable} = 0;
380 push @payments, $payment;
383 my @empty = grep { $_->{paid} eq '' } @payments;
385 (sort_by { DateTime->from_kivitendo($_->{datepaid}) } grep { $_->{paid} ne '' } @payments),
389 $form->{totalpaid} = sum map { $_->{paid} } @payments;
392 print $::form->parse_html_template('ar/form_header', {
393 paid_missing => $::form->{invtotal} - $::form->{totalpaid},
394 show_exch => ($::form->{defaultcurrency} && ($::form->{currency} ne $::form->{defaultcurrency})),
395 payments => \@payments,
396 transactions => \@transactions,
397 project_labels => \%project_labels,
399 AR_chart_id => $form->{AR_chart_id},
401 follow_up_trans_info => $follow_up_trans_info,
402 today => DateTime->today,
405 $main::lxdebug->leave_sub();
409 $main::lxdebug->enter_sub();
411 $main::auth->assert('ar_transactions');
413 my $form = $main::form;
414 my %myconfig = %main::myconfig;
415 my $locale = $main::locale;
416 my $cgi = $::request->{cgi};
419 my $follow_ups = FU->follow_ups('trans_id' => $form->{id}, 'not_done' => 1);
420 if ( @{ $follow_ups} ) {
421 $form->{follow_up_length} = scalar(@{$follow_ups});
422 $form->{follow_up_due_length} = sum(map({ $_->{due} * 1 } @{ $follow_ups }));
426 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
427 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
429 $form->{is_closed} = $transdate <= $closedto;
431 # ToDO: - insert a global check for stornos, so that a storno is only possible a limited time after saving it
432 $form->{show_storno_button} =
434 !IS->has_storno(\%myconfig, $form, 'ar') &&
435 !IS->is_storno(\%myconfig, $form, 'ar') &&
436 ($form->{totalpaid} == 0 || $form->{totalpaid} eq "");
438 $form->{show_mark_as_paid_button} = $form->{id} && $::instance_conf->get_ar_show_mark_as_paid();
440 print $::form->parse_html_template('ar/form_footer');
442 $main::lxdebug->leave_sub();
446 $::auth->assert('ar_transactions');
448 SL::DB::Invoice->new(id => $::form->{id})->load->mark_as_paid;
450 $::form->redirect($::locale->text("Marked as paid"));
454 $::form->{transdate} = DateTime->today_local->to_kivitendo if !$::form->{transdate};
455 $::form->{gldate} = $::form->{transdate} if !$::form->{gldate};
460 $main::lxdebug->enter_sub();
462 $main::auth->assert('ar_transactions');
464 my $form = $main::form;
465 my %myconfig = %main::myconfig;
469 my ($totaltax, $exchangerate);
471 $form->{invtotal} = 0;
473 delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
475 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
476 qw(exchangerate creditlimit creditremaining);
478 my @flds = qw(amount AR_amount projectnumber oldprojectnumber project_id);
482 for my $i (1 .. $form->{rowcount}) {
483 $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
484 if ($form->{"amount_$i"}) {
487 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
490 ($tmpnetamount,$form->{"tax_$i"}) = $form->calculate_tax($form->{"amount_$i"},$rate,$form->{taxincluded},2);
492 $totaltax += $form->{"tax_$i"};
493 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
498 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
499 $form->{rowcount} = $count + 1;
500 map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
502 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'buy');
503 $form->{exchangerate} = $form->{forex} if $form->{forex};
505 $form->{invdate} = $form->{transdate};
507 &check_name("customer");
510 ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
512 for my $i (1 .. $form->{paidaccounts}) {
513 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
516 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
517 } qw(paid exchangerate);
519 $form->{totalpaid} += $form->{"paid_$i"};
521 $form->{"forex_$i"} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
522 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
526 $form->{creditremaining} -=
527 ($form->{invtotal} - $form->{totalpaid} + $form->{oldtotalpaid} -
528 $form->{oldinvtotal});
529 $form->{oldinvtotal} = $form->{invtotal};
530 $form->{oldtotalpaid} = $form->{totalpaid};
534 $main::lxdebug->leave_sub();
538 # ToDO: fix $closedto and $invdate
541 $main::lxdebug->enter_sub();
543 $main::auth->assert('ar_transactions');
545 my $form = $main::form;
546 my %myconfig = %main::myconfig;
547 my $locale = $main::locale;
549 $form->mtime_ischanged('ar');
550 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
552 my $invdate = $form->datetonum($form->{transdate}, \%myconfig);
554 for my $i (1 .. $form->{paidaccounts}) {
556 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
557 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
559 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
561 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
562 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
564 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
565 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
566 $form->error($locale->text('Cannot post payment for a closed period!'))
567 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
569 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
570 # $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid);
571 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
576 ($form->{AR}) = split /--/, $form->{AR};
577 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
578 if (AR->post_payment(\%myconfig, \%$form)) {
579 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
580 $form->{what_done} = 'invoice';
581 $form->{addition} = "PAYMENT POSTED";
583 $form->redirect($locale->text('Payment posted!'))
585 $form->error($locale->text('Cannot post payment!'));
588 $main::lxdebug->leave_sub();
593 $main::auth->assert('ar_transactions');
595 my $form = $main::form;
602 $main::lxdebug->enter_sub();
604 $main::auth->assert('ar_transactions');
606 my $form = $main::form;
607 my %myconfig = %main::myconfig;
608 my $locale = $main::locale;
612 $form->mtime_ischanged('ar');
616 # check if there is an invoice number, invoice and due date
617 $form->isblank("transdate", $locale->text('Invoice Date missing!'));
618 $form->isblank("duedate", $locale->text('Due Date missing!'));
619 $form->isblank("customer", $locale->text('Customer missing!'));
621 if ($myconfig{mandatory_departments} && !$form->{department_id}) {
622 $form->{saved_message} = $::locale->text('You have to specify a department.');
627 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
628 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
630 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
631 if ($form->date_max_future($transdate, \%myconfig));
633 $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
635 $form->error($locale->text('Zero amount posting!'))
636 unless grep $_*1, map $form->parse_amount(\%myconfig, $form->{"amount_$_"}), 1..$form->{rowcount};
638 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
639 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
643 for my $i (1 .. $form->{paidaccounts}) {
644 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
645 $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
647 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
649 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
650 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
652 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
653 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
654 $form->error($locale->text('Cannot post payment for a closed period!'))
655 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
657 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
658 $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
659 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
664 # if oldcustomer ne customer redo form
665 my ($customer) = split /--/, $form->{customer};
666 if ($form->{oldcustomer} ne "$customer--$form->{customer_id}") {
668 $::dispatcher->end_request;
671 $form->{AR}{receivables} = $form->{ARselected};
674 $form->{id} = 0 if $form->{postasnew};
675 $form->error($locale->text('Cannot post transaction!')) unless AR->post_transaction(\%myconfig, \%$form);
678 if(!exists $form->{addition} && $form->{id} ne "") {
679 $form->{snumbers} = "invnumber_$form->{invnumber}";
680 $form->{what_done} = "invoice";
681 $form->{addition} = "POSTED";
684 # /saving the history
686 $form->redirect($locale->text('Transaction posted!')) unless $inline;
688 $main::lxdebug->leave_sub();
692 $main::lxdebug->enter_sub();
694 $main::auth->assert('ar_transactions');
696 my $form = $main::form;
697 my %myconfig = %main::myconfig;
699 $form->{postasnew} = 1;
701 if(!exists $form->{addition} && $form->{id} ne "") {
702 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
703 $form->{what_done} = "invoice";
704 $form->{addition} = "POSTED AS NEW";
707 # /saving the history
710 $main::lxdebug->leave_sub();
714 $main::lxdebug->enter_sub();
716 $main::auth->assert('ar_transactions');
718 my $form = $main::form;
719 my %myconfig = %main::myconfig;
721 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);
722 $form->{paidaccounts} = 1;
724 $form->{invdate} = $form->current_date(\%myconfig);
727 $main::lxdebug->leave_sub();
731 $main::lxdebug->enter_sub();
733 $main::auth->assert('ar_transactions');
735 my $form = $main::form;
736 my $locale = $main::locale;
738 $form->{title} = $locale->text('Confirm!');
742 delete $form->{header};
745 <form method=post action=$form->{script}>
748 foreach my $key (keys %$form) {
749 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
750 $form->{$key} =~ s/\"/"/g;
751 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
755 <h2 class=confirm>$form->{title}</h2>
758 . $locale->text('Are you sure you want to delete Transaction')
759 . qq| $form->{invnumber}</h4>
761 <input name=action class=submit type=submit value="|
762 . $locale->text('Yes') . qq|">
766 $main::lxdebug->leave_sub();
770 $main::lxdebug->enter_sub();
772 $main::auth->assert('ar_transactions');
774 my $form = $main::form;
775 my %myconfig = %main::myconfig;
776 my $locale = $main::locale;
778 if (AR->delete_transaction(\%myconfig, \%$form)) {
780 if(!exists $form->{addition}) {
781 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
782 $form->{what_done} = "invoice";
783 $form->{addition} = "DELETED";
786 # /saving the history
787 $form->redirect($locale->text('Transaction deleted!'));
789 $form->error($locale->text('Cannot delete transaction!'));
791 $main::lxdebug->leave_sub();
795 $main::lxdebug->enter_sub();
797 $main::auth->assert('invoice_edit');
799 my $form = $main::form;
800 my %myconfig = %main::myconfig;
801 my $locale = $main::locale;
802 my $cgi = $::request->{cgi};
804 # setup customer selection
805 $form->all_vc(\%myconfig, "customer", "AR");
807 $form->{title} = $locale->text('AR Transactions');
809 # Auch in Rechnungsübersicht nach Kundentyp filtern - jan
810 $form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 },
811 "customers" => "ALL_VC",
812 "business_types" => "ALL_BUSINESS_TYPES");
813 $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
814 $form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
815 $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
817 $form->{CT_CUSTOM_VARIABLES} = CVar->get_configs('module' => 'CT');
818 ($form->{CT_CUSTOM_VARIABLES_FILTER_CODE},
819 $form->{CT_CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $form->{CT_CUSTOM_VARIABLES},
820 'include_prefix' => 'l_',
821 'include_value' => 'Y');
823 # constants and subs for template
824 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
827 print $form->parse_html_template('ar/search', { %myconfig });
829 $main::lxdebug->leave_sub();
832 sub create_subtotal_row {
833 $main::lxdebug->enter_sub();
835 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
837 my $form = $main::form;
838 my %myconfig = %main::myconfig;
840 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
842 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
844 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
846 map { $totals->{$_} = 0 } @{ $subtotal_columns };
848 $main::lxdebug->leave_sub();
853 sub ar_transactions {
854 $main::lxdebug->enter_sub();
856 $main::auth->assert('invoice_edit');
858 my $form = $main::form;
859 my %myconfig = %main::myconfig;
860 my $locale = $main::locale;
862 my ($callback, $href, @columns);
864 ($form->{customer}, $form->{customer_id}) = split(/--/, $form->{customer});
866 report_generator_set_default_sort('transdate', 1);
868 AR->ar_transactions(\%myconfig, \%$form);
870 $form->{title} = $locale->text('AR Transactions');
872 my $report = SL::ReportGenerator->new(\%myconfig, $form);
875 qw(ids transdate id type invnumber ordnumber cusordnumber name netamount tax amount paid
876 datepaid due duedate transaction_description notes salesman employee shippingpoint shipvia
877 marge_total marge_percent globalprojectnumber customernumber country ustid taxzone
878 payment_terms charts customertype direct_debit dunning_description department);
880 my $ct_cvar_configs = CVar->get_configs('module' => 'CT');
881 my @ct_includeable_custom_variables = grep { $_->{includeable} } @{ $ct_cvar_configs };
882 my @ct_searchable_custom_variables = grep { $_->{searchable} } @{ $ct_cvar_configs };
884 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @ct_includeable_custom_variables;
885 push @columns, map { "cvar_$_->{name}" } @ct_includeable_custom_variables;
887 my @hidden_variables = map { "l_${_}" } @columns;
888 push @hidden_variables, "l_subtotal", qw(open closed customer invnumber ordnumber cusordnumber transaction_description notes project_id transdatefrom transdateto duedatefrom duedateto
889 employee_id salesman_id business_id parts_partnumber parts_description department_id);
890 push @hidden_variables, map { "cvar_$_->{name}" } @ct_searchable_custom_variables;
892 $href = build_std_url('action=ar_transactions', grep { $form->{$_} } @hidden_variables);
895 'ids' => { raw_header_data => $::request->presenter->checkbox_tag("", id => "check_all", checkall => "[data-checkall=1]"), align => 'center' },
896 'transdate' => { 'text' => $locale->text('Date'), },
897 'id' => { 'text' => $locale->text('ID'), },
898 'type' => { 'text' => $locale->text('Type'), },
899 'invnumber' => { 'text' => $locale->text('Invoice'), },
900 'ordnumber' => { 'text' => $locale->text('Order'), },
901 'cusordnumber' => { 'text' => $locale->text('Customer Order Number'), },
902 'name' => { 'text' => $locale->text('Customer'), },
903 'netamount' => { 'text' => $locale->text('Amount'), },
904 'tax' => { 'text' => $locale->text('Tax'), },
905 'amount' => { 'text' => $locale->text('Total'), },
906 'paid' => { 'text' => $locale->text('Paid'), },
907 'datepaid' => { 'text' => $locale->text('Date Paid'), },
908 'due' => { 'text' => $locale->text('Amount Due'), },
909 'duedate' => { 'text' => $locale->text('Due Date'), },
910 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
911 'notes' => { 'text' => $locale->text('Notes'), },
912 'salesman' => { 'text' => $locale->text('Salesperson'), },
913 'employee' => { 'text' => $locale->text('Employee'), },
914 'shippingpoint' => { 'text' => $locale->text('Shipping Point'), },
915 'shipvia' => { 'text' => $locale->text('Ship via'), },
916 'globalprojectnumber' => { 'text' => $locale->text('Document Project Number'), },
917 'marge_total' => { 'text' => $locale->text('Ertrag'), },
918 'marge_percent' => { 'text' => $locale->text('Ertrag prozentual'), },
919 'customernumber' => { 'text' => $locale->text('Customer Number'), },
920 'country' => { 'text' => $locale->text('Country'), },
921 'ustid' => { 'text' => $locale->text('USt-IdNr.'), },
922 'taxzone' => { 'text' => $locale->text('Steuersatz'), },
923 'payment_terms' => { 'text' => $locale->text('Payment Terms'), },
924 'charts' => { 'text' => $locale->text('Buchungskonto'), },
925 'customertype' => { 'text' => $locale->text('Customer type'), },
926 'direct_debit' => { 'text' => $locale->text('direct debit'), },
927 'department' => { 'text' => $locale->text('Department'), },
928 dunning_description => { 'text' => $locale->text('Dunning level'), },
932 foreach my $name (qw(id transdate duedate invnumber ordnumber cusordnumber name datepaid employee shippingpoint shipvia transaction_description direct_debit)) {
933 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
934 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
937 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
939 $form->{"l_type"} = "Y";
940 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
942 $column_defs{ids}->{visible} = 'HTML';
944 $report->set_columns(%column_defs);
945 $report->set_column_order(@columns);
947 $report->set_export_options('ar_transactions', @hidden_variables, qw(sort sortdir));
949 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
951 CVar->add_custom_variables_to_report('module' => 'CT',
952 'trans_id_field' => 'customer_id',
953 'configs' => $ct_cvar_configs,
954 'column_defs' => \%column_defs,
955 'data' => $form->{AR});
958 if ($form->{customer}) {
959 push @options, $locale->text('Customer') . " : $form->{customer}";
961 if ($form->{cp_name}) {
962 push @options, $locale->text('Contact Person') . " : $form->{cp_name}";
965 if ($form->{department_id}) {
966 my $department = SL::DB::Manager::Department->find_by( id => $form->{department_id} );
967 push @options, $locale->text('Department') . " : " . $department->description;
969 if ($form->{invnumber}) {
970 push @options, $locale->text('Invoice Number') . " : $form->{invnumber}";
972 if ($form->{ordnumber}) {
973 push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
975 if ($form->{cusordnumber}) {
976 push @options, $locale->text('Customer Order Number') . " : $form->{cusordnumber}";
978 if ($form->{notes}) {
979 push @options, $locale->text('Notes') . " : $form->{notes}";
981 if ($form->{transaction_description}) {
982 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
984 if ($form->{parts_partnumber}) {
985 push @options, $locale->text('Part Number') . " : $form->{parts_partnumber}";
987 if ($form->{parts_description}) {
988 push @options, $locale->text('Part Description') . " : $form->{parts_description}";
990 if ($form->{transdatefrom}) {
991 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
993 if ($form->{transdateto}) {
994 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
997 push @options, $locale->text('Open');
999 if ($form->{employee_id}) {
1000 my $employee = SL::DB::Employee->new(id => $form->{employee_id})->load;
1001 push @options, $locale->text('Employee') . ' : ' . $employee->name;
1003 if ($form->{salesman_id}) {
1004 my $salesman = SL::DB::Employee->new(id => $form->{salesman_id})->load;
1005 push @options, $locale->text('Salesman') . ' : ' . $salesman->name;
1007 if ($form->{closed}) {
1008 push @options, $locale->text('Closed');
1011 $form->{ALL_PRINTERS} = SL::DB::Manager::Printer->get_all_sorted;
1013 $report->set_options('top_info_text' => join("\n", @options),
1014 'raw_top_info_text' => $form->parse_html_template('ar/ar_transactions_header'),
1015 'raw_bottom_info_text' => $form->parse_html_template('ar/ar_transactions_bottom'),
1016 'output_format' => 'HTML',
1017 'title' => $form->{title},
1018 'attachment_basename' => $locale->text('invoice_list') . strftime('_%Y%m%d', localtime time),
1020 $report->set_options_from_form();
1021 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1023 # add sort and escape callback, this one we use for the add sub
1024 $form->{callback} = $href .= "&sort=$form->{sort}";
1026 # escape callback for href
1027 $callback = $form->escape($href);
1029 my @subtotal_columns = qw(netamount amount paid due marge_total marge_percent);
1031 my %totals = map { $_ => 0 } @subtotal_columns;
1032 my %subtotals = map { $_ => 0 } @subtotal_columns;
1036 foreach my $ar (@{ $form->{AR} }) {
1037 $ar->{tax} = $ar->{amount} - $ar->{netamount};
1038 $ar->{due} = $ar->{amount} - $ar->{paid};
1040 map { $subtotals{$_} += $ar->{$_};
1041 $totals{$_} += $ar->{$_} } @subtotal_columns;
1043 $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
1044 $totals{marge_percent} = $totals{netamount} ? ($totals{marge_total} * 100 / $totals{netamount} ) : 0;
1046 my $is_storno = $ar->{storno} && $ar->{storno_id};
1047 my $has_storno = $ar->{storno} && !$ar->{storno_id};
1050 $has_storno ? $locale->text("Invoice with Storno (abbreviation)") :
1051 $is_storno ? $locale->text("Storno (one letter abbreviation)") :
1052 $ar->{amount} < 0 ? $locale->text("Credit note (one letter abbreviation)") :
1053 $ar->{invoice} ? $locale->text("Invoice (one letter abbreviation)") :
1054 $locale->text("AR Transaction (abbreviation)");
1056 map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(netamount tax amount paid due marge_total marge_percent);
1058 $ar->{direct_debit} = $ar->{direct_debit} ? $::locale->text('yes') : $::locale->text('no');
1062 foreach my $column (@columns) {
1064 'data' => $ar->{$column},
1065 'align' => $column_alignment{$column},
1069 $row->{invnumber}->{link} = build_std_url("script=" . ($ar->{invoice} ? 'is.pl' : 'ar.pl'), 'action=edit')
1070 . "&id=" . E($ar->{id}) . "&callback=${callback}";
1073 raw_data => $::request->presenter->checkbox_tag("id[]", value => $ar->{id}, "data-checkall" => 1),
1078 my $row_set = [ $row ];
1080 if (($form->{l_subtotal} eq 'Y')
1081 && (($idx == (scalar @{ $form->{AR} } - 1))
1082 || ($ar->{ $form->{sort} } ne $form->{AR}->[$idx + 1]->{ $form->{sort} }))) {
1083 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1086 $report->add_data($row_set);
1091 $report->add_separator();
1092 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1094 $report->generate_with_headers();
1096 $main::lxdebug->leave_sub();
1100 $main::lxdebug->enter_sub();
1102 $main::auth->assert('ar_transactions');
1104 my $form = $main::form;
1105 my %myconfig = %main::myconfig;
1106 my $locale = $main::locale;
1108 # don't cancel cancelled transactions
1109 if (IS->has_storno(\%myconfig, $form, 'ar')) {
1110 $form->{title} = $locale->text("Cancel Accounts Receivables Transaction");
1111 $form->error($locale->text("Transaction has already been cancelled!"));
1114 AR->storno($form, \%myconfig, $form->{id});
1116 # saving the history
1117 if(!exists $form->{addition} && $form->{id} ne "") {
1118 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1119 $form->{addition} = "STORNO";
1120 $form->{what_done} = "invoice";
1121 $form->save_history;
1123 # /saving the history
1125 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1127 $main::lxdebug->leave_sub();