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);
42 use SL::ReportGenerator;
44 require "bin/mozilla/arap.pl";
45 require "bin/mozilla/common.pl";
46 require "bin/mozilla/drafts.pl";
47 require "bin/mozilla/reportgenerator.pl";
52 # this is for our long dates
53 # $locale->text('January')
54 # $locale->text('February')
55 # $locale->text('March')
56 # $locale->text('April')
57 # $locale->text('May ')
58 # $locale->text('June')
59 # $locale->text('July')
60 # $locale->text('August')
61 # $locale->text('September')
62 # $locale->text('October')
63 # $locale->text('November')
64 # $locale->text('December')
66 # this is for our short month
67 # $locale->text('Jan')
68 # $locale->text('Feb')
69 # $locale->text('Mar')
70 # $locale->text('Apr')
71 # $locale->text('May')
72 # $locale->text('Jun')
73 # $locale->text('Jul')
74 # $locale->text('Aug')
75 # $locale->text('Sep')
76 # $locale->text('Oct')
77 # $locale->text('Nov')
78 # $locale->text('Dec')
81 $main::lxdebug->enter_sub();
83 $main::auth->assert('general_ledger');
85 my $form = $main::form;
86 my %myconfig = %main::myconfig;
88 return $main::lxdebug->leave_sub() if (load_draft_maybe());
91 if(!exists $form->{addition} && ($form->{id} ne "")) {
92 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
93 $form->{addition} = "ADDED";
98 $form->{title} = "Add";
99 $form->{callback} = "ar.pl?action=add&DONT_LOAD_DRAFT=1" unless $form->{callback};
101 AR->get_transdate(\%myconfig, $form);
102 $form->{initial_transdate} = $form->{transdate};
103 create_links(dont_save => 1);
104 $form->{transdate} = $form->{initial_transdate};
106 $main::lxdebug->leave_sub();
110 $main::lxdebug->enter_sub();
112 $main::auth->assert('general_ledger');
114 my $form = $main::form;
116 # show history button
117 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
118 #/show hhistory button
119 $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
120 $form->{title} = "Edit";
125 $main::lxdebug->leave_sub();
129 $main::lxdebug->enter_sub();
131 $main::auth->assert('general_ledger');
133 my $form = $main::form;
138 $main::lxdebug->leave_sub();
142 $main::lxdebug->enter_sub();
144 $main::auth->assert('general_ledger');
147 my $form = $main::form;
148 my %myconfig = %main::myconfig;
150 $form->create_links("AR", \%myconfig, "customer");
153 if (!$params{dont_save}) {
154 %saved = map { ($_ => $form->{$_}) } qw(direct_debit id taxincluded);
155 $saved{duedate} = $form->{duedate} if $form->{duedate};
158 IS->get_customer(\%myconfig, \%$form);
160 $form->{$_} = $saved{$_} for keys %saved;
161 $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
162 $form->{rowcount} = 1;
165 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
167 map { $form->{selectcurrency} .= "<option>$_\n" } $form->get_all_currencies(\%myconfig);
170 if (@{ $form->{all_customer} || [] }) {
171 $form->{customer} = "$form->{customer}--$form->{customer_id}";
172 map { $form->{selectcustomer} .= "<option>$_->{name}--$_->{id}\n" }
173 (@{ $form->{all_customer} });
177 if (@{ $form->{all_departments} || [] }) {
178 $form->{selectdepartment} = "<option>\n";
179 $form->{department} = "$form->{department}--$form->{department_id}";
182 $form->{selectdepartment} .=
183 "<option>$_->{description}--$_->{id}\n"
184 } (@{ $form->{all_departments} || [] });
187 $form->{employee} = "$form->{employee}--$form->{employee_id}";
190 if (@{ $form->{all_employees} || [] }) {
191 $form->{selectemployee} = "";
192 map { $form->{selectemployee} .= "<option>$_->{name}--$_->{id}\n" }
193 (@{ $form->{all_employees} || [] });
196 # build the popup menus
197 $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
199 AR->setup_form($form);
202 ($form->datetonum($form->{transdate}, \%myconfig) <=
203 $form->datetonum($form->{closedto}, \%myconfig));
205 $main::lxdebug->leave_sub();
209 $main::lxdebug->enter_sub();
211 $main::auth->assert('general_ledger');
213 my $form = $main::form;
214 my %myconfig = %main::myconfig;
215 my $locale = $main::locale;
216 my $cgi = $::request->{cgi};
218 my ($title, $readonly, $exchangerate, $rows);
219 my ($notes, $department, $customer, $employee, $amount, $project);
223 $title = $form->{title};
224 # $locale->text('Add Accounts Receivables Transaction')
225 # $locale->text('Edit Accounts Receivables Transaction')
226 $form->{title} = $locale->text("$title Accounts Receivables Transaction");
228 $form->{javascript} = qq|<script type="text/javascript">
230 function setTaxkey(accno, row) {
231 var taxkey = accno.options[accno.selectedIndex].value;
232 var reg = /--([0-9]*)/;
233 var found = reg.exec(taxkey);
234 var index = found[1];
235 index = parseInt(index);
236 var tax = 'taxchart_' + row;
237 for (var i = 0; i < document.getElementById(tax).options.length; ++i) {
238 var reg2 = new RegExp("^"+ index, "");
239 if (reg2.exec(document.getElementById(tax).options[i].value)) {
240 document.getElementById(tax).options[i].selected = true;
247 # show history button js
248 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
249 #/show history button js
250 $readonly = ($form->{id}) ? "readonly" : "";
252 $form->{radier} = ($::instance_conf->get_ar_changeable == 2)
253 ? ($form->current_date(\%myconfig) eq $form->{gldate})
254 : ($::instance_conf->get_ar_changeable == 1);
255 $readonly = ($form->{radier}) ? "" : $readonly;
257 # set option selected
258 foreach my $item (qw(customer currency department employee)) {
259 $form->{"select$item"} =~ s/ selected//;
260 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
263 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'buy');
264 $form->{exchangerate} = $form->{forex} if $form->{forex};
266 $rows = max 2, $form->numtextrows($form->{notes}, 50);
268 my @old_project_ids = grep { $_ } map { $form->{"project_id_$_"} } 1..$form->{rowcount};
270 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
272 "old_id" => \@old_project_ids },
273 "charts" => { "key" => "ALL_CHARTS",
274 "transdate" => $form->{transdate} },
275 "taxcharts" => { "key" => "ALL_TAXCHARTS",
276 "module" => "AR" },);
278 $_->{link_split} = { map { $_ => 1 } split/:/, $_->{link} } for @{ $form->{ALL_CHARTS} };
280 my %project_labels = map { $_->{id} => $_->{projectnumber} } @{ $form->{"ALL_PROJECTS"} };
282 my (@AR_amount_values);
284 my (@AR_paid_values);
289 foreach my $item (@{ $form->{ALL_CHARTS} }) {
290 if ($item->{link_split}{AR_amount}) {
291 $taxchart_init = $item->{tax_id} if ($taxchart_init eq "");
292 my $key = "$item->{accno}--$item->{tax_id}";
293 push(@AR_amount_values, $key);
294 } elsif ($item->{link_split}{AR}) {
295 push(@AR_values, $item->{accno});
296 } elsif ($item->{link_split}{AR_paid}) {
297 push(@AR_paid_values, $item->{accno});
300 # weirdness for AR_amount
301 $chart_labels{$item->{accno}} = "$item->{accno}--$item->{description}";
302 $chart_labels{"$item->{accno}--$item->{tax_id}"} = "$item->{accno}--$item->{description}";
304 $charts{$item->{accno}} = $item;
307 my %taxchart_labels = ();
308 my @taxchart_values = ();
310 foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
311 my $key = "$item->{id}--$item->{rate}";
312 $taxchart_init = $key if ($taxchart_init eq $item->{id});
313 push(@taxchart_values, $key);
314 $taxchart_labels{$key} = "$item->{taxdescription} " . ($item->{rate} * 100) . ' %';
315 $taxcharts{$item->{id}} = $item;
318 my $follow_up_vc = $form->{customer};
319 $follow_up_vc =~ s/--.*?//;
320 my $follow_up_trans_info = "$form->{invnumber} ($follow_up_vc)";
322 $form->{javascript} .=
323 qq|<script type="text/javascript" src="js/show_vc_details.js"></script>| .
324 qq|<script type="text/javascript" src="js/follow_up.js"></script>|;
326 # $amount = $locale->text('Amount');
327 # $project = $locale->text('Project');
330 for my $i (1 .. $form->{rowcount}) {
332 amount => $form->{"amount_$i"},
333 tax => $form->{"tax_$i"},
334 project_id => ($i==$form->{rowcount}) ? $form->{globalproject_id} : $form->{"project_id_$i"},
337 my $selected_accno_full;
338 my ($accno_row) = split(/--/, $form->{"AR_amount_$i"});
339 my $item = $charts{$accno_row};
340 $selected_accno_full = "$item->{accno}--$item->{tax_id}";
342 my $selected_taxchart = $form->{"taxchart_$i"};
343 my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full);
344 my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_AR_amount_$i"});
346 if ($previous_accno &&
347 ($previous_accno eq $selected_accno) &&
348 ($previous_tax_id ne $selected_tax_id)) {
349 my $item = $taxcharts{$selected_tax_id};
350 $selected_taxchart = "$item->{id}--$item->{rate}";
353 if (!$form->{"taxchart_$i"}) {
354 if ($form->{"AR_amount_$i"} =~ m/.--./) {
355 $selected_taxchart = join '--', map { ($_->{id}, $_->{rate}) } first { $_->{id} == $item->{tax_id} } @{ $form->{ALL_TAXCHARTS} };
357 $selected_taxchart = $taxchart_init;
361 $transaction->{selectAR_amount} =
362 NTI($cgi->popup_menu('-name' => "AR_amount_$i",
363 '-id' => "AR_amount_$i",
364 '-style' => 'width:400px',
365 '-onChange' => "setTaxkey(this, $i)",
366 '-values' => \@AR_amount_values,
367 '-labels' => \%chart_labels,
368 '-default' => $selected_accno_full))
369 . $cgi->hidden('-name' => "previous_AR_amount_$i",
370 '-default' => $selected_accno_full);
372 $transaction->{taxchart} =
373 NTI($cgi->popup_menu('-name' => "taxchart_$i",
374 '-id' => "taxchart_$i",
375 '-style' => 'width:200px',
376 '-values' => \@taxchart_values,
377 '-labels' => \%taxchart_labels,
378 '-default' => $selected_taxchart));
380 push @transactions, $transaction;
383 $form->{invtotal_unformatted} = $form->{invtotal};
386 NTI($cgi->popup_menu('-name' => "ARselected", '-id' => "ARselected",
387 '-style' => 'width:400px',
388 '-values' => \@AR_values, '-labels' => \%chart_labels,
389 '-default' => $form->{ARselected}));
392 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
394 my $now = $form->current_date(\%myconfig);
397 for my $i (1 .. $form->{paidaccounts}) {
399 paid => $form->{"paid_$i"},
400 exchangerate => $form->{"exchangerate_$i"} || '',
401 gldate => $form->{"gldate_$i"},
402 acc_trans_id => $form->{"acc_trans_id_$i"},
403 source => $form->{"source_$i"},
404 memo => $form->{"memo_$i"},
405 AR_paid => $form->{"AR_paid_$i"},
406 forex => $form->{"forex_$i"},
407 datepaid => $form->{"datepaid_$i"},
408 paid_project_id => $form->{"paid_project_id_$i"},
409 gldate => $form->{"gldate_$i"},
412 # default account for current assets (i.e. 1801 - SKR04)
413 $form->{accno_arap} = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
415 $payment->{selectAR_paid} =
416 NTI($cgi->popup_menu('-name' => "AR_paid_$i",
417 '-id' => "AR_paid_$i",
418 '-values' => \@AR_paid_values,
419 '-labels' => \%chart_labels,
420 '-default' => $form->{accno_arap}));
424 $payment->{changeable} =
425 SL::DB::Default->get->payments_changeable == 0 ? !$payment->{acc_trans_id} # never
426 : SL::DB::Default->get->payments_changeable == 2 ? $payment->{gldate} eq '' || $payment->{gldate} eq $now
429 push @payments, $payment;
432 $form->{totalpaid} = sum map { $_->{paid} } @payments;
435 print $::form->parse_html_template('ar/form_header', {
436 paid_missing => $::form->{invtotal} - $::form->{totalpaid},
437 show_exch => ($::form->{defaultcurrency} && ($::form->{currency} ne $::form->{defaultcurrency})),
438 payments => \@payments,
439 transactions => \@transactions,
440 project_labels => \%project_labels,
442 ARselected => $ARselected,
444 follow_up_trans_info => $follow_up_trans_info,
447 $main::lxdebug->leave_sub();
451 $main::lxdebug->enter_sub();
453 $main::auth->assert('general_ledger');
455 my $form = $main::form;
456 my %myconfig = %main::myconfig;
457 my $locale = $main::locale;
458 my $cgi = $::request->{cgi};
461 my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
462 if ( @{ $follow_ups} ) {
463 $form->{follow_up_length} = scalar(@{$follow_ups});
464 $form->{follow_up_due_length} = sum(map({ $_->{due} * 1 } @{ $follow_ups }));
468 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
469 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
471 $form->{is_closed} = $transdate <= $closedto;
473 # ToDO: - insert a global check for stornos, so that a storno is only possible a limited time after saving it
474 $form->{show_storno_button} =
476 !IS->has_storno(\%myconfig, $form, 'ar') &&
477 !IS->is_storno(\%myconfig, $form, 'ar') &&
478 ($form->{totalpaid} == 0 || $form->{totalpaid} eq "");
480 $form->{show_mark_as_paid_button} = $form->{id} && $::instance_conf->get_ar_show_mark_as_paid();
482 print $::form->parse_html_template('ar/form_footer');
484 $main::lxdebug->leave_sub();
488 $main::lxdebug->enter_sub();
490 $main::auth->assert('general_ledger');
492 my $form = $main::form;
493 my %myconfig = %main::myconfig;
495 &mark_as_paid_common(\%myconfig,"ar");
497 $main::lxdebug->leave_sub();
501 $main::lxdebug->enter_sub();
503 $main::auth->assert('general_ledger');
505 my $form = $main::form;
506 my %myconfig = %main::myconfig;
510 my ($totaltax, $exchangerate);
512 $form->{invtotal} = 0;
514 delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
516 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
517 qw(exchangerate creditlimit creditremaining);
519 my @flds = qw(amount AR_amount projectnumber oldprojectnumber project_id);
523 for my $i (1 .. $form->{rowcount}) {
524 $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
525 $form->{"tax_$i"} = $form->parse_amount(\%myconfig, $form->{"tax_$i"});
526 if ($form->{"amount_$i"}) {
529 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
531 if ($form->{taxincluded}) {
532 $form->{"tax_$i"} = $form->{"amount_$i"} / ($rate + 1) * $rate;
534 $form->{"tax_$i"} = $form->{"amount_$i"} * $rate;
537 $form->{"tax_$i"} = 0;
539 $form->{"tax_$i"} = $form->round_amount($form->{"tax_$i"}, 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 $form->redirect($locale->text('Payment posted!')) if (AR->post_payment(\%myconfig, \%$form));
632 $form->error($locale->text('Cannot post payment!'));
634 $main::lxdebug->leave_sub();
639 $main::auth->assert('general_ledger');
641 my $form = $main::form;
648 $main::lxdebug->enter_sub();
650 $main::auth->assert('general_ledger');
652 my $form = $main::form;
653 my %myconfig = %main::myconfig;
654 my $locale = $main::locale;
660 # check if there is an invoice number, invoice and due date
661 $form->isblank("transdate", $locale->text('Invoice Date missing!'));
662 $form->isblank("duedate", $locale->text('Due Date missing!'));
663 $form->isblank("customer", $locale->text('Customer missing!'));
665 if ($myconfig{mandatory_departments} && !$form->{department}) {
666 $form->{saved_message} = $::locale->text('You have to specify a department.');
671 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
672 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
674 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
675 if ($form->date_max_future($transdate, \%myconfig));
676 $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
678 $form->error($locale->text('Zero amount posting!'))
679 unless grep $_*1, map $form->parse_amount(\%myconfig, $form->{"amount_$_"}), 1..$form->{rowcount};
681 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
682 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
686 for my $i (1 .. $form->{paidaccounts}) {
687 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
688 $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
690 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
692 $form->error($locale->text('Cannot post payment for a closed period!'))
693 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
695 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
696 $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
697 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
702 # if oldcustomer ne customer redo form
703 my ($customer) = split /--/, $form->{customer};
704 if ($form->{oldcustomer} ne "$customer--$form->{customer_id}") {
709 $form->{AR}{receivables} = $form->{ARselected};
712 $form->{id} = 0 if $form->{postasnew};
713 $form->error($locale->text('Cannot post transaction!')) unless AR->post_transaction(\%myconfig, \%$form);
716 if(!exists $form->{addition} && $form->{id} ne "") {
717 $form->{snumbers} = "invnumber_$form->{invnumber}";
718 $form->{addition} = "POSTED";
721 # /saving the history
722 remove_draft() if $form->{remove_draft};
724 $form->redirect($locale->text('Transaction posted!')) unless $inline;
726 $main::lxdebug->leave_sub();
730 $main::lxdebug->enter_sub();
732 $main::auth->assert('general_ledger');
734 my $form = $main::form;
735 my %myconfig = %main::myconfig;
737 $form->{postasnew} = 1;
739 if(!exists $form->{addition} && $form->{id} ne "") {
740 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
741 $form->{addition} = "POSTED AS NEW";
744 # /saving the history
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 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);
759 $form->{paidaccounts} = 1;
761 $form->{invdate} = $form->current_date(\%myconfig);
764 $main::lxdebug->leave_sub();
768 $main::lxdebug->enter_sub();
770 $main::auth->assert('general_ledger');
772 my $form = $main::form;
773 my $locale = $main::locale;
775 $form->{title} = $locale->text('Confirm!');
779 delete $form->{header};
782 <form method=post action=$form->{script}>
785 foreach my $key (keys %$form) {
786 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
787 $form->{$key} =~ s/\"/"/g;
788 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
792 <h2 class=confirm>$form->{title}</h2>
795 . $locale->text('Are you sure you want to delete Transaction')
796 . qq| $form->{invnumber}</h4>
798 <input name=action class=submit type=submit value="|
799 . $locale->text('Yes') . qq|">
803 $main::lxdebug->leave_sub();
807 $main::lxdebug->enter_sub();
809 $main::auth->assert('general_ledger');
811 my $form = $main::form;
812 my %myconfig = %main::myconfig;
813 my $locale = $main::locale;
815 if (AR->delete_transaction(\%myconfig, \%$form)) {
817 if(!exists $form->{addition}) {
818 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
819 $form->{addition} = "DELETED";
822 # /saving the history
823 $form->redirect($locale->text('Transaction deleted!'));
825 $form->error($locale->text('Cannot delete transaction!'));
827 $main::lxdebug->leave_sub();
831 $main::lxdebug->enter_sub();
833 $main::auth->assert('invoice_edit');
835 my $form = $main::form;
836 my %myconfig = %main::myconfig;
837 my $locale = $main::locale;
838 my $cgi = $::request->{cgi};
840 # setup customer selection
841 $form->all_vc(\%myconfig, "customer", "AR");
843 $form->{title} = $locale->text('AR Transactions');
845 # Auch in Rechnungsübersicht nach Kundentyp filtern - jan
846 $form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 },
847 "departments" => "ALL_DEPARTMENTS",
848 "customers" => "ALL_VC",
849 "business_types" => "ALL_BUSINESS_TYPES");
850 $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
851 $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
853 # constants and subs for template
854 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
857 print $form->parse_html_template('ar/search', { %myconfig });
859 $main::lxdebug->leave_sub();
862 sub create_subtotal_row {
863 $main::lxdebug->enter_sub();
865 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
867 my $form = $main::form;
868 my %myconfig = %main::myconfig;
870 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
872 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
874 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
876 map { $totals->{$_} = 0 } @{ $subtotal_columns };
878 $main::lxdebug->leave_sub();
883 sub ar_transactions {
884 $main::lxdebug->enter_sub();
886 $main::auth->assert('invoice_edit');
888 my $form = $main::form;
889 my %myconfig = %main::myconfig;
890 my $locale = $main::locale;
892 my ($callback, $href, @columns);
894 ($form->{customer}, $form->{customer_id}) = split(/--/, $form->{customer});
896 report_generator_set_default_sort('transdate', 1);
898 AR->ar_transactions(\%myconfig, \%$form);
900 $form->{title} = $locale->text('AR Transactions');
902 my $report = SL::ReportGenerator->new(\%myconfig, $form);
905 qw(transdate id type invnumber ordnumber name netamount tax amount paid
906 datepaid due duedate transaction_description notes salesman employee shippingpoint shipvia
907 marge_total marge_percent globalprojectnumber customernumber country ustid taxzone payment_terms charts customertype);
909 my @hidden_variables = map { "l_${_}" } @columns;
910 push @hidden_variables, "l_subtotal", qw(open closed customer invnumber ordnumber transaction_description notes project_id transdatefrom transdateto employee_id salesman_id business_id);
912 $href = build_std_url('action=ar_transactions', grep { $form->{$_} } @hidden_variables);
915 'transdate' => { 'text' => $locale->text('Date'), },
916 'id' => { 'text' => $locale->text('ID'), },
917 'type' => { 'text' => $locale->text('Type'), },
918 'invnumber' => { 'text' => $locale->text('Invoice'), },
919 'ordnumber' => { 'text' => $locale->text('Order'), },
920 'name' => { 'text' => $locale->text('Customer'), },
921 'netamount' => { 'text' => $locale->text('Amount'), },
922 'tax' => { 'text' => $locale->text('Tax'), },
923 'amount' => { 'text' => $locale->text('Total'), },
924 'paid' => { 'text' => $locale->text('Paid'), },
925 'datepaid' => { 'text' => $locale->text('Date Paid'), },
926 'due' => { 'text' => $locale->text('Amount Due'), },
927 'duedate' => { 'text' => $locale->text('Due Date'), },
928 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
929 'notes' => { 'text' => $locale->text('Notes'), },
930 'salesman' => { 'text' => $locale->text('Salesperson'), },
931 'employee' => { 'text' => $locale->text('Employee'), },
932 'shippingpoint' => { 'text' => $locale->text('Shipping Point'), },
933 'shipvia' => { 'text' => $locale->text('Ship via'), },
934 'globalprojectnumber' => { 'text' => $locale->text('Document Project Number'), },
935 'marge_total' => { 'text' => $locale->text('Ertrag'), },
936 'marge_percent' => { 'text' => $locale->text('Ertrag prozentual'), },
937 'customernumber' => { 'text' => $locale->text('Customer Number'), },
938 'country' => { 'text' => $locale->text('Country'), },
939 'ustid' => { 'text' => $locale->text('USt-IdNr.'), },
940 'taxzone' => { 'text' => $locale->text('Steuersatz'), },
941 'payment_terms' => { 'text' => $locale->text('Payment Terms'), },
942 'charts' => { 'text' => $locale->text('Buchungskonto'), },
943 'customertype' => { 'text' => $locale->text('Customer type'), },
946 foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid employee shippingpoint shipvia transaction_description)) {
947 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
948 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
951 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
953 $form->{"l_type"} = "Y";
954 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
956 $report->set_columns(%column_defs);
957 $report->set_column_order(@columns);
959 $report->set_export_options('ar_transactions', @hidden_variables, qw(sort sortdir));
961 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
964 if ($form->{customer}) {
965 push @options, $locale->text('Customer') . " : $form->{customer}";
967 if ($form->{department}) {
968 my ($department) = split /--/, $form->{department};
969 push @options, $locale->text('Department') . " : $department";
971 if ($form->{department_id}) {
972 push @options, $locale->text('Department Id') . " : $form->{department_id}";
974 if ($form->{invnumber}) {
975 push @options, $locale->text('Invoice Number') . " : $form->{invnumber}";
977 if ($form->{ordnumber}) {
978 push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
980 if ($form->{notes}) {
981 push @options, $locale->text('Notes') . " : $form->{notes}";
983 if ($form->{transaction_description}) {
984 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
986 if ($form->{transdatefrom}) {
987 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
989 if ($form->{transdateto}) {
990 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
993 push @options, $locale->text('Open');
995 if ($form->{employee_id}) {
996 my $employee = SL::DB::Employee->new(id => $form->{employee_id})->load;
997 push @options, $locale->text('Employee') . ' : ' . $employee->name;
999 if ($form->{salesman_id}) {
1000 my $salesman = SL::DB::Employee->new(id => $form->{salesman_id})->load;
1001 push @options, $locale->text('Salesman') . ' : ' . $salesman->name;
1003 if ($form->{closed}) {
1004 push @options, $locale->text('Closed');
1007 $report->set_options('top_info_text' => join("\n", @options),
1008 'raw_bottom_info_text' => $form->parse_html_template('ar/ar_transactions_bottom'),
1009 'output_format' => 'HTML',
1010 'title' => $form->{title},
1011 'attachment_basename' => $locale->text('invoice_list') . strftime('_%Y%m%d', localtime time),
1013 $report->set_options_from_form();
1014 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1016 # add sort and escape callback, this one we use for the add sub
1017 $form->{callback} = $href .= "&sort=$form->{sort}";
1019 # escape callback for href
1020 $callback = $form->escape($href);
1022 my @subtotal_columns = qw(netamount amount paid due marge_total marge_percent);
1024 my %totals = map { $_ => 0 } @subtotal_columns;
1025 my %subtotals = map { $_ => 0 } @subtotal_columns;
1029 foreach my $ar (@{ $form->{AR} }) {
1030 $ar->{tax} = $ar->{amount} - $ar->{netamount};
1031 $ar->{due} = $ar->{amount} - $ar->{paid};
1033 map { $subtotals{$_} += $ar->{$_};
1034 $totals{$_} += $ar->{$_} } @subtotal_columns;
1036 $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
1037 $totals{marge_percent} = $totals{netamount} ? ($totals{marge_total} * 100 / $totals{netamount} ) : 0;
1039 map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(netamount tax amount paid due marge_total marge_percent);
1041 my $is_storno = $ar->{storno} && $ar->{storno_id};
1042 my $has_storno = $ar->{storno} && !$ar->{storno_id};
1045 $has_storno ? $locale->text("Invoice with Storno (abbreviation)") :
1046 $is_storno ? $locale->text("Storno (one letter abbreviation)") :
1047 $ar->{amount} < 0 ? $locale->text("Credit note (one letter abbreviation)") :
1048 $ar->{invoice} ? $locale->text("Invoice (one letter abbreviation)") :
1049 $locale->text("AR Transaction (abbreviation)");
1053 foreach my $column (@columns) {
1055 'data' => $ar->{$column},
1056 'align' => $column_alignment{$column},
1060 $row->{invnumber}->{link} = build_std_url("script=" . ($ar->{invoice} ? 'is.pl' : 'ar.pl'), 'action=edit')
1061 . "&id=" . E($ar->{id}) . "&callback=${callback}";
1063 my $row_set = [ $row ];
1065 if (($form->{l_subtotal} eq 'Y')
1066 && (($idx == (scalar @{ $form->{AR} } - 1))
1067 || ($ar->{ $form->{sort} } ne $form->{AR}->[$idx + 1]->{ $form->{sort} }))) {
1068 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1071 $report->add_data($row_set);
1076 $report->add_separator();
1077 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1079 $report->generate_with_headers();
1081 $main::lxdebug->leave_sub();
1085 $main::lxdebug->enter_sub();
1087 $main::auth->assert('general_ledger');
1089 my $form = $main::form;
1090 my %myconfig = %main::myconfig;
1091 my $locale = $main::locale;
1093 # don't cancel cancelled transactions
1094 if (IS->has_storno(\%myconfig, $form, 'ar')) {
1095 $form->{title} = $locale->text("Cancel Accounts Receivables Transaction");
1096 $form->error($locale->text("Transaction has already been cancelled!"));
1099 AR->storno($form, \%myconfig, $form->{id});
1101 # saving the history
1102 if(!exists $form->{addition} && $form->{id} ne "") {
1103 $form->{snumbers} = "ordnumber_$form->{ordnumber}";
1104 $form->{addition} = "STORNO";
1105 $form->save_history;
1107 # /saving the history
1109 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1111 $main::lxdebug->leave_sub();