80 lines
2.3 KiB
PHP
80 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Models\CollectionType;
|
|
use App\Models\ExpenseCategory;
|
|
use App\Models\ProductCategory;
|
|
use App\Models\PaymentMethod;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class MasterController extends Controller
|
|
{
|
|
private function getModel($type)
|
|
{
|
|
switch ($type) {
|
|
case 'collection': return new CollectionType();
|
|
case 'expense': return new ExpenseCategory();
|
|
case 'product': return new ProductCategory();
|
|
case 'payment_method': return new PaymentMethod();
|
|
default: return null;
|
|
}
|
|
}
|
|
|
|
public function index($type)
|
|
{
|
|
$model = $this->getModel($type);
|
|
if (!$model) return response()->json(['message' => 'Invalid master type'], 400);
|
|
return response()->json($model::all());
|
|
}
|
|
|
|
public function store(Request $request, $type)
|
|
{
|
|
$model = $this->getModel($type);
|
|
if (!$model) return response()->json(['message' => 'Invalid master type'], 400);
|
|
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'status' => 'required|string|in:Active,Inactive'
|
|
]);
|
|
|
|
$item = $model::create($validated);
|
|
return response()->json($item, 201);
|
|
}
|
|
|
|
public function update(Request $request, $type, $id)
|
|
{
|
|
$model = $this->getModel($type);
|
|
if (!$model) return response()->json(['message' => 'Invalid master type'], 400);
|
|
|
|
$item = $model::findOrFail($id);
|
|
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'status' => 'required|string|in:Active,Inactive'
|
|
]);
|
|
|
|
$item->update($validated);
|
|
return response()->json($item);
|
|
}
|
|
|
|
public function destroy($type, $id)
|
|
{
|
|
$model = $this->getModel($type);
|
|
if (!$model) return response()->json(['message' => 'Invalid master type'], 400);
|
|
|
|
$item = $model::findOrFail($id);
|
|
|
|
if ($type === 'collection' && (
|
|
strtolower($item->name) === 'product sale' ||
|
|
strtolower($item->name) === 'product saled'
|
|
)) {
|
|
return response()->json(['message' => 'System required item cannot be deleted'], 422);
|
|
}
|
|
|
|
$item->delete();
|
|
return response()->json(['message' => 'Deleted successfully']);
|
|
}
|
|
}
|