public function csvProduct(Application $app, Request $request)
{
$form = $app['form.factory']->createBuilder('admin_csv_import')->getForm();
$headers = $this->getProductCsvHeader();
if ('POST' === $request->getMethod()) {
$form->handleRequest($request);
if ($form->isValid()) {
$formFile = $form['import_file']->getData();
if (!empty($formFile)) {
log_info('商品CSV登録開始');
$data = $this->getImportData($app, $formFile);
if ($data === false) {
$this->addErrors('CSVのフォーマットが一致しません。');
return $this->render($app, $form, $headers, $this->productTwig);
}
$keys = array_keys($headers);
$columnHeaders = $data->getColumnHeaders();
if ($keys !== $columnHeaders) {
$this->addErrors('CSVのフォーマットが一致しません。');
return $this->render($app, $form, $headers, $this->productTwig);
}
$size = count($data);
if ($size < 1) {
$this->addErrors('CSVデータが存在しません。');
return $this->render($app, $form, $headers, $this->productTwig);
}
$headerSize = count($keys);
$this->em = $app['orm.em'];
$this->em->getConfiguration()->setSQLLogger(null);
$this->em->getConnection()->beginTransaction();
$BaseInfo = $app['eccube.repository.base_info']->get();
// CSVファイルの登録処理
foreach ($data as $row) {
if ($headerSize != count($row)) {
$this->addErrors($data->key() + 1 . '行目のCSVフォーマットが一致しません。');
return $this->render($app, $form, $headers, $this->productTwig);
}
if ($row['商品ID'] == '') {
$Product = new Product();
$this->em->persist($Product);
} else {
if (preg_match('/^\\d+$/', $row['商品ID'])) {
$Product = $app['eccube.repository.product']->find($row['商品ID']);
if (!$Product) {
$this->addErrors($data->key() + 1 . '行目の商品IDが存在しません。');
return $this->render($app, $form, $headers, $this->productTwig);
}
} else {
$this->addErrors($data->key() + 1 . '行目の商品IDが存在しません。');
return $this->render($app, $form, $headers, $this->productTwig);
}
}
if ($row['公開ステータス(ID)'] == '') {
$this->addErrors($data->key() + 1 . '行目の公開ステータス(ID)が設定されていません。');
} else {
if (preg_match('/^\\d+$/', $row['公開ステータス(ID)'])) {
$Disp = $app['eccube.repository.master.disp']->find($row['公開ステータス(ID)']);
if (!$Disp) {
$this->addErrors($data->key() + 1 . '行目の公開ステータス(ID)が存在しません。');
} else {
$Product->setStatus($Disp);
}
} else {
$this->addErrors($data->key() + 1 . '行目の公開ステータス(ID)が存在しません。');
}
}
if (Str::isBlank($row['商品名'])) {
$this->addErrors($data->key() + 1 . '行目の商品名が設定されていません。');
return $this->render($app, $form, $headers, $this->productTwig);
} else {
$Product->setName(Str::trimAll($row['商品名']));
}
if (Str::isNotBlank($row['ショップ用メモ欄'])) {
$Product->setNote(Str::trimAll($row['ショップ用メモ欄']));
} else {
$Product->setNote(null);
}
if (Str::isNotBlank($row['商品説明(一覧)'])) {
$Product->setDescriptionList(Str::trimAll($row['商品説明(一覧)']));
} else {
$Product->setDescriptionList(null);
}
if (Str::isNotBlank($row['商品説明(詳細)'])) {
$Product->setDescriptionDetail(Str::trimAll($row['商品説明(詳細)']));
} else {
$Product->setDescriptionDetail(null);
}
if (Str::isNotBlank($row['検索ワード'])) {
$Product->setSearchWord(Str::trimAll($row['検索ワード']));
} else {
$Product->setSearchWord(null);
}
if (Str::isNotBlank($row['フリーエリア'])) {
$Product->setFreeArea(Str::trimAll($row['フリーエリア']));
} else {
$Product->setFreeArea(null);
}
if ($row['商品削除フラグ'] == '') {
$Product->setDelFlg(Constant::DISABLED);
} else {
if ($row['商品削除フラグ'] == (string) Constant::DISABLED || $row['商品削除フラグ'] == (string) Constant::ENABLED) {
$Product->setDelFlg($row['商品削除フラグ']);
} else {
$this->addErrors($data->key() + 1 . '行目の商品削除フラグが設定されていません。');
return $this->render($app, $form, $headers, $this->productTwig);
}
}
// 商品画像登録
$this->createProductImage($row, $Product);
$this->em->flush($Product);
// 商品カテゴリ登録
$this->createProductCategory($row, $Product, $app, $data);
//タグ登録
$this->createProductTag($row, $Product, $app, $data);
// 商品規格が存在しなければ新規登録
$ProductClasses = $Product->getProductClasses();
if ($ProductClasses->count() < 1) {
// 規格分類1(ID)がセットされていると規格なし商品、規格あり商品を作成
$ProductClassOrg = $this->createProductClass($row, $Product, $app, $data);
if ($BaseInfo->getOptionProductDeliveryFee() == Constant::ENABLED) {
if ($row['送料'] != '') {
$deliveryFee = str_replace(',', '', $row['送料']);
if (preg_match('/^\\d+$/', $deliveryFee) && $deliveryFee >= 0) {
$ProductClassOrg->setDeliveryFee($deliveryFee);
} else {
$this->addErrors($data->key() + 1 . '行目の送料は0以上の数値を設定してください。');
}
}
}
if ($row['規格分類1(ID)'] != '') {
if ($row['規格分類1(ID)'] == $row['規格分類2(ID)']) {
$this->addErrors($data->key() + 1 . '行目の規格分類1(ID)と規格分類2(ID)には同じ値を使用できません。');
} else {
// 商品規格あり
// 企画分類あり商品を作成
$ProductClass = clone $ProductClassOrg;
$ProductStock = clone $ProductClassOrg->getProductStock();
// 規格分類1、規格分類2がnullであるデータの削除フラグを1にセット
$ProductClassOrg->setDelFlg(Constant::ENABLED);
// 規格分類1、2をそれぞれセットし作成
$ClassCategory1 = null;
if (preg_match('/^\\d+$/', $row['規格分類1(ID)'])) {
$ClassCategory1 = $app['eccube.repository.class_category']->find($row['規格分類1(ID)']);
if (!$ClassCategory1) {
$this->addErrors($data->key() + 1 . '行目の規格分類1(ID)が存在しません。');
} else {
$ProductClass->setClassCategory1($ClassCategory1);
}
} else {
$this->addErrors($data->key() + 1 . '行目の規格分類1(ID)が存在しません。');
}
if ($row['規格分類2(ID)'] != '') {
if (preg_match('/^\\d+$/', $row['規格分類2(ID)'])) {
$ClassCategory2 = $app['eccube.repository.class_category']->find($row['規格分類2(ID)']);
if (!$ClassCategory2) {
$this->addErrors($data->key() + 1 . '行目の規格分類2(ID)が存在しません。');
} else {
if ($ClassCategory1 && $ClassCategory1->getClassName()->getId() == $ClassCategory2->getClassName()->getId()) {
$this->addErrors($data->key() + 1 . '行目の規格分類1(ID)と規格分類2(ID)の規格名が同じです。');
} else {
$ProductClass->setClassCategory2($ClassCategory2);
}
}
} else {
$this->addErrors($data->key() + 1 . '行目の規格分類2(ID)が存在しません。');
}
}
$ProductClass->setProductStock($ProductStock);
$ProductStock->setProductClass($ProductClass);
$this->em->persist($ProductClass);
$this->em->persist($ProductStock);
}
} else {
if ($row['規格分類2(ID)'] != '') {
$this->addErrors($data->key() + 1 . '行目の規格分類1(ID)が存在しません。');
}
}
} else {
// 商品規格の更新
$flag = false;
$classCategoryId1 = $row['規格分類1(ID)'] == '' ? null : $row['規格分類1(ID)'];
$classCategoryId2 = $row['規格分類2(ID)'] == '' ? null : $row['規格分類2(ID)'];
foreach ($ProductClasses as $pc) {
$classCategory1 = is_null($pc->getClassCategory1()) ? null : $pc->getClassCategory1()->getId();
$classCategory2 = is_null($pc->getClassCategory2()) ? null : $pc->getClassCategory2()->getId();
// 登録されている商品規格を更新
if ($classCategory1 == $classCategoryId1 && $classCategory2 == $classCategoryId2) {
$this->updateProductClass($row, $Product, $pc, $app, $data);
if ($BaseInfo->getOptionProductDeliveryFee() == Constant::ENABLED) {
if ($row['送料'] != '') {
$deliveryFee = str_replace(',', '', $row['送料']);
if (preg_match('/^\\d+$/', $deliveryFee) && $deliveryFee >= 0) {
$pc->setDeliveryFee($deliveryFee);
} else {
$this->addErrors($data->key() + 1 . '行目の送料は0以上の数値を設定してください。');
}
}
}
$flag = true;
break;
}
}
// 商品規格を登録
if (!$flag) {
$pc = $ProductClasses[0];
if ($pc->getClassCategory1() == null && $pc->getClassCategory2() == null) {
// 規格分類1、規格分類2がnullであるデータの削除フラグを1にセット
$pc->setDelFlg(Constant::ENABLED);
}
if ($row['規格分類1(ID)'] == $row['規格分類2(ID)']) {
$this->addErrors($data->key() + 1 . '行目の規格分類1(ID)と規格分類2(ID)には同じ値を使用できません。');
} else {
// 必ず規格分類1がセットされている
// 規格分類1、2をそれぞれセットし作成
$ClassCategory1 = null;
if (preg_match('/^\\d+$/', $classCategoryId1)) {
$ClassCategory1 = $app['eccube.repository.class_category']->find($classCategoryId1);
if (!$ClassCategory1) {
$this->addErrors($data->key() + 1 . '行目の規格分類1(ID)が存在しません。');
}
} else {
$this->addErrors($data->key() + 1 . '行目の規格分類1(ID)が存在しません。');
}
$ClassCategory2 = null;
if ($row['規格分類2(ID)'] != '') {
if ($pc->getClassCategory1() != null && $pc->getClassCategory2() == null) {
$this->addErrors($data->key() + 1 . '行目の規格分類2(ID)は設定できません。');
} else {
if (preg_match('/^\\d+$/', $classCategoryId2)) {
$ClassCategory2 = $app['eccube.repository.class_category']->find($classCategoryId2);
if (!$ClassCategory2) {
$this->addErrors($data->key() + 1 . '行目の規格分類2(ID)が存在しません。');
} else {
if ($ClassCategory1 && $ClassCategory1->getClassName()->getId() == $ClassCategory2->getClassName()->getId()) {
$this->addErrors($data->key() + 1 . '行目の規格分類1(ID)と規格分類2(ID)の規格名が同じです。');
}
}
} else {
$this->addErrors($data->key() + 1 . '行目の規格分類2(ID)が存在しません。');
}
}
} else {
if ($pc->getClassCategory1() != null && $pc->getClassCategory2() != null) {
$this->addErrors($data->key() + 1 . '行目の規格分類2(ID)に値を設定してください。');
}
}
$ProductClass = $this->createProductClass($row, $Product, $app, $data, $ClassCategory1, $ClassCategory2);
if ($BaseInfo->getOptionProductDeliveryFee() == Constant::ENABLED) {
if ($row['送料'] != '') {
$deliveryFee = str_replace(',', '', $row['送料']);
if (preg_match('/^\\d+$/', $deliveryFee) && $deliveryFee >= 0) {
$ProductClass->setDeliveryFee($deliveryFee);
} else {
$this->addErrors($data->key() + 1 . '行目の送料は0以上の数値を設定してください。');
}
}
}
$Product->addProductClass($ProductClass);
}
}
}
if ($this->hasErrors()) {
return $this->render($app, $form, $headers, $this->productTwig);
}
$this->em->persist($Product);
}
$this->em->flush();
$this->em->getConnection()->commit();
log_info('商品CSV登録完了');
$app->addSuccess('admin.product.csv_import.save.complete', 'admin');
}
}
}
return $this->render($app, $form, $headers, $this->productTwig);
}