|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of EC-CUBE |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* http://www.lockon.co.jp/ |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software; you can redistribute it and/or |
|
10
|
|
|
* modify it under the terms of the GNU General Public License |
|
11
|
|
|
* as published by the Free Software Foundation; either version 2 |
|
12
|
|
|
* of the License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU General Public License |
|
20
|
|
|
* along with this program; if not, write to the Free Software |
|
21
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
namespace Eccube\Repository; |
|
26
|
|
|
|
|
27
|
|
|
use Doctrine\ORM\EntityRepository; |
|
28
|
|
|
use Doctrine\ORM\NoResultException; |
|
29
|
|
|
use Eccube\Common\Constant; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* TaxRuleRepository |
|
33
|
|
|
* |
|
34
|
|
|
* This class was generated by the Doctrine ORM. Add your own custom |
|
35
|
|
|
* repository methods below. |
|
36
|
|
|
*/ |
|
37
|
|
|
class TaxRuleRepository extends EntityRepository |
|
38
|
|
|
{ |
|
39
|
|
|
private $rules = array(); |
|
40
|
|
|
|
|
41
|
|
|
protected $app; |
|
42
|
|
|
|
|
43
|
1087 |
|
public function setApplication($app) |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
1087 |
|
$this->app = $app; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
11 |
|
public function newTaxRule() |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
11 |
|
$TaxRule = new \Eccube\Entity\TaxRule(); |
|
51
|
11 |
|
$CalcRule = $this->getEntityManager() |
|
52
|
11 |
|
->getRepository('Eccube\Entity\Master\Taxrule') |
|
53
|
11 |
|
->find(1); |
|
54
|
11 |
|
$TaxRule->setCalcRule($CalcRule); |
|
55
|
11 |
|
$TaxRule->setTaxAdjust(0); |
|
56
|
11 |
|
$TaxRule->setDelFlg(0); |
|
57
|
|
|
|
|
58
|
11 |
|
return $TaxRule; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* 現在有効な税率設定情報を返す |
|
63
|
|
|
* |
|
64
|
|
|
* @param int|null|\Eccube\Entity\Product $Product 商品 |
|
65
|
|
|
* @param int|null|\Eccube\Entity\ProductClass $ProductClass 商品規格 |
|
66
|
|
|
* @param int|null|\Eccube\Entity\Master\Pref $Pref 都道府県 |
|
67
|
|
|
* @param int|null|\Eccube\Entity\Master\Country $Country 国 |
|
68
|
|
|
* @return \Eccube\Entity\TaxRule 税設定情報 |
|
69
|
|
|
* |
|
70
|
|
|
* @throws NoResultException |
|
71
|
|
|
*/ |
|
72
|
340 |
|
public function getByRule($Product = null, $ProductClass = null, $Pref = null, $Country = null) |
|
73
|
|
|
{ |
|
74
|
340 |
|
if (!$this->app) { |
|
75
|
|
|
throw new \LogicException(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// Pref Country 設定 |
|
79
|
340 |
|
if (!$Pref && !$Country && $this->app['security']->getToken() && $this->app['security']->isGranted('ROLE_USER')) { |
|
80
|
|
|
/* @var $Customer \Eccube\Entity\Customer */ |
|
81
|
51 |
|
$Customer = $this->app['security']->getToken()->getUser(); |
|
82
|
51 |
|
$Pref = $Customer->getPref(); |
|
83
|
51 |
|
$Country = $Customer->getCountry(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// 商品単位税率設定がOFFの場合 |
|
87
|
|
|
/** @var $BaseInfo \Eccube\Entity\BaseInfo */ |
|
88
|
340 |
|
$BaseInfo = $this->app['eccube.repository.base_info']->get(); |
|
89
|
340 |
|
if ($BaseInfo->getOptionProductTaxRule() !== Constant::ENABLED) { |
|
90
|
318 |
|
$Product = null; |
|
91
|
318 |
|
$ProductClass = null; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// Cache Key 設定 |
|
95
|
340 |
|
if ($Product instanceof \Eccube\Entity\Product) { |
|
|
|
|
|
|
96
|
26 |
|
$productId = $Product->getId(); |
|
97
|
318 |
|
} elseif ($Product) { |
|
|
|
|
|
|
98
|
|
|
$productId = $Product; |
|
99
|
|
|
} else { |
|
100
|
318 |
|
$productId = '0'; |
|
101
|
|
|
} |
|
102
|
340 |
|
if ($ProductClass instanceof \Eccube\Entity\ProductClass) { |
|
|
|
|
|
|
103
|
25 |
|
$productClassId = $ProductClass->getId(); |
|
104
|
318 |
|
} elseif ($ProductClass) { |
|
|
|
|
|
|
105
|
|
|
$productClassId = $ProductClass; |
|
106
|
|
|
} else { |
|
107
|
318 |
|
$productClassId = '0'; |
|
108
|
|
|
} |
|
109
|
340 |
|
if ($Pref instanceof \Eccube\Entity\Master\Pref) { |
|
|
|
|
|
|
110
|
52 |
|
$prefId = $Pref->getId(); |
|
111
|
290 |
|
} elseif ($Pref) { |
|
112
|
|
|
$prefId = $Pref; |
|
113
|
|
|
} else { |
|
114
|
290 |
|
$prefId = '0'; |
|
115
|
|
|
} |
|
116
|
340 |
|
if ($Country instanceof \Eccube\Entity\Master\Country) { |
|
|
|
|
|
|
117
|
2 |
|
$countryId = $Country->getId(); |
|
118
|
340 |
|
} elseif ($Country) { |
|
119
|
|
|
$countryId = $Country; |
|
120
|
|
|
} else { |
|
121
|
340 |
|
$countryId = '0'; |
|
122
|
|
|
} |
|
123
|
340 |
|
$cacheKey = $productId.':'.$productClassId.':'.$prefId.':'.$countryId; |
|
124
|
|
|
|
|
125
|
|
|
// すでに取得している場合はキャッシュから |
|
126
|
340 |
|
if (isset($this->rules[$cacheKey])) { |
|
127
|
338 |
|
return $this->rules[$cacheKey]; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
340 |
|
$parameters = array(); |
|
131
|
340 |
|
$qb = $this->createQueryBuilder('t') |
|
132
|
340 |
|
->where('t.apply_date < :apply_date'); |
|
133
|
340 |
|
$parameters[':apply_date'] = new \DateTime(); |
|
134
|
|
|
|
|
135
|
|
|
// Pref |
|
136
|
340 |
|
if ($Pref) { |
|
137
|
52 |
|
$qb->andWhere('t.Pref IS NULL OR t.Pref = :Pref'); |
|
138
|
52 |
|
$parameters['Pref'] = $Pref; |
|
139
|
|
|
} else { |
|
140
|
290 |
|
$qb->andWhere('t.Pref IS NULL'); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
// Country |
|
144
|
340 |
|
if ($Country) { |
|
145
|
2 |
|
$qb->andWhere('t.Country IS NULL OR t.Country = :Country'); |
|
146
|
2 |
|
$parameters['Country'] = $Country; |
|
147
|
|
|
} else { |
|
148
|
340 |
|
$qb->andWhere('t.Country IS NULL'); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/* |
|
152
|
|
|
* Product, ProductClass が persist される前に TaxRuleEventSubscriber によってアクセスされる |
|
153
|
|
|
* 場合があるため、ID の存在もチェックする. |
|
154
|
|
|
* https://github.com/EC-CUBE/ec-cube/issues/677 |
|
155
|
|
|
*/ |
|
156
|
|
|
|
|
157
|
|
|
// Product |
|
158
|
340 |
|
if ($Product && $productId > 0) { |
|
159
|
26 |
|
$qb->andWhere('t.Product IS NULL OR t.Product = :Product'); |
|
160
|
26 |
|
$parameters['Product'] = $Product; |
|
161
|
|
|
} else { |
|
162
|
321 |
|
$qb->andWhere('t.Product IS NULL'); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
// ProductClass |
|
166
|
340 |
|
if ($ProductClass && $productClassId > 0) { |
|
167
|
25 |
|
$qb->andWhere('t.ProductClass IS NULL OR t.ProductClass = :ProductClass'); |
|
168
|
25 |
|
$parameters['ProductClass'] = $ProductClass; |
|
169
|
|
|
} else { |
|
170
|
336 |
|
$qb->andWhere('t.ProductClass IS NULL'); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
$TaxRules = $qb |
|
174
|
340 |
|
->setParameters($parameters) |
|
175
|
340 |
|
->orderBy('t.apply_date', 'DESC') // 実際は usort() でソートする |
|
176
|
340 |
|
->getQuery() |
|
177
|
340 |
|
->getResult(); |
|
178
|
|
|
|
|
179
|
|
|
// 地域設定を優先するが、システムパラメーターなどに設定を持っていくか |
|
180
|
|
|
// 後に書いてあるほど優先される |
|
181
|
340 |
|
$priorityKeys = explode(',', $this->app['config']['tax_rule_priority']); |
|
|
|
|
|
|
182
|
340 |
|
$priorityKeys = array(); |
|
183
|
340 |
|
foreach (explode(',', $this->app['config']['tax_rule_priority']) as $key) { |
|
184
|
340 |
|
$priorityKeys[] = str_replace('_', '', preg_replace('/_id\z/', '', $key)); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
340 |
|
foreach ($TaxRules as $TaxRule) { |
|
188
|
340 |
|
$rank = 0; |
|
189
|
340 |
|
foreach ($priorityKeys as $index => $key) { |
|
190
|
340 |
|
$arrayProperties = array_change_key_case($TaxRule->toArray()); |
|
191
|
340 |
|
if ($arrayProperties[$key]) { |
|
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
// 配列の数値添字を重みとして利用する |
|
194
|
340 |
|
$rank += 1 << ($index + 1); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
340 |
|
$TaxRule->setRank($rank); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
// 適用日降順, rank 降順にソートする |
|
201
|
340 |
|
usort($TaxRules, function($a, $b) { |
|
|
|
|
|
|
202
|
8 |
|
return $a->compareTo($b); |
|
203
|
340 |
|
}); |
|
204
|
|
|
|
|
205
|
340 |
|
if (!empty($TaxRules)) { |
|
206
|
340 |
|
$this->rules[$cacheKey] = $TaxRules[0]; |
|
207
|
|
|
|
|
208
|
340 |
|
return $TaxRules[0]; |
|
209
|
|
|
} else { |
|
210
|
|
|
throw new NoResultException(); |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* getList |
|
216
|
|
|
* |
|
217
|
|
|
* @return array|null |
|
218
|
|
|
*/ |
|
219
|
5 |
|
public function getList() |
|
220
|
|
|
{ |
|
221
|
5 |
|
$qb = $this->createQueryBuilder('t') |
|
222
|
5 |
|
->orderBy('t.apply_date', 'DESC') |
|
223
|
5 |
|
->where('t.Product IS NULL AND t.ProductClass IS NULL'); |
|
224
|
|
|
$TaxRules = $qb |
|
225
|
5 |
|
->getQuery() |
|
226
|
5 |
|
->getResult(); |
|
227
|
|
|
|
|
228
|
5 |
|
return $TaxRules; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* getById |
|
233
|
|
|
* |
|
234
|
|
|
* @param int $id |
|
|
|
|
|
|
235
|
|
|
* @return array |
|
236
|
|
|
*/ |
|
237
|
1 |
|
public function getById($id) |
|
238
|
|
|
{ |
|
239
|
|
|
$criteria = array( |
|
240
|
1 |
|
'id' => $id, |
|
241
|
|
|
); |
|
242
|
|
|
|
|
243
|
1 |
|
return $this->findOneBy($criteria); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* getByTime |
|
248
|
|
|
* |
|
249
|
|
|
* @param string $applyDate |
|
250
|
|
|
* @return mixed |
|
251
|
|
|
*/ |
|
252
|
|
|
public function getByTime($applyDate) |
|
253
|
|
|
{ |
|
254
|
|
|
$criteria = array( |
|
255
|
|
|
'apply_date' => $applyDate, |
|
256
|
|
|
); |
|
257
|
|
|
|
|
258
|
|
|
return $this->findOneBy($criteria); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* 税規約の削除. |
|
263
|
|
|
* |
|
264
|
|
|
* @param int|\Eccube\Entity\TaxRule $TaxRule 税規約 |
|
265
|
|
|
* @return void |
|
266
|
|
|
* @throws NoResultException |
|
267
|
|
|
*/ |
|
268
|
3 |
|
public function delete($TaxRule) |
|
269
|
|
|
{ |
|
270
|
3 |
|
if (!$TaxRule instanceof \Eccube\Entity\TaxRule) { |
|
|
|
|
|
|
271
|
1 |
|
$TaxRule = $this->find($TaxRule); |
|
272
|
|
|
} |
|
273
|
3 |
|
if (!$TaxRule) { |
|
274
|
|
|
throw new NoResultException; |
|
|
|
|
|
|
275
|
|
|
} |
|
276
|
3 |
|
$TaxRule->setDelFlg(1); |
|
277
|
3 |
|
$em = $this->getEntityManager(); |
|
278
|
3 |
|
$em->persist($TaxRule); |
|
279
|
3 |
|
$em->flush(); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* TaxRule のキャッシュをクリアする. |
|
284
|
|
|
* |
|
285
|
|
|
* getByRule() をコールすると、結果をキャッシュし、2回目以降はデータベースへアクセスしない. |
|
286
|
|
|
* このメソッドをコールすると、キャッシュをクリアし、再度データベースを参照して結果を取得する. |
|
287
|
|
|
*/ |
|
288
|
8 |
|
public function clearCache() |
|
289
|
|
|
{ |
|
290
|
8 |
|
$this->rules = array(); |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
|