From a2c93e1cde65e60b17e0204183033896cca70a4f Mon Sep 17 00:00:00 2001 From: einhverfr Date: Mon, 2 Oct 2006 23:40:18 +0000 Subject: Broke out price matrix logic (warning, this is untested but cannot test from this machine so will be tested tonight) git-svn-id: https://ledger-smb.svn.sourceforge.net/svnroot/ledger-smb/trunk@183 4979c152-3d1c-0410-bac9-87ea11338e46 --- LedgerSMB/IR.pm | 42 ++-------- LedgerSMB/IS.pm | 125 ++--------------------------- LedgerSMB/JC.pm | 48 +---------- LedgerSMB/OE.pm | 173 +--------------------------------------- LedgerSMB/PriceMatrix.pm | 204 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 220 insertions(+), 372 deletions(-) create mode 100644 LedgerSMB/PriceMatrix.pm (limited to 'LedgerSMB') diff --git a/LedgerSMB/IR.pm b/LedgerSMB/IR.pm index b2acfa72..92e2c964 100755 --- a/LedgerSMB/IR.pm +++ b/LedgerSMB/IR.pm @@ -32,7 +32,7 @@ #====================================================================== package IR; - +use LedgerSMB::PriceMatrix; sub post_invoice { my ($self, $myconfig, $form) = @_; @@ -850,11 +850,7 @@ sub retrieve_invoice { &exchangerate_defaults($dbh, $form); # price matrix and vendor partnumber - $query = qq|SELECT partnumber - FROM partsvendor - WHERE parts_id = ? - AND vendor_id = $form->{vendor_id}|; - my $pmh = $dbh->prepare($query) || $form->dberror($query); + my $pmh = PriceMatrix::PriceMatrixQuery($dbh, $form); # tax rates for part $query = qq|SELECT c.accno @@ -885,7 +881,7 @@ sub retrieve_invoice { # price matrix $ref->{sellprice} = $form->round_amount($ref->{fxsellprice} * $form->{$form->{currency}}, $decimalplaces); - &price_matrix($pmh, $ref, $decimalplaces, $form); + PriceMatrix::price_matrix($pmh, $ref, $decimalplaces, $form, $myconfig); $ref->{sellprice} = $ref->{fxsellprice}; $ref->{qty} *= -1; @@ -974,11 +970,7 @@ sub retrieve_item { my $tth = $dbh->prepare($query) || $form->dberror($query); # price matrix - $query = qq|SELECT p.* - FROM partsvendor p - WHERE p.parts_id = ? - AND vendor_id = $form->{vendor_id}|; - my $pmh = $dbh->prepare($query) || $form->dberror($query); + my $pmh = PriceMatrix::price_matrix_query($dbh, $form); my $ref; my $ptref; @@ -1000,7 +992,7 @@ sub retrieve_item { chop $ref->{taxaccounts}; # get vendor price and partnumber - &price_matrix($pmh, $ref, $decimalplaces, $form); + PriceMatrix::price_matrix($pmh, $ref, $decimalplaces, $form, $myconfig); $ref->{description} = $ref->{translation} if $ref->{translation}; $ref->{partsgroup} = $ref->{grouptranslation} if $ref->{grouptranslation}; @@ -1060,30 +1052,6 @@ sub exchangerate_defaults { } -sub price_matrix { - my ($pmh, $ref, $decimalplaces, $form) = @_; - - $pmh->execute($ref->{id}); - my $mref = $pmh->fetchrow_hashref(NAME_lc); - - if ($mref->{partnumber} ne "") { - $ref->{partnumber} = $mref->{partnumber}; - } - - if ($mref->{lastcost}) { - # do a conversion - $ref->{sellprice} = $form->round_amount($mref->{lastcost} * $form->{$mref->{curr}}, $decimalplaces); - } - $pmh->finish; - - $ref->{sellprice} *= 1; - - # add 0:price to matrix - $ref->{pricematrix} = "0:$ref->{sellprice}"; - -} - - sub vendor_details { my ($self, $myconfig, $form) = @_; diff --git a/LedgerSMB/IS.pm b/LedgerSMB/IS.pm index a8a7f811..eda3a984 100755 --- a/LedgerSMB/IS.pm +++ b/LedgerSMB/IS.pm @@ -32,7 +32,7 @@ #====================================================================== package IS; - +use LedgerSMB::PriceMatrix; sub invoice_details { my ($self, $myconfig, $form) = @_; @@ -1377,7 +1377,7 @@ sub retrieve_invoice { &exchangerate_defaults($dbh, $form); # query for price matrix - my $pmh = &price_matrix_query($dbh, $form); + my $pmh = PriceMatrix::price_matrix_query($dbh, $form); # taxes $query = qq|SELECT c.accno @@ -1409,7 +1409,7 @@ sub retrieve_invoice { # price matrix $ref->{sellprice} = ($ref->{fxsellprice} * $form->{$form->{currency}}); - &price_matrix($pmh, $ref, $form->{transdate}, $decimalplaces, $form, $myconfig); + PriceMatrix::price_matrix($pmh, $ref, $form->{transdate}, $decimalplaces, $form, $myconfig); $ref->{sellprice} = $ref->{fxsellprice}; $ref->{partsgroup} = $ref->{partsgrouptranslation} if $ref->{partsgrouptranslation}; @@ -1503,7 +1503,7 @@ sub retrieve_item { # price matrix - my $pmh = &price_matrix_query($dbh, $form); + my $pmh = PriceMatrix::price_matrix_query($dbh, $form); my $transdate = $form->datetonum($myconfig, $form->{transdate}); @@ -1524,7 +1524,7 @@ sub retrieve_item { chop $ref->{taxaccounts}; # get matrix - &price_matrix($pmh, $ref, $transdate, $decimalplaces, $form, $myconfig); + PriceMatrix::price_matrix($pmh, $ref, $transdate, $decimalplaces, $form, $myconfig); $ref->{description} = $ref->{translation} if $ref->{translation}; $ref->{partsgroup} = $ref->{grouptranslation} if $ref->{grouptranslation}; @@ -1539,121 +1539,6 @@ sub retrieve_item { } -sub price_matrix_query { - my ($dbh, $form) = @_; - - my $query = qq|SELECT p.id AS parts_id, 0 AS customer_id, 0 AS pricegroup_id, - 0 AS pricebreak, p.sellprice, NULL AS validfrom, NULL AS validto, - '$form->{defaultcurrency}' AS curr, '' AS pricegroup - FROM parts p - WHERE p.id = ? - - UNION - - SELECT p.*, g.pricegroup - FROM partscustomer p - LEFT JOIN pricegroup g ON (g.id = p.pricegroup_id) - WHERE p.parts_id = ? - AND p.customer_id = $form->{customer_id} - - UNION - - SELECT p.*, g.pricegroup - FROM partscustomer p - LEFT JOIN pricegroup g ON (g.id = p.pricegroup_id) - JOIN customer c ON (c.pricegroup_id = g.id) - WHERE p.parts_id = ? - AND c.id = $form->{customer_id} - - UNION - - SELECT p.*, '' AS pricegroup - FROM partscustomer p - WHERE p.customer_id = 0 - AND p.pricegroup_id = 0 - AND p.parts_id = ? - - ORDER BY customer_id DESC, pricegroup_id DESC, pricebreak - - |; - my $sth = $dbh->prepare($query) || $form->dberror($query); - - $sth; - -} - - -sub price_matrix { - my ($pmh, $ref, $transdate, $decimalplaces, $form, $myconfig) = @_; - - $pmh->execute($ref->{id}, $ref->{id}, $ref->{id}, $ref->{id}); - - $ref->{pricematrix} = ""; - - my $customerprice; - my $pricegroupprice; - my $sellprice; - my $baseprice; - my $mref; - my %p = (); - my $i = 0; - - while ($mref = $pmh->fetchrow_hashref(NAME_lc)) { - - # check date - if ($mref->{validfrom}) { - next if $transdate < $form->datetonum($myconfig, $mref->{validfrom}); - } - if ($mref->{validto}) { - next if $transdate > $form->datetonum($myconfig, $mref->{validto}); - } - - # convert price - $sellprice = $form->round_amount($mref->{sellprice} * $form->{$mref->{curr}}, $decimalplaces); - - $mref->{pricebreak} *= 1; - - if ($mref->{customer_id}) { - $p{$mref->{pricebreak}} = $sellprice; - $customerprice = 1; - } - - if ($mref->{pricegroup_id}) { - if (!$customerprice) { - $p{$mref->{pricebreak}} = $sellprice; - $pricegroupprice = 1; - } - } - - if (!$customerprice && !$pricegroupprice) { - $p{$mref->{pricebreak}} = $sellprice; - } - - if (($mref->{pricebreak} + $mref->{customer_id} + $mref->{pricegroup_id}) == 0) { - $baseprice = $sellprice; - } - - $i++; - - } - $pmh->finish; - - if (! exists $p{0}) { - $p{0} = $baseprice; - } - - if ($i > 1) { - $ref->{sellprice} = $p{0}; - for (sort { $a <=> $b } keys %p) { $ref->{pricematrix} .= "${_}:$p{$_} " } - } else { - $ref->{sellprice} = $form->round_amount($p{0} * (1 - $form->{tradediscount}), $decimalplaces); - $ref->{pricematrix} = "0:$ref->{sellprice} " if $ref->{sellprice}; - } - chop $ref->{pricematrix}; - -} - - sub exchangerate_defaults { my ($dbh, $form) = @_; diff --git a/LedgerSMB/JC.pm b/LedgerSMB/JC.pm index b5c4d5d2..02fcb914 100755 --- a/LedgerSMB/JC.pm +++ b/LedgerSMB/JC.pm @@ -35,6 +35,7 @@ package JC; use LedgerSMB::IS; +use LedgerSMB::PriceMatrix; sub get_jcitems { my ($self, $myconfig, $form) = @_; @@ -268,12 +269,12 @@ sub jcparts { my $sth = $dbh->prepare($query); $sth->execute || $form->dberror($query); - my $pmh = price_matrix_query($dbh, $project_id, $customer_id); + my $pmh = PriceMatrix::price_matrix_query($dbh, $form); IS::exchangerate_defaults($dbh, $form); while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { $ref->{description} = $ref->{translation} if $ref->{translation}; - IS::price_matrix($pmh, $ref, $form->datetonum($form->{transdate}), 4, $form, $myconfig); + PriceMatrix::price_matrix($pmh, $ref, $form->{transdate}, 4, $form, $myconfig); push @{ $form->{all_parts} }, $ref; } $sth->finish; @@ -552,49 +553,6 @@ sub save { } -sub price_matrix_query { - my ($dbh, $project_id, $customer_id) = @_; - - my $query = qq|SELECT p.id AS parts_id, 0 AS customer_id, 0 AS pricegroup_id, - 0 AS pricebreak, p.sellprice, NULL AS validfrom, NULL AS validto, - (SELECT substr(curr,1,3) FROM defaults) AS curr, '' AS pricegroup - FROM parts p - WHERE p.id = ? - - UNION - - SELECT p.*, g.pricegroup - FROM partscustomer p - LEFT JOIN pricegroup g ON (g.id = p.pricegroup_id) - WHERE p.parts_id = ? - AND p.customer_id = $customer_id - - UNION - - SELECT p.*, g.pricegroup - FROM partscustomer p - LEFT JOIN pricegroup g ON (g.id = p.pricegroup_id) - JOIN customer c ON (c.pricegroup_id = g.id) - WHERE p.parts_id = ? - AND c.id = $customer_id - - UNION - - SELECT p.*, '' AS pricegroup - FROM partscustomer p - WHERE p.customer_id = 0 - AND p.pricegroup_id = 0 - AND p.parts_id = ? - - ORDER BY customer_id DESC, pricegroup_id DESC, pricebreak - - |; - my $sth = $dbh->prepare($query) || $form->dberror($query); - - $sth; - -} - 1; diff --git a/LedgerSMB/OE.pm b/LedgerSMB/OE.pm index 5ffac31a..9ebbad19 100755 --- a/LedgerSMB/OE.pm +++ b/LedgerSMB/OE.pm @@ -778,6 +778,7 @@ sub delete { sub retrieve { + use LedgerSMB::PriceMatrix; my ($self, $myconfig, $form) = @_; # connect to database @@ -874,7 +875,7 @@ sub retrieve { &exchangerate_defaults($dbh, $form); # query for price matrix - my $pmh = &price_matrix_query($dbh, $form); + my $pmh = PriceMatrix::price_matrix_query($dbh, $form); # taxes $query = qq| @@ -923,7 +924,7 @@ sub retrieve { } # partnumber and price matrix - &price_matrix( + PriceMatrix::price_matrix( $pmh, $ref, $form->{transdate}, $decimalplaces, $form, $myconfig); @@ -957,174 +958,6 @@ sub retrieve { } -sub price_matrix_query { - my ($dbh, $form) = @_; - - my $query; - my $sth; - - my @queryargs; - - if ($form->{customer_id}) { - my $defaultcurrency = $form->{dbh}->quote( - $form->{defaultcurrency}); - my $customer_id = $form->{dbh}->quote($form->{customer_id}); - $query = qq| - SELECT p.id AS parts_id, 0 AS customer_id, - 0 AS pricegroup_id, 0 AS pricebreak, - p.sellprice, NULL AS validfrom, NULL AS validto, - $defaultcurrency AS curr, '' AS pricegroup - FROM parts p - WHERE p.id = ? - - UNION - - SELECT p.*, g.pricegroup - FROM partscustomer p - LEFT JOIN pricegroup g ON (g.id = p.pricegroup_id) - WHERE p.parts_id = ? - AND p.customer_id = $customer_id - - UNION - - SELECT p.*, g.pricegroup - FROM partscustomer p - LEFT JOIN pricegroup g ON (g.id = p.pricegroup_id) - JOIN customer c ON (c.pricegroup_id = g.id) - WHERE p.parts_id = ? - AND c.id = $customer_id - - UNION - - SELECT p.*, '' AS pricegroup - FROM partscustomer p - WHERE p.customer_id = 0 - AND p.pricegroup_id = 0 - AND p.parts_id = ? - - ORDER BY customer_id DESC, pricegroup_id DESC, - pricebreak - |; - $sth = $dbh->prepare($query) || $form->dberror($query); - } elsif ($form->{vendor_id}) { - my $vendor_id = $form->{dbh}->quote($form->{vendor_id}); - # price matrix and vendor's partnumber - $query = qq| - SELECT partnumber - FROM partsvendor - WHERE parts_id = ? - AND vendor_id = $vendor_id|; - $sth = $dbh->prepare($query) || $form->dberror($query); - } - - $sth; -} - - -sub price_matrix { - my ($pmh, $ref, $transdate, $decimalplaces, $form, $myconfig) = @_; - $ref->{pricematrix} = ""; - my $customerprice; - my $pricegroupprice; - my $sellprice; - my $mref; - my %p = (); - - # depends if this is a customer or vendor - if ($form->{customer_id}) { - $pmh->execute($ref->{id}, $ref->{id}, $ref->{id}, $ref->{id}); - - while ($mref = $pmh->fetchrow_hashref(NAME_lc)) { - - # check date - if ($mref->{validfrom}) { - next if $transdate < $form->datetonum( - $myconfig, $mref->{validfrom}); - } - if ($mref->{validto}) { - next if $transdate > $form->datetonum( - $myconfig, $mref->{validto}); - } - - # convert price - $sellprice = $form->round_amount($mref->{sellprice} - * $form->{$mref->{curr}}, $decimalplaces); - - if ($mref->{customer_id}) { - $ref->{sellprice} = $sellprice - if !$mref->{pricebreak}; - $p{$mref->{pricebreak}} = $sellprice; - $customerprice = 1; - } - - if ($mref->{pricegroup_id}) { - if (! $customerprice) { - $ref->{sellprice} = $sellprice - if !$mref->{pricebreak}; - $p{$mref->{pricebreak}} = $sellprice; - } - $pricegroupprice = 1; - } - - if (!$customerprice && !$pricegroupprice) { - $p{$mref->{pricebreak}} = $sellprice; - } - - } - $pmh->finish; - - if (%p) { - if ($ref->{sellprice}) { - $p{0} = $ref->{sellprice}; - } - for (sort { $a <=> $b } keys %p) { - $ref->{pricematrix} .= "${_}:$p{$_} "; - } - } else { - if ($init) { - $ref->{sellprice} = $form->round_amount( - $ref->{sellprice}, $decimalplaces); - } else { - $ref->{sellprice} = $form->round_amount( - $ref->{sellprice} * - (1 - $form->{tradediscount}), - $decimalplaces); - } - $ref->{pricematrix} = "0:$ref->{sellprice} " - if $ref->{sellprice}; - } - chop $ref->{pricematrix}; - - } - - - if ($form->{vendor_id}) { - $pmh->execute($ref->{id}); - - $mref = $pmh->fetchrow_hashref(NAME_lc); - - if ($mref->{partnumber} ne "") { - $ref->{partnumber} = $mref->{partnumber}; - } - - if ($mref->{lastcost}) { - # do a conversion - $ref->{sellprice} = $form->round_amount( - $mref->{lastcost} * $form->{$mref->{curr}}, - $decimalplaces); - } - $pmh->finish; - - $ref->{sellprice} *= 1; - - # add 0:price to matrix - $ref->{pricematrix} = "0:$ref->{sellprice}"; - - } - -} - - sub exchangerate_defaults { my ($dbh2, $form) = @_; $dbh = $form->{dbh}; diff --git a/LedgerSMB/PriceMatrix.pm b/LedgerSMB/PriceMatrix.pm new file mode 100644 index 00000000..ad2d3b08 --- /dev/null +++ b/LedgerSMB/PriceMatrix.pm @@ -0,0 +1,204 @@ +#===================================================================== +# LedgerSMB +# Small Medium Business Accounting software +# http://www.ledgersmb.org/ +# +# Copyright (C) 2006 +# This work contains copyrighted information from a number of sources all used +# with permission. +# +# This file contains source code included with or based on SQL-Ledger which +# is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed +# under the GNU General Public License version 2 or, at your option, any later +# version. For a full list including contact information of contributors, +# maintainers, and copyright holders, see the CONTRIBUTORS file. +# +# Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork): +# Copyright (C) 2001 +# +# Author: DWS Systems Inc. +# Web: http://www.sql-ledger.org +# +# Contributors: +# +#====================================================================== +# +# This file has undergone whitespace cleanup +# +#====================================================================== +# +# Price Matrix module +# +# +#====================================================================== + +package PriceMatrix; + +sub price_matrix_query { + my ($dbh, $form) = @_; + + my $query; + my $sth; + + my @queryargs; + + if ($form->{customer_id}) { + my $defaultcurrency = $form->{dbh}->quote( + $form->{defaultcurrency}); + my $customer_id = $form->{dbh}->quote($form->{customer_id}); + $query = qq| + SELECT p.id AS parts_id, 0 AS customer_id, + 0 AS pricegroup_id, 0 AS pricebreak, + p.sellprice, NULL AS validfrom, NULL AS validto, + (SELECT substr(curr,1,3) FROM defaults) AS curr, + '' AS pricegroup + FROM parts p + WHERE p.id = ? + + UNION + + SELECT p.*, g.pricegroup + FROM partscustomer p + LEFT JOIN pricegroup g ON (g.id = p.pricegroup_id) + WHERE p.parts_id = ? + AND p.customer_id = $customer_id + + UNION + + SELECT p.*, g.pricegroup + FROM partscustomer p + LEFT JOIN pricegroup g ON (g.id = p.pricegroup_id) + JOIN customer c ON (c.pricegroup_id = g.id) + WHERE p.parts_id = ? + AND c.id = $customer_id + + UNION + + SELECT p.*, '' AS pricegroup + FROM partscustomer p + WHERE p.customer_id = 0 + AND p.pricegroup_id = 0 + AND p.parts_id = ? + + ORDER BY customer_id DESC, pricegroup_id DESC, + pricebreak + |; + $sth = $dbh->prepare($query) || $form->dberror($query); + } elsif ($form->{vendor_id}) { + my $vendor_id = $form->{dbh}->quote($form->{vendor_id}); + # price matrix and vendor's partnumber + $query = qq| + SELECT partnumber + FROM partsvendor + WHERE parts_id = ? + AND vendor_id = $vendor_id|; + $sth = $dbh->prepare($query) || $form->dberror($query); + } + + $sth; +} + + +sub price_matrix { + my ($pmh, $ref, $transdate, $decimalplaces, $form, $myconfig) = @_; + $ref->{pricematrix} = ""; + my $customerprice; + my $pricegroupprice; + my $sellprice; + my $mref; + my %p = (); + + # depends if this is a customer or vendor + if ($form->{customer_id}) { + $pmh->execute($ref->{id}, $ref->{id}, $ref->{id}, $ref->{id}); + + while ($mref = $pmh->fetchrow_hashref(NAME_lc)) { + + # check date + if ($mref->{validfrom}) { + next if $transdate < $form->datetonum( + $myconfig, $mref->{validfrom}); + } + if ($mref->{validto}) { + next if $transdate > $form->datetonum( + $myconfig, $mref->{validto}); + } + + # convert price + $sellprice = $form->round_amount($mref->{sellprice} + * $form->{$mref->{curr}}, $decimalplaces); + + if ($mref->{customer_id}) { + $ref->{sellprice} = $sellprice + if !$mref->{pricebreak}; + $p{$mref->{pricebreak}} = $sellprice; + $customerprice = 1; + } + + if ($mref->{pricegroup_id}) { + if (! $customerprice) { + $ref->{sellprice} = $sellprice + if !$mref->{pricebreak}; + $p{$mref->{pricebreak}} = $sellprice; + } + $pricegroupprice = 1; + } + + if (!$customerprice && !$pricegroupprice) { + $p{$mref->{pricebreak}} = $sellprice; + } + + } + $pmh->finish; + + if (%p) { + if ($ref->{sellprice}) { + $p{0} = $ref->{sellprice}; + } + for (sort { $a <=> $b } keys %p) { + $ref->{pricematrix} .= "${_}:$p{$_} "; + } + } else { + if ($init) { + $ref->{sellprice} = $form->round_amount( + $ref->{sellprice}, $decimalplaces); + } else { + $ref->{sellprice} = $form->round_amount( + $ref->{sellprice} * + (1 - $form->{tradediscount}), + $decimalplaces); + } + $ref->{pricematrix} = "0:$ref->{sellprice} " + if $ref->{sellprice}; + } + chop $ref->{pricematrix}; + + } + + + if ($form->{vendor_id}) { + $pmh->execute($ref->{id}); + + $mref = $pmh->fetchrow_hashref(NAME_lc); + + if ($mref->{partnumber} ne "") { + $ref->{partnumber} = $mref->{partnumber}; + } + + if ($mref->{lastcost}) { + # do a conversion + $ref->{sellprice} = $form->round_amount( + $mref->{lastcost} * $form->{$mref->{curr}}, + $decimalplaces); + } + $pmh->finish; + + $ref->{sellprice} *= 1; + + # add 0:price to matrix + $ref->{pricematrix} = "0:$ref->{sellprice}"; + + } + +} +1; -- cgit v1.2.3