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 $payment->{selectAR_paid} =
413 NTI($cgi->popup_menu('-name' => "AR_paid_$i",
414 '-id' => "AR_paid_$i",
415 '-values' => \@AR_paid_values,
416 '-labels' => \%chart_labels,
417 '-default' => $payment->{AR_paid}));
421 $payment->{changeable} =
422 SL::DB::Default->get->payments_changeable == 0 ? !$payment->{acc_trans_id} # never
423 : SL::DB::Default->get->payments_changeable == 2 ? $payment->{gldate} eq '' || $payment->{gldate} eq $now
426 push @payments, $payment;
429 $form->{totalpaid} = sum map { $_->{paid} } @payments;
432 print $::form->parse_html_template('ar/form_header', {
433 paid_missing => $::form->{invtotal} - $::form->{totalpaid},
434 show_exch => ($::form->{defaultcurrency} && ($::form->{currency} ne $::form->{defaultcurrency})),
435 payments => \@payments,
436 transactions => \@transactions,
437 project_labels => \%project_labels,
439 ARselected => $ARselected,
441 follow_up_trans_info => $follow_up_trans_info,
444 $main::lxdebug->leave_sub();
448 $main::lxdebug->enter_sub();
450 $main::auth->assert('general_ledger');
452 my $form = $main::form;
453 my %myconfig = %main::myconfig;
454 my $locale = $main::locale;
455 my $cgi = $::request->{cgi};
458 my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
459 if ( @{ $follow_ups} ) {
460 $form->{follow_up_length} = scalar(@{$follow_ups});
461 $form->{follow_up_due_length} = sum(map({ $_->{due} * 1 } @{ $follow_ups }));
465 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
466 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
468 $form->{is_closed} = $transdate <= $closedto;
470 # ToDO: - insert a global check for stornos, so that a storno is only possible a limited time after saving it
471 $form->{show_storno_button} =
473 !IS->has_storno(\%myconfig, $form, 'ar') &&
474 !IS->is_storno(\%myconfig, $form, 'ar') &&
475 ($form->{totalpaid} == 0 || $form->{totalpaid} eq "");
477 $form->{show_mark_as_paid_button} = $form->{id} && $::instance_conf->get_ar_show_mark_as_paid();
479 print $::form->parse_html_template('ar/form_footer');
481 $main::lxdebug->leave_sub();
485 $main::lxdebug->enter_sub();
487 $main::auth->assert('general_ledger');
489 my $form = $main::form;
490 my %myconfig = %main::myconfig;
492 &mark_as_paid_common(\%myconfig,"ar");
494 $main::lxdebug->leave_sub();
498 $main::lxdebug->enter_sub();
500 $main::auth->assert('general_ledger');
502 my $form = $main::form;
503 my %myconfig = %main::myconfig;
507 my ($totaltax, $exchangerate);
509 $form->{invtotal} = 0;
511 delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
513 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
514 qw(exchangerate creditlimit creditremaining);
516 my @flds = qw(amount AR_amount projectnumber oldprojectnumber project_id);
520 for my $i (1 .. $form->{rowcount}) {
521 $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
522 $form->{"tax_$i"} = $form->parse_amount(\%myconfig, $form->{"tax_$i"});
523 if ($form->{"amount_$i"}) {
526 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
528 if ($form->{taxincluded}) {
529 $form->{"tax_$i"} = $form->{"amount_$i"} / ($rate + 1) * $rate;
531 $form->{"tax_$i"} = $form->{"amount_$i"} * $rate;
534 $form->{"tax_$i"} = 0;
536 $form->{"tax_$i"} = $form->round_amount($form->{"tax_$i"}, 2);
538 $totaltax += $form->{"tax_$i"};
539 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
544 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
545 $form->{rowcount} = $count + 1;
546 map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
548 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'buy');
549 $form->{exchangerate} = $form->{forex} if $form->{forex};
551 $form->{invdate} = $form->{transdate};
553 $form->{invdate} = $form->{transdate};
555 my %saved_variables = map +( $_ => $form->{$_} ), qw(AR AR_amount_1 taxchart_1 customer_id notes);
557 &check_name("customer");
559 $form->{AR} = $saved_variables{AR};
560 if ($saved_variables{AR_amount_1} =~ m/.--./) {
561 map { $form->{$_} = $saved_variables{$_} } qw(AR_amount_1 taxchart_1);
563 delete $form->{taxchart_1};
567 ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
569 for my $i (1 .. $form->{paidaccounts}) {
570 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
573 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
574 } qw(paid exchangerate);
576 $form->{totalpaid} += $form->{"paid_$i"};
578 $form->{"forex_$i"} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
579 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
583 $form->{creditremaining} -=
584 ($form->{invtotal} - $form->{totalpaid} + $form->{oldtotalpaid} -
585 $form->{oldinvtotal});
586 $form->{oldinvtotal} = $form->{invtotal};
587 $form->{oldtotalpaid} = $form->{totalpaid};
591 $main::lxdebug->leave_sub();
595 # ToDO: fix $closedto and $invdate
598 $main::lxdebug->enter_sub();
600 $main::auth->assert('general_ledger');
602 my $form = $main::form;
603 my %myconfig = %main::myconfig;
604 my $locale = $main::locale;
606 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
608 my $invdate = $form->datetonum($form->{transdate}, \%myconfig);
610 for my $i (1 .. $form->{paidaccounts}) {
612 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
613 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
615 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
617 $form->error($locale->text('Cannot post payment for a closed period!')) if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
619 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
620 # $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid);
621 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
626 ($form->{AR}) = split /--/, $form->{AR};
627 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
628 $form->redirect($locale->text('Payment posted!')) if (AR->post_payment(\%myconfig, \%$form));
629 $form->error($locale->text('Cannot post payment!'));
631 $main::lxdebug->leave_sub();
636 $main::auth->assert('general_ledger');
638 my $form = $main::form;
645 $main::lxdebug->enter_sub();
647 $main::auth->assert('general_ledger');
649 my $form = $main::form;
650 my %myconfig = %main::myconfig;
651 my $locale = $main::locale;
657 # check if there is an invoice number, invoice and due date
658 $form->isblank("transdate", $locale->text('Invoice Date missing!'));
659 $form->isblank("duedate", $locale->text('Due Date missing!'));
660 $form->isblank("customer", $locale->text('Customer missing!'));
662 if ($myconfig{mandatory_departments} && !$form->{department}) {
663 $form->{saved_message} = $::locale->text('You have to specify a department.');
668 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
669 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
671 $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
672 if ($form->date_max_future($transdate, \%myconfig));
673 $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
675 $form->error($locale->text('Zero amount posting!'))
676 unless grep $_*1, map $form->parse_amount(\%myconfig, $form->{"amount_$_"}), 1..$form->{rowcount};
678 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
679 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
683 for my $i (1 .. $form->{paidaccounts}) {
684 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
685 $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
687 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
689 $form->error($locale->text('Cannot post payment for a closed period!'))
690 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
692 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
693 $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
694 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
699 # if oldcustomer ne customer redo form
700 my ($customer) = split /--/, $form->{customer};
701 if ($form->{oldcustomer} ne "$customer--$form->{customer_id}") {
706 $form->{AR}{receivables} = $form->{ARselected};
709 $form->{id} = 0 if $form->{postasnew};
710 $form->error($locale->text('Cannot post transaction!')) unless AR->post_transaction(\%myconfig, \%$form);
713 if(!exists $form->{addition} && $form->{id} ne "") {
714 $form->{snumbers} = "invnumber_$form->{invnumber}";
715 $form->{addition} = "POSTED";
718 # /saving the history
719 remove_draft() if $form->{remove_draft};
721 $form->redirect($locale->text('Transaction posted!')) unless $inline;
723 $main::lxdebug->leave_sub();
727 $main::lxdebug->enter_sub();
729 $main::auth->assert('general_ledger');
731 my $form = $main::form;
732 my %myconfig = %main::myconfig;
734 $form->{postasnew} = 1;
736 if(!exists $form->{addition} && $form->{id} ne "") {
737 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
738 $form->{addition} = "POSTED AS NEW";
741 # /saving the history
744 $main::lxdebug->leave_sub();
748 $main::lxdebug->enter_sub();
750 $main::auth->assert('general_ledger');
752 my $form = $main::form;
753 my %myconfig = %main::myconfig;
755 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);
756 $form->{paidaccounts} = 1;
758 $form->{invdate} = $form->current_date(\%myconfig);
761 $main::lxdebug->leave_sub();
765 $main::lxdebug->enter_sub();
767 $main::auth->assert('general_ledger');
769 my $form = $main::form;
770 my $locale = $main::locale;
772 $form->{title} = $locale->text('Confirm!');
776 delete $form->{header};
779 <form method=post action=$form->{script}>
782 foreach my $key (keys %$form) {
783 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
784 $form->{$key} =~ s/\"/"/g;
785 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
789 <h2 class=confirm>$form->{title}</h2>
792 . $locale->text('Are you sure you want to delete Transaction')
793 . qq| $form->{invnumber}</h4>
795 <input name=action class=submit type=submit value="|
796 . $locale->text('Yes') . qq|">
800 $main::lxdebug->leave_sub();
804 $main::lxdebug->enter_sub();
806 $main::auth->assert('general_ledger');
808 my $form = $main::form;
809 my %myconfig = %main::myconfig;
810 my $locale = $main::locale;
812 if (AR->delete_transaction(\%myconfig, \%$form)) {
814 if(!exists $form->{addition}) {
815 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
816 $form->{addition} = "DELETED";
819 # /saving the history
820 $form->redirect($locale->text('Transaction deleted!'));
822 $form->error($locale->text('Cannot delete transaction!'));
824 $main::lxdebug->leave_sub();
828 $main::lxdebug->enter_sub();
830 $main::auth->assert('invoice_edit');
832 my $form = $main::form;
833 my %myconfig = %main::myconfig;
834 my $locale = $main::locale;
835 my $cgi = $::request->{cgi};
837 # setup customer selection
838 $form->all_vc(\%myconfig, "customer", "AR");
840 $form->{title} = $locale->text('AR Transactions');
842 # Auch in Rechnungsübersicht nach Kundentyp filtern - jan
843 $form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 },
844 "departments" => "ALL_DEPARTMENTS",
845 "customers" => "ALL_VC",
846 "business_types" => "ALL_BUSINESS_TYPES");
847 $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
848 $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
850 # constants and subs for template
851 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
854 print $form->parse_html_template('ar/search', { %myconfig });
856 $main::lxdebug->leave_sub();
859 sub create_subtotal_row {
860 $main::lxdebug->enter_sub();
862 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
864 my $form = $main::form;
865 my %myconfig = %main::myconfig;
867 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
869 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
871 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
873 map { $totals->{$_} = 0 } @{ $subtotal_columns };
875 $main::lxdebug->leave_sub();
880 sub ar_transactions {
881 $main::lxdebug->enter_sub();
883 $main::auth->assert('invoice_edit');
885 my $form = $main::form;
886 my %myconfig = %main::myconfig;
887 my $locale = $main::locale;
889 my ($callback, $href, @columns);
891 ($form->{customer}, $form->{customer_id}) = split(/--/, $form->{customer});
893 report_generator_set_default_sort('transdate', 1);
895 AR->ar_transactions(\%myconfig, \%$form);
897 $form->{title} = $locale->text('AR Transactions');
899 my $report = SL::ReportGenerator->new(\%myconfig, $form);
902 qw(transdate id type invnumber ordnumber name netamount tax amount paid
903 datepaid due duedate transaction_description notes salesman employee shippingpoint shipvia
904 marge_total marge_percent globalprojectnumber customernumber country ustid taxzone payment_terms charts customertype);
906 my @hidden_variables = map { "l_${_}" } @columns;
907 push @hidden_variables, "l_subtotal", qw(open closed customer invnumber ordnumber transaction_description notes project_id transdatefrom transdateto employee_id salesman_id business_id);
909 $href = build_std_url('action=ar_transactions', grep { $form->{$_} } @hidden_variables);
912 'transdate' => { 'text' => $locale->text('Date'), },
913 'id' => { 'text' => $locale->text('ID'), },
914 'type' => { 'text' => $locale->text('Type'), },
915 'invnumber' => { 'text' => $locale->text('Invoice'), },
916 'ordnumber' => { 'text' => $locale->text('Order'), },
917 'name' => { 'text' => $locale->text('Customer'), },
918 'netamount' => { 'text' => $locale->text('Amount'), },
919 'tax' => { 'text' => $locale->text('Tax'), },
920 'amount' => { 'text' => $locale->text('Total'), },
921 'paid' => { 'text' => $locale->text('Paid'), },
922 'datepaid' => { 'text' => $locale->text('Date Paid'), },
923 'due' => { 'text' => $locale->text('Amount Due'), },
924 'duedate' => { 'text' => $locale->text('Due Date'), },
925 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
926 'notes' => { 'text' => $locale->text('Notes'), },
927 'salesman' => { 'text' => $locale->text('Salesperson'), },
928 'employee' => { 'text' => $locale->text('Employee'), },
929 'shippingpoint' => { 'text' => $locale->text('Shipping Point'), },
930 'shipvia' => { 'text' => $locale->text('Ship via'), },
931 'globalprojectnumber' => { 'text' => $locale->text('Document Project Number'), },
932 'marge_total' => { 'text' => $locale->text('Ertrag'), },
933 'marge_percent' => { 'text' => $locale->text('Ertrag prozentual'), },
934 'customernumber' => { 'text' => $locale->text('Customer Number'), },
935 'country' => { 'text' => $locale->text('Country'), },
936 'ustid' => { 'text' => $locale->text('USt-IdNr.'), },
937 'taxzone' => { 'text' => $locale->text('Steuersatz'), },
938 'payment_terms' => { 'text' => $locale->text('Payment Terms'), },
939 'charts' => { 'text' => $locale->text('Buchungskonto'), },
940 'customertype' => { 'text' => $locale->text('Customer type'), },
943 foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid employee shippingpoint shipvia transaction_description)) {
944 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
945 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
948 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
950 $form->{"l_type"} = "Y";
951 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
953 $report->set_columns(%column_defs);
954 $report->set_column_order(@columns);
956 $report->set_export_options('ar_transactions', @hidden_variables, qw(sort sortdir));
958 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
961 if ($form->{customer}) {
962 push @options, $locale->text('Customer') . " : $form->{customer}";
964 if ($form->{department}) {
965 my ($department) = split /--/, $form->{department};
966 push @options, $locale->text('Department') . " : $department";
968 if ($form->{department_id}) {
969 push @options, $locale->text('Department Id') . " : $form->{department_id}";
971 if ($form->{invnumber}) {
972 push @options, $locale->text('Invoice Number') . " : $form->{invnumber}";
974 if ($form->{ordnumber}) {
975 push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
977 if ($form->{notes}) {
978 push @options, $locale->text('Notes') . " : $form->{notes}";
980 if ($form->{transaction_description}) {
981 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
983 if ($form->{transdatefrom}) {
984 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
986 if ($form->{transdateto}) {
987 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
990 push @options, $locale->text('Open');
992 if ($form->{employee_id}) {
993 my $employee = SL::DB::Employee->new(id => $form->{employee_id})->load;
994 push @options, $locale->text('Employee') . ' : ' . $employee->name;
996 if ($form->{salesman_id}) {
997 my $salesman = SL::DB::Employee->new(id => $form->{salesman_id})->load;
998 push @options, $locale->text('Salesman') . ' : ' . $salesman->name;
1000 if ($form->{closed}) {
1001 push @options, $locale->text('Closed');
1004 $report->set_options('top_info_text' => join("\n", @options),
1005 'raw_bottom_info_text' => $form->parse_html_template('ar/ar_transactions_bottom'),
1006 'output_format' => 'HTML',
1007 'title' => $form->{title},
1008 'attachment_basename' => $locale->text('invoice_list') . strftime('_%Y%m%d', localtime time),
1010 $report->set_options_from_form();
1011 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1013 # add sort and escape callback, this one we use for the add sub
1014 $form->{callback} = $href .= "&sort=$form->{sort}";
1016 # escape callback for href
1017 $callback = $form->escape($href);
1019 my @subtotal_columns = qw(netamount amount paid due marge_total marge_percent);
1021 my %totals = map { $_ => 0 } @subtotal_columns;
1022 my %subtotals = map { $_ => 0 } @subtotal_columns;
1026 foreach my $ar (@{ $form->{AR} }) {
1027 $ar->{tax} = $ar->{amount} - $ar->{netamount};
1028 $ar->{due} = $ar->{amount} - $ar->{paid};
1030 map { $subtotals{$_} += $ar->{$_};
1031 $totals{$_} += $ar->{$_} } @subtotal_columns;
1033 $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
1034 $totals{marge_percent} = $totals{netamount} ? ($totals{marge_total} * 100 / $totals{netamount} ) : 0;
1036 map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(netamount tax amount paid due marge_total marge_percent);
1038 my $is_storno = $ar->{storno} && $ar->{storno_id};
1039 my $has_storno = $ar->{storno} && !$ar->{storno_id};
1042 $has_storno ? $locale->text("Invoice with Storno (abbreviation)") :
1043 $is_storno ? $locale->text("Storno (one letter abbreviation)") :
1044 $ar->{amount} < 0 ? $locale->text("Credit note (one letter abbreviation)") :
1045 $ar->{invoice} ? $locale->text("Invoice (one letter abbreviation)") :
1046 $locale->text("AR Transaction (abbreviation)");
1050 foreach my $column (@columns) {
1052 'data' => $ar->{$column},
1053 'align' => $column_alignment{$column},
1057 $row->{invnumber}->{link} = build_std_url("script=" . ($ar->{invoice} ? 'is.pl' : 'ar.pl'), 'action=edit')
1058 . "&id=" . E($ar->{id}) . "&callback=${callback}";
1060 my $row_set = [ $row ];
1062 if (($form->{l_subtotal} eq 'Y')
1063 && (($idx == (scalar @{ $form->{AR} } - 1))
1064 || ($ar->{ $form->{sort} } ne $form->{AR}->[$idx + 1]->{ $form->{sort} }))) {
1065 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1068 $report->add_data($row_set);
1073 $report->add_separator();
1074 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1076 $report->generate_with_headers();
1078 $main::lxdebug->leave_sub();
1082 $main::lxdebug->enter_sub();
1084 $main::auth->assert('general_ledger');
1086 my $form = $main::form;
1087 my %myconfig = %main::myconfig;
1088 my $locale = $main::locale;
1090 # don't cancel cancelled transactions
1091 if (IS->has_storno(\%myconfig, $form, 'ar')) {
1092 $form->{title} = $locale->text("Cancel Accounts Receivables Transaction");
1093 $form->error($locale->text("Transaction has already been cancelled!"));
1096 AR->storno($form, \%myconfig, $form->{id});
1098 # saving the history
1099 if(!exists $form->{addition} && $form->{id} ne "") {
1100 $form->{snumbers} = "ordnumber_$form->{ordnumber}";
1101 $form->{addition} = "STORNO";
1102 $form->save_history;
1104 # /saving the history
1106 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1108 $main::lxdebug->leave_sub();