|
- <?php
-
- namespace Lc\CaracoleBundle\Solver\Order;
-
- use App\Entity\Order\OrderShop;
- use Doctrine\Common\Collections\ArrayCollection;
- use Doctrine\Common\Collections\Collection;
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\CaracoleBundle\Model\File\DocumentInterface;
- use Lc\CaracoleBundle\Model\File\DocumentModel;
- use Lc\CaracoleBundle\Model\Order\OrderProductInterface;
- use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
- use Lc\CaracoleBundle\Model\Order\OrderShopModel;
- use Lc\CaracoleBundle\Model\Order\OrderStatusInterface;
- use Lc\CaracoleBundle\Model\Order\OrderStatusModel;
- use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
- use Lc\CaracoleBundle\Model\Product\ProductFamilyModel;
- use Lc\CaracoleBundle\Model\Product\ProductInterface;
- use Lc\CaracoleBundle\Model\Reduction\ReductionCartInterface;
- use Lc\CaracoleBundle\Model\Reduction\ReductionCreditInterface;
- use Lc\CaracoleBundle\Model\Section\SectionInterface;
- use Lc\CaracoleBundle\Solver\Product\ProductFamilySectionPropertySolver;
- use Lc\CaracoleBundle\Solver\Product\ProductSolver;
-
- class OrderShopSolver
- {
- protected EntityManagerInterface $entityManager;
- protected ProductSolver $productSolver;
-
- public function __construct(
- EntityManagerInterface $entityManager,
- ProductSolver $productSolver
- ) {
- $this->entityManager = $entityManager;
- $this->productSolver = $productSolver;
- }
-
- public static function getTypeDeliveryChoices()
- {
- return [
- OrderShopModel::DELIVERY_TYPE_HOME,
- OrderShopModel::DELIVERY_TYPE_POINTSALE,
- ];
- }
-
- public function isEmpty(OrderShopInterface $orderShop): bool
- {
- return $orderShop->getOrderProducts()->isEmpty();
- }
-
- public function countQuantities(OrderShopInterface $orderShop): int
- {
- return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts());
- }
-
- public function countQuantitiesByOrderProducts($orderProducts = []): int
- {
- $count = 0;
-
- foreach ($orderProducts as $orderProduct) {
- $count += $orderProduct->getQuantityOrder();
- }
-
- return $count;
- }
-
- public function getOrderProductsByParentCategory(OrderShopInterface $orderShop): array
- {
- $categoriesArray = [];
-
- 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;
- }
-
- // getOrderProductsByProductFamily
- public function getOrderProductsByProductFamily(
- OrderShopInterface $orderShop,
- ProductFamilyInterface $productFamily
- ): array {
- $arrayOrderProducts = [];
-
- foreach ($orderShop->getOrderProducts() as $orderProduct) {
- if ($orderProduct->getProduct()->getProductFamily() == $productFamily) {
- $arrayOrderProducts[] = $orderProduct;
- }
- }
-
- return $arrayOrderProducts;
- }
-
- public function getQuantityOrderByProduct(
- OrderShopInterface $orderShop,
- ProductInterface $product,
- $byWeight = false
- ): float {
- $quantity = 0;
- $productFamily = $product->getProductFamily();
- $behaviorCountStock = $productFamily->getBehaviorCountStock();
-
- foreach ($orderShop->getOrderProducts() as $orderProduct) {
- if ($orderProduct->getProduct()->getId() == $product->getId()
- || (($behaviorCountStock == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY || $behaviorCountStock == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE)
- && $orderProduct->getProduct()->getProductFamily()->getId() == $productFamily->getId())) {
- if ($byWeight) {
- $quantity += $orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct(
- ) / $this->productSolver->getUnitInherited(
- $orderProduct->getProduct()
- )->getCoefficient());
- } else {
- $quantity += $orderProduct->getQuantityOrder();
- }
- }
- }
-
- return $quantity;
- }
-
- // isProductAvailable
- public function isProductAvailable(
- SectionInterface $section,
- ?OrderShopInterface $orderShop,
- ProductInterface $product,
- int $quantityOrder = 0,
- bool $checkCart = false
- ) {
- if ($product->getStatus() != 1 || $product->getProductFamily()->getStatus(
- ) != 1 || !$this->productSolver->isProductSaleStatusOn($product)) {
- return false;
- }
-
-
- $productFamily = $product->getProductFamily();
- $quantityAsked = $quantityOrder;
-
- if ($productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
- if (!$quantityOrder) {
- $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product, true);
- } else {
- $quantityAsked = ($this->productSolver->getQuantityInherited($product)
- / $this->productSolver->getUnitInherited($product)->getCoefficient()) * $quantityOrder;
- }
-
- if ($checkCart) {
- $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product, true);
- }
- }
-
- if (($productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY
- || $productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT)) {
- if (!$quantityOrder) {
- $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product);
- }
-
- if ($checkCart) {
- $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product);
- }
- }
-
- if ($this->productSolver->getAvailableQuantityInherited($product) >= $quantityAsked) {
- return true;
- } else {
- return false;
- }
- }
-
- public function isOneProductAvailable(SectionInterface $section, $productArray): bool
- {
- foreach($productArray as $product) {
- if($this->isProductAvailable($section, null, $product, 1, false)) {
- return true;
- }
- }
- return false;
- }
-
- public function isOneProductAvailableAddCart(OrderShopInterface $orderShop, $products): bool
- {
- foreach ($products as $product) {
- if ($this->isProductAvailable($orderShop->getSection(), $orderShop, $product, 1, true)) {
- return true;
- }
- }
- return false;
- }
-
- public function isOrderProductAvailableAddCart(OrderProductInterface $orderProduct, OrderShopInterface $orderShop)
- {
- return $this->isProductAvailable(
- $orderShop->getSection(),
- $orderShop,
- $orderProduct->getProduct(),
- 1,
- true
- );
- }
-
- public function getTotalOrderPayments(OrderShopInterface $orderShop, $mergeComplementaryOrderShop = false): float
- {
- $totalAmount = floatval(0);
-
- foreach ($orderShop->getOrderPayments() as $orderPayment) {
- $totalAmount = $orderPayment->getAmount() + $totalAmount;
- }
-
- if ($mergeComplementaryOrderShop) {
- foreach ($this->getValidComplementaryOrderShops($orderShop) as $complementaryOrderShop) {
- foreach ($complementaryOrderShop->getOrderPayments() as $orderPayment) {
- $totalAmount = $orderPayment->getAmount() + $totalAmount;
- }
- }
- }
-
- return $totalAmount;
- }
-
- public function getValidComplementaryOrderShops(OrderShopInterface $orderShop): Collection
- {
- $arrayComplementaryOrderShops = new ArrayCollection();
- foreach ($orderShop->getComplementaryOrderShops() as $complementaryOrderShop) {
- if ($this->isValid($complementaryOrderShop)) {
- $arrayComplementaryOrderShops[] = $complementaryOrderShop;
- }
- }
- return $arrayComplementaryOrderShops;
- }
-
- public function countValidComplementaryOrderShops(OrderShopInterface $orderShop): int
- {
- return count($this->getValidComplementaryOrderShops($orderShop));
- }
-
- public function getOrderStatusHistory(OrderShopInterface $orderShop, OrderStatusInterface $status)
- {
- $orderStatusHistories = $orderShop->getOrderStatusHistories();
-
- if (count($orderStatusHistories) > 0) {
- foreach ($orderStatusHistories as $orderStatusHistory) {
- if ($orderStatusHistory->getOrderStatus() === $status) {
- return $orderStatusHistory;
- }
- }
- }
-
- return null;
- }
-
- public function getDocumentInvoice(OrderShopInterface $orderShop): ?DocumentInterface
- {
- foreach ($orderShop->getDocuments() as $document) {
- if ($document->getType() == DocumentModel::TYPE_INVOICE) {
- return $document;
- }
- }
-
- return null;
- }
-
- public function isDeliveryHome(OrderShopInterface $orderShop): bool
- {
- return $orderShop->getDeliveryType() == OrderShopModel::DELIVERY_TYPE_HOME;
- }
-
- public function isDeliveryPointSale(OrderShopInterface $orderShop): bool
- {
- return $orderShop->getDeliveryType() == OrderShopModel::DELIVERY_TYPE_POINTSALE;
- }
-
- public function isComplementaryOrderShop(OrderShopInterface $orderShop): bool
- {
- return (bool)$orderShop->getMainOrderShop();
- }
-
- public function mergeComplentaryOrderShops(
- OrderShopInterface $orderShop,
- bool $combineProducts = true,
- bool $onlySameSection = false
- ): OrderShopInterface {
- $this->entityManager->refresh($orderShop);
-
- if ($this->getValidComplementaryOrderShops($orderShop)) {
- foreach ($this->getValidComplementaryOrderShops($orderShop) as $complementaryOrderShop) {
-
- if (!$onlySameSection || $complementaryOrderShop->getSection()->getId()
- == $orderShop->getSection()->getId()) {
-
- // @TODO : obligatoire sinon un seul orderProduct de présent
- $this->entityManager->refresh($complementaryOrderShop);
-
- foreach ($complementaryOrderShop->getOrderProducts() as $orderProductAdd) {
- $updated = false;
- foreach ($orderShop->getOrderProducts() as $orderProduct) {
- if ($combineProducts && $orderProduct->getProduct()->getId(
- ) == $orderProductAdd->getProduct()->getId()
- && (string)$orderProduct->getPrice() == (string)$orderProductAdd->getPrice()
- ) {
- $orderProduct->setUpdatedOnMergeComplementaryOrderShop(true);
- $orderProduct->setQuantityOrder(
- $orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder()
- );
-
- $updated = true;
- }
- }
-
- if (!$updated) {
- $orderProductAdd->setOnMergeComplementaryOrderShop($complementaryOrderShop);
- $orderProductAdd->setCreatedOnMergeComplementaryOrderShop(true);
- $orderShop->addOrderProduct($orderProductAdd);
- }
- }
- }
- }
- }
-
- return $orderShop;
- }
- public function refreshOrderShops(array $orderShops): array
- {
- foreach ($orderShops as $orderShop) {
- foreach ($orderShop->getOrderProducts() as $orderProduct) {
- $this->entityManager->refresh($orderProduct);
- }
- $this->entityManager->refresh($orderShop);
- }
- return $orderShops;
- }
-
- public function isReductionCreditAddedToOrder(
- OrderShopInterface $orderShop,
- ReductionCreditInterface $reductionCredit
- ) {
- foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
- if ($orderReductionCredit->getReductionCredit() == $reductionCredit) {
- return true;
- }
- }
-
- return false;
- }
-
- public function isReductionCartAddedToOrder(
- OrderShopInterface $orderShop,
- ReductionCartInterface $reductionCart
- ) {
- foreach ($orderShop->getOrderReductionCarts() as $orderReductionCart) {
- if ($orderReductionCart->getReductionCart() == $reductionCart) {
- return true;
- }
- }
-
- return false;
- }
-
- public function hasOrderProductAlreadyInCart(
- OrderShopInterface $orderShop,
- OrderProductInterface $orderProductTest
- ): ?OrderProductInterface {
- foreach ($orderShop->getOrderProducts() as $orderProduct) {
- if ($orderProduct->getProduct() == $orderProductTest->getProduct()) {
- return $orderProduct;
- }
- }
-
- return null;
- }
-
- public function isValid(OrderShopInterface $orderShop): bool
- {
- if ($orderShop->getOrderStatus() && in_array(
- $orderShop->getOrderStatus()->getAlias(),
- OrderStatusModel::$statusAliasAsValid
- ) > 0) {
- return true;
- }
-
- return false;
- }
-
- public function isCart(OrderShopInterface $orderShop): bool
- {
- if ($orderShop->getOrderStatus() && in_array(
- $orderShop->getOrderStatus()->getAlias(),
- OrderStatusModel::$statusAliasAsCart
- ) > 0) {
- return true;
- }
-
- return false;
- }
-
- public function isDone(OrderShopInterface $orderShop): bool
- {
- if ($orderShop->getOrderStatus() && $orderShop->getOrderStatus()->getAlias() == OrderStatusModel::ALIAS_DONE) {
- return true;
- }
- return false;
- }
-
- // getProductQuantityMaxAddCart
- public function getProductQuantityMaxAddCart(OrderShopInterface $orderShop, ProductInterface $product)
- {
- $productFamily = $product->getProductFamily();
-
- $byWeight = false;
- if ($productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
- $byWeight = true;
- }
-
- return max(
- $this->productSolver->getAvailableQuantityInherited($product) - $this->getQuantityOrderByProduct(
- $orderShop,
- $product,
- $byWeight
- ),
- 0
- );
- }
-
- public function hasMakeAChoiceAboutComplementaryOrder(OrderShop $orderShop): bool
- {
- return $orderShop->getMainOrderShop() || $orderShop->getDeclineComplementaryOrderShop();
- }
- }
|