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.

ProductDistributionBuilder.php 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\Service\DistributionSolver;
  6. use common\logic\Distribution\ProductDistribution\Model\ProductDistribution;
  7. use common\logic\Distribution\ProductDistribution\Repository\ProductDistributionRepository;
  8. use common\logic\Product\Product\Model\Product;
  9. use common\logic\Product\Product\Service\ProductSolver;
  10. class ProductDistributionBuilder extends AbstractBuilder
  11. {
  12. protected ProductDistributionRepository $productDistributionRepository;
  13. protected DistributionSolver $distributionSolver;
  14. protected ProductSolver $productSolver;
  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. }
  21. public function instanciateProductDistribution(Distribution $distribution, Product $product): ProductDistribution
  22. {
  23. $productDistribution = new ProductDistribution();
  24. $productDistribution->populateFieldObject('id_distribution', 'distribution', $distribution);
  25. $productDistribution->populateFieldObject('id_product', 'product', $product);
  26. return $productDistribution;
  27. }
  28. public function createProductDistribution(Distribution $distribution, Product $product): ProductDistribution
  29. {
  30. $productDistribution = $this->instanciateProductDistribution($distribution, $product);
  31. $this->initProductDistribution($productDistribution);
  32. $this->saveCreate($productDistribution);
  33. return $productDistribution;
  34. }
  35. public function createProductDistributionIfNotExist(Distribution $distribution, Product $product): ProductDistribution
  36. {
  37. return $this->productDistributionRepository->findOneProductDistribution($distribution, $product)
  38. ?? $this->createProductDistribution($distribution, $product);
  39. }
  40. public function updateProductDistribution(ProductDistribution $productDistribution): ProductDistribution
  41. {
  42. $this->initProductDistribution($productDistribution);
  43. $this->saveUpdate($productDistribution);
  44. return $productDistribution;
  45. }
  46. public function initProductDistribution(ProductDistribution $productDistribution): ProductDistribution
  47. {
  48. $this->initActive($productDistribution);
  49. $this->initQuantityMax($productDistribution);
  50. return $productDistribution;
  51. }
  52. public function initActive(ProductDistribution $productDistribution): ProductDistribution
  53. {
  54. $productDistribution->active = (int) $this->productSolver->isProductActiveByDay(
  55. $productDistribution->product,
  56. $this->distributionSolver->getDistributionDayAsString($productDistribution->distribution)
  57. );
  58. return $productDistribution;
  59. }
  60. public function initQuantityMax(ProductDistribution $productDistribution): ProductDistribution
  61. {
  62. $productDistribution->quantity_max = $this->productSolver->getProductQuantityMaxByDay(
  63. $productDistribution->product,
  64. $this->distributionSolver->getDistributionDayAsString($productDistribution->distribution)
  65. );
  66. return $productDistribution;
  67. }
  68. }