Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ProductDistributionBuilder.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. }