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.

SharedPointSale.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace domain\PointSale\SharedPointSale;
  3. use common\components\ActiveRecordCommon;
  4. use domain\PointSale\PointSale\PointSale;
  5. use domain\Producer\Producer\Producer;
  6. use domain\User\User\User;
  7. class SharedPointSale extends ActiveRecordCommon
  8. {
  9. public ?Producer $producerCurrent;
  10. public const SCENARIO_CREATE = 'create';
  11. public const SCENARIO_CONFIRM = 'confirm';
  12. public static function tableName()
  13. {
  14. return 'shared_point_sale';
  15. }
  16. public function rules()
  17. {
  18. return [
  19. [['id_point_sale', 'id_producer_with_sharing', 'created_at', 'created_by'], 'required'],
  20. ['id_point_sale_with_sharing', 'required', 'on' => self::SCENARIO_CONFIRM],
  21. [['id_point_sale', 'id_producer_with_sharing', 'id_point_sale_with_sharing', 'status',
  22. 'created_by', 'confirmed_by', 'declined_by'], 'integer'],
  23. [['created_at', 'confirmed_at', 'declined_at'], 'safe'],
  24. ['id_point_sale', 'verifySharedPointSale', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_CONFIRM]],
  25. ['id_point_sale', 'exist', 'targetClass' => PointSale::class, 'targetAttribute' => 'id'],
  26. ['id_point_sale_with_sharing', 'exist', 'targetClass' => PointSale::class, 'targetAttribute' => 'id'],
  27. ['id_point_sale_with_sharing', 'verifyConfirm'],
  28. ['id_producer_with_sharing', 'exist', 'targetClass' => Producer::class, 'targetAttribute' => 'id'],
  29. [['created_by', 'confirmed_by', 'declined_by'], 'exist', 'targetClass' => User::class, 'targetAttribute' => 'id'],
  30. ];
  31. }
  32. public function attributeLabels()
  33. {
  34. return [
  35. 'id_point_sale' => 'Point de vente',
  36. 'id_producer_with_sharing' => 'Producteur',
  37. 'id_point_sale_with_sharing' => 'Point de vente partagé',
  38. ];
  39. }
  40. public function verifySharedPointSale($attribute, $params)
  41. {
  42. $sharedPointSaleRepository = SharedPointSaleRepository::getInstance();
  43. if($this->scenario == self::SCENARIO_CREATE || $this->scenario == self::SCENARIO_CONFIRM) {
  44. $sharedPointSaleConfirmed = $sharedPointSaleRepository->findSharedPointsSaleConfirmedByPointSale(
  45. $this->getPointSale(),
  46. $this->getProducerWithSharing()
  47. );
  48. if($sharedPointSaleConfirmed && count($sharedPointSaleConfirmed)) {
  49. $this->addError($attribute, "Ce point de vente est déjà partagé avec ce producteur.");
  50. }
  51. }
  52. if($this->scenario == self::SCENARIO_CREATE) {
  53. $sharedPointSaleRequest = $sharedPointSaleRepository->findSharedPointSaleRequestByPointSale(
  54. $this->getPointSale(),
  55. $this->getProducerWithSharing()
  56. );
  57. if($sharedPointSaleRequest && count($sharedPointSaleRequest)) {
  58. $this->addError($attribute, "Ce point de vente a déjà une demande de partage en attente avec ce producteur.");
  59. }
  60. }
  61. }
  62. public function verifyConfirm($attribute, $params)
  63. {
  64. if($this->scenario == self::SCENARIO_CONFIRM) {
  65. if($this->producerCurrent->id != $this->getProducerWithSharing()->id) {
  66. $this->addError($attribute, "Vous ne pouvez pas confirmer cette demande de partage.");
  67. }
  68. }
  69. }
  70. /* Getters / Setters */
  71. public function getId(): ?int
  72. {
  73. return $this->id;
  74. }
  75. public function getPointSale(): PointSale
  76. {
  77. return $this->pointSaleRelation;
  78. }
  79. public function setPointSale(PointSale $pointSale): self
  80. {
  81. $this->populateFieldObject('id_point_sale', 'pointSaleRelation', $pointSale);
  82. return $this;
  83. }
  84. public function getProducerWithSharing(): Producer
  85. {
  86. return $this->producerWithSharingRelation;
  87. }
  88. public function setProducerWithSharing(Producer $producerWithSharing): self
  89. {
  90. $this->populateFieldObject('id_producer_with_sharing', 'producerWithSharingRelation', $producerWithSharing);
  91. return $this;
  92. }
  93. public function getPointSaleWithSharing(): ?PointSale
  94. {
  95. return $this->pointSaleWithSharingRelation;
  96. }
  97. public function setPointSaleWithSharing(PointSale $pointSaleWithSharing = null): self
  98. {
  99. if($pointSaleWithSharing) {
  100. $this->populateFieldObject('id_point_sale_with_sharing', 'pointSaleWithSharingRelation', $pointSaleWithSharing);
  101. }
  102. return $this;
  103. }
  104. public function getStatus(): int
  105. {
  106. return $this->status;
  107. }
  108. public function setStatus(int $status): self
  109. {
  110. $this->status = $status;
  111. return $this;
  112. }
  113. public function getCreatedAt(): \DateTime
  114. {
  115. return new \DateTime($this->created_at);
  116. }
  117. public function setCreatedAt(\DateTime $createdAt): self
  118. {
  119. $this->created_at = $createdAt->format('Y-m-d H:i:s');
  120. return $this;
  121. }
  122. public function getConfirmedAt(): \DateTime
  123. {
  124. return new \DateTime($this->confirmed_at);
  125. }
  126. public function setConfirmedAt(\DateTime $confirmedAt): self
  127. {
  128. $this->confirmed_at = $confirmedAt->format('Y-m-d H:i:s');
  129. return $this;
  130. }
  131. public function getDeclinedAt(): \DateTime
  132. {
  133. return new \DateTime($this->declined_at);
  134. }
  135. public function setDeclinedAt(\DateTime $declinedAt): self
  136. {
  137. $this->declined_at = $declinedAt->format('Y-m-d H:i:s');
  138. return $this;
  139. }
  140. public function getCreatedBy(): User
  141. {
  142. return $this->createdByRelation;
  143. }
  144. public function setCreatedBy(User $createdBy): SharedPointSale
  145. {
  146. $this->populateFieldObject('created_by', 'createdByRelation', $createdBy);
  147. return $this;
  148. }
  149. public function getConfirmedBy(): User
  150. {
  151. return $this->confirmedByRelation;
  152. }
  153. public function setConfirmedBy(User $confirmedBy): SharedPointSale
  154. {
  155. $this->populateFieldObject('confirmed_by', 'confirmedByRelation', $confirmedBy);
  156. return $this;
  157. }
  158. public function getDeclinedBy(): User
  159. {
  160. return $this->declinedByRelation;
  161. }
  162. public function setDeclinedBy(User $declinedBy): SharedPointSale
  163. {
  164. $this->populateFieldObject('declined_by', 'declinedByRelation', $declinedBy);
  165. return $this;
  166. }
  167. /* Relations */
  168. public function getPointSaleRelation()
  169. {
  170. return $this->hasOne(PointSale::class, ['id' => 'id_point_sale']);
  171. }
  172. public function getProducerWithSharingRelation()
  173. {
  174. return $this->hasOne(Producer::class, ['id' => 'id_producer_with_sharing']);
  175. }
  176. public function getPointSaleWithSharingRelation()
  177. {
  178. return $this->hasOne(PointSale::class, ['id' => 'id_point_sale_with_sharing']);
  179. }
  180. public function getCreatedByRelation()
  181. {
  182. return $this->hasOne(User::class, ['id' => 'created_by']);
  183. }
  184. public function getConfirmedByRelation()
  185. {
  186. return $this->hasOne(User::class, ['id' => 'confirmed_by']);
  187. }
  188. public function getDeclinedByRelation()
  189. {
  190. return $this->hasOne(User::class, ['id' => 'declined_by']);
  191. }
  192. }