summaryrefslogtreecommitdiff
path: root/LedgerSMB/PriceMatrix.pm
blob: 0a92562c615b47a580384fe8cedaad18f293b185 (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.validto, 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.validto, 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.validto, p.curr, g.pricegroup
  73. FROM partscustomer p
  74. LEFT JOIN pricegroup g ON (g.id = p.pricegroup_id)
  75. WHERE p.customer_id = 0
  76. AND p.pricegroup_id = 0
  77. AND p.parts_id = ?
  78. ORDER BY customer_id DESC, pricegroup_id DESC,
  79. pricebreak
  80. |;
  81. $sth = $dbh->prepare($query) || $form->dberror($query);
  82. } elsif ($form->{vendor_id}) {
  83. my $vendor_id = $form->{dbh}->quote($form->{vendor_id});
  84. # price matrix and vendor's partnumber
  85. $query = qq|
  86. SELECT partnumber
  87. FROM partsvendor
  88. WHERE parts_id = ?
  89. AND vendor_id = $vendor_id|;
  90. $sth = $dbh->prepare($query) || $form->dberror($query);
  91. }
  92. $sth;
  93. }
  94. sub price_matrix {
  95. my ($pmh, $ref, $transdate, $decimalplaces, $form, $myconfig) = @_;
  96. $ref->{pricematrix} = "";
  97. my $customerprice;
  98. my $pricegroupprice;
  99. my $sellprice;
  100. my $mref;
  101. my %p = ();
  102. # depends if this is a customer or vendor
  103. if ($form->{customer_id}) {
  104. $pmh->execute($ref->{id}, $ref->{id}, $ref->{id}, $ref->{id});
  105. while ($mref = $pmh->fetchrow_hashref(NAME_lc)) {
  106. # check date
  107. if ($mref->{validfrom}) {
  108. next if $transdate < $form->datetonum(
  109. $myconfig, $mref->{validfrom});
  110. }
  111. if ($mref->{validto}) {
  112. next if $transdate > $form->datetonum(
  113. $myconfig, $mref->{validto});
  114. }
  115. # convert price
  116. $sellprice = $form->round_amount($mref->{sellprice}
  117. * $form->{$mref->{curr}}, $decimalplaces);
  118. if ($mref->{customer_id}) {
  119. $ref->{sellprice} = $sellprice
  120. if !$mref->{pricebreak};
  121. $p{$mref->{pricebreak}} = $sellprice;
  122. $customerprice = 1;
  123. }
  124. if ($mref->{pricegroup_id}) {
  125. if (! $customerprice) {
  126. $ref->{sellprice} = $sellprice
  127. if !$mref->{pricebreak};
  128. $p{$mref->{pricebreak}} = $sellprice;
  129. }
  130. $pricegroupprice = 1;
  131. }
  132. if (!$customerprice && !$pricegroupprice) {
  133. $p{$mref->{pricebreak}} = $sellprice;
  134. }
  135. }
  136. $pmh->finish;
  137. if (%p) {
  138. if ($ref->{sellprice}) {
  139. $p{0} = $ref->{sellprice};
  140. }
  141. for (sort { $a <=> $b } keys %p) {
  142. $ref->{pricematrix} .= "${_}:$p{$_} ";
  143. }
  144. } else {
  145. if ($init) {
  146. $ref->{sellprice} = $form->round_amount(
  147. $ref->{sellprice}, $decimalplaces);
  148. } else {
  149. $ref->{sellprice} = $form->round_amount(
  150. $ref->{sellprice} *
  151. (1 - $form->{tradediscount}),
  152. $decimalplaces);
  153. }
  154. $ref->{pricematrix} = "0:$ref->{sellprice} "
  155. if $ref->{sellprice};
  156. }
  157. chop $ref->{pricematrix};
  158. }
  159. if ($form->{vendor_id}) {
  160. $pmh->execute($ref->{id});
  161. $mref = $pmh->fetchrow_hashref(NAME_lc);
  162. if ($mref->{partnumber} ne "") {
  163. $ref->{partnumber} = $mref->{partnumber};
  164. }
  165. if ($mref->{lastcost}) {
  166. # do a conversion
  167. $ref->{sellprice} = $form->round_amount(
  168. $mref->{lastcost} * $form->{$mref->{curr}},
  169. $decimalplaces);
  170. }
  171. $pmh->finish;
  172. $ref->{sellprice} *= 1;
  173. # add 0:price to matrix
  174. $ref->{pricematrix} = "0:$ref->{sellprice}";
  175. }
  176. }
  177. 1;