Fix Laravel Pint formatting
This commit is contained in:
+32
-20
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
@@ -35,19 +34,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 */
|
||||
@@ -84,13 +83,18 @@ 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;
|
||||
}
|
||||
|
||||
@@ -108,11 +112,14 @@ 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
|
||||
@@ -121,6 +128,7 @@ 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
|
||||
@@ -207,7 +215,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);
|
||||
}
|
||||
|
||||
@@ -242,10 +250,12 @@ 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)
|
||||
@@ -259,10 +269,12 @@ 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)
|
||||
@@ -314,10 +326,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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user