<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Designation 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 getDesignationList()
    {
        return Designation::where($this->getCondition())->orderBy('id','desc')->get();
    }

     public function getDesignationById($id)
    {
      
       $condition = $this->getCondition();

       $condition += ['id' => $id];
       
        return Designation::where($condition)->first();

    }


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

            

          $designation                = new Designation;
          $designation->title         = $object['title'];

          $designation->save();


            return with($designation);
          

        });
    }

    public function updateDesignation($object,$designation_id)
    {

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

            $designation = Designation::find($designation_id);

            if(isset($designation->id))
            {
       
            
             $designation->title         = $object['title'];

             $designation->update();

                return with($designation);
                
               
            }
            
            return 0;
        });
    }
}
