Current File : /home/tradevaly/demo.tradevaly.com.bd/app/Http/Controllers/ProductController.php |
<?php
namespace App\Http\Controllers;
use App\Models\Company;
use App\Models\Product;
use App\Models\ProductPrice;
use App\Models\ProductInventory;
use App\Models\ProductCategoryDetail;
use App\Models\ProductCategory;
use App\Models\Unit;
use App\Models\Size;
use App\Models\Color;
use App\Models\Country;
use Illuminate\Http\Request;
use Auth;
use App\Rules\Required;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$selectProduct = ['id', 'company_id', 'product_name', 'status'];
$searchItem = $request->all();
$productCondition = [];
if( !empty($searchItem )){
if( !empty($searchItem['product_name'] )){
$productCondition['product_name'] = $searchItem['product_name'];
}
}
$selectInventory = ['id', 'product_id', 'batch_no', 'inventory_status', 'status'];
$inventoryCondition = [];
$inventoryOrderBy = 'DESC';
$inventoryLimit = 1;
$selectPrice = ['id', 'product_id', 'price_type', 'net_price', 'status'];
$priceCondition = [];
$priceOrderBy = 'DESC';
$priceLimit = 1;
$receiveData = Product::getProductWithOthers($selectProduct, $productCondition, '', $selectInventory, $inventoryCondition, $selectPrice, $priceCondition, $priceOrderBy, $priceLimit, $inventoryOrderBy, $inventoryLimit);
$dataRelation = $receiveData->get()->toArray();
//dd($dataRelation);
$data = $receiveData->paginate(10);
return view('backend.products.index',compact('data', 'dataRelation', 'request'))
->with('i', ($request->input('page', 1) - 1) * 10);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$companyList = Company::getCompanyList();
$companyList[10203040] = 'Dummy';
$topCatList = ProductCategory::getTopCategoryList();
$sizeList = Size::getSize();
$unitList = Unit::getUnit();
$colorList = Color::getColor();
$productList = Product::getProductList();
$productList = array_values($productList);
//dd($productList);
$country = Country::countryList();
//dd($country);
return view('backend.products.create',compact('companyList', 'country', 'topCatList', 'unitList', 'sizeList', 'colorList', 'productList'));
}
public function storeBasicPackageWiseCompanyProduct(Request $request)
{
$this->validate($request, [
'company_id' => 'required',
'package_name' => 'required',
'country_id' => 'required',
'first_step_cat_id' => 'required',
'second_step_cat_id' => 'required',
]);
$input = $request->all();
$store['created_user_id'] = !empty(Auth::user()->id) ? Auth::user()->id : 0;
$store['updated_at'] = Null;
$store['company_id'] = !empty($input['company_id']) ? $input['company_id'] : 0;
$store['is_service'] = !empty($input['is_service']) ? $input['is_service'] : 0;
$store['show_type'] = !empty($input['show_type']) ? $input['show_type'] : 0;
$store['product_name'] = !empty($input['product_name']) ? $input['product_name'] : '';
$store['country_id'] = !empty($input['country_id']) ? $input['country_id'] : '';
$store['first_step_cat_id'] = !empty($input['first_step_cat_id']) ? $input['first_step_cat_id'] : '';
$store['second_step_cat_id'] = !empty($input['second_step_cat_id']) ? $input['second_step_cat_id'] : '';
$responseStatus = '';
$responseMessage = '';
if( $saveProduct = Product::create($store) ) {
$responseStatus = 'success';
$responseMessage = 'Product added successfully.';
}else{
$responseStatus = 'error';
$responseMessage = 'Something went wrong, please try again.';
}
//$add = CompanyCountry::create(['name' => $request->input('name')]);
$redirect = 'products.index';
!empty($input['submit_next']) ? $redirect = 'products.create' : $redirect = 'products.index';
//dd($redirect);
return redirect()->route($redirect)
->with($responseStatus, $responseMessage);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'company_id' => 'required',
'package_name' => 'required',
'product_name' => 'required',
'country_id' => 'required',
'first_step_cat_id' => 'required',
'second_step_cat_id' => 'required',
'feature_product_image' => 'required',
// 'gallery1' => 'required|image|mimes:jpeg,png,jpg|max:512',
// 'gallery2' => 'required|image|mimes:jpeg,png,jpg|max:512',
// 'gallery3' => 'required|image|mimes:jpeg,png,jpg|max:512',
// 'gallery4' => 'required|image|mimes:jpeg,png,jpg|max:512',
// 'gallery5' => 'required|image|mimes:jpeg,png,jpg|max:512',
// 'gallery5.required' => 'Photo Gallery 5 field is required.',
'price_type' => 'required',
'unit_id' => 'required',
'minimum_order' => 'required',
]);
$input = $request->all();
$extraValidation = '';
// start price section validatio
if( $input['price_type'] == 1){
if( empty($input['regular_price']) ){
$extraValidation = 'Regular price is required.';
}else if( !empty($input['offcer_price']) ){
if( empty($input['discount_close_date'] ) ){
$extraValidation = 'Discount close date is required.';
}
if( empty($input['offer_type']) ){
$extraValidation = 'Offer type is required.';
}
}
} else if( $input['price_type'] == 2){
if( empty($input['form_price']) || empty($input['to_price']) ){
$extraValidation = 'Price range both are required.';
}
}
// end price section validatio
//dd($input);
$input['created_user_id'] = !empty(Auth::user()->id) ? Auth::user()->id : 0;
$input['updated_at'] = Null;
/* $input['first_step_cat_id'] = !empty($input['first_step_cat_id']) ? json_encode($input['first_step_cat_id']) : 0;
$input['second_step_cat_id'] = !empty($input['second_step_cat_id']) ? json_encode($input['second_step_cat_id']) : '';
$input['third_step_cat_id'] = !empty($input['third_step_cat_id']) ? json_encode($input['third_step_cat_id']) : 0;
$input['fourth_step_cat_id'] = !empty($input['fourth_step_cat_id']) ? json_encode($input['fourth_step_cat_id']) : 0;*/
$input['size_id'] = !empty($input['size_id']) ? json_encode($input['size_id']) : '';
$input['color_id'] = !empty($input['color_id']) ? json_encode($input['color_id']) : '';
$input['discount'] = 0;
if( !empty($input['regular_price'] ) && !empty($input['offcer_price'] ) ){
$decreaseValue = $input['regular_price'] - $input['offcer_price'];
$discount = ($decreaseValue / $input['regular_price']) * 100;
$input['discount'] = number_format($discount, 2);
}
// dd($input);
$responseStatus = '';
$responseMessage = '';
if( !empty( $extraValidation ) ){
$responseStatus = 'error';
$responseMessage = $extraValidation;
}else{
$catelogFile = '';
if ( !empty($input['catelog_file']) ) {
$catelogFilePath = $input['catelog_file'];
$catelogFile = date('Ymdhis').$catelogFilePath->getClientOriginalName();
$path = $input['tds_file']->storeAs('products/catelog_file', $catelogFile, 'public');
}
$input['catelog_file'] = $catelogFile;
$tdsFile = '';
if ( !empty($input['tds_file']) ) {
$tdsFilePath = $input['tds_file'];
$tdsFile = date('Ymdhis').$tdsFilePath->getClientOriginalName();
$path = $input['tds_file']->storeAs('products/tds_file', $tdsFile, 'public');
}
$input['tds_file'] = $tdsFile;
$featureProductImageFile = '';
if ( !empty($input['feature_product_image']) ) {
$featureProductImagePath = $input['feature_product_image'];
$featureProductImageFile = date('Ymdhis').$featureProductImagePath->getClientOriginalName();
$path = $input['feature_product_image']->storeAs('products/product_feature_image', $featureProductImageFile, 'public');
}
$input['feature_product_image'] = $featureProductImageFile;
$gallery1File = '';
if ( !empty($input['gallery1']) ) {
$gallery1Path = $input['gallery1'];
$gallery1File = date('Ymdhis').$gallery1Path->getClientOriginalName();
$path = $input['gallery1']->storeAs('products/product_image_gallery', $gallery1File, 'public');
}
$gallery2File = '';
if ( !empty($input['gallery2']) ) {
$gallery2Path = $input['gallery2'];
$gallery2File = date('Ymdhis').$gallery2Path->getClientOriginalName();
$path = $input['gallery2']->storeAs('products/product_image_gallery', $gallery2File, 'public');
}
$gallery3File = '';
if ( !empty($input['gallery3']) ) {
$gallery3Path = $input['gallery3'];
$gallery3File = date('Ymdhis').$gallery3Path->getClientOriginalName();
$path = $input['gallery3']->storeAs('products/product_image_gallery', $gallery3File, 'public');
}
$gallery4File = '';
if ( !empty($input['gallery4']) ) {
$gallery4Path = $input['gallery4'];
$gallery4File = date('Ymdhis').$gallery4Path->getClientOriginalName();
$path = $input['gallery4']->storeAs('products/product_image_gallery', $gallery4File, 'public');
}
$gallery5File = '';
if ( !empty($input['gallery5']) ) {
$gallery5Path = $input['gallery5'];
$gallery5File = date('Ymdhis').$gallery5Path->getClientOriginalName();
$path = $input['gallery5']->storeAs('products/product_image_gallery', $gallery5File, 'public');
}
$galleries = [$gallery1File, $gallery2File, $gallery3File, $gallery4File, $gallery5File];
$input['galleries'] = json_encode($galleries);
//$product = Product::create($input);
$saveProduct = Product::create($input);
$price = new ProductPrice();
$price->created_user_id = !empty(Auth::user()->id) ? Auth::user()->id : 0;
$price->price_type = $input['price_type'];
$price->country_id = $input['currency_country_id'];
$price->regular_price = $input['regular_price'];
$price->offcer_price = $input['offcer_price'];
$price->discount = $input['discount'];
$price->discount_close_date = $input['discount_close_date'];
$price->offer_type = $input['offer_type'];
//$price->net_price = $input['net_price'];
if( !empty($input['regular_price'] ) ){
$price->net_price = $input['regular_price'];
}
if( !empty($input['offcer_price'] ) ){
$price->net_price = $input['offcer_price'];
}
$price->from_price = !empty($input['from_price']) ? $input['from_price'] : '';
$price->to_price = !empty($input['to_price']) ? $input['to_price'] : '';
$inventory = new ProductInventory();
$inventory->created_user_id = !empty(Auth::user()->id) ? Auth::user()->id : 0;
$inventory->batch_no = $saveProduct->id.'-'.date('Ymdhis');
$inventory->in_stock = $input['in_stock'];
$inventory->inventory_status = $input['in_stock'];
$inventory->minimum_order = $input['minimum_order'];
if( $saveProduct ) {
$saveProduct->productPrice()->save($price);
$saveProduct->productInventory()->save($inventory);
$responseStatus = 'success';
$responseMessage = 'Product added successfully.';
}else{
$responseStatus = 'error';
$responseMessage = 'Something went wrong, please try again.';
}
}
$redirect = 'products.index';
!empty($input['submit_next']) ? $redirect = 'products.create' : $redirect = 'products.index';
//dd($redirect);
return redirect()->route($redirect)
->with($responseStatus, $responseMessage);
}
/**
* Display the specified resource.
*
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function show(Product $product)
{
$sizeList = Size::getSize();
$unitList = Unit::getUnit();
$colorList = Color::getColor();
$selectProduct = ['id', 'company_id', 'product_name', 'unit_id', 'status'];
$productCondition['id'] = $product->id;
$selectInventory = ['id', 'product_id', 'batch_no', 'in_stock', 'sale_qty', 'inventory_status', 'status'];
$inventoryCondition = [];
$inventoryOrderBy = 'DESC';
$inventoryLimit = '';
$selectPrice = ['id', 'product_id', 'price_type', 'regular_price', 'offcer_price', 'discount', 'discount_close_date', 'offer_type', 'from_price', 'to_price', 'net_price', 'status'];
$priceCondition = [];
$priceOrderBy = 'DESC';
$priceLimit = '';
$receiveData = Product::getProductWithOthers($selectProduct, $productCondition, '', $selectInventory, $inventoryCondition, $selectPrice, $priceCondition, $priceOrderBy, $priceLimit, $inventoryOrderBy, $inventoryLimit);
$data = $receiveData->get()->toArray();
// dd($data[0]['product_inventory']);
return view('backend.products.show',compact('data', 'sizeList', 'unitList', 'colorList'));
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function edit(Product $product)
{
//dd($product->id);
$companyList = Company::getCompanyList();
//dd($companyCatList);
$sizeList = Size::getSize();
$unitList = Unit::getUnit();
$colorList = Color::getColor();
$packageName = Company::getPackageNameByCompanyWise($product->company_id);
$topCatList = ProductCategory::getTopCategoryList();
$colName = 'first_parent_id';
$valueIDs = !empty($product->first_step_cat_id) ? json_decode($product->first_step_cat_id) : 0;
$secondCondition['second_parent_id'] = 0;
//dd($secondJsonConditions);
$secondCatList = ProductCategory::getCategoryListForEdit($colName, $secondCondition, $valueIDs);
//dd($secondCatList);
$colName = 'second_parent_id';
$valueIDs = !empty($product->second_step_cat_id) ? json_decode($product->second_step_cat_id) : 0;
$thirdCondition['third_parent_id'] = 0;
$trdCatList = ProductCategory::getCategoryListForEdit($colName, $thirdCondition, $valueIDs);
//dd($trdCatList);
$colName = 'third_parent_id';
$valueIDs = !empty($product->third_step_cat_id) ? json_decode($product->third_step_cat_id) : 0;
$frthCondition= [];
//dd($valueIDs);
$frthCatList = ProductCategory::getCategoryListForEdit($colName, $frthCondition, $valueIDs);
//dd($frthCatList);
return view('backend.products.edit',compact('product', 'companyList', 'topCatList', 'secondCatList', 'trdCatList', 'frthCatList', 'sizeList', 'unitList', 'colorList', 'packageName'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Product $product)
{
$features = $product->feature_product_image ;
$galleries = json_decode($product->galleries) ;
$gal1 = !empty($galleries[0]) ? $galleries[0] : '';
$gal2 = !empty($galleries[1]) ? $galleries[1] : '';
$gal3 = !empty($galleries[2]) ? $galleries[2] : '';
$gal4 = !empty($galleries[2]) ? $galleries[3] : '';
$gal5 = !empty($galleries[2]) ? $galleries[4] : '';
$input = $request->all();
$this->validate($request, [
'company_id' => 'required',
'package_name' => 'required',
'product_name' => 'required',
'first_step_cat_id' => 'required',
'first_step_cat_id.required' => 'Top category field is required.',
'tentitive_delivery_days' => 'required',
/*'product_name' => [new Required('Product feature image', '')],
'gallery1' => 'required|image|mimes:jpeg,png,jpg|max:512',
'gallery1.required' => 'Photo Gallery 1 field is required.',
'gallery2' => 'required|image|mimes:jpeg,png,jpg|max:512',
'gallery2.required' => 'Photo Gallery 2 field is required.',
'gallery3' => 'required|image|mimes:jpeg,png,jpg|max:512',
'gallery3.required' => 'Photo Gallery 3 field is required.',
'gallery4' => 'required|image|mimes:jpeg,png,jpg|max:512',
'gallery4.required' => 'Photo Gallery 4 field is required.',
'gallery5' => 'required|image|mimes:jpeg,png,jpg|max:512',
'gallery5.required' => 'Photo Gallery 5 field is required.',*/
]);
if ( public_path('storage/app/public/products/feature_product_image/'.$features) == false ) {
$this->validate($request, [
'feature_product_image' => 'required|image|mimes:jpeg,png,jpg|max:512',
]);
}
if ( public_path('storage/app/public/products/product_image_gallery/'.$gal1) == false ) {
$this->validate($request, [
'gallery1' => 'required|image|mimes:jpeg,png,jpg|max:512',
]);
}
// dd($gal2);
/*if ( ! public_path('storage/app/public/products/product_image_gallery/'.$gal2) ) {
$this->validate($request, [
'gallery2' => 'required|image|mimes:jpeg,png,jpg|max:512',
]);
}
if ( ! public_path('storage/app/public/products/product_image_gallery/'.$gal3) ) {
$this->validate($request, [
'gallery3' => 'required|image|mimes:jpeg,png,jpg|max:512',
]);
}
if ( ! public_path('storage/app/public/products/product_image_gallery/'.$gal4) ) {
$this->validate($request, [
'gallery4' => 'required|image|mimes:jpeg,png,jpg|max:512',
]);
}
if ( ! public_path('storage/app/public/products/product_image_gallery/'.$gal5) ) {
$this->validate($request, [
'gallery5' => 'required|image|mimes:jpeg,png,jpg|max:512',
]);
}*/
dd('Hold');
$input = $request->all();
$input['modified_user_id'] = !empty(Auth::user()->id) ? Auth::user()->id : 0;
$input['updated_at'] = date("Y-m-d h:i:sa");;
$catelogFile = '';
if ( !empty($input['catelog_file']) ) {
$catelogFilePath = $input['catelog_file'];
$catelogFile = date('Ymdhis').$catelogFilePath->getClientOriginalName();
$path = $input['tds_file']->storeAs('products/catelog_file', $catelogFile, 'public');
}
$input['catelog_file'] = $catelogFile;
$tdsFile = '';
if ( !empty($input['tds_file']) ) {
$tdsFilePath = $input['tds_file'];
$tdsFile = date('Ymdhis').$tdsFilePath->getClientOriginalName();
$path = $input['tds_file']->storeAs('products/tds_file', $tdsFile, 'public');
}
$input['tds_file'] = $tdsFile;
$featureProductImageFile = $features;
if ( !empty($input['feature_product_image']) ) {
$featureProductImagePath = $input['feature_product_image'];
$featureProductImageFile = date('Ymdhis').$featureProductImagePath->getClientOriginalName();
$path = $input['feature_product_image']->storeAs('products/product_feature_image', $featureProductImageFile, 'public');
}
$input['feature_product_image'] = $featureProductImageFile;
$gallery1File = $gal1;
if ( !empty($input['gallery1']) ) {
$gallery1Path = $input['gallery1'];
$gallery1File = date('Ymdhis').$gallery1Path->getClientOriginalName();
$path = $input['gallery1']->storeAs('products/product_image_gallery', $gallery1File, 'public');
}
$gallery2File = $gal2;
if ( !empty($input['gallery2']) ) {
$gallery2Path = $input['gallery2'];
$gallery2File = date('Ymdhis').$gallery2Path->getClientOriginalName();
$path = $input['gallery2']->storeAs('products/product_image_gallery', $gallery2File, 'public');
}
$gallery3File = $gal3;
if ( !empty($input['gallery3']) ) {
$gallery3Path = $input['gallery3'];
$gallery3File = date('Ymdhis').$gallery3Path->getClientOriginalName();
$path = $input['gallery3']->storeAs('products/product_image_gallery', $gallery3File, 'public');
}
$gallery4File = $gal4;
if ( !empty($input['gallery4']) ) {
$gallery4Path = $input['gallery4'];
$gallery4File = date('Ymdhis').$gallery4Path->getClientOriginalName();
$path = $input['gallery4']->storeAs('products/product_image_gallery', $gallery4File, 'public');
}
$gallery5File = $gal5;
if ( !empty($input['gallery5']) ) {
$gallery5Path = $input['gallery5'];
$gallery5File = date('Ymdhis').$gallery5Path->getClientOriginalName();
$path = $input['gallery5']->storeAs('products/product_image_gallery', $gallery5File, 'public');
}
$galleries = [$gallery1File, $gallery2File, $gallery3File, $gallery4File, $gallery5File];
$input['galleries'] = json_encode($galleries);
$data = Product::find($product->id);
if( $data->update($input) ) {
$responseStatus = 'success';
$responseMessage = 'Product basic and gallery informations updated successfully.';
}else{
$responseStatus = 'error';
$responseMessage = 'Something went wrong, please try again.';
}
//$add = CompanyCountry::create(['name' => $request->input('name')]);
return redirect()->route('products.index')
->with($responseStatus, $responseMessage);
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function destroy(Product $product)
{
$update['deleted_user_id'] = !empty(Auth::user()->id) ? Auth::user()->id : 0;
$data = Product::find($product->id);
$galleries = json_decode($data->galleries);
//dd($galleries);
if( $data->delete() ) {
// delete gallery image
if( !empty($galleries) ){
foreach ($galleries as $key => $value) {
if( file_exists('storage/app/public/products/product_image_gallery/'.$value) ){
unlink('storage/app/public/products/product_image_gallery/'.$value);
}
}
}
// delete gallery feature_product_image
if( !empty($data->feature_product_image) ){
if( file_exists('storage/app/public/products/product_feature_image/'.$data->feature_product_image) ){
unlink('storage/app/public/products/product_feature_image/'.$data->feature_product_image);
}
}
// delete gallery catelog_file
if( !empty($data->catelog_file) ){
if( file_exists('storage/app/public/products/catelog_file/'.$data->catelog_file) ){
unlink('storage/app/public/products/catelog_file/'.$data->catelog_file);
}
}
// delete gallery tds_file
if( !empty($data->tds_file) ){
if( file_exists('storage/app/public/products/tds_file/'.$data->tds_file) ){
unlink('storage/app/public/products/tds_file/'.$data->tds_file);
}
}
$price = ProductPrice::where('product_id', $product->id);
//$price->update(['deleted_user_id' => !empty(Auth::user()->id) ? Auth::user()->id : 0]);
$price->delete();
$inventory = ProductInventory::where('product_id', $product->id);
//$inventory->update(['deleted_user_id' => !empty(Auth::user()->id) ? Auth::user()->id : 0]);
$inventory->delete();
$responseStatus = 'success';
$responseMessage = 'Product deleted successfully.';
}else{
$responseStatus = 'error';
$responseMessage = 'Something went wrong, please try again.';
}
//$add = CompanyPackage::create(['name' => $request->input('name')]);
return redirect()->route('products.index')
->with($responseStatus, $responseMessage);
}
public function activeInactive($id)
{
$data = Product::findOrFail($id);
//dd($data;die;
$responseMessage = '';
$updateStatus = 0;
if($data->status == 0){
$updateStatus = 1;
$input['status'] = $updateStatus;
$responseMessage = 'Product active successfully';
}else{
$updateStatus = 0;
$input['status'] = $updateStatus;
$responseMessage = 'Product inactive successfully';
}
if( $data->update($input) ) {
$price = ProductPrice::where('product_id', $id)->update(['status' => $updateStatus]);
$inventory = ProductInventory::where('product_id', $id)->update(['status' => $updateStatus]);
$responseStatus = 'success';
}else{
$responseStatus = 'error';
$responseMessage = 'Something went wrong, please try again.';
}
//$add = Country::create(['name' => $request->input('name')]);
return redirect()->route('products.index')
->with($responseStatus, $responseMessage);
}
}