Viewing file: VariantController.php (9.14 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use App\Models\ProductTranslation;
use App\Models\ProductVariants;
use App\Models\InventoryHistory;
use App\Models\StoreSettings;
use App\Models\Categories;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use DateTime;
class VariantController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//
}
// get product
function index($product_id,Request $req)
{
return response()->json("not allowed",403);
// if (count($req->query())===0) {
// $variants= ProductVariants::where("product_id",$product_id)->get();
// return response()->json(["data"=>$variants]);
// }else{
// $pr= new ProductVariants;
// $table = $pr->getTable();
// $columns = \Schema::getColumnListing($table);
// $variants = ProductVariants::query();
// $params = $req->query();
// foreach ($params as $key => $value) {
// if (in_array($key,$columns,true)) {
// if (!empty($value) || $value==0){
// $variants->where($key,$value);
// }
// }else{
// return response()->json(['error' => 'true', 'message' =>"invalid parameter ".$key]);
// }
// }
// $variants=$variants->where("product_id",$product_id)->get();
// return response()->json(["data"=>$variants]);
// }
}
//create new category
public function create($product_id,Request $req)
{
try {
$vendor_id =auth()->user()->id;
$validator = Validator::make($req->all(), [
'name' => 'required|string|between:2,120',
"price" => 'required',
"supplier" => 'nullable|string|between:2,120',
"visibility" => 'required|integer|in:0,1',
"inventory_check" => 'required|integer|in:0,1',
"inventory" =>'required|integer|between:0,3000'
]);
if ($validator->fails()) {
return response()->json(['error' => 'true', 'message' => $validator->errors()->all()],402);
}
DB::beginTransaction();
$data=StoreSettings::select('low_stock')->where('vendor_id',$vendor_id)->first();
$low_stock=ceil(($data->low_stock*$req->input('inventory'))/100);
$product = ProductVariants::create([
'name' => $req->input('name'),
'tags_key' => $req->input('tags_key'),
'tags' => $req->input('tags'),
'price' => $req->input('price'),
'buy_price' => $req->input('buy_price'),
'supplier' => $req->input('supplier'),
'inventory' => $req->input('inventory'),
'visibility' => $req->input('visibility'),
'inventory_check' => $req->input('inventory_check'),
'low_stock' => $low_stock,
'product_id' => $product_id,
]);
} catch (\Exception $exception) {
DB::rollback();
return response()->json(['error' => 'true', 'message' =>$exception->getMessage()],500);
}
try {
if ($product) {
$InventoryHistory = InventoryHistory::create([
'variant_id' => $product->id,
'product_id' => $product_id,
'no_of_items' => $req->input('inventory'),
"supplier" => $req->input("supplier"),
'buy_price' => $req->input('buy_price'),
'type' => 1,
"previous_quantity" => 0,
]);
DB::commit();
// return response()->json(['message' => "Product Variant Added Successfully"]);
$d = new DateTime();
$t=$d->format("ymdHisv");
$data=["variant_id"=>$product->id,"low_stock"=>$low_stock,"_id"=>$t];
return response()->json(['message' => "Product Variant Added Successfully","data"=>$data]);
} else {
DB::rollback();
return response()->json(['error' => 'true','message' => "Variant not created"],500);
}
} catch (\Exception $exception) {
DB::rollback();
return response()->json(['error' => 'true', 'message' =>$exception->getMessage()],500);
}
}
// update category
public function update($product_id,Request $req)
{
$vendor_id =auth()->user()->id;
$validator = Validator::make($req->all(), [
'name' => 'required|string|between:2,120',
"price" => 'required',
"supplier" => 'nullable|string|between:2,120',
"visibility" => 'required|integer|in:0,1',
"inventory_check" => 'required|integer|in:0,1',
"inventory" =>'required|integer|between:0,3000'
]);
if ($validator->fails()) {
return response()->json(['error' => 'true', 'message' => $validator->errors()->all()],402);
}
try {
DB::beginTransaction();
$data=StoreSettings::select('low_stock')->where('vendor_id',$vendor_id)->first();
$low_stock=ceil(($data->low_stock*$req->input('inventory'))/100);
$variants = ProductVariants::where(['id' => $req->input('id')])->update([
'name' => $req->input('name'),
'tags_key' => $req->input('tags_key'),
'tags' => $req->input('tags'),
'price' => $req->input('price'),
'buy_price' => $req->input('buy_price'),
'supplier' => $req->input('supplier'),
'inventory' => $req->input('inventory'),
'visibility' => $req->input('visibility'),
'inventory_check' => $req->input('inventory_check'),
'low_stock' => $low_stock,
]);
} catch (\Exception $exception) {
DB::rollback();
return response()->json(['error' => 'true', 'message' =>$exception->getMessage()],500);
}
try{
if ($variants) {
if ($req->input('no_of_items')>0) {
$InventoryHistory = InventoryHistory::create([
'variant_id' => $req->input('id'),
'product_id' => $product_id,
'no_of_items' => $req->input('no_of_items'),
'type' => $req->input('inventory_type'),
"supplier" => $req->input("supplier"),
'buy_price' => $req->input('buy_price'),
"previous_quantity" => $req->input('previous_quantity'),
]);
}
DB::commit();
$data=["low_stock"=>$low_stock];
return response()->json(['message' => "Updated Successfully","data"=>$data]);
} else {
DB::rollback();
return response()->json(['error' => 'true','message' => "Variant not updated"],500);
}
} catch (\Exception $exception) {
DB::rollback();
return response()->json(['error' => 'true', 'message' =>$exception->getMessage()],500);
}
}
// variant image
public function image($variant_id,Request $req)
{
// return "hello";
try {
$variants = ProductVariants::where(['id' => $variant_id])->update([
'images' => $req->input('images')
]);
if ($variants) {
return response()->json(['message' => "Updated Successfully"]);
} else {
return response()->json(['error' => 'true','message' => "Variant image info not updated"],500);
}
} catch (\Exception $exception) {
return response()->json(['error' => 'true', 'message' =>$exception->getMessage()],500);
}
}
// delete product variant
public function delete($variant_id,Request $req)
{
try{
$product = ProductVariants::where('id',$variant_id)->first();
if (empty($product)) {
return response()->json(["error"=>"true",'message' => "Invalid variant"],402);
}else {
$product->is_archived=1;
$product->save();
}
if (intval($req->input('product_id'))>0) {
Product::find($req->input('product_id'))->update([
'visibility'=>0
]);
}
return response()->json(['message' => "Successfully Deleted."]);
} catch (\Exception $exception) {
return response()->json(['error' => 'true', 'message' =>$exception->getMessage()],500);
}
}
}
|