Viewing file: ProductController.php (25.5 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller; use App\Models\Category; use App\Models\CategoryDescription; use App\Models\LengthClass; use App\Models\Manufacturer; use App\Models\Product; use App\Models\ProductOption; use App\Models\ProductAttributeGroup; use App\Models\ProductDescription; use App\Models\ProductDiscount; use App\Models\ProductImage; use App\Models\ProductRelated; use App\Models\Setting; use App\Models\StoreProductOption; use App\Models\ProductRelatedAttribute; use App\Models\ProductSpecial; use App\Models\StockStatus; use App\Models\TaxRate; use App\Models\WeightClass; use App\Traits\CustomFileTrait; use Carbon\Carbon; use GuzzleHttp\Client; use Illuminate\Database\Eloquent\Model; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\File; use Illuminate\Support\Str;
class ProductController extends Controller { use CustomFileTrait; protected $path = ''; protected $customDateFormat = ''; protected $databaseDateFormat = '';
public function __construct() { $this->path = public_path(config('constant.file_path.product')); $this->customDateFormat = config('constant.date_format')['custom_date_format']; $this->databaseDateFormat = config('constant.date_format')['database_date_format']; }
public function index(Request $request) {
$name = $request->get('name', ''); $model = $request->get('model', ''); $quantity = $request->get('quantity', ''); $status = $request->get('status', '1');
$records = Product::select('id','image','category_id', 'model','price', 'location', 'quantity','sort_order','status') ->with('productDescription:name,id,product_id','category:name,category_id') ->when($name != ''|| $model != '' || $quantity != '' || $status != '' , function($q) use($name,$model,$quantity,$status) { $q->where('model','like',"%$model%"); $q->where('quantity','like',"%$quantity%"); $status != '2' ? $q->where('status','like',"%$status%") : null; $q->whereHas('productDescription',function($q) use($name){ $q->where('name','like',"%$name%"); }); })->orderBy('created_at','DESC')->paginate($this->defaultPaginate);
return view('admin.product.index',['records' => $records,'status' => $status]); }
protected function getRequiredData () { $data['category'] = Category::getActivePluck(); $data['stock_status'] = StockStatus::getActivePluck(); $data['manufacturer'] = Manufacturer::getActivePluck(); $data['tax_rate'] = TaxRate::getActivePluck(); $data['lenght_class'] = LengthClass::getActivePluck(); $data['weight_class'] = WeightClass::getActivePluck(); $data['pluckProducts'] = Product::with('productDescription:name,id,product_id')->has('productDescription')->get(); $data['product_options'] = ProductOption::with('productoptionDescription')->get(); $data['attributeArray'] = ProductAttributeGroup::with('ProductAttributeGroupDescription:language_id,attribute_group_id,name','relationAttributes:language_id,name,id,group_id')->select('id')->get();
return $data; }
public function add() { $data = $this->getRequiredData();
return view('admin.product.add',['data' => $data,'chatGPT' => $this->getChatGPTConfig()]); }
public function store(Request $request) {
DB::beginTransaction();
try { $this->validateData($request);
$product = new Product($request->only('model', 'sku', 'quantity', 'price', 'category_id'));
//if has main image if ($request->hasFile('main_image')) { $this->createDirectory($this->path); $product->image = $this->saveCustomFileAndGetImageName(request()->file('main_image'), $this->path); }
$product->stock_status_id = $request->stock_status_id ? $request->stock_status_id : 0; $product->manufacturer_id = $request->manufacturer_id ? $request->manufacturer_id : 0; $product->tax_rate_id = $request->tax_rate_id ? $request->tax_rate_id : 0; if ($request->date_available) { $product->date_available = $request->date_available; } else { $product->date_available = date('d/m/Y'); }
$product->length = $request->length ? $request->length : 0; $product->width = $request->width ? $request->width : 0; $product->height = $request->height ? $request->height : 0; $product->weight_class_id = $request->weight_class_id ? $request->weight_class_id : 0; $product->weight = $request->weight ? $request->weight : 0; $product->status = $request->status ? $request->status : 0; $product->sort_order = $request->sort_order ? $request->sort_order : 0;
$product->save();
// Save Description // $description = new ProductDescription($request->only('name','description','meta_title','meta_description','meta_keyword')); // $description->product_id = $product->id; // $description->save();
$description = new ProductDescription(); $buildMultiLanguage = $description->buildMultiLang($product->id, $request->multilanguage); $description->upsert($buildMultiLanguage, ['product_id', 'short_description', 'description', 'language_id', 'name', 'meta_title', 'meta_description', 'meta_keyword']);
// Save Attributes $attributesArray = $this->getAttributeProductData($product->id, $request->attributesArray); if (count($attributesArray) > 0) { ProductRelatedAttribute::insert($attributesArray); }
// Save Related Product $relatedProducts = $this->getRelatedProductData($product->id, $request->related_id); ProductRelated::insert($relatedProducts);
// Save Product Image if ($request->product_image) { if (array_key_exists('image', $request->product_image)) { $productImages = $this->getproductImages($product->id, $request->product_image); ProductImage::insert($productImages); } }
// Save Product Special if ($request->special_price != null) { $specialProduct = [ 'product_id' => $product->id, 'price' => $request->special_price, 'start_date' => $this->changeDateFormat($request->start_date, $this->customDateFormat, $this->databaseDateFormat), 'end_date' => $this->changeDateFormat($request->end_date, $this->customDateFormat, $this->databaseDateFormat) ]; ProductSpecial::insert($specialProduct); }
//save Options $optionArr = []; if ($request->optionPost) { $postOptions = explode(',', $request->optionPost); foreach ($postOptions as $key => $value) { for ($i = 0; $i < count($request->option[$value]['label']); $i++) { if ($request->option[$value]['label'][$i] != null) { $optionArr [] = [ 'label' => $request->option[$value]['label'][$i], 'price' => $request->option[$value]['price'][$i], 'color_code' => array_key_exists('color_code', $request->option[$value]) ? $request->option[$value]['color_code'][$i] : '', 'option_id' => $value, 'product_id' => $product->id ]; } } } }
if (count($optionArr) > 0) { StoreProductOption::insert($optionArr); }
//Section Inventory API $api_url = Setting::where('key', 'api_url')->first(); $api_token = Setting::where('key', 'api_token')->first(); if ($api_url && isset($api_url->value) && $api_token && isset($api_token->value)) { try{ $client = new Client();
$api_form_data=[ 'type' => 'is_service', 'name' => isset($request->multilanguage['4']['name']) ? $request->multilanguage['4']['name'] : 'Product-' . Str::random(4), 'code' => strtoupper(Str::random(6)), 'category_id' => $request->category_id, 'brand_id' => $request->manufacturer_id, 'tax_method' => '1', 'price' => $request->price, 'quantity' => $request->quantity ];
$response = $client->post($api_url->value . '/api/product/store', [ 'headers' => [ 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $api_token->value, ], 'form_params' => $api_form_data ]); $response = $response->getBody()->getContents();
if (isset(json_decode($response)->status) && json_decode($response)->status != 'success') { throw new \Exception('Invalid Request'); } }catch(\Exception $ex){ throw new \Exception($ex->getMessage()); } } //END
cache()->forget('newProducts'); cache()->forget('trendingProducts'); cache()->forget('topBrands'); cache()->flush(); DB::commit();
return redirect(route('product'))->with('success', 'Product Created Successfully'); }catch(\Exception $ex){ DB::rollBack(); return redirect(route('product'))->withErrors(['errors'=>$ex->getMessage()]); } }
public function edit($id) {
$data = $this->getRequiredData(); $data['relatedIds'] = ProductRelated::getRelatedIds($id); $data['productRelatedAttribute'] = ProductRelatedAttribute::pluckByProduct($id); $data['attributeIds'] = array_keys($data['productRelatedAttribute']); $data['productOptions'] = StoreProductOption::with('productoptionDescription') ->where('product_id',$id)->get();
//get unique id $optionids = []; $optionID = 0; $optionCommaSeprate ='';
foreach ($data['productOptions'] as $key => $value) { if($value->option_id != $optionID) { $optionids []=$value->option_id; } $optionID = $value->option_id; }
$optionids = array_unique($optionids);
$data['optionCommaSeprate'] = implode(',',$optionids); $data['optionIDArr'] = $optionids;
$data['options'] = ProductOption::whereIn('id',$optionids)->with('productoptionDescription')->get();
$data['data'] = Product::with('category','productMultipleDescription','images','special','discountProduct')->findOrFail($id);
return view('admin.product.edit',[ 'data' => $data, ]); }
public function update(Request $request,$id) {
DB::beginTransaction();
try { $this->validateData($request);
$product = Product::whereId($id)->first();
if ($request->hasFile('main_image')) { $this->removeOldImage($product->image, $this->path); $product->image = $this->saveCustomFileAndGetImageName(request()->file('main_image'), $this->path); }
$product->fill($request->only(Product::$fillableValue))->save();
$product->productRelated()->delete();
// Save Related Product $relatedProducts = $this->getRelatedProductData($product->id, $request->related_id); ProductRelated::insert($relatedProducts);
// Save Attributes ProductRelatedAttribute::deleteByProduct($id);
$attributesArray = $this->getAttributeProductData($product->id, $request->attributesArray); if (count($attributesArray) > 0) { ProductRelatedAttribute::insert($attributesArray); }
$description = new ProductDescription(); $description->where('product_id', $id)->delete(); $buildMultiLanguage = $description->buildMultiLang($product->id, $request->multilanguage); $description->upsert($buildMultiLanguage, ['product_id', 'short_description', 'description', 'language_id', 'name', 'meta_title', 'meta_description', 'meta_keyword']);
//update Options StoreProductOption::where('product_id', $id)->delete(); $optionArr = []; if ($request->optionPost) { $postOptions = explode(',', $request->optionPost);
foreach ($postOptions as $key => $value) { for ($i = 0; $i < count($request->option[$value]['label']); $i++) { if ($request->option[$value]['label'][$i] != null) { $optionArr [] = [ 'label' => $request->option[$value]['label'][$i], 'price' => $request->option[$value]['price'][$i], 'color_code' => array_key_exists('color_code', $request->option[$value]) ? $request->option[$value]['color_code'][$i] : '', 'option_id' => $value, 'product_id' => $product->id ]; } } } }
if (count($optionArr) > 0) { StoreProductOption::insert($optionArr); }
//image update $oldImagesData = ProductImage::where('product_id', $id)->get(); $oldImageIds = $oldImagesData->pluck('id')->toArray(); $productImageIds = []; $newImageArray = []; if ($request->product_image['sort_order_image']) { foreach ($request->product_image['sort_order_image'] as $key => $value) { if (isset($request->product_image['id'][$key])) { $imageId = $request->product_image['id'][$key]; $productImageIds[] = $imageId; $productImage = ProductImage::whereId($imageId)->first(); if (isset($request->product_image['image'][$key])) { $image = $request->product_image['image'][$key]; $this->removeOldImage($productImage->image, $this->path); $productImage->image = $this->saveCustomFileAndGetImageName($image, $this->path); } $productImage->sort_order_image = $request->product_image['sort_order_image'][$key]; $productImage->save(); } else { if (array_key_exists('image', $request->product_image)) { $image = $request->product_image['image'][$key]; $imageName = $this->saveCustomFileAndGetImageName($image, $this->path); $newImageArray[] = [ 'product_id' => $id, 'sort_order_image' => $request->product_image['sort_order_image'][$key], 'image' => $imageName ]; }
} } }
$deletedImageIds = array_diff($oldImageIds, $productImageIds); $oldProductImages = $oldImagesData->pluck('image', 'id')->toArray();
ProductImage::whereIn('id', $deletedImageIds)->delete(); ProductImage::insert($newImageArray);
// Remove deleted Images foreach ($deletedImageIds as $key => $value) { $this->removeOldImage($oldProductImages[$value], $this->path); }
if ($request->special_price != null) { ProductSpecial::where('product_id', $id)->delete(); $newSpecialArray[] = [ 'product_id' => $id, 'price' => $request->special_price, 'start_date' => $this->changeDateFormat($request->start_date, $this->customDateFormat, $this->databaseDateFormat), 'end_date' => $this->changeDateFormat($request->end_date, $this->customDateFormat, $this->databaseDateFormat) ];
ProductSpecial::insert($newSpecialArray);
}
//Section Inventory API $api_url = Setting::where('key', 'api_url')->first(); $api_token = Setting::where('key', 'api_token')->first(); if ($api_url && isset($api_url->value) && $api_token && isset($api_token->value)) { $client = new Client();
$api_form_data = [ 'type' => 'is_service', 'name' => isset($request->multilanguage['4']['name']) ? $request->multilanguage['4']['name'] : 'Product-' . Str::random(4), 'code' => strtoupper(Str::random(6)), 'category_id' => $request->category_id, 'brand_id' => $request->manufacturer_id, 'tax_method' => '1', 'price' => $request->price, 'quantity' => $request->quantity ];
$response = $client->post($api_url->value . '/api/product/update/'.$id, [ 'headers' => [ 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $api_token->value, ], 'form_params' => $api_form_data ]); $response = $response->getBody()->getContents(); if (isset(json_decode($response)->status) && json_decode($response)->status != 'success') { throw new \Exception('Invalid Request'); } } //END
DB::commit();
cache()->forget('newProducts'); cache()->forget('trendingProducts'); cache()->forget('topBrands');
cache()->flush(); return redirect(route('product'))->with('success', 'Product Updated Successfully'); }catch(\Exception $ex){ DB::rollBack(); return redirect()->back()->withErrors(['failed'=>$ex->getMessage()]); } }
protected function updateDiscountProduct($request,$productId) { $existIds = []; $newDiscountArray = []; foreach ($request->discount['discount_price'] as $key => $value ) { if (isset($request->discount['id'][$key])) { $discountid = $request->discount['id'][$key]; $existIds[] = $discountid; $productDiscount = ProductDiscount::whereId($discountid)->first();
$productDiscount->sort_order_discount = $request->discount['sort_order_discount'][$key]; $productDiscount->price = $request->discount['discount_price'][$key]; $productDiscount->quantity = $request->discount['quantity'][$key]; $productDiscount->start_date = $this->changeDateFormat($request->discount['start_date'][$key],$this->customDateFormat,$this->databaseDateFormat); $productDiscount->end_date = $this->changeDateFormat($request->discount['end_date'][$key],$this->customDateFormat,$this->databaseDateFormat); $productDiscount->save(); } else { $newDiscountArray[] = [ 'product_id' => $productId, 'sort_order_discount' => $request->discount['sort_order_discount'][$key], 'price' => $request->discount['discount_price'][$key], 'quantity' => $request->discount['quantity'][$key], 'start_date' => $this->changeDateFormat($request->discount['start_date'][$key],$this->customDateFormat,$this->databaseDateFormat), 'end_date' => $this->changeDateFormat($request->discount['end_date'][$key],$this->customDateFormat,$this->databaseDateFormat) ]; } } ProductDiscount::whereNotIn('id',$existIds)->where('product_id', $productId)->delete(); ProductDiscount::insert($newDiscountArray); cache()->forget('newProducts'); cache()->forget('trendingProducts'); cache()->forget('topBrands'); }
public function delete($id) { DB::beginTransaction();
try{ if(! $data = Product::whereId($id)->first()) { return redirect()->back()->with('error', 'Something went wrong'); }
$this->removeOldImage($data->image,$this->path); $images = $data->images()->pluck('image'); if(count($images) > 0){ foreach($images as $key => $value) { $this->removeOldImage($value,$this->path); } }
$data->productRelated()->delete(); $data->special()->delete(); $data->images()->delete(); $data->productDescription()->delete(); $data->delete();
//Section Inventory API $api_url = Setting::where('key', 'api_url')->first(); $api_token = Setting::where('key', 'api_token')->first(); if ($api_url && isset($api_url->value) && $api_token && isset($api_token->value)) { $client = new Client();
$response = $client->get($api_url->value . '/api/product/delete/'.$id, [ 'headers' => [ 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $api_token->value, ], ]); $response = $response->getBody()->getContents();
if (isset(json_decode($response)->status) && json_decode($response)->status != 'success') { throw new \Exception('Invalid Request'); } } //END
cache()->forget('newProducts'); cache()->forget('trendingProducts'); cache()->forget('topBrands'); DB::commit(); return redirect(route('product'))->with('success', 'Product Deleted Successfully'); }catch(\Exception $ex){ DB::rollBack(); return redirect(route('product'))->withErrors(['errors'=>$ex->getMessage()]); } }
public function getDetail(Request $request) { return Product::select('model','price','id')->with('productDescription:name,product_id')->whereId($request->id)->first(); }
protected function getRelatedProductData($productId,$relatedIds) { $dataArray = []; if(isset($relatedIds)) { foreach($relatedIds as $key => $value) { $dataArray[] = [ 'product_id' => $productId, 'related_id' => $value ]; } } return $dataArray; }
protected function getDiscountProductData($productId,$discount) { $dataArray = []; foreach($discount['discount_price'] as $key => $value) { $dataArray[] = [ 'product_id' => $productId, 'quantity' => $discount['quantity'][$key], 'sort_order_discount' => $discount['sort_order_discount'][$key], 'price' => $discount['discount_price'][$key], 'start_date' => $this->changeDateFormat($discount['start_date'][$key],$this->customDateFormat,$this->databaseDateFormat), 'end_date' => $this->changeDateFormat($discount['end_date'][$key],$this->customDateFormat,$this->databaseDateFormat) ]; } return $dataArray; }
protected function getAttributeProductData($productId,$attributesArray) { $dataArray = [];
if($attributesArray) { foreach ($attributesArray as $key => $value) { if ($value['text'] != null) { $dataArray[] = [ 'product_id' => $productId, 'attribute_id' => $value['attribute_id'] ?? 2, 'text' => $value['text'], ]; }
} } return $dataArray; }
protected function getproductImages($productId,$productImages) { $dataArray = [];
foreach($productImages['sort_order_image'] as $key => $value) { $image = $this->saveCustomFileAndGetImageName($productImages['image'][$key],$this->path); $dataArray[] = [ 'product_id' => $productId, 'sort_order_image' => $value, 'image' => $image ]; } return $dataArray; }
protected function validateData ($request) {
$conditionArray = [];
$validateFields = [ 'multilanguage.*.name' => ['required'], 'category_id' => ['required'], 'model' => ['required'], 'quantity' => ['required'], 'price' => ['required'], ];
$validationArray = array_merge($conditionArray,$validateFields); $this->validate($request,$validationArray); }
}
|