<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * staff Class
 *
 * @package     edusol
 * @subpackage  Account_model
 * @author      Atif Alvi
 * @link        http://facebook.com/prince.atif5
 */

class Account_model extends MY_Model {

        private $i = 1;
   
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->model('user_model');
        $this->load->library('hajanasms');

        
    }
 public function getmainhead($id=0)
    {
            return  $this->db->select("*")->where('id',$id)->where('is_active',1)->get('head')->row_array();
    }
   public function printList($result = null) {
        if (count($result)) {
            echo "<ul>";

            foreach ($result as $item) {
                echo "<li>";
                echo $item['name'];
                if (count($item['level'])) {
                   $this->printList($item['level']);
                }
                echo "</li>";
            }

            echo "</ul>";
        }
    }
   public function getChildren($parent,$level = 0) {
    $query =$this->db->query("SELECT * FROM head WHERE parent_id = $parent and is_active=1")->result_array();
    $GLOBALS['level'] = $level;
    $children = array();
    
    $i = 0;
    foreach ($query as $key => $value) {
    
        $children[$i] = array();
        $children[$i]['name'] = $value['name'];
        $children[$i]['id'] = $value['id'];
        $children[$i]['opbalance']=$value['opbalance'];
        $children[$i]['cr_dr']=$value['cr_dr'];
        if(isset($level)){
                 
                for ($j=1; $j<$level; $j++) {
                        $children[$i]['level_'.$level] = $this->getChildren($value['id'],($level - 1));
                }
                
        }
        //$children[$i]['level'] = $this->getChildren($value['id']);
    $i++;
    }
    $this->i = 1;
return $children;
}
   public function getParent($head_id,$level = 0) {
    $query =$this->db->query("SELECT * FROM head WHERE id = $head_id and is_active=1")->result_array();
    
    $GLOBALS['level'] = $level;
    $children = array();
    
    $i = 0;
    foreach ($query as $key => $value) {
    
        $children[$i] = array();
        $children[$i]['name'] = $value['name'];
        $children[$i]['id'] = $value['id'];
        if(isset($level)){
                 
                for ($j=1; $j<$level; $j++) {
                        $children[$i]['level_'.$level] = $this->getParent($value['parent_id'],($level - 1));
                }
                
        }
        //$children[$i]['level'] = $this->getChildren($value['id']);
    $i++;
    }
    $this->i = 1;
return $children;
}

        public function getname($v=0)
        {
                return $this->db->query("SELECT id,name,parent_id from head where id=$v and is_active=1")->row_array();
        }
    public function getlevel2($v=0)
    {
            $result=$this->db->query("SELECT id,name from head where parent_id=$v and is_active=0")->result_array();
            $child=array();
            
            foreach ($result as $key => $value) {
            $child[$key]=array();
            $child[$key]['name']=$value['name'];
            $child[$key]['children']=getlevel2($key['id']);
            
            }
        
    }
    public function mainhead()
    {
         return   $this->db->query("SELECT id,name FROM `head` WHERE parent_id=0 and is_active='1'")->result_array();
    }
    public function getdroplevel($tbl="",$id)
    {
          return  $this->db->select("id,name")->where('parent_id',$id)->where('is_active',1)->get($tbl)->result_array();
    }
    public function update_assining($data)
    {
           $fields = array(

                            'branch'=>$data['branch'],
                            'programe_id'=>$data['prog'],
                            'project'=>$data['pro'],
                            'main_head'=>$data['main_head'],
                            'level2'=>$data['level2'],
                            'level3'=>$data['level3'],
                            'level4'=>$data['level4'],
                            'level5'=>$data['level5']
                );
                 $this->db->where('id',$data['id']);
             return $this->db->update('assign_account',$fields);
    }
    public function save_assining($data)
	{
         
              
            $fields = array(

                            'branch'=>$data['branch'],
                            'programe_id'=>$data['prog'],
                            'project'=>$data['pro'],
                            'main_head'=>$data['main_head'],
                            'level2'=>$data['level2'],
                            'level3'=>$data['level3'],
                            'level4'=>$data['level4'],
                            'level5'=>$data['level5'],
                            'bankid'=>$data['bank']
                );
              
             return $this->db->insert('assign_account',$fields);
                 
	}

            //saving data into sql
	public function save_program($data)
	{
                
               
            $fields = array(

                            'name'=>$data['name'],
                            
                );
              
             return $this->db->insert('program',$fields);
                 
	}

public function update_program($data)
	{
                
               
            $fields = array(

                            'name'=>$data['name'],
                            
                );
              $this->db->where('id',$data['id']);
             return $this->db->update('program',$fields);
                 
	}
public function delete_assign($id)
{
    $fields_class=array('is_delete'=>'1');
                       $this->db->where('id',$id);
                return $this->db->update('assign_account',$fields_class);
}
   public function delete_program($id)
   {
                    
                       $fields_class=array('is_delete'=>'1');
                       $this->db->where('id',$id);
                return $this->db->update('program',$fields_class);
   }
   public function delete_bank($id)
   {                  
                       $fields_bank=array('is_active'=>0);
                       $this->db->where('id',$id);
                return $this->db->update('bank_def',$fields_bank);
   }
    
    public function save_project($data)
    {
            $id=$data['id'];
            if($id>0){
                 $fields = array(
                        'p_name' => $data['name'], 
                        'program_id' =>$data['program'],
                );   
                $this->db->where('id',$id);
                 return $this->db->update('project',$fields);
            }else{
                $fields = array(
                        'p_name' => $data['name'], 
                        'program_id' =>$data['program'], 
                );
            return $this->db->insert('project',$fields);
    }
    }
    public function delete_project($id)
   {
                    
                       $fields=array('is_delete'=>'1');
                       $this->db->where('id',$id);
                return $this->db->update('project',$fields);
   }

   public function SearchId($tbl="",$id){
           $this->db->select("name");
           return $this->db->where('id',$id)->get($tbl)->row_array()['name'];
   }
    public function save_levels($data)
    {
            
        $id=$this->user_model->userInfo('id')['id'];
            $fields = array(
                             'name' => $data['name'], 
                             'parent_id' =>$data['main_head'] , 
                             'user_id' =>$id , 
                             'opbalance' =>$data['opbalance'] , 
                             'cr_dr' =>$data['cr/dr'],
                             
                           );
            return $this->db->insert('head',$fields);
    }
public function update_level($data)
    {
        $id=$this->user_model->userInfo('id')['id'];
            $fields = array(
                             'name' => $data['name'], 
                             'parent_id' =>$data['main_head'] , 
                             'user_id' =>$id ,
                             'opbalance' =>$data['opbalance'] , 
                             'cr_dr' =>$data['cr/dr'],
                           );
            $this->db->where('id',$data['id']);
                return $this->db->update('head',$fields);
    }
    public function update_l5($data)
    {
           
        $id=$this->user_model->userInfo('id')['id'];
        
            $fields = array(
                              
                             'user_id' =>$id ,
                             'opbalance' =>$data['opbalance'] , 
                             'cr_dr' =>$data['cr/dr'],
                           );
            $this->db->where('id',$data['id']);
            $this->db->update('head',$fields);
            return true;
    }

    public function delete_level($id)
    {

                       $fields=array('is_active'=>'0');
                       $this->db->where('id',$id);
                return $this->db->update('head',$fields);

    }
     public function del_l3($id)
    {

                       $fields=array('is_active'=>'0');
                       $this->db->where('id',$id);
                return $this->db->update('head',$fields);

    }
     public function delete_level4($id)
    {

                       $fields=array('is_delete'=>'1');
                       $this->db->where('id',$id);
                return $this->db->update('level_4',$fields);

    }
     public function delete_level5($id)
    {

                       $fields=array('is_active'=>'0');
                       $this->db->where('id',$id);
                return $this->db->update('head',$fields);

    }
    public function getlevl5assign(){
        
            $result=$this->db->query("SELECT * FROM `assign_account` where  is_delete=0 and programe_id=0 and project=0 and level5!=0 ")->result_array();
         return $result;
    }
    public function getlevl4assign(){
        
            $result=$this->db->query("SELECT * FROM `assign_account` where  is_delete=0 and programe_id=0 and level4!=0 and level5=0 ")->result_array();
         return $result;
    }
    public function getlevl3assign(){
        
            $result=$this->db->query("SELECT * FROM `assign_account` where  is_delete=0 and programe_id=0 and level3!=0 and level4=0 and level5=0 ")->result_array();
         return $result;
    }
    public function getlevl2assign(){
        
            $result=$this->db->query("SELECT * FROM `assign_account` where  is_delete=0 and programe_id=0 and project=0 and level2!=0 and level3=0 and level4=0 and level5=0 ")->result_array();
         return $result;
    }
    public function save_level3($data)
    {
            $id=$this->user_model->userInfo('id')['id'];
            if($data['id']>0){

                    $fields = array(
                             'name' => $data['name'], 
                             'parent_id' =>$data['level2'] , 
                             'user_id' =>$id , 
                             'opbalance' =>$data['opbalance'] , 
                             'cr_dr' =>$data['cr/dr'],
                           );
                           $this->db->where('id',$data['id']);
            return $this->db->update('head',$fields); 
            }else{
     
            $fields = array(
                             'name' => $data['name'], 
                            'parent_id' =>$data['level2'] , 
                             'user_id' =>$id ,
                             'opbalance' =>$data['opbalance'] , 
                             'cr_dr' =>$data['cr/dr'], 
                           );
            return $this->db->insert('head',$fields); 
                    }
    }
    public function save_level4($data)
    {
            $id=$this->user_model->userInfo('id')['id'];
            if($data['id']>0){

                    $fields = array(
                             'name' => $data['name'], 
                             'parent_id' =>$data['level3'] , 
                             'user_id' =>$id , 
                             'opbalance' =>$data['opbalance'] , 
                             'cr_dr' =>$data['cr/dr'],
                           );
                           $this->db->where('id',$data['id']);
            return $this->db->update('head',$fields); 
            }else{
     
            $fields = array(
                             'name' => $data['name'], 
                             'parent_id' =>$data['level3'] , 
                             'user_id' =>$id ,
                             'opbalance' =>$data['opbalance'] , 
                             'cr_dr' =>$data['cr/dr'],
                           );
            return $this->db->insert('head',$fields); 
                    }
    }
    public function save_level5($data)
    {
           $id=$this->user_model->userInfo('id')['id'];
            if($data['id']>0){

                    $fields = array(
                             'name' => $data['name'], 
                             'parent_id' =>$data['level4'] , 
                             'user_id' =>$id ,
                             'opbalance' =>$data['opbalance'] , 
                             'cr_dr' =>$data['cr/dr'],
                           );
                           $this->db->where('id',$data['id']);
            return $this->db->update('head',$fields); 
            }else{
     
            $fields = array(
                             'name' => $data['name'], 
                             'parent_id' =>$data['level4'] , 
                             'user_id' =>$id ,'opbalance' =>$data['opbalance'] , 
                             'cr_dr' =>$data['cr/dr'], );
            return $this->db->insert('head',$fields); 
                    }
    }
    public function All_programe()
    {
           
        $branch=$this->user_model->getbranch();
            return $this->db->query("SELECT program.id,program.name 
            FROM assign_account join program on program.id=assign_account.programe_id 
            where assign_account.branch=$branch and assign_account.project=0 and assign_account.is_delete=0 and program.is_delete=0")->result_array();
    } 

   

    public function All_project_id($id=0)
    {
            return $this->db->query("SELECT * FROM `project` WHERE  id=$id and is_delete=0")->row_array();
    } 
    public function All_programme_id($id=0)
    {
            return $this->db->query("SELECT * FROM `program` WHERE id=$id and is_delete=0")->row_array();
    } 
    public function All_project()
    {
            return $this->db->query("SELECT id,p_name FROM `project` WHERE is_delete='0'")->result_array();
    }
     public function All_level2($main=0)
    {
            
            return $this->db->query("SELECT id,name FROM head WHERE parent_id=$main")->result_array();
    }
     public function All_level3($l2=0)
    {
            return $this->db->query("SELECT id,name FROM head WHERE parent_id=$l2")->result_array();
    }
     public function All_level4($l3=0)
    {
            return $this->db->query("SELECT id,name FROM head WHERE parent_id=$l3")->result_array();
    }
     public function All_level5($l4=0)
    {
            return $this->db->query("SELECT id,name FROM head WHERE parent_id=$l4")->result_array();
    }
    public function All_mainhead()
    {
            return $this->db->query("SELECT id,name FROM `main_head` WHERE is_delete='0'")->result_array();
    }

     public function SaveTransaction_updated($data)
    {
    
           $cr;$dr;$amount;$tot=0;$nar_arr=array();$narr="";
            foreach ($data['rec'] as $key => $value) {  
              $nar_arr[]=$value['naration'];
            }
           
            $narr=implode(',',(array)$nar_arr);
           
           $curdate= date('Y-m-d H:i:s');
           
           
           $branch=$this->user_model->getbranch();
           $b_contacts = $this->db->select('contact,b_h_contact')->where('id',$branch)->get('branch')->row_array();
           if(!empty($b_contacts))
           {
              $numbers[]=$b_contacts['contact']; 
              $numbers[]=$b_contacts['b_h_contact']; 
           }
                
            //923362771110
            //923337241654
            
           $b_name = $this->db->select('name')->from('branch')->where('id',$branch)->get()->row()->name;
           
        //   var_dump('<pre>',$numbers);
        //   die();
      
        
        
           $count=count($data['rec']);
           $id=$this->user_model->userInfo('id')['id'];

            $t = microtime(true);
            $micro = sprintf("%06d",($t - floor($t)) * 1000000);
            $date = new DateTime(date('Y-m-d H:i:s.'.$micro, $t));
            $trans_id=$date->getTimestamp();

           $get_program_name=$this->db->select('*')
                               ->from('program')
                               ->where('id',$data['prog'])
                               ->get()->row()->name;

       $get_project_name=$this->db->select('*')
                               ->from('project')
                               ->where('id',$data['proj'])
                               ->get()->row()->p_name;

         foreach ($data['rec'] as $key => $value) {   
                
                       $amount=$value['cr_amount'];
                        $tot=$tot+$amount;
       $get_head_name=$this->db->select('*')
                               ->from('account_heads')
                               ->where('id',$value['level5'])
                               ->get()->row()->name;
      $get_last_blance=$this->db->select('*')
                                ->from('transction')
                                ->where('head',$value['level5'])
                                ->where('branch',$branch)
                                ->LIMIT(1)
                                ->order_by('id','desc')
                                ->get()->row();
            if(!empty($get_last_blance))
            {
                $blnc=$get_last_blance->after_transction;
            }else
            {
                $op_blnc=$this->db->select('*')
                                ->from('opening_balance')
                                ->where('head',$value['level5'])
                                ->where('branch',$branch)
                                ->get()->row();
            if(!empty($op_blnc))  
            {
                               $pp= $op_blnc->has_cr;
                              $cmot= $op_blnc->amount;
                    if($pp == 0)
                    {
                        $actl=0-$cmot;
                        $blnc=$actl;
                    }else
                    {
                        $blnc=$cmot;
                    }
            }        
            }
            if(empty($blnc))
            {
                $blnc=0;
            }

       $get_head_name=$this->db->select('*')
                               ->from('account_heads')
                               ->where('id',$value['level5'])
                               ->get()->row()->name;
                      
              $nts = $data['datee'];
$datee = str_replace('/', '-', $nts);
$date= date('Y-m-d', strtotime($datee));
                    
           $fields = array(
                    'date'=>$date,
                    'created_date'=>$curdate,
                    'type'=>"cash payment",
                    'head' =>$value['level5'],
                    'narration' =>$value['naration'],
                    'program' =>$data['prog'],
                    'project' =>$data['proj'],
                    'program_name' =>$get_program_name,
                        'project_name' =>$get_project_name,
                        'amount' =>$amount,
                        'created_by'=>$id ,
                        'branch' =>$branch,
                        'head_name' =>$get_head_name,
                        'paid_to' =>$data['paidto'],
                        'balance' =>$blnc,
                        'after_transction'=>$blnc + $amount,
                        'transction_id'=>$trans_id,
                        'voucher_no'=>$data['voucher_no'],
                        'has_cr'=>1
                        );
                        
        $this->db->insert('transction',$fields); 
        $aid[]=$this->db->insert_id();
         }
         
             
           //manualty intry
             
       $get_credit_name=$this->db->select('*')
                               ->from('account_heads')
                               ->where('id',6)
                               ->get()->row()->name; 
         
      $get_last_blance=$this->db->select('*')
                                ->from('transction')
                                ->where('head',6)
                                ->where('branch',$branch)
                                ->LIMIT(1)
                                 ->order_by('id','desc')
                                ->get()->row();
            if(!empty($get_last_blance))
            {
                $blnc=$get_last_blance->after_transction;
            }else
            {
                $op_blnc=$this->db->select('*')
                                ->from('opening_balance')
                                ->where('head',6)
                                ->where('branch',$branch)
                                ->get()->row();
             if(!empty($op_blnc))
            {
                            
                    if($op_blnc->has_cr == 0)
                    {
                        $actl=0-$op_blnc->amount;
                        $blnc=$actl;
                    }else
                    {
                        $blnc=$op_blnc->amount;
                    }
            }
            }
             if(empty($blnc))
            {
                $blnc=0;
            }
          
         $amount=$tot;
         $nts = $data['datee'];
$datee = str_replace('/', '-', $nts);
$date= date('Y-m-d', strtotime($datee));
              
         $hard_intry=array(
                    'date'=>$date,
                    'created_date'=>$curdate,
                    'type'=>"cash payment",
                    'head' =>6,
                    'head_name' =>$get_credit_name,
                    'narration' =>$narr,
                    'program' =>$data['prog'],
                    'project' =>$data['proj'],
                    'program_name' =>$get_program_name,
                    'project_name' =>$get_project_name,
                    'amount' =>$amount,
                    'created_by'=>$id ,
                    'paid_to'=>$data['paidto'],
                    'branch' =>$branch,
                    'balance' =>$blnc,
                    'after_transction'=>$blnc - $amount ,
                    'transction_id'=>$trans_id,
                    'voucher_no'=>$data['voucher_no'],
                    'has_cr'=>0
             );
          
        $this->db->insert('transction',$hard_intry); 
        $aid[]=$this->db->insert_id(); 
        
        	//Cash In Hand final balance
		 $heads_cash=$this->db->select('*')
                             ->from('account_heads')
                             ->where('has_child',0)
                             ->where('id',6)
                             ->get()->row_array();
           
        $sm_cash=0;  
        $sm_cashdr=0;
        $sm_cashcr=0; 
       if(!empty($heads_cash))
       {
           $branch=$this->user_model->getbranch();
                  
         $fetch_level_1=$this->db->select('sum(amount) as amount')
                             ->from('transction')
                             ->where('head',6)
                             ->where('branch',$branch)
                             ->where('has_cr',1)
                             ->get()->row_array()['amount'];
        
                 if(!empty($fetch_level_1))
                                {
                     $sm_cashdr=$sm_cashdr+$fetch_level_1;
                             }
         $fetch_level_cr=$this->db->select('sum(amount) as amount')
                                  ->from('transction')
                                  ->where('head',6)
                                  ->where('branch',$branch)
                                  ->where('has_cr',0)
                                  ->get()->row_array()['amount']; 
            if(!empty($fetch_level_cr))
                                {
                 $sm_cashcr=$sm_cashcr+$fetch_level_cr;
                           }                      
                           
                   $op_bln=$this->db->select('*')
                                    ->from('opening_balance')
                                    ->where('branch',$branch)
                                    ->where('head',6)
                                    ->get()->row_array();
                            if(!empty($op_bln)) 
                            {
                                if($op_bln['has_cr']==0)
                                {
                                   $sm_cashcr=$sm_cashcr+$op_bln['amount'];
                                }else
                                {
                                    $sm_cashdr=$sm_cashdr+$op_bln['amount'];
                                }
                             }
        }
        $sm_cash=($sm_cashdr-$sm_cashcr);
	
         $message = "Cash Paid to $data[paidto] of Amount:$tot\nCash In Hand:$sm_cash\n$b_name"; 
          $check = @$this->db->select('is_permit')->where('id',11)->get('tsms_permission')->row()->is_permit;
          if($check==1){
            $this->hajanasms->sendManyNumber($numbers,$message);
          }
        
       return $aid;
    }

     public function SaveTransaction_cash_receipt($data)
    {
        
           $cr;$dr;$amount;$tot=0;
           $nar_arr=array();$narr="";
            foreach ($data['rec'] as $key => $value) {  
              $nar_arr[]=$value['naration'];
            }
           
            $narr=implode(',',(array)$nar_arr);
           $curdate= date('Y-m-d H:i:s');
           
           $branch=$this->user_model->getbranch();
           $b_contacts = $this->db->select('contact,b_h_contact')->where('id',$branch)->get('branch')->row_array();
              $b_name = $this->db->select('name')->from('branch')->where('id',$branch)->get()->row()->name;
        
            if(!empty($b_contacts))
           {
              $numbers[]=$b_contacts['contact']; 
              $numbers[]=$b_contacts['b_h_contact']; 
           }     
          // var_dump('<pre>',$data['rec']);
          // die();
           $count=count($data['rec']);
           $id=$this->user_model->userInfo('id')['id'];

            $t = microtime(true);
                   $micro = sprintf("%06d",($t - floor($t)) * 1000000);
                    $date = new DateTime(date('Y-m-d H:i:s.'.$micro, $t));
                  $trans_id=$date->getTimestamp();

         $get_program_name=$this->db->select('*')
                               ->from('program')
                               ->where('id',$data['prog'])
                               ->get()->row()->name;

       $get_project_name=$this->db->select('*')
                               ->from('project')
                               ->where('id',$data['proj'])
                               ->get()->row()->p_name;

         foreach ($data['rec'] as $key => $value) {   
  
        $amount=$value['cr_amount'];
        
                        $tot=$tot+$amount;
       $get_head_name=$this->db->select('*')
                               ->from('account_heads')
                               ->where('id',$value['level5'])
                               ->get()->row()->name;
      $get_last_blance=$this->db->select('*')
                                ->from('transction')
                                ->where('head',$value['level5'])
                                ->where('branch',$branch)
                                ->LIMIT(1)
                                ->order_by('id','desc')
                                ->get()->row();
            if(!empty($get_last_blance))
            {
                $blnc=$get_last_blance->after_transction;
            }else
            {
                $op_blnc=$this->db->select('*')
                                ->from('opening_balance')
                                ->where('head',$value['level5'])
                                ->where('branch',$branch)
                                ->get()->row();
                if(!empty($op_blnc))   
                {
                               $tcl= $op_blnc->has_cr;
                               $tccl=$op_blnc->amount;
                    if($tcl == 0)
                    {
                        $actl=0-$tccl;
                        $blnc=$actl;
                    }else
                    {
                        $blnc=$tccl;
                    }
                }    
            }
             if(empty($blnc))
            {
                $blnc=0;
            }

       $get_head_name=$this->db->select('*')
                               ->from('account_heads')
                               ->where('id',$value['level5'])
                               ->get()->row()->name;
 $nts = $data['datee'];
$datee = str_replace('/', '-', $nts);
$date= date('Y-m-d', strtotime($datee));
         
            $fields = array(
                   'date'=>$date,
                   'created_date'=>$curdate,
                   'type'=>"cash receipt",
                   'head' =>$value['level5'],
                    'narration' =>$value['naration'],
                       'program' =>$data['prog'],
                        'project' =>$data['proj'],
                        'program_name' =>$get_program_name,
                        'project_name' =>$get_project_name,
                        'amount' =>$amount,
                        'created_by'=>$id ,
                        'branch' =>$branch,
                        'head_name' =>$get_head_name,
                        'paid_from' =>$data['fromto'],
                        'balance' =>$blnc,
                        'after_transction'=>$blnc - $amount ,
                        'transction_id'=>$trans_id,
                        'voucher_no'=>$data['voucher_no'],
                        'has_cr'=>0
                        );
                        
        $this->db->insert('transction',$fields); 
        $aid[]=$this->db->insert_id();
        
         }
         
         
         
          //manualty intry
             
       $get_credit_name=$this->db->select('*')
                               ->from('account_heads')
                               ->where('id',6)
                               ->get()->row()->name; 
         
      $get_last_blance=$this->db->select('*')
                                ->from('transction')
                                ->where('head',6)
                                ->where('branch',$branch)
                                ->LIMIT(1)
                                 ->order_by('id','desc')
                                ->get()->row();
            if(!empty($get_last_blance))
            {
                $blnc=$get_last_blance->after_transction;
            }else
            {
                $op_blnc=$this->db->select('*')
                                ->from('opening_balance')
                                ->where('head',6)
                                ->where('branch',$branch)
                                ->get()->row();
            if(!empty($op_blnc))     
            {
                    if($op_blnc->has_cr == 0)
                    {
                        $actl=0-$op_blnc->amount;
                        $blnc=$actl;
                    }else
                    {
                        $blnc=$op_blnc->amount;
                    }
            }
            }
             if(empty($blnc))
            {
                $blnc=0;
            }
          
         $amount=$tot;
     $nts = $data['datee'];
$datee = str_replace('/', '-', $nts);
$date= date('Y-m-d', strtotime($datee)); 
         $hard_intry=array(
                    'date'=>$date,
                    'created_date'=>$curdate,
                    'type'=>"cash receipt",
                    'head' =>6,
                    'head_name' =>$get_credit_name,
                    'narration' =>$narr,
                    'program' =>$data['prog'],
                    'project' =>$data['proj'],
                    'program_name' =>$get_program_name,
                    'project_name' =>$get_project_name,
                    'amount' =>$amount,
                    'created_by'=>$id ,
                    'paid_from'=>$data['fromto'],
                    'branch' =>$branch,
                    'balance' =>$blnc,
                    'after_transction'=>$blnc + $amount ,
                    'transction_id'=>$trans_id,
                    'voucher_no'=>$data['voucher_no'],
                    'has_cr'=>1
             );
          
        $this->db->insert('transction',$hard_intry); 
        $aid[]=$this->db->insert_id(); 
        
        //Cash In Hand final balance
		 $heads_cash=$this->db->select('*')
                             ->from('account_heads')
                             ->where('has_child',0)
                             ->where('id',6)
                             ->get()->row_array();
           
        $sm_cash=0;  
        $sm_cashdr=0;
        $sm_cashcr=0; 
       if(!empty($heads_cash))
       {
           $branch=$this->user_model->getbranch();
                  
         $fetch_level_1=$this->db->select('sum(amount) as amount')
                             ->from('transction')
                             ->where('head',6)
                             ->where('branch',$branch)
                             ->where('has_cr',1)
                             ->get()->row_array()['amount'];
        
                 if(!empty($fetch_level_1))
                                {
                     $sm_cashdr=$sm_cashdr+$fetch_level_1;
                             }
         $fetch_level_cr=$this->db->select('sum(amount) as amount')
                                  ->from('transction')
                                  ->where('head',6)
                                  ->where('branch',$branch)
                                  ->where('has_cr',0)
                                  ->get()->row_array()['amount']; 
            if(!empty($fetch_level_cr))
                                {
                 $sm_cashcr=$sm_cashcr+$fetch_level_cr;
                           }                      
                           
                   $op_bln=$this->db->select('*')
                                    ->from('opening_balance')
                                    ->where('branch',$branch)
                                    ->where('head',6)
                                    ->get()->row_array();
                            if(!empty($op_bln)) 
                            {
                                if($op_bln['has_cr']==0)
                                {
                                   $sm_cashcr=$sm_cashcr+$op_bln['amount'];
                                }else
                                {
                                    $sm_cashdr=$sm_cashdr+$op_bln['amount'];
                                }
                             }
        }
        $sm_cash=($sm_cashdr-$sm_cashcr);
         $message = "Cash Received From $data[fromto] of Amount:$tot\nCash In Hand:$sm_cash\n$b_name"; 
          $check = @$this->db->select('is_permit')->where('id',10)->get('tsms_permission')->row()->is_permit;
          if($check==1){
            $this->hajanasms->sendManyNumber($numbers,$message);
          }
          
       return $aid;
    }

      public function SaveTransaction_bank_payment($data)
    {
          $cr;$dr;$amount;$tot=0;
          $nar_arr=array();$narr="";
            foreach ($data['rec'] as $key => $value) {  
              $nar_arr[]=$value['naration'];
            }
           
            $narr=implode(',',(array)$nar_arr);
          $curdate= date('Y-m-d H:i:s');
          $branch=$this->user_model->getbranch();
             $b_name = $this->db->select('name')->from('branch')->where('id',$branch)->get()->row()->name;
        
          $b_contacts = $this->db->select('contact,b_h_contact')->where('id',$branch)->get('branch')->row_array();
          if(!empty($b_contacts))
           {
              $numbers[]=$b_contacts['contact']; 
              $numbers[]=$b_contacts['b_h_contact']; 
           }
           
          $count=count($data['rec']);
          $id=$this->user_model->userInfo('id')['id'];

          $t = microtime(true);
          $micro = sprintf("%06d",($t - floor($t)) * 1000000);
          $date = new DateTime(date('Y-m-d H:i:s.'.$micro, $t));
          $trans_id=$date->getTimestamp();

          $get_program_name=$this->db->select('*')
                               ->from('program')
                               ->where('id',$data['prog'])
                               ->get()->row()->name;

         $get_project_name=$this->db->select('*')
                               ->from('project')
                               ->where('id',$data['proj'])
                               ->get()->row()->p_name;

         $get_creditbank_name=$this->db->select('*')
                               ->from('account_heads')
                               ->where('id',$data['bank_cr'])
                               ->get()->row()->name;                       

         foreach ($data['rec'] as $key => $value) {   
        
                       $amount=$value['cr_amount'];
                    $tot=$tot+$amount;
       $get_head_name=$this->db->select('*')
                               ->from('account_heads')
                               ->where('id',$value['level5'])
                               ->get()->row()->name;
      $get_last_blance=$this->db->select('*')
                                ->from('transction')
                                ->where('head',$value['level5'])
                                ->where('branch',$branch)
                                ->LIMIT(1)
                                ->order_by('id','desc')
                                ->get()->row();
            if(!empty($get_last_blance))
            {
                $blnc=$get_last_blance->after_transction;
            }else
            {
                $op_blnc=$this->db->select('*')
                                ->from('opening_balance')
                                ->where('head',$value['level5'])
                                ->where('branch',$branch)
                                ->get()->row();
                if(!empty($op_blnc))
                {
                    if($op_blnc->has_cr == 0)
                    {
                        $actl=0-$op_blnc->amount;
                        $blnc=$actl;
                    }else
                    {
                        $blnc=$op_blnc->amount;
                    }
                }
            }
          if(empty($blnc))
            {
                $blnc=0;
            }
          $nts = $data['datee'];
$datee = str_replace('/', '-', $nts);
$date= date('Y-m-d', strtotime($datee));            
         $fields = array(
                    'date'=>$date,
                    'created_date'=>$curdate,
                    'type'=>"bank payment",
                    'head' =>$value['level5'],
                    'head_name' =>$get_head_name,
                    'narration' =>$value['naration'],
                    'program' =>$data['prog'],
                    'project' =>$data['proj'],
                    'program_name' =>$get_program_name,
                    'project_name' =>$get_project_name,
                    'amount' =>$amount,
                    'created_by'=>$id ,
                    'paid_to'=>$data['paidto'],
                    'branch' =>$branch,
                    'balance' =>$blnc,
                    'after_transction'=>$blnc + $amount ,
                    'transction_id'=>$trans_id,
                    'voucher_no'=>$data['voucher_no'],
                    'chq'=>$data['chq'],
                    'has_cr'=>1
                        );
                        
        $this->db->insert('transction',$fields); 
        $aid[]=$this->db->insert_id();
         }
         $message = "Bank Payment made to $data[paidto] of Amount:$tot\n$b_name"; 
          $check = @$this->db->select('is_permit')->where('id',17)->get('tsms_permission')->row()->is_permit;
          if($check==1){
            $this->hajanasms->sendManyNumber($numbers,$message);
          }
              //manualty intry
             
       $get_creditbank_name=$this->db->select('*')
                               ->from('account_heads')
                               ->where('id',$data['bank_cr'])
                               ->get()->row()->name; 
         
      $get_last_blance=$this->db->select('*')
                                ->from('transction')
                                ->where('head',$data['bank_cr'])
                                ->where('branch',$branch)
                                ->LIMIT(1)
                                 ->order_by('id','desc')
                                ->get()->row();
            if(!empty($get_last_blance))
            {
                $blnc=$get_last_blance->after_transction;
            }else
            {
                $op_blnc=$this->db->select('*')
                                ->from('opening_balance')
                                ->where('head',$data['bank_cr'])
                                ->where('branch',$branch)
                                ->get()->row();
                if(!empty($op_blnc))
                {
                    if($op_blnc->has_cr == 0)
                    {
                        $actl=0-$op_blnc->amount;
                        $blnc=$actl;
                    }else
                    {
                        $blnc=$op_blnc->amount;
                    }
                }    
            }
             if(empty($blnc))
            {
                $blnc=0;
            }
          
         $amount=$tot;
        $nts = $data['datee'];
$datee = str_replace('/', '-', $nts);
$date= date('Y-m-d', strtotime($datee));
   
         $hard_intry=array(
                    'date'=>$date,
                    'created_date'=>$curdate,
                    'type'=>"bank payment",
                    'head' =>$data['bank_cr'],
                    'head_name' =>$get_creditbank_name,
                    'narration' =>$narr,
                    'program' =>$data['prog'],
                    'project' =>$data['proj'],
                    'program_name' =>$get_program_name,
                    'project_name' =>$get_project_name,
                    'amount' =>$amount,
                    'created_by'=>$id ,
                    'paid_to'=>$data['paidto'],
                    'branch' =>$branch,
                    'balance' =>$blnc,
                    'after_transction'=>$blnc - $amount,
                    'transction_id'=>$trans_id,
                    'voucher_no'=>$data['voucher_no'],
                    'chq'=>$data['chq'],
                    'has_cr'=>0
             );
             
        $this->db->insert('transction',$hard_intry); 
        $aid[]=$this->db->insert_id();         
       return $aid;
    }

      public function SaveTransactions_bank_receipt($data)
    {
                
           $cr;$dr;$amount;$tot=0;
           $nar_arr=array();$narr="";
            foreach ($data['rec'] as $key => $value) {  
              $nar_arr[]=$value['naration'];
            }
           
            $narr=implode(',',(array)$nar_arr);
           $curdate= date('Y-m-d H:i:s');
           
           $branch=$this->user_model->getbranch();
              $b_name = $this->db->select('name')->from('branch')->where('id',$branch)->get()->row()->name;
        
           $b_contacts = $this->db->select('contact,b_h_contact')->where('id',$branch)->get('branch')->row_array();
           if(!empty($b_contacts))
           {
              $numbers[]=$b_contacts['contact']; 
              $numbers[]=$b_contacts['b_h_contact']; 
           }
          $count=count($data['rec']);
           $id=$this->user_model->userInfo('id')['id'];

            $t = microtime(true);
                   $micro = sprintf("%06d",($t - floor($t)) * 1000000);
                    $date = new DateTime(date('Y-m-d H:i:s.'.$micro, $t));
                  $trans_id=$date->getTimestamp();

         $get_program_name=$this->db->select('*')
                               ->from('program')
                               ->where('id',$data['prog'])
                               ->get()->row()->name;

       $get_project_name=$this->db->select('*')
                               ->from('project')
                               ->where('id',$data['proj'])
                               ->get()->row()->p_name;

         $get_debitbank_name=$this->db->select('*')
                               ->from('account_heads')
                               ->where('id',$data['bank_cr'])
                               ->get()->row()->name;                       

         foreach ($data['rec'] as $key => $value) {   
        
         $get_last_blance=$this->db->select('*')
                                ->from('transction')
                                ->where('head',$value['level5'])
                                ->where('branch',$branch)
                                ->LIMIT(1)
                                 ->order_by('id','desc')
                                ->get()->row();
            if(!empty($get_last_blance))
            {
                $blnc=$get_last_blance->after_transction;
            }else
            {
                $op_blnc=$this->db->select('*')
                                ->from('opening_balance')
                                ->where('head',$value['level5'])
                                ->where('branch',$branch)
                                ->get()->row();
                if(!empty($op_blnc))
                {
                    if($op_blnc->has_cr == 0)
                    {
                        $actl=0-$op_blnc->amount;
                        $blnc=$actl;
                    }else
                    {
                        $blnc=$op_blnc->amount;
                    }
                }
            }
             if(empty($blnc))
            {
                $blnc=0;
            }
            
                       $amount=$value['cr_amount'];
                      $tot=$tot+$amount;
       $get_head_name=$this->db->select('*')
                               ->from('account_heads')
                               ->where('id',$value['level5'])
                               ->get()->row()->name;

   $nts = $data['datee'];
$datee = str_replace('/', '-', $nts);
$date= date('Y-m-d', strtotime($datee));
   
            $fields = array(
                    'date'=>$date,
                    'created_date'=>$curdate,
                    'type'=>"bank receipt",
                    'head' =>$value['level5'],
                    'narration' =>$value['naration'],
                    'program' =>$data['prog'],
                    'project' =>$data['proj'],
                    'program_name' =>$get_program_name,
                    'project_name' =>$get_project_name,
                    'amount' =>$amount,
                    'paid_from'=>$data['fromto'],
                    'created_by'=>$id ,
                    'branch' =>$branch,
                    'head_name' =>$get_head_name,
                    'balance' =>$blnc,
                    'after_transction'=>$blnc - $amount ,
                    'transction_id'=>$trans_id,
                    'voucher_no'=>$data['voucher_no'],
                    'chq'=>$data['chq'],
                    'has_cr'=>0
                        );
                        
        $this->db->insert('transction',$fields); 
        $aid[]=$this->db->insert_id();
         }
          $message = "Bank Recieved from $data[fromto] of Amount:$tot\n$b_name"; 
          $check = @$this->db->select('is_permit')->where('id',18)->get('tsms_permission')->row()->is_permit;
          if($check==1){
            $this->hajanasms->sendManyNumber($numbers,$message);
          }
        
          //manualty intry
             
       $get_debitbank_name=$this->db->select('*')
                               ->from('account_heads')
                               ->where('id',$data['bank_cr'])
                               ->get()->row()->name; 
         
      $get_last_blance=$this->db->select('*')
                                ->from('transction')
                                ->where('head',$data['bank_cr'])
                                ->where('branch',$branch)
                                ->LIMIT(1)
                                 ->order_by('id','desc')
                                ->get()->row();
            if(!empty($get_last_blance))
            {
                $blnc=$get_last_blance->after_transction;
            }else
            {
                $op_blnc=$this->db->select('*')
                                ->from('opening_balance')
                                ->where('head',$data['bank_cr'])
                                ->where('branch',$branch)
                                ->get()->row();
                if(!empty($op_blnc))  
                {
                    if($op_blnc->has_cr == 0)
                    {
                        $actl=0-$op_blnc->amount;
                        $blnc=$actl;
                    }else
                    {
                        $blnc=$op_blnc->amount;
                    }
                }    
            }
             if(empty($blnc))
            {
                $blnc=0;
            }
          
         $amount=$tot;
        $nts = $data['datee'];
$datee = str_replace('/', '-', $nts);
$date= date('Y-m-d', strtotime($datee));
   
         $hard_intry=array(
                    'date'=>$date,
                    'created_date'=>$curdate,
                    'type'=>"bank receipt",
                    'head' =>$data['bank_cr'],
                    'head_name' =>$get_debitbank_name,
                    'narration' =>$narr,
                    'program' =>$data['prog'],
                    'project' =>$data['proj'],
                    'program_name' =>$get_program_name,
                    'project_name' =>$get_project_name,
                    'amount' =>$amount,
                    'created_by'=>$id ,
                    'paid_from'=>$data['fromto'],
                    'branch' =>$branch,
                    'balance' =>$blnc,
                    'after_transction'=>$blnc + $amount ,
                    'transction_id'=>$trans_id,
                    'voucher_no'=>$data['voucher_no'],
                    'chq'=>$data['chq'],
                    'has_cr'=>1
             );
             
        $this->db->insert('transction',$hard_intry); 
        $aid[]=$this->db->insert_id();         
        
       return $aid;
    }

    public function SaveTransactions_journal_voucher($data)
    {
        // echo "<pre>";print_r($data);exit;
            $cr;$dr;$amount;$aid=array();
           $curdate= date('Y-m-d H:i:s');
           $branch=$this->user_model->getbranch();
              $b_name = $this->db->select('name')->from('branch')->where('id',$branch)->get()->row()->name;
        
           $b_contacts = $this->db->select('contact,b_h_contact')->where('id',$branch)->get('branch')->row_array();
           if(!empty($b_contacts))
           {
              $numbers[]=$b_contacts['contact']; 
              $numbers[]=$b_contacts['b_h_contact']; 
           }
           $count=count($data['rec']);
           $id=$this->user_model->userInfo('id')['id'];

            $t = microtime(true);
                   $micro = sprintf("%06d",($t - floor($t)) * 1000000);
                    $date = new DateTime(date('Y-m-d H:i:s.'.$micro, $t));
                  $trans_id=$date->getTimestamp();

         $get_program_name=$this->db->select('*')
                               ->from('program')
                               ->where('id',$data['prog'])
                               ->get()->row()->name;

       $get_project_name=$this->db->select('*')
                               ->from('project')
                               ->where('id',$data['proj'])
                               ->get()->row()->p_name;                     
   
         foreach ($data['rec'] as $key => $value) {   
  
             if($value['type']=="D"){
   
                   $amount=$value['dr_amount'];
                
                 $get_last_blance=$this->db->select('*')
                                ->from('transction')
                                ->where('head',$value['level5'])
                                ->where('branch',$branch)
                                ->LIMIT(1)
                                 ->order_by('id','desc')
                                ->get()->row();
                if(!empty($get_last_blance))
                {
                   $blnc=$get_last_blance->after_transction;
                   $after_balance=$blnc+$amount;
               }else
               {
                $op_blnc=$this->db->select('*')
                                ->from('opening_balance')
                                ->where('head',$value['level5'])
                                ->where('branch',$branch)
                                ->get()->row();
                    if(!empty($op_blnc))  
                    {
                    if($op_blnc->has_cr == 0)
                    {
                        $actl=0-$op_blnc->amount;
                        $blnc=$actl;
                         $after_balance=$blnc+$amount;
                    }else
                    {
                        $blnc=$op_blnc->amount;
                         $after_balance=$blnc+$amount;
                    }
                    }
                 }
            
               }
               else if($value['type']=="C"){
                       $amount=$value['cr_amount'];
                         $get_last_blance=$this->db->select('*')
                                ->from('transction')
                                ->where('head',$value['level5'])
                                ->where('branch',$branch)
                                ->LIMIT(1)
                                 ->order_by('id','desc')
                                ->get()->row();
                if(!empty($get_last_blance))
                {
                   $blnc=$get_last_blance->after_transction;
                   $after_balance=$blnc-$amount;
               }else
               {
                $op_blnc=$this->db->select('*')
                                ->from('opening_balance')
                                ->where('head',$value['level5'])
                                ->where('branch',$branch)
                                ->get()->row();
                if(!empty($op_blnc))  
                    {                
                               $cl= $op_blnc->has_cr;
                               $cll=$op_blnc->amount;
                    if($cl == 0)
                    {
                        $actl=0-$cll;
                        $blnc=$actl;
                         $after_balance=$blnc-$amount;
                    }else
                    {
                        $blnc=$cll;
                         $after_balance=$blnc-$amount;
                    }
                    }
                 }
               }

        if(empty($blnc))
        {
            $blnc=0;
        }
        if(empty($after_balance))
        {
            $after_balance=0;
        }
            if($value['type']=="D"){
         
           $get_head_name=$this->db->select('*')
                               ->from('account_heads')
                               ->where('id',$value['level5'])
                               ->get()->row()->name;
                $nts = $data['datee'];
$datee = str_replace('/', '-', $nts);
$date= date('Y-m-d', strtotime($datee));
                 
                     $fields = array(
                    'date'=>$date,
                    'created_date'=>$curdate,
                    'type'=>"journal voucher",
                    'head' =>$value['level5'],
                    'narration' =>$value['naration'],
                    'program' =>$data['prog'],
                    'project' =>$data['proj'],
                    'program_name' =>$get_program_name,
                    'project_name' =>$get_project_name,
                    'amount' =>$amount,
                    'created_by'=>$id ,
                    'branch' =>$branch,
                    'head_name' =>$get_head_name,
                    'balance' =>$blnc,
                    'after_transction'=>$after_balance,
                    'transction_id'=>$trans_id,
                    'voucher_no'=>$data['voucher_no'],
                    'has_cr'=>1
                        );
                        
        $this->db->insert('transction',$fields); 
        $aid[]=$this->db->insert_id();
        
               }
               else if($value['type']=="C"){
       $get_head_name=$this->db->select('*')
                               ->from('account_heads')
                               ->where('id',$value['level5'])
                               ->get()->row()->name;
          $nts = $data['datee'];
$datee = str_replace('/', '-', $nts);
$date= date('Y-m-d', strtotime($datee));                  
                  $fields = array(
                    'date'=>$date,
                    'created_date'=>$curdate,
                    'type'=>"journal voucher",
                    'head' =>$value['level5'],
                    'narration' =>$value['naration'],
                    'program' =>$data['prog'],
                    'project' =>$data['proj'],
                    'program_name' =>$get_program_name,
                    'project_name' =>$get_project_name,
                    'amount' =>$amount,
                    'created_by'=>$id ,
                    'branch' =>$branch,
                    'head_name' =>$get_head_name,
                    'balance' =>$blnc,
                    'after_transction'=>$after_balance,
                    'voucher_no'=>$data['voucher_no'],
                    'transction_id'=>$trans_id,
                    'has_cr'=>0
                        );
                        
        $this->db->insert('transction',$fields); 
        $aid[]=$this->db->insert_id();
        
                     
               }
         
         }
         $message = "Journal Voucher of Amount $data[total] is passed From\n$b_name"; 
          $check = @$this->db->select('is_permit')->where('id',12)->get('tsms_permission')->row()->is_permit;
          if($check==1){
            $this->hajanasms->sendManyNumber($numbers,$message);
          }
       
       return $aid;
    }

      public function SaveTransactions_updated_petty_cash($data)
    {
           $cr;$dr;$amount;$tot=0;
           $nar_arr=array();$narr="";
            foreach ($data['rec'] as $key => $value) {  
              $nar_arr[]=$value['naration'];
            }
           
            $narr=implode(',',(array)$nar_arr);
           $curdate= date('Y-m-d H:i:s');
           $branch=$this->user_model->getbranch();
           $count=count($data['rec']);
           $id=$this->user_model->userInfo('id')['id'];

            $t = microtime(true);
                   $micro = sprintf("%06d",($t - floor($t)) * 1000000);
                    $date = new DateTime(date('Y-m-d H:i:s.'.$micro, $t));
                  $trans_id=$date->getTimestamp();

         $get_program_name=$this->db->select('*')
                               ->from('program')
                               ->where('id',$data['prog'])
                               ->get()->row()->name;

       $get_project_name=$this->db->select('*')
                               ->from('project')
                               ->where('id',$data['proj'])
                               ->get()->row()->p_name;                      

         foreach ($data['rec'] as $key => $value) {   
        
                       $amount=$value['cr_amount'];
                      $tot=$tot+$amount;
                      
                       $get_last_blance=$this->db->select('*')
                                ->from('transction')
                                ->where('head',$value['level5'])
                                ->where('branch',$branch)
                                ->LIMIT(1)
                                 ->order_by('id','desc')
                                ->get()->row();
            if(!empty($get_last_blance))
            {
                $blnc=$get_last_blance->after_transction;
            }else
            {
                $op_blnc=$this->db->select('*')
                                ->from('opening_balance')
                                ->where('head',$value['level5'])
                                ->where('branch',$branch)
                                ->get()->row();
                if(!empty($op_blnc)) 
                {
                    if($op_blnc->has_cr == 0)
                    {
                        $actl=0-$op_blnc->amount;
                        $blnc=$actl;
                    }else
                    {
                        $blnc=$op_blnc->amount;
                    }
                }        
            }
            if(empty($blnc))
            {
                $blnc=0;
            }
       $get_head_name=$this->db->select('*')
                               ->from('account_heads')
                               ->where('id',$value['level5'])
                               ->get()->row()->name;
               $nts = $data['datee'];
$datee = str_replace('/', '-', $nts);
$date= date('Y-m-d', strtotime($datee));
                  
         $fields = array(
                    'date'=>$date,
                    'created_date'=>$curdate,
                     'type'=>"petty cash",
                     'head' =>$value['level5'],
                     'narration' =>$value['naration'],
                     'program' =>$data['prog'],
                     'project' =>$data['proj'],
                     'program_name' =>$get_program_name,
                     'project_name' =>$get_project_name,
                     'amount' =>$amount,
                     'created_by'=>$id ,
                     'branch' =>$branch,
                     'head_name' =>$get_head_name,
                     'balance' =>$blnc,
                     'after_transction'=>$blnc+$amount,
                     'transction_id'=>$trans_id,
                     'paid_to'=>$data['paidto'],
                     'voucher_no'=>$data['voucher_no'],
                     'has_cr'=>1
                        );
                        
        $this->db->insert('transction',$fields); 
        $aid[]=$this->db->insert_id();
         }
         
          //manualty intry
             
       $get_name=$this->db->select('*')
                               ->from('account_heads')
                               ->where('id',7)
                               ->get()->row()->name; 
         
      $get_last_blance=$this->db->select('*')
                                ->from('transction')
                                ->where('head',7)
                                ->where('branch',$branch)
                                ->LIMIT(1)
                                 ->order_by('id','desc')
                                ->get()->row();
            if(!empty($get_last_blance))
            {
                $blnc=$get_last_blance->after_transction;
            }else
            {
                $op_blnc=$this->db->select('*')
                                ->from('opening_balance')
                                ->where('head',7)
                                ->where('branch',$branch)
                                ->get()->row();
                if(!empty($op_blnc)) 
                {
                    if($op_blnc->has_cr == 0)
                    {
                        $actl=0-$op_blnc->amount;
                        $blnc=$actl;
                    }else
                    {
                        $blnc=$op_blnc->amount;
                    }
                }
            }
          if(empty($blnc))
          {
              $blnc=0;
          }
         $amount=$tot;
       $nts = $data['datee'];
$datee = str_replace('/', '-', $nts);
$date= date('Y-m-d', strtotime($datee));
   
         $hard_intry=array(
                    'date'=>$date,
                    'created_date'=>$curdate,
                    'type'=>"petty cash",
                    'head' =>7,
                    'head_name' =>$get_name,
                    'narration' =>$narr,
                    'program' =>$data['prog'],
                    'project' =>$data['proj'],
                    'program_name' =>$get_program_name,
                    'project_name' =>$get_project_name,
                    'amount' =>$amount,
                    'created_by'=>$id ,
                    'paid_from'=>$data['paidto'],
                    'branch' =>$branch,
                    'balance' =>$blnc,
                    'after_transction'=>$blnc - $amount ,
                    'transction_id'=>$trans_id,
                     'paid_to'=>$data['paidto'],
                     'voucher_no'=>$data['voucher_no'],
                     'has_cr'=>0
             );
             
        $this->db->insert('transction',$hard_intry); 
        $aid[]=$this->db->insert_id();         
       return $aid;
    }


    public function SaveTransaction($data)
    {
             
           $cr;$dr;$amount;
           $date= date('Y-m-d H:i:s');
           $branch=$this->user_model->getbranch();
           $count=count($data['rec']);
           $id=$this->user_model->userInfo('id')['id'];
           
        $fields = array(   'type'=>$data['type'],
                           'voucher_no'=>$data['voucher'],
                           'cheque_no'=>$data['cheque'],
                            'branch'=>$branch,
                         'name'=>$data['to'],
                         'project_id'=>$data['proj'],
                        'date'=>$data['date'],
                         'created_by'=>$id ,
                         'created_date'=>$date);
                        
       $this->db->insert('trans',$fields);
        $aid=$this->db->insert_id();
       $bnkamount=0;
       
       
       foreach ($data['rec'] as $key => $value) {
              
               if($value['type']=="D"){
                       $dr=$value['level5'];
                       $bankid=$dr;
                       $cr=0;
                       $amount=$value['dr_amount'];
               }else if($value['type']=="C"){
                       $cr=$value['level5'];
                       $bankid=$cr;
                       $dr=0;
                       $amount=$value['cr_amount'];
               }
               if($data['type']=="cash payment" || $data['type']=="bank payment"){
              
               if($dr=="81"){
                $cash=$this->db->query("SELECT * FROM `head` WHERE id=$dr and is_active=1")->row_array();
                $cash['balance']=$cash['balance']+$amount;
                if($cash['cr_dr']=="null"){
                        $update = array('balance' =>$cash['balance'],'cr_dr'=>"dr" );
                }else
                {
                        $update = array('balance' =>$cash['balance'] );
                }
                $this->db->where('id',$dr);
                $this->db->update('head',$update);
               }
               
               }
               
      
       $fields = array(
                        'cr' =>$cr,
                       'level5' =>$value['level5'],
                       'dr' =>$dr,
                       'naration' =>$value['naration'],
                       'amount' =>$amount,
                       'transaction_id' =>$aid,
                        );
                        
        $this->db->insert('transaction_detail',$fields); 
         
       }
       
       if($data['type']=="cash payment"){
                       
        $cash=$this->db->query('SELECT * FROM `head` WHERE id=80 and is_active=1')->row_array();
        $cash['balance']=$cash['balance']-$data['total'];

        if($cash['cr_dr']=="null"){
                $update = array('balance' =>$cash['balance'],'cr_dr'=>"cr" );
        }else
        {
                $update = array('balance' =>$cash['balance'] );
        }
        $this->db->where('id',$cash['id']);
        $this->db->update('head',$update);
        }else if($data['type']=="cash receipt"){
                $cash=$this->db->query('SELECT * FROM `head` WHERE id=80 and is_active=1')->row_array();
                $cash['balance']=$cash['balance']+$data['total'];
                if($cash['cr_dr']=="null"){
                $update = array('balance' =>$cash['balance'],'cr_dr'=>"dr" );
                }else{
                         $update = array('balance' =>$cash['balance'] );
                       }
                       $update = array('balance' =>$cash['balance'] );
                       $this->db->where('id',$cash['id']);
                       $this->db->update('head',$update);
               }elseif($data['type']=="bank payment"){
                       $bank=$this->db->query("SELECT * FROM `head` WHERE id=$bankid and is_active=1")->row_array();
                       $bank['balance']=$bank['balance']-$data['total'];
                        if($bank['cr_dr']=="null"){
                         $update = array('balance' =>$bank['balance'],'cr_dr'=>"cr" );
                       }else{
                         $update = array('balance' =>$bank['balance'] );
                       }
                       
                       $this->db->where('id',$bankid);
                       $this->db->update('head',$update);
               }else if($data['type']=="bank receipt"){
                       $bank=$this->db->query("SELECT * FROM `head` WHERE id=$bankid and is_active=1")->row_array();
                       $bank['balance']=$bank['balance']+$data['total'];
                        if($bank['cr_dr']=="null"){
                         $update = array('balance' =>$bank['balance'],'cr_dr'=>"dr" );
                       }else{
                         $update = array('balance' =>$bank['balance'] );
                       }
                       $this->db->where('id',$bankid);
                       $this->db->update('head',$update);
               }else if($data['type']=="petty cash"){
                      $bank=$this->db->query("SELECT * FROM `head` WHERE id=81 and is_active=1")->row_array();
                       $bank['balance']=$bank['balance']-$data['total'];
                        if($bank['cr_dr']=="null"){
                         $update = array('balance' =>$bank['balance'],'cr_dr'=>"dr" );
                       }else{
                         $update = array('balance' =>$bank['balance'] );
                       }
                       $this->db->where('id',81);
                       $this->db->update('head',$update);
               }else if($data['type']=="journal voucher"){
                       if($value['type']=="D"){
                               $cr=null;
                               $dr=$value['level5'];
                               $amount=$value['dr_amount'];
                       }else if($value['type']=="C") {
                                $cr=$value['level5'];
                               $dr=null;
                               $amount=$value['cr_amount'];
                       }
               }
       
       return $aid;
    }
    public function search_junction($id=0)
    {
   
        $this->db->select('*');
        $this->db->join('trans','trans.id=transaction_detail.transaction_id');
       return $this->db->where('transaction_id',$id)->where('type!=','petty cash')->get('transaction_detail')->result_array();
    }
    public function search_junctionjournal($id=0)
    {
        $this->db->select('transaction_detail.*');
        $this->db->join('trans','trans.id=transaction_detail.transaction_id');
       return $this->db->where('transaction_id',$id)->get('transaction_detail')->result_array();
    }
    
   public function save_cash_deposit($data,$url="NULL")
    {
           $date= date('Y-m-d H:i:s');
           $branch=$this->user_model->getbranch();
           $id=$this->user_model->userInfo('id')['id'];
        $fields = array('project_id' =>$data['project'] ,
                         'main_head_id'=>$data['main_head'] ,
                         'level_2_id'=>$data['level2'] ,
                         'level_3_id'=>$data['level3'],
                         'level4'=>$data['level4'],
                         'level5'=>$data['level5'],
                         'submit_by'=>$data['submit_by'],
                         'bank_id'=>$data['bank'],
                         'slip_no'=>$data['slip_no'],
                         'slip_pic'=>$url,
                         'branch'=>$branch,
                         'amount'=>$data['amount'],
                         'description'=>$data['description'],
                         'date'=>$data['date'],
                         'created_by'=>$id ,'created_date'=>$date);
        $balance=$this->db->select('o_balance')->where('id',$data['bank'])->get('bank_def')->result_array()[0]['o_balance'];
       $newbalance=$balance+$data['amount'];
       $fieldsbank=array('o_balance'=>$newbalance);
       $this->db->where('id',$data['bank']);
 	$this->db->update('bank_def',$fieldsbank);
       $this->db->insert('cash_deposit',$fields);
       $aid=$this->db->insert_id();
      $history=array('Slip_name'=>"Cash deposit",
      		'befor_add_amount'=>$balance,
      		'new_amount_add/sub'=>$data['amount'],
      		'newbalnce'=>$newbalance,
      		'bank_id'=>$data['bank']);
      $this->db->insert('balance_history',$history);	
       return $aid;
    }
    
    public function All_recipt($limit,$offset)
    {
        $branch=$this->user_model->getbranch();
        $this->db->select("*"); 
        $this->db->from('trans');
        $this->db->where('type',"cash payment")->where('branch',$branch);
        $query = $this->db->limit($limit,$offset)->get()->result_array(); 
        
        return $query;     
    }
    public function All_pety($id="",$last_id="")
    {
         $branch=$this->user_model->getbranch();
        $this->db->select("*"); 
        $this->db->from('transction');
        $this->db->where('type',"petty cash")->where('branch',$branch);
        if(!empty($id) && !empty($last_id)){
        $this->db->where('id >',$last_id);
                // ->where('id <=',$id);
        }
        $query = $this->db->get()->result_array(); 
        return $query;     
    }
    public function All_pety_show()
    {
        
        
        $branch=$this->user_model->getbranch();
        $this->db->select("*"); 
        $this->db->from('transction');
        $this->db->where('type',"petty cash")
                 ->where('is_checked',0)
                 ->where('head !=',7)
                 ->group_by('transction_id')
                // ->order_by('transction_id','desc')
                 ->where('branch',$branch);
        
        $query = $this->db->get()->result_array(); 
    //   var_dump('<pre>',$query);
    //   die();
        return $query;     
    }
    public function All_vouc($limit,$offset)
    {
        $branch=$this->user_model->getbranch();
        $this->db->select("*"); 
        $this->db->from('trans');
        $this->db->where('type',"cash receipt")->where('branch',$branch);
        $query = $this->db->limit($limit,$offset)->get()->result_array(); 
        return $query;   
    }
    public function All_Jv($limit,$offset,$branch=0)
    {
        if($branch==0){
                 $this->db->select("*"); 
        $this->db->from('trans');
        $this->db->where('type',"journal voucher");
        $query = $this->db->limit($limit,$offset)->get()->result_array();

        }else{
        $this->db->select("*"); 
        $this->db->from('trans');
        $this->db->where('type',"journal voucher")->where('branch',$branch);
        $query = $this->db->limit($limit,$offset)->get()->result_array(); 
            }
        return $query;     
    }
    public function AllTrans($limit,$offset)
    {
        $branch=$this->user_model->getbranch();
        $this->db->select("*"); 
        $this->db->from('trans');
        $this->db->where('branch',$branch);
        $query=$this->db->limit($limit,$offset)->get()->result_array(); 
        return $query;       
    }
    public function Alltransaction()
    {
        $branch=$this->user_model->getbranch();
        $this->db->select("*"); 
        $this->db->from('trans');
        $this->db->where('branch',$branch);
        $query=$this->db->get()->result_array(); 
        return $query;       
    }
    public function main_head_trans($main=0)
    {
        $branch=$this->user_model->getbranch();
        $this->db->select("*"); 
        $this->db->from('trans');
        $this->db->where('branch',$branch)->where('main_head',$main);
        $query=$this->db->get()->result_array(); 
        return $query;    
    }
    public function l2_trans($main=0,$l2=0)
    {
        $branch=$this->user_model->getbranch();
        $this->db->select("trans.*"); 
        $this->db->from('trans');
        $this->db->join('transaction_detail','trans.id=transaction_detail.transaction_id');
        $this->db->where('trans.branch',$branch)->where('trans.main_head',$main)->where('transaction_detail.level2',$l2);
        $this->db->group_by('trans.id'); 
        $query=$this->db->get()->result_array(); 
        return $query;
    }
    public function l3_trans($main=0,$l2=0,$l3=0)
    {
        $branch=$this->user_model->getbranch();
        $this->db->select("trans.*"); 
        $this->db->from('trans');
        $this->db->join('transaction_detail','trans.id=transaction_detail.transaction_id');
        $this->db->where('trans.branch',$branch)->where('trans.main_head',$main)->where('transaction_detail.level2',$l2);
        $this->db->where('transaction_detail.level3',$l3);
        $this->db->group_by('trans.id'); 
        $query=$this->db->get()->result_array(); 
        return $query;
    }
    public function l4_trans($main=0,$l2=0,$l3=0,$l4=0)
    {
        $branch=$this->user_model->getbranch();
        $this->db->select("trans.*"); 
        $this->db->from('trans');
        $this->db->join('transaction_detail','trans.id=transaction_detail.transaction_id');
        $this->db->where('trans.branch',$branch)->where('trans.main_head',$main)->where('transaction_detail.level2',$l2);
        $this->db->where('transaction_detail.level3',$l3)->where('transaction_detail.level4',$l4);
        $this->db->group_by('trans.id'); 
        $query=$this->db->get()->result_array(); 
        return $query;
    }
    public function l5_trans($l5=0)
    {
        $branch=$this->user_model->getbranch();
        $this->db->select("trans.*"); 
        $this->db->from('trans');
        $this->db->join('transaction_detail','trans.id=transaction_detail.transaction_id');
        $this->db->where('trans.branch',$branch)->where('transaction_detail.dr',$l5)->or_where('transaction_detail.cr',$l5);
        $this->db->not_like('trans.type','petty');
        $this->db->not_like('trans.type','petty');
        $this->db->group_by('trans.id'); 
        $query=$this->db->get()->result_array(); 
        return $query;
    }
    public function getopeningblnce($id=0)
    {
            $this->db->select('opbalance,cr_dr');
            $this->db->from('head');
            $this->db->where('id',$id);
            return $this->db->get()->row_array();
    }
    //=============================Bank Defination================================//
    
    public function savebank($data)
    {
           $id =$this->user_model->userinfo('id')['id'];
           $branch=$this->user_model->getbranch();
           $date=date('Y-m-d');
           
           $bank_field = array(
                           'b_name' =>$data['bnk_name'] ,
                           'b_code'=>$data['b_code'] ,
                           'Account_no'=>$data['a_num'],
                           'title'=>$data['title'] ,
                           'purpose'=>$data['purpose'] ,
                           'user_id'=>$id,
                           'branch'=>$branch,
                           'creat_date'=>$date
                                );
             
                                if($data['id']>0)
                                {
                                    $get_nm=$this->db->select('*')
                                                     ->from('bank_def')
                                                     ->where('id',$data['id'])
                                                     ->get()->row()->b_name;
                                   
                                    $get_head_id=$this->db->select('*')
                                                     ->from('account_heads')
                                                     ->where('name',$get_nm)
                                                     ->get()->row()->id;      
                            $up_heaad=array(
                                'name'=>$data['bnk_name']
                                );
                              
                              $this->db->where("id",$get_head_id);
                             $bool=$this->db->update('account_heads',$up_heaad);  
                                                     
                                $this->db->where("id",$data['id']);
                                $bool=$this->db->update('bank_def',$bank_field);
                            
                                }
                                else
                                {
                                $bool=$this->create('bank_def',$bank_field);
                                 $cod=$this->db->select('*')
                                               ->from('account_heads')
                                               ->where('id',5)
                                               ->get()->row_array();
                                     $code_prnt=$cod['code'];            
                                     if($cod['has_child']==0)
                                      {
                                        $code=$code_prnt."-01";
                                       }else
                                      {
                                       $count_childs=$this->db->select('id')
                                                              ->from('account_heads')
                                                               ->where('parent',5)
                                                              ->get()->result_array();
                                           $totl=count($count_childs)+1;
                  
                                          $code=$code_prnt."-0".$totl;           
                                      } 
                                $add_head=array(
                                'name'=>$data['bnk_name'],
                                'parent'=>5,
                                'has_child'=>0,
                                'most_parent'=>1,
                                'code'=>$code,
                                'description'=>$data['purpose']
                            );
                            $this->db->insert('account_heads',$add_head);
                            $up_data=array(
                                    'has_child'=>1
                            );
                            $this->db->where('id',5)
                                     ->update('account_heads',$up_data);
                                }

        return $bool;
 
    }
    public function update_bank($data=array())
    {
      $fields = array('o_balance' =>$data['opbalance'],'balance'=>$data['opbalance'] );
      $this->db->where('id',$data['id']);
      $this->db->update('bank_def',$fields);
      return true;      
    }
    public function GetBankId($id=0)
    {
            $branch=$this->user_model->getbranch();
            return $this->db->select('*')->from('bank_def')->where('id',$id)->get()->row_array();
    }
    public function count()
    {
            $branch=$this->user_model->getbranch();
            $count=$this->db->select('*')->from('bank_def')->where('branch',$branch)->get()->result_array();
            $count=count($count);
            return $count;
    }
    
    public function bank_list($limit=0,$offset=0)
    {
           
           $this->db->select('*');
           $this->db->from('bank_def');
           return $this->db->limit($limit,$offset)->get()->result_array();
    }
    public function bank_list_local($limit=0,$offset=0)
    {
           $branch=$this->user_model->getbranch();
           $this->db->select('bank_def.*');
           $this->db->from('assign_account');
           $this->db->join('bank_def','bank_def.id=assign_account.bankid');
           $this->db->where('bankid!=',0)->where('assign_account.branch',$branch)->where('is_delete',0);
           return $this->db->limit($limit,$offset)->get()->result_array();
    }
    public function all($value=array())
    {    $branch=$this->user_model->getbranch();
        if($value==null){
         $this->db->select('*');
           $this->db->from('bank_def')->where('branch',$branch);
           return $this->db->get()->result_array();
            }else{
               return  $this->db->query("SELECT bank_def.b_name,bank_def.b_code,bank_def.id,bank_def.Account_no FROM `assign_account` join bank_def on assign_account.bankid=bank_def.id where bank_def.is_active=1 and assign_account.branch=$branch and assign_account.is_delete=0 ")->result_array();
           
            }
    }
    //==============================Bank Payment=====================================//
   
    public function All_payment($limit=0,$offset=0)
    {
         $branch=$this->user_model->getbranch();
        $this->db->select("*"); 
        $this->db->from('trans');
        $this->db->where('type',"bank payment")->where('branch',$branch);
        $query = $this->db->limit($limit,$offset)->get()->result_array();
        return $query;     
    }
    //==============================Bank Recipt=====================================//
    
    public function All_bankrecipt($limit=0,$offset=0)
    {
             $branch=$this->user_model->getbranch();
        $this->db->select("*"); 
        $this->db->from('trans');
        $this->db->where('type',"bank receipt")->where('branch',$branch);
        $query = $this->db->limit($limit,$offset)->get()->result_array();
        return $query;     
    }
     public function All_cashdeposit($limit=0,$offset=0)
    {
             $branch=$this->user_model->getbranch();
        $this->db->select("*"); 
        $this->db->from('trans');
        $this->db->where('type',"Cash deposite")->where('branch',$branch);
        $query = $this->db->limit($limit,$offset)->get()->result_array();
        return $query;     
    }
   


    public function search($t="",$id)
    {
       return  $this->db->from($t)->where('id',$id)->get()->row_array();
    }
    public function searchhistory($t="",$id)
    {
       return  $this->db->from($t)->where('last_id',$id)->get()->row_array();
    }
    public function voucher_history()
    {
        return  $this->db->from('voucher_history')->get()->result_array();
            
    }
    public function print_invoce($t="",$id,$col=array())
    {
        return $this->db->select($col)->from($t)->where('id',$id)->get()->result_array()[0];
    }
    public function transaction_datewise($d1="0000-00-00",$d2="0000-00-00",$limit=0,$offset=0)
    {
            
            $branch=$this->user_model->getbranch();
         $this->db->select("*")->where('branch',$branch)->where('date>=',$d1)->where('date<=',$d2);
         $data=$this->db->limit($offset,$limit)->get('trans')->result_array();
         return $data;
    }
    public function Alltrans_datewise($d1="0000-00-00",$d2="0000-00-00")
    {
            
            $branch=$this->user_model->getbranch();
         $this->db->select("*")->where('branch',$branch)->where('date>=',$d1)->where('date<=',$d2);
         $data=$this->db->get('trans')->result_array();
         return $data;
    }
    public function Alltrans_upper($d1="0000-00-00")
    {
          $branch=$this->user_model->getbranch();
         $this->db->select("*")->where('branch',$branch)->where('date<',$d1);
         $data=$this->db->get('trans')->result_array();
         return $data;  
    }
    
    
    public function ledgerrec($id=0)
    {
          return  $this->db->select('*')->where('is_delete',0)->or_where('main_head_id',$id)->or_where('level_2_id',$id)->or_where('level_3_id',$id)->or_where('level4',$id)->or_where('level5',$id)->get('cash_receipt')->result_array();
    }
    public function getlevl5assigndrop(){
        $branch=$this->user_model->getbranch();
            $result=$this->db->query("SELECT head.id,head.name,assign_account.id as assignid FROM `assign_account` join head on head.id=assign_account.level5 where is_active=1 and is_delete=0 and programe_id=0 and project=0 and level5!=0 and assign_account.branch=$branch ")->result_array();
         return $result;
    }
    public function opbalancel5(){
        $branch=$this->user_model->getbranch();
            $result=$this->db->query("SELECT head.id,head.name,assign_account.id as assignid FROM `assign_account` join head on head.id=assign_account.level5 where  is_delete=0 and programe_id=0 and project=0 and level5!=0 and assign_account.branch=$branch and main_head!=5 and main_head!=4 ")->result_array();
         return $result;
    }
    public function petycashl5(){
        $branch=$this->user_model->getbranch();
            $result=$this->db->query("SELECT head.id,head.name,assign_account.id as assignid FROM `assign_account` join head on head.id=assign_account.level5 where  is_delete=0 and programe_id=0 and project=0 and level5!=0 and assign_account.branch=$branch and main_head!=4 ")->result_array();
         return $result;
    }
    public function l5withoutexpense(){
        $branch=$this->user_model->getbranch();
            $result=$this->db->query("SELECT head.id,head.name,assign_account.id as assignid FROM `assign_account` join head on head.id=assign_account.level5 where  is_delete=0 and programe_id=0 and project=0 and level5!=0 and assign_account.branch=$branch and main_head!=5 ")->result_array();
         return $result;
    }
    public function saveop($fields=array())
    {
            
            $ammount=0;
            if($fields['type']=="D"){
                    $fields['type']="DR";
                    $ammount=$fields['dr_amount'];
            }else{
                    $fields['type']="CR";
                    $ammount=$fields['cr_amount'];
            }
            $db = array('balance' =>$ammount ,
                          'opbalance' =>$ammount ,
                            'cr_dr'=>$fields['type'] );
                            $this->db->where('id',$fields['level5']);
                            $this->db->update('head',$db);
    }
    public function Cashinhand()
    {
        $branch=$this->user_model->getbranch();
        $cash_pay=$this->db->query("SELECT sum(transaction_detail.amount) as total FROM `trans` join transaction_detail on transaction_detail.transaction_id=trans.id where trans.type='cash payment' and trans.main_head!=4 and trans.branch=$branch")->result_array()[0]['total'];
        $cash_recpt=$this->db->query("SELECT sum(transaction_detail.amount) as total FROM `trans` join transaction_detail on transaction_detail.transaction_id=trans.id where trans.type='cash receipt' and trans.main_head!=5 and trans.branch=$branch")->result_array()[0]['total'];
        return $cash_recpt-$cash_pay;
    }
    public function allbankl5()
    {
            $list=$this->db->query("SELECT * FROM `assign_account` where level4=74 and level5!=0")->result_array();
            
            $record=array();
           foreach ($list as $key => $value) {
                
                $record[$key]['id']=$value['level5'];
                $record[$key]['name']=$this->account_model->search('head',$value['level5'])['name'];
                $record[$key]['balance']=$this->account_model->search('head',$value['level5'])['balance'];

           }
           return $record;
    }
     public function alldata($t="")
    {
        $this->db->select('*');
        $this->db->where('is_del',0);
        $data=$this->db->get($t)->result_array();
        return $data;
    }
    public function insertnaration($data=array())
    {
            $fields = array('name' =>$data['name'] ,'description'=>$data['description'] );
         if($data['id']>0){
                 $this->db->where('id',$data['id']);
                 $b=$this->db->update('naration',$fields);
         }   
         else{
                 $b=$this->db->insert('naration',$fields);
         }
         return $b;
    }
    public function getammount($id)
    {
            $d=$this->db->query("SELECT * FROM `transaction_detail` where transaction_id=$id and cr!=0")->result_array();
            $amount=0;
            foreach ($d as $key => $value) {

                        $amount+=$value['amount'];

            }
            return $amount;
    }
    public function getprogramme($id=0)
    {
            $pid=$this->search('project',$id)['program_id'];
           $d=$this->db->query("SELECT * FROM `program` where id=$pid")->row_array();
           return $d['name'];
    }
    public function Alltrans5()
    {
        $branch=$this->user_model->getbranch();
        $this->db->distinct();
        $this->db->select("level5"); 
        $this->db->from('trans');
        $this->db->join('transaction_detail','trans.id=transaction_detail.transaction_id');
        $this->db->where('trans.branch',$branch);
        $query=$this->db->get()->result_array(); 
        return $query;   
    }
    public function alltransview($type="")
    {
       
        $branch=$this->user_model->getbranch();
        $this->db->select("trans.*"); 
        $this->db->from('trans');
        $this->db->where('branch',$branch)->where('type',$type);
        $query=$this->db->get()->result_array(); 
        return $query;     

    }
     public function save_budget($data=array())
    {
           
            $date1=$data['date1'];
            $date2=$data['date2'];
             
        if(count($this->check($data['main_head'],$date1,$date2))>0){
                
                $this->db->query("DELETE from budget where main_head=$data[main_head] and  month BETWEEN '$date1' and '$date2' ");
                }
        foreach ($data['income'] as $key => $value) {
        foreach ($value['price'] as $ke => $val) {
                $fields = array('head' => $value['head'] ,
                                'month'=>$data['month'][$ke],
                                'budget_price'=>$val,
                                'main_head'=>$data['main_head'],);
                 $bool=$this->db->insert('budget',$fields);
                 
        }
        
        }
       
        return $bool;
    }
    public function check($head=0,$date1="0000-00",$date2="0000-00")
    {
            return $this->db->query("SELECT * FROM `budget` WHERE main_head=$head and   `month` BETWEEN '$date1' AND '$date2'  ")->result_array();
    }
    public function findbudget($id=0,$date="0000-00")
    {
            
            return $this->db->select('budget_price')->where('head',$id)->where('month',$date)->get('budget')->row_array()['budget_price'];
            
    }
    public function incomebudget($main=0,$head=0,$date="0000-00")
    {
            
            $branch=$this->user_model->getbranch();
            $this->db->select('sum(transaction_detail.amount) as total')->from('trans');
            $this->db->join('transaction_detail','trans.id=transaction_detail.transaction_id');
            $this->db->where('transaction_detail.level5',$head)->where('branch',$branch)->where('LEFT(trans.date,7)',$date);
if($main==4){            
$sum=$this->db->where_in('type',array('bank receipt','cash receipt'))->get()->row_array()['total'];
}else if($main==5){
$sum=$this->db->where_in('type',array('bank payment','cash payment'))->get()->row_array()['total'];
}
            return $sum;

    }
    public function searchdate($type="",$date1="0000-00",$date2="0000-00")
    {
        $branch=$this->user_model->getbranch();
        return  $this->db->from('trans')->where('left(date,7)>=',$date1)->where('left(date,7)<=',$date2)->where_in('type',array($type))->get()->result_array();
    }
     public function getchild($type=0)
    {
            $l5=$this->getChildren(0,5);
           $head=array();
           $i=0;
           foreach ($l5 as $key => $value) {
                        if($value['id']==$type){
                        $level = 5; 
                        
                          $val = $value['level_'.$level];
                          foreach($val as $key2 => $v) {
                            
                            $val2 = $v['level_'.($level - 1)];
                              foreach($val2 as $key3 => $vv) {
                                 
                              $val3 = $vv['level_'.($level - 2)];
                              foreach ($val3 as $key => $vvv) {
                                  
                               if(@$vvv['level_'.($level - 3)]){ 
                              $val4 = $vvv['level_'.($level - 3)];
                              
                                foreach ($val4 as $key => $vvvv) {
                                               
                                            $head[++$i]['name']=$vvvv['name'];
                                            $head[$i]['id']=$vvvv['id'];


           }}}}}}}
           
           return $head;
    }
    public function plsdatewiseexp($head=0,$date1="0000-00-00",$date2="0000-00-00")
    {
            $branch=$this->user_model->getbranch();
            $this->db->select('sum(transaction_detail.amount) as total')->from('trans');
            $this->db->join('transaction_detail','trans.id=transaction_detail.transaction_id');
            $this->db->where('transaction_detail.level5',$head)->where('branch',$branch)->where('left(trans.date,10)>=',$date1)->where('left(trans.date,10)<=',$date2);
            $sum=$this->db->where_in('type',array('cash payment','bank payment'))->get()->row_array()['total'];
            return $sum;
    }
      public function plsdatewiseincome($head=0,$date1="0000-00-00",$date2="0000-00-00")
    {
            $branch=$this->user_model->getbranch();
            $this->db->select('sum(transaction_detail.amount) as total')->from('trans');
            $this->db->join('transaction_detail','trans.id=transaction_detail.transaction_id');
            $this->db->where('transaction_detail.level5',$head)->where('branch',$branch)->where('trans.date>=',$date1)->where('trans.date<=',$date2);
            $sum=$this->db->where_in('type',array('cash receipt','bank receipt'))->get()->row_array()['total'];
            return $sum;
    }
    public function balancesheet($head=0,$date1="0000-00-00",$date2="0000-00-00")
    {
            $branch=$this->user_model->getbranch();
            $this->db->select('sum(transaction_detail.amount) as total')->from('trans');
            $this->db->join('transaction_detail','trans.id=transaction_detail.transaction_id');
            $this->db->where('transaction_detail.level5',$head)->where('branch',$branch)->where('trans.date>=',$date1)->where('trans.date<=',$date2);
            $sum=$this->db->where_in('type',array('cash receipt','bank receipt'))->get()->row_array()['total'];
            return $sum;
    }
    
    

    
}   

