Projekt

Allgemein

Profil

GX-Bug #68910 » OrderTaxInformation.inc.php

Latest Version, since last revision isn't the most recent - src/system/classes/orders/OrderTaxInformation.inc.php - Josua Michalak, 30.03.2022 11:01

 
1
<?php
2
/* --------------------------------------------------------------
3
   OrderTaxInformation.inc.php 2022-01-26
4
   Gambio GmbH
5
   http://www.gambio.de
6
   Copyright (c) 2022 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
/**
13
 * Class OrderTaxInformations
14
 */
15
class OrderTaxInformation
16
{
17
    /**
18
     * @var TaxItem
19
     */
20
    protected $taxItem;
21
    
22
    /**
23
     * @var TaxItemWriter
24
     */
25
    protected $taxItemWriter;
26
    
27
    /**
28
     * @var TaxItemReader
29
     */
30
    protected        $taxItemReader;
31
    
32
    protected static $tableOrders      = 'orders';
33
        
34
    protected static $tableTaxClass    = 'tax_class';
35
    
36
    protected static $tableTaxRate     = 'tax_rates';
37
    
38
    
39
    /** @var int */
40
    protected $orderId = 0;
41
    
42
    
43
    /**
44
     *
45
     */
46
    public function __construct()
47
    {
48
        $this->taxItem       = MainFactory::create_object('TaxItem', []);
49
        $this->taxItemWriter = MainFactory::create_object('TaxItemWriter', [$this->taxItem]);
50
        $this->taxItemReader = MainFactory::create_object('TaxItemReader', []);
51
    }
52
    
53
    
54
    /**
55
     * @param $p_orderId
56
     */
57
    public function saveUnsavedTaxInformation($p_orderId)
58
    {
59
        $this->orderId = (int)$p_orderId;
60
        
61
        $isSavedBefore = $this->taxItemReader->orderIdIsSaved($this->orderId);
62
        
63
        if ($isSavedBefore === false) {
64
            $this->saveTaxInformation($p_orderId);
65
        }
66
    }
67
    
68
    
69
    /**
70
     * @param int $p_orderId
71
     */
72
    public function saveTaxInformation($p_orderId)
73
    {
74
        $this->orderId = (int)$p_orderId;
75
        
76
        /* Get Order information */
77
        
78
        $query = "SELECT
79
					orders_total.`title`,
80
					orders_total.`value`,
81
                    geo_zones.`geo_zone_name` AS geo_zone
82
					FROM orders_total
83
                    LEFT JOIN `orders` on orders_total.orders_id = orders.orders_id
84
                         LEFT JOIN `countries` on orders.delivery_country_iso_code_2 = countries.countries_iso_code_2
85
                         INNER JOIN (SELECT zone_country_id, MAX(geo_zone_id) AS geo_zone_id
86
                                     FROM zones_to_geo_zones
87
                                     GROUP BY zone_country_id) zones
88
                                    ON countries.countries_id = zones.zone_country_id
89
                         LEFT JOIN `geo_zones` on zones.geo_zone_id = geo_zones.geo_zone_id
90
					WHERE
91
						orders_total.`class` = 'ot_tax' AND orders_total.`orders_id` = '" . strval($this->orderId) . "'";
92
        
93
        $result = xtc_db_query($query);
94
        
95
        $taxesInfoArray = [];
96
        while ($taxBasicInformation = xtc_db_fetch_array($result)) {
97
            $taxesInfoArray[] = [
98
                'title'    => $taxBasicInformation['title'],
99
                'value'    => $taxBasicInformation['value'],
100
                'geo_zone' => $taxBasicInformation['geo_zone'],
101
            ];
102
        }
103
        
104
        foreach ($taxesInfoArray as $taxInfoArray) {
105
            if (is_array($taxInfoArray)) {
106
                $taxItem = $this->_prepareTaxInfoDataset($taxInfoArray);
107
                $this->taxItemWriter->insertDB($taxItem);
108
            }
109
        }
110
    }
111
    
112
    
113
    /**
114
     * @param array $taxInfoArray
115
     *
116
     * @return \TaxItem
117
     */
118
    protected function _prepareTaxInfoDataset(array $taxInfoArray = null)
119
    {
120
        $taxItem = clone $this->taxItem;
121
        
122
        $taxItem->setLastChangeDatetime(new DateTime());
123
        
124
        if (is_array($taxInfoArray)) {
125
            $title = $this->_getTaxTitle($taxInfoArray['title']);
126
            $taxItem->setTaxDescription($title);
127
            $taxItem->setTax($taxInfoArray['value']);
128
            
129
            $additionalTaxInfo = $this->_getAdditionalTaxInfo($title, $taxInfoArray['geo_zone']);
130
            
131
            $taxItem->setTaxClass($additionalTaxInfo->getTaxClass());
132
            $taxItem->setTaxRate($additionalTaxInfo->getTaxRate());
133
            $taxItem->setTaxZone($additionalTaxInfo->getTaxZone());
134
            $taxItem->setCurrency($additionalTaxInfo->getCurrency());
135
            $taxItem->setOrderId($this->orderId);
136
            
137
            /* Get gross and net */
138
            
139
            $taxRate = $taxItem->getTaxRate();
140
            $tax     = $taxItem->getTax();
141
            
142
            if ($taxRate > 0) {
143
                $net   = ($tax / $taxRate) * 100;
144
                $net   = round($net, 4);
145
                $gross = $net + $tax;
146
                
147
                $taxItem->setNet($net);
148
                $taxItem->setGross($gross);
149
            }
150
            
151
            return $taxItem;
152
        }
153
    }
154
    
155
    
156
    /**
157
     * @param string $p_taxDescription
158
     * @param string $p_geoZone
159
     *
160
     * @return TaxItem
161
     */
162
    protected function _getAdditionalTaxInfo($p_taxDescription, $p_geoZone)
163
    {
164
        $taxItem = clone $this->taxItem;
165
        
166
        $taxClass = $this->_getTaxClass($p_taxDescription);
167
        $taxZone  = $this->_getTaxZone($p_geoZone);
168
        $taxRate  = $this->_getTaxRate($p_taxDescription);
169
        $currency = $this->_getCurrency();
170
        
171
        $taxItem->setTaxClass($taxClass);
172
        $taxItem->setTaxZone($taxZone);
173
        $taxItem->setCurrency($currency);
174
        $taxItem->setTaxRate($taxRate);
175
        
176
        return $taxItem;
177
    }
178
    
179
    
180
    /**
181
     * @param string $p_taxDescription
182
     *
183
     * @return string
184
     */
185
    protected function _getTaxClass($p_taxDescription)
186
    {
187
        $where   = 'tax_description = \'' . xtc_db_input($p_taxDescription) . '\'';
188
        $taxInfo = $this->_getOneDataset(self::$tableTaxRate, $where);
189
        
190
        $taxClassId = $taxInfo['tax_class_id'];
191
        
192
        $where   = 'tax_class_id = ' . (int)$taxClassId;
193
        $taxInfo = $this->_getOneDataset(self::$tableTaxClass, $where);
194
        
195
        $taxClass = $taxInfo['tax_class_title'];
196
        
197
        return $taxClass;
198
    }
199
    
200
    
201
    /**
202
     * @param $p_taxDescription
203
     *
204
     * @return mixed
205
     */
206
    protected function _getTaxRate($p_taxDescription)
207
    {
208
        $where   = 'tax_description = \'' . xtc_db_input($p_taxDescription) . '\'';
209
        $taxInfo = $this->_getOneDataset(self::$tableTaxRate, $where);
210
        
211
        $taxRate = $taxInfo['tax_rate'];
212
        
213
        return $taxRate;
214
    }
215
    
216
    
217
    /**
218
     * @param $p_taxZone
219
     *
220
     * @return string
221
     */
222
    protected function _getTaxZone($p_taxZone)
223
    {
224
        return $p_taxZone;
225
    }
226
    
227
    
228
    /**
229
     * @return string
230
     */
231
    protected function _getCurrency()
232
    {
233
        $orderId = $this->orderId;
234
        
235
        $where   = 'orders_id = ' . (int)$orderId;
236
        $taxInfo = $this->_getOneDataset(self::$tableOrders, $where);
237
        
238
        $currency = $taxInfo['currency'];
239
        
240
        return $currency;
241
    }
242
    
243
    
244
    /**
245
     * @return DateTime
246
     */
247
    protected function _getDateOfPurchase()
248
    {
249
        $where      = 'orders_id = ' . (int)$this->orderId;
250
        $orderArray = $this->_getOneDataset(self::$tableOrders, $where);
251
        
252
        $dateOfPurchase = new EmptyDateTime($orderArray['date_purchased']);
253
        
254
        return $dateOfPurchase;
255
    }
256
    
257
    
258
    /**
259
     * @param string $tablename
260
     * @param string $where
261
     *
262
     * @return array
263
     */
264
    protected function _getOneDataset($tablename, $where)
265
    {
266
        $query = "SELECT * FROM `%s` WHERE %s";
267
        $query = sprintf($query, $tablename, $where);
268
        
269
        $result = xtc_db_query($query);
270
        
271
        $oneDataset = xtc_db_fetch_array($result);
272
        
273
        return $oneDataset;
274
    }
275
    
276
    
277
    /**
278
     * @param string $taxTitle
279
     *
280
     * @return string
281
     */
282
    protected function _getTaxTitle($taxTitle)
283
    {
284
        $title                 = $taxTitle;
285
        $taxRateStringPosition = 6;
286
        
287
        if ($_SESSION['customers_status']['customers_status_show_price_tax'] === '1' && defined('TAX_ADD_TAX')) {
288
            $taxRateStringPosition = strlen(TAX_ADD_TAX);
289
        } elseif (defined('TAX_NO_TAX')) {
290
            $taxRateStringPosition = strlen(TAX_NO_TAX);
291
        }
292
        
293
        $title = substr($title, $taxRateStringPosition); // remove i.e. "inkl. "
294
        $title = substr($title, 0, -1); // remove ":"
295
        
296
        if (isset($_SESSION['customer_cart_tax_info'])
297
            && array_key_exists($title,
298
                                $_SESSION['customer_cart_tax_info'])) {
299
            $title = xtc_get_tax_description($_SESSION['customer_cart_tax_info'][$title]['tax_class_id'],
300
                                             $_SESSION['customer_cart_tax_info'][$title]['country_id'],
301
                                             $_SESSION['customer_cart_tax_info'][$title]['zone_id'],
302
                                             -1,
303
                                             true);
304
        }
305
        
306
        return $title;
307
    }
308
}
    (1-1/1)