Viewing file: ProductController.php (6 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use App\Models\ProductVariants;
use App\Models\InventoryHistory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
// use GuzzleHttp\Client;
use DateTime;
class ProductController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//
}
// get product
function index($vendor_id)
{
return "helllo world";
}
//create new product
public function create($vendor_id,Request $req)
{
$pr= new Product;
$pr_table = $pr->getTable();
$columns_pa = \Schema::getColumnListing($pr_table);
$ps= new ProductVariants;
$ps_table = $ps->getTable();
$columns_ps = \Schema::getColumnListing($ps_table);
$columns=array_merge($columns_pa,$columns_ps);
$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]);
}
}
$product_code=$vendor_id.'_'."1001";
$pr_code=Product::select('product_code')->where('vendor_id',$vendor_id)->orderBY('id','desc')->first();
if ($pr_code) {
$temp=substr($pr_code->product_code, strpos($pr_code->product_code, "_") + 1);
// strstr($pr_code->product_code,$vendor_id.'_');
$product_code=$vendor_id.'_'.$temp+1;
}
DB::beginTransaction();
try {
$product= Product::create([
'page_id' => $req->input('page_id'),
'title' => $req->input('title'),
'product_code'=>$product_code,
'vendor_id' => $vendor_id,
'description' => $req->input('description'),
"folder" => $req->input("folder"),
'visibility' => $req->input('visibility'),
'categories_1' => $req->input('categories_1'),
'categories_2' => $req->input('categories_2'),
'categories_3' => $req->input('categories_3'),
'sub_categories_1' => $req->input('sub_categories_1'),
'sub_categories_2' => $req->input('sub_categories_2'),
'sub_categories_3' => $req->input('sub_categories_3')
]);
} catch (\Exception $exception) {
DB::rollback();
return response()->json(['error' => 'true', 'message' =>$exception->errorInfo[2]]);
}
if ($product) {
try {
$ProductVariants = ProductVariants::create([
'name' => 'Default Variant',
'buy_price' => $req->input('buy_price'),
'price' => $req->input('price'),
'inventory' => $req->input('inventory'),
"supplier" => $req->input("supplier"),
'low_stock' => 5,
'product_id' => $product->id
]);
}
catch (\Exception $exception) {
DB::rollback();
return response()->json(['error' => 'true', 'message' =>$exception->errorInfo[2]]);
}
$InventoryHistory = InventoryHistory::create([
'variant_id' => $ProductVariants->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();
$d = new DateTime();
$t=$d->format("ymdHisv");
$data=["_id"=>$t,"id"=>$product->id,"variant_id"=>$ProductVariants->id,'product_code'=>$product_code,"created_at"=> $product->created_at];
return response()->json(['message' => "Product Added Successfully","data"=>$data]);
} else {
DB::rollback();
return response()->json(['error' => 'true','message' => "Something is wrong please try again"]);
}
}
// update product
public function update($vendor_id,Request $req)
{
$pr= new Product;
$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]);
}
}
$product = Product::where('id',$req->input('id'))->first();
if (empty($product)) {
return response()->json(['error' => 'true', 'message' =>"Invalid product"]);
}else{
$product->title = $req->input('title');
$product->description = $req->input('description');
$product->categories_1 = $req->input('categories_1');
$product->categories_2 = $req->input('categories_2');
$product->categories_3 = $req->input('categories_3');
$product->sub_categories_1 = $req->input('sub_categories_1');
$product->sub_categories_2 = $req->input('sub_categories_2');
$product->sub_categories_3 = $req->input('sub_categories_3');
$product->visibility = $req->input('visibility');
$ven=$product->save();
if ($ven) {
return response()->json(['message' => "Product Successfully Updated."]);
}else {
return response()->json(['error' => 'true', 'message' => "Something is wrong please try again."]);
}
}
}
// delete categories
public function delete($id,Request $req)
{
return $id;
}
}
|