1 | <?php |
||
37 | class TaxRuleRepository extends EntityRepository |
||
38 | { |
||
39 | private $rules = array(); |
||
40 | |||
41 | protected $app; |
||
42 | |||
43 | 533 | public function setApplication($app) |
|
47 | |||
48 | 1 | public function newTaxRule() |
|
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 | 182 | public function getByRule($Product = null, $ProductClass = null, $Pref = null, $Country = null) |
|
73 | { |
||
74 | 2 | if (!$this->app) { |
|
75 | throw new \LogicException(); |
||
76 | } |
||
77 | |||
78 | // Pref Country 設定 |
||
79 | if (!$Pref && !$Country && $this->app['security']->getToken() && $this->app['security']->isGranted('ROLE_USER')) { |
||
80 | /* @var $Customer \Eccube\Entity\Customer */ |
||
81 | $Customer = $this->app['security']->getToken()->getUser(); |
||
82 | $Pref = $Customer->getPref(); |
||
83 | $Country = $Customer->getCountry(); |
||
84 | } |
||
85 | |||
86 | // 商品単位税率設定がOFFの場合 |
||
87 | /** @var $BaseInfo \Eccube\Entity\BaseInfo */ |
||
88 | $BaseInfo = $this->app['eccube.repository.base_info']->get(); |
||
89 | if ($BaseInfo->getOptionProductTaxRule() !== Constant::ENABLED) { |
||
90 | 2 | $Product = null; |
|
91 | 2 | $ProductClass = null; |
|
92 | } |
||
93 | |||
94 | // Cache Key 設定 |
||
95 | 2 | if ($Product instanceof \Eccube\Entity\Product) { |
|
96 | $productId = $Product->getId(); |
||
97 | 2 | } elseif ($Product) { |
|
98 | $productId = $Product; |
||
99 | } else { |
||
100 | 2 | $productId = '0'; |
|
101 | 2 | } |
|
102 | 2 | if ($ProductClass instanceof \Eccube\Entity\ProductClass) { |
|
103 | $productClassId = $ProductClass->getId(); |
||
104 | 2 | } else if ($ProductClass instanceof \Eccube\Entity\ShipmentItem) { |
|
105 | // XXX https://github.com/EC-CUBE/ec-cube/issues/1029 |
||
106 | // 注文処理時、TaxRuleEventSubscriber::prePersistからの呼び出しで、 |
||
107 | // $ProductClassにShipmentItemがsetされて呼び出されるのに対応 |
||
108 | $productClassId = ''; |
||
109 | 2 | } elseif ($ProductClass) { |
|
110 | $productClassId = $ProductClass; |
||
111 | } else { |
||
112 | 2 | $productClassId = '0'; |
|
113 | 1 | } |
|
114 | 2 | if ($Pref instanceof \Eccube\Entity\Master\Pref) { |
|
115 | $prefId = $Pref->getId(); |
||
116 | 2 | } elseif ($Pref) { |
|
117 | $prefId = $Pref; |
||
118 | } else { |
||
119 | 2 | $prefId = '0'; |
|
120 | 1 | } |
|
121 | 2 | if ($Country instanceof \Eccube\Entity\Master\Country) { |
|
122 | $countryId = $Country->getId(); |
||
123 | 2 | } elseif ($Country) { |
|
124 | $countryId = $Country; |
||
125 | } else { |
||
126 | 2 | $countryId = '0'; |
|
127 | 2 | } |
|
128 | 2 | $cacheKey = $productId.':'.$productClassId.':'.$prefId.':'.$countryId; |
|
129 | |||
130 | // すでに取得している場合はキャッシュから |
||
131 | 2 | if (isset($this->rules[$cacheKey])) { |
|
132 | 10 | return $this->rules[$cacheKey]; |
|
133 | } |
||
134 | |||
135 | 182 | $parameters = array(); |
|
136 | 182 | $qb = $this->createQueryBuilder('t') |
|
137 | ->where('t.apply_date < CURRENT_TIMESTAMP()'); |
||
138 | |||
139 | // Pref |
||
140 | 182 | if ($Pref) { |
|
141 | $qb->andWhere('t.Pref IS NULL OR t.Pref = :Pref'); |
||
142 | 26 | $parameters['Pref'] = $Pref; |
|
143 | } else { |
||
144 | $qb->andWhere('t.Pref IS NULL'); |
||
145 | 26 | } |
|
146 | |||
147 | // Country |
||
148 | 182 | if ($Country) { |
|
149 | $qb->andWhere('t.Country IS NULL OR t.Country = :Country'); |
||
150 | 2 | $parameters['Country'] = $Country; |
|
151 | } else { |
||
152 | $qb->andWhere('t.Country IS NULL'); |
||
153 | 2 | } |
|
154 | |||
155 | /* |
||
156 | * Product, ProductClass が persist される前に TaxRuleEventSubscriber によってアクセスされる |
||
157 | * 場合があるため、ID の存在もチェックする. |
||
158 | * https://github.com/EC-CUBE/ec-cube/issues/677 |
||
159 | */ |
||
160 | |||
161 | // Product |
||
162 | 182 | if ($Product && $productId > 0) { |
|
163 | $qb->andWhere('t.Product IS NULL OR t.Product = :Product'); |
||
164 | 2 | $parameters['Product'] = $Product; |
|
165 | } else { |
||
166 | $qb->andWhere('t.Product IS NULL'); |
||
167 | 2 | } |
|
168 | |||
169 | // ProductClass |
||
170 | 182 | if ($ProductClass && $productClassId > 0) { |
|
171 | $qb->andWhere('t.ProductClass IS NULL OR t.ProductClass = :ProductClass'); |
||
172 | 1 | $parameters['ProductClass'] = $ProductClass; |
|
173 | } else { |
||
174 | $qb->andWhere('t.ProductClass IS NULL'); |
||
175 | 1 | } |
|
176 | |||
177 | $TaxRules = $qb |
||
178 | 182 | ->setParameters($parameters) |
|
179 | 182 | ->orderBy('t.apply_date', 'DESC') // 実際は usort() でソートする |
|
180 | 182 | ->getQuery() |
|
181 | ->getResult(); |
||
182 | |||
183 | // 地域設定を優先するが、システムパラメーターなどに設定を持っていくか |
||
184 | // 後に書いてあるほど優先される |
||
185 | $priorityKeys = explode(',', $this->app['config']['tax_rule_priority']); |
||
186 | 182 | $priorityKeys = array(); |
|
187 | foreach (explode(',', $this->app['config']['tax_rule_priority']) as $key) { |
||
188 | $priorityKeys[] = str_replace('_', '', preg_replace('/_id\z/', '', $key)); |
||
189 | } |
||
190 | |||
191 | foreach ($TaxRules as $TaxRule) { |
||
192 | 182 | $rank = 0; |
|
193 | foreach ($priorityKeys as $index => $key) { |
||
194 | $arrayProperties = array_change_key_case($TaxRule->toArray()); |
||
195 | if ($arrayProperties[$key]) { |
||
196 | |||
197 | // 配列の数値添字を重みとして利用する |
||
198 | 4 | $rank += 1 << ($index + 1); |
|
199 | } |
||
200 | } |
||
201 | $TaxRule->setRank($rank); |
||
202 | } |
||
203 | |||
204 | // 適用日降順, rank 降順にソートする |
||
205 | usort($TaxRules, function($a, $b) { |
||
206 | return $a->compareTo($b); |
||
207 | }); |
||
208 | |||
209 | 182 | if (!empty($TaxRules)) { |
|
210 | 182 | $this->rules[$cacheKey] = $TaxRules[0]; |
|
211 | |||
212 | 182 | return $TaxRules[0]; |
|
213 | } else { |
||
214 | throw new NoResultException(); |
||
215 | } |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * getList |
||
220 | * |
||
221 | * @return array|null |
||
222 | */ |
||
223 | 3 | public function getList() |
|
234 | |||
235 | /** |
||
236 | * getById |
||
237 | * |
||
238 | * @param int $id |
||
239 | * @return array |
||
240 | */ |
||
241 | 1 | public function getById($id) |
|
249 | |||
250 | /** |
||
251 | * getByTime |
||
252 | * |
||
253 | * @param string $applyDate |
||
254 | * @return mixed |
||
255 | */ |
||
256 | public function getByTime($applyDate) |
||
264 | |||
265 | /** |
||
266 | * 税規約の削除. |
||
267 | * |
||
268 | * @param int|\Eccube\Entity\TaxRule $TaxRule 税規約 |
||
269 | * @return void |
||
270 | * @throws NoResultException |
||
271 | */ |
||
272 | 2 | public function delete($TaxRule) |
|
285 | |||
286 | /** |
||
287 | * TaxRule のキャッシュをクリアする. |
||
288 | * |
||
289 | * getByRule() をコールすると、結果をキャッシュし、2回目以降はデータベースへアクセスしない. |
||
290 | * このメソッドをコールすると、キャッシュをクリアし、再度データベースを参照して結果を取得する. |
||
291 | */ |
||
292 | 6 | public function clearCache() |
|
296 | } |
||
297 |