Completed
Pull Request — 4.0 (#4331)
by Kentaro
05:18
created

TaxRule::compareTo()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 9.648

Importance

Changes 0
Metric Value
cc 9
nc 7
nop 1
dl 0
loc 25
ccs 8
cts 10
cp 0.8
crap 9.648
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Entity;
15
16
use Doctrine\ORM\Mapping as ORM;
17
18 1
if (!class_exists('\Eccube\Entity\TaxRule')) {
19
    /**
20
     * TaxRule
21
     *
22
     * @ORM\Table(name="dtb_tax_rule")
23
     * @ORM\InheritanceType("SINGLE_TABLE")
24
     * @ORM\DiscriminatorColumn(name="discriminator_type", type="string", length=255)
25
     * @ORM\HasLifecycleCallbacks()
26
     * @ORM\Entity(repositoryClass="Eccube\Repository\TaxRuleRepository")
27
     */
28
    class TaxRule extends \Eccube\Entity\AbstractEntity
29
    {
30
        /**
31
         * @var integer
32
         */
33
        const DEFAULT_TAX_RULE_ID = 1;
34
35
        /**
36
         * @var integer
37
         */
38
        private $sort_no;
39
40
        /**
41
         * is default
42
         *
43
         * @return bool
44
         */
45 4
        public function isDefaultTaxRule()
46
        {
47 4
            return self::DEFAULT_TAX_RULE_ID === $this->getId();
48
        }
49
50
        /**
51
         * Set sortNo
52
         *
53
         * @param  integer $sortNo
54
         *
55
         * @return TaxRule
56
         */
57 465
        public function setSortNo($sortNo)
58
        {
59 465
            $this->sort_no = $sortNo;
60
61 465
            return $this;
62
        }
63
64
        /**
65
         * Get sortNo
66
         *
67
         * @return integer
68
         */
69 5
        public function getSortNo()
70
        {
71 5
            return $this->sort_no;
72
        }
73
74
        /**
75
         * @var int
76
         *
77
         * @ORM\Column(name="id", type="integer", options={"unsigned":true})
78
         * @ORM\Id
79
         * @ORM\GeneratedValue(strategy="IDENTITY")
80
         */
81
        private $id;
82
83
        /**
84
         * @var string
85
         *
86
         * @ORM\Column(name="tax_rate", type="decimal", precision=10, scale=0, options={"unsigned":true,"default":8})
87
         */
88
        private $tax_rate = 8;
89
90
        /**
91
         * @var string
92
         *
93
         * @ORM\Column(name="tax_adjust", type="decimal", precision=10, scale=0, options={"unsigned":true,"default":0})
94
         */
95
        private $tax_adjust = 0;
96
97
        /**
98
         * @var \DateTime
99
         *
100
         * @ORM\Column(name="apply_date", type="datetimetz")
101
         */
102
        private $apply_date;
103
104
        /**
105
         * @var \DateTime
106
         *
107
         * @ORM\Column(name="create_date", type="datetimetz")
108
         */
109
        private $create_date;
110
111
        /**
112
         * @var \DateTime
113
         *
114
         * @ORM\Column(name="update_date", type="datetimetz")
115
         */
116
        private $update_date;
117
118
        /**
119
         * @var \Eccube\Entity\ProductClass
120
         *
121
         * @ORM\OneToOne(targetEntity="Eccube\Entity\ProductClass", inversedBy="TaxRule")
122
         * @ORM\JoinColumns({
123
         *   @ORM\JoinColumn(name="product_class_id", referencedColumnName="id")
124
         * })
125
         */
126
        private $ProductClass;
127
128
        /**
129
         * @var \Eccube\Entity\Member
130
         *
131
         * @ORM\ManyToOne(targetEntity="Eccube\Entity\Member")
132
         * @ORM\JoinColumns({
133
         *   @ORM\JoinColumn(name="creator_id", referencedColumnName="id")
134
         * })
135
         */
136
        private $Creator;
137
138
        /**
139
         * @var \Eccube\Entity\Master\Country
140
         *
141
         * @ORM\ManyToOne(targetEntity="Eccube\Entity\Master\Country")
142
         * @ORM\JoinColumns({
143
         *   @ORM\JoinColumn(name="country_id", referencedColumnName="id")
144
         * })
145
         */
146
        private $Country;
147
148
        /**
149
         * @var \Eccube\Entity\Master\Pref
150
         *
151
         * @ORM\ManyToOne(targetEntity="Eccube\Entity\Master\Pref")
152
         * @ORM\JoinColumns({
153
         *   @ORM\JoinColumn(name="pref_id", referencedColumnName="id")
154
         * })
155
         */
156
        private $Pref;
157
158
        /**
159
         * @var \Eccube\Entity\Product
160
         *
161
         * @ORM\ManyToOne(targetEntity="Eccube\Entity\Product")
162
         * @ORM\JoinColumns({
163
         *   @ORM\JoinColumn(name="product_id", referencedColumnName="id")
164
         * })
165
         */
166
        private $Product;
167
168
        /**
169
         * @var \Eccube\Entity\Master\RoundingType
170
         *
171
         * @ORM\ManyToOne(targetEntity="Eccube\Entity\Master\RoundingType")
172
         * @ORM\JoinColumns({
173
         *   @ORM\JoinColumn(name="rounding_type_id", referencedColumnName="id")
174
         * })
175
         */
176
        private $RoundingType;
177
178
        /**
179
         * Get id.
180
         *
181
         * @return int
182
         */
183 224
        public function getId()
184
        {
185 224
            return $this->id;
186
        }
187
188
        /**
189
         * Set taxRate.
190
         *
191
         * @param string $taxRate
192
         *
193
         * @return TaxRule
194
         */
195 30
        public function setTaxRate($taxRate)
196
        {
197 30
            $this->tax_rate = $taxRate;
198
199 30
            return $this;
200
        }
201
202
        /**
203
         * Get taxRate.
204
         *
205
         * @return string
206
         */
207 469
        public function getTaxRate()
208
        {
209 469
            return $this->tax_rate;
210
        }
211
212
        /**
213
         * Set taxAdjust.
214
         *
215
         * @param string $taxAdjust
216
         *
217
         * @return TaxRule
218
         */
219 29
        public function setTaxAdjust($taxAdjust)
220
        {
221 29
            $this->tax_adjust = $taxAdjust;
222
223 29
            return $this;
224
        }
225
226
        /**
227
         * Get taxAdjust.
228
         *
229
         * @return string
230
         */
231 465
        public function getTaxAdjust()
232
        {
233 465
            return $this->tax_adjust;
234
        }
235
236
        /**
237
         * Set applyDate.
238
         *
239
         * @param \DateTime $applyDate
240
         *
241
         * @return TaxRule
242
         */
243 40
        public function setApplyDate($applyDate)
244
        {
245 40
            $this->apply_date = $applyDate;
246
247 40
            return $this;
248
        }
249
250
        /**
251
         * Get applyDate.
252
         *
253
         * @return \DateTime
254
         */
255 13
        public function getApplyDate()
256
        {
257 13
            return $this->apply_date;
258
        }
259
260
        /**
261
         * Set createDate.
262
         *
263
         * @param \DateTime $createDate
264
         *
265
         * @return TaxRule
266
         */
267 28
        public function setCreateDate($createDate)
268
        {
269 28
            $this->create_date = $createDate;
270
271 28
            return $this;
272
        }
273
274
        /**
275
         * Get createDate.
276
         *
277
         * @return \DateTime
278
         */
279
        public function getCreateDate()
280
        {
281
            return $this->create_date;
282
        }
283
284
        /**
285
         * Set updateDate.
286
         *
287
         * @param \DateTime $updateDate
288
         *
289
         * @return TaxRule
290
         */
291 36
        public function setUpdateDate($updateDate)
292
        {
293 36
            $this->update_date = $updateDate;
294
295 36
            return $this;
296
        }
297
298
        /**
299
         * Get updateDate.
300
         *
301
         * @return \DateTime
302
         */
303
        public function getUpdateDate()
304
        {
305
            return $this->update_date;
306
        }
307
308
        /**
309
         * Set productClass.
310
         *
311
         * @param \Eccube\Entity\ProductClass|null $productClass
312
         *
313
         * @return TaxRule
314
         */
315 16
        public function setProductClass(\Eccube\Entity\ProductClass $productClass = null)
316
        {
317 16
            $this->ProductClass = $productClass;
318
319 16
            return $this;
320
        }
321
322
        /**
323
         * Get productClass.
324
         *
325
         * @return \Eccube\Entity\ProductClass|null
326
         */
327
        public function getProductClass()
328
        {
329
            return $this->ProductClass;
330
        }
331
332
        /**
333
         * Set creator.
334
         *
335
         * @param \Eccube\Entity\Member|null $creator
336
         *
337
         * @return TaxRule
338
         */
339 27
        public function setCreator(\Eccube\Entity\Member $creator = null)
340
        {
341 27
            $this->Creator = $creator;
342
343 27
            return $this;
344
        }
345
346
        /**
347
         * Get creator.
348
         *
349
         * @return \Eccube\Entity\Member|null
350
         */
351
        public function getCreator()
352
        {
353
            return $this->Creator;
354
        }
355
356
        /**
357
         * Set country.
358
         *
359
         * @param \Eccube\Entity\Master\Country|null $country
360
         *
361
         * @return TaxRule
362
         */
363 2
        public function setCountry(\Eccube\Entity\Master\Country $country = null)
364
        {
365 2
            $this->Country = $country;
366
367 2
            return $this;
368
        }
369
370
        /**
371
         * Get country.
372
         *
373
         * @return \Eccube\Entity\Master\Country|null
374
         */
375
        public function getCountry()
376
        {
377
            return $this->Country;
378
        }
379
380
        /**
381
         * Set pref.
382
         *
383
         * @param \Eccube\Entity\Master\Pref|null $pref
384
         *
385
         * @return TaxRule
386
         */
387 1
        public function setPref(\Eccube\Entity\Master\Pref $pref = null)
388
        {
389 1
            $this->Pref = $pref;
390
391 1
            return $this;
392
        }
393
394
        /**
395
         * Get pref.
396
         *
397
         * @return \Eccube\Entity\Master\Pref|null
398
         */
399
        public function getPref()
400
        {
401
            return $this->Pref;
402
        }
403
404
        /**
405
         * Set product.
406
         *
407
         * @param \Eccube\Entity\Product|null $product
408
         *
409
         * @return TaxRule
410
         */
411 18
        public function setProduct(\Eccube\Entity\Product $product = null)
412
        {
413 18
            $this->Product = $product;
414
415 18
            return $this;
416
        }
417
418
        /**
419
         * Get product.
420
         *
421
         * @return \Eccube\Entity\Product|null
422
         */
423
        public function getProduct()
424
        {
425
            return $this->Product;
426
        }
427
428
        /**
429
         * Set roundingType.
430
         *
431
         * @return TaxRule
432
         */
433
        public function setRoundingType(\Eccube\Entity\Master\RoundingType $RoundingType = null)
434 29
        {
435
            $this->RoundingType = $RoundingType;
436 29
437
            return $this;
438 29
        }
439
440
        /**
441
         * Get roundingType.
442
         *
443
         * @return \Eccube\Entity\Master\RoundingType|null
444
         */
445
        public function getRoundingType()
446 471
        {
447
            return $this->RoundingType;
448 471
        }
449
450
        /**
451
         * 自分自身と Target を比較し, ソートのための数値を返す.
452
         *
453
         * 以下の順で比較し、
454
         *
455
         * 同一であれば 0
456
         * 自分の方が大きければ正の整数
457
         * 小さければ負の整数を返す.
458
         *
459
         * 1. 商品別税率が有効
460
         * 2. apply_date
461
         * 3. sort_no
462
         *
463
         * このメソッドは usort() 関数などで使用する.
464
         *
465
         * @param TaxRule $Target 比較対象の TaxRule
466
         *
467
         * @return integer
468
         */
469 7
        public function compareTo(TaxRule $Target)
470
        {
471 7
            if ($this->isProductTaxRule() && !$Target->isProductTaxRule()) {
472 5
                return -1;
473
            } elseif (!$this->isProductTaxRule() && $Target->isProductTaxRule()) {
474
                return 1;
475 5
            } else {
476 4
                if ($this->getApplyDate()->format('YmdHis') == $Target->getApplyDate()->format('YmdHis')) {
477
                    if ($this->getSortNo() == $Target->getSortNo()) {
478 1
                        return 0;
479
                    }
480
                    if ($this->getSortNo() > $Target->getSortNo()) {
481 2
                        return -1;
482 2
                    } else {
483
                        return 1;
484
                    }
485
                } else {
486
                    if ($this->getApplyDate()->format('YmdHis') > $Target->getApplyDate()->format('YmdHis')) {
487
                        return -1;
488
                    } else {
489
                        return 1;
490
                    }
491
                }
492
            }
493
        }
494
495
        /**
496
         * 商品別税率設定が適用されているかどうか.
497
         *
498
         * @return bool 商品別税率が適用されている場合 true
499
         */
500
        public function isProductTaxRule()
501
        {
502
            return ($this->getProductClass() !== null || $this->getProduct() !== null);
503
        }
504
    }
505
}
506