207 lines
7.8 KiB
PHP
Executable File
207 lines
7.8 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers\Admin\Manage;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Admin\Manage\TransactionRequest;
|
|
use App\Models\Customer;
|
|
use App\Models\Service;
|
|
use App\Models\Transaction;
|
|
use App\Models\User;
|
|
use App\Traits\DeleteAttachment;
|
|
use App\Traits\UploadAttachment;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class TransactionController extends Controller
|
|
{
|
|
use DeleteAttachment, UploadAttachment;
|
|
|
|
public function index(Request $request): View|JsonResponse
|
|
{
|
|
if ($request->ajax()) {
|
|
$transactions = Transaction::with(['user', 'user.biography', 'cashier', 'cashier.biography', 'customer', 'service', 'barbershop'])
|
|
->barbershop()
|
|
->when(auth()->user()->role_id === 5, function ($query) {
|
|
return $query->where('user_id', auth()->id());
|
|
});
|
|
|
|
return DataTables::eloquent($transactions)
|
|
->addIndexColumn()
|
|
->editColumn('created_at', function ($item) {
|
|
return formatDateIndo($item->created_at);
|
|
})
|
|
->editColumn('payment_method', function ($item) {
|
|
switch ($item->payment_method) {
|
|
case '1':
|
|
return '<span class="badge badge-primary">Tunai</span>';
|
|
case '2':
|
|
return '<span class="badge badge-info">Qris</span>';
|
|
case '2':
|
|
return '<span class="badge badge-success">Lainnya</span>';
|
|
|
|
default:
|
|
return '<span class="badge badge-danger">Tidak Diketahui</span>';
|
|
}
|
|
})
|
|
->addColumn('image', function ($item) {
|
|
if ($item->attachment && $item->attachment->formatted_attachment) {
|
|
$imageUrl = Storage::url("transactions/{$item->attachment->formatted_attachment}");
|
|
|
|
return '<a href="'.$imageUrl.'" ><img src="'.$imageUrl.'" alt="Check-in Image" height="77" width="77"></a>';
|
|
}
|
|
|
|
return '-';
|
|
})
|
|
->addColumn('actions', function ($item) {
|
|
return view('partials.admin._actions', ['item' => $item]);
|
|
})
|
|
->rawColumns(['payment_method', 'image', 'actions'])
|
|
->make();
|
|
}
|
|
|
|
return view('pages.admin.manage.transaction.index', [
|
|
'pageTitle' => 'Transaksi',
|
|
]);
|
|
}
|
|
|
|
public function create(): View
|
|
{
|
|
return view('pages.admin.manage.transaction.create', [
|
|
'pageTitle' => 'Tambah Transaksi',
|
|
'services' => Service::query()->barbershop()->latest()->get(),
|
|
'customers' => Customer::query()->barbershop()->latest()->get(),
|
|
'barber' => User::with('biography')->barbershop()->barber()->latest()->get(),
|
|
]);
|
|
}
|
|
|
|
public function store(TransactionRequest $request): View
|
|
{
|
|
$validatedData = $request->validated();
|
|
$findCustomer = Customer::find($validatedData['customer']);
|
|
|
|
$newTransaction = null;
|
|
|
|
DB::transaction(function () use ($validatedData, $findCustomer, &$newTransaction) {
|
|
$customer = null;
|
|
$userId = Auth::id();
|
|
$barbershopId = Auth::user()->barbershop_id;
|
|
|
|
if (! $findCustomer) {
|
|
$customer = Customer::create([
|
|
'user_id' => $userId,
|
|
'barbershop_id' => $barbershopId,
|
|
'name' => $validatedData['customer'],
|
|
]);
|
|
}
|
|
$newTransaction = Transaction::create(array_merge($validatedData, [
|
|
'user_id' => $validatedData['barber'],
|
|
'cashier_id' => $userId,
|
|
'barbershop_id' => $barbershopId,
|
|
'service_id' => $validatedData['service'],
|
|
'customer_id' => $customer ? $customer->id : $validatedData['customer'],
|
|
]));
|
|
|
|
if (isset($validatedData['image'])) {
|
|
$this->uploadAttachment($validatedData['image'], 'transactions', Transaction::class, $newTransaction->id, 'transaction');
|
|
}
|
|
});
|
|
|
|
if (! $newTransaction) {
|
|
notify()->error('Transaksi tidak dapat dibuat, silakan coba lagi', 'Kesalahan');
|
|
|
|
return redirect()->back()->withInput();
|
|
}
|
|
|
|
$transactionData = $newTransaction->load(['user', 'user.biography', 'cashier', 'cashier.biography', 'customer', 'service', 'barbershop']);
|
|
|
|
notify()->success('Data berhasil ditambahkan', 'Berhasil');
|
|
|
|
return view('pages.admin.manage.transaction.print', [
|
|
'transaction' => $transactionData,
|
|
'url' => route('manage.transactions'),
|
|
'showingBarberName' => $validatedData['showing_barber_name'],
|
|
'showingCashierName' => $validatedData['showing_cashier_name'],
|
|
]);
|
|
}
|
|
|
|
public function edit(Transaction $transaction): View
|
|
{
|
|
return view('pages.admin.manage.transaction.edit', [
|
|
'pageTitle' => 'Edit Transaksi',
|
|
'services' => Service::query()->barbershop()->latest()->get(),
|
|
'customers' => Customer::query()->barbershop()->latest()->get(),
|
|
'barber' => User::with('biography')->barbershop()->barber()->latest()->get(),
|
|
'transaction' => $transaction,
|
|
]);
|
|
}
|
|
|
|
public function update(Transaction $transaction, TransactionRequest $request): RedirectResponse
|
|
{
|
|
$validatedData = $request->validated();
|
|
|
|
DB::transaction(function () use ($validatedData, $transaction) {
|
|
$transaction->update(array_merge($validatedData, [
|
|
'user_id' => $validatedData['barber'],
|
|
'service_id' => $validatedData['service'],
|
|
'customer_id' => $validatedData['customer'],
|
|
]));
|
|
if (isset($validatedData['image'])) {
|
|
$this->uploadAttachment($validatedData['image'], 'transactions', Transaction::class, $transaction->id, 'transaction');
|
|
}
|
|
});
|
|
|
|
notify()->success('Data berhasil diubah', 'Berhasil');
|
|
|
|
return redirect('dashboard/manage/transactions');
|
|
}
|
|
|
|
public function delete(Transaction $transaction): RedirectResponse
|
|
{
|
|
DB::transaction(function () use ($transaction) {
|
|
$this->deleteAttachment('transactions', $transaction);
|
|
$transaction->delete();
|
|
});
|
|
|
|
notify()->success('Data berhasil dihapus', 'Berhasil');
|
|
|
|
return back();
|
|
}
|
|
|
|
public function getPrice(string $serviceId): JsonResponse
|
|
{
|
|
$service = Service::find($serviceId);
|
|
|
|
if (! $service) {
|
|
return response()->json([
|
|
'status' => false,
|
|
'message' => 'Service not found',
|
|
], 404);
|
|
}
|
|
|
|
return response()->json([
|
|
'status' => true,
|
|
'message' => 'Data retrieved successfully',
|
|
'data' => formatToRupiah($service->price),
|
|
], 200);
|
|
}
|
|
|
|
public function print(Transaction $transaction, TransactionRequest $request): View
|
|
{
|
|
$validatedData = $request->validated();
|
|
|
|
return view('pages.admin.manage.transaction.print', [
|
|
'transaction' => $transaction->load(['user', 'user.biography', 'customer', 'service', 'barbershop']),
|
|
'url' => route('manage.transactions'),
|
|
'showingBarberName' => $validatedData['showing_barber_name'],
|
|
'showingCashierName' => $validatedData['showing_cashier_name'],
|
|
]);
|
|
}
|
|
}
|