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