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.

pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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\OrderReductionCreditInterface;
  9. use Lc\ShopBundle\Context\OrderShopInterface;
  10. use Lc\ShopBundle\Context\OrderStatusHistoryInterface;
  11. use Lc\ShopBundle\Context\PriceUtilsInterface;
  12. use Lc\ShopBundle\Context\ProductFamilyUtilsInterface;
  13. use Lc\ShopBundle\Context\ReductionCreditInterface;
  14. use Lc\ShopBundle\Model\ProductFamily;
  15. use Lc\ShopBundle\Services\CreditUtils;
  16. use Lc\ShopBundle\Services\DocumentUtils;
  17. use Lc\ShopBundle\Services\UserUtils;
  18. use Lc\ShopBundle\Services\Utils;
  19. use Symfony\Component\Routing\RouterInterface;
  20. use Symfony\Component\Security\Core\Security;
  21. class OrderUtils
  22. {
  23. use OrderUtilsReductionTrait;
  24. use OrderUtilsStockTrait;
  25. use OrderUtilsPaymentTrait;
  26. use OrderUtilsDocumentTrait;
  27. use OrderUtilsCartTrait;
  28. protected $em;
  29. protected $security;
  30. protected $userUtils;
  31. protected $merchantUtils;
  32. protected $orderShopRepo;
  33. protected $reductionCreditRepo ;
  34. protected $orderReductionCreditRepo ;
  35. protected $documentRepo ;
  36. protected $priceUtils;
  37. protected $productFamilyUtils;
  38. protected $documentUtils;
  39. protected $utils;
  40. protected $creditUtils;
  41. protected $router;
  42. public function __construct(EntityManagerInterface $em, Security $security, RouterInterface $router, UserUtils $userUtils,
  43. MerchantUtilsInterface $merchantUtils, PriceUtilsInterface $priceUtils, ProductFamilyUtilsInterface $productFamilyUtils,
  44. DocumentUtils $documentUtils, Utils $utils, CreditUtils $creditUtils)
  45. {
  46. $this->em = $em;
  47. $this->security = $security;
  48. $this->userUtils = $userUtils;
  49. $this->merchantUtils = $merchantUtils;
  50. $this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName());
  51. $this->reductionCreditRepo = $this->em->getRepository($this->em->getClassMetadata(ReductionCreditInterface::class)->getName());
  52. $this->orderReductionCreditRepo = $this->em->getRepository($this->em->getClassMetadata(OrderReductionCreditInterface::class)->getName());
  53. $this->documentRepo = $this->em->getRepository($this->em->getClassMetadata(DocumentInterface::class)->getName());
  54. $this->priceUtils = $priceUtils;
  55. $this->productFamilyUtils = $productFamilyUtils;
  56. $this->documentUtils = $documentUtils;
  57. $this->utils = $utils;
  58. $this->creditUtils = $creditUtils;
  59. $this->router = $router;
  60. }
  61. public function createOrderShop($params)
  62. {
  63. //TODO vérifier que l'utilisateur n'a pas déjà une commande en cours
  64. $orderShop = new OrderShop();
  65. $orderShopBelongTo = false;
  66. if (isset($params['user']) && $params['user']) {
  67. $orderShopBelongTo = true;
  68. $orderShop->setUser($params['user']);
  69. }
  70. if (isset($params['visitor']) && $params['visitor']) {
  71. $orderShopBelongTo = true;
  72. $orderShop->setVisitor($params['visitor']);
  73. }
  74. if (!$orderShopBelongTo) {
  75. throw new \ErrorException('La commande doit être liée à un utilisateur ou à un visiteur.');
  76. }
  77. if (isset($params['merchant']) && $params['merchant']) {
  78. $orderShop->setMerchant($params['merchant']);
  79. } else {
  80. throw new \ErrorException('La commande doit être liée à un merchant.');
  81. }
  82. $orderShop = $this->changeOrderStatus('cart', $orderShop);
  83. return $orderShop;
  84. }
  85. public function addOrderProduct($orderShop, $orderProductAdd, $persist = true)
  86. {
  87. $return = false;
  88. $user = $this->security->getUser();
  89. $visitor = $this->userUtils->getVisitorCurrent();
  90. if (!$orderShop) {
  91. $orderShop = $this->createOrderShop([
  92. 'user' => $user,
  93. 'visitor' => $visitor,
  94. 'merchant' => $this->merchantUtils->getMerchantCurrent()
  95. ]);
  96. }
  97. if($this->isOrderProductAvailableAddCart($orderProductAdd, $orderShop)) {
  98. if ($orderProductAdd->getQuantityOrder() > 0) {
  99. $updated = false;
  100. $orderProductAdd->setTitle($orderProductAdd->getTitleOrderShop());
  101. $orderProductAdd->setPrice($this->priceUtils->getPrice($orderProductAdd->getProduct()));
  102. $orderProductAdd->setBuyingPrice($this->priceUtils->getBuyingPrice($orderProductAdd->getProduct()));
  103. $orderProductAdd->setUnit($orderProductAdd->getProduct()->getUnitInherited());
  104. $orderProductAdd->setTaxRate($orderProductAdd->getProduct()->getTaxRateInherited());
  105. $orderProductAdd->setQuantityProduct($orderProductAdd->getProduct()->getQuantityInherited());
  106. $productFamily = $this->productFamilyUtils->getProductFamilyBySlug($orderProductAdd->getProduct()->getProductFamily()->getSlug());
  107. $reductionCatalog = $productFamily->getReductionCatalog();
  108. if ($reductionCatalog) {
  109. $orderProductReductionCatalog = new OrderProductReductionCatalog();
  110. $orderProductReductionCatalog->setTitle($reductionCatalog->getTitle());
  111. $orderProductReductionCatalog->setValue($reductionCatalog->getValue());
  112. $orderProductReductionCatalog->setUnit($reductionCatalog->getUnit());
  113. $orderProductReductionCatalog->setBehaviorTaxRate($reductionCatalog->getBehaviorTaxRate());
  114. $orderProductAdd->setOrderProductReductionCatalog($orderProductReductionCatalog);
  115. }
  116. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  117. if ($orderProduct->getProduct()->getId() == $orderProductAdd->getProduct()->getId()
  118. && $orderProduct->getRedelivery() == $orderProductAdd->getRedelivery()
  119. && (string) $this->priceUtils->getPrice($orderProduct) == (string) $this->priceUtils->getPrice($orderProductAdd)
  120. && $this->compareOrderProductReductionCatalog($orderProduct->getOrderProductReductionCatalog(), $orderProductAdd->getOrderProductReductionCatalog())) {
  121. $orderProduct->setQuantityOrder($orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder());
  122. if ($persist) {
  123. $this->em->persist($orderProduct);
  124. }
  125. $updated = true;
  126. $return = true;
  127. break;
  128. }
  129. }
  130. if (!$updated) {
  131. $orderShop->addOrderProduct($orderProductAdd);
  132. if ($persist) {
  133. if (isset($orderProductReductionCatalog)) {
  134. $this->em->persist($orderProductReductionCatalog);
  135. }
  136. $this->em->persist($orderProductAdd);
  137. $this->em->persist($orderShop);
  138. }
  139. $return = true;
  140. }
  141. if ($persist) {
  142. $this->em->flush();
  143. }
  144. $this->eventOrderShopChangeQuantity($orderShop) ;
  145. }
  146. }
  147. else {
  148. $availableQuantity = $orderProductAdd->getProduct()->getAvailableQuantityInherited() ;
  149. $textError = "Le produit <strong>".$orderProductAdd->getTitleOrderShop()."</strong> n'est pas disponible" ;
  150. if($availableQuantity !== false && $availableQuantity > 0) {
  151. $unit = '' ;
  152. if($orderProductAdd->getProduct()->getProductFamily()->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  153. $unit = $orderProductAdd->getProduct()->getUnitInherited()->getUnit() ;
  154. }
  155. $textError .= ' dans cette quantité ' ;
  156. $textError .= '<br />'.$availableQuantity.$unit.' disponible(s) dont '.$this->getQuantityOrderByProduct($orderShop, $orderProductAdd->getProduct()).$unit.' déjà dans votre panier.' ;
  157. }
  158. $this->utils->addFlash('error', $textError);
  159. }
  160. return $return ;
  161. }
  162. public function countQuantities($orderShop)
  163. {
  164. return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts());
  165. }
  166. public function countQuantitiesByOrderProducts($orderProducts = [])
  167. {
  168. $count = 0;
  169. foreach ($orderProducts as $orderProduct) {
  170. $count += $orderProduct->getQuantityOrder();
  171. }
  172. return $count;
  173. }
  174. public function getOrderProductsByParentCategory($orderShop = null)
  175. {
  176. $categoriesArray = [];
  177. if ($orderShop) {
  178. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  179. $productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories();
  180. $category = $productCategories[0]->getParentCategory();
  181. $labelCategory = $category->getTitle();
  182. if (!isset($categoriesArray[$labelCategory])) {
  183. $categoriesArray[$labelCategory] = [];
  184. }
  185. $categoriesArray[$labelCategory][] = $orderProduct;
  186. }
  187. }
  188. return $categoriesArray;
  189. }
  190. public function getOrderDatas($order = null)
  191. {
  192. $data = [];
  193. if (!$order) {
  194. $order = $this->getCartCurrent();
  195. }
  196. $data['order'] = $order;
  197. if ($order) {
  198. $data['count'] = $this->countQuantities($order);
  199. $data['total_with_tax'] = $this->priceUtils->getTotalWithTax($order);
  200. $data['order_products_by_category'] = $this->getOrderProductsByParentCategory($order);
  201. }
  202. return $data;
  203. }
  204. public function getOrderAsJsonObject(OrderShopInterface $order)
  205. {
  206. $data['id'] = $order->getId();
  207. $data['user'] = $order->getUser()->getSummary();
  208. $data['orderStatus'] = $order->getOrderStatus()->__tosString();
  209. $data['deliveryAddress'] = $order->getDeliveryAddress()->getSummary();
  210. $data['invoiceAddress'] = $order->getInvoiceAddress()->getSummary();
  211. $data['total'] = $this->priceUtils->getTotal($order);
  212. $data['totalWithTax'] = $this->priceUtils->getTotalWithTax($order);
  213. $data['totalWithTaxAndReduction'] = $this->priceUtils->getTotalWithTax($order);
  214. $i = 0;
  215. foreach ($this->getOrderProductsByParentCategory($order) as $labelCategory => $orderProducts) {
  216. foreach ($orderProducts as $orderProduct) {
  217. $data['orderProducts'][$i]['id'] = $orderProduct->getId();
  218. $data['orderProducts'][$i]['product'] = $orderProduct->getProduct()->getId();
  219. $data['orderProducts'][$i]['quantityOrder'] = $orderProduct->getQuantityOrder();
  220. $data['orderProducts'][$i]['labelCategory'] = $labelCategory;
  221. $data['orderProducts'][$i]['title'] = $orderProduct->getTitle();
  222. $data['orderProducts'][$i]['price'] = $this->priceUtils->getPrice($orderProduct);
  223. $data['orderProducts'][$i]['priceWithTax'] = $this->priceUtils->getPriceWithTax($orderProduct);
  224. $data['orderProducts'][$i]['priceWithTaxAndReduction'] = $this->priceUtils->getPriceWithTaxAndReduction($orderProduct);
  225. $data['orderProducts'][$i]['quantity'] = $orderProduct->getQuantityOrder();
  226. $data['orderProducts'][$i]['totalWithTaxAndReduction'] = $this->priceUtils->getTotalOrderProductsWithTaxAndReduction(array($orderProduct));
  227. $i++;
  228. }
  229. }
  230. return $data;
  231. }
  232. public function newOrderStatusHistory($order, $status, $origin = 'user')
  233. {
  234. $orderStatusHistoryClass = $this->em->getClassMetadata(OrderStatusHistoryInterface::class);
  235. $orderStatusHistory = new $orderStatusHistoryClass->name;
  236. $orderStatusHistory->setOrderShop($order);
  237. $orderStatusHistory->setOrderStatus($status);
  238. $orderStatusHistory->setOrigin($origin);
  239. $this->em->persist($orderStatusHistory);
  240. }
  241. public function mergeOrderShops($orderShop1, $orderShop2, $persist = true)
  242. {
  243. if ($orderShop1 && $orderShop2) {
  244. foreach ($orderShop2->getOrderProducts() as $orderProduct) {
  245. $this->addOrderProduct($orderShop1, $orderProduct);
  246. if($persist) {
  247. $this->em->remove($orderProduct);
  248. }
  249. }
  250. if($persist) {
  251. $this->em->remove($orderShop2);
  252. $this->em->persist($orderShop1);
  253. $this->em->flush();
  254. }
  255. return $orderShop1;
  256. }
  257. }
  258. public function groupOrderProductsByProductFamily($orderProducts)
  259. {
  260. $orderProductsByProductFamily = [];
  261. foreach ($orderProducts as $orderProduct) {
  262. if ($orderProduct->getProduct() && $orderProduct->getProduct()->getProductFamily()) {
  263. $productFamily = $orderProduct->getProduct()->getProductFamily();
  264. if (!isset($orderProductsByProductFamily[$productFamily->getId()])) {
  265. $orderProductsByProductFamily[$productFamily->getId()] = [
  266. 'order_products' => [],
  267. 'total_quantity_weight' => 0,
  268. ];
  269. }
  270. $orderProductsByProductFamily[$productFamily->getId()]['order_products'][] = $orderProduct;
  271. $orderProductsByProductFamily[$productFamily->getId()]['total_quantity_weight'] += ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient()) * $orderProduct->getQuantityOrder();
  272. }
  273. }
  274. return $orderProductsByProductFamily;
  275. }
  276. public function isOrderShopPositiveAmount(OrderShopInterface $orderShop)
  277. {
  278. return $this->priceUtils->getTotalWithTax($orderShop) > 0 ;
  279. }
  280. public function eventOrderShopChangeQuantity(OrderShopInterface $orderShop)
  281. {
  282. }
  283. }