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.

164 line
5.4KB

  1. <?php
  2. namespace domain\Product\ProductPrice\Service;
  3. use domain\PointSale\PointSale\PointSale;
  4. use domain\Product\ProductPrice\Model\ProductPrice;
  5. use domain\User\User\User;
  6. use domain\User\User\UserSolver;
  7. use domain\User\UserGroup\UserGroupRepository;
  8. use domain\_\AbstractService;
  9. use domain\_\SolverInterface;
  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 = UserGroupRepository::getInstance()->findOneUserGroupById($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 = UserGroupRepository::getInstance()->findOneUserGroupById($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. }