id()) ->orderBy('updated_at', 'desc'); return inertia('Paylater/Index', [ 'histories' => $histories->paginate(20), ]); } public function show(Request $request, PaylaterHistory $paylater) { if ($paylater->type == PaylaterHistory::TYPE_REPAYMENT) { $deposit = DepositHistory::where('related_id', $paylater->id)->first(); if (!in_array($deposit->is_valid, [DepositHistory::STATUS_VALID])) { return redirect()->route('transactions.deposit.show', [ 'deposit' => $deposit, 'back' => 'customer.paylater.index' ]); } } return inertia('Paylater/Detail', [ 'paylater' => $paylater, ]); } public function create(Request $request) { return inertia('Paylater/Repay', [ 'amount' => $request->user('customer')->paylater->usage, 'payments' => GeneralService::getEnablePayment(), ]); } public function store(Request $request) { $request->validate([ 'amount' => 'required|numeric|min:1000', 'payment' => [ 'required', Rule::in([Setting::PAYMENT_MANUAL, Setting::PAYMENT_MIDTRANS, Setting::PAYMENT_CASH_DEPOSIT]), ], ]); $customer = $request->user('customer'); // validate amount if ($customer->paylater->usage < $request->amount) { return redirect()->back() ->with('message', ['type' => 'error', 'message' => 'Nominal Tagihan tidak boleh lebih dari tagihan']); } // only 1 repayment at a time $repayment = DepositHistory::query() ->where([ ['customer_id', '=', $customer->id], ['type', '=', DepositHistory::TYPE_REPAYMENT] ])->where(function ($query) { $query->where('is_valid', '!=', DepositHistory::STATUS_VALID) ->where('is_valid', '!=', DepositHistory::STATUS_REJECT) ->where('is_valid', '!=', DepositHistory::STATUS_EXPIRED); })->first(); if ($repayment != null) { return redirect()->back() ->with('message', ['type' => 'error', 'message' => 'Selesaikan pembayaran tagihan sebelumnya']); } DB::beginTransaction(); $code = GeneralService::generateDepositRepayCode(); $paylater = $customer->paylaterHistories()->create([ 'credit' => $request->amount, 'description' => $code, 'type' => PaylaterHistory::TYPE_REPAYMENT, ]); $deposit = DepositHistory::make([ 'description' => $code, 'related_type' => PaylaterHistory::class, 'related_id' => $paylater->id, 'customer_id' => $customer->id, 'debit' => $request->amount, 'payment_channel' => $request->payment, 'type' => DepositHistory::TYPE_REPAYMENT, ]); if (in_array($request->payment, [Setting::PAYMENT_MANUAL, Setting::PAYMENT_CASH_DEPOSIT])) { $deposit->is_valid = DepositHistory::STATUS_WAIT_UPLOAD; $paylater->is_valid = PaylaterHistory::STATUS_WAIT_UPLOAD; $deposit->save(); } if ($request->payment == Setting::PAYMENT_MIDTRANS) { $deposit->is_valid = DepositHistory::STATUS_WAIT_PAYMENT; $paylater->is_valid = PaylaterHistory::STATUS_WAIT_PAYMENT; $deposit->save(); $token = (new MidtransService($deposit, Setting::getByKey('MIDTRANS_SERVER_KEY')))->getSnapToken(); $deposit->update(['payment_token' => $token]); } $paylater->save(); DB::commit(); return redirect()->route('transactions.deposit.show', [ 'deposit' => $deposit->id, 'direct' => 'true', 'back' => 'customer.paylater.index' ]); } }