ultimatepos/app/Services/Api/ProductService.php

56 lines
1.7 KiB
PHP

<?php
namespace App\Services\Api;
use App\Product;
use Illuminate\Http\Request;
class ProductService
{
public function list(Request $request, int $business_id)
{
$query = Product::where('business_id', $business_id)
->with(['brand', 'unit', 'category', 'sub_category', 'product_variations.variations']);
if ($request->filled('search')) {
$search = $request->input('search');
$query->where(function ($q) use ($search) {
$q->where('name', 'like', "%{$search}%")
->orWhere('sku', 'like', "%{$search}%");
});
}
if ($request->filled('type')) {
$query->where('type', $request->input('type'));
}
return $query->orderBy('name')->paginate($request->input('per_page', 25));
}
public function find(int $business_id, int $id): Product
{
return Product::where('business_id', $business_id)
->with(['brand', 'unit', 'category', 'sub_category', 'product_variations.variations'])
->findOrFail($id);
}
public function searchForPos(Request $request, int $business_id)
{
$term = $request->input('term', '');
$location_id = $request->input('location_id');
$query = Product::where('business_id', $business_id)
->where('is_inactive', 0)
->with(['product_variations.variations']);
if (! empty($term)) {
$query->where(function ($q) use ($term) {
$q->where('name', 'like', "%{$term}%")
->orWhere('sku', 'like', "%{$term}%");
});
}
return $query->limit(20)->get();
}
}