dstpabuaran.com/app/Http/Controllers/Admin/Manage/TransactionController.php
Yoga Pangestu b85bbb1ba4 feat: enhance purchase and restock management with variant support
- Added existing_material_ids to PurchaseForEdit and RestockForEdit types.
- Introduced RawMaterialVariant and ProductVariantForRestock types for better variant handling.
- Updated PurchaseCreate and PurchaseEdit components to fetch and display raw material variants.
- Enhanced RestockCreate and RestockEdit components to manage product variants dynamically.
- Modified TransactionCreate and TransactionEdit components to support product variants.
- Added routes for fetching active products and raw materials.
2026-08-20 10:48:24 +07:00

146 lines
5.4 KiB
PHP

<?php
namespace App\Http\Controllers\Admin\Manage;
use App\Enums\OrderChannel;
use App\Enums\PaymentType;
use App\Enums\PriceType;
use App\Enums\Role;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Manage\TransactionRequest;
use App\Http\Requests\PaginatedRequest;
use App\Models\Order;
use App\Models\User;
use App\Services\Admin\Manage\TransactionService;
use App\Services\Admin\Master\CustomerService;
use App\Services\Admin\Master\Product\ProductService;
use App\Services\Admin\Master\Product\ProductVariantService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;
class TransactionController extends Controller
{
public function __construct(
private TransactionService $service,
private ProductService $productService,
private ProductVariantService $productVariantService,
private CustomerService $customerService,
) {}
public function index(PaginatedRequest $request): Response
{
$filters = $request->only(['status', 'channel', 'payment_type', 'customer_id', 'marketing_id', 'created_by_id', 'date_from', 'date_to']);
$user = $request->user();
return Inertia::render('admin/manage/transaction/index', [
'transactions' => $this->service->paginated(
...$request->validatedWithDefaults(),
filters: $filters,
user: $user,
),
'filters' => $filters,
'filterOptions' => $this->service->getFilterOptions(),
'highlight' => $request->input('highlight'),
]);
}
public function create(): Response
{
$user = auth()->user();
$isCashier = $user->hasRole(Role::CASHIER);
return Inertia::render('admin/manage/transaction/create', [
'products' => $this->productService->getActiveProducts(),
'customers' => $this->customerService->getAll(),
'employees' => $this->getEmployees(),
'channelOptions' => OrderChannel::toSelect(),
'paymentTypeOptions' => PaymentType::toSelect(),
'priceTypeOptions' => PriceType::toSelect()
->filter(fn ($p) => ! in_array($p['value'], [PriceType::CAPITAL->value, PriceType::REJECT_CAPITAL->value]))
->when($isCashier, fn ($q) => $q->filter(fn ($p) => in_array($p['value'], [PriceType::RETAIL->value, PriceType::REJECT_SELLING->value])))
->when(! $isCashier, fn ($q) => $q->filter(fn ($p) => $p['value'] !== PriceType::RETAIL->value))
->values(),
]);
}
public function store(TransactionRequest $request): RedirectResponse
{
return $this->handleAction(
fn () => $this->service->store($request->validated()),
'Transaksi berhasil ditambahkan.',
'admin.manage.transactions.index',
'admin.manage.transactions.create'
);
}
public function edit(Order $transaction): Response
{
$user = auth()->user();
$isCashier = $user->hasRole(Role::CASHIER);
return Inertia::render('admin/manage/transaction/edit', [
'transaction' => $this->service->getForEdit($transaction),
'products' => $this->productService->getActiveProducts(),
'customers' => $this->customerService->getAll(),
'employees' => $this->getEmployees(),
'channelOptions' => OrderChannel::toSelect(),
'paymentTypeOptions' => PaymentType::toSelect(),
'priceTypeOptions' => PriceType::toSelect()
->filter(fn ($p) => ! in_array($p['value'], [PriceType::CAPITAL->value, PriceType::REJECT_CAPITAL->value]))
->when($isCashier, fn ($q) => $q->filter(fn ($p) => in_array($p['value'], [PriceType::RETAIL->value, PriceType::REJECT_SELLING->value])))
->when(! $isCashier, fn ($q) => $q->filter(fn ($p) => $p['value'] !== PriceType::RETAIL->value))
->values(),
]);
}
public function update(TransactionRequest $request, Order $transaction): RedirectResponse
{
return $this->handleAction(
fn () => $this->service->update($transaction, $request->validated()),
'Transaksi berhasil diperbarui.',
'admin.manage.transactions.index',
'admin.manage.transactions.edit',
['transaction' => $transaction]
);
}
public function destroy(Order $transaction): RedirectResponse
{
return $this->handleAction(
fn () => $this->service->destroy($transaction),
'Transaksi berhasil dihapus.',
'admin.manage.transactions.index'
);
}
public function updateStatus(Order $transaction): RedirectResponse
{
return $this->handleAction(
fn () => $this->service->updateStatus($transaction, request('status')),
'Status transaksi berhasil diperbarui.',
'admin.manage.transactions.index'
);
}
public function items(Order $transaction): JsonResponse
{
return response()->json([
'items' => $this->service->getItems($transaction),
]);
}
private function getEmployees()
{
return User::query()
->select('id')
->active()
->with('userProfile:id,user_id,full_name')
->orderBy('id')
->get()
->filter(fn (User $user) => $user->userProfile?->full_name)
->values();
}
}