<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DB;
use Crypt;
use App\Account;
use EF;
class Head extends Model
{
   
   private $generic_condition;
  

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

    public function getRemoveHeadList($nature)
    {
        return Head::where(['is_recycle' => 1, 'nature' => $nature])->orderby('id','asc')->get();
    }

    public function generateNatureWiseCode($nature)
    {
            $data = ['old_head_id' => 0, 'code' => 0];

            // FIRST CHECK OLD 
            $old_head_list = $this->getRemoveHeadList($nature);

            foreach ($old_head_list as $rows) {
                
                $data['old_head_id'] = $rows->id;

                $data['code']        = $rows->code;

                return $data;
            }
            // END

            $condition = ['nature' => $nature] + $this->getCondition();

            $nature = EF::getNatureValue($nature);

            // CHECK IF NATURE CODE IS ALREADY DEFINE OR NAT
            if($nature > 0)
            {

                $head_code = Head::where($condition)->max('code');

                if($head_code > 0)
                {
                    $data['code'] = '00'.($head_code+1);
                }
                else
                {
                    $data['code'] = $nature.'01';
                }
            }
           
            return $data;
    }

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

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

     public function getAllHeads($nature = [])
     {
        if(@count($nature) === 0)
        {
            $nature = EF::getNatureList();
        }

        return Head::where($this->getCondition())->whereIn('nature',$nature)->orderBy('code','asc')->get();
     } 

    public function getHeadByBillCategoryId($bill_category_id)
    {

        $condition = $this->getCondition();

        $condition += ['bill_category_id' => $bill_category_id];

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

    }

     public function storeHead($object)
    {

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



            $generate_code      =  $this->generateNatureWiseCode($object['nature']);

            $head               =  new head;
            $head->code         =  $generate_code['code'];
            $head->name         =  $object['name'];
            $head->nature       =  $object['nature'];
            $head->branch_id    =  $object['branch'];

            if(isset($object['flag']))
            {
                $head->flag = $object['flag'];
            }

            if(isset($object['bill_category_id']))
            {
                $head->bill_category_id = $object['bill_category_id'];

            }
            else
            {
                 $head->bill_category_id = 0;
            }


            $head->save();
            $old_head =  head::find($generate_code['old_head_id']);

            if(isset($old_head->id))
            {
                $old_head->is_recycle = '2';

                $old_head->update();
            }

            return with($head);

        });
    }


    public function updateHead($object)
    {

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

            $head = Head::find($object['head_id']);

      

            if(isset($head->id) && $this->isexsitSubhead($head->id)==0)
            {

              // $head->code            = ;
              $head->name           = $object['name'];
              // $head->nature         = $object['nature'];
              // $head->branch_id      = $object[EF::branchWhiteLabel()['module']];

              $head->update();
            }
            else{
             
              $head->name           = $object['name'];
              // $head->nature         = $object['nature'];
              $head->update();
            }

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

    
    public function removeHead($id)
    {

        return DB::transaction(function () use ($id) {

            $head = Head::find($id);


            if(isset($head->id) && $head->flag == "0" &&$this->isexsitSubhead($head->id)==0)
            {
                $head->is_recycle   = '1';
                
                $head->update();

                
            }
            
            return with($head);
        });
    }


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

        return Account::where($condition)->where('head_id', $id)->count();
    }

    public function getHeadById($id)
    {

        $condition = $this->getCondition();

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

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

    }


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

    public function generateHeadCode()
    {
        $max = Head::where($this->getCondition())->max('code');

        if($max > 0)
        {
            $max = explode('-',$max);

            $left_digit  = $max[0]+1;
            $right_digit = 0;

            $max = str_pad(($left_digit),3, '0', STR_PAD_LEFT).'-'.str_pad(($right_digit),2, '0', STR_PAD_LEFT);
        }
        else
        {
            $max = '001-00';
        }

        return  $max; 
        
    }


}
