Created
September 22, 2019 16:05
-
-
Save ahmadwaliesipick/9eb8f759ec8a5b360b7b8263e12289c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use App\Http\Requests\ProductCreateRequest; | |
use App\Http\Requests\ProductDeleteRequest; | |
use App\Http\Requests\ProductEditRequest; | |
use App\Http\Requests\ProductGetRequest; | |
use App\Http\Requests\ProductPutRequest; | |
use App\Inventory; | |
use App\Product; | |
use App\Vendor; | |
use http\Url; | |
use Illuminate\Http\Request; | |
use Dingo\Api\Routing\Helpers; | |
use Illuminate\Support\Facades\Auth; | |
use Illuminate\Support\Facades\DB; | |
use App\Attachment; | |
use Intervention\Image\Facades\Image; | |
/** | |
* @group Product management | |
* | |
* */ | |
class ProductController extends Controller | |
{ | |
use Helpers; | |
/** | |
* Create product | |
* | |
*/ | |
public function create(ProductCreateRequest $request){ | |
$vendor_id = null; | |
if($request->vendor){ | |
$vendor = Vendor::firstOrCreate(['name' => $request->vendor,'company_id'=>$request->company_id]); | |
$vendor_id = $vendor->id; | |
} | |
$data=[ | |
'title'=>$request->title, | |
'price'=>$request->price, | |
'user_id'=>Auth::user()->id, | |
'vendor_id'=>$vendor_id, | |
'category_id'=>$request->category_id, | |
'company_id'=>$request->company_id, | |
'slug'=>str_slug($request->title), | |
]; | |
$product = Product::create($data); | |
if(!empty($request->images)){ | |
foreach($request->images as $image){ | |
Attachment::create([ | |
'ref_id'=> $product->id, | |
'modal'=> 'product', | |
'name'=> $image, | |
]); | |
} | |
} | |
return $this->response->array($product); | |
} | |
/** | |
* Get products | |
* | |
*/ | |
public function get(ProductGetRequest $request){ | |
$products = Product::with('category','vendor','inventory','attachments')->withCount([ | |
'inventory_up AS inventory_up_sum' => function ($serviceRequest) { | |
$serviceRequest->select(DB::raw("SUM(quantity) as inventory_up_sum")); | |
} | |
])->withCount([ | |
'inventory_down AS inventory_down_sum' => function ($serviceRequest) { | |
$serviceRequest->select(DB::raw("SUM(quantity) as inventory_down_sum")); | |
} | |
]); | |
if($request->date_to && $request->date){ | |
$products = $products->whereBetween('created_at', [$request->date.' 00:00:01', | |
$request->date_to.' 23:59:59']); | |
}elseif($request->date){ | |
$products = $products->whereDate('created_at', $request->date); | |
} | |
$products = $products->whereNull('deleted_at'); | |
if($request->company_id){ | |
$products = $products->where('company_id', $request->company_id); | |
} | |
if($request->id){ | |
return $this->response->array($products->find($request->id)); | |
} | |
if(!empty($request->title)){ | |
$products = $products->where('title','like', '%'.$request->title.'%'); | |
} | |
if(!empty($request->sortBy)){ | |
$descending= 'desc'; | |
if(!empty($request->descending) && $request->descending == 'false'){ | |
$descending= 'asc'; | |
} | |
if($request->sortBy == 'title' || $request->sortBy == 'id' || $request->sortBy == 'price'){ | |
$products = $products->orderBy($request->sortBy,$descending); | |
} | |
}else{ | |
$products = $products->orderBy('inventory_down_sum','desc'); | |
} | |
if($request->size){ | |
if($request->size > 0){ | |
$finalProducts = $products->paginate($request->size); | |
}else{ | |
$finalProducts = $products->paginate($products->get()->count()); | |
} | |
foreach($finalProducts as &$finalProduct){ | |
$finalProduct->available_qty = $finalProduct->inventory_up_sum - $finalProduct->inventory_down_sum; | |
} | |
return $this->response->array($finalProducts); | |
} | |
$products = $products->get(); | |
return $this->response->array($products); | |
} | |
/** | |
* Edit product | |
* | |
*/ | |
public function edit(ProductEditRequest $request){ | |
$vendor_id= null; | |
if($request->vendor){ | |
$vendor = Vendor::firstOrCreate(['name' => $request->vendor,'company_id'=>$request->company_id]); | |
$vendor_id = $vendor->id; | |
} | |
$product = Product::find($request->id); | |
$product->title = $request->title; | |
$product->vendor_id = $vendor_id; | |
$product->slug=str_slug($request->title); | |
$product->price = $request->price; | |
$product->category_id = $request->category_id; | |
$product->save(); | |
if(!empty($request->images)){ | |
Attachment::where('ref_id',$product->id)->where('modal','product')->delete(); | |
foreach($request->images as $image){ | |
Attachment::create([ | |
'ref_id'=> $product->id, | |
'modal'=> 'product', | |
'name'=> $image, | |
]); | |
} | |
} | |
return $this->response->array($product); | |
} | |
/** | |
* Update Product Status | |
* | |
*/ | |
public function put(ProductPutRequest $request){ | |
$product = Product::find($request->id); | |
$product->status = $request->status; | |
$product->save(); | |
return $this->response->array($product); | |
} | |
/** | |
* Delete product | |
* | |
*/ | |
public function delete(ProductDeleteRequest $request){ | |
$product = Product::find($request->id); | |
$product = $product->delete(); | |
return $this->response->array($product); | |
} | |
public function uploadImages(Request $request){ | |
if(empty($request->file('capture_image'))){ | |
return ['status'=>'error','msg'=>'No image uploaded!']; | |
} | |
$images =[]; | |
$imagesHelp =[]; | |
if(is_array($request->file('capture_image'))){ | |
foreach($request->file('capture_image') as $image){ | |
$path = $image->store('product_images'); | |
$path = basename($path); | |
$images[]=$path; | |
$imagesHelp[$path]=$image->getClientOriginalName(); | |
} | |
}else{ | |
$path = $request->file('capture_image')->store('product_images'); | |
$path = basename($path); | |
$images[]=$path; | |
$imagesHelp[$path]=$request->file('capture_image')->getClientOriginalName(); | |
//save thumbnail | |
$resize = Image::make($request->file('capture_image'))->resize(320, 240); | |
$resize->save(storage_path('app/public/product_images_thumbnails/'.$path)); | |
} | |
return ['images'=>$images,'imagesHelp'=>$imagesHelp]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment