<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Branch extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'tbl_branch';

    /**
     * The primary key for the model.
     *
     * @var string
     */
    protected $primaryKey = 'branch_id';

    /**
     * The primary key type.
     *
     * @var string
     */
    protected $keyType = 'string';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'branch_id',
        'branch_name',
        'db_name'
    ];

    /**
     * Get the invoices associated with the branch.
     */
    public function invoices()
    {
        return $this->hasMany(Invoice::class, 'cmp_id', 'branch_id');
    }

    /**
     * Get the user branches associated with this branch.
     */
    public function userBranches()
    {
        return $this->hasMany(UserBranch::class, 'branch_id', 'branch_id');
    }

    /**
     * Get the users associated with this branch.
     */
    public function users()
    {
        return $this->belongsToMany(User::class, 'user_branch', 'branch_id', 'user_id');
    }
}
