|
- <?php
-
- namespace Lc\CaracoleBundle\Solver\Order;
-
- use Lc\CaracoleBundle\Model\Order\OrderProductInterface;
- use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
- use Lc\CaracoleBundle\Solver\Price\PriceSolver;
- use Lc\CaracoleBundle\Solver\Product\ProductFamilySolver;
- use Lc\CaracoleBundle\Solver\Product\ProductSolver;
-
- class OrderProductSolver
- {
- protected ProductSolver $productSolver;
- protected ProductFamilySolver $productFamilySolver;
- protected OrderProductReductionCatalogSolver $orderProductReductionCatalogSolver;
- protected PriceSolver $priceSolver;
-
- public function __construct(
- ProductSolver $productSolver,
- ProductFamilySolver $productFamilySolver,
- OrderProductReductionCatalogSolver $orderProductReductionCatalogSolver,
- PriceSolver $priceSolver
- ) {
- $this->productSolver = $productSolver;
- $this->productFamilySolver = $productFamilySolver;
- $this->orderProductReductionCatalogSolver = $orderProductReductionCatalogSolver;
- $this->priceSolver = $priceSolver;
- }
-
- public function compare(OrderProductInterface $orderProduct1, OrderProductInterface $orderProduct2)
- {
- return $orderProduct1->getProduct()->getId() == $orderProduct2->getProduct()->getId()
- && $orderProduct1->getRedelivery() == $orderProduct2->getRedelivery()
- && (string)$this->priceSolver->getPrice($orderProduct1)
- == (string)$this->priceSolver->getPrice($orderProduct2)
- && $this->orderProductReductionCatalogSolver->compare(
- $orderProduct1->getOrderProductReductionCatalog(),
- $orderProduct2->getOrderProductReductionCatalog()
- );
- }
-
- // groupOrderProductsByProductFamily
- public function getOrderProductsByProductFamily(array $orderProducts): array
- {
- $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;
- }
-
- // getOrderProductsFormOrderShopsGroupByStorageOrder
- public function getOrderProductsByOrderShopsGroupByStorageOrder(array $orderShops): array
- {
- $orderProductsByStorageOrder = [];
-
- foreach ($orderShops as $orderShop) {
- foreach ($orderShop->getOrderProducts() as $orderProduct) {
- if ($orderProduct->getProduct() && $orderProduct->getProduct()->getProductFamily()) {
- $productFamily = $orderProduct->getProduct()->getProductFamily();
- $storageOrder = 0;
-
- if ($productFamily->getStorageOrder()) {
- $storageOrder = $productFamily->getStorageOrder();
- }
-
- if (!isset($orderProductsByStorageOrder[$storageOrder])) {
- $orderProductsByStorageOrder[$productFamily->getId()] = [
- 'order_products' => [],
- 'total_quantity_weight' => 0,
- ];
- }
-
- $orderProductsByStorageOrder[$storageOrder]['order_products'][] = $orderProduct;
- $orderProductsByStorageOrder[$storageOrder]['total_quantity_weight'] += ($orderProduct->getQuantityProduct(
- ) / $orderProduct->getUnit()->getCoefficient()) * $orderProduct->getQuantityOrder();
- }
- }
- }
-
- return $orderProductsByStorageOrder;
- }
-
- public function getTitleOrderShop(OrderProductInterface $orderProduct)
- {
- $product = $orderProduct->getProduct();
- $productFamily = $product->getProductFamily();
-
- $titleProduct = $product->getTitle();
- $titleProductFamily = $productFamily->getTitle();
-
- if (strlen($titleProduct) > 0 && strlen($titleProductFamily) > 0) {
- $title = $titleProductFamily . ' - ' . $titleProduct;
- } else {
- $title = strlen($titleProduct) ? $titleProduct : $titleProductFamily;
- }
-
- if ($this->productFamilySolver->hasProductsWithVariousWeight($productFamily)) {
- $title .= ' - ' . $this->productSolver->getQuantityLabelInherited($product);
- }
-
- return $title;
- }
-
- public function getTitleSummaryAfterAddCart(OrderProductInterface $orderProduct)
- {
- $title = '';
-
- $product = $orderProduct->getProduct();
- $productFamily = $product->getProductFamily();
- $titleProduct = $this->productSolver->getTitleInherited($product);
- $titleProductFamily = $productFamily->getTitle();
-
- // multiple
- if ($productFamily->getActiveProducts() && $productFamily->getBehaviorAddToCart() == 'multiple') {
- $title .= $titleProduct;
- if ($this->productFamilySolver->hasProductsWithVariousWeight($productFamily)) {
- $title .= ' - ' . $this->productSolver->getQuantityLabelInherited($product);
- }
- }
-
- // simple
- if ($productFamily->getBehaviorAddToCart() == 'simple') {
-
- if($orderProduct->getOrderPackProduct()) {
- $title .= $this->productFamilySolver->getTitleProduct($product);
- }
- else {
- if ($productFamily->getActiveProducts()) {
- $title .= $titleProduct;
- if ($this->productFamilySolver->hasProductsWithVariousWeight($productFamily)) {
- $title .= ' - ' . $this->productSolver->getQuantityLabelInherited($product);
- }
- }
- }
- }
-
- return $title;
- }
- }
|