summaryrefslogtreecommitdiff
path: root/LedgerSMB/PriceMatrix.pm
blob: af2dc8d88a0e232c60128c3f03d6f8fef0470cbb (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.parts_id, p.customer_id AS entity_id,
  45. NULL AS pricegroup_id,
  46. p.pricebreak, p.sellprice, p.validfrom,
  47. p.validto, p.curr, NULL AS pricegroup,
  48. 1 as priority
  49. FROM partscustomer p
  50. WHERE p.parts_id = ?
  51. AND coalesce(p.validfrom, $transdate) <=
  52. $transdate
  53. AND coalesce(p.validto, $transdate) >=
  54. $transdate
  55. AND p.customer_id = $entity_id
  56. UNION
  57. SELECT p.parts_id, p.customer_id AS entity_id,
  58. p.pricegroup_id,
  59. p.pricebreak, p.sellprice, p.validfrom,
  60. p.validto, p.curr, g.pricegroup, 2 AS priority
  61. FROM partscustomer p
  62. JOIN pricegroup g ON (g.id = p.pricegroup_id)
  63. JOIN customer c ON (c.pricegroup_id = g.id)
  64. WHERE p.parts_id = ?
  65. AND coalesce(p.validfrom, $transdate) <=
  66. $transdate
  67. AND coalesce(p.validto, $transdate) >=
  68. $transdate
  69. AND c.id = $entity_id
  70. UNION
  71. SELECT p.parts_id, p.customer_id AS entity_id,
  72. p.pricegroup_id,
  73. p.pricebreak, p.sellprice, p.validfrom,
  74. p.validto, p.curr, g.pricegroup, 3 AS priority
  75. FROM partscustomer p
  76. LEFT JOIN pricegroup g ON (g.id = p.pricegroup_id)
  77. WHERE p.customer_id = 0
  78. AND p.pricegroup_id = 0
  79. AND coalesce(p.validfrom, $transdate) <=
  80. $transdate
  81. AND coalesce(p.validto, $transdate) >=
  82. $transdate
  83. AND p.parts_id = ?
  84. ORDER BY priority LIMIT 1;
  85. |;
  86. $sth = $dbh->prepare($query) || $form->dberror($query);
  87. }
  88. elsif ( $form->{vendor_id} ) {
  89. # price matrix and vendor's partnumber
  90. $query = qq|
  91. SELECT partnumber, lastcost
  92. FROM partsvendor
  93. WHERE parts_id = ?
  94. AND entity_id = $entity_id|;
  95. $sth = $dbh->prepare($query) || $form->dberror($query);
  96. }
  97. $sth;
  98. }
  99. sub price_matrix {
  100. my ( $pmh, $ref, $transdate, $decimalplaces, $form, $myconfig ) = @_;
  101. my $customerprice;
  102. my $pricegroupprice;
  103. my $sellprice;
  104. my $mref;
  105. my %p = ();
  106. # depends if this is a customer or vendor
  107. if ( $form->{customer_id} ) {
  108. $pmh->execute( $ref->{id}, $ref->{id}, $ref->{id} );
  109. } elsif ( $form->{vendor_id} ) {
  110. $pmh->execute( $ref->{id} );
  111. } else {
  112. $form->error('Invalid Form');
  113. return;
  114. }
  115. if ( $mref = $pmh->fetchrow_hashref(NAME_lc) ) {
  116. if ($form->{customer_id}){
  117. $form->db_parse_numeric(sth=>$sth, hashref=>$mref);
  118. $sellprice = $mref->{sellprice} || $ref->{sellprice};
  119. if ($mref->{pricebreak}){
  120. $sellprice = $sellprice
  121. - ($sellprice * ($mref->{pricebreak} / 100));
  122. }
  123. $ref->{sellprice} = $sellprice;
  124. } elsif ($form->{vendor_id}){
  125. $sellprice = $mref->{lastcost} || $ref->{sellprice};
  126. $ref->{sellprice} = $sellprice;
  127. }
  128. }
  129. }
  130. 1;