﻿<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use App\Scopes\OrganisationScope;


/**
 * SaleReturnItems
 */
class SaleReturnItems extends Model
{
    use HasFactory;

    /**
     * fillable
     *
     * @var array
     */
    public $fillable = [
        'sale_return_id',
        'invoice_item_id',
        'product_id',
        'product_name',
        'return_qty',
        'return_price',
        'return_sub_total',
        'created_by',
        'updated_by',
    ];

    protected static function booted()
    {
        static::addGlobalScope(new OrganisationScope);

        // Automatically set the organisation_id before creating the model
        static::creating(function ($model) {
            if (auth()->check()) {
                $model->organisation_id = auth()->user()->organisation_id;
            }
        });
    }

    /**
     * product
     *
     * @return void
     */
    public function product()
    {
        return $this->belongsTo(Product::class, 'product_id', 'id');
    }
    public function productStock(): BelongsTo
    {
        return $this->belongsTo(ProductStock::class);
    }