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.

OrderUtils.php 28KB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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\DocumentUtils;
  25. use Lc\ShopBundle\Services\Price\OrderShopPriceUtils;
  26. use Lc\ShopBundle\Services\UserUtils;
  27. use Lc\ShopBundle\Services\Utils;
  28. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  29. use Symfony\Component\Security\Core\Security;
  30. class OrderUtils
  31. {
  32. use OrderUtilsReductionTrait;
  33. protected $em;
  34. protected $security;
  35. protected $userUtils;
  36. protected $merchantUtils;
  37. protected $orderShopRepo;
  38. protected $reductionCreditRepo ;
  39. protected $orderReductionCreditRepo ;
  40. protected $priceUtils;
  41. protected $productFamilyUtils;
  42. protected $documentUtils;
  43. protected $utils;
  44. public function __construct(EntityManagerInterface $em, Security $security, UserUtils $userUtils,
  45. MerchantUtilsInterface $merchantUtils, PriceUtilsInterface $priceUtils, ProductFamilyUtilsInterface $productFamilyUtils,
  46. DocumentUtils $documentUtils, Utils $utils)
  47. {
  48. $this->em = $em;
  49. $this->security = $security;
  50. $this->userUtils = $userUtils;
  51. $this->merchantUtils = $merchantUtils;
  52. $this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName());
  53. $this->reductionCreditRepo = $this->em->getRepository($this->em->getClassMetadata(ReductionCreditInterface::class)->getName());
  54. $this->orderReductionCreditRepo = $this->em->getRepository($this->em->getClassMetadata(OrderReductionCreditInterface::class)->getName());
  55. $this->priceUtils = $priceUtils;
  56. $this->productFamilyUtils = $productFamilyUtils;
  57. $this->documentUtils = $documentUtils;
  58. $this->utils = $utils;
  59. }
  60. public function getCartCurrent()
  61. {
  62. $paramsSearchOrderShop = [];
  63. $user = $this->security->getUser();
  64. $visitor = $this->userUtils->getVisitorCurrent();
  65. $orderShop = null;
  66. $orderShopUser = null;
  67. $orderShopVisitor = null;
  68. if ($user) {
  69. $orderShopUser = $this->orderShopRepo->findCartCurrent([
  70. 'user' => $user
  71. ]);
  72. }
  73. if ($visitor) {
  74. $orderShopVisitor = $this->orderShopRepo->findCartCurrent([
  75. 'visitor' => $visitor
  76. ]);
  77. }
  78. if ($orderShopUser || $orderShopVisitor) {
  79. // merge
  80. if ($orderShopUser && $orderShopVisitor && $orderShopUser != $orderShopVisitor
  81. && $orderShopVisitor->getOrderProducts() && count($orderShopVisitor->getOrderProducts())) {
  82. $orderShop = $this->mergeOrderShops($orderShopUser, $orderShopVisitor);
  83. $this->utils->addFlash('success', "Votre panier visiteur vient d'être fusionné avec votre panier client.");
  84. } else {
  85. $orderShop = ($orderShopUser) ? $orderShopUser : $orderShopVisitor;
  86. }
  87. // set user
  88. if ($orderShop && $user && !$orderShop->getUser()) {
  89. $orderShop->setUser($user);
  90. $this->em->persist($orderShop);
  91. $this->em->flush();
  92. }
  93. }
  94. return $orderShop;
  95. }
  96. public function createOrderShop($params)
  97. {
  98. $orderShop = new OrderShop();
  99. $orderShopBelongTo = false;
  100. if (isset($params['user']) && $params['user']) {
  101. $orderShopBelongTo = true;
  102. $orderShop->setUser($params['user']);
  103. }
  104. if (isset($params['visitor']) && $params['visitor']) {
  105. $orderShopBelongTo = true;
  106. $orderShop->setVisitor($params['visitor']);
  107. }
  108. if (!$orderShopBelongTo) {
  109. throw new \ErrorException('La commande doit être liée à un utilisateur ou à un visiteur.');
  110. }
  111. if (isset($params['merchant']) && $params['merchant']) {
  112. $orderShop->setMerchant($params['merchant']);
  113. } else {
  114. throw new \ErrorException('La commande doit être liée à un merchant.');
  115. }
  116. $orderShop = $this->changeOrderStatus('cart', $orderShop);
  117. return $orderShop;
  118. }
  119. public function addOrderProduct($orderShop, $orderProductAdd, $persist = true)
  120. {
  121. $return = false;
  122. $user = $this->security->getUser();
  123. $visitor = $this->userUtils->getVisitorCurrent();
  124. if (!$orderShop) {
  125. $orderShop = $this->createOrderShop([
  126. 'user' => $user,
  127. 'visitor' => $visitor,
  128. 'merchant' => $this->merchantUtils->getMerchantCurrent()
  129. ]);
  130. }
  131. if($this->isOrderProductAvailableAddCart($orderProductAdd, $orderShop)) {
  132. if ($orderProductAdd->getQuantityOrder() > 0) {
  133. $updated = false;
  134. $orderProductAdd->setTitle($orderProductAdd->getTitleOrderShop());
  135. $orderProductAdd->setPrice($this->priceUtils->getPrice($orderProductAdd->getProduct()));
  136. $orderProductAdd->setUnit($orderProductAdd->getProduct()->getUnitInherited());
  137. $orderProductAdd->setTaxRate($orderProductAdd->getProduct()->getTaxRateInherited());
  138. $orderProductAdd->setQuantityProduct($orderProductAdd->getProduct()->getQuantityInherited());
  139. $productFamily = $this->productFamilyUtils->getProductFamilyBySlug($orderProductAdd->getProduct()->getProductFamily()->getSlug());
  140. $reductionCatalog = $productFamily->getReductionCatalog();
  141. if ($reductionCatalog) {
  142. $orderProductReductionCatalog = new OrderProductReductionCatalog();
  143. $orderProductReductionCatalog->setTitle($reductionCatalog->getTitle());
  144. $orderProductReductionCatalog->setValue($reductionCatalog->getValue());
  145. $orderProductReductionCatalog->setUnit($reductionCatalog->getUnit());
  146. $orderProductReductionCatalog->setBehaviorTaxRate($reductionCatalog->getBehaviorTaxRate());
  147. $orderProductAdd->setOrderProductReductionCatalog($orderProductReductionCatalog);
  148. }
  149. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  150. if ($orderProduct->getProduct()->getId() == $orderProductAdd->getProduct()->getId()
  151. && (string)$this->priceUtils->getPrice($orderProduct) == (string)$this->priceUtils->getPrice($orderProductAdd)
  152. && $this->compareOrderProductReductionCatalog($orderProduct->getOrderProductReductionCatalog(), $orderProductAdd->getOrderProductReductionCatalog())) {
  153. $orderProduct->setQuantityOrder($orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder());
  154. if ($persist) {
  155. $this->em->persist($orderProduct);
  156. }
  157. $updated = true;
  158. $return = true;
  159. break;
  160. }
  161. }
  162. if (!$updated) {
  163. $orderShop->addOrderProduct($orderProductAdd);
  164. if (isset($orderProductReductionCatalog)) {
  165. $this->em->persist($orderProductReductionCatalog);
  166. if ($persist) {
  167. if (isset($orderProductReductionCatalog)) {
  168. $this->em->persist($orderProductReductionCatalog);
  169. }
  170. $this->em->persist($orderProductAdd);
  171. $this->em->persist($orderShop);
  172. }
  173. }
  174. $return = true;
  175. }
  176. if ($persist) {
  177. $this->em->flush();
  178. }
  179. }
  180. }
  181. else {
  182. $availableQuantity = $orderProductAdd->getProduct()->getAvailableQuantityInherited() ;
  183. $textError = "Le produit <strong>".$orderProductAdd->getTitleOrderShop()."</strong> n'est pas disponible" ;
  184. if($availableQuantity !== false && $availableQuantity > 0) {
  185. $unit = '' ;
  186. if($orderProductAdd->getProduct()->getProductFamily()->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  187. $unit = $orderProductAdd->getProduct()->getUnitInherited()->getUnit() ;
  188. }
  189. $textError .= ' dans cette quantité ' ;
  190. $textError .= '<br />'.$availableQuantity.$unit.' disponible(s) dont '.$this->getQuantityOrderByProduct($orderShop, $orderProductAdd->getProduct()).$unit.' déjà dans votre panier.' ;
  191. }
  192. $this->utils->addFlash('error', $textError);
  193. }
  194. return $return ;
  195. }
  196. public function countQuantities($orderShop)
  197. {
  198. return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts());
  199. }
  200. public function countQuantitiesByOrderProducts($orderProducts = [])
  201. {
  202. $count = 0;
  203. foreach ($orderProducts as $orderProduct) {
  204. $count += $orderProduct->getQuantityOrder();
  205. }
  206. return $count;
  207. }
  208. public function getOrderProductsByParentCategory($orderShop = null)
  209. {
  210. $categoriesArray = [];
  211. if ($orderShop) {
  212. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  213. $productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories();
  214. $category = $productCategories[0]->getParentCategory();
  215. $labelCategory = $category->getTitle();
  216. if (!isset($categoriesArray[$labelCategory])) {
  217. $categoriesArray[$labelCategory] = [];
  218. }
  219. $categoriesArray[$labelCategory][] = $orderProduct;
  220. }
  221. }
  222. return $categoriesArray;
  223. }
  224. public function getOrderDatas($order = null)
  225. {
  226. $data = [];
  227. if (!$order) {
  228. $order = $this->getCartCurrent();
  229. }
  230. $data['order'] = $order;
  231. if ($order) {
  232. $data['count'] = $this->countQuantities($order);
  233. $data['total_with_tax'] = $this->priceUtils->getTotalWithTax($order);
  234. $data['order_products_by_category'] = $this->getOrderProductsByParentCategory($order);
  235. }
  236. return $data;
  237. }
  238. public function getOrderAsJsonObject(OrderShopInterface $order)
  239. {
  240. $data['id'] = $order->getId();
  241. $data['user'] = $order->getUser()->getSummary();
  242. $data['orderStatus'] = $order->getOrderStatus()->__tosString();
  243. $data['deliveryAddress'] = $order->getDeliveryAddress()->getSummary();
  244. $data['invoiceAddress'] = $order->getInvoiceAddress()->getSummary();
  245. $data['total'] = $this->priceUtils->getTotal($order);
  246. $data['totalWithTax'] = $this->priceUtils->getTotalWithTax($order);
  247. $data['totalWithTaxAndReduction'] = $this->priceUtils->getTotalWithTax($order);
  248. $i = 0;
  249. foreach ($this->getOrderProductsByParentCategory($order) as $labelCategory => $orderProducts) {
  250. foreach ($orderProducts as $orderProduct) {
  251. $data['orderProducts'][$i]['id'] = $orderProduct->getId();
  252. $data['orderProducts'][$i]['product'] = $orderProduct->getProduct()->getId();
  253. $data['orderProducts'][$i]['quantityOrder'] = $orderProduct->getQuantityOrder();
  254. $data['orderProducts'][$i]['labelCategory'] = $labelCategory;
  255. $data['orderProducts'][$i]['title'] = $orderProduct->getTitle();
  256. $data['orderProducts'][$i]['price'] = $this->priceUtils->getPrice($orderProduct);
  257. $data['orderProducts'][$i]['priceWithTax'] = $this->priceUtils->getPriceWithTax($orderProduct);
  258. $data['orderProducts'][$i]['priceWithTaxAndReduction'] = $this->priceUtils->getPriceWithTaxAndReduction($orderProduct);
  259. $data['orderProducts'][$i]['quantity'] = $orderProduct->getQuantityOrder();
  260. $data['orderProducts'][$i]['totalWithTaxAndReduction'] = $this->priceUtils->getTotalOrderProductsWithTaxAndReduction(array($orderProduct));
  261. $i++;
  262. }
  263. }
  264. return $data;
  265. }
  266. public function newOrderStatusHistory($order, $status, $origin = 'user')
  267. {
  268. $orderStatusHistoryClass = $this->em->getClassMetadata(OrderStatusHistoryInterface::class);
  269. $orderStatusHistory = new $orderStatusHistoryClass->name;
  270. $orderStatusHistory->setOrderShop($order);
  271. $orderStatusHistory->setOrderStatus($status);
  272. $orderStatusHistory->setOrigin($origin);
  273. $this->em->persist($orderStatusHistory);
  274. }
  275. public function mergeOrderShops($orderShop1, $orderShop2)
  276. {
  277. if ($orderShop1 && $orderShop2) {
  278. foreach ($orderShop2->getOrderProducts() as $orderProduct) {
  279. $this->addOrderProduct($orderShop1, $orderProduct);
  280. $this->em->remove($orderProduct);
  281. }
  282. $this->em->remove($orderShop2);
  283. $this->em->persist($orderShop1);
  284. $this->em->flush();
  285. return $orderShop1;
  286. }
  287. }
  288. public function createDocumentInvoice(OrderShopInterface $orderShop)
  289. {
  290. $merchantAddress = $orderShop->getMerchant()->getAddress();
  291. $buyerAddress = $orderShop->getInvoiceAddress();
  292. $document = $this->documentUtils->createDocument([
  293. 'type' => Document::TYPE_INVOICE,
  294. 'title' => '',
  295. 'status' => 1,
  296. 'order_shops' => [$orderShop],
  297. 'merchant_address' => $merchantAddress,
  298. 'buyer_address' => $buyerAddress,
  299. 'created_by' => $orderShop->getUser()
  300. ]);
  301. return $document;
  302. }
  303. public function groupOrderProductsByProductFamily($orderProducts)
  304. {
  305. $orderProductsByProductFamily = [];
  306. foreach ($orderProducts as $orderProduct) {
  307. if ($orderProduct->getProduct() && $orderProduct->getProduct()->getProductFamily()) {
  308. $productFamily = $orderProduct->getProduct()->getProductFamily();
  309. if (!isset($orderProductsByProductFamily[$productFamily->getId()])) {
  310. $orderProductsByProductFamily[$productFamily->getId()] = [
  311. 'order_products' => [],
  312. 'total_quantity_weight' => 0,
  313. ];
  314. }
  315. $orderProductsByProductFamily[$productFamily->getId()]['order_products'][] = $orderProduct;
  316. $orderProductsByProductFamily[$productFamily->getId()]['total_quantity_weight'] += ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient()) * $orderProduct->getQuantityOrder();
  317. }
  318. }
  319. return $orderProductsByProductFamily;
  320. }
  321. public function createOrderPayment($orderShop, $type, $amount, $reference = null, $comment = null, $paidAt = null)
  322. {
  323. $classOrderPayment = $this->em->getClassMetadata(OrderPaymentInterface::class)->getName();
  324. $orderPayment = new $classOrderPayment;
  325. $orderPayment->setOrderShop($orderShop);
  326. $orderPayment->setType($type);
  327. $orderPayment->setAmount($amount);
  328. $orderPayment->setReference($reference);
  329. $orderPayment->setComment($comment);
  330. if ($paidAt) {
  331. $orderPayment->setPaidAt($paidAt);
  332. } else {
  333. $orderPayment->setPaidAt(new \DateTime('now'));
  334. }
  335. $this->em->persist($orderPayment);
  336. $this->em->flush();
  337. }
  338. public function isOrderPaid($order)
  339. {
  340. if ($this->getTotalOrderPayments($order) >= $this->priceUtils->getTotalWithTax($order)) {
  341. return true;
  342. } else {
  343. $this->utils->addFlash('error', 'error.order.noPayment');
  344. return false;
  345. }
  346. }
  347. public function getTotalOrderPayments($order): float
  348. {
  349. $totalAmount = floatval(0);
  350. foreach ($order->getOrderPayments() as $orderPayment) {
  351. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  352. }
  353. return $totalAmount;
  354. }
  355. public function deductAvailabilityProduct(\Lc\ShopBundle\Model\OrderShop $orderShop)
  356. {
  357. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  358. switch ($orderProduct->getProduct()->getProductFamily()->getBehaviorCountStock()) {
  359. case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE :
  360. //Disponibilité par unité de référence
  361. $oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited();
  362. $newAvailability = $oldAvailability - ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient());
  363. $orderProduct->getProduct()->getProductFamily()->setAvailableQuantity($newAvailability);
  364. $this->em->persist($orderProduct->getProduct()->getProductFamily());
  365. break;
  366. case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY :
  367. $oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited();
  368. $newAvailability = $oldAvailability - $orderProduct->getQuantityOrder();
  369. $orderProduct->getProduct()->getProductFamily()->setAvailableQuantity($newAvailability);
  370. $this->em->persist($orderProduct->getProduct()->getProductFamily());
  371. break;
  372. case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT :
  373. $oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited();
  374. $newAvailability = $oldAvailability - $orderProduct->getQuantityOrder();
  375. $orderProduct->getProduct()->setAvailableQuantity($newAvailability);
  376. $this->em->persist($orderProduct->getProduct());
  377. break;
  378. }
  379. $this->em->flush();
  380. }
  381. }
  382. public function isCartAllowToBeOrder($order){
  383. return true;
  384. }
  385. public function getReductionCreditsAvailableByUser($user)
  386. {
  387. $reductionCredits = $this->reductionCreditRepo->findReductionCreditsByUser($user) ;
  388. $reductionCreditsArray = [] ;
  389. foreach($reductionCredits as $reductionCredit) {
  390. if(!$this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit, $user)) {
  391. $reductionCreditsArray[] = $reductionCredit ;
  392. }
  393. }
  394. return $reductionCreditsArray ;
  395. }
  396. public function isReductionCreditAddedToOrder($orderShop, $reductionCredit)
  397. {
  398. foreach($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  399. if($orderReductionCredit->getReductionCredit() == $reductionCredit) {
  400. return true ;
  401. }
  402. }
  403. return false ;
  404. }
  405. public function isProductAvailable(Product $product, $quantityOrder = 0, $checkCart = false, $orderShop = null)
  406. {
  407. if(!$orderShop) {
  408. $orderShop = $this->getCartCurrent() ;
  409. }
  410. $productFamily = $product->getProductFamily() ;
  411. $quantityAsked = $quantityOrder;
  412. if($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  413. if(!$quantityOrder) {
  414. $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product, true) ;
  415. }
  416. else {
  417. $quantityAsked = ($product->getQuantityInherited() / $product->getUnitInherited()->getCoefficient()) * $quantityOrder;
  418. }
  419. if($checkCart) {
  420. $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product, true) ;
  421. }
  422. }
  423. if(($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY
  424. || $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT)) {
  425. if(!$quantityOrder) {
  426. $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product) ;
  427. }
  428. if($checkCart) {
  429. $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product) ;
  430. }
  431. }
  432. if ($product->getAvailableQuantityInherited() >= $quantityAsked
  433. || $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_UNLIMITED) {
  434. return true;
  435. }
  436. else {
  437. return false;
  438. }
  439. }
  440. public function isOneProductAvailableAddCart($products): bool
  441. {
  442. $orderShop = $this->getCartCurrent() ;
  443. foreach($products as $product) {
  444. if($this->isProductAvailable($product, 1, true)) {
  445. return true ;
  446. }
  447. }
  448. return false ;
  449. }
  450. public function isOrderProductAvailable(OrderProductInterface $orderProduct)
  451. {
  452. return $this->isProductAvailable($orderProduct->getProduct(), $orderProduct->getQuantityOrder()) ;
  453. }
  454. public function isOrderProductAvailableAddCart(OrderProductInterface $orderProduct, $orderShop = null)
  455. {
  456. $product = $orderProduct->getProduct() ;
  457. return $this->isProductAvailable($product, $orderProduct->getQuantityOrder(), true, $orderShop);
  458. }
  459. public function getQuantityOrderByProduct($orderShop, $product, $byWeight = false)
  460. {
  461. $quantity = 0 ;
  462. $productFamily = $product->getProductFamily() ;
  463. $behaviorCountStock = $productFamily->getBehaviorCountStock() ;
  464. if($orderShop) {
  465. foreach($orderShop->getOrderProducts() as $orderProduct) {
  466. if($orderProduct->getProduct()->getId() == $product->getId()
  467. || ( ($behaviorCountStock == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY || $behaviorCountStock == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE)
  468. && $orderProduct->getProduct()->getProductFamily()->getId() == $productFamily->getId())) {
  469. if($byWeight) {
  470. $quantity += $orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct() / $product->getUnitInherited()->getCoefficient()) ;
  471. }
  472. else {
  473. $quantity += $orderProduct->getQuantityOrder() ;
  474. }
  475. }
  476. }
  477. }
  478. return $quantity ;
  479. }
  480. public function getProductQuantityMaxAddCart($product)
  481. {
  482. $orderShop = $this->getCartCurrent() ;
  483. $productFamily = $product->getProductFamily() ;
  484. $byWeight = false ;
  485. if($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  486. $byWeight = true ;
  487. }
  488. return $product->getAvailableQuantityInherited() - $this->getQuantityOrderByProduct($orderShop, $product, $byWeight) ;
  489. }
  490. }