? Fallagassrini

Fallagassrini Bypass Shell

echo"
Fallagassrini
";
Current Path : /home1/savoy/public_html/savoyglobal.net/sgms/application/models/

Linux gator3171.hostgator.com 4.19.286-203.ELK.el7.x86_64 #1 SMP Wed Jun 14 04:33:55 CDT 2023 x86_64
Upload File :
Current File : /home1/savoy/public_html/savoyglobal.net/sgms/application/models/hrmodel.php

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


class Hrmodel extends CI_Model
{
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    function add($postdata)
    {
        $data=array();
        $data['resfunction']=$postdata['resfunction'];
        $table = $postdata['tab_name'];
        unset ($postdata['tab_name']);
        unset ($postdata['resfunction']);
        $res=$this->db->insert($table,$postdata);
        $cnt=$this->db->insert_id();
        if($res>0)
        {
            $data['res']=1;
            $data['msg']='Record Added Successfully';
        }
        else
        {
            $data['res']=0;
            $data['msg']='Error On Adding Record';
        }
        return $data;
    }

    function update($postdata)
    {
        $data=array();
        $data['resfunction']=$postdata['resfunction'];
        $table = $postdata['tab_name'];
        $field = $postdata['tab_field'];
        $id= $postdata['tab_id'];
        unset ($postdata['resfunction']);
        unset ($postdata['tab_name']);
        unset ($postdata['tab_field']);
        unset ($postdata['tab_id']);
        $this->db->where($field, $id);
        $res=$this->db->update($table,$postdata);
        if($res>0)
        {
            $data['res']=1;
            $data['msg']='Record Updated Successfully';
        }
        else
        {
            $data['res']=0;
            $data['msg']='Error On updating Record';
        }
        return $data;
    }

        /*Search function starts here*/

    function search_designation($pagenum)
    {
        $data=array();
        $this->db->from('designation');
        $postdata=$_SESSION['searchdata'];
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            $this->db->like($searchfield, $searchtext);
        }
        $count= $this->db->count_all_results();
        $arr=$this->fpaginate->paginate($pagenum,$count);
        $data['startpage']=$arr[0];
        $data['endpage']=$arr[1];
        $start=$arr[2];
        $perpage=$arr[3];
        $data['count']=$count;
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            $this->db->like($searchfield, $searchtext);
        }
        $this->db->select('*');
        $this->db->from('designation');
        $this->db->limit($perpage, $start);
        $this->db->order_by('designation_id','asc');
        $data['results']=$this->db->get();
        return $data;
    }

    function search_department($pagenum)
    {
        $data=array();
        $this->db->from('department');
        $postdata=$_SESSION['searchdata'];
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            $this->db->like($searchfield, $searchtext);
        }
        $count= $this->db->count_all_results();
        $arr=$this->fpaginate->paginate($pagenum,$count);
        $data['startpage']=$arr[0];
        $data['endpage']=$arr[1];
        $start=$arr[2];
        $perpage=$arr[3];
        $data['count']=$count;
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            $this->db->like($searchfield, $searchtext);
        }
        $this->db->select('*');
        $this->db->from('department');
        $this->db->limit($perpage, $start);
        $this->db->order_by('department_id','asc');
        $data['results']=$this->db->get();
        return $data;
    }
    function search_grade($pagenum)
    {
        $data=array();
        $this->db->from('grade');
        $postdata=$_SESSION['searchdata'];
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            $this->db->like($searchfield, $searchtext);
        }
        $count= $this->db->count_all_results();
        $arr=$this->fpaginate->paginate($pagenum,$count);
        $data['startpage']=$arr[0];
        $data['endpage']=$arr[1];
        $start=$arr[2];
        $perpage=$arr[3];
        $data['count']=$count;
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            $this->db->like($searchfield, $searchtext);
        }
        $this->db->select('*');
        $this->db->from('grade');
        $this->db->limit($perpage, $start);
        $this->db->order_by('grade_id','asc');
        $data['results']=$this->db->get();
        return $data;
    }

    function search_status($pagenum)
    {
        $data=array();
        $this->db->from('status');
        $postdata=$_SESSION['searchdata'];
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            $this->db->like($searchfield, $searchtext);
        }
        $count= $this->db->count_all_results();
        $arr=$this->fpaginate->paginate($pagenum,$count);
        $data['startpage']=$arr[0];
        $data['endpage']=$arr[1];
        $start=$arr[2];
        $perpage=$arr[3];
        $data['count']=$count;
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            $this->db->like($searchfield, $searchtext);
        }
        $this->db->select('*');
        $this->db->from('status');
        $this->db->limit($perpage, $start);
        $this->db->order_by('status_id','asc');
        $data['results']=$this->db->get();
        return $data;
    }
    function search_leave_type($pagenum)
    {
        $data=array();
        $this->db->from('leave_type');
        $postdata=$_SESSION['searchdata'];
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            $this->db->like($searchfield, $searchtext);
        }
        $count= $this->db->count_all_results();
        $arr=$this->fpaginate->paginate($pagenum,$count);
        $data['startpage']=$arr[0];
        $data['endpage']=$arr[1];
        $start=$arr[2];
        $perpage=$arr[3];
        $data['count']=$count;
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            $this->db->like($searchfield, $searchtext);
        }
        $this->db->select('*');
        $this->db->from('leave_type');
        $this->db->limit($perpage, $start);
        $this->db->order_by('leave_type_id','asc');
        $data['results']=$this->db->get();
        return $data;
    }
    function search_special_holiday($pagenum)
    {

        $data=array();
        $this->db->from('timesheet_special_holiday');
        $postdata=$_SESSION['searchdata'];
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            $this->db->like($searchfield, $searchtext);
        }
        $count= $this->db->count_all_results();
        $arr=$this->fpaginate->paginate($pagenum,$count);
        $data['startpage']=$arr[0];
        $data['endpage']=$arr[1];
        $start=$arr[2];
        $perpage=$arr[3];
        $data['count']=$count;
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            $this->db->like($searchfield, $searchtext);
        }
        $this->db->select('*');
        $this->db->from('timesheet_special_holiday');
        $this->db->limit($perpage, $start);
        $this->db->order_by('special_holiday_id','desc');
        $data['results']=$this->db->get();
        return $data;
    }
    function search_allowdeduc($pagenum)
    {
        $data=array();
        $this->db->from('allowdeduc');
        $postdata=$_SESSION['searchdata'];
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            $this->db->like($searchfield, $searchtext);
        }
        $count= $this->db->count_all_results();
        $arr=$this->fpaginate->paginate($pagenum,$count);
        $data['startpage']=$arr[0];
        $data['endpage']=$arr[1];
        $start=$arr[2];
        $perpage=$arr[3];
        $data['count']=$count;
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            $this->db->like($searchfield, $searchtext);
        }
        $this->db->select('*');
        $this->db->from('allowdeduc');
        $this->db->limit($perpage, $start);
        $this->db->order_by('allowdeduc_id','asc');
        $data['results']=$this->db->get();
        return $data;
    }
    function search_employee($pagenum)
    {
        $data=array();
        if($this->mastermodel->check_private())
        {
            $flag=1;
        }
        else
        {
            $flag=0;
        }
        $this->db->from('employee');
        $this->db->join('master','masterid=employee_id');
        $this->db->join('department','department.department_id=employee.employee_department_id');
        $this->db->join('designation','designation.designation_id=employee.employee_designation_id');
        $postdata=$_SESSION['searchdata'];
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            if($searchfield == 'employee_visa_expiry_date')
            {
                $searchtext=$this->mastermodel->convdatformat($searchtext);
            }
            $this->db->like($searchfield, $searchtext);
        }
        $count= $this->db->count_all_results();
        $arr=$this->fpaginate->paginate($pagenum,$count);
        $data['startpage']=$arr[0];
        $data['endpage']=$arr[1];
        $start=$arr[2];
        $perpage=$arr[3];
        $data['count']=$count;
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            if($searchfield == 'employee_visa_expiry_date')
            {
                $searchtext=$this->mastermodel->convdatformat($searchtext);
            }
            $this->db->like($searchfield, $searchtext);
        }
        $this->db->select('*');
        $this->db->from('employee');
        $this->db->join('master','masterid=employee_id');
        $this->db->join('department','department.department_id=employee.employee_department_id');
        $this->db->join('designation','designation.designation_id=employee.employee_designation_id');
        if ($flag==1)
        {
            $this->db->where("employee.employee_id IN (".$_SESSION['employees'].")", NULL, FALSE);
        }
        $this->db->order_by('employee.employee_no','asc');
        $data['results']=$this->db->get();
        return $data;
    }

    function search_offer_letter($pagenum)
    {
        $data=array();
        $this->db->from('offer_letter');
        $this->db->join('master','masterid=offer_letter_id');
        $this->db->join('designation','designation.designation_id=offer_letter.designation_id');
        $postdata=$_SESSION['searchdata'];
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            if($searchfield == 'joining_date')
            {
                $searchtext=$this->mastermodel->convdatformat($searchtext);
            }
            $this->db->like($searchfield, $searchtext);
        }
        $count= $this->db->count_all_results();
        $arr=$this->fpaginate->paginate($pagenum,$count);
        $data['startpage']=$arr[0];
        $data['endpage']=$arr[1];
        $start=$arr[2];
        $perpage=$arr[3];
        $data['count']=$count;
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            if($searchfield == 'joining_date')
            {
                $searchtext=$this->mastermodel->convdatformat($searchtext);
            }
            $this->db->like($searchfield, $searchtext);
        }
        $this->db->select('*');
        $this->db->from('offer_letter');
        $this->db->join('master','masterid=offer_letter_id');
        $this->db->join('designation','designation.designation_id=offer_letter.designation_id');

        $this->db->order_by('joining_date','asc');
        $data['results']=$this->db->get();
        return $data;
    }

    function search_resume_management($pagenum)
    {
        $data=array();
        $this->db->from('resume_management');
        $this->db->join('master','masterid=resume_management_id');
        $postdata=$_SESSION['searchdata'];
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            if($searchfield == 'submission_date')
            {
                $searchtext=$this->mastermodel->convdatformat($searchtext);
            }
            $this->db->like($searchfield, $searchtext);
        }
        $count= $this->db->count_all_results();
        $arr=$this->fpaginate->paginate($pagenum,$count);
        $data['startpage']=$arr[0];
        $data['endpage']=$arr[1];
        $start=$arr[2];
        $perpage=$arr[3];
        $data['count']=$count;
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            if($searchfield == 'submission_date')
            {
                $searchtext=$this->mastermodel->convdatformat($searchtext);
            }
            $this->db->like($searchfield, $searchtext);
        }
        $this->db->select('*');
        $this->db->from('resume_management');
        $this->db->join('master','masterid=resume_management_id');

        $this->db->order_by('submission_date','asc');
        $data['results']=$this->db->get();
        return $data;
    }

    function search_leave_allocation($pagenum)
    {
        $data=array();
        if($this->mastermodel->check_private())
        {
            $flag=1;
        }
        else
        {
            $flag=0;
        }
        $this->db->from('leave_allocation');
        $this->db->join('master','masterid=leave_allocation_id');
        $this->db->join('employee','employee.employee_id=leave_allocation.employee_id');
        $postdata=$_SESSION['searchdata'];
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            
            $this->db->like($searchfield, $searchtext);
        }
        $count= $this->db->count_all_results();
        $arr=$this->fpaginate->paginate($pagenum,$count);
        $data['startpage']=$arr[0];
        $data['endpage']=$arr[1];
        $start=$arr[2];
        $perpage=$arr[3];
        $data['count']=$count;
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
          
            $this->db->like($searchfield, $searchtext);
        }
        $this->db->select('*');
        $this->db->from('leave_allocation');
        $this->db->join('master','masterid=leave_allocation_id');
        $this->db->join('employee','employee.employee_id=leave_allocation.employee_id');
        if ($flag==1)
        {
            $this->db->where("employee.employee_id IN (".$_SESSION['employees'].")", NULL, FALSE);
        }
        $data['results']=$this->db->get();
        return $data;
    }
     function search_leave_application($pagenum)
    {
        $data=array();
        if($this->mastermodel->check_private())
        {
            $flag=1;
        }
        else
        {
            $flag=0;
        }
        $this->db->from('leave_application');
        $this->db->join('master','masterid=leave_application_id');
        $this->db->join('employee','employee.employee_id=leave_application.employee_id');
        $this->db->join('leave_type','leave_type.leave_type_id=leave_application.leave_type_id');
        $postdata=$_SESSION['searchdata'];
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];

            $this->db->like($searchfield, $searchtext);
        }
        $count= $this->db->count_all_results();
        $arr=$this->fpaginate->paginate($pagenum,$count);
        $data['startpage']=$arr[0];
        $data['endpage']=$arr[1];
        $start=$arr[2];
        $perpage=$arr[3];
        $data['count']=$count;
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];

            $this->db->like($searchfield, $searchtext);
        }
        $this->db->select('*');
        $this->db->from('leave_application');
        $this->db->join('master','masterid=leave_application_id');
        $this->db->join('employee','employee.employee_id=leave_application.employee_id');
        $this->db->join('leave_type','leave_type.leave_type_id=leave_application.leave_type_id');
        if ($flag==1)
        {
            $this->db->where("employee.employee_id IN (".$_SESSION['employees'].")", NULL, FALSE);
        }
        $data['results']=$this->db->get();
       
        return $data;
    }

    function search_employee_salary($pagenum)
    {
        $data=array();
        if($this->mastermodel->check_private())
        {
            $flag=1;
        }
        else
        {
            $flag=0;
        }
        $this->db->from('employee_salary');
        $this->db->join('master','masterid=employee_salary_id');
        $this->db->join('employee','employee.employee_id=employee_salary.employee_id');
        $postdata=$_SESSION['searchdata'];
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            if($searchfield == 'wef_date')
            {
                $searchtext=$this->mastermodel->convdatformat($searchtext);
            }
            $this->db->like($searchfield, $searchtext);
        }
        $count= $this->db->count_all_results();
        $arr=$this->fpaginate->paginate($pagenum,$count);
        $data['startpage']=$arr[0];
        $data['endpage']=$arr[1];
        $start=$arr[2];
        $perpage=$arr[3];
        $data['count']=$count;
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            if($searchfield == 'wef_date')
            {
                $searchtext=$this->mastermodel->convdatformat($searchtext);
            }
            $this->db->like($searchfield, $searchtext);
        }
        $this->db->select('*');
        $this->db->from('employee_salary');
        $this->db->join('master','masterid=employee_salary_id');
        $this->db->join('employee','employee.employee_id=employee_salary.employee_id');
        if ($flag==1)
        {
            $this->db->where("employee.employee_id IN (".$_SESSION['employees'].")", NULL, FALSE);
        }
        $this->db->order_by('wef_date','desc');
        $data['results']=$this->db->get();
        return $data;
    }

    function search_salary_payment($pagenum)
    {
        $data=array();
        $data['startpage']='';
        $data['endpage']='';
        $data['count']='';
        $data['results']='';
        return $data;
    }
    
    function search_attendance_marking($pagenum)
    {
        $data=array();
        $data['startpage']='';
        $data['endpage']='';
        $data['count']='';
        $data['results']='';
        return $data;
    }

    function search_payslip($pagenum)
    {
        $data=array();
        $data['startpage']='';
        $data['endpage']='';
        $data['count']='';
        $data['results']='';
        return $data;
    }

    function search_weekly_holiday($pagenum)
    {
        $data=array();
        $count= 1;
        $arr=$this->fpaginate->paginate($pagenum,$count);
        $data['startpage']=$arr[0];
        $data['endpage']=$arr[1];
        $start=$arr[2];
        $perpage=$arr[3];
        $data['count']=$count;
        $data['results']='';
        return $data;
    }
    
    function search_update_attendance($pagenum)
    {
        $data=array();
        $count= 1;
        $arr=$this->fpaginate->paginate($pagenum,$count);
        $data['startpage']=$arr[0];
        $data['endpage']=$arr[1];
        $start=$arr[2];
        $perpage=$arr[3];
        $data['count']=$count;
        $data['results']='';
        return $data;
    }

    function search_mark_attendance($pagenum)
    {
        $data=array();
        $count= 1;
        $arr=$this->fpaginate->paginate($pagenum,$count);
        $data['startpage']=$arr[0];
        $data['endpage']=$arr[1];
        $start=$arr[2];
        $perpage=$arr[3];
        $data['count']=$count;
        $data['results']='';
        return $data;
    }
    
    function search_work_shift($pagenum)
    {
        $data=array();
        $this->db->from('timesheet_work_shift');
        $postdata=$_SESSION['searchdata'];
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            $this->db->like($searchfield, $searchtext);
        }
        $count= $this->db->count_all_results();
        $arr=$this->fpaginate->paginate($pagenum,$count);
        $data['startpage']=$arr[0];
        $data['endpage']=$arr[1];
        $start=$arr[2];
        $perpage=$arr[3];
        $data['count']=$count;
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            $this->db->like($searchfield, $searchtext);
        }
        $this->db->select('*');
        $this->db->from('timesheet_work_shift');
        $this->db->limit($perpage, $start);
        $this->db->order_by('work_shift_id','desc');
        $data['results']=$this->db->get();
        return $data;
    }
    
    function search_attendance_report($pagenum)
    {
        $data=array();
        $this->db->from('report');
        $this->db->where('report_module_id',$_SESSION['current_mode']);
        $postdata=$_SESSION['searchdata'];
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            $this->db->like($searchfield, $searchtext);
        }
        $count= $this->db->count_all_results();
        $arr=$this->fpaginate->paginate($pagenum,$count);
        $data['startpage']=$arr[0];
        $data['endpage']=$arr[1];
        $start=$arr[2];
        $perpage=$arr[3];
        $data['count']=$count;
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            $this->db->like($searchfield, $searchtext);
        }
        $this->db->select('*');
        $this->db->from('report');
        $this->db->where('report_module_id',$_SESSION['current_mode']);
        $this->db->limit($perpage, $start);
        $this->db->order_by('report_sort','asc');
        $data['results']=$this->db->get();
        return $data;
    }
    /*Search function ends here*/

    /*  Add functions starts here*/

    function insert_employee($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $masterid = $this->mastermodel->insertmasterdata();
        $expiry_date = $this->mastermodel->convertdateformat($postdata['employee_visa_expiry_date']);
        $dob = $this->mastermodel->convertdateformat($postdata['employee_dob']);
        $labour_date= $this->mastermodel->convertdateformat($postdata['employee_labour_card_expiry_date']);
        $tblValues=array('employee_id'=>$masterid,
                        'employee_no'=> $postdata['employee_no'],
                        'employee_name'=> $postdata['employee_name'],
                        'employee_gender'=> $postdata['employee_gender'],
                        'employee_dob'=> $dob,
                        'employee_joining_date'=> $this->mastermodel->convertdateformat($postdata['employee_joining_date']),
                        'employee_nationality'=> $postdata['employee_nationality'],
                        'employee_branch_id'=> $postdata['employee_branch_id'],
                        'employee_designation_id'=> $postdata['employee_designation_id'],
                        'employee_department_id'=> $postdata['employee_department_id'],
                        'employee_grade_id'=> $postdata['employee_grade_id'],
                        'employee_status_id'=> $postdata['employee_status_id'],
                        'employee_local_address'=> $postdata['employee_local_address'],
                        'employee_permanent_address'=> $postdata['employee_permanent_address'],
                        'employee_contact_no'=> $postdata['employee_contact_no'],
                        'employee_email'=> $postdata['employee_email'],
                        'employee_passport_no'=> $postdata['employee_passport_no'],
                        'employee_visa_type'=> $postdata['employee_visa_type'],
                        'employee_visa_expiry_date'=>$expiry_date,
                        'employee_labour_card_no'=>$postdata['employee_labour_card_no'],
                        'device_no'=>$postdata['device_no'],
                        'employee_labour_card_expiry_date'=>$labour_date,
                        'employee_reporting_to'=>$postdata['employee_id']);
        $this->db->insert('employee', $tblValues);

        $parent= $this->accountingmodel->get_account_settings('employee','sub_account_settings');
 
        if($this->mastermodel->get_num_rows('chart_master','chart_account_code',$postdata['employee_no'])==0)
        {
            $tblValues=array('chart_account_code'=>$postdata['employee_no'],
                        'chart_account_name'=>$postdata['employee_name'],
                        'chart_account_type'=>3,
                        'parent'=>$parent,
                        'inactive'=>0);
            $this->db->insert('chart_master', $tblValues);
        }
        else
        {
            $tblValues=array(
                        'chart_account_name'=>$postdata['employee_name'],
                        'chart_account_type'=>3,
                        'parent'=>$parent,
                        'inactive'=>0);
            $this->db->where('chart_account_code', $postdata['employee_no']);
            $this->db->update('chart_master', $tblValues);
        }
        
        $i=1;
        while(isset($postdata['course'.$i]))
        {
            $tblValues=array(
                            'employee_id'=>$masterid,
                            'course'=>$postdata['course'.$i],
                            'university'=>$postdata['university'.$i],
                            'yearofpassing'=>$postdata['yearofpassing'.$i]);
            $this->db->insert('employee_educational_qualification', $tblValues);
            $i++;
        }
         $i=1;
        while(isset($postdata['employer'.$i]))
        {
            $tblValues=array(
                            'employee_id'=>$masterid,
                            'employer'=>$postdata['employer'.$i],
                            'designation'=>$postdata['designation'.$i],
                            'period'=>$postdata['period'.$i]);
            $this->db->insert('employee_professional_experience', $tblValues);
            $i++;
        }
         $i=1;
        while(isset($postdata['course_add'.$i]))
        {
            $tblValues=array(
                            'employee_id'=>$masterid,
                            'course'=>$postdata['course_add'.$i],
                            'institute'=>$postdata['institute'.$i],
                            'yearofpassing'=>$postdata['year_passing'.$i]);
            $this->db->insert('employee_additional_certification', $tblValues);
            $i++;
        }

        $data['resfunction']='search_employee';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Record Added Successfully';
            return $data;
        }
    }
    
    function uploadfile($postdata)
    {
         $this->db->trans_begin();
         $tbl1Values=array('employee_id'=>$postdata['employee_id'],
             'file_name'=>$postdata['file_name'],
             'file_location'=>$postdata['file_location']);
          $this->db->insert('employee_file', $tbl1Values);
          $this->db->trans_commit();
    }

    function insert_offer_letter($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $masterid = $this->mastermodel->insertmasterdata();
        $interview_date = $this->mastermodel->convertdateformat($postdata['interview_date']);
        $joining_date = $this->mastermodel->convertdateformat($postdata['joining_date']);
        $tblValues=array('offer_letter_id'=>$masterid,
                        'candidate_name'=> $postdata['candidate_name'],
                        'candidate_email'=> $postdata['candidate_email'],
                        'interview_date'=>  $interview_date,
                        'joining_date'=> $joining_date,
                        'contact_no'=> $postdata['contact_no'],
                        'designation_id'=> $postdata['designation_id'],
                        'filename'=> $postdata['filename']);
        $this->db->insert('offer_letter', $tblValues);
        $data['resfunction']='search_offer_letter';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Record Added Successfully';
            return $data;
        }
    }

    function insert_resume_management($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $masterid = $this->mastermodel->insertmasterdata();
        $submit_date = $this->mastermodel->convertdateformat($postdata['submission_date']);
        $tblValues=array('resume_management_id'=>$masterid,
                        'candidate_name'=> $postdata['candidate_name'],
                        'qualification'=> $postdata['qualification'],
                        'email'=> $postdata['email'],
                        'submission_date'=>  $submit_date,
                        'expected_salary'=> $postdata['expected_salary'],
                        'experience'=> $postdata['experience'],
                        'contact_no'=> $postdata['contact_no'],
                        'filename'=> $postdata['filename']);
        $this->db->insert('resume_management', $tblValues);
        $data['resfunction']='search_resume_management';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Record Added Successfully';
            return $data;
        }
    }
    function insert_leave_allocation($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $masterid = $this->mastermodel->insertmasterdata();
        $tblValues=array('leave_allocation_id'=>$masterid,
                        'employee_id'=> $postdata['employee_id'],
                        'leave_allocation_year'=> $postdata['leave_allocation_year']);
        $this->db->insert('leave_allocation', $tblValues);
        $i=1;
        while(isset($postdata['leave_type_id_'.$i]))
        {
             $tblValues=array('leave_allocation_id'=>$masterid,
                        'leave_type_id'=>$postdata['leave_type_id_'.$i],
                        'leave_allocation_days'=> $postdata['leave_allocation_days_'.$i]);
            $this->db->insert('leave_allocation_detail', $tblValues);
            $i++;
        }
        $data['resfunction']='search_leave_allocation';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Record Added Successfully';
            return $data;
        }
    }
     function insert_leave_application($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $masterid = $this->mastermodel->insertmasterdata();
        $fromdate = $this->mastermodel->convertdateformat($postdata['from_date']);
        $todate = $this->mastermodel->convertdateformat($postdata['to_date']);
        $tblValues=array('leave_application_id'=>$masterid,
                        'employee_id'=> $postdata['employee_id'],
                        'leave_type_id'=> $postdata['leave_type_id'],
                        'from_date'=> $fromdate,
                        'to_date'=> $todate,
                        'remarks'=> $postdata['remarks']);
        $this->db->insert('leave_application', $tblValues);
        $data['resfunction']='search_leave_application';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Record Added Successfully';
            return $data;
        }
    }

    function insert_employee_salary($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $masterid = $this->mastermodel->insertmasterdata();
        $wef_date = $this->mastermodel->convertdateformat($postdata['wef_date']);
        if(isset($postdata['return_ticket']))
        {
            $return_ticket=1;
        }
        else
        {
            $return_ticket=0;
        }
        $tblValues=array('employee_salary_id'=>$masterid,
                        'employee_id' => $postdata['employee_id'],
                        'wef_date'=> $wef_date,
                        'basic_salary'=> $postdata['basic_salary'],
                        'from_ticket'=> $postdata['from_ticket'],
                        'to_ticket'=> $postdata['to_ticket'],
                        'return_ticket'=> $return_ticket,
                        'no_tickets'=> $postdata['no_tickets'],
                        
                        'payment_method'=> $postdata['payment_method']);
        $this->db->insert('employee_salary', $tblValues);
        $i=1;
        while(isset($postdata['amount_'.$i]))
        {
           $tblValues=array(
                            'employee_salary_id'=>$masterid,
                            'allowdeduc_id'=>$postdata['allowdeduc_id_'.$i],
                            'amount'=>$postdata['amount_'.$i]);
           $this->db->insert('employee_salary_allowdeduc', $tblValues);
           $i++;
        }
        $i=1;
        while(isset($postdata['type'.$i]))
        {
            $tblValues=array(
                            'employee_salary_id'=>$masterid,
                            'type'=>$postdata['type'.$i],
                            'company'=>$postdata['company'.$i],
                            'policy'=>$postdata['policy'.$i],
                            'plan'=>$postdata['plan'.$i],
                            'sum'=>$postdata['sum'.$i]);
            $this->db->insert('employee_salary_medical', $tblValues);
            $i++;
        }
        $data['resfunction']='search_employee_salary';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Record Added Successfully';
            return $data;
        }
    }

    function insert_single_salary_payment($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $masterid = $this->mastermodel->insertmasterdata();
        $salary_date = $this->mastermodel->convertdateformat($postdata['salary_date']);
        $pay_date=date('Y-m-d');
        $netpayment=$postdata['basic_salary']+$postdata['allowance']-$postdata['deduction'];
        $tblValues=array('employee_salary_slip_id'=>$masterid,
                        'employee_id' => $postdata['employee_id'],
                        'salary_date'=> $salary_date,
                        'pay_date'=> $pay_date,
                        'basic_salary'=> $postdata['basic_salary'],
                        'allowance_amount'=> $postdata['allowance'],
                        'deduction_amount'=> $postdata['deduction'],
                        'net_payment'=> $netpayment,
                        'commission_amount'=> $postdata['commission'],
                        'paid_amount'=> $postdata['payamount']
                        );
        $this->db->insert('employee_salary_slip', $tblValues);
        $i=1;
        while(isset($postdata['amount_'.$i]))
        {
           $tblValues=array(
                            'employee_salary_slip_id'=>$masterid,
                            'allowdeduc_id'=>$postdata['allowdeduc_id_'.$i],
                            'amount'=>$postdata['amount_'.$i]);
           $this->db->insert('employee_salary_slip_allowdeduc', $tblValues);
           $i++;
        }
        
        $data['resfunction']='search_salary_payment';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Employee Salary Processed successfully';
            return $data;
        }
    }

    function insert_multiple_salary_payment($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $salary_date = $this->mastermodel->convertdateformat($postdata['salary_date']);
        $pay_date=date('Y-m-d');
        if(isset ($postdata['salempdet']))
        {
                for($i=0;$i<sizeof($postdata['salempdet']);$i++)
                {
                    if(isset($postdata['salempdet'][$i]))
                    {
                        $masterid = $this->mastermodel->insertmasterdata();
                        $employee_id=$postdata['salempdet'][$i];
                        $salary_payment=$this->get_employee_salary_data('employee_salary',$employee_id,$postdata['salary_date']);
                        $basic_salary = $salary_payment->basic_salary;
                        $employee_salary_id=$salary_payment->employee_salary_id;
                        $allowdeduc_details=$this->mastermodel->get_data('employee_salary_allowdeduc',$employee_salary_id,'employee_salary_id');
                        $tblValues=array('employee_salary_slip_id'=>$masterid,
                                'employee_id' => $employee_id,
                                'salary_date'=> $salary_date,
                                'pay_date'=> $pay_date,
                                'basic_salary'=> $basic_salary,
                                );
                        $this->db->insert('employee_salary_slip', $tblValues);

                        $allowance=0;
                        $deduction=0;
                        foreach ($allowdeduc_details as $list)
                        {
                            $tblValues=array(
                                'employee_salary_slip_id'=>$masterid,
                                'allowdeduc_id'=>$list['allowdeduc_id'],
                                'amount'=>$list['amount']);
                            $res=$this->db->insert('employee_salary_slip_allowdeduc', $tblValues);
                          $allowdeductype=$this->mastermodel->get_data_srow('allowdeduc',$list['allowdeduc_id'],'allowdeduc_id');
                          if($allowdeductype->allowdeduc_type==1)  //allowance
                          {
                              if($allowdeductype->allowdeduc_mode==1)
                              {
                                  $sf=1;
                                  $amount = $list['amount'] * $sf;
                                  $asubtotal= $amount;
                              }
                              else
                              {
                                   $sf=0.01;
                                   $amount= $list['amount'] * $sf;
                                   $asubtotal= $basic_salary *$amount;
                              }
                              $allowance+=$asubtotal;
                          }
                          else
                          {
                              if($allowdeductype->allowdeduc_mode==1)
                              {

                                  $sf=1;
                                  $amount= $list['amount'] * $sf;
                                  $dsubtotal= $amount;
                              }
                              else
                              {

                                   $sf=0.01;
                                   $amount= $list['amount'] * $sf;
                                   $dsubtotal= $basic_salary *$amount;
                              }
                              $deduction+=$dsubtotal;
                          }
                     }
                     $netpayment=$basic_salary+$allowance-$deduction;
                     $tblValues=array('allowance_amount'=>$allowance,'deduction_amount'=>$deduction,'net_payment'=>$netpayment,'paid_amount'=>$netpayment);
                     $this->db->where('employee_salary_slip_id',$masterid);
                     $res=$this->db->update('employee_salary_slip', $tblValues);
                  }
              }
                $data['resfunction']='search_salary_payment';
                if ($this->db->trans_status() === FALSE)
                {
                    $this->db->trans_rollback();
                    $data['res']=0;
                    $data['msg']='Error On Adding Record';
                    return $data;
                }
                else
                {
                    $this->db->trans_commit();
                    $data['res']=1;
                    $data['msg']='Salary processed succesfully';
                    return $data;
                }
        }
        else
        {
                    $data['resfunction']='search_salary_payment';
                    $this->db->trans_rollback();
                    $data['res']=1;
                    $data['msg']='No employee selected';
                    return $data;
        }
    }

    function insert_single_posted_salary_payment($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $this->db->where('employee_salary_slip_id',$postdata['employee_salary_slip_id']);
        $this->db->delete('employee_salary_slip');
        $this->db->where('employee_salary_slip_id',$postdata['employee_salary_slip_id']);
        $this->db->delete('employee_salary_slip_allowdeduc');
        
        $masterid = $this->mastermodel->insertmasterdata();
        $salary_date = $this->mastermodel->convertdateformat($postdata['salary_date']);
        $pay_date=date('Y-m-d');
        $netpayment=$postdata['basic_salary']+$postdata['allowance']-$postdata['deduction'];
        $tblValues=array('employee_salary_slip_id'=>$masterid,
                        'employee_id' => $postdata['employee_id'],
                        'salary_date'=> $salary_date,
                        'pay_date'=> $pay_date,
                        'basic_salary'=> $postdata['basic_salary'],
                        'allowance_amount'=> $postdata['allowance'],
                        'deduction_amount'=> $postdata['deduction'],
                        'net_payment'=> $netpayment,
                        'commission_amount'=> $postdata['commission'],
                        'paid_amount'=> $postdata['payamount'],
                        'unpost'=>0
                        );
        $this->db->insert('employee_salary_slip', $tblValues);
        

        $reference_code= $this->accountingmodel->getreferencecode(3);

        $ref_id = $this->mastermodel->insertmasterdata();
        $refdata=array('ref_id'=>$ref_id,
                        'ref_transaction_type_id'=>'3',
                        'ref_code'=>$reference_code);
        $this->db->insert('refs', $refdata);
        
        $fiscal_year_id=$this->mastermodel->getSingleFieldValue('company','fiscal_year_id','company_id','1');

        $audittraildata=array('ref_id'=>$ref_id,
                              'transaction_date'=>date("Y-m-d"),
                              'fiscal_year'=>$fiscal_year_id,
                              );
        $this->db->insert('audit_trail', $audittraildata);
        
        
        $i=1;
        while(isset($postdata['amount_'.$i]))
        {
            
           $allowaccount=$this->mastermodel->getSingleFieldValue('allowdeduc','chart_account_code','allowdeduc_id',$postdata['allowdeduc_id_'.$i]);

           if($postdata['mode_id_'.$i]=='Deduction')
                   $postdata['alltotal_'.$i]=0-$postdata['alltotal_'.$i];
           
            $tblValues=array('ref_id'=>$ref_id,
                        'payment_date'=>date("Y-m-d"),
                        'account_code'=>$allowaccount,
                        'amount'=>$postdata['alltotal_'.$i],
                        'memo'=>$postdata['memo'],
                        'unpost'=>1);
            $this->db->insert('unposted_journal', $tblValues);
           $tblValues=array('ref_id'=>$ref_id,
                            'transaction_date'=>date("Y-m-d"),
                            'account_code'=>$allowaccount,
                            'memo'=>$postdata['memo'],
                            'amount'=>$postdata['alltotal_'.$i]);
           $str= $this->db->insert('gl_trans', $tblValues);  
           $tblValues=array(
                            'employee_salary_slip_id'=>$masterid,
                            'allowdeduc_id'=>$postdata['allowdeduc_id_'.$i],
                            'amount'=>$postdata['amount_'.$i]);
           $this->db->insert('employee_salary_slip_allowdeduc', $tblValues);
           $i++;
        }
        
        $tblValues=array('ref_id'=>$ref_id,
                        'payment_date'=>date("Y-m-d"),
                        'account_code'=>$postdata['empaccount'],
                        'amount'=>-$postdata['payamount'],
                        'memo'=>$postdata['memo'],
                        'unpost'=>1);
        $this->db->insert('unposted_journal', $tblValues);
        
        $tblValues=array('ref_id'=>$ref_id,
                        'transaction_date'=>date("Y-m-d"),
                        'account_code'=>$postdata['empaccount'],
                        'memo'=>$postdata['memo'],
                        'amount'=>-$postdata['payamount']);
        $this->db->insert('gl_trans', $tblValues);
        
        
        $commission_acc=$this->mastermodel->getSingleFieldValue('main_account_settings','commission','main_account_settings_id','1');

        $salary_acc=$this->mastermodel->getSingleFieldValue('sub_account_settings','salary','sub_account_settings_id','1');
        
        $tblValues=array('ref_id'=>$ref_id,
                        'payment_date'=>date("Y-m-d"),
                        'account_code'=>$salary_acc,
                        'amount'=>$postdata['basic_salary'],
                        'memo'=>$postdata['memo'],
                        'unpost'=>1);
        $this->db->insert('unposted_journal', $tblValues);
        
        $tblValues=array('ref_id'=>$ref_id,
                        'transaction_date'=>date("Y-m-d"),
                        'account_code'=>$salary_acc,
                        'memo'=>$postdata['memo'],
                        'amount'=>$postdata['basic_salary']);
        $this->db->insert('gl_trans', $tblValues);
        
        

        if(!empty($postdata['commission']))
        {
            $tblValues=array('ref_id'=>$ref_id,
                        'payment_date'=>date("Y-m-d"),
                        'account_code'=>$commission_acc,
                        'amount'=>$postdata['commission'],
                        'memo'=>'Commission Paid',
                        'unpost'=>1);
             $this->db->insert('unposted_journal', $tblValues);
            
            $tblValues=array('ref_id'=>$ref_id,
                            'transaction_date'=>date("Y-m-d"),
                            'account_code'=>$commission_acc,
                            'memo'=>'Commission Paid',
                            'amount'=>$postdata['commission']);
            $this->db->insert('gl_trans', $tblValues);    
       
        }

        /*Second Transaction*/
        
        $reference_code= $this->accountingmodel->getreferencecode(1);

        $ref_id = $this->mastermodel->insertmasterdata();
        $refdata=array('ref_id'=>$ref_id,
                        'ref_transaction_type_id'=>'1',
                        'ref_code'=>$reference_code);
        $this->db->insert('refs', $refdata);
        
      
        $audittraildata=array('ref_id'=>$ref_id,
                              'transaction_date'=>date("Y-m-d"),
                              'fiscal_year'=>$fiscal_year_id,
                              );
        $this->db->insert('audit_trail', $audittraildata);
        
        $tblValues=array('ref_id'=>$ref_id,
                        'payment_date'=>date("Y-m-d"),
                        'account_code'=>$postdata['empaccount'],
                        'amount'=>$postdata['payamount'],
                        'memo'=>$postdata['memo'],
                        'unpost'=>1);
        $this->db->insert('unposted_journal', $tblValues);
        
       $tblValues=array('ref_id'=>$ref_id,
                        'transaction_date'=>date("Y-m-d"),
                        'account_code'=>$postdata['empaccount'],
                        'memo'=>$postdata['memo'],
                        'amount'=>$postdata['payamount']);
        $this->db->insert('gl_trans', $tblValues);
        
        $tblValues=array('ref_id'=>$ref_id,
                        'payment_date'=>date("Y-m-d"),
                        'account_code'=>$postdata['bank_account_code'],
                        'amount'=>-$postdata['payamount'],
                        'memo'=>$postdata['memo'],
                        'unpost'=>1);
        $this->db->insert('unposted_journal', $tblValues);
        
        
        $tblValues=array('ref_id'=>$ref_id,
                        'transaction_date'=>date("Y-m-d"),
                        'account_code'=>$postdata['bank_account_code'],
                        'memo'=>$postdata['memo'],
                        'amount'=>-$postdata['payamount']);
        $this->db->insert('gl_trans', $tblValues);
        
        $tblValues3=array('ref_id'=>$ref_id,
                         'bank_account_code'=>$postdata['bank_account_code'],
                       
                         'transaction_date'=>date("Y-m-d"),
                         'amount'=>-$postdata['payamount'],
                         );
        $query= $this->db->insert('bank_transaction', $tblValues3);
        

        
        $data['resfunction']='search_payslip';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Employee Salary Processed successfully';
            return $data;
        }
    }
    
    function insert_multiple_posted_salary_payment($postdata)
    {
        
        
        $data=array();
        $this->db->trans_begin();
        $paydate=date("Y-m-d");
        
        $memo=$postdata['memo'];
        $bankaccount=$postdata['bank_account_code'];
        
        if(isset($postdata['salempdet']))
        {
               
            for($i=0;$i<sizeof($postdata['salempdet']);$i++)
            {
                if(isset($postdata['salempdet'][$i]))
                {
                            $salid=$postdata['salempdet'][$i];



                            $tblValues=array(
                                'unpost'=>0
                                );
                            $this->db->where('employee_salary_slip_id',$salid);
                            $this->db->update('employee_salary_slip', $tblValues);


                            $reference_code= $this->accountingmodel->getreferencecode(3);

                            $ref_id = $this->mastermodel->insertmasterdata();
                            $refdata=array('ref_id'=>$ref_id,
                                            'ref_transaction_type_id'=>'3',
                                            'ref_code'=>$reference_code);
                            $this->db->insert('refs', $refdata);

                            $fiscal_year_id=$this->mastermodel->getSingleFieldValue('company','fiscal_year_id','company_id','1');

                            $audittraildata=array('ref_id'=>$ref_id,
                                                'transaction_date'=>date("Y-m-d"),
                                                'fiscal_year'=>$fiscal_year_id,
                                                );
                            $this->db->insert('audit_trail', $audittraildata);


                            $this->db->query("insert into unposted_journal select '', '$ref_id' , '$paydate',chart_account_code,if(allowdeduc_mode<1,if(allowdeduc_type<1,-(basic_salary*amount)/100,-amount),amount),'$memo',1 from employee_salary_slip_allowdeduc
                                            join allowdeduc on allowdeduc.allowdeduc_id=employee_salary_slip_allowdeduc.allowdeduc_id 
                                            join employee_salary_slip on employee_salary_slip.employee_salary_slip_id=employee_salary_slip_allowdeduc.employee_salary_slip_id 
                                            where employee_salary_slip.employee_salary_slip_id='$salid'");

                            $this->db->query("insert into gl_trans select '','$ref_id' , '$paydate',chart_account_code,'$memo',if(allowdeduc_mode<1,if(allowdeduc_type<1,-(basic_salary*amount)/100,-amount),amount) from employee_salary_slip_allowdeduc
                                            join allowdeduc on allowdeduc.allowdeduc_id=employee_salary_slip_allowdeduc.allowdeduc_id 
                                            join employee_salary_slip on employee_salary_slip.employee_salary_slip_id=employee_salary_slip_allowdeduc.employee_salary_slip_id 
                                            where employee_salary_slip.employee_salary_slip_id='$salid'");

                            $this->db->query("insert into unposted_journal select '','$ref_id' , '$paydate',employee_no,0-paid_amount,'$memo',1 from employee_salary_slip
                                            join employee on employee_salary_slip.employee_id=employee.employee_id where employee_salary_slip_id='$salid'");


                            $this->db->query("insert into gl_trans select '','$ref_id' , '$paydate',employee_no,'$memo',0-paid_amount from employee_salary_slip
                                            join employee on employee_salary_slip.employee_id=employee.employee_id where employee_salary_slip_id='$salid'");


                            $commission_acc=$this->mastermodel->getSingleFieldValue('main_account_settings','commission','main_account_settings_id','1');

                            $salary_acc=$this->mastermodel->getSingleFieldValue('sub_account_settings','salary','sub_account_settings_id','1');


                            $this->db->query("insert into unposted_journal select '','$ref_id' , '$paydate','$salary_acc',basic_salary,'$memo',1 from employee_salary_slip
                                            join employee on employee_salary_slip.employee_id=employee.employee_id where employee_salary_slip_id='$salid'");


                            $this->db->query("insert into gl_trans select '','$ref_id' , '$paydate',$salary_acc,'$memo',basic_salary from employee_salary_slip
                                            join employee on employee_salary_slip.employee_id=employee.employee_id where employee_salary_slip_id='$salid'");


                            $this->db->query("insert into unposted_journal select '','$ref_id' , '$paydate','$commission_acc',commission_amount,'$memo',1 from employee_salary_slip
                                            join employee on employee_salary_slip.employee_id=employee.employee_id where employee_salary_slip_id='$salid'");


                            $this->db->query("insert into gl_trans select '','$ref_id' , '$paydate','$commission_acc','$memo',commission_amount from employee_salary_slip
                                            join employee on employee_salary_slip.employee_id=employee.employee_id where employee_salary_slip_id='$salid'");



                            /*Second Transaction*/

                            $reference_code= $this->accountingmodel->getreferencecode(1);

                            $ref_id = $this->mastermodel->insertmasterdata();
                            $refdata=array('ref_id'=>$ref_id,
                                            'ref_transaction_type_id'=>'1',
                                            'ref_code'=>$reference_code);
                            $this->db->insert('refs', $refdata);


                            $audittraildata=array('ref_id'=>$ref_id,
                                                'transaction_date'=>date("Y-m-d"),
                                                'fiscal_year'=>$fiscal_year_id,
                                                );
                            $this->db->insert('audit_trail', $audittraildata);

                            $this->db->query("insert into unposted_journal select '','$ref_id' , '$paydate',employee_no,paid_amount,'$memo',1 from employee_salary_slip
                                            join employee on employee_salary_slip.employee_id=employee.employee_id where employee_salary_slip_id='$salid'");


                            $this->db->query("insert into gl_trans select '','$ref_id' , '$paydate',employee_no,'$memo',paid_amount from employee_salary_slip
                                            join employee on employee_salary_slip.employee_id=employee.employee_id where employee_salary_slip_id='$salid'");


                            $this->db->query("insert into unposted_journal select '','$ref_id' , '$paydate','$bankaccount',0-paid_amount,'$memo',1 from employee_salary_slip
                                            join employee on employee_salary_slip.employee_id=employee.employee_id where employee_salary_slip_id='$salid'");


                            $this->db->query("insert into gl_trans select '','$ref_id' , '$paydate','$bankaccount','$memo',0-paid_amount from employee_salary_slip
                                            join employee on employee_salary_slip.employee_id=employee.employee_id where employee_salary_slip_id='$salid'");

                            $this->db->query("insert into bank_transaction select '','$ref_id' ,'$bankaccount', '$paydate',0-paid_amount,'' from employee_salary_slip
                                            join employee on employee_salary_slip.employee_id=employee.employee_id where employee_salary_slip_id='$salid'");






                }
            }
        }
        
        $data['resfunction']='search_payslip';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Employee Salary Processed successfully';
            return $data;
        }
        
        
    }
    
    function insert_special_holiday($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $tblValues=array(
                        'special_holiday_start_date'=>$this->mastermodel->convertdateformat($postdata['special_holiday_start_date']),
                        'special_holiday_end_date'=>$this->mastermodel->convertdateformat($postdata['special_holiday_end_date']),
                        'special_holiday_name'=>$postdata['special_holiday_name']);
        $this->db->insert('timesheet_special_holiday', $tblValues);


        $data['resfunction']='search_special_holiday';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Attendance Marked Successfully';
            return $data;
        }
    }

    function insert_work_shift($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $tblValues=array(
                        'work_shift_name'=> $postdata['work_shift_name'],
                        'work_shift_wef_date'=> $this->mastermodel->convertdateformat($postdata['work_shift_wef_date']),
                        'work_shift_starting_time'=> $postdata['start_hour'].':'.$postdata['start_minute'],
                        'work_shift_ending_time'=>$postdata['end_hour'].':'.$postdata['end_minute'],
                        'work_shift_ending_time_half'=>$postdata['end_hour_half'].':'.$postdata['end_minute_half'],
                        'work_shift_break_starting_time'=> $postdata['break_start_hour'].':'.$postdata['break_start_minute'],
                        'work_shift_break_ending_time'=>$postdata['break_end_hour'].':'.$postdata['break_end_minute'],
                        'work_shift_grace_time'=> $postdata['work_shift_grace_time']
                        );
        $this->db->insert('timesheet_work_shift', $tblValues);
        $work_shift_id=mysql_insert_id();
  
        $i=1;
        if(isset($postdata['employees_2']))
        {
            foreach($postdata['employees_2'] as $employee)
            {
                $tblValues=array(
                                'work_shift_id'=>$work_shift_id,
                                'employee_id'=>$employee);
                $this->db->insert('timesheet_work_shift_employees', $tblValues);
                $i++;
            }
        }
    
        $data['resfunction']='search_work_shift';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Record Added Successfully';
            return $data;
        }
    }
    
    function insert_attendance_marking($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $weekly_holidays=$this->mastermodel->getdatas('timesheet_weekly_holiday','weekly_holiday_id');
        $holiday=array();
        foreach ($weekly_holidays as $weekly)
        {
            $holiday[]=$weekly['weekly_holiday'];
        }
        $weekly_holidays_half=$this->mastermodel->getdatas('timesheet_weekly_holiday_half','weekly_holiday_id');
        $holiday_half=array();
        foreach ($weekly_holidays_half as $weekly_half)
        {
            $holiday_half[]=$weekly_half['weekly_holiday_half'];
        }
        $start_date = $this->mastermodel->convertdateformat($postdata['from_date']);
        $end_date = $this->mastermodel->convertdateformat($postdata['to_date']);
        while (strtotime($start_date) <= strtotime($end_date))
        {
            if(in_array(date('l', strtotime($start_date)), $holiday) || $this->is_special_holiday($start_date)>0)
            {
                
            }
            else if(in_array(date('l', strtotime($start_date)), $holiday_half))
            {
                foreach($postdata['employee'] as $employee)
                {
                    $shift_details = $this->get_employee_shift($employee,$start_date);
                    $num = $this->mastermodel->number_rows('timesheet_attendance','employee_device_id',$employee,'attendance_date',$start_date." ".$shift_details->work_shift_starting_time,'attendance_mode',0);
                    
                    if($num==0)
                    {
                        $tblValues= array('employee_device_id'=>$employee,
                        'attendance_date'=> $start_date." ".$shift_details->work_shift_starting_time,
                        'attendance_mode'=> 0,
                        'attendance_remarks'=> $postdata['attendance_remarks']
                        );
                        $this->db->insert('timesheet_attendance', $tblValues);
                    }
                    $num = $this->mastermodel->number_rows('timesheet_attendance','employee_device_id',$employee,'attendance_date',$start_date." ".$shift_details->work_shift_ending_time_half,'attendance_mode',1);
                    if($num==0)
                    {
                        $tblValues1= array('employee_device_id'=>$employee,
                        'attendance_date'=> $start_date." ".$shift_details->work_shift_ending_time_half,
                        'attendance_mode'=> 1,
                        'attendance_remarks'=> $postdata['attendance_remarks'],
                        );
                         $this->db->insert('timesheet_attendance', $tblValues1);
                    }
                }

            }
            else
            {
                foreach($postdata['employee'] as $employee)
                {
                    $shift_details = $this->get_employee_shift($employee,$start_date);
                    $num = $this->mastermodel->number_rows('timesheet_attendance','employee_device_id',$employee,'attendance_date',$start_date." ".$shift_details->work_shift_starting_time,'attendance_mode',0);
                    if($num==0)
                    {
                        $tblValues= array('employee_device_id'=>$employee,
                        'attendance_date'=> $start_date." ".$shift_details->work_shift_starting_time,
                        'attendance_mode'=> 0,
                        'attendance_remarks'=> $postdata['attendance_remarks']
                        );
                        $this->db->insert('timesheet_attendance', $tblValues);
                    }
                    $num = $this->mastermodel->number_rows('timesheet_attendance','employee_device_id',$employee,'attendance_date',$start_date." ".$shift_details->work_shift_ending_time,'attendance_mode',1);
                    if($num==0)
                    {
                        $tblValues1= array('employee_device_id'=>$employee,
                        'attendance_date'=> $start_date." ".$shift_details->work_shift_ending_time,
                        'attendance_mode'=> 1,
                        'attendance_remarks'=> $postdata['attendance_remarks'],
                        );
                         $this->db->insert('timesheet_attendance', $tblValues1);
                    }

              
                }
            }
            $start_date = date ("Y-m-d", strtotime("+1 day", strtotime($start_date)));
        }
       
        $data['resfunction']='search_attendance_marking';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Record Added Successfully';
            return $data;
        }
    }

    function insert_update_attendance($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        for($i=1;$i<=$postdata['num_rows'];$i++)
        {
            if(isset($postdata['present_'.$i]))
            {
                $tblValues=array(
                            'attendance_mode'=> $postdata['attendance_mode_'.$i],
                            'attendance_remarks'=> $postdata['attendance_remarks_'.$i]
                            );
                $this->db->where('timesheet_attendance_id', $postdata['timesheet_attendance_id_'.$i]);
                $this->db->update('timesheet_attendance', $tblValues);
            }
            else
            {
                $this->db->where('timesheet_attendance_id', $postdata['timesheet_attendance_id_'.$i]);
                $this->db->delete('timesheet_attendance');
            }
        }

        $data['resfunction']='search_update_attendance';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Attendance Updated Successfully';
            return $data;
        }
    }
    
    function insert_mark_attendance($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $date =  $this->mastermodel->convertdateformat($postdata['date']);
        $employee_device_id=  $this->mastermodel->getSingleFieldValue('employee','device_no','employee_id',$postdata['employee_id']);
        $tblValues= array('employee_device_id'=>$employee_device_id,
        'attendance_date'=> $date." ".$postdata['time_hour'].":".$postdata['time_minute'].":00",
        'attendance_mode'=> $postdata['attendance_mode'],
        'attendance_remarks'=> $postdata['attendance_remarks']
        );
        $this->db->insert('timesheet_attendance', $tblValues);
        
        $data['resfunction']='search_mark_attendance';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Attendance Marked Successfully';
            return $data;
        }
    }
    /*  Add functions endss here*/


    /* Update functions starts here*/

    function update_employee($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $this->mastermodel->updatemasterdata($postdata['employee_id']);
        $expiry_date = $this->mastermodel->convertdateformat($postdata['employee_visa_expiry_date']);
        $dob = $this->mastermodel->convertdateformat($postdata['employee_dob']);
        $labour_date= $this->mastermodel->convertdateformat($postdata['employee_labour_card_expiry_date']);
        $tblValues=array(
                        'employee_no'=> $postdata['employee_no'],
                        'employee_name'=> $postdata['employee_name'],
                        'employee_gender'=> $postdata['employee_gender'],
                        'employee_dob'=> $dob,
                        'employee_joining_date'=> $this->mastermodel->convertdateformat($postdata['employee_joining_date']),
                        'employee_nationality'=> $postdata['employee_nationality'],
                        'employee_designation_id'=> $postdata['employee_designation_id'],
                        'employee_department_id'=> $postdata['employee_department_id'],
                        'employee_branch_id'=> $postdata['employee_branch_id'],
                        'employee_grade_id'=> $postdata['employee_grade_id'],
                        'employee_status_id'=> $postdata['employee_status_id'],
                        'employee_local_address'=> $postdata['employee_local_address'],
                        'employee_permanent_address'=> $postdata['employee_permanent_address'],
                        'employee_contact_no'=> $postdata['employee_contact_no'],
                        'employee_email'=> $postdata['employee_email'],
                        'employee_passport_no'=> $postdata['employee_passport_no'],
                        'employee_visa_type'=> $postdata['employee_visa_type'],
                        'employee_visa_expiry_date'=>$expiry_date,
                        'employee_reporting_to'=>$postdata['employee_reporting_id'],
                        'device_no'=>$postdata['device_no'],
                        'employee_labour_card_no'=>$postdata['employee_labour_card_no'],
                        'employee_labour_card_expiry_date'=>$labour_date);
        $this->db->where('employee_id', $postdata['employee_id']);
        $this->db->update('employee', $tblValues);

         $this->db->where('employee_id',$postdata['employee_id']);
         $this->db->delete('employee_educational_qualification');
        $i=1;
        while(isset ($postdata['course'.$i]))
        {
            if($postdata['course'.$i]!='')
            {
                $tblValues=array(
                                'employee_id'=>$postdata['employee_id'],
                                'course'=>$postdata['course'.$i],
                                'university'=>$postdata['university'.$i],
                                'yearofpassing'=>$postdata['yearofpassing'.$i]);
                $this->db->insert('employee_educational_qualification', $tblValues);
            }
            $i++;
        }

        $this->db->where('employee_id',$postdata['employee_id']);
        $this->db->delete('employee_professional_experience');
        $i=1;
        while(isset ($postdata['employer'.$i]))
        {
            if($postdata['employer'.$i]!='')
            {
                $tblValues=array(
                                 'employee_id'=>$postdata['employee_id'],
                                'employer'=>$postdata['employer'.$i],
                                'designation'=>$postdata['designation'.$i],
                                'period'=>$postdata['period'.$i]);
                $this->db->insert('employee_professional_experience', $tblValues);
            }
            $i++;
        }

        $this->db->where('employee_id',$postdata['employee_id']);
         $this->db->delete('employee_additional_certification');
         $i=1;
        while(isset ($postdata['course_add'.$i]))
        {
            if($postdata['course_add'.$i]!='')
            {
                $tblValues=array(
                                 'employee_id'=>$postdata['employee_id'],
                                'course'=>$postdata['course_add'.$i],
                                'institute'=>$postdata['institute'.$i],
                                'yearofpassing'=>$postdata['year_passing'.$i]);
                $this->db->insert('employee_additional_certification', $tblValues);
            }
            $i++;
        }
        $data['resfunction']='search_employee';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Updating Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Record Updated Successfully';
            return $data;
        }
    }

    function update_offer_letter($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $this->mastermodel->updatemasterdata($postdata['offer_letter_id']);
        $interview_date = $this->mastermodel->convertdateformat($postdata['interview_date']);
        $joining_date = $this->mastermodel->convertdateformat($postdata['joining_date']);
        $tblValues=array(
                        'candidate_name'=> $postdata['candidate_name'],
                        'candidate_email'=> $postdata['candidate_email'],
                        'interview_date'=>  $interview_date,
                        'joining_date'=> $joining_date,
                        'contact_no'=> $postdata['contact_no'],
                        'designation_id'=> $postdata['designation_id']);
        $this->db->where('offer_letter_id', $postdata['offer_letter_id']);
        $this->db->update('offer_letter', $tblValues);
        if($postdata['filename']!='')
        {
            $tblValues=array(
            'filename'=>$postdata['filename']);
            $this->db->where('offer_letter_id', $postdata['offer_letter_id']);
            $this->db->update('offer_letter', $tblValues);
        }
        $data['resfunction']='search_offer_letter';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Updating Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Record Updated Successfully';
            return $data;
        }
    }

    function update_resume_management($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $this->mastermodel->updatemasterdata($postdata['resume_management_id']);
        $submit_date = $this->mastermodel->convertdateformat($postdata['submission_date']);
        $tblValues=array(
                        'candidate_name'=> $postdata['candidate_name'],
                        'qualification'=> $postdata['qualification'],
                        'email'=> $postdata['email'],
                        'submission_date'=>  $submit_date,
                        'expected_salary'=> $postdata['expected_salary'],
                        'experience'=> $postdata['experience'],
                        'contact_no'=> $postdata['contact_no'],
                        'filename'=> $postdata['filename']);
        $this->db->where('resume_management_id', $postdata['resume_management_id']);
        $this->db->update('resume_management', $tblValues);
        $data['resfunction']='search_resume_management';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Updating Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Record Updated Successfully';
            return $data;
        }
    }

    function update_leave_allocation($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $this->mastermodel->updatemasterdata($postdata['leave_allocation_id']);
        $tblValues=array(
                        'leave_allocation_year'=> $postdata['leave_allocation_year']);
        $this->db->where('leave_allocation_id', $postdata['leave_allocation_id']);
        $this->db->update('leave_allocation', $tblValues);
        $this->db->where('leave_allocation_id', $postdata['leave_allocation_id']);
        $this->db->delete('leave_allocation_detail');
        $i=1;
        while(isset($postdata['leave_type_id_'.$i]))
        {
             $tblValues=array('leave_allocation_id'=>$postdata['leave_allocation_id'],
                        'leave_type_id'=>$postdata['leave_type_id_'.$i],
                        'leave_allocation_days'=> $postdata['leave_allocation_days_'.$i]);
            $this->db->insert('leave_allocation_detail', $tblValues);
            $i++;
        }
        $data['resfunction']='search_leave_allocation';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Updating Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Record Updated Successfully';
            return $data;
        }
    }

    function update_leave_application($postdata)
    {
        var_dump($postdata);
        $data=array();
        $this->db->trans_begin();
        $this->mastermodel->updatemasterdata($postdata['leave_application_id']);
        $fromdate = $this->mastermodel->convertdateformat($postdata['from_date']);
        $todate = $this->mastermodel->convertdateformat($postdata['to_date']);
        $tblValues=array(
                         'leave_type_id'=> $postdata['leave_type_id'],
                        'from_date'=> $fromdate,
                        'to_date'=> $todate,
                        'remarks'=> $postdata['remarks']);
        $this->db->where('leave_application_id', $postdata['leave_application_id']);
        $this->db->update('leave_application', $tblValues);
        $data['resfunction']='search_leave_application';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Updating Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Record Updated Successfully';
            return $data;
        }
    }

    function update_employee_salary($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $this->mastermodel->updatemasterdata($postdata['employee_salary_id']);
        $wef_date = $this->mastermodel->convertdateformat($postdata['wef_date']);
        if(isset($postdata['return_ticket']))
        {
            $return_ticket=1;
        }
        else
        {
            $return_ticket=0;
        }
        $tblValues=array(
                       'employee_id' => $postdata['employee_id'],
                        'wef_date'=> $wef_date,
                        'basic_salary'=> $postdata['basic_salary'],
                        'from_ticket'=> $postdata['from_ticket'],
                        'to_ticket'=> $postdata['to_ticket'],
                        'return_ticket'=> $return_ticket,
                        'no_tickets'=> $postdata['no_tickets'],
                        
                        'payment_method'=> $postdata['payment_method']);
            $this->db->where('employee_salary_id', $postdata['employee_salary_id']);
            $this->db->update('employee_salary', $tblValues);

         $this->db->where('employee_salary_id',$postdata['employee_salary_id']);
         $this->db->delete('employee_salary_medical');
        $i=1;
        while(isset ($postdata['type'.$i]))
        {
            $tblValues=array(
                            'employee_salary_id'=>$postdata['employee_salary_id'],
                            'type'=>$postdata['type'.$i],
                            'company'=>$postdata['company'.$i],
                            'policy'=>$postdata['policy'.$i],
                            'plan'=>$postdata['plan'.$i],
                            'sum'=>$postdata['sum'.$i]);
            $this->db->insert('employee_salary_medical', $tblValues);
            $i++;
        }
         $i=1;

         $this->db->where('employee_salary_id',$postdata['employee_salary_id']);
         $this->db->delete('employee_salary_allowdeduc');
        while(isset ($postdata['amount_'.$i]))
        {
            $tblValues=array(
                            'employee_salary_id'=>$postdata['employee_salary_id'],
                            'allowdeduc_id'=>$postdata['allowdeduc_id_'.$i],
                            'amount'=>$postdata['amount_'.$i]);
           $this->db->insert('employee_salary_allowdeduc', $tblValues);
            $i++;
        }
        $data['resfunction']='search_employee_salary';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Updating Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Record Updated Successfully';
            return $data;
        }
    }

    /* Update functions ends here*/

    function get_employee_salary_id($employee_id,$date)
    {
        $data = array();
        $date = $this->mastermodel->convertdateformat($date);
        $sql =  "SELECT tt.*
                FROM employee_salary tt
                INNER JOIN
                (SELECT employee_id, MAX(wef_date) AS wefdate
                FROM employee_salary
                GROUP BY employee_id) groupedtt ON tt.employee_id = groupedtt.employee_id AND tt.wef_date = groupedtt.wefdate
                where tt.employee_id = '$employee_id' AND wefdate <= '$date'";
        $Q = $this->db->query($sql);
        $result= $Q->row();
        if(!empty ($result))
        return $result->employee_salary_id;
        else
            return '';
    }

    function count_data_salary($table,$id,$date)
    {
        $date=$this->mastermodel->convdatformat($date);
        $query = "select employee_salary_id from $table where employee_id = '$id' and wef_date <= '$date' order by wef_date DESC limit 1";
        $res =  mysql_query($query);
        $num= mysql_num_rows($res);
        return $num;
    }

    function get_employee_salary_data($table,$id,$date)
    {
        $date=$this->mastermodel->convdatformat($date);
        $data = array();
        $sql = "select * from $table where employee_id = '$id' and wef_date <= '$date' order by wef_date DESC limit 1";
        $Q= $this->db->query($sql) ;
        $row=$Q->row();
        return $row;
    }
    
    function check_salary_month($id,$date)
    {
         $date=$this->mastermodel->convdatformat($date);
         $this->db->select('employee_salary_slip_id');
         $this->db->from('employee_salary_slip');
         $this->db->where('employee_id',$id);
         $where="month(salary_date)=month('$date') and year(salary_date)=year('$date')";
         $this->db->where($where,NULL,FALSE);
         return $this->db->count_all_results();
    }
    
    function checksalaryposted($id)
    {
        
         $this->db->select('unpost');
         $this->db->from('employee_salary_slip');
         $this->db->where('employee_salary_slip_id',$id);
         $q=  $this->db->get();
         return $q->row()->unpost;
    }

    function getunpaidemployees($date)
    {
        $data = array();
        $date=$this->mastermodel->convdatformat($date);
        $this->db->where("employee.employee_id NOT IN (SELECT `employee_id` FROM `employee_salary_slip` where month(salary_date)=month('$date') and year(salary_date)=year('$date'))", NULL, FALSE);
        $Q = $this->db->get('employee');
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }

   function loademployeesalary($post,$date)
   {
        $data=array();
        $salary_date=$this->mastermodel->convdatformat($date);
        $res=$this->db->query("SELECT employee_salary_slip.employee_salary_slip_id,employee_salary_slip.salary_date,employee_salary_slip.employee_id,employee.employee_no,employee.employee_name from employee_salary_slip join employee on employee.employee_id=employee_salary_slip.employee_id where month(salary_date)=month('$salary_date')and year(salary_date)=year('$salary_date') and unpost='$post' order by employee_salary_slip.employee_id");
        foreach ($res->result_array() as $row)
         {
             $data[] = $row;
         }
        return $data;
   }
   
   function getemployeelist()
   {
        $this->db->select('*');
        $this->db->from('employee');
        $this->db->join('master','masterid=employee_id');
        $this->db->join('department','department.department_id=employee.employee_department_id');
        $this->db->join('designation','designation.designation_id=employee.employee_designation_id');
        $this->db->order_by('employee_visa_expiry_date','asc');
        $Q = $this->db->get();
         if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
   }
   function getmaxexpense()
   {
      $res= $this->db->query("SELECT max(`chart_account_code`) as max FROM `chart_master` WHERE `chart_account_type`='3' and parent='0'");
      $row=$res->row();
      return $row->max;
       
   }
   function addallowdeduc($postdata)
   {
        $data=array();
        $this->db->trans_begin();
        
        $accno=$this->getmaxexpense();

        $dedacc=$accno+1;
       
        $tblValues=array('allowdeduc_name'=>$postdata['allowdeduc_name'], 'allowdeduc_type'=>$_POST['allowdeduc_type']
                            ,'allowdeduc_mode'=>$postdata['allowdeduc_mode'],'chart_account_code'=>$dedacc);
        $this->db->insert('allowdeduc', $tblValues);
        
      
        $tblValues=array('chart_account_code'=>$dedacc, 'chart_account_name'=>$postdata['allowdeduc_name']
                            ,'chart_account_type'=>3,'parent'=>0);
        $this->db->insert('chart_master', $tblValues);

               
        
        $data['resfunction']='search_allowdeduc';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Record Added Successfully';
            return $data;
        }
   }
   
   function updateallowdeduc($postdata)
   {
        $data=array();
        $this->db->trans_begin();
        
       
       
        $tblValues=array('allowdeduc_name'=>$postdata['allowdeduc_name']);
        $this->db->where('allowdeduc_id',$postdata['tab_id']);
        $this->db->update('allowdeduc', $tblValues);
        
      
        $tblValues=array('chart_account_name'=>$postdata['allowdeduc_name']
                        );
        $this->db->where('chart_account_code',$postdata['chart_account_code']);
        $this->db->update('chart_master', $tblValues);

               
        
        $data['resfunction']='search_allowdeduc';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Record Added Successfully';
            return $data;
        }
   }
   
   function loadunpostedsalary($sal_id)
   {
        $this->db->select('*');
        $this->db->from('employee_salary_slip');
        $this->db->join('employee','employee.employee_id=employee_salary_slip.employee_id');
        $this->db->join('department','department.department_id=employee.employee_department_id');
        $this->db->join('designation','designation.designation_id=employee.employee_designation_id');
        $this->db->where('employee_salary_slip_id',$sal_id);
        $this->db->where('unpost',1);
        $Q = $this->db->get();
        
        return $Q->row();
       
       
    }
    
   function loadpostedsalary($sal_id)
   {
        $this->db->select('*');
        $this->db->from('employee_salary_slip');
        $this->db->join('employee','employee.employee_id=employee_salary_slip.employee_id');
        $this->db->join('department','department.department_id=employee.employee_department_id');
        $this->db->join('designation','designation.designation_id=employee.employee_designation_id');
        $this->db->where('employee_salary_slip_id',$sal_id);
        $this->db->where('unpost',0);
        $Q = $this->db->get();
        
        return $Q->row();
       
       
    }
   
    function get_commission_pay_acc()
    {
            $data=array();
            $qry="SELECT cm.account_code as code  FROM mainacc_setting mas join chart_master cm ON cm.id=mas.commission_pay_acc ";
            $res=$this->db->query($qry);
            $data=$res->row()->code ;
            return $data;
    }

    function updateleavestatus()
    {
         $tblValues=array(
                        'leave_status'=>$_POST['leave_status']);
            $this->db->where('leave_application_id', $_POST['leave_application_id']);
            return $this->db->update('leave_application', $tblValues);
    }

    function update_special_holiday($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $tblValues=array(
                        'special_holiday_start_date'=>$this->mastermodel->convertdateformat($postdata['special_holiday_start_date']),
                        'special_holiday_end_date'=>$this->mastermodel->convertdateformat($postdata['special_holiday_end_date']),
                        'special_holiday_name'=>$postdata['special_holiday_name']);
        $this->db->where('special_holiday_id', $postdata['special_holiday_id']);
        $this->db->update('timesheet_special_holiday', $tblValues);

        $data['resfunction']='search_special_holiday';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Updating Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Record Updated Successfully';
            return $data;
        }
    }

    function update_weekly_holiday($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $this->db->truncate('timesheet_weekly_holiday');
        foreach($postdata['weekly_holiday'] as $holiday)
        {
            $tblValues=array('weekly_holiday'=>$holiday);
            $this->db->insert('timesheet_weekly_holiday', $tblValues);
        }

        $this->db->truncate('timesheet_weekly_holiday_half');
        foreach($postdata['weekly_holiday_half'] as $holiday)
        {
            $tblValues=array('weekly_holiday_half'=>$holiday);
            $this->db->insert('timesheet_weekly_holiday_half', $tblValues);
        }

        $data['resfunction']='search_weekly_holiday';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Updating Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Holidays Updated Successfully';
            return $data;
        }
    }

    function update_work_shift($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $tblValues=array(
                        'work_shift_name'=> $postdata['work_shift_name'],
                        'work_shift_wef_date'=> $this->mastermodel->convertdateformat($postdata['work_shift_wef_date']),
                        'work_shift_starting_time'=> $postdata['start_hour'].':'.$postdata['start_minute'],
                        'work_shift_ending_time'=>$postdata['end_hour'].':'.$postdata['end_minute'],
                        'work_shift_break_starting_time'=> $postdata['break_start_hour'].':'.$postdata['break_start_minute'],
                        'work_shift_break_ending_time'=>$postdata['break_end_hour'].':'.$postdata['break_end_minute'],
                        'work_shift_ending_time_half'=>$postdata['end_hour_half'].':'.$postdata['end_minute_half'],
                        'work_shift_grace_time'=> $postdata['work_shift_grace_time']);
        $this->db->where('work_shift_id', $postdata['work_shift_id']);
        $this->db->update('timesheet_work_shift', $tblValues);

        $this->db->where('work_shift_id', $postdata['work_shift_id']);
        $this->db->delete('timesheet_work_shift_employees');
        $i=1;
        if(isset($postdata['employees_2']))
        {
            foreach($postdata['employees_2'] as $employee)
            {
                $tblValues=array(
                                'work_shift_id'=>$postdata['work_shift_id'],
                                'employee_id'=>$employee);
                $this->db->insert('timesheet_work_shift_employees', $tblValues);
                $i++;
            }
        }

        $data['resfunction']='search_work_shift';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res']=0;
            $data['msg']='Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res']=1;
            $data['msg']='Record Added Successfully';
            return $data;
        }
    }
    
    function get_employee_shift($employee_device_id,$date)
    {
        $qry="SELECT * from timesheet_work_shift inner join timesheet_work_shift_employees
            ON (`timesheet_work_shift_employees`.`work_shift_id` = `timesheet_work_shift`.`work_shift_id`)
            inner join employee ON (`employee`.`employee_id` = `timesheet_work_shift_employees`.`employee_id`)
            WHERE device_no = '$employee_device_id' AND work_shift_wef_date <='$date' order by work_shift_wef_date desc limit 1";
        $res=$this->db->query($qry);
        $data=$res->row();
        return $data;
    }
    
    function get_employee_attendance($employee_id,$from_date,$to_date)
    {
        $data = array();
        $from_date =  $this->mastermodel->convertdateformat($from_date);
        $from_date = $from_date." 00:00:00";
        $to_date =  $this->mastermodel->convertdateformat($to_date);
        $to_date = $to_date." 23:59:59";
        $employee_device_id=$this->mastermodel->getSingleFieldValue('employee','device_no','employee_id',$employee_id);
        $this->db->where('employee_device_id',$employee_device_id);
        $this->db->where('attendance_date >=',$from_date);
        $this->db->where('attendance_date <=',$to_date);
        $this->db->order_by('attendance_date','asc');
        $Q=$this->db->get('timesheet_attendance');
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
        
        $Q->free_result();
        return $data;
    }
    
    function get_monthly_attendance_report($postdata)
    {
        $data=array();
        if($this->mastermodel->check_private())
        {
            $flag=1;
        }
        else
        {
            $flag=0;
        }
       
        $qry="SELECT * FROM `employee`
        join `department`  on `department`.`department_id`=`employee`.`employee_department_id`
        JOIN `designation` ON `designation`.`designation_id` = `employee`.`employee_designation_id`
        where `employee`.`device_no` !=''";
        
        if($postdata['employee_id']!="")
            $qry.=" and `employee`.`employee_id` = '".$postdata['employee_id']."'";

        if($postdata['department_id']!="")
            $qry.=" and `department`.`department_id` = '".$postdata['department_id']."'";

        if($postdata['designation_id']!="")
            $qry.=" and `designation`.`designation_id` = '".$postdata['designation_id']."'";
        if ($flag==1)
            $qry.=" and `employee`.`employee_id`  in (".$_SESSION['employees'].")";
        
        $qry.="order by `department`.`department_id`,`employee`.`employee_id`";
        $res=$this->db->query($qry);
         if ($res->num_rows() > 0)
         {
             foreach ($res->result_array() as $row)
             {
                 $data[] = $row;
             }
         }
         $res->free_result();
         return $data;
    }
    
    function get_attendance_day($employee_device_id,$date)
    {
        $this->db->from('timesheet_attendance');
        $this->db->like('attendance_date',$date);
        $this->db->where('employee_device_id',$employee_device_id);
        $num = $this->db->count_all_results();
        return $num;
        
    }

    function get_time_day($employee_device_id,$date)
    {
        $qry="SELECT * FROM `timesheet_attendance`
        where `employee_device_id` = '$employee_device_id' and attendance_date like '$date %' order by attendance_date asc";
        $res=$this->db->query($qry);
        if ($res->num_rows() == 0)
        {
            return 0;
        }
        else
        {
            $flag=0;
            $temp=0;
            $hours=0;
            $inmode=0;
            $outmode=0;
            foreach ($res->result_array() as $row)
             {
                if($flag % 2 == 0)
                {
                    if($row['attendance_mode'] != 0)
                        return -1;
                    else
                    {
                        $temp = $row['attendance_date'];
                        $inmode++;
                    }
                }
                if($flag % 2 == 1)
                {
                    if($row['attendance_mode'] != 1)
                    {
                        return -1;
                    }
                    else
                    {
                        $hours += round(abs(strtotime($row['attendance_date']) - strtotime($temp))/60/60,2);
                        $temp=0;
                        $outmode++;
                    }
                }
                $flag++;
                
            }
            if($inmode == $outmode)
                return $hours;
            else
                return -1;
        }

    }

    function get_late_day($employee_id,$employee_device_id,$date)
    {
        $this->db->where('timesheet_attendance.employee_device_id',$employee_device_id);
        $this->db->like('timesheet_attendance.attendance_date',$date);
        $this->db->where('timesheet_attendance.attendance_mode','0');
        $this->db->order_by('timesheet_attendance.attendance_date','asc');
        $Q1=$this->db->get('timesheet_attendance');
        $row1=$Q1->row();
        if(empty($row1))
            return '';

        $this->db->where('timesheet_work_shift_employees.employee_id',$employee_id);
        $this->db->where('work_shift_wef_date <=',$date);
        $this->db->join('timesheet_work_shift_employees','timesheet_work_shift_employees.work_shift_id=timesheet_work_shift.work_shift_id');
        $this->db->order_by('work_shift_wef_date','desc');
        $Q=$this->db->get('timesheet_work_shift');
        $row=$Q->row();

        $to_time = strtotime($row1->attendance_date);
        $from_time = strtotime($date." ".$row->work_shift_starting_time);
        if(round(abs($to_time - $from_time) / 60,2) > $row->work_shift_grace_time)
        {
            return round(abs($to_time - $from_time) / 60,2);
        }
        else
            return '';
        
    }
    
    function get_leave_day($employee_id,$date)
    {
        $this->db->select('*');
        $this->db->join('leave_type','leave_type.leave_type_id=leave_application.leave_type_id');
        $this->db->where("'".$date."' BETWEEN from_date AND to_date", NULL, FALSE);
        $this->db->where('leave_status',"Approved");
        $this->db->where('employee_id',$employee_id);
        $Q=$this->db->get('leave_application');
        if($Q->num_rows()>0)
            return $Q->row();
        else
            return 0;
    }

    function get_employee_timesheet_report($postdata)
    {
        $ts = strtotime($postdata['month']." ".$postdata['year']);
        $from_date=date('Y-m-01', $ts);
        $to_date=date('Y-m-t', $ts);
        return $this->get_employee_attendance($postdata['employee_id'], $from_date, $to_date);
    }

    function get_employee_attendance_status($employee_device_id,$employee_id,$date,$attendance_mode)
    {
        if($this->first_punch_in($employee_device_id,$date))
        {
            $this->db->where('timesheet_work_shift_employees.employee_id',$employee_id);
            $this->db->where('work_shift_wef_date <=',substr($date,0,11));
            $this->db->join('timesheet_work_shift_employees','timesheet_work_shift_employees.work_shift_id=timesheet_work_shift.work_shift_id');
            $this->db->order_by('work_shift_wef_date','desc');
            $Q=$this->db->get('timesheet_work_shift');
            $row=$Q->row();

            $to_time = strtotime($date);
            $from_time = strtotime(substr($date,0,11)." ".$row->work_shift_starting_time);
            if(round(abs($to_time - $from_time) / 60,2) > $row->work_shift_grace_time)
                return "Late In";
            else
                return '';
        }
        if($this->last_punch_out($employee_device_id,$date))
        {
            $this->db->where('timesheet_work_shift_employees.employee_id',$employee_id);
            $this->db->where('work_shift_wef_date <=',substr($date,0,11));
            $this->db->join('timesheet_work_shift_employees','timesheet_work_shift_employees.work_shift_id=timesheet_work_shift.work_shift_id');
            $this->db->order_by('work_shift_wef_date','desc');
            $Q=$this->db->get('timesheet_work_shift');
            $row=$Q->row();

            $to_time = strtotime($date);
            $from_time = strtotime(substr($date,0,11)." ".$row->work_shift_ending_time);
            if($to_time < $from_time)
                return "Early Out";
            else
                return '';
        }
        if($attendance_mode == 2)
        {
            $this->db->where('timesheet_work_shift_employees.employee_id',$employee_id);
            $this->db->where('work_shift_wef_date <=',substr($date,0,11));
            $this->db->join('timesheet_work_shift_employees','timesheet_work_shift_employees.work_shift_id=timesheet_work_shift.work_shift_id');
            $this->db->order_by('work_shift_wef_date','desc');
            $Q=$this->db->get('timesheet_work_shift');
            $row=$Q->row();

            $to_time = strtotime($date);
            $from_time = strtotime(substr($date,0,11)." ".$row->work_shift_break_starting_time);
            if($to_time < $from_time)
                return "Early Break Out";
            else
                return '';
        }
        if($attendance_mode == 3)
        {
            $this->db->where('timesheet_work_shift_employees.employee_id',$employee_id);
            $this->db->where('work_shift_wef_date <=',substr($date,0,11));
            $this->db->join('timesheet_work_shift_employees','timesheet_work_shift_employees.work_shift_id=timesheet_work_shift.work_shift_id');
            $this->db->order_by('work_shift_wef_date','desc');
            $Q=$this->db->get('timesheet_work_shift');
            $row=$Q->row();

            $to_time = strtotime($date);
            $from_time = strtotime(substr($date,0,11)." ".$row->work_shift_break_ending_time);
            if($to_time>$from_time)
            {
            if(round(abs($to_time - $from_time) / 60,2) > $row->work_shift_grace_time)
                return "Late Break In";
            else
                return '';
            }
            else
                return '';
        }
    }

    function first_punch_in($employee_device_id,$date)
    {
        $this->db->where('timesheet_attendance.employee_device_id',$employee_device_id);
        $this->db->like('timesheet_attendance.attendance_date',substr($date,0,11));
        $this->db->where('timesheet_attendance.attendance_mode','0');
        $this->db->order_by('timesheet_attendance.attendance_date','asc');
        $Q1=$this->db->get('timesheet_attendance');
        $row1=$Q1->row();
        if(!empty ($row1))
        {
            if($row1->attendance_date==$date)
                return 1;
            else
                return 0;
        }

    }

    function last_punch_out($employee_device_id,$date)
    {
        $this->db->where('timesheet_attendance.employee_device_id',$employee_device_id);
        $this->db->like('timesheet_attendance.attendance_date',substr($date,0,11));
        $this->db->where('timesheet_attendance.attendance_mode','1');
        $this->db->order_by('timesheet_attendance.attendance_date','desc');
        $Q1=$this->db->get('timesheet_attendance');
        $row1=$Q1->row();
        if(!empty ($row1))
        {
            if($row1->attendance_date==$date)
                return 1;
            else
                return 0;
        }

    }


    function get_punches($device_no,$date)
    {
        $data = array();
        $date=$this->mastermodel->convertdateformat($date);
        $this->db->where('employee_device_id',$device_no);
        $this->db->like('timesheet_attendance.attendance_date',$date);
        $this->db->order_by('attendance_date','asc');
        $Q=$this->db->get('timesheet_attendance');
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }

    function is_special_holiday($date)
    {
        $this->db->from('timesheet_special_holiday');
        $this->db->where('special_holiday_start_date <=', $date);
        $this->db->where('special_holiday_end_date >=', $date);
        $num = $this->db->count_all_results();
        $this->db->last_query();
        return $num;
    }
    
    function get_leave_approval_list()
    {
        $staff_list = $_SESSION['employee_id'] . ',';
        $employees = $this->mastermodel->traceemployee($_SESSION['employee_id'], $staff_list);
        $this->db->select('*');
        $this->db->from('leave_application');
        $this->db->join('master','masterid=leave_application_id');
        $this->db->join('employee','employee.employee_id=leave_application.employee_id');
        $this->db->join('leave_type','leave_type.leave_type_id=leave_application.leave_type_id');
        $this->db->where("employee.employee_id IN (".$employees.")", NULL, FALSE);
        $this->db->where('leave_status', 'Pending');
        
        $data['results']=$this->db->get();
       
        return $data;
    }
}

bypass 1.0, Devloped By El Moujahidin (the source has been moved and devloped)
Email: contact@elmoujehidin.net