/**
  * クーポンの新規作成/編集確定
  *
  * @param Application $app
  * @param Request $request
  * @param $id
  * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
  */
 public function edit(Application $app, Request $request, $id = null)
 {
     $Coupon = null;
     if (!$id) {
         // 新規登録
         $Coupon = new CouponCoupon();
         $Coupon->setEnableFlag(Constant::ENABLED);
         $Coupon->setDelFlg(Constant::DISABLED);
     } else {
         // 更新
         $Coupon = $app['eccube.plugin.coupon.repository.coupon']->find($id);
         if (!$Coupon) {
             $app->addError('admin.coupon.notfound', 'admin');
             return $app->redirect($app->url('admin_coupon_list'));
         }
     }
     $form = $app['form.factory']->createBuilder('admin_coupon', $Coupon)->getForm();
     // クーポンコードの発行
     if (!$id) {
         $form->get('coupon_cd')->setData($app['eccube.plugin.coupon.service.coupon']->generateCouponCd());
     }
     $details = array();
     $CouponDetails = $Coupon->getCouponDetails();
     foreach ($CouponDetails as $CouponDetail) {
         $details[] = clone $CouponDetail;
     }
     $form->get('CouponDetails')->setData($details);
     $form->handleRequest($request);
     if ($form->isSubmitted() && $form->isValid()) {
         /** @var \Plugin\Coupon\Entity\CouponCoupon $Coupon */
         $Coupon = $form->getData();
         $CouponDetails = $app['eccube.plugin.coupon.repository.coupon_detail']->findBy(array('Coupon' => $Coupon));
         foreach ($CouponDetails as $CouponDetail) {
             $Coupon->removeCouponDetail($CouponDetail);
             $app['orm.em']->remove($CouponDetail);
             $app['orm.em']->flush($CouponDetail);
             // Postgres対応
         }
         $CouponDetails = $form->get('CouponDetails')->getData();
         foreach ($CouponDetails as $CouponDetail) {
             $CouponDetail->setCoupon($Coupon);
             $CouponDetail->setCouponType($Coupon->getCouponType());
             $CouponDetail->setDelFlg(Constant::DISABLED);
             $Coupon->addCouponDetail($CouponDetail);
             $app['orm.em']->persist($CouponDetail);
         }
         $app['orm.em']->persist($Coupon);
         $app['orm.em']->flush();
         // 成功時のメッセージを登録する
         $app->addSuccess('admin.plugin.coupon.regist.success', 'admin');
         return $app->redirect($app->url('admin_coupon_list'));
     }
     return $this->renderRegistView($app, array('form' => $form->createView(), 'id' => $id));
 }
 private function getTestData($couponType = 1, $discountType = 1)
 {
     $Coupon = new CouponCoupon();
     $date1 = new \DateTime();
     $date2 = new \DateTime();
     $Coupon->setCouponCd('aaaaaaaa');
     $Coupon->setCouponType($couponType);
     $Coupon->setCouponName('クーポン');
     $Coupon->setDiscountType($discountType);
     $Coupon->setCouponUseTime(1);
     $Coupon->setDiscountPrice(100);
     $Coupon->setDiscountRate(10);
     $Coupon->setEnableFlag(1);
     $d1 = $date1->setDate(2016, 1, 1);
     $Coupon->setAvailableFromDate($d1);
     $d2 = $date2->setDate(2016, 12, 31);
     $Coupon->setAvailableToDate($d2);
     return $Coupon;
 }
 /**
  * カテゴリがクーポン適用の対象か調査する.
  * 下位のカテゴリから上位のカテゴリに向けて検索する
  *
  * @param \Plugin\Coupon\Entity\CouponCoupon $Coupon
  * @param \Eccube\Entity\Order $Order
  * @return boolean
  */
 private function containsCategory(\Plugin\Coupon\Entity\CouponCoupon $Coupon, \Eccube\Entity\Order $Order)
 {
     // クーポンの対象カテゴリIDを配列にする
     $targetCategoryIds = array();
     foreach ($Coupon->getCouponDetails() as $detail) {
         $targetCategoryIds[] = $detail->getCategory()->getId();
     }
     // 受注データからカテゴリIDを取得する
     foreach ($Order->getOrderDetails() as $orderDetail) {
         foreach ($orderDetail->getProduct()->getProductCategories() as $productCategory) {
             if ($this->existsDepthCategory($targetCategoryIds, $productCategory->getCategory())) {
                 return true;
             }
         }
     }
     return false;
 }
 private function getTestData($couponType = 1)
 {
     //        $CouponDetail = new CouponCouponDetail();
     //
     //       $Product = $this->createProduct();
     //
     //        $CouponDetail->setProduct($Product);
     //
     //        $data = array(
     //            'coupon_cd' => 'aaaaaaaa',
     //            'coupon_type' => '1',
     //            'coupon_name' => 'クーポン',
     //            'discount_type' => '1',
     //            'coupon_use_time' => '1',
     //            'discount_price' => '1',
     //            'discount_rate' => '1',
     //            'enalbe_flag' => '1',
     //            'available_from_date' => new \DateTime(),
     //            'available_to_date' => new \DateTime(),
     //            'CouponDetails' => array($CouponDetail),
     //        );
     //
     //        return $data;
     $Coupon = new CouponCoupon();
     $date1 = new \DateTime();
     $date2 = new \DateTime();
     $Coupon->setCouponCd('aaaaaaaa');
     $Coupon->setCouponType($couponType);
     $Coupon->setCouponName('クーポン');
     $Coupon->setDiscountType(1);
     $Coupon->setCouponUseTime(1);
     $Coupon->setDiscountPrice(100);
     $Coupon->setDiscountRate(10);
     $Coupon->setEnableFlag(1);
     $d1 = $date1->setDate(2016, 1, 1);
     $Coupon->setAvailableFromDate($d1);
     $d2 = $date2->setDate(2016, 12, 31);
     $Coupon->setAvailableToDate($d2);
     return $Coupon;
 }
 /**
  * クーポン情報を生成する
  *
  * @param $data
  * @return CouponCoupon
  */
 protected function newCoupon($data)
 {
     $dateTime = new \DateTime();
     $coupon = new \Plugin\Coupon\Entity\CouponCoupon();
     $coupon->setCouponCd($data['coupon_cd']);
     $coupon->setCouponName($data['coupon_name']);
     $coupon->setCouponType($data['coupon_type']);
     $coupon->setDiscountType($data['discount_type']);
     $coupon->setDiscountPrice($data['discount_price']);
     $coupon->setDiscountRate($data['discount_rate']);
     $coupon->setCouponUseTime($data['coupon_use_time']);
     $coupon->setEnableFlag(Constant::ENABLED);
     $coupon->setDelFlg(Constant::DISABLED);
     $coupon->setAvailableFromDate($data['available_from_date']);
     $coupon->setAvailableToDate($data['available_to_date']);
     return $coupon;
 }