1<?php
2/**
3 * Copyright since 2007 PrestaShop SA and Contributors
4 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5 *
6 * NOTICE OF LICENSE
7 *
8 * This source file is subject to the Open Software License (OSL 3.0)
9 * that is bundled with this package in the file LICENSE.md.
10 * It is also available through the world-wide-web at this URL:
11 * https://opensource.org/licenses/OSL-3.0
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@prestashop.com so we can send you a copy immediately.
15 *
16 * DISCLAIMER
17 *
18 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19 * versions in the future. If you wish to customize PrestaShop for your
20 * needs please refer to https://devdocs.prestashop.com/ for more information.
21 *
22 * @author    PrestaShop SA and Contributors <contact@prestashop.com>
23 * @copyright Since 2007 PrestaShop SA and Contributors
24 * @license   https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25 */
26use PrestaShop\PrestaShop\Adapter\Presenter\Object\ObjectPresenter;
27use PrestaShop\PrestaShop\Adapter\Product\PriceFormatter;
28use Symfony\Component\Translation\TranslatorInterface;
29
30class DeliveryOptionsFinderCore
31{
32    private $context;
33    private $objectPresenter;
34    private $translator;
35    private $priceFormatter;
36
37    public function __construct(
38        Context $context,
39        TranslatorInterface $translator,
40        ObjectPresenter $objectPresenter,
41        PriceFormatter $priceFormatter
42    ) {
43        $this->context = $context;
44        $this->objectPresenter = $objectPresenter;
45        $this->translator = $translator;
46        $this->priceFormatter = $priceFormatter;
47    }
48
49    private function isFreeShipping($cart, array $carrier)
50    {
51        $free_shipping = false;
52
53        if ($carrier['is_free']) {
54            $free_shipping = true;
55        } else {
56            foreach ($cart->getCartRules() as $rule) {
57                if ($rule['free_shipping'] && !$rule['carrier_restriction']) {
58                    $free_shipping = true;
59
60                    break;
61                }
62            }
63        }
64
65        return $free_shipping;
66    }
67
68    public function getSelectedDeliveryOption()
69    {
70        return current($this->context->cart->getDeliveryOption(null, false, false));
71    }
72
73    public function getDeliveryOptions()
74    {
75        $delivery_option_list = $this->context->cart->getDeliveryOptionList();
76        $include_taxes = !Product::getTaxCalculationMethod((int) $this->context->cart->id_customer) && (int) Configuration::get('PS_TAX');
77        $display_taxes_label = (Configuration::get('PS_TAX') && !Configuration::get('AEUC_LABEL_TAX_INC_EXC'));
78
79        $carriers_available = [];
80
81        if (isset($delivery_option_list[$this->context->cart->id_address_delivery])) {
82            foreach ($delivery_option_list[$this->context->cart->id_address_delivery] as $id_carriers_list => $carriers_list) {
83                foreach ($carriers_list as $carriers) {
84                    if (is_array($carriers)) {
85                        foreach ($carriers as $carrier) {
86                            $carrier = array_merge($carrier, $this->objectPresenter->present($carrier['instance']));
87                            $delay = $carrier['delay'][$this->context->language->id];
88                            unset($carrier['instance'], $carrier['delay']);
89                            $carrier['delay'] = $delay;
90                            if ($this->isFreeShipping($this->context->cart, $carriers_list)) {
91                                $carrier['price'] = $this->translator->trans(
92                                    'Free',
93                                    [],
94                                    'Shop.Theme.Checkout'
95                                );
96                            } else {
97                                if ($include_taxes) {
98                                    $carrier['price'] = $this->priceFormatter->format($carriers_list['total_price_with_tax']);
99                                    if ($display_taxes_label) {
100                                        $carrier['price'] = $this->translator->trans(
101                                            '%price% tax incl.',
102                                            ['%price%' => $carrier['price']],
103                                            'Shop.Theme.Checkout'
104                                        );
105                                    }
106                                } else {
107                                    $carrier['price'] = $this->priceFormatter->format($carriers_list['total_price_without_tax']);
108                                    if ($display_taxes_label) {
109                                        $carrier['price'] = $this->translator->trans(
110                                            '%price% tax excl.',
111                                            ['%price%' => $carrier['price']],
112                                            'Shop.Theme.Checkout'
113                                        );
114                                    }
115                                }
116                            }
117
118                            if (count($carriers) > 1) {
119                                $carrier['label'] = $carrier['price'];
120                            } else {
121                                $carrier['label'] = $carrier['name'] . ' - ' . $carrier['delay'] . ' - ' . $carrier['price'];
122                            }
123
124                            // If carrier related to a module, check for additionnal data to display
125                            $carrier['extraContent'] = '';
126                            if ($carrier['is_module']) {
127                                if ($moduleId = Module::getModuleIdByName($carrier['external_module_name'])) {
128                                    $carrier['extraContent'] = Hook::exec('displayCarrierExtraContent', ['carrier' => $carrier], $moduleId);
129                                }
130                            }
131
132                            $carriers_available[$id_carriers_list] = $carrier;
133                        }
134                    }
135                }
136            }
137        }
138
139        return $carriers_available;
140    }
141}
142