add test batches

This commit is contained in:
root
2026-06-08 23:45:55 -04:00
parent c792b8be05
commit 8d4d610b82
1480 changed files with 22587 additions and 10762 deletions
+20 -32
View File
@@ -2,6 +2,7 @@
namespace App\Models;
use App\Models\BaseModel;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
@@ -34,19 +35,19 @@ class Invoice extends BaseModel
];
protected $casts = [
'parent_id' => 'integer',
'updated_by' => 'integer',
'parent_id' => 'integer',
'updated_by' => 'integer',
'has_discount' => 'boolean',
'total_amount' => 'decimal:2',
'balance' => 'decimal:2',
'paid_amount' => 'decimal:2',
'balance' => 'decimal:2',
'paid_amount' => 'decimal:2',
'issue_date' => 'datetime',
'due_date' => 'datetime',
'issue_date' => 'datetime',
'due_date' => 'datetime',
// created_at/updated_at exist but are DB-managed; you can still cast if you like:
'created_at' => 'datetime',
'updated_at' => 'datetime',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
/* Optional relationships */
@@ -83,18 +84,13 @@ class Invoice extends BaseModel
private function normalizeDateTimes(): void
{
foreach (['issue_date', 'due_date', 'created_at', 'updated_at'] as $field) {
if (! $this->isDirty($field)) {
continue;
}
if (!$this->isDirty($field)) continue;
$val = $this->{$field};
if ($val === null || $val === '' || $val === '0000-00-00' || $val === '0000-00-00 00:00:00') {
// keep NULL for due_date; keep other values as-is (DB defaults/triggers)
if ($field === 'due_date') {
$this->{$field} = null;
}
if ($field === 'due_date') $this->{$field} = null;
continue;
}
@@ -112,14 +108,11 @@ class Invoice extends BaseModel
}
$s = trim((string) $raw);
if ($s === '') {
return null;
}
if ($s === '') return null;
// strict datetime
try {
$dt = Carbon::createFromFormat('Y-m-d H:i:s', $s, 'UTC');
return $dt->utc()->format('Y-m-d H:i:s');
} catch (\Throwable $e) {
// ignore
@@ -128,7 +121,6 @@ class Invoice extends BaseModel
// date-only sneaks in => store at 12:00:00
try {
$d = Carbon::createFromFormat('Y-m-d', $s, 'UTC')->setTime(12, 0, 0);
return $d->utc()->format('Y-m-d H:i:s');
} catch (\Throwable $e) {
// ignore
@@ -215,7 +207,7 @@ class Invoice extends BaseModel
->selectRaw('(SELECT COALESCE(SUM(du.discount_amount),0) FROM discount_usages du WHERE du.invoice_id = i.id) AS discount')
->where('i.parent_id', $parentId);
if (! empty($schoolYear)) {
if (!empty($schoolYear)) {
$q->where('i.school_year', $schoolYear);
}
@@ -250,12 +242,10 @@ class Invoice extends BaseModel
public static function getInvoicesByUserIds(array $parentIds, string $schoolYear): array
{
$parentIds = array_values(array_unique(array_map('intval', array_filter($parentIds))));
if (empty($parentIds)) {
return [];
}
if (empty($parentIds)) return [];
return DB::table('invoices as i')
->select(['i.id', 'i.parent_id', 'i.invoice_number', 'i.status', 'i.balance', 'i.total_amount', 'i.issue_date', 'i.due_date'])
->select(['i.id','i.parent_id','i.invoice_number','i.status','i.balance','i.total_amount','i.issue_date','i.due_date'])
->selectRaw('(SELECT COALESCE(SUM(du.discount_amount),0) FROM discount_usages du WHERE du.invoice_id = i.id) AS discount')
->whereIn('i.parent_id', $parentIds)
->where('i.school_year', $schoolYear)
@@ -269,12 +259,10 @@ class Invoice extends BaseModel
public static function getAllInvoicesByUserIds(array $parentIds, string $schoolYear): array
{
$parentIds = array_values(array_unique(array_map('intval', array_filter($parentIds))));
if (empty($parentIds)) {
return [];
}
if (empty($parentIds)) return [];
return DB::table('invoices as i')
->select(['i.id', 'i.parent_id', 'i.invoice_number', 'i.status', 'i.balance', 'i.issue_date'])
->select(['i.id','i.parent_id','i.invoice_number','i.status','i.balance','i.issue_date'])
->selectRaw('(SELECT COALESCE(SUM(du.discount_amount),0) FROM discount_usages du WHERE du.invoice_id = i.id) AS discount')
->whereIn('i.parent_id', $parentIds)
->where('i.school_year', $schoolYear)
@@ -326,10 +314,10 @@ class Invoice extends BaseModel
$affected = DB::table('invoices')
->where('id', $invoiceId)
->update([
'total_amount' => DB::raw('GREATEST(total_amount - '.$amount.', 0)'),
'balance' => DB::raw('GREATEST(balance - '.$amount.', 0)'),
'total_amount' => DB::raw('GREATEST(total_amount - ' . $amount . ', 0)'),
'balance' => DB::raw('GREATEST(balance - ' . $amount . ', 0)'),
]);
return $affected >= 0;
}
}
}