You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
4.0KB

  1. <?php
  2. namespace common\logic\Product\ProductPrice;
  3. use common\logic\BaseService;
  4. use common\logic\PointSale\PointSale\PointSale;
  5. use common\logic\SolverInterface;
  6. use common\logic\User\User\User;
  7. class ProductPriceSolver extends BaseService implements SolverInterface
  8. {
  9. // getSpecificPricesFilterByPriorityMatch
  10. public function filterByPriorityMatch(
  11. array $specificPrices,
  12. User $user,
  13. PointSale $pointSale): array
  14. {
  15. $priorityMatchSpecificPrice = $this->getPriorityMatch($specificPrices, $user, $pointSale);
  16. $specificPricesFilter = [];
  17. foreach ($specificPrices as $keySpecificPrice => $specificPrice) {
  18. if (($priorityMatchSpecificPrice && $specificPrice->$priorityMatchSpecificPrice($user, $pointSale))
  19. || $specificPrice->matchFromQuantityOnly()) {
  20. $specificPricesFilter[] = $specificPrice;
  21. }
  22. }
  23. return $specificPricesFilter;
  24. }
  25. // getPriorityMatchOfSpecificPriceArray
  26. public function getPriorityMatch(
  27. array $specificPriceArray,
  28. User $user = null,
  29. PointSale $pointSale = null): ?string
  30. {
  31. $typeMatchArray = [
  32. 'matchUser',
  33. 'matchUserGroup',
  34. 'matchPointSale',
  35. 'matchUserPointSale',
  36. 'matchUserGroupPointSale'
  37. ];
  38. foreach($typeMatchArray as $typeMatch) {
  39. if($this->hasMatchOfType($specificPriceArray, $typeMatch, $user, $pointSale)) {
  40. return $typeMatch;
  41. }
  42. }
  43. return null;
  44. }
  45. public function hasMatchOfType(
  46. array $specificPriceArray,
  47. string $typeMatch,
  48. User $user = null,
  49. PointSale $pointSale = null): bool
  50. {
  51. foreach($specificPriceArray as $specificPrice) {
  52. if($specificPrice->$typeMatch($user, $pointSale)) {
  53. return true;
  54. }
  55. }
  56. return false;
  57. }
  58. public function matchUser(ProductPrice $productPrice, User $user = null, PointSale $pointSale = null): bool
  59. {
  60. return $user
  61. && $productPrice->id_user
  62. && !$productPrice->id_point_sale
  63. && !$productPrice->id_user_group
  64. && $productPrice->id_user == $user->id;
  65. }
  66. public function matchUserGroup(ProductPrice $productPrice, User $user = null, PointSale $pointSale = null): bool
  67. {
  68. return $user
  69. && $productPrice->id_user_group
  70. && !$productPrice->id_point_sale
  71. && !$productPrice->id_user
  72. && $user->belongsToUserGroup($productPrice->id_user_group);
  73. }
  74. public function matchPointSale(ProductPrice $productPrice, User $user = null, PointSale $pointSale = null): bool
  75. {
  76. return $pointSale
  77. && $productPrice->id_point_sale
  78. && !$productPrice->id_user
  79. && !$productPrice->id_user_group
  80. && $productPrice->id_point_sale == $pointSale->id;
  81. }
  82. public function matchUserPointSale(ProductPrice $productPrice, User $user = null, PointSale $pointSale = null): bool
  83. {
  84. return $pointSale && $user
  85. && $productPrice->id_point_sale
  86. && $productPrice->id_user
  87. && $productPrice->id_point_sale == $pointSale->id
  88. && $productPrice->id_user == $user->id;
  89. }
  90. public function matchUserGroupPointSale(ProductPrice $productPrice, User $user = null, PointSale $pointSale = null): bool
  91. {
  92. return $user
  93. && $pointSale
  94. && $productPrice->id_user_group
  95. && $productPrice->id_point_sale
  96. && !$productPrice->id_user
  97. && $user->belongsToUserGroup($productPrice->id_user_group)
  98. && $productPrice->id_point_sale == $pointSale->id;
  99. }
  100. public function matchFromQuantityOnly(ProductPrice $productPrice): bool
  101. {
  102. return !$productPrice->id_user
  103. && !$productPrice->id_point_sale
  104. && !$productPrice->id_user_group
  105. && $productPrice->from_quantity;
  106. }
  107. }