<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\DB;

class SupAgeing extends Model
{
    use HasFactory;

    protected $table = "supplier_ageing";

    protected $fillable = [
        'age_code',
        'description',
        'group_type',
        'interval_type',
        'interval_30_days',
        'interval_7_days',
        'interval_custom',
        'custom_age_1',
        'custom_age_2',
        'custom_age_3',
        'custom_age_4',
        'custom_age_5',
        'custom_age_6',
        'custom_age_7',
        'custom_interval_1',
        'custom_interval_2',
        'custom_interval_3',
        'custom_interval_4',
        'custom_interval_5',
        'custom_interval_6',
    ];

    // Edit an ageing record
    public static function editAgeing($id, array $data)
    {
        return DB::update(
            "UPDATE supplier_ageing SET 
                age_code = ?, 
                description = ?, 
                group_type = ?, 
                interval_type = ?,
                interval_30_days = ?,
                interval_7_days = ?,
                interval_custom = ?,
                custom_age_1 = ?,
                custom_age_2 = ?,
                custom_age_3 = ?,
                custom_age_4 = ?,
                custom_age_5 = ?,
                custom_age_6 = ?,
                custom_age_7 = ?,
                custom_interval_1 = ?,
                custom_interval_2 = ?,
                custom_interval_3 = ?,
                custom_interval_4 = ?,
                custom_interval_5 = ?,
                custom_interval_6 = ?
            WHERE id = ?",
            [
                $data['age_code'],
                $data['description'],
                $data['group_type'],
                $data['interval_type'],
                $data['interval_30_days'] ?? 0,
                $data['interval_7_days'] ?? 0,
                $data['interval_custom'] ?? 0,
                $data['custom_age_1'] ?? null,
                $data['custom_age_2'] ?? null,
                $data['custom_age_3'] ?? null,
                $data['custom_age_4'] ?? null,
                $data['custom_age_5'] ?? null,
                $data['custom_age_6'] ?? null,
                $data['custom_age_7'] ?? null,
                $data['custom_interval_1'] ?? null,
                $data['custom_interval_2'] ?? null,
                $data['custom_interval_3'] ?? null,
                $data['custom_interval_4'] ?? null,
                $data['custom_interval_5'] ?? null,
                $data['custom_interval_6'] ?? null,
                $id
            ]
        );
    }

    // Delete an ageing record
    public static function deleteAgeing($id)
    {
        return DB::delete("DELETE FROM supplier_ageing WHERE id = ?", [$id]);
    }
}