Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

479 rindas
22KB

  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. protected $session ;
  34. public function __construct(EntityManagerInterface $em, Security $security, UserUtils $userUtils,
  35. MerchantUtilsInterface $merchantUtils, PriceUtils $priceUtils, ProductFamilyUtilsInterface $productFamilyUtils,
  36. DocumentUtils $documentUtils, SessionInterface $session)
  37. {
  38. $this->em = $em;
  39. $this->security = $security;
  40. $this->userUtils = $userUtils;
  41. $this->merchantUtils = $merchantUtils;
  42. $this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName());
  43. $this->priceUtils = $priceUtils;
  44. $this->productFamilyUtils = $productFamilyUtils;
  45. $this->documentUtils = $documentUtils ;
  46. $this->session = $session ;
  47. }
  48. public function getCartCurrent()
  49. {
  50. $paramsSearchOrderShop = [];
  51. $user = $this->security->getUser();
  52. $visitor = $this->userUtils->getVisitorCurrent();
  53. $orderShop = null;
  54. $orderShopUser = null;
  55. $orderShopVisitor = null;
  56. if ($user) {
  57. $orderShopUser = $this->orderShopRepo->findCartCurrent([
  58. 'user' => $user
  59. ]);
  60. }
  61. if ($visitor) {
  62. $orderShopVisitor = $this->orderShopRepo->findCartCurrent([
  63. 'visitor' => $visitor
  64. ]);
  65. }
  66. if ($orderShopUser || $orderShopVisitor) {
  67. // merge
  68. if ($orderShopUser && $orderShopVisitor && $orderShopUser != $orderShopVisitor
  69. && $orderShopVisitor->getOrderProducts() && count($orderShopVisitor->getOrderProducts())) {
  70. $orderShop = $this->mergeOrderShops($orderShopUser, $orderShopVisitor);
  71. $this->session->getFlashBag()->add('success', "Votre panier visiteur vient d'être fusionné avec votre panier client.");
  72. }
  73. else {
  74. $orderShop = ($orderShopUser) ? $orderShopUser : $orderShopVisitor;
  75. }
  76. // set user
  77. if($orderShop && $user && !$orderShop->getUser()) {
  78. $orderShop->setUser($user) ;
  79. $this->em->persist($orderShop) ;
  80. $this->em->flush() ;
  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->changeOrderStatus('cart', $orderShop);
  106. return $orderShop;
  107. }
  108. public function addOrderProduct($orderShop, $orderProductAdd, $persist = true)
  109. {
  110. $return = false ;
  111. $user = $this->security->getUser() ;
  112. $visitor = $this->userUtils->getVisitorCurrent() ;
  113. if(!$orderShop) {
  114. $orderShop = $this->createOrderShop([
  115. 'user' => $user,
  116. 'visitor' => $visitor,
  117. 'merchant' => $this->merchantUtils->getMerchantCurrent()
  118. ]);
  119. }
  120. if ($orderProductAdd->getQuantityOrder() > 0) {
  121. $updated = false;
  122. $orderProductAdd->setTitle($orderProductAdd->getTitleOrderShop());
  123. $orderProductAdd->setPrice($this->priceUtils->getPrice($orderProductAdd->getProduct()));
  124. $orderProductAdd->setUnit($orderProductAdd->getProduct()->getUnitInherited());
  125. $orderProductAdd->setTaxRate($orderProductAdd->getProduct()->getTaxRateInherited());
  126. $orderProductAdd->setQuantityProduct($orderProductAdd->getProduct()->getQuantityInherited());
  127. $productFamily = $this->productFamilyUtils->getProductFamilyBySlug($orderProductAdd->getProduct()->getProductFamily()->getSlug());
  128. $reductionCatalog = $productFamily->getReductionCatalog();
  129. if ($reductionCatalog) {
  130. $orderProductReductionCatalog = new OrderProductReductionCatalog();
  131. $orderProductReductionCatalog->setTitle($reductionCatalog->getTitle());
  132. $orderProductReductionCatalog->setValue($reductionCatalog->getValue());
  133. $orderProductReductionCatalog->setUnit($reductionCatalog->getUnit());
  134. $orderProductReductionCatalog->setBehaviorTaxRate($reductionCatalog->getBehaviorTaxRate());
  135. $orderProductAdd->setOrderProductReductionCatalog($orderProductReductionCatalog);
  136. }
  137. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  138. if ($orderProduct->getProduct()->getId() == $orderProductAdd->getProduct()->getId()
  139. && (string)$this->priceUtils->getPrice($orderProduct) == (string)$this->priceUtils->getPrice($orderProductAdd)
  140. && $this->compareOrderProductReductionCatalog($orderProduct->getOrderProductReductionCatalog(), $orderProductAdd->getOrderProductReductionCatalog())) {
  141. $orderProduct->setQuantityOrder($orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder());
  142. if ($persist) {
  143. $this->em->persist($orderProduct);
  144. }
  145. $updated = true;
  146. $return = true ;
  147. break;
  148. }
  149. }
  150. if (!$updated) {
  151. $orderShop->addOrderProduct($orderProductAdd);
  152. if (isset($orderProductReductionCatalog)) {
  153. $this->em->persist($orderProductReductionCatalog);
  154. if ($persist) {
  155. if (isset($orderProductReductionCatalog)) {
  156. $this->em->persist($orderProductReductionCatalog);
  157. }
  158. $this->em->persist($orderProductAdd);
  159. $this->em->persist($orderShop);
  160. }
  161. }
  162. if ($persist) {
  163. $this->em->flush();
  164. }
  165. $return = true ;
  166. }
  167. }
  168. return $return ;
  169. }
  170. public function compareOrderProductReductionCatalog($orderProductReductionCatalog1, $orderProductReductionCatalog2)
  171. {
  172. return (!$orderProductReductionCatalog1 && !$orderProductReductionCatalog2)
  173. || ($orderProductReductionCatalog1
  174. && $orderProductReductionCatalog2
  175. && $orderProductReductionCatalog1->getUnit() == $orderProductReductionCatalog2->getUnit()
  176. && (string) $orderProductReductionCatalog1->getValue() == (string) $orderProductReductionCatalog2->getValue()
  177. && $orderProductReductionCatalog1->getBehaviorTaxRate() == $orderProductReductionCatalog2->getBehaviorTaxRate()) ;
  178. }
  179. public function countQuantities($orderShop)
  180. {
  181. return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts());
  182. }
  183. public function countQuantitiesByOrderProducts($orderProducts = [])
  184. {
  185. $count = 0;
  186. foreach ($orderProducts as $orderProduct) {
  187. $count += $orderProduct->getQuantityOrder();
  188. }
  189. return $count;
  190. }
  191. public function getOrderProductsByParentCategory($orderShop = null)
  192. {
  193. $categoriesArray = [];
  194. if($orderShop) {
  195. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  196. $productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories();
  197. $category = $productCategories[0]->getParentCategory();
  198. $labelCategory = $category->getTitle();
  199. if (!isset($categoriesArray[$labelCategory])) {
  200. $categoriesArray[$labelCategory] = [];
  201. }
  202. $categoriesArray[$labelCategory][] = $orderProduct;
  203. }
  204. }
  205. return $categoriesArray;
  206. }
  207. public function getOrderDatas($order = null)
  208. {
  209. $data = [];
  210. if (!$order) {
  211. $order = $this->getCartCurrent();
  212. }
  213. $data['order'] = $order;
  214. if ($order) {
  215. $data['count'] = $this->countQuantities($order);
  216. $data['total_with_tax'] = $this->priceUtils->getTotalWithTaxAndReduction($order);
  217. $data['order_products_by_category'] = $this->getOrderProductsByParentCategory($order);
  218. }
  219. return $data;
  220. }
  221. public function getOrderAsJsonObject(OrderShopInterface $order)
  222. {
  223. $data['id'] = $order->getId();
  224. $data['user'] = $order->getUser()->getSummary();
  225. $data['orderStatus'] = $order->getOrderStatus()->__tosString();
  226. $data['deliveryAddress'] = $order->getDeliveryAddress()->getSummary();
  227. $data['invoiceAddress'] = $order->getInvoiceAddress()->getSummary();
  228. $data['total'] = $this->priceUtils->getTotal($order);
  229. $data['totalWithTax'] = $this->priceUtils->getTotalWithTax($order);
  230. $data['totalWithTaxAndReduction'] = $this->priceUtils->getTotalWithTax($order);
  231. $i = 0;
  232. foreach ($this->getOrderProductsByParentCategory($order) as $labelCategory => $orderProducts) {
  233. foreach ($orderProducts as $orderProduct) {
  234. $data['orderProducts'][$i]['id'] = $orderProduct->getId();
  235. $data['orderProducts'][$i]['product'] = $orderProduct->getProduct()->getId();
  236. $data['orderProducts'][$i]['quantityOrder'] = $orderProduct->getQuantityOrder();
  237. $data['orderProducts'][$i]['labelCategory'] = $labelCategory;
  238. $data['orderProducts'][$i]['title'] = $orderProduct->getTitle();
  239. $data['orderProducts'][$i]['price'] = $this->priceUtils->getPrice($orderProduct);
  240. $data['orderProducts'][$i]['priceWithTax'] = $this->priceUtils->getPriceWithTax($orderProduct);
  241. $data['orderProducts'][$i]['priceWithTaxAndReduction'] = $this->priceUtils->getPriceWithTaxAndReduction($orderProduct);
  242. $data['orderProducts'][$i]['quantity'] = $orderProduct->getQuantityOrder();
  243. $data['orderProducts'][$i]['totalWithTaxAndReduction'] = $this->priceUtils->getTotalOrderProductsWithTaxAndReduction(array($orderProduct));
  244. $i++;
  245. }
  246. }
  247. return $data;
  248. }
  249. public function getSummaryOrderProductReductionCatalog($orderProductReductionCatalog)
  250. {
  251. $text = '';
  252. if ($orderProductReductionCatalog) {
  253. if ($orderProductReductionCatalog->getUnit() == 'amount') {
  254. $text .= '- ' . $orderProductReductionCatalog->getValue() . '&nbsp;€';
  255. }
  256. if ($orderProductReductionCatalog->getUnit() == 'percent') {
  257. $text .= '- ' . $orderProductReductionCatalog->getValue() . '&nbsp;%';
  258. }
  259. }
  260. return $text;
  261. }
  262. public function newOrderStatusHistory($order, $status, $origin = 'user')
  263. {
  264. $orderStatusHistoryClass = $this->em->getClassMetadata(OrderStatusHistoryInterface::class);
  265. $orderStatusHistory = new $orderStatusHistoryClass->name;
  266. $orderStatusHistory->setOrderShop($order);
  267. $orderStatusHistory->setOrderStatus($status);
  268. $orderStatusHistory->setOrigin($origin);
  269. $this->em->persist($orderStatusHistory);
  270. }
  271. public function createOrderReductionCart(OrderShopInterface $orderShop, ReductionCartInterface $reductionCart)
  272. {
  273. $orderReductionCartClass = $this->em->getClassMetadata(OrderReductionCartInterface::class);
  274. $orderReductionCart = new $orderReductionCartClass->name;
  275. $orderReductionCart->setOrderShop($orderShop);
  276. $orderReductionCart->setReductionCart($reductionCart);
  277. $orderReductionCart->setTitle($reductionCart->getTitle());
  278. $orderReductionCart->setValue($reductionCart->getValue());
  279. $orderReductionCart->setUnit($reductionCart->getUnit());
  280. $orderReductionCart->setBehaviorTaxRate($reductionCart->getBehaviorTaxRate());
  281. $orderReductionCart->setFreeShipping($reductionCart->getFreeShipping());
  282. $orderReductionCart->setAppliedTo($reductionCart->getAppliedTo());
  283. $orderReductionCart->setType($reductionCart->getType());
  284. $this->em->persist($orderReductionCart) ;
  285. $this->em->flush() ;
  286. return $orderReductionCart;
  287. }
  288. public function isReductionCreditAllowToBeAddToOrder($orderShop, $reductionCredit){
  289. if($this->orderShopRepo->findValidOrderWithReductionCredit($reductionCredit, $orderShop->getUser())>0){
  290. return false;
  291. }else{
  292. return true;
  293. }
  294. }
  295. public function createOrderReductionCredit(OrderShopInterface $orderShop, ReductionCreditInterface $reductionCredit)
  296. {
  297. $orderReductionCreditClass = $this->em->getClassMetadata(OrderReductionCreditInterface::class);
  298. $orderReductionCredit = new $orderReductionCreditClass->name;
  299. $orderReductionCredit->setOrderShop($orderShop);
  300. $orderReductionCredit->setReductionCredit($reductionCredit);
  301. $orderReductionCredit->setTitle($reductionCredit->getTitle());
  302. $orderReductionCredit->setValue($reductionCredit->getValue());
  303. $orderReductionCredit->setUnit($reductionCredit->getUnit());
  304. $orderReductionCredit->setBehaviorTaxRate($reductionCredit->getBehaviorTaxRate());
  305. return $orderReductionCredit;
  306. }
  307. /*public function getReductionCreditsAvailable($order)
  308. {
  309. $reductionCreditRepo = $this->em->getRepository(ReductionCreditInterface::class);
  310. $reductionCredits = $reductionCreditRepo->getReductionCreditByUser($order->getUser());
  311. foreach ($reductionCredits as $reductionCredit){
  312. }
  313. }*/
  314. public function mergeOrderShops($orderShop1, $orderShop2)
  315. {
  316. if ($orderShop1 && $orderShop2) {
  317. foreach ($orderShop2->getOrderProducts() as $orderProduct) {
  318. $this->addOrderProduct($orderShop1, $orderProduct);
  319. $this->em->remove($orderProduct);
  320. }
  321. $this->em->remove($orderShop2);
  322. $this->em->persist($orderShop1);
  323. $this->em->flush();
  324. return $orderShop1;
  325. }
  326. }
  327. public function createDocumentInvoice(OrderShopInterface $orderShop)
  328. {
  329. $merchantAddress = $orderShop->getMerchant()->getAddress() ;
  330. $buyerAddress = $orderShop->getInvoiceAddress() ;
  331. $document = $this->documentUtils->createDocument([
  332. 'type' => Document::TYPE_INVOICE,
  333. 'title' => '',
  334. 'status' => 1,
  335. 'order_shops' => [$orderShop],
  336. 'merchant_address' => $merchantAddress,
  337. 'buyer_address' => $buyerAddress,
  338. 'created_by' => $orderShop->getUser()
  339. ]) ;
  340. return $document ;
  341. }
  342. public function groupOrderProductsByProductFamily($orderProducts)
  343. {
  344. $orderProductsByProductFamily = [] ;
  345. foreach($orderProducts as $orderProduct) {
  346. if($orderProduct->getProduct() && $orderProduct->getProduct()->getProductFamily()) {
  347. $productFamily = $orderProduct->getProduct()->getProductFamily() ;
  348. if(!isset($orderProductsByProductFamily[$productFamily->getId()])) {
  349. $orderProductsByProductFamily[$productFamily->getId()] = [
  350. 'order_products' => [],
  351. 'total_quantity_weight' => 0,
  352. ] ;
  353. }
  354. $orderProductsByProductFamily[$productFamily->getId()]['order_products'][] = $orderProduct ;
  355. $orderProductsByProductFamily[$productFamily->getId()]['total_quantity_weight'] += ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient()) * $orderProduct->getQuantityOrder() ;
  356. }
  357. }
  358. return $orderProductsByProductFamily ;
  359. }
  360. public function createOrderPayment($orderShop, $type, $amount, $reference = null, $comment = null, $paidAt = null)
  361. {
  362. $classOrderPayment = $this->em->getClassMetadata(OrderPaymentInterface::class)->getName() ;
  363. $orderPayment = new $classOrderPayment ;
  364. $orderPayment->setOrderShop($orderShop) ;
  365. $orderPayment->setType($type) ;
  366. $orderPayment->setAmount($amount) ;
  367. $orderPayment->setReference($reference) ;
  368. $orderPayment->setComment($comment) ;
  369. if($paidAt) {
  370. $orderPayment->setPaidAt($paidAt) ;
  371. }
  372. else {
  373. $orderPayment->setPaidAt(new \DateTime('now')) ;
  374. }
  375. $this->em->persist($orderPayment) ;
  376. $this->em->flush() ;
  377. }
  378. public function isOrderPaid($orderShop){
  379. if($this->getTotalOrderPayments($orderShop) >= $this->priceUtils->getTotalWithTaxAndReduction($orderShop)){
  380. return true;
  381. }else{
  382. return false;
  383. }
  384. }
  385. public function getTotalOrderPayments($orderShop):float
  386. {
  387. $totalAmount = floatval(0);
  388. foreach ($orderShop->getOrderPayments() as $orderPayment){
  389. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  390. }
  391. return $totalAmount;
  392. }
  393. }