<?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');
    }

   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();
       			if(!empty($data['allexpense']))
            {
       				   $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['date']) || $data['expenseCategory']!=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))
                  {
                      $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);
                      }//echo '<pre>';print_r($datax);die();
                      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'));
        }
    }
    
}
