Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

491 lines
23KB

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