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.

147 lines
3.4KB

  1. <?php
  2. namespace domain\Product\Rotating;
  3. use common\components\ActiveRecordCommon;
  4. use common\components\Date;
  5. use domain\Distribution\DistributionRotating\DistributionRotating;
  6. use domain\Producer\Producer\Producer;
  7. use domain\Product\RotatingProduct\RotatingProduct;
  8. use yii\db\ActiveQuery;
  9. class Rotating extends ActiveRecordCommon
  10. {
  11. // Produits sélectionnés dans le formulaire
  12. public $selected_products_ids;
  13. public static function tableName()
  14. {
  15. return 'rotating';
  16. }
  17. public function rules()
  18. {
  19. return [
  20. [['id_producer', 'name', 'day', 'selected_products_ids'], 'required'],
  21. [['name'], 'string', 'max' => 255],
  22. [['id_producer', 'day', 'status'], 'integer'],
  23. [['selected_products_ids'], 'verifySelectedProductsIds'],
  24. [['selected_products_ids'], 'safe'],
  25. ];
  26. }
  27. public function attributeLabels()
  28. {
  29. return [
  30. 'id' => 'ID',
  31. 'id_producer' => 'Producteur',
  32. 'name' => 'Nom',
  33. 'day' => 'Jour',
  34. 'status' => 'Statut',
  35. 'selected_products_ids' => "Produits"
  36. ];
  37. }
  38. public function verifySelectedProductsIds($attribute, $params)
  39. {
  40. if(count($this->getSelectedProductsIds()) < 2) {
  41. $this->addError($attribute, "Vous devez sélectionner au moins deux produits.");
  42. }
  43. }
  44. /* Getters / Setters */
  45. public function getId(): ?int
  46. {
  47. return $this->id;
  48. }
  49. public function getProducer(): Producer
  50. {
  51. return $this->producerRelation;
  52. }
  53. public function setProducer(Producer $producer): self
  54. {
  55. $this->populateFieldObject('id_producer', 'producerRelation', $producer);
  56. return $this;
  57. }
  58. public function getName(): string
  59. {
  60. return $this->name;
  61. }
  62. public function setName(string $name): self
  63. {
  64. $this->name = $name;
  65. return $this;
  66. }
  67. public function getDay(): int
  68. {
  69. return $this->day;
  70. }
  71. public function setDay(int $day): self
  72. {
  73. $this->day = $day;
  74. return $this;
  75. }
  76. public function getStatus(): int
  77. {
  78. return $this->status;
  79. }
  80. public function setStatus(int $status): self
  81. {
  82. $this->status = $status;
  83. return $this;
  84. }
  85. public function getSelectedProductsIds()
  86. {
  87. return $this->selected_products_ids;
  88. }
  89. public function setSelectedProductsIds(array $selectedProductsIdsArray): self
  90. {
  91. $this->selected_products_ids = $selectedProductsIdsArray;
  92. return $this;
  93. }
  94. public function getDistributionRotatings(): array
  95. {
  96. return $this->distributionRotatingsRelation;
  97. }
  98. public function getRotatingProducts(): array
  99. {
  100. return $this->rotatingProductsRelation;
  101. }
  102. /* Relations */
  103. public function getProducerRelation(): ActiveQuery
  104. {
  105. return $this->hasOne(Producer::class, ['id' => 'id_producer']);
  106. }
  107. public function getRotatingProductsRelation()
  108. {
  109. return $this->hasMany(RotatingProduct::class, ['id_rotating' => 'id']);
  110. }
  111. public function getDistributionRotatingsRelation()
  112. {
  113. return $this->hasMany(DistributionRotating::class, ['id_rotating' => 'id']);
  114. }
  115. /* Méthodes */
  116. public function getDayAsString(string $lang = 'fr'): string
  117. {
  118. return Date::getDayOfWeekStringByNumber($this->getDay(), $lang);
  119. }
  120. }