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);
41 use SL::ReportGenerator;
43 require "bin/mozilla/arap.pl";
44 require "bin/mozilla/common.pl";
45 require "bin/mozilla/drafts.pl";
46 require "bin/mozilla/reportgenerator.pl";
51 # this is for our long dates
52 # $locale->text('January')
53 # $locale->text('February')
54 # $locale->text('March')
55 # $locale->text('April')
56 # $locale->text('May ')
57 # $locale->text('June')
58 # $locale->text('July')
59 # $locale->text('August')
60 # $locale->text('September')
61 # $locale->text('October')
62 # $locale->text('November')
63 # $locale->text('December')
65 # this is for our short month
66 # $locale->text('Jan')
67 # $locale->text('Feb')
68 # $locale->text('Mar')
69 # $locale->text('Apr')
70 # $locale->text('May')
71 # $locale->text('Jun')
72 # $locale->text('Jul')
73 # $locale->text('Aug')
74 # $locale->text('Sep')
75 # $locale->text('Oct')
76 # $locale->text('Nov')
77 # $locale->text('Dec')
80 $main::lxdebug->enter_sub();
82 $main::auth->assert('general_ledger');
84 my $form = $main::form;
85 my %myconfig = %main::myconfig;
87 return $main::lxdebug->leave_sub() if (load_draft_maybe());
90 if(!exists $form->{addition} && ($form->{id} ne "")) {
91 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
92 $form->{addition} = "ADDED";
97 $form->{title} = "Add";
98 $form->{callback} = "ar.pl?action=add&DONT_LOAD_DRAFT=1" unless $form->{callback};
100 AR->get_transdate(\%myconfig, $form);
101 $form->{initial_transdate} = $form->{transdate};
103 $form->{transdate} = $form->{initial_transdate};
105 $main::lxdebug->leave_sub();
109 $main::lxdebug->enter_sub();
111 $main::auth->assert('general_ledger');
113 my $form = $main::form;
115 # show history button
116 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
117 #/show hhistory button
118 $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
119 $form->{title} = "Edit";
124 $main::lxdebug->leave_sub();
128 $main::lxdebug->enter_sub();
130 $main::auth->assert('general_ledger');
132 my $form = $main::form;
137 $main::lxdebug->leave_sub();
141 $main::lxdebug->enter_sub();
143 $main::auth->assert('general_ledger');
145 my $form = $main::form;
146 my %myconfig = %main::myconfig;
148 my ($duedate, $taxincluded);
150 $form->create_links("AR", \%myconfig, "customer");
151 $duedate = $form->{duedate};
153 $taxincluded = $form->{taxincluded};
154 my $id = $form->{id};
155 IS->get_customer(\%myconfig, \%$form);
156 $form->{taxincluded} = $taxincluded;
159 $form->{duedate} = $duedate if $duedate;
160 $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
161 $form->{rowcount} = 1;
164 $form->{notes} = $form->{intnotes} unless $form->{notes};
167 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
169 map { $form->{selectcurrency} .= "<option>$_\n" } $form->get_all_currencies(\%myconfig);
172 if (@{ $form->{all_customer} || [] }) {
173 $form->{customer} = "$form->{customer}--$form->{customer_id}";
174 map { $form->{selectcustomer} .= "<option>$_->{name}--$_->{id}\n" }
175 (@{ $form->{all_customer} });
179 if (@{ $form->{all_departments} || [] }) {
180 $form->{selectdepartment} = "<option>\n";
181 $form->{department} = "$form->{department}--$form->{department_id}";
184 $form->{selectdepartment} .=
185 "<option>$_->{description}--$_->{id}\n"
186 } (@{ $form->{all_departments} || [] });
189 $form->{employee} = "$form->{employee}--$form->{employee_id}";
192 if (@{ $form->{all_employees} || [] }) {
193 $form->{selectemployee} = "";
194 map { $form->{selectemployee} .= "<option>$_->{name}--$_->{id}\n" }
195 (@{ $form->{all_employees} || [] });
198 # build the popup menus
199 $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
201 AR->setup_form($form);
204 ($form->datetonum($form->{transdate}, \%myconfig) <=
205 $form->datetonum($form->{closedto}, \%myconfig));
207 $main::lxdebug->leave_sub();
211 $main::lxdebug->enter_sub();
213 $main::auth->assert('general_ledger');
215 my $form = $main::form;
216 my %myconfig = %main::myconfig;
217 my $locale = $main::locale;
218 my $cgi = $::request->{cgi};
220 my ($title, $readonly, $exchangerate, $rows);
221 my ($notes, $department, $customer, $employee, $amount, $project);
225 $title = $form->{title};
226 # $locale->text('Add Accounts Receivables Transaction')
227 # $locale->text('Edit Accounts Receivables Transaction')
228 $form->{title} = $locale->text("$title Accounts Receivables Transaction");
230 $form->{javascript} = qq|<script type="text/javascript">
232 function setTaxkey(accno, row) {
233 var taxkey = accno.options[accno.selectedIndex].value;
234 var reg = /--([0-9]*)/;
235 var found = reg.exec(taxkey);
236 var index = found[1];
237 index = parseInt(index);
238 var tax = 'taxchart_' + row;
239 for (var i = 0; i < document.getElementById(tax).options.length; ++i) {
240 var reg2 = new RegExp("^"+ index, "");
241 if (reg2.exec(document.getElementById(tax).options[i].value)) {
242 document.getElementById(tax).options[i].selected = true;
249 # show history button js
250 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
251 #/show history button js
252 $readonly = ($form->{id}) ? "readonly" : "";
254 $form->{radier} = ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
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 $form->{fokus} = "arledger.customer";
320 my $follow_up_vc = $form->{customer};
321 $follow_up_vc =~ s/--.*?//;
322 my $follow_up_trans_info = "$form->{invnumber} ($follow_up_vc)";
324 $form->{javascript} .=
325 qq|<script type="text/javascript" src="js/show_vc_details.js"></script>| .
326 qq|<script type="text/javascript" src="js/follow_up.js"></script>|;
328 # $amount = $locale->text('Amount');
329 # $project = $locale->text('Project');
332 for my $i (1 .. $form->{rowcount}) {
334 amount => $form->{"amount_$i"},
335 tax => $form->{"tax_$i"},
336 project_id => $form->{"project_id_$i"},
339 my $selected_accno_full;
340 my ($accno_row) = split(/--/, $form->{"AR_amount_$i"});
341 my $item = $charts{$accno_row};
342 $selected_accno_full = "$item->{accno}--$item->{tax_id}";
344 my $selected_taxchart = $form->{"taxchart_$i"};
345 my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full);
346 my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_AR_amount_$i"});
348 if ($previous_accno &&
349 ($previous_accno eq $selected_accno) &&
350 ($previous_tax_id ne $selected_tax_id)) {
351 my $item = $taxcharts{$selected_tax_id};
352 $selected_taxchart = "$item->{id}--$item->{rate}";
355 if (!$form->{"taxchart_$i"}) {
356 if ($form->{"AR_amount_$i"} =~ m/.--./) {
357 $selected_taxchart = join '--', map { ($_->{id}, $_->{rate}) } first { $_->{id} == $item->{tax_id} } @{ $form->{ALL_TAXCHARTS} };
359 $selected_taxchart = $taxchart_init;
363 $transaction->{selectAR_amount} =
364 NTI($cgi->popup_menu('-name' => "AR_amount_$i",
365 '-id' => "AR_amount_$i",
366 '-style' => 'width:400px',
367 '-onChange' => "setTaxkey(this, $i)",
368 '-values' => \@AR_amount_values,
369 '-labels' => \%chart_labels,
370 '-default' => $selected_accno_full))
371 . $cgi->hidden('-name' => "previous_AR_amount_$i",
372 '-default' => $selected_accno_full);
374 $transaction->{taxchart} =
375 NTI($cgi->popup_menu('-name' => "taxchart_$i",
376 '-id' => "taxchart_$i",
377 '-style' => 'width:200px',
378 '-values' => \@taxchart_values,
379 '-labels' => \%taxchart_labels,
380 '-default' => $selected_taxchart));
382 push @transactions, $transaction;
385 $form->{invtotal_unformatted} = $form->{invtotal};
388 NTI($cgi->popup_menu('-name' => "ARselected", '-id' => "ARselected",
389 '-style' => 'width:400px',
390 '-values' => \@AR_values, '-labels' => \%chart_labels,
391 '-default' => $form->{ARselected}));
394 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
396 my $now = $form->current_date(\%myconfig);
399 for my $i (1 .. $form->{paidaccounts}) {
401 paid => $form->{"paid_$i"},
402 exchangerate => $form->{"exchangerate_$i"} || '',
403 gldate => $form->{"gldate_$i"},
404 acc_trans_id => $form->{"acc_trans_id_$i"},
405 source => $form->{"source_$i"},
406 memo => $form->{"memo_$i"},
407 AR_paid => $form->{"AR_paid_$i"},
408 forex => $form->{"forex_$i"},
409 datepaid => $form->{"datepaid_$i"},
410 paid_project_id => $form->{"paid_project_id_$i"},
411 gldate => $form->{"gldate_$i"},
414 $payment->{selectAR_paid} =
415 NTI($cgi->popup_menu('-name' => "AR_paid_$i",
416 '-id' => "AR_paid_$i",
417 '-values' => \@AR_paid_values,
418 '-labels' => \%chart_labels,
419 '-default' => $payment->{AR_paid}));
423 $payment->{changeable} =
424 $::lx_office_conf{features}->{payments_changeable} == 0 ? !$payment->{acc_trans_id} # never
425 : $::lx_office_conf{features}->{payments_changeable} == 2 ? $payment->{gldate} eq '' || $payment->{gldate} eq $now
428 push @payments, $payment;
431 $form->{totalpaid} = sum map { $_->{paid} } @payments;
434 print $::form->parse_html_template('ar/form_header', {
435 paid_missing => $::form->{invtotal} - $::form->{totalpaid},
436 show_exch => ($::form->{defaultcurrency} && ($::form->{currency} ne $::form->{defaultcurrency})),
437 payments => \@payments,
438 transactions => \@transactions,
439 project_labels => \%project_labels,
441 ARselected => $ARselected,
443 follow_up_trans_info => $follow_up_trans_info,
446 $main::lxdebug->leave_sub();
450 $main::lxdebug->enter_sub();
452 $main::auth->assert('general_ledger');
454 my $form = $main::form;
455 my %myconfig = %main::myconfig;
456 my $locale = $main::locale;
457 my $cgi = $::request->{cgi};
459 my ($transdate, $closedto);
461 my $follow_ups_block;
463 my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
465 if (@{ $follow_ups} ) {
466 my $num_due = sum map { $_->{due} * 1 } @{ $follow_ups };
467 $follow_ups_block = qq|<p>| . $locale->text("There are #1 unfinished follow-ups of which #2 are due.", scalar @{ $follow_ups }, $num_due) . qq|</p>|;
475 <input name=gldate type=hidden value="| . Q($form->{gldate}) . qq|">
477 <input name=callback type=hidden value="$form->{callback}">
479 . $cgi->hidden('-name' => 'draft_id', '-default' => [$form->{draft_id}])
480 . $cgi->hidden('-name' => 'draft_description', '-default' => [$form->{draft_description}])
486 if (!$form->{id} && $form->{draft_id}) {
487 print(NTI($cgi->checkbox('-name' => 'remove_draft', '-id' => 'remove_draft',
488 '-value' => 1, '-checked' => $form->{remove_draft},
490 qq| <label for="remove_draft">| .
491 $locale->text("Remove draft when posting") .
495 $transdate = $form->datetonum($form->{transdate}, \%myconfig);
496 $closedto = $form->datetonum($form->{closedto}, \%myconfig);
498 print qq|<input class="submit" type="submit" name="action" id="update_button" value="| . $locale->text('Update') . qq|">\n|;
500 # ToDO: - insert a global check for stornos, so that a storno is only possible a limited time after saving it
501 print qq| <input class=submit type=submit name=action value="| . $locale->text('Storno') . qq|"> |
502 if ($form->{id} && !IS->has_storno(\%myconfig, $form, 'ar') && !IS->is_storno(\%myconfig, $form, 'ar') && (($form->{totalpaid} == 0) || ($form->{totalpaid} eq "")));
505 if ($form->{radier}) {
507 <input class=submit type=submit name=action value="| . $locale->text('Post') . qq|">
508 <input class=submit type=submit name=action value="| . $locale->text('Delete') . qq|"> |;
510 if ($transdate > $closedto) {
512 <input class=submit type=submit name=action value="| . $locale->text('Use As Template') . qq|"> |;
515 <input class=submit type=submit name=action value="| . $locale->text('Post Payment') . qq|">
516 <input type="button" class="submit" onclick="follow_up_window()" value="|
517 . $locale->text('Follow-Up')
521 if ($transdate > $closedto) {
522 print qq| <input class=submit type=submit name=action value="| . $locale->text('Post') . qq|"> | .
523 NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'), '-class' => 'submit'));
527 if ($form->{menubar}) {
528 require "bin/mozilla/menu.pl";
531 # button for saving history
532 if($form->{id} ne "") {
533 print qq| <input type=button class=submit onclick=set_history_window($form->{id}); name=history id=history value=| . $locale->text('history') . qq|> |;
535 # /button for saving history
536 # mark_as_paid button
537 if($form->{id} ne "") {
538 print qq|<input type="submit" class="submit" name="action" value="|
539 . $locale->text('mark as paid') . qq|">|;
541 # /mark_as_paid button
547 $main::lxdebug->leave_sub();
551 $main::lxdebug->enter_sub();
553 $main::auth->assert('general_ledger');
555 my $form = $main::form;
556 my %myconfig = %main::myconfig;
558 &mark_as_paid_common(\%myconfig,"ar");
560 $main::lxdebug->leave_sub();
564 $main::lxdebug->enter_sub();
566 $main::auth->assert('general_ledger');
568 my $form = $main::form;
569 my %myconfig = %main::myconfig;
573 my ($totaltax, $exchangerate);
575 $form->{invtotal} = 0;
577 delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
579 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
580 qw(exchangerate creditlimit creditremaining);
582 my @flds = qw(amount AR_amount projectnumber oldprojectnumber project_id);
586 for my $i (1 .. $form->{rowcount}) {
587 $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
588 $form->{"tax_$i"} = $form->parse_amount(\%myconfig, $form->{"tax_$i"});
589 if ($form->{"amount_$i"}) {
592 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
594 if ($form->{taxincluded}) {
595 $form->{"tax_$i"} = $form->{"amount_$i"} / ($rate + 1) * $rate;
597 $form->{"tax_$i"} = $form->{"amount_$i"} * $rate;
600 $form->{"tax_$i"} = 0;
602 $form->{"tax_$i"} = $form->round_amount($form->{"tax_$i"}, 2);
604 $totaltax += $form->{"tax_$i"};
605 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
610 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
611 $form->{rowcount} = $count + 1;
612 map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
614 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'buy');
615 $form->{exchangerate} = $form->{forex} if $form->{forex};
617 $form->{invdate} = $form->{transdate};
619 $form->{invdate} = $form->{transdate};
621 my %saved_variables = map +( $_ => $form->{$_} ), qw(AR AR_amount_1 taxchart_1 customer_id);
623 &check_name("customer");
625 # check_name loads customer notes into notes, but ar only knows intnotes, so copy them
626 $form->{notes} = $form->{intnotes} if $saved_variables{customer_id} != $form->{customer_id};
628 $form->{AR} = $saved_variables{AR};
629 if ($saved_variables{AR_amount_1} =~ m/.--./) {
630 map { $form->{$_} = $saved_variables{$_} } qw(AR_amount_1 taxchart_1);
632 delete $form->{taxchart_1};
636 ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
638 for my $i (1 .. $form->{paidaccounts}) {
639 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
642 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
643 } qw(paid exchangerate);
645 $form->{totalpaid} += $form->{"paid_$i"};
647 $form->{"forex_$i"} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
648 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
652 $form->{creditremaining} -=
653 ($form->{invtotal} - $form->{totalpaid} + $form->{oldtotalpaid} -
654 $form->{oldinvtotal});
655 $form->{oldinvtotal} = $form->{invtotal};
656 $form->{oldtotalpaid} = $form->{totalpaid};
660 $main::lxdebug->leave_sub();
664 # ToDO: fix $closedto and $invdate
667 $main::lxdebug->enter_sub();
669 $main::auth->assert('general_ledger');
671 my $form = $main::form;
672 my %myconfig = %main::myconfig;
673 my $locale = $main::locale;
675 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
677 my $invdate = $form->datetonum($form->{transdate}, \%myconfig);
679 for my $i (1 .. $form->{paidaccounts}) {
681 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
682 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
684 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
686 $form->error($locale->text('Cannot post payment for a closed period!')) if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
688 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
689 # $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid);
690 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
695 ($form->{AR}) = split /--/, $form->{AR};
696 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
697 $form->redirect($locale->text('Payment posted!')) if (AR->post_payment(\%myconfig, \%$form));
698 $form->error($locale->text('Cannot post payment!'));
700 $main::lxdebug->leave_sub();
705 $main::auth->assert('general_ledger');
707 my $form = $main::form;
714 $main::lxdebug->enter_sub();
716 $main::auth->assert('general_ledger');
718 my $form = $main::form;
719 my %myconfig = %main::myconfig;
720 my $locale = $main::locale;
726 # check if there is an invoice number, invoice and due date
727 $form->isblank("transdate", $locale->text('Invoice Date missing!'));
728 $form->isblank("duedate", $locale->text('Due Date missing!'));
729 $form->isblank("customer", $locale->text('Customer missing!'));
731 if ($myconfig{mandatory_departments} && !$form->{department}) {
732 $form->{saved_message} = $::locale->text('You have to specify a department.');
737 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
738 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
739 $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
741 $form->error($locale->text('Zero amount posting!'))
742 unless grep $_*1, map $form->parse_amount(\%myconfig, $form->{"amount_$_"}), 1..$form->{rowcount};
744 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
745 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency}));
749 for my $i (1 .. $form->{paidaccounts}) {
750 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
751 $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
753 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
755 $form->error($locale->text('Cannot post payment for a closed period!'))
756 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
758 if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
759 $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
760 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
765 # if oldcustomer ne customer redo form
766 my ($customer) = split /--/, $form->{customer};
767 if ($form->{oldcustomer} ne "$customer--$form->{customer_id}") {
772 $form->{AR}{receivables} = $form->{ARselected};
775 $form->{id} = 0 if $form->{postasnew};
776 $form->error($locale->text('Cannot post transaction!')) unless AR->post_transaction(\%myconfig, \%$form);
779 if(!exists $form->{addition} && $form->{id} ne "") {
780 $form->{snumbers} = "invnumber_$form->{invnumber}";
781 $form->{addition} = "POSTED";
784 # /saving the history
785 remove_draft() if $form->{remove_draft};
787 $form->redirect($locale->text('Transaction posted!')) unless $inline;
789 $main::lxdebug->leave_sub();
793 $main::lxdebug->enter_sub();
795 $main::auth->assert('general_ledger');
797 my $form = $main::form;
798 my %myconfig = %main::myconfig;
800 $form->{postasnew} = 1;
802 if(!exists $form->{addition} && $form->{id} ne "") {
803 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
804 $form->{addition} = "POSTED AS NEW";
807 # /saving the history
810 $main::lxdebug->leave_sub();
813 sub use_as_template {
814 $main::lxdebug->enter_sub();
816 $main::auth->assert('general_ledger');
818 my $form = $main::form;
819 my %myconfig = %main::myconfig;
821 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);
822 $form->{paidaccounts} = 1;
824 $form->{invdate} = $form->current_date(\%myconfig);
827 $main::lxdebug->leave_sub();
831 $main::lxdebug->enter_sub();
833 $main::auth->assert('general_ledger');
835 my $form = $main::form;
836 my $locale = $main::locale;
838 $form->{title} = $locale->text('Confirm!');
842 delete $form->{header};
845 <form method=post action=$form->{script}>
848 foreach my $key (keys %$form) {
849 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
850 $form->{$key} =~ s/\"/"/g;
851 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
855 <h2 class=confirm>$form->{title}</h2>
858 . $locale->text('Are you sure you want to delete Transaction')
859 . qq| $form->{invnumber}</h4>
861 <input name=action class=submit type=submit value="|
862 . $locale->text('Yes') . qq|">
866 $main::lxdebug->leave_sub();
870 $main::lxdebug->enter_sub();
872 $main::auth->assert('general_ledger');
874 my $form = $main::form;
875 my %myconfig = %main::myconfig;
876 my $locale = $main::locale;
878 if (AR->delete_transaction(\%myconfig, \%$form)) {
880 if(!exists $form->{addition}) {
881 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
882 $form->{addition} = "DELETED";
885 # /saving the history
886 $form->redirect($locale->text('Transaction deleted!'));
888 $form->error($locale->text('Cannot delete transaction!'));
890 $main::lxdebug->leave_sub();
894 $main::lxdebug->enter_sub();
896 $main::auth->assert('general_ledger | invoice_edit');
898 my $form = $main::form;
899 my %myconfig = %main::myconfig;
900 my $locale = $main::locale;
901 my $cgi = $::request->{cgi};
903 my ($customer, $department);
904 my ($jsscript, $button1, $button2);
906 # setup customer selection
907 $form->all_vc(\%myconfig, "customer", "AR");
909 $form->{title} = $locale->text('AR Transactions');
910 $form->{jsscript} = 1;
912 # Auch in Rechnungsübersicht nach Kundentyp filtern - jan
913 $form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 },
914 "departments" => "ALL_DEPARTMENTS",
915 "customers" => "ALL_VC",
916 "business_types" => "ALL_BUSINESS_TYPES");
917 $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
918 $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
920 # constants and subs for template
921 $form->{jsscript} = 1;
922 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
925 print $form->parse_html_template('ar/search', { %myconfig });
927 $main::lxdebug->leave_sub();
930 sub create_subtotal_row {
931 $main::lxdebug->enter_sub();
933 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
935 my $form = $main::form;
936 my %myconfig = %main::myconfig;
938 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
940 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
942 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
944 map { $totals->{$_} = 0 } @{ $subtotal_columns };
946 $main::lxdebug->leave_sub();
951 sub ar_transactions {
952 $main::lxdebug->enter_sub();
954 $main::auth->assert('general_ledger | invoice_edit');
956 my $form = $main::form;
957 my %myconfig = %main::myconfig;
958 my $locale = $main::locale;
960 my ($callback, $href, @columns);
962 ($form->{customer}, $form->{customer_id}) = split(/--/, $form->{customer});
964 report_generator_set_default_sort('transdate', 1);
966 AR->ar_transactions(\%myconfig, \%$form);
968 $form->{title} = $locale->text('AR Transactions');
970 my $report = SL::ReportGenerator->new(\%myconfig, $form);
973 qw(transdate id type invnumber ordnumber name netamount tax amount paid
974 datepaid due duedate transaction_description notes salesman employee shippingpoint shipvia
975 marge_total marge_percent globalprojectnumber customernumber country ustid taxzone payment_terms charts customertype);
977 my @hidden_variables = map { "l_${_}" } @columns;
978 push @hidden_variables, "l_subtotal", qw(open closed customer invnumber ordnumber transaction_description notes project_id transdatefrom transdateto employee_id salesman_id business_id);
980 $href = build_std_url('action=ar_transactions', grep { $form->{$_} } @hidden_variables);
983 'transdate' => { 'text' => $locale->text('Date'), },
984 'id' => { 'text' => $locale->text('ID'), },
985 'type' => { 'text' => $locale->text('Type'), },
986 'invnumber' => { 'text' => $locale->text('Invoice'), },
987 'ordnumber' => { 'text' => $locale->text('Order'), },
988 'name' => { 'text' => $locale->text('Customer'), },
989 'netamount' => { 'text' => $locale->text('Amount'), },
990 'tax' => { 'text' => $locale->text('Tax'), },
991 'amount' => { 'text' => $locale->text('Total'), },
992 'paid' => { 'text' => $locale->text('Paid'), },
993 'datepaid' => { 'text' => $locale->text('Date Paid'), },
994 'due' => { 'text' => $locale->text('Amount Due'), },
995 'duedate' => { 'text' => $locale->text('Due Date'), },
996 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
997 'notes' => { 'text' => $locale->text('Notes'), },
998 'salesman' => { 'text' => $locale->text('Salesperson'), },
999 'employee' => { 'text' => $locale->text('Employee'), },
1000 'shippingpoint' => { 'text' => $locale->text('Shipping Point'), },
1001 'shipvia' => { 'text' => $locale->text('Ship via'), },
1002 'globalprojectnumber' => { 'text' => $locale->text('Document Project Number'), },
1003 'marge_total' => { 'text' => $locale->text('Ertrag'), },
1004 'marge_percent' => { 'text' => $locale->text('Ertrag prozentual'), },
1005 'customernumber' => { 'text' => $locale->text('Customer Number'), },
1006 'country' => { 'text' => $locale->text('Country'), },
1007 'ustid' => { 'text' => $locale->text('USt-IdNr.'), },
1008 'taxzone' => { 'text' => $locale->text('Steuersatz'), },
1009 'payment_terms' => { 'text' => $locale->text('Payment Terms'), },
1010 'charts' => { 'text' => $locale->text('Buchungskonto'), },
1011 'customertype' => { 'text' => $locale->text('Customer type'), },
1014 foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid employee shippingpoint shipvia transaction_description)) {
1015 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
1016 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
1019 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
1021 $form->{"l_type"} = "Y";
1022 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
1024 $report->set_columns(%column_defs);
1025 $report->set_column_order(@columns);
1027 $report->set_export_options('ar_transactions', @hidden_variables, qw(sort sortdir));
1029 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
1032 if ($form->{customer}) {
1033 push @options, $locale->text('Customer') . " : $form->{customer}";
1035 if ($form->{department}) {
1036 my ($department) = split /--/, $form->{department};
1037 push @options, $locale->text('Department') . " : $department";
1039 if ($form->{department_id}) {
1040 push @options, $locale->text('Department Id') . " : $form->{department_id}";
1042 if ($form->{invnumber}) {
1043 push @options, $locale->text('Invoice Number') . " : $form->{invnumber}";
1045 if ($form->{ordnumber}) {
1046 push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
1048 if ($form->{notes}) {
1049 push @options, $locale->text('Notes') . " : $form->{notes}";
1051 if ($form->{transaction_description}) {
1052 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
1054 if ($form->{transdatefrom}) {
1055 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1057 if ($form->{transdateto}) {
1058 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
1060 if ($form->{open}) {
1061 push @options, $locale->text('Open');
1063 if ($form->{employee_id}) {
1064 my $employee = SL::DB::Employee->new(id => $form->{employee_id})->load;
1065 push @options, $locale->text('Employee') . ' : ' . $employee->name;
1067 if ($form->{salesman_id}) {
1068 my $salesman = SL::DB::Employee->new(id => $form->{salesman_id})->load;
1069 push @options, $locale->text('Salesman') . ' : ' . $salesman->name;
1071 if ($form->{closed}) {
1072 push @options, $locale->text('Closed');
1075 $report->set_options('top_info_text' => join("\n", @options),
1076 'raw_bottom_info_text' => $form->parse_html_template('ar/ar_transactions_bottom'),
1077 'output_format' => 'HTML',
1078 'title' => $form->{title},
1079 'attachment_basename' => $locale->text('invoice_list') . strftime('_%Y%m%d', localtime time),
1081 $report->set_options_from_form();
1082 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1084 # add sort and escape callback, this one we use for the add sub
1085 $form->{callback} = $href .= "&sort=$form->{sort}";
1087 # escape callback for href
1088 $callback = $form->escape($href);
1090 my @subtotal_columns = qw(netamount amount paid due marge_total marge_percent);
1092 my %totals = map { $_ => 0 } @subtotal_columns;
1093 my %subtotals = map { $_ => 0 } @subtotal_columns;
1097 foreach my $ar (@{ $form->{AR} }) {
1098 $ar->{tax} = $ar->{amount} - $ar->{netamount};
1099 $ar->{due} = $ar->{amount} - $ar->{paid};
1101 map { $subtotals{$_} += $ar->{$_};
1102 $totals{$_} += $ar->{$_} } @subtotal_columns;
1104 $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
1105 $totals{marge_percent} = $totals{netamount} ? ($totals{marge_total} * 100 / $totals{netamount} ) : 0;
1107 map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(netamount tax amount paid due marge_total marge_percent);
1109 my $is_storno = $ar->{storno} && $ar->{storno_id};
1110 my $has_storno = $ar->{storno} && !$ar->{storno_id};
1113 $has_storno ? $locale->text("Invoice with Storno (abbreviation)") :
1114 $is_storno ? $locale->text("Storno (one letter abbreviation)") :
1115 $ar->{amount} < 0 ? $locale->text("Credit note (one letter abbreviation)") :
1116 $ar->{invoice} ? $locale->text("Invoice (one letter abbreviation)") :
1117 $locale->text("AR Transaction (abbreviation)");
1121 foreach my $column (@columns) {
1123 'data' => $ar->{$column},
1124 'align' => $column_alignment{$column},
1128 $row->{invnumber}->{link} = build_std_url("script=" . ($ar->{invoice} ? 'is.pl' : 'ar.pl'), 'action=edit')
1129 . "&id=" . E($ar->{id}) . "&callback=${callback}";
1131 my $row_set = [ $row ];
1133 if (($form->{l_subtotal} eq 'Y')
1134 && (($idx == (scalar @{ $form->{AR} } - 1))
1135 || ($ar->{ $form->{sort} } ne $form->{AR}->[$idx + 1]->{ $form->{sort} }))) {
1136 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1139 $report->add_data($row_set);
1144 $report->add_separator();
1145 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1147 $report->generate_with_headers();
1149 $main::lxdebug->leave_sub();
1153 $main::lxdebug->enter_sub();
1155 $main::auth->assert('general_ledger');
1157 my $form = $main::form;
1158 my %myconfig = %main::myconfig;
1159 my $locale = $main::locale;
1161 # don't cancel cancelled transactions
1162 if (IS->has_storno(\%myconfig, $form, 'ar')) {
1163 $form->{title} = $locale->text("Cancel Accounts Receivables Transaction");
1164 $form->error($locale->text("Transaction has already been cancelled!"));
1167 AR->storno($form, \%myconfig, $form->{id});
1169 # saving the history
1170 if(!exists $form->{addition} && $form->{id} ne "") {
1171 $form->{snumbers} = "ordnumber_$form->{ordnumber}";
1172 $form->{addition} = "STORNO";
1173 $form->save_history;
1175 # /saving the history
1177 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1179 $main::lxdebug->leave_sub();