Viewing File: /home/ubuntu/shop-website-base/app/Http/Controllers/Admin/ProductController.php
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Helpers\Helper, App\Helpers\EnvEditorHelper;
use DB, Hash, Exception, File, Setting, Validator;
use App\Models\User, App\Models\Settings, App\Models\Product, App\Models\ProductGallery;
use App\Models\Category, App\Models\SubCategory, App\Models\ProductInventory;
use App\Jobs\SendEmailJob;
use App\Repositories\ProductRepository as ProductRepo;
class ProductController extends Controller {
/**
* Create a new controller instance.
*
* @return void
*/
protected $paginate_count;
public function __construct() {
$this->middleware('auth:admin');
$this->paginate_count = 10;
}
/**
* @method products_create()
*
* @uses To create product details
*
* @created
*
* @updated
*
* @param
*
* @return return view page
*
*/
public function products_create() {
$product = new Product;
$categories = selected(Category::orderby('name', 'asc')->where('status',APPROVED)->get(), '', 'id');
return view('admin.products.create')
->with('page', 'products')
->with('sub_page', 'products-create')
->with('product', $product)
->with('categories', $categories)
->with('sub_categories' , []);
}
/**
* @method products_save()
*
* @uses To save the product details of new/existing product object based on details
*
* @created
*
* @updated
*
* @param object request - product Form Data
*
* @return success message
*
*/
public function products_save(Request $request) {
try {
DB::begintransaction();
$rules = [
'name' => 'required|max:191',
'picture' => 'mimes:jpg,png,jpeg',
'description' => '',
'category_id' => 'required',
'sub_category_id' => 'required',
];
Helper::custom_validator($request->all(),$rules);
$product = Product::find($request->product_id) ?? new product;
$product->category_id = $request->category_id ?: $product->category_id;
$product->sub_category_id = $request->sub_category_id ?: $product->sub_category_id;
$product->amount = $request->amount ?: $product->amount;
$product->quantity = $request->quantity ?: $product->quantity;
$product->units = $request->units ?: $product->units;
$message = $request->product_id ? tr('product_updated_success') : tr('product_created_success');
$product->name = $request->name ?: $product->name;
$product->unique_id = routefreestring($product->name);
$product->description = $request->description ?: '';
$product->is_banner = $request->is_banner ?: 0;
$product->is_today_special = $request->is_today_special ?: 0;
// Upload picture
if($request->hasFile('picture')) {
if($request->product_id) {
Helper::storage_delete_file($product->picture, FILE_PATH_PRODUCT);
// Delete the old pic
}
$product->picture = Helper::storage_upload_file($request->file('picture'), FILE_PATH_PRODUCT);
}
if($product->save()) {
if(!$request->product_id) {
$products_inventory = ProductInventory::where('product_id',$product->id)->first() ?? new ProductInventory;
$products_inventory->product_id = $product->id;
$products_inventory->total_quantity += $request->quantity;
$products_inventory->remaining_quantity += $request->quantity;
$products_inventory->save();
$product->product_stock_status = PRODUCT_AVAILABLE;
$product->save();
}
DB::commit();
return redirect(route('admin.products.view', ['product_id' => $product->id]))->with('flash_success', $message);
}
throw new Exception(tr('product_save_failed'));
} catch(Exception $e){
DB::rollback();
return redirect()->back()->withInput()->with('flash_error', $e->getMessage());
}
}
/**
* @method products_view()
*
* @uses displays the specified product details based on product id
*
* @created Sakthi
*
* @updated
*
* @param object $request - product Id
*
* @return View page
*
*/
public function products_view(Request $request) {
try {
$product = Product::find($request->product_id);
if(!$product) {
throw new Exception(tr('product_not_found'), 101);
}
return view('admin.products.view')
->with('page', 'products')
->with('sub_page', 'products-view')
->with('product', $product);
} catch (Exception $e) {
return redirect()->back()->with('flash_error', $e->getMessage());
}
}
/**
* @method products_delete()
*
* @uses delete the product details based on product id
*
* @created Sakthi
*
* @updated
*
* @param object $request - product Id
*
* @return response of success/failure details with view page
*
*/
public function products_delete(Request $request) {
try {
DB::begintransaction();
$product = Product::find($request->product_id);
if(!$product) {
throw new Exception(tr('product_not_found'), 101);
}
if($product->delete()) {
DB::commit();
return redirect()->route('admin.products.index')->with('flash_success',tr('product_deleted_success'));
}
throw new Exception(tr('product_delete_failed'));
} catch(Exception $e){
DB::rollback();
return redirect()->back()->with('flash_error', $e->getMessage());
}
}
/**
* @method products_status
*
* @uses To update product status as DECLINED/APPROVED based on product id
*
* @created Sakthi
*
* @updated
*
* @param object $request - product Id
*
* @return response success/failure message
*
**/
public function products_status(Request $request) {
try {
DB::beginTransaction();
$product = Product::find($request->product_id);
if(!$product) {
throw new Exception(tr('product_not_found'), 101);
}
$product->status = $product->status ? DECLINED : APPROVED ;
if($product->save()) {
DB::commit();
$message = $product->status ? tr('product_approve_success') : tr('product_decline_success');
return redirect()->back()->with('flash_success', $message);
}
throw new Exception(tr('product_status_change_failed'));
} catch(Exception $e) {
DB::rollback();
return redirect()->route('admin.products.index')->with('flash_error', $e->getMessage());
}
}
/**
* @method products_quantity_save
*
* @uses
*
* @created
*
* @updated
*
* @param object $request - product Id
*
* @return response success/failure message
*
**/
public function products_quantity_save(Request $request) {
try {
DB::beginTransaction();
$product = Product::find($request->product_id);
if(!$product) {
throw new Exception(tr('product_not_found'), 101);
}
$product->quantity = $product->quantity + $request->quantity;
$products_inventory = ProductInventory::where('product_id',$product->id)->first() ?? new ProductInventory;
$products_inventory->product_id = $product->id;
$products_inventory->total_quantity += $request->quantity;
$products_inventory->remaining_quantity += $request->quantity;
$product->product_stock_status = PRODUCT_AVAILABLE;
$products_inventory->save();
if($product->save()) {
DB::commit();
return redirect()->route('admin.products.index')->with('flash_success', 'Quantity Updated');
}
throw new Exception(tr('product_status_change_failed'));
} catch(Exception $e) {
DB::rollback();
return redirect()->route('admin.products.index')->with('flash_error', $e->getMessage());
}
}
/**
* @method products_update_quantity
*
* @uses
*
* @created
*
* @updated
*
* @param object $request - product Id
*
* @return response success/failure message
*
**/
public function products_update_quantity(Request $request) {
$products = Product::orderby('name', 'asc')->get();
return view('admin.products.update_quantity')
->with('page', 'products')
->with('sub_page', 'products-create')
->with('products', $products);
}
/**
* @method products_edit()
*
* @uses To display and update product details based on the product id
*
* @created Sakthi
*
* @updated
*
* @param object $request - product Id
*
* @return redirect view page
*
*/
public function products_edit(Request $request) {
try {
$product = Product::find($request->product_id);
$categories = selected(Category::orderby('name', 'asc')->where('status',APPROVED)->get(), $product->category_id, 'id');
$sub_categories = selected(SubCategory::orderby('name', 'asc')->where('status',APPROVED)->get(), $product->sub_category_id, 'id');
if(!$product) {
throw new Exception(tr('product_not_found'), 101);
}
return view('admin.products.edit')
->with('page' , 'products')
->with('sub_page', 'products-view')
->with('product', $product)
->with('categories', $categories)
->with('sub_categories', $sub_categories);
} catch(Exception $e) {
return redirect()->route('admin.products.index')->with('flash_error', $e->getMessage());
}
}
/**
* @method products_index()
*
* @uses To display all product
*
* @created Sakthi
*
* @updated
*
* @param
*
* @return return view page
*
*/
public function products_index(Request $request) {
$base_query = Product::orderBy('updated_at','desc')->CommonResponse();
if($request->search_key) {
$base_query = $base_query
->where('name','LIKE','%'.$request->search_key.'%');
}
$products = $base_query->paginate($this->paginate_count);
return view('admin.products.index')
->with('page', 'products')
->with('sub_page' , 'products-view')
->with('products' , $products);
}
/**
* @method get_sub_categories()
*
* @uses - Used to get subcategory list based on the selected category
*
* @created vidhya R
*
* @updated vidhya R
*
* @param
*
* @return JSON Response
*
*/
public function get_sub_categories(Request $request) {
$category_id = $request->category_id;
$sub_categories = SubCategory::where('category_id', '=', $category_id)
->where('status' , APPROVED)
->orderBy('name', 'asc')
->get();
$view_page = view('admin.products._sub_categories_list')->with('sub_categories' , $sub_categories)->render();
$response_array = ['success' => true , 'view' => $view_page];
return response()->json($response_array , 200);
}
/**
* @method products_gallery_index()
*
* @uses To dispaly product gallery images
*
* @created Sakthi
*
* @updated
*
* @param
*
* @return return view page
*
*/
public function products_gallery_index(Request $request) {
try {
$product = Product::find($request->product_id);
if(!$product) {
throw new Exception(tr('product_not_found'), 101);
}
$product_galleries = ProductGallery::where('product_id', $request->product_id)->orderBy('updated_at','desc')->get();
return view('admin.products.gallery')
->with('page','products')
->with('sub_page' , 'products-view')
->with('product' , $product)
->with('product_galleries' , $product_galleries);
} catch (Exception $e) {
return redirect()->route('admin.products.index')->with('flash_error', $e->getMessage());
}
}
/**
* @method products_gallery_delete()
*
* @uses delete the product gallery images based on gallery id
*
* @created Sakthi
*
* @updated
*
* @param object $request - gallery Id
*
* @return response of success/failure details with view page
*
*/
public function products_gallery_delete(Request $request) {
try {
DB::begintransaction();
$gallery = ProductGallery::find($request->gallery_id);
if(!$gallery) {
throw new Exception(tr('gallery_not_found'), 101);
}
Helper::storage_delete_file($gallery->picture, FILE_PATH_PRODUCT);
if($gallery->delete()) {
DB::commit();
return redirect()->back()->with('flash_success',tr('gallery_deleted_success'));
}
throw new Exception(tr('gallery_delete_failed'),101);
} catch(Exception $e){
DB::rollback();
return redirect()->back()->with('flash_error', $e->getMessage());
}
}
/**
* @method products_gallery_save()
*
* @uses Save gallery images for the product
*
* @created Sakthi
*
* @updated
*
* @param object $request - product Id
*
* @return response of success/failure details with view page
*
*/
public function products_gallery_save(Request $request) {
try {
DB::begintransaction();
if($request->hasFile('pictures')) {
$data = ProductRepo::product_gallery_upload($request->file('pictures'), $request->product_id, $status = YES);
DB::commit();
}
$product = Product::find($request->product_id);
if(!$product) {
throw new Exception(tr('product_not_found'), 101);
}
$products_galleries = ProductGallery::where('product_id', $request->product_id)->orderBy('updated_at','desc')->get();
return redirect()->route('admin.products.gallery.index',['product_id' => $request->product_id])
->with('page','products')
->with('sub_page' , 'products-view')
->with('product' , $product)
->with('products_galleries' , $products_galleries)
->with('flash_success', tr('gallery_added_success'));
} catch(Exception $e){
DB::rollback();
return redirect()->back()->with('flash_error', $e->getMessage());
}
}
/**
* @method product_out_of_stock()
*
* @uses update product status
*
* @created Ganesh
*
* @updated
*
* @param object $request - product Id
*
* @return response of success/failure details with view page
*
*/
public function product_out_of_stock(Request $request) {
try {
DB::beginTransaction();
$rules = [
'product_id' => 'required|exists:products,id',
];
Helper::custom_validator($request->all(),$rules);
$product = Product::firstWhere('id',$request->product_id);
$product->product_stock_status = $product->product_stock_status == YES ? NO :YES;
if($product->save()) {
DB::commit();
$message = $product->product_stock_status ? tr('product_available') : tr('product_out_of_stock');
return redirect()->back()->with('flash_success',$message);
}
throw new Exception(tr('product_stock_update_failed'),101);
} catch(Exception $e) {
DB::rollback();
return redirect()->back()->with('flash_error', $e->getMessage());
}
}
public function product_stock_status(Request $request){
try {
$products_inventory = ProductInventory::firstWhere('product_id',$request->product_id);
return $this->sendResponse($message = '', '', $products_inventory);
} catch(Exception $e) {
return $this->sendError($e->getMessage(), $e->getCode());
}
}
/**
* @method product_today_special_status()
*
* @uses update product today special status
*
* @created Ganesh
*
* @updated
*
* @param object $request - product Id
*
* @return response of success/failure details with view page
*
*/
public function product_today_special_status(Request $request) {
try {
DB::beginTransaction();
$rules = [
'product_id' => 'required|exists:products,id',
];
Helper::custom_validator($request->all(),$rules);
$product = Product::firstWhere('id',$request->product_id);
$product->is_today_special = $product->is_today_special == YES ? NO :YES;
if($product->save()) {
DB::commit();
$message = $product->is_today_special ? tr('product_today_special_added') : tr('product_today_special_removed');
return redirect()->back()->with('flash_success',$message);
}
throw new Exception(tr('product_today_special_status_failed'),101);
} catch(Exception $e) {
DB::rollback();
return redirect()->back()->with('flash_error', $e->getMessage());
}
}
/**
* @method product_banner_status()
*
* @uses update product banner status
*
* @created Ganesh
*
* @updated
*
* @param object $request - product Id
*
* @return response of success/failure details with view page
*
*/
public function product_banner_status(Request $request) {
try {
DB::beginTransaction();
$rules = [
'product_id' => 'required|exists:products,id',
];
Helper::custom_validator($request->all(),$rules);
$product = Product::firstWhere('id',$request->product_id);
$product->is_banner = $product->is_banner == YES ? NO :YES;
if($product->save()) {
DB::commit();
$message = $product->is_banner ? tr('product_added_to_banner') : tr('product_removed_from_banner');
return redirect()->back()->with('flash_success',$message);
}
throw new Exception(tr('product_banner_status_update_failed'),101);
} catch(Exception $e) {
DB::rollback();
return redirect()->back()->with('flash_error', $e->getMessage());
}
}
}
Back to Directory
File Manager