Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

164 lines
5.4KB

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