Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ShoppingService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ShoppingService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
45 | class ShoppingService |
||
|
|||
46 | { |
||
47 | /** @var \Eccube\Application */ |
||
48 | public $app; |
||
49 | |||
50 | /** @var \Eccube\Service\CartService */ |
||
51 | protected $cartService; |
||
52 | |||
53 | /** @var \Eccube\Service\OrderService */ |
||
54 | protected $orderService; |
||
55 | |||
56 | /** @var \Eccube\Entity\BaseInfo */ |
||
57 | protected $BaseInfo; |
||
58 | |||
59 | /** @var \Doctrine\ORM\EntityManager */ |
||
60 | protected $em; |
||
61 | |||
62 | 61 | public function __construct(Application $app, $cartService, $orderService) |
|
69 | |||
70 | /** |
||
71 | * セッションにセットされた受注情報を取得 |
||
72 | * |
||
73 | * @param null $status |
||
74 | * @return null|object |
||
75 | */ |
||
76 | 38 | public function getOrder($status = null) |
|
100 | |||
101 | /** |
||
102 | * 非会員情報を取得 |
||
103 | * |
||
104 | * @param $sesisonKey |
||
105 | * @return $Customer|null |
||
106 | */ |
||
107 | 14 | public function getNonMember($sesisonKey) |
|
122 | |||
123 | /** |
||
124 | * 受注情報を作成 |
||
125 | * |
||
126 | * @param $Customer |
||
127 | * @return \Eccube\Entity\Order |
||
128 | */ |
||
129 | 39 | public function createOrder($Customer) |
|
130 | { |
||
131 | // ランダムなpre_order_idを作成 |
||
132 | do { |
||
133 | 39 | $preOrderId = sha1(Str::random(32)); |
|
134 | 39 | $Order = $this->app['eccube.repository.order']->findOneBy(array( |
|
135 | 39 | 'pre_order_id' => $preOrderId, |
|
136 | 39 | 'OrderStatus' => $this->app['config']['order_processing'], |
|
137 | )); |
||
138 | 39 | } while ($Order); |
|
139 | |||
140 | // 受注情報、受注明細情報、お届け先情報、配送商品情報を作成 |
||
141 | 39 | $Order = $this->registerPreOrder( |
|
142 | $Customer, |
||
143 | $preOrderId); |
||
144 | |||
145 | 39 | $this->cartService->setPreOrderId($preOrderId); |
|
146 | 39 | $this->cartService->save(); |
|
147 | |||
148 | 39 | return $Order; |
|
149 | } |
||
150 | |||
151 | /** |
||
152 | * 仮受注情報作成 |
||
153 | * |
||
154 | * @param $Customer |
||
155 | * @param $preOrderId |
||
156 | * @return mixed |
||
157 | * @throws \Doctrine\ORM\NoResultException |
||
158 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
159 | */ |
||
160 | 39 | public function registerPreOrder(Customer $Customer, $preOrderId) |
|
161 | { |
||
162 | |||
163 | 39 | $this->em = $this->app['orm.em']; |
|
164 | |||
165 | // 受注情報を作成 |
||
166 | 39 | $Order = $this->getNewOrder($Customer); |
|
167 | 39 | $Order->setPreOrderId($preOrderId); |
|
168 | |||
169 | 39 | $this->em->persist($Order); |
|
170 | |||
171 | // 配送業者情報を取得 |
||
172 | 39 | $deliveries = $this->getDeliveriesCart(); |
|
173 | |||
174 | // お届け先情報を作成 |
||
175 | 39 | $Order = $this->getNewShipping($Order, $Customer, $deliveries); |
|
176 | |||
177 | // 受注明細情報、配送商品情報を作成 |
||
178 | 39 | $Order = $this->getNewDetails($Order); |
|
179 | |||
180 | // 小計 |
||
181 | 39 | $subTotal = $this->orderService->getSubTotal($Order); |
|
182 | |||
183 | // 消費税のみの小計 |
||
184 | 39 | $tax = $this->orderService->getTotalTax($Order); |
|
185 | |||
186 | // 配送料合計金額 |
||
187 | 39 | $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($Order->getShippings())); |
|
188 | |||
189 | // 小計 |
||
190 | 39 | $Order->setSubTotal($subTotal); |
|
191 | |||
192 | // 配送料無料条件(合計金額) |
||
193 | 39 | $this->setDeliveryFreeAmount($Order); |
|
194 | |||
195 | // 配送料無料条件(合計数量) |
||
196 | 39 | $this->setDeliveryFreeQuantity($Order); |
|
197 | |||
198 | // 初期選択の支払い方法をセット |
||
199 | 39 | $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries); |
|
200 | 39 | $payments = $this->getPayments($payments, $subTotal); |
|
201 | |||
202 | 39 | if (count($payments) > 0) { |
|
203 | 39 | $payment = $payments[0]; |
|
204 | 39 | $Order->setPayment($payment); |
|
205 | 39 | $Order->setPaymentMethod($payment->getMethod()); |
|
206 | 39 | $Order->setCharge($payment->getCharge()); |
|
207 | } else { |
||
208 | $Order->setCharge(0); |
||
209 | } |
||
210 | |||
211 | 39 | $Order->setTax($tax); |
|
212 | |||
213 | // 合計金額の計算 |
||
214 | 39 | $this->calculatePrice($Order); |
|
215 | |||
216 | 39 | $this->em->flush(); |
|
217 | |||
218 | 39 | return $Order; |
|
219 | |||
220 | } |
||
221 | |||
222 | /** |
||
223 | * 受注情報を作成 |
||
224 | * @param $Customer |
||
225 | * @return \Eccube\Entity\Order |
||
226 | */ |
||
227 | 39 | public function getNewOrder(Customer $Customer) |
|
234 | |||
235 | |||
236 | /** |
||
237 | * 受注情報を作成 |
||
238 | * @return \Eccube\Entity\Order |
||
239 | */ |
||
240 | 39 | public function newOrder() |
|
246 | |||
247 | /** |
||
248 | * 受注情報を作成 |
||
249 | * |
||
250 | * @param \Eccube\Entity\Order $Order |
||
251 | * @param \Eccube\Entity\Customer|null $Customer |
||
252 | * @return \Eccube\Entity\Order |
||
253 | */ |
||
254 | 39 | public function copyToOrderFromCustomer(Order $Order, Customer $Customer = null) |
|
288 | |||
289 | |||
290 | /** |
||
291 | * 配送業者情報を取得 |
||
292 | * |
||
293 | * @return array |
||
294 | */ |
||
295 | 39 | public function getDeliveriesCart() |
|
304 | |||
305 | /** |
||
306 | * 配送業者情報を取得 |
||
307 | * |
||
308 | * @param Order $Order |
||
309 | * @return array |
||
310 | */ |
||
311 | 32 | public function getDeliveriesOrder(Order $Order) |
|
320 | |||
321 | /** |
||
322 | * 配送業者情報を取得 |
||
323 | * |
||
324 | * @param $productTypes |
||
325 | * @return array |
||
326 | */ |
||
327 | 42 | public function getDeliveries($productTypes) |
|
349 | |||
350 | |||
351 | /** |
||
352 | * お届け先情報を作成 |
||
353 | * |
||
354 | * @param Order $Order |
||
355 | * @param Customer $Customer |
||
356 | * @param $deliveries |
||
357 | * @return Order |
||
358 | */ |
||
359 | 39 | public function getNewShipping(Order $Order, Customer $Customer, $deliveries) |
|
383 | |||
384 | /** |
||
385 | * お届け先情報を作成 |
||
386 | * |
||
387 | * @param \Eccube\Entity\Shipping $Shipping |
||
388 | * @param \Eccube\Entity\Customer|null $Customer |
||
389 | * @return \Eccube\Entity\Shipping |
||
390 | */ |
||
391 | 40 | public function copyToShippingFromCustomer(Shipping $Shipping, Customer $Customer = null) |
|
392 | { |
||
393 | 40 | if (is_null($Customer)) { |
|
394 | 1 | return $Shipping; |
|
395 | } |
||
396 | |||
397 | 39 | $CustomerAddress = $this->app['eccube.repository.customer_address']->findOneBy( |
|
398 | 39 | array('Customer' => $Customer), |
|
399 | 39 | array('id' => 'ASC') |
|
400 | ); |
||
401 | |||
402 | 39 | if (!is_null($CustomerAddress)) { |
|
403 | $Shipping |
||
404 | 25 | ->setName01($CustomerAddress->getName01()) |
|
405 | 25 | ->setName02($CustomerAddress->getName02()) |
|
406 | 25 | ->setKana01($CustomerAddress->getKana01()) |
|
407 | 25 | ->setKana02($CustomerAddress->getKana02()) |
|
408 | 25 | ->setCompanyName($CustomerAddress->getCompanyName()) |
|
409 | 25 | ->setTel01($CustomerAddress->getTel01()) |
|
410 | 25 | ->setTel02($CustomerAddress->getTel02()) |
|
411 | 25 | ->setTel03($CustomerAddress->getTel03()) |
|
412 | 25 | ->setFax01($CustomerAddress->getFax01()) |
|
413 | 25 | ->setFax02($CustomerAddress->getFax02()) |
|
414 | 25 | ->setFax03($CustomerAddress->getFax03()) |
|
415 | 25 | ->setZip01($CustomerAddress->getZip01()) |
|
416 | 25 | ->setZip02($CustomerAddress->getZip02()) |
|
417 | 25 | ->setZipCode($CustomerAddress->getZip01().$CustomerAddress->getZip02()) |
|
418 | 25 | ->setPref($CustomerAddress->getPref()) |
|
419 | 25 | ->setAddr01($CustomerAddress->getAddr01()) |
|
420 | 25 | ->setAddr02($CustomerAddress->getAddr02()); |
|
421 | } else { |
||
422 | $Shipping |
||
423 | 14 | ->setName01($Customer->getName01()) |
|
424 | 14 | ->setName02($Customer->getName02()) |
|
425 | 14 | ->setKana01($Customer->getKana01()) |
|
426 | 14 | ->setKana02($Customer->getKana02()) |
|
427 | 14 | ->setCompanyName($Customer->getCompanyName()) |
|
428 | 14 | ->setTel01($Customer->getTel01()) |
|
429 | 14 | ->setTel02($Customer->getTel02()) |
|
430 | 14 | ->setTel03($Customer->getTel03()) |
|
431 | 14 | ->setFax01($Customer->getFax01()) |
|
432 | 14 | ->setFax02($Customer->getFax02()) |
|
433 | 14 | ->setFax03($Customer->getFax03()) |
|
434 | 14 | ->setZip01($Customer->getZip01()) |
|
435 | 14 | ->setZip02($Customer->getZip02()) |
|
436 | 14 | ->setZipCode($Customer->getZip01().$Customer->getZip02()) |
|
437 | 14 | ->setPref($Customer->getPref()) |
|
438 | 14 | ->setAddr01($Customer->getAddr01()) |
|
439 | 14 | ->setAddr02($Customer->getAddr02()); |
|
440 | } |
||
441 | |||
442 | 39 | return $Shipping; |
|
443 | } |
||
444 | |||
445 | |||
446 | /** |
||
447 | * 受注明細情報、配送商品情報を作成 |
||
448 | * |
||
449 | * @param Order $Order |
||
450 | * @return Order |
||
451 | */ |
||
452 | 39 | public function getNewDetails(Order $Order) |
|
476 | |||
477 | /** |
||
478 | * 受注明細情報を作成 |
||
479 | * |
||
480 | * @param Product $Product |
||
481 | * @param ProductClass $ProductClass |
||
482 | * @param $quantity |
||
483 | * @return \Eccube\Entity\OrderDetail |
||
484 | */ |
||
485 | 40 | public function getNewOrderDetail(Product $Product, ProductClass $ProductClass, $quantity) |
|
513 | |||
514 | /** |
||
515 | * 配送商品情報を作成 |
||
516 | * |
||
517 | * @param Order $Order |
||
518 | * @param Product $Product |
||
519 | * @param ProductClass $ProductClass |
||
520 | * @param $quantity |
||
521 | * @return \Eccube\Entity\ShipmentItem |
||
522 | */ |
||
523 | 39 | public function getNewShipmentItem(Order $Order, Product $Product, ProductClass $ProductClass, $quantity) |
|
577 | |||
578 | /** |
||
579 | * お届け先ごとの送料合計を取得 |
||
580 | * |
||
581 | * @param $shippings |
||
582 | * @return int |
||
583 | */ |
||
584 | 40 | public function getShippingDeliveryFeeTotal($shippings) |
|
594 | |||
595 | /** |
||
596 | * 商品ごとの配送料を取得 |
||
597 | * |
||
598 | * @param Shipping $Shipping |
||
599 | * @return int |
||
600 | */ |
||
601 | 39 | public function getProductDeliveryFee(Shipping $Shipping) |
|
610 | |||
611 | /** |
||
612 | * 住所などの情報が変更された時に金額の再計算を行う |
||
613 | * |
||
614 | * @param Order $Order |
||
615 | * @return Order |
||
616 | */ |
||
617 | 10 | View Code Duplication | public function getAmount(Order $Order) |
618 | { |
||
619 | |||
620 | // 初期選択の配送業者をセット |
||
621 | 10 | $shippings = $Order->getShippings(); |
|
622 | |||
623 | // 配送料合計金額 |
||
624 | 10 | $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings)); |
|
625 | |||
626 | // 配送料無料条件(合計金額) |
||
627 | 10 | $this->setDeliveryFreeAmount($Order); |
|
628 | |||
629 | // 配送料無料条件(合計数量) |
||
630 | 10 | $this->setDeliveryFreeQuantity($Order); |
|
631 | |||
632 | // 合計金額の計算 |
||
633 | 10 | $this->calculatePrice($Order); |
|
634 | |||
635 | 10 | return $Order; |
|
636 | |||
637 | } |
||
638 | |||
639 | /** |
||
640 | * 配送料金の設定 |
||
641 | * |
||
642 | * @param Shipping $Shipping |
||
643 | * @param Delivery|null $Delivery |
||
644 | */ |
||
645 | 39 | public function setShippingDeliveryFee(Shipping $Shipping, Delivery $Delivery = null) |
|
665 | |||
666 | /** |
||
667 | * 配送料無料条件(合計金額)の条件を満たしていれば配送料金を0に設定 |
||
668 | * |
||
669 | * @param Order $Order |
||
670 | */ |
||
671 | 41 | View Code Duplication | public function setDeliveryFreeAmount(Order $Order) |
687 | |||
688 | /** |
||
689 | * 配送料無料条件(合計数量)の条件を満たしていれば配送料金を0に設定 |
||
690 | * |
||
691 | * @param Order $Order |
||
692 | */ |
||
693 | 41 | View Code Duplication | public function setDeliveryFreeQuantity(Order $Order) |
709 | |||
710 | |||
711 | /** |
||
712 | * 商品公開ステータスチェック、在庫チェック、購入制限数チェックを行い、在庫情報をロックする |
||
713 | * |
||
714 | * @param $em トランザクション制御されているEntityManager |
||
715 | * @param Order $Order 受注情報 |
||
716 | * @return bool true : 成功、false : 失敗 |
||
717 | */ |
||
718 | 13 | public function isOrderProduct($em, \Eccube\Entity\Order $Order) |
|
757 | |||
758 | /** |
||
759 | * 受注情報、お届け先情報の更新 |
||
760 | * |
||
761 | * @param Order $Order 受注情報 |
||
762 | * @param $data フォームデータ |
||
763 | * |
||
764 | * @deprecated since 3.0.5, to be removed in 3.1 |
||
765 | */ |
||
766 | public function setOrderUpdate(Order $Order, $data) |
||
767 | { |
||
768 | // 受注情報を更新 |
||
769 | $Order->setOrderDate(new \DateTime()); |
||
770 | $Order->setOrderStatus($this->app['eccube.repository.order_status']->find($this->app['config']['order_new'])); |
||
771 | $Order->setMessage($data['message']); |
||
772 | // お届け先情報を更新 |
||
773 | $shippings = $data['shippings']; |
||
774 | foreach ($shippings as $Shipping) { |
||
775 | $Delivery = $Shipping->getDelivery(); |
||
776 | $deliveryFee = $this->app['eccube.repository.delivery_fee']->findOneBy(array( |
||
777 | 'Delivery' => $Delivery, |
||
778 | 'Pref' => $Shipping->getPref() |
||
779 | )); |
||
780 | $deliveryTime = $Shipping->getDeliveryTime(); |
||
781 | if (!empty($deliveryTime)) { |
||
782 | $Shipping->setShippingDeliveryTime($deliveryTime->getDeliveryTime()); |
||
783 | } |
||
784 | $Shipping->setDeliveryFee($deliveryFee); |
||
785 | // 商品ごとの配送料合計 |
||
786 | $productDeliveryFeeTotal = 0; |
||
787 | if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) { |
||
788 | $productDeliveryFeeTotal += $this->getProductDeliveryFee($Shipping); |
||
789 | } |
||
790 | $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal); |
||
791 | $Shipping->setShippingDeliveryName($Delivery->getName()); |
||
792 | } |
||
793 | // 配送料無料条件(合計金額) |
||
794 | $this->setDeliveryFreeAmount($Order); |
||
795 | // 配送料無料条件(合計数量) |
||
796 | $this->setDeliveryFreeQuantity($Order); |
||
797 | } |
||
798 | |||
799 | |||
800 | /** |
||
801 | * 受注情報の更新 |
||
802 | * |
||
803 | * @param Order $Order 受注情報 |
||
804 | */ |
||
805 | 10 | public function setOrderUpdateData(Order $Order) |
|
806 | { |
||
807 | // 受注情報を更新 |
||
808 | 10 | $Order->setOrderDate(new \DateTime()); |
|
809 | 10 | $OrderStatus = $this->app['eccube.repository.order_status']->find($this->app['config']['order_new']); |
|
810 | 10 | $this->setOrderStatus($Order, $OrderStatus); |
|
811 | |||
812 | } |
||
813 | |||
814 | |||
815 | /** |
||
816 | * 在庫情報の更新 |
||
817 | * |
||
818 | * @param $em トランザクション制御されているEntityManager |
||
819 | * @param Order $Order 受注情報 |
||
820 | */ |
||
821 | 10 | public function setStockUpdate($em, Order $Order) |
|
822 | { |
||
823 | |||
824 | 10 | $orderDetails = $Order->getOrderDetails(); |
|
825 | |||
826 | // 在庫情報更新 |
||
827 | 10 | foreach ($orderDetails as $orderDetail) { |
|
828 | // 在庫が無制限かチェックし、制限ありなら在庫数を更新 |
||
829 | 10 | if ($orderDetail->getProductClass()->getStockUnlimited() == Constant::DISABLED) { |
|
830 | |||
831 | 1 | $productStock = $em->getRepository('Eccube\Entity\ProductStock')->find( |
|
832 | 1 | $orderDetail->getProductClass()->getProductStock()->getId() |
|
833 | ); |
||
834 | |||
835 | // 在庫情報の在庫数を更新 |
||
836 | 1 | $stock = $productStock->getStock() - $orderDetail->getQuantity(); |
|
837 | 1 | $productStock->setStock($stock); |
|
838 | |||
839 | // 商品規格情報の在庫数を更新 |
||
840 | 10 | $orderDetail->getProductClass()->setStock($stock); |
|
841 | |||
842 | } |
||
843 | } |
||
844 | |||
845 | } |
||
846 | |||
847 | |||
848 | /** |
||
849 | * 会員情報の更新 |
||
850 | * |
||
851 | * @param Order $Order 受注情報 |
||
852 | * @param Customer $user ログインユーザ |
||
853 | */ |
||
854 | 6 | public function setCustomerUpdate(Order $Order, Customer $user) |
|
871 | |||
872 | |||
873 | /** |
||
874 | * 支払方法選択の表示設定 |
||
875 | * |
||
876 | * @param $payments 支払選択肢情報 |
||
877 | * @param $subTotal 小計 |
||
878 | * @return array |
||
879 | */ |
||
880 | 42 | public function getPayments($payments, $subTotal) |
|
898 | |||
899 | /** |
||
900 | * お届け日を取得 |
||
901 | * |
||
902 | * @param Order $Order |
||
903 | * @return array |
||
904 | */ |
||
905 | 34 | public function getFormDeliveryDates(Order $Order) |
|
906 | { |
||
907 | |||
908 | // お届け日の設定 |
||
909 | 34 | $minDate = 0; |
|
910 | 34 | $deliveryDateFlag = false; |
|
911 | |||
912 | // 配送時に最大となる商品日数を取得 |
||
913 | 34 | foreach ($Order->getOrderDetails() as $detail) { |
|
914 | 34 | $deliveryDate = $detail->getProductClass()->getDeliveryDate(); |
|
915 | 34 | if (!is_null($deliveryDate)) { |
|
916 | 2 | if ($deliveryDate->getValue() < 0) { |
|
917 | // 配送日数がマイナスの場合はお取り寄せなのでスキップする |
||
918 | 1 | $deliveryDateFlag = false; |
|
919 | 1 | break; |
|
920 | } |
||
921 | |||
922 | 1 | if ($minDate < $deliveryDate->getValue()) { |
|
923 | $minDate = $deliveryDate->getValue(); |
||
924 | } |
||
925 | // 配送日数が設定されている |
||
926 | 34 | $deliveryDateFlag = true; |
|
927 | } |
||
928 | } |
||
929 | |||
930 | // 配達最大日数期間を設定 |
||
931 | 34 | $deliveryDates = array(); |
|
932 | |||
933 | // 配送日数が設定されている |
||
934 | 34 | if ($deliveryDateFlag) { |
|
935 | 1 | $period = new \DatePeriod ( |
|
936 | 1 | new \DateTime($minDate.' day'), |
|
937 | 1 | new \DateInterval('P1D'), |
|
938 | 1 | new \DateTime($minDate + $this->app['config']['deliv_date_end_max'].' day') |
|
939 | ); |
||
940 | |||
941 | 1 | foreach ($period as $day) { |
|
942 | 1 | $deliveryDates[$day->format('Y/m/d')] = $day->format('Y/m/d'); |
|
943 | } |
||
944 | } |
||
945 | |||
946 | 34 | return $deliveryDates; |
|
947 | |||
948 | } |
||
949 | |||
950 | /** |
||
951 | * 支払方法を取得 |
||
952 | * |
||
953 | * @param $deliveries |
||
954 | * @param Order $Order |
||
955 | * @return array |
||
956 | */ |
||
957 | 34 | public function getFormPayments($deliveries, Order $Order) |
|
958 | { |
||
959 | |||
960 | 34 | $productTypes = $this->orderService->getProductTypes($Order); |
|
961 | |||
962 | 34 | if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED && count($productTypes) > 1) { |
|
963 | // 複数配送時の支払方法 |
||
964 | |||
965 | $payments = $this->app['eccube.repository.payment']->findAllowedPayments($deliveries); |
||
966 | } else { |
||
967 | |||
968 | // 配送業者をセット |
||
969 | 34 | $shippings = $Order->getShippings(); |
|
970 | 34 | $Shipping = $shippings[0]; |
|
971 | 34 | $payments = $this->app['eccube.repository.payment']->findPayments($Shipping->getDelivery(), true); |
|
972 | |||
973 | } |
||
974 | 34 | $payments = $this->getPayments($payments, $Order->getSubTotal()); |
|
975 | |||
976 | 34 | return $payments; |
|
977 | |||
978 | } |
||
979 | |||
980 | /** |
||
981 | * お届け先ごとにFormを作成 |
||
982 | * |
||
983 | * @param Order $Order |
||
984 | * @return \Symfony\Component\Form\Form |
||
985 | * @deprecated since 3.0, to be removed in 3.1 |
||
986 | */ |
||
987 | View Code Duplication | public function getShippingForm(Order $Order) |
|
988 | { |
||
989 | $message = $Order->getMessage(); |
||
990 | |||
991 | $deliveries = $this->getDeliveriesOrder($Order); |
||
992 | |||
993 | // 配送業者の支払方法を取得 |
||
994 | $payments = $this->getFormPayments($deliveries, $Order); |
||
995 | |||
996 | $builder = $this->app['form.factory']->createBuilder('shopping', null, array( |
||
997 | 'payments' => $payments, |
||
998 | 'payment' => $Order->getPayment(), |
||
999 | 'message' => $message, |
||
1000 | )); |
||
1001 | |||
1002 | $builder |
||
1003 | ->add('shippings', 'collection', array( |
||
1004 | 'type' => 'shipping_item', |
||
1005 | 'data' => $Order->getShippings(), |
||
1006 | )); |
||
1007 | |||
1008 | $form = $builder->getForm(); |
||
1009 | |||
1010 | return $form; |
||
1011 | |||
1012 | } |
||
1013 | |||
1014 | /** |
||
1015 | * お届け先ごとにFormBuilderを作成 |
||
1016 | * |
||
1017 | * @param Order $Order |
||
1018 | * @return \Symfony\Component\Form\FormBuilderInterface |
||
1019 | */ |
||
1020 | 32 | View Code Duplication | public function getShippingFormBuilder(Order $Order) |
1021 | { |
||
1022 | 32 | $message = $Order->getMessage(); |
|
1023 | |||
1024 | 32 | $deliveries = $this->getDeliveriesOrder($Order); |
|
1025 | |||
1026 | // 配送業者の支払方法を取得 |
||
1027 | 32 | $payments = $this->getFormPayments($deliveries, $Order); |
|
1028 | |||
1029 | 32 | $builder = $this->app['form.factory']->createBuilder('shopping', null, array( |
|
1030 | 32 | 'payments' => $payments, |
|
1031 | 32 | 'payment' => $Order->getPayment(), |
|
1032 | 32 | 'message' => $message, |
|
1033 | )); |
||
1034 | |||
1035 | $builder |
||
1036 | 32 | ->add('shippings', 'collection', array( |
|
1037 | 32 | 'type' => 'shipping_item', |
|
1038 | 32 | 'data' => $Order->getShippings(), |
|
1039 | )); |
||
1040 | |||
1041 | 32 | return $builder; |
|
1042 | |||
1043 | } |
||
1044 | |||
1045 | |||
1046 | /** |
||
1047 | * フォームデータを更新 |
||
1048 | * |
||
1049 | * @param Order $Order |
||
1050 | * @param array $data |
||
1051 | */ |
||
1052 | 10 | public function setFormData(Order $Order, array $data) |
|
1053 | { |
||
1054 | |||
1055 | // お問い合わせ |
||
1056 | 10 | $Order->setMessage($data['message']); |
|
1057 | |||
1058 | // お届け先情報を更新 |
||
1059 | 10 | $shippings = $data['shippings']; |
|
1060 | 10 | foreach ($shippings as $Shipping) { |
|
1061 | |||
1062 | 10 | $deliveryTime = $Shipping->getDeliveryTime(); |
|
1063 | 10 | if (!empty($deliveryTime)) { |
|
1064 | 10 | $Shipping->setShippingDeliveryTime($deliveryTime->getDeliveryTime()); |
|
1065 | } |
||
1066 | |||
1067 | } |
||
1068 | |||
1069 | } |
||
1070 | |||
1071 | /** |
||
1072 | * 配送料の合計金額を計算 |
||
1073 | * |
||
1074 | * @param Order $Order |
||
1075 | * @return Order |
||
1076 | */ |
||
1077 | 9 | View Code Duplication | public function calculateDeliveryFee(Order $Order) |
1078 | { |
||
1079 | |||
1080 | // 配送業者を取得 |
||
1081 | 9 | $shippings = $Order->getShippings(); |
|
1082 | |||
1083 | // 配送料合計金額 |
||
1084 | 9 | $Order->setDeliveryFeeTotal($this->getShippingDeliveryFeeTotal($shippings)); |
|
1085 | |||
1086 | // 配送料無料条件(合計金額) |
||
1087 | 9 | $this->setDeliveryFreeAmount($Order); |
|
1088 | |||
1089 | // 配送料無料条件(合計数量) |
||
1090 | 9 | $this->setDeliveryFreeQuantity($Order); |
|
1091 | |||
1092 | 9 | return $Order; |
|
1093 | |||
1094 | } |
||
1095 | |||
1096 | |||
1097 | /** |
||
1098 | * 購入処理を行う |
||
1099 | * |
||
1100 | * @param Order $Order |
||
1101 | * @throws ShoppingException |
||
1102 | */ |
||
1103 | 9 | public function processPurchase(Order $Order) |
|
1129 | |||
1130 | |||
1131 | /** |
||
1132 | * 値引き可能かチェック |
||
1133 | * |
||
1134 | * @param Order $Order |
||
1135 | * @param $discount |
||
1136 | * @return bool |
||
1137 | */ |
||
1138 | public function isDiscount(Order $Order, $discount) |
||
1147 | |||
1148 | |||
1149 | /** |
||
1150 | * 値引き金額をセット |
||
1151 | * |
||
1152 | * @param Order $Order |
||
1153 | * @param $discount |
||
1154 | */ |
||
1155 | public function setDiscount(Order $Order, $discount) |
||
1156 | { |
||
1157 | |||
1158 | $Order->setDiscount($Order->getDiscount() + $discount); |
||
1159 | |||
1160 | } |
||
1161 | |||
1162 | |||
1163 | /** |
||
1164 | * 合計金額を計算 |
||
1165 | * |
||
1166 | * @param Order $Order |
||
1167 | * @return Order |
||
1168 | */ |
||
1169 | 40 | public function calculatePrice(Order $Order) |
|
1170 | { |
||
1171 | |||
1172 | 40 | $total = $Order->getTotalPrice(); |
|
1173 | |||
1174 | 40 | if ($total < 0) { |
|
1175 | // 合計金額がマイナスの場合、0を設定し、discountは値引きされた額のみセット |
||
1176 | $total = 0; |
||
1177 | } |
||
1178 | |||
1179 | 40 | $Order->setTotal($total); |
|
1180 | 40 | $Order->setPaymentTotal($total); |
|
1181 | |||
1182 | 40 | return $Order; |
|
1183 | |||
1184 | } |
||
1185 | |||
1186 | /** |
||
1187 | * 受注ステータスをセット |
||
1188 | * |
||
1189 | * @param Order $Order |
||
1190 | * @param $status |
||
1191 | * @return Order |
||
1192 | */ |
||
1193 | 10 | public function setOrderStatus(Order $Order, $status) |
|
1194 | { |
||
1195 | |||
1196 | 10 | $Order->setOrderDate(new \DateTime()); |
|
1197 | 10 | $Order->setOrderStatus($this->app['eccube.repository.order_status']->find($status)); |
|
1198 | |||
1199 | 10 | $event = new EventArgs( |
|
1200 | array( |
||
1201 | 10 | 'Order' => $Order, |
|
1202 | ), |
||
1203 | 10 | null |
|
1204 | ); |
||
1205 | 10 | $this->app['eccube.event.dispatcher']->dispatch(EccubeEvents::SERVICE_SHOPPING_ORDER_STATUS, $event); |
|
1206 | |||
1207 | 10 | return $Order; |
|
1208 | |||
1209 | } |
||
1210 | |||
1211 | /** |
||
1212 | * 受注メール送信を行う |
||
1213 | * |
||
1214 | * @param Order $Order |
||
1215 | * @return MailHistory |
||
1216 | */ |
||
1217 | 9 | public function sendOrderMail(Order $Order) |
|
1240 | |||
1241 | |||
1242 | /** |
||
1243 | * 受注処理完了通知 |
||
1244 | * |
||
1245 | * @param Order $Order |
||
1246 | */ |
||
1247 | public function notifyComplete(Order $Order) |
||
1248 | { |
||
1259 | |||
1260 | |||
1261 | } |
||
1262 |