kivitendo/SL/IS.pm @ 728729b6
d319704a | Moritz Bunkus | #=====================================================================
|
||
# LX-Office ERP
|
||||
# Copyright (C) 2004
|
||||
# Based on SQL-Ledger Version 2.1.9
|
||||
# Web http://www.lx-office.org
|
||||
#
|
||||
#=====================================================================
|
||||
# SQL-Ledger Accounting
|
||||
# Copyright (C) 1998-2002
|
||||
#
|
||||
# Author: Dieter Simader
|
||||
# Email: dsimader@sql-ledger.org
|
||||
# Web: http://www.sql-ledger.org
|
||||
#
|
||||
# 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.
|
||||
#======================================================================
|
||||
#
|
||||
# Inventory invoicing module
|
||||
#
|
||||
#======================================================================
|
||||
package IS;
|
||||
54e4131e | Moritz Bunkus | use SL::AM;
|
||
8e206587 | Moritz Bunkus | use SL::Common;
|
||
5b47ed3e | Moritz Bunkus | use SL::DBUtils;
|
||
5fdc44cb | Moritz Bunkus | use SL::MoreCommon;
|
||
07d71c33 | Stephan Köhler | |||
d319704a | Moritz Bunkus | sub invoice_details {
|
||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form, $locale) = @_;
|
||||
532b0d4c | Moritz Bunkus | $form->{duedate} ||= $form->{invdate};
|
||
d319704a | Moritz Bunkus | |||
# connect to database
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
532b0d4c | Moritz Bunkus | my $sth;
|
||
d319704a | Moritz Bunkus | |||
532b0d4c | Moritz Bunkus | my $query = qq|SELECT date | . conv_dateq($form->{duedate}) . qq| - date | . conv_dateq($form->{invdate}) . qq| AS terms|;
|
||
($form->{terms}) = selectrow_query($form, $dbh, $query);
|
||||
d319704a | Moritz Bunkus | |||
96d10ecc | Moritz Bunkus | my (@project_ids, %projectnumbers);
|
||
push(@project_ids, $form->{"globalproject_id"}) if ($form->{"globalproject_id"});
|
||||
# sort items by partsgroup
|
||||
for $i (1 .. $form->{rowcount}) {
|
||||
$partsgroup = "";
|
||||
if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
|
||||
$partsgroup = $form->{"partsgroup_$i"};
|
||||
}
|
||||
push @partsgroup, [$i, $partsgroup];
|
||||
push(@project_ids, $form->{"project_id_$i"}) if ($form->{"project_id_$i"});
|
||||
}
|
||||
if (@project_ids) {
|
||||
$query = "SELECT id, projectnumber FROM project WHERE id IN (" .
|
||||
join(", ", map({ "?" } @project_ids)) . ")";
|
||||
$sth = $dbh->prepare($query);
|
||||
$sth->execute(@project_ids) ||
|
||||
$form->dberror($query . " (" . join(", ", @project_ids) . ")");
|
||||
while (my $ref = $sth->fetchrow_hashref()) {
|
||||
$projectnumbers{$ref->{id}} = $ref->{projectnumber};
|
||||
}
|
||||
$sth->finish();
|
||||
}
|
||||
$form->{"globalprojectnumber"} =
|
||||
$projectnumbers{$form->{"globalproject_id"}};
|
||||
d319704a | Moritz Bunkus | my $tax = 0;
|
||
my $item;
|
||||
my $i;
|
||||
my @partsgroup = ();
|
||||
my $partsgroup;
|
||||
my %oid = ('Pg' => 'oid',
|
||||
'Oracle' => 'rowid');
|
||||
# sort items by partsgroup
|
||||
for $i (1 .. $form->{rowcount}) {
|
||||
$partsgroup = "";
|
||||
if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
|
||||
$partsgroup = $form->{"partsgroup_$i"};
|
||||
}
|
||||
push @partsgroup, [$i, $partsgroup];
|
||||
}
|
||||
my $sameitem = "";
|
||||
my @taxaccounts;
|
||||
my %taxaccounts;
|
||||
my %taxbase;
|
||||
my $taxrate;
|
||||
my $taxamount;
|
||||
my $taxbase;
|
||||
my $taxdiff;
|
||||
54e4131e | Moritz Bunkus | my $nodiscount;
|
||
my $yesdiscount;
|
||||
my $nodiscount_subtotal = 0;
|
||||
my $discount_subtotal = 0;
|
||||
my $position = 0;
|
||||
my $subtotal_header = 0;
|
||||
my $subposition = 0;
|
||||
96d10ecc | Moritz Bunkus | my @arrays =
|
||
qw(runningnumber number description longdescription qty ship unit bin
|
||||
deliverydate_oe ordnumber_oe transdate_oe licensenumber validuntil
|
||||
partnotes serialnumber reqdate sellprice listprice netprice
|
||||
discount p_discount discount_sub nodiscount_sub
|
||||
linetotal nodiscount_linetotal tax_rate projectnumber);
|
||||
my @tax_arrays =
|
||||
qw(taxbase tax taxdescription taxrate taxnumber);
|
||||
d319704a | Moritz Bunkus | foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
|
||
$i = $item->[0];
|
||||
if ($item->[1] ne $sameitem) {
|
||||
push(@{ $form->{description} }, qq|$item->[1]|);
|
||||
$sameitem = $item->[1];
|
||||
96d10ecc | Moritz Bunkus | map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
|
||
d319704a | Moritz Bunkus | }
|
||
$form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
|
||||
8c89bb02 | Moritz Bunkus | if ($form->{"id_$i"} != 0) {
|
||
d319704a | Moritz Bunkus | |||
54e4131e | Moritz Bunkus | # add number, description and qty to $form->{number},
|
||
if ($form->{"subtotal_$i"} && !$subtotal_header) {
|
||||
$subtotal_header = $i;
|
||||
$position = int($position);
|
||||
$subposition = 0;
|
||||
$position++;
|
||||
} elsif ($subtotal_header) {
|
||||
$subposition += 1;
|
||||
$position = int($position);
|
||||
$position = $position.".".$subposition;
|
||||
} else {
|
||||
$position = int($position);
|
||||
$position++;
|
||||
}
|
||||
push(@{ $form->{runningnumber} }, $position);
|
||||
d319704a | Moritz Bunkus | push(@{ $form->{number} }, qq|$form->{"partnumber_$i"}|);
|
||
push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
|
||||
push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
|
||||
push(@{ $form->{"partnotes"} }, qq|$form->{"partnotes_$i"}|);
|
||||
push(@{ $form->{description} }, qq|$form->{"description_$i"}|);
|
||||
54e4131e | Moritz Bunkus | push(@{ $form->{longdescription} }, qq|$form->{"longdescription_$i"}|);
|
||
d319704a | Moritz Bunkus | push(@{ $form->{qty} },
|
||
$form->format_amount($myconfig, $form->{"qty_$i"}));
|
||||
081a4f97 | Moritz Bunkus | push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
|
||
56eb3444 | Stephan Köhler | push(@{ $form->{deliverydate_oe} }, qq|$form->{"deliverydate_$i"}|);
|
||
d319704a | Moritz Bunkus | |||
081a4f97 | Moritz Bunkus | push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
|
||
5cbbffe2 | Stephan Köhler | push(@{ $form->{ordnumber_oe} }, qq|$form->{"ordnumber_$i"}|);
|
||
push(@{ $form->{transdate_oe} }, qq|$form->{"transdate_$i"}|);
|
||||
54e4131e | Moritz Bunkus | push(@{ $form->{invnumber} }, qq|$form->{"invnumber"}|);
|
||
push(@{ $form->{invdate} }, qq|$form->{"invdate"}|);
|
||||
d319704a | Moritz Bunkus | |||
if ($form->{lizenzen}) {
|
||||
if ($form->{"licensenumber_$i"}) {
|
||||
532b0d4c | Moritz Bunkus | $query = qq|SELECT licensenumber, validuntil FROM license WHERE id = ?|;
|
||
($licensenumber, $validuntil) = selectrow_query($form, $dbh, $query, conv_i($form->{"licensenumber_$i"}));
|
||||
d319704a | Moritz Bunkus | push(@{ $form->{licensenumber} }, $licensenumber);
|
||
532b0d4c | Moritz Bunkus | push(@{ $form->{validuntil} }, $locale->date($myconfig, $validuntil, 0));
|
||
d319704a | Moritz Bunkus | } else {
|
||
push(@{ $form->{licensenumber} }, "");
|
||||
push(@{ $form->{validuntil} }, "");
|
||||
}
|
||||
}
|
||||
# listprice
|
||||
push(@{ $form->{listprice} }, $form->{"listprice_$i"});
|
||||
my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
|
||||
my ($dec) = ($sellprice =~ /\.(\d+)/);
|
||||
$dec = length $dec;
|
||||
my $decimalplaces = ($dec > 2) ? $dec : 2;
|
||||
081a4f97 | Moritz Bunkus | my $i_discount =
|
||
$form->round_amount(
|
||||
$sellprice * $form->parse_amount($myconfig,
|
||||
$form->{"discount_$i"}) / 100,
|
||||
$decimalplaces);
|
||||
a26dbea9 | Stephan Köhler | |||
081a4f97 | Moritz Bunkus | my $discount =
|
||
$form->round_amount($form->{"qty_$i"} * $i_discount, $decimalplaces);
|
||||
d319704a | Moritz Bunkus | |||
# keep a netprice as well, (sellprice - discount)
|
||||
a26dbea9 | Stephan Köhler | $form->{"netprice_$i"} = $sellprice - $i_discount;
|
||
d319704a | Moritz Bunkus | push(@{ $form->{netprice} },
|
||
($form->{"netprice_$i"} != 0)
|
||||
? $form->format_amount(
|
||||
$myconfig, $form->{"netprice_$i"},
|
||||
$decimalplaces
|
||||
)
|
||||
: " ");
|
||||
my $linetotal =
|
||||
$form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
|
||||
54e4131e | Moritz Bunkus | my $nodiscount_linetotal =
|
||
$form->round_amount($form->{"qty_$i"} * $sellprice, 2);
|
||||
d319704a | Moritz Bunkus | $discount =
|
||
($discount != 0)
|
||||
? $form->format_amount($myconfig, $discount * -1, $decimalplaces)
|
||||
: " ";
|
||||
$linetotal = ($linetotal != 0) ? $linetotal : " ";
|
||||
081a4f97 | Moritz Bunkus | push(@{ $form->{discount} }, $discount);
|
||
ecbb5ab1 | Stephan Köhler | push(@{ $form->{p_discount} }, $form->{"discount_$i"});
|
||
54e4131e | Moritz Bunkus | if (($form->{"discount_$i"} ne "") && ($form->{"discount_$i"} != 0)) {
|
||
$form->{discount_p} = $form->{"discount_$i"};
|
||||
}
|
||||
d319704a | Moritz Bunkus | $form->{total} += $linetotal;
|
||
54e4131e | Moritz Bunkus | $discount_subtotal += $linetotal;
|
||
$form->{nodiscount_total} += $nodiscount_linetotal;
|
||||
$nodiscount_subtotal += $nodiscount_linetotal;
|
||||
$form->{discount_total} += $form->parse_amount($myconfig, $discount);
|
||||
if ($form->{"subtotal_$i"} && $subtotal_header && ($subtotal_header != $i)) {
|
||||
$discount_subtotal = $form->format_amount($myconfig, $discount_subtotal, 2);
|
||||
push(@{ $form->{discount_sub} }, $discount_subtotal);
|
||||
$nodiscount_subtotal = $form->format_amount($myconfig, $nodiscount_subtotal, 2);
|
||||
push(@{ $form->{nodiscount_sub} }, $nodiscount_subtotal);
|
||||
$discount_subtotal = 0;
|
||||
$nodiscount_subtotal = 0;
|
||||
$subtotal_header = 0;
|
||||
} else {
|
||||
push(@{ $form->{discount_sub} }, "");
|
||||
push(@{ $form->{nodiscount_sub} }, "");
|
||||
}
|
||||
if ($linetotal == $netto_linetotal) {
|
||||
$nodiscount += $linetotal;
|
||||
}
|
||||
d319704a | Moritz Bunkus | |||
push(@{ $form->{linetotal} },
|
||||
$form->format_amount($myconfig, $linetotal, 2));
|
||||
54e4131e | Moritz Bunkus | push(@{ $form->{nodiscount_linetotal} },
|
||
$form->format_amount($myconfig, $nodiscount_linetotal, 2));
|
||||
96d10ecc | Moritz Bunkus | push(@{ $form->{projectnumber} }, $projectnumbers{$form->{"project_id_$i"}});
|
||
d319704a | Moritz Bunkus | |||
532b0d4c | Moritz Bunkus | @taxaccounts = split(/ /, $form->{"taxaccounts_$i"});
|
||
d319704a | Moritz Bunkus | $taxrate = 0;
|
||
$taxdiff = 0;
|
||||
map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
|
||||
if ($form->{taxincluded}) {
|
||||
# calculate tax
|
||||
$taxamount = $linetotal * $taxrate / (1 + $taxrate);
|
||||
$taxbase = $linetotal - $taxamount;
|
||||
} else {
|
||||
$taxamount = $linetotal * $taxrate;
|
||||
$taxbase = $linetotal;
|
||||
}
|
||||
if ($form->round_amount($taxrate, 7) == 0) {
|
||||
if ($form->{taxincluded}) {
|
||||
foreach $item (@taxaccounts) {
|
||||
$taxamount =
|
||||
$form->round_amount($linetotal * $form->{"${item}_rate"} /
|
||||
(1 + abs($form->{"${item}_rate"})),
|
||||
2);
|
||||
$taxaccounts{$item} += $taxamount;
|
||||
$taxdiff += $taxamount;
|
||||
$taxbase{$item} += $taxbase;
|
||||
}
|
||||
$taxaccounts{ $taxaccounts[0] } += $taxdiff;
|
||||
} else {
|
||||
foreach $item (@taxaccounts) {
|
||||
$taxaccounts{$item} += $linetotal * $form->{"${item}_rate"};
|
||||
$taxbase{$item} += $taxbase;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
foreach $item (@taxaccounts) {
|
||||
$taxaccounts{$item} +=
|
||||
$taxamount * $form->{"${item}_rate"} / $taxrate;
|
||||
$taxbase{$item} += $taxbase;
|
||||
}
|
||||
}
|
||||
0bb0eb67 | Stephan Köhler | $tax_rate = $taxrate * 100;
|
||
eb8a578d | Stephan Köhler | push(@{ $form->{tax_rate} }, qq|$tax_rate|);
|
||
d319704a | Moritz Bunkus | if ($form->{"assembly_$i"}) {
|
||
$sameitem = "";
|
||||
# get parts and push them onto the stack
|
||||
my $sortorder = "";
|
||||
if ($form->{groupitems}) {
|
||||
$sortorder =
|
||||
qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
|
||||
} else {
|
||||
$sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
|
||||
}
|
||||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|SELECT p.partnumber, p.description, p.unit, a.qty, pg.partsgroup
|
||||
FROM assembly a
|
||||
JOIN parts p ON (a.parts_id = p.id)
|
||||
LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
|
||||
WHERE (a.bom = '1') AND (a.id = ?) $sortorder|;
|
||||
$sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{"id_$i"}));
|
||||
d319704a | Moritz Bunkus | |||
while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
|
||||
if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
|
||||
96d10ecc | Moritz Bunkus | map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
|
||
d319704a | Moritz Bunkus | $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
|
||
push(@{ $form->{description} }, $sameitem);
|
||||
}
|
||||
map { $form->{"a_$_"} = $ref->{$_} } qw(partnumber description);
|
||||
push(@{ $form->{description} },
|
||||
$form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}
|
||||
)
|
||||
. qq| -- $form->{"a_partnumber"}, $form->{"a_description"}|);
|
||||
96d10ecc | Moritz Bunkus | map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
|
||
d319704a | Moritz Bunkus | |||
}
|
||||
$sth->finish;
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach my $item (sort keys %taxaccounts) {
|
||||
54e4131e | Moritz Bunkus | push(@{ $form->{taxbase} },
|
||
$form->format_amount($myconfig, $taxbase{$item}, 2));
|
||||
d319704a | Moritz Bunkus | |||
54e4131e | Moritz Bunkus | $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
|
||
d319704a | Moritz Bunkus | |||
54e4131e | Moritz Bunkus | push(@{ $form->{tax} }, $form->format_amount($myconfig, $taxamount, 2));
|
||
5e59cc54 | Udo Spallek | push(@{ $form->{taxdescription} }, $form->{"${item}_description"} . q{ } . 100 * $form->{"${item}_rate"} . q{%});
|
||
54e4131e | Moritz Bunkus | push(@{ $form->{taxrate} },
|
||
$form->format_amount($myconfig, $form->{"${item}_rate"} * 100));
|
||||
push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
|
||||
d319704a | Moritz Bunkus | }
|
||
for my $i (1 .. $form->{paidaccounts}) {
|
||||
if ($form->{"paid_$i"}) {
|
||||
push(@{ $form->{payment} }, $form->{"paid_$i"});
|
||||
532b0d4c | Moritz Bunkus | my ($accno, $description) = split(/--/, $form->{"AR_paid_$i"});
|
||
d319704a | Moritz Bunkus | push(@{ $form->{paymentaccount} }, $description);
|
||
push(@{ $form->{paymentdate} }, $form->{"datepaid_$i"});
|
||||
push(@{ $form->{paymentsource} }, $form->{"source_$i"});
|
||||
$form->{paid} += $form->parse_amount($myconfig, $form->{"paid_$i"});
|
||||
}
|
||||
}
|
||||
02aab73f | Thomas Kasulke | if($form->{taxincluded}) {
|
||
$form->{subtotal} = $form->format_amount($myconfig, $form->{total} - $tax, 2);
|
||||
}
|
||||
else {
|
||||
$form->{subtotal} = $form->format_amount($myconfig, $form->{total}, 2);
|
||||
}
|
||||
54e4131e | Moritz Bunkus | $yesdiscount = $form->{nodiscount_total} - $nodiscount;
|
||
$form->{nodiscount_subtotal} = $form->format_amount($myconfig, $form->{nodiscount_total}, 2);
|
||||
$form->{discount_total} = $form->format_amount($myconfig, $form->{discount_total}, 2);
|
||||
$form->{nodiscount} = $form->format_amount($myconfig, $nodiscount, 2);
|
||||
$form->{yesdiscount} = $form->format_amount($myconfig, $yesdiscount, 2);
|
||||
d319704a | Moritz Bunkus | $form->{invtotal} =
|
||
($form->{taxincluded}) ? $form->{total} : $form->{total} + $tax;
|
||||
$form->{total} =
|
||||
$form->format_amount($myconfig, $form->{invtotal} - $form->{paid}, 2);
|
||||
b9b02c1a | Philip Reetz | |||
$form->{invtotal} = $form->format_amount($myconfig, $form->{invtotal}, 2);
|
||||
$form->{paid} = $form->format_amount($myconfig, $form->{paid}, 2);
|
||||
54e4131e | Moritz Bunkus | $form->set_payment_options($myconfig, $form->{invdate});
|
||
d319704a | Moritz Bunkus | |||
$form->{username} = $myconfig->{name};
|
||||
$dbh->disconnect;
|
||||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
sub project_description {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($self, $dbh, $id) = @_;
|
||||
532b0d4c | Moritz Bunkus | my $query = qq|SELECT description FROM project WHERE id = ?|;
|
||
my ($description) = selectrow_query($form, $dbh, $query, conv_i($id));
|
||||
d319704a | Moritz Bunkus | |||
$main::lxdebug->leave_sub();
|
||||
return $_;
|
||||
}
|
||||
sub customer_details {
|
||||
$main::lxdebug->enter_sub();
|
||||
f83b4aff | Moritz Bunkus | my ($self, $myconfig, $form, @wanted_vars) = @_;
|
||
d319704a | Moritz Bunkus | |||
# connect to database
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
# get contact id, set it if nessessary
|
||||
54e4131e | Moritz Bunkus | $form->{cp_id} *= 1;
|
||
d319704a | Moritz Bunkus | |||
6ed3eaf6 | Moritz Bunkus | my @values = (conv_i($form->{customer_id}));
|
||
532b0d4c | Moritz Bunkus | |||
my $where = "";
|
||||
d319704a | Moritz Bunkus | if ($form->{cp_id}) {
|
||
532b0d4c | Moritz Bunkus | $where = qq| AND (cp.cp_id = ?) |;
|
||
push(@values, conv_i($form->{cp_id}));
|
||||
d319704a | Moritz Bunkus | }
|
||
# get rest for the customer
|
||||
532b0d4c | Moritz Bunkus | my $query =
|
||
qq|SELECT ct.*, cp.*, ct.notes as customernotes,
|
||||
ct.phone AS customerphone, ct.fax AS customerfax, ct.email AS customeremail
|
||||
FROM customer ct
|
||||
LEFT JOIN contacts cp on ct.id = cp.cp_cv_id
|
||||
WHERE (ct.id = ?) $where
|
||||
ORDER BY cp.cp_id
|
||||
LIMIT 1|;
|
||||
my $ref = selectfirst_hashref_query($form, $dbh, $query, @values);
|
||||
d319704a | Moritz Bunkus | |||
54e4131e | Moritz Bunkus | # remove id and taxincluded before copy back
|
||
delete @$ref{qw(id taxincluded)};
|
||||
f83b4aff | Moritz Bunkus | |||
d416d4c4 | Moritz Bunkus | @wanted_vars = grep({ $_ } @wanted_vars);
|
||
f83b4aff | Moritz Bunkus | if (scalar(@wanted_vars) > 0) {
|
||
my %h_wanted_vars;
|
||||
map({ $h_wanted_vars{$_} = 1; } @wanted_vars);
|
||||
map({ delete($ref->{$_}) unless ($h_wanted_vars{$_}); } keys(%{$ref}));
|
||||
}
|
||||
7d2455cf | Stephan Köhler | map { $form->{$_} = $ref->{$_} } keys %$ref;
|
||
54e4131e | Moritz Bunkus | |||
if ($form->{delivery_customer_id}) {
|
||||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|SELECT *, notes as customernotes
|
||||
FROM customer
|
||||
WHERE id = ?
|
||||
LIMIT 1|;
|
||||
$ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{delivery_customer_id}));
|
||||
54e4131e | Moritz Bunkus | |||
map { $form->{"dc_$_"} = $ref->{$_} } keys %$ref;
|
||||
}
|
||||
if ($form->{delivery_vendor_id}) {
|
||||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|SELECT *, notes as customernotes
|
||||
FROM customer
|
||||
WHERE id = ?
|
||||
LIMIT 1|;
|
||||
$ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{delivery_vendor_id}));
|
||||
54e4131e | Moritz Bunkus | |||
map { $form->{"dv_$_"} = $ref->{$_} } keys %$ref;
|
||||
}
|
||||
d319704a | Moritz Bunkus | $dbh->disconnect;
|
||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
sub post_invoice {
|
||||
$main::lxdebug->enter_sub();
|
||||
0bb0eb67 | Stephan Köhler | |||
5fdc44cb | Moritz Bunkus | my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_;
|
||
d319704a | Moritz Bunkus | |||
# connect to database, turn off autocommit
|
||||
5fdc44cb | Moritz Bunkus | my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
|
||
d319704a | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | my ($query, $sth, $null, $project_id, @values);
|
||
d319704a | Moritz Bunkus | my $exchangerate = 0;
|
||
820545bc | Moritz Bunkus | if (!$form->{employee_id}) {
|
||
d319704a | Moritz Bunkus | $form->get_employee($dbh);
|
||
}
|
||||
fb37acdc | Moritz Bunkus | |||
$form->{defaultcurrency} = $form->get_default_currency($myconfig);
|
||||
d319704a | Moritz Bunkus | |||
($null, $form->{department_id}) = split(/--/, $form->{department});
|
||||
54e4131e | Moritz Bunkus | |||
532b0d4c | Moritz Bunkus | my $all_units = AM->retrieve_units($myconfig, $form);
|
||
54e4131e | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | if (!$payments_only) {
|
||
if ($form->{id}) {
|
||||
&reverse_invoice($dbh, $form);
|
||||
1c084510 | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | } else {
|
||
$query = qq|SELECT nextval('glid')|;
|
||||
($form->{"id"}) = selectrow_query($form, $dbh, $query);
|
||||
d319704a | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | $query = qq|INSERT INTO ar (id, invnumber) VALUES (?, ?)|;
|
||
do_query($form, $dbh, $query, $form->{"id"}, $form->{"id"});
|
||||
7712480e | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | if (!$form->{invnumber}) {
|
||
$form->{invnumber} =
|
||||
$form->update_defaults($myconfig, $form->{type} eq "credit_note" ?
|
||||
"cnnumber" : "invnumber", $dbh);
|
||||
}
|
||||
7712480e | Moritz Bunkus | }
|
||
d319704a | Moritz Bunkus | }
|
||
my ($netamount, $invoicediff) = (0, 0);
|
||||
my ($amount, $linetotal, $lastincomeaccno);
|
||||
5fdc44cb | Moritz Bunkus | my ($currencies) = selectfirst_array_query($form, $dbh, qq|SELECT curr FROM defaults|);
|
||
my $defaultcurrency = (split m/:/, $currencies)[0];
|
||||
if ($form->{currency} eq $defaultcurrency) {
|
||||
d319704a | Moritz Bunkus | $form->{exchangerate} = 1;
|
||
} else {
|
||||
$exchangerate =
|
||||
$form->check_exchangerate($myconfig, $form->{currency},
|
||||
$form->{transdate}, 'buy');
|
||||
}
|
||||
$form->{exchangerate} =
|
||||
($exchangerate)
|
||||
? $exchangerate
|
||||
: $form->parse_amount($myconfig, $form->{exchangerate});
|
||||
$form->{expense_inventory} = "";
|
||||
532b0d4c | Moritz Bunkus | my %baseunits;
|
||
d319704a | Moritz Bunkus | foreach my $i (1 .. $form->{rowcount}) {
|
||
54e4131e | Moritz Bunkus | if ($form->{type} eq "credit_note") {
|
||
$form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"}) * -1;
|
||||
532b0d4c | Moritz Bunkus | $form->{shipped} = 1;
|
||
54e4131e | Moritz Bunkus | } else {
|
||
$form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
|
||||
}
|
||||
my $basefactor;
|
||||
my $basqty;
|
||||
4d8a6515 | Philip Reetz | $form->{"marge_percent_$i"} = $form->parse_amount($myconfig, $form->{"marge_percent_$i"}) * 1;
|
||
$form->{"marge_absolut_$i"} = $form->parse_amount($myconfig, $form->{"marge_absolut_$i"}) * 1;
|
||||
$form->{"lastcost_$i"} = $form->{"lastcost_$i"} * 1;
|
||||
54e4131e | Moritz Bunkus | if ($form->{storno}) {
|
||
$form->{"qty_$i"} *= -1;
|
||||
}
|
||||
d319704a | Moritz Bunkus | |||
8c89bb02 | Moritz Bunkus | if ($form->{"id_$i"}) {
|
||
532b0d4c | Moritz Bunkus | my $item_unit;
|
||
d319704a | Moritz Bunkus | |||
532b0d4c | Moritz Bunkus | if (defined($baseunits{$form->{"id_$i"}})) {
|
||
$item_unit = $baseunits{$form->{"id_$i"}};
|
||||
54e4131e | Moritz Bunkus | } else {
|
||
532b0d4c | Moritz Bunkus | # get item baseunit
|
||
$query = qq|SELECT unit FROM parts WHERE id = ?|;
|
||||
($item_unit) = selectrow_query($form, $dbh, $query, conv_i($form->{"id_$i"}));
|
||||
$baseunits{$form->{"id_$i"}} = $item_unit;
|
||||
54e4131e | Moritz Bunkus | }
|
||
532b0d4c | Moritz Bunkus | if (defined($all_units->{$item_unit}->{factor})
|
||
&& ($all_units->{$item_unit}->{factor} ne '')
|
||||
&& ($all_units->{$item_unit}->{factor} != 0)) {
|
||||
$basefactor = $all_units->{$form->{"unit_$i"}}->{factor} / $all_units->{$item_unit}->{factor};
|
||||
} else {
|
||||
$basefactor = 1;
|
||||
}
|
||||
$baseqty = $form->{"qty_$i"} * $basefactor;
|
||||
d319704a | Moritz Bunkus | |||
# undo discount formatting
|
||||
$form->{"discount_$i"} =
|
||||
$form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
|
||||
my ($allocated, $taxrate) = (0, 0);
|
||||
my $taxamount;
|
||||
# keep entered selling price
|
||||
my $fxsellprice =
|
||||
$form->parse_amount($myconfig, $form->{"sellprice_$i"});
|
||||
my ($dec) = ($fxsellprice =~ /\.(\d+)/);
|
||||
$dec = length $dec;
|
||||
my $decimalplaces = ($dec > 2) ? $dec : 2;
|
||||
# deduct discount
|
||||
my $discount =
|
||||
$form->round_amount($fxsellprice * $form->{"discount_$i"},
|
||||
$decimalplaces);
|
||||
$form->{"sellprice_$i"} = $fxsellprice - $discount;
|
||||
# add tax rates
|
||||
532b0d4c | Moritz Bunkus | map({ $taxrate += $form->{"${_}_rate"} } split(/ /,
|
||
$form->{"taxaccounts_$i"}));
|
||||
d319704a | Moritz Bunkus | |||
# round linetotal to 2 decimal places
|
||||
$linetotal =
|
||||
$form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
|
||||
if ($form->{taxincluded}) {
|
||||
$taxamount = $linetotal * ($taxrate / (1 + $taxrate));
|
||||
$form->{"sellprice_$i"} =
|
||||
$form->{"sellprice_$i"} * (1 / (1 + $taxrate));
|
||||
} else {
|
||||
$taxamount = $linetotal * $taxrate;
|
||||
}
|
||||
$netamount += $linetotal;
|
||||
if ($taxamount != 0) {
|
||||
map {
|
||||
$form->{amount}{ $form->{id} }{$_} +=
|
||||
$taxamount * $form->{"${_}_rate"} / $taxrate
|
||||
532b0d4c | Moritz Bunkus | } split(/ /, $form->{"taxaccounts_$i"});
|
||
d319704a | Moritz Bunkus | }
|
||
# add amount to income, $form->{amount}{trans_id}{accno}
|
||||
$amount =
|
||||
$form->{"sellprice_$i"} * $form->{"qty_$i"} * $form->{exchangerate};
|
||||
$linetotal =
|
||||
$form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2) *
|
||||
$form->{exchangerate};
|
||||
$linetotal = $form->round_amount($linetotal, 2);
|
||||
# this is the difference from the inventory
|
||||
$invoicediff += ($amount - $linetotal);
|
||||
$form->{amount}{ $form->{id} }{ $form->{"income_accno_$i"} } +=
|
||||
$linetotal;
|
||||
$lastincomeaccno = $form->{"income_accno_$i"};
|
||||
# adjust and round sellprice
|
||||
$form->{"sellprice_$i"} =
|
||||
$form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate},
|
||||
$decimalplaces);
|
||||
5fdc44cb | Moritz Bunkus | next if $payments_only;
|
||
d319704a | Moritz Bunkus | if ($form->{"inventory_accno_$i"} || $form->{"assembly_$i"}) {
|
||
# adjust parts onhand quantity
|
||||
if ($form->{"assembly_$i"}) {
|
||||
# do not update if assembly consists of all services
|
||||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|SELECT sum(p.inventory_accno_id)
|
||||
FROM parts p
|
||||
JOIN assembly a ON (a.parts_id = p.id)
|
||||
WHERE a.id = ?|;
|
||||
$sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{"id_$i"}));
|
||||
d319704a | Moritz Bunkus | |||
if ($sth->fetchrow_array) {
|
||||
ef17e41a | Moritz Bunkus | $form->update_balance($dbh, "parts", "onhand", qq|id = ?|,
|
||
$baseqty * -1, $form->{"id_$i"})
|
||||
d319704a | Moritz Bunkus | unless $form->{shipped};
|
||
}
|
||||
$sth->finish;
|
||||
# record assembly item as allocated
|
||||
54e4131e | Moritz Bunkus | &process_assembly($dbh, $form, $form->{"id_$i"}, $baseqty);
|
||
d319704a | Moritz Bunkus | } else {
|
||
ef17e41a | Moritz Bunkus | $form->update_balance($dbh, "parts", "onhand", qq|id = ?|,
|
||
$baseqty * -1, $form->{"id_$i"})
|
||||
d319704a | Moritz Bunkus | unless $form->{shipped};
|
||
fe3df360 | Philip Reetz | $allocated = &cogs($dbh, $form, $form->{"id_$i"}, $baseqty, $basefactor, $i);
|
||
d319704a | Moritz Bunkus | }
|
||
}
|
||||
604959de | Moritz Bunkus | # get pricegroup_id and save it
|
||
532b0d4c | Moritz Bunkus | ($null, my $pricegroup_id) = split(/--/, $form->{"sellprice_pg_$i"});
|
||
07d71c33 | Stephan Köhler | $pricegroup_id *= 1;
|
||
54e4131e | Moritz Bunkus | my $subtotal = $form->{"subtotal_$i"} * 1;
|
||
07d71c33 | Stephan Köhler | |||
d319704a | Moritz Bunkus | # save detail record in invoice table
|
||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|INSERT INTO invoice (trans_id, parts_id, description, longdescription, qty,
|
||||
sellprice, fxsellprice, discount, allocated, assemblyitem,
|
||||
unit, deliverydate, project_id, serialnumber, pricegroup_id,
|
||||
4d8a6515 | Philip Reetz | ordnumber, transdate, cusordnumber, base_qty, subtotal,
|
||
marge_percent, marge_total, lastcost)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
|
||||
532b0d4c | Moritz Bunkus | |||
@values = (conv_i($form->{id}), conv_i($form->{"id_$i"}),
|
||||
$form->{"description_$i"}, $form->{"longdescription_$i"}, $form->{"qty_$i"},
|
||||
$form->{"sellprice_$i"}, $fxsellprice,
|
||||
$form->{"discount_$i"}, $allocated, 'f',
|
||||
$form->{"unit_$i"}, conv_date($form->{"deliverydate_$i"}), conv_i($form->{"project_id_$i"}),
|
||||
$form->{"serialnumber_$i"}, conv_i($pricegroup_id),
|
||||
$form->{"ordnumber_$i"}, conv_date($form->{"transdate_$i"}),
|
||||
4d8a6515 | Philip Reetz | $form->{"cusordnumber_$i"}, $baseqty, $subtotal,
|
||
$form->{"marge_percent_$i"}, $form->{"marge_absolut_$i"},
|
||||
$form->{"lastcost_$i"});
|
||||
532b0d4c | Moritz Bunkus | do_query($form, $dbh, $query, @values);
|
||
if ($form->{lizenzen} && $form->{"licensenumber_$i"}) {
|
||||
$query =
|
||||
qq|INSERT INTO licenseinvoice (trans_id, license_id)
|
||||
VALUES ((SELECT id FROM invoice WHERE trans_id = ? ORDER BY oid DESC LIMIT 1), ?)|;
|
||||
@values = (conv_i($form->{"id"}), conv_i($form->{"licensenumber_$i"}));
|
||||
do_query($form, $dbh, $query, @values);
|
||||
d319704a | Moritz Bunkus | }
|
||
}
|
||||
}
|
||||
$form->{datepaid} = $form->{invdate};
|
||||
# total payments, don't move we need it here
|
||||
for my $i (1 .. $form->{paidaccounts}) {
|
||||
54e4131e | Moritz Bunkus | if ($form->{type} eq "credit_note") {
|
||
$form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"}) * -1;
|
||||
} else {
|
||||
$form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
|
||||
}
|
||||
d319704a | Moritz Bunkus | $form->{paid} += $form->{"paid_$i"};
|
||
$form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
|
||||
}
|
||||
my ($tax, $diff) = (0, 0);
|
||||
$netamount = $form->round_amount($netamount, 2);
|
||||
# figure out rounding errors for total amount vs netamount + taxes
|
||||
if ($form->{taxincluded}) {
|
||||
$amount = $form->round_amount($netamount * $form->{exchangerate}, 2);
|
||||
$diff += $amount - $netamount * $form->{exchangerate};
|
||||
$netamount = $amount;
|
||||
532b0d4c | Moritz Bunkus | foreach my $item (split(/ /, $form->{taxaccounts})) {
|
||
d319704a | Moritz Bunkus | $amount = $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate};
|
||
$form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount, 2);
|
||||
$tax += $form->{amount}{ $form->{id} }{$item};
|
||||
$netamount -= $form->{amount}{ $form->{id} }{$item};
|
||||
}
|
||||
$invoicediff += $diff;
|
||||
######## this only applies to tax included
|
||||
if ($lastincomeaccno) {
|
||||
$form->{amount}{ $form->{id} }{$lastincomeaccno} += $invoicediff;
|
||||
}
|
||||
} else {
|
||||
$amount = $form->round_amount($netamount * $form->{exchangerate}, 2);
|
||||
$diff = $amount - $netamount * $form->{exchangerate};
|
||||
$netamount = $amount;
|
||||
532b0d4c | Moritz Bunkus | foreach my $item (split(/ /, $form->{taxaccounts})) {
|
||
d319704a | Moritz Bunkus | $form->{amount}{ $form->{id} }{$item} =
|
||
$form->round_amount($form->{amount}{ $form->{id} }{$item}, 2);
|
||||
$amount =
|
||||
$form->round_amount(
|
||||
$form->{amount}{ $form->{id} }{$item} * $form->{exchangerate},
|
||||
2);
|
||||
$diff +=
|
||||
$amount - $form->{amount}{ $form->{id} }{$item} *
|
||||
$form->{exchangerate};
|
||||
$form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount, 2);
|
||||
$tax += $form->{amount}{ $form->{id} }{$item};
|
||||
}
|
||||
}
|
||||
$form->{amount}{ $form->{id} }{ $form->{AR} } = $netamount + $tax;
|
||||
$form->{paid} =
|
||||
$form->round_amount($form->{paid} * $form->{exchangerate} + $diff, 2);
|
||||
# reverse AR
|
||||
$form->{amount}{ $form->{id} }{ $form->{AR} } *= -1;
|
||||
# update exchangerate
|
||||
5fdc44cb | Moritz Bunkus | if (($form->{currency} ne $defaultcurrency) && !$exchangerate) {
|
||
d319704a | Moritz Bunkus | $form->update_exchangerate($dbh, $form->{currency}, $form->{invdate},
|
||
$form->{exchangerate}, 0);
|
||||
}
|
||||
d4651135 | Moritz Bunkus | $project_id = conv_i($form->{"globalproject_id"});
|
||
d319704a | Moritz Bunkus | foreach my $trans_id (keys %{ $form->{amount} }) {
|
||
foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
|
||||
next unless ($form->{expense_inventory} =~ /$accno/);
|
||||
5fdc44cb | Moritz Bunkus | |||
$form->{amount}{$trans_id}{$accno} = $form->round_amount($form->{amount}{$trans_id}{$accno}, 2);
|
||||
if (!$payments_only && ($form->{amount}{$trans_id}{$accno} != 0)) {
|
||||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id)
|
||||
VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?,
|
||||
(SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
|
||||
@values = (conv_i($trans_id), $accno, $form->{amount}{$trans_id}{$accno}, conv_date($form->{invdate}), $accno, conv_i($project_id));
|
||||
do_query($form, $dbh, $query, @values);
|
||||
d319704a | Moritz Bunkus | $form->{amount}{$trans_id}{$accno} = 0;
|
||
}
|
||||
}
|
||||
foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
|
||||
5fdc44cb | Moritz Bunkus | $form->{amount}{$trans_id}{$accno} = $form->round_amount($form->{amount}{$trans_id}{$accno}, 2);
|
||
if (!$payments_only && ($form->{amount}{$trans_id}{$accno} != 0)) {
|
||||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id)
|
||||
VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?,
|
||||
(SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
|
||||
@values = (conv_i($trans_id), $accno, $form->{amount}{$trans_id}{$accno}, conv_date($form->{invdate}), $accno, conv_i($project_id));
|
||||
do_query($form, $dbh, $query, @values);
|
||||
d319704a | Moritz Bunkus | }
|
||
}
|
||||
}
|
||||
# deduct payment differences from diff
|
||||
for my $i (1 .. $form->{paidaccounts}) {
|
||||
if ($form->{"paid_$i"} != 0) {
|
||||
$amount =
|
||||
$form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2);
|
||||
$diff -= $amount - $form->{"paid_$i"} * $form->{exchangerate};
|
||||
}
|
||||
}
|
||||
# record payments and offsetting AR
|
||||
54e4131e | Moritz Bunkus | if (!$form->{storno}) {
|
||
for my $i (1 .. $form->{paidaccounts}) {
|
||||
532b0d4c | Moritz Bunkus | |||
next if ($form->{"paid_$i"} == 0);
|
||||
my ($accno) = split(/--/, $form->{"AR_paid_$i"});
|
||||
$form->{"datepaid_$i"} = $form->{invdate}
|
||||
unless ($form->{"datepaid_$i"});
|
||||
$form->{datepaid} = $form->{"datepaid_$i"};
|
||||
$exchangerate = 0;
|
||||
5fdc44cb | Moritz Bunkus | if ($form->{currency} eq $defaultcurrency) {
|
||
532b0d4c | Moritz Bunkus | $form->{"exchangerate_$i"} = 1;
|
||
} else {
|
||||
$exchangerate =
|
||||
$form->check_exchangerate($myconfig, $form->{currency},
|
||||
$form->{"datepaid_$i"}, 'buy');
|
||||
$form->{"exchangerate_$i"} =
|
||||
$exchangerate ? $exchangerate
|
||||
54e4131e | Moritz Bunkus | : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
|
||
532b0d4c | Moritz Bunkus | }
|
||
# record AR
|
||||
$amount = $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} + $diff, 2);
|
||||
if ($form->{amount}{ $form->{id} }{ $form->{AR} } != 0) {
|
||||
$query =
|
||||
qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id)
|
||||
VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?,
|
||||
(SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
|
||||
@values = (conv_i($form->{"id"}), $form->{AR}, $amount, $form->{"datepaid_$i"}, $form->{AR}, $project_id);
|
||||
do_query($form, $dbh, $query, @values);
|
||||
}
|
||||
# record payment
|
||||
$form->{"paid_$i"} *= -1;
|
||||
$query =
|
||||
qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, taxkey, project_id)
|
||||
VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?,
|
||||
(SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
|
||||
@values = (conv_i($form->{"id"}), $accno, $form->{"paid_$i"}, $form->{"datepaid_$i"},
|
||||
$form->{"source_$i"}, $form->{"memo_$i"}, $accno, $project_id);
|
||||
do_query($form, $dbh, $query, @values);
|
||||
# exchangerate difference
|
||||
$form->{fx}{$accno}{ $form->{"datepaid_$i"} } +=
|
||||
$form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) + $diff;
|
||||
# gain/loss
|
||||
$amount =
|
||||
$form->{"paid_$i"} * $form->{exchangerate} - $form->{"paid_$i"} *
|
||||
$form->{"exchangerate_$i"};
|
||||
if ($amount > 0) {
|
||||
$form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } +=
|
||||
$amount;
|
||||
} else {
|
||||
$form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } +=
|
||||
$amount;
|
||||
}
|
||||
$diff = 0;
|
||||
# update exchange rate
|
||||
5fdc44cb | Moritz Bunkus | if (($form->{currency} ne $defaultcurrency) && !$exchangerate) {
|
||
532b0d4c | Moritz Bunkus | $form->update_exchangerate($dbh, $form->{currency},
|
||
$form->{"datepaid_$i"},
|
||||
$form->{"exchangerate_$i"}, 0);
|
||||
d319704a | Moritz Bunkus | }
|
||
}
|
||||
}
|
||||
5fdc44cb | Moritz Bunkus | if ($payments_only) {
|
||
$query = qq|UPDATE ar SET paid = ?, datepaid = ? WHERE id = ?|;
|
||||
do_query($form, $dbh, $query, $form->{paid}, $form->{paid} ? conv_date($form->{datepaid}) : undef, conv_i($form->{id}));
|
||||
if (!$provided_dbh) {
|
||||
$dbh->commit();
|
||||
$dbh->disconnect();
|
||||
}
|
||||
$main::lxdebug->leave_sub();
|
||||
return;
|
||||
}
|
||||
d319704a | Moritz Bunkus | # record exchange rate differences and gains/losses
|
||
foreach my $accno (keys %{ $form->{fx} }) {
|
||||
foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
|
||||
if (
|
||||
($form->{fx}{$accno}{$transdate} =
|
||||
$form->round_amount($form->{fx}{$accno}{$transdate}, 2)
|
||||
) != 0
|
||||
) {
|
||||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, cleared, fx_transaction, taxkey, project_id)
|
||||
VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, '0', '1',
|
||||
(SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
|
||||
@values = (conv_i($form->{"id"}), $accno, $form->{fx}{$accno}{$transdate}, conv_date($transdate), $accno, $project_id);
|
||||
do_query($form, $dbh, $query, @values);
|
||||
d319704a | Moritz Bunkus | }
|
||
}
|
||||
}
|
||||
$amount = $netamount + $tax;
|
||||
# fill in subject if there is none
|
||||
$form->{subject} = qq|$form->{label} $form->{invnumber}|
|
||||
unless $form->{subject};
|
||||
# if there is a message stuff it into the intnotes
|
||||
my $cc = "Cc: $form->{cc}\\r\n" if $form->{cc};
|
||||
my $bcc = "Bcc: $form->{bcc}\\r\n" if $form->{bcc};
|
||||
my $now = scalar localtime;
|
||||
$form->{intnotes} .= qq|\r
|
||||
\r| if $form->{intnotes};
|
||||
$form->{intnotes} .= qq|[email]\r
|
||||
Date: $now
|
||||
To: $form->{email}\r
|
||||
$cc${bcc}Subject: $form->{subject}\r
|
||||
\r
|
||||
Message: $form->{message}\r| if $form->{message};
|
||||
# save AR record
|
||||
$query = qq|UPDATE ar set
|
||||
532b0d4c | Moritz Bunkus | invnumber = ?,
|
||
ordnumber = ?,
|
||||
quonumber = ?,
|
||||
cusordnumber = ?,
|
||||
transdate = ?,
|
||||
orddate = ?,
|
||||
quodate = ?,
|
||||
customer_id = ?,
|
||||
amount = ?,
|
||||
netamount = ?,
|
||||
paid = ?,
|
||||
datepaid = ?,
|
||||
duedate = ?,
|
||||
deliverydate = ?,
|
||||
invoice = '1',
|
||||
shippingpoint = ?,
|
||||
shipvia = ?,
|
||||
terms = ?,
|
||||
notes = ?,
|
||||
intnotes = ?,
|
||||
taxincluded = ?,
|
||||
curr = ?,
|
||||
department_id = ?,
|
||||
payment_id = ?,
|
||||
type = ?,
|
||||
language_id = ?,
|
||||
taxzone_id = ?,
|
||||
shipto_id = ?,
|
||||
delivery_customer_id = ?,
|
||||
delivery_vendor_id = ?,
|
||||
employee_id = ?,
|
||||
salesman_id = ?,
|
||||
storno = ?,
|
||||
globalproject_id = ?,
|
||||
c63316e6 | Moritz Bunkus | cp_id = ?,
|
||
4d8a6515 | Philip Reetz | transaction_description = ?,
|
||
marge_total = ?,
|
||||
marge_percent = ?
|
||||
532b0d4c | Moritz Bunkus | WHERE id = ?|;
|
||
@values = ($form->{"invnumber"}, $form->{"ordnumber"}, $form->{"quonumber"}, $form->{"cusordnumber"},
|
||||
conv_date($form->{"invdate"}), conv_date($form->{"orddate"}), conv_date($form->{"quodate"}),
|
||||
conv_i($form->{"customer_id"}), $amount, $netamount, $form->{"paid"},
|
||||
conv_date($form->{"datepaid"}), conv_date($form->{"duedate"}), conv_date($form->{"deliverydate"}),
|
||||
$form->{"shippingpoint"}, $form->{"shipvia"}, conv_i($form->{"terms"}),
|
||||
$form->{"notes"}, $form->{"intnotes"}, $form->{"taxincluded"} ? 't' : 'f',
|
||||
$form->{"currency"}, conv_i($form->{"department_id"}), conv_i($form->{"payment_id"}),
|
||||
$form->{"type"}, conv_i($form->{"language_id"}), conv_i($form->{"taxzone_id"}),
|
||||
conv_i($form->{"shipto_id"}),
|
||||
conv_i($form->{"delivery_customer_id"}), conv_i($form->{"delivery_vendor_id"}),
|
||||
conv_i($form->{"employee_id"}), conv_i($form->{"salesman_id"}),
|
||||
$form->{"storno"} ? 't' : 'f', conv_i($form->{"globalproject_id"}),
|
||||
c63316e6 | Moritz Bunkus | conv_i($form->{"cp_id"}), $form->{transaction_description},
|
||
4d8a6515 | Philip Reetz | $form->{marge_total}, $form->{marge_percent},
|
||
532b0d4c | Moritz Bunkus | conv_i($form->{"id"}));
|
||
do_query($form, $dbh, $query, @values);
|
||||
6eea36b2 | Thomas Kasulke | |||
if($form->{"formname"} eq "credit_note") {
|
||||
for my $i (1 .. $form->{paidaccounts}) {
|
||||
$query = qq|UPDATE parts SET onhand = onhand - ? WHERE id = ?|;
|
||||
@values = (conv_i($form->{"qty_$i"}), conv_i($form->{"id_$i"}));
|
||||
do_query($form, $dbh, $query, @values);
|
||||
}
|
||||
}
|
||||
54e4131e | Moritz Bunkus | if ($form->{storno}) {
|
||
532b0d4c | Moritz Bunkus | $query =
|
||
qq!UPDATE ar SET
|
||||
paid = paid + amount,
|
||||
storno = 't',
|
||||
intnotes = ? || intnotes
|
||||
WHERE id = ?!;
|
||||
do_query($form, $dbh, $query, "Rechnung storniert am $form->{invdate} ", conv_i($form->{"storno_id"}));
|
||||
do_query($form, $dbh, qq|UPDATE ar SET paid = amount WHERE id = ?|, conv_i($form->{"id"}));
|
||||
54e4131e | Moritz Bunkus | }
|
||
d319704a | Moritz Bunkus | # add shipto
|
||
$form->{name} = $form->{customer};
|
||||
$form->{name} =~ s/--$form->{customer_id}//;
|
||||
54e4131e | Moritz Bunkus | |||
if (!$form->{shipto_id}) {
|
||||
$form->add_shipto($dbh, $form->{id}, "AR");
|
||||
}
|
||||
d319704a | Moritz Bunkus | |||
# save printed, emailed and queued
|
||||
$form->save_status($dbh);
|
||||
8e206587 | Moritz Bunkus | Common::webdav_folder($form) if ($main::webdav);
|
||
d319704a | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | my $rc = 1;
|
||
if (!$provided_dbh) {
|
||||
$dbh->commit();
|
||||
$dbh->disconnect();
|
||||
}
|
||||
d319704a | Moritz Bunkus | |||
$main::lxdebug->leave_sub();
|
||||
return $rc;
|
||||
}
|
||||
5fdc44cb | Moritz Bunkus | sub _delete_payments {
|
||
$main::lxdebug->enter_sub();
|
||||
54e4131e | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | my ($self, $form, $dbh) = @_;
|
||
54e4131e | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | my @delete_oids;
|
||
54e4131e | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | # Delete old payment entries from acc_trans.
|
||
my $query =
|
||||
qq|SELECT oid
|
||||
FROM acc_trans
|
||||
WHERE (trans_id = ?) AND fx_transaction
|
||||
UNION
|
||||
SELECT at.oid
|
||||
FROM acc_trans at
|
||||
LEFT JOIN chart c ON (at.chart_id = c.id)
|
||||
WHERE (trans_id = ?) AND (c.link LIKE '%AR_paid%')|;
|
||||
push @delete_oids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}), conv_i($form->{id}));
|
||||
$query =
|
||||
qq|SELECT at.oid
|
||||
FROM acc_trans at
|
||||
LEFT JOIN chart c ON (at.chart_id = c.id)
|
||||
WHERE (trans_id = ?)
|
||||
AND ((c.link = 'AR') OR (c.link LIKE '%:AR') OR (c.link LIKE 'AR:%'))
|
||||
ORDER BY at.oid
|
||||
OFFSET 1|;
|
||||
push @delete_oids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}));
|
||||
if (@delete_oids) {
|
||||
$query = qq|DELETE FROM acc_trans WHERE oid IN (| . join(", ", @delete_oids) . qq|)|;
|
||||
do_query($form, $dbh, $query);
|
||||
1c084510 | Moritz Bunkus | }
|
||
54e4131e | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | $main::lxdebug->leave_sub();
|
||
}
|
||||
54e4131e | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | sub post_payment {
|
||
$main::lxdebug->enter_sub();
|
||||
54e4131e | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | my ($self, $myconfig, $form, $locale) = @_;
|
||
54e4131e | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | # connect to database, turn off autocommit
|
||
my $dbh = $form->dbconnect_noauto($myconfig);
|
||||
54e4131e | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | my (%payments, $old_form, $row, $item, $query, %keep_vars);
|
||
532b0d4c | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | $old_form = save_form();
|
||
54e4131e | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | # Delete all entries in acc_trans from prior payments.
|
||
$self->_delete_payments($form, $dbh);
|
||||
54e4131e | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | # Save the new payments the user made before cleaning up $form.
|
||
map { $payments{$_} = $form->{$_} } grep m/^datepaid_\d+$|^memo_\d+$|^source_\d+$|^exchangerate_\d+$|^paid_\d+$|^AR_paid_\d+$|^paidaccounts$/, keys %{ $form };
|
||||
529c6ea4 | Sven Schöling | |||
5fdc44cb | Moritz Bunkus | # Clean up $form so that old content won't tamper the results.
|
||
%keep_vars = map { $_, 1 } qw(login password id);
|
||||
map { delete $form->{$_} unless $keep_vars{$_} } keys %{ $form };
|
||||
54e4131e | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | # Retrieve the invoice from the database.
|
||
$self->retrieve_invoice($myconfig, $form);
|
||||
529c6ea4 | Sven Schöling | |||
9c5108ec | Moritz Bunkus | # Set up the content of $form in the way that IS::post_invoice() expects.
|
||
5fdc44cb | Moritz Bunkus | $form->{exchangerate} = $form->format_amount($myconfig, $form->{exchangerate});
|
||
529c6ea4 | Sven Schöling | |||
5fdc44cb | Moritz Bunkus | for $row (1 .. scalar @{ $form->{invoice_details} }) {
|
||
$item = $form->{invoice_details}->[$row - 1];
|
||||
map { $item->{$_} = $form->format_amount($myconfig, $item->{$_}) } qw(qty sellprice discount);
|
||||
map { $form->{"${_}_${row}"} = $item->{$_} } keys %{ $item };
|
||||
1c084510 | Moritz Bunkus | }
|
||
548f4467 | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | $form->{rowcount} = scalar @{ $form->{invoice_details} };
|
||
548f4467 | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | delete @{$form}{qw(invoice_details paidaccounts storno paid)};
|
||
548f4467 | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | # Restore the payment options from the user input.
|
||
map { $form->{$_} = $payments{$_} } keys %payments;
|
||||
54e4131e | Moritz Bunkus | |||
6bd1a382 | Moritz Bunkus | # Get the AR accno (which is normally done by Form::create_links()).
|
||
5fdc44cb | Moritz Bunkus | $query =
|
||
qq|SELECT c.accno
|
||||
FROM acc_trans at
|
||||
LEFT JOIN chart c ON (at.chart_id = c.id)
|
||||
WHERE (trans_id = ?)
|
||||
AND ((c.link = 'AR') OR (c.link LIKE '%:AR') OR (c.link LIKE 'AR:%'))
|
||||
ORDER BY at.oid
|
||||
LIMIT 1|;
|
||||
54e4131e | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | ($form->{AR}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{id}));
|
||
54e4131e | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | # Post the new payments.
|
||
$self->post_invoice($myconfig, $form, $dbh, 1);
|
||||
restore_form($old_form);
|
||||
54e4131e | Moritz Bunkus | |||
5fdc44cb | Moritz Bunkus | my $rc = $dbh->commit();
|
||
$dbh->disconnect();
|
||||
$main::lxdebug->leave_sub();
|
||||
return $rc;
|
||||
54e4131e | Moritz Bunkus | }
|
||
d319704a | Moritz Bunkus | sub process_assembly {
|
||
$main::lxdebug->enter_sub();
|
||||
my ($dbh, $form, $id, $totalqty) = @_;
|
||||
532b0d4c | Moritz Bunkus | my $query =
|
||
qq|SELECT a.parts_id, a.qty, p.assembly, p.partnumber, p.description, p.unit,
|
||||
p.inventory_accno_id, p.income_accno_id, p.expense_accno_id
|
||||
FROM assembly a
|
||||
JOIN parts p ON (a.parts_id = p.id)
|
||||
WHERE (a.id = ?)|;
|
||||
my $sth = prepare_execute_query($form, $dbh, $query, conv_i($id));
|
||||
d319704a | Moritz Bunkus | |||
while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
|
||||
my $allocated = 0;
|
||||
$ref->{inventory_accno_id} *= 1;
|
||||
$ref->{expense_accno_id} *= 1;
|
||||
# multiply by number of assemblies
|
||||
$ref->{qty} *= $totalqty;
|
||||
if ($ref->{assembly}) {
|
||||
&process_assembly($dbh, $form, $ref->{parts_id}, $ref->{qty});
|
||||
next;
|
||||
} else {
|
||||
if ($ref->{inventory_accno_id}) {
|
||||
$allocated = &cogs($dbh, $form, $ref->{parts_id}, $ref->{qty});
|
||||
}
|
||||
}
|
||||
# save detail record for individual assembly item in invoice table
|
||||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|INSERT INTO invoice (trans_id, description, parts_id, qty, sellprice, fxsellprice, allocated, assemblyitem, unit)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)|;
|
||||
@values = (conv_i($form->{id}), $ref->{description}, conv_i($ref->{parts_id}), $ref->{qty}, 0, 0, $allocated, 't', $ref->{unit});
|
||||
do_query($form, $dbh, $query, @values);
|
||||
d319704a | Moritz Bunkus | |||
}
|
||||
$sth->finish;
|
||||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
sub cogs {
|
||||
$main::lxdebug->enter_sub();
|
||||
fe3df360 | Philip Reetz | my ($dbh, $form, $id, $totalqty, $basefactor, $row) = @_;
|
||
$form->{taxzone_id} *=1;
|
||||
532b0d4c | Moritz Bunkus | my $transdate = $form->{invdate} ? $dbh->quote($form->{invdate}) : "current_date";
|
||
my $taxzone_id = $form->{"taxzone_id"} * 1;
|
||||
my $query =
|
||||
qq|SELECT i.id, i.trans_id, i.base_qty, i.allocated, i.sellprice,
|
||||
c1.accno AS inventory_accno,
|
||||
c1.new_chart_id AS inventory_new_chart,
|
||||
date($transdate) - c1.valid_from AS inventory_valid,
|
||||
c2.accno AS income_accno,
|
||||
c2.new_chart_id AS income_new_chart,
|
||||
date($transdate) - c2.valid_from AS income_valid,
|
||||
c3.accno AS expense_accno,
|
||||
c3.new_chart_id AS expense_new_chart,
|
||||
date($transdate) - c3.valid_from AS expense_valid
|
||||
FROM invoice i, parts p
|
||||
LEFT JOIN chart c1 ON ((SELECT inventory_accno_id FROM buchungsgruppen WHERE id = p.buchungsgruppen_id) = c1.id)
|
||||
LEFT JOIN chart c2 ON ((SELECT income_accno_id_${taxzone_id} FROM buchungsgruppen WHERE id = p.buchungsgruppen_id) = c2.id)
|
||||
LEFT JOIN chart c3 ON ((select expense_accno_id_${taxzone_id} FROM buchungsgruppen WHERE id = p.buchungsgruppen_id) = c3.id)
|
||||
WHERE (i.parts_id = p.id)
|
||||
AND (i.parts_id = ?)
|
||||
AND ((i.base_qty + i.allocated) < 0)
|
||||
ORDER BY trans_id|;
|
||||
my $sth = prepare_execute_query($form, $dbh, $query, conv_i($id));
|
||||
d319704a | Moritz Bunkus | |||
my $allocated = 0;
|
||||
my $qty;
|
||||
while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
|
||||
54e4131e | Moritz Bunkus | if (($qty = (($ref->{base_qty} * -1) - $ref->{allocated})) > $totalqty) {
|
||
d319704a | Moritz Bunkus | $qty = $totalqty;
|
||
}
|
||||
532b0d4c | Moritz Bunkus | $form->update_balance($dbh, "invoice", "allocated", qq|id = $ref->{id}|, $qty);
|
||
d319704a | Moritz Bunkus | |||
# total expenses and inventory
|
||||
# sellprice is the cost of the item
|
||||
54e4131e | Moritz Bunkus | $linetotal = $form->round_amount(($ref->{sellprice} * $qty) / $basefactor, 2);
|
||
d319704a | Moritz Bunkus | |||
fe3df360 | Philip Reetz | if (!$main::eur) {
|
||
$ref->{expense_accno} = ($form->{"expense_accno_$row"}) ? $form->{"expense_accno_$row"} : $ref->{expense_accno};
|
||||
d319704a | Moritz Bunkus | # add to expense
|
||
$form->{amount}{ $form->{id} }{ $ref->{expense_accno} } += -$linetotal;
|
||||
$form->{expense_inventory} .= " " . $ref->{expense_accno};
|
||||
fe3df360 | Philip Reetz | $ref->{inventory_accno} = ($form->{"inventory_accno_$row"}) ? $form->{"inventory_accno_$row"} : $ref->{inventory_accno};
|
||
d319704a | Moritz Bunkus | # deduct inventory
|
||
$form->{amount}{ $form->{id} }{ $ref->{inventory_accno} } -= -$linetotal;
|
||||
$form->{expense_inventory} .= " " . $ref->{inventory_accno};
|
||||
}
|
||||
# add allocated
|
||||
532b0d4c | Moritz Bunkus | $allocated -= $qty;
|
||
d319704a | Moritz Bunkus | |||
last if (($totalqty -= $qty) <= 0);
|
||||
}
|
||||
$sth->finish;
|
||||
$main::lxdebug->leave_sub();
|
||||
return $allocated;
|
||||
}
|
||||
sub reverse_invoice {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($dbh, $form) = @_;
|
||||
# reverse inventory items
|
||||
532b0d4c | Moritz Bunkus | my $query =
|
||
qq|SELECT i.id, i.parts_id, i.qty, i.assemblyitem, p.assembly, p.inventory_accno_id
|
||||
FROM invoice i
|
||||
JOIN parts p ON (i.parts_id = p.id)
|
||||
WHERE i.trans_id = ?|;
|
||||
my $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{"id"}));
|
||||
d319704a | Moritz Bunkus | |||
while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
|
||||
if ($ref->{inventory_accno_id} || $ref->{assembly}) {
|
||||
# if the invoice item is not an assemblyitem adjust parts onhand
|
||||
if (!$ref->{assemblyitem}) {
|
||||
# adjust onhand in parts table
|
||||
532b0d4c | Moritz Bunkus | $form->update_balance($dbh, "parts", "onhand", qq|id = $ref->{parts_id}|, $ref->{qty});
|
||
d319704a | Moritz Bunkus | }
|
||
# loop if it is an assembly
|
||||
next if ($ref->{assembly});
|
||||
# de-allocated purchases
|
||||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|SELECT i.id, i.trans_id, i.allocated
|
||||
FROM invoice i
|
||||
WHERE (i.parts_id = ?) AND (i.allocated > 0)
|
||||
ORDER BY i.trans_id DESC|;
|
||||
my $sth2 = prepare_execute_query($form, $dbh, $query, conv_i($ref->{"parts_id"}));
|
||||
while (my $inhref = $sth2->fetchrow_hashref(NAME_lc)) {
|
||||
d319704a | Moritz Bunkus | $qty = $ref->{qty};
|
||
if (($ref->{qty} - $inhref->{allocated}) > 0) {
|
||||
$qty = $inhref->{allocated};
|
||||
}
|
||||
# update invoice
|
||||
532b0d4c | Moritz Bunkus | $form->update_balance($dbh, "invoice", "allocated", qq|id = $inhref->{id}|, $qty * -1);
|
||
d319704a | Moritz Bunkus | |||
last if (($ref->{qty} -= $qty) <= 0);
|
||||
}
|
||||
532b0d4c | Moritz Bunkus | $sth2->finish;
|
||
d319704a | Moritz Bunkus | }
|
||
}
|
||||
$sth->finish;
|
||||
# delete acc_trans
|
||||
532b0d4c | Moritz Bunkus | @values = (conv_i($form->{id}));
|
||
do_query($form, $dbh, qq|DELETE FROM acc_trans WHERE trans_id = ?|, @values);
|
||||
do_query($form, $dbh, qq|DELETE FROM invoice WHERE trans_id = ?|, @values);
|
||||
d319704a | Moritz Bunkus | |||
if ($form->{lizenzen}) {
|
||||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|DELETE FROM licenseinvoice
|
||||
WHERE trans_id in (SELECT id FROM invoice WHERE trans_id = ?)|;
|
||||
do_query($form, $dbh, $query, @values);
|
||||
d319704a | Moritz Bunkus | }
|
||
532b0d4c | Moritz Bunkus | do_query($form, $dbh, qq|DELETE FROM shipto WHERE (trans_id = ?) AND (module = 'AR')|, @values);
|
||
d319704a | Moritz Bunkus | |||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
sub delete_invoice {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form, $spool) = @_;
|
||||
# connect to database
|
||||
my $dbh = $form->dbconnect_noauto($myconfig);
|
||||
&reverse_invoice($dbh, $form);
|
||||
532b0d4c | Moritz Bunkus | my @values = (conv_i($form->{id}));
|
||
d319704a | Moritz Bunkus | # delete AR record
|
||
532b0d4c | Moritz Bunkus | do_query($form, $dbh, qq|DELETE FROM ar WHERE id = ?|, @values);
|
||
d319704a | Moritz Bunkus | |||
# delete spool files
|
||||
532b0d4c | Moritz Bunkus | my @spoolfiles = selectall_array_query($form, $dbh, qq|SELECT spoolfile FROM status WHERE trans_id = ?|, @values);
|
||
d319704a | Moritz Bunkus | |||
# delete status entries
|
||||
532b0d4c | Moritz Bunkus | do_query($form, $dbh, qq|DELETE FROM status WHERE trans_id = ?|, @values);
|
||
d319704a | Moritz Bunkus | |||
my $rc = $dbh->commit;
|
||||
$dbh->disconnect;
|
||||
if ($rc) {
|
||||
532b0d4c | Moritz Bunkus | map { unlink "$spool/$_" if -f "$spool/$_"; } @{ $spoolfiles };
|
||
d319704a | Moritz Bunkus | }
|
||
$main::lxdebug->leave_sub();
|
||||
return $rc;
|
||||
}
|
||||
sub retrieve_invoice {
|
||||
$main::lxdebug->enter_sub();
|
||||
0bb0eb67 | Stephan Köhler | |||
d319704a | Moritz Bunkus | my ($self, $myconfig, $form) = @_;
|
||
# connect to database
|
||||
my $dbh = $form->dbconnect_noauto($myconfig);
|
||||
532b0d4c | Moritz Bunkus | my ($sth, $ref, $query);
|
||
my $query_transdate = ", current_date AS invdate" if !$form->{id};
|
||||
$query =
|
||||
qq|SELECT
|
||||
7eae8fac | Moritz Bunkus | (SELECT c.accno FROM chart c WHERE d.inventory_accno_id = c.id) AS inventory_accno,
|
||
(SELECT c.accno FROM chart c WHERE d.income_accno_id = c.id) AS income_accno,
|
||||
(SELECT c.accno FROM chart c WHERE d.expense_accno_id = c.id) AS expense_accno,
|
||||
(SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
|
||||
(SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
|
||||
532b0d4c | Moritz Bunkus | d.curr AS currencies
|
||
${query_transdate}
|
||||
FROM defaults d|;
|
||||
$ref = selectfirst_hashref_query($form, $dbh, $query);
|
||||
map { $form->{$_} = $ref->{$_} } keys %{ $ref };
|
||||
d319704a | Moritz Bunkus | |||
if ($form->{id}) {
|
||||
532b0d4c | Moritz Bunkus | my $id = conv_i($form->{id});
|
||
d319704a | Moritz Bunkus | |||
# retrieve invoice
|
||||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|SELECT
|
||||
a.invnumber, a.ordnumber, a.quonumber, a.cusordnumber,
|
||||
a.orddate, a.quodate, a.globalproject_id,
|
||||
a.transdate AS invdate, a.deliverydate, a.paid, a.storno, a.gldate,
|
||||
a.shippingpoint, a.shipvia, a.terms, a.notes, a.intnotes, a.taxzone_id,
|
||||
a.duedate, a.taxincluded, a.curr AS currency, a.shipto_id, a.cp_id,
|
||||
a.employee_id, a.salesman_id, a.payment_id,
|
||||
a.language_id, a.delivery_customer_id, a.delivery_vendor_id, a.type,
|
||||
c63316e6 | Moritz Bunkus | a.transaction_description,
|
||
532b0d4c | Moritz Bunkus | e.name AS employee
|
||
FROM ar a
|
||||
LEFT JOIN employee e ON (e.id = a.employee_id)
|
||||
WHERE a.id = ?|;
|
||||
$ref = selectfirst_hashref_query($form, $dbh, $query, $id);
|
||||
map { $form->{$_} = $ref->{$_} } keys %{ $ref };
|
||||
d319704a | Moritz Bunkus | |||
$form->{exchangerate} =
|
||||
532b0d4c | Moritz Bunkus | $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate}, "buy");
|
||
d319704a | Moritz Bunkus | |||
532b0d4c | Moritz Bunkus | # get shipto
|
||
$query = qq|SELECT * FROM shipto WHERE (trans_id = ?) AND (module = 'AR')|;
|
||||
$ref = selectfirst_hashref_query($form, $dbh, $query, $id);
|
||||
delete $ref->{id};
|
||||
map { $form->{$_} = $ref->{$_} } keys %{ $ref };
|
||||
foreach my $vc (qw(customer vendor)) {
|
||||
next if !$form->{"delivery_${vc}_id"};
|
||||
($form->{"delivery_${vc}_string"})
|
||||
= selectrow_query($form, $dbh, qq|SELECT name FROM customer WHERE id = ?|, $id);
|
||||
54e4131e | Moritz Bunkus | }
|
||
d319704a | Moritz Bunkus | # get printed, emailed
|
||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|SELECT printed, emailed, spoolfile, formname
|
||||
FROM status
|
||||
WHERE trans_id = ?|;
|
||||
$sth = prepare_execute_query($form, $dbh, $query, $id);
|
||||
d319704a | Moritz Bunkus | |||
while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
|
||||
$form->{printed} .= "$ref->{formname} " if $ref->{printed};
|
||||
$form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
|
||||
$form->{queued} .= "$ref->{formname} $ref->{spoolfile} "
|
||||
if $ref->{spoolfile};
|
||||
}
|
||||
$sth->finish;
|
||||
map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
|
||||
2e5a8be3 | Moritz Bunkus | my $transdate =
|
||
$form->{deliverydate} ? $dbh->quote($form->{deliverydate}) :
|
||||
532b0d4c | Moritz Bunkus | $form->{invdate} ? $dbh->quote($form->{invdate}) :
|
||
"current_date";
|
||||
my $taxzone_id = $form->{taxzone_id} *= 1;
|
||||
$taxzone_id = 0 if (0 > $taxzone_id) || (3 < $taxzone_id);
|
||||
54e4131e | Moritz Bunkus | |||
d319704a | Moritz Bunkus | # retrieve individual items
|
||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|SELECT
|
||||
c1.accno AS inventory_accno,
|
||||
c1.new_chart_id AS inventory_new_chart,
|
||||
date($transdate) - c1.valid_from AS inventory_valid,
|
||||
c2.accno AS income_accno,
|
||||
c2.new_chart_id AS income_new_chart,
|
||||
date($transdate) - c2.valid_from as income_valid,
|
||||
c3.accno AS expense_accno,
|
||||
c3.new_chart_id AS expense_new_chart,
|
||||
date($transdate) - c3.valid_from AS expense_valid,
|
||||
i.description, i.longdescription, i.qty, i.fxsellprice AS sellprice,
|
||||
i.discount, i.parts_id AS id, i.unit, i.deliverydate,
|
||||
i.project_id, i.serialnumber, i.id AS invoice_pos, i.pricegroup_id,
|
||||
4d8a6515 | Philip Reetz | i.ordnumber, i.transdate, i.cusordnumber, i.subtotal, i.lastcost,
|
||
532b0d4c | Moritz Bunkus | |||
p.partnumber, p.assembly, p.bin, p.notes AS partnotes,
|
||||
p.inventory_accno_id AS part_inventory_accno_id, p.formel,
|
||||
pr.projectnumber,
|
||||
pg.partsgroup,
|
||||
prg.pricegroup
|
||||
FROM invoice i
|
||||
LEFT JOIN parts p ON (i.parts_id = p.id)
|
||||
LEFT JOIN project pr ON (i.project_id = pr.id)
|
||||
LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
|
||||
LEFT JOIN pricegroup prg ON (i.pricegroup_id = prg.id)
|
||||
LEFT JOIN chart c1 ON
|
||||
((SELECT inventory_accno_id
|
||||
FROM buchungsgruppen
|
||||
WHERE id = p.buchungsgruppen_id) = c1.id)
|
||||
LEFT JOIN chart c2 ON
|
||||
((SELECT income_accno_id_${taxzone_id}
|
||||
FROM buchungsgruppen
|
||||
WHERE id=p.buchungsgruppen_id) = c2.id)
|
||||
LEFT JOIN chart c3 ON
|
||||
((SELECT expense_accno_id_${taxzone_id}
|
||||
FROM buchungsgruppen
|
||||
WHERE id = p.buchungsgruppen_id) = c3.id)
|
||||
WHERE (i.trans_id = ?)
|
||||
AND NOT (i.assemblyitem = '1')
|
||||
ORDER BY i.id|;
|
||||
$sth = prepare_execute_query($form, $dbh, $query, $id);
|
||||
54e4131e | Moritz Bunkus | |||
d319704a | Moritz Bunkus | while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
|
||
54e4131e | Moritz Bunkus | if (!$ref->{"part_inventory_accno_id"}) {
|
||
map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid));
|
||||
}
|
||||
delete($ref->{"part_inventory_accno_id"});
|
||||
d319704a | Moritz Bunkus | |||
532b0d4c | Moritz Bunkus | foreach my $type (qw(inventory income expense)) {
|
||
while ($ref->{"${type}_new_chart"} && ($ref->{"${type}_valid"} >=0)) {
|
||||
my $query =
|
||||
qq|SELECT accno, new_chart_id, date($transdate) - valid_from
|
||||
FROM chart
|
||||
WHERE id = ?|;
|
||||
($ref->{"${type}_accno"},
|
||||
$ref->{"${type}_new_chart"},
|
||||
$ref->{"${type}_valid"})
|
||||
= selectrow_query($form, $dbh, $query, $ref->{"${type}_new_chart"});
|
||||
}
|
||||
}
|
||||
54e4131e | Moritz Bunkus | |||
d319704a | Moritz Bunkus | # get tax rates and description
|
||
532b0d4c | Moritz Bunkus | my $accno_id =
|
||
80ec19fe | Moritz Bunkus | ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
|
||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber
|
||||
FROM tax t
|
||||
LEFT JOIN chart c ON (c.id = t.chart_id)
|
||||
WHERE t.id IN
|
||||
(SELECT tk.tax_id
|
||||
FROM taxkeys tk
|
||||
WHERE tk.chart_id = (SELECT id FROM chart WHERE accno = ?)
|
||||
6eea36b2 | Thomas Kasulke | AND startdate <= date($transdate)
|
||
532b0d4c | Moritz Bunkus | ORDER BY startdate DESC
|
||
LIMIT 1)
|
||||
ORDER BY c.accno|;
|
||||
my $stw = prepare_execute_query($form, $dbh, $query, $accno_id);
|
||||
d319704a | Moritz Bunkus | $ref->{taxaccounts} = "";
|
||
54e4131e | Moritz Bunkus | my $i=0;
|
||
d319704a | Moritz Bunkus | while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
|
||
# if ($customertax{$ref->{accno}}) {
|
||||
54e4131e | Moritz Bunkus | if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
|
||
$i++;
|
||||
$ptr->{accno} = $i;
|
||||
}
|
||||
d319704a | Moritz Bunkus | $ref->{taxaccounts} .= "$ptr->{accno} ";
|
||
54e4131e | Moritz Bunkus | |||
d319704a | Moritz Bunkus | if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
|
||
$form->{"$ptr->{accno}_rate"} = $ptr->{rate};
|
||||
54e4131e | Moritz Bunkus | $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription};
|
||
d319704a | Moritz Bunkus | $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
|
||
$form->{taxaccounts} .= "$ptr->{accno} ";
|
||||
}
|
||||
}
|
||||
if ($form->{lizenzen}) {
|
||||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|SELECT l.licensenumber, l.id AS licenseid
|
||||
FROM license l, licenseinvoice li
|
||||
WHERE l.id = li.license_id AND li.trans_id = ?|;
|
||||
my ($licensenumber, $licenseid)
|
||||
= selectrow_query($form, $dbh, $query, conv_i($ref->{invoice_pos}));
|
||||
$ref->{lizenzen} = "<option value=\"$licenseid\">$licensenumber</option>";
|
||||
54e4131e | Moritz Bunkus | }
|
||
d319704a | Moritz Bunkus | |||
532b0d4c | Moritz Bunkus | $ref->{qty} *= -1 if $form->{type} eq "credit_note";
|
||
d319704a | Moritz Bunkus | chop $ref->{taxaccounts};
|
||
push @{ $form->{invoice_details} }, $ref;
|
||||
$stw->finish;
|
||||
}
|
||||
$sth->finish;
|
||||
8e206587 | Moritz Bunkus | Common::webdav_folder($form) if ($main::webdav);
|
||
d319704a | Moritz Bunkus | }
|
||
my $rc = $dbh->commit;
|
||||
$dbh->disconnect;
|
||||
$main::lxdebug->leave_sub();
|
||||
return $rc;
|
||||
}
|
||||
sub get_customer {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form) = @_;
|
||||
# connect to database
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
my $dateformat = $myconfig->{dateformat};
|
||||
$dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
|
||||
532b0d4c | Moritz Bunkus | my (@values, $duedate, $ref, $query);
|
||
if ($form->{invdate}) {
|
||||
$duedate = "to_date(?, '$dateformat')";
|
||||
push @values, $form->{invdate};
|
||||
} else {
|
||||
$duedate = "current_date";
|
||||
}
|
||||
d319704a | Moritz Bunkus | |||
532b0d4c | Moritz Bunkus | my $cid = conv_i($form->{customer_id});
|
||
d319704a | Moritz Bunkus | |||
# get customer
|
||||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|SELECT
|
||||
c.name AS customer, c.discount, c.creditlimit, c.terms,
|
||||
c.email, c.cc, c.bcc, c.language_id, c.payment_id AS customer_payment_id,
|
||||
c.street, c.zipcode, c.city, c.country,
|
||||
c.notes AS intnotes, c.klass as customer_klass, c.taxzone_id, c.salesman_id,
|
||||
$duedate + COALESCE(pt.terms_netto, 0) AS duedate,
|
||||
b.discount AS tradediscount, b.description AS business
|
||||
FROM customer c
|
||||
LEFT JOIN business b ON (b.id = c.business_id)
|
||||
LEFT JOIN payment_terms pt ON (c.payment_id = pt.id)
|
||||
WHERE c.id = ?|;
|
||||
push @values, $cid;
|
||||
$ref = selectfirst_hashref_query($form, $dbh, $query, @values);
|
||||
d319704a | Moritz Bunkus | map { $form->{$_} = $ref->{$_} } keys %$ref;
|
||
54e4131e | Moritz Bunkus | |||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|SELECT sum(amount - paid) AS dunning_amount
|
||||
FROM ar
|
||||
WHERE (paid < amount)
|
||||
AND (customer_id = ?)
|
||||
AND (dunning_config_id IS NOT NULL)|;
|
||||
$ref = selectfirst_hashref_query($form, $dbh, $query, $cid);
|
||||
54e4131e | Moritz Bunkus | map { $form->{$_} = $ref->{$_} } keys %$ref;
|
||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|SELECT dnn.dunning_description AS max_dunning_level
|
||||
FROM dunning_config dnn
|
||||
WHERE id IN (SELECT dunning_config_id
|
||||
FROM ar
|
||||
WHERE (paid < amount) AND (customer_id = ?) AND (dunning_config_id IS NOT NULL))
|
||||
ORDER BY dunning_level DESC LIMIT 1|;
|
||||
$ref = selectfirst_hashref_query($form, $dbh, $query, $cid);
|
||||
54e4131e | Moritz Bunkus | map { $form->{$_} = $ref->{$_} } keys %$ref;
|
||
#check whether payment_terms are better than old payment_terms
|
||||
if (($form->{payment_id} ne "") && ($form->{customer_payment_id} ne "")) {
|
||||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|SELECT
|
||||
(SELECT ranking FROM payment_terms WHERE id = ?),
|
||||
(SELECT ranking FROM payment_terms WHERE id = ?)|;
|
||||
my ($old_ranking, $new_ranking)
|
||||
= selectrow_query($form, $dbh, $query, conv_i($form->{payment_id}), conv_i($form->{customer_payment_id}));
|
||||
54e4131e | Moritz Bunkus | if ($new_ranking > $old_ranking) {
|
||
532b0d4c | Moritz Bunkus | $form->{payment_id} = $form->{customer_payment_id};
|
||
54e4131e | Moritz Bunkus | }
|
||
}
|
||||
532b0d4c | Moritz Bunkus | |||
54e4131e | Moritz Bunkus | if ($form->{payment_id} eq "") {
|
||
532b0d4c | Moritz Bunkus | $form->{payment_id} = $form->{customer_payment_id};
|
||
54e4131e | Moritz Bunkus | }
|
||
d319704a | Moritz Bunkus | $form->{creditremaining} = $form->{creditlimit};
|
||
532b0d4c | Moritz Bunkus | $query = qq|SELECT SUM(amount - paid) FROM ar WHERE customer_id = ?|;
|
||
my ($value) = selectrow_query($form, $dbh, $query, $cid);
|
||||
$form->{creditremaining} -= $value;
|
||||
$query =
|
||||
qq|SELECT o.amount,
|
||||
(SELECT e.buy FROM exchangerate e
|
||||
WHERE e.curr = o.curr
|
||||
AND e.transdate = o.transdate)
|
||||
FROM oe o
|
||||
WHERE o.customer_id = ?
|
||||
AND o.quotation = '0'
|
||||
AND o.closed = '0'|;
|
||||
$sth = prepare_execute_query($form, $dbh, $query, $cid);
|
||||
d319704a | Moritz Bunkus | |||
while (my ($amount, $exch) = $sth->fetchrow_array) {
|
||||
$exch = 1 unless $exch;
|
||||
$form->{creditremaining} -= $amount * $exch;
|
||||
}
|
||||
$sth->finish;
|
||||
# get shipto if we did not converted an order or invoice
|
||||
if (!$form->{shipto}) {
|
||||
map { delete $form->{$_} }
|
||||
532b0d4c | Moritz Bunkus | qw(shiptoname shiptodepartment_1 shiptodepartment_2
|
||
shiptostreet shiptozipcode shiptocity shiptocountry
|
||||
shiptocontact shiptophone shiptofax shiptoemail);
|
||||
d319704a | Moritz Bunkus | |||
532b0d4c | Moritz Bunkus | $query = qq|SELECT * FROM shipto WHERE trans_id = ? AND module = 'CT'|;
|
||
$ref = selectfirst_hashref_query($form, $dbh, $query, $cid);
|
||||
delete $ref->{id};
|
||||
d319704a | Moritz Bunkus | map { $form->{$_} = $ref->{$_} } keys %$ref;
|
||
}
|
||||
# setup last accounts used for this customer
|
||||
if (!$form->{id} && $form->{type} !~ /_(order|quotation)/) {
|
||||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|SELECT c.id, c.accno, c.description, c.link, c.category
|
||||
FROM chart c
|
||||
JOIN acc_trans ac ON (ac.chart_id = c.id)
|
||||
JOIN ar a ON (a.id = ac.trans_id)
|
||||
WHERE a.customer_id = ?
|
||||
AND NOT (c.link LIKE '%_tax%' OR c.link LIKE '%_paid%')
|
||||
AND a.id IN (SELECT max(a2.id) FROM ar a2 WHERE a2.customer_id = ?)|;
|
||||
$sth = prepare_execute_query($form, $dbh, $query, $cid, $cid);
|
||||
d319704a | Moritz Bunkus | |||
my $i = 0;
|
||||
while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
|
||||
if ($ref->{category} eq 'I') {
|
||||
$i++;
|
||||
$form->{"AR_amount_$i"} = "$ref->{accno}--$ref->{description}";
|
||||
f1c1921d | Moritz Bunkus | |||
if ($form->{initial_transdate}) {
|
||||
my $tax_query =
|
||||
532b0d4c | Moritz Bunkus | qq|SELECT tk.tax_id, t.rate
|
||
FROM taxkeys tk
|
||||
LEFT JOIN tax t ON tk.tax_id = t.id
|
||||
6eea36b2 | Thomas Kasulke | WHERE (tk.chart_id = ?) AND (startdate <= date(?))
|
||
532b0d4c | Moritz Bunkus | ORDER BY tk.startdate DESC
|
||
LIMIT 1|;
|
||||
f1c1921d | Moritz Bunkus | my ($tax_id, $rate) =
|
||
selectrow_query($form, $dbh, $tax_query, $ref->{id},
|
||||
$form->{initial_transdate});
|
||||
$form->{"taxchart_$i"} = "${tax_id}--${rate}";
|
||||
}
|
||||
d319704a | Moritz Bunkus | }
|
||
if ($ref->{category} eq 'A') {
|
||||
3253777d | Moritz Bunkus | $form->{ARselected} = $form->{AR_1} = $ref->{accno};
|
||
d319704a | Moritz Bunkus | }
|
||
}
|
||||
$sth->finish;
|
||||
$form->{rowcount} = $i if ($i && !$form->{type});
|
||||
}
|
||||
$dbh->disconnect;
|
||||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
sub retrieve_item {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form) = @_;
|
||||
2e5a8be3 | Moritz Bunkus | # connect to database
|
||
my $dbh = $form->dbconnect($myconfig);
|
||||
d319704a | Moritz Bunkus | my $i = $form->{rowcount};
|
||
532b0d4c | Moritz Bunkus | my $where = qq|NOT p.obsolete = '1'|;
|
||
my @values;
|
||||
d319704a | Moritz Bunkus | |||
532b0d4c | Moritz Bunkus | foreach my $column (qw(p.partnumber p.description pgpartsgroup)) {
|
||
my ($table, $field) = split m/\./, $column;
|
||||
next if !$form->{"${field}_${i}"};
|
||||
$where .= qq| AND lower(${column}) ILIKE ?|;
|
||||
push @values, '%' . $form->{"${field}_${i}"} . '%';
|
||||
d319704a | Moritz Bunkus | }
|
||
if ($form->{"description_$i"}) {
|
||||
532b0d4c | Moritz Bunkus | $where .= qq| ORDER BY p.description|;
|
||
d319704a | Moritz Bunkus | } else {
|
||
532b0d4c | Moritz Bunkus | $where .= qq| ORDER BY p.partnumber|;
|
||
d319704a | Moritz Bunkus | }
|
||
2e5a8be3 | Moritz Bunkus | my $transdate;
|
||
54e4131e | Moritz Bunkus | if ($form->{type} eq "invoice") {
|
||
2e5a8be3 | Moritz Bunkus | $transdate =
|
||
$form->{deliverydate} ? $dbh->quote($form->{deliverydate}) :
|
||||
532b0d4c | Moritz Bunkus | $form->{invdate} ? $dbh->quote($form->{invdate}) :
|
||
"current_date";
|
||||
2e5a8be3 | Moritz Bunkus | } else {
|
||
$transdate =
|
||||
532b0d4c | Moritz Bunkus | $form->{transdate} ? $dbh->quote($form->{transdate}) :
|
||
"current_date";
|
||||
54e4131e | Moritz Bunkus | }
|
||
532b0d4c | Moritz Bunkus | my $taxzone_id = $form->{taxzone_id} * 1;
|
||
$taxzone_id = 0 if (0 > $taxzone_id) || (3 < $taxzone_id);
|
||||
my $query =
|
||||
qq|SELECT
|
||||
p.id, p.partnumber, p.description, p.sellprice,
|
||||
c9b8ad08 | Philip Reetz | p.listprice, p.inventory_accno_id, p.lastcost,
|
||
532b0d4c | Moritz Bunkus | |||
c1.accno AS inventory_accno,
|
||||
c1.new_chart_id AS inventory_new_chart,
|
||||
date($transdate) - c1.valid_from AS inventory_valid,
|
||||
c2.accno AS income_accno,
|
||||
c2.new_chart_id AS income_new_chart,
|
||||
date($transdate) - c2.valid_from AS income_valid,
|
||||
c3.accno AS expense_accno,
|
||||
c3.new_chart_id AS expense_new_chart,
|
||||
date($transdate) - c3.valid_from AS expense_valid,
|
||||
p.unit, p.assembly, p.bin, p.onhand,
|
||||
p.notes AS partnotes, p.notes AS longdescription,
|
||||
p.not_discountable, p.formel, p.payment_id AS part_payment_id,
|
||||
pg.partsgroup
|
||||
FROM parts p
|
||||
LEFT JOIN chart c1 ON
|
||||
((SELECT inventory_accno_id
|
||||
FROM buchungsgruppen
|
||||
WHERE id = p.buchungsgruppen_id) = c1.id)
|
||||
LEFT JOIN chart c2 ON
|
||||
((SELECT income_accno_id_${taxzone_id}
|
||||
FROM buchungsgruppen
|
||||
WHERE id = p.buchungsgruppen_id) = c2.id)
|
||||
LEFT JOIN chart c3 ON
|
||||
((SELECT expense_accno_id_${taxzone_id}
|
||||
FROM buchungsgruppen
|
||||
WHERE id = p.buchungsgruppen_id) = c3.id)
|
||||
LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
|
||||
WHERE $where|;
|
||||
my $sth = prepare_execute_query($form, $dbh, $query, @values);
|
||||
d319704a | Moritz Bunkus | |||
while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
|
||||
54e4131e | Moritz Bunkus | # In der Buchungsgruppe ist immer ein Bestandskonto verknuepft, auch wenn
|
||
# es sich um eine Dienstleistung handelt. Bei Dienstleistungen muss das
|
||||
# Buchungskonto also aus dem Ergebnis rausgenommen werden.
|
||||
if (!$ref->{inventory_accno_id}) {
|
||||
map({ delete($ref->{"inventory_${_}"}); } qw(accno new_chart valid));
|
||||
}
|
||||
delete($ref->{inventory_accno_id});
|
||||
532b0d4c | Moritz Bunkus | foreach my $type (qw(inventory income expense)) {
|
||
while ($ref->{"${type}_new_chart"} && ($ref->{"${type}_valid"} >=0)) {
|
||||
my $query =
|
||||
qq|SELECT accno, new_chart_id, date($transdate) - valid_from
|
||||
FROM chart
|
||||
WHERE id = ?|;
|
||||
($ref->{"${type}_accno"},
|
||||
$ref->{"${type}_new_chart"},
|
||||
$ref->{"${type}_valid"})
|
||||
= selectrow_query($form, $dbh, $query, $ref->{"${type}_new_chart"});
|
||||
}
|
||||
54e4131e | Moritz Bunkus | }
|
||
#check whether payment_terms are better than old payment_terms
|
||||
532b0d4c | Moritz Bunkus | if (($form->{payment_id} ne "") && ($form->{part_payment_id} ne "")) {
|
||
$query =
|
||||
qq|SELECT
|
||||
(SELECT ranking FROM payment_terms WHERE id = ?),
|
||||
(SELECT ranking FROM payment_terms WHERE id = ?)|;
|
||||
my ($old_ranking, $new_ranking)
|
||||
= selectrow_query($form, $dbh, $query, conv_i($form->{payment_id}), conv_i($form->{part_payment_id}));
|
||||
if ($new_ranking > $old_ranking) {
|
||||
$form->{payment_id} = $form->{customer_payment_id};
|
||||
54e4131e | Moritz Bunkus | }
|
||
}
|
||||
532b0d4c | Moritz Bunkus | if ($form->{payment_id} eq "") {
|
||
$form->{payment_id} = $form->{part_payment_id};
|
||||
}
|
||||
d319704a | Moritz Bunkus | # get tax rates and description
|
||
532b0d4c | Moritz Bunkus | $accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
|
||
$query =
|
||||
qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber
|
||||
FROM tax t
|
||||
LEFT JOIN chart c ON (c.id = t.chart_id)
|
||||
WHERE t.id in
|
||||
(SELECT tk.tax_id
|
||||
FROM taxkeys tk
|
||||
WHERE tk.chart_id = (SELECT id from chart WHERE accno = ?)
|
||||
AND startdate <= ?
|
||||
ORDER BY startdate DESC
|
||||
LIMIT 1)
|
||||
ORDER BY c.accno|;
|
||||
6eea36b2 | Thomas Kasulke | @values = ($accno_id, $transdate eq "current_date" ? "now" : $transdate);
|
||
d319704a | Moritz Bunkus | $stw = $dbh->prepare($query);
|
||
532b0d4c | Moritz Bunkus | $stw->execute(@values) || $form->dberror($query);
|
||
d319704a | Moritz Bunkus | |||
$ref->{taxaccounts} = "";
|
||||
54e4131e | Moritz Bunkus | my $i = 0;
|
||
d319704a | Moritz Bunkus | while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
|
||
# if ($customertax{$ref->{accno}}) {
|
||||
54e4131e | Moritz Bunkus | if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
|
||
$i++;
|
||||
$ptr->{accno} = $i;
|
||||
}
|
||||
d319704a | Moritz Bunkus | $ref->{taxaccounts} .= "$ptr->{accno} ";
|
||
54e4131e | Moritz Bunkus | |||
d319704a | Moritz Bunkus | if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
|
||
$form->{"$ptr->{accno}_rate"} = $ptr->{rate};
|
||||
54e4131e | Moritz Bunkus | $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription};
|
||
d319704a | Moritz Bunkus | $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
|
||
$form->{taxaccounts} .= "$ptr->{accno} ";
|
||||
}
|
||||
}
|
||||
$stw->finish;
|
||||
chop $ref->{taxaccounts};
|
||||
54e4131e | Moritz Bunkus | if ($form->{language_id}) {
|
||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|SELECT tr.translation, tr.longdescription
|
||||
FROM translation tr
|
||||
WHERE tr.language_id = ? AND tr.parts_id = ?|;
|
||||
@values = (conv_i($form->{language_id}), conv_i($ref->{id}));
|
||||
my ($translation, $longdescription) = selectrow_query($form, $dbh, $query, @values);
|
||||
54e4131e | Moritz Bunkus | if ($translation ne "") {
|
||
$ref->{description} = $translation;
|
||||
$ref->{longdescription} = $longdescription;
|
||||
} else {
|
||||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|SELECT tr.translation, tr.longdescription
|
||||
FROM translation tr
|
||||
WHERE tr.language_id IN
|
||||
(SELECT id
|
||||
FROM language
|
||||
WHERE article_code = (SELECT article_code FROM language WHERE id = ?))
|
||||
AND tr.parts_id = ?
|
||||
LIMIT 1|;
|
||||
@values = (conv_i($form->{language_id}), conv_i($ref->{id}));
|
||||
my ($translation, $longdescription) = selectrow_query($form, $dbh, $query, @values);
|
||||
54e4131e | Moritz Bunkus | if ($translation ne "") {
|
||
$ref->{description} = $translation;
|
||||
$ref->{longdescription} = $longdescription;
|
||||
}
|
||||
}
|
||||
}
|
||||
d319704a | Moritz Bunkus | |||
push @{ $form->{item_list} }, $ref;
|
||||
if ($form->{lizenzen}) {
|
||||
if ($ref->{inventory_accno} > 0) {
|
||||
$query =
|
||||
532b0d4c | Moritz Bunkus | qq|SELECT l.*
|
||
FROM license l
|
||||
WHERE l.parts_id = ? AND NOT l.id IN (SELECT li.license_id FROM licenseinvoice li)|;
|
||||
my $stw = prepare_execute_query($form, $dbh, $query, conv_i($ref->{id}));
|
||||
while (my $ptr = $stw->fetchrow_hashref(NAME_lc)) {
|
||||
d319704a | Moritz Bunkus | push @{ $form->{LIZENZEN}{ $ref->{id} } }, $ptr;
|
||
}
|
||||
$stw->finish;
|
||||
}
|
||||
}
|
||||
}
|
||||
$sth->finish;
|
||||
$dbh->disconnect;
|
||||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
07d71c33 | Stephan Köhler | ##########################
|
||
# get pricegroups from database
|
||||
# build up selected pricegroup
|
||||
0bb0eb67 | Stephan Köhler | # if an exchange rate - change price
|
||
07d71c33 | Stephan Köhler | # for each part
|
||
#
|
||||
sub get_pricegroups_for_parts {
|
||||
0bb0eb67 | Stephan Köhler | |||
07d71c33 | Stephan Köhler | $main::lxdebug->enter_sub();
|
||
my ($self, $myconfig, $form) = @_;
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
604959de | Moritz Bunkus | $form->{"PRICES"} = {};
|
||
07d71c33 | Stephan Köhler | my $i = 1;
|
||
my $id = 0;
|
||||
54e4131e | Moritz Bunkus | my $dimension_units = AM->retrieve_units($myconfig, $form, "dimension");
|
||
my $service_units = AM->retrieve_units($myconfig, $form, "service");
|
||||
my $all_units = AM->retrieve_units($myconfig, $form);
|
||||
07d71c33 | Stephan Köhler | while (($form->{"id_$i"}) or ($form->{"new_id_$i"})) {
|
||
604959de | Moritz Bunkus | $form->{"PRICES"}{$i} = [];
|
||
07d71c33 | Stephan Köhler | |||
$id = $form->{"id_$i"};
|
||||
if (!($form->{"id_$i"}) and $form->{"new_id_$i"}) {
|
||||
$id = $form->{"new_id_$i"};
|
||||
}
|
||||
532b0d4c | Moritz Bunkus | ($price, $selectedpricegroup_id) = split(/--/,
|
||
$form->{"sellprice_pg_$i"});
|
||||
07d71c33 | Stephan Köhler | |||
$pricegroup_old = $form->{"pricegroup_old_$i"};
|
||||
54e4131e | Moritz Bunkus | $form->{"new_pricegroup_$i"} = $selectedpricegroup_id;
|
||
$form->{"old_pricegroup_$i"} = $pricegroup_old;
|
||||
07d71c33 | Stephan Köhler | |||
532b0d4c | Moritz Bunkus | $price_new = $form->{"price_new_$i"};
|
||
07d71c33 | Stephan Köhler | $price_old = $form->{"price_old_$i"};
|
||
54e4131e | Moritz Bunkus | if (!$form->{"unit_old_$i"}) {
|
||
# Neue Ware aus der Datenbank. In diesem Fall ist unit_$i die
|
||||
# Einheit, wie sie in den Stammdaten hinterlegt wurde.
|
||||
# Es sollte also angenommen werden, dass diese ausgewaehlt war.
|
||||
$form->{"unit_old_$i"} = $form->{"unit_$i"};
|
||||
}
|
||||
532b0d4c | Moritz Bunkus | |||
54e4131e | Moritz Bunkus | # Die zuletzt ausgewaehlte mit der aktuell ausgewaehlten Einheit
|
||
# vergleichen und bei Unterschied den Preis entsprechend umrechnen.
|
||||
$form->{"selected_unit_$i"} = $form->{"unit_$i"} unless ($form->{"selected_unit_$i"});
|
||||
my $check_units = $form->{"inventory_accno_$i"} ? $dimension_units : $service_units;
|
||||
if (!$check_units->{$form->{"selected_unit_$i"}} ||
|
||||
($check_units->{$form->{"selected_unit_$i"}}->{"base_unit"} ne
|
||||
$all_units->{$form->{"unit_old_$i"}}->{"base_unit"})) {
|
||||
# Die ausgewaehlte Einheit ist fuer diesen Artikel nicht gueltig
|
||||
# (z.B. Dimensionseinheit war ausgewaehlt, es handelt sich aber
|
||||
# um eine Dienstleistung). Dann keinerlei Umrechnung vornehmen.
|
||||
$form->{"unit_old_$i"} = $form->{"selected_unit_$i"} = $form->{"unit_$i"};
|
||||
}
|
||||
532b0d4c | Moritz Bunkus | |||
54e4131e | Moritz Bunkus | my $basefactor = 1;
|
||
532b0d4c | Moritz Bunkus | |||
54e4131e | Moritz Bunkus | if ($form->{"unit_old_$i"} ne $form->{"selected_unit_$i"}) {
|
||
if (defined($all_units->{$form->{"unit_old_$i"}}->{"factor"}) &&
|
||||
$all_units->{$form->{"unit_old_$i"}}->{"factor"}) {
|
||||
$basefactor = $all_units->{$form->{"selected_unit_$i"}}->{"factor"} /
|
||||
$all_units->{$form->{"unit_old_$i"}}->{"factor"};
|
||||
}
|
||||
}
|
||||
532b0d4c | Moritz Bunkus | |||
54e4131e | Moritz Bunkus | if (!$form->{"basefactor_$i"}) {
|
||
$form->{"basefactor_$i"} = 1;
|
||||
}
|
||||
07d71c33 | Stephan Köhler | |||
532b0d4c | Moritz Bunkus | $query =
|
||
qq|SELECT
|
||||
pricegroup_id,
|
||||
(SELECT p.sellprice FROM parts p WHERE p.id = ?) AS default_sellprice,
|
||||
(SELECT pg.pricegroup FROM pricegroup pg WHERE id = pricegroup_id) AS pricegroup,
|
||||
price,
|
||||
'' AS selected
|
||||
FROM prices
|
||||
WHERE parts_id = ?
|
||||
UNION
|
||||
SELECT
|
||||
0 as pricegroup_id,
|
||||
(SELECT sellprice FROM parts WHERE id = ?) AS default_sellprice,
|
||||
'' AS pricegroup,
|
||||
(SELECT DISTINCT sellprice FROM parts where id = ?) AS price,
|
||||
'selected' AS selected
|
||||
FROM prices
|
||||
ORDER BY pricegroup|;
|
||||
@values = (conv_i($id), conv_i($id), conv_i($id), conv_i($id));
|
||||
my $pkq = prepare_execute_query($form, $dbh, $query, @values);
|
||||
while ($pkr = $pkq->fetchrow_hashref(NAME_lc)) {
|
||||
0bb0eb67 | Stephan Köhler | $pkr->{id} = $id;
|
||
$pkr->{selected} = '';
|
||||
# if there is an exchange rate change price
|
||||
07d71c33 | Stephan Köhler | if (($form->{exchangerate} * 1) != 0) {
|
||
0bb0eb67 | Stephan Köhler | |||
07d71c33 | Stephan Köhler | $pkr->{price} /= $form->{exchangerate};
|
||
}
|
||||
54e4131e | Moritz Bunkus | |||
$pkr->{price} *= $form->{"basefactor_$i"};
|
||||
532b0d4c | Moritz Bunkus | |||
54e4131e | Moritz Bunkus | $pkr->{price} *= $basefactor;
|
||
532b0d4c | Moritz Bunkus | |||
0bb0eb67 | Stephan Köhler | $pkr->{price} = $form->format_amount($myconfig, $pkr->{price}, 5);
|
||
07d71c33 | Stephan Köhler | |||
if ($selectedpricegroup_id eq undef) {
|
||||
if ($pkr->{pricegroup_id} eq $form->{customer_klass}) {
|
||||
0bb0eb67 | Stephan Köhler | |||
07d71c33 | Stephan Köhler | $pkr->{selected} = ' selected';
|
||
0bb0eb67 | Stephan Köhler | # no customer pricesgroup set
|
||
07d71c33 | Stephan Köhler | if ($pkr->{price} == $pkr->{default_sellprice}) {
|
||
0bb0eb67 | Stephan Köhler | $pkr->{price} = $form->{"sellprice_$i"};
|
||
07d71c33 | Stephan Köhler | |||
} else {
|
||||
0bb0eb67 | Stephan Köhler | |||
$form->{"sellprice_$i"} = $pkr->{price};
|
||||
07d71c33 | Stephan Köhler | }
|
||
532b0d4c | Moritz Bunkus | } elsif ($pkr->{price} == $pkr->{default_sellprice}) {
|
||
$pkr->{price} = $form->{"sellprice_$i"};
|
||||
$pkr->{selected} = ' selected';
|
||||
07d71c33 | Stephan Köhler | }
|
||
}
|
||||
54e4131e | Moritz Bunkus | |||
0bb0eb67 | Stephan Köhler | if ($selectedpricegroup_id or $selectedpricegroup_id == 0) {
|
||
07d71c33 | Stephan Köhler | if ($selectedpricegroup_id ne $pricegroup_old) {
|
||
if ($pkr->{pricegroup_id} eq $selectedpricegroup_id) {
|
||||
54e4131e | Moritz Bunkus | $pkr->{selected} = ' selected';
|
||
07d71c33 | Stephan Köhler | }
|
||
532b0d4c | Moritz Bunkus | } elsif (($price_new != $form->{"sellprice_$i"}) and ($price_new ne 0)) {
|
||
if ($pkr->{pricegroup_id} == 0) {
|
||||
$pkr->{price} = $form->{"sellprice_$i"};
|
||||
$pkr->{selected} = ' selected';
|
||||
}
|
||||
} elsif ($pkr->{pricegroup_id} eq $selectedpricegroup_id) {
|
||||
$pkr->{selected} = ' selected';
|
||||
if ( ($pkr->{pricegroup_id} == 0)
|
||||
and ($pkr->{price} == $form->{"sellprice_$i"})) {
|
||||
# $pkr->{price} = $form->{"sellprice_$i"};
|
||||
07d71c33 | Stephan Köhler | } else {
|
||
532b0d4c | Moritz Bunkus | $pkr->{price} = $form->{"sellprice_$i"};
|
||
07d71c33 | Stephan Köhler | }
|
||
}
|
||||
}
|
||||
54e4131e | Moritz Bunkus | push @{ $form->{PRICES}{$i} }, $pkr;
|
||
07d71c33 | Stephan Köhler | }
|
||
54e4131e | Moritz Bunkus | $form->{"basefactor_$i"} *= $basefactor;
|
||
07d71c33 | Stephan Köhler | $i++;
|
||
$pkq->finish;
|
||||
}
|
||||
$dbh->disconnect;
|
||||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
95155b0d | Moritz Bunkus | sub has_storno {
|
||
$main::lxdebug->enter_sub();
|
||||
a1a3bfd8 | Moritz Bunkus | my ($self, $myconfig, $form, $table) = @_;
|
||
95155b0d | Moritz Bunkus | |||
$main::lxdebug->leave_sub() and return 0 unless ($form->{id});
|
||||
1fa91538 | Sven Schöling | # make sure there's no funny stuff in $table
|
||
# ToDO: die when this happens and throw an error
|
||||
$main::lxdebug->leave_sub() and return 0 if ($table =~ /\W/);
|
||||
95155b0d | Moritz Bunkus | my $dbh = $form->dbconnect($myconfig);
|
||
805e8da4 | Sven Schöling | my $query = qq|SELECT storno FROM $table WHERE storno_id = ?|;
|
||
my ($result) = selectrow_query($form, $dbh, $query, $form->{id});
|
||||
$dbh->disconnect();
|
||||
$main::lxdebug->leave_sub();
|
||||
return $result;
|
||||
}
|
||||
sub is_storno {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form, $table) = @_;
|
||||
$main::lxdebug->leave_sub() and return 0 unless ($form->{id});
|
||||
# make sure there's no funny stuff in $table
|
||||
# ToDO: die when this happens and throw an error
|
||||
$main::lxdebug->leave_sub() and return 0 if ($table =~ /\W/);
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
a1a3bfd8 | Moritz Bunkus | my $query = qq|SELECT storno FROM $table WHERE id = ?|;
|
||
95155b0d | Moritz Bunkus | my ($result) = selectrow_query($form, $dbh, $query, $form->{id});
|
||
$dbh->disconnect();
|
||||
$main::lxdebug->leave_sub();
|
||||
return $result;
|
||||
}
|
||||
d319704a | Moritz Bunkus | 1;
|