#=====================================================================
# LedgerSMB Small Medium Business Accounting
# Copyright (c) 2005
#
# Author: DWS Systems Inc.
# Web: http://sourceforge.net/projects/ledger-smb/
#
# Contributors:
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#======================================================================
#
# AR / AP
#
#======================================================================
# any custom scripts for this one
if (-f "$form->{path}/custom_aa.pl") {
eval { require "$form->{path}/custom_aa.pl"; };
}
if (-f "$form->{path}/$form->{login}_aa.pl") {
eval { require "$form->{path}/$form->{login}_aa.pl"; };
}
1;
# end of main
# this is for our long dates
# $locale->text('January')
# $locale->text('February')
# $locale->text('March')
# $locale->text('April')
# $locale->text('May ')
# $locale->text('June')
# $locale->text('July')
# $locale->text('August')
# $locale->text('September')
# $locale->text('October')
# $locale->text('November')
# $locale->text('December')
# this is for our short month
# $locale->text('Jan')
# $locale->text('Feb')
# $locale->text('Mar')
# $locale->text('Apr')
# $locale->text('May')
# $locale->text('Jun')
# $locale->text('Jul')
# $locale->text('Aug')
# $locale->text('Sep')
# $locale->text('Oct')
# $locale->text('Nov')
# $locale->text('Dec')
sub add {
$form->{title} = "Add";
$form->{callback} = "$form->{script}?action=add&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}" unless $form->{callback};
&create_links;
$form->{focus} = "amount_1";
&display_form;
}
sub edit {
$form->{title} = "Edit";
&create_links;
&display_form;
}
sub display_form {
&form_header;
&form_footer;
}
sub create_links {
$form->create_links($form->{ARAP}, \%myconfig, $form->{vc});
$duedate = $form->{duedate};
$taxincluded = $form->{taxincluded};
$form->{formname} = "transaction";
$form->{format} = "postscript" if $myconfig{printer};
$form->{media} = $myconfig{printer};
$form->{selectformname} = qq||.$locale->text('Transaction');
if ($latex) {
if ($form->{ARAP} eq 'AR') {
$form->{selectformname} .= qq|
|.$locale->text('Receipt');
} else {
$form->{selectformname} .= qq|
|.$locale->text('Check');
}
}
# currencies
@curr = split /:/, $form->{currencies};
$form->{defaultcurrency} = $curr[0];
chomp $form->{defaultcurrency};
for (@curr) { $form->{selectcurrency} .= " $_\n" }
AA->get_name(\%myconfig, \%$form);
$form->{currency} =~ s/ //g;
$form->{duedate} = $duedate if $duedate;
$form->{taxincluded} = $taxincluded if $form->{id};
$form->{notes} = $form->{intnotes} if !$form->{id};
$form->{"old$form->{vc}"} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
$form->{oldtransdate} = $form->{transdate};
# customers/vendors
$form->{"select$form->{vc}"} = "";
if (@{ $form->{"all_$form->{vc}"} }) {
$form->{$form->{vc}} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
for (@{ $form->{"all_$form->{vc}"} }) { $form->{"select$form->{vc}"} .= qq| $_->{name}\n| }
}
# departments
if (@{ $form->{all_department} }) {
$form->{selectdepartment} = " \n";
$form->{department} = "$form->{department}--$form->{department_id}" if $form->{department_id};
for (@{ $form->{all_department} }) { $form->{selectdepartment} .= qq| $_->{description}\n| }
}
$form->{employee} = "$form->{employee}--$form->{employee_id}";
# sales staff
if (@{ $form->{all_employee} }) {
$form->{selectemployee} = "";
for (@{ $form->{all_employee} }) { $form->{selectemployee} .= qq| $_->{name}\n| }
}
# projects
if (@{ $form->{all_project} }) {
$form->{selectprojectnumber} = " \n";
for (@{ $form->{all_project} }) { $form->{selectprojectnumber} .= qq| $_->{projectnumber}\n| }
}
if (@{ $form->{all_language} }) {
$form->{selectlanguage} = " \n";
for (@{ $form->{all_language} }) { $form->{selectlanguage} .= qq| $_->{description}\n| }
}
# forex
$form->{forex} = $form->{exchangerate};
$exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
$netamount = 0;
$tax = 0;
$taxrate = 0;
$ml = ($form->{ARAP} eq 'AR') ? 1 : -1;
foreach $key (keys %{ $form->{"$form->{ARAP}_links"} }) {
$form->{"select$key"} = "";
foreach $ref (@{ $form->{"$form->{ARAP}_links"}{$key} }) {
if ($key eq "$form->{ARAP}_tax") {
$form->{"select$form->{ARAP}_tax_$ref->{accno}"} = " $ref->{accno}--$ref->{description}\n";
next;
}
$form->{"select$key"} .= " $ref->{accno}--$ref->{description}\n";
}
# if there is a value we have an old entry
for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
if ($key eq "$form->{ARAP}_paid") {
$form->{"$form->{ARAP}_paid_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
$form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{amount} * -1 * $ml;
$form->{"datepaid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{transdate};
$form->{"source_$i"} = $form->{acc_trans}{$key}->[$i-1]->{source};
$form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i-1]->{memo};
$form->{"forex_$i"} = $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i-1]->{exchangerate};
$form->{paidaccounts}++;
} else {
$akey = $key;
$akey =~ s/$form->{ARAP}_//;
if ($key eq "$form->{ARAP}_tax") {
$form->{"${key}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
$form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = $form->{acc_trans}{$key}->[$i-1]->{amount} * $ml;
$tax += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
$taxrate += $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"};
} else {
$form->{"${akey}_$i"} = $form->{acc_trans}{$key}->[$i-1]->{amount} * $ml;
if ($akey eq 'amount') {
$form->{"description_$i"} = $form->{acc_trans}{$key}->[$i-1]->{memo};
$form->{rowcount}++;
$netamount += $form->{"${akey}_$i"};
$form->{"projectnumber_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{projectnumber}--$form->{acc_trans}{$key}->[$i-1]->{project_id}" if $form->{acc_trans}{$key}->[$i-1]->{project_id};
} else {
$form->{invtotal} = $form->{acc_trans}{$key}->[$i-1]->{amount} * -1 * $ml;
}
$form->{"${key}_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
}
}
}
}
$form->{paidaccounts} = 1 if not defined $form->{paidaccounts};
if ($form->{taxincluded}) {
$diff = 0;
# add tax to individual amounts
for $i (1 .. $form->{rowcount}) {
if ($netamount) {
$amount = $form->{"amount_$i"} * (1 + $tax / $netamount);
$form->{"amount_$i"} = $form->round_amount($amount, 2);
}
}
}
$form->{invtotal} = $netamount + $tax;
# check if calculated is equal to stored
# taxincluded is terrible to calculate
# this works only if all taxes are checked
@taxaccounts = split / /, $form->{taxaccounts};
if ($form->{id}) {
if ($form->{taxincluded}) {
$ml = 1;
for (0 .. 1) {
$taxrate = 0;
$diff = 0;
for (@taxaccounts) { $taxrate += $form->{"${_}_rate"} if ($form->{"${_}_rate"} * $ml) > 0 }
$taxrate *= $ml;
foreach $item (@taxaccounts) {
if (($form->{"${item}_rate"} * $ml) > 0) {
if ($taxrate) {
$amount = $form->{invtotal} * $form->{"${item}_rate"} / (1 + $taxrate);
$tax = $form->round_amount($amount, 2);
$tax{$item} = $form->round_amount($amount - $diff, 2);
$diff = $tax{$item} - ($amount - $diff);
if ($tax) {
if ($form->{"tax_$item"} == $tax{$item}) {
$form->{"calctax_$item"} = 1;
}
}
}
}
}
$ml *= -1;
}
} else {
for (@taxaccounts) {
$tax = $form->round_amount($netamount * $form->{"${_}_rate"}, 2);
if ($tax) {
if ($form->{"tax_$_"} == $tax) {
$form->{"calctax_$_"} = 1;
}
}
}
}
} else {
for (@taxaccounts) { $form->{"calctax_$_"} = 1 }
}
$form->{rowcount}++ if ($form->{id} || !$form->{rowcount});
$form->{$form->{ARAP}} = $form->{"$form->{ARAP}_1"};
$form->{rowcount} = 1 unless $form->{"$form->{ARAP}_amount_1"};
$form->{locked} = ($form->{revtrans}) ? '1' : ($form->datetonum(\%myconfig, $form->{transdate}) <= $form->datetonum(\%myconfig, $form->{closedto}));
# readonly
if (! $form->{readonly}) {
$form->{readonly} = 1 if $myconfig{acs} =~ /$form->{ARAP}--Add Transaction/;
}
}
sub form_header {
$title = $form->{title};
$form->{title} = $locale->text("$title $form->{ARAP} Transaction");
$form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
# $locale->text('Add AR Transaction')
# $locale->text('Edit AR Transaction')
# $locale->text('Add AP Transaction')
# $locale->text('Edit AP Transaction')
# set option selected
for ("$form->{ARAP}", "currency") {
$form->{"select$_"} =~ s/ selected//;
$form->{"select$_"} =~ s/ \Q$form->{$_}\E/ $form->{$_}/;
}
for ("$form->{vc}", "department", "employee", "formname") {
$form->{"select$_"} = $form->unescape($form->{"select$_"});
$form->{"select$_"} =~ s/ selected//;
$form->{"select$_"} =~ s/( {selectprojectnumber} = $form->unescape($form->{selectprojectnumber});
# format amounts
$form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
$exchangerate = qq||;
$exchangerate .= qq|
|.$locale->text('Currency').qq|
$form->{selectcurrency} | if $form->{defaultcurrency};
$exchangerate .= qq|
{defaultcurrency}>
|;
if ($form->{defaultcurrency} && $form->{currency} ne $form->{defaultcurrency}) {
if ($form->{forex}) {
$exchangerate .= qq|
|.$locale->text('Exchange Rate').qq|
{exchangerate}>$form->{exchangerate}
|;
} else {
$exchangerate .= qq|
|.$locale->text('Exchange Rate').qq|
{exchangerate}>
|;
}
}
$exchangerate .= qq|
{forex}>
|;
$taxincluded = "";
if ($form->{taxaccounts}) {
$taxincluded = qq|
{taxincluded}>
|.$locale->text('Tax Included').qq|
|;
}
if (($rows = $form->numtextrows($form->{notes}, 50) - 1) < 2) {
$rows = 2;
}
$notes = qq||;
$department = qq|
|.$locale->text('Department').qq|
$form->{selectdepartment}
| if $form->{selectdepartment};
$n = ($form->{creditremaining} < 0) ? "0" : "1";
$name = ($form->{"select$form->{vc}"}) ? qq|$form->{"select$form->{vc}"} | : qq| |;
$employee = qq|
|;
if ($form->{selectemployee}) {
$label = ($form->{ARAP} eq 'AR') ? $locale->text('Salesperson') : $locale->text('Employee');
$employee = qq|
$label
$form->{selectemployee}
|;
}
$focus = ($form->{focus}) ? $form->{focus} : "amount_$form->{rowcount}";
$form->header;
print qq|