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/OE.pm | 173 +------------------------------------------------------- 1 file changed, 3 insertions(+), 170 deletions(-) (limited to 'LedgerSMB/OE.pm') 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}; -- cgit v1.2.3