Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

73 linhas
2.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->initActive($productDistribution);
  32. $this->initQuantityMax($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 initActive(ProductDistribution $productDistribution): ProductDistribution
  42. {
  43. $productDistribution->active = (int) $this->productSolver->isProductActiveByDay(
  44. $productDistribution->product,
  45. $this->distributionSolver->getDistributionDayAsString($productDistribution->distribution)
  46. );
  47. return $productDistribution;
  48. }
  49. public function initQuantityMax(ProductDistribution $productDistribution): ProductDistribution
  50. {
  51. $productDistribution->quantity_max = $this->productSolver->getProductQuantityMaxByDay(
  52. $productDistribution->product,
  53. $this->distributionSolver->getDistributionDayAsString($productDistribution->distribution)
  54. );
  55. return $productDistribution;
  56. }
  57. }