Failed Conditions
Push — master ( d534e7...8e1a86 )
by Yangsin
285:02 queued 279:03
created

ProductClassController::newProductClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 2
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\ClassName;
30
use Eccube\Entity\Product;
31
use Eccube\Entity\ProductClass;
32
use Eccube\Event\EccubeEvents;
33
use Eccube\Event\EventArgs;
34
use Symfony\Component\Form\FormError;
35
use Symfony\Component\HttpFoundation\Request;
36
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
37
use Symfony\Component\Validator\Constraints as Assert;
38
39
40
class ProductClassController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
41
{
42
43
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
44
     * 商品規格が登録されていなければ新規登録、登録されていれば更新画面を表示する
45
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
46 1
    public function index(Application $app, Request $request, $id)
47
    {
48
49
        /** @var $Product \Eccube\Entity\Product */
50 1
        $Product = $app['eccube.repository.product']->find($id);
51 1
        $hasClassCategoryFlg = false;
52
53 1
        if (!$Product) {
54
            throw new NotFoundHttpException('商品が存在しません');
55
        }
56
57
58
        // 商品規格情報が存在しなければ新規登録させる
59 1
        if (!$Product->hasProductClass()) {
60
            // 登録画面を表示
61
62 1
            log_info('商品規格新規登録表示', array($id));
63
64 1
            $builder = $app['form.factory']->createBuilder();
65
66
            $builder
67 1
                ->add('class_name1', 'entity', array(
68 1
                    'class' => 'Eccube\Entity\ClassName',
69 1
                    'property' => 'name',
70 1
                    'empty_value' => '規格1を選択',
71
                    'constraints' => array(
72 1
                        new Assert\NotBlank(),
73 1
                    ),
74 1
                ))
75 1
                ->add('class_name2', 'entity', array(
76 1
                    'class' => 'Eccube\Entity\ClassName',
77 1
                    'property' => 'name',
78 1
                    'empty_value' => '規格2を選択',
79 1
                    'required' => false,
80 1
                ));
81
82 1
            $event = new EventArgs(
83
                array(
84 1
                    'builder' => $builder,
85 1
                    'Product' => $Product,
86 1
                ),
87
                $request
88 1
            );
89 1
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_INDEX_INITIALIZE, $event);
90
91 1
            $form = $builder->getForm();
92
93 1
            $productClassForm = null;
94
95 1
            if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
96
97
                $form->handleRequest($request);
98
99
                if ($form->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
100
101
                    $data = $form->getData();
102
103
                    $ClassName1 = $data['class_name1'];
104
                    $ClassName2 = $data['class_name2'];
105
106
                    log_info('選択された商品規格', array($ClassName1, $ClassName2));
107
108
                    // 各規格が選択されている際に、分類を保有しているか確認
109
                    $class1Valied = $this->isValiedCategory($ClassName1);
110
                    $class2Valied = $this->isValiedCategory($ClassName2);
111
112
                    // 規格が選択されていないか、選択された状態で分類が保有されていれば、画面表示
113
                    if($class1Valied && $class2Valied){
114
                        $hasClassCategoryFlg = true;
115
                    }
116
117
                    if (!is_null($ClassName2) && $ClassName1->getId() == $ClassName2->getId()) {
118
                        // 規格1と規格2が同じ値はエラー
119
120
                        $form['class_name2']->addError(new FormError('規格1と規格2は、同じ値を使用できません。'));
121
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
122
                    } else {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
123
124
                        // 規格分類が設定されていない商品規格を取得
125
                        $orgProductClasses = $Product->getProductClasses();
126
                        $sourceProduct = $orgProductClasses[0];
127
128
                        // 規格分類が組み合わされた商品規格を取得
129
                        $ProductClasses = $this->createProductClasses($app, $Product, $ClassName1, $ClassName2);
130
131
                        // 組み合わされた商品規格にデフォルト値をセット
132
                        foreach ($ProductClasses as $productClass) {
133
                            $this->setDefualtProductClass($app, $productClass, $sourceProduct);
134
                        }
135
136
137
                        $builder = $app['form.factory']->createBuilder();
138
139
                        $builder
140
                            ->add('product_classes', 'collection', array(
141
                                'type' => 'admin_product_class',
142
                                'allow_add' => true,
143
                                'allow_delete' => true,
144
                                'data' => $ProductClasses,
145
                             ));
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 28 spaces, but found 29.
Loading history...
146
147
                        $event = new EventArgs(
148
                            array(
149
                                'builder' => $builder,
150
                                'Product' => $Product,
151
                                'ProductClasses' => $ProductClasses,
152
                            ),
153
                            $request
154
                        );
155
                        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_INDEX_CLASSES, $event);
156
157
                        $productClassForm = $builder->getForm()->createView();
158
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
159
                    }
160
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
161
                }
162
            }
163
164 1
            return $app->render('Product/product_class.twig', array(
165 1
                'form' => $form->createView(),
166 1
                'classForm' => $productClassForm,
167 1
                'Product' => $Product,
168 1
                'not_product_class' => true,
169 1
                'error' => null,
170 1
                'has_class_category_flg' => $hasClassCategoryFlg,
171 1
            ));
172
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
173
        } else {
174
            // 既に商品規格が登録されている場合、商品規格画面を表示する
175
176
            log_info('商品規格登録済表示', array($id));
177
178
            // 既に登録されている商品規格を取得
179
            $ProductClasses = $this->getProductClassesExcludeNonClass($Product);
180
181
            // 設定されている規格分類1、2を取得(商品規格の規格分類には必ず同じ値がセットされている)
182
            $ProductClass = $ProductClasses[0];
183
            $ClassName1 = $ProductClass->getClassCategory1()->getClassName();
184
            $ClassName2 = null;
185
            if (!is_null($ProductClass->getClassCategory2())) {
186
                $ClassName2 = $ProductClass->getClassCategory2()->getClassName();
187
            }
188
189
            // 規格分類が組み合わされた空の商品規格を取得
190
            $createProductClasses = $this->createProductClasses($app, $Product, $ClassName1, $ClassName2);
191
192
193
            $mergeProductClasses = array();
194
195
            // 商品税率が設定されている場合、商品税率を項目に設定
196
            $BaseInfo = $app['eccube.repository.base_info']->get();
197 View Code Duplication
            if ($BaseInfo->getOptionProductTaxRule() == Constant::ENABLED) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
198
                foreach ($ProductClasses as $class) {
199
                    if ($class->getTaxRule() && !$class->getTaxRule()->getDelFlg()) {
200
                        $class->setTaxRate($class->getTaxRule()->getTaxRate());
201
                    }
202
                }
203
            }
204
205
206
            // 登録済み商品規格と空の商品規格をマージ
207
            $flag = false;
208
            foreach ($createProductClasses as $createProductClass) {
209
                // 既に登録済みの商品規格にチェックボックスを設定
210
                foreach ($ProductClasses as $productClass) {
211
                    if ($productClass->getClassCategory1() == $createProductClass->getClassCategory1() &&
212
                            $productClass->getClassCategory2() == $createProductClass->getClassCategory2()) {
213
                                // チェックボックスを追加
214
                                $productClass->setAdd(true);
215
                                $flag = true;
216
                                break;
217
                    }
218
                }
219
220
                if (!$flag) {
221
                    $mergeProductClasses[] = $createProductClass;
222
                }
223
224
                $flag = false;
225 1
            }
226
227
            // 登録済み商品規格と空の商品規格をマージ
228
            foreach ($mergeProductClasses as $mergeProductClass) {
229
                // 空の商品規格にデフォルト値を設定
230
                $this->setDefualtProductClass($app, $mergeProductClass, $ProductClass);
231
                $ProductClasses->add($mergeProductClass);
0 ignored issues
show
Bug introduced by
The method add cannot be called on $ProductClasses (of type array<integer,object<Eccube\Entity\ProductClass>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
232
            }
233
234
            $builder = $app['form.factory']->createBuilder();
235
236
            $builder
237
                ->add('product_classes', 'collection', array(
238
                    'type' => 'admin_product_class',
239 1
                    'allow_add' => true,
240
                    'allow_delete' => true,
241 1
                    'data' => $ProductClasses,
242
                ));
243
244
            $event = new EventArgs(
245
                array(
246
                    'builder' => $builder,
247
                    'Product' => $Product,
248
                    'ProductClasses' => $ProductClasses,
249
                ),
250
                $request
251
            );
252
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_INDEX_CLASSES, $event);
253
254
            $productClassForm = $builder->getForm()->createView();
255
256
            return $app->render('Product/product_class.twig', array(
257
                'classForm' => $productClassForm,
258
                'Product' => $Product,
259
                'class_name1' => $ClassName1,
260
                'class_name2' => $ClassName2,
261
                'not_product_class' => false,
262
                'error' => null,
263
                'has_class_category_flg' => true,
264
            ));
265
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
266
        }
267
268
    }
269
270
271
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
272
     * 商品規格の登録、更新、削除を行う
273
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
274 2
    public function edit(Application $app, Request $request, $id)
275
    {
276
277
        /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */
278 2
        $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete');
279 2
        $softDeleteFilter->setExcludes(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
280
            'Eccube\Entity\TaxRule'
281 2
        ));
282
283
        /** @var $Product \Eccube\Entity\Product */
284 2
        $Product = $app['eccube.repository.product']->find($id);
285
286 2
        if (!$Product) {
287
            throw new NotFoundHttpException('商品が存在しません');
288
        }
289
290 2
        $builder = $app['form.factory']->createBuilder();
291
        $builder
292 2
                ->add('product_classes', 'collection', array(
293 2
                    'type' => 'admin_product_class',
294 2
                    'allow_add' => true,
295 2
                    'allow_delete' => true,
296 2
            ));
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 12.
Loading history...
297
298 2
        $event = new EventArgs(
299
            array(
300 2
                'builder' => $builder,
301 2
                'Product' => $Product,
302 2
            ),
303
            $request
304 2
        );
305 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_INITIALIZE, $event);
306
307 2
        $form = $builder->getForm();
308
309 2
        $ProductClasses = $this->getProductClassesExcludeNonClass($Product);
310
311 2
        if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
312
313 2
            $form->handleRequest($request);
314
315 2
            switch ($request->get('mode')) {
316 2
                case 'edit':
317
                    // 新規登録
318
                    log_info('商品規格新規登録開始', array($id));
319
320 View Code Duplication
                    if (count($ProductClasses) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
321
                        // 既に登録されていれば最初の画面に戻す
322
                        log_info('商品規格登録済', array($id));
323
                        return $app->redirect($app->url('admin_product_product_class', array('id' => $id)));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
324
                    }
325
326
                    $addProductClasses = array();
327
328
                    $tmpProductClass = null;
329 View Code Duplication
                    foreach ($form->get('product_classes') as $formData) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
330
                        // 追加対象の行をvalidate
331
                        $ProductClass = $formData->getData();
332
333
                        if ($ProductClass->getAdd()) {
334
                            if ($formData->isValid()) {
335
                                $addProductClasses[] = $ProductClass;
336
                            } else {
337
                                // 対象行のエラー
338
                                return $this->render($app, $Product, $ProductClass, true, $form);
339
                            }
340
                        }
341
                        $tmpProductClass = $ProductClass;
342
                    }
343
344 View Code Duplication
                    if (count($addProductClasses) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
345
                        // 対象がなければエラー
346
                        log_info('商品規格が未選択', array($id));
347
                        $error = array('message' => '商品規格が選択されていません。');
348
                        return $this->render($app, $Product, $tmpProductClass, true, $form, $error);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
349
                    }
350
351
                    // 選択された商品規格を登録
352
                    $this->insertProductClass($app, $Product, $addProductClasses);
353
354
                    // デフォルトの商品規格を更新
355
                    $defaultProductClass = $app['eccube.repository.product_class']
356
                            ->findOneBy(array('Product' => $Product, 'ClassCategory1' => null, 'ClassCategory2' => null));
357
358
                    $defaultProductClass->setDelFlg(Constant::ENABLED);
359
360
                    $app['orm.em']->flush();
361
362
                    log_info('商品規格新規登録完了', array($id));
363
364
                    $event = new EventArgs(
365
                        array(
366
                            'form' => $form,
367
                            'Product' => $Product,
368
                            'defaultProductClass' => $defaultProductClass,
369
                        ),
370
                        $request
371
                    );
372
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_COMPLETE, $event);
373
374
                    $app->addSuccess('admin.product.product_class.save.complete', 'admin');
375
376
                    break;
377 2
                case 'update':
378
                    // 更新
379
                    log_info('商品規格更新開始', array($id));
380
381 View Code Duplication
                    if (count($ProductClasses) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
382
                        // 商品規格が0件であれば最初の画面に戻す
383
                        log_info('商品規格が存在しません', array($id));
384
                        return $app->redirect($app->url('admin_product_product_class', array('id' => $id)));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
385
                    }
386
387
                    $checkProductClasses = array();
388
                    $removeProductClasses = array();
389
390
                    $tempProductClass = null;
391 View Code Duplication
                    foreach ($form->get('product_classes') as $formData) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
392
                        // 追加対象の行をvalidate
393
                        $ProductClass = $formData->getData();
394
395
                        if ($ProductClass->getAdd()) {
396
                            if ($formData->isValid()) {
397
                                $checkProductClasses[] = $ProductClass;
398
                            } else {
399
                                return $this->render($app, $Product, $ProductClass, false, $form);
400
                            }
401
                        } else {
402
                            // 削除対象の行
403
                            $removeProductClasses[] = $ProductClass;
404
                        }
405
                        $tempProductClass = $ProductClass;
406
                    }
407
408 View Code Duplication
                    if (count($checkProductClasses) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
409
                        // 対象がなければエラー
410
                        log_info('商品規格が存在しません', array($id));
411
                        $error = array('message' => '商品規格が選択されていません。');
412
                        return $this->render($app, $Product, $tempProductClass, false, $form, $error);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
413
                    }
414
415
416
                    // 登録対象と更新対象の行か判断する
417
                    $addProductClasses = array();
418
                    $updateProductClasses = array();
419
                    foreach ($checkProductClasses as $cp) {
420
                        $flag = false;
421
                        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
422
                        // 既に登録済みの商品規格か確認
423
                        foreach ($ProductClasses as $productClass) {
424
                            if ($productClass->getProduct()->getId() == $id &&
425
                                    $productClass->getClassCategory1() == $cp->getClassCategory1() &&
426
                                    $productClass->getClassCategory2() == $cp->getClassCategory2()) {
427
                                $updateProductClasses[] = $cp;
428
429
                                // 商品情報
430
                                $cp->setProduct($Product);
431
                                // 商品在庫
432
                                $productStock = $productClass->getProductStock();
433
                                if (!$cp->getStockUnlimited()) {
434
                                    $productStock->setStock($cp->getStock());
435
                                } else {
436
                                    $productStock->setStock(null);
437
                                }
438
                                $this->setDefualtProductClass($app, $productClass, $cp);
439
                                $flag = true;
440
                                break;
441
                            }
442
                        }
443
                        if (!$flag) {
444
                            $addProductClasses[] = $cp;
445
                        }
446
                    }
447
448
                    foreach ($removeProductClasses as $rc) {
449
                        // 登録されている商品規格に削除フラグをセット
450
                        foreach ($ProductClasses as $productClass) {
451
                            if ($productClass->getProduct()->getId() == $id &&
452
                                    $productClass->getClassCategory1() == $rc->getClassCategory1() &&
453
                                    $productClass->getClassCategory2() == $rc->getClassCategory2()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
454
455
                                $productClass->setDelFlg(Constant::ENABLED);
456
                                break;
457
                            }
458
                        }
459
                    }
460
461
                    // 選択された商品規格を登録
462
                    $this->insertProductClass($app, $Product, $addProductClasses);
463
464
                    $app['orm.em']->flush();
465
466
                    log_info('商品規格更新完了', array($id));
467
468
                    $event = new EventArgs(
469
                        array(
470
                            'form' => $form,
471
                            'Product' => $Product,
472
                            'updateProductClasses' => $updateProductClasses,
473
                            'addProductClasses' => $addProductClasses,
474
                        ),
475
                        $request
476
                    );
477
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_UPDATE, $event);
478
479
                    $app->addSuccess('admin.product.product_class.update.complete', 'admin');
480
481
                    break;
482 2
                case 'delete':
483
                    // 削除
484
                    log_info('商品規格削除開始', array($id));
485
486 View Code Duplication
                    if (count($ProductClasses) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
487
                        // 既に商品が削除されていれば元の画面に戻す
488
                        log_info('商品規格が存在しません', array($id));
489
                        return $app->redirect($app->url('admin_product_product_class', array('id' => $id)));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
490
                    }
491
492
                    foreach ($ProductClasses as $ProductClass) {
493
                        // 登録されている商品規格に削除フラグをセット
494
                        $ProductClass->setDelFlg(Constant::ENABLED);
495
                    }
496
497
                    /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */
498
                    $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete');
499
                    $softDeleteFilter->setExcludes(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
500
                        'Eccube\Entity\ProductClass'
501
                    ));
502
503
                    // デフォルトの商品規格を更新
504
                    $defaultProductClass = $app['eccube.repository.product_class']
505
                            ->findOneBy(array('Product' => $Product, 'ClassCategory1' => null, 'ClassCategory2' => null, 'del_flg' => Constant::ENABLED));
506
507
                    $defaultProductClass->setDelFlg(Constant::DISABLED);
508
509
                    $app['orm.em']->flush();
510
                    log_info('商品規格削除完了', array($id));
511
512
                    $event = new EventArgs(
513
                        array(
514
                            'form' => $form,
515
                            'Product' => $Product,
516
                            'defaultProductClass' => $defaultProductClass,
517
                        ),
518
                        $request
519
                    );
520
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_DELETE, $event);
521
522
                    $app->addSuccess('admin.product.product_class.delete.complete', 'admin');
523
524
                    break;
525 2
                default:
526 2
                    break;
527 2
            }
528
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
529 2
        }
530
531 2
        return $app->redirect($app->url('admin_product_product_class', array('id' => $id)));
532
    }
533
534
535
536
    /**
537
     * 登録、更新時のエラー画面表示
538
     *
539
     */
540
    protected function render($app, $Product, $ProductClass, $not_product_class, $classForm, $error = null)
541
    {
542
543
        $ClassName1 = null;
544
        $ClassName2 = null;
545
        // 規格を取得
546
        if (isset($ProductClass)) {
547
            $ClassCategory1 = $ProductClass->getClassCategory1();
548
            if ($ClassCategory1) {
549
                $ClassName1 = $ClassCategory1->getClassName();
550
            }
551
            $ClassCategory2 = $ProductClass->getClassCategory2();
552
            if ($ClassCategory2) {
553
                $ClassName2 = $ClassCategory2->getClassName();
554
            }
555
        }
556
557
        $form = $app->form()
558
            ->add('class_name1', 'entity', array(
559
                'class' => 'Eccube\Entity\ClassName',
560
                'property' => 'name',
561
                'empty_value' => '規格1を選択',
562
                'data' => $ClassName1,
563
            ))
564
            ->add('class_name2', 'entity', array(
565
                'class' => 'Eccube\Entity\ClassName',
566
                'property' => 'name',
567
                'empty_value' => '規格2を選択',
568
                'data' => $ClassName2,
569
            ))
570
            ->getForm();
571
572
        log_info('商品規格登録エラー');
573
574
575
        return $app->render('Product/product_class.twig', array(
576
            'form' => $form->createView(),
577
            'classForm' => $classForm->createView(),
578
            'Product' => $Product,
579
            'class_name1' => $ClassName1,
580
            'class_name2' => $ClassName2,
581
            'not_product_class' => $not_product_class,
582
            'error' => $error,
583
            'has_class_category_flg' => true,
584
        ));
585
    }
586
587
588
    /**
589
     * 規格1と規格2を組み合わせた商品規格を作成
590
     */
591
    private function createProductClasses($app, Product $Product, ClassName $ClassName1 = null, ClassName $ClassName2 = null)
592
    {
593
594
        $ClassCategories1 = array();
595
        if ($ClassName1) {
596
            $ClassCategories1 = $app['eccube.repository.class_category']->findBy(array('ClassName' => $ClassName1));
597
        }
598
599
        $ClassCategories2 = array();
600
        if ($ClassName2) {
601
            $ClassCategories2 = $app['eccube.repository.class_category']->findBy(array('ClassName' => $ClassName2));
602
        }
603
604
        $ProductClasses = array();
605
        foreach ($ClassCategories1 as $ClassCategory1) {
606
            if ($ClassCategories2) {
607
                foreach ($ClassCategories2 as $ClassCategory2) {
608
                    $ProductClass = $this->newProductClass($app);
609
                    $ProductClass->setProduct($Product);
610
                    $ProductClass->setClassCategory1($ClassCategory1);
611
                    $ProductClass->setClassCategory2($ClassCategory2);
612
                    $ProductClass->setTaxRate(null);
613
                    $ProductClass->setDelFlg(Constant::DISABLED);
614
                    $ProductClasses[] = $ProductClass;
615
                }
616
            } else {
617
                $ProductClass = $this->newProductClass($app);
618
                $ProductClass->setProduct($Product);
619
                $ProductClass->setClassCategory1($ClassCategory1);
620
                $ProductClass->setTaxRate(null);
621
                $ProductClass->setDelFlg(Constant::DISABLED);
622
                $ProductClasses[] = $ProductClass;
623
            }
624
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
625
        }
626
        return $ProductClasses;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
627
    }
628
629
    /**
630
     * 新しい商品規格を作成
631
     */
632
    private function newProductClass(Application $app)
633
    {
634
        $ProductType = $app['eccube.repository.master.product_type']->find($app['config']['product_type_normal']);
635
636
        $ProductClass = new ProductClass();
637
        $ProductClass->setProductType($ProductType);
638
        return $ProductClass;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
639
    }
640
641
    /**
642
     * 商品規格のコピーを取得.
643
     *
644
     * @see http://symfony.com/doc/current/cookbook/form/form_collections.html
645
     * @param Product $Product
646
     * @return \Eccube\Entity\ProductClass[]
647
     */
648
    private function getProductClassesOriginal(Product $Product)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
649
    {
650
        $ProductClasses = $Product->getProductClasses();
651
        return $ProductClasses->filter(function($ProductClass) {
0 ignored issues
show
Unused Code introduced by
The parameter $ProductClass is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
introduced by
Missing blank line before return statement
Loading history...
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
652
            return true;
653
        });
654
    }
655
656
    /**
657
     * 規格なし商品を除いて商品規格を取得.
658
     *
659
     * @param Product $Product
660
     * @return \Eccube\Entity\ProductClass[]
661
     */
662 2
    private function getProductClassesExcludeNonClass(Product $Product)
663
    {
664 2
        $ProductClasses = $Product->getProductClasses();
665 2
        return $ProductClasses->filter(function($ProductClass) {
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
666
            $ClassCategory1 = $ProductClass->getClassCategory1();
667
            $ClassCategory2 = $ProductClass->getClassCategory2();
668
            return ($ClassCategory1 || $ClassCategory2);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
669 2
        });
670
    }
671
672
    /**
673
     * デフォルトとなる商品規格を設定
674
     *
675
     * @param $productClassDest コピー先となる商品規格
676
     * @param $productClassOrig コピー元となる商品規格
677
     */
678
    private function setDefualtProductClass($app, $productClassDest, $productClassOrig) {
679
        $productClassDest->setDeliveryDate($productClassOrig->getDeliveryDate());
680
        $productClassDest->setProduct($productClassOrig->getProduct());
681
        $productClassDest->setProductType($productClassOrig->getProductType());
682
        $productClassDest->setCode($productClassOrig->getCode());
683
        $productClassDest->setStock($productClassOrig->getStock());
684
        $productClassDest->setStockUnlimited($productClassOrig->getStockUnlimited());
685
        $productClassDest->setSaleLimit($productClassOrig->getSaleLimit());
686
        $productClassDest->setPrice01($productClassOrig->getPrice01());
687
        $productClassDest->setPrice02($productClassOrig->getPrice02());
688
        $productClassDest->setDeliveryFee($productClassOrig->getDeliveryFee());
689
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
690
        // 個別消費税
691
        $BaseInfo = $app['eccube.repository.base_info']->get();
692
        if ($BaseInfo->getOptionProductTaxRule() == Constant::ENABLED) {
693
            if ($productClassOrig->getTaxRate() !== false && $productClassOrig->getTaxRate() !== null) {
694
                $productClassDest->setTaxRate($productClassOrig->getTaxRate());
695 View Code Duplication
                if ($productClassDest->getTaxRule()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
696
                    $productClassDest->getTaxRule()->setTaxRate($productClassOrig->getTaxRate());
697
                    $productClassDest->getTaxRule()->setDelFlg(Constant::DISABLED);
698
                } else {
699
                    $taxrule = $app['eccube.repository.tax_rule']->newTaxRule();
700
                    $taxrule->setTaxRate($productClassOrig->getTaxRate());
701
                    $taxrule->setApplyDate(new \DateTime());
702
                    $taxrule->setProduct($productClassDest->getProduct());
703
                    $taxrule->setProductClass($productClassDest);
704
                    $productClassDest->setTaxRule($taxrule);
705
                }
706
            } else {
707
                if ($productClassDest->getTaxRule()) {
708
                    $productClassDest->getTaxRule()->setDelFlg(Constant::ENABLED);
709
                }
710
            }
711
        }
712
    }
713
714
715
    /**
716
     * 商品規格を登録
717
     *
718
     * @param $ProductClasses 登録される商品規格
719
     */
720
    private function insertProductClass($app, $Product, $ProductClasses) {
721
722
        $BaseInfo = $app['eccube.repository.base_info']->get();
723
724
        // 選択された商品を登録
725
        foreach ($ProductClasses as $ProductClass) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
726
727
            $ProductClass->setDelFlg(Constant::DISABLED);
728
            $ProductClass->setProduct($Product);
729
            $app['orm.em']->persist($ProductClass);
730
731
            // 在庫情報を作成
732
            $ProductStock = new \Eccube\Entity\ProductStock();
733
            $ProductClass->setProductStock($ProductStock);
734
            $ProductStock->setProductClass($ProductClass);
735
            if (!$ProductClass->getStockUnlimited()) {
736
                $ProductStock->setStock($ProductClass->getStock());
737
            } else {
738
                // 在庫無制限時はnullを設定
739
                $ProductStock->setStock(null);
740
            }
741
            $app['orm.em']->persist($ProductStock);
742
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
743
        }
744
745
        // 商品税率が設定されている場合、商品税率をセット
746
        if ($BaseInfo->getOptionProductTaxRule() == Constant::ENABLED) {
747
            // 初期設定の税設定.
748
            $TaxRule = $app['eccube.repository.tax_rule']->find(\Eccube\Entity\TaxRule::DEFAULT_TAX_RULE_ID);
749
            // 初期税率設定の計算方法を設定する
750
            $CalcRule = $TaxRule->getCalcRule();
751
            foreach ($ProductClasses as $ProductClass) {
752
                if ($ProductClass->getTaxRate() !== false && $ProductClass !== null) {
753
                    $TaxRule = new \Eccube\Entity\TaxRule();
754
                    $TaxRule->setProduct($Product);
755
                    $TaxRule->setProductClass($ProductClass);
756
                    $TaxRule->setCalcRule($CalcRule);
757
                    $TaxRule->setTaxRate($ProductClass->getTaxRate());
758
                    $TaxRule->setTaxAdjust(0);
759
                    $TaxRule->setApplyDate(new \DateTime());
760
                    $TaxRule->setDelFlg(Constant::DISABLED);
761
                    $app['orm.em']->persist($TaxRule);
762
                }
763
            }
764
        }
765
766
    }
767
768
    /**
769
     * 規格の分類判定
770
     *
771
     * @param $class_name
772
     * @return boolean
773
     */
774
    private function isValiedCategory($class_name)
775
    {
776
        if (empty($class_name)) {
777
            return true;
778
        }
779
        if (count($class_name->getClassCategories()) < 1) {
780
            return false;
781
        }
782
        return true;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
783
    }
784
}
785