Files
crewsportswear/app/Http/Controllers/teamstore/TeamStoreController.php
2019-09-27 20:09:24 +08:00

733 lines
20 KiB
PHP

<?php namespace App\Http\Controllers\teamstore;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Auth;
use App\Models\teamstore\TeamStoreModel;
use App\Models\user\UserModel;
use Mail;
use Analytics;
class TeamStoreController extends Controller {
public function index(Request $request, $teamStoreURL)
{
// var_dump($teamStoreURL);
$m = new TeamStoreModel;
$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"
);
}
}
// var_dump($thumbnails);
return view('teamstore-sublayouts.index')
->with('store_array', $store_array)
->with('product_array', $product_array)
->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('teamstore-sublayouts.stores')
->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->selectTeamStore('Password', $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();
$itemCount = $post['itemCount'];
$cssValue = $post['jersey_chk'];
$product_id = $post['product_id'];
$classname = "row" . $itemCount;
$m = new TeamStoreModel;
$teams_array = array();
$teams_array = $m->getTeams($product_id);
$sizes_array = $m->getSizes();
?>
<tr class="<?php echo $classname; ?>">
<td class="td-hide" style="<?php echo $cssValue; ?>">
<input type="text" class="form-control input-sm capitalizeText cls-uniformName" name="uniformName[]" placeholder="i.e John Doe" data-error="#err-uniformName<?php echo $itemCount ?>" required >
<span id="err-uniformName<?php echo $itemCount ?>" style="color: #dd4b39"></span>
</td>
<td>
<select class="form-control input-sm cls-uniformTeam" name="uniformTeam[]" data-error="#err-uniformTeam<?php echo $itemCount ?>" require>
<option value="">Select Team</option>
<?php
foreach($teams_array as $team){
?><option value="<?php echo $team->Team ?>"><?php echo $team->Team ?></option><?php
}
?>
<option value="others">Others</option>
</select>
<span id="err-uniformTeam<?php echo $itemCount ?>" style="color: #dd4b39"></span>
</td>
<td>
<div id="selectOptionNumber1">
<select class="form-control input-sm cls-uniformNumber" name="uniformNumber[]" data-error="#err-uniformNumber<?php echo $itemCount ?>" require >
<option value="">Select Number</option>
<?php
for($i = 0 ; $i < 100 ; $i++){
?><option value="<?php echo $i ?>"><?php echo $i ?></option><?php
}
?>
</select>
<span id="err-uniformNumber<?php echo $itemCount ?>" style="color: #dd4b39"></span>
</div>
</td>
<td>
<div id="fetchSizes">
<select class="form-control input-sm cls-uniformSize" name="uniformSize[]" data-error="#err-uniformSize<?php echo $itemCount ?>" require>
<option value="">Select Size</option>
<?php
foreach($sizes_array as $size){
?><option value="<?php echo $size->Size ?>"><?php echo $size->Size ?></option><?php
}
?>
</select>
<span id="err-uniformSize<?php echo $itemCount ?>" style="color: #dd4b39"></span>
</div>
</td>
<td class="text-center">
<span class="changeToRemoveBtn">
<button type="button" class="btn btn-danger btn-sm removeRow" data-toggle="tooltip" title="Remove"><i class="fa fa-remove" aria-hidden="true"></i></button>
</span>
</td>
</tr>
<tr id="addnew-btn-tbl-row">
<td colspan="5" ><button type="button" id="addNewRow" class="btn btn-success btn-sm pull-right" data-toggle="tooltip" title="Add Another"><i class="fa fa-plus" aria-hidden="true"></i> Add Row</button></td>
</tr>
<?php
}
public function addToCart(Request $request)
{
$post = $request->all();
$m = new TeamStoreModel;
$product_id = $post['product_id'];
$store_url = $post['store_url'];
$store_id = $post['store_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->selectTeamStoreProducts('Id', $product_id);
$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;
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
);
}
}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']
);
}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']
);
}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
);
}
}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
);
}
}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
);
}
}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
);
}
}
$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);
$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');
});
}
}