#=====================================================================
# LedgerSMB Small Medium Business Accounting
# Copyright (c) 2001
#
# Author: DWS Systems Inc.
# Web: http://sourceforge.net/projects/ledger-smb/
#
#
# 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.
#======================================================================
#
# Inventory invoicing module
#
#======================================================================
use SL::IS;
use SL::PE;
require "$form->{path}/arap.pl";
require "$form->{path}/io.pl";
1;
# end of main
sub add {
$form->{title} = $locale->text('Add Sales Invoice');
$form->{callback} = "$form->{script}?action=add&type=$form->{type}&login=$form->{login}&path=$form->{path}&sessionid=$form->{sessionid}" unless $form->{callback};
&invoice_links;
&prepare_invoice;
&display_form;
}
sub edit {
$form->{title} = $locale->text('Edit Sales Invoice');
&invoice_links;
&prepare_invoice;
&display_form;
}
sub invoice_links {
$form->{vc} = "customer";
$form->{type} = "invoice";
# create links
$form->create_links("AR", \%myconfig, "customer", 1);
# currencies
@curr = split /:/, $form->{currencies};
$form->{defaultcurrency} = $curr[0];
chomp $form->{defaultcurrency};
for (@curr) { $form->{selectcurrency} .= "$_\n" }
if (@{ $form->{all_customer} }) {
unless ($form->{customer_id}) {
$form->{customer_id} = $form->{all_customer}->[0]->{id};
}
}
AA->get_name(\%myconfig, \%$form);
delete $form->{notes};
IS->retrieve_invoice(\%myconfig, \%$form);
$form->{oldlanguage_code} = $form->{language_code};
$form->get_partsgroup(\%myconfig, { language_code => $form->{language_code}, searchitems => 'nolabor' });
if (@{ $form->{all_partsgroup} }) {
$form->{selectpartsgroup} = " \n";
foreach $ref (@ { $form->{all_partsgroup} }) {
if ($ref->{translation}) {
$form->{selectpartsgroup} .= qq| $ref->{translation}\n|;
} else {
$form->{selectpartsgroup} .= qq| $ref->{partsgroup}\n|;
}
}
}
if (@{ $form->{all_project} }) {
$form->{selectprojectnumber} = " \n";
for (@{ $form->{all_project} }) { $form->{selectprojectnumber} .= qq| $_->{projectnumber}\n| }
}
$form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
$form->{oldtransdate} = $form->{transdate};
$form->{selectcustomer} = "";
if (@{ $form->{all_customer} }) {
$form->{customer} = "$form->{customer}--$form->{customer_id}";
for (@{ $form->{all_customer} }) { $form->{selectcustomer} .= 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| }
}
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;
foreach $key (keys %{ $form->{AR_links} }) {
$form->{"select$key"} = "";
foreach $ref (@{ $form->{AR_links}{$key} }) {
$form->{"select$key"} .= " $ref->{accno}--$ref->{description}\n";
}
if ($key eq "AR_paid") {
for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
$form->{"AR_paid_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
# reverse paid
$form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{amount} * -1;
$form->{"datepaid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{transdate};
$form->{"forex_$i"} = $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i-1]->{exchangerate};
$form->{"source_$i"} = $form->{acc_trans}{$key}->[$i-1]->{source};
$form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i-1]->{memo};
$form->{"cleared_$i"} = $form->{acc_trans}{$key}->[$i-1]->{cleared};
$form->{paidaccounts} = $i;
}
} else {
$form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}" if $form->{acc_trans}{$key}->[0]->{accno};
}
}
for (qw(AR_links acc_trans)) { delete $form->{$_} }
$form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
$form->{AR} = $form->{AR_1} unless $form->{id};
$form->{locked} = ($form->{revtrans}) ? '1' : ($form->datetonum(\%myconfig, $form->{transdate}) <= $form->datetonum(\%myconfig, $form->{closedto}));
if (! $form->{readonly}) {
$form->{readonly} = 1 if $myconfig{acs} =~ /AR--Sales Invoice/;
}
}
sub prepare_invoice {
$form->{type} = "invoice";
$form->{formname} = "invoice";
$form->{sortby} ||= "runningnumber";
$form->{format} = "postscript" if $myconfig{printer};
$form->{media} = $myconfig{printer};
$form->{selectformname} = qq| |.$locale->text('Invoice').qq|
|.$locale->text('Pick List').qq|
|.$locale->text('Packing List');
$i = 0;
$form->{currency} =~ s/ //g;
$form->{oldcurrency} = $form->{currency};
if ($form->{id}) {
for (qw(invnumber ordnumber ponumber quonumber shippingpoint shipvia notes intnotes)) { $form->{$_} = $form->quote($form->{$_}) }
foreach $ref (@{ $form->{invoice_details} } ) {
$i++;
for (keys %$ref) { $form->{"${_}_$i"} = $ref->{$_} }
$form->{"projectnumber_$i"} = qq|$ref->{projectnumber}--$ref->{project_id}| if $ref->{project_id};
$form->{"partsgroup_$i"} = qq|$ref->{partsgroup}--$ref->{partsgroup_id}| if $ref->{partsgroup_id};
$form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
$dec = length $dec;
$decimalplaces = ($dec > 2) ? $dec : 2;
$form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
$form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
$form->{"oldqty_$i"} = $form->{"qty_$i"};
for (qw(partnumber sku description unit)) { $form->{"${_}_$i"} = $form->quote($form->{"${_}_$i"}) }
$form->{rowcount} = $i;
}
}
}
sub form_header {
# set option selected
for (qw(AR currency)) {
$form->{"select$_"} =~ s/ selected//;
$form->{"select$_"} =~ s/option>\Q$form->{$_}\E/option selected>$form->{$_}/;
}
for (qw(customer department employee)) {
$form->{"select$_"} = $form->unescape($form->{"select$_"});
$form->{"select$_"} =~ s/ selected//;
$form->{"select$_"} =~ s/( {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| $form->{exchangerate} {exchangerate}> |;
} else {
$exchangerate .= qq||.$locale->text('Exchange Rate').qq| {exchangerate}> |;
}
}
$exchangerate .= qq|
{forex}>
|;
if ($form->{selectcustomer}) {
$customer = qq|$form->{selectcustomer}
|;
} else {
$customer = qq| |;
}
$department = qq|
|.$locale->text('Department').qq|
$form->{selectdepartment}
| if $form->{selectdepartment};
$n = ($form->{creditremaining} < 0) ? "0" : "1";
if ($form->{business}) {
$business = qq|
|.$locale->text('Business').qq|
$form->{business}
|.$locale->text('Trade Discount').qq|
|.$form->format_amount(\%myconfig, $form->{tradediscount} * 100).qq| %
|;
}
$employee = qq|
|;
$employee = qq|
|.$locale->text('Salesperson').qq|
$form->{selectemployee}
| if $form->{selectemployee};
$i = $form->{rowcount} + 1;
$focus = "partnumber_$i";
$form->header;
print qq|