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.

110 lines
3.6 KiB
PHTML

1 year ago
<?php
namespace App\Http\Controllers\Admin;
1 year ago
use App\Http\Controllers\Controller;
1 year ago
use App\Models\Customer;
use App\Models\DepositHistory;
1 year ago
use App\Models\PaylaterHistory;
1 year ago
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
1 year ago
class DepositController extends Controller
{
public function index(Request $request)
1 year ago
{
1 year ago
$deposits = DepositHistory::with(['customer', 'account', 'depositLocation', 'editor'])
->where('credit', 0);
if ($request->q != '') {
1 year ago
$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%");
});
});
}
1 year ago
$sortBy = 'updated_at';
$sortRule = 'desc';
if ($request->sortBy != '' && $request->sortRule != '') {
$sortBy = $request->sortBy;
$sortRule = $request->sortRule;
}
$deposits->orderBy($sortBy, $sortRule);
if ($request->status != '') {
1 year ago
$deposits->where('is_valid', $request->status);
}
if ($request->customer_id != '') {
1 year ago
$deposits->where('is_valid', $request->customer_id);
}
1 year ago
$customers = Customer::with(['paylater'])->orderBy('deposit_balance', 'desc');
$stats = [
'deposit_this_month' => DepositHistory::whereMonth('created_at', now()->month)
->whereYear('created_at', now()->year)
->sum('debit'),
'deposit_today' => DepositHistory::whereDate('created_at', now())
->sum('debit'),
'paylater_this_month' => PaylaterHistory::whereMonth('created_at', now()->month)
->whereYear('created_at', now()->year)
->sum('debit'),
'paylater_today' => PaylaterHistory::whereDate('created_at', now())
->sum('debit'),
];
return inertia('DepositHistory/Index', [
1 year ago
'deposits' => $deposits->paginate(),
'_q' => $request->q,
'_sortBy' => $sortBy,
'_sortRule' => $sortRule,
'customers' => $customers->paginate(10, '*', 'customer_page'),
'stats' => $stats,
]);
1 year ago
}
public function edit(DepositHistory $deposit)
{
1 year ago
return inertia('DepositHistory/Form', [
'deposit' => $deposit->load(['customer', 'account', 'depositLocation', 'editor']),
]);
}
public function update(Request $request, DepositHistory $deposit)
1 year ago
{
$request->validate([
1 year ago
'is_valid' => [
'required',
Rule::in([DepositHistory::STATUS_VALID, DepositHistory::STATUS_REJECT]),
],
1 year ago
'debit' => 'required|numeric',
]);
if ($request->status == DepositHistory::STATUS_REJECT) {
$request->validate(['reject_reason' => 'required|string']);
}
DB::beginTransaction();
$deposit->update([
1 year ago
'debit' => $request->debit,
'is_valid' => $request->is_valid,
'note' => $request->reject_reason,
]);
1 year ago
if ($request->status == DepositHistory::STATUS_VALID) {
$deposit->update_customer_balance();
$deposit->create_notification_user();
}
DB::commit();
session()->flash('message', ['type' => 'success', 'message' => 'Item has beed updated']);
1 year ago
}
}