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);
44 use SL::ReportGenerator;
46 require "bin/mozilla/arap.pl";
47 require "bin/mozilla/common.pl";
48 require "bin/mozilla/reportgenerator.pl";
53 # this is for our long dates
54 # $locale->text('January')
55 # $locale->text('February')
56 # $locale->text('March')
57 # $locale->text('April')
58 # $locale->text('May ')
59 # $locale->text('June')
60 # $locale->text('July')
61 # $locale->text('August')
62 # $locale->text('September')
63 # $locale->text('October')
64 # $locale->text('November')
65 # $locale->text('December')
67 # this is for our short month
68 # $locale->text('Jan')
69 # $locale->text('Feb')
70 # $locale->text('Mar')
71 # $locale->text('Apr')
72 # $locale->text('May')
73 # $locale->text('Jun')
74 # $locale->text('Jul')
75 # $locale->text('Aug')
76 # $locale->text('Sep')
77 # $locale->text('Oct')
78 # $locale->text('Nov')
79 # $locale->text('Dec')
82 $main::lxdebug->enter_sub();
84 $main::auth->assert('ar_transactions');
86 my $form = $main::form;
87 my %myconfig = %main::myconfig;
90 if(!exists $form->{addition} && ($form->{id} ne "")) {
91 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
92 $form->{addition} = "ADDED";
97 $form->{title} = "Add";
98 $form->{callback} = "ar.pl?action=add" unless $form->{callback};
100 AR->get_transdate(\%myconfig, $form);
101 $form->{initial_transdate} = $form->{transdate};
102 create_links(dont_save => 1);
103 $form->{transdate} = $form->{initial_transdate};
105 $main::lxdebug->leave_sub();
109 $main::lxdebug->enter_sub();
111 $main::auth->assert('ar_transactions');
113 my $form = $main::form;
115 # show history button
116 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
117 #/show hhistory button
118 $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
119 $form->{title} = "Edit";
124 $main::lxdebug->leave_sub();
128 $main::lxdebug->enter_sub();
130 $main::auth->assert('ar_transactions');
132 my $form = $main::form;
137 $main::lxdebug->leave_sub();
140 sub _retrieve_invoice_object {
141 return undef if !$::form->{id};
142 return $::form->{invoice_obj} if $::form->{invoice_obj} && $::form->{invoice_obj}->id == $::form->{id};
143 return SL::DB::Invoice->new(id => $::form->{id})->load;
147 $main::lxdebug->enter_sub();
149 $main::auth->assert('ar_transactions');
152 my $form = $main::form;
153 my %myconfig = %main::myconfig;
155 $form->create_links("AR", \%myconfig, "customer");
156 $form->{invoice_obj} = _retrieve_invoice_object();
159 if (!$params{dont_save}) {
160 %saved = map { ($_ => $form->{$_}) } qw(direct_debit id taxincluded);
161 $saved{duedate} = $form->{duedate} if $form->{duedate};
162 $saved{currency} = $form->{currency} if $form->{currency};
165 IS->get_customer(\%myconfig, \%$form);
167 $form->{$_} = $saved{$_} for keys %saved;
168 $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
169 $form->{rowcount} = 1;
172 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
174 $form->{selectcurrency} = "";
175 map { $form->{selectcurrency} .= "<option>$_\n" } $form->get_all_currencies(\%myconfig);
178 if (@{ $form->{all_customer} || [] }) {
179 $form->{customer} = "$form->{customer}--$form->{customer_id}";
180 map { $form->{selectcustomer} .= "<option>$_->{name}--$_->{id}\n" }
181 (@{ $form->{all_customer} });
185 if (@{ $form->{all_departments} || [] }) {
186 $form->{selectdepartment} = "<option>\n";
187 $form->{department} = "$form->{department}--$form->{department_id}";
190 $form->{selectdepartment} .=
191 "<option>$_->{description}--$_->{id}\n"
192 } (@{ $form->{all_departments} || [] });
195 $form->{employee} = "$form->{employee}--$form->{employee_id}";
198 if (@{ $form->{all_employees} || [] }) {
199 $form->{selectemployee} = "";
200 map { $form->{selectemployee} .= "<option>$_->{name}--$_->{id}\n" }
201 (@{ $form->{all_employees} || [] });
204 # build the popup menus
205 $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
207 AR->setup_form($form);
210 ($form->datetonum($form->{transdate}, \%myconfig) <=
211 $form->datetonum($form->{closedto}, \%myconfig));
213 $main::lxdebug->leave_sub();
217 $main::lxdebug->enter_sub();
219 $main::auth->assert('ar_transactions');
221 my $form = $main::form;
222 my %myconfig = %main::myconfig;
223 my $locale = $main::locale;
224 my $cgi = $::request->{cgi};
226 $form->{invoice_obj} = _retrieve_invoice_object();
228 my ($title, $readonly, $exchangerate, $rows);
229 my ($notes, $department, $customer, $employee, $amount, $project);
233 $title = $form->{title};
234 # $locale->text('Add Accounts Receivables Transaction')
235 # $locale->text('Edit Accounts Receivables Transaction')
236 $form->{title} = $locale->text("$title Accounts Receivables Transaction");
238 $form->{javascript} = qq|<script type="text/javascript">
240 function setTaxkey(accno, row) {
241 var taxkey = accno.options[accno.selectedIndex].value;
242 var reg = /--([0-9]*)/;
243 var found = reg.exec(taxkey);
244 var index = found[1];
245 index = parseInt(index);
246 var tax = 'taxchart_' + row;
247 for (var i = 0; i < document.getElementById(tax).options.length; ++i) {
248 var reg2 = new RegExp("^"+ index, "");
249 if (reg2.exec(document.getElementById(tax).options[i].value)) {
250 document.getElementById(tax).options[i].selected = true;
257 # show history button js
258 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
259 #/show history button js
260 $readonly = ($form->{id}) ? "readonly" : "";
262 $form->{radier} = ($::instance_conf->get_ar_changeable == 2)
263 ? ($form->current_date(\%myconfig) eq $form->{gldate})
264 : ($::instance_conf->get_ar_changeable == 1);
265 $readonly = ($form->{radier}) ? "" : $readonly;
267 # set option selected
268 foreach my $item (qw(customer currency department employee)) {
269 $form->{"select$item"} =~ s/ selected//;
270 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
273 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'buy');
274 $form->{exchangerate} = $form->{forex} if $form->{forex};
276 # format exchangerate
277 $form->{exchangerate} = $form->{exchangerate} ? $form->format_amount(\%myconfig, $form->{exchangerate}) : '';
279 $rows = max 2, $form->numtextrows($form->{notes}, 50);
281 my @old_project_ids = grep { $_ } map { $form->{"project_id_$_"} } 1..$form->{rowcount};
283 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
285 "old_id" => \@old_project_ids },
286 "charts" => { "key" => "ALL_CHARTS",
287 "transdate" => $form->{transdate} },
288 "taxcharts" => { "key" => "ALL_TAXCHARTS",
289 "module" => "AR" },);
291 $_->{link_split} = { map { $_ => 1 } split/:/, $_->{link} } for @{ $form->{ALL_CHARTS} };
293 my %project_labels = map { $_->{id} => $_->{projectnumber} } @{ $form->{"ALL_PROJECTS"} };
295 my (@AR_amount_values);
297 my (@AR_paid_values);
302 foreach my $item (@{ $form->{ALL_CHARTS} }) {
303 if ($item->{link_split}{AR_amount}) {
304 $taxchart_init = $item->{tax_id} if ($taxchart_init eq "");
305 my $key = "$item->{accno}--$item->{tax_id}";
306 push(@AR_amount_values, $key);
307 } elsif ($item->{link_split}{AR}) {
308 push(@AR_values, $item->{accno});
309 } elsif ($item->{link_split}{AR_paid}) {
310 push(@AR_paid_values, $item->{accno});
313 # weirdness for AR_amount
314 $chart_labels{$item->{accno}} = "$item->{accno}--$item->{description}";
315 $chart_labels{"$item->{accno}--$item->{tax_id}"} = "$item->{accno}--$item->{description}";
317 $charts{$item->{accno}} = $item;
320 my %taxchart_labels = ();
321 my @taxchart_values = ();
323 foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
324 my $key = "$item->{id}--$item->{rate}";
325 $taxchart_init = $key if ($taxchart_init eq $item->{id});
326 push(@taxchart_values, $key);
327 $taxchart_labels{$key} = "$item->{taxdescription} " . ($item->{rate} * 100) . ' %';
328 $taxcharts{$item->{id}} = $item;
331 my $follow_up_vc = $form->{customer};
332 $follow_up_vc =~ s/--.*?//;
333 my $follow_up_trans_info = "$form->{invnumber} ($follow_up_vc)";
335 $form->{javascript} .=
336 qq|<script type="text/javascript" src="js/show_vc_details.js"></script>| .
337 qq|<script type="text/javascript" src="js/follow_up.js"></script>| .
338 qq|<script type="text/javascript" src="js/kivi.Draft.js"></script>|;
340 # $amount = $locale->text('Amount');
341 # $project = $locale->text('Project');
344 for my $i (1 .. $form->{rowcount}) {
346 amount => $form->{"amount_$i"},
347 tax => $form->{"tax_$i"},
348 project_id => ($i==$form->{rowcount}) ? $form->{globalproject_id} : $form->{"project_id_$i"},
351 my $selected_accno_full;
352 my ($accno_row) = split(/--/, $form->{"AR_amount_$i"});
353 my $item = $charts{$accno_row};
354 $selected_accno_full = "$item->{accno}--$item->{tax_id}";
356 my $selected_taxchart = $form->{"taxchart_$i"};
357 my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full);
358 my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_AR_amount_$i"});
360 if ($previous_accno &&
361 ($previous_accno eq $selected_accno) &&
362 ($previous_tax_id ne $selected_tax_id)) {
363 my $item = $taxcharts{$selected_tax_id};
364 $selected_taxchart = "$item->{id}--$item->{rate}";
367 if (!$form->{"taxchart_$i"}) {
368 if ($form->{"AR_amount_$i"} =~ m/.--./) {
369 $selected_taxchart = join '--', map { ($_->{id}, $_->{rate}) } first { $_->{id} == $item->{tax_id} } @{ $form->{ALL_TAXCHARTS} };
371 $selected_taxchart = $taxchart_init;
375 $transaction->{selectAR_amount} =
376 NTI($cgi->popup_menu('-name' => "AR_amount_$i",
377 '-id' => "AR_amount_$i",
378 '-style' => 'width:400px',
379 '-onChange' => "setTaxkey(this, $i)",
380 '-values' => \@AR_amount_values,
381 '-labels' => \%chart_labels,
382 '-default' => $selected_accno_full))
383 . $cgi->hidden('-name' => "previous_AR_amount_$i",
384 '-default' => $selected_accno_full);
386 $transaction->{taxchart} =
387 NTI($cgi->popup_menu('-name' => "taxchart_$i",
388 '-id' => "taxchart_$i",
389 '-style' => 'width:200px',
390 '-values' => \@taxchart_values,
391 '-labels' => \%taxchart_labels,
392 '-default' => $selected_taxchart));
394 push @transactions, $transaction;
397 $form->{invtotal_unformatted} = $form->{invtotal};
400 NTI($cgi->popup_menu('-name' => "ARselected", '-id' => "ARselected",
401 '-style' => 'width:400px',
402 '-values' => \@AR_values, '-labels' => \%chart_labels,
403 '-default' => $form->{ARselected}));
406 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
408 my $now = $form->current_date(\%myconfig);
411 for my $i (1 .. $form->{paidaccounts}) {
413 paid => $form->{"paid_$i"},
414 exchangerate => $form->{"exchangerate_$i"} || '',
415 gldate => $form->{"gldate_$i"},
416 acc_trans_id => $form->{"acc_trans_id_$i"},
417 source => $form->{"source_$i"},
418 memo => $form->{"memo_$i"},
419 AR_paid => $form->{"AR_paid_$i"},
420 forex => $form->{"forex_$i"},
421 datepaid => $form->{"datepaid_$i"},
422 paid_project_id => $form->{"paid_project_id_$i"},
423 gldate => $form->{"gldate_$i"},
426 # default account for current assets (i.e. 1801 - SKR04) if no account is selected
427 $form->{accno_arap} = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
429 $payment->{selectAR_paid} =
430 NTI($cgi->popup_menu('-name' => "AR_paid_$i",
431 '-id' => "AR_paid_$i",
432 '-values' => \@AR_paid_values,
433 '-labels' => \%chart_labels,
434 '-default' => $payment->{AR_paid} || $form->{accno_arap}));
438 $payment->{changeable} =
439 SL::DB::Default->get->payments_changeable == 0 ? !$payment->{acc_trans_id} # never
440 : SL::DB::Default->get->payments_changeable == 2 ? $payment->{gldate} eq '' || $payment->{gldate} eq $now
443 #deaktivieren von gebuchten Zahlungen ausserhalb der Bücherkontrolle, vorher prüfen ob heute eingegeben
444 if ($form->date_closed($payment->{"gldate_$i"})) {
445 $payment->{changeable} = 0;
448 push @payments, $payment;
451 my @empty = grep { $_->{paid} eq '' } @payments;
453 (sort_by { DateTime->from_kivitendo($_->{datepaid}) } grep { $_->{paid} ne '' } @payments),
457 $form->{totalpaid} = sum map { $_->{paid} } @payments;
460 print $::form->parse_html_template('ar/form_header', {
461 paid_missing => $::form->{invtotal} - $::form->{totalpaid},
462 show_exch => ($::form->{defaultcurrency} && ($::form->{currency} ne $::form->{defaultcurrency})),
463 payments => \@payments,
464 transactions => \@transactions,
465 project_labels => \%project_labels,
467 ARselected => $ARselected,
469 follow_up_trans_info => $follow_up_trans_info,
470 today => DateTime->today,
473 $main::lxdebug->leave_sub();
477 $main::lxdebug->enter_sub();
479 $main::auth->assert('ar_transactions');
481 my $form = $main::form;
482 my %myconfig = %main::myconfig;
483 my $locale = $main::locale;
484 my $cgi = $::request->{cgi};
487 my $follow_ups = FU->follow_ups('trans_id' => $form->{id}, 'not_done' => 1);
488 if ( @{ $follow_ups} ) {
489 $form->{follow_up_length} = scalar(@{$follow_ups});
490 $form->{follow_up_due_length} = sum(map({ $_->{due} * 1 } @{ $follow_ups }));
494 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
495 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
497 $form->{is_closed} = $transdate <= $closedto;
499 # ToDO: - insert a global check for stornos, so that a storno is only possible a limited time after saving it
500 $form->{show_storno_button} =
502 !IS->has_storno(\%myconfig, $form, 'ar') &&
503 !IS->is_storno(\%myconfig, $form, 'ar') &&
504 ($form->{totalpaid} == 0 || $form->{totalpaid} eq "");
506 $form->{show_mark_as_paid_button} = $form->{id} && $::instance_conf->get_ar_show_mark_as_paid();
508 print $::form->parse_html_template('ar/form_footer');
510 $main::lxdebug->leave_sub();
514 $main::lxdebug->enter_sub();
516 $main::auth->assert('ar_transactions');
518 my $form = $main::form;
519 my %myconfig = %main::myconfig;
521 &mark_as_paid_common(\%myconfig,"ar");
523 $main::lxdebug->leave_sub();
527 $::form->{transdate} = DateTime->today_local->to_kivitendo if !$::form->{transdate};
528 $::form->{gldate} = $::form->{transdate} if !$::form->{gldate};
533 $main::lxdebug->enter_sub();
535 $main::auth->assert('ar_transactions');
537 my $form = $main::form;
538 my %myconfig = %main::myconfig;
542 my ($totaltax, $exchangerate);
544 $form->{invtotal} = 0;
546 delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
548 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
549 qw(exchangerate creditlimit creditremaining);
551 my @flds = qw(amount AR_amount projectnumber oldprojectnumber project_id);
555 for my $i (1 .. $form->{rowcount}) {
556 $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
557 if ($form->{"amount_$i"}) {
560 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
563 ($tmpnetamount,$form->{"tax_$i"}) = $form->calculate_tax($form->{"amount_$i"},$rate,$form->{taxincluded},2);
565 $totaltax += $form->{"tax_$i"};
566 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
571 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
572 $form->{rowcount} = $count + 1;
573 map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
575 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'buy');
576 $form->{exchangerate} = $form->{forex} if $form->{forex};
578 $form->{invdate} = $form->{transdate};
580 my %saved_variables = map +( $_ => $form->{$_} ), qw(AR AR_amount_1 taxchart_1 customer_id notes);
582 &check_name("customer");
584 $form->{AR} = $saved_variables{AR};
585 if ($saved_variables{AR_amount_1} =~ m/.--./) {
586 map { $form->{$_} = $saved_variables{$_} } qw(AR_amount_1 taxchart_1);
588 delete $form->{taxchart_1};
592 ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
594 for my $i (1 .. $form->{paidaccounts}) {
595 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
598 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
599 } qw(paid exchangerate);
601 $form->{totalpaid} += $form->{"paid_$i"};
603 $form->{"forex_$i"} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
604 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
608 $form->{creditremaining} -=
609 ($form->{invtotal} - $form->{totalpaid} + $form->{oldtotalpaid} -
610 $form->{oldinvtotal});
611 $form->{oldinvtotal} = $form->{invtotal};
612 $form->{oldtotalpaid} = $form->{totalpaid};
616 $main::lxdebug->leave_sub();
620 # ToDO: fix $closedto and $invdate
623 $main::lxdebug->enter_sub();
625 $main::auth->assert('ar_transactions');
627 my $form = $main::form;
628 my %myconfig = %main::myconfig;
629 my $locale = $main::locale;
631 $form->mtime_ischanged('ar');
632 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
634 my $invdate = $form->datetonum($form->{transdate}, \%myconfig);
636 for my $i (1 .. $form->{paidaccounts}) {
638 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
639 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
641 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
643 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
644 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
646 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
647 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
648 $form->error($locale->text('Cannot post payment for a closed period!'))
649 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
651 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
652 # $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid);
653 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
658 ($form->{AR}) = split /--/, $form->{AR};
659 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
660 if (AR->post_payment(\%myconfig, \%$form)) {
661 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
662 $form->{what_done} = 'invoice';
663 $form->{addition} = "PAYMENT POSTED";
665 $form->redirect($locale->text('Payment posted!'))
667 $form->error($locale->text('Cannot post payment!'));
670 $main::lxdebug->leave_sub();
675 $main::auth->assert('ar_transactions');
677 my $form = $main::form;
684 $main::lxdebug->enter_sub();
686 $main::auth->assert('ar_transactions');
688 my $form = $main::form;
689 my %myconfig = %main::myconfig;
690 my $locale = $main::locale;
694 $form->mtime_ischanged('ar');
698 # check if there is an invoice number, invoice and due date
699 $form->isblank("transdate", $locale->text('Invoice Date missing!'));
700 $form->isblank("duedate", $locale->text('Due Date missing!'));
701 $form->isblank("customer", $locale->text('Customer missing!'));
703 if ($myconfig{mandatory_departments} && !$form->{department}) {
704 $form->{saved_message} = $::locale->text('You have to specify a department.');
709 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
710 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
712 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
713 if ($form->date_max_future($transdate, \%myconfig));
715 $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
717 $form->error($locale->text('Zero amount posting!'))
718 unless grep $_*1, map $form->parse_amount(\%myconfig, $form->{"amount_$_"}), 1..$form->{rowcount};
720 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
721 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
725 for my $i (1 .. $form->{paidaccounts}) {
726 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
727 $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
729 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
731 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
732 if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
734 #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
735 # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
736 $form->error($locale->text('Cannot post payment for a closed period!'))
737 if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
739 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
740 $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
741 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
746 # if oldcustomer ne customer redo form
747 my ($customer) = split /--/, $form->{customer};
748 if ($form->{oldcustomer} ne "$customer--$form->{customer_id}") {
750 $::dispatcher->end_request;
753 $form->{AR}{receivables} = $form->{ARselected};
756 $form->{id} = 0 if $form->{postasnew};
757 $form->error($locale->text('Cannot post transaction!')) unless AR->post_transaction(\%myconfig, \%$form);
760 if(!exists $form->{addition} && $form->{id} ne "") {
761 $form->{snumbers} = "invnumber_$form->{invnumber}";
762 $form->{what_done} = "invoice";
763 $form->{addition} = "POSTED";
766 # /saving the history
768 $form->redirect($locale->text('Transaction posted!')) unless $inline;
770 $main::lxdebug->leave_sub();
774 $main::lxdebug->enter_sub();
776 $main::auth->assert('ar_transactions');
778 my $form = $main::form;
779 my %myconfig = %main::myconfig;
781 $form->{postasnew} = 1;
783 if(!exists $form->{addition} && $form->{id} ne "") {
784 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
785 $form->{what_done} = "invoice";
786 $form->{addition} = "POSTED AS NEW";
789 # /saving the history
792 $main::lxdebug->leave_sub();
796 $main::lxdebug->enter_sub();
798 $main::auth->assert('ar_transactions');
800 my $form = $main::form;
801 my %myconfig = %main::myconfig;
803 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);
804 $form->{paidaccounts} = 1;
806 $form->{invdate} = $form->current_date(\%myconfig);
809 $main::lxdebug->leave_sub();
813 $main::lxdebug->enter_sub();
815 $main::auth->assert('ar_transactions');
817 my $form = $main::form;
818 my $locale = $main::locale;
820 $form->{title} = $locale->text('Confirm!');
824 delete $form->{header};
827 <form method=post action=$form->{script}>
830 foreach my $key (keys %$form) {
831 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
832 $form->{$key} =~ s/\"/"/g;
833 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
837 <h2 class=confirm>$form->{title}</h2>
840 . $locale->text('Are you sure you want to delete Transaction')
841 . qq| $form->{invnumber}</h4>
843 <input name=action class=submit type=submit value="|
844 . $locale->text('Yes') . qq|">
848 $main::lxdebug->leave_sub();
852 $main::lxdebug->enter_sub();
854 $main::auth->assert('ar_transactions');
856 my $form = $main::form;
857 my %myconfig = %main::myconfig;
858 my $locale = $main::locale;
860 if (AR->delete_transaction(\%myconfig, \%$form)) {
862 if(!exists $form->{addition}) {
863 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
864 $form->{what_done} = "invoice";
865 $form->{addition} = "DELETED";
868 # /saving the history
869 $form->redirect($locale->text('Transaction deleted!'));
871 $form->error($locale->text('Cannot delete transaction!'));
873 $main::lxdebug->leave_sub();
877 $main::lxdebug->enter_sub();
879 $main::auth->assert('invoice_edit');
881 my $form = $main::form;
882 my %myconfig = %main::myconfig;
883 my $locale = $main::locale;
884 my $cgi = $::request->{cgi};
886 # setup customer selection
887 $form->all_vc(\%myconfig, "customer", "AR");
889 $form->{title} = $locale->text('AR Transactions');
891 # Auch in Rechnungsübersicht nach Kundentyp filtern - jan
892 $form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 },
893 "departments" => "ALL_DEPARTMENTS",
894 "customers" => "ALL_VC",
895 "business_types" => "ALL_BUSINESS_TYPES");
896 $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
897 $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
899 $form->{CT_CUSTOM_VARIABLES} = CVar->get_configs('module' => 'CT');
900 ($form->{CT_CUSTOM_VARIABLES_FILTER_CODE},
901 $form->{CT_CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $form->{CT_CUSTOM_VARIABLES},
902 'include_prefix' => 'l_',
903 'include_value' => 'Y');
905 # constants and subs for template
906 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
909 print $form->parse_html_template('ar/search', { %myconfig });
911 $main::lxdebug->leave_sub();
914 sub create_subtotal_row {
915 $main::lxdebug->enter_sub();
917 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
919 my $form = $main::form;
920 my %myconfig = %main::myconfig;
922 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
924 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
926 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
928 map { $totals->{$_} = 0 } @{ $subtotal_columns };
930 $main::lxdebug->leave_sub();
935 sub ar_transactions {
936 $main::lxdebug->enter_sub();
938 $main::auth->assert('invoice_edit');
940 my $form = $main::form;
941 my %myconfig = %main::myconfig;
942 my $locale = $main::locale;
944 my ($callback, $href, @columns);
946 ($form->{customer}, $form->{customer_id}) = split(/--/, $form->{customer});
948 report_generator_set_default_sort('transdate', 1);
950 AR->ar_transactions(\%myconfig, \%$form);
952 $form->{title} = $locale->text('AR Transactions');
954 my $report = SL::ReportGenerator->new(\%myconfig, $form);
957 qw(ids transdate id type invnumber ordnumber cusordnumber name netamount tax amount paid
958 datepaid due duedate transaction_description notes salesman employee shippingpoint shipvia
959 marge_total marge_percent globalprojectnumber customernumber country ustid taxzone
960 payment_terms charts customertype direct_debit dunning_description department);
962 my $ct_cvar_configs = CVar->get_configs('module' => 'CT');
963 my @ct_includeable_custom_variables = grep { $_->{includeable} } @{ $ct_cvar_configs };
964 my @ct_searchable_custom_variables = grep { $_->{searchable} } @{ $ct_cvar_configs };
966 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @ct_includeable_custom_variables;
967 push @columns, map { "cvar_$_->{name}" } @ct_includeable_custom_variables;
969 my @hidden_variables = map { "l_${_}" } @columns;
970 push @hidden_variables, "l_subtotal", qw(open closed customer invnumber ordnumber cusordnumber transaction_description notes project_id transdatefrom transdateto duedatefrom duedateto
971 employee_id salesman_id business_id parts_partnumber parts_description department_id);
972 push @hidden_variables, map { "cvar_$_->{name}" } @ct_searchable_custom_variables;
974 $href = build_std_url('action=ar_transactions', grep { $form->{$_} } @hidden_variables);
977 'ids' => { raw_header_data => $::request->presenter->checkbox_tag("", id => "check_all", checkall => "[data-checkall=1]"), align => 'center' },
978 'transdate' => { 'text' => $locale->text('Date'), },
979 'id' => { 'text' => $locale->text('ID'), },
980 'type' => { 'text' => $locale->text('Type'), },
981 'invnumber' => { 'text' => $locale->text('Invoice'), },
982 'ordnumber' => { 'text' => $locale->text('Order'), },
983 'cusordnumber' => { 'text' => $locale->text('Customer Order Number'), },
984 'name' => { 'text' => $locale->text('Customer'), },
985 'netamount' => { 'text' => $locale->text('Amount'), },
986 'tax' => { 'text' => $locale->text('Tax'), },
987 'amount' => { 'text' => $locale->text('Total'), },
988 'paid' => { 'text' => $locale->text('Paid'), },
989 'datepaid' => { 'text' => $locale->text('Date Paid'), },
990 'due' => { 'text' => $locale->text('Amount Due'), },
991 'duedate' => { 'text' => $locale->text('Due Date'), },
992 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
993 'notes' => { 'text' => $locale->text('Notes'), },
994 'salesman' => { 'text' => $locale->text('Salesperson'), },
995 'employee' => { 'text' => $locale->text('Employee'), },
996 'shippingpoint' => { 'text' => $locale->text('Shipping Point'), },
997 'shipvia' => { 'text' => $locale->text('Ship via'), },
998 'globalprojectnumber' => { 'text' => $locale->text('Document Project Number'), },
999 'marge_total' => { 'text' => $locale->text('Ertrag'), },
1000 'marge_percent' => { 'text' => $locale->text('Ertrag prozentual'), },
1001 'customernumber' => { 'text' => $locale->text('Customer Number'), },
1002 'country' => { 'text' => $locale->text('Country'), },
1003 'ustid' => { 'text' => $locale->text('USt-IdNr.'), },
1004 'taxzone' => { 'text' => $locale->text('Steuersatz'), },
1005 'payment_terms' => { 'text' => $locale->text('Payment Terms'), },
1006 'charts' => { 'text' => $locale->text('Buchungskonto'), },
1007 'customertype' => { 'text' => $locale->text('Customer type'), },
1008 'direct_debit' => { 'text' => $locale->text('direct debit'), },
1009 'department' => { 'text' => $locale->text('Department'), },
1010 dunning_description => { 'text' => $locale->text('Dunning level'), },
1014 foreach my $name (qw(id transdate duedate invnumber ordnumber cusordnumber name datepaid employee shippingpoint shipvia transaction_description direct_debit)) {
1015 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
1016 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
1019 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
1021 $form->{"l_type"} = "Y";
1022 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
1024 $column_defs{ids}->{visible} = 'HTML';
1026 $report->set_columns(%column_defs);
1027 $report->set_column_order(@columns);
1029 $report->set_export_options('ar_transactions', @hidden_variables, qw(sort sortdir));
1031 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
1033 CVar->add_custom_variables_to_report('module' => 'CT',
1034 'trans_id_field' => 'customer_id',
1035 'configs' => $ct_cvar_configs,
1036 'column_defs' => \%column_defs,
1037 'data' => $form->{AR});
1040 if ($form->{customer}) {
1041 push @options, $locale->text('Customer') . " : $form->{customer}";
1043 if ($form->{cp_name}) {
1044 push @options, $locale->text('Contact Person') . " : $form->{cp_name}";
1047 # $form->{department} seems to never be filled, and showing the department_id
1048 # at the top of the report doesn't make much sense.
1049 # So determine the department name from the id whenever we have a filter for
1051 if ($form->{department}) {
1052 my ($department) = split /--/, $form->{department};
1053 push @options, $locale->text('Department') . " : $department";
1055 if ($form->{department_id}) {
1056 # push @options, $locale->text('Department Id') . " : $form->{department_id}";
1057 unless ($form->{department}) {
1058 require SL::DB::Department;
1059 my $department = SL::DB::Manager::Department->find_by(id => $::form->{department_id});
1060 push @options, $locale->text('Department') . " : " . $department->description if $department;
1063 if ($form->{invnumber}) {
1064 push @options, $locale->text('Invoice Number') . " : $form->{invnumber}";
1066 if ($form->{ordnumber}) {
1067 push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
1069 if ($form->{cusordnumber}) {
1070 push @options, $locale->text('Customer Order Number') . " : $form->{cusordnumber}";
1072 if ($form->{notes}) {
1073 push @options, $locale->text('Notes') . " : $form->{notes}";
1075 if ($form->{transaction_description}) {
1076 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
1078 if ($form->{parts_partnumber}) {
1079 push @options, $locale->text('Part Number') . " : $form->{parts_partnumber}";
1081 if ($form->{parts_description}) {
1082 push @options, $locale->text('Part Description') . " : $form->{parts_description}";
1084 if ($form->{transdatefrom}) {
1085 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1087 if ($form->{transdateto}) {
1088 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
1090 if ($form->{open}) {
1091 push @options, $locale->text('Open');
1093 if ($form->{employee_id}) {
1094 my $employee = SL::DB::Employee->new(id => $form->{employee_id})->load;
1095 push @options, $locale->text('Employee') . ' : ' . $employee->name;
1097 if ($form->{salesman_id}) {
1098 my $salesman = SL::DB::Employee->new(id => $form->{salesman_id})->load;
1099 push @options, $locale->text('Salesman') . ' : ' . $salesman->name;
1101 if ($form->{closed}) {
1102 push @options, $locale->text('Closed');
1105 $form->{ALL_PRINTERS} = SL::DB::Manager::Printer->get_all_sorted;
1107 $report->set_options('top_info_text' => join("\n", @options),
1108 'raw_top_info_text' => $form->parse_html_template('ar/ar_transactions_header'),
1109 'raw_bottom_info_text' => $form->parse_html_template('ar/ar_transactions_bottom'),
1110 'output_format' => 'HTML',
1111 'title' => $form->{title},
1112 'attachment_basename' => $locale->text('invoice_list') . strftime('_%Y%m%d', localtime time),
1114 $report->set_options_from_form();
1115 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1117 # add sort and escape callback, this one we use for the add sub
1118 $form->{callback} = $href .= "&sort=$form->{sort}";
1120 # escape callback for href
1121 $callback = $form->escape($href);
1123 my @subtotal_columns = qw(netamount amount paid due marge_total marge_percent);
1125 my %totals = map { $_ => 0 } @subtotal_columns;
1126 my %subtotals = map { $_ => 0 } @subtotal_columns;
1130 foreach my $ar (@{ $form->{AR} }) {
1131 $ar->{tax} = $ar->{amount} - $ar->{netamount};
1132 $ar->{due} = $ar->{amount} - $ar->{paid};
1134 map { $subtotals{$_} += $ar->{$_};
1135 $totals{$_} += $ar->{$_} } @subtotal_columns;
1137 $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
1138 $totals{marge_percent} = $totals{netamount} ? ($totals{marge_total} * 100 / $totals{netamount} ) : 0;
1140 my $is_storno = $ar->{storno} && $ar->{storno_id};
1141 my $has_storno = $ar->{storno} && !$ar->{storno_id};
1144 $has_storno ? $locale->text("Invoice with Storno (abbreviation)") :
1145 $is_storno ? $locale->text("Storno (one letter abbreviation)") :
1146 $ar->{amount} < 0 ? $locale->text("Credit note (one letter abbreviation)") :
1147 $ar->{invoice} ? $locale->text("Invoice (one letter abbreviation)") :
1148 $locale->text("AR Transaction (abbreviation)");
1150 map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(netamount tax amount paid due marge_total marge_percent);
1152 $ar->{direct_debit} = $ar->{direct_debit} ? $::locale->text('yes') : $::locale->text('no');
1156 foreach my $column (@columns) {
1158 'data' => $ar->{$column},
1159 'align' => $column_alignment{$column},
1163 $row->{invnumber}->{link} = build_std_url("script=" . ($ar->{invoice} ? 'is.pl' : 'ar.pl'), 'action=edit')
1164 . "&id=" . E($ar->{id}) . "&callback=${callback}";
1167 raw_data => $::request->presenter->checkbox_tag("id[]", value => $ar->{id}, "data-checkall" => 1),
1172 my $row_set = [ $row ];
1174 if (($form->{l_subtotal} eq 'Y')
1175 && (($idx == (scalar @{ $form->{AR} } - 1))
1176 || ($ar->{ $form->{sort} } ne $form->{AR}->[$idx + 1]->{ $form->{sort} }))) {
1177 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1180 $report->add_data($row_set);
1185 $report->add_separator();
1186 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1188 $report->generate_with_headers();
1190 $main::lxdebug->leave_sub();
1194 $main::lxdebug->enter_sub();
1196 $main::auth->assert('ar_transactions');
1198 my $form = $main::form;
1199 my %myconfig = %main::myconfig;
1200 my $locale = $main::locale;
1202 # don't cancel cancelled transactions
1203 if (IS->has_storno(\%myconfig, $form, 'ar')) {
1204 $form->{title} = $locale->text("Cancel Accounts Receivables Transaction");
1205 $form->error($locale->text("Transaction has already been cancelled!"));
1208 AR->storno($form, \%myconfig, $form->{id});
1210 # saving the history
1211 if(!exists $form->{addition} && $form->{id} ne "") {
1212 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1213 $form->{addition} = "STORNO";
1214 $form->{what_done} = "invoice";
1215 $form->save_history;
1217 # /saving the history
1219 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1221 $main::lxdebug->leave_sub();