add tests batch 20
This commit is contained in:
+22
-25
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class Payment extends BaseModel
|
||||
@@ -33,16 +34,16 @@ class Payment extends BaseModel
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'parent_id' => 'integer',
|
||||
'invoice_id' => 'integer',
|
||||
'number_of_installments' => 'integer',
|
||||
'updated_by' => 'integer',
|
||||
'parent_id' => 'integer',
|
||||
'invoice_id' => 'integer',
|
||||
'number_of_installments' => 'integer',
|
||||
'updated_by' => 'integer',
|
||||
|
||||
'total_amount' => 'decimal:2',
|
||||
'paid_amount' => 'decimal:2',
|
||||
'balance' => 'decimal:2',
|
||||
'total_amount' => 'decimal:2',
|
||||
'paid_amount' => 'decimal:2',
|
||||
'balance' => 'decimal:2',
|
||||
|
||||
'payment_date' => 'datetime',
|
||||
'payment_date' => 'datetime',
|
||||
];
|
||||
|
||||
/* Optional relationships */
|
||||
@@ -83,20 +84,18 @@ class Payment extends BaseModel
|
||||
{
|
||||
/** @var self|null $payment */
|
||||
$payment = static::query()->find($paymentId);
|
||||
if (! $payment) {
|
||||
return false;
|
||||
}
|
||||
if (!$payment) return false;
|
||||
|
||||
$newBalance = (float) $payment->balance - (float) $amountPaid;
|
||||
$newBalance = (float)$payment->balance - (float)$amountPaid;
|
||||
|
||||
// keep precision stable
|
||||
$paidAmount = (float) $payment->paid_amount + (float) $amountPaid;
|
||||
$paidAmount = (float)$payment->paid_amount + (float)$amountPaid;
|
||||
|
||||
$affected = static::query()
|
||||
->whereKey($paymentId)
|
||||
->update([
|
||||
'paid_amount' => $paidAmount,
|
||||
'balance' => $newBalance,
|
||||
'balance' => $newBalance,
|
||||
]);
|
||||
|
||||
return $affected > 0;
|
||||
@@ -117,21 +116,21 @@ class Payment extends BaseModel
|
||||
public static function generateNewTransactionId(): string
|
||||
{
|
||||
$currentYear = date('Y');
|
||||
$prefix = $currentYear.'-';
|
||||
$prefix = $currentYear . '-';
|
||||
|
||||
$latest = static::query()
|
||||
->where('transaction_id', 'like', $prefix.'%')
|
||||
->where('transaction_id', 'like', $prefix . '%')
|
||||
->orderByDesc('transaction_id')
|
||||
->first();
|
||||
|
||||
if ($latest && ! empty($latest->transaction_id)) {
|
||||
$lastNumber = (int) str_replace($prefix, '', (string) $latest->transaction_id);
|
||||
if ($latest && !empty($latest->transaction_id)) {
|
||||
$lastNumber = (int) str_replace($prefix, '', (string)$latest->transaction_id);
|
||||
$newNumber = $lastNumber + 1;
|
||||
} else {
|
||||
$newNumber = 1;
|
||||
}
|
||||
|
||||
return $prefix.str_pad((string) $newNumber, 6, '0', STR_PAD_LEFT);
|
||||
return $prefix . str_pad((string)$newNumber, 6, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,13 +139,11 @@ class Payment extends BaseModel
|
||||
*/
|
||||
public static function getPaymentsByInvoice($invoiceIds): array
|
||||
{
|
||||
if (! is_array($invoiceIds)) {
|
||||
if (!is_array($invoiceIds)) {
|
||||
$invoiceIds = [$invoiceIds];
|
||||
}
|
||||
$invoiceIds = array_values(array_unique(array_map('intval', array_filter($invoiceIds))));
|
||||
if (empty($invoiceIds)) {
|
||||
return [];
|
||||
}
|
||||
if (empty($invoiceIds)) return [];
|
||||
|
||||
// Subquery: latest payment_date per invoice (Full/Partial only)
|
||||
$latestSub = DB::table('payments')
|
||||
@@ -158,7 +155,7 @@ class Payment extends BaseModel
|
||||
$rows = DB::table('payments as p')
|
||||
->joinSub($latestSub, 'latest', function ($join) {
|
||||
$join->on('latest.invoice_id', '=', 'p.invoice_id')
|
||||
->on('latest.max_date', '=', 'p.payment_date');
|
||||
->on('latest.max_date', '=', 'p.payment_date');
|
||||
})
|
||||
->whereIn('p.invoice_id', $invoiceIds)
|
||||
->whereIn('p.status', ['Full', 'Paid', 'Partially Paid'])
|
||||
@@ -172,4 +169,4 @@ class Payment extends BaseModel
|
||||
|
||||
return $rows->map(fn ($r) => (array) $r)->all();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user