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.

85 lines
2.9KB

  1. <?php
  2. namespace common\logic\Distribution\ProductDistribution;
  3. use common\logic\BaseService;
  4. use common\logic\BuilderInterface;
  5. use common\logic\Distribution\Distribution\Distribution;
  6. use common\logic\Distribution\Distribution\DistributionSolver;
  7. use common\logic\Product\Product\Product;
  8. class ProductDistributionBuilder extends BaseService implements BuilderInterface
  9. {
  10. protected ProductDistributionRepository $productDistributionRepository;
  11. protected DistributionSolver $distributionSolver;
  12. public function __construct()
  13. {
  14. $this->productDistributionRepository = $this->loadService(ProductDistributionRepository::class);
  15. $this->distributionSolver = $this->loadService(DistributionSolver::class);
  16. }
  17. public function instanciate(Distribution $distribution, Product $product): ProductDistribution
  18. {
  19. $productDistribution = new ProductDistribution();
  20. $productDistribution->populateRelation('distribution', $distribution);
  21. $productDistribution->id_distribution = $distribution->id;
  22. $productDistribution->populateRelation('product', $product);
  23. $productDistribution->id_product = $product->id;
  24. return $productDistribution;
  25. }
  26. public function create(Distribution $distribution, Product $product): ProductDistribution
  27. {
  28. $productDistribution = $this->instanciate($distribution, $product);
  29. $this
  30. ->initActive($productDistribution)
  31. ->initQuantityMax($productDistribution);
  32. $productDistribution->save();
  33. return $productDistribution;
  34. }
  35. public function createIfNotExist(Distribution $distribution, Product $product): ProductDistribution
  36. {
  37. $productDistribution = $this->productDistributionRepository->getOne($distribution, $product);
  38. if(!$productDistribution) {
  39. $productDistribution = $this->create($distribution, $product);
  40. }
  41. return $productDistribution;
  42. }
  43. public function initActive(ProductDistribution $productDistribution): ProductDistribution
  44. {
  45. $day = $this->distributionSolver->getDayAsString($productDistribution->distribution);
  46. $productDistribution->active = 0;
  47. if ($productDistribution->product->active && $productDistribution->product->$day) {
  48. $productDistribution->active = 1;
  49. }
  50. return $productDistribution;
  51. }
  52. public function initQuantityMax(ProductDistribution $productDistribution): ProductDistribution
  53. {
  54. $product = $productDistribution->product;
  55. $day = $this->distributionSolver->getDayAsString($productDistribution->distribution);
  56. $productDistribution->quantity_max = $product->quantity_max;
  57. $fieldQuantityMax = 'quantity_max_' . $day;
  58. if (isset($product->$fieldQuantityMax) && $product->$fieldQuantityMax > 0) {
  59. $productDistribution->quantity_max = $product->$fieldQuantityMax;
  60. }
  61. return $productDistribution;
  62. }
  63. }