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.

99 lines
3.9KB

  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->update($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. public function updateProductDistributionQuantityMax(ProductDistribution $productDistribution, float $quantityMax)
  69. {
  70. $productDistribution->quantity_max = ($quantityMax == -1) ? null : (float) abs($quantityMax);
  71. $this->update($productDistribution);
  72. return $productDistribution;
  73. }
  74. public function updateProductDistributionActive(ProductDistribution $productDistribution, int $active)
  75. {
  76. $productDistribution->active = $active;
  77. $this->update($productDistribution);
  78. }
  79. }