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.

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