<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\AssignAllowance;
use App\Contract;


class ContractDetail 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 storeContractDetail($object,$id)
    {
        return \DB::transaction(function () use ($object,$id) 
        {

        $assign_allowance = new AssignAllowance;

          //TO GET ASSIGNED ALLOWANCE LIST BY SCALE OR DESIGNATION
         $current_alloted_allowances = $assign_allowance->getAssignedAllowanceByCriteria($object['scale'],$object['designation']);

     	     $data = array();

     foreach($current_alloted_allowances as $rows)
     {
     	  
          $data[] =
          [
          	'contract_id'  => $id,
          	'assign_allowance_id' => $rows->id,
          	'amount'              => $rows->allowance->amount,
            'created_at'          => now(),
          ];

     }
        if(@count($data))
        {
           $contract_detail = new ContractDetail;

            $contract_detail->massInsertContractDetail($data);

            return 1;
        }

     

        });
    }

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



        $allowance = new Allowance;

        $curr_allowances = [];

       
         for($i = 0; $i< @count($object['allowances']); $i++)
         {
         

          $curr_allowances[$i] = $allowance->getAllowanceById($object['allowances'][$i]);


         }

           $data = array();

     foreach($curr_allowances as $rows)
     {
        
          $data[] =
          [
            'contract_id'         => $object['contract_id'],
            'assign_allowance_id' => 0,
            'amount'              => $rows->amount,
            'percentage'          => $rows->percentage,
            'allowance_id'        => $rows->id,
            'asset_id'            => 0,
            'created_at'          => now(),
          ];

     }
        if(@count($data))
        {
           $contract_detail = new ContractDetail;

            $contract_detail->massInsertContractDetail($data);

            return 1;
        }

     

        });
    }

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

        $asset       = new Asset;
        $curr_assets = [];

       
         for($i = 0; $i< @count($object['assets']); $i++)
         {
         
          $curr_assets[$i] = $asset->getAssetById($object['assets'][$i]);

         }

           $data = array();

     foreach($curr_assets as $rows)
     {
        
          $data[] =
          [
            'contract_id'         => $object['contract'],
            'assign_allowance_id' => 0,
            'amount'              => 0,
            'allowance_id'        => 0,
            'percentage'          => 0,
            'asset_id'            => $rows->id,
            'created_at'          => now(),
          ];

     }
        if(@count($data))
        {
           $contract_detail = new ContractDetail;

            $contract_detail->massInsertContractDetail($data);

            return 1;
        }

     

        });
    }
    // CONTRACT DETAIL STORE BY CONTRACT
    public function storeDetailByContract($object)
    {
        return \DB::transaction(function () use ($object) 
        {

          $assign_allowance = new AssignAllowance;

          $assign_allowance = $assign_allowance->getAssignAllowanceById($object['assign_allowance']);

          $contract_list = $object['contract_list']; 


          foreach($contract_list as $rows)
          {
              
                $data[] =
                [
                  'contract_id'  => $rows,
                  'assign_allowance_id' => $assign_allowance->id,
                  'amount'              => $assign_allowance->allowance->amount,
                  'created_at'          => now(),
                ];

          }

          if(@count($data))
          {
             $contract_detail = new ContractDetail;

              $contract_detail->massInsertContractDetail($data);

              return 1;
          }

     

        });
    }
 public function getNewContractDetailList($minute_contract_details,$minute_contracts)
 
    {
        return ContractDetail::where($this->getCondition())->whereNotIn('id',$minute_contract_details)->orderBy('id','desc')->get();
    }

    public function updateContractDetail($object,$contract_detail_id)
    {

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

            $contract_detail = ContractDetail::find($contract_detail_id);

            if(isset($contract_detail->id))
            {
       
            
             $contract_detail->amount         = $object['amount'];

             $contract_detail->update();

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

      public function uploadAssetFile($object)
    {

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

            $contract_detail = ContractDetail::find($object['id']);

            if(isset($contract_detail->id))
            {
       
            
             $contract_detail->form         = $object['asset_form'];

             $contract_detail->update();

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

    public function removeContractDetail($id)
    {
          return \DB::transaction(function () use ($id) {

            $contract_detail = ContractDetail::find($id);

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

                $contract_detail->is_recycle   = '1';

                $contract_detail->update();
            }
            
            return with($contract_detail);
        });
    }

     public function removeByContractId($contract_id)
    {
          return \DB::transaction(function () use ($contract_id) {

          $condition = $this->getCondition();
          $column_list = ['is_recycle'=> '1'];
         ContractDetail::where($condition)->update($column_list);
            
            return 1;
        });
    }
    public function massInsertContractDetail($object)
    {
        return \DB::transaction(function () use ($object)  {

            ContractDetail::insert($object);
        });
    }

    public function getContractDetailByContractId($contract_id)
    {

      $condition = $this->getCondition();
      $condition += ['contract_id'=> $contract_id];
        return ContractDetail::where($condition)->orderBy('id','desc')->with(['assignAllowance','contract'])->get();
    }

    public function getContractAssets($contract_id)
    {
      $condition = $this->getCondition();
      $condition += ['contract_id'=> $contract_id, 'assign_allowance_id' => '0','allowance_id' => '0'];
      return ContractDetail::where($condition)->orderBy('id','desc')->with(['asset'])->get();
    }

    public function getContractDetailList($contract_id_list)
    {
    
           $condition = $this->getCondition();

        return ContractDetail::where($condition)->whereIn('contract_id',$contract_id_list)->orderBy('id','desc')->get();

          

    }

    public function getContractDetailById($contract_detail_id)
    {
    
           $condition = $this->getCondition();

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

        return ContractDetail::where($condition)->first();

          

    }



     public function getContractDetailListByDate($refreshed_date,$operator)
    {
    
           $condition = $this->getCondition();

        return ContractDetail::where($condition)->whereDate('created_at',$operator,$refreshed_date)->orderBy('id','desc')->get();

          

    }



     public function assignAllowance()
    {
        return $this->belongsTo(AssignAllowance::class,'assign_allowance_id')->with(['allowance'])->withDefault();
    }

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

     public function asset()
    {
        return $this->belongsTo(Asset::class,'asset_id')->withDefault();
    }


    public function contract()
    {
        return $this->belongsTo(Contract::class,'contract_id')->withDefault();
    }






}

