<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class CustomerAdvancePaymentMaster extends BaseModel
{
    use HasFactory;

    protected $guarded = [];

    protected static function boot()
    {
        parent::boot();
        static::addGlobalScope('typeScope', function ($builder) {
            if (auth()->check() && auth()->user()->shop_id != null) {
                $builder->where('transition_location_id', auth()->user()->shop_id);
            }
        });
        static::creating(function ($model) {
            if (auth()->check()) {
                $user = auth()->user();
                $model->transition_location_id = $user->shop_id;
                $model->transition_create_by = $user->id;
            }
        });

        static::updating(function ($model) {
            if (auth()->check()) {
                $model->transition_update_by = auth()->id();
            }
        });
    }

    public function customer()
    {
        return $this->hasOne(Client::class, 'id', 'customer_id');
    }

    public function details()
    {
        return $this->hasMany(CustomerAdvancePaymentDetail::class, 'advance_payment_master_id', 'id')->with('attribute', 'product');
    }

    public function warehouse()
    {
        return $this->hasOne(Warehouse::class, 'id', 'warehouse_id');
    }

    public function payments()
    {
        return $this->hasMany(CustomerPaymentCollection::class, 'advance_payment_master_id', 'id');
    }
}
