Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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