| Server IP : 14.225.204.176 / Your IP : 216.73.216.6 Web Server : nginx/1.24.0 System : Linux nodejs-ybgk 6.8.0-40-generic #40-Ubuntu SMP PREEMPT_DYNAMIC Fri Jul 5 10:34:03 UTC 2024 x86_64 User : root ( 0) PHP Version : 8.1.34 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/html/php/thanhphuong/app/Http/Controllers/ |
Upload File : |
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Keygen;
use App\Brand;
use App\Category;
use App\Unit;
use Carbon\Carbon;
use App\Tax;
use App\Warehouse;
use App\Supplier;
use App\Product;
use App\Purchase;
use App\ProductPurchase;
use App\Product_Warehouse;
use App\Product_Supplier;
use App\GeneralSetting;
use Auth;
use DNS1D;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Illuminate\Validation\Rule;
use DB;
use App\Variant;
use App\ProductVariant;
class ProductController extends Controller
{
public function index()
{
$role = Role::find(Auth::user()->role_id);
if($role->hasPermissionTo('products-index')){
$permissions = Role::findByName($role->name)->permissions;
foreach ($permissions as $permission)
$all_permission[] = $permission->name;
if(empty($all_permission))
$all_permission[] = 'dummy text';
return view('product.index', compact('all_permission'));
}
else
return redirect()->back()->with('not_permitted', 'Sorry! You are not allowed to access this module');
}
public function productData(Request $request)
{
$columns = array(
2 => 'name',
3 => 'code',
4 => 'brand_id',
6 => 'qty',
);
Product::query()->update(['alert_quantity' => 5]);
$totalData = Product::where('is_active', true)->count();
$totalFiltered = $totalData;
if($request->input('length') != -1)
$limit = $request->input('length');
else
$limit = $totalData;
$start = $request->input('start');
$order = 'products.'.$columns[$request->input('order.0.column')];
$dir = $request->input('order.0.dir');
if(empty($request->input('search.value'))){
$products = Product::with('category', 'brand', 'unit')->offset($start)
->where('is_active', true)
->limit($limit)
->orderBy($order,$dir)
->get();
}
else
{
$search = $request->input('search.value');
$products = Product::select('products.*')
->with('category', 'brand', 'unit')
->join('categories', 'products.category_id', '=', 'categories.id')
->leftjoin('brands', 'products.brand_id', '=', 'brands.id')
->where([
['products.name', 'LIKE', "%{$search}%"],
['products.is_active', true]
])
->orWhere([
['products.code', 'LIKE', "%{$search}%"],
['products.is_active', true]
])
->orWhere([
['categories.name', 'LIKE', "%{$search}%"],
['categories.is_active', true],
['products.is_active', true]
])
->orWhere([
['brands.title', 'LIKE', "%{$search}%"],
['brands.is_active', true],
['products.is_active', true]
])
->offset($start)
->limit($limit)
->orderBy($order,$dir)->get();
$totalFiltered = Product::
join('categories', 'products.category_id', '=', 'categories.id')
->leftjoin('brands', 'products.brand_id', '=', 'brands.id')
->where([
['products.name','LIKE',"%{$search}%"],
['products.is_active', true]
])
->orWhere([
['products.code', 'LIKE', "%{$search}%"],
['products.is_active', true]
])
->orWhere([
['categories.name', 'LIKE', "%{$search}%"],
['categories.is_active', true],
['products.is_active', true]
])
->orWhere([
['brands.title', 'LIKE', "%{$search}%"],
['brands.is_active', true],
['products.is_active', true]
])
->count();
}
$data = array();
if(!empty($products))
{
foreach ($products as $key=>$product)
{
$nestedData['id'] = $product->id;
$nestedData['key'] = $key;
$product_image = explode(",", $product->image);
$product_image = htmlspecialchars($product_image[0]);
$nestedData['image'] = '<img src="'.url('public/images/product', $product_image).'" height="75" width="75">';
$total_qty = 0;
$product_variant_data = ProductVariant::where("product_id", $product->id)->get();
if(count($product_variant_data) > 0){
$nestedData['name'] = "";
$nestedData['code'] = "";
$nestedData['qty'] = "";
$nestedData['price'] = "";
$nestedData['subtotal'] = "";
foreach ($product_variant_data as $key => $product_variant) {
$variant_data = Variant::find($product_variant->variant_id);
$nestedData['name'] .= $variant_data->name . "<br>";
$nestedData['code'] .= $product_variant->item_code . "<br>";
$variant_qty = 0;
$lims_product_variant_warehouse_data = Product_Warehouse::where('product_id', $product->id)
->where('variant_id', $product_variant->variant_id)
->orderBy('warehouse_id')
->get();
if(count($lims_product_variant_warehouse_data) > 0){
foreach ($lims_product_variant_warehouse_data as $key => $product_variant_warehouse_data) {
$variant_qty += $product_variant_warehouse_data->qty;
}
}
$nestedData['qty'] .= number_format((float)$variant_qty, 0, '.', '.') . "<br>";
$total_qty = $variant_qty;
$purchaseHistories = ProductPurchase::where("product_id", $product->id)
->where("variant_id", $variant_data->id)
->get();
// Tính tổng qty
$totalQty = $purchaseHistories->sum('qty');
// Tính tổng total
$totalTotal = $purchaseHistories->sum('total');
// Tính trung bình giá 1 qty
$unitPrice = 0;
if($totalQty > 0){
$unitPrice = $totalTotal / $totalQty;
}
$nestedData['price'] .= number_format((float)($unitPrice), 0, '.', '.') . "<br>";
$nestedData['subtotal'] .= number_format((float)($unitPrice * $total_qty), 0, '.', '.') . "<br>";
$product->price = $unitPrice;
}
} else {
$nestedData['name'] = $product->name;
$nestedData['code'] = $product->code;
$nestedData['qty'] = number_format((float)$product->qty, 0, '.', '.');
$total_qty = $product->qty;
$nestedData['price'] = "";
$nestedData['subtotal'] = "";
$purchaseHistories = ProductPurchase::where("product_id", $product->id)->get();
// Tính tổng qty
$totalQty = $purchaseHistories->sum('qty');
// Tính tổng total
$totalTotal = $purchaseHistories->sum('total');
// Tính trung bình giá 1 qty
$unitPrice = 0;
if($totalQty > 0){
$unitPrice = $totalTotal / $totalQty;
}
$nestedData['price'] .= number_format((float)($unitPrice), 0, '.', '.') . "<br>";
$nestedData['subtotal'] .= number_format((float)($unitPrice * $total_qty), 0, '.', '.') . "<br>";
$product->price = $unitPrice;
}
if($product->brand_id)
$nestedData['brand'] = $product->brand->title;
else
$nestedData['brand'] = "N/A";
$nestedData['category'] = $product->category->name;
if($product->unit_id)
$nestedData['unit'] = $product->unit->unit_name;
else
$nestedData['unit'] = 'N/A';
$nestedData['options'] = '<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">'.trans("file.action").'
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu edit-options dropdown-menu-right dropdown-default" user="menu">
<li>
<button="type" class="btn btn-link view"><i class="fa fa-eye"></i> '.trans('file.View').'</button>
</li>';
if(in_array("products-edit", $request['all_permission']))
$nestedData['options'] .= '<li>
<a href="'.route('products.edit', $product->id).'" class="btn btn-link"><i class="fa fa-edit"></i> '.trans('file.edit').'</a>
</li>';
if(in_array("products-delete", $request['all_permission']))
$nestedData['options'] .= \Form::open(["route" => ["products.destroy", $product->id], "method" => "DELETE"] ).'
<li>
<button type="submit" class="btn btn-link" onclick="return confirmDelete()"><i class="fa fa-trash"></i> '.trans("file.delete").'</button>
</li>'.\Form::close().'
</ul>
</div>';
// data for product details by one click
if($product->tax_id)
$tax = Tax::find($product->tax_id)->name;
else
$tax = "N/A";
if($product->tax_method == 1)
$tax_method = trans('file.Exclusive');
else
$tax_method = trans('file.Inclusive');
$role = Role::find(Auth::user()->role_id);
$permission_view_price = $role->hasPermissionTo('view_price');
if(!$permission_view_price) {
$product->cost = 0;
$product->price = 0;
}
if(!isset($product->alert_quantity)){
$product->alert_quantity = 0;
}
$product->qty = 0;
$dataProductWarehouseMinQty = Product_Warehouse::select('warehouse_id', DB::raw('min(qty) as qty'))->where('product_id', $product->id)->groupBy('warehouse_id')->get();
foreach ($dataProductWarehouseMinQty as $key=>$item){
$product->qty += $item->qty;
}
$nestedData['product'] = array( '[ "'.$product->type.'"', ' "'.$product->name.'"', ' "'.$product->code.'"', ' "'.$nestedData['brand'].'"', ' "'.$nestedData['category'].'"', ' "'.$nestedData['unit'].'"', ' "'.number_format((float)$product->cost, 0, '.', '.').'"', ' "'.number_format((float)$product->price, 0, '.', '.').'"', ' "'.$tax.'"', ' "'.$tax_method.'"', ' "'.$product->alert_quantity.'"', ' "'.preg_replace('/\s+/S', " ", $product->product_details).'"', ' "'.$product->id.'"', ' "'.$product->product_list.'"', ' "'.$product->qty_list.'"', ' "'.$product->price_list.'"', ' "'.$product->qty.'"', ' "'.$product->image.'"]');
$data[] = $nestedData;
}
}
$json_data = array(
"draw" => intval($request->input('draw')),
"recordsTotal" => intval($totalData),
"recordsFiltered" => intval($totalFiltered),
"data" => $data
);
echo json_encode($json_data);
}
public function create()
{
$role = Role::firstOrCreate(['id' => Auth::user()->role_id]);
if ($role->hasPermissionTo('products-add')){
$lims_product_list = Product::where([ ['is_active', true], ['type', 'standard'] ])->get();
$lims_brand_list = Brand::where('is_active', true)->get();
$lims_category_list = Category::where('is_active', true)->get();
$lims_unit_list = Unit::where('is_active', true)->get();
$lims_tax_list = Tax::where('is_active', true)->get();
$lims_warehouse_list = Warehouse::where('is_active', true)->get();
return view('product.create',compact('lims_product_list', 'lims_brand_list', 'lims_category_list', 'lims_unit_list', 'lims_tax_list', 'lims_warehouse_list'));
}
else
return redirect()->back()->with('not_permitted', 'Sorry! You are not allowed to access this module');
}
public function store(Request $request)
{
$this->validate($request, [
'code' => [
'max:255',
Rule::unique('products')->where(function ($query) {
return $query->where('is_active', 1);
}),
],
]);
$data = $request->except('image', 'file');
$data['name'] = htmlspecialchars($data['name']);
if($data['type'] == 'combo'){
$data['product_list'] = implode(",", $data['product_id']);
$data['qty_list'] = implode(",", $data['product_qty']);
$data['price_list'] = implode(",", $data['unit_price']);
$data['cost'] = $data['unit_id'] = $data['purchase_unit_id'] = $data['sale_unit_id'] = 0;
}
elseif($data['type'] == 'digital')
$data['cost'] = $data['unit_id'] = $data['purchase_unit_id'] = $data['sale_unit_id'] = 0;
$data['product_details'] = str_replace('"', '@', $data['product_details']);
if($data['starting_date'])
$data['starting_date'] = date('Y-m-d', strtotime($data['starting_date']));
if($data['last_date'])
$data['last_date'] = date('Y-m-d', strtotime($data['last_date']));
$data['is_active'] = true;
$images = $request->image;
$image_names = [];
if($images) {
foreach ($images as $key => $image) {
$imageName = $image->getClientOriginalName();
$image->move('public/images/product', $imageName);
$image_names[] = $imageName;
}
$data['image'] = implode(",", $image_names);
}
else {
$data['image'] = 'zummXD2dvAtI.png';
}
$file = $request->file;
if ($file) {
$ext = pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
$fileName = strtotime(date('Y-m-d H:i:s'));
$fileName = $fileName . '.' . $ext;
$file->move('public/product/files', $fileName);
$data['file'] = $fileName;
}
$lims_product_data = Product::create($data);
//dealing with product variant
if(isset($data['is_variant'])) {
foreach ($data['variant_name'] as $key => $variant_name) {
$lims_variant_data = Variant::firstOrCreate(['name' => $data['variant_name'][$key]]);
$lims_variant_data->name = $data['variant_name'][$key];
$lims_variant_data->save();
$lims_product_variant_data = new ProductVariant;
$lims_product_variant_data->product_id = $lims_product_data->id;
$lims_product_variant_data->variant_id = $lims_variant_data->id;
$lims_product_variant_data->position = $key + 1;
$lims_product_variant_data->item_code = $data['item_code'][$key];
$lims_product_variant_data->additional_price = $data['additional_price'][$key];
$lims_product_variant_data->qty = 0;
$lims_product_variant_data->save();
}
}
if(isset($data['is_diffPrice'])) {
foreach ($data['diff_price'] as $key => $diff_price) {
if($diff_price) {
Product_Warehouse::create([
"product_id" => $lims_product_data->id,
"warehouse_id" => $data["warehouse_id"][$key],
"qty" => 0,
"price" => $diff_price
]);
}
}
}
\Session::flash('create_message', 'Product created successfully');
}
public function edit($id)
{
$role = Role::firstOrCreate(['id' => Auth::user()->role_id]);
if ($role->hasPermissionTo('products-edit')) {
$lims_product_list = Product::where([ ['is_active', true], ['type', 'standard'] ])->get();
$lims_brand_list = Brand::where('is_active', true)->get();
$lims_category_list = Category::where('is_active', true)->get();
$lims_unit_list = Unit::where('is_active', true)->get();
$lims_tax_list = Tax::where('is_active', true)->get();
$lims_product_data = Product::where('id', $id)->first();
$lims_product_variant_data = $lims_product_data->variant()->orderBy('position')->get();
$lims_warehouse_list = Warehouse::where('is_active', true)->get();
$role = Role::find(Auth::user()->role_id);
$permission_view_price = $role->hasPermissionTo('view_price');
if(!$permission_view_price) {
$lims_product_data->cost = 0;
$lims_product_data->price = 0;
}
return view('product.edit',compact('lims_product_list', 'lims_brand_list', 'lims_category_list', 'lims_unit_list', 'lims_tax_list', 'lims_product_data', 'lims_product_variant_data', 'lims_warehouse_list'));
}
else
return redirect()->back()->with('not_permitted', 'Sorry! You are not allowed to access this module');
}
public function updateProduct(Request $request)
{
$this->validate($request, [
'name' => [
'max:255',
Rule::unique('products')->ignore($request->input('id'))->where(function ($query) {
return $query->where('is_active', 1);
}),
],
'code' => [
'max:255',
Rule::unique('products')->ignore($request->input('id'))->where(function ($query) {
return $query->where('is_active', 1);
}),
]
]);
$lims_product_data = Product::findOrFail($request->input('id'));
$data = $request->except('image', 'file', 'prev_img');
$data['name'] = htmlspecialchars($data['name']);
if($data['type'] == 'combo') {
$data['product_list'] = implode(",", $data['product_id']);
$data['qty_list'] = implode(",", $data['product_qty']);
$data['price_list'] = implode(",", $data['unit_price']);
$data['cost'] = $data['unit_id'] = $data['purchase_unit_id'] = $data['sale_unit_id'] = 0;
}
elseif($data['type'] == 'digital')
$data['cost'] = $data['unit_id'] = $data['purchase_unit_id'] = $data['sale_unit_id'] = 0;
if(!isset($data['featured']))
$data['featured'] = 0;
if(!isset($data['promotion']))
$data['promotion'] = null;
$data['product_details'] = str_replace('"', '@', $data['product_details']);
$data['product_details'] = $data['product_details'];
if($data['starting_date'])
$data['starting_date'] = date('Y-m-d', strtotime($data['starting_date']));
if($data['last_date'])
$data['last_date'] = date('Y-m-d', strtotime($data['last_date']));
//dealing with previous images
if($request->prev_img) {
$lims_product_data->image = implode(",", $request->prev_img);
$lims_product_data->save();
}
//dealing with new images
$images = $request->image;
$image_names = [];
if($images) {
foreach ($images as $key => $image) {
$imageName = $image->getClientOriginalName();
$image->move('public/images/product', $imageName);
$image_names[] = $imageName;
}
if($lims_product_data->image != 'zummXD2dvAtI.png') {
$data['image'] = $lims_product_data->image.','.implode(",", $image_names);
}
else{
$data['image'] = implode(",", $image_names);
}
}
else {
$data['image'] = $lims_product_data->image;
}
if(empty($images) && empty($request->prev_img)){
$data['image'] = 'zummXD2dvAtI.png';
}
$file = $request->file;
if ($file) {
$ext = pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
$fileName = strtotime(date('Y-m-d H:i:s'));
$fileName = $fileName . '.' . $ext;
$file->move('public/product/files', $fileName);
$data['file'] = $fileName;
}
$lims_product_variant_data = ProductVariant::where('product_id', $request->input('id'))->select('id', 'variant_id')->get();
foreach ($lims_product_variant_data as $key => $value) {
if (!in_array($value->variant_id, $data['variant_id'])) {
ProductVariant::find($value->id)->delete();
}
}
//dealing with product variant
if(isset($data['is_variant'])) {
foreach ($data['variant_name'] as $key => $variant_name) {
if($data['product_variant_id'][$key] == 0) {
$lims_variant_data = Variant::firstOrCreate(['name' => $data['variant_name'][$key]]);
$lims_product_variant_data = new ProductVariant();
$lims_product_variant_data->product_id = $lims_product_data->id;
$lims_product_variant_data->variant_id = $lims_variant_data->id;
$lims_product_variant_data->position = $key + 1;
$lims_product_variant_data->item_code = $data['item_code'][$key];
$lims_product_variant_data->additional_price = $data['additional_price'][$key];
$lims_product_variant_data->qty = 0;
$lims_product_variant_data->save();
}
else {
Variant::find($data['variant_id'][$key])->update(['name' => $variant_name]);
ProductVariant::find($data['product_variant_id'][$key])->update([
'position' => $key+1,
'item_code' => $data['item_code'][$key],
'additional_price' => $data['additional_price'][$key]
]);
}
}
} else {
//Case uncheck is_variant
$data['is_variant'] = null;
foreach ($lims_product_variant_data as $key => $value) {
ProductVariant::find($value->id)->delete();
}
}
if(isset($data['is_diffPrice'])) {
foreach ($data['diff_price'] as $key => $diff_price) {
if($diff_price) {
$lims_product_warehouse_data = Product_Warehouse::FindProductWithoutVariant($lims_product_data->id, $data['warehouse_id'][$key])->first();
if($lims_product_warehouse_data) {
$lims_product_warehouse_data->price = $diff_price;
$lims_product_warehouse_data->save();
}
else {
Product_Warehouse::create([
"product_id" => $lims_product_data->id,
"warehouse_id" => $data["warehouse_id"][$key],
"qty" => 0,
"price" => $diff_price
]);
}
}
}
}
else {
$data['is_diffPrice'] = false;
foreach ($data['warehouse_id'] as $key => $warehouse_id) {
$lims_product_warehouse_data = Product_Warehouse::FindProductWithoutVariant($lims_product_data->id, $warehouse_id)->first();
if($lims_product_warehouse_data) {
$lims_product_warehouse_data->price = null;
$lims_product_warehouse_data->save();
}
}
}
$lims_product_data->update($data);
\Session::flash('edit_message', 'Product updated successfully');
}
public function generateCode()
{
$id = Keygen::numeric(8)->generate();
return $id;
}
public function search(Request $request)
{
$product_code = explode(" ", $request['data']);
$lims_product_data = Product::where('code', $product_code[0])->first();
$product[] = $lims_product_data->name;
$product[] = $lims_product_data->code;
$product[] = $lims_product_data->qty;
$product[] = $lims_product_data->price;
$product[] = $lims_product_data->id;
return $product;
}
public function saleUnit($id)
{
$unit = Unit::where("base_unit", $id)->orWhere('id', $id)->pluck('unit_name','id');
return json_encode($unit);
}
public function getData($id)
{
$data = Product::select('name', 'code')->where('id', $id)->get();
return $data[0];
}
public function getHistory($id)
{
$date_format = GeneralSetting::select('date_format')->latest()->first();
$purchaseHistories = ProductPurchase::select( 'product_purchases.*', 'purchases.reference_no', 'purchases.inventory_receiving_voucher', 'purchases.created_at as product_purchases.created_at')
->leftJoin('purchases', 'purchases.id', '=', 'product_purchases.purchase_id')
->where("product_purchases.product_id", $id)
->orderBy('product_purchases.created_at', 'asc')
->get();
$data = array();
if(!empty($purchaseHistories))
{
foreach ($purchaseHistories as $key=>$product)
{
$nestedData['key'] = $key + 1;
$nestedData['name'] = $product->reference_no;
$nestedData['qty'] = number_format((float)$product->qty, 0, '.', '.');
$nestedData['recieved'] = number_format((float)$product->recieved, 0, '.', '.');
$nestedData['tax'] = number_format((float)$product->tax, 0, '.', '.');
$nestedData['tax_rate'] = number_format((float)$product->tax_rate, 0, '.', '.');
$nestedData['created_at'] = date(config('date_format'), strtotime($product->created_at->toDateString()));
$nestedData['inventory_receiving_voucher'] = $product->inventory_receiving_voucher ?? "";
$nestedData['discount'] = number_format((float)$product->discount, 0, '.', '.');
$nestedData['total'] = number_format((float)$product->total, 0, '.', '.');
$data[] = $nestedData;
}
}
$json_data = array(
"data" => $data
);
echo json_encode($json_data);
}
public function getDataFromQRCode($code, $seri_batch = 0)
{
//Params must be product_code, not use p_id
$QRCodeData = array();
$QRCodeData['info'] = array();
$QRCodeData['warehouse'] = array();
$QRCodeData['purchases'] = array();
$date_format = GeneralSetting::select('date_format')->latest()->first();
$data = Product::select('products.*')
->with('category', 'brand', 'unit')
->join('categories', 'products.category_id', '=', 'categories.id')
->leftjoin('brands', 'products.brand_id', '=', 'brands.id')
->where('products.code', $code)->first();
if(empty($data->id)) {
return ['error' => "No product"];
}
$productWarehouse = $this->productWarehouseData($data->id);
$productPurchases = $this->productPurchaseData($data->id, $seri_batch);
$user = Auth::user();
if(isset($user)){
$role = Role::find($user->role_id);
}
if(isset($role) && $role->hasPermissionTo('products-index')){
$permissions = Role::findByName($role->name)->permissions;
foreach ($permissions as $permission)
$all_permission[] = $permission->name;
if(empty($all_permission))
$all_permission[] = 'dummy text';
}
$QRCodeData['warehouse'] = $productWarehouse;
$QRCodeData['purchases'] = $productPurchases;
$QRCodeData['info'] = $data;
if((isset($role) && !$role->hasPermissionTo('products-index')) || !isset($role) ){
$QRCodeData["info"]->brand->title = null;
$QRCodeData["info"]->qty = null;
$QRCodeData['warehouse'] = [];
}
return view('product.scan_qrcode_data', compact('QRCodeData','date_format'));
}
public function productPurchaseData ($id, $seri_batch) {
$productPurchases = Purchase::select('purchases.*', 'product_purchases.*')
->with('supplier', 'warehouse')
->leftJoin('suppliers', 'purchases.supplier_id', '=', 'suppliers.id')
->leftJoin('product_purchases', 'purchases.id', '=', 'product_purchases.purchase_id')
->where('product_purchases.product_id', $id)
->where(function($query) use ($seri_batch) {
$query->orWhere('product_purchases.serial_number', 'LIKE', trim($seri_batch))
->orWhere('product_purchases.batch_number', 'LIKE', trim($seri_batch));
})
->orderBy('product_purchases.created_at', 'DESC')
->get();
return ['product_purchases' => $productPurchases];
}
public function productWarehouseData($id)
{
$warehouse = [];
$qty = [];
$warehouse_name = [];
$variant_name = [];
$variant_qty = [];
$product_warehouse = [];
$product_variant_warehouse = [];
$lims_product_data = Product::select('id', 'is_variant')->find($id);
if($lims_product_data->is_variant) {
$lims_product_variant_warehouse_data = Product_Warehouse::where('product_id', $lims_product_data->id)->orderBy('warehouse_id')->get();
$lims_product_warehouse_data = Product_Warehouse::select('warehouse_id', DB::raw('min(qty) as qty'))->where('product_id', $id)->groupBy('warehouse_id')->get();
foreach ($lims_product_variant_warehouse_data as $key => $product_variant_warehouse_data) {
$lims_warehouse_data = Warehouse::find($product_variant_warehouse_data->warehouse_id);
$lims_variant_data = Variant::find($product_variant_warehouse_data->variant_id);
if(!empty($lims_warehouse_data)) {
$warehouse_name[] = $lims_warehouse_data->name;
} else {
$warehouse_name[] = '-';
}
if(!empty($lims_variant_data)) {
$variant_name[] = $lims_variant_data->name;
} else {
$variant_name[] = "-";
}
$variant_qty[] = $product_variant_warehouse_data->qty;
}
}
else{
$lims_product_warehouse_data = Product_Warehouse::where('product_id', $id)->get();
}
foreach ($lims_product_warehouse_data as $key => $product_warehouse_data) {
$lims_warehouse_data = Warehouse::find($product_warehouse_data->warehouse_id);
$warehouse[] = $lims_warehouse_data->name;
$qty[] = $product_warehouse_data->qty;
}
$product_warehouse = [$warehouse, $qty];
$product_variant_warehouse = [$warehouse_name, $variant_name, $variant_qty];
return ['product_warehouse' => $product_warehouse, 'product_variant_warehouse' => $product_variant_warehouse];
}
public function printBarcode()
{
$lims_product_list = Product::where('is_active', true)->get();
return view('product.print_barcode', compact('lims_product_list'));
}
public function printQrcode()
{
$lims_product_list = Product::where('is_active', true)->get();
return view('product.print_qrcode', compact('lims_product_list'));
}
public function limsProductSearch(Request $request)
{
$todayDate = date('Y-m-d');
$product_code = explode(" ", $request['data']);
$lims_product_data = Product::where('code', $product_code[0])->first();
//$product[] = $lims_product_data->id;
$product[] = $lims_product_data->name;
$product[] = $lims_product_data->code;
$product[] = $lims_product_data->price;
$product[] = DNS1D::getBarcodePNG($lims_product_data->code, $lims_product_data->barcode_symbology);
$product[] = $lims_product_data->promotion_price;
$product[] = config('currency');
$product[] = config('currency_position');
$purchases = Purchase::select('purchases.*', 'product_purchases.*')
->with('supplier', 'warehouse')
->leftJoin('suppliers', 'purchases.supplier_id', '=', 'suppliers.id')
->leftJoin('product_purchases', 'purchases.id', '=', 'product_purchases.purchase_id')
->where('product_purchases.product_id', $lims_product_data->id)
->get();
$product[] = $purchases;
return $product;
}
public function importProduct(Request $request)
{
//get file
$upload=$request->file('file');
$ext = pathinfo($upload->getClientOriginalName(), PATHINFO_EXTENSION);
if($ext != 'csv')
return redirect()->back()->with('message', 'Please upload a CSV file');
$filePath=$upload->getRealPath();
//open and read
$file=fopen($filePath, 'r');
$header= fgetcsv($file);
$escapedHeader=[];
//validate
foreach ($header as $key => $value) {
$lheader=strtolower($value);
$escapedItem=preg_replace('/[^a-z]/', '', $lheader);
array_push($escapedHeader, $escapedItem);
}
//looping through other columns
while($columns=fgetcsv($file))
{
foreach ($columns as $key => $value) {
$value=preg_replace('/\D/','',$value);
}
$data= array_combine($escapedHeader, $columns);
if($data['brand'] != 'N/A' && $data['brand'] != ''){
$lims_brand_data = Brand::firstOrCreate(['title' => $data['brand'], 'is_active' => true]);
$brand_id = $lims_brand_data->id;
}
else
$brand_id = null;
$lims_category_data = Category::firstOrCreate(['name' => $data['category'], 'is_active' => true]);
$lims_unit_data = Unit::where('unit_code', $data['unitcode'])->first();
if(!$lims_unit_data)
return redirect()->back()->with('not_permitted', 'Unit code does not exist in the database.');
$product = Product::firstOrNew([ 'name'=>$data['name'], 'is_active'=>true ]);
if($data['image'])
$product->image = $data['image'];
else
$product->image = 'zummXD2dvAtI.png';
$product->name = $data['name'];
$product->code = $data['code'];
$product->type = strtolower($data['type']);
$product->barcode_symbology = 'C128';
$product->brand_id = $brand_id;
$product->category_id = $lims_category_data->id;
$product->unit_id = $lims_unit_data->id;
$product->purchase_unit_id = $lims_unit_data->id;
$product->sale_unit_id = $lims_unit_data->id;
$product->cost = $data['cost'];
$product->price = $data['price'];
$product->tax_method = 1;
$product->qty = 0;
$product->product_details = $data['productdetails'];
$product->is_active = true;
$product->save();
if($data['variantname']) {
//dealing with variants
$variant_names = explode(",", $data['variantname']);
$item_codes = explode(",", $data['itemcode']);
$additional_prices = explode(",", $data['additionalprice']);
foreach ($variant_names as $key => $variant_name) {
$variant = Variant::firstOrCreate(['name' => $variant_name]);
if($data['itemcode'])
$item_code = $item_codes[$key];
else
$item_code = $variant_name . '-' . $data['code'];
if($data['additionalprice'])
$additional_price = $additional_prices[$key];
else
$additional_price = 0;
ProductVariant::create([
'product_id' => $product->id,
'variant_id' => $variant->id,
'position' => $key + 1,
'item_code' => $item_code,
'additional_price' => $additional_price,
'qty' => 0
]);
}
$product->is_variant = true;
$product->save();
}
}
return redirect('products')->with('import_message', 'Product imported successfully');
}
public function deleteBySelection(Request $request)
{
$product_id = $request['productIdArray'];
foreach ($product_id as $id) {
$lims_product_data = Product::findOrFail($id);
$lims_product_data->is_active = false;
$lims_product_data->save();
}
return 'Product deleted successfully!';
}
public function destroy($id)
{
$lims_product_data = Product::findOrFail($id);
$lims_product_data->is_active = false;
/*if($lims_product_data->image != 'zummXD2dvAtI.png') {
$images = explode(",", $lims_product_data->image);
foreach ($images as $key => $image) {
unlink('public/images/product/'.$image);
}
}*/
$lims_product_data->save();
return redirect('products')->with('message', 'Product deleted successfully');
}
}