summaryrefslogtreecommitdiff
path: root/LedgerSMB/PriceMatrix.pm
blob: 2c6cc3326fdaced55293ce992ac6b846ad8b863b (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( $form->{defaultcurrency} );
  42. my $customer_id = $form->{dbh}->quote( $form->{customer_id} );
  43. $query = qq|
  44. SELECT p.id AS parts_id, 0 AS customer_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.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. }
  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. $form->db_parse_numeric(sth=>$sth, hashref=>$mref);
  108. # check date
  109. if ( $mref->{validfrom} ) {
  110. next
  111. if $transdate <
  112. $form->datetonum( $myconfig, $mref->{validfrom} );
  113. }
  114. if ( $mref->{validto} ) {
  115. next
  116. if $transdate >
  117. $form->datetonum( $myconfig, $mref->{validto} );
  118. }
  119. # convert price
  120. $sellprice =
  121. $form->round_amount(
  122. $mref->{sellprice} * $form->{ $mref->{curr} },
  123. $decimalplaces );
  124. if ( $mref->{customer_id} ) {
  125. $ref->{sellprice} = $sellprice
  126. if !$mref->{pricebreak};
  127. $p{ $mref->{pricebreak} } = $sellprice;
  128. $customerprice = 1;
  129. }
  130. if ( $mref->{pricegroup_id} ) {
  131. if ( !$customerprice ) {
  132. $ref->{sellprice} = $sellprice
  133. if !$mref->{pricebreak};
  134. $p{ $mref->{pricebreak} } = $sellprice;
  135. }
  136. $pricegroupprice = 1;
  137. }
  138. if ( !$customerprice && !$pricegroupprice ) {
  139. $p{ $mref->{pricebreak} } = $sellprice;
  140. }
  141. }
  142. $pmh->finish;
  143. if (%p) {
  144. if ( $ref->{sellprice} ) {
  145. $p{0} = $ref->{sellprice};
  146. }
  147. for ( sort { $a <=> $b } keys %p ) {
  148. $ref->{pricematrix} .= "${_}:$p{$_} ";
  149. }
  150. }
  151. else {
  152. if ($init) {
  153. $ref->{sellprice} =
  154. $form->round_amount( $ref->{sellprice}, $decimalplaces );
  155. }
  156. else {
  157. $ref->{sellprice} =
  158. $form->round_amount(
  159. $ref->{sellprice} * ( 1 - $form->{tradediscount} ),
  160. $decimalplaces );
  161. }
  162. $ref->{pricematrix} = "0:$ref->{sellprice} "
  163. if $ref->{sellprice};
  164. }
  165. chop $ref->{pricematrix};
  166. }
  167. if ( $form->{vendor_id} ) {
  168. $pmh->execute( $ref->{id} );
  169. $mref = $pmh->fetchrow_hashref(NAME_lc);
  170. $form->db_parse_numeric(sth=>$sth, hashref=>$mref);
  171. if ( $mref->{partnumber} ne "" ) {
  172. $ref->{partnumber} = $mref->{partnumber};
  173. }
  174. if ( $mref->{lastcost} ) {
  175. # do a conversion
  176. $ref->{sellprice} =
  177. $form->round_amount( $mref->{lastcost} * $form->{ $mref->{curr} },
  178. $decimalplaces );
  179. }
  180. $pmh->finish;
  181. $ref->{sellprice} *= 1;
  182. # add 0:price to matrix
  183. $ref->{pricematrix} = "0:$ref->{sellprice}";
  184. }
  185. }
  186. 1;