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