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.

698 line
19KB

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