Files
merchbay/app/Http/Controllers/teamstore/TeamStoreController.php
franknstayn 5483c9517d first commit
2021-07-03 18:39:08 +08:00

798 lines
22 KiB
PHP

<?php
namespace App\Http\Controllers\teamstore;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
// use Auth;
use Illuminate\Support\Facades\Auth;
use App\Models\teamstore\TeamStoreModel;
use App\Models\user\UserModel;
// use Mail;
use Illuminate\Support\Facades\Mail;
use Analytics;
class TeamStoreController extends Controller
{
public function index(Request $request, $teamStoreURL)
{
// var_dump($teamStoreURL);
$m = new TeamStoreModel;
$UserModel = new UserModel;
$store_array = $m->selectTeamStore('StoreUrl', $teamStoreURL);
$product_array = $m->selectTeamStoreProducts('TeamStoreId', $store_array[0]->Id);
$user_role = '';
if (Auth::check()) {
$user_role = Auth::user()->role;
$store_id = Auth::user()->store_id;
} else {
$user_role = null;
$store_id = null;
}
if ($store_array[0]->Password != null) {
if ($request->session()->get('teamstore_data_array') == null) {
if ($store_id != $store_array[0]->Id) {
return redirect('teamstore');
}
} else {
if ($user_role != "store_owner") {
if ($request->session()->get('teamstore_data_array')[0]->StoreUrl != $store_array[0]->StoreUrl) {
return redirect()->back();
}
}
}
}
foreach ($product_array as $p => $pr_arr) {
$thumbnails_array = $m->getProductThumbnails($pr_arr->Id);
if (!empty($thumbnails_array)) {
foreach ($thumbnails_array as $t => $thumb) {
if ($thumb->ImageClass == 'custom') {
$displayThumbnails = $thumb->Image;
break;
}
if ($thumb->ImageClass == 'active') {
$displayThumbnails = $thumb->Image;
break;
}
}
$thumbnails[] = array(
'folder' => $store_array[0]->ImageFolder,
'product_id' => $pr_arr->Id,
'thumb' => $displayThumbnails
);
} else {
$thumbnails[] = array(
'folder' => $store_array[0]->ImageFolder,
'product_id' => $pr_arr->Id,
'thumb' => "product-image-placeholder.png"
);
}
}
$getAnnouncement = $UserModel->getAnnouncement($store_array[0]->Id);
if (count($getAnnouncement) > 0) {
$data = $getAnnouncement[0];
} else {
$data = (object) array(
'Id' => 0,
'StoreId' => "",
'Announcement' => "",
'IsActive' => 0,
'DateCreated' => ""
);
}
return view('teamstore-sublayouts.index')
->with('store_array', $store_array)
->with('product_array', $product_array)
->with('announcement', $data)
->with('thumbnails', $thumbnails);
}
public function storelist(Request $request)
{
// $analyticsData = Analytics::getMostVisitedPages(14, 50);
// foreach($analyticsData as $key => $val){
// if (strpos($val['url'], 'teamstore') !== false) {
// $teamstore[] = $val['url'];
// }
// }
// foreach($teamstore as $t){
// $sad = explode('/', $t);
// if(count($sad) == 4){
// $arr_teamstore[] = explode('?', $sad['3'])['0'];
// }
// }
// var_dump(array_unique($arr_teamstore));
$m = new TeamStoreModel;
$q = $request->input('q');
$sort = $request->input('s');
if (isset($q) && isset($sort)) {
if ($sort == "latest") {
$field = "Id";
$sort_value = "DESC";
} elseif ($sort == "al-desc") {
$field = "StoreName";
$sort_value = "DESC";
} elseif ($sort == "oldest") {
$field = "Id";
$sort_value = "ASC";
} else {
$field = "StoreName";
$sort_value = "ASC";
}
if ($q != "") {
// keyword and sort
$stores_array = $m->selectTeamstoreSearch($field, $sort_value, $q);
} else {
// sort only
$stores_array = $m->selectTeamstoreFilter($field, $sort_value);
}
} else {
$field = "StoreName";
$sort_value = "ASC";
$sort = "al-asc";
$stores_array = $m->selectTeamstoreFilter($field, $sort_value);
}
return view('merchbay.index')
->with('stores_array', $stores_array)
->with('keyword', $q)
->with('filter', $sort);
}
public function checkTeamStorePassword(Request $request)
{
$m = new TeamStoreModel;
$post = $request->all();
$store_array = $m->checkStorePassword($post['store_id'], $post['password']);
if ($store_array) {
$request->session()->put('teamstore_data_array', $store_array);
return redirect('teamstore/' . $store_array[0]->StoreUrl);
} else {
return redirect()->back()->with('errors', 'Invalid Password.');
}
}
private $teams_array;
public function productDetails($teamStoreURL, $productURL)
{
$m = new TeamStoreModel;
$teams_array = array();
$store_array = $m->selectTeamStore('StoreUrl', $teamStoreURL);
$product_array = $m->selectTeamStoreProducts('ProductURL', $productURL);
$thumbnails_array = $m->getThumbnails($product_array[0]->Id);
$teams_array = $m->getTeams($product_array[0]->Id);
// $sizes_array = $m->getSizes();
if (empty($thumbnails_array)) {
$data = (object) array(
'Image' => 'product-image-placeholder.png',
'ImageClass' => 'active'
);
$thumbnails_array[] = $data;
}
$x = explode(",", $product_array[0]->AvailableSizes);
foreach ($x as $s) {
$h[] = $m->getSizesByBracket($s);
}
foreach ($h as $d) {
foreach ($d as $g) {
$sizes_array[] = $g;
}
}
if ($product_array[0]->ProductAvailableQty != null) {
$soldQty = $m->getSoldQty($product_array[0]->Id);
$availableQty = $product_array[0]->ProductAvailableQty - $soldQty[0]->SoldQty;
} else {
// echo 'no qty';
$availableQty = null;
}
// $product_array[0]->ProductAvailableQty
return view('teamstore-sublayouts.product-details')
->with('store_array', $store_array)
->with('product_array', $product_array)
->with('thumbnails_array', $thumbnails_array)
->with('teams_array', $teams_array)
->with('sizes_array', $sizes_array)
->with('available_qty', $availableQty);
}
public function login(Request $request)
{
return view('teamstore-sublayouts.login');
}
public function clearSession(Request $request)
{
$request->session()->forget('teamstore_data_array');
return redirect("{{ url('/') }}");
}
public function addNewRow(Request $request)
{
$post = $request->all();
$TeamStoreModel = new TeamStoreModel;
$item = $TeamStoreModel->selectTeamStoreProductByIdHash($post['p_id']);
$x = explode(",", $item[0]->AvailableSizes);
foreach ($x as $s) {
$h[] = $TeamStoreModel->getSizesByBracket($s);
}
foreach ($h as $d) {
foreach ($d as $g) {
$sizes_array[] = $g;
}
}
if ($item[0]->ProductAvailableQty != null) {
$soldQty = $TeamStoreModel->getSoldQty($item[0]->Id);
$availableQty = $item[0]->ProductAvailableQty - $soldQty[0]->SoldQty;
} else {
// echo 'no qty';
$availableQty = null;
}
$handle_form = view('teamstore-sublayouts.forms.' . $item[0]->ProductForm)
->with('sizes_array', $sizes_array)
->with('availableQty', $availableQty)
->render();
return $handle_form;
}
public function addToCart(Request $request)
{
$post = $request->all();
$m = new TeamStoreModel;
$hash_product_id = $post['p_id'];
if ($request->session()->has('cartkey')) {
$cartKey = $request->session()->get('cartkey');
} else {
$request->session()->put('cartkey', sha1(time() . str_random(6)));
$cartKey = $cartKey = $request->session()->get('cartkey');
}
$product_array = $m->selectTeamStoreProductByIdHash($hash_product_id);
$product_id = $product_array[0]->Id;
$TeamStoreId = $product_array[0]->TeamStoreId;
$ProductPrice = $product_array[0]->ProductPrice;
$ProductURL = $product_array[0]->ProductURL;
$product_form = $product_array[0]->ProductForm;
$design_code = $product_array[0]->DesignCode;
$product_name = $product_array[0]->ProductName;
$shipping_cost_id = $product_array[0]->ShippingCostId;
$teamstore_array = $m->selectTeamStore('Id', $TeamStoreId);
$store_url = $teamstore_array[0]->StoreUrl;
$store_id = $teamstore_array[0]->Id;
if ($product_form == "jersey-and-shorts-form") {
$order_names = $post['order_names'];
$order_number = $post['order_number'];
$order_jersey_size = $post['order_jersey_size'];
$order_shorts_size = $post['order_shorts_size'];
foreach ($order_names as $key => $val) {
if ($order_jersey_size[$key] == "none" || $order_shorts_size[$key] == "none") {
$final_price = $ProductPrice / 2;
} else {
$final_price = $ProductPrice;
}
$items[] = array(
'ProductId' => $product_id,
'StoreURL' => $store_url,
'StoreId' => $store_id,
'FormUsed' => $product_form,
'CartKey' => $cartKey,
'DesignCode' => $design_code,
'ProductURL' => $ProductURL,
'ProductName' => $product_name,
'Name' => $order_names[$key],
'Number' => $order_number[$key],
'JerseySize' => $order_jersey_size[$key],
'ShortsSize' => $order_shorts_size[$key],
'Price' => $final_price,
'Quantity' => 1,
'ShippingCostId' => $shipping_cost_id
);
}
} elseif ($product_form == "tshirt-form") {
$items[] = array(
'ProductId' => $product_id,
'StoreURL' => $store_url,
'StoreId' => $store_id,
'FormUsed' => $product_form,
'CartKey' => $cartKey,
'DesignCode' => $design_code,
'ProductURL' => $ProductURL,
'ProductName' => $product_name,
'Size' => $post['uniformSize'],
'Price' => $ProductPrice,
'Quantity' => $post['quantity'],
'ShippingCostId' => $shipping_cost_id
);
} elseif ($product_form == "quantity-form") {
$items[] = array(
'ProductId' => $product_id,
'StoreURL' => $store_url,
'StoreId' => $store_id,
'FormUsed' => $product_form,
'CartKey' => $cartKey,
'DesignCode' => $design_code,
'ProductURL' => $ProductURL,
'ProductName' => $product_name,
'Price' => $ProductPrice,
'Quantity' => $post['quantity'],
'ShippingCostId' => $shipping_cost_id
);
} elseif ($product_form == "name-number-form") {
$order_names = $post['order_names'];
$order_number = $post['order_number'];
foreach ($order_names as $key => $val) {
$items[] = array(
'ProductId' => $product_id,
'StoreURL' => $store_url,
'StoreId' => $store_id,
'FormUsed' => $product_form,
'CartKey' => $cartKey,
'DesignCode' => $design_code,
'ProductURL' => $ProductURL,
'ProductName' => $product_name,
'Name' => $order_names[$key],
'Number' => $order_number[$key],
'Price' => $ProductPrice,
'Quantity' => 1,
'ShippingCostId' => $shipping_cost_id
);
}
} elseif ($product_form == "name-number-size-form") {
$order_names = $post['order_names'];
$order_number = $post['order_number'];
$order_size = $post['order_size'];
foreach ($order_names as $key => $val) {
$items[] = array(
'ProductId' => $product_id,
'StoreURL' => $store_url,
'StoreId' => $store_id,
'FormUsed' => $product_form,
'CartKey' => $cartKey,
'DesignCode' => $design_code,
'ProductURL' => $ProductURL,
'ProductName' => $product_name,
'Name' => $order_names[$key],
'Size' => $order_size[$key],
'Number' => $order_number[$key],
'Price' => $ProductPrice,
'Quantity' => 1,
'ShippingCostId' => $shipping_cost_id
);
}
} elseif ($product_form == "number-form") {
$order_number = $post['order_number'];
foreach ($order_number as $key => $val) {
$items[] = array(
'ProductId' => $product_id,
'StoreURL' => $store_url,
'StoreId' => $store_id,
'FormUsed' => $product_form,
'CartKey' => $cartKey,
'DesignCode' => $design_code,
'ProductURL' => $ProductURL,
'ProductName' => $product_name,
'Number' => $order_number[$key],
'Price' => $ProductPrice,
'Quantity' => 1,
'ShippingCostId' => $shipping_cost_id
);
}
} elseif ($product_form == "name-name2-size-form") {
$order_names = $post['order_names'];
$order_names2 = $post['order_names2'];
$order_size = $post['order_size'];
foreach ($order_names as $key => $val) {
$items[] = array(
'ProductId' => $product_id,
'StoreURL' => $store_url,
'StoreId' => $store_id,
'FormUsed' => $product_form,
'CartKey' => $cartKey,
'DesignCode' => $design_code,
'ProductURL' => $ProductURL,
'ProductName' => $product_name,
'Name' => $order_names[$key],
'Name2' => $order_names2[$key],
'Size' => $order_size[$key],
'Price' => $ProductPrice,
'Quantity' => 1,
'ShippingCostId' => $shipping_cost_id
);
}
} elseif ($product_form == "name-size-form") {
$order_names = $post['order_names'];
$order_size = $post['order_size'];
foreach ($order_names as $key => $val) {
$items[] = array(
'ProductId' => $product_id,
'StoreURL' => $store_url,
'StoreId' => $store_id,
'FormUsed' => $product_form,
'CartKey' => $cartKey,
'DesignCode' => $design_code,
'ProductURL' => $ProductURL,
'ProductName' => $product_name,
'Name' => $order_names[$key],
'Size' => $order_size[$key],
'Price' => $ProductPrice,
'Quantity' => 1,
'ShippingCostId' => $shipping_cost_id
);
}
} elseif ($product_form == "jersey-and-shorts-quantity-form") {
$order_shorts_size = $post['order_shorts_size'];
$order_jersey_size = $post['order_jersey_size'];
$quantity = $post['quantity'];
foreach ($order_jersey_size as $key => $val) {
if ($order_jersey_size[$key] != "none" || $order_shorts_size[$key] != "none") {
if ($order_jersey_size[$key] == "none" || $order_shorts_size[$key] == "none") {
$final_price = $ProductPrice / 2;
} else {
$final_price = $ProductPrice;
}
$items[] = array(
'ProductId' => $product_id,
'StoreURL' => $store_url,
'StoreId' => $store_id,
'FormUsed' => $product_form,
'CartKey' => $cartKey,
'DesignCode' => $design_code,
'ProductURL' => $ProductURL,
'ProductName' => $product_name,
'JerseySize' => $order_jersey_size[$key],
'ShortsSize' => $order_shorts_size[$key],
'Price' => $final_price,
'Quantity' => $quantity[$key],
'ShippingCostId' => $shipping_cost_id
);
}
}
} elseif ($product_form == "number-jersey-shorts-form") {
$order_number = $post['order_number'];
$order_jersey_size = $post['order_jersey_size'];
$order_shorts_size = $post['order_shorts_size'];
foreach ($order_number as $key => $val) {
if ($order_jersey_size[$key] == "none" || $order_shorts_size[$key] == "none") {
$final_price = $ProductPrice / 2;
} else {
$final_price = $ProductPrice;
}
$items[] = array(
'ProductId' => $product_id,
'StoreURL' => $store_url,
'StoreId' => $store_id,
'FormUsed' => $product_form,
'CartKey' => $cartKey,
'DesignCode' => $design_code,
'ProductURL' => $ProductURL,
'ProductName' => $product_name,
'Number' => $order_number[$key],
'JerseySize' => $order_jersey_size[$key],
'ShortsSize' => $order_shorts_size[$key],
'Price' => $final_price,
'Quantity' => 1,
'ShippingCostId' => $shipping_cost_id
);
}
}
$i = $m->insertToCart($items);
if ($i['i']) {
return response()->json(array(
'success' => true
));
}
return response()->json(array(
'success' => false,
'message' => "Something went wrong. Please refresh the page."
));
}
public function cart(Request $request)
{
$m = new TeamStoreModel;
$cartKey = $request->session()->get('cartkey');
$items = $m->myCart($cartKey);
$getSubtotal = $m->getSubtotal($cartKey);
$items_group = $m->myCartGroup($cartKey);
//var_dump($items_group);
$grouped_item = $m->selectTeamStoreGroupByCartKey($cartKey);
if ($grouped_item) {
$defId = $grouped_item[0]->StoreId;
} else {
$defId = 0;
}
$item_thumbs = $m->selectDisplayCartThumb();
$store_array = $m->selectTeamStore('Id', $defId);
if ($items) {
$voucherIds = array();
foreach ($items as $item) {
if ($item->VoucherId != null) {
$voucherIds[] = $item->VoucherId;
}
}
$vouchers = $m->selectVoucherWhereIn($voucherIds);
}
$totalValue = 0;
if (!empty($vouchers)) {
foreach ($vouchers as $voucher) {
$totalValue = $totalValue + $voucher->VoucherValue;
if ($voucher->VoucherType == "Percentage") {
$voucherData = array(
'totalValue' => $totalValue,
'type' => 'Percentage'
);
} else {
$voucherData = array(
'totalValue' => $totalValue,
'type' => 'Flat'
);
}
}
if ($voucherData['type'] == "Percentage") {
$getPercentageValue = $voucherData['totalValue'] / 100;
$getDiscountValue = ($getSubtotal[0]->Subtotal * $getPercentageValue);
$finalSubTotal = $getSubtotal[0]->Subtotal - $getDiscountValue;
} else {
//Flat voucher computation here..
}
} else {
$finalSubTotal = $getSubtotal[0]->Subtotal;
}
return view('sublayouts.cart')
->with('item_group', $items_group)
->with('row', $items)
->with('img_thumb', $item_thumbs)
->with('getSubtotal', $finalSubTotal)
->with('store_array', $store_array);
}
public function addVoucher(Request $request)
{
$cartKey = $request->session()->get('cartkey');
if ($cartKey == "") {
return false;
}
$TeamStoreModel = new TeamStoreModel;
$post = $request->all();
$grouped_item = $TeamStoreModel->selectTeamStoreGroupByCartKey($cartKey);
$store_id = $grouped_item[0]->StoreId;
$vocher = $post['voucher'];
$store_array = $TeamStoreModel->selectTeamStore('Id', $grouped_item[0]->StoreId);
$getSubtotal = $TeamStoreModel->getSubtotal($cartKey);
$data = array(
'store_id' => $store_id,
'voucher' => $vocher
);
$getVoucher = $TeamStoreModel->selectVoucher($data);
if ($getVoucher) {
$items = $TeamStoreModel->myCart($cartKey);
// check if if voucher is already in used;
foreach ($items as $item) {
if ($getVoucher[0]->Id == $item->VoucherId) {
return response()->json(array(
'success' => false,
'message' => 'This voucher is already in used.'
));
}
}
// insert vocuher to cart_tmp
$voucherData = array(
'VoucherId' => $getVoucher[0]->Id,
'StoreURL' => $store_array[0]->StoreUrl,
'StoreId' => $store_array[0]->Id,
'CartKey' => $cartKey,
'ProductName' => $getVoucher[0]->VoucherCode,
'Price' => '00.00',
'Quantity' => 1
);
$i = $TeamStoreModel->insertToCart($voucherData);
$lastId = $i['lastId'];
$removeItemURL = url('removeitem') . '/' . $lastId;
//get all voucher used.
$updated_items = $TeamStoreModel->myCart($cartKey);
foreach ($updated_items as $item) {
if ($item->VoucherId != null) {
$voucherIds[] = $item->VoucherId;
}
}
$vouchers = $TeamStoreModel->selectVoucherWhereIn($voucherIds);
$totalValue = 0;
if (!empty($vouchers)) {
foreach ($vouchers as $voucher) {
$totalValue = $totalValue + $voucher->VoucherValue;
if ($voucher->VoucherType == "Percentage") {
$voucherData = array(
'totalValue' => $totalValue,
'type' => 'Percentage'
);
} else {
$voucherData = array(
'totalValue' => $totalValue,
'type' => 'Flat'
);
}
}
if ($voucherData['type'] == "Percentage") {
$getPercentageValue = $voucherData['totalValue'] / 100;
$getDiscountValue = ($getSubtotal[0]->Subtotal * $getPercentageValue);
$finalSubTotal = $getSubtotal[0]->Subtotal - $getDiscountValue;
} else {
//Flat voucher computation here..
}
} else {
$finalSubTotal = $getSubtotal[0]->Subtotal;
}
if ($getVoucher[0]->VoucherType == "Percentage") {
$offData = $getVoucher[0]->VoucherValue . '%';
} else {
$offData = $getVoucher[0]->VoucherValue . ' ' . $store_array[0]->StoreCurrency;
}
$message = '<div class="btn-group">
<button type="button" class="btn btn-default btn-xs">' . $getVoucher[0]->VoucherCode . ' ' . $offData . ' OFF</button>
<button type="button" class="btn btn-default dropdown-toggle btn-xs" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="caret"></span>
<span class="sr-only"></span>
</button>
<ul class="dropdown-menu">
<li><a href="' . $removeItemURL . '"><i class="fa fa-times"></i> Remove</a></li>
</ul>
</div>';
// return success
return response()->json(array(
'success' => true,
'message' => $message,
'subtotal' => $finalSubTotal
));
} else {
return response()->json(array(
'success' => false,
'message' => "Sorry, the voucher code is invalid. Please try again."
));
}
}
public function checkout(Request $request)
{
$m = new TeamStoreModel;
$cartKey = $request->session()->get('cartkey');
$usermodel = new UserModel;
$userId = Auth::user()->id;
$array_address_book = $usermodel->selectAddresBook('UserId', $userId);
// var_dump($array_address_book);
$items = $m->myCart($cartKey);
$getSubtotal = $m->getSubtotal($cartKey);
return view('sublayouts.checkout')
->with('row', $items)
->with('getSubtotal', $getSubtotal)
->with('array_address_book', $array_address_book);
}
public function mail()
{
// $user = User::find(1)->toArray();
// var_dump($user);
// Mail::send('emails.mailExample', $user, function($message) use ($user) {
// $message->to($user->email);
// $message->subject('E-Mail Example');
// });
// dd('Mail Send Successfully');
Mail::raw('Text to e-mail', function ($message) {
$message->from('us@example.com', 'Laravel');
$message->to('frank.begornia@yahoo.com')->subject('sample email');
});
}
}