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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
30 # Accounts Receivables
32 #======================================================================
34 use POSIX qw(strftime);
35 use List::Util qw(sum first max);
36 use List::UtilsBy qw(sort_by);
43 use SL::ReportGenerator;
45 require "bin/mozilla/arap.pl";
46 require "bin/mozilla/common.pl";
47 require "bin/mozilla/drafts.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('general_ledger');
86 my $form = $main::form;
87 my %myconfig = %main::myconfig;
89 return $main::lxdebug->leave_sub() if (load_draft_maybe());
92 if(!exists $form->{addition} && ($form->{id} ne "")) {
93 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
94 $form->{addition} = "ADDED";
99 $form->{title} = "Add";
100 $form->{callback} = "ar.pl?action=add&DONT_LOAD_DRAFT=1" unless $form->{callback};
102 AR->get_transdate(\%myconfig, $form);
103 $form->{initial_transdate} = $form->{transdate};
104 create_links(dont_save => 1);
105 $form->{transdate} = $form->{initial_transdate};
107 $main::lxdebug->leave_sub();
111 $main::lxdebug->enter_sub();
113 $main::auth->assert('general_ledger');
115 my $form = $main::form;
117 # show history button
118 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
119 #/show hhistory button
120 $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
121 $form->{title} = "Edit";
126 $main::lxdebug->leave_sub();
130 $main::lxdebug->enter_sub();
132 $main::auth->assert('general_ledger');
134 my $form = $main::form;
139 $main::lxdebug->leave_sub();
143 $main::lxdebug->enter_sub();
145 $main::auth->assert('general_ledger');
148 my $form = $main::form;
149 my %myconfig = %main::myconfig;
151 $form->create_links("AR", \%myconfig, "customer");
154 if (!$params{dont_save}) {
155 %saved = map { ($_ => $form->{$_}) } qw(direct_debit id taxincluded);
156 $saved{duedate} = $form->{duedate} if $form->{duedate};
159 IS->get_customer(\%myconfig, \%$form);
161 $form->{$_} = $saved{$_} for keys %saved;
162 $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
163 $form->{rowcount} = 1;
166 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
168 map { $form->{selectcurrency} .= "<option>$_\n" } $form->get_all_currencies(\%myconfig);
171 if (@{ $form->{all_customer} || [] }) {
172 $form->{customer} = "$form->{customer}--$form->{customer_id}";
173 map { $form->{selectcustomer} .= "<option>$_->{name}--$_->{id}\n" }
174 (@{ $form->{all_customer} });
178 if (@{ $form->{all_departments} || [] }) {
179 $form->{selectdepartment} = "<option>\n";
180 $form->{department} = "$form->{department}--$form->{department_id}";
183 $form->{selectdepartment} .=
184 "<option>$_->{description}--$_->{id}\n"
185 } (@{ $form->{all_departments} || [] });
188 $form->{employee} = "$form->{employee}--$form->{employee_id}";
191 if (@{ $form->{all_employees} || [] }) {
192 $form->{selectemployee} = "";
193 map { $form->{selectemployee} .= "<option>$_->{name}--$_->{id}\n" }
194 (@{ $form->{all_employees} || [] });
197 # build the popup menus
198 $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
200 AR->setup_form($form);
203 ($form->datetonum($form->{transdate}, \%myconfig) <=
204 $form->datetonum($form->{closedto}, \%myconfig));
206 $main::lxdebug->leave_sub();
210 $main::lxdebug->enter_sub();
212 $main::auth->assert('general_ledger');
214 my $form = $main::form;
215 my %myconfig = %main::myconfig;
216 my $locale = $main::locale;
217 my $cgi = $::request->{cgi};
219 my ($title, $readonly, $exchangerate, $rows);
220 my ($notes, $department, $customer, $employee, $amount, $project);
224 $title = $form->{title};
225 # $locale->text('Add Accounts Receivables Transaction')
226 # $locale->text('Edit Accounts Receivables Transaction')
227 $form->{title} = $locale->text("$title Accounts Receivables Transaction");
229 $form->{javascript} = qq|<script type="text/javascript">
231 function setTaxkey(accno, row) {
232 var taxkey = accno.options[accno.selectedIndex].value;
233 var reg = /--([0-9]*)/;
234 var found = reg.exec(taxkey);
235 var index = found[1];
236 index = parseInt(index);
237 var tax = 'taxchart_' + row;
238 for (var i = 0; i < document.getElementById(tax).options.length; ++i) {
239 var reg2 = new RegExp("^"+ index, "");
240 if (reg2.exec(document.getElementById(tax).options[i].value)) {
241 document.getElementById(tax).options[i].selected = true;
248 # show history button js
249 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
250 #/show history button js
251 $readonly = ($form->{id}) ? "readonly" : "";
253 $form->{radier} = ($::instance_conf->get_ar_changeable == 2)
254 ? ($form->current_date(\%myconfig) eq $form->{gldate})
255 : ($::instance_conf->get_ar_changeable == 1);
256 $readonly = ($form->{radier}) ? "" : $readonly;
258 # set option selected
259 foreach my $item (qw(customer currency department employee)) {
260 $form->{"select$item"} =~ s/ selected//;
261 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
264 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'buy');
265 $form->{exchangerate} = $form->{forex} if $form->{forex};
267 $rows = max 2, $form->numtextrows($form->{notes}, 50);
269 my @old_project_ids = grep { $_ } map { $form->{"project_id_$_"} } 1..$form->{rowcount};
271 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
273 "old_id" => \@old_project_ids },
274 "charts" => { "key" => "ALL_CHARTS",
275 "transdate" => $form->{transdate} },
276 "taxcharts" => { "key" => "ALL_TAXCHARTS",
277 "module" => "AR" },);
279 $_->{link_split} = { map { $_ => 1 } split/:/, $_->{link} } for @{ $form->{ALL_CHARTS} };
281 my %project_labels = map { $_->{id} => $_->{projectnumber} } @{ $form->{"ALL_PROJECTS"} };
283 my (@AR_amount_values);
285 my (@AR_paid_values);
290 foreach my $item (@{ $form->{ALL_CHARTS} }) {
291 if ($item->{link_split}{AR_amount}) {
292 $taxchart_init = $item->{tax_id} if ($taxchart_init eq "");
293 my $key = "$item->{accno}--$item->{tax_id}";
294 push(@AR_amount_values, $key);
295 } elsif ($item->{link_split}{AR}) {
296 push(@AR_values, $item->{accno});
297 } elsif ($item->{link_split}{AR_paid}) {
298 push(@AR_paid_values, $item->{accno});
301 # weirdness for AR_amount
302 $chart_labels{$item->{accno}} = "$item->{accno}--$item->{description}";
303 $chart_labels{"$item->{accno}--$item->{tax_id}"} = "$item->{accno}--$item->{description}";
305 $charts{$item->{accno}} = $item;
308 my %taxchart_labels = ();
309 my @taxchart_values = ();
311 foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
312 my $key = "$item->{id}--$item->{rate}";
313 $taxchart_init = $key if ($taxchart_init eq $item->{id});
314 push(@taxchart_values, $key);
315 $taxchart_labels{$key} = "$item->{taxdescription} " . ($item->{rate} * 100) . ' %';
316 $taxcharts{$item->{id}} = $item;
319 my $follow_up_vc = $form->{customer};
320 $follow_up_vc =~ s/--.*?//;
321 my $follow_up_trans_info = "$form->{invnumber} ($follow_up_vc)";
323 $form->{javascript} .=
324 qq|<script type="text/javascript" src="js/show_vc_details.js"></script>| .
325 qq|<script type="text/javascript" src="js/follow_up.js"></script>|;
327 # $amount = $locale->text('Amount');
328 # $project = $locale->text('Project');
331 for my $i (1 .. $form->{rowcount}) {
333 amount => $form->{"amount_$i"},
334 tax => $form->{"tax_$i"},
335 project_id => ($i==$form->{rowcount}) ? $form->{globalproject_id} : $form->{"project_id_$i"},
338 my $selected_accno_full;
339 my ($accno_row) = split(/--/, $form->{"AR_amount_$i"});
340 my $item = $charts{$accno_row};
341 $selected_accno_full = "$item->{accno}--$item->{tax_id}";
343 my $selected_taxchart = $form->{"taxchart_$i"};
344 my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full);
345 my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_AR_amount_$i"});
347 if ($previous_accno &&
348 ($previous_accno eq $selected_accno) &&
349 ($previous_tax_id ne $selected_tax_id)) {
350 my $item = $taxcharts{$selected_tax_id};
351 $selected_taxchart = "$item->{id}--$item->{rate}";
354 if (!$form->{"taxchart_$i"}) {
355 if ($form->{"AR_amount_$i"} =~ m/.--./) {
356 $selected_taxchart = join '--', map { ($_->{id}, $_->{rate}) } first { $_->{id} == $item->{tax_id} } @{ $form->{ALL_TAXCHARTS} };
358 $selected_taxchart = $taxchart_init;
362 $transaction->{selectAR_amount} =
363 NTI($cgi->popup_menu('-name' => "AR_amount_$i",
364 '-id' => "AR_amount_$i",
365 '-style' => 'width:400px',
366 '-onChange' => "setTaxkey(this, $i)",
367 '-values' => \@AR_amount_values,
368 '-labels' => \%chart_labels,
369 '-default' => $selected_accno_full))
370 . $cgi->hidden('-name' => "previous_AR_amount_$i",
371 '-default' => $selected_accno_full);
373 $transaction->{taxchart} =
374 NTI($cgi->popup_menu('-name' => "taxchart_$i",
375 '-id' => "taxchart_$i",
376 '-style' => 'width:200px',
377 '-values' => \@taxchart_values,
378 '-labels' => \%taxchart_labels,
379 '-default' => $selected_taxchart));
381 push @transactions, $transaction;
384 $form->{invtotal_unformatted} = $form->{invtotal};
387 NTI($cgi->popup_menu('-name' => "ARselected", '-id' => "ARselected",
388 '-style' => 'width:400px',
389 '-values' => \@AR_values, '-labels' => \%chart_labels,
390 '-default' => $form->{ARselected}));
393 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
395 my $now = $form->current_date(\%myconfig);
398 for my $i (1 .. $form->{paidaccounts}) {
400 paid => $form->{"paid_$i"},
401 exchangerate => $form->{"exchangerate_$i"} || '',
402 gldate => $form->{"gldate_$i"},
403 acc_trans_id => $form->{"acc_trans_id_$i"},
404 source => $form->{"source_$i"},
405 memo => $form->{"memo_$i"},
406 AR_paid => $form->{"AR_paid_$i"},
407 forex => $form->{"forex_$i"},
408 datepaid => $form->{"datepaid_$i"},
409 paid_project_id => $form->{"paid_project_id_$i"},
410 gldate => $form->{"gldate_$i"},
413 # default account for current assets (i.e. 1801 - SKR04) if no account is selected
414 $form->{accno_arap} = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
416 $payment->{selectAR_paid} =
417 NTI($cgi->popup_menu('-name' => "AR_paid_$i",
418 '-id' => "AR_paid_$i",
419 '-values' => \@AR_paid_values,
420 '-labels' => \%chart_labels,
421 '-default' => $payment->{AR_paid} || $form->{accno_arap}));
425 $payment->{changeable} =
426 SL::DB::Default->get->payments_changeable == 0 ? !$payment->{acc_trans_id} # never
427 : SL::DB::Default->get->payments_changeable == 2 ? $payment->{gldate} eq '' || $payment->{gldate} eq $now
430 push @payments, $payment;
433 my @empty = grep { $_->{paid} eq '' } @payments;
435 (sort_by { DateTime->from_kivitendo($_->{datepaid}) } grep { $_->{paid} ne '' } @payments),
439 $form->{totalpaid} = sum map { $_->{paid} } @payments;
442 print $::form->parse_html_template('ar/form_header', {
443 paid_missing => $::form->{invtotal} - $::form->{totalpaid},
444 show_exch => ($::form->{defaultcurrency} && ($::form->{currency} ne $::form->{defaultcurrency})),
445 payments => \@payments,
446 transactions => \@transactions,
447 project_labels => \%project_labels,
449 ARselected => $ARselected,
451 follow_up_trans_info => $follow_up_trans_info,
452 today => DateTime->today,
455 $main::lxdebug->leave_sub();
459 $main::lxdebug->enter_sub();
461 $main::auth->assert('general_ledger');
463 my $form = $main::form;
464 my %myconfig = %main::myconfig;
465 my $locale = $main::locale;
466 my $cgi = $::request->{cgi};
469 my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
470 if ( @{ $follow_ups} ) {
471 $form->{follow_up_length} = scalar(@{$follow_ups});
472 $form->{follow_up_due_length} = sum(map({ $_->{due} * 1 } @{ $follow_ups }));
476 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
477 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
479 $form->{is_closed} = $transdate <= $closedto;
481 # ToDO: - insert a global check for stornos, so that a storno is only possible a limited time after saving it
482 $form->{show_storno_button} =
484 !IS->has_storno(\%myconfig, $form, 'ar') &&
485 !IS->is_storno(\%myconfig, $form, 'ar') &&
486 ($form->{totalpaid} == 0 || $form->{totalpaid} eq "");
488 $form->{show_mark_as_paid_button} = $form->{id} && $::instance_conf->get_ar_show_mark_as_paid();
490 print $::form->parse_html_template('ar/form_footer');
492 $main::lxdebug->leave_sub();
496 $main::lxdebug->enter_sub();
498 $main::auth->assert('general_ledger');
500 my $form = $main::form;
501 my %myconfig = %main::myconfig;
503 &mark_as_paid_common(\%myconfig,"ar");
505 $main::lxdebug->leave_sub();
509 $main::lxdebug->enter_sub();
511 $main::auth->assert('general_ledger');
513 my $form = $main::form;
514 my %myconfig = %main::myconfig;
518 my ($totaltax, $exchangerate);
520 $form->{invtotal} = 0;
522 delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
524 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
525 qw(exchangerate creditlimit creditremaining);
527 my @flds = qw(amount AR_amount projectnumber oldprojectnumber project_id);
531 for my $i (1 .. $form->{rowcount}) {
532 $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
533 if ($form->{"amount_$i"}) {
536 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
539 ($tmpnetamount,$form->{"tax_$i"}) = $form->calculate_tax($form->{"amount_$i"},$rate,$form->{taxincluded},2);
541 $totaltax += $form->{"tax_$i"};
542 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
547 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
548 $form->{rowcount} = $count + 1;
549 map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
551 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'buy');
552 $form->{exchangerate} = $form->{forex} if $form->{forex};
554 $form->{invdate} = $form->{transdate};
556 $form->{invdate} = $form->{transdate};
558 my %saved_variables = map +( $_ => $form->{$_} ), qw(AR AR_amount_1 taxchart_1 customer_id notes);
560 &check_name("customer");
562 $form->{AR} = $saved_variables{AR};
563 if ($saved_variables{AR_amount_1} =~ m/.--./) {
564 map { $form->{$_} = $saved_variables{$_} } qw(AR_amount_1 taxchart_1);
566 delete $form->{taxchart_1};
570 ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
572 for my $i (1 .. $form->{paidaccounts}) {
573 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
576 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
577 } qw(paid exchangerate);
579 $form->{totalpaid} += $form->{"paid_$i"};
581 $form->{"forex_$i"} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
582 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
586 $form->{creditremaining} -=
587 ($form->{invtotal} - $form->{totalpaid} + $form->{oldtotalpaid} -
588 $form->{oldinvtotal});
589 $form->{oldinvtotal} = $form->{invtotal};
590 $form->{oldtotalpaid} = $form->{totalpaid};
594 $main::lxdebug->leave_sub();
598 # ToDO: fix $closedto and $invdate
601 $main::lxdebug->enter_sub();
603 $main::auth->assert('general_ledger');
605 my $form = $main::form;
606 my %myconfig = %main::myconfig;
607 my $locale = $main::locale;
609 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
611 my $invdate = $form->datetonum($form->{transdate}, \%myconfig);
613 for my $i (1 .. $form->{paidaccounts}) {
615 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
616 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
618 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
620 $form->error($locale->text('Cannot post payment for a closed period!')) if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
622 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
623 # $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid);
624 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
629 ($form->{AR}) = split /--/, $form->{AR};
630 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
631 if (AR->post_payment(\%myconfig, \%$form)) {
632 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
633 $form->{what_done} = 'invoice';
634 $form->{addition} = "PAYMENT POSTED";
636 $form->redirect($locale->text('Payment posted!'))
638 $form->error($locale->text('Cannot post payment!'));
641 $main::lxdebug->leave_sub();
646 $main::auth->assert('general_ledger');
648 my $form = $main::form;
655 $main::lxdebug->enter_sub();
657 $main::auth->assert('general_ledger');
659 my $form = $main::form;
660 my %myconfig = %main::myconfig;
661 my $locale = $main::locale;
667 # check if there is an invoice number, invoice and due date
668 $form->isblank("transdate", $locale->text('Invoice Date missing!'));
669 $form->isblank("duedate", $locale->text('Due Date missing!'));
670 $form->isblank("customer", $locale->text('Customer missing!'));
672 if ($myconfig{mandatory_departments} && !$form->{department}) {
673 $form->{saved_message} = $::locale->text('You have to specify a department.');
678 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
679 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
681 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
682 if ($form->date_max_future($transdate, \%myconfig));
683 $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
685 $form->error($locale->text('Zero amount posting!'))
686 unless grep $_*1, map $form->parse_amount(\%myconfig, $form->{"amount_$_"}), 1..$form->{rowcount};
688 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
689 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
693 for my $i (1 .. $form->{paidaccounts}) {
694 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
695 $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
697 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
699 $form->error($locale->text('Cannot post payment for a closed period!'))
700 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
702 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
703 $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
704 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
709 # if oldcustomer ne customer redo form
710 my ($customer) = split /--/, $form->{customer};
711 if ($form->{oldcustomer} ne "$customer--$form->{customer_id}") {
716 $form->{AR}{receivables} = $form->{ARselected};
719 $form->{id} = 0 if $form->{postasnew};
720 $form->error($locale->text('Cannot post transaction!')) unless AR->post_transaction(\%myconfig, \%$form);
723 if(!exists $form->{addition} && $form->{id} ne "") {
724 $form->{snumbers} = "invnumber_$form->{invnumber}";
725 $form->{what_done} = "invoice";
726 $form->{addition} = "POSTED";
729 # /saving the history
730 remove_draft() if $form->{remove_draft};
732 $form->redirect($locale->text('Transaction posted!')) unless $inline;
734 $main::lxdebug->leave_sub();
738 $main::lxdebug->enter_sub();
740 $main::auth->assert('general_ledger');
742 my $form = $main::form;
743 my %myconfig = %main::myconfig;
745 $form->{postasnew} = 1;
747 if(!exists $form->{addition} && $form->{id} ne "") {
748 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
749 $form->{what_done} = "invoice";
750 $form->{addition} = "POSTED AS NEW";
753 # /saving the history
756 $main::lxdebug->leave_sub();
760 $main::lxdebug->enter_sub();
762 $main::auth->assert('general_ledger');
764 my $form = $main::form;
765 my %myconfig = %main::myconfig;
767 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);
768 $form->{paidaccounts} = 1;
770 $form->{invdate} = $form->current_date(\%myconfig);
773 $main::lxdebug->leave_sub();
777 $main::lxdebug->enter_sub();
779 $main::auth->assert('general_ledger');
781 my $form = $main::form;
782 my $locale = $main::locale;
784 $form->{title} = $locale->text('Confirm!');
788 delete $form->{header};
791 <form method=post action=$form->{script}>
794 foreach my $key (keys %$form) {
795 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
796 $form->{$key} =~ s/\"/"/g;
797 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
801 <h2 class=confirm>$form->{title}</h2>
804 . $locale->text('Are you sure you want to delete Transaction')
805 . qq| $form->{invnumber}</h4>
807 <input name=action class=submit type=submit value="|
808 . $locale->text('Yes') . qq|">
812 $main::lxdebug->leave_sub();
816 $main::lxdebug->enter_sub();
818 $main::auth->assert('general_ledger');
820 my $form = $main::form;
821 my %myconfig = %main::myconfig;
822 my $locale = $main::locale;
824 if (AR->delete_transaction(\%myconfig, \%$form)) {
826 if(!exists $form->{addition}) {
827 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
828 $form->{what_done} = "invoice";
829 $form->{addition} = "DELETED";
832 # /saving the history
833 $form->redirect($locale->text('Transaction deleted!'));
835 $form->error($locale->text('Cannot delete transaction!'));
837 $main::lxdebug->leave_sub();
841 $main::lxdebug->enter_sub();
843 $main::auth->assert('invoice_edit');
845 my $form = $main::form;
846 my %myconfig = %main::myconfig;
847 my $locale = $main::locale;
848 my $cgi = $::request->{cgi};
850 # setup customer selection
851 $form->all_vc(\%myconfig, "customer", "AR");
853 $form->{title} = $locale->text('AR Transactions');
855 # Auch in Rechnungsübersicht nach Kundentyp filtern - jan
856 $form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 },
857 "departments" => "ALL_DEPARTMENTS",
858 "customers" => "ALL_VC",
859 "business_types" => "ALL_BUSINESS_TYPES");
860 $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
861 $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
863 $form->{CT_CUSTOM_VARIABLES} = CVar->get_configs('module' => 'CT');
864 ($form->{CT_CUSTOM_VARIABLES_FILTER_CODE},
865 $form->{CT_CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $form->{CT_CUSTOM_VARIABLES},
866 'include_prefix' => 'l_',
867 'include_value' => 'Y');
869 # constants and subs for template
870 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
873 print $form->parse_html_template('ar/search', { %myconfig });
875 $main::lxdebug->leave_sub();
878 sub create_subtotal_row {
879 $main::lxdebug->enter_sub();
881 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
883 my $form = $main::form;
884 my %myconfig = %main::myconfig;
886 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
888 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
890 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
892 map { $totals->{$_} = 0 } @{ $subtotal_columns };
894 $main::lxdebug->leave_sub();
899 sub ar_transactions {
900 $main::lxdebug->enter_sub();
902 $main::auth->assert('invoice_edit');
904 my $form = $main::form;
905 my %myconfig = %main::myconfig;
906 my $locale = $main::locale;
908 my ($callback, $href, @columns);
910 ($form->{customer}, $form->{customer_id}) = split(/--/, $form->{customer});
912 report_generator_set_default_sort('transdate', 1);
914 AR->ar_transactions(\%myconfig, \%$form);
916 $form->{title} = $locale->text('AR Transactions');
918 my $report = SL::ReportGenerator->new(\%myconfig, $form);
921 qw(transdate id type invnumber ordnumber cusordnumber name netamount tax amount paid
922 datepaid due duedate transaction_description notes salesman employee shippingpoint shipvia
923 marge_total marge_percent globalprojectnumber customernumber country ustid taxzone payment_terms charts customertype direct_debit);
925 my $ct_cvar_configs = CVar->get_configs('module' => 'CT');
926 my @ct_includeable_custom_variables = grep { $_->{includeable} } @{ $ct_cvar_configs };
927 my @ct_searchable_custom_variables = grep { $_->{searchable} } @{ $ct_cvar_configs };
929 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @ct_includeable_custom_variables;
930 push @columns, map { "cvar_$_->{name}" } @ct_includeable_custom_variables;
932 my @hidden_variables = map { "l_${_}" } @columns;
933 push @hidden_variables, "l_subtotal", qw(open closed customer invnumber ordnumber cusordnumber transaction_description notes project_id transdatefrom transdateto duedatefrom duedateto employee_id salesman_id business_id);
934 push @hidden_variables, map { "cvar_$_->{name}" } @ct_searchable_custom_variables;
936 $href = build_std_url('action=ar_transactions', grep { $form->{$_} } @hidden_variables);
939 'transdate' => { 'text' => $locale->text('Date'), },
940 'id' => { 'text' => $locale->text('ID'), },
941 'type' => { 'text' => $locale->text('Type'), },
942 'invnumber' => { 'text' => $locale->text('Invoice'), },
943 'ordnumber' => { 'text' => $locale->text('Order'), },
944 'cusordnumber' => { 'text' => $locale->text('Customer Order Number'), },
945 'name' => { 'text' => $locale->text('Customer'), },
946 'netamount' => { 'text' => $locale->text('Amount'), },
947 'tax' => { 'text' => $locale->text('Tax'), },
948 'amount' => { 'text' => $locale->text('Total'), },
949 'paid' => { 'text' => $locale->text('Paid'), },
950 'datepaid' => { 'text' => $locale->text('Date Paid'), },
951 'due' => { 'text' => $locale->text('Amount Due'), },
952 'duedate' => { 'text' => $locale->text('Due Date'), },
953 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
954 'notes' => { 'text' => $locale->text('Notes'), },
955 'salesman' => { 'text' => $locale->text('Salesperson'), },
956 'employee' => { 'text' => $locale->text('Employee'), },
957 'shippingpoint' => { 'text' => $locale->text('Shipping Point'), },
958 'shipvia' => { 'text' => $locale->text('Ship via'), },
959 'globalprojectnumber' => { 'text' => $locale->text('Document Project Number'), },
960 'marge_total' => { 'text' => $locale->text('Ertrag'), },
961 'marge_percent' => { 'text' => $locale->text('Ertrag prozentual'), },
962 'customernumber' => { 'text' => $locale->text('Customer Number'), },
963 'country' => { 'text' => $locale->text('Country'), },
964 'ustid' => { 'text' => $locale->text('USt-IdNr.'), },
965 'taxzone' => { 'text' => $locale->text('Steuersatz'), },
966 'payment_terms' => { 'text' => $locale->text('Payment Terms'), },
967 'charts' => { 'text' => $locale->text('Buchungskonto'), },
968 'customertype' => { 'text' => $locale->text('Customer type'), },
969 'direct_debit' => { 'text' => $locale->text('direct debit'), },
973 foreach my $name (qw(id transdate duedate invnumber ordnumber cusordnumber name datepaid employee shippingpoint shipvia transaction_description direct_debit)) {
974 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
975 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
978 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
980 $form->{"l_type"} = "Y";
981 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
983 $report->set_columns(%column_defs);
984 $report->set_column_order(@columns);
986 $report->set_export_options('ar_transactions', @hidden_variables, qw(sort sortdir));
988 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
990 CVar->add_custom_variables_to_report('module' => 'CT',
991 'trans_id_field' => 'customer_id',
992 'configs' => $ct_cvar_configs,
993 'column_defs' => \%column_defs,
994 'data' => $form->{AR});
997 if ($form->{customer}) {
998 push @options, $locale->text('Customer') . " : $form->{customer}";
1000 if ($form->{cp_name}) {
1001 push @options, $locale->text('Contact Person') . " : $form->{cp_name}";
1003 if ($form->{department}) {
1004 my ($department) = split /--/, $form->{department};
1005 push @options, $locale->text('Department') . " : $department";
1007 if ($form->{department_id}) {
1008 push @options, $locale->text('Department Id') . " : $form->{department_id}";
1010 if ($form->{invnumber}) {
1011 push @options, $locale->text('Invoice Number') . " : $form->{invnumber}";
1013 if ($form->{ordnumber}) {
1014 push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
1016 if ($form->{cusordnumber}) {
1017 push @options, $locale->text('Customer Order Number') . " : $form->{cusordnumber}";
1019 if ($form->{notes}) {
1020 push @options, $locale->text('Notes') . " : $form->{notes}";
1022 if ($form->{transaction_description}) {
1023 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
1025 if ($form->{transdatefrom}) {
1026 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1028 if ($form->{transdateto}) {
1029 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
1031 if ($form->{open}) {
1032 push @options, $locale->text('Open');
1034 if ($form->{employee_id}) {
1035 my $employee = SL::DB::Employee->new(id => $form->{employee_id})->load;
1036 push @options, $locale->text('Employee') . ' : ' . $employee->name;
1038 if ($form->{salesman_id}) {
1039 my $salesman = SL::DB::Employee->new(id => $form->{salesman_id})->load;
1040 push @options, $locale->text('Salesman') . ' : ' . $salesman->name;
1042 if ($form->{closed}) {
1043 push @options, $locale->text('Closed');
1046 $report->set_options('top_info_text' => join("\n", @options),
1047 'raw_bottom_info_text' => $form->parse_html_template('ar/ar_transactions_bottom'),
1048 'output_format' => 'HTML',
1049 'title' => $form->{title},
1050 'attachment_basename' => $locale->text('invoice_list') . strftime('_%Y%m%d', localtime time),
1052 $report->set_options_from_form();
1053 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1055 # add sort and escape callback, this one we use for the add sub
1056 $form->{callback} = $href .= "&sort=$form->{sort}";
1058 # escape callback for href
1059 $callback = $form->escape($href);
1061 my @subtotal_columns = qw(netamount amount paid due marge_total marge_percent);
1063 my %totals = map { $_ => 0 } @subtotal_columns;
1064 my %subtotals = map { $_ => 0 } @subtotal_columns;
1068 foreach my $ar (@{ $form->{AR} }) {
1069 $ar->{tax} = $ar->{amount} - $ar->{netamount};
1070 $ar->{due} = $ar->{amount} - $ar->{paid};
1072 map { $subtotals{$_} += $ar->{$_};
1073 $totals{$_} += $ar->{$_} } @subtotal_columns;
1075 $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
1076 $totals{marge_percent} = $totals{netamount} ? ($totals{marge_total} * 100 / $totals{netamount} ) : 0;
1078 map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(netamount tax amount paid due marge_total marge_percent);
1080 my $is_storno = $ar->{storno} && $ar->{storno_id};
1081 my $has_storno = $ar->{storno} && !$ar->{storno_id};
1084 $has_storno ? $locale->text("Invoice with Storno (abbreviation)") :
1085 $is_storno ? $locale->text("Storno (one letter abbreviation)") :
1086 $ar->{amount} < 0 ? $locale->text("Credit note (one letter abbreviation)") :
1087 $ar->{invoice} ? $locale->text("Invoice (one letter abbreviation)") :
1088 $locale->text("AR Transaction (abbreviation)");
1090 $ar->{direct_debit} = $ar->{direct_debit} ? $::locale->text('yes') : $::locale->text('no');
1094 foreach my $column (@columns) {
1096 'data' => $ar->{$column},
1097 'align' => $column_alignment{$column},
1101 $row->{invnumber}->{link} = build_std_url("script=" . ($ar->{invoice} ? 'is.pl' : 'ar.pl'), 'action=edit')
1102 . "&id=" . E($ar->{id}) . "&callback=${callback}";
1104 my $row_set = [ $row ];
1106 if (($form->{l_subtotal} eq 'Y')
1107 && (($idx == (scalar @{ $form->{AR} } - 1))
1108 || ($ar->{ $form->{sort} } ne $form->{AR}->[$idx + 1]->{ $form->{sort} }))) {
1109 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1112 $report->add_data($row_set);
1117 $report->add_separator();
1118 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1120 $report->generate_with_headers();
1122 $main::lxdebug->leave_sub();
1126 $main::lxdebug->enter_sub();
1128 $main::auth->assert('general_ledger');
1130 my $form = $main::form;
1131 my %myconfig = %main::myconfig;
1132 my $locale = $main::locale;
1134 # don't cancel cancelled transactions
1135 if (IS->has_storno(\%myconfig, $form, 'ar')) {
1136 $form->{title} = $locale->text("Cancel Accounts Receivables Transaction");
1137 $form->error($locale->text("Transaction has already been cancelled!"));
1140 AR->storno($form, \%myconfig, $form->{id});
1142 # saving the history
1143 if(!exists $form->{addition} && $form->{id} ne "") {
1144 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1145 $form->{addition} = "STORNO";
1146 $form->{what_done} = "invoice";
1147 $form->save_history;
1149 # /saving the history
1151 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1153 $main::lxdebug->leave_sub();