Viewing file: VariantController.php (7.04 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\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)
{
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)
{
$pr= new ProductVariants;
$pr_table = $pr->getTable();
$columns = \Schema::getColumnListing($pr_table);
$params = collect($req->all())->keys();
foreach ($params as $key) {
if (in_array($key,$columns,true)) {
continue;
}else{
return response()->json(['error' => 'true', 'message' =>"invalid field ".$key]);
}
}
DB::beginTransaction();
try {
$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' => 5,
'product_id' => $product_id,
]);
} catch (\Exception $exception) {
DB::rollback();
return response()->json(['error' => 'true', 'message' =>$exception->errorInfo[2]]);
}
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"=>5,"_id"=>$t];
return response()->json(['message' => "Product Variant Added Successfully","data"=>$data]);
} else {
DB::rollback();
return response()->json(['error' => 'true','message' => "Something is wrong please try again"]);
}
}
// update category
public function update($product_id,Request $req)
{
// $pt= new ProductVariants;
// $pt_table = $pt->getTable();
// $columns = \Schema::getColumnListing($pt_table);
// $params = collect($req->all())->keys();
// foreach ($params as $key) {
// if (in_array($key,$columns,true)) {
// continue;
// }else{
// return response()->json(['error' => 'true', 'message' =>"invalid field ".$key]);
// }
// }
DB::beginTransaction();
try {
$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' => 5,
]);
} catch (\Exception $exception) {
DB::rollback();
return response()->json(['error' => 'true', 'message' =>$exception->errorInfo[2]]);
}
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();
return response()->json(['message' => "Updated Successfully"]);
} else {
DB::rollback();
return response()->json(['error' => 'true','message' => "Something is wrong please try again"]);
}
}
// variant image
public function image($variant_id,Request $req)
{
// return "hello";
try {
$variants = ProductVariants::where(['id' => $variant_id])->update([
'images' => $req->input('images')
]);
} catch (\Exception $exception) {
return response()->json(['error' => 'true', 'message' =>$exception->errorInfo[2]]);
}
if ($variants) {
return response()->json(['message' => "Updated Successfully"]);
} else {
return response()->json(['error' => 'true','message' => "Something is wrong please try again"]);
}
}
// delete categories
public function delete($id,Request $req)
{
return $id;
}
}
|