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);
44 use SL::ReportGenerator;
46 require "bin/mozilla/arap.pl";
47 require "bin/mozilla/common.pl";
48 require "bin/mozilla/drafts.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('general_ledger');
87 my $form = $main::form;
88 my %myconfig = %main::myconfig;
90 return $main::lxdebug->leave_sub() if (load_draft_maybe());
93 if(!exists $form->{addition} && ($form->{id} ne "")) {
94 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
95 $form->{addition} = "ADDED";
100 $form->{title} = "Add";
101 $form->{callback} = "ar.pl?action=add&DONT_LOAD_DRAFT=1" unless $form->{callback};
103 AR->get_transdate(\%myconfig, $form);
104 $form->{initial_transdate} = $form->{transdate};
105 create_links(dont_save => 1);
106 $form->{transdate} = $form->{initial_transdate};
108 $main::lxdebug->leave_sub();
112 $main::lxdebug->enter_sub();
114 $main::auth->assert('general_ledger');
116 my $form = $main::form;
118 # show history button
119 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
120 #/show hhistory button
121 $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
122 $form->{title} = "Edit";
127 $main::lxdebug->leave_sub();
131 $main::lxdebug->enter_sub();
133 $main::auth->assert('general_ledger');
135 my $form = $main::form;
140 $main::lxdebug->leave_sub();
143 sub _retrieve_invoice_object {
144 return undef if !$::form->{id};
145 return $::form->{invoice_obj} if $::form->{invoice_obj} && $::form->{invoice_obj}->id == $::form->{id};
146 return SL::DB::Invoice->new(id => $::form->{id})->load;
150 $main::lxdebug->enter_sub();
152 $main::auth->assert('general_ledger');
155 my $form = $main::form;
156 my %myconfig = %main::myconfig;
158 $form->create_links("AR", \%myconfig, "customer");
159 $form->{invoice_obj} = _retrieve_invoice_object();
162 if (!$params{dont_save}) {
163 %saved = map { ($_ => $form->{$_}) } qw(direct_debit id taxincluded);
164 $saved{duedate} = $form->{duedate} if $form->{duedate};
167 IS->get_customer(\%myconfig, \%$form);
169 $form->{$_} = $saved{$_} for keys %saved;
170 $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
171 $form->{rowcount} = 1;
174 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
176 map { $form->{selectcurrency} .= "<option>$_\n" } $form->get_all_currencies(\%myconfig);
179 if (@{ $form->{all_customer} || [] }) {
180 $form->{customer} = "$form->{customer}--$form->{customer_id}";
181 map { $form->{selectcustomer} .= "<option>$_->{name}--$_->{id}\n" }
182 (@{ $form->{all_customer} });
186 if (@{ $form->{all_departments} || [] }) {
187 $form->{selectdepartment} = "<option>\n";
188 $form->{department} = "$form->{department}--$form->{department_id}";
191 $form->{selectdepartment} .=
192 "<option>$_->{description}--$_->{id}\n"
193 } (@{ $form->{all_departments} || [] });
196 $form->{employee} = "$form->{employee}--$form->{employee_id}";
199 if (@{ $form->{all_employees} || [] }) {
200 $form->{selectemployee} = "";
201 map { $form->{selectemployee} .= "<option>$_->{name}--$_->{id}\n" }
202 (@{ $form->{all_employees} || [] });
205 # build the popup menus
206 $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
208 AR->setup_form($form);
211 ($form->datetonum($form->{transdate}, \%myconfig) <=
212 $form->datetonum($form->{closedto}, \%myconfig));
214 $main::lxdebug->leave_sub();
218 $main::lxdebug->enter_sub();
220 $main::auth->assert('general_ledger');
222 my $form = $main::form;
223 my %myconfig = %main::myconfig;
224 my $locale = $main::locale;
225 my $cgi = $::request->{cgi};
227 $form->{invoice_obj} = _retrieve_invoice_object();
229 my ($title, $readonly, $exchangerate, $rows);
230 my ($notes, $department, $customer, $employee, $amount, $project);
234 $title = $form->{title};
235 # $locale->text('Add Accounts Receivables Transaction')
236 # $locale->text('Edit Accounts Receivables Transaction')
237 $form->{title} = $locale->text("$title Accounts Receivables Transaction");
239 $form->{javascript} = qq|<script type="text/javascript">
241 function setTaxkey(accno, row) {
242 var taxkey = accno.options[accno.selectedIndex].value;
243 var reg = /--([0-9]*)/;
244 var found = reg.exec(taxkey);
245 var index = found[1];
246 index = parseInt(index);
247 var tax = 'taxchart_' + row;
248 for (var i = 0; i < document.getElementById(tax).options.length; ++i) {
249 var reg2 = new RegExp("^"+ index, "");
250 if (reg2.exec(document.getElementById(tax).options[i].value)) {
251 document.getElementById(tax).options[i].selected = true;
258 # show history button js
259 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
260 #/show history button js
261 $readonly = ($form->{id}) ? "readonly" : "";
263 $form->{radier} = ($::instance_conf->get_ar_changeable == 2)
264 ? ($form->current_date(\%myconfig) eq $form->{gldate})
265 : ($::instance_conf->get_ar_changeable == 1);
266 $readonly = ($form->{radier}) ? "" : $readonly;
268 # set option selected
269 foreach my $item (qw(customer currency department employee)) {
270 $form->{"select$item"} =~ s/ selected//;
271 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
274 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'buy');
275 $form->{exchangerate} = $form->{forex} if $form->{forex};
277 $rows = max 2, $form->numtextrows($form->{notes}, 50);
279 my @old_project_ids = grep { $_ } map { $form->{"project_id_$_"} } 1..$form->{rowcount};
281 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
283 "old_id" => \@old_project_ids },
284 "charts" => { "key" => "ALL_CHARTS",
285 "transdate" => $form->{transdate} },
286 "taxcharts" => { "key" => "ALL_TAXCHARTS",
287 "module" => "AR" },);
289 $_->{link_split} = { map { $_ => 1 } split/:/, $_->{link} } for @{ $form->{ALL_CHARTS} };
291 my %project_labels = map { $_->{id} => $_->{projectnumber} } @{ $form->{"ALL_PROJECTS"} };
293 my (@AR_amount_values);
295 my (@AR_paid_values);
300 foreach my $item (@{ $form->{ALL_CHARTS} }) {
301 if ($item->{link_split}{AR_amount}) {
302 $taxchart_init = $item->{tax_id} if ($taxchart_init eq "");
303 my $key = "$item->{accno}--$item->{tax_id}";
304 push(@AR_amount_values, $key);
305 } elsif ($item->{link_split}{AR}) {
306 push(@AR_values, $item->{accno});
307 } elsif ($item->{link_split}{AR_paid}) {
308 push(@AR_paid_values, $item->{accno});
311 # weirdness for AR_amount
312 $chart_labels{$item->{accno}} = "$item->{accno}--$item->{description}";
313 $chart_labels{"$item->{accno}--$item->{tax_id}"} = "$item->{accno}--$item->{description}";
315 $charts{$item->{accno}} = $item;
318 my %taxchart_labels = ();
319 my @taxchart_values = ();
321 foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
322 my $key = "$item->{id}--$item->{rate}";
323 $taxchart_init = $key if ($taxchart_init eq $item->{id});
324 push(@taxchart_values, $key);
325 $taxchart_labels{$key} = "$item->{taxdescription} " . ($item->{rate} * 100) . ' %';
326 $taxcharts{$item->{id}} = $item;
329 my $follow_up_vc = $form->{customer};
330 $follow_up_vc =~ s/--.*?//;
331 my $follow_up_trans_info = "$form->{invnumber} ($follow_up_vc)";
333 $form->{javascript} .=
334 qq|<script type="text/javascript" src="js/show_vc_details.js"></script>| .
335 qq|<script type="text/javascript" src="js/follow_up.js"></script>|;
337 # $amount = $locale->text('Amount');
338 # $project = $locale->text('Project');
341 for my $i (1 .. $form->{rowcount}) {
343 amount => $form->{"amount_$i"},
344 tax => $form->{"tax_$i"},
345 project_id => ($i==$form->{rowcount}) ? $form->{globalproject_id} : $form->{"project_id_$i"},
348 my $selected_accno_full;
349 my ($accno_row) = split(/--/, $form->{"AR_amount_$i"});
350 my $item = $charts{$accno_row};
351 $selected_accno_full = "$item->{accno}--$item->{tax_id}";
353 my $selected_taxchart = $form->{"taxchart_$i"};
354 my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full);
355 my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_AR_amount_$i"});
357 if ($previous_accno &&
358 ($previous_accno eq $selected_accno) &&
359 ($previous_tax_id ne $selected_tax_id)) {
360 my $item = $taxcharts{$selected_tax_id};
361 $selected_taxchart = "$item->{id}--$item->{rate}";
364 if (!$form->{"taxchart_$i"}) {
365 if ($form->{"AR_amount_$i"} =~ m/.--./) {
366 $selected_taxchart = join '--', map { ($_->{id}, $_->{rate}) } first { $_->{id} == $item->{tax_id} } @{ $form->{ALL_TAXCHARTS} };
368 $selected_taxchart = $taxchart_init;
372 $transaction->{selectAR_amount} =
373 NTI($cgi->popup_menu('-name' => "AR_amount_$i",
374 '-id' => "AR_amount_$i",
375 '-style' => 'width:400px',
376 '-onChange' => "setTaxkey(this, $i)",
377 '-values' => \@AR_amount_values,
378 '-labels' => \%chart_labels,
379 '-default' => $selected_accno_full))
380 . $cgi->hidden('-name' => "previous_AR_amount_$i",
381 '-default' => $selected_accno_full);
383 $transaction->{taxchart} =
384 NTI($cgi->popup_menu('-name' => "taxchart_$i",
385 '-id' => "taxchart_$i",
386 '-style' => 'width:200px',
387 '-values' => \@taxchart_values,
388 '-labels' => \%taxchart_labels,
389 '-default' => $selected_taxchart));
391 push @transactions, $transaction;
394 $form->{invtotal_unformatted} = $form->{invtotal};
397 NTI($cgi->popup_menu('-name' => "ARselected", '-id' => "ARselected",
398 '-style' => 'width:400px',
399 '-values' => \@AR_values, '-labels' => \%chart_labels,
400 '-default' => $form->{ARselected}));
403 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
405 my $now = $form->current_date(\%myconfig);
408 for my $i (1 .. $form->{paidaccounts}) {
410 paid => $form->{"paid_$i"},
411 exchangerate => $form->{"exchangerate_$i"} || '',
412 gldate => $form->{"gldate_$i"},
413 acc_trans_id => $form->{"acc_trans_id_$i"},
414 source => $form->{"source_$i"},
415 memo => $form->{"memo_$i"},
416 AR_paid => $form->{"AR_paid_$i"},
417 forex => $form->{"forex_$i"},
418 datepaid => $form->{"datepaid_$i"},
419 paid_project_id => $form->{"paid_project_id_$i"},
420 gldate => $form->{"gldate_$i"},
423 # default account for current assets (i.e. 1801 - SKR04) if no account is selected
424 $form->{accno_arap} = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
426 $payment->{selectAR_paid} =
427 NTI($cgi->popup_menu('-name' => "AR_paid_$i",
428 '-id' => "AR_paid_$i",
429 '-values' => \@AR_paid_values,
430 '-labels' => \%chart_labels,
431 '-default' => $payment->{AR_paid} || $form->{accno_arap}));
435 $payment->{changeable} =
436 SL::DB::Default->get->payments_changeable == 0 ? !$payment->{acc_trans_id} # never
437 : SL::DB::Default->get->payments_changeable == 2 ? $payment->{gldate} eq '' || $payment->{gldate} eq $now
440 push @payments, $payment;
443 my @empty = grep { $_->{paid} eq '' } @payments;
445 (sort_by { DateTime->from_kivitendo($_->{datepaid}) } grep { $_->{paid} ne '' } @payments),
449 $form->{totalpaid} = sum map { $_->{paid} } @payments;
452 print $::form->parse_html_template('ar/form_header', {
453 paid_missing => $::form->{invtotal} - $::form->{totalpaid},
454 show_exch => ($::form->{defaultcurrency} && ($::form->{currency} ne $::form->{defaultcurrency})),
455 payments => \@payments,
456 transactions => \@transactions,
457 project_labels => \%project_labels,
459 ARselected => $ARselected,
461 follow_up_trans_info => $follow_up_trans_info,
462 today => DateTime->today,
465 $main::lxdebug->leave_sub();
469 $main::lxdebug->enter_sub();
471 $main::auth->assert('general_ledger');
473 my $form = $main::form;
474 my %myconfig = %main::myconfig;
475 my $locale = $main::locale;
476 my $cgi = $::request->{cgi};
479 my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
480 if ( @{ $follow_ups} ) {
481 $form->{follow_up_length} = scalar(@{$follow_ups});
482 $form->{follow_up_due_length} = sum(map({ $_->{due} * 1 } @{ $follow_ups }));
486 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
487 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
489 $form->{is_closed} = $transdate <= $closedto;
491 # ToDO: - insert a global check for stornos, so that a storno is only possible a limited time after saving it
492 $form->{show_storno_button} =
494 !IS->has_storno(\%myconfig, $form, 'ar') &&
495 !IS->is_storno(\%myconfig, $form, 'ar') &&
496 ($form->{totalpaid} == 0 || $form->{totalpaid} eq "");
498 $form->{show_mark_as_paid_button} = $form->{id} && $::instance_conf->get_ar_show_mark_as_paid();
500 print $::form->parse_html_template('ar/form_footer');
502 $main::lxdebug->leave_sub();
506 $main::lxdebug->enter_sub();
508 $main::auth->assert('general_ledger');
510 my $form = $main::form;
511 my %myconfig = %main::myconfig;
513 &mark_as_paid_common(\%myconfig,"ar");
515 $main::lxdebug->leave_sub();
519 $main::lxdebug->enter_sub();
521 $main::auth->assert('general_ledger');
523 my $form = $main::form;
524 my %myconfig = %main::myconfig;
528 my ($totaltax, $exchangerate);
530 $form->{invtotal} = 0;
532 delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
534 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
535 qw(exchangerate creditlimit creditremaining);
537 my @flds = qw(amount AR_amount projectnumber oldprojectnumber project_id);
541 for my $i (1 .. $form->{rowcount}) {
542 $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
543 if ($form->{"amount_$i"}) {
546 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
549 ($tmpnetamount,$form->{"tax_$i"}) = $form->calculate_tax($form->{"amount_$i"},$rate,$form->{taxincluded},2);
551 $totaltax += $form->{"tax_$i"};
552 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
557 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
558 $form->{rowcount} = $count + 1;
559 map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
561 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'buy');
562 $form->{exchangerate} = $form->{forex} if $form->{forex};
564 $form->{invdate} = $form->{transdate};
566 $form->{invdate} = $form->{transdate};
568 my %saved_variables = map +( $_ => $form->{$_} ), qw(AR AR_amount_1 taxchart_1 customer_id notes);
570 &check_name("customer");
572 $form->{AR} = $saved_variables{AR};
573 if ($saved_variables{AR_amount_1} =~ m/.--./) {
574 map { $form->{$_} = $saved_variables{$_} } qw(AR_amount_1 taxchart_1);
576 delete $form->{taxchart_1};
580 ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
582 for my $i (1 .. $form->{paidaccounts}) {
583 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
586 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
587 } qw(paid exchangerate);
589 $form->{totalpaid} += $form->{"paid_$i"};
591 $form->{"forex_$i"} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
592 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
596 $form->{creditremaining} -=
597 ($form->{invtotal} - $form->{totalpaid} + $form->{oldtotalpaid} -
598 $form->{oldinvtotal});
599 $form->{oldinvtotal} = $form->{invtotal};
600 $form->{oldtotalpaid} = $form->{totalpaid};
604 $main::lxdebug->leave_sub();
608 # ToDO: fix $closedto and $invdate
611 $main::lxdebug->enter_sub();
613 $main::auth->assert('general_ledger');
615 my $form = $main::form;
616 my %myconfig = %main::myconfig;
617 my $locale = $main::locale;
619 $form->mtime_ischanged('ar');
620 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
622 my $invdate = $form->datetonum($form->{transdate}, \%myconfig);
624 for my $i (1 .. $form->{paidaccounts}) {
626 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
627 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
629 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
631 $form->error($locale->text('Cannot post payment for a closed period!')) if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
633 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
634 # $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid);
635 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
640 ($form->{AR}) = split /--/, $form->{AR};
641 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
642 if (AR->post_payment(\%myconfig, \%$form)) {
643 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
644 $form->{what_done} = 'invoice';
645 $form->{addition} = "PAYMENT POSTED";
647 $form->redirect($locale->text('Payment posted!'))
649 $form->error($locale->text('Cannot post payment!'));
652 $main::lxdebug->leave_sub();
657 $main::auth->assert('general_ledger');
659 my $form = $main::form;
666 $main::lxdebug->enter_sub();
668 $main::auth->assert('general_ledger');
670 my $form = $main::form;
671 my %myconfig = %main::myconfig;
672 my $locale = $main::locale;
676 $form->mtime_ischanged('ar');
680 # check if there is an invoice number, invoice and due date
681 $form->isblank("transdate", $locale->text('Invoice Date missing!'));
682 $form->isblank("duedate", $locale->text('Due Date missing!'));
683 $form->isblank("customer", $locale->text('Customer missing!'));
685 if ($myconfig{mandatory_departments} && !$form->{department}) {
686 $form->{saved_message} = $::locale->text('You have to specify a department.');
691 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
692 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
694 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
695 if ($form->date_max_future($transdate, \%myconfig));
696 $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
698 $form->error($locale->text('Zero amount posting!'))
699 unless grep $_*1, map $form->parse_amount(\%myconfig, $form->{"amount_$_"}), 1..$form->{rowcount};
701 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
702 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
706 for my $i (1 .. $form->{paidaccounts}) {
707 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
708 $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
710 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
712 $form->error($locale->text('Cannot post payment for a closed period!'))
713 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
715 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
716 $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
717 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
722 # if oldcustomer ne customer redo form
723 my ($customer) = split /--/, $form->{customer};
724 if ($form->{oldcustomer} ne "$customer--$form->{customer_id}") {
729 $form->{AR}{receivables} = $form->{ARselected};
732 $form->{id} = 0 if $form->{postasnew};
733 $form->error($locale->text('Cannot post transaction!')) unless AR->post_transaction(\%myconfig, \%$form);
736 if(!exists $form->{addition} && $form->{id} ne "") {
737 $form->{snumbers} = "invnumber_$form->{invnumber}";
738 $form->{what_done} = "invoice";
739 $form->{addition} = "POSTED";
742 # /saving the history
743 remove_draft() if $form->{remove_draft};
745 $form->redirect($locale->text('Transaction posted!')) unless $inline;
747 $main::lxdebug->leave_sub();
751 $main::lxdebug->enter_sub();
753 $main::auth->assert('general_ledger');
755 my $form = $main::form;
756 my %myconfig = %main::myconfig;
758 $form->{postasnew} = 1;
760 if(!exists $form->{addition} && $form->{id} ne "") {
761 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
762 $form->{what_done} = "invoice";
763 $form->{addition} = "POSTED AS NEW";
766 # /saving the history
769 $main::lxdebug->leave_sub();
773 $main::lxdebug->enter_sub();
775 $main::auth->assert('general_ledger');
777 my $form = $main::form;
778 my %myconfig = %main::myconfig;
780 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);
781 $form->{paidaccounts} = 1;
783 $form->{invdate} = $form->current_date(\%myconfig);
786 $main::lxdebug->leave_sub();
790 $main::lxdebug->enter_sub();
792 $main::auth->assert('general_ledger');
794 my $form = $main::form;
795 my $locale = $main::locale;
797 $form->{title} = $locale->text('Confirm!');
801 delete $form->{header};
804 <form method=post action=$form->{script}>
807 foreach my $key (keys %$form) {
808 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
809 $form->{$key} =~ s/\"/"/g;
810 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
814 <h2 class=confirm>$form->{title}</h2>
817 . $locale->text('Are you sure you want to delete Transaction')
818 . qq| $form->{invnumber}</h4>
820 <input name=action class=submit type=submit value="|
821 . $locale->text('Yes') . qq|">
825 $main::lxdebug->leave_sub();
829 $main::lxdebug->enter_sub();
831 $main::auth->assert('general_ledger');
833 my $form = $main::form;
834 my %myconfig = %main::myconfig;
835 my $locale = $main::locale;
837 if (AR->delete_transaction(\%myconfig, \%$form)) {
839 if(!exists $form->{addition}) {
840 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
841 $form->{what_done} = "invoice";
842 $form->{addition} = "DELETED";
845 # /saving the history
846 $form->redirect($locale->text('Transaction deleted!'));
848 $form->error($locale->text('Cannot delete transaction!'));
850 $main::lxdebug->leave_sub();
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;
861 my $cgi = $::request->{cgi};
863 # setup customer selection
864 $form->all_vc(\%myconfig, "customer", "AR");
866 $form->{title} = $locale->text('AR Transactions');
868 # Auch in Rechnungsübersicht nach Kundentyp filtern - jan
869 $form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 },
870 "departments" => "ALL_DEPARTMENTS",
871 "customers" => "ALL_VC",
872 "business_types" => "ALL_BUSINESS_TYPES");
873 $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
874 $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
876 $form->{CT_CUSTOM_VARIABLES} = CVar->get_configs('module' => 'CT');
877 ($form->{CT_CUSTOM_VARIABLES_FILTER_CODE},
878 $form->{CT_CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $form->{CT_CUSTOM_VARIABLES},
879 'include_prefix' => 'l_',
880 'include_value' => 'Y');
882 # constants and subs for template
883 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
886 print $form->parse_html_template('ar/search', { %myconfig });
888 $main::lxdebug->leave_sub();
891 sub create_subtotal_row {
892 $main::lxdebug->enter_sub();
894 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
896 my $form = $main::form;
897 my %myconfig = %main::myconfig;
899 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
901 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
903 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
905 map { $totals->{$_} = 0 } @{ $subtotal_columns };
907 $main::lxdebug->leave_sub();
912 sub ar_transactions {
913 $main::lxdebug->enter_sub();
915 $main::auth->assert('invoice_edit');
917 my $form = $main::form;
918 my %myconfig = %main::myconfig;
919 my $locale = $main::locale;
921 my ($callback, $href, @columns);
923 ($form->{customer}, $form->{customer_id}) = split(/--/, $form->{customer});
925 report_generator_set_default_sort('transdate', 1);
927 AR->ar_transactions(\%myconfig, \%$form);
929 $form->{title} = $locale->text('AR Transactions');
931 my $report = SL::ReportGenerator->new(\%myconfig, $form);
934 qw(transdate id type invnumber ordnumber cusordnumber name netamount tax amount paid
935 datepaid due duedate transaction_description notes salesman employee shippingpoint shipvia
936 marge_total marge_percent globalprojectnumber customernumber country ustid taxzone payment_terms charts customertype direct_debit dunning_description);
938 my $ct_cvar_configs = CVar->get_configs('module' => 'CT');
939 my @ct_includeable_custom_variables = grep { $_->{includeable} } @{ $ct_cvar_configs };
940 my @ct_searchable_custom_variables = grep { $_->{searchable} } @{ $ct_cvar_configs };
942 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @ct_includeable_custom_variables;
943 push @columns, map { "cvar_$_->{name}" } @ct_includeable_custom_variables;
945 my @hidden_variables = map { "l_${_}" } @columns;
946 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);
947 push @hidden_variables, map { "cvar_$_->{name}" } @ct_searchable_custom_variables;
949 $href = build_std_url('action=ar_transactions', grep { $form->{$_} } @hidden_variables);
952 'transdate' => { 'text' => $locale->text('Date'), },
953 'id' => { 'text' => $locale->text('ID'), },
954 'type' => { 'text' => $locale->text('Type'), },
955 'invnumber' => { 'text' => $locale->text('Invoice'), },
956 'ordnumber' => { 'text' => $locale->text('Order'), },
957 'cusordnumber' => { 'text' => $locale->text('Customer Order Number'), },
958 'name' => { 'text' => $locale->text('Customer'), },
959 'netamount' => { 'text' => $locale->text('Amount'), },
960 'tax' => { 'text' => $locale->text('Tax'), },
961 'amount' => { 'text' => $locale->text('Total'), },
962 'paid' => { 'text' => $locale->text('Paid'), },
963 'datepaid' => { 'text' => $locale->text('Date Paid'), },
964 'due' => { 'text' => $locale->text('Amount Due'), },
965 'duedate' => { 'text' => $locale->text('Due Date'), },
966 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
967 'notes' => { 'text' => $locale->text('Notes'), },
968 'salesman' => { 'text' => $locale->text('Salesperson'), },
969 'employee' => { 'text' => $locale->text('Employee'), },
970 'shippingpoint' => { 'text' => $locale->text('Shipping Point'), },
971 'shipvia' => { 'text' => $locale->text('Ship via'), },
972 'globalprojectnumber' => { 'text' => $locale->text('Document Project Number'), },
973 'marge_total' => { 'text' => $locale->text('Ertrag'), },
974 'marge_percent' => { 'text' => $locale->text('Ertrag prozentual'), },
975 'customernumber' => { 'text' => $locale->text('Customer Number'), },
976 'country' => { 'text' => $locale->text('Country'), },
977 'ustid' => { 'text' => $locale->text('USt-IdNr.'), },
978 'taxzone' => { 'text' => $locale->text('Steuersatz'), },
979 'payment_terms' => { 'text' => $locale->text('Payment Terms'), },
980 'charts' => { 'text' => $locale->text('Buchungskonto'), },
981 'customertype' => { 'text' => $locale->text('Customer type'), },
982 'direct_debit' => { 'text' => $locale->text('direct debit'), },
983 dunning_description => { 'text' => $locale->text('Dunning level'), },
987 foreach my $name (qw(id transdate duedate invnumber ordnumber cusordnumber name datepaid employee shippingpoint shipvia transaction_description direct_debit)) {
988 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
989 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
992 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
994 $form->{"l_type"} = "Y";
995 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
997 $report->set_columns(%column_defs);
998 $report->set_column_order(@columns);
1000 $report->set_export_options('ar_transactions', @hidden_variables, qw(sort sortdir));
1002 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
1004 CVar->add_custom_variables_to_report('module' => 'CT',
1005 'trans_id_field' => 'customer_id',
1006 'configs' => $ct_cvar_configs,
1007 'column_defs' => \%column_defs,
1008 'data' => $form->{AR});
1011 if ($form->{customer}) {
1012 push @options, $locale->text('Customer') . " : $form->{customer}";
1014 if ($form->{cp_name}) {
1015 push @options, $locale->text('Contact Person') . " : $form->{cp_name}";
1017 if ($form->{department}) {
1018 my ($department) = split /--/, $form->{department};
1019 push @options, $locale->text('Department') . " : $department";
1021 if ($form->{department_id}) {
1022 push @options, $locale->text('Department Id') . " : $form->{department_id}";
1024 if ($form->{invnumber}) {
1025 push @options, $locale->text('Invoice Number') . " : $form->{invnumber}";
1027 if ($form->{ordnumber}) {
1028 push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
1030 if ($form->{cusordnumber}) {
1031 push @options, $locale->text('Customer Order Number') . " : $form->{cusordnumber}";
1033 if ($form->{notes}) {
1034 push @options, $locale->text('Notes') . " : $form->{notes}";
1036 if ($form->{transaction_description}) {
1037 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
1039 if ($form->{transdatefrom}) {
1040 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1042 if ($form->{transdateto}) {
1043 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
1045 if ($form->{open}) {
1046 push @options, $locale->text('Open');
1048 if ($form->{employee_id}) {
1049 my $employee = SL::DB::Employee->new(id => $form->{employee_id})->load;
1050 push @options, $locale->text('Employee') . ' : ' . $employee->name;
1052 if ($form->{salesman_id}) {
1053 my $salesman = SL::DB::Employee->new(id => $form->{salesman_id})->load;
1054 push @options, $locale->text('Salesman') . ' : ' . $salesman->name;
1056 if ($form->{closed}) {
1057 push @options, $locale->text('Closed');
1060 $report->set_options('top_info_text' => join("\n", @options),
1061 'raw_bottom_info_text' => $form->parse_html_template('ar/ar_transactions_bottom'),
1062 'output_format' => 'HTML',
1063 'title' => $form->{title},
1064 'attachment_basename' => $locale->text('invoice_list') . strftime('_%Y%m%d', localtime time),
1066 $report->set_options_from_form();
1067 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1069 # add sort and escape callback, this one we use for the add sub
1070 $form->{callback} = $href .= "&sort=$form->{sort}";
1072 # escape callback for href
1073 $callback = $form->escape($href);
1075 my @subtotal_columns = qw(netamount amount paid due marge_total marge_percent);
1077 my %totals = map { $_ => 0 } @subtotal_columns;
1078 my %subtotals = map { $_ => 0 } @subtotal_columns;
1082 foreach my $ar (@{ $form->{AR} }) {
1083 $ar->{tax} = $ar->{amount} - $ar->{netamount};
1084 $ar->{due} = $ar->{amount} - $ar->{paid};
1086 map { $subtotals{$_} += $ar->{$_};
1087 $totals{$_} += $ar->{$_} } @subtotal_columns;
1089 $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
1090 $totals{marge_percent} = $totals{netamount} ? ($totals{marge_total} * 100 / $totals{netamount} ) : 0;
1092 map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(netamount tax amount paid due marge_total marge_percent);
1094 my $is_storno = $ar->{storno} && $ar->{storno_id};
1095 my $has_storno = $ar->{storno} && !$ar->{storno_id};
1098 $has_storno ? $locale->text("Invoice with Storno (abbreviation)") :
1099 $is_storno ? $locale->text("Storno (one letter abbreviation)") :
1100 $ar->{amount} < 0 ? $locale->text("Credit note (one letter abbreviation)") :
1101 $ar->{invoice} ? $locale->text("Invoice (one letter abbreviation)") :
1102 $locale->text("AR Transaction (abbreviation)");
1104 $ar->{direct_debit} = $ar->{direct_debit} ? $::locale->text('yes') : $::locale->text('no');
1108 foreach my $column (@columns) {
1110 'data' => $ar->{$column},
1111 'align' => $column_alignment{$column},
1115 $row->{invnumber}->{link} = build_std_url("script=" . ($ar->{invoice} ? 'is.pl' : 'ar.pl'), 'action=edit')
1116 . "&id=" . E($ar->{id}) . "&callback=${callback}";
1118 my $row_set = [ $row ];
1120 if (($form->{l_subtotal} eq 'Y')
1121 && (($idx == (scalar @{ $form->{AR} } - 1))
1122 || ($ar->{ $form->{sort} } ne $form->{AR}->[$idx + 1]->{ $form->{sort} }))) {
1123 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1126 $report->add_data($row_set);
1131 $report->add_separator();
1132 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1134 $report->generate_with_headers();
1136 $main::lxdebug->leave_sub();
1140 $main::lxdebug->enter_sub();
1142 $main::auth->assert('general_ledger');
1144 my $form = $main::form;
1145 my %myconfig = %main::myconfig;
1146 my $locale = $main::locale;
1148 # don't cancel cancelled transactions
1149 if (IS->has_storno(\%myconfig, $form, 'ar')) {
1150 $form->{title} = $locale->text("Cancel Accounts Receivables Transaction");
1151 $form->error($locale->text("Transaction has already been cancelled!"));
1154 AR->storno($form, \%myconfig, $form->{id});
1156 # saving the history
1157 if(!exists $form->{addition} && $form->{id} ne "") {
1158 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1159 $form->{addition} = "STORNO";
1160 $form->{what_done} = "invoice";
1161 $form->save_history;
1163 # /saving the history
1165 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1167 $main::lxdebug->leave_sub();