Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

146 lines
4.6KB

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