@@ -287,9 +287,25 @@ public function rejectPayout(PayoutRequest $payoutRequest, int $rejectedBy, stri
287287 'status ' => EarningsLedger::STATUS_AVAILABLE ,
288288 ]);
289289
290- // คืนหนี้ที่หัก (ถ้ามี)
290+ // คืนหนี้ที่หัก (ถ้ามี) → สร้าง WalletDebt ใหม่คืนให้ User
291291 if ($ payoutRequest ->debt_deduction > 0 ) {
292- // TODO: implement debt reversal if needed
292+ WalletDebt::create ([
293+ 'user_id ' => $ payoutRequest ->user_id ,
294+ 'original_amount ' => $ payoutRequest ->debt_deduction ,
295+ 'deducted_amount ' => 0 ,
296+ 'remaining_amount ' => $ payoutRequest ->debt_deduction ,
297+ 'reason ' => 'คืนหนี้จาก Payout ที่ถูกปฏิเสธ # ' . $ payoutRequest ->id ,
298+ 'source_type ' => 'PayoutRequest ' ,
299+ 'source_id ' => $ payoutRequest ->id ,
300+ 'status ' => 'active ' ,
301+ 'priority ' => 0 ,
302+ ]);
303+
304+ Log::info ('Debt reversed due to payout rejection ' , [
305+ 'payout_request_id ' => $ payoutRequest ->id ,
306+ 'user_id ' => $ payoutRequest ->user_id ,
307+ 'reversed_amount ' => $ payoutRequest ->debt_deduction ,
308+ ]);
293309 }
294310
295311 $ payoutRequest ->update ([
@@ -396,6 +412,9 @@ protected function getSourceWallet(string $earningType): string
396412
397413 /**
398414 * โอนเงินเข้า User Wallet
415+ *
416+ * ใช้ WalletService เพื่อให้ audit trail, balance tracking,
417+ * total_income, และ last_transaction_at ถูกต้อง
399418 */
400419 protected function transferToUserWallet (PayoutRequest $ payoutRequest ): void
401420 {
@@ -404,31 +423,19 @@ protected function transferToUserWallet(PayoutRequest $payoutRequest): void
404423 throw new \Exception ("ไม่พบ User # {$ payoutRequest ->user_id }" );
405424 }
406425
407- // ตรวจสอบว่า User มี Wallet หรือไม่
408- $ wallet = $ user ->wallet ;
409- if (! $ wallet ) {
410- // สร้าง wallet ใหม่ถ้ายังไม่มี
411- $ wallet = $ user ->wallet ()->create ([
412- 'balance ' => 0 ,
413- 'currency ' => 'THB ' ,
414- ]);
415- }
416-
417- // เพิ่มเงินเข้า wallet
418- $ wallet ->increment ('balance ' , $ payoutRequest ->net_amount );
419-
420- // สร้าง transaction record
421- if (method_exists ($ wallet , 'transactions ' )) {
422- $ wallet ->transactions ()->create ([
423- 'user_id ' => $ user ->id ,
424- 'type ' => 'payout ' ,
425- 'amount ' => $ payoutRequest ->net_amount ,
426- 'balance_after ' => $ wallet ->fresh ()->balance ,
427- 'description ' => "รับเงินจากการถอน - {$ payoutRequest ->earning_type }" ,
428- 'reference_type ' => 'PayoutRequest ' ,
429- 'reference_id ' => $ payoutRequest ->id ,
430- ]);
431- }
426+ // ใช้ WalletService เพื่อ consistency กับส่วนอื่นของระบบ
427+ $ walletService = new WalletService ;
428+ $ wallet = $ walletService ->getOrCreateWallet ($ user );
429+
430+ // ใช้ deposit() เพื่อให้ balance_before/after, total_income, log ถูกต้อง
431+ $ walletService ->deposit (
432+ $ wallet ,
433+ $ payoutRequest ->net_amount ,
434+ "รับเงินจากการถอน - {$ payoutRequest ->earning_type }" ,
435+ 'PayoutRequest ' ,
436+ $ payoutRequest ->id ,
437+ ['payout_request_id ' => $ payoutRequest ->id , 'earning_type ' => $ payoutRequest ->earning_type ]
438+ );
432439 }
433440
434441 /**
0 commit comments