Current File : /home/tradevaly/demo.tradevaly.com.bd/app/Http/Controllers/CompanyWithCategoryMappingController.php |
<?php
namespace App\Http\Controllers;
use App\Models\CompanyWithCategoryMapping;
use Illuminate\Http\Request;
use App\Models\ProductCategory;
use App\Models\Company;
use App\Models\CompanyCategory;
use App\Rules\Required;
use Auth;
class CompanyWithCategoryMappingController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$proCatList = ProductCategory::getCategoryList();
$companyCategory = CompanyCategory::getCat([]);
$company = Company::getCompanyList([]);
$data = CompanyWithCategoryMapping::orderBy('id','DESC')->paginate(100);
return view('backend.company-mappings.index',compact('data', 'proCatList', 'companyCategory', 'company' ))
->with('i', ($request->input('page', 1) - 1) * 100);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$topCatList = ProductCategory::getTopCategoryList();
$companyCategory = CompanyCategory::getCat([]);
$companies = Company::getCompanyList([]);
return view('backend.company-mappings.create',compact( 'topCatList', 'companyCategory', 'companies' ));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
//'product_category_id' => ['required', new Required('Product Top Category', '')],
'product_category_id' => 'required',
'company_category_id' => 'required',
'company_id' => 'required',
]);
$input = $request->all();
$input['updated_at'] = Null;
$responseStatus = '';
$responseMessage = '';
if( isset($input['company_id']) && !empty($input['company_id']) ){
foreach ($input['company_id'] as $companyID) {
// start product first step category with company category and company
if( isset($input['product_category_id']) && !empty($input['product_category_id']) ){
foreach ($input['product_category_id'] as $productFirstStepCategoryID) {
$firstStepData['product_category_id'] = $productFirstStepCategoryID;
$firstStepData['product_category_step'] = 1;
$firstStepData['company_category_id'] = $input['company_category_id'];
$firstStepData['company_id'] = $companyID;
CompanyWithCategoryMapping::create($firstStepData);
}
}
// end product first step category with company category and company
// start product second step category with company category and company
if( isset($input['second_step_cat_id']) && !empty($input['second_step_cat_id']) ){
foreach ($input['second_step_cat_id'] as $productSecondStepCategoryID) {
$firstStepData['product_category_id'] = $productSecondStepCategoryID;
$firstStepData['product_category_step'] = 2;
$firstStepData['company_category_id'] = $input['company_category_id'];
$firstStepData['company_id'] = $companyID;
CompanyWithCategoryMapping::create($firstStepData);
}
}
// end product second step category with company category and company
// start product third step category with company category and company
if( isset($input['third_step_cat_id']) && !empty($input['third_step_cat_id']) ){
foreach ($input['third_step_cat_id'] as $productThiredStepCategoryID) {
$firstStepData['product_category_id'] = $productThiredStepCategoryID;
$firstStepData['product_category_step'] = 3;
$firstStepData['company_category_id'] = $input['company_category_id'];
$firstStepData['company_id'] = $companyID;
CompanyWithCategoryMapping::create($firstStepData);
}
}
// end product third step category with company category and company
// start product 4rth step category with company category and company
if( isset($input['fourth_step_cat_id']) && !empty($input['fourth_step_cat_id']) ){
foreach ($input['fourth_step_cat_id'] as $productFrthStepCategoryID) {
$firstStepData['product_category_id'] = $productFrthStepCategoryID;
$firstStepData['product_category_step'] = 4;
$firstStepData['company_category_id'] = $input['company_category_id'];
$firstStepData['company_id'] = $companyID;
CompanyWithCategoryMapping::create($firstStepData);
}
}
// end product 4rth step category with company category and company
}
$responseStatus = 'success';
$responseMessage = 'Company mapping added successfully.';
}else{
$responseStatus = 'error';
$responseMessage = 'Something went wrong, please try again.';
}
return redirect()->route('company-with-category-mappings.create')
->with($responseStatus, $responseMessage);
}
/**
* Display the specified resource.
*
* @param \App\Models\CompanyWithCategoryMapping $companyWithCategoryMapping
* @return \Illuminate\Http\Response
*/
public function show(CompanyWithCategoryMapping $companyWithCategoryMapping)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\CompanyWithCategoryMapping $companyWithCategoryMapping
* @return \Illuminate\Http\Response
*/
public function edit(CompanyWithCategoryMapping $companyWithCategoryMapping)
{
//dd($companyWithCategoryMapping->toArray());
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\CompanyWithCategoryMapping $companyWithCategoryMapping
* @return \Illuminate\Http\Response
*/
public function update(Request $request, CompanyWithCategoryMapping $companyWithCategoryMapping)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\CompanyWithCategoryMapping $companyWithCategoryMapping
* @return \Illuminate\Http\Response
*/
public function destroy(CompanyWithCategoryMapping $companyWithCategoryMapping)
{
//dd($companyWithCategoryMapping);
if( $companyWithCategoryMapping->product_category_step == 1 ){
$cond['first_parent_id'] = $companyWithCategoryMapping->product_category_id;
$findSecondChildCatList = ProductCategory::select('id')->where($cond)->get()->toArray();
$storeIdOnly = [];
if( !empty($findSecondChildCatList) ){
foreach ($findSecondChildCatList as $key => $value) {
$storeIdOnly[] = $value['id'];
}
}
// start update delete user ID and Delete operation
CompanyWithCategoryMapping::whereIn('product_category_id', $storeIdOnly)->where('company_category_id', $companyWithCategoryMapping->company_category_id)->update([ 'deleted_user_id' => !empty(Auth::user()->id) ? Auth::user()->id : 0]);
CompanyWithCategoryMapping::whereIn('product_category_id', $storeIdOnly)->where('company_category_id', $companyWithCategoryMapping->company_category_id)->delete();
// end update delete user ID and Delete operation
}
if( $companyWithCategoryMapping->product_category_step == 2 ){
$cond['second_parent_id'] = $companyWithCategoryMapping->product_category_id;
$findSecondChildCatList = ProductCategory::select('id')->where($cond)->get()->toArray();
$storeIdOnly = [];
if( !empty($findSecondChildCatList) ){
foreach ($findSecondChildCatList as $key => $value) {
$storeIdOnly[] = $value['id'];
}
}
// start update delete user ID and Delete operation
CompanyWithCategoryMapping::whereIn('product_category_id', $storeIdOnly)->where('company_category_id', $companyWithCategoryMapping->company_category_id)->update([ 'deleted_user_id' => !empty(Auth::user()->id) ? Auth::user()->id : 0]);
CompanyWithCategoryMapping::whereIn('product_category_id', $storeIdOnly)->where('company_category_id', $companyWithCategoryMapping->company_category_id)->delete();
// end update delete user ID and Delete operation
}
if( $companyWithCategoryMapping->third_parent_id == 3 ){
$cond['third_parent_id'] = $companyWithCategoryMapping->product_category_id;
$findSecondChildCatList = ProductCategory::select('id')->where($cond)->get()->toArray();
$storeIdOnly = [];
if( !empty($findSecondChildCatList) ){
foreach ($findSecondChildCatList as $key => $value) {
$storeIdOnly[] = $value['id'];
}
}
// start update delete user ID and Delete operation
CompanyWithCategoryMapping::whereIn('product_category_id', $storeIdOnly)->where('company_category_id', $companyWithCategoryMapping->company_category_id)->update([ 'deleted_user_id' => !empty(Auth::user()->id) ? Auth::user()->id : 0]);
CompanyWithCategoryMapping::whereIn('product_category_id', $storeIdOnly)->where('company_category_id', $companyWithCategoryMapping->company_category_id)->delete();
// end update delete user ID and Delete operation
}
$data = CompanyWithCategoryMapping::find($companyWithCategoryMapping->id);
//dd($companyWithCategoryMapping->id);
if( $data->delete() && $data->update([ 'deleted_user_id' => !empty(Auth::user()->product_category_id) ? Auth::user()->id : 0]) ) {
$responseStatus = 'success';
$responseMessage = 'Company mapping deleted successfully.';
}else{
$responseStatus = 'error';
$responseMessage = 'Something went wrong, please try again.';
}
//$add = CompanyCompanyWithCategoryMapping::create(['name' => $request->input('name')]);
return redirect()->route('company-with-category-mappings.index')
->with($responseStatus, $responseMessage);
}
public function activeInactive($id)
{
$data = CompanyWithCategoryMapping::findOrFail($id);
//dd($data;die;
$responseMessage = '';
if($data->status == 0){
$input['status'] = 1;
$responseMessage = 'Company mapping active successfully';
}else{
$input['status'] = 0;
$responseMessage = 'Company mapping inactive successfully';
}
if( $data->update($input) ) {
$responseStatus = 'success';
if( $data->product_category_step == 1 ){
//dd('pp');
$cond['first_parent_id'] = $data->product_category_id;
$findSecondChildCatList = ProductCategory::select('id')->where($cond)->get()->toArray();
$storeIdOnly = [];
if( !empty($findSecondChildCatList) ){
foreach ($findSecondChildCatList as $key => $value) {
$storeIdOnly[] = $value['id'];
}
}
//dd($input);
//dd($storeIdOnly);
CompanyWithCategoryMapping::whereIn('product_category_id', $storeIdOnly)->where('company_category_id', $data->company_category_id)->update([ 'status' => $input['status'] ]);
}
if( $data->product_category_step == 2 ){
$cond['second_parent_id'] = $data->product_category_id;
$findSecondChildCatList = ProductCategory::select('id')->where($cond)->get()->toArray();
$storeIdOnly = [];
if( !empty($findSecondChildCatList) ){
foreach ($findSecondChildCatList as $key => $value) {
$storeIdOnly[] = $value['id'];
}
}
CompanyWithCategoryMapping::whereIn('product_category_id', $storeIdOnly)->where('company_category_id', $data->company_category_id)->update([ 'status' => $input['status'] ]);
}
if( $data->third_parent_id == 3 ){
$cond['third_parent_id'] = $data->product_category_id;
$findSecondChildCatList = ProductCategory::select('id')->where($cond)->get()->toArray();
$storeIdOnly = [];
if( !empty($findSecondChildCatList) ){
foreach ($findSecondChildCatList as $key => $value) {
$storeIdOnly[] = $value['id'];
}
}
CompanyWithCategoryMapping::whereIn('product_category_id', $storeIdOnly)->where('company_category_id', $data->company_category_id)->update([ 'status' => $input['status'] ]);
}
}else{
$responseStatus = 'error';
$responseMessage = 'Something went wrong, please try again.';
}
//$add = Country::create(['name' => $request->input('name')]);
return redirect()->route('company-with-category-mappings.index')
->with($responseStatus, $responseMessage);
}
}