<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class AssignAllowance extends Model
{
     private $generic_condition;

   public function __construct()
    {
           
       $this->setCondition(['is_recycle' => '0']);
    }

    public function setCondition($condition = [])
    {
        $this->generic_condition = $condition;
    }

    public function getCondition()
    {
        return $this->generic_condition;
    }

    public function getAssignedAllowanceList()
    {
        return AssignAllowance::where($this->getCondition())->orderBy('id','desc')->with(['allowance','designation','scale'])->get();
    }

     public function storeAssignAllowance($object)
    {
        return \DB::transaction(function () use ($object)  {

            

          $assign_allowance                      = new AssignAllowance;
          $assign_allowance->scale_id            = $object['scale'];
          $assign_allowance->designation_id      = $object['designation'];
          $assign_allowance->allowance_id        = $object['allowance'];


          $assign_allowance->save();


            return with($assign_allowance);
          

        });
    }

     public function updateAssignAllowance($object)
    {

       return \DB::transaction(function () use ($object) {

            $assign_allowance = AssignAllowance::find($object['id']);

            if(isset($assign_allowance->id))
            {
       
           
          $assign_allowance->scale_id            = $object['scale'];
          $assign_allowance->designation_id      = $object['designation'];
          $assign_allowance->allowance_id        = $object['allowance'];

        

             $assign_allowance->update();

                return with($assign_allowance);
                
                return 1;
            }
            
            return 0;
        });
    }

    public function getAssignAllowanceById($id)
    {
      
       $condition = $this->getCondition();
       $condition += ['id' => $id];
        return AssignAllowance::where($condition)->first();

    }
    public function getAssignedAllowanceByCriteria($scale,$designation)
    {
      $condition = $this->getCondition();
      $condition += ['scale_id' => $scale ,'designation_id' => $designation];
        return AssignAllowance::where($condition)->get();
    }
     public function scale()
    
    {
        return $this->belongsTo(Scale::class,'scale_id')->withDefault();
    }
    public function designation()
    {
        return $this->belongsTo(Designation::class,'designation_id')->withDefault();
    }

   
     public function allowance()
    {
        return $this->belongsTo(Allowance::class,'allowance_id')->withDefault();
    }
}
