You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
4.1 KiB
PHP
116 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Customer;
|
|
use App\Models\DepositHistory;
|
|
use App\Models\PaylaterHistory;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class DepositController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$deposits = DepositHistory::with(['customer', 'account', 'depositLocation', 'editor'])
|
|
->where('credit', 0)
|
|
->where('type', DepositHistory::TYPE_DEPOSIT);
|
|
|
|
if ($request->q != '') {
|
|
$deposits->where(function ($query) use ($request) {
|
|
$query->where('description', 'ilike', "%$request->q%")
|
|
->orWhereHas('customer', function ($query) use ($request) {
|
|
$query->where('fullname', 'ilike', "%$request->q%")
|
|
->orWhere('email', 'ilike', "%$request->q%")
|
|
->orWhere('phone', 'ilike', "%$request->q%");
|
|
});
|
|
});
|
|
}
|
|
|
|
$sortBy = 'updated_at';
|
|
$sortRule = 'desc';
|
|
if ($request->sortBy != '' && $request->sortRule != '') {
|
|
$sortBy = $request->sortBy;
|
|
$sortRule = $request->sortRule;
|
|
}
|
|
|
|
$deposits->orderBy($sortBy, $sortRule);
|
|
|
|
if ($request->status != '') {
|
|
$deposits->where('is_valid', $request->status);
|
|
}
|
|
|
|
$customers = Customer::with(['paylater'])->orderBy('deposit_balance', 'desc');
|
|
|
|
$stats = [
|
|
'deposit_this_month' => DepositHistory::whereMonth('created_at', now()->month)
|
|
->whereYear('created_at', now()->year)
|
|
->where('is_valid', DepositHistory::STATUS_VALID)
|
|
->where('type', DepositHistory::TYPE_DEPOSIT)
|
|
->sum('debit'),
|
|
'deposit_today' => DepositHistory::whereDate('created_at', now())
|
|
->where('is_valid', DepositHistory::STATUS_VALID)
|
|
->where('type', DepositHistory::TYPE_DEPOSIT)
|
|
->sum('debit'),
|
|
'paylater_this_month' => PaylaterHistory::whereMonth('created_at', now()->month)
|
|
->where('is_valid', PaylaterHistory::STATUS_VALID)
|
|
->where('type', DepositHistory::TYPE_DEPOSIT)
|
|
->whereYear('created_at', now()->year)
|
|
->sum('debit'),
|
|
'paylater_today' => PaylaterHistory::whereDate('created_at', now())
|
|
->where('is_valid', PaylaterHistory::STATUS_VALID)
|
|
->where('type', DepositHistory::TYPE_DEPOSIT)
|
|
->sum('debit'),
|
|
];
|
|
|
|
return inertia('DepositHistory/Index', [
|
|
'deposits' => $deposits->paginate(),
|
|
'_q' => $request->q,
|
|
'_sortBy' => $sortBy,
|
|
'_sortRule' => $sortRule,
|
|
'customers' => $customers->paginate(10, '*', 'customer_page'),
|
|
'stats' => $stats,
|
|
]);
|
|
}
|
|
|
|
public function edit(DepositHistory $deposit)
|
|
{
|
|
return inertia('DepositHistory/Form', [
|
|
'deposit' => $deposit->load(['customer', 'account', 'depositLocation', 'editor']),
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request, DepositHistory $deposit)
|
|
{
|
|
$request->validate([
|
|
'is_valid' => [
|
|
'required',
|
|
Rule::in([DepositHistory::STATUS_VALID, DepositHistory::STATUS_REJECT]),
|
|
],
|
|
'debit' => 'required|numeric',
|
|
]);
|
|
|
|
if ($request->is_valid == DepositHistory::STATUS_REJECT) {
|
|
$request->validate(['reject_reason' => 'required|string']);
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
$deposit->update([
|
|
'debit' => $request->debit,
|
|
'is_valid' => $request->is_valid,
|
|
'note' => $request->reject_reason,
|
|
]);
|
|
|
|
if ($request->is_valid == DepositHistory::STATUS_VALID) {
|
|
$deposit->update_customer_balance();
|
|
$deposit->create_notification_user();
|
|
}
|
|
DB::commit();
|
|
|
|
return redirect()->route('deposit.index')
|
|
->with('message', ['type' => 'success', 'message' => 'Item has beed updated']);
|
|
}
|
|
}
|