Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 75 additions & 72 deletions Controller/CouponShoppingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Plugin\Coupon4\Controller;

use Eccube\Controller\AbstractController;
use Eccube\Entity\Customer;
use Eccube\Entity\Order;
use Eccube\Repository\DeliveryTimeRepository;
use Eccube\Service\CartService;
Expand All @@ -24,10 +23,10 @@
use Plugin\Coupon4\Repository\CouponOrderRepository;
use Plugin\Coupon4\Repository\CouponRepository;
use Plugin\Coupon4\Service\CouponService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;

class CouponShoppingController extends AbstractController
Expand Down Expand Up @@ -84,13 +83,10 @@ public function __construct(DeliveryTimeRepository $deliveryTimeRepository, Cart
/**
* クーポン入力、登録画面.
*
* @param Request $request
* @param Request $request
*
* @return array|RedirectResponse
* @Route("/plugin/coupon/shopping/shopping_coupon", name="plugin_coupon_shopping")
* @Template("Coupon4/Resource/template/default/shopping_coupon.twig")
*
* @see https://github.com/EC-CUBE/coupon-plugin/issues/128
* @return \Symfony\Component\HttpFoundation\JsonResponse|RedirectResponse
*/
public function shoppingCoupon(Request $request)
{
Expand All @@ -113,84 +109,91 @@ public function shoppingCoupon(Request $request)

$form->get('coupon_cd')->setData($couponCd);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// サービスの取得
/** @var CouponService $service */
$service = $this->couponService;
$formCouponCd = $form->get('coupon_cd')->getData();
$formCouponCancel = $form->get('coupon_use')->getData();
// ---------------------------------
// クーポンコード入力項目追加
// ----------------------------------
if ($formCouponCancel == 0) {
if (!is_null($formCouponCd)) {
// 画面上のクーポンコードが入力されておらず、既にクーポンコードが登録されていればクーポンを無効にする
$this->couponService->removeCouponOrder($Order);
}
// サービスの取得
/** @var CouponService $service */
$service = $this->couponService;
$formCouponCd = $request->get('cd');
// ---------------------------------
// クーポンコード入力項目追加
// ----------------------------------
$message = 'ok';
if (empty($formCouponCd)) {
if (!is_null($formCouponCd)) {
// 画面上のクーポンコードが入力されておらず、既にクーポンコードが登録されていればクーポンを無効にする
$message = $this->couponService->removeCouponOrder($Order);
}
} else {
// クーポンコードが入力されている
$discount = 0;
$error = false;
// クーポン情報を取得
/* @var $Coupon Coupon */
$Coupon = $this->couponRepository->findActiveCoupon($formCouponCd);
if (!$Coupon) {
$form->get('coupon_cd')->addError(new FormError(trans('plugin_coupon.front.shopping.notexists')));
$error = true;
}

return $this->redirectToRoute('shopping');
if ($this->isGranted('ROLE_USER')) {
$Customer = $this->getUser();
} else {
// クーポンコードが入力されている
$discount = 0;
$error = false;
// クーポン情報を取得
/* @var $Coupon Coupon */
$Coupon = $this->couponRepository->findActiveCoupon($formCouponCd);
if (!$Coupon) {
$form->get('coupon_cd')->addError(new FormError(trans('plugin_coupon.front.shopping.notexists')));
$error = true;
}

if ($this->isGranted('ROLE_USER')) {
$Customer = $this->getUser();
} else {
$Customer = $this->orderHelper->getNonMember();
if ($Coupon) {
if ($Coupon->getCouponMember()) {
$form->get('coupon_cd')->addError(new FormError(trans('plugin_coupon.front.shopping.member')));
$error = true;
}
$Customer = $this->orderHelper->getNonMember();
if ($Coupon) {
if ($Coupon->getCouponMember()) {
$form->get('coupon_cd')->addError(new FormError(trans('plugin_coupon.front.shopping.member')));
$error = true;
}
}
}

$couponUsedOrNot = $this->couponService->checkCouponUsedOrNot($formCouponCd, $Customer);
if ($Coupon && $couponUsedOrNot) {
// 既に存在している
$form->get('coupon_cd')->addError(new FormError(trans('plugin_coupon.front.shopping.sameuser')));
$error = true;
}
$couponUsedOrNot = $this->couponService->checkCouponUsedOrNot($formCouponCd, $Customer);
if ($Coupon && $couponUsedOrNot) {
// 既に存在している
$form->get('coupon_cd')->addError(new FormError(trans('plugin_coupon.front.shopping.sameuser')));
$error = true;
}

// ----------------------------------
// 値引き項目追加 / 合計金額上書き
// ----------------------------------
if (!$error && $Coupon) {
$couponProducts = $service->existsCouponProduct($Coupon, $Order);
$discount = $service->recalcOrder($Coupon, $couponProducts);
// ----------------------------------
// 値引き項目追加 / 合計金額上書き
// ----------------------------------
if (!$error && $Coupon) {
$couponProducts = $service->existsCouponProduct($Coupon, $Order);
$discount = $service->recalcOrder($Coupon, $couponProducts);

// クーポン情報を登録
$service->saveCouponOrder($Order, $Coupon, $formCouponCd, $Customer, $discount);
// クーポン情報を登録
$service->saveCouponOrder($Order, $Coupon, $formCouponCd, $Customer, $discount);

return $this->redirectToRoute('shopping');
$message = 'ok';
} else {
// エラーが発生した場合、前回設定されているクーポンがあればその金額を再設定する
if ($couponCd && $Coupon) {
// クーポン情報を取得
$Coupon = $this->couponRepository->findActiveCoupon($couponCd);
if ($Coupon) {
$couponProducts = $service->existsCouponProduct($Coupon, $Order);
// 値引き額を取得
$discount = $service->recalcOrder($Coupon, $couponProducts);
// クーポン情報を登録
$service->saveCouponOrder($Order, $Coupon, $couponCd, $Customer, $discount);
$message = 'useng';
}
} else {
// エラーが発生した場合、前回設定されているクーポンがあればその金額を再設定する
if ($couponCd && $Coupon) {
// クーポン情報を取得
$Coupon = $this->couponRepository->findActiveCoupon($couponCd);
if ($Coupon) {
$couponProducts = $service->existsCouponProduct($Coupon, $Order);
// 値引き額を取得
$discount = $service->recalcOrder($Coupon, $couponProducts);
// クーポン情報を登録
$service->saveCouponOrder($Order, $Coupon, $couponCd, $Customer, $discount);
}
if ($error) {
$message = 'ng';
}
}
}
}

return [
'form' => $form->createView(),
'Order' => $Order,
];
if ($request->isXmlHttpRequest()) {
// ajaxでのリクエストの場合、jsonで返す
return $this->json([$message]);

} else {
// ajax以外でのリクエスト
throw new NotFoundHttpException();

}

}
}
29 changes: 29 additions & 0 deletions Form/Extension/OrderTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Plugin\Coupon4\Form\Extension;

use Eccube\Form\Type\Shopping\OrderType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

class OrderTypeExtension extends AbstractTypeExtension
{

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('coupon_cd', TextType::class, [
'label' => 'plugin_coupon.front.code',
'required' => false,
'trim' => true,
'mapped' => false,
]);

}

public function getExtendedType()
{
return OrderType::class;
}
}
4 changes: 4 additions & 0 deletions Resource/locale/messages.ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,14 @@ plugin_coupon.front.shopping.member: このクーポンは会員のみ利用で
plugin_coupon.front.shopping.lowerlimit: このクーポンを利用するためには、対象商品をlowerLimit円以上購入してください。
plugin_coupon.front.shopping.changeorder: 注文内容が変更になりました。クーポンをご利用の場合は再度設定をお願いいたします。
plugin_coupon.front.shopping.notfound: このクーポンコードは利用できません。
plugin_coupon.front.shopping.blank: クーポンコードが入力されていません。
plugin_coupon.front.shopping.cancel: クーポンをキャンセルしました。
plugin_coupon.front.shopping.header: クーポン
plugin_coupon.front.shopping.message.use_code: クーポンコード %code% を利用しています。
plugin_coupon.front.shopping.message.empty: クーポンをお持ちの方はクーポンコードを入力してください。
plugin_coupon.front.shopping.button.add_coupon: クーポンを変更する
plugin_coupon.front.shopping.button.use_coupon: クーポンを利用する
plugin_coupon.front.shopping.button.cancel_coupon: クーポンをキャンセルする
plugin_coupon.front.shopping_coupon.header: クーポンコードの入力
plugin_coupon.front.shopping_coupon.body: 利用するクーポンコードを入力してください。
plugin_coupon.front.shopping_coupon.use: クーポンを利用する
Expand Down
58 changes: 45 additions & 13 deletions Resource/template/default/coupon_shopping_item.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,48 @@
file that was distributed with this source code.
#}
<script type="text/javascript">
$(function () {
$('#coupon_button').on("click", function () {
$(this).attr('disabled', 'disabled');
$(function() {
$('.coupon-submit').on('click', function() {
$(this).prop('disabled', true);

var code = $(this).data('coupon');
if (code == 'cancel') {
code = '';
} else if (!code) {
code = $('#shopping_order_coupon_cd').val();
}

$.ajax({
url: "{{ url('plugin_coupon_shopping') }}",
cache: false,
type: 'POST',
data: $('#shopping-form').serialize(),
url: '{{ url("shopping_redirect_to") }}',
success: function(data) {
window.location.href = '{{ url('plugin_coupon_shopping') }}';
},
error: function() {
window.location.href = '{{ url('plugin_coupon_shopping') }}';
data: {'cd': code},
dataType: 'json'
}).done(function(data) {
if (data == 'ok') {
$('#shopping-form').attr('action', '{{ url("shopping") }}').submit();
} else if (data == 'cancel') {
alert("{{ 'plugin_coupon.front.shopping.cancel'|trans }}");
$('#shopping-form').attr('action', '{{ url("shopping") }}').submit();
} else if (data == 'blank') {
alert("{{ 'plugin_coupon.front.shopping.blank'|trans }}");
} else if (data == 'ng') {
alert("{{ 'plugin_coupon.front.shopping.notfound'|trans }}");
} else if (data == 'useng') {
alert("{{ 'plugin_coupon.front.shopping.couponusetime'|trans }}");
} else {
alert("{{ 'plugin_coupon.front.shopping.couponusetime'|trans }}");
}
return false;
}).fail(function(data) {
alert("{{ 'plugin_coupon.front.shopping.couponusetime'|trans }}");
}).always(function(data) {
$('.coupon-submit').prop('disabled', false);
});
return false;
});

// append to layout
$(".ec-orderConfirm").last().before($("#coupon").detach());
$('.ec-orderConfirm').last().before($('#coupon').detach());
})
</script>

Expand All @@ -37,9 +60,18 @@
<div id="customer_detail_box" class="column">
{% if CouponOrder %}
<strong class="text-danger">{{ 'plugin_coupon.front.shopping.message.use_code'|trans({'%code%': CouponOrder.coupon_cd }) }}</strong>
<p>
<button type="button" class="ec-inlineBtn coupon-submit" data-coupon="cancel">{{ 'plugin_coupon.front.shopping.button.cancel_coupon'|trans }}</button>
</p>
{% else %}
{{ 'plugin_coupon.front.shopping.message.empty'|trans }}
<p>
{{ form_widget(form.coupon_cd, {'attr': {'class': 'form-control'}}) }}
{{ form_errors(form.coupon_cd) }}
</p>
<p>
<button type="button" id="coupon_button" class="ec-inlineBtn coupon-submit">{{ 'plugin_coupon.front.shopping.button.use_coupon'|trans }}</button>
</p>
{% endif %}
<p><a class="btn btn-default btn-sm" id="coupon_button" href="{{ url('plugin_coupon_shopping') }}">{{ 'plugin_coupon.front.shopping.button.add_coupon'|trans }}</a></p>
</div>
</div>
7 changes: 5 additions & 2 deletions Service/CouponService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

use Doctrine\ORM\EntityManagerInterface;
use Eccube\Common\Constant;
use Eccube\Entity\Category;
use Eccube\Entity\Customer;
use Eccube\Entity\ItemHolderInterface;
use Eccube\Entity\Master\RoundingType;
use Eccube\Entity\Order;
use Eccube\Entity\OrderItem;
use Eccube\Entity\ProductClass;
Expand All @@ -32,7 +32,6 @@
use Eccube\Service\TaxRuleService;
use Plugin\Coupon4\Entity\Coupon;
use Plugin\Coupon4\Entity\CouponOrder;
use Eccube\Entity\Category;
use Plugin\Coupon4\Repository\CouponOrderRepository;
use Plugin\Coupon4\Repository\CouponRepository;
use Plugin\Coupon4\Service\PurchaseFlow\Processor\CouponProcessor;
Expand Down Expand Up @@ -383,7 +382,11 @@ public function removeCouponOrder(ItemHolderInterface $Order)

$this->setOrderCompleteMailMessage($Order, null, null);
$this->entityManager->flush($Order);

return 'cancel';
}

return 'blank';
}

/**
Expand Down