Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

674 lines
18KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Model\Order;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Gedmo\Mapping\Annotation as Gedmo;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Lc\CaracoleBundle\Doctrine\Extension\FilterSectionInterface;
  8. use Lc\CaracoleBundle\Model\Address\AddressInterface;
  9. use Lc\CaracoleBundle\Model\Credit\CreditHistoryInterface;
  10. use Lc\CaracoleBundle\Model\File\DocumentInterface;
  11. use Lc\CaracoleBundle\Model\File\DocumentModel;
  12. use Lc\CaracoleBundle\Model\Section\SectionInterface;
  13. use Lc\SovBundle\Model\Ticket\TicketInterface;
  14. use Lc\CaracoleBundle\Model\User\VisitorInterface;
  15. use Lc\SovBundle\Doctrine\Pattern\AbstractLightEntity;
  16. use Lc\SovBundle\Model\User\UserInterface;
  17. /**
  18. * @ORM\MappedSuperclass()
  19. */
  20. abstract class OrderShopModel extends AbstractLightEntity implements FilterSectionInterface
  21. {
  22. /**
  23. * @ORM\ManyToOne(targetEntity="Lc\SovBundle\Model\User\UserInterface", inversedBy="orders", fetch="EAGER")
  24. */
  25. protected $user;
  26. /**
  27. * @ORM\ManyToOne(targetEntity="Lc\CaracoleBundle\Model\User\VisitorInterface", inversedBy="orders")
  28. */
  29. protected $visitor;
  30. /**
  31. * @ORM\ManyToOne(targetEntity="Lc\CaracoleBundle\Model\Address\AddressInterface")
  32. */
  33. protected $invoiceAddress;
  34. /**
  35. * @ORM\Column(type="text", nullable=true)
  36. */
  37. protected $invoiceAddressText;
  38. /**
  39. * @ORM\Column(type="datetime", nullable=true)
  40. */
  41. protected $validationDate;
  42. /**
  43. * @ORM\Column(type="text", nullable=true)
  44. */
  45. protected $comment;
  46. /**
  47. * @ORM\Column(type="boolean", nullable=true)
  48. */
  49. protected $autoPayment;
  50. /**
  51. * @ORM\Column(type="string", length=31, nullable=true)
  52. */
  53. protected $meanPayment;
  54. /**
  55. * @ORM\OneToMany(targetEntity="Lc\CaracoleBundle\Model\Order\OrderStatusHistoryInterface", mappedBy="orderShop", orphanRemoval=true)
  56. */
  57. protected $orderStatusHistories;
  58. /**
  59. * @ORM\OneToMany(targetEntity="Lc\CaracoleBundle\Model\Order\OrderPaymentInterface", mappedBy="orderShop", orphanRemoval=true)
  60. */
  61. protected $orderPayments;
  62. /**
  63. * @ORM\OneToMany(targetEntity="Lc\CaracoleBundle\Model\Order\OrderProductInterface", mappedBy="orderShop", cascade={"persist", "remove"}, orphanRemoval=true, fetch="EAGER")
  64. */
  65. protected $orderProducts;
  66. /**
  67. * @ORM\Column(type="text", nullable=true)
  68. */
  69. protected $deliveryInfos;
  70. /**
  71. * @ORM\ManyToOne(targetEntity="Lc\CaracoleBundle\Model\Order\OrderStatusInterface")
  72. * @ORM\JoinColumn(nullable=false)
  73. */
  74. protected $orderStatus;
  75. /**
  76. * @ORM\OneToMany(targetEntity="Lc\CaracoleBundle\Model\Order\OrderReductionCartInterface", mappedBy="orderShop", orphanRemoval=true)
  77. */
  78. protected $orderReductionCarts;
  79. /**
  80. * @ORM\OneToMany(targetEntity="Lc\CaracoleBundle\Model\Order\OrderReductionCreditInterface", mappedBy="orderShop", orphanRemoval=true)
  81. */
  82. protected $orderReductionCredits;
  83. /**
  84. * @ORM\ManyToMany(targetEntity="Lc\CaracoleBundle\Model\File\DocumentInterface", inversedBy="orderShops")
  85. */
  86. protected $documents;
  87. /**
  88. * @Gedmo\Blameable(on="create")
  89. * @ORM\ManyToOne(targetEntity="Lc\SovBundle\Model\User\UserInterface")
  90. * @ORM\JoinColumn(nullable=true)
  91. */
  92. protected $createdBy;
  93. /**
  94. * @Gedmo\Blameable(on="update")
  95. * @ORM\ManyToOne(targetEntity="Lc\SovBundle\Model\User\UserInterface")
  96. * @ORM\JoinColumn(nullable=true)
  97. */
  98. protected $updatedBy;
  99. /**
  100. * @ORM\OneToMany(targetEntity="Lc\SovBundle\Model\Ticket\TicketInterface", mappedBy="orderShop")
  101. */
  102. protected $tickets;
  103. /**
  104. * @ORM\ManyToOne(targetEntity="Lc\CaracoleBundle\Model\Section\SectionInterface", inversedBy="orderShops")
  105. * @ORM\JoinColumn(nullable=false)
  106. */
  107. protected $section;
  108. /**
  109. * @ORM\Column(type="integer", nullable=true)
  110. */
  111. protected $cycleId;
  112. public function __construct()
  113. {
  114. $this->orderStatusHistories = new ArrayCollection();
  115. $this->orderPayments = new ArrayCollection();
  116. $this->orderProducts = new ArrayCollection();
  117. $this->creditHistories = new ArrayCollection();
  118. $this->orderReductionCarts = new ArrayCollection();
  119. $this->orderReductionCredits = new ArrayCollection();
  120. $this->documents = new ArrayCollection();
  121. }
  122. public function __toString()
  123. {
  124. if ($this->getValidationDate()) {
  125. return 'Commande du ' . $this->getValidationDate()->format('d/m/Y');
  126. } else {
  127. return 'Commande #' . $this->getId();
  128. }
  129. }
  130. public function countQuantities()
  131. {
  132. return self::countQuantitiesByOrderProducts($this->getOrderProducts());
  133. }
  134. public static function countQuantitiesByOrderProducts($orderProducts = [])
  135. {
  136. $count = 0;
  137. foreach ($orderProducts as $orderProduct) {
  138. $count += $orderProduct->getQuantityOrder();
  139. }
  140. return $count;
  141. }
  142. public function getOrderProductsByParentCategory()
  143. {
  144. $categoriesArray = [];
  145. foreach ($this->getOrderProducts() as $orderProduct) {
  146. $productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories();
  147. $category = $productCategories[0]->getParentCategory();
  148. $labelCategory = $category->getTitle();
  149. if (!isset($categoriesArray[$labelCategory])) {
  150. $categoriesArray[$labelCategory] = [];
  151. }
  152. $categoriesArray[$labelCategory][] = $orderProduct;
  153. }
  154. return $categoriesArray;
  155. }
  156. public function hasOrderProductAlreadyInCart($orderProductTest)
  157. {
  158. foreach($this->getOrderProducts() as $orderProduct) {
  159. if($orderProduct->getProduct() == $orderProductTest->getProduct()) {
  160. return $orderProduct ;
  161. }
  162. }
  163. return false ;
  164. }
  165. public function getOrderProductsByProductFamily($productFamily)
  166. {
  167. $arrayOrderProducts = [] ;
  168. foreach($this->getOrderProducts() as $orderProduct) {
  169. if($orderProduct->getProduct()->getProductFamily() == $productFamily) {
  170. $arrayOrderProducts[] = $orderProduct ;
  171. }
  172. }
  173. return $arrayOrderProducts ;
  174. }
  175. public function getValidationDate(): ?\DateTimeInterface
  176. {
  177. return $this->validationDate;
  178. }
  179. public function setValidationDate(\DateTimeInterface $validationDate): self
  180. {
  181. $this->validationDate = $validationDate;
  182. return $this;
  183. }
  184. public function getDateCreated()
  185. {
  186. $orderStatusHistory = $this->getOrderStatusHistory(OrderStatusModel::ALIAS_WAITING_DELIVERY);
  187. if ($orderStatusHistory) {
  188. return $orderStatusHistory->getCreatedAt();
  189. }
  190. return null;
  191. }
  192. public function getOrderStatusHistory($status)
  193. {
  194. $orderStatusHistories = $this->getOrderStatusHistories();
  195. if (count($orderStatusHistories) > 0) {
  196. foreach ($orderStatusHistories as $orderStatusHistory) {
  197. if ($orderStatusHistory->getOrderStatus() == $status) {
  198. return $orderStatusHistory;
  199. }
  200. }
  201. }
  202. return null;
  203. }
  204. public function getUser(): ?UserInterface
  205. {
  206. return $this->user;
  207. }
  208. public function setUser(?UserInterface $user): self
  209. {
  210. $this->user = $user;
  211. return $this;
  212. }
  213. public function getInvoiceAddress(): ?AddressInterface
  214. {
  215. return $this->invoiceAddress;
  216. }
  217. public function setInvoiceAddress(?AddressInterface $invoiceAddress): self
  218. {
  219. $this->invoiceAddress = $invoiceAddress;
  220. return $this;
  221. }
  222. public function getInvoiceAddressText(): ?string
  223. {
  224. return $this->invoiceAddressText;
  225. }
  226. public function setInvoiceAddressText(string $invoiceAddressText): self
  227. {
  228. $this->invoiceAddressText = $invoiceAddressText;
  229. return $this;
  230. }
  231. public function getComment(): ?string
  232. {
  233. return $this->comment;
  234. }
  235. public function setComment(?string $comment): self
  236. {
  237. $this->comment = $comment;
  238. return $this;
  239. }
  240. public function getAutoPayment(): ?bool
  241. {
  242. return $this->autoPayment;
  243. }
  244. public function setAutoPayment(bool $autoPayment): self
  245. {
  246. $this->autoPayment = $autoPayment;
  247. return $this;
  248. }
  249. public function getMeanPayment(): ?string
  250. {
  251. return $this->meanPayment;
  252. }
  253. public function setMeanPayment(string $meanPayment): self
  254. {
  255. $this->meanPayment = $meanPayment;
  256. return $this;
  257. }
  258. /**
  259. * @return Collection|OrderStatusHistoryInterface[]
  260. */
  261. public function getOrderStatusHistories(): Collection
  262. {
  263. return $this->orderStatusHistories;
  264. }
  265. public function addOrderStatusHistory(OrderStatusHistoryInterface $orderStatusHistory): self
  266. {
  267. if (!$this->orderStatusHistories->contains($orderStatusHistory)) {
  268. $this->orderStatusHistories[] = $orderStatusHistory;
  269. $orderStatusHistory->setOrderShop($this);
  270. }
  271. return $this;
  272. }
  273. public function removeOrderStatusHistory(OrderStatusHistoryInterface $orderStatusHistory): self
  274. {
  275. if ($this->orderStatusHistories->contains($orderStatusHistory)) {
  276. $this->orderStatusHistories->removeElement($orderStatusHistory);
  277. // set the owning side to null (unless already changed)
  278. if ($orderStatusHistory->getOrderShop() === $this) {
  279. $orderStatusHistory->setOrderShop(null);
  280. }
  281. }
  282. return $this;
  283. }
  284. /**
  285. * @return Collection|OrderPaymentInterface[]
  286. */
  287. public function getOrderPayments($meanPayment = null): Collection
  288. {
  289. if ($meanPayment) {
  290. $orderPaymentsReturn = new ArrayCollection();
  291. foreach ($this->orderPayments as $orderPayment) {
  292. if ($orderPayment->getMeanPayment() == $meanPayment) {
  293. $orderPaymentsReturn[] = $orderPayment;
  294. }
  295. }
  296. return $orderPaymentsReturn;
  297. }
  298. return $this->orderPayments;
  299. }
  300. public function addOrderPayment(OrderPaymentInterface $orderPayment): self
  301. {
  302. if (!$this->orderPayments->contains($orderPayment)) {
  303. $this->orderPayments[] = $orderPayment;
  304. $orderPayment->setOrderShop($this);
  305. }
  306. return $this;
  307. }
  308. public function removeOrderPayment(OrderPaymentInterface $orderPayment): self
  309. {
  310. if ($this->orderPayments->contains($orderPayment)) {
  311. $this->orderPayments->removeElement($orderPayment);
  312. // set the owning side to null (unless already changed)
  313. if ($orderPayment->getOrderShop() === $this) {
  314. $orderPayment->setOrderShop(null);
  315. }
  316. }
  317. return $this;
  318. }
  319. /**
  320. * @return Collection|OrderProductInterface[]
  321. */
  322. public function getOrderProducts(): Collection
  323. {
  324. return $this->orderProducts;
  325. }
  326. public function addOrderProduct(OrderProductInterface $orderProduct): self
  327. {
  328. if (!$this->orderProducts->contains($orderProduct)) {
  329. $this->orderProducts[] = $orderProduct;
  330. $orderProduct->setOrderShop($this);
  331. }
  332. return $this;
  333. }
  334. public function removeOrderProduct(OrderProductInterface $orderProduct): self
  335. {
  336. if ($this->orderProducts->contains($orderProduct)) {
  337. $this->orderProducts->removeElement($orderProduct);
  338. // set the owning side to null (unless already changed)
  339. if ($orderProduct->getOrderShop() === $this) {
  340. $orderProduct->setOrderShop(null);
  341. }
  342. }
  343. return $this;
  344. }
  345. /**
  346. * @return Collection|CreditHistoryInterface[]
  347. */
  348. public function getCreditHistories(): Collection
  349. {
  350. return $this->creditHistories;
  351. }
  352. public function addCreditHistory(CreditHistoryInterface $creditHistory): self
  353. {
  354. if (!$this->creditHistories->contains($creditHistory)) {
  355. $this->creditHistories[] = $creditHistory;
  356. $creditHistory->setOrderShop($this);
  357. }
  358. return $this;
  359. }
  360. public function removeCreditHistory(CreditHistoryInterface $creditHistory): self
  361. {
  362. if ($this->creditHistories->contains($creditHistory)) {
  363. $this->creditHistories->removeElement($creditHistory);
  364. // set the owning side to null (unless already changed)
  365. if ($creditHistory->getOrderShop() === $this) {
  366. $creditHistory->setOrderShop(null);
  367. }
  368. }
  369. return $this;
  370. }
  371. public function getVisitor(): ?VisitorInterface
  372. {
  373. return $this->visitor;
  374. }
  375. public function setVisitor(?VisitorInterface $visitor): self
  376. {
  377. $this->visitor = $visitor;
  378. return $this;
  379. }
  380. public function getDeliveryInfos(): ?string
  381. {
  382. return $this->deliveryInfos;
  383. }
  384. public function setDeliveryInfos(?string $deliveryInfos): self
  385. {
  386. $this->deliveryInfos = $deliveryInfos;
  387. return $this;
  388. }
  389. public function getOrderStatus(): ?OrderStatusInterface
  390. {
  391. return $this->orderStatus;
  392. }
  393. public function setOrderStatusProtected(?OrderStatusInterface $orderStatus): self
  394. {
  395. $this->orderStatus = $orderStatus;
  396. return $this;
  397. }
  398. /**
  399. * @return Collection|OrderReductionCartInterface[]
  400. */
  401. public function getOrderReductionCarts(): Collection
  402. {
  403. return $this->orderReductionCarts;
  404. }
  405. public function addOrderReductionCart(OrderReductionCartInterface $orderReductionCart): self
  406. {
  407. if (!$this->orderReductionCarts->contains($orderReductionCart)) {
  408. $this->orderReductionCarts[] = $orderReductionCart;
  409. $orderReductionCart->setOrderShop($this);
  410. }
  411. return $this;
  412. }
  413. public function removeOrderReductionCart(OrderReductionCartInterface $orderReductionCart): self
  414. {
  415. if ($this->orderReductionCarts->contains($orderReductionCart)) {
  416. $this->orderReductionCarts->removeElement($orderReductionCart);
  417. // set the owning side to null (unless already changed)
  418. if ($orderReductionCart->getOrderShop() === $this) {
  419. $orderReductionCart->setOrderShop(null);
  420. }
  421. }
  422. return $this;
  423. }
  424. /**
  425. * @return Collection|OrderReductionCreditInterface[]
  426. */
  427. public function getOrderReductionCredits(): Collection
  428. {
  429. return $this->orderReductionCredits;
  430. }
  431. public function addOrderReductionCredit(OrderReductionCreditInterface $orderReductionCredit): self
  432. {
  433. if (!$this->orderReductionCredits->contains($orderReductionCredit)) {
  434. $this->orderReductionCredits[] = $orderReductionCredit;
  435. $orderReductionCredit->setOrderShop($this);
  436. }
  437. return $this;
  438. }
  439. public function removeOrderReductionCredit(OrderReductionCreditInterface $orderReductionCredit): self
  440. {
  441. if ($this->orderReductionCredits->contains($orderReductionCredit)) {
  442. $this->orderReductionCredits->removeElement($orderReductionCredit);
  443. // set the owning side to null (unless already changed)
  444. if ($orderReductionCredit->getOrderShop() === $this) {
  445. $orderReductionCredit->setOrderShop(null);
  446. }
  447. }
  448. return $this;
  449. }
  450. /**
  451. * @return Collection|DocumentInterface[]
  452. */
  453. public function getDocuments(): Collection
  454. {
  455. return $this->documents;
  456. }
  457. public function addDocument(DocumentInterface $document): self
  458. {
  459. if (!$this->documents->contains($document)) {
  460. $this->documents[] = $document;
  461. }
  462. return $this;
  463. }
  464. public function removeDocument(DocumentInterface $document): self
  465. {
  466. if ($this->documents->contains($document)) {
  467. $this->documents->removeElement($document);
  468. }
  469. return $this;
  470. }
  471. public function getDocumentInvoice(): DocumentInterface
  472. {
  473. foreach ($this->getDocuments() as $document) {
  474. if ($document->getType() == DocumentModel::TYPE_INVOICE) {
  475. return $document;
  476. }
  477. }
  478. return false;
  479. }
  480. /**
  481. * @return Collection|TicketInterface[]
  482. */
  483. public function getTickets(): Collection
  484. {
  485. return $this->tickets;
  486. }
  487. public function addTicket(TicketInterface $ticket): self
  488. {
  489. if (!$this->tickets->contains($ticket)) {
  490. $this->tickets[] = $ticket;
  491. $ticket->setOrderShop($this);
  492. }
  493. return $this;
  494. }
  495. public function removeTicket(TicketInterface $ticket): self
  496. {
  497. if ($this->tickets->contains($ticket)) {
  498. $this->tickets->removeElement($ticket);
  499. // set the owning side to null (unless already changed)
  500. if ($ticket->getOrderShop() === $this) {
  501. $ticket->setOrderShop(null);
  502. }
  503. }
  504. return $this;
  505. }
  506. public function isValid()
  507. {
  508. if ($this->getOrderStatus() && in_array(
  509. $this->getOrderStatus()->getAlias(),
  510. OrderStatusModel::$statusAliasAsValid
  511. ) > 0) {
  512. return true;
  513. }
  514. return false;
  515. }
  516. public function isCart()
  517. {
  518. if ($this->getOrderStatus() && in_array(
  519. $this->getOrderStatus()->getAlias(),
  520. OrderStatusModel::$statusAliasAsCart
  521. ) > 0) {
  522. return true;
  523. }
  524. return false;
  525. }
  526. public function getSection(): ?SectionInterface
  527. {
  528. return $this->section;
  529. }
  530. public function setSection(?SectionInterface $section): self
  531. {
  532. $this->section = $section;
  533. return $this;
  534. }
  535. public function getCycleId(): ?int
  536. {
  537. return $this->cycleId;
  538. }
  539. public function setCycleId(?int $cycleId): self
  540. {
  541. $this->cycleId = $cycleId;
  542. return $this;
  543. }
  544. }