|
- <?php
-
- namespace Lc\CaracoleBundle\Repository\Order;
-
- use Lc\CaracoleBundle\Builder\File\DocumentBuilder;
- use Lc\CaracoleBundle\Factory\File\DocumentFactory;
- use Lc\CaracoleBundle\Model\File\DocumentModel;
- use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
- use Lc\CaracoleBundle\Resolver\Price\PriceResolver;
- use Lc\CaracoleBundle\Resolver\Reference\DocumentReferenceResolver;
- use Lc\SovBundle\Repository\AbstractStore;
-
- class OrderShopStore extends AbstractStore
- {
- protected OrderShopRepositoryQuery $query;
- protected PriceResolver $priceResolver;
- protected DocumentReferenceResolver $documentReferenceResolver;
- protected DocumentBuilder $documentBuilder;
-
- public function __construct(
- OrderShopRepositoryQuery $query,
- PriceResolver $priceResolver,
- DocumentReferenceResolver $documentReferenceResolver,
- DocumentBuilder $documentBuilder
- ) {
- $this->query = $query;
- $this->priceResolver = $priceResolver;
- $this->documentReferenceResolver = $documentReferenceResolver;
- $this->documentBuilder = $documentBuilder;
- }
-
- public function getDatas(OrderShopInterface $orderShop = null): array
- {
- $data = [];
-
- if (is_null($orderShop)) {
- $orderShop = $this->getCartCurrent();
- }
-
- $data['order'] = $orderShop;
-
- if ($orderShop) {
- $data['count'] = $orderShop->countQuantities();
- $data['total_with_tax'] = $this->priceResolver->getTotalWithTax($orderShop);
- $data['order_products_by_category'] = $orderShop->getOrderProductsByParentCategory();
- $data['total_remaining_to_be_paid'] = $this->getTotalRemainingToBePaid($orderShop);
- }
- return $data;
- }
-
- public function getAsJsonObject(OrderShopInterface $orderShop): array
- {
- $data['id'] = $orderShop->getId();
- $data['user'] = $orderShop->getUser()->getSummary();
- $data['orderStatus'] = $orderShop->getOrderStatus()->__toString();
- $data['deliveryAddress'] = $orderShop->getDeliveryAddress()->getSummary();
- $data['invoiceAddress'] = $orderShop->getInvoiceAddress()->getSummary();
- $data['total'] = $this->priceResolver->getTotal($orderShop);
- $data['totalWithTax'] = $this->priceResolver->getTotalWithTax($orderShop);
- $data['totalWithTaxAndReduction'] = $this->priceResolver->getTotalWithTax($orderShop);
- $i = 0;
-
- foreach ($orderShop->getOrderProductsByParentCategory() 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->priceResolver->getPrice($orderProduct);
- $data['orderProducts'][$i]['priceWithTax'] = $this->priceResolver->getPriceWithTax($orderProduct);
- $data['orderProducts'][$i]['priceWithTaxAndReduction'] = $this->priceResolver->getPriceWithTaxAndReduction(
- $orderProduct
- );
- $data['orderProducts'][$i]['quantity'] = $orderProduct->getQuantityOrder();
- $data['orderProducts'][$i]['totalWithTaxAndReduction'] = $this->priceResolver->getTotalOrderProductsWithTaxAndReduction(
- array($orderProduct)
- );
- $i++;
- }
- }
-
- return $data;
- }
-
- public function groupOrderProductsByProductFamily(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;
- }
-
- // isOrderShopPositiveAmount
- public function isPositiveAmount(OrderShopInterface $orderShop)
- {
- return $this->priceResolver->getTotalWithTax($orderShop) >= 0;
- }
-
- public function isPaid(OrderShopInterface $orderShop, $mergeComplementaryOrderShop = false)
- {
- $totalOrderPayments = $this->getTotalOrderPayments($orderShop, $mergeComplementaryOrderShop);
- $totalOrder = $this->priceResolver->getTotalWithTax($orderShop);
-
- if ((abs($totalOrderPayments - $totalOrder) < 0.00001
- || $totalOrderPayments >= $totalOrder)
- && $totalOrder > 0) {
- return true;
- } else {
- 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->priceResolver->getTotalWithTax($orderShop) - $this->getTotalOrderPayments($orderShop);
- }
-
- // isOrderShopPositiveAmountRemainingToBePaid
- public function isPositiveAmountRemainingToBePaid(OrderShopInterface $orderShop): bool
- {
- return $this->getTotalRemainingToBePaid($orderShop) > 0;
- }
-
- public function getCartByUserOrCreateIt($user)
- {
- $newOrderShop = $this->em->getRepository(OrderShopInterface::class)->findCartCurrent(['user' => $user]);
- if ($newOrderShop === null) {
- $newOrderShop = $this->createOrderShop(
- array(
- 'user' => $user,
- 'merchant' => $this->merchantUtils->getMerchantUser()
- )
- );
- }
-
- return $newOrderShop;
- }
-
- public function isCartAllowToBeOrder(OrderShopInterface $orderShop)
- {
- return true;
- }
-
- /*
- public function getCartCurrent(SectionInterface $section, UserInterface $user = null, VisitorInterface $visitor = null)
- {
- $paramsSearchOrderShop = [];
-
- $user = $this->security->getUser();
- $visitor = $this->userUtils->getVisitorCurrent();
-
- $orderShop = null;
- $orderShopUser = null;
- $orderShopVisitor = null;
-
- if ($user) {
- $orderShopUser = $this->orderShopRepo->findCartCurrent(
- [
- 'user' => $user
- ]
- );
- }
-
- if ($visitor) {
- $orderShopVisitor = $this->orderShopRepo->findCartCurrent(
- [
- 'visitor' => $visitor
- ]
- );
- }
-
- if ($orderShopUser || $orderShopVisitor) {
- // merge
- if ($orderShopUser && $orderShopVisitor && $orderShopUser != $orderShopVisitor
- && $orderShopVisitor->getOrderProducts() && count($orderShopVisitor->getOrderProducts())
- && $orderShopUser->getOrderStatus()->getAlias() == OrderStatus::ALIAS_CART) {
- $orderShop = $this->mergeOrderShops($orderShopUser, $orderShopVisitor);
- $this->utils->addFlash(
- 'success',
- "Votre panier visiteur vient d'être fusionné avec votre panier client."
- );
- } else {
- $orderShop = ($orderShopUser) ? $orderShopUser : $orderShopVisitor;
- }
- // set user
- if ($orderShop && $user && !$orderShop->getUser()) {
- $orderShop->setUser($user);
- $orderShop->setVisitor(null);
- $this->em->persist($orderShop);
- $this->em->flush();
- }
- }
-
- return $orderShop;
- }*/
- }
|