Projekt

Allgemein

Profil

GX-Bug #67456 » ot_shipping.php

includes/classes/order_total - Till Tepelmann, 18.08.2020 13:10

 
1
<?php
2
/* --------------------------------------------------------------
3
   ot_shipping.php 2020-08-13
4
   Gambio GmbH
5
   http://www.gambio.de
6
   Copyright (c) 2019 Gambio GmbH
7
   Released under the GNU General Public License (Version 2)
8
   [http://www.gnu.org/licenses/gpl-2.0.html]
9
   --------------------------------------------------------------
10

    
11

    
12
   based on: 
13
   (c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
14
   (c) 2002-2003 osCommerce(ot_shipping.php,v 1.15 2003/02/07); www.oscommerce.com 
15
   (c) 2003	 nextcommerce (ot_shipping.php,v 1.13 2003/08/24); www.nextcommerce.org
16
   (c) 2003 XT-Commerce - community made shopping http://www.xt-commerce.com ($Id: ot_shipping.php 1002 2005-07-10 16:11:37Z mz $)
17

    
18
   Released under the GNU General Public License 
19
   ---------------------------------------------------------------------------------------*/
20

    
21
class ot_shipping_ORIGIN
22
{
23
    public $code;
24
    public $title;
25
    public $description;
26
    public $enabled;
27
    public $sort_order;
28
    public $output;
29
    
30
    
31
    public function __construct()
32
    {
33
        $this->code        = 'ot_shipping';
34
        $this->title       = defined('MODULE_ORDER_TOTAL_SHIPPING_TITLE') ? MODULE_ORDER_TOTAL_SHIPPING_TITLE : '';
35
        $this->description = defined(
36
            'MODULE_ORDER_TOTAL_SHIPPING_DESCRIPTION'
37
        ) ? MODULE_ORDER_TOTAL_SHIPPING_DESCRIPTION : '';
38
        $this->enabled     = defined('MODULE_ORDER_TOTAL_SHIPPING_STATUS')
39
                             && MODULE_ORDER_TOTAL_SHIPPING_STATUS === 'true';
40
        $this->sort_order  = defined(
41
            'MODULE_ORDER_TOTAL_SHIPPING_SORT_ORDER'
42
        ) ? MODULE_ORDER_TOTAL_SHIPPING_SORT_ORDER : '0';
43
        
44
        $this->output = [];
45
    }
46
    
47
    
48
    public function process()
49
    {
50
        /** @var \order_ORIGIN $order */
51
        $order   = $GLOBALS['order'];
52
        /** @var \xtcPrice_ORIGIN $xtPrice */
53
        $xtPrice = $GLOBALS['xtPrice'];
54
        
55
        if (MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING === 'true'
56
            && $order->info['shipping_class'] !== 'selfpickup_selfpickup') {
57
            $pass = false;
58
            switch (MODULE_ORDER_TOTAL_SHIPPING_DESTINATION) {
59
                case 'national':
60
                    if ((int)$order->delivery['country_id'] === (int)STORE_COUNTRY) {
61
                        $pass = true;
62
                    }
63
                    break;
64
                case 'international':
65
                    if ((int)$order->delivery['country_id'] !== (int)STORE_COUNTRY) {
66
                        $pass = true;
67
                    }
68
                    break;
69
                case 'both':
70
                    $pass = true;
71
                    break;
72
                default:
73
                    $pass = false;
74
                    break;
75
            }
76
            
77
            $t_shipping_free_over = (double)MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING_OVER;
78
            if ((int)$_SESSION['customers_status']['customers_status_show_price_tax'] === 0
79
                && (int)MODULE_ORDER_TOTAL_SHIPPING_TAX_CLASS > 0) {
80
                $t_shipping_free_over /= (1 + $xtPrice->TAX[MODULE_ORDER_TOTAL_SHIPPING_TAX_CLASS] / 100);
81
            }
82
            
83
            if (($pass === true)
84
                && (($order->info['total'] - $order->info['shipping_cost']) >= $xtPrice->xtcFormat(
85
                        $t_shipping_free_over,
86
                        false,
87
                        0,
88
                        true
89
                    ))) {
90
                $order->info['shipping_method'] = $this->title;
91
                $order->info['total']           -= $order->info['shipping_cost'];
92
                $order->info['shipping_cost']   = 0;
93
            }
94
        }
95
        
96
        $module = substr($_SESSION['shipping']['id'], 0, strpos($_SESSION['shipping']['id'], '_'));
97
        
98
        // BOF GM_MOD
99
        if (!isset($GLOBALS[$module])
100
            && file_exists(
101
                DIR_FS_CATALOG . 'includes/modules/shipping/' . basename($module) . '.php'
102
            )) {
103
            include_once DIR_FS_CATALOG . 'includes/modules/shipping/' . basename($module) . '.php';
104
            $GLOBALS[$module] = new $module;
105
        }
106
        // EOF GM_MOD
107
        if (xtc_not_null($order->info['shipping_method']) && $_SESSION['cart']->get_content_type() !== 'virtual') {
108
            $deliveryCountryId = $order->delivery['country']['id'];
109
            $deliveryZoneId = $order->delivery['zone_id'];
110
            
111
            $calledFromCart = strpos(gm_get_env_info('SCRIPT_NAME'), 'shopping_cart.php') !== false;
112
            if ($calledFromCart && isset($_SESSION['cart_shipping_country'])) {
113
                $deliveryCountryId = $_SESSION['cart_shipping_country'];
114
                $deliveryZoneId = 0;
115
            }
116
    
117
            $shippingTaxRate        = xtc_get_tax_rate($GLOBALS[$module]->tax_class, $deliveryCountryId,
118
                $deliveryZoneId);
119
            $shippingTaxDescription = xtc_get_tax_description($GLOBALS[$module]->tax_class, $deliveryCountryId,
120
                $deliveryZoneId);
121
    
122
            if ((int)$_SESSION['customers_status']['customers_status_show_price_tax'] === 1) {
123
                $shippingCostInclTax = xtc_add_tax($order->info['shipping_cost'], $shippingTaxRate);
124
                $tax                 = $xtPrice->xtcCalculateCurr($shippingCostInclTax)
125
                                       - $xtPrice->xtcCalculateCurr($order->info['shipping_cost']);
126
        
127
                $order->info['total'] -= round($xtPrice->xtcCalculateCurr($order->info['shipping_cost']), 2);
128
                $order->info['total'] += $xtPrice->xtcCalculateCurr($shippingCostInclTax);
129
        
130
                if ($GLOBALS[$module]->tax_class > 0 || $tax > 0) {
131
                    $order->info['shipping_cost']                                     = $shippingCostInclTax;
132
                    $order->info['tax']                                               += $tax;
133
                    $order->info['tax_groups'][TAX_ADD_TAX . $shippingTaxDescription] += $tax;
134
                }
135
            } elseif ((int)$_SESSION['customers_status']['customers_status_show_price_tax'] === 0
136
                      && (int)$_SESSION['customers_status']['customers_status_add_tax_ot'] === 1) {
137
        
138
                $tax = xtc_add_tax($order->info['shipping_cost'], $shippingTaxRate) - $order->info['shipping_cost'];
139
                $tax = $xtPrice->xtcCalculateCurr($tax);
140
        
141
                $order->info['tax']                                              += $tax;
142
                $order->info['tax_groups'][TAX_NO_TAX . $shippingTaxDescription] += $tax;
143
            }
144
    
145
            $title = $order->info['shipping_method'];
146
            if ($order->info['shipping_class'] !== 'selfpickup_selfpickup') {
147
                $isCheckout = strpos(basename(gm_get_env_info('SCRIPT_NAME')), 'checkout') === 0;
148
                $showShippingModuleTitle = defined('MODULE_ORDER_TOTAL_SHIPPING_SHOW_TITLE') && MODULE_ORDER_TOTAL_SHIPPING_SHOW_TITLE === 'true';
149
                if (!$isCheckout && isset($_SESSION['customer_country_id'], $_SESSION['customer_zone_id'])) {
150
                    /** @var \CountryService $countryService */
151
                    $countryService = StaticGXCoreLoader::getService('Country');
152
                    $customerCountry = $countryService->getCountryById(new IdType((int)$_SESSION['customer_country_id']));
153
                    $shippingCostsLang = MainFactory::create('LanguageTextManager', 'cart_shipping_costs');
154
                    if ($showShippingModuleTitle === true) {
155
                        $title .= sprintf(
156
                            ' %s %s',
157
                            $shippingCostsLang->get_text('to'),
158
                            (string)$customerCountry->getIso2()
159
                        );
160
                    } else {
161
                        $title = sprintf(
162
                            '%s %s %s',
163
                            $shippingCostsLang->get_text('delivery'),
164
                            $shippingCostsLang->get_text('to'),
165
                            (string)$customerCountry->getIso2()
166
                        );
167
                    }
168
                }
169
            }
170
            
171
            $this->output[] = [
172
                'title' => $title . ':',
173
                'text'  => $xtPrice->xtcFormat($order->info['shipping_cost'], true, 0, true),
174
                'value' => $xtPrice->xtcFormat($order->info['shipping_cost'], false, 0, true),
175
            ];
176
        }
177
    }
178
    
179
    
180
    public function check()
181
    {
182
        if (!isset($this->_check)) {
183
            $check_query  = xtc_db_query(
184
                "select `value` from `gx_configurations` where `key` = 'configuration/MODULE_ORDER_TOTAL_SHIPPING_STATUS'"
185
            );
186
            $this->_check = xtc_db_num_rows($check_query);
187
        }
188
        
189
        return $this->_check;
190
    }
191
    
192
    
193
    public function keys()
194
    {
195
        return [
196
            'configuration/MODULE_ORDER_TOTAL_SHIPPING_STATUS',
197
            'configuration/MODULE_ORDER_TOTAL_SHIPPING_SORT_ORDER',
198
            'configuration/MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING',
199
            'configuration/MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING_OVER',
200
            'configuration/MODULE_ORDER_TOTAL_SHIPPING_DESTINATION',
201
            'configuration/MODULE_ORDER_TOTAL_SHIPPING_TAX_CLASS',
202
            'configuration/MODULE_ORDER_TOTAL_SHIPPING_SHOW_TITLE',
203
        ];
204
    }
205
    
206
    
207
    public function install()
208
    {
209
        xtc_db_query(
210
            "insert into `gx_configurations` (`key`, `value`, `legacy_group_id`, `sort_order`, `type`) values ('configuration/MODULE_ORDER_TOTAL_SHIPPING_STATUS', 'true','6', '1','switcher')"
211
        );
212
        xtc_db_query(
213
            "insert into `gx_configurations` (`key`, `value`, `legacy_group_id`, `sort_order`) values ('configuration/MODULE_ORDER_TOTAL_SHIPPING_SORT_ORDER', '30','6', '2')"
214
        );
215
        xtc_db_query(
216
            "insert into `gx_configurations` (`key`, `value`, `legacy_group_id`, `sort_order`, `type`) values ('configuration/MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING', 'false','6', '3', 'switcher')"
217
        );
218
        // Todo: $currencies->format use_function validation
219
        xtc_db_query(
220
            "insert into `gx_configurations` (`key`, `value`, `legacy_group_id`, `sort_order`, `type`) values ('configuration/MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING_OVER', '50', '6', '4', 'currencies->format')"
221
        );
222
        xtc_db_query(
223
            "insert into `gx_configurations` (`key`, `value`, `legacy_group_id`, `sort_order`, `type`) values ('configuration/MODULE_ORDER_TOTAL_SHIPPING_DESTINATION', 'national','6', '5', 'shipping-destination')"
224
        );
225
        xtc_db_query(
226
            "insert into `gx_configurations` (`key`, `value`, `legacy_group_id`, `sort_order`, `type`) values ('configuration/MODULE_ORDER_TOTAL_SHIPPING_TAX_CLASS', '0','6', '7', 'tax-class')"
227
        );
228
    
229
        xtc_db_query(
230
            'INSERT INTO `gx_configurations` ' .
231
            '(`key`, `value`, `legacy_group_id`, `sort_order`, `type`) VALUES ' .
232
            "('configuration/MODULE_ORDER_TOTAL_SHIPPING_SHOW_TITLE', 'false', '6', '8', 'switcher')"
233
        );
234

    
235

    
236
    }
237
    
238
    
239
    public function remove()
240
    {
241
        xtc_db_query(
242
            'delete from ' . TABLE_CONFIGURATION . " where `key` in ('" . implode("', '", $this->keys())
243
            . "')"
244
        );
245
    }
246
}
247

    
248
MainFactory::load_origin_class('ot_shipping');
(2-2/2)