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.

442 lines
21KB

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