use SL::DBUtils;
use SL::MoreCommon;
+our (%myconfig, $form);
+
sub post_transaction {
$main::lxdebug->enter_sub();
}
# adjust paidaccounts if there is no date in the last row
- $form->{paidaccounts}-- unless $form->{"datepaid_$form->{paidaccounts}"};
- $form->{paid} = 0;
-
- # add payments
- for $i (1 .. $form->{paidaccounts}) {
- $form->{"paid_$i"} = $form->round_amount($form->parse_amount($myconfig, $form->{"paid_$i"}), 2);
- $form->{paid} += $form->{"paid_$i"};
- $form->{datepaid} = $form->{"datepaid_$i"};
- }
+ # this does not apply to stornos, where the paid field is set manually
+ unless ($form->{storno}) {
+ $form->{paidaccounts}-- unless $form->{"datepaid_$form->{paidaccounts}"};
+ $form->{paid} = 0;
+
+ # add payments
+ for $i (1 .. $form->{paidaccounts}) {
+ $form->{"paid_$i"} = $form->round_amount($form->parse_amount($myconfig, $form->{"paid_$i"}), 2);
+ $form->{paid} += $form->{"paid_$i"};
+ $form->{datepaid} = $form->{"datepaid_$i"};
+ }
- $form->{amount} = $form->{netamount} + $form->{total_tax};
- $form->{paid} = $form->round_amount($form->{paid} * $form->{exchangerate}, 2);
+ $form->{amount} = $form->{netamount} + $form->{total_tax};
+ }
+ $form->{paid} = $form->round_amount($form->{paid} * ($form->{exchangerate} || 1), 2);
($null, $form->{employee_id}) = split /--/, $form->{employee};
$form->{tax} = $taxamount;
$form->{invtotal} = $totalamount + $totaltax;
+
+ $main::lxdebug->leave_sub();
}
+sub storno {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $form, $myconfig, $id) = @_;
+
+ my ($query, $new_id, $storno_row, $acc_trans_rows);
+ my $dbh = $form->get_standard_dbh($myconfig);
+
+ $query = qq|SELECT nextval('glid')|;
+ ($new_id) = selectrow_query($form, $dbh, $query);
+
+ $query = qq|SELECT * FROM ar WHERE id = ?|;
+ $storno_row = selectfirst_hashref_query($form, $dbh, $query, $id);
+
+ $storno_row->{id} = $new_id;
+ $storno_row->{storno_id} = $id;
+ $storno_row->{storno} = 't';
+ $storno_row->{invnumber} = 'Storno-' . $storno_row->{invnumber};
+ $storno_row->{amount} *= -1;
+ $storno_row->{netamount} *= -1;
+ $storno_row->{paid} = $storno_amount->{amount};
+
+ delete @$storno_row{qw(itime mtime)};
+
+ $query = sprintf 'INSERT INTO ar (%s) VALUES (%s)', join(', ', keys %$storno_row), join(', ', map '?', values %$storno_row);
+ do_query($form, $dbh, $query, (values %$storno_row));
+
+ $query = qq|UPDATE ar SET paid = amount + paid, storno = 't' WHERE id = ?|;
+ do_query($form, $dbh, $query, $id);
+
+ # now copy acc_trans entries
+ $query = qq|SELECT * FROM acc_trans WHERE trans_id = ?|;
+ for my $row (@{ selectall_hashref_query($form, $dbh, $query, $id) }) {
+ delete @$row{qw(itime mtime)};
+ $query = sprintf 'INSERT INTO acc_trans (%s) VALUES (%s)', join(', ', keys %$row), join(', ', map '?', values %$row);
+ $row->{trans_id} = $new_id;
+ $row->{amount} *= -1;
+ do_query($form, $dbh, $query, (values %$row));
+ }
+
+ $dbh->commit;
+
+ $main::lxdebug->leave_sub();
+}
+
+
1;
use SL::PE;
use SL::ReportGenerator;
+use strict;
+use warnings;
+
+# imports
+our ($cgi, $form, $lxdebug, $locale, %myconfig);
+
require "bin/mozilla/arap.pl";
require "bin/mozilla/common.pl";
require "bin/mozilla/drafts.pl";
sub create_links {
$lxdebug->enter_sub();
+ my ($duedate, $taxincluded, @curr);
+
$form->create_links("AR", \%myconfig, "customer");
$duedate = $form->{duedate};
sub form_header {
$lxdebug->enter_sub();
+ my ($title, $readonly, $exchangerate, $rows);
+ my ($taxincluded, $notes, $department, $customer, $employee, $amount, $project);
+ my ($jsscript, $button1, $button2, $onload);
+ my ($selectAR_amount, $selectAR_paid, $korrektur_checked, $ARselected, $tax);
+ my (@column_index, %column_data);
+
+
$title = $form->{title};
$form->{title} = $locale->text("$title Accounts Receivables Transaction");
$readonly = ($form->{radier}) ? "" : $readonly;
# set option selected
- foreach $item (qw(customer currency department employee)) {
+ foreach my $item (qw(customer currency department employee)) {
$form->{"select$item"} =~ s/ selected//;
$form->{"select$item"} =~
s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
</tr>
| if $form->{selectdepartment};
- $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
+ my $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
- $customer =
- ($form->{selectcustomer})
- ? qq|<select name="customer"
-onchange="document.getElementById('update_button').click();">$form->{
-selectcustomer}</select>|
+ $customer = ($form->{selectcustomer})
+ ? qq|<select name="customer" onchange="document.getElementById('update_button').click();">$form->{selectcustomer}</select>|
: qq|<input name=customer value="$form->{customer}" size=35>|;
$employee = qq|
$amount = $locale->text('Amount');
$project = $locale->text('Project');
- for $i (1 .. $form->{rowcount}) {
+ for my $i (1 .. $form->{rowcount}) {
# format amounts
$form->{"amount_$i"} =
my $totalpaid = 0;
$form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
- for $i (1 .. $form->{paidaccounts}) {
+ for my $i (1 .. $form->{paidaccounts}) {
print "
<tr>
";
sub form_footer {
$lxdebug->enter_sub();
+ my ($transdate, $closedto);
+
print qq|
<input name=gldate type=hidden value="| . Q($form->{gldate}) . qq|">
my $display = shift;
+ my ($totaltax, $exchangerate, $totalpaid);
+
$form->{invtotal} = 0;
map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
qw(exchangerate creditlimit creditremaining);
- @flds = qw(amount AR_amount projectnumber oldprojectnumber project_id);
- $count = 0;
- @a = ();
+ my @flds = qw(amount AR_amount projectnumber oldprojectnumber project_id);
+ my $count = 0;
+ my @a = ();
- for $i (1 .. $form->{rowcount}) {
- $form->{"amount_$i"} =
- $form->parse_amount(\%myconfig, $form->{"amount_$i"});
+ for my $i (1 .. $form->{rowcount}) {
+ $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
$form->{"tax_$i"} = $form->parse_amount(\%myconfig, $form->{"tax_$i"});
if ($form->{"amount_$i"}) {
push @a, {};
- $j = $#a;
+ my $j = $#a;
if (!$form->{"korrektur_$i"}) {
- ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
+ my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
if ($taxkey > 1) {
if ($form->{taxincluded}) {
$form->{"tax_$i"} = $form->{"amount_$i"} / ($rate + 1) * $rate;
)));
$form->{invdate} = $form->{transdate};
- $save_AR = $form->{AR};
- &check_name(customer);
+ my $save_AR = $form->{AR};
+ check_name("customer");
$form->{AR} = $save_AR;
$form->{invtotal} =
($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
- for $i (1 .. $form->{paidaccounts}) {
+ for my $i (1 .. $form->{paidaccounts}) {
if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
map {
$form->{"${_}_$i"} =
$lxdebug->leave_sub();
}
+#
+# ToDO: fix $closedto and $invdate
+#
sub post_payment {
$lxdebug->enter_sub();
$form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
- for $i (1 .. $form->{paidaccounts}) {
+ for my $i (1 .. $form->{paidaccounts}) {
+
if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
- $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
+ my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
$form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
- $form->error($locale->text('Cannot post payment for a closed period!'))
- if ($datepaid <= $closedto);
+# $form->error($locale->text('Cannot post payment for a closed period!')) if ($datepaid <= $closedto);
if ($form->{currency} ne $form->{defaultcurrency}) {
- $form->{"exchangerate_$i"} = $form->{exchangerate}
- if ($invdate == $datepaid);
- $form->isblank("exchangerate_$i",
- $locale->text('Exchangerate for payment missing!'));
+# $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid);
+ $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
}
}
}
($form->{AR}) = split /--/, $form->{AR};
($form->{AR_paid}) = split /--/, $form->{AR_paid};
- $form->redirect($locale->text('Payment posted!'))
- if (AR->post_payment(\%myconfig, \%$form));
- $form->error($locale->text('Cannot post payment!'));
-
+ $form->redirect($locale->text('Payment posted!')) if (AR->post_payment(\%myconfig, \%$form));
+ $form->error($locale->text('Cannot post payment!'));
$lxdebug->leave_sub();
}
+sub _post {
+ # inline post
+ post(1);
+}
+
sub post {
$lxdebug->enter_sub();
+ my ($inline) = @_;
+
+ my ($datepaid);
+
# check if there is an invoice number, invoice and due date
$form->isblank("transdate", $locale->text('Invoice Date missing!'));
$form->isblank("duedate", $locale->text('Due Date missing!'));
$form->isblank("customer", $locale->text('Customer missing!'));
- $closedto = $form->datetonum($form->{closedto}, \%myconfig);
- $transdate = $form->datetonum($form->{transdate}, \%myconfig);
+ my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
+ my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
$form->error($locale->text('Cannot post transaction for a closed period!')) if ($transdate <= $closedto);
$form->error($locale->text('Zero amount posting!'))
delete($form->{AR});
- for $i (1 .. $form->{paidaccounts}) {
+ for my $i (1 .. $form->{paidaccounts}) {
if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
$datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
}
# if oldcustomer ne customer redo form
- ($customer) = split /--/, $form->{customer};
+ my ($customer) = split /--/, $form->{customer};
if ($form->{oldcustomer} ne "$customer--$form->{customer_id}") {
- &update;
+ update();
exit;
}
$form->{AR}{receivables} = $form->{ARselected};
$form->{storno} = 0;
+ $lxdebug->message(0, $form->{amount});
$form->{id} = 0 if $form->{postasnew};
+ $form->error($locale->text('Cannot post transaction!')) unless AR->post_transaction(\%myconfig, \%$form);
-
- if (AR->post_transaction(\%myconfig, \%$form)) {
- # saving the history
- if(!exists $form->{addition} && $form->{id} ne "") {
- $form->{snumbers} = "invnumber_$form->{invnumber}";
- $form->{addition} = "POSTED";
- $form->save_history($form->dbconnect(\%myconfig));
- }
- # /saving the history
- remove_draft() if $form->{remove_draft};
- $form->redirect($locale->text('Transaction posted!'));
+ # saving the history
+ if(!exists $form->{addition} && $form->{id} ne "") {
+ $form->{snumbers} = "invnumber_$form->{invnumber}";
+ $form->{addition} = "POSTED";
+ $form->save_history($form->dbconnect(\%myconfig));
}
- $form->error($locale->text('Cannot post transaction!'));
+ # /saving the history
+ remove_draft() if $form->{remove_draft};
+
+ $form->redirect($locale->text('Transaction posted!')) unless $inline;
$lxdebug->leave_sub();
}
<form method=post action=$form->{script}>
|;
- foreach $key (keys %$form) {
+ foreach my $key (keys %$form) {
$form->{$key} =~ s/\"/"/g;
print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
}
sub yes {
$lxdebug->enter_sub();
- if (AR->delete_transaction(\%myconfig, \%$form, $spool)) {
+ if (AR->delete_transaction(\%myconfig, \%$form)) {
# saving the history
if(!exists $form->{addition}) {
$form->{snumbers} = qq|invnumber_| . $form->{invnumber};
sub search {
$lxdebug->enter_sub();
+ my ($customer, $department);
+ my ($jsscript, $button1, $button2, $onload);
+
# setup customer selection
$form->all_vc(\%myconfig, "customer", "AR");
sub ar_transactions {
$lxdebug->enter_sub();
+ my ($callback, $href, @columns);
+
+ $form->{customer} = $form->unescape($form->{customer});
($form->{customer}, $form->{customer_id}) = split(/--/, $form->{customer});
$form->{sort} ||= 'transdate';
my $report = SL::ReportGenerator->new(\%myconfig, $form);
- my @columns =
+ @columns =
qw(transdate id type invnumber ordnumber name netamount tax amount paid
datepaid due duedate transaction_description notes employee shippingpoint shipvia
globalprojectnumber);
my @hidden_variables = map { "l_${_}" } @columns;
push @hidden_variables, "l_subtotal", qw(open closed customer invnumber ordnumber transaction_description notes project_id transdatefrom transdateto);
- my $href = build_std_url('action=ar_transactions', grep { $form->{$_} } @hidden_variables);
+ $href = build_std_url('action=ar_transactions', grep { $form->{$_} } @hidden_variables);
my %column_defs = (
'transdate' => { 'text' => $locale->text('Date'), },
push @options, $locale->text('Customer') . " : $form->{customer}";
}
if ($form->{department}) {
- ($department) = split /--/, $form->{department};
+ my ($department) = split /--/, $form->{department};
push @options, $locale->text('Department') . " : $department";
}
if ($form->{invnumber}) {
my $idx = 0;
- foreach $ar (@{ $form->{AR} }) {
+ foreach my $ar (@{ $form->{AR} }) {
$ar->{tax} = $ar->{amount} - $ar->{netamount};
$ar->{due} = $ar->{amount} - $ar->{paid};
sub storno {
$lxdebug->enter_sub();
+ # don't cancel cancelled transactions
if (IS->has_storno(\%myconfig, $form, 'ar')) {
$form->{title} = $locale->text("Cancel Accounts Receivables Transaction");
$form->error($locale->text("Transaction has already been cancelled!"));
}
- # negate amount/taxes
- for my $i (1 .. $form->{rowcount}) {
- $form->{"amount_$i"} *= -1;
- $form->{"tax_$i"} *= -1;
- }
-
- # format things
- for my $i (1 .. $form->{rowcount}) {
- for (qw(amount tax)) {
- $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) if $form->{"${_}_$i"};
- }
- }
-
- $form->{storno} = 1;
- $form->{storno_id} = $form->{id};
- $form->{id} = 0;
-
- $form->{invnumber} = "Storno-" . $form->{invnumber};
-
- post();
+ AR->storno($form, \%myconfig, $form->{id});
# saving the history
if(!exists $form->{addition} && $form->{id} ne "") {
}
# /saving the history
+ $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
+
$lxdebug->leave_sub();
}