summaryrefslogtreecommitdiff
path: root/LedgerSMB/PriceMatrix.pm
blob: bfebde4a9f11e4ae1f27b302377c79745a81ae34 (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. my $entity_id = $form->{dbh}->quote( $form->{entity_id} );
  41. if ( $form->{customer_id} ) {
  42. my $defaultcurrency = $form->{dbh}->quote( $form->{defaultcurrency} );
  43. $query = qq|
  44. SELECT p.id AS parts_id, 0 AS entity_id,
  45. 0 AS pricegroup_id, 0 AS pricebreak,
  46. p.sellprice, NULL AS validfrom, NULL AS validto,
  47. (SELECT substr(value,1,3) FROM defaults WHERE
  48. setting_key = 'curr') AS curr,
  49. '' AS pricegroup
  50. FROM parts p
  51. WHERE p.id = ?
  52. UNION
  53. SELECT p.parts_id, p.entity_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.entity_id = $entity_id
  60. UNION
  61. SELECT p.parts_id, p.entity_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 = $entity_id
  69. UNION
  70. SELECT p.parts_id, p.entity_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.entity_id = 0
  76. AND p.pricegroup_id = 0
  77. AND p.parts_id = ?
  78. ORDER BY entity_id DESC, pricegroup_id DESC,
  79. pricebreak
  80. |;
  81. $sth = $dbh->prepare($query) || $form->dberror($query);
  82. }
  83. elsif ( $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 entity_id = $entity_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. $form->db_parse_numeric(sth=>$sth, hashref=>$mref);
  107. # check date
  108. if ( $mref->{validfrom} ) {
  109. next
  110. if $transdate <
  111. $form->datetonum( $myconfig, $mref->{validfrom} );
  112. }
  113. if ( $mref->{validto} ) {
  114. next
  115. if $transdate >
  116. $form->datetonum( $myconfig, $mref->{validto} );
  117. }
  118. # convert price
  119. $sellprice =
  120. $form->round_amount(
  121. $mref->{sellprice} * $form->{ $mref->{curr} },
  122. $decimalplaces );
  123. if ( $mref->{customer_id} ) {
  124. $ref->{sellprice} = $sellprice
  125. if !$mref->{pricebreak};
  126. $p{ $mref->{pricebreak} } = $sellprice;
  127. $customerprice = 1;
  128. }
  129. if ( $mref->{pricegroup_id} ) {
  130. if ( !$customerprice ) {
  131. $ref->{sellprice} = $sellprice
  132. if !$mref->{pricebreak};
  133. $p{ $mref->{pricebreak} } = $sellprice;
  134. }
  135. $pricegroupprice = 1;
  136. }
  137. if ( !$customerprice && !$pricegroupprice ) {
  138. $p{ $mref->{pricebreak} } = $sellprice;
  139. }
  140. }
  141. $pmh->finish;
  142. if (%p) {
  143. if ( $ref->{sellprice} ) {
  144. $p{0} = $ref->{sellprice};
  145. }
  146. for ( sort { $a <=> $b } keys %p ) {
  147. $ref->{pricematrix} .= "${_}:$p{$_} ";
  148. }
  149. }
  150. else {
  151. if ($init) {
  152. $ref->{sellprice} =
  153. $form->round_amount( $ref->{sellprice}, $decimalplaces );
  154. }
  155. else {
  156. $ref->{sellprice} =
  157. $form->round_amount(
  158. $ref->{sellprice} * ( 1 - $form->{tradediscount} ),
  159. $decimalplaces );
  160. }
  161. $ref->{pricematrix} = "0:$ref->{sellprice} "
  162. if $ref->{sellprice};
  163. }
  164. chop $ref->{pricematrix};
  165. }
  166. if ( $form->{vendor_id} ) {
  167. $pmh->execute( $ref->{id} );
  168. $mref = $pmh->fetchrow_hashref(NAME_lc);
  169. $form->db_parse_numeric(sth=>$sth, hashref=>$mref);
  170. if ( $mref->{partnumber} ne "" ) {
  171. $ref->{partnumber} = $mref->{partnumber};
  172. }
  173. if ( $mref->{lastcost} ) {
  174. # do a conversion
  175. $ref->{sellprice} =
  176. $form->round_amount( $mref->{lastcost} * $form->{ $mref->{curr} },
  177. $decimalplaces );
  178. }
  179. $pmh->finish;
  180. $ref->{sellprice} *= 1;
  181. # add 0:price to matrix
  182. $ref->{pricematrix} = "0:$ref->{sellprice}";
  183. }
  184. }
  185. 1;