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 29KB

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