From 9917f8b33e95e32dc8b755e5356918a1dcd73971 Mon Sep 17 00:00:00 2001 From: franknstayn Date: Thu, 23 Dec 2021 22:01:02 +0800 Subject: [PATCH] update flat voucher --- .../Controllers/paypal/PaypalController.php | 11 +++- .../teamstore/TeamStoreController.php | 61 ++++++++++++++----- app/Models/teamstore/TeamStoreModel.php | 15 ++++- resources/views/merchbay/cart.blade.php | 2 +- 4 files changed, 66 insertions(+), 23 deletions(-) diff --git a/app/Http/Controllers/paypal/PaypalController.php b/app/Http/Controllers/paypal/PaypalController.php index d5bf7da..96d95f9 100755 --- a/app/Http/Controllers/paypal/PaypalController.php +++ b/app/Http/Controllers/paypal/PaypalController.php @@ -321,10 +321,15 @@ class PaypalController extends Controller $m->updateVoucherValueInCart($data, $item_id); } else { - $voucherData = array( - 'totalValue' => $totalValue, - 'type' => 'Flat' + $data = array( + 'Price' => round($totalValue * -1, 2) ); + + $m->updateVoucherValueInCart($data, $item_id); + // $voucherData = array( + // 'totalValue' => $totalValue, + // 'type' => 'Flat' + // ); } } diff --git a/app/Http/Controllers/teamstore/TeamStoreController.php b/app/Http/Controllers/teamstore/TeamStoreController.php index 2f8b1ee..2814f62 100755 --- a/app/Http/Controllers/teamstore/TeamStoreController.php +++ b/app/Http/Controllers/teamstore/TeamStoreController.php @@ -589,19 +589,15 @@ class TeamStoreController extends Controller $m = new TeamStoreModel; $UserModel = new UserModel; $array_address_book = null; - - if(!Auth::guest()){ + + if (!Auth::guest()) { $userId = Auth::user()->id; - $array_address_book = $UserModel->selectAddresBook('UserId', $userId); - if (count($array_address_book) <= 0) { - $array_address_book = null; - } + $array_address_book = $UserModel->selectAddresBook('UserId', $userId); + if (count($array_address_book) <= 0) { + $array_address_book = null; + } } - - - - $cartKey = $request->session()->get('cartkey'); $items = $m->myCart($cartKey); $getSubtotal = $m->getSubtotal($cartKey); @@ -651,19 +647,21 @@ class TeamStoreController extends Controller $finalSubTotal = $getSubtotal[0]->Subtotal - $getDiscountValue; } else { //Flat voucher computation here.. + $finalSubTotal = $getSubtotal[0]->Subtotal - $voucherData['totalValue']; + // var_dump($voucherData['totalValue']); } } else { $finalSubTotal = $getSubtotal[0]->Subtotal; } - - if($cartKey != null) { + + if ($cartKey != null) { $shippingFee = app(\App\Http\Controllers\paypal\PaypalController::class)->getShippingFee($cartKey); $tax = app(\App\Http\Controllers\paypal\PaypalController::class)->getTax($cartKey); - }else{ + } else { $shippingFee = 0; $tax = []; } - + return view('merchbay.cart') ->with('item_group', $items_group) @@ -680,6 +678,7 @@ class TeamStoreController extends Controller public function addVoucher(Request $request) { $cartKey = $request->session()->get('cartkey'); + $userId = Auth::user()->id; if ($cartKey == "") { return false; @@ -694,6 +693,14 @@ class TeamStoreController extends Controller $store_array = $TeamStoreModel->selectTeamStore('Id', $grouped_item[0]->StoreId); $getSubtotal = $TeamStoreModel->getSubtotal($cartKey); + if ($cartKey != null) { + $shippingFee = app(\App\Http\Controllers\paypal\PaypalController::class)->getShippingFee($cartKey); + $tax = app(\App\Http\Controllers\paypal\PaypalController::class)->getTax($cartKey); + } else { + $shippingFee = 0; + $tax = []; + } + $data = array( 'store_id' => $store_id, 'voucher' => $vocher @@ -726,6 +733,25 @@ class TeamStoreController extends Controller 'message' => 'This voucher has expired.' )); } + + if ($getVoucher[0]->VoucherType === "Flat") { + + $isUsed = $TeamStoreModel->checkVoucher($userId, $getVoucher[0]->Id); + + if (count($isUsed) >= 1) { + return response()->json(array( + 'success' => false, + 'message' => 'You already used this voucher.', + )); + } + + if ($getSubtotal[0]->Subtotal <= $getVoucher[0]->VoucherValue) { + return response()->json(array( + 'success' => false, + 'message' => 'Please add more item to redeem this voucher.', + )); + } + } } // insert vocuher to cart_tmp @@ -770,12 +796,13 @@ class TeamStoreController extends Controller } } + // voucher computation if ($voucherData['type'] == "Percentage") { $getPercentageValue = $voucherData['totalValue'] / 100; $getDiscountValue = ($getSubtotal[0]->Subtotal * $getPercentageValue); $finalSubTotal = $getSubtotal[0]->Subtotal - $getDiscountValue; } else { - //Flat voucher computation here.. + $finalSubTotal = $getSubtotal[0]->Subtotal - $voucherData['totalValue']; } } else { $finalSubTotal = $getSubtotal[0]->Subtotal; @@ -787,6 +814,8 @@ class TeamStoreController extends Controller $offData = $getVoucher[0]->VoucherValue . ' ' . $store_array[0]->StoreCurrency; } + + $message = '