Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

113 lines
4.4KB

  1. <?php
  2. namespace domain\Distribution\ProductDistribution;
  3. use domain\Distribution\Distribution\Distribution;
  4. use domain\Distribution\Distribution\DistributionRepository;
  5. use domain\Distribution\Distribution\DistributionSolver;
  6. use domain\Product\Product\Product;
  7. use domain\Product\Product\ProductSolver;
  8. use domain\_\AbstractBuilder;
  9. class ProductDistributionBuilder extends AbstractBuilder
  10. {
  11. protected ProductDistributionRepository $productDistributionRepository;
  12. protected DistributionSolver $distributionSolver;
  13. protected ProductSolver $productSolver;
  14. protected DistributionRepository $distributionRepository;
  15. public function loadDependencies(): void
  16. {
  17. $this->productDistributionRepository = $this->loadService(ProductDistributionRepository::class);
  18. $this->distributionSolver = $this->loadService(DistributionSolver::class);
  19. $this->productSolver = $this->loadService(ProductSolver::class);
  20. $this->distributionRepository = $this->loadService(DistributionRepository::class);
  21. }
  22. public function instanciateProductDistribution(Distribution $distribution, Product $product): ProductDistribution
  23. {
  24. $productDistribution = new ProductDistribution();
  25. $productDistribution->populateFieldObject('id_distribution', 'distribution', $distribution);
  26. $productDistribution->populateFieldObject('id_product', 'product', $product);
  27. return $productDistribution;
  28. }
  29. public function createProductDistribution(Distribution $distribution, Product $product): ProductDistribution
  30. {
  31. $productDistribution = $this->instanciateProductDistribution($distribution, $product);
  32. $this->initProductDistribution($productDistribution);
  33. $this->saveCreate($productDistribution);
  34. return $productDistribution;
  35. }
  36. public function createProductDistributionIfNotExist(Distribution $distribution, Product $product): ProductDistribution
  37. {
  38. return $this->productDistributionRepository->findOneProductDistribution($distribution, $product)
  39. ?? $this->createProductDistribution($distribution, $product);
  40. }
  41. public function updateProductDistribution(ProductDistribution $productDistribution): ProductDistribution
  42. {
  43. $this->initProductDistribution($productDistribution);
  44. $this->update($productDistribution);
  45. return $productDistribution;
  46. }
  47. public function initProductDistribution(ProductDistribution $productDistribution): ProductDistribution
  48. {
  49. $this->initActive($productDistribution);
  50. $this->initQuantityMax($productDistribution);
  51. return $productDistribution;
  52. }
  53. public function initActive(ProductDistribution $productDistribution): ProductDistribution
  54. {
  55. $productDistribution->active = (int) $this->productSolver->isProductActiveByDay(
  56. $productDistribution->product,
  57. $this->distributionSolver->getDistributionDayAsString($productDistribution->distribution)
  58. );
  59. return $productDistribution;
  60. }
  61. public function initQuantityMax(ProductDistribution $productDistribution): ProductDistribution
  62. {
  63. $productDistribution->quantity_max = $this->productSolver->getProductQuantityMaxByDay(
  64. $productDistribution->product,
  65. $this->distributionSolver->getDistributionDayAsString($productDistribution->distribution)
  66. );
  67. return $productDistribution;
  68. }
  69. public function updateProductDistributionQuantityMax(ProductDistribution $productDistribution, float $quantityMax)
  70. {
  71. $productDistribution->quantity_max = ($quantityMax == -1) ? null : (float) abs($quantityMax);
  72. $this->update($productDistribution);
  73. return $productDistribution;
  74. }
  75. public function updateProductDistributionActive(ProductDistribution $productDistribution, int $active)
  76. {
  77. $productDistribution->active = $active;
  78. $this->update($productDistribution);
  79. }
  80. public function disableProductDistributionsIncomingByProduct(Product $product)
  81. {
  82. $distributionsIncomingArray = $this->distributionRepository->findDistributionsIncoming(true);
  83. foreach($distributionsIncomingArray as $distribution) {
  84. ProductDistribution::updateAll([
  85. 'active' => false
  86. ], [
  87. 'id_product' => $product->id,
  88. 'id_distribution' => $distribution->id,
  89. ]);
  90. }
  91. }
  92. }