| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Eccube\Service\PurchaseFlow\Processor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Doctrine\ORM\EntityManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Eccube\Annotation\Inject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Eccube\Entity\ItemHolderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Eccube\Entity\Master\OrderItemType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Eccube\Entity\Master\TaxDisplayType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Eccube\Entity\Master\TaxType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Eccube\Entity\BaseInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Eccube\Entity\Order; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Eccube\Entity\OrderItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Eccube\Entity\Shipping; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Eccube\Service\PurchaseFlow\ItemHolderProcessor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Eccube\Service\PurchaseFlow\ProcessResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Eccube\Service\PurchaseFlow\PurchaseContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Eccube\Util\EntityUtil; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * 使用ポイント値引明細追加. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | class UsePointProcessor implements ItemHolderProcessor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @Inject("orm.em") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var EntityManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $entityManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var BaseInfo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected $BaseInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * DeliveryFeeProcessor constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param $app | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function __construct(EntityManager $entityManager, BaseInfo $BaseInfo) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->entityManager = $entityManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->BaseInfo = $BaseInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param ItemHolderInterface $itemHolder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param PurchaseContext     $context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @return ProcessResult | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     public function process(ItemHolderInterface $itemHolder, PurchaseContext $context) | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         if ($itemHolder->getUsePoint() > 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             if ($itemHolder->getUsePoint() > $context->getUser()->getPoint()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |                 // TODO カートに戻さないように修正する | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                 return ProcessResult::error('利用ポイントが所有ポイントを上回っています.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             // XXX delete/insert ではなく, update/insert の方がいいかも | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             $this->removePointDiscountItem($itemHolder); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             return $this->addPointDiscountItem($itemHolder); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         return ProcessResult::success(); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * 明細追加処理 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param ItemHolderInterface $itemHolder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     protected function addPointDiscountItem(ItemHolderInterface $itemHolder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $DiscountType = $this->entityManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             ->find(OrderItemType::class, OrderItemType::DISCOUNT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         // TODO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $TaxInclude = $this->entityManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             ->find(TaxDisplayType::class, TaxDisplayType::INCLUDED); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $Taxion = $this->entityManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             ->find(TaxType::class, TaxType::TAXATION); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         /** @var Order $Order */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $Order = $itemHolder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $priceOfUsePoint = $this->usePointToPrice($Order->getUsePoint()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         if (($itemHolder->getTotal() + $priceOfUsePoint) < 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             // TODO カートに戻さないように修正する | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             // TODO 送料・手数料も考慮する | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             return ProcessResult::error('利用ポイントがお支払い金額を上回っています.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $OrderItem = new OrderItem(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $OrderItem->setProductName('ポイント値引') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             ->setPrice($priceOfUsePoint) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             ->setPriceIncTax($priceOfUsePoint) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             ->setTaxRate(8) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             ->setQuantity(1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             ->setOrderItemType($DiscountType) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             ->setTaxDisplayType($TaxInclude) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             ->setTaxType($Taxion) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             ->setOrder($itemHolder); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $itemHolder->addItem($OrderItem); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         return ProcessResult::success(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * 既存のポイント明細を削除する. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @param ItemHolderInterface $itemHolder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     protected function removePointDiscountItem(ItemHolderInterface $itemHolder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         foreach ($itemHolder->getItems() as $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             if ($item->isDiscount() && $item->getProductName() == 'ポイント値引') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 $itemHolder->removeOrderItem($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 $this->entityManager->remove($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * 利用ポイントを単価に換算する. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @param integer $usePoint 利用ポイント | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @return integer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     protected function usePointToPrice($usePoint) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         return ($usePoint * $this->BaseInfo->getPointConversionRate()) * -1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 132 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |  |