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.

115 lines
4.6KB

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