Failed Conditions
Push — master ( 746c21...bb3715 )
by Kentaro
35:35
created

ProductClassController::insertProductClass()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 47
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 0
cts 0
cp 0
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 28
nc 12
nop 3
crap 56
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
    public function index(Application $app, Request $request, $id)
47
    {
48
49
        /** @var $Product \Eccube\Entity\Product */
50
        $Product = $app['eccube.repository.product']->find($id);
51
        $hasClassCategoryFlg = false;
52
53
        if (!$Product) {
54
            throw new NotFoundHttpException();
55
        }
56
57
58
        // 商品規格情報が存在しなければ新規登録させる
59
        if (!$Product->hasProductClass()) {
60
            // 登録画面を表示
61
62
            $builder = $app['form.factory']->createBuilder();
63
64
            $builder
65
                ->add('class_name1', 'entity', array(
66
                    'class' => 'Eccube\Entity\ClassName',
67
                    'property' => 'name',
68
                    'empty_value' => '規格1を選択',
69
                    'constraints' => array(
70
                        new Assert\NotBlank(),
71
                    ),
72
                ))
73
                ->add('class_name2', 'entity', array(
74
                    'class' => 'Eccube\Entity\ClassName',
75
                    'property' => 'name',
76
                    'empty_value' => '規格2を選択',
77
                    'required' => false,
78
                ));
79
80
            $event = new EventArgs(
81
                array(
82
                    'builder' => $builder,
83
                    'Product' => $Product,
84
                ),
85
                $request
86
            );
87
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_INDEX_INITIALIZE, $event);
88
89
            $form = $builder->getForm();
90
91
            $productClassForm = null;
92
93
            if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
94
95
                $form->handleRequest($request);
96
97
                if ($form->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
98
99
                    $data = $form->getData();
100
101
                    $ClassName1 = $data['class_name1'];
102
                    $ClassName2 = $data['class_name2'];
103
104
                    // 各規格が選択されている際に、分類を保有しているか確認
105
                    $class1Valied = $this->isValiedCategory($ClassName1);
106
                    $class2Valied = $this->isValiedCategory($ClassName2);
107
108
                    // 規格が選択されていないか、選択された状態で分類が保有されていれば、画面表示
109
                    if($class1Valied && $class2Valied){
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
110
                        $hasClassCategoryFlg = true;
111
                    }
112
113
                    if (!is_null($ClassName2) && $ClassName1->getId() == $ClassName2->getId()) {
114
                        // 規格1と規格2が同じ値はエラー
115
116
                        $form['class_name2']->addError(new FormError('規格1と規格2は、同じ値を使用できません。'));
117
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
118
                    } else {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
119
120
                        // 規格分類が設定されていない商品規格を取得
121
                        $orgProductClasses = $Product->getProductClasses();
122
                        $sourceProduct = $orgProductClasses[0];
123
124
                        // 規格分類が組み合わされた商品規格を取得
125
                        $ProductClasses = $this->createProductClasses($app, $Product, $ClassName1, $ClassName2);
126
127
                        // 組み合わされた商品規格にデフォルト値をセット
128
                        foreach ($ProductClasses as $productClass) {
129
                            $this->setDefualtProductClass($app, $productClass, $sourceProduct);
130
                        }
131
132
133
                        $builder = $app['form.factory']->createBuilder();
134
135
                        $builder
136
                            ->add('product_classes', 'collection', array(
137
                                'type' => 'admin_product_class',
138
                                'allow_add' => true,
139
                                'allow_delete' => true,
140
                                'data' => $ProductClasses,
141
                             ));
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...
142
143
                        $event = new EventArgs(
144
                            array(
145
                                'builder' => $builder,
146
                                'Product' => $Product,
147
                                'ProductClasses' => $ProductClasses,
148
                            ),
149
                            $request
150
                        );
151
                        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_INDEX_CLASSES, $event);
152
153
                        $productClassForm = $builder->getForm()->createView();
154
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
155
                    }
156
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
157
                }
158
            }
159
160
            return $app->render('Product/product_class.twig', array(
161
                'form' => $form->createView(),
162
                'classForm' => $productClassForm,
163
                'Product' => $Product,
164
                'not_product_class' => true,
165
                'error' => null,
166
                'has_class_category_flg' => $hasClassCategoryFlg,
167
            ));
168
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
169
        } else {
170
            // 既に商品規格が登録されている場合、商品規格画面を表示する
171
172
            // 既に登録されている商品規格を取得
173
            $ProductClasses = $this->getProductClassesExcludeNonClass($Product);
174
175
            // 設定されている規格分類1、2を取得(商品規格の規格分類には必ず同じ値がセットされている)
176
            $ProductClass = $ProductClasses[0];
177
            $ClassName1 = $ProductClass->getClassCategory1()->getClassName();
178
            $ClassName2 = null;
179
            if (!is_null($ProductClass->getClassCategory2())) {
180
                $ClassName2 = $ProductClass->getClassCategory2()->getClassName();
181
            }
182
183
            // 規格分類が組み合わされた空の商品規格を取得
184
            $createProductClasses = $this->createProductClasses($app, $Product, $ClassName1, $ClassName2);
185
186
187
            $mergeProductClasses = array();
188
189
            // 商品税率が設定されている場合、商品税率を項目に設定
190
            $BaseInfo = $app['eccube.repository.base_info']->get();
191 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...
192
                foreach ($ProductClasses as $class) {
193
                    if ($class->getTaxRule() && !$class->getTaxRule()->getDelFlg()) {
194
                        $class->setTaxRate($class->getTaxRule()->getTaxRate());
195
                    }
196
                }
197
            }
198
199
200
            // 登録済み商品規格と空の商品規格をマージ
201
            $flag = false;
202
            foreach ($createProductClasses as $createProductClass) {
203
                // 既に登録済みの商品規格にチェックボックスを設定
204
                foreach ($ProductClasses as $productClass) {
205
                    if ($productClass->getClassCategory1() == $createProductClass->getClassCategory1() &&
206
                            $productClass->getClassCategory2() == $createProductClass->getClassCategory2()) {
207
                                // チェックボックスを追加
208
                                $productClass->setAdd(true);
209
                                $flag = true;
210
                                break;
211
                    }
212
                }
213
214
                if (!$flag) {
215
                    $mergeProductClasses[] = $createProductClass;
216
                }
217
218
                $flag = false;
219
            }
220
221
            // 登録済み商品規格と空の商品規格をマージ
222
            foreach ($mergeProductClasses as $mergeProductClass) {
223
                // 空の商品規格にデフォルト値を設定
224
                $this->setDefualtProductClass($app, $mergeProductClass, $ProductClass);
225
                $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...
226
            }
227
228
            $builder = $app['form.factory']->createBuilder();
229 1
230
            $builder
231
                ->add('product_classes', 'collection', array(
232
                    'type' => 'admin_product_class',
233
                    'allow_add' => true,
234
                    'allow_delete' => true,
235 1
                    'data' => $ProductClasses,
236
                ));
237
238
            $event = new EventArgs(
239 1
                array(
240
                    'builder' => $builder,
241
                    'Product' => $Product,
242
                    'ProductClasses' => $ProductClasses,
243
                ),
244
                $request
245
            );
246
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_INDEX_CLASSES, $event);
247
248
            $productClassForm = $builder->getForm()->createView();
249
250
            return $app->render('Product/product_class.twig', array(
251
                'classForm' => $productClassForm,
252
                'Product' => $Product,
253 1
                'class_name1' => $ClassName1,
254 1
                'class_name2' => $ClassName2,
255
                'not_product_class' => false,
256
                'error' => null,
257
                'has_class_category_flg' => true,
258
            ));
259
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
260
        }
261
262
    }
263
264
265
    /**
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...
266
     * 商品規格の登録、更新、削除を行う
267
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
268
    public function edit(Application $app, Request $request, $id)
269
    {
270
271
        /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */
272
        $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete');
273
        $softDeleteFilter->setExcludes(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
274
            'Eccube\Entity\TaxRule'
275
        ));
276
277
        /** @var $Product \Eccube\Entity\Product */
278
        $Product = $app['eccube.repository.product']->find($id);
279
280
        if (!$Product) {
281
            throw new NotFoundHttpException();
282
        }
283
284
        $builder = $app['form.factory']->createBuilder();
285
        $builder
286
                ->add('product_classes', 'collection', array(
287
                    'type' => 'admin_product_class',
288
                    'allow_add' => true,
289
                    'allow_delete' => true,
290
            ));
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...
291
292
        $event = new EventArgs(
293
            array(
294
                'builder' => $builder,
295
                'Product' => $Product,
296
            ),
297
            $request
298
        );
299
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_INITIALIZE, $event);
300 1
301
        $form = $builder->getForm();
302
303
        $ProductClasses = $this->getProductClassesExcludeNonClass($Product);
304
305
        if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
306
307
            $form->handleRequest($request);
308
309
            switch ($request->get('mode')) {
310
                case 'edit':
311
                    // 新規登録
312
313 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...
314
                        // 既に登録されていれば最初の画面に戻す
315
                        return $app->redirect($app->url('admin_product_product_class', array('id' => $id)));
316
                    }
317
318
                    $addProductClasses = array();
319
320
                    $tmpProductClass = null;
321 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...
322
                        // 追加対象の行をvalidate
323
                        $ProductClass = $formData->getData();
324
325
                        if ($ProductClass->getAdd()) {
326
                            if ($formData->isValid()) {
327
                                $addProductClasses[] = $ProductClass;
328
                            } else {
329
                                // 対象行のエラー
330
                                return $this->render($app, $Product, $ProductClass, true, $form);
331
                            }
332
                        }
333
                        $tmpProductClass = $ProductClass;
334
                    }
335
336 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...
337
                        // 対象がなければエラー
338
                        $error = array('message' => '商品規格が選択されていません。');
339
                        return $this->render($app, $Product, $tmpProductClass, true, $form, $error);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
340
                    }
341
342
                    // 選択された商品規格を登録
343
                    $this->insertProductClass($app, $Product, $addProductClasses);
344
345
                    // デフォルトの商品規格を更新
346
                    $defaultProductClass = $app['eccube.repository.product_class']
347
                            ->findOneBy(array('Product' => $Product, 'ClassCategory1' => null, 'ClassCategory2' => null));
348
349
                    $defaultProductClass->setDelFlg(Constant::ENABLED);
350
351
                    $app['orm.em']->flush();
352
353
                    $event = new EventArgs(
354
                        array(
355
                            'form' => $form,
356
                            'Product' => $Product,
357
                            'defaultProductClass' => $defaultProductClass,
358
                        ),
359
                        $request
360
                    );
361
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_COMPLETE, $event);
362
363
                    $app->addSuccess('admin.product.product_class.save.complete', 'admin');
364
365
                    break;
366
                case 'update':
367
                    // 更新
368
369 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...
370
                        // 商品規格が0件であれば最初の画面に戻す
371
                        return $app->redirect($app->url('admin_product_product_class', array('id' => $id)));
372
                    }
373
374
                    $checkProductClasses = array();
375
                    $removeProductClasses = array();
376
377
                    $tempProductClass = null;
378 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...
379
                        // 追加対象の行をvalidate
380
                        $ProductClass = $formData->getData();
381
382
                        if ($ProductClass->getAdd()) {
383
                            if ($formData->isValid()) {
384
                                $checkProductClasses[] = $ProductClass;
385
                            } else {
386
                                return $this->render($app, $Product, $ProductClass, false, $form);
387
                            }
388
                        } else {
389 1
                            // 削除対象の行
390
                            $removeProductClasses[] = $ProductClass;
391
                        }
392
                        $tempProductClass = $ProductClass;
393
                    }
394
395 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...
396
                        // 対象がなければエラー
397
                        $error = array('message' => '商品規格が選択されていません。');
398
                        return $this->render($app, $Product, $tempProductClass, false, $form, $error);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
399
                    }
400
401
402
                    // 登録対象と更新対象の行か判断する
403
                    $addProductClasses = array();
404
                    $updateProductClasses = array();
405
                    foreach ($checkProductClasses as $cp) {
406
                        $flag = false;
407
                        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
408
                        // 既に登録済みの商品規格か確認
409
                        foreach ($ProductClasses as $productClass) {
410
                            if ($productClass->getProduct()->getId() == $id &&
411
                                    $productClass->getClassCategory1() == $cp->getClassCategory1() &&
412
                                    $productClass->getClassCategory2() == $cp->getClassCategory2()) {
413
                                $updateProductClasses[] = $cp;
414
415
                                // 商品情報
416
                                $cp->setProduct($Product);
417
                                // 商品在庫
418
                                $productStock = $productClass->getProductStock();
419
                                if (!$cp->getStockUnlimited()) {
420 1
                                    $productStock->setStock($cp->getStock());
421
                                } else {
422
                                    $productStock->setStock(null);
423
                                }
424
                                $this->setDefualtProductClass($app, $productClass, $cp);
425
                                $flag = true;
426 1
                                break;
427
                            }
428
                        }
429
                        if (!$flag) {
430
                            $addProductClasses[] = $cp;
431
                        }
432
                    }
433
434
                    foreach ($removeProductClasses as $rc) {
435
                        // 登録されている商品規格に削除フラグをセット
436
                        foreach ($ProductClasses as $productClass) {
437
                            if ($productClass->getProduct()->getId() == $id &&
438
                                    $productClass->getClassCategory1() == $rc->getClassCategory1() &&
439
                                    $productClass->getClassCategory2() == $rc->getClassCategory2()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
440
441
                                $productClass->setDelFlg(Constant::ENABLED);
442
                                break;
443
                            }
444
                        }
445
                    }
446
447
                    // 選択された商品規格を登録
448
                    $this->insertProductClass($app, $Product, $addProductClasses);
449
450
                    $app['orm.em']->flush();
451
452
                    $event = new EventArgs(
453
                        array(
454
                            'form' => $form,
455
                            'Product' => $Product,
456
                            'updateProductClasses' => $updateProductClasses,
457
                            'addProductClasses' => $addProductClasses,
458
                        ),
459
                        $request
460
                    );
461
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_UPDATE, $event);
462
463
                    $app->addSuccess('admin.product.product_class.update.complete', 'admin');
464
465
                    break;
466
                case 'delete':
467
                    // 削除
468
469 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...
470
                        // 既に商品が削除されていれば元の画面に戻す
471
                        return $app->redirect($app->url('admin_product_product_class', array('id' => $id)));
472
                    }
473
474
                    foreach ($ProductClasses as $ProductClass) {
475
                        // 登録されている商品規格に削除フラグをセット
476
                        $ProductClass->setDelFlg(Constant::ENABLED);
477
                    }
478
479
                    /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */
480
                    $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete');
481
                    $softDeleteFilter->setExcludes(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
482
                        'Eccube\Entity\ProductClass'
483
                    ));
484
485
                    // デフォルトの商品規格を更新
486
                    $defaultProductClass = $app['eccube.repository.product_class']
487
                            ->findOneBy(array('Product' => $Product, 'ClassCategory1' => null, 'ClassCategory2' => null, 'del_flg' => Constant::ENABLED));
488
489
                    $defaultProductClass->setDelFlg(Constant::DISABLED);
490
491
                    $app['orm.em']->flush();
492
493
                    $event = new EventArgs(
494
                        array(
495
                            'form' => $form,
496
                            'Product' => $Product,
497
                            'defaultProductClass' => $defaultProductClass,
498
                        ),
499
                        $request
500
                    );
501
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_DELETE, $event);
502
503
                    $app->addSuccess('admin.product.product_class.delete.complete', 'admin');
504
505
                    break;
506
                default:
507
                    break;
508
            }
509
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
510
        }
511
512
        return $app->redirect($app->url('admin_product_product_class', array('id' => $id)));
513
    }
514
515
516
517
    /**
518
     * 登録、更新時のエラー画面表示
519
     *
520
     */
521
    protected function render($app, $Product, $ProductClass, $not_product_class, $classForm, $error = null)
522
    {
523
524
        $ClassName1 = null;
525
        $ClassName2 = null;
526
        // 規格を取得
527
        if (isset($ProductClass)) {
528
            $ClassCategory1 = $ProductClass->getClassCategory1();
529
            if ($ClassCategory1) {
530
                $ClassName1 = $ClassCategory1->getClassName();
531
            }
532
            $ClassCategory2 = $ProductClass->getClassCategory2();
533
            if ($ClassCategory2) {
534
                $ClassName2 = $ClassCategory2->getClassName();
535
            }
536
        }
537
538
        $form = $app->form()
539
            ->add('class_name1', 'entity', array(
540
                'class' => 'Eccube\Entity\ClassName',
541
                'property' => 'name',
542
                'empty_value' => '規格1を選択',
543
                'data' => $ClassName1,
544
            ))
545
            ->add('class_name2', 'entity', array(
546
                'class' => 'Eccube\Entity\ClassName',
547
                'property' => 'name',
548
                'empty_value' => '規格2を選択',
549
                'data' => $ClassName2,
550
            ))
551
            ->getForm();
552
553
554 1
        return $app->render('Product/product_class.twig', array(
555
            'form' => $form->createView(),
556
            'classForm' => $classForm->createView(),
557
            'Product' => $Product,
558
            'class_name1' => $ClassName1,
559
            'class_name2' => $ClassName2,
560
            'not_product_class' => $not_product_class,
561
            'error' => $error,
562 1
            'has_class_category_flg' => true,
563
        ));
564
    }
565
566
567
    /**
568
     * 規格1と規格2を組み合わせた商品規格を作成
569
     */
570
    private function createProductClasses($app, Product $Product, ClassName $ClassName1 = null, ClassName $ClassName2 = null)
571
    {
572
573
        $ClassCategories1 = array();
574
        if ($ClassName1) {
575
            $ClassCategories1 = $app['eccube.repository.class_category']->findBy(array('ClassName' => $ClassName1));
576
        }
577
578
        $ClassCategories2 = array();
579
        if ($ClassName2) {
580
            $ClassCategories2 = $app['eccube.repository.class_category']->findBy(array('ClassName' => $ClassName2));
581
        }
582
583
        $ProductClasses = array();
584
        foreach ($ClassCategories1 as $ClassCategory1) {
585
            if ($ClassCategories2) {
586
                foreach ($ClassCategories2 as $ClassCategory2) {
587
                    $ProductClass = $this->newProductClass($app);
588
                    $ProductClass->setProduct($Product);
589
                    $ProductClass->setClassCategory1($ClassCategory1);
590
                    $ProductClass->setClassCategory2($ClassCategory2);
591
                    $ProductClass->setTaxRate(null);
592
                    $ProductClass->setDelFlg(Constant::DISABLED);
593
                    $ProductClasses[] = $ProductClass;
594
                }
595
            } else {
596
                $ProductClass = $this->newProductClass($app);
597
                $ProductClass->setProduct($Product);
598
                $ProductClass->setClassCategory1($ClassCategory1);
599
                $ProductClass->setTaxRate(null);
600
                $ProductClass->setDelFlg(Constant::DISABLED);
601
                $ProductClasses[] = $ProductClass;
602
            }
603
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
604
        }
605
        return $ProductClasses;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
606
    }
607
608
    /**
609
     * 新しい商品規格を作成
610
     */
611
    private function newProductClass(Application $app)
612
    {
613
        $ProductType = $app['eccube.repository.master.product_type']->find($app['config']['product_type_normal']);
614
615
        $ProductClass = new ProductClass();
616
        $ProductClass->setProductType($ProductType);
617
        return $ProductClass;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
618
    }
619
620
    /**
621
     * 商品規格のコピーを取得.
622
     *
623
     * @see http://symfony.com/doc/current/cookbook/form/form_collections.html
624
     * @param Product $Product
625
     * @return \Eccube\Entity\ProductClass[]
626
     */
627
    private function getProductClassesOriginal(Product $Product)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
628
    {
629
        $ProductClasses = $Product->getProductClasses();
630
        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...
631
            return true;
632
        });
633
    }
634
635
    /**
636
     * 規格なし商品を除いて商品規格を取得.
637
     *
638
     * @param Product $Product
639
     * @return \Eccube\Entity\ProductClass[]
640
     */
641
    private function getProductClassesExcludeNonClass(Product $Product)
642
    {
643
        $ProductClasses = $Product->getProductClasses();
644
        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...
645
            $ClassCategory1 = $ProductClass->getClassCategory1();
646
            $ClassCategory2 = $ProductClass->getClassCategory2();
647
            return ($ClassCategory1 || $ClassCategory2);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
648
        });
649
    }
650
651
    /**
652
     * デフォルトとなる商品規格を設定
653
     *
654
     * @param $productClassDest コピー先となる商品規格
655
     * @param $productClassOrig コピー元となる商品規格
656
     */
657
    private function setDefualtProductClass($app, $productClassDest, $productClassOrig) {
658
        $productClassDest->setDeliveryDate($productClassOrig->getDeliveryDate());
659
        $productClassDest->setProduct($productClassOrig->getProduct());
660
        $productClassDest->setProductType($productClassOrig->getProductType());
661
        $productClassDest->setCode($productClassOrig->getCode());
662
        $productClassDest->setStock($productClassOrig->getStock());
663
        $productClassDest->setStockUnlimited($productClassOrig->getStockUnlimited());
664
        $productClassDest->setSaleLimit($productClassOrig->getSaleLimit());
665
        $productClassDest->setPrice01($productClassOrig->getPrice01());
666
        $productClassDest->setPrice02($productClassOrig->getPrice02());
667
        $productClassDest->setDeliveryFee($productClassOrig->getDeliveryFee());
668
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
669
        // 個別消費税
670
        $BaseInfo = $app['eccube.repository.base_info']->get();
671
        if ($BaseInfo->getOptionProductTaxRule() == Constant::ENABLED) {
672
            if ($productClassOrig->getTaxRate() !== false && $productClassOrig->getTaxRate() !== null) {
673
                $productClassDest->setTaxRate($productClassOrig->getTaxRate());
674 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...
675
                    $productClassDest->getTaxRule()->setTaxRate($productClassOrig->getTaxRate());
676
                    $productClassDest->getTaxRule()->setDelFlg(Constant::DISABLED);
677
                } else {
678
                    $taxrule = $app['eccube.repository.tax_rule']->newTaxRule();
679
                    $taxrule->setTaxRate($productClassOrig->getTaxRate());
680
                    $taxrule->setApplyDate(new \DateTime());
681
                    $taxrule->setProduct($productClassDest->getProduct());
682
                    $taxrule->setProductClass($productClassDest);
683
                    $productClassDest->setTaxRule($taxrule);
684
                }
685
            } else {
686
                if ($productClassDest->getTaxRule()) {
687
                    $productClassDest->getTaxRule()->setDelFlg(Constant::ENABLED);
688
                }
689
            }
690
        }
691
    }
692
693
694
    /**
695
     * 商品規格を登録
696
     *
697
     * @param $ProductClasses 登録される商品規格
698
     */
699
    private function insertProductClass($app, $Product, $ProductClasses) {
700
701
        $BaseInfo = $app['eccube.repository.base_info']->get();
702
703
        // 選択された商品を登録
704
        foreach ($ProductClasses as $ProductClass) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
705
706
            $ProductClass->setDelFlg(Constant::DISABLED);
707
            $ProductClass->setProduct($Product);
708
            $app['orm.em']->persist($ProductClass);
709
710
            // 在庫情報を作成
711
            $ProductStock = new \Eccube\Entity\ProductStock();
712
            $ProductClass->setProductStock($ProductStock);
713
            $ProductStock->setProductClass($ProductClass);
714
            if (!$ProductClass->getStockUnlimited()) {
715
                $ProductStock->setStock($ProductClass->getStock());
716
            } else {
717
                // 在庫無制限時はnullを設定
718
                $ProductStock->setStock(null);
719
            }
720
            $app['orm.em']->persist($ProductStock);
721
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
722
        }
723
724
        // 商品税率が設定されている場合、商品税率をセット
725
        if ($BaseInfo->getOptionProductTaxRule() == Constant::ENABLED) {
726
            // 初期設定の税設定.
727
            $TaxRule = $app['eccube.repository.tax_rule']->find(\Eccube\Entity\TaxRule::DEFAULT_TAX_RULE_ID);
728
            // 初期税率設定の計算方法を設定する
729
            $CalcRule = $TaxRule->getCalcRule();
730
            foreach ($ProductClasses as $ProductClass) {
731
                if ($ProductClass->getTaxRate() !== false && $ProductClass !== null) {
732
                    $TaxRule = new \Eccube\Entity\TaxRule();
733
                    $TaxRule->setProduct($Product);
734
                    $TaxRule->setProductClass($ProductClass);
735
                    $TaxRule->setCalcRule($CalcRule);
736
                    $TaxRule->setTaxRate($ProductClass->getTaxRate());
737
                    $TaxRule->setTaxAdjust(0);
738
                    $TaxRule->setApplyDate(new \DateTime());
739
                    $TaxRule->setDelFlg(Constant::DISABLED);
740
                    $app['orm.em']->persist($TaxRule);
741
                }
742
            }
743
        }
744
745
    }
746
747
    /**
748
     * 規格の分類判定
749
     *
750
     * @param $class_name
751
     * @return boolean
752
     */
753
    private function isValiedCategory($class_name)
754
    {
755
        if (empty($class_name)) {
756
            return true;
757
        }
758
        if (count($class_name->getClassCategories()) < 1) {
759
            return false;
760
        }
761
        return true;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
762
    }
763
}
764