reconstruction of the project
This commit is contained in:
+107
-78
@@ -2,15 +2,17 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\BaseModel;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class AdditionalCharge extends Model
|
||||
class AdditionalCharge extends BaseModel
|
||||
{
|
||||
protected $table = 'additional_charges';
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
// CI useTimestamps = false
|
||||
public $timestamps = true;
|
||||
|
||||
protected $fillable = [
|
||||
'parent_id',
|
||||
'invoice_id',
|
||||
@@ -28,20 +30,18 @@ class AdditionalCharge extends Model
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'parent_id' => 'integer',
|
||||
'invoice_id' => 'integer',
|
||||
'amount' => 'decimal:2',
|
||||
'created_by' => 'integer',
|
||||
'due_date' => 'date',
|
||||
'parent_id' => 'integer',
|
||||
'invoice_id' => 'integer',
|
||||
'amount' => 'decimal:2', // adjust if you store more precision
|
||||
'due_date' => 'date', // if due_date is DATE/DATETIME
|
||||
'created_by' => 'integer',
|
||||
];
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Relationships
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
/* =========================
|
||||
* Relationships (optional)
|
||||
* ========================= */
|
||||
|
||||
public function parent()
|
||||
public function parentUser()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'parent_id');
|
||||
}
|
||||
@@ -51,82 +51,112 @@ class AdditionalCharge extends Model
|
||||
return $this->belongsTo(Invoice::class, 'invoice_id');
|
||||
}
|
||||
|
||||
public function creator()
|
||||
/* =========================
|
||||
* Query scopes (optional)
|
||||
* ========================= */
|
||||
|
||||
public function scopeForTerm(Builder $q, string $schoolYear, ?string $semester = null): Builder
|
||||
{
|
||||
return $this->belongsTo(User::class, 'created_by');
|
||||
}
|
||||
$q->where('school_year', $schoolYear);
|
||||
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Custom Query Scopes (CI equivalent of byParentTerm)
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
public function scopeByParentTerm(Builder $query, int $parentId, string $year, string $semester, ?string $status = null)
|
||||
{
|
||||
$query->where('parent_id', $parentId)
|
||||
->where('school_year', $year)
|
||||
->where('semester', $semester);
|
||||
|
||||
if ($status !== null) {
|
||||
$query->where('status', $status);
|
||||
if ($semester !== null && $semester !== '') {
|
||||
$q->where('semester', $semester);
|
||||
}
|
||||
|
||||
return $query->orderBy('id', 'DESC');
|
||||
return $q;
|
||||
}
|
||||
|
||||
public function scopeStatus(Builder $q, ?string $status): Builder
|
||||
{
|
||||
return ($status !== null && $status !== '')
|
||||
? $q->where('status', $status)
|
||||
: $q;
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Custom Methods
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
public function scopeByParentTerm(
|
||||
Builder $q,
|
||||
int $parentId,
|
||||
string $year,
|
||||
string $sem,
|
||||
?string $status = null
|
||||
): Builder {
|
||||
$q->where('parent_id', $parentId)
|
||||
->where('school_year', $year)
|
||||
->where('semester', $sem);
|
||||
|
||||
public function markApplied($ids, int $invoiceId): bool
|
||||
if ($status !== null && $status !== '') {
|
||||
$q->where('status', $status);
|
||||
}
|
||||
|
||||
return $q->orderByDesc('id');
|
||||
}
|
||||
|
||||
/* =========================
|
||||
* CI methods equivalents
|
||||
* ========================= */
|
||||
|
||||
public static function byParentTerm(int $parentId, string $year, string $sem, ?string $status = null)
|
||||
{
|
||||
return static::query()
|
||||
->where('parent_id', $parentId)
|
||||
->where('school_year', $year)
|
||||
->where('semester', $sem)
|
||||
->when($status !== null, fn ($q) => $q->where('status', $status))
|
||||
->orderByDesc('id')
|
||||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set status=applied and invoice_id for given IDs.
|
||||
* Returns true if nothing to update (empty ids) or update succeeded.
|
||||
*/
|
||||
public static function markApplied($ids, int $invoiceId): bool
|
||||
{
|
||||
$ids = is_array($ids) ? $ids : [$ids];
|
||||
$ids = array_values(array_filter(array_map('intval', $ids)));
|
||||
|
||||
if (empty($ids)) {
|
||||
if (count($ids) === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$ids = array_map('intval', $ids);
|
||||
|
||||
return $this->whereIn('id', $ids)
|
||||
$affected = static::query()
|
||||
->whereIn('id', $ids)
|
||||
->update([
|
||||
'status' => 'applied',
|
||||
'invoice_id' => $invoiceId
|
||||
]) >= 0;
|
||||
'status' => 'applied',
|
||||
'invoice_id' => $invoiceId,
|
||||
]);
|
||||
|
||||
return $affected >= 0; // update returns affected rows; 0 is still "ok"
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Pagination Search (CI paginate alternative)
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
public function listAllForTerm(
|
||||
/**
|
||||
* listAllForTerm (Laravel version)
|
||||
* - joins users + invoices
|
||||
* - supports status + q search
|
||||
* - paginates
|
||||
* - fallback: if no rows for semester and allowed, retry year-only
|
||||
*/
|
||||
public static function listAllForTerm(
|
||||
string $schoolYear,
|
||||
string $semester,
|
||||
?string $status = null,
|
||||
?string $q = null,
|
||||
int $perPage = 50,
|
||||
bool $fallbackToYear = true
|
||||
)
|
||||
{
|
||||
$build = function (?string $semesterFilter) use ($schoolYear, $status, $q) {
|
||||
$query = self::query()
|
||||
->select('additional_charges.*')
|
||||
->selectRaw("CONCAT(COALESCE(users.lastname,''), ', ', COALESCE(users.firstname,'')) AS parent_name")
|
||||
->selectRaw('invoices.invoice_number')
|
||||
) {
|
||||
$build = function (?string $semFilter) use ($schoolYear, $status, $q) {
|
||||
$query = static::query()
|
||||
->select([
|
||||
'additional_charges.*',
|
||||
DB::raw("CONCAT(COALESCE(users.lastname, ''), ', ', COALESCE(users.firstname, '')) AS parent_name"),
|
||||
'invoices.invoice_number',
|
||||
])
|
||||
->leftJoin('users', 'users.id', '=', 'additional_charges.parent_id')
|
||||
->leftJoin('invoices', 'invoices.id', '=', 'additional_charges.invoice_id')
|
||||
->where('additional_charges.school_year', $schoolYear);
|
||||
|
||||
if ($semesterFilter !== null && $semesterFilter !== '') {
|
||||
$query->where('additional_charges.semester', $semesterFilter);
|
||||
if ($semFilter !== null && $semFilter !== '') {
|
||||
$query->where('additional_charges.semester', $semFilter);
|
||||
}
|
||||
|
||||
if (!empty($status)) {
|
||||
@@ -134,26 +164,25 @@ class AdditionalCharge extends Model
|
||||
}
|
||||
|
||||
if (!empty($q)) {
|
||||
$query->where(function ($sub) use ($q) {
|
||||
$sub->where('additional_charges.title', 'like', "%$q%")
|
||||
->orWhere('additional_charges.description', 'like', "%$q%")
|
||||
->orWhere('users.firstname', 'like', "%$q%")
|
||||
->orWhere('users.lastname', 'like', "%$q%")
|
||||
->orWhere('invoices.invoice_number', 'like', "%$q%");
|
||||
$term = trim($q);
|
||||
$query->where(function ($w) use ($term) {
|
||||
$w->where('additional_charges.title', 'like', "%{$term}%")
|
||||
->orWhere('additional_charges.description', 'like', "%{$term}%")
|
||||
->orWhere('users.firstname', 'like', "%{$term}%")
|
||||
->orWhere('users.lastname', 'like', "%{$term}%")
|
||||
->orWhere('invoices.invoice_number', 'like', "%{$term}%");
|
||||
});
|
||||
}
|
||||
|
||||
return $query;
|
||||
return $query->orderByDesc('additional_charges.id');
|
||||
};
|
||||
|
||||
$result = $build($semester)
|
||||
->orderBy('additional_charges.id', 'DESC')
|
||||
->paginate($perPage);
|
||||
// First attempt: year + semester
|
||||
$result = $build($semester)->paginate($perPage);
|
||||
|
||||
if ($fallbackToYear && $result->count() === 0 && $semester !== '') {
|
||||
$result = $build(null)
|
||||
->orderBy('additional_charges.id', 'DESC')
|
||||
->paginate($perPage);
|
||||
// Fallback: if no rows and allowed, retry year-only
|
||||
if ($fallbackToYear && $result->total() === 0 && $semester !== '') {
|
||||
$result = $build(null)->paginate($perPage);
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
||||
Reference in New Issue
Block a user