Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

165 lines
5.5KB

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