summaryrefslogtreecommitdiff
path: root/LedgerSMB/PriceMatrix.pm
blob: a2de4baf298286cf041d584068d83a7f88bb06c7 (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. # http://www.ledgersmb.org/
  5. #
  6. # Copyright (C) 2006
  7. # This work contains copyrighted information from a number of sources all used
  8. # with permission.
  9. #
  10. # This file contains source code included with or based on SQL-Ledger which
  11. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  12. # under the GNU General Public License version 2 or, at your option, any later
  13. # version. For a full list including contact information of contributors,
  14. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  15. #
  16. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  17. # Copyright (C) 2001
  18. #
  19. # Author: DWS Systems Inc.
  20. # Web: http://www.sql-ledger.org
  21. #
  22. # Contributors:
  23. #
  24. #======================================================================
  25. #
  26. # This file has undergone whitespace cleanup
  27. #
  28. #======================================================================
  29. #
  30. # Price Matrix module
  31. #
  32. #
  33. #======================================================================
  34. package PriceMatrix;
  35. sub price_matrix_query {
  36. my ($dbh, $form) = @_;
  37. my $query;
  38. my $sth;
  39. my @queryargs;
  40. if ($form->{customer_id}) {
  41. my $defaultcurrency = $form->{dbh}->quote(
  42. $form->{defaultcurrency});
  43. my $customer_id = $form->{dbh}->quote($form->{customer_id});
  44. $query = qq|
  45. SELECT p.id AS parts_id, 0 AS customer_id,
  46. 0 AS pricegroup_id, 0 AS pricebreak,
  47. p.sellprice, NULL AS validfrom, NULL AS validto,
  48. (SELECT substr(curr,1,3) FROM defaults) AS curr,
  49. '' AS pricegroup
  50. FROM parts p
  51. WHERE p.id = ?
  52. UNION
  53. SELECT p.parts_id, p.customer_id, p.pricegroup_id,
  54. p.pricebreak, p.sellprice, p.validfrom,
  55. p.valid_to, p.curr, g.pricegroup
  56. FROM partscustomer p
  57. LEFT JOIN pricegroup g ON (g.id = p.pricegroup_id)
  58. WHERE p.parts_id = ?
  59. AND p.customer_id = $customer_id
  60. UNION
  61. SELECT p.parts_id, p.customer_id, p.pricegroup_id,
  62. p.pricebreak, p.sellprice, p.validfrom,
  63. p.valid_to, p.curr, g.pricegroup
  64. FROM partscustomer p
  65. LEFT JOIN pricegroup g ON (g.id = p.pricegroup_id)
  66. JOIN customer c ON (c.pricegroup_id = g.id)
  67. WHERE p.parts_id = ?
  68. AND c.id = $customer_id
  69. UNION
  70. SELECT p.parts_id, p.customer_id, p.pricegroup_id,
  71. p.pricebreak, p.sellprice, p.validfrom,
  72. p.valid_to, p.curr, g.pricegroup
  73. FROM partscustomer p
  74. WHERE p.customer_id = 0
  75. AND p.pricegroup_id = 0
  76. AND p.parts_id = ?
  77. ORDER BY customer_id DESC, pricegroup_id DESC,
  78. pricebreak
  79. |;
  80. $sth = $dbh->prepare($query) || $form->dberror($query);
  81. } elsif ($form->{vendor_id}) {
  82. my $vendor_id = $form->{dbh}->quote($form->{vendor_id});
  83. # price matrix and vendor's partnumber
  84. $query = qq|
  85. SELECT partnumber
  86. FROM partsvendor
  87. WHERE parts_id = ?
  88. AND vendor_id = $vendor_id|;
  89. $sth = $dbh->prepare($query) || $form->dberror($query);
  90. }
  91. $sth;
  92. }
  93. sub price_matrix {
  94. my ($pmh, $ref, $transdate, $decimalplaces, $form, $myconfig) = @_;
  95. $ref->{pricematrix} = "";
  96. my $customerprice;
  97. my $pricegroupprice;
  98. my $sellprice;
  99. my $mref;
  100. my %p = ();
  101. # depends if this is a customer or vendor
  102. if ($form->{customer_id}) {
  103. $pmh->execute($ref->{id}, $ref->{id}, $ref->{id}, $ref->{id});
  104. while ($mref = $pmh->fetchrow_hashref(NAME_lc)) {
  105. # check date
  106. if ($mref->{validfrom}) {
  107. next if $transdate < $form->datetonum(
  108. $myconfig, $mref->{validfrom});
  109. }
  110. if ($mref->{validto}) {
  111. next if $transdate > $form->datetonum(
  112. $myconfig, $mref->{validto});
  113. }
  114. # convert price
  115. $sellprice = $form->round_amount($mref->{sellprice}
  116. * $form->{$mref->{curr}}, $decimalplaces);
  117. if ($mref->{customer_id}) {
  118. $ref->{sellprice} = $sellprice
  119. if !$mref->{pricebreak};
  120. $p{$mref->{pricebreak}} = $sellprice;
  121. $customerprice = 1;
  122. }
  123. if ($mref->{pricegroup_id}) {
  124. if (! $customerprice) {
  125. $ref->{sellprice} = $sellprice
  126. if !$mref->{pricebreak};
  127. $p{$mref->{pricebreak}} = $sellprice;
  128. }
  129. $pricegroupprice = 1;
  130. }
  131. if (!$customerprice && !$pricegroupprice) {
  132. $p{$mref->{pricebreak}} = $sellprice;
  133. }
  134. }
  135. $pmh->finish;
  136. if (%p) {
  137. if ($ref->{sellprice}) {
  138. $p{0} = $ref->{sellprice};
  139. }
  140. for (sort { $a <=> $b } keys %p) {
  141. $ref->{pricematrix} .= "${_}:$p{$_} ";
  142. }
  143. } else {
  144. if ($init) {
  145. $ref->{sellprice} = $form->round_amount(
  146. $ref->{sellprice}, $decimalplaces);
  147. } else {
  148. $ref->{sellprice} = $form->round_amount(
  149. $ref->{sellprice} *
  150. (1 - $form->{tradediscount}),
  151. $decimalplaces);
  152. }
  153. $ref->{pricematrix} = "0:$ref->{sellprice} "
  154. if $ref->{sellprice};
  155. }
  156. chop $ref->{pricematrix};
  157. }
  158. if ($form->{vendor_id}) {
  159. $pmh->execute($ref->{id});
  160. $mref = $pmh->fetchrow_hashref(NAME_lc);
  161. if ($mref->{partnumber} ne "") {
  162. $ref->{partnumber} = $mref->{partnumber};
  163. }
  164. if ($mref->{lastcost}) {
  165. # do a conversion
  166. $ref->{sellprice} = $form->round_amount(
  167. $mref->{lastcost} * $form->{$mref->{curr}},
  168. $decimalplaces);
  169. }
  170. $pmh->finish;
  171. $ref->{sellprice} *= 1;
  172. # add 0:price to matrix
  173. $ref->{pricematrix} = "0:$ref->{sellprice}";
  174. }
  175. }
  176. 1;