Failed Conditions
Push — experimental/3.1 ( 965511...751c7a )
by chihiro
21s
created

ShippingMultipleController::getCustomerAddressId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2017 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
namespace Eccube\Controller;
25
26
27
use Eccube\Application;
28
use Eccube\Common\Constant;
29
use Eccube\Entity\CustomerAddress;
30
use Eccube\Entity\Master\OrderItemType;
31
use Eccube\Entity\ShipmentItem;
32
use Eccube\Entity\Shipping;
33
use Eccube\Event\EccubeEvents;
34
use Eccube\Event\EventArgs;
35
use Eccube\Form\Type\ShippingMultipleType;
36
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
37
use Symfony\Component\HttpFoundation\Request;
38
39
class ShippingMultipleController extends AbstractShoppingController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
40
{
41
42
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
43
     * 複数配送処理
44
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
45
    public function index(Application $app, Request $request)
46
    {
47
        // カートチェック
48
        $response = $app->forward($app->path("shopping/checkToCart"));
49
        if ($response->isRedirection() || $response->getContent()) {
50
            return $response;
51
        }
52
53
        /** @var \Eccube\Entity\Order $Order */
54
        $Order = $app['eccube.service.shopping']->getOrder($app['config']['order_processing']);
55
        if (!$Order) {
56
            log_info('購入処理中の受注情報がないため購入エラー');
57
            $app->addError('front.shopping.order.error');
58
            return $app->redirect($app->url('shopping_error'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
59
        }
60
61
        // 処理しやすいようにすべてのShippingItemをまとめる
62
        $ShipmentItems = array();
63
        foreach ($Order->getShippings() as $Shipping) {
64
            foreach ($Shipping->getProductOrderItems() as $ShipmentItem) {
65
                $ShipmentItems[] = $ShipmentItem;
66
            }
67
        }
68
69
        // Orderに含まれる商品ごとの数量を求める
70
        $ItemQuantitiesByClassId = array();
71
        foreach ($ShipmentItems as $item) {
72
            $itemId = $item->getProductClass()->getId();
73
            $quantity = $item->getQuantity();
74
            if (array_key_exists($itemId, $ItemQuantitiesByClassId)) {
75
                $ItemQuantitiesByClassId[$itemId] += $quantity;
76
            } else {
77
                $ItemQuantitiesByClassId[$itemId] = $quantity;
78
            }
79
        }
80
81
        // FormBuilder用に商品ごとにShippingItemをまとめる
82
        $ShipmentItemsForFormBuilder = array();
83
        $tmpAddedClassIds = array();
84
        foreach ($ShipmentItems as $item) {
85
            $itemId = $item->getProductClass()->getId();
86
            if (!in_array($itemId, $tmpAddedClassIds)) {
87
                $ShipmentItemsForFormBuilder[] = $item;
88
                $tmpAddedClassIds[] = $itemId;
89
            }
90
        }
91
92
        // Form生成
93
        $builder = $app->form();
94
        $builder
95
            ->add('shipping_multiple', CollectionType::class, array(
96
                'entry_type' => ShippingMultipleType::class,
97
                'data' => $ShipmentItemsForFormBuilder,
98
                'allow_add' => true,
99
                'allow_delete' => true,
100
            ));
101
        // Event
102
        $event = new EventArgs(
103
            array(
104
                'builder' => $builder,
105
                'Order' => $Order,
106
            ),
107
            $request
108
        );
109
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_SHIPPING_MULTIPLE_INITIALIZE, $event);
110
111
        $form = $builder->getForm();
112
        $form->handleRequest($request);
113
114
        $errors = array();
115
        if ($form->isSubmitted() && $form->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
116
117
            log_info('複数配送設定処理開始', array($Order->getId()));
118
119
            $data = $form['shipping_multiple'];
120
121
            // フォームの入力から、送り先ごとに商品の数量を集計する
122
            $arrShipmentItemTemp = array();
123
            foreach ($data as $mulitples) {
124
                $ShipmentItem = $mulitples->getData();
125
                foreach ($mulitples as $items) {
126
                    foreach ($items as $item) {
127
                        $cusAddId = $this->getCustomerAddressId($item['customer_address']->getData());
128
                        $itemId = $ShipmentItem->getProductClass()->getId();
129
                        $quantity = $item['quantity']->getData();
130
131
                        if (isset($arrShipmentItemTemp[$cusAddId]) && array_key_exists($itemId, $arrShipmentItemTemp[$cusAddId])) {
132
                            $arrShipmentItemTemp[$cusAddId][$itemId] = $arrShipmentItemTemp[$cusAddId][$itemId] + $quantity;
133
                        } else {
134
                            $arrShipmentItemTemp[$cusAddId][$itemId] = $quantity;
135
                        }
136
                    }
137
                }
138
            }
139
140
            // フォームの入力から、商品ごとの数量を集計する
141
            $itemQuantities = array();
142
            foreach ($arrShipmentItemTemp as $FormItemByAddress) {
143
                foreach ($FormItemByAddress as $itemId => $quantity) {
144
                    if (array_key_exists($itemId, $itemQuantities)) {
145
                        $itemQuantities[$itemId] = $itemQuantities[$itemId] + $quantity;
146
                    } else {
147
                        $itemQuantities[$itemId] = $quantity;
148
                    }
149
                }
150
            }
151
152
            // 「Orderに含まれる商品ごとの数量」と「フォームに入力された商品ごとの数量」が一致しているかの確認
153
            // 数量が異なっているならエラーを表示する
154
            foreach ($ItemQuantitiesByClassId as $key => $value) {
155
                if (array_key_exists($key, $itemQuantities)) {
156
                    if ($itemQuantities[$key] != $value) {
157
                        $errors[] = array('message' => $app->trans('shopping.multiple.quantity.diff'));
158
159
                        // 対象がなければエラー
160
                        log_info('複数配送設定入力チェックエラー', array($Order->getId()));
161
                        return $app->render('Shopping/shipping_multiple.twig', array(
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
162
                            'form' => $form->createView(),
163
                            'shipmentItems' => $ShipmentItemsForFormBuilder,
164
                            'compItemQuantities' => $ItemQuantitiesByClassId,
165
                            'errors' => $errors,
166
                        ));
167
                    }
168
                }
169
            }
170
171
            // -- ここから先がお届け先を再生成する処理 --
172
173
            // お届け先情報をすべて削除
174
            foreach ($Order->getShippings() as $Shipping) {
175
                $app['orm.em']->remove($Shipping);
176
            }
177
178
            // お届け先のリストを作成する
179
            $ShippingList = array();
180
            foreach ($data as $mulitples) {
181
                $ShipmentItem = $mulitples->getData();
182
                $ProductClass = $ShipmentItem->getProductClass();
183
                $Delivery = $ShipmentItem->getShipping()->getDelivery();
184
                $productTypeId = $ProductClass->getProductType()->getId();
185
186
                foreach ($mulitples as $items) {
187
                    foreach ($items as $item) {
188
                        $CustomerAddress = $this->getCustomerAddress($app, $item['customer_address']->getData());
189
                        $cusAddId = $this->getCustomerAddressId($item['customer_address']->getData());
190
191
                        $Shipping = new Shipping();
192
                        $Shipping
193
                            ->setFromCustomerAddress($CustomerAddress)
194
                            ->setDelivery($Delivery)
195
                            ->setDelFlg(Constant::DISABLED);
196
197
                        $ShippingList[$cusAddId][$productTypeId] = $Shipping;
198
                    }
199
                }
200
            }
201
            // お届け先のリストを保存
202
            foreach ($ShippingList as $ShippingListByAddress) {
203
                foreach ($ShippingListByAddress as $Shipping) {
204
                    $app['orm.em']->persist($Shipping);
205
                }
206
            }
207
208
            $ProductOrderType = $app['eccube.repository.master.order_item_type']->find(OrderItemType::PRODUCT);
209
210
            // お届け先に、配送商品の情報(ShipmentItem)を関連付ける
211
            foreach ($data as $mulitples) {
212
                $ShipmentItem = $mulitples->getData();
213
                $ProductClass = $ShipmentItem->getProductClass();
214
                $Product = $ShipmentItem->getProduct();
215
                $productTypeId = $ProductClass->getProductType()->getId();
216
                $productClassId = $ProductClass->getId();
217
218
                foreach ($mulitples as $items) {
219
                    foreach ($items as $item) {
220
                        $cusAddId = $this->getCustomerAddressId($item['customer_address']->getData());
221
222
                        // お届け先から商品の数量を取得
223
                        $quantity = 0;
0 ignored issues
show
Unused Code introduced by
$quantity is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
224
                        if (isset($arrShipmentItemTemp[$cusAddId]) && array_key_exists($productClassId, $arrShipmentItemTemp[$cusAddId])) {
225
                            $quantity = $arrShipmentItemTemp[$cusAddId][$productClassId];
226
                            unset($arrShipmentItemTemp[$cusAddId][$productClassId]);
227
                        } else {
228
                            // この配送先には送る商品がないのでスキップ(通常ありえない)
229
                            continue;
230
                        }
231
232
                        // 関連付けるお届け先のインスタンスを取得
233
                        $Shipping = $ShippingList[$cusAddId][$productTypeId];
234
235
                        // インスタンスを生成して保存
236
                        $ShipmentItem = new ShipmentItem();
237
                        $ShipmentItem->setShipping($Shipping)
238
                            ->setOrder($Order)
239
                            ->setProductClass($ProductClass)
240
                            ->setProduct($Product)
241
                            ->setProductName($Product->getName())
242
                            ->setProductCode($ProductClass->getCode())
243
                            ->setPrice($ProductClass->getPrice02())
244
                            ->setQuantity($quantity)
245
                            ->setOrderItemType($ProductOrderType);
246
247
                        $ClassCategory1 = $ProductClass->getClassCategory1();
248
                        if (!is_null($ClassCategory1)) {
249
                            $ShipmentItem->setClasscategoryName1($ClassCategory1->getName());
250
                            $ShipmentItem->setClassName1($ClassCategory1->getClassName()->getName());
251
                        }
252
                        $ClassCategory2 = $ProductClass->getClassCategory2();
253
                        if (!is_null($ClassCategory2)) {
254
                            $ShipmentItem->setClasscategoryName2($ClassCategory2->getName());
255
                            $ShipmentItem->setClassName2($ClassCategory2->getClassName()->getName());
256
                        }
257
                        $Shipping->addShipmentItem($ShipmentItem);
258
                        $app['orm.em']->persist($ShipmentItem);
259
                    }
260
                }
261
            }
262
263
            // 送料を計算(お届け先ごと)
264
            foreach ($ShippingList as $data) {
265
                // data is product type => shipping
266
                foreach ($data as $Shipping) {
267
                    // 配送料金の設定
268
                    $app['eccube.service.shopping']->setShippingDeliveryFee($Shipping);
269
                }
270
            }
271
272
            // 合計金額の再計算
273
            $flowResult = $this->executePurchaseFlow($app, $Order);
274
            if ($flowResult->hasWarning() || $flowResult->hasError()) {
275
                return $app->redirect($app->url('shopping_error'));
276
            }
277
278
            // 配送先を更新
279
            $app['orm.em']->flush();
280
281
            $event = new EventArgs(
282
                array(
283
                    'form' => $form,
284
                    'Order' => $Order,
285
                ),
286
                $request
287
            );
288
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_SHIPPING_MULTIPLE_COMPLETE, $event);
289
290
            log_info('複数配送設定処理完了', array($Order->getId()));
291
            return $app->redirect($app->url('shopping'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
292
        }
293
294
        return $app->render('Shopping/shipping_multiple.twig', array(
295
            'form' => $form->createView(),
296
            'shipmentItems' => $ShipmentItemsForFormBuilder,
297
            'compItemQuantities' => $ItemQuantitiesByClassId,
298
            'errors' => $errors,
299
        ));
300
    }
301
302
    /**
303
     * フォームの情報からお届け先のインデックスを返す
304
     * @param mixed $CustomerAddressData
305
     * @return int
306
     */
307
    private function getCustomerAddressId($CustomerAddressData)
308
    {
309
        if ($CustomerAddressData instanceof CustomerAddress) {
310
            return $CustomerAddressData->getId();
311
        } else {
312
            return $CustomerAddressData;
313
        }
314
    }
315
316
    /**
317
     * フォームの情報からお届け先のインスタンスを返す
318
     *
319
     * @param Application $app
320
     * @param mixed $CustomerAddressData
321
     * @return CustomerAddress
322
     */
323
    private function getCustomerAddress(Application $app, $CustomerAddressData)
324
    {
325
        if ($CustomerAddressData instanceof CustomerAddress) {
326
            return $CustomerAddressData;
327
        } else {
328
            $cusAddId = $CustomerAddressData;
329
            $customerAddresses = $app['session']->get($this->sessionCustomerAddressKey);
330
            $customerAddresses = unserialize($customerAddresses);
331
332
            $CustomerAddress = $customerAddresses[$cusAddId];
333
            $pref = $app['eccube.repository.master.pref']->find($CustomerAddress->getPref()->getId());
334
            $CustomerAddress->setPref($pref);
335
336
            return $CustomerAddress;
337
        }
338
    }
339
}