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.

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