206 lines
6.5 KiB
PHP
206 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\OriginCountry;
|
|
use App\Models\StockEntry;
|
|
use App\Models\StockPosition;
|
|
use App\Models\PhysicalItem;
|
|
use App\Models\Supplier;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class StockEntryController extends Controller
|
|
{
|
|
/**
|
|
* Display a paginated listing of stock entries.
|
|
*
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$query = StockEntry::query()
|
|
->with(['physicalItem', 'supplier', 'stockPosition', 'stockBatch', 'physicalItem.manufacturer', 'countryOfOrigin']);
|
|
|
|
// Apply filters if provided
|
|
if ($request->has('search')) {
|
|
$search = $request->search;
|
|
$query->whereHas('physicalItem', function($q) use ($search) {
|
|
$q->where('name', 'like', "%{$search}%");
|
|
});
|
|
}
|
|
|
|
// Sort
|
|
$sortField = $request->input('sort_field', 'updated_at');
|
|
$sortDirection = $request->input('sort_direction', 'desc');
|
|
$query->orderBy($sortField, $sortDirection);
|
|
|
|
// Paginate
|
|
$perPage = $request->input('per_page', 10);
|
|
$page = $request->input('page', 1);
|
|
|
|
$entries = $query->paginate($perPage, ['*'], 'page', $page);
|
|
|
|
return response()->json([
|
|
'data' => $entries->items(),
|
|
'meta' => [
|
|
'total' => $entries->total(),
|
|
'per_page' => $entries->perPage(),
|
|
'current_page' => $entries->currentPage(),
|
|
'last_page' => $entries->lastPage(),
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created stock entry.
|
|
*
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function addData(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'physical_item_id' => 'required|integer|exists:vat_warehouse.physical_item,id',
|
|
'supplier_id' => 'required|integer|exists:vat_warehouse.supplier,id',
|
|
'count' => 'required|integer|min:0',
|
|
'price' => 'nullable|numeric|min:0',
|
|
'bought' => 'nullable|date',
|
|
'description' => 'nullable|string',
|
|
'note' => 'nullable|string',
|
|
'stock_position_id' => 'required|integer|exists:stock_positions,id',
|
|
'country_of_origin_id' => 'required|integer|exists:vat_warehouse.country_of_origin,id',
|
|
'on_the_way' => 'boolean',
|
|
'stock_batch_id' => 'nullable|integer|exists:stock_batch,id',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json(['errors' => $validator->errors()], 422);
|
|
}
|
|
|
|
$entry = StockEntry::create($request->all() + [
|
|
'created_by' => 1,
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'Stock entry created successfully',
|
|
'data' => $entry->load(['physicalItem', 'supplier', 'stockPosition', 'stockBatch']),
|
|
], 201);
|
|
}
|
|
|
|
/**
|
|
* Display the specified stock entry.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function show($id)
|
|
{
|
|
$entry = StockEntry::with(['physicalItem', 'supplier', 'stockPosition', 'stockBatch'])
|
|
->findOrFail($id);
|
|
|
|
return response()->json(['data' => $entry]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified stock entry.
|
|
*
|
|
* @param Request $request
|
|
* @param int $id
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function updateData(Request $request, $id)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'physical_item_id' => 'integer|exists:vat_warehouse.physical_item,id',
|
|
'supplier_id' => 'integer|exists:vat_warehouse.supplier,id',
|
|
'count' => 'integer|min:0',
|
|
'price' => 'nullable|numeric|min:0',
|
|
'bought' => 'nullable|date',
|
|
'description' => 'nullable|string',
|
|
'note' => 'nullable|string',
|
|
'stock_position_id' => 'integer|exists:stock_positions,id',
|
|
'country_of_origin_id' => 'integer|exists:vat_warehouse.country_of_origin,id',
|
|
'on_the_way' => 'boolean',
|
|
'stock_batch_id' => 'nullable|integer|exists:stock_batch,id',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json(['errors' => $validator->errors()], 422);
|
|
}
|
|
|
|
$entry = StockEntry::findOrFail($id);
|
|
$entry->update($request->all() + [
|
|
'updated_by' => 1,
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'Stock entry updated successfully',
|
|
'data' => $entry->fresh(['physicalItem', 'supplier', 'stockPosition', 'stockBatch']),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Remove the specified stock entry.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$entry = StockEntry::findOrFail($id);
|
|
$entry->delete();
|
|
|
|
return response()->json([
|
|
'message' => 'Stock entry deleted successfully'
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get options for dropdown lists.
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function getOptions()
|
|
{
|
|
$stockPositions = StockPosition::select('id', 'line', 'rack', 'shelf', 'position')->get()
|
|
->map(function($position) {
|
|
return [
|
|
'id' => $position->id,
|
|
'name' => $position->line . '-' . $position->rack . '-' . $position->shelf . '-' . $position->position
|
|
];
|
|
});
|
|
|
|
// Get suppliers from warehouse DB
|
|
$suppliers = Supplier::select('id', 'name')->get();
|
|
|
|
// Get physical items from warehouse DB
|
|
$countriesOrigin = OriginCountry::select('id', 'code as name')->get();
|
|
|
|
return response()->json([
|
|
'stockPositions' => $stockPositions,
|
|
'suppliers' => $suppliers,
|
|
'countriesOrigin' => $countriesOrigin,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get options for dropdown lists.
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function getItems(Request $request)
|
|
{
|
|
// Get physical items from warehouse DB
|
|
$physicalItems = PhysicalItem::select('id', 'name')
|
|
->where('name', 'like', '%' . $request->input('item_name', '') . '%')
|
|
->get();
|
|
|
|
return response()->json([
|
|
'physicalItems' => $physicalItems,
|
|
]);
|
|
}
|
|
}
|