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