<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

use Auth;
use DB;
use Crypt;

class Setting 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 storeSetting($object)
    {
          
        return DB::transaction(function () use ($object){
        
            $setting = new Setting;

            $setting->title         = $object['title'];
            $setting->type          = $object['type'];
            $setting->value         = $object['value'];

            
            if(isset($object['carry_forward']))
            {
                $setting->carry_forward = 1;
            }
            else
            {
                $setting->carry_forward = 0;
            }
             

            $setting->save();

            //dd($setting);

            return with($setting);
            
        });

    } 


    // Updating Setting modal data

    public function updateSetting($object)
    {
          
        return DB::transaction(function () use ($object){
        
            $setting = Setting::find($object['setting']);
             
          if(isset($setting->id))
          {
            
            $setting->title         = $object['title'];
            $setting->type          = $object['type'];
            $setting->value         = $object['value'];
            
            
            if(isset($object['carry_forward']))
            {
                $setting->carry_forward = 1;
            }
            else
            {
                $setting->carry_forward = 0;
            }

            $setting->update();
           }

            return with($setting);
        });

    } 

    
     //RETRIEVING setting BY ID
    public function getsettingById($id)
    {
      
       $condition = $this->getCondition();
       $condition += ['id' => $id];
        return Setting::where($condition)->first();

    }
    public function getSettingList()
    {
        return Setting::where($this->getCondition())->orderBy('id','desc')->get();

    }
    
    

    // ALL FORIGN KEY RELATIONSHIP FUNCTION
    


    // END ALL FORIGN KEY RELATIONSHIP FUNCTION


    // Storing setting modal data

    
}
