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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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, $meanPayment, $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->setMeanPayment($meanPayment);
  330. $orderPayment->setAmount($amount);
  331. $orderPayment->setReference($reference);
  332. $orderPayment->setComment($comment);
  333. $orderPayment->setEditable(false);
  334. if ($paidAt) {
  335. $orderPayment->setPaidAt($paidAt);
  336. } else {
  337. $orderPayment->setPaidAt(new \DateTime('now'));
  338. }
  339. $this->em->persist($orderPayment);
  340. $this->em->flush();
  341. return $orderPayment ;
  342. }
  343. public function isOrderPaid($order)
  344. {
  345. if ($this->getTotalOrderPayments($order) >= $this->priceUtils->getTotalWithTax($order)) {
  346. return true;
  347. } else {
  348. return false;
  349. }
  350. }
  351. public function getTotalOrderPayments($order): float
  352. {
  353. $totalAmount = floatval(0);
  354. foreach ($order->getOrderPayments() as $orderPayment) {
  355. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  356. }
  357. return $totalAmount;
  358. }
  359. public function deductAvailabilityProduct(\Lc\ShopBundle\Model\OrderShop $orderShop)
  360. {
  361. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  362. switch ($orderProduct->getProduct()->getProductFamily()->getBehaviorCountStock()) {
  363. case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE :
  364. //Disponibilité par unité de référence
  365. $oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited();
  366. $newAvailability = $oldAvailability - ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient());
  367. $orderProduct->getProduct()->getProductFamily()->setAvailableQuantity($newAvailability);
  368. $this->em->persist($orderProduct->getProduct()->getProductFamily());
  369. break;
  370. case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY :
  371. $oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited();
  372. $newAvailability = $oldAvailability - $orderProduct->getQuantityOrder();
  373. $orderProduct->getProduct()->getProductFamily()->setAvailableQuantity($newAvailability);
  374. $this->em->persist($orderProduct->getProduct()->getProductFamily());
  375. break;
  376. case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT :
  377. $oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited();
  378. $newAvailability = $oldAvailability - $orderProduct->getQuantityOrder();
  379. $orderProduct->getProduct()->setAvailableQuantity($newAvailability);
  380. $this->em->persist($orderProduct->getProduct());
  381. break;
  382. }
  383. $this->em->flush();
  384. }
  385. }
  386. public function isCartAllowToBeOrder($order){
  387. return true;
  388. }
  389. public function getReductionCreditsAvailableByUser($user)
  390. {
  391. $reductionCredits = $this->reductionCreditRepo->findReductionCreditsByUser($user) ;
  392. $reductionCreditsArray = [] ;
  393. foreach($reductionCredits as $reductionCredit) {
  394. if(!$this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit, $user)) {
  395. $reductionCreditsArray[] = $reductionCredit ;
  396. }
  397. }
  398. return $reductionCreditsArray ;
  399. }
  400. public function isReductionCreditAddedToOrder($orderShop, $reductionCredit)
  401. {
  402. foreach($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  403. if($orderReductionCredit->getReductionCredit() == $reductionCredit) {
  404. return true ;
  405. }
  406. }
  407. return false ;
  408. }
  409. public function isProductAvailable(Product $product, $quantityOrder = 0, $checkCart = false, $orderShop = null)
  410. {
  411. if(!$orderShop) {
  412. $orderShop = $this->getCartCurrent() ;
  413. }
  414. $productFamily = $product->getProductFamily() ;
  415. $quantityAsked = $quantityOrder;
  416. if($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  417. if(!$quantityOrder) {
  418. $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product, true) ;
  419. }
  420. else {
  421. $quantityAsked = ($product->getQuantityInherited() / $product->getUnitInherited()->getCoefficient()) * $quantityOrder;
  422. }
  423. if($checkCart) {
  424. $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product, true) ;
  425. }
  426. }
  427. if(($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY
  428. || $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT)) {
  429. if(!$quantityOrder) {
  430. $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product) ;
  431. }
  432. if($checkCart) {
  433. $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product) ;
  434. }
  435. }
  436. if ($product->getAvailableQuantityInherited() >= $quantityAsked
  437. || $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_UNLIMITED) {
  438. return true;
  439. }
  440. else {
  441. return false;
  442. }
  443. }
  444. public function isOneProductAvailableAddCart($products): bool
  445. {
  446. $orderShop = $this->getCartCurrent() ;
  447. foreach($products as $product) {
  448. if($this->isProductAvailable($product, 1, true)) {
  449. return true ;
  450. }
  451. }
  452. return false ;
  453. }
  454. public function isOrderProductAvailable(OrderProductInterface $orderProduct)
  455. {
  456. return $this->isProductAvailable($orderProduct->getProduct(), $orderProduct->getQuantityOrder()) ;
  457. }
  458. public function isOrderProductAvailableAddCart(OrderProductInterface $orderProduct, $orderShop = null)
  459. {
  460. $product = $orderProduct->getProduct() ;
  461. return $this->isProductAvailable($product, $orderProduct->getQuantityOrder(), true, $orderShop);
  462. }
  463. public function getQuantityOrderByProduct($orderShop, $product, $byWeight = false)
  464. {
  465. $quantity = 0 ;
  466. $productFamily = $product->getProductFamily() ;
  467. $behaviorCountStock = $productFamily->getBehaviorCountStock() ;
  468. if($orderShop) {
  469. foreach($orderShop->getOrderProducts() as $orderProduct) {
  470. if($orderProduct->getProduct()->getId() == $product->getId()
  471. || ( ($behaviorCountStock == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY || $behaviorCountStock == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE)
  472. && $orderProduct->getProduct()->getProductFamily()->getId() == $productFamily->getId())) {
  473. if($byWeight) {
  474. $quantity += $orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct() / $product->getUnitInherited()->getCoefficient()) ;
  475. }
  476. else {
  477. $quantity += $orderProduct->getQuantityOrder() ;
  478. }
  479. }
  480. }
  481. }
  482. return $quantity ;
  483. }
  484. public function getProductQuantityMaxAddCart($product)
  485. {
  486. $orderShop = $this->getCartCurrent() ;
  487. $productFamily = $product->getProductFamily() ;
  488. $byWeight = false ;
  489. if($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  490. $byWeight = true ;
  491. }
  492. return $product->getAvailableQuantityInherited() - $this->getQuantityOrderByProduct($orderShop, $product, $byWeight) ;
  493. }
  494. }