|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- <?php
-
- namespace Lc\CaracoleBundle\Solver\Order;
-
- 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\ReductionCreditInterface;
- use Lc\CaracoleBundle\Solver\Price\PriceSolver;
- use Lc\CaracoleBundle\Solver\Product\ProductSolver;
-
- class OrderShopSolver
- {
- protected PriceSolver $priceSolver;
- protected EntityManagerInterface $entityManager;
- protected ProductSolver $productSolver;
-
- public function __construct(PriceSolver $priceSolver, EntityManagerInterface $entityManager, ProductSolver $productSolver)
- {
- $this->priceSolver = $priceSolver;
- $this->entityManager = $entityManager;
- $this->productSolver = $productSolver;
- }
-
- 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
- ): int {
- $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(
- ) / $orderProduct->getProduct()->getUnitInherited()->getCoefficient());
- } else {
- $quantity += $orderProduct->getQuantityOrder();
- }
- }
- }
-
- return $quantity;
- }
-
- // isProductAvailable
- public function isProductAvailable(ProductInterface $product, $quantityOrder = 0, $checkCart = false, $orderShop = null)
- {
- if ($product->getStatus() != 1 || $product->getProductFamily()->getStatus() != 1 || !$this->productSolver->isProductSaleStatusOn($product)) {
- return false;
- }
-
- // @TODO : orderShop à définir où est appelé isAvailable
- if ($checkCart && !$orderShop) {
- throw new \Exception("Attention : définir le orderShop à l'endroit où est appelé isAvailable");
- }
-
- $productFamily = $product->getProductFamily();
- $quantityAsked = $quantityOrder;
-
- if ($productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
- if (!$quantityOrder) {
- $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product, true);
- } else {
- $quantityAsked = ($product->getQuantityInherited() / $product->getUnitInherited()->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 ($product->getAvailableQuantityInherited() >= $quantityAsked
- || $productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_UNLIMITED) {
- return true;
- } else {
- return false;
- }
- }
-
- public function isOneProductAvailableAddCart(OrderShopInterface $orderShop, $products): bool
- {
- foreach ($products as $product) {
- if ($this->isProductAvailable($product, 1, true, $orderShop)) {
- return true;
- }
- }
-
- return false;
- }
-
- public function getTotalOrderPayments(OrderShopInterface $orderShop, $mergeComplementaryOrderShop = false): float
- {
- $totalAmount = floatval(0);
-
- foreach ($orderShop->getOrderPayments() as $orderPayment) {
- $totalAmount = $orderPayment->getAmount() + $totalAmount;
- }
-
- if ($mergeComplementaryOrderShop) {
- foreach ($orderShop->getComplementaryOrderShops() as $complementaryOrderShop) {
- foreach ($complementaryOrderShop->getOrderPayments() as $orderPayment) {
- $totalAmount = $orderPayment->getAmount() + $totalAmount;
- }
- }
- }
-
- return $totalAmount;
- }
-
- public function getTotalRemainingToBePaid(OrderShopInterface $orderShop): float
- {
- return $this->priceSolver->getTotalWithTax($orderShop) - $this->getTotalOrderPayments($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
- ): OrderShopInterface {
- $this->entityManager->refresh($orderShop);
-
- if ($orderShop->getComplementaryOrderShops()) {
- foreach ($orderShop->getComplementaryOrderShops() as $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 isReductionCreditAddedToOrder(
- OrderShopInterface $orderShop,
- ReductionCreditInterface $reductionCredit
- ) {
- foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
- if ($orderReductionCredit->getReductionCredit() == $reductionCredit) {
- 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;
- }
-
- // isOrderShopPositiveAmount
- public function isPositiveAmount(OrderShopInterface $orderShop): bool
- {
- return $this->priceSolver->getTotalWithTax($orderShop) >= 0;
- }
-
- public function isPaid(OrderShopInterface $orderShop, $mergeComplementaryOrderShop = false): bool
- {
- $totalOrderPayments = $this->getTotalOrderPayments($orderShop, $mergeComplementaryOrderShop);
- $totalOrder = $this->priceSolver->getTotalWithTax($orderShop);
-
- if ((abs($totalOrderPayments - $totalOrder) < 0.00001
- || $totalOrderPayments >= $totalOrder)
- && $totalOrder > 0) {
- return true;
- } else {
- return false;
- }
- }
-
- // isOrderShopPositiveAmountRemainingToBePaid
- public function isPositiveAmountRemainingToBePaid(OrderShopInterface $orderShop): bool
- {
- return $this->getTotalRemainingToBePaid($orderShop) > 0;
- }
-
- public function isCartAllowToBeOrder(OrderShopInterface $orderShop): bool
- {
- return true;
- }
- }
|