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\Controller\Admin\Product; |
26
|
|
|
|
27
|
|
|
use Eccube\Application; |
28
|
|
|
use Eccube\Common\Constant; |
29
|
|
|
use Eccube\Entity\Category; |
30
|
|
|
use Eccube\Entity\Product; |
31
|
|
|
use Eccube\Entity\ProductCategory; |
32
|
|
|
use Eccube\Entity\ProductClass; |
33
|
|
|
use Eccube\Entity\ProductImage; |
34
|
|
|
use Eccube\Entity\ProductStock; |
35
|
|
|
use Eccube\Exception\CsvImportException; |
36
|
|
|
use Eccube\Service\CsvImportService; |
37
|
|
|
use Eccube\Util\Str; |
38
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
39
|
|
|
use Symfony\Component\HttpFoundation\Request; |
40
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
41
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
class CsvImportController |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
|
47
|
|
|
private $errors = array(); |
48
|
|
|
|
49
|
|
|
private $fileName; |
50
|
|
|
|
51
|
|
|
private $em; |
52
|
|
|
|
53
|
|
|
private $productTwig = 'Product/csv_product.twig'; |
54
|
|
|
|
55
|
|
|
private $categoryTwig = 'Product/csv_category.twig'; |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
|
|
|
|
59
|
|
|
* 商品登録CSVアップロード |
60
|
|
|
*/ |
|
|
|
|
61
|
|
|
public function csvProduct(Application $app, Request $request) |
62
|
|
|
{ |
63
|
|
|
$form = $app['form.factory']->createBuilder('admin_csv_import')->getForm(); |
64
|
|
|
|
65
|
|
|
$headers = $this->getProductCsvHeader(); |
66
|
|
|
|
67
|
|
|
if ('POST' === $request->getMethod()) { |
|
|
|
|
68
|
|
|
|
69
|
|
|
$form->handleRequest($request); |
70
|
|
|
|
71
|
|
|
if ($form->isValid()) { |
|
|
|
|
72
|
|
|
|
73
|
|
|
$formFile = $form['import_file']->getData(); |
74
|
|
|
|
75
|
|
|
if (!empty($formFile)) { |
|
|
|
|
76
|
|
|
|
77
|
|
|
$data = $this->getImportData($app, $formFile); |
78
|
|
|
if ($data === false) { |
79
|
|
|
$this->addErrors('CSVのフォーマットが一致しません。'); |
80
|
|
|
return $this->render($app, $form, $headers, $this->productTwig); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$keys = array_keys($headers); |
84
|
|
|
$columnHeaders = $data->getColumnHeaders(); |
85
|
|
|
if ($keys !== $columnHeaders) { |
86
|
|
|
$this->addErrors('CSVのフォーマットが一致しません。'); |
87
|
|
|
return $this->render($app, $form, $headers, $this->productTwig); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$size = count($data); |
91
|
|
|
if ($size < 1) { |
92
|
|
|
$this->addErrors('CSVデータが存在しません。'); |
93
|
|
|
return $this->render($app, $form, $headers, $this->productTwig); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$headerSize = count($keys); |
97
|
|
|
|
98
|
|
|
$this->em = $app['orm.em']; |
99
|
|
|
$this->em->getConfiguration()->setSQLLogger(null); |
100
|
|
|
|
101
|
|
|
$this->em->getConnection()->beginTransaction(); |
102
|
|
|
|
103
|
|
|
$BaseInfo = $app['eccube.repository.base_info']->get(); |
104
|
|
|
|
105
|
|
|
// CSVファイルの登録処理 |
106
|
|
|
foreach ($data as $row) { |
|
|
|
|
107
|
|
|
|
108
|
|
|
if ($headerSize != count($row)) { |
109
|
|
|
$this->addErrors(($data->key() + 1) . '行目のCSVフォーマットが一致しません。'); |
|
|
|
|
110
|
|
|
return $this->render($app, $form, $headers, $this->productTwig); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ($row['商品ID'] == '') { |
114
|
|
|
$Product = new Product(); |
115
|
|
|
$this->em->persist($Product); |
116
|
|
|
} else { |
117
|
|
|
if (is_numeric($row['商品ID'])) { |
118
|
|
|
$Product = $app['eccube.repository.product']->find($row['商品ID']); |
119
|
|
|
if (!$Product) { |
120
|
|
|
$this->addErrors(($data->key() + 1) . '行目の商品IDが存在しません。'); |
|
|
|
|
121
|
|
|
return $this->render($app, $form, $headers, $this->productTwig); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
} else { |
124
|
|
|
$this->addErrors(($data->key() + 1) . '行目の商品IDが存在しません。'); |
|
|
|
|
125
|
|
|
return $this->render($app, $form, $headers, $this->productTwig); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($row['公開ステータス(ID)'] == '') { |
131
|
|
|
$this->addErrors(($data->key() + 1) . '行目の公開ステータス(ID)が設定されていません。'); |
|
|
|
|
132
|
|
|
} else { |
133
|
|
|
if (is_numeric($row['公開ステータス(ID)'])) { |
134
|
|
|
$Disp = $app['eccube.repository.master.disp']->find($row['公開ステータス(ID)']); |
135
|
|
|
if (!$Disp) { |
136
|
|
|
$this->addErrors(($data->key() + 1) . '行目の公開ステータス(ID)が存在しません。'); |
|
|
|
|
137
|
|
|
} else { |
138
|
|
|
$Product->setStatus($Disp); |
139
|
|
|
} |
140
|
|
|
} else { |
141
|
|
|
$this->addErrors(($data->key() + 1) . '行目の公開ステータス(ID)が存在しません。'); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
View Code Duplication |
if (Str::isBlank($row['商品名'])) { |
|
|
|
|
146
|
|
|
$this->addErrors(($data->key() + 1) . '行目の商品名が設定されていません。'); |
|
|
|
|
147
|
|
|
return $this->render($app, $form, $headers, $this->productTwig); |
|
|
|
|
148
|
|
|
} else { |
149
|
|
|
$Product->setName(Str::trimAll($row['商品名'])); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
View Code Duplication |
if (Str::isNotBlank($row['ショップ用メモ欄'])) { |
|
|
|
|
153
|
|
|
$Product->setNote(Str::trimAll($row['ショップ用メモ欄'])); |
154
|
|
|
} else { |
155
|
|
|
$Product->setNote(null); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
View Code Duplication |
if (Str::isNotBlank($row['商品説明(一覧)'])) { |
|
|
|
|
159
|
|
|
$Product->setDescriptionList(Str::trimAll($row['商品説明(一覧)'])); |
160
|
|
|
} else { |
161
|
|
|
$Product->setDescriptionList(null); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
View Code Duplication |
if (Str::isNotBlank($row['商品説明(詳細)'])) { |
|
|
|
|
165
|
|
|
$Product->setDescriptionDetail(Str::trimAll($row['商品説明(詳細)'])); |
166
|
|
|
} else { |
167
|
|
|
$Product->setDescriptionDetail(null); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
View Code Duplication |
if (Str::isNotBlank($row['検索ワード'])) { |
|
|
|
|
171
|
|
|
$Product->setSearchWord(Str::trimAll($row['検索ワード'])); |
172
|
|
|
} else { |
173
|
|
|
$Product->setSearchWord(null); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
View Code Duplication |
if (Str::isNotBlank($row['フリーエリア'])) { |
|
|
|
|
177
|
|
|
$Product->setFreeArea(Str::trimAll($row['フリーエリア'])); |
178
|
|
|
} else { |
179
|
|
|
$Product->setFreeArea(null); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if ($row['商品削除フラグ'] == '') { |
183
|
|
|
$Product->setDelFlg(Constant::DISABLED); |
184
|
|
|
} else { |
185
|
|
|
if ($row['商品削除フラグ'] == (string)Constant::DISABLED || $row['商品削除フラグ'] == (string)Constant::ENABLED) { |
|
|
|
|
186
|
|
|
$Product->setDelFlg($row['商品削除フラグ']); |
187
|
|
|
} else { |
188
|
|
|
$this->addErrors(($data->key() + 1) . '行目の商品削除フラグが設定されていません。'); |
|
|
|
|
189
|
|
|
return $this->render($app, $form, $headers, $this->productTwig); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// 商品画像登録 |
194
|
|
|
$this->createProductImage($row, $Product); |
195
|
|
|
|
196
|
|
|
$this->em->flush($Product); |
197
|
|
|
|
198
|
|
|
// 商品カテゴリ登録 |
199
|
|
|
$this->createProductCategory($row, $Product, $app, $data); |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
// 商品規格が存在しなければ新規登録 |
203
|
|
|
$ProductClasses = $Product->getProductClasses(); |
204
|
|
|
if ($ProductClasses->count() < 1) { |
205
|
|
|
// 規格分類1(ID)がセットされていると規格なし商品、規格あり商品を作成 |
206
|
|
|
$ProductClassOrg = $this->createProductClass($row, $Product, $app, $data); |
207
|
|
|
if ($BaseInfo->getOptionProductDeliveryFee() == Constant::ENABLED) { |
208
|
|
|
if ($row['送料'] != '') { |
209
|
|
|
$deliveryFee = str_replace(',', '', $row['送料']); |
210
|
|
|
if (is_numeric($deliveryFee) && $deliveryFee >= 0) { |
211
|
|
|
$ProductClassOrg->setDeliveryFee($deliveryFee); |
212
|
|
|
} else { |
213
|
|
|
$this->addErrors(($data->key() + 1) . '行目の送料は0以上の数値を設定してください。'); |
|
|
|
|
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
if ($row['規格分類1(ID)'] != '') { |
|
|
|
|
219
|
|
|
|
220
|
|
|
if ($row['規格分類1(ID)'] == $row['規格分類2(ID)']) { |
221
|
|
|
$this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)と規格分類2(ID)には同じ値を使用できません。'); |
|
|
|
|
222
|
|
|
} else { |
223
|
|
|
// 商品規格あり |
224
|
|
|
// 企画分類あり商品を作成 |
225
|
|
|
$ProductClass = clone $ProductClassOrg; |
226
|
|
|
$ProductStock = clone $ProductClassOrg->getProductStock(); |
227
|
|
|
|
228
|
|
|
// 規格分類1、規格分類2がnullであるデータの削除フラグを1にセット |
229
|
|
|
$ProductClassOrg->setDelFlg(Constant::ENABLED); |
230
|
|
|
|
231
|
|
|
// 規格分類1、2をそれぞれセットし作成 |
232
|
|
|
$ClassCategory1 = null; |
233
|
|
|
if (is_numeric($row['規格分類1(ID)'])) { |
234
|
|
|
$ClassCategory1 = $app['eccube.repository.class_category']->find($row['規格分類1(ID)']); |
235
|
|
|
if (!$ClassCategory1) { |
236
|
|
|
$this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。'); |
|
|
|
|
237
|
|
|
} else { |
238
|
|
|
$ProductClass->setClassCategory1($ClassCategory1); |
239
|
|
|
} |
240
|
|
|
} else { |
241
|
|
|
$this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。'); |
|
|
|
|
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
if ($row['規格分類2(ID)'] != '') { |
245
|
|
|
if (is_numeric($row['規格分類2(ID)'])) { |
246
|
|
|
$ClassCategory2 = $app['eccube.repository.class_category']->find($row['規格分類2(ID)']); |
247
|
|
View Code Duplication |
if (!$ClassCategory2) { |
|
|
|
|
248
|
|
|
$this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。'); |
|
|
|
|
249
|
|
|
} else { |
250
|
|
|
if ($ClassCategory1 && |
251
|
|
|
($ClassCategory1->getClassName()->getId() == $ClassCategory2->getClassName()->getId()) |
252
|
|
|
) { |
253
|
|
|
$this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)と規格分類2(ID)の規格名が同じです。'); |
|
|
|
|
254
|
|
|
} else { |
255
|
|
|
$ProductClass->setClassCategory2($ClassCategory2); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
} else { |
259
|
|
|
$this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。'); |
|
|
|
|
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
$ProductClass->setProductStock($ProductStock); |
263
|
|
|
$ProductStock->setProductClass($ProductClass); |
264
|
|
|
|
265
|
|
|
$this->em->persist($ProductClass); |
266
|
|
|
$this->em->persist($ProductStock); |
267
|
|
|
} |
268
|
|
|
|
|
|
|
|
269
|
|
|
} else { |
270
|
|
|
if ($row['規格分類2(ID)'] != '') { |
271
|
|
|
$this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。'); |
|
|
|
|
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
|
|
|
|
275
|
|
|
} else { |
276
|
|
|
// 商品規格の更新 |
277
|
|
|
|
278
|
|
|
$flag = false; |
279
|
|
|
$classCategoryId1 = $row['規格分類1(ID)'] == '' ? null : $row['規格分類1(ID)']; |
280
|
|
|
$classCategoryId2 = $row['規格分類2(ID)'] == '' ? null : $row['規格分類2(ID)']; |
281
|
|
|
|
282
|
|
|
foreach ($ProductClasses as $pc) { |
|
|
|
|
283
|
|
|
|
284
|
|
|
$classCategory1 = is_null($pc->getClassCategory1()) ? null : $pc->getClassCategory1()->getId(); |
285
|
|
|
$classCategory2 = is_null($pc->getClassCategory2()) ? null : $pc->getClassCategory2()->getId(); |
286
|
|
|
|
287
|
|
|
// 登録されている商品規格を更新 |
288
|
|
|
if ($classCategory1 == $classCategoryId1 && |
289
|
|
|
$classCategory2 == $classCategoryId2 |
290
|
|
|
) { |
291
|
|
|
$this->updateProductClass($row, $Product, $pc, $app, $data); |
292
|
|
|
|
293
|
|
|
if ($BaseInfo->getOptionProductDeliveryFee() == Constant::ENABLED) { |
294
|
|
|
if ($row['送料'] != '') { |
295
|
|
|
$deliveryFee = str_replace(',', '', $row['送料']); |
296
|
|
|
if (is_numeric($deliveryFee) && $deliveryFee >= 0) { |
297
|
|
|
$pc->setDeliveryFee($deliveryFee); |
298
|
|
|
} else { |
299
|
|
|
$this->addErrors(($data->key() + 1) . '行目の送料は0以上の数値を設定してください。'); |
|
|
|
|
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
$flag = true; |
305
|
|
|
break; |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
// 商品規格を登録 |
310
|
|
|
if (!$flag) { |
311
|
|
|
$pc = $ProductClasses[0]; |
312
|
|
|
if ($pc->getClassCategory1() == null && |
313
|
|
|
$pc->getClassCategory2() == null |
314
|
|
|
) { |
|
|
|
|
315
|
|
|
|
316
|
|
|
// 規格分類1、規格分類2がnullであるデータの削除フラグを1にセット |
317
|
|
|
$pc->setDelFlg(Constant::ENABLED); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
if ($row['規格分類1(ID)'] == $row['規格分類2(ID)']) { |
321
|
|
|
$this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)と規格分類2(ID)には同じ値を使用できません。'); |
|
|
|
|
322
|
|
|
} else { |
|
|
|
|
323
|
|
|
|
324
|
|
|
// 必ず規格分類1がセットされている |
325
|
|
|
// 規格分類1、2をそれぞれセットし作成 |
326
|
|
|
$ClassCategory1 = null; |
327
|
|
|
if (is_numeric($classCategoryId1)) { |
328
|
|
|
$ClassCategory1 = $app['eccube.repository.class_category']->find($classCategoryId1); |
329
|
|
|
if (!$ClassCategory1) { |
330
|
|
|
$this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。'); |
|
|
|
|
331
|
|
|
} |
332
|
|
|
} else { |
333
|
|
|
$this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。'); |
|
|
|
|
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
$ClassCategory2 = null; |
337
|
|
|
if ($row['規格分類2(ID)'] != '') { |
338
|
|
|
if ($pc->getClassCategory1() != null && $pc->getClassCategory2() == null) { |
339
|
|
|
$this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)は設定できません。'); |
|
|
|
|
340
|
|
|
} else { |
341
|
|
|
if (is_numeric($classCategoryId2)) { |
342
|
|
|
$ClassCategory2 = $app['eccube.repository.class_category']->find($classCategoryId2); |
343
|
|
View Code Duplication |
if (!$ClassCategory2) { |
|
|
|
|
344
|
|
|
$this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。'); |
|
|
|
|
345
|
|
|
} else { |
346
|
|
|
if ($ClassCategory1 && |
347
|
|
|
($ClassCategory1->getClassName()->getId() == $ClassCategory2->getClassName()->getId()) |
348
|
|
|
) { |
349
|
|
|
$this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)と規格分類2(ID)の規格名が同じです。'); |
|
|
|
|
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
} else { |
353
|
|
|
$this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。'); |
|
|
|
|
354
|
|
|
} |
355
|
|
|
|
|
|
|
|
356
|
|
|
} |
357
|
|
|
} else { |
358
|
|
|
if ($pc->getClassCategory1() != null && $pc->getClassCategory2() != null) { |
359
|
|
|
$this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)に値を設定してください。'); |
|
|
|
|
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
$ProductClass = $this->createProductClass($row, $Product, $app, $data, $ClassCategory1, $ClassCategory2); |
363
|
|
|
|
364
|
|
|
if ($BaseInfo->getOptionProductDeliveryFee() == Constant::ENABLED) { |
365
|
|
|
if ($row['送料'] != '') { |
366
|
|
|
$deliveryFee = str_replace(',', '', $row['送料']); |
367
|
|
|
if (is_numeric($deliveryFee) && $deliveryFee >= 0) { |
368
|
|
|
$ProductClass->setDeliveryFee($deliveryFee); |
369
|
|
|
} else { |
370
|
|
|
$this->addErrors(($data->key() + 1) . '行目の送料は0以上の数値を設定してください。'); |
|
|
|
|
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
$Product->addProductClass($ProductClass); |
376
|
|
|
} |
377
|
|
|
|
|
|
|
|
378
|
|
|
} |
379
|
|
|
|
|
|
|
|
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
|
383
|
|
|
if ($this->hasErrors()) { |
384
|
|
|
return $this->render($app, $form, $headers, $this->productTwig); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
$this->em->persist($Product); |
388
|
|
|
|
|
|
|
|
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
$this->em->flush(); |
392
|
|
|
$this->em->getConnection()->commit(); |
393
|
|
|
|
394
|
|
|
$app->addSuccess('admin.product.csv_import.save.complete', 'admin'); |
395
|
|
|
} |
396
|
|
|
|
|
|
|
|
397
|
|
|
} |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
return $this->render($app, $form, $headers, $this->productTwig); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
|
|
|
|
404
|
|
|
* カテゴリ登録CSVアップロード |
405
|
|
|
*/ |
|
|
|
|
406
|
|
|
public function csvCategory(Application $app, Request $request) |
407
|
|
|
{ |
408
|
|
|
|
409
|
|
|
$form = $app['form.factory']->createBuilder('admin_csv_import')->getForm(); |
410
|
|
|
|
411
|
|
|
$headers = $this->getCategoryCsvHeader(); |
412
|
|
|
|
413
|
|
|
if ('POST' === $request->getMethod()) { |
|
|
|
|
414
|
|
|
|
415
|
|
|
$form->handleRequest($request); |
416
|
|
|
|
417
|
|
|
if ($form->isValid()) { |
|
|
|
|
418
|
|
|
|
419
|
|
|
$formFile = $form['import_file']->getData(); |
420
|
|
|
|
421
|
|
|
if (!empty($formFile)) { |
|
|
|
|
422
|
|
|
|
423
|
|
|
$data = $this->getImportData($app, $formFile); |
424
|
|
View Code Duplication |
if ($data === false) { |
|
|
|
|
425
|
|
|
$this->addErrors('CSVのフォーマットが一致しません。'); |
426
|
|
|
return $this->render($app, $form, $headers, $this->categoryTwig); |
|
|
|
|
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
$keys = array_keys($headers); |
430
|
|
|
$columnHeaders = $data->getColumnHeaders(); |
431
|
|
|
if ($keys !== $columnHeaders) { |
432
|
|
|
$this->addErrors('CSVのフォーマットが一致しません。'); |
433
|
|
|
return $this->render($app, $form, $headers, $this->categoryTwig); |
|
|
|
|
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
$size = count($data); |
437
|
|
|
if ($size < 1) { |
438
|
|
|
$this->addErrors('CSVデータが存在しません。'); |
439
|
|
|
return $this->render($app, $form, $headers, $this->categoryTwig); |
|
|
|
|
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
$headerSize = count($keys); |
443
|
|
|
|
444
|
|
|
$this->em = $app['orm.em']; |
445
|
|
|
$this->em->getConfiguration()->setSQLLogger(null); |
446
|
|
|
|
447
|
|
|
$this->em->getConnection()->beginTransaction(); |
448
|
|
|
|
449
|
|
|
// CSVファイルの登録処理 |
450
|
|
|
foreach ($data as $row) { |
|
|
|
|
451
|
|
|
|
452
|
|
|
if ($headerSize != count($row)) { |
453
|
|
|
$this->addErrors(($data->key() + 1) . '行目のCSVフォーマットが一致しません。'); |
|
|
|
|
454
|
|
|
return $this->render($app, $form, $headers, $this->categoryTwig); |
|
|
|
|
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
if ($row['カテゴリID'] == '') { |
458
|
|
|
$Category = new Category(); |
459
|
|
|
} else { |
460
|
|
View Code Duplication |
if (!is_numeric($row['カテゴリID'])) { |
|
|
|
|
461
|
|
|
$this->addErrors(($data->key() + 1) . '行目のカテゴリIDが存在しません。'); |
|
|
|
|
462
|
|
|
return $this->render($app, $form, $headers, $this->categoryTwig); |
|
|
|
|
463
|
|
|
} |
464
|
|
|
$Category = $app['eccube.repository.category']->find($row['カテゴリID']); |
465
|
|
View Code Duplication |
if (!$Category) { |
|
|
|
|
466
|
|
|
$this->addErrors(($data->key() + 1) . '行目のカテゴリIDが存在しません。'); |
|
|
|
|
467
|
|
|
return $this->render($app, $form, $headers, $this->categoryTwig); |
|
|
|
|
468
|
|
|
} |
469
|
|
View Code Duplication |
if ($row['カテゴリID'] == $row['親カテゴリID']) { |
|
|
|
|
470
|
|
|
$this->addErrors(($data->key() + 1) . '行目のカテゴリIDと親カテゴリIDが同じです。'); |
|
|
|
|
471
|
|
|
return $this->render($app, $form, $headers, $this->categoryTwig); |
|
|
|
|
472
|
|
|
} |
473
|
|
|
|
|
|
|
|
474
|
|
|
} |
475
|
|
|
|
476
|
|
View Code Duplication |
if (Str::isBlank($row['カテゴリ名'])) { |
|
|
|
|
477
|
|
|
$this->addErrors(($data->key() + 1) . '行目のカテゴリ名が設定されていません。'); |
|
|
|
|
478
|
|
|
return $this->render($app, $form, $headers, $this->categoryTwig); |
|
|
|
|
479
|
|
|
} else { |
480
|
|
|
$Category->setName(Str::trimAll($row['カテゴリ名'])); |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
if ($row['親カテゴリID'] != '') { |
|
|
|
|
484
|
|
|
|
485
|
|
View Code Duplication |
if (!is_numeric($row['親カテゴリID'])) { |
|
|
|
|
486
|
|
|
$this->addErrors(($data->key() + 1) . '行目の親カテゴリIDが存在しません。'); |
|
|
|
|
487
|
|
|
return $this->render($app, $form, $headers, $this->categoryTwig); |
|
|
|
|
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
$ParentCategory = $app['eccube.repository.category']->find($row['親カテゴリID']); |
491
|
|
View Code Duplication |
if (!$ParentCategory) { |
|
|
|
|
492
|
|
|
$this->addErrors(($data->key() + 1) . '行目の親カテゴリIDが存在しません。'); |
|
|
|
|
493
|
|
|
return $this->render($app, $form, $headers, $this->categoryTwig); |
|
|
|
|
494
|
|
|
} |
495
|
|
|
|
|
|
|
|
496
|
|
|
} else { |
497
|
|
|
$ParentCategory = null; |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
$Category->setParent($ParentCategory); |
501
|
|
|
if ($ParentCategory) { |
502
|
|
|
$Category->setLevel($ParentCategory->getLevel() + 1); |
503
|
|
|
} else { |
504
|
|
|
$Category->setLevel(1); |
505
|
|
|
} |
506
|
|
|
|
507
|
|
View Code Duplication |
if ($app['config']['category_nest_level'] < $Category->getLevel()) { |
|
|
|
|
508
|
|
|
$this->addErrors(($data->key() + 1) . '行目のカテゴリが最大レベルを超えているため設定できません。'); |
|
|
|
|
509
|
|
|
return $this->render($app, $form, $headers, $this->categoryTwig); |
|
|
|
|
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
$status = $app['eccube.repository.category']->save($Category); |
513
|
|
|
|
514
|
|
|
if (!$status) { |
515
|
|
|
$this->addErrors(($data->key() + 1) . '行目のカテゴリが設定できません。'); |
|
|
|
|
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
if ($this->hasErrors()) { |
519
|
|
|
return $this->render($app, $form, $headers, $this->categoryTwig); |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
$this->em->persist($Category); |
523
|
|
|
|
|
|
|
|
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
$this->em->flush(); |
527
|
|
|
$this->em->getConnection()->commit(); |
528
|
|
|
|
529
|
|
|
$app->addSuccess('admin.category.csv_import.save.complete', 'admin'); |
530
|
|
|
} |
531
|
|
|
|
|
|
|
|
532
|
|
|
} |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
return $this->render($app, $form, $headers, $this->categoryTwig); |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
|
539
|
|
|
/** |
|
|
|
|
540
|
|
|
* アップロード用CSV雛形ファイルダウンロード |
541
|
|
|
*/ |
|
|
|
|
542
|
|
|
public function csvTemplate(Application $app, Request $request, $type) |
543
|
|
|
{ |
544
|
|
|
set_time_limit(0); |
545
|
|
|
|
546
|
|
|
$response = new StreamedResponse(); |
547
|
|
|
|
548
|
|
|
if ($type == 'product') { |
549
|
|
|
$headers = $this->getProductCsvHeader(); |
550
|
|
|
$filename = 'product.csv'; |
551
|
|
|
} else if ($type == 'category') { |
552
|
|
|
$headers = $this->getCategoryCsvHeader(); |
553
|
|
|
$filename = 'category.csv'; |
554
|
|
|
} else { |
555
|
|
|
throw new NotFoundHttpException(); |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
$response->setCallback(function () use ($app, $request, $headers) { |
559
|
|
|
|
560
|
|
|
// ヘッダ行の出力 |
561
|
|
|
$row = array(); |
562
|
|
|
foreach ($headers as $key => $value) { |
563
|
|
|
$row[] = mb_convert_encoding($key, $app['config']['csv_export_encoding'], 'UTF-8'); |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
$fp = fopen('php://output', 'w'); |
567
|
|
|
fputcsv($fp, $row, $app['config']['csv_export_separator']); |
568
|
|
|
fclose($fp); |
569
|
|
|
|
570
|
|
|
}); |
571
|
|
|
|
572
|
|
|
$response->headers->set('Content-Type', 'application/octet-stream'); |
573
|
|
|
$response->headers->set('Content-Disposition', 'attachment; filename=' . $filename); |
|
|
|
|
574
|
|
|
$response->send(); |
575
|
|
|
|
576
|
|
|
return $response; |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
|
580
|
|
|
/** |
581
|
|
|
* 登録、更新時のエラー画面表示 |
582
|
|
|
* |
583
|
|
|
*/ |
584
|
|
|
protected function render($app, $form, $headers, $twig) |
585
|
|
|
{ |
586
|
|
|
|
587
|
|
|
if ($this->hasErrors()) { |
588
|
|
|
if ($this->em) { |
589
|
|
|
$this->em->getConnection()->rollback(); |
590
|
|
|
} |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
if (!empty($this->fileName)) { |
594
|
|
|
try { |
595
|
|
|
$fs = new Filesystem(); |
596
|
|
|
$fs->remove($app['config']['csv_temp_realdir'] . '/' . $this->fileName); |
|
|
|
|
597
|
|
|
} catch (\Exception $e) { |
598
|
|
|
// エラーが発生しても無視する |
599
|
|
|
} |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
return $app->render($twig, array( |
603
|
|
|
'form' => $form->createView(), |
604
|
|
|
'headers' => $headers, |
605
|
|
|
'errors' => $this->errors, |
606
|
|
|
)); |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
|
610
|
|
|
/** |
611
|
|
|
* アップロードされたCSVファイルの行ごとの処理 |
612
|
|
|
* |
613
|
|
|
* @param $formFile |
614
|
|
|
* @return CsvImportService |
615
|
|
|
*/ |
616
|
|
|
protected function getImportData($app, $formFile) |
617
|
|
|
{ |
618
|
|
|
// アップロードされたCSVファイルを一時ディレクトリに保存 |
619
|
|
|
$this->fileName = 'upload_' . Str::random() . '.' . $formFile->getClientOriginalExtension(); |
|
|
|
|
620
|
|
|
$formFile->move($app['config']['csv_temp_realdir'], $this->fileName); |
621
|
|
|
|
622
|
|
|
$file = file_get_contents($app['config']['csv_temp_realdir'] . '/' . $this->fileName); |
|
|
|
|
623
|
|
|
// アップロードされたファイルがUTF-8以外は文字コード変換を行う |
624
|
|
|
$encode = Str::characterEncoding(substr($file, 0, 6)); |
625
|
|
|
if ($encode != 'UTF-8') { |
626
|
|
|
$file = mb_convert_encoding($file, 'UTF-8', $encode); |
627
|
|
|
} |
628
|
|
|
$file = Str::convertLineFeed($file); |
629
|
|
|
|
630
|
|
|
$tmp = tmpfile(); |
631
|
|
|
fwrite($tmp, $file); |
632
|
|
|
rewind($tmp); |
633
|
|
|
$meta = stream_get_meta_data($tmp); |
634
|
|
|
$file = new \SplFileObject($meta['uri']); |
635
|
|
|
|
636
|
|
|
set_time_limit(0); |
637
|
|
|
|
638
|
|
|
// アップロードされたCSVファイルを行ごとに取得 |
639
|
|
|
$data = new CsvImportService($file, $app['config']['csv_import_delimiter'], $app['config']['csv_import_enclosure']); |
640
|
|
|
|
641
|
|
|
$ret = $data->setHeaderRowNumber(0); |
642
|
|
|
|
643
|
|
|
return ($ret !== false) ? $data : false; |
|
|
|
|
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
|
647
|
|
|
/** |
648
|
|
|
* 商品画像の削除、登録 |
649
|
|
|
*/ |
650
|
|
|
protected function createProductImage($row, Product $Product) |
651
|
|
|
{ |
652
|
|
|
if ($row['商品画像'] != '') { |
|
|
|
|
653
|
|
|
|
654
|
|
|
// 画像の削除 |
655
|
|
|
$ProductImages = $Product->getProductImage(); |
656
|
|
|
foreach ($ProductImages as $ProductImage) { |
657
|
|
|
$Product->removeProductImage($ProductImage); |
658
|
|
|
$this->em->remove($ProductImage); |
659
|
|
|
} |
660
|
|
|
|
661
|
|
|
// 画像の登録 |
662
|
|
|
$images = explode(',', $row['商品画像']); |
663
|
|
|
$rank = 1; |
664
|
|
|
foreach ($images as $image) { |
|
|
|
|
665
|
|
|
|
666
|
|
|
$ProductImage = new ProductImage(); |
667
|
|
|
$ProductImage->setFileName(Str::trimAll($image)); |
668
|
|
|
$ProductImage->setProduct($Product); |
669
|
|
|
$ProductImage->setRank($rank); |
670
|
|
|
|
671
|
|
|
$Product->addProductImage($ProductImage); |
672
|
|
|
$rank++; |
673
|
|
|
$this->em->persist($ProductImage); |
674
|
|
|
} |
675
|
|
|
} |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
|
679
|
|
|
/** |
680
|
|
|
* 商品カテゴリの削除、登録 |
681
|
|
|
*/ |
682
|
|
|
protected function createProductCategory($row, Product $Product, $app, $data) |
683
|
|
|
{ |
684
|
|
|
if ($row['商品カテゴリ(ID)'] != '') { |
685
|
|
|
// カテゴリの削除 |
686
|
|
|
$ProductCategories = $Product->getProductCategories(); |
687
|
|
|
foreach ($ProductCategories as $ProductCategory) { |
688
|
|
|
$Product->removeProductCategory($ProductCategory); |
689
|
|
|
$this->em->remove($ProductCategory); |
690
|
|
|
$this->em->flush($ProductCategory); |
691
|
|
|
} |
692
|
|
|
|
693
|
|
|
// カテゴリの登録 |
694
|
|
|
$categories = explode(',', $row['商品カテゴリ(ID)']); |
695
|
|
|
$rank = 1; |
696
|
|
|
foreach ($categories as $category) { |
|
|
|
|
697
|
|
|
|
698
|
|
|
if (is_numeric($category)) { |
699
|
|
|
$Category = $app['eccube.repository.category']->find($category); |
700
|
|
|
if (!$Category) { |
701
|
|
|
$this->addErrors(($data->key() + 1) . '行目の商品カテゴリ(ID)「' . $category . '」が存在しません。'); |
|
|
|
|
702
|
|
|
} else { |
703
|
|
|
$ProductCategory = new ProductCategory(); |
704
|
|
|
$ProductCategory->setProductId($Product->getId()); |
705
|
|
|
$ProductCategory->setCategoryId($Category->getId()); |
706
|
|
|
$ProductCategory->setProduct($Product); |
707
|
|
|
$ProductCategory->setCategory($Category); |
708
|
|
|
$ProductCategory->setRank($rank); |
709
|
|
|
$Product->addProductCategory($ProductCategory); |
710
|
|
|
$rank++; |
711
|
|
|
$this->em->persist($ProductCategory); |
712
|
|
|
} |
713
|
|
|
} else { |
714
|
|
|
$this->addErrors(($data->key() + 1) . '行目の商品カテゴリ(ID)「' . $category . '」が存在しません。'); |
|
|
|
|
715
|
|
|
} |
716
|
|
|
} |
717
|
|
|
|
|
|
|
|
718
|
|
|
} |
719
|
|
|
} |
720
|
|
|
|
721
|
|
|
|
722
|
|
|
/** |
723
|
|
|
* 商品規格分類1、商品規格分類2がnullとなる商品規格情報を作成 |
724
|
|
|
*/ |
725
|
|
|
protected function createProductClass($row, Product $Product, $app, $data, $ClassCategory1 = null, $ClassCategory2 = null) |
726
|
|
|
{ |
727
|
|
|
// 規格分類1、規格分類2がnullとなる商品を作成 |
728
|
|
|
|
729
|
|
|
$ProductClass = new ProductClass(); |
730
|
|
|
$ProductClass->setProduct($Product); |
731
|
|
|
|
732
|
|
|
|
733
|
|
View Code Duplication |
if ($row['商品種別(ID)'] == '') { |
|
|
|
|
734
|
|
|
$this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が設定されていません。'); |
|
|
|
|
735
|
|
|
} else { |
736
|
|
|
if (is_numeric($row['商品種別(ID)'])) { |
737
|
|
|
$ProductType = $app['eccube.repository.master.product_type']->find($row['商品種別(ID)']); |
738
|
|
|
if (!$ProductType) { |
739
|
|
|
$this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が存在しません。'); |
|
|
|
|
740
|
|
|
} else { |
741
|
|
|
$ProductClass->setProductType($ProductType); |
742
|
|
|
} |
743
|
|
|
} else { |
744
|
|
|
$this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が存在しません。'); |
|
|
|
|
745
|
|
|
} |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
$ProductClass->setClassCategory1($ClassCategory1); |
749
|
|
|
$ProductClass->setClassCategory2($ClassCategory2); |
750
|
|
|
|
751
|
|
|
if ($row['発送日目安(ID)'] != '') { |
752
|
|
|
if (is_numeric($row['発送日目安(ID)'])) { |
753
|
|
|
$DeliveryDate = $app['eccube.repository.delivery_date']->find($row['発送日目安(ID)']); |
754
|
|
|
if (!$DeliveryDate) { |
755
|
|
|
$this->addErrors(($data->key() + 1) . '行目の発送日目安(ID)が存在しません。'); |
|
|
|
|
756
|
|
|
} else { |
757
|
|
|
$ProductClass->setDeliveryDate($DeliveryDate); |
758
|
|
|
} |
759
|
|
|
} else { |
760
|
|
|
$this->addErrors(($data->key() + 1) . '行目の発送日目安(ID)が存在しません。'); |
|
|
|
|
761
|
|
|
} |
762
|
|
|
} |
763
|
|
|
|
764
|
|
View Code Duplication |
if (Str::isNotBlank($row['商品コード'])) { |
|
|
|
|
765
|
|
|
$ProductClass->setCode(Str::trimAll($row['商品コード'])); |
766
|
|
|
} else { |
767
|
|
|
$ProductClass->setCode(null); |
768
|
|
|
} |
769
|
|
|
|
770
|
|
View Code Duplication |
if ($row['在庫数無制限フラグ'] == '') { |
|
|
|
|
771
|
|
|
$this->addErrors(($data->key() + 1) . '行目の在庫数無制限フラグが設定されていません。'); |
|
|
|
|
772
|
|
|
} else { |
773
|
|
|
if ($row['在庫数無制限フラグ'] == (string) Constant::DISABLED) { |
774
|
|
|
$ProductClass->setStockUnlimited(Constant::DISABLED); |
775
|
|
|
// 在庫数が設定されていなければエラー |
776
|
|
|
if ($row['在庫数'] == '') { |
777
|
|
|
$this->addErrors(($data->key() + 1) . '行目の在庫数が設定されていません。'); |
|
|
|
|
778
|
|
|
} else { |
779
|
|
|
$stock = str_replace(',', '', $row['在庫数']); |
780
|
|
|
if (is_numeric($stock) && $stock >= 0) { |
781
|
|
|
$ProductClass->setStock($stock); |
782
|
|
|
} else { |
783
|
|
|
$this->addErrors(($data->key() + 1) . '行目の在庫数は0以上の数値を設定してください。'); |
|
|
|
|
784
|
|
|
} |
785
|
|
|
} |
786
|
|
|
|
|
|
|
|
787
|
|
|
} else if ($row['在庫数無制限フラグ'] == (string) Constant::ENABLED) { |
788
|
|
|
$ProductClass->setStockUnlimited(Constant::ENABLED); |
789
|
|
|
$ProductClass->setStock(null); |
790
|
|
|
} else { |
791
|
|
|
$this->addErrors(($data->key() + 1) . '行目の在庫数無制限フラグが設定されていません。'); |
|
|
|
|
792
|
|
|
} |
793
|
|
|
} |
794
|
|
|
|
795
|
|
|
if ($row['販売制限数'] != '') { |
796
|
|
|
$saleLimit = str_replace(',', '', $row['販売制限数']); |
797
|
|
|
if (is_numeric($saleLimit) && $saleLimit >= 0) { |
798
|
|
|
$ProductClass->setSaleLimit($saleLimit); |
799
|
|
|
} else { |
800
|
|
|
$this->addErrors(($data->key() + 1) . '行目の販売制限数は0以上の数値を設定してください。'); |
|
|
|
|
801
|
|
|
} |
802
|
|
|
} |
803
|
|
|
|
804
|
|
|
if ($row['通常価格'] != '') { |
805
|
|
|
$price01 = str_replace(',', '', $row['通常価格']); |
806
|
|
|
if (is_numeric($price01) && $price01 >= 0) { |
807
|
|
|
$ProductClass->setPrice01($price01); |
808
|
|
|
} else { |
809
|
|
|
$this->addErrors(($data->key() + 1) . '行目の通常価格は0以上の数値を設定してください。'); |
|
|
|
|
810
|
|
|
} |
811
|
|
|
} |
812
|
|
|
|
813
|
|
|
if ($row['販売価格'] == '') { |
814
|
|
|
$this->addErrors(($data->key() + 1) . '行目の販売価格が設定されていません。'); |
|
|
|
|
815
|
|
|
} else { |
816
|
|
|
$price02 = str_replace(',', '', $row['販売価格']); |
817
|
|
|
if (is_numeric($price02) && $price02 >= 0) { |
818
|
|
|
$ProductClass->setPrice02($price02); |
819
|
|
|
} else { |
820
|
|
|
$this->addErrors(($data->key() + 1) . '行目の販売価格は0以上の数値を設定してください。'); |
|
|
|
|
821
|
|
|
} |
822
|
|
|
} |
823
|
|
|
|
824
|
|
|
if ($row['送料'] != '') { |
825
|
|
|
$delivery_fee = str_replace(',', '', $row['送料']); |
826
|
|
|
if (is_numeric($delivery_fee) && $delivery_fee >= 0) { |
827
|
|
|
$ProductClass->setDeliveryFee($delivery_fee); |
828
|
|
|
} else { |
829
|
|
|
$this->addErrors(($data->key() + 1) . '行目の送料は0以上の数値を設定してください。'); |
|
|
|
|
830
|
|
|
} |
831
|
|
|
} |
832
|
|
|
|
833
|
|
View Code Duplication |
if ($row['商品規格削除フラグ'] == '') { |
|
|
|
|
834
|
|
|
$ProductClass->setDelFlg(Constant::DISABLED); |
835
|
|
|
} else { |
836
|
|
|
if ($row['商品規格削除フラグ'] == (string) Constant::DISABLED || $row['商品規格削除フラグ'] == (string) Constant::ENABLED) { |
837
|
|
|
$ProductClass->setDelFlg($row['商品規格削除フラグ']); |
838
|
|
|
} else { |
839
|
|
|
$this->addErrors(($data->key() + 1) . '行目の商品規格削除フラグが設定されていません。'); |
|
|
|
|
840
|
|
|
} |
841
|
|
|
} |
842
|
|
|
|
843
|
|
|
$Product->addProductClass($ProductClass); |
844
|
|
|
$ProductStock = new ProductStock(); |
845
|
|
|
$ProductClass->setProductStock($ProductStock); |
846
|
|
|
$ProductStock->setProductClass($ProductClass); |
847
|
|
|
|
848
|
|
|
if (!$ProductClass->getStockUnlimited()) { |
849
|
|
|
$ProductStock->setStock($ProductClass->getStock()); |
850
|
|
|
} else { |
851
|
|
|
// 在庫無制限時はnullを設定 |
852
|
|
|
$ProductStock->setStock(null); |
853
|
|
|
} |
854
|
|
|
|
855
|
|
|
$this->em->persist($ProductClass); |
856
|
|
|
$this->em->persist($ProductStock); |
857
|
|
|
|
858
|
|
|
return $ProductClass; |
859
|
|
|
|
860
|
|
|
} |
861
|
|
|
|
862
|
|
|
|
863
|
|
|
/** |
864
|
|
|
* 商品規格情報を更新 |
865
|
|
|
*/ |
866
|
|
|
protected function updateProductClass($row, Product $Product, ProductClass $ProductClass, $app, $data) |
867
|
|
|
{ |
868
|
|
|
|
869
|
|
|
$ProductClass->setProduct($Product); |
870
|
|
|
|
871
|
|
View Code Duplication |
if ($row['商品種別(ID)'] == '') { |
|
|
|
|
872
|
|
|
$this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が設定されていません。'); |
|
|
|
|
873
|
|
|
} else { |
874
|
|
|
if (is_numeric($row['商品種別(ID)'])) { |
875
|
|
|
$ProductType = $app['eccube.repository.master.product_type']->find($row['商品種別(ID)']); |
876
|
|
|
if (!$ProductType) { |
877
|
|
|
$this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が存在しません。'); |
|
|
|
|
878
|
|
|
} else { |
879
|
|
|
$ProductClass->setProductType($ProductType); |
880
|
|
|
} |
881
|
|
|
} else { |
882
|
|
|
$this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が存在しません。'); |
|
|
|
|
883
|
|
|
} |
884
|
|
|
} |
885
|
|
|
|
886
|
|
|
// 規格分類1、2をそれぞれセットし作成 |
887
|
|
|
if ($row['規格分類1(ID)'] != '') { |
888
|
|
|
if (is_numeric($row['規格分類1(ID)'])) { |
889
|
|
|
$ClassCategory = $app['eccube.repository.class_category']->find($row['規格分類1(ID)']); |
890
|
|
|
if (!$ClassCategory) { |
891
|
|
|
$this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。'); |
|
|
|
|
892
|
|
|
} else { |
893
|
|
|
$ProductClass->setClassCategory1($ClassCategory); |
894
|
|
|
} |
895
|
|
|
} else { |
896
|
|
|
$this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。'); |
|
|
|
|
897
|
|
|
} |
898
|
|
|
} |
899
|
|
|
|
900
|
|
|
if ($row['規格分類2(ID)'] != '') { |
901
|
|
|
if (is_numeric($row['規格分類2(ID)'])) { |
902
|
|
|
$ClassCategory = $app['eccube.repository.class_category']->find($row['規格分類2(ID)']); |
903
|
|
|
if (!$ClassCategory) { |
904
|
|
|
$this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。'); |
|
|
|
|
905
|
|
|
} else { |
906
|
|
|
$ProductClass->setClassCategory2($ClassCategory); |
907
|
|
|
} |
908
|
|
|
} else { |
909
|
|
|
$this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。'); |
|
|
|
|
910
|
|
|
} |
911
|
|
|
} |
912
|
|
|
|
913
|
|
|
if ($row['発送日目安(ID)'] != '') { |
914
|
|
|
if (is_numeric($row['発送日目安(ID)'])) { |
915
|
|
|
$DeliveryDate = $app['eccube.repository.delivery_date']->find($row['発送日目安(ID)']); |
916
|
|
|
if (!$DeliveryDate) { |
917
|
|
|
$this->addErrors(($data->key() + 1) . '行目の発送日目安(ID)が存在しません。'); |
|
|
|
|
918
|
|
|
} else { |
919
|
|
|
$ProductClass->setDeliveryDate($DeliveryDate); |
920
|
|
|
} |
921
|
|
|
} else { |
922
|
|
|
$this->addErrors(($data->key() + 1) . '行目の発送日目安(ID)が存在しません。'); |
|
|
|
|
923
|
|
|
} |
924
|
|
|
} |
925
|
|
|
|
926
|
|
View Code Duplication |
if (Str::isNotBlank($row['商品コード'])) { |
|
|
|
|
927
|
|
|
$ProductClass->setCode(Str::trimAll($row['商品コード'])); |
928
|
|
|
} else { |
929
|
|
|
$ProductClass->setCode(null); |
930
|
|
|
} |
931
|
|
|
|
932
|
|
View Code Duplication |
if ($row['在庫数無制限フラグ'] == '') { |
|
|
|
|
933
|
|
|
$this->addErrors(($data->key() + 1) . '行目の在庫数無制限フラグが設定されていません。'); |
|
|
|
|
934
|
|
|
} else { |
935
|
|
|
if ($row['在庫数無制限フラグ'] == (string) Constant::DISABLED) { |
936
|
|
|
$ProductClass->setStockUnlimited(Constant::DISABLED); |
937
|
|
|
// 在庫数が設定されていなければエラー |
938
|
|
|
if ($row['在庫数'] == '') { |
939
|
|
|
$this->addErrors(($data->key() + 1) . '行目の在庫数が設定されていません。'); |
|
|
|
|
940
|
|
|
} else { |
941
|
|
|
$stock = str_replace(',', '', $row['在庫数']); |
942
|
|
|
if (is_numeric($stock) && $stock >= 0) { |
943
|
|
|
$ProductClass->setStock($row['在庫数']); |
944
|
|
|
} else { |
945
|
|
|
$this->addErrors(($data->key() + 1) . '行目の在庫数は0以上の数値を設定してください。'); |
|
|
|
|
946
|
|
|
} |
947
|
|
|
} |
948
|
|
|
|
|
|
|
|
949
|
|
|
} else if ($row['在庫数無制限フラグ'] == (string) Constant::ENABLED) { |
950
|
|
|
$ProductClass->setStockUnlimited(Constant::ENABLED); |
951
|
|
|
$ProductClass->setStock(null); |
952
|
|
|
} else { |
953
|
|
|
$this->addErrors(($data->key() + 1) . '行目の在庫数無制限フラグが設定されていません。'); |
|
|
|
|
954
|
|
|
} |
955
|
|
|
} |
956
|
|
|
|
957
|
|
|
if ($row['販売制限数'] != '') { |
958
|
|
|
$saleLimit = str_replace(',', '', $row['販売制限数']); |
959
|
|
|
if (is_numeric($saleLimit) && $saleLimit >= 0) { |
960
|
|
|
$ProductClass->setSaleLimit($saleLimit); |
961
|
|
|
} else { |
962
|
|
|
$this->addErrors(($data->key() + 1) . '行目の販売制限数は0以上の数値を設定してください。'); |
|
|
|
|
963
|
|
|
} |
964
|
|
|
} |
965
|
|
|
|
966
|
|
|
if ($row['通常価格'] != '') { |
967
|
|
|
$price01 = str_replace(',', '', $row['通常価格']); |
968
|
|
|
if (is_numeric($price01) && $price01 >= 0) { |
969
|
|
|
$ProductClass->setPrice01($price01); |
970
|
|
|
} else { |
971
|
|
|
$this->addErrors(($data->key() + 1) . '行目の通常価格は0以上の数値を設定してください。'); |
|
|
|
|
972
|
|
|
} |
973
|
|
|
} |
974
|
|
|
|
975
|
|
|
if ($row['販売価格'] == '') { |
976
|
|
|
$this->addErrors(($data->key() + 1) . '行目の販売価格が設定されていません。'); |
|
|
|
|
977
|
|
|
} else { |
978
|
|
|
$price02 = str_replace(',', '', $row['販売価格']); |
979
|
|
|
if (is_numeric($price02) && $price02 >= 0) { |
980
|
|
|
$ProductClass->setPrice02($price02); |
981
|
|
|
} else { |
982
|
|
|
$this->addErrors(($data->key() + 1) . '行目の販売価格は0以上の数値を設定してください。'); |
|
|
|
|
983
|
|
|
} |
984
|
|
|
} |
985
|
|
|
|
986
|
|
View Code Duplication |
if ($row['商品規格削除フラグ'] == '') { |
|
|
|
|
987
|
|
|
$ProductClass->setDelFlg(Constant::DISABLED); |
988
|
|
|
} else { |
989
|
|
|
if ($row['商品規格削除フラグ'] == (string) Constant::DISABLED || $row['商品規格削除フラグ'] == (string) Constant::ENABLED) { |
990
|
|
|
$ProductClass->setDelFlg($row['商品規格削除フラグ']); |
991
|
|
|
} else { |
992
|
|
|
$this->addErrors(($data->key() + 1) . '行目の商品規格削除フラグが設定されていません。'); |
|
|
|
|
993
|
|
|
} |
994
|
|
|
} |
995
|
|
|
|
996
|
|
|
$ProductStock = $ProductClass->getProductStock(); |
997
|
|
|
|
998
|
|
|
if (!$ProductClass->getStockUnlimited()) { |
999
|
|
|
$ProductStock->setStock($ProductClass->getStock()); |
1000
|
|
|
} else { |
1001
|
|
|
// 在庫無制限時はnullを設定 |
1002
|
|
|
$ProductStock->setStock(null); |
1003
|
|
|
} |
1004
|
|
|
|
1005
|
|
|
return $ProductClass; |
1006
|
|
|
} |
1007
|
|
|
|
1008
|
|
|
/** |
1009
|
|
|
* 登録、更新時のエラー画面表示 |
1010
|
|
|
* |
1011
|
|
|
*/ |
1012
|
|
|
protected function addErrors($message) |
1013
|
|
|
{ |
1014
|
|
|
$e = new CsvImportException($message); |
1015
|
|
|
$this->errors[] = $e; |
1016
|
|
|
} |
1017
|
|
|
|
1018
|
|
|
/** |
1019
|
|
|
* @return array |
1020
|
|
|
*/ |
1021
|
|
|
protected function getErrors() |
1022
|
|
|
{ |
1023
|
|
|
return $this->errors; |
1024
|
|
|
} |
1025
|
|
|
|
1026
|
|
|
/** |
1027
|
|
|
* |
1028
|
|
|
* @return boolean |
1029
|
|
|
*/ |
1030
|
|
|
protected function hasErrors() |
1031
|
|
|
{ |
1032
|
|
|
return count($this->getErrors()) > 0; |
1033
|
|
|
} |
1034
|
|
|
|
1035
|
|
|
/** |
1036
|
|
|
* 商品登録CSVヘッダー定義 |
1037
|
|
|
*/ |
1038
|
|
|
private function getProductCsvHeader() |
1039
|
|
|
{ |
1040
|
|
|
return array( |
1041
|
|
|
'商品ID' => 'id', |
1042
|
|
|
'公開ステータス(ID)' => 'status', |
1043
|
|
|
'商品名' => 'name', |
1044
|
|
|
'ショップ用メモ欄' => 'note', |
1045
|
|
|
'商品説明(一覧)' => 'description_list', |
1046
|
|
|
'商品説明(詳細)' => 'description_detail', |
1047
|
|
|
'検索ワード' => 'search_word', |
1048
|
|
|
'フリーエリア' => 'free_area', |
1049
|
|
|
'商品削除フラグ' => 'product_del_flg', |
1050
|
|
|
'商品画像' => 'product_image', |
1051
|
|
|
'商品カテゴリ(ID)' => 'product_category', |
1052
|
|
|
'商品種別(ID)' => 'product_type', |
1053
|
|
|
'規格分類1(ID)' => 'class_category1', |
1054
|
|
|
'規格分類2(ID)' => 'class_category2', |
1055
|
|
|
'発送日目安(ID)' => 'deliveryFee', |
1056
|
|
|
'商品コード' => 'product_code', |
1057
|
|
|
'在庫数' => 'stock', |
1058
|
|
|
'在庫数無制限フラグ' => 'stock_unlimited', |
1059
|
|
|
'販売制限数' => 'sale_limit', |
1060
|
|
|
'通常価格' => 'price01', |
1061
|
|
|
'販売価格' => 'price02', |
1062
|
|
|
'送料' => 'delivery_fee', |
1063
|
|
|
'商品規格削除フラグ' => 'product_class_del_flg', |
1064
|
|
|
); |
1065
|
|
|
} |
1066
|
|
|
|
1067
|
|
|
|
1068
|
|
|
/** |
1069
|
|
|
* カテゴリCSVヘッダー定義 |
1070
|
|
|
*/ |
1071
|
|
|
private function getCategoryCsvHeader() |
1072
|
|
|
{ |
1073
|
|
|
return array( |
1074
|
|
|
'カテゴリID' => 'id', |
1075
|
|
|
'カテゴリ名' => 'category_name', |
1076
|
|
|
'親カテゴリID' => 'parent_category_id', |
1077
|
|
|
); |
1078
|
|
|
} |
1079
|
|
|
} |
1080
|
|
|
|