Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

119 lines
5.7KB

  1. <?php
  2. namespace domain\PointSale\SharedPointSale;
  3. use common\helpers\GlobalParam;
  4. use domain\_\AbstractResolver;
  5. use domain\Distribution\Distribution\Distribution;
  6. use domain\Distribution\Distribution\DistributionRepository;
  7. use domain\Distribution\PointSaleDistribution\PointSaleDistributionRepository;
  8. use domain\PointSale\PointSale\PointSale;
  9. use domain\PointSale\PointSale\PointSaleRepository;
  10. use yii\helpers\Html;
  11. class SharedPointSaleResolver extends AbstractResolver
  12. {
  13. protected PointSaleRepository $pointSaleRepository;
  14. protected SharedPointSaleRepository $sharedPointSaleRepository;
  15. protected PointSaleDistributionRepository $pointSaleDistributionRepository;
  16. protected DistributionRepository $distributionRepository;
  17. public function loadDependencies(): void
  18. {
  19. $this->pointSaleRepository = $this->loadService(PointSaleRepository::class);
  20. $this->sharedPointSaleRepository = $this->loadService(SharedPointSaleRepository::class);
  21. $this->distributionRepository = $this->loadService(DistributionRepository::class);
  22. $this->pointSaleDistributionRepository = $this->loadService(PointSaleDistributionRepository::class);
  23. }
  24. public function getPointsSaleSharedWithPointSale(PointSale $pointSale, Distribution $distribution = null, array &$pointsSaleSharedWithPointSaleArray = []): array
  25. {
  26. $idProducerContext = $this->getProducerContextId();
  27. $sharedPointsSaleConfirmedArray = $this->sharedPointSaleRepository->findSharedPointsSaleConfirmedByPointSale($pointSale);
  28. foreach($sharedPointsSaleConfirmedArray as $sharedPointSaleConfirmed) {
  29. if ($sharedPointSaleConfirmed->getPointSale()->id != $pointSale->id
  30. && !in_array($sharedPointSaleConfirmed->getPointSale(), $pointsSaleSharedWithPointSaleArray)
  31. && $sharedPointSaleConfirmed->getPointSale()->getProducer()->id != $idProducerContext) {
  32. $distributionProducer = null;
  33. if($distribution) {
  34. $distributionProducer = $this->distributionRepository
  35. ->setProducerContext($sharedPointSaleConfirmed->getPointSale()->getProducer())
  36. ->findOneDistribution($distribution->date, true);
  37. }
  38. if(!$distribution || ($distributionProducer && $this->pointSaleDistributionRepository->findOnePointSaleDistribution($distributionProducer, $sharedPointSaleConfirmed->getPointSale(), true))) {
  39. $pointsSaleSharedWithPointSaleArray[] = $sharedPointSaleConfirmed->getPointSale();
  40. $this->getPointsSaleSharedWithPointSale($sharedPointSaleConfirmed->getPointSale(), $distribution, $pointsSaleSharedWithPointSaleArray);
  41. }
  42. }
  43. if ($sharedPointSaleConfirmed->getPointSaleWithSharing()->id != $pointSale->id
  44. && !in_array($sharedPointSaleConfirmed->getPointSaleWithSharing(), $pointsSaleSharedWithPointSaleArray)
  45. && $sharedPointSaleConfirmed->getProducerWithSharing()->id != $idProducerContext) {
  46. $distributionProducer = null;
  47. if($distribution) {
  48. $distributionProducer = $this->distributionRepository
  49. ->setProducerContext($sharedPointSaleConfirmed->getProducerWithSharing())
  50. ->findOneDistribution($distribution->date, true);
  51. }
  52. if(!$distribution || ($distributionProducer && $this->pointSaleDistributionRepository->findOnePointSaleDistribution($distributionProducer, $sharedPointSaleConfirmed->getPointSaleWithSharing(), true))) {
  53. $pointsSaleSharedWithPointSaleArray[] = $sharedPointSaleConfirmed->getPointSaleWithSharing();
  54. $this->getPointsSaleSharedWithPointSale($sharedPointSaleConfirmed->getPointSaleWithSharing(), $distribution, $pointsSaleSharedWithPointSaleArray);
  55. }
  56. }
  57. }
  58. return array_unique($pointsSaleSharedWithPointSaleArray, SORT_REGULAR);
  59. }
  60. public function getProducersSharingPointSaleAsString(PointSale $pointSale, Distribution $distribution = null, string $separator = ', ', bool $withLink = false): string
  61. {
  62. $pointsSaleSharedWithPointSaleArray = $this->getPointsSaleSharedWithPointSale($pointSale, $distribution);
  63. return implode($separator, array_map(
  64. function($pointSale) use ($withLink) {
  65. $return = '';
  66. if($withLink) {
  67. $return .= '<a href="'.\Yii::$app->urlManagerProducer->createUrl(['site/index', 'slug_producer' => $pointSale->getProducer()->slug]).'">';
  68. }
  69. $return .= $pointSale->getProducer()->getName();
  70. if($withLink) {
  71. $return .= '</a>';
  72. }
  73. return $return;
  74. },
  75. $pointsSaleSharedWithPointSaleArray)
  76. );
  77. }
  78. public function countPointsSaleSharedWithPointSale(PointSale $pointSale): int
  79. {
  80. return count($this->getPointsSaleSharedWithPointSale($pointSale));
  81. }
  82. public function hasPointSaleSharedWithPointSale(PointSale $pointSale): bool
  83. {
  84. return (bool) $this->countPointsSaleSharedWithPointSale($pointSale);
  85. }
  86. public function countPointsSaleShared(): int
  87. {
  88. $count = 0;
  89. $pointsSaleArray = $this->pointSaleRepository->findPointSales();
  90. foreach($pointsSaleArray as $pointSale) {
  91. if($this->hasPointSaleSharedWithPointSale($pointSale)) {
  92. $count ++;
  93. }
  94. }
  95. return $count;
  96. }
  97. public function countSharedPointsSaleRequestsOthers(): int
  98. {
  99. return count($this->sharedPointSaleRepository->findSharedPointsSaleRequestsOthers());
  100. }
  101. }