No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

OrderUtils.php 17KB

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