1 | <?php |
||
37 | class TaxRuleRepository extends EntityRepository |
||
38 | { |
||
39 | private $rules = array(); |
||
40 | |||
41 | protected $app; |
||
42 | |||
43 | 586 | 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 | 215 | 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 | } elseif ($ProductClass) { |
|
105 | $productClassId = $ProductClass; |
||
106 | } else { |
||
107 | 2 | $productClassId = '0'; |
|
108 | 1 | } |
|
109 | 2 | if ($Pref instanceof \Eccube\Entity\Master\Pref) { |
|
110 | $prefId = $Pref->getId(); |
||
111 | 2 | } elseif ($Pref) { |
|
112 | $prefId = $Pref; |
||
113 | } else { |
||
114 | 2 | $prefId = '0'; |
|
115 | 1 | } |
|
116 | 2 | if ($Country instanceof \Eccube\Entity\Master\Country) { |
|
117 | $countryId = $Country->getId(); |
||
118 | 2 | } elseif ($Country) { |
|
119 | $countryId = $Country; |
||
120 | } else { |
||
121 | 2 | $countryId = '0'; |
|
122 | 2 | } |
|
123 | 2 | $cacheKey = $productId.':'.$productClassId.':'.$prefId.':'.$countryId; |
|
124 | |||
125 | // すでに取得している場合はキャッシュから |
||
126 | 2 | if (isset($this->rules[$cacheKey])) { |
|
127 | 9 | return $this->rules[$cacheKey]; |
|
128 | } |
||
129 | |||
130 | 215 | $parameters = array(); |
|
131 | 215 | $qb = $this->createQueryBuilder('t') |
|
132 | ->where('t.apply_date < CURRENT_TIMESTAMP()'); |
||
133 | |||
134 | // Pref |
||
135 | 215 | if ($Pref) { |
|
136 | $qb->andWhere('t.Pref IS NULL OR t.Pref = :Pref'); |
||
137 | 37 | $parameters['Pref'] = $Pref; |
|
138 | } else { |
||
139 | $qb->andWhere('t.Pref IS NULL'); |
||
140 | 37 | } |
|
141 | |||
142 | // Country |
||
143 | 215 | if ($Country) { |
|
144 | $qb->andWhere('t.Country IS NULL OR t.Country = :Country'); |
||
145 | 2 | $parameters['Country'] = $Country; |
|
146 | } else { |
||
147 | $qb->andWhere('t.Country IS NULL'); |
||
148 | 2 | } |
|
149 | |||
150 | /* |
||
151 | * Product, ProductClass が persist される前に TaxRuleEventSubscriber によってアクセスされる |
||
152 | * 場合があるため、ID の存在もチェックする. |
||
153 | * https://github.com/EC-CUBE/ec-cube/issues/677 |
||
154 | */ |
||
155 | |||
156 | // Product |
||
157 | 215 | if ($Product && $productId > 0) { |
|
158 | $qb->andWhere('t.Product IS NULL OR t.Product = :Product'); |
||
159 | 2 | $parameters['Product'] = $Product; |
|
160 | } else { |
||
161 | $qb->andWhere('t.Product IS NULL'); |
||
162 | 2 | } |
|
163 | |||
164 | // ProductClass |
||
165 | 215 | if ($ProductClass && $productClassId > 0) { |
|
166 | $qb->andWhere('t.ProductClass IS NULL OR t.ProductClass = :ProductClass'); |
||
167 | 1 | $parameters['ProductClass'] = $ProductClass; |
|
168 | } else { |
||
169 | $qb->andWhere('t.ProductClass IS NULL'); |
||
170 | 1 | } |
|
171 | |||
172 | $TaxRules = $qb |
||
173 | 215 | ->setParameters($parameters) |
|
174 | 215 | ->orderBy('t.apply_date', 'DESC') // 実際は usort() でソートする |
|
175 | 215 | ->getQuery() |
|
176 | ->getResult(); |
||
177 | |||
178 | // 地域設定を優先するが、システムパラメーターなどに設定を持っていくか |
||
179 | // 後に書いてあるほど優先される |
||
180 | $priorityKeys = explode(',', $this->app['config']['tax_rule_priority']); |
||
181 | 215 | $priorityKeys = array(); |
|
182 | foreach (explode(',', $this->app['config']['tax_rule_priority']) as $key) { |
||
183 | $priorityKeys[] = str_replace('_', '', preg_replace('/_id\z/', '', $key)); |
||
184 | } |
||
185 | |||
186 | foreach ($TaxRules as $TaxRule) { |
||
187 | 215 | $rank = 0; |
|
188 | foreach ($priorityKeys as $index => $key) { |
||
189 | $arrayProperties = array_change_key_case($TaxRule->toArray()); |
||
190 | if ($arrayProperties[$key]) { |
||
191 | |||
192 | // 配列の数値添字を重みとして利用する |
||
193 | 4 | $rank += 1 << ($index + 1); |
|
194 | } |
||
195 | } |
||
196 | $TaxRule->setRank($rank); |
||
197 | } |
||
198 | |||
199 | // 適用日降順, rank 降順にソートする |
||
200 | usort($TaxRules, function($a, $b) { |
||
201 | return $a->compareTo($b); |
||
202 | }); |
||
203 | |||
204 | 215 | if (!empty($TaxRules)) { |
|
205 | 215 | $this->rules[$cacheKey] = $TaxRules[0]; |
|
206 | |||
207 | 215 | return $TaxRules[0]; |
|
208 | } else { |
||
209 | throw new NoResultException(); |
||
210 | } |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * getList |
||
215 | * |
||
216 | * @return array|null |
||
217 | */ |
||
218 | 3 | public function getList() |
|
229 | |||
230 | /** |
||
231 | * getById |
||
232 | * |
||
233 | * @param int $id |
||
234 | * @return array |
||
235 | */ |
||
236 | 1 | public function getById($id) |
|
244 | |||
245 | /** |
||
246 | * getByTime |
||
247 | * |
||
248 | * @param string $applyDate |
||
249 | * @return mixed |
||
250 | */ |
||
251 | public function getByTime($applyDate) |
||
259 | |||
260 | /** |
||
261 | * 税規約の削除. |
||
262 | * |
||
263 | * @param int|\Eccube\Entity\TaxRule $TaxRule 税規約 |
||
264 | * @return void |
||
265 | * @throws NoResultException |
||
266 | */ |
||
267 | 2 | public function delete($TaxRule) |
|
280 | |||
281 | /** |
||
282 | * TaxRule のキャッシュをクリアする. |
||
283 | * |
||
284 | * getByRule() をコールすると、結果をキャッシュし、2回目以降はデータベースへアクセスしない. |
||
285 | * このメソッドをコールすると、キャッシュをクリアし、再度データベースを参照して結果を取得する. |
||
286 | */ |
||
287 | 7 | public function clearCache() |
|
291 | } |
||
292 |