summaryrefslogtreecommitdiff
path: root/LedgerSMB/PriceMatrix.pm
blob: 68fa5b121e5ee2bcddbb3886c4eea653b688d234 (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. # 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. if ( $mref->{partnumber} ne "" ) {
  170. $ref->{partnumber} = $mref->{partnumber};
  171. }
  172. if ( $mref->{lastcost} ) {
  173. # do a conversion
  174. $ref->{sellprice} =
  175. $form->round_amount( $mref->{lastcost} * $form->{ $mref->{curr} },
  176. $decimalplaces );
  177. }
  178. $pmh->finish;
  179. $ref->{sellprice} *= 1;
  180. # add 0:price to matrix
  181. $ref->{pricematrix} = "0:$ref->{sellprice}";
  182. }
  183. }
  184. 1;