|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- <?php
-
- namespace Lc\ShopBundle\Services\Order;
-
- use App\Entity\OrderProductReductionCatalog;
- use App\Entity\OrderShop;
- use App\Entity\OrderStatus;
- use Lc\ShopBundle\Context\DocumentInterface;
- use Lc\ShopBundle\Context\MerchantUtilsInterface;
- use Lc\ShopBundle\Context\OrderReductionCreditInterface;
- use Lc\ShopBundle\Context\OrderShopInterface;
- use Lc\ShopBundle\Context\OrderStatusHistoryInterface;
- use Lc\ShopBundle\Context\PriceUtilsInterface;
- use Lc\ShopBundle\Context\ProductFamilyUtilsInterface;
- use Lc\ShopBundle\Context\ReductionCreditInterface;
- use Lc\ShopBundle\Context\SectionInterface;
- use Lc\ShopBundle\Context\SectionUtilsInterface;
- use Lc\ShopBundle\Context\UserUtilsInterface;
- use Lc\ShopBundle\Manager\EntityManager;
- use Lc\ShopBundle\Model\ProductFamily;
- use Lc\ShopBundle\Services\CreditUtils;
- use Lc\ShopBundle\Services\DocumentUtils;
- use Lc\ShopBundle\Services\UserUtils;
- use Lc\ShopBundle\Services\Utils;
- use Lc\ShopBundle\Services\UtilsManager;
- use Symfony\Component\Routing\RouterInterface;
-
- use Symfony\Component\Security\Core\Security;
-
- class OrderUtils
- {
- use OrderUtilsReductionTrait;
- use OrderUtilsStockTrait;
- use OrderUtilsPaymentTrait;
- use OrderUtilsDocumentTrait;
- use OrderUtilsCartTrait;
-
- protected $em;
- protected $security;
- protected $userUtils;
- protected $merchantUtils;
- protected $orderShopRepo;
- protected $reductionCreditRepo ;
- protected $orderReductionCreditRepo ;
- protected $documentRepo ;
- protected $priceUtils;
- protected $productFamilyUtils;
- protected $documentUtils;
- protected $utils;
- protected $creditUtils;
- protected $router;
- protected $sectionUtils ;
-
- public function __construct(
- EntityManager $em,
- Security $security,
- RouterInterface $router,
- UserUtilsInterface $userUtils,
- MerchantUtilsInterface $merchantUtils,
- PriceUtilsInterface $priceUtils,
- ProductFamilyUtilsInterface $productFamilyUtils,
- DocumentUtils $documentUtils,
- Utils $utils,
- CreditUtils $creditUtils,
- SectionUtilsInterface $sectionUtils
- )
- {
- $this->em = $em;
- $this->security = $security;
- $this->userUtils = $userUtils;
- $this->merchantUtils = $merchantUtils;
- $this->orderShopRepo = $this->em->getRepository($this->em->getEntityName(OrderShopInterface::class));
- $this->reductionCreditRepo = $this->em->getRepository($this->em->getEntityName(ReductionCreditInterface::class));
- $this->orderReductionCreditRepo = $this->em->getRepository($this->em->getEntityName(OrderReductionCreditInterface::class));
- $this->documentRepo = $this->em->getRepository($this->em->getEntityName(DocumentInterface::class));
- $this->priceUtils = $priceUtils;
- $this->productFamilyUtils = $productFamilyUtils;
- $this->documentUtils = $documentUtils;
- $this->utils = $utils;
- $this->creditUtils = $creditUtils;
- $this->router = $router;
- $this->sectionUtils = $sectionUtils ;
- }
-
- public function createOrderShop($params)
- {
- //TODO vérifier que l'utilisateur n'a pas déjà une commande en cours
- $orderShop = new OrderShop();
-
- $orderShopBelongTo = false;
- if (isset($params['user']) && $params['user']) {
- $orderShopBelongTo = true;
- $orderShop->setUser($params['user']);
- }
- if (isset($params['visitor']) && $params['visitor'] && !$orderShop->getUser()) {
- $orderShopBelongTo = true;
- $orderShop->setVisitor($params['visitor']);
- }
-
- if (!$orderShopBelongTo) {
- throw new \ErrorException('La commande doit être liée à un utilisateur ou à un visiteur.');
- }
-
- if (isset($params['merchant']) && $params['merchant']) {
- $orderShop->setMerchant($params['merchant']);
- } else {
- throw new \ErrorException('La commande doit être liée à un merchant.');
- }
-
- // pour le moment, à la création, on lie simplement la commande à la section "Marché"
- $section = $this->sectionUtils->getSectionMarket() ;
- if($section) {
- $orderShop->setSection($section) ;
- }
- else {
- throw new \ErrorException('La commande doit être liée à une section.');
- }
-
- $orderShop = $this->changeOrderStatus('cart', $orderShop);
-
- return $orderShop;
- }
-
- public function addOrderProduct($orderShop, $orderProductAdd, $persist = true)
- {
- $return = false;
-
- if (!$orderShop) {
- $user = $this->security->getUser();
- $visitor = $this->userUtils->getVisitorCurrent();
-
- $orderShop = $this->createOrderShop([
- 'user' => $user,
- 'visitor' => $visitor,
- 'merchant' => $this->merchantUtils->getMerchantCurrent(),
- ]);
- }
-
- if($this->isOrderProductAvailableAddCart($orderProductAdd, $orderShop)) {
- if ($orderProductAdd->getQuantityOrder() > 0) {
- $updated = false;
-
- $orderProductAdd->setTitle($orderProductAdd->getTitleOrderShop());
- $orderProductAdd->setPrice($this->priceUtils->getPrice($orderProductAdd->getProduct()));
- $orderProductAdd->setBuyingPrice($this->priceUtils->getBuyingPrice($orderProductAdd->getProduct()));
- $orderProductAdd->setUnit($orderProductAdd->getProduct()->getUnitInherited());
- $orderProductAdd->setTaxRate($orderProductAdd->getProduct()->getTaxRateInherited());
- $orderProductAdd->setQuantityProduct($orderProductAdd->getProduct()->getQuantityInherited());
-
- $productFamily = $this->productFamilyUtils->getProductFamilyBySlug($orderProductAdd->getProduct()->getProductFamily()->getSlug());
- if($productFamily) {
- $reductionCatalog = $productFamily->getReductionCatalog();
- if ($reductionCatalog) {
- $orderProductReductionCatalog = new OrderProductReductionCatalog();
- $orderProductReductionCatalog->setTitle($reductionCatalog->getTitle());
- $orderProductReductionCatalog->setValue($reductionCatalog->getValue());
- $orderProductReductionCatalog->setUnit($reductionCatalog->getUnit());
- $orderProductReductionCatalog->setBehaviorTaxRate($reductionCatalog->getBehaviorTaxRate());
-
- $orderProductAdd->setOrderProductReductionCatalog($orderProductReductionCatalog);
- }
- }
-
- foreach ($orderShop->getOrderProducts() as $orderProduct) {
- if ($orderProduct->getProduct()->getId() == $orderProductAdd->getProduct()->getId()
- && $orderProduct->getRedelivery() == $orderProductAdd->getRedelivery()
- && (string) $this->priceUtils->getPrice($orderProduct) == (string) $this->priceUtils->getPrice($orderProductAdd)
- && $this->compareOrderProductReductionCatalog($orderProduct->getOrderProductReductionCatalog(), $orderProductAdd->getOrderProductReductionCatalog())) {
-
- $orderProduct->setQuantityOrder($orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder());
-
- if ($persist) {
- $this->em->persist($orderProduct);
- }
-
- $updated = true;
- $return = true;
-
- break;
- }
- }
-
- if (!$updated) {
- $orderShop->addOrderProduct($orderProductAdd);
-
- if ($persist) {
- if (isset($orderProductReductionCatalog)) {
- $this->em->persist($orderProductReductionCatalog);
- }
- $this->em->persist($orderProductAdd);
- $this->em->persist($orderShop);
- }
-
- $return = true;
- }
-
- if ($persist) {
- $this->em->flush();
- }
-
- $this->eventCartChange($orderShop) ;
- }
- }
- else {
- $availableQuantity = $orderProductAdd->getProduct()->getAvailableQuantityInherited() ;
- $textError = "Le produit <strong>".$orderProductAdd->getTitleOrderShop()."</strong> n'est pas disponible" ;
- if($availableQuantity !== false && $availableQuantity > 0) {
- $unit = '' ;
- if($orderProductAdd->getProduct()->getProductFamily()->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
- $unit = $orderProductAdd->getProduct()->getUnitInherited()->getUnitReference()->getUnit() ;
- }
- $textError .= ' dans cette quantité ' ;
-
- $user = $this->security->getUser() ;
- if($user && $user->hasRole('ROLE_USER')) {
- $textError .= '<br />'.$availableQuantity.$unit.' disponible(s) dont '.$this->getQuantityOrderByProduct($orderShop, $orderProductAdd->getProduct()).$unit.' déjà dans votre panier.' ;
- }
- }
- $this->utils->addFlash('error', $textError);
- }
-
- return $return ;
- }
-
- public function countQuantities($orderShop)
- {
- return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts());
- }
-
- public function countQuantitiesByOrderProducts($orderProducts = [])
- {
- $count = 0;
-
- foreach ($orderProducts as $orderProduct) {
- $count += $orderProduct->getQuantityOrder();
- }
-
- return $count;
- }
-
- public function getOrderProductsByParentCategory($orderShop = null)
- {
- $categoriesArray = [];
- if ($orderShop) {
- foreach ($orderShop->getOrderProducts() as $orderProduct) {
- $productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories();
- $category = $productCategories[0]->getParentCategory();
- $labelCategory = $category->getTitle();
- if (!isset($categoriesArray[$labelCategory])) {
- $categoriesArray[$labelCategory] = [];
- }
- $categoriesArray[$labelCategory][] = $orderProduct;
- }
- }
-
- return $categoriesArray;
- }
-
- public function getOrderDatas($order = null)
- {
- $data = [];
- if (!$order) {
- $order = $this->getCartCurrent();
- }
- $data['order'] = $order;
-
- if ($order) {
- $data['count'] = $this->countQuantities($order);
- $data['total_with_tax'] = $this->priceUtils->getTotalWithTax($order);
- $data['order_products_by_category'] = $this->getOrderProductsByParentCategory($order);
- $data['total_remaining_to_be_paid'] = $this->getTotalRemainingToBePaid($order) ;
- }
- return $data;
- }
-
-
- public function getOrderAsJsonObject(OrderShopInterface $order)
- {
- $data['id'] = $order->getId();
- $data['user'] = $order->getUser()->getSummary();
- $data['orderStatus'] = $order->getOrderStatus()->__toString();
- $data['deliveryAddress'] = $order->getDeliveryAddress()->getSummary();
- $data['invoiceAddress'] = $order->getInvoiceAddress()->getSummary();
- $data['total'] = $this->priceUtils->getTotal($order);
- $data['totalWithTax'] = $this->priceUtils->getTotalWithTax($order);
- $data['totalWithTaxAndReduction'] = $this->priceUtils->getTotalWithTax($order);
- $i = 0;
- foreach ($this->getOrderProductsByParentCategory($order) as $labelCategory => $orderProducts) {
-
- foreach ($orderProducts as $orderProduct) {
- $data['orderProducts'][$i]['id'] = $orderProduct->getId();
- $data['orderProducts'][$i]['product'] = $orderProduct->getProduct()->getId();
- $data['orderProducts'][$i]['quantityOrder'] = $orderProduct->getQuantityOrder();
- $data['orderProducts'][$i]['labelCategory'] = $labelCategory;
- $data['orderProducts'][$i]['title'] = $orderProduct->getTitle();
- $data['orderProducts'][$i]['price'] = $this->priceUtils->getPrice($orderProduct);
- $data['orderProducts'][$i]['priceWithTax'] = $this->priceUtils->getPriceWithTax($orderProduct);
- $data['orderProducts'][$i]['priceWithTaxAndReduction'] = $this->priceUtils->getPriceWithTaxAndReduction($orderProduct);
- $data['orderProducts'][$i]['quantity'] = $orderProduct->getQuantityOrder();
- $data['orderProducts'][$i]['totalWithTaxAndReduction'] = $this->priceUtils->getTotalOrderProductsWithTaxAndReduction(array($orderProduct));
- $i++;
- }
- }
-
- return $data;
- }
-
-
- public function newOrderStatusHistory($order, $status, $origin = 'user')
- {
- $orderStatusHistoryClass = $this->em->getEntityName(OrderStatusHistoryInterface::class);
- $orderStatusHistory = new $orderStatusHistoryClass;
- $orderStatusHistory->setOrderShop($order);
- $orderStatusHistory->setOrderStatus($status);
- $orderStatusHistory->setOrigin($origin);
- $this->em->persist($orderStatusHistory);
- }
-
-
- public function mergeOrderShops($orderShop1, $orderShop2, $persist = true)
- {
- if ($orderShop1 && $orderShop2) {
-
- foreach ($orderShop2->getOrderProducts() as $orderProduct) {
-
- $orderProductAlreadyInCart = $this->hasOrderProductAlreadyInCart($orderShop1, $orderProduct) ;
- if($orderProductAlreadyInCart) {
- if($orderProduct->getQuantityOrder() > $orderProductAlreadyInCart->getQuantityOrder()) {
- $orderShop1->removeOrderProduct($orderProductAlreadyInCart) ;
- $this->addOrderProduct($orderShop1, $orderProduct);
- }
- }
- else {
- $this->addOrderProduct($orderShop1, $orderProduct);
- }
-
- if($persist) {
- $this->em->remove($orderProduct);
- }
- }
-
- if($persist) {
- $this->em->remove($orderShop2);
- $this->em->persist($orderShop1);
- $this->em->flush();
- }
-
- return $orderShop1;
- }
- }
-
-
- public function hasOrderProductAlreadyInCart($orderShop, $orderProductTest)
- {
- foreach($orderShop->getOrderProducts() as $orderProduct) {
- if($orderProduct->getProduct() == $orderProductTest->getProduct()) {
- return $orderProduct ;
- }
- }
-
- return false ;
- }
-
- public function getOrderProductsByProductFamilyInCart($productFamily)
- {
- $arrayOrderProducts = [] ;
- $orderShop = $this->getCartCurrent() ;
-
- if($orderShop) {
- foreach($orderShop->getOrderProducts() as $orderProduct) {
- if($orderProduct->getProduct()->getProductFamily() == $productFamily) {
- $arrayOrderProducts[] = $orderProduct ;
- }
- }
- }
-
- return $arrayOrderProducts ;
- }
-
- public function groupOrderProductsByProductFamily($orderProducts)
- {
- $orderProductsByProductFamily = [];
- foreach ($orderProducts as $orderProduct) {
- if ($orderProduct->getProduct() && $orderProduct->getProduct()->getProductFamily()) {
- $productFamily = $orderProduct->getProduct()->getProductFamily();
- if (!isset($orderProductsByProductFamily[$productFamily->getId()])) {
- $orderProductsByProductFamily[$productFamily->getId()] = [
- 'order_products' => [],
- 'total_quantity_weight' => 0,
- ];
- }
- $orderProductsByProductFamily[$productFamily->getId()]['order_products'][] = $orderProduct;
- $orderProductsByProductFamily[$productFamily->getId()]['total_quantity_weight'] += ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient()) * $orderProduct->getQuantityOrder();
- }
- }
-
- return $orderProductsByProductFamily;
- }
-
- public function isOrderShopPositiveAmount(OrderShopInterface $orderShop)
- {
- return $this->priceUtils->getTotalWithTax($orderShop) >= 0 ;
- }
-
- public function eventCartChange(OrderShopInterface $orderShop)
- {
-
- }
-
- public function addPayment($orderShop, $meanPayment, $amount)
- {
- $this->createOrderPayment([
- 'orderShop' => $orderShop,
- 'meanPayment' => $meanPayment,
- 'amount' => $amount
- ]) ;
- $this->em->refresh($orderShop) ;
-
- if($this->isOrderPaid($orderShop)) {
- $nextStatus = OrderStatus::ALIAS_PAID ;
- }
- else {
- $nextStatus = OrderStatus::ALIAS_PARTIAL_PAYMENT ;
- }
-
- if($orderShop->getOrderStatus()->getAlias() != $nextStatus) {
- $orderShop = $this->changeOrderStatus(
- $nextStatus,
- $orderShop
- );
- }
-
- return $orderShop ;
- }
-
- }
|