<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class PaymentVoucher extends Model
{
    use HasFactory;

    protected $table = "tbl_payment_voucher";

    protected $fillable = [
        'idsupplier',
        'idway_to_pay',
        'idsource_bank',
        'idreceiving_bank',
        'series',
        'entry_date',
        'document_date',
        'amount',
        'remaining_amount',
        'idcompany',
        'status'
    ];

    protected $hidden = [
        'created_at',
        'updated_at'
    ];

    # Relaciones
    public function supplier(): HasOne
    {
        return $this->hasOne(Supplier::class, 'id', 'idsupplier');
    }

    public function wayToPay(): HasOne
    {
        return $this->hasOne(WayToPay::class, 'id', 'idway_to_pay');
    }

    public function sourceBank(): HasOne
    {
        return $this->hasOne(Bank::class, 'id', 'idsource_bank');
    }

    public function receivingBank(): HasOne
    {
        return $this->hasOne(Bank::class, 'id', 'idreceiving_bank');
    }

    public function company(): HasOne
    {
        return $this->hasOne(Company::class, 'id', 'idcompany');
    }

    public function accountReceivableDets(): HasMany
    {
        return $this->hasMany(AccountReceivableDet::class, 'idpayment_voucher', 'id')->active();
    }

    # Query scopes
    public function scopeActiveForID($query, $id)
    {
        return $query->where('id', $id)->where('status', 1);
    }

    public function scopeActive($query)
    {
        return $query->where('status', 1);
    }

    public function scopeCompany($query)
    {
        return $query->when(
            request('idcompany'),
            fn($q) => $q->where('idcompany', request('idcompany'))
        );
    }

    # Filtros
    public function scopeFilters($query)
    {
        $query->when(
            request('search'),
            fn($query) => $query->where('series', 'like', '%' . request('search') . '%')
                ->orWhereHas(
                    'supplier',
                    fn($query) => $query->where('name_rz', 'like', '%' . request('search') . '%')
                        ->orWhere('rut', 'like', '%' . request('search') . '%')
                )
        );

        $query->when(
            request('idsupplier'),
            fn($query) => $query->where('idsupplier', request('idsupplier'))
        );

        $query->when(
            request('idway_to_pay'),
            fn($query) => $query->where('idway_to_pay', request('idway_to_pay'))
        );

        $query->when(
            request('idsource_bank'),
            fn($query) => $query->where('idsource_bank', request('idsource_bank'))
        );

        $query->when(
            request('idreceiving_bank'),
            fn($query) => $query->where('idreceiving_bank', request('idreceiving_bank'))
        );

        $query->company();

        $query->when(
            request('status'),
            fn($query) => $query->where('status', request('status'))
        )->when(
            !request('status'),
            fn($query) => $query->where('status', 1)
        );


        $query->when(
            request('orderBy'),
            fn($query) => $query->orderBy(request('orderBy'), 'desc')
        )->when(
            !request('orderBy'),
            fn($query) => $query->orderBy('id', 'desc')
        );
    }
}
