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

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

   public function insert_salaryExpense()
   {
      $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            if(isset($_POST['btnSave']))
            {
                $this->load->helper(array('url','form'));
                $this->load->library('form_validation');
                $config = array(
                  array(
                      'field' => 'data[date]',
                      'label' => 'Date',
                      'rules' => 'required|xss_clean|trim'
                      ),
                   array(
                      'field' => 'data[employee_id]',
                      'label' => 'Employee Id',
                      'rules' => 'required|xss_clean|trim'
                      ),
                     array(
                      'field' => 'data[description]',
                      'label' => 'description',
                      'rules' => 'xss_clean|trim'
                      ),
                      array(
                      'field' => 'data[customerId]',
                      'label' => 'customerId',
                      'rules' => 'xss_clean|trim'
                      ),
                      array(
                      'field' => 'data[salary_type]',
                      'label' => 'Salary Type',
                      'rules' => 'required|xss_clean|trim'
                      ),
                      array(
                      'field' => 'data[salaryAmount]',
                      'label' => ' Salary Amount ',
                      'rules' => 'required|xss_clean|trim|numeric'
                      )
                );
                $this->form_validation->set_rules($config);
                if ($this->form_validation->run() == FALSE) {
                    $errormessage['errormessage'] = "salary Info Missing......!";
                    $this->session->set_userdata($errormessage);
                    $data['allCustomerId'] = $this->CREGModel->getallcustomerId();
                    $data['allCustomers'] = $this->CREGModel->getallcustomers();
                    $data['allEmployeeId']=$this->employeemodel->getAllEmployeeId();
                    $this->load->view('common/header');
                    $this->load->view('common/menu');
                    $this->load->view('admin/expense/salaryExpenseEntry',$data);
                    $this->load->view('common/footer');
                }
                else
                {
                    $data = $this->input->post('data', TRUE);
                    $data['year']=date('Y');
                   // print_r($data);die();
                    if(!empty($data) && $data['employee_id']!=null)
                    {
                        $duplicate=$this->salaryexpensesetupmodel->checkduplicat($data);
                        if(empty($duplicate))
                        {
                            
                            if($data['salary_type']==3){
                                $customerId = $data['customerId'];
                                $getcust['all_custinfo'] = $this->CREGModel->getcustomerByIds($customerId);
                                $getcom= $getcust['all_custinfo']['com_emp'];
                                    if($getcom > $data['salaryAmount']){
                                        $datacomup['customerId']= $data['customerId'];
                                        $datacomup['commission']= $getcom-$data['salaryAmount'];
                                        $updatecustom=$this->CREGModel->updatecomamount($datacomup);
                                        
                                        if(!empty($updatecustom)){
                                            $execution=$this->salaryexpensesetupmodel->saveSalaryExpense($data);
                                            if(!empty($execution))
                                            {
                                                $message['message'] = "Salary Expense Saved! <h3><b> Token No: ".$this->db->insert_id()."</b></h3>";
                                                $this->session->set_userdata($message);
                                                redirect(admin_url().'/salaryexpensesetup/index','refresh');
                                            }
                                            else
                                            {
                                                $errormessage['errormessage']="Salary Expense Not Saved.......!";
                                                $this->session->set_userdata($errormessage);
                                                redirect(admin_url().'/salaryexpensesetup/index','refresh');
                                            }
                                        }
                                    }
                                    else{
                                        $errormessage['errormessage']="No Enough Commission .......!";
                                        $this->session->set_userdata($errormessage);
                                        redirect(admin_url().'/salaryexpensesetup/index','refresh');
                                    }
                            }
                            elseif($data['salary_type']==4){
                                $customerId = $data['customerId'];
                                $getcust['all_custinfo'] = $this->CREGModel->getcustomerByIds($customerId);
                                $getcom= $getcust['all_custinfo']['incentive'];
                                    if($getcom > $data['salaryAmount']){
                                        $datacomup['customerId']= $data['customerId'];
                                        $datacomup['incentive']= $getcom-$data['salaryAmount'];
                                        $updatecustom=$this->CREGModel->updateincamount($datacomup);
                                        
                                        if(!empty($updatecustom)){
                                            $execution=$this->salaryexpensesetupmodel->saveSalaryExpense($data);
                                            if(!empty($execution))
                                            {
                                                $message['message'] = "Salary Expense Saved! <h3><b> Token No: ".$this->db->insert_id()."</b></h3>";
                                                $this->session->set_userdata($message);
                                                redirect(admin_url().'/salaryexpensesetup/index','refresh');
                                            }
                                            else
                                            {
                                                $errormessage['errormessage']="Salary Expense Not Saved.......!";
                                                $this->session->set_userdata($errormessage);
                                                redirect(admin_url().'/salaryexpensesetup/index','refresh');
                                            }
                                        }
                                    }
                                    else{
                                        $errormessage['errormessage']="No Enough Incentive .......!";
                                        $this->session->set_userdata($errormessage);
                                        redirect(admin_url().'/salaryexpensesetup/index','refresh');
                                    }
                            }
                            
                            $execution=$this->salaryexpensesetupmodel->saveSalaryExpense($data);
                            if(!empty($execution))
                            {
                                $message['message'] = "Salary Expense Saved! <h3><b> Token No: ".$this->db->insert_id()."</b></h3>";
                                $this->session->set_userdata($message);
                                redirect(admin_url().'/salaryexpensesetup/index','refresh');
                            }
                            else
                            {
                                $errormessage['errormessage']="Salary Expense Not Saved.......!";
                                $this->session->set_userdata($errormessage);
                                redirect(admin_url().'/salaryexpensesetup/index','refresh');
                            }
                        }
                        else
                        {
                            $errormessage['errormessage']="Salary Expense Already Exist.......!";
                             $this->session->set_userdata($errormessage);
                             redirect(admin_url().'/salaryexpensesetup/index','refresh');
                        }
                    }
                    else
                    {
                        $errormessage['errormessage']="Salary Information Missing.......!";
                        $this->session->set_userdata($errormessage);
                        redirect(admin_url().'/salaryexpensesetup/index','refresh');
                    }
                }
            }
            else
            {
              redirect(admin_url().'/salaryexpensesetup/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['allsalaryExpense'] = $this->salaryexpensesetupmodel->getallsalaryExpense();
            $data['total_expense']    = $this->salaryexpensesetupmodel->getTotalSalaryExpense();

            if(!empty($data['allsalaryExpense']))
            {
                  $year = date('Y');
                  $data['salary_expense_year']    = $this->salaryexpensesetupmodel->getSalaryExpenseByYear($year);
                  $this->load->view('common/header');
                  $this->load->view('common/menu');
                  $this->load->view('admin/expense/viewSalaryExpense',$data);
                  $this->load->view('common/footer');
            }
            else
            {
                  $errormessage['errormessage'] = "No Salary 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
        {
            redirect(base_url('admin','refresh'));
        }
   }

   public function delete_salaryExpense($id)
   {
      $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            $execution=$this->salaryexpensesetupmodel->deleteSalaryExpense($id);
            if(!empty($execution))
            {
                $message['message']="Data Deleted.......!";
                $this->session->set_userdata($message);
                redirect('admin/salaryexpensesetup/viewExpense','refresh');
            }
            else
            {
                $errormessage['errormessage'] = "Not Deleted ......!";
                $this->session->set_userdata($errormessage);
                redirect('admin/salaryexpensesetup/viewExpense','refresh');
            }
        }
        else
        {
            redirect(base_url('admin','refresh'));
        }
   }

   public function edit_salaryExpense($id)
   {
      $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            //$data['allEmployeeId']=$this->employeemodel->getAllEmployeeId();
            $data['salaryInfo']=$this->salaryexpensesetupmodel->getSalaryExpenseByid($id);
            if(!empty($data['salaryInfo']))
            {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/expense/editSalaryExpense',$data);
                $this->load->view('common/footer');
            }
            else
            {
                // $errormessage['errormessage'] = "Invalid Data......!";
                // $this->session->set_userdata($errormessage);
                redirect('admin/salaryexpensesetup/viewExpense','refresh');
            }
        }
        else
        {
            redirect(base_url('admin','refresh'));
        }
   }

   public function update_salaryExpense()
   {
      $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
            if(isset($_POST['btnUpdate']))
            {
                $this->load->helper(array('url','form'));
                $this->load->library('form_validation');
                $config = array(
                  array(
                      'field' => 'data[date]',
                      'label' => 'Date',
                      'rules' => 'required|xss_clean|trim'
                      ),
                      array(
                      'field' => 'data[salary_type]',
                      'label' => 'Salary Type',
                      'rules' => 'required|xss_clean|trim'
                      ),
                      array(
                      'field' => 'data[salaryAmount]',
                      'label' => ' Salary Amount ',
                      'rules' => 'required|xss_clean|trim|numeric'
                      )
                );
                $this->form_validation->set_rules($config);
                if ($this->form_validation->run() == FALSE) {
                    $errormessage['errormessage'] = " Salary Info Missing......!";
                    $this->session->set_userdata($errormessage);
                    //$data['allEmployeeId']=$this->employeemodel->getAllEmployeeId();
                    $this->load->view('common/header');
                    $this->load->view('common/menu');
                    $this->load->view('admin/expense/editSalaryExpense',$data);
                    $this->load->view('common/footer');
                }
                else
                {
                    $data = $this->input->post('data', TRUE);
                    $data['expenseId']=$this->input->post('expenseId', TRUE);
                    if(!empty($data))
                    {
                        $execution=$this->salaryexpensesetupmodel->updateSalaryExpense($data);
                        if(!empty($execution))
                        {
                            $message['message']="Data Updated.......!";
                             $this->session->set_userdata($message);
                             redirect('admin/salaryexpensesetup/viewExpense','refresh');
                        }
                        else
                        {
                            $errormessage['errormessage'] = " Data Not Updated......!";
                            $this->session->set_userdata($errormessage);
                            redirect('admin/salaryexpensesetup/viewExpense','refresh');
                        }
                    }
                    else
                    {
                      $errormessage['errormessage'] = " Salary Info Missing......!";
                      $this->session->set_userdata($errormessage);
                      redirect('admin/salaryexpensesetup/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 invoice($expenseId) {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            $data['SalaryexpenseInvoiceInfo'] = $this->salaryexpensesetupmodel->getinvoiceInfoByid($expenseId);
            //echo '<pre>';print_r($data);die();
            if ($data['SalaryexpenseInvoiceInfo']) {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/expense/salaryExpenseinvoice', $data);
                $this->load->view('common/footer');
            } else {
                redirect(admin_url() . '/expensesetup/viewExpense', 'refresh');
            }
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }

    public function searchSalaryExpense($flag=''){
        if(!empty($flag) && $flag=='month'){
            $date=$this->input->post('data',true);

            if(!empty($date)){
                $data['salary_expense']              = $this->salaryexpensesetupmodel->getSalaryExpenseBydaterange($date);
                $data['total_expense_in_date']       = $this->salaryexpensesetupmodel->getTotalSalaryInDateRange($date);

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