Root Zanli
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
o5t6x7pgljbm
/
public_html
/
app
/
Http
/
Controllers
/
Api
/
Filename :
StoreController.php
back
Copy
<?php namespace App\Http\Controllers\Api; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Models\Product; use App\Models\Store; use App\Models\StoreProduct; use App\Models\ProductCategory; use App\Models\ProductClaimed; use App\Models\ProductWishlist; use App\Models\ProductClaimedApprovalFile; use App\Models\User; use Exception; use Illuminate\Support\Facades\Log; use Helpers; class StoreController extends Controller { function listStoreProducts(Request $request){ $validated = $request->validate([ 'limit' => 'numeric', 'recommended' => 'boolean', ]); $productCategoryIds = $this->getProductCategoryIds($request); $activeStore = CommonController::getActiveStore($request); $shouldRecommend = $request->has('recommended') ? $request->get('recommended') : false; $pageLimit = $request->has('limit') ? $request->get('limit') : 10; $user = $request->user(); $orderBy = 'RECENTLY_UPDATED'; $shouldPaginate = true; $products = $this->getProducts($user, $activeStore, $orderBy, $pageLimit, $shouldPaginate, $productCategoryIds, $shouldRecommend); $data = ['products' => $products]; return Helpers::successResponse(array('data'=>$data), 200, 'store products fetched successfully.'); } private function getProducts($user, $activeStore, $orderBy, $pageLimit, $shouldPaginate, $productCategoryIds, $shouldRecommend, $product_ids = array()){ $queryBuilder = Product::join("store_product", "store_product.product_id", "products.product_id") ->where("store_product.store_id", $activeStore->store_id) ->where('products.status', 'ACTIVE'); if(count($product_ids) > 0) $queryBuilder = $queryBuilder->whereIn('products.product_id', $product_ids); if(count($productCategoryIds) > 0) $queryBuilder = $queryBuilder ->whereIn("products.product_category_id", $productCategoryIds); if($shouldRecommend) $queryBuilder->inRandomOrder(); else if($orderBy == 'RECENTLY_CREATED') $queryBuilder->orderBy("store_product.created_at",'desc'); else if($orderBy == 'RECENTLY_UPDATED') $queryBuilder->orderBy("store_product.updated_at",'desc'); else $queryBuilder->orderBy("store_product.updated_at",'desc'); if($shouldPaginate) $products = $queryBuilder->paginate($pageLimit); else $products = $queryBuilder->limit($pageLimit)->get(); $idToProduct = array(); foreach($products AS $product){ $product->images; $idToProduct[$product->product_id] = $product; $product['added_in_wishlist']=false; $task = $product->getTaskForUser($user); $product['task_id'] = isset($task->task_id) ? $task->task_id : null; } $this->enrichWishlistFlag($user, $idToProduct); return $products; } /** * returns valid product category ids from "product_category_ids" or "product_category_names" * if both are passed then only "product_category_ids" will be considered */ private function getProductCategoryIds(Request $request){ $product_category_ids = $request->has('product_category_ids') ? (!is_array($request->product_category_ids) ? array($request->product_category_ids) : $request->product_category_ids) : array(); $product_category_names = $request->has('product_category_names') ? (!is_array($request->product_category_names) ? array($request->product_category_names) : $request->product_category_names) : array(); if(count($product_category_ids)>0){ $product_category_ids = ProductCategory::select('product_category_id') ->whereIn("product_category_id", $product_category_ids) ->get(); } else if(count($product_category_names) > 0){ $product_category_ids = ProductCategory::select('product_category_id') ->whereIn("product_category_name", $product_category_names) ->get(); } return $product_category_ids; } private function enrichWishlistFlag( $user, $idToProduct){ $productIds = array_keys($idToProduct); $user_id = $user->user_id; $userWishlist = ProductWishlist::where("user_id", $user_id) ->whereIn("product_id", $productIds) ->get(); foreach($userWishlist as $wishlistProduct){ $product_id = $wishlistProduct->product_id; $idToProduct[$product_id]->added_in_wishlist = true; } return $idToProduct; } public function getStoreHomeScreenDetails(Request $request){ $validated = $request->validate([ 'num_newly_added_products' => 'numeric', 'limit_popular_products' => 'numeric', 'limit_recommended_products' => 'numeric' ]); $user = $request->user(); $activeStore = CommonController::getActiveStore($request); $orderBy = 'RECENTLY_UPDATED'; $shouldPaginate = false; $limit_newly_added_products = $request->has('num_newly_added_products') ? $request->numnewly_added_products : 4; $limit_popular_products = $request->has('limit_popular_products') ? $request->limit_popular_products : 6; $limit_recommended_products = $request->has('limit_recommended_products') ? $request->limit_recommended_products : 5; $product_category_ids = isset($request->product_category_id) && !empty($request->product_category_id) ? array($request->product_category_id) : array(); $data = array(); $data['newly_added_products'] = $this->getProducts($user, $activeStore, 'RECENTLY_CREATED', $limit_newly_added_products, $shouldPaginate, array(), false); $data['categories'] = ProductCategory::get(); $data['popular_products'] = $this->getProducts($user, $activeStore, 'RECENTLY_UPDATED', $limit_popular_products, $shouldPaginate,$product_category_ids, false); $data['recommended_products'] = $this->getProducts($user, $activeStore, null, $limit_recommended_products, $shouldPaginate, array(), true); return Helpers::successResponse($data, 200, 'Data fetched successfully.'); } public function claimProduct(Request $request){ $validated = $request->validate([ "product_id" => "required|numeric", /*"comments" => "string"*/ ]); $user = $request->user(); $comments = $request->comments; $activeStore = CommonController::getActiveStore($request); $product = Product::find([$request->product_id])->first(); if(!isset($product)){ Log::info("product_id $request->product_id not found"); return Helpers::responseMessage(400, 'Product id provided is not present'); } $storeProduct = StoreProduct::where('product_id', $request->product_id)->first(); if(!isset($storeProduct)){ Log::info("product_id $product->product_id is not added in store"); return Helpers::responseMessage(400, 'Product is not added in the store'); } $status = $this->getApprovalStatus($product, $user); $wallet = array(); if($status == 'APPROVED' && $product->product_sp > 0){ try{ $wallet = $this->reduceSPFromWallet($user, $product); }catch(Exception $e){ Log::error($e->getMessage()); return Helpers::responseMessage(500, $e->getMessage()); } } //Saving product claim $productClaimed = ProductClaimed::create([ 'store_product_id'=>$storeProduct->store_product_id, 'user_id' => $user->user_id, 'status' => $status, 'comments' => $comments, ]); if( ! (isset($productClaimed) && $productClaimed->product_claimed_id > 0)){ Log::info("error is saving product claim"); return Helpers::responseMessage(500, 'error is saving product claim'); } if(isset($request->approval_files) && $request->hasFile('approval_files')) { $files = array(); if(is_array($request->approval_files)) $files = $request->file('approval_files'); else{ $files[] = $request->file('approval_files'); } $savedFiles = array(); foreach($files AS $key => $file){ $productClaimedApprovalFile = $this->saveProductClaimedApprovalFile($productClaimed, $file); if(isset($productClaimedApprovalFile)) $savedFiles[] = $productClaimedApprovalFile; } } $productClaimed->approvalFiles; if(isset($wallet['balance'])) $productClaimed['sp_balance'] = $wallet['balance']; /* Notification Code - Start */ $title = 'Reward claimed'; $msg = $user->full_name.' has claimed the '.$product->product_name.' reward.'; $type = 'product_claimed'; $parentUser = User::find($activeStore->created_by); if($parentUser->device_type == 'android'){ Helpers::sendPushAndroid(array($parentUser->device_token), $title, $msg, $type, $productClaimed->product_claimed_id); } else if($parentUser->device_type == 'ios'){ Helpers::sendPushIOS(array($parentUser->device_token), $title, $msg, $type, $productClaimed->product_claimed_id); } Helpers::storeNotifications($user->user_id, array($parentUser->user_id), $title, $msg, $type, $productClaimed->product_claimed_id); /* Notification Code - End */ return Helpers::successResponse(array('data'=>$productClaimed), 200, 'product claimed successfully.'); } private function saveProductClaimedApprovalFile($productClaimed, $file){ $fileName = rand().time().'.'.$file->getClientOriginalExtension(); // Save original size image $file->move(public_path('product_claim_files'), $fileName); $productClaimedApprovalFile = ProductClaimedApprovalFile::insert( ['product_claimed_id' => $productClaimed->product_claimed_id, 'file_name' => $fileName]); return $productClaimedApprovalFile; } private function getApprovalStatus(Product $product, User $user){ $status = 'PENDING_APPROVAL'; if($product->product_type=='REWARD'){ $associatedTask = $product->getTaskForUser($user); if(isset($associatedTask) && $associatedTask!= null && $associatedTask->assignedTo->status=='APPROVED'){ Log::info("associated task is already approved, preapproving this product as well"); $status = 'APPROVED'; } } else { Log::info("it is a global product so preapproving this product."); $status="APPROVED"; } return $status; } private function reduceSPFromWallet($user, $product){ //Check if SP can be reduced if yes, reduce balance. $spWalletController = new SPWalletController(); $wallet = array(); if($spWalletController->canCreditSP($user, $product->product_sp)){ $wallet = $spWalletController->creditSP($user, $product->product_sp, [ 'transaction_title' => "Product Purchase for product_id: $product->product_id", 'ref_table' => 'product_claimed', 'ref_table_id' => null, 'description' => "Product Purchase for product_id: $product->product_id", ]); } else { // $wallet = $spWalletController->getSPWallet($user); // throw new Exception("not enough balance to purchase the product " . print_r($wallet, true)); throw new Exception("not enough balance to purchase the product"); } return $wallet; } public function get_product_by_id(Request $request){ $validated = $request->validate([ 'product_id' => 'required|numeric', ]); $product_ids = array($request->product_id); $product_category_ids = array(); $active_store = CommonController::getActiveStore($request); $should_recommend = false; $page_limit = 1; $user = $request->user(); $order_by = 'RECENTLY_UPDATED'; $should_paginate = false; $products = $this->getProducts($user, $active_store, $order_by, $page_limit, $should_paginate, $product_category_ids, $should_recommend, $product_ids); $product = null; if(count($products)>0){ $product = $products[0]; if($product->product_type=='REWARD') $product['associated_task_details']=$product->getTaskForUser($user); } else { return Helpers::responseMessage(404, "product not found for provided product_id"); } return Helpers::successResponse(['data' => $product], 200, "product retrieved succesfully"); } } ?>