選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

OrderShopModel.php 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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. }