<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of expensesetup
 *
 * @author BusyMan
 */
class BalanceSetup extends OWN_Controller{
    function __construct() {
        parent::__construct();
        $this->admin_login_check();
        // $this->load->model('expensesetupmodel/expensesetupmodel','expensesetupmodel'); 
        // $this->load->model('salaryexpensesetupmodel/salaryexpensesetupmodel','salaryexpensesetupmodel'); 
        // $this->load->model('extraincomesetupmodel/extraincomesetupmodel','extraincomesetupmodel');
        //  $this->load->model('incomesetupmodel/incomesetupmodel','incomesetupmodel');
         $this->load->model('balancesetupmodel/balancesetupmodel','balancesetupmodel');
    }

    public function index(){
    	$admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !=""))
        {
        	 $data['expense']=$this->balancesetupmodel->getTotalExpense();
           $data['salaryExpense']=$this->balancesetupmodel->getTotalSalaryExpense();
           $data['landIncome']=$this->balancesetupmodel->getTotalLandIncome();
           $data['extraIncome']=$this->balancesetupmodel->getTotalExtraIncome();
           //echo '<pre>';print_r($data);die();
           if(empty($data['expense']['expenseAmount']) && empty($data['salaryExpense']['salaryAmount']) && empty($data['landIncome']['amount']) && empty($data['extraIncome']['amount']))
           {
                $errormessage['errormessage']='No Income And Expense Found ';
                $this->session->set_userdata($errormessage);
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('dashbord');
                $this->load->view('common/footer');

           }else{
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/balance/balanceView',$data);
                $this->load->view('common/footer');
           }
        }else{
        	redirect(base_url('admin','refresh'));
        }
    }

    public function balanceBydaterange()
    {
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            if(isset($_POST['btnSdateRange']))
            {
                $this->load->helper(array('form','url'));
                 $this->load->library('form_validation');
                $config=array(
                    array(
                        'field' => 'data[fromdate]',
                        'label' => 'Date From',
                        'rules' => 'required|xss_clean|trim'
                        ),
                      array(
                        'field' => 'data[todate]',
                        'label' => 'Date To',
                        'rules' => 'required|xss_clean|trim'
                        )
                    ); 
              $this->form_validation->set_rules($config);
              if($this->form_validation->run()==false)
              {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/balance/balanceView');
                $this->load->view('common/footer');
              }
              else
              {
                  $data=$this->input->post('data',true);
                  if(!empty($data))
                  {
                       $data['expense']         = $this->balancesetupmodel->getTotalExpenseBydate($data);
                       $data['salaryExpense']   = $this->balancesetupmodel->getTotalSalaryExpenseBydate($data);
                       $data['landIncome']      = $this->balancesetupmodel->getTotalLandIncomeBydate($data);
                       $data['extraIncome']     = $this->balancesetupmodel->getTotalExtraIncomeBydate($data);
                       //echo '<pre>';print_r($data);die();
                       if(empty($data['expense']['expenseAmount']) && empty($data['salaryExpense']['salaryAmount']) && empty($data['landIncome']['amount']) && empty($data['extraIncome']['amount']))
                       {
                            $errormessage['errormessage']='No Income Found In This Date Range';
                            $this->session->set_userdata($errormessage);
                            $this->load->view('common/header');
                            $this->load->view('common/menu');
                            $this->load->view('dashbord');
                            $this->load->view('common/footer');
                            
                       }
                       else
                       {
                            
                            $this->load->view('common/header');
                            $this->load->view('common/menu');
                            $this->load->view('admin/balance/balanceView',$data);
                            $this->load->view('common/footer');
                       }
                  }
                  else
                  {
                      $errormessage['errormessage']='please Select date';
                      $this->session->set_userdata($errormessage);
                      redirect('admin/balancesetup','refresh');
                  }
              }
            }
        }
        else{
           redirect(base_url('admin','refresh'));
        }
    }
}

/*   ---------------- count amount date by for loop--------------------


$count=count($amountAndYear);
        		for($i=0;$i<$count;$i++)
        		{
			     	$atoms = explode('-',$amountAndYear[$i]['date']);
					 $year = $atoms[2];
					 if($year==date('Y'))
					 {
					 	$countAmount=$countAmount+$amountAndYear[$i]['amount'];
					 }
        		}
        		echo $countAmount;
  				die();


      ------------------------another way using foreach-----------------------------

      $amountAndYear=$this->extraincomesetupmodel->getAmount_Year();
          if(!empty($amountAndYear))
          {
                  $countAmountExtra=0;
                foreach($amountAndYear as $value)
                {
                    $atoms = explode('-',$value['date']);
                    $year = $atoms[2];
                    if($year==date('Y'))
                     {
                       $countAmountExtra=$countAmountExtra+$value['amount'];
                     }
                }
                echo $countAmountExtra;
          }
          else
          {
              $countAmountExtra=0;
          }

      --------------------------------------------------------------------------

   ---------------- count amount date by for loop--------------------*/