<?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 ExpenseSetup extends OWN_Controller{
    function __construct() {
        parent::__construct();
        $this->admin_login_check();
        $this->load->model('expensesetupmodel/expensesetupmodel','expensesetupmodel'); //expencecategorymodel
        $this->load->model('expencecategorymodel/expencecategorymodel','expencecategorymodel');
        $this->load->model('balancesetupmodel/balancesetupmodel','balancesetupmodel');
        $this->load->model('landcustomerregistrationmodel/landcustomerregistrationmodel', 'CREGModel');
        $this->load->model('extraincomesetupmodel/extraincomesetupmodel', 'extraincomesetupmodel');
        $this->load->model('employeemodel/employeemodel','employeemodel');
    }

   public function index()
   {
    $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !=""))
        {
       		  $data['allExpenseCategory']=$this->expencecategorymodel->getAllExpenseCategory();
       		  $this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('admin/expense/expenseEntry',$data);
            $this->load->view('common/footer');
        }
        else{
            redirect(base_url('admin','refresh'));
        }
   }

   public function insert_Expense(){
    $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !=""))
        {
         		$this->load->helper(array('form','url'));
              $this->load->library('form_validation');
                $config=array(

                array(
                    'field' => 'data[expenseAmount]',
                    'label' => 'Expense Amount',
                    'rules' => 'required|xss_clean|trim|numeric'
                    ),
                array(
                    'field' => 'data[expenseCategory]',
                    'label' => 'Expense Category',
                    'rules' => 'required|xss_clean|trim'
                    ),
                array(
                    'field' => 'data[date]',
                    'label' => 'Date',
                    'rules' => 'required|xss_clean|trim'
                    ),

                 array(
                    'field' => 'data[expenseDescription]',
                    'label' => 'Expense Description',
                    'rules' => 'xss_clean|trim'
                    )
            );

        $this->form_validation->set_rules($config);
        if($this->form_validation->run()==false){
            $errormessage['errormessage']="Expense Information Missing......!";
            $this->session->set_userdata($errormessage);
             $data['allExpenseCategory']=$this->expencecategorymodel->getAllExpenseCategory();
     		     $this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('admin/expense/expenseEntry',$data);
            $this->load->view('common/footer');
        }
        else
        {
           $data =$this->input->post('data',true);
           $data['year']=date('Y');
           $data['employee_id']=$this->session->userdata('employeeId');
           if(!empty($data) && $data['expenseCategory']!=null)
           {
                $execution=$this->expensesetupmodel->saveExpense($data);
                if(!empty($execution))
                {
                    $message['message']="Expense   Saved......!";
                    $this->session->set_userdata($message);
                    redirect(admin_url().'/expensesetup/index','refresh');
                }
                else
                {
                    $errormessage['errormessage']="Expense Not Saved......!";
                    $this->session->set_userdata($errormessage);
                    redirect(admin_url().'/expensesetup/index','refresh');

                }

            }
            else
            {
                 $errormessage['errormessage']="Expense Information Missing......!";
                $this->session->set_userdata($errormessage);
                 redirect(admin_url().'/expensesetup/index','refresh');
            }
        }
      }
      else{
            redirect(base_url('admin','refresh'));
        }
   }

   		public function viewExpense(){
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !=""))
        {
       			$data['allexpense']=$this->expensesetupmodel->getexpenseInfo();
       			$data['total_expe']=$this->expensesetupmodel->expenseAmount();
       			if(!empty($data['allexpense'])) {
                    $current_year          = date('Y');
                    $data['exp_current']   = $this->expensesetupmodel->expensePerYear($current_year);
       				$this->load->view('common/header');
    		        $this->load->view('common/menu');
    		        $this->load->view('admin/expense/viewExpense',$data);
    		        $this->load->view('common/footer');
       			}
       			else
       			{
         				$errormessage['errormessage']="In This Time No Expense Available......!";
                $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{
            redirect(base_url('admin','refresh'));
        }
    }

    public function edit_expense($expenseId)
    {
      $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !=""))
        {
          $data['allExpenseCategory']=$this->expencecategorymodel->getAllExpenseCategory();
          $data['allexpenseById']=$this->expensesetupmodel->getexpenseInfoById($expenseId);
          if(!empty($data['allexpenseById']))
            {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/expense/editExpense',$data);
                $this->load->view('common/footer');
            }
            else
            {
                redirect(admin_url().'/expensesetup/viewExpense','refresh');
            }
        }
        else
        {
            redirect(base_url('admin','refresh'));
        }
    }

    public function update_Expense()
    {
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !=""))
        {
            if(isset($_POST['btnUpdate']))
            {
                    $this->load->helper(array('form','url'));
                    $this->load->library('form_validation');
                    $config=array(

                    array(
                        'field' => 'data[expenseAmount]',
                        'label' => 'Expense Amount',
                        'rules' => 'required|xss_clean|trim|numeric'
                        ),
                    array(
                        'field' => 'data[expenseCategory]',
                        'label' => 'Expense Category',
                        'rules' => 'required|xss_clean|trim'
                        ),
                    array(
                        'field' => 'data[date]',
                        'label' => 'Date',
                        'rules' => 'required|xss_clean|trim'
                        ),
                     array(
                        'field' => 'data[expenseDescription]',
                        'label' => 'Expense Description',
                        'rules' => 'xss_clean|trim'
                        )
                );

              $this->form_validation->set_rules($config);
              if($this->form_validation->run()==false)
              {
                    $errormessage['errormessage']="Expense Information Missing......!";
                    $this->session->set_userdata($errormessage);
                     $data['allExpenseCategory']=$this->expencecategorymodel->getAllExpenseCategory();
                     $this->load->view('common/header');
                    $this->load->view('common/menu');
                    $this->load->view('admin/expense/viewExpense',$data);
                    $this->load->view('common/footer');
              }
              else
              {
                  $data =$this->input->post('data',true);
                  $expenseId=$this->input->post('expenseId',true);
                  if(!empty($data) && $data['expenseCategory']!=null)
                  {
                    //print_r($data);die();
                      $execution=$this->expensesetupmodel->updateExpenseByid($data,$expenseId);
                      if(!empty($execution))
                      {
                          $message['message']='Expense Information Updated';
                          $this->session->set_userdata($message);
                          redirect('admin/expensesetup/viewExpense','refresh');
                      }
                      else
                      {
                          $errormessage['errormessage']='Expense Information Not Updated';
                          $this->session->set_userdata($errormessage);
                         redirect('admin/expensesetup/viewExpense','refresh');
                      }
                  }
                  else
                  {
                      $errormessage['errormessage']='Expense Information Not Found';
                      $this->session->set_userdata($errormessage);
                      redirect('admin/expensesetup/viewExpense','refresh');
                  }
              }
            }
            else
            {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('dashbord');
                $this->load->view('common/footer');
            }

        }
        else
        {
            redirect(base_url('admin','refresh'));
        }
    }

    public function delete_expense($expenseId)
    {
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !=""))
        {
           $execution=$this->expensesetupmodel->deleteExpenseByid($expenseId);
           if($execution){
              $message['message']='Expense Information Deleted';
              $this->session->set_userdata($message);
              redirect('admin/expensesetup/viewExpense','refresh');
           }else{
              $errormessage['errormessage']='Expense Information Not Deleted';
              $this->session->set_userdata($errormessage);
              redirect('admin/expensesetup/viewExpense','refresh');
           }
        }
        else
        {
            redirect(base_url('admin','refresh'));
        }
    }



    public function searchBydate()
    {
      $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !=""))
        {
          $this->load->view('common/header');
          $this->load->view('common/menu');
          $this->load->view('admin/expense/viewExpensebydate');
          $this->load->view('common/footer');
        }
        else{
           redirect(base_url('admin','refresh'));
        }
    }

    

    public function searchSingleBydate()
    {
      $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !=""))
        {
          if(isset($_POST['btnSdate']))
          {
               $this->load->helper(array('form','url'));
               $this->load->library('form_validation');
              $config=array(
                    array(
                        'field' => 'data[date]',
                        'label' => 'Date',
                        'rules' => 'xss_clean|trim'
                        ),
                        array(
                        'field' => 'data[expenseCategory]',
                        'label' => 'expenseCategory',
                        'rules' => 'xss_clean|trim'
                        ),
                      array(
                      'field' => 'data[salary_type]',
                      'label' => 'Salary Type',
                      'rules' => '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/expense/viewExpensebydate');
                $this->load->view('common/footer');
              }
              else
              {
                  $data=$this->input->post('data',true);
                  if(!empty($data))/*!empty($data['date']) || $data['expenseCategory']!=null && empty($data['salary_type']) || $data['salary_type']==null*/
                  {
                      if (empty($data['salary_type']) || $data['salary_type']==null) {
                        $datax['expense']=$this->expensesetupmodel->getexpenseBydate($data);
                      }
                      if($data['expenseCategory']==null || empty($data['expenseCategory']))
                        {
                          $datax['salaryexpense']=$this->expensesetupmodel->getsalaryexpenseBydate($data);
                        }
                      if(empty($datax['expense']) && empty($datax['salaryexpense']))
                      {
                        if(!empty($data['date']) && !empty($data['expenseCategory'])){$errormessage['errormessage']='No Expense Found In this Category Of The date';}
                        else{$errormessage['errormessage']='No Expense Found';}
                        $this->session->set_userdata($errormessage);
                        redirect('admin/expensesetup/searchBydate','refresh');
                      }
                      else
                      {
                        $this->load->view('common/header');
                        $this->load->view('common/menu');
                        $this->load->view('admin/expense/viewExpensebydate',$datax);
                        $this->load->view('common/footer');
                      }
                  }
                  else
                  {
                        $errormessage['errormessage']='Please Select Date or Category';
                        $this->session->set_userdata($errormessage);
                        redirect('admin/expensesetup/searchBydate','refresh');
                  }
              }
          }
        }
        else{
           redirect(base_url('admin','refresh'));
        }
    }

    public function searchBydaterange()
    {

      $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'
                        ),
                        array(
                        'field' => 'data[expenseCategory]',
                        'label' => 'expenseCategory',
                        'rules' => 'xss_clean|trim'
                        ),
                        array(
                        'field' => 'data[salary_type]',
                        'label' => 'Salary Type',
                        'rules' => '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/expense/viewExpensebydate');
                $this->load->view('common/footer');
              }
              else
              {
                  $data=$this->input->post('data',true);
                  if(!empty($data))
                  {
                      if (empty($data['salary_type']) || $data['salary_type']==null) {
                        $datax['expense']=$this->expensesetupmodel->getBydaterange($data);
                      }
                      if($data['expenseCategory']==null || empty($data['expenseCategory']))// scince incomeCategory is not available in salary expense
                      {
                        $datax['salaryexpense']=$this->expensesetupmodel->getsalaryBydaterange($data);
                      }
                      if(empty($datax['expense']) && empty($datax['salaryexpense']))
                      {
                        if(!empty($data['date']) && !empty($data['expenseCategory'])){$errormessage['errormessage']='No Income Found In this Category Of The Date';}
                        $errormessage['errormessage']='No Expense Found In this date';
                        $this->session->set_userdata($errormessage);
                        redirect('admin/expensesetup/searchBydate','refresh');
                      }
                      else
                      {
                        $this->load->view('common/header');
                        $this->load->view('common/menu');
                        $this->load->view('admin/expense/viewExpensebydate',$datax);
                        $this->load->view('common/footer');
                      }
                  }
                  else
                  {
                        $errormessage['errormessage']='Please Select Date range or Expense Category';
                        $this->session->set_userdata($errormessage);
                        redirect('admin/expensesetup/searchBydate','refresh');
                  }
              }
          }
        }
        else{

           redirect(base_url('admin','refresh'));
        }
    }

  public function searchByemployee()
    {
      $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !=""))
        {
          $data['allEmployeeId']=$this->employeemodel->getAllEmployeeId();
          $this->load->view('common/header');
          $this->load->view('common/menu');
          $this->load->view('admin/expense/viewExpensebyemployee',$data);
          $this->load->view('common/footer');
        }
        else{
           redirect(base_url('admin','refresh'));
        }
    }


    public function searchBydateandemployee()
    {

      $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !=""))
        {

          if(isset($_POST['btnSdateRangeandEmp']))
          {


               $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'
                        ),
                        array(
                        'field' => 'data[employee_id]',
                        'label' => 'employee_id',
                        'rules' => 'xss_clean|trim'
                        ),
                        array(
                        'field' => 'data[salary_type]',
                        'label' => 'Salary Type',
                        'rules' => '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/expense/viewExpensebyemployee');
                $this->load->view('common/footer');
              }
              else
              {
                  $data=$this->input->post('data',true);
                  if(!empty($data))
                  {
                      
                      // if (empty($data['salary_type']) || $data['salary_type']==null && $data['employee_id']==null || empty($data['employee_id'])) {
                      //   $datax['salaryexpensewithemp']=$this->expensesetupmodel->getsalaryBydaterangeandEmployeename($data);
                      // }

                      // else

                        if (empty($data['salary_type']) || $data['salary_type']==null) {
                        $datax['expense']=$this->expensesetupmodel->getBydaterangeandemp($data);
                      }
                      elseif($data['employee_id']==null || empty($data['employee_id']))
                      // scince incomeCategory is not available in salary expense
                      {
                        $datax['salaryexpense']=$this->expensesetupmodel->getsalaryBydaterangeEmployee($data);
                      }

                      


                      // if(empty($datax['salaryexpensewithemp']) && empty($datax['expense']) && empty($datax['salaryexpense']) )
                      if(empty($datax['expense']) && empty($datax['salaryexpense']) )
                      {
                        if(!empty($data['date']) && !empty($data['employee_id'])){$errormessage['errormessage']='No Income Found In this Category Of The Date';}
                        $errormessage['errormessage']='No Expense Found In this date';
                        $this->session->set_userdata($errormessage);
                        redirect('admin/expensesetup/searchByemployee','refresh');
                      }
                      else
                      {
                        $this->load->view('common/header');
                        $this->load->view('common/menu');
                        $this->load->view('admin/expense/viewExpensebyemployee',$datax);
                        $this->load->view('common/footer');
                      }
                  }
                  else
                  {
                        $errormessage['errormessage']='Please Select Date range or Employee';
                        $this->session->set_userdata($errormessage);
                        redirect('admin/expensesetup/searchByemployee','refresh');
                  }
              }
          }
        }
        else{

           redirect(base_url('admin','refresh'));
        }
    }

    public function invoice($expenseId) {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            $data['expenseInvoiceInfo'] = $this->expensesetupmodel->getinvoiceInfoByid($expenseId);
            //echo '<pre>';print_r($data);die();
            if ($data['expenseInvoiceInfo']) {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/expense/expense_invoice',$data);
                $this->load->view('common/footer');
            } else {
                redirect(admin_url() . '/expensesetup/viewExpense', 'refresh');
            }
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }

   public function expenseSearch(){
        $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();

            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/expense/expense_search',$data);
                $this->load->view('common/footer');
            }
        }else{
            redirect(base_url('admin','refresh'));
        }
   }

   public function searchExpense($flag=''){
       if(!empty($flag) && $flag=='year'){
           $admin_id = $this->session->userdata('admin_id');
           if ((!empty($admin_id)) || ($admin_id != "")) {
               $year = $this->input->post('select_year');
               if(is_numeric($year) > 0){
                   $data['allincome']       =   $this->extraincomesetupmodel->getIncomeByYear($year);
                   $data['expense_list']    =   $this->expensesetupmodel->getExpenseByYear($year);
                   $data['salary_expense']  =   $this->expensesetupmodel->getSalaryExpenseByYear($year);
                   $data['get_amount']      =   $this->expensesetupmodel->expensePerYear($year);
                   $rows = sizeof($data['salary_expense']);
                   for($i=0; $i<$rows; $i++){
                       $name =    $this->expensesetupmodel->getUserNameById($data['salary_expense'][$i]['employee_id']);
                       $name =    $name['userName'];
                       $data['salary_expense'][$i]['name'] = $name;
                   }
                   $data['year']            =   $year;
                   $this->load->view('common/header');
                   $this->load->view('common/menu');
                   $this->load->view('admin/expense/search_result_table',$data);
                   $this->load->view('common/footer');
               }else{
                   redirect(base_url('admin/home', 'refresh'));
               }
           } else {
               redirect(base_url('admin/home', 'refresh'));
           }
       }else if(!empty($flag) && $flag=='month'){
           $date=$this->input->post('data',true);

           if(!empty($date)){
               $data['expense_list']    = $this->expensesetupmodel->getExpenseByDateRange($date);
               $data['salary_expense']  = $this->expensesetupmodel->getSalaryExpenseByRange($date);
               $data['get_amount']      = $this->expensesetupmodel->getTotalAmountInDateRange($date);

               $rows = sizeof($data['salary_expense']);
               for($i=0; $i<$rows; $i++){
                   $name =    $this->expensesetupmodel->getUserNameById($data['salary_expense'][$i]['employee_id']);
                   $name =    $name['userName'];
                   $data['salary_expense'][$i]['name'] = $name;
               }

               $data['from_date']       = $date['fromdate'];
               $data['to_date']         = $date['todate'];
               $this->load->view('common/header');
               $this->load->view('common/menu');
               $this->load->view('admin/expense/search_result_table',$data);
               $this->load->view('common/footer');
           }
       }else{
           redirect(base_url('admin/home', 'refresh'));
       }
   }

}