<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Auth;
use DB;
use Crypt;
use App\Scale;
use App\Designation;
use App\User;
use App\ContractDetail;

class Contract 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 storeContract($object)
    {
          
        return DB::transaction(function () use ($object){
        
            $contract = new Contract;

            $contract->user_id         = $object['employee'];
            $contract->branch_id       = $object['branch'];

            $contract->designation_id  = $object['designation']; 
            $contract->scale_id        = $object['scale']; 
            $contract->start_date      = $object['start_date'];
            $contract->end_date        = $object['end_date'];
            $contract->basic_salary    = $object['basic_salary'];
            $contract->contract_form   = $object['contract_form'];
            // $contract->location        = $object['location'];

            $contract->status          = 'Active';

            $contract->save();

            $curr_user = $contract->user;

            if(isset($curr_user->id))
            {
               $curr_user->branch_id = $contract->branch_id;

                   $curr_user->update();
               
            }

            return with($contract);

        });

    } 



    // Updating contract modal data

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

            $contract_detail = new ContractDetail;
            $user            = new User;

            $contract = Contract::find($object['contract']);



            if(isset($contract->id))
            {

             if($contract->designation_id != $object['designation'] ||$contract->scale_id != $object['scale'])
             {
             
                $previous_contract_details = $contract_detail->removeByContractId($contract->id);
             }

               $user = User::find($contract->user_id);
              if(isset($user->id) && $user->designation_id != $object['designation'])
              {


               $update_designation = $user->updateUserDesignation($contract->user_id,$object['designation']);
              }
           
            
               
                $contract->designation_id  = $object['designation']; 
                $contract->scale_id        = $object['scale']; 
                $contract->start_date      = $object['start_date'];
                $contract->end_date        = $object['end_date'];
                $contract->basic_salary    = $object['basic_salary'];
                $contract->contract_form   = $object['contract_form'];
                $contract->status          = $object['status'];
                $contract->branch_id       = $object['branch'];

                // $contract->location        = $object['location'];

                $contract->update();

                // BRANCH UPDATE
                $curr_user = $contract->user;



                if(isset($curr_user->id))
                {
                   $curr_user->branch_id = $contract->branch_id;

                   $curr_user->update();
                }

                $curr_contract_details     = $contract_detail->storeContractDetail($object,$contract->id);
           }


            return with($contract);

        });

    }


    public function getContracts($object)
    {
          return DB::transaction(function () use ($object){
            // $condition = $this->getCondition();
            // $condition = ['contracts.is_recycle' =>'0'];
            // $condition += ['designation_id' => $object['designation_id'],'scale_id' => $object['scale_id'], 'assign_allowance_id' => $object['assign_id']];
         // $contract_list = Contract::where($condition)
         // // ->join('contract_details','contract_details.contract_id','=','contracts.id')
         // // ->whereNotExists(function($query)
         // //    {
         // //        $query->select(DB::raw(1))
         // //              ->from('contract_details')
         // //              ->join('assign_allowances','assign_allowances.id','=','contract_details.assign_allowance_id')
         // //              ->whereRaw('assign_allowances.id = contract_details.assign_allowance_id');
         // //    })
         // ->get();

        // THIS PORTION FOR THE CONTRACT TABLE
        $condition = $this->getCondition();
      
        $condition += ['designation_id' => $object['designation_id'],'scale_id' => $object['scale_id'], 'status' => 'Active'];
        // END THIS PORTION FOR THE CONTRACT TABLE

        // THIS PORTION FOR THE CONTRACT DETAIL TABLE
        $other_condition = ['assign_allowance_id' => $object['assign_id']];

        $other_condition += $this->getCondition();
        // END THIS PORTION FOR THE CONTRACT TABLE

        $contract_list = Contract::where($condition)->whereNotExists( function ($query) use ($other_condition) {
              $query->select(DB::raw(1))
              ->from('contract_details')
              ->whereRaw('contracts.id = contract_details.contract_id')
              ->where($other_condition);
          })->get();

      return with($contract_list);
        });

    }

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

    }

    public function getContractByUser($id,$status_in = ['Active'])
    {
      $condition = $this->getCondition();

      $condition += ['user_id' => $id];

      return Contract::where($condition)->whereIn('status', $status_in)->first();

    }
    public function getContractList($status_in = ['Active','Deactive'])
    {
        return Contract::where($this->getCondition())->whereIn('status', $status_in)->orderBy('id','desc')->with(['user','designation','scale'])->get();
    }

    public function getNewContractList($status_in = ['Active'],$minute_contracts)
    {
        return Contract::where($this->getCondition())->whereIn('status', $status_in)->whereNotIn('id',$minute_contracts)->orderBy('id','desc')->with(['user','designation','scale'])->get();
    }



   

    // ALL FORIGN KEY RELATIONSHIP FUNCTION
    public function designation()
    {
        return $this->belongsTo(Designation::class,'designation_id')->withDefault();
    }

    public function user()
    {
        return $this->belongsTo(User::class,'user_id')->withDefault();
    }

     public function branch()
    {
        return $this->belongsTo(Branch::class,'branch_id')->withDefault();
    }


    public function scale()
    {
        return $this->belongsTo(Scale::class,'scale_id')->withDefault();
    }


    // END ALL FORIGN KEY RELATIONSHIP FUNCTION


    // Storing contract modal data

    
     

}
