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.

OrderUtils.php 21KB

4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <?php
  2. namespace Lc\ShopBundle\Services;
  3. use App\Entity\OrderProductReductionCatalog;
  4. use App\Entity\OrderShop;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Lc\ShopBundle\Context\DocumentInterface;
  7. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  8. use Lc\ShopBundle\Context\OrderPaymentInterface;
  9. use Lc\ShopBundle\Context\OrderReductionCartInterface;
  10. use Lc\ShopBundle\Context\OrderProductInterface;
  11. use Lc\ShopBundle\Context\OrderReductionCreditInterface;
  12. use Lc\ShopBundle\Context\OrderShopInterface;
  13. use Lc\ShopBundle\Context\OrderStatusHistoryInterface;
  14. use Lc\ShopBundle\Context\OrderStatusInterface;
  15. use Lc\ShopBundle\Context\ProductFamilyUtilsInterface;
  16. use Lc\ShopBundle\Context\ReductionCartInterface;
  17. use Lc\ShopBundle\Context\ReductionCreditInterface;
  18. use Lc\ShopBundle\Context\UserInterface;
  19. use Lc\ShopBundle\Model\Document;
  20. use Lc\ShopBundle\Form\Backend\Order\OrderReductionCreditType;
  21. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  22. use Symfony\Component\Security\Core\Security;
  23. class OrderUtils
  24. {
  25. protected $em;
  26. protected $security;
  27. protected $userUtils;
  28. protected $merchantUtils;
  29. protected $orderShopRepo;
  30. protected $priceUtils;
  31. protected $productFamilyUtils;
  32. protected $documentUtils ;
  33. public function __construct(EntityManagerInterface $em, Security $security, UserUtils $userUtils,
  34. MerchantUtilsInterface $merchantUtils, PriceUtils $priceUtils, ProductFamilyUtilsInterface $productFamilyUtils,
  35. DocumentUtils $documentUtils)
  36. {
  37. $this->em = $em;
  38. $this->security = $security;
  39. $this->userUtils = $userUtils;
  40. $this->merchantUtils = $merchantUtils;
  41. $this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName());
  42. $this->priceUtils = $priceUtils;
  43. $this->productFamilyUtils = $productFamilyUtils;
  44. $this->documentUtils = $documentUtils ;
  45. }
  46. public function getCartCurrent()
  47. {
  48. $paramsSearchOrderShop = [];
  49. $user = $this->security->getUser();
  50. $visitor = $this->userUtils->getVisitorCurrent();
  51. $orderShop = null;
  52. $orderShopUser = null;
  53. $orderShopVisitor = null;
  54. if ($user) {
  55. $orderShopUser = $this->orderShopRepo->findCartCurrent([
  56. 'user' => $user
  57. ]);
  58. }
  59. if ($visitor) {
  60. $orderShopVisitor = $this->orderShopRepo->findCartCurrent([
  61. 'visitor' => $visitor
  62. ]);
  63. }
  64. if ($orderShopUser || $orderShopVisitor) {
  65. // merge
  66. if ($orderShopUser && $orderShopVisitor && $orderShopUser != $orderShopVisitor
  67. && $orderShopVisitor->getOrderProducts() && count($orderShopVisitor->getOrderProducts())) {
  68. $orderShop = $this->mergeOrderShops($orderShopUser, $orderShopVisitor);
  69. $this->session->getFlashBag()->add('success', "Votre panier visiteur vient d'être fusionné avec votre panier client.");
  70. }
  71. else {
  72. $orderShop = ($orderShopUser) ? $orderShopUser : $orderShopVisitor;
  73. }
  74. // set user
  75. if($orderShop && $user && !$orderShop->getUser()) {
  76. $orderShop->setUser($user) ;
  77. $this->em->persist($orderShop) ;
  78. $this->em->flush() ;
  79. }
  80. }
  81. if (!$user && !$visitor) {
  82. $this->session->getFlashBag()->add('error', 'Vous devez accepter les cookies ou vous connecter pour avoir un panier sur le site.');
  83. }
  84. return $orderShop;
  85. }
  86. public function createOrderShop($params)
  87. {
  88. $orderShop = new OrderShop();
  89. $orderShopBelongTo = false;
  90. if (isset($params['user']) && $params['user']) {
  91. $orderShopBelongTo = true;
  92. $orderShop->setUser($params['user']);
  93. }
  94. if (isset($params['visitor']) && $params['visitor']) {
  95. $orderShopBelongTo = true;
  96. $orderShop->setVisitor($params['visitor']);
  97. }
  98. if (!$orderShopBelongTo) {
  99. throw new \ErrorException('La commande doit être liée à un utilisateur ou à un visiteur.');
  100. }
  101. if (isset($params['merchant']) && $params['merchant']) {
  102. $orderShop->setMerchant($params['merchant']);
  103. } else {
  104. throw new \ErrorException('La commande doit être liée à un merchant.');
  105. }
  106. $orderShop = $this->changeOrderStatus('cart', $orderShop);
  107. return $orderShop;
  108. }
  109. public function addOrderProduct($orderShop, $orderProductAdd, $persist = true)
  110. {
  111. if(!$orderShop) {
  112. $orderShop = $this->createOrderShop([
  113. 'user' => $this->security->getUser(),
  114. 'visitor' => $this->userUtils->getVisitorCurrent(),
  115. 'merchant' => $this->merchantUtils->getMerchantCurrent()
  116. ]);
  117. }
  118. if ($orderProductAdd->getQuantityOrder() > 0) {
  119. $updated = false;
  120. $orderProductAdd->setTitle($orderProductAdd->getTitleOrderShop());
  121. $orderProductAdd->setPrice($this->priceUtils->getPrice($orderProductAdd->getProduct()));
  122. $orderProductAdd->setUnit($orderProductAdd->getProduct()->getUnitInherited());
  123. $orderProductAdd->setTaxRate($orderProductAdd->getProduct()->getTaxRateInherited());
  124. $orderProductAdd->setQuantityProduct($orderProductAdd->getProduct()->getQuantityInherited());
  125. $productFamily = $this->productFamilyUtils->getProductFamilyBySlug($orderProductAdd->getProduct()->getProductFamily()->getSlug());
  126. $reductionCatalog = $productFamily->getReductionCatalog();
  127. if ($reductionCatalog) {
  128. $orderProductReductionCatalog = new OrderProductReductionCatalog();
  129. $orderProductReductionCatalog->setTitle($reductionCatalog->getTitle());
  130. $orderProductReductionCatalog->setValue($reductionCatalog->getValue());
  131. $orderProductReductionCatalog->setUnit($reductionCatalog->getUnit());
  132. $orderProductReductionCatalog->setBehaviorTaxRate($reductionCatalog->getBehaviorTaxRate());
  133. $orderProductAdd->setOrderProductReductionCatalog($orderProductReductionCatalog);
  134. }
  135. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  136. if ($orderProduct->getProduct()->getId() == $orderProductAdd->getProduct()->getId()
  137. && (string)$this->priceUtils->getPrice($orderProduct) == (string)$this->priceUtils->getPrice($orderProductAdd)
  138. && $this->compareOrderProductReductionCatalog($orderProduct->getOrderProductReductionCatalog(), $orderProductAdd->getOrderProductReductionCatalog())) {
  139. $orderProduct->setQuantityOrder($orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder());
  140. if ($persist) {
  141. $this->em->persist($orderProduct);
  142. }
  143. $updated = true;
  144. break;
  145. }
  146. }
  147. if (!$updated) {
  148. $orderShop->addOrderProduct($orderProductAdd);
  149. if (isset($orderProductReductionCatalog)) {
  150. $this->em->persist($orderProductReductionCatalog);
  151. if ($persist) {
  152. if (isset($orderProductReductionCatalog)) {
  153. $this->em->persist($orderProductReductionCatalog);
  154. }
  155. $this->em->persist($orderProductAdd);
  156. $this->em->persist($orderShop);
  157. }
  158. }
  159. if ($persist) {
  160. $this->em->flush();
  161. }
  162. }
  163. }
  164. }
  165. public function compareOrderProductReductionCatalog($orderProductReductionCatalog1, $orderProductReductionCatalog2)
  166. {
  167. return (!$orderProductReductionCatalog1 && !$orderProductReductionCatalog2)
  168. || ($orderProductReductionCatalog1
  169. && $orderProductReductionCatalog2
  170. && $orderProductReductionCatalog1->getUnit() == $orderProductReductionCatalog2->getUnit()
  171. && (string) $orderProductReductionCatalog1->getValue() == (string) $orderProductReductionCatalog2->getValue()
  172. && $orderProductReductionCatalog1->getBehaviorTaxRate() == $orderProductReductionCatalog2->getBehaviorTaxRate()) ;
  173. }
  174. public function countQuantities($orderShop)
  175. {
  176. return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts());
  177. }
  178. public function countQuantitiesByOrderProducts($orderProducts = [])
  179. {
  180. $count = 0;
  181. foreach ($orderProducts as $orderProduct) {
  182. $count += $orderProduct->getQuantityOrder();
  183. }
  184. return $count;
  185. }
  186. public function getOrderProductsByParentCategory($orderShop = null)
  187. {
  188. $categoriesArray = [];
  189. if($orderShop) {
  190. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  191. $productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories();
  192. $category = $productCategories[0]->getParentCategory();
  193. $labelCategory = $category->getTitle();
  194. if (!isset($categoriesArray[$labelCategory])) {
  195. $categoriesArray[$labelCategory] = [];
  196. }
  197. $categoriesArray[$labelCategory][] = $orderProduct;
  198. }
  199. }
  200. return $categoriesArray;
  201. }
  202. public function getOrderDatas($order = null)
  203. {
  204. $data = [];
  205. if (!$order) {
  206. $order = $this->getCartCurrent();
  207. }
  208. $data['order'] = $order;
  209. if ($order) {
  210. $data['count'] = $this->countQuantities($order);
  211. $data['total_with_tax'] = $this->priceUtils->getTotalWithTaxAndReduction($order);
  212. $data['order_products_by_category'] = $this->getOrderProductsByParentCategory($order);
  213. }
  214. return $data;
  215. }
  216. public function getOrderAsJsonObject(OrderShopInterface $order)
  217. {
  218. $data['id'] = $order->getId();
  219. $data['user'] = $order->getUser()->getSummary();
  220. $data['orderStatus'] = $order->getOrderStatus()->__tosString();
  221. $data['deliveryAddress'] = $order->getDeliveryAddress()->getSummary();
  222. $data['invoiceAddress'] = $order->getInvoiceAddress()->getSummary();
  223. $data['total'] = $this->priceUtils->getTotal($order);
  224. $data['totalWithTax'] = $this->priceUtils->getTotalWithTax($order);
  225. $data['totalWithTaxAndReduction'] = $this->priceUtils->getTotalWithTax($order);
  226. $i = 0;
  227. foreach ($this->getOrderProductsByParentCategory($order) as $labelCategory => $orderProducts) {
  228. foreach ($orderProducts as $orderProduct) {
  229. $data['orderProducts'][$i]['id'] = $orderProduct->getId();
  230. $data['orderProducts'][$i]['product'] = $orderProduct->getProduct()->getId();
  231. $data['orderProducts'][$i]['quantityOrder'] = $orderProduct->getQuantityOrder();
  232. $data['orderProducts'][$i]['labelCategory'] = $labelCategory;
  233. $data['orderProducts'][$i]['title'] = $orderProduct->getTitle();
  234. $data['orderProducts'][$i]['price'] = $this->priceUtils->getPrice($orderProduct);
  235. $data['orderProducts'][$i]['priceWithTax'] = $this->priceUtils->getPriceWithTax($orderProduct);
  236. $data['orderProducts'][$i]['priceWithTaxAndReduction'] = $this->priceUtils->getPriceWithTaxAndReduction($orderProduct);
  237. $data['orderProducts'][$i]['quantity'] = $orderProduct->getQuantityOrder();
  238. $data['orderProducts'][$i]['totalWithTaxAndReduction'] = $this->priceUtils->getTotalOrderProductsWithTaxAndReduction(array($orderProduct));
  239. $i++;
  240. }
  241. }
  242. return $data;
  243. }
  244. public function getSummaryOrderProductReductionCatalog($orderProductReductionCatalog)
  245. {
  246. $text = '';
  247. if ($orderProductReductionCatalog) {
  248. if ($orderProductReductionCatalog->getUnit() == 'amount') {
  249. $text .= '- ' . $orderProductReductionCatalog->getValue() . '&nbsp;€';
  250. }
  251. if ($orderProductReductionCatalog->getUnit() == 'percent') {
  252. $text .= '- ' . $orderProductReductionCatalog->getValue() . '&nbsp;%';
  253. }
  254. }
  255. return $text;
  256. }
  257. public function newOrderStatusHistory($order, $status, $origin = 'user')
  258. {
  259. $orderStatusHistoryClass = $this->em->getClassMetadata(OrderStatusHistoryInterface::class);
  260. $orderStatusHistory = new $orderStatusHistoryClass->name;
  261. $orderStatusHistory->setOrderShop($order);
  262. $orderStatusHistory->setOrderStatus($status);
  263. $orderStatusHistory->setOrigin($origin);
  264. $this->em->persist($orderStatusHistory);
  265. }
  266. public function createOrderReductionCart(OrderShopInterface $orderShop, ReductionCartInterface $reductionCart)
  267. {
  268. $orderReductionCartClass = $this->em->getClassMetadata(OrderReductionCartInterface::class);
  269. $orderReductionCart = new $orderReductionCartClass->name;
  270. $orderReductionCart->setOrderShop($orderShop);
  271. $orderReductionCart->setReductionCart($reductionCart);
  272. $orderReductionCart->setTitle($reductionCart->getTitle());
  273. $orderReductionCart->setValue($reductionCart->getValue());
  274. $orderReductionCart->setUnit($reductionCart->getUnit());
  275. $orderReductionCart->setBehaviorTaxRate($reductionCart->getBehaviorTaxRate());
  276. $orderReductionCart->setFreeShipping($reductionCart->getFreeShipping());
  277. $orderReductionCart->setAppliedTo($reductionCart->getAppliedTo());
  278. $orderReductionCart->setType($reductionCart->getType());
  279. $this->em->persist($orderReductionCart) ;
  280. $this->em->flush() ;
  281. return $orderReductionCart;
  282. }
  283. public function isReductionCreditAllowToBeAddToOrder($orderShop, $reductionCredit){
  284. if($this->orderShopRepo->findValidOrderWithReductionCredit($reductionCredit, $orderShop->getUser())>0){
  285. return false;
  286. }else{
  287. return true;
  288. }
  289. }
  290. public function createOrderReductionCredit(OrderShopInterface $orderShop, ReductionCreditInterface $reductionCredit)
  291. {
  292. $orderReductionCreditClass = $this->em->getClassMetadata(OrderReductionCreditInterface::class);
  293. $orderReductionCredit = new $orderReductionCreditClass->name;
  294. $orderReductionCredit->setOrderShop($orderShop);
  295. $orderReductionCredit->setReductionCredit($reductionCredit);
  296. $orderReductionCredit->setTitle($reductionCredit->getTitle());
  297. $orderReductionCredit->setValue($reductionCredit->getValue());
  298. $orderReductionCredit->setUnit($reductionCredit->getUnit());
  299. $orderReductionCredit->setBehaviorTaxRate($reductionCredit->getBehaviorTaxRate());
  300. return $orderReductionCredit;
  301. }
  302. /*public function getReductionCreditsAvailable($order)
  303. {
  304. $reductionCreditRepo = $this->em->getRepository(ReductionCreditInterface::class);
  305. $reductionCredits = $reductionCreditRepo->getReductionCreditByUser($order->getUser());
  306. foreach ($reductionCredits as $reductionCredit){
  307. }
  308. }*/
  309. public function mergeOrderShops($orderShop1, $orderShop2)
  310. {
  311. if ($orderShop1 && $orderShop2) {
  312. foreach ($orderShop2->getOrderProducts() as $orderProduct) {
  313. $this->addOrderProduct($orderShop1, $orderProduct);
  314. $this->em->remove($orderProduct);
  315. }
  316. $this->em->remove($orderShop2);
  317. $this->em->persist($orderShop1);
  318. $this->em->flush();
  319. return $orderShop1;
  320. }
  321. }
  322. public function createDocumentInvoice(OrderShopInterface $orderShop)
  323. {
  324. $merchantAddress = $orderShop->getMerchant()->getAddress() ;
  325. $buyerAddress = $orderShop->getBillingAddress() ;
  326. $document = $this->documentUtils->createDocument([
  327. 'type' => Document::TYPE_INVOICE,
  328. 'title' => '',
  329. 'status' => 1,
  330. 'order_shops' => [$orderShop],
  331. 'merchant_address' => $merchantAddress,
  332. 'buyer_address' => $buyerAddress,
  333. 'created_by' => $orderShop->getUser()
  334. ]) ;
  335. return $document ;
  336. }
  337. public function groupOrderProductsByProductFamily($orderProducts)
  338. {
  339. $orderProductsByProductFamily = [] ;
  340. foreach($orderProducts as $orderProduct) {
  341. if($orderProduct->getProduct() && $orderProduct->getProduct()->getProductFamily()) {
  342. $productFamily = $orderProduct->getProduct()->getProductFamily() ;
  343. if(!isset($orderProductsByProductFamily[$productFamily->getId()])) {
  344. $orderProductsByProductFamily[$productFamily->getId()] = [
  345. 'order_products' => [],
  346. 'total_quantity_weight' => 0,
  347. ] ;
  348. }
  349. $orderProductsByProductFamily[$productFamily->getId()]['order_products'][] = $orderProduct ;
  350. $orderProductsByProductFamily[$productFamily->getId()]['total_quantity_weight'] += ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient()) * $orderProduct->getQuantityOrder() ;
  351. }
  352. }
  353. return $orderProductsByProductFamily ;
  354. }
  355. public function createOrderPayment($orderShop, $type, $amount, $reference = null, $comment = null, $paidAt = null)
  356. {
  357. $classOrderPayment = $this->em->getClassMetadata(OrderPaymentInterface::class)->getName() ;
  358. $orderPayment = new $classOrderPayment ;
  359. $orderPayment->setOrderShop($orderShop) ;
  360. $orderPayment->setType($type) ;
  361. $orderPayment->setAmount($amount) ;
  362. $orderPayment->setReference($reference) ;
  363. $orderPayment->setComment($comment) ;
  364. $orderPayment->setPaidAt($paidAt) ;
  365. $this->em->persist($orderPayment) ;
  366. $this->em->flush() ;
  367. }
  368. }