<?php
/**
 * 2007-2023 PrestaShop
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to http://www.prestashop.com for more information.
 *
 * @author    PrestaShop SA <contact@prestashop.com>
 * @copyright 2007-2023 PrestaShop SA
 * @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 *  International Registered Trademark & Property of PrestaShop SA
 */
if (!defined('_PS_VERSION_')) {
    exit;
}

class Cart extends CartCore
{
    public function getPackageShippingCost($id_carrier = null, $use_tax = true, ?Country $default_country = null, $product_list = null, $id_zone = null, bool $keepOrderPrices = false)
    {
        parent::getPackageShippingCost(
            $id_carrier,
            $use_tax,
            $default_country,
            $product_list,
            $id_zone,
            $keepOrderPrices
        );

        if ($this->isVirtualCart()) {
            return 0;
        }

        if (null === $product_list) {
            $products = $this->getProducts(false, false, null, true, $keepOrderPrices);
        } else {
            foreach ($product_list as $key => $value) {
                if ($value['is_virtual'] == 1) {
                    unset($product_list[$key]);
                }
            }
            $products = $product_list;
        }

        if (Configuration::get('PS_TAX_ADDRESS_TYPE') == 'id_address_invoice') {
            $address_id = (int) $this->id_address_invoice;
        } else {
            $address_id = (int) $this->id_address_delivery;
        }

        if (!Address::addressExists($address_id, true)) {
            $address_id = null;
        }

        $AgileHelperClass = PrestaShop\Module\AgileMultisellerPro\Helper\AgileHelper::class;
        $entityManager = $AgileHelperClass::buildContainer()
            ->get('doctrine.orm.entity_manager');
        $sellerinfoShippingRepository = $entityManager->getRepository(
            PrestaShop\Module\AgileMultisellerPro\Entity\SellerinfoShipping::class
        );

        return $sellerinfoShippingRepository->getTotalShippingCost(
            $products,
            $this->id,
            $this->id_customer,
            $address_id,
            $use_tax
        );
    }

    public function getTotalShippingCost($delivery_option = null, $use_tax = true, ?Country $default_country = null)
    {
        parent::getTotalShippingCost($delivery_option, $use_tax, $default_country);

        $AgileHelperClass = PrestaShop\Module\AgileMultisellerPro\Helper\AgileHelper::class;
        $entityManager = $AgileHelperClass::buildContainer()
            ->get('doctrine.orm.entity_manager');
        $sellerinfoShippingRepository = $entityManager->getRepository(
            PrestaShop\Module\AgileMultisellerPro\Entity\SellerinfoShipping::class
        );

        return $sellerinfoShippingRepository->getTotalShippingCost(
            $this->getProducts(),
            $this->id,
            $this->id_customer,
            $this->id_address_delivery,
            $use_tax
        );
    }
}
