? Fallagassrini

Fallagassrini Bypass Shell

echo"
Fallagassrini
";
Current Path : /home1/savoy/public_html/savoyglobal.net/hr/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/hr/application/models/timesheetmodel.php

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


class Timesheetmodel 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']='Erron 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']='Erron On updating Record';
        }
        return $data;
    }

        /*Search function starts here*/

    function search_customer($pagenum)
    {
        
        $data=array();
        $this->db->from('payroll_customer');
        $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('payroll_customer');
        $this->db->limit($perpage, $start);
        $this->db->order_by('customer_name','asc');
        $data['results']=$this->db->get();
        return $data;
    }

    function search_manpower_hiring($pagenum)
    {

        $data=array();
        $this->db->from('payroll_manpower_hiring');
        $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('payroll_manpower_hiring');
        $this->db->limit($perpage, $start);
        $this->db->order_by('company_id','desc');
        $data['results']=$this->db->get();
        return $data;
    }

    function search_purchase_order($pagenum)
    {

        $data=array();
        $this->db->from('payroll_purchase_order');
        $this->db->join('payroll_customer','payroll_customer.customer_id=payroll_purchase_order.customer_id');
        $this->db->join('site','site.site_id=payroll_purchase_order.purchase_order_site_id');
        $postdata=$_SESSION['searchdata'];
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            if($searchfield=="purchase_order_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=="purchase_order_date")
            {
                $searchtext=$this->mastermodel->convdatformat($searchtext);
            }
            $this->db->like($searchfield, $searchtext);
        }
        $this->db->select('*');
        $this->db->from('payroll_purchase_order');
        $this->db->join('payroll_customer','payroll_customer.customer_id=payroll_purchase_order.customer_id');
        $this->db->join('site','site.site_id=payroll_purchase_order.purchase_order_site_id');
        $this->db->limit($perpage, $start);
        $this->db->order_by('purchase_order_id','desc');
        $data['results']=$this->db->get();
        return $data;
    }
    
    function search_client_timesheet($pagenum)
    {

        $data=array();
        $this->db->from('payroll_client_timesheet');
        $this->db->join('payroll_purchase_order','payroll_purchase_order.purchase_order_id=payroll_client_timesheet.purchase_order_id');
        $this->db->join('payroll_customer','payroll_customer.customer_id=payroll_purchase_order.customer_id');
        $this->db->join('site','site.site_id=payroll_purchase_order.purchase_order_site_id');
        $postdata=$_SESSION['searchdata'];
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            if($searchfield=="client_timesheet_start_date" || $searchfield=="client_timesheet_end_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=="client_timesheet_start_date" || $searchfield=="client_timesheet_end_date")
            {
                $searchtext=$this->mastermodel->convdatformat($searchtext);
            }
            $this->db->like($searchfield, $searchtext);
        }
        $this->db->select('*');
        $this->db->from('payroll_client_timesheet');
        $this->db->join('payroll_purchase_order','payroll_purchase_order.purchase_order_id=payroll_client_timesheet.purchase_order_id');
        $this->db->join('payroll_customer','payroll_customer.customer_id=payroll_purchase_order.customer_id');
        $this->db->join('site','site.site_id=payroll_purchase_order.purchase_order_site_id');
        $this->db->limit($perpage, $start);
        $this->db->order_by('client_timesheet_id','desc');
        $data['results']=$this->db->get();
        return $data;
    }

    function search_manpower_timesheet($pagenum)
    {

        $data=array();
        $this->db->from('payroll_manpower_timesheet');
        $this->db->join('payroll_manpower_hiring','payroll_manpower_hiring.company_id=payroll_manpower_timesheet.manpower_timesheet_company_id');
        $this->db->join('payroll_purchase_order','payroll_purchase_order.purchase_order_id=payroll_manpower_timesheet.purchase_order_id');
        $this->db->join('payroll_customer','payroll_customer.customer_id=payroll_purchase_order.customer_id');
        $this->db->join('site','site.site_id=payroll_purchase_order.purchase_order_site_id');
        $postdata=$_SESSION['searchdata'];
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            if($searchfield=="manpower_timesheet_start_date" || $searchfield=="manpower_timesheet_end_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=="manpower_timesheet_start_date" || $searchfield=="manpower_timesheet_end_date")
            {
                $searchtext=$this->mastermodel->convdatformat($searchtext);
            }
            $this->db->like($searchfield, $searchtext);
        }
        $this->db->select('*');
        $this->db->from('payroll_manpower_timesheet');
        $this->db->join('payroll_manpower_hiring','payroll_manpower_hiring.company_id=payroll_manpower_timesheet.manpower_timesheet_company_id');
        $this->db->join('payroll_purchase_order','payroll_purchase_order.purchase_order_id=payroll_manpower_timesheet.purchase_order_id');
        $this->db->join('payroll_customer','payroll_customer.customer_id=payroll_purchase_order.customer_id');
        $this->db->join('site','site.site_id=payroll_purchase_order.purchase_order_site_id');
        $this->db->limit($perpage, $start);
        $this->db->order_by('manpower_timesheet_id','desc');
        $data['results']=$this->db->get();
        return $data;
    }

    function search_invoice($pagenum)
    {

        $data=array();
        $this->db->from('payroll_invoice');
        $this->db->join('payroll_invoice_purchase_order','payroll_invoice_purchase_order.invoice_id=payroll_invoice.invoice_id');
        $this->db->join('payroll_purchase_order','payroll_invoice_purchase_order.po_id=payroll_purchase_order.purchase_order_id');
        $this->db->join('payroll_customer','payroll_customer.customer_id=payroll_purchase_order.customer_id');
        $this->db->join('site','site.site_id=payroll_purchase_order.purchase_order_site_id');
        $this->db->group_by('payroll_invoice.invoice_id');
        $postdata=$_SESSION['searchdata'];
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            if($searchfield=="invoice_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=="invoice_date")
            {
                $searchtext=$this->mastermodel->convdatformat($searchtext);
            }
            $this->db->like($searchfield, $searchtext);
        }
        $this->db->select('*');
        $this->db->from('payroll_invoice');
        $this->db->join('payroll_invoice_purchase_order','payroll_invoice_purchase_order.invoice_id=payroll_invoice.invoice_id');
        $this->db->join('payroll_purchase_order','payroll_invoice_purchase_order.po_id=payroll_purchase_order.purchase_order_id');
        $this->db->join('payroll_customer','payroll_customer.customer_id=payroll_purchase_order.customer_id');
        $this->db->join('site','site.site_id=payroll_purchase_order.purchase_order_site_id');
        $this->db->group_by('payroll_invoice.invoice_id');
        $this->db->limit($perpage, $start);
        $this->db->order_by('payroll_invoice.invoice_id','desc');
        $data['results']=$this->db->get();
        return $data;
    }

    function search_internal_timesheet($pagenum)
    {

        $data=array();
        $this->db->from('payroll_internal_timesheet');
        $this->db->join('payroll_internal_job','payroll_internal_job.internal_job_id=payroll_internal_timesheet.internal_job_id');
        $this->db->join('camp','payroll_internal_job.internal_job_camp_id=camp.camp_id');
        $postdata=$_SESSION['searchdata'];
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            if($searchfield=="internal_timesheet_submission_date" || $searchfield=="internal_timesheet_start_date" || $searchfield=="internal_timesheet_end_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=="internal_timesheet_submission_date" || $searchfield=="internal_timesheet_start_date" || $searchfield=="internal_timesheet_end_date")
            {
                $searchtext=$this->mastermodel->convdatformat($searchtext);
            }
            $this->db->like($searchfield, $searchtext);
        }
        $this->db->select('*');
        $this->db->from('payroll_internal_timesheet');
        $this->db->join('payroll_internal_job','payroll_internal_job.internal_job_id=payroll_internal_timesheet.internal_job_id');
        $this->db->join('camp','payroll_internal_job.internal_job_camp_id=camp.camp_id');
        $this->db->limit($perpage, $start);
        $this->db->order_by('internal_timesheet_id','desc');
        $data['results']=$this->db->get();
        return $data;
    }

    function search_internal_job($pagenum)
    {

        $data=array();
        $this->db->from('payroll_internal_job');
        $this->db->join('camp','payroll_internal_job.internal_job_camp_id=camp.camp_id');
        $postdata=$_SESSION['searchdata'];
        if(!empty($postdata))
        {
            $searchtext=$postdata['searchtext'];
            $searchfield=$postdata['searchopt'];
            if($searchfield=="purchase_order_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'];
            $this->db->like($searchfield, $searchtext);
        }
        $this->db->select('*');
        $this->db->from('payroll_internal_job');
        $this->db->join('camp','payroll_internal_job.internal_job_camp_id=camp.camp_id');
        $this->db->limit($perpage, $start);
        $this->db->order_by('internal_job_id','desc');
        $data['results']=$this->db->get();
        return $data;
    }

        /*Search function ends here*/


        /*Add function starts here*/



    function insert_manpower_hiring($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $tblValues=array(
                        'company_name'=>$postdata['company_name'],
                        'company_cordinator'=>$postdata['company_cordinator'],
                        'company_address'=>$postdata['company_address'],
                        'company_pobox'=>$postdata['company_pobox'],
                        'company_city'=>$postdata['company_city'],
                        'company_country'=>$postdata['company_country'],
                        'company_email'=>$postdata['company_email'],
                        'company_contact_no'=>$postdata['company_contact_no']);
        $this->db->insert('payroll_manpower_hiring', $tblValues);
        $company_id=mysql_insert_id();
        $i=1;
        while (isset($postdata['manpower_hiring_employee_no'.$i]))
        {
            if($postdata['manpower_hiring_employee_no'.$i]!='')
            {
                $tblValues=array('manpower_hiring_employee_company_id'=>$company_id,
                                'manpower_hiring_employee_no'=>$postdata['manpower_hiring_employee_no'.$i],
                                'manpower_hiring_employee_name'=>$postdata['manpower_hiring_employee_name'.$i],
                                'manpower_hiring_employee_trade_id'=>$postdata['manpower_hiring_employee_trade_id'.$i],
                                'manpower_hiring_employee_active'=>$postdata['manpower_hiring_employee_active'.$i],
                                'manpower_hiring_employee_contact_no'=>$postdata['manpower_hiring_employee_contact_no'.$i],);
                $this->db->insert('payroll_manpower_hiring_employee', $tblValues);
            }
            $employee_id=  mysql_insert_id();
            $tblValues=array(
                       'manpower_hiring_trade_change_employee_id'=>$employee_id,
                       'manpower_hiring_trade_change_date'=>$this->mastermodel->convertdateformat($postdata['manpower_hiring_trade_change_date']),
                       'manpower_hiring_trade_change_trade_id'=>$postdata['manpower_hiring_employee_trade_id'.$i],
                       'manpower_hiring_trade_change_status'=>0,
                       );
               $this->db->insert('payroll_manpower_hiring_trade_change', $tblValues);
            $i++;
        }
        
        $data['resfunction']='search_manpower_hiring';
        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_customer($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $tblValues=array(
                        'customer_name'=>$postdata['customer_name'],
                        'customer_code'=>$postdata['customer_code'],
                        'customer_address'=>$postdata['customer_address'],
                        'customer_pobox'=>$postdata['customer_pobox'],
                        'customer_city'=>$postdata['customer_city'],
                        'customer_state'=>$postdata['customer_state'],
                        'customer_country'=>$postdata['customer_country'],
                        'customer_email'=>$postdata['customer_email'],
                        'customer_contact_no'=>$postdata['customer_contact_no'],
                        'customer_status'=>$postdata['customer_status'],
                        'customer_status'=>$postdata['customer_marketing_executve']);
        $this->db->insert('payroll_customer', $tblValues);
        $customer_id=mysql_insert_id();
        $i=1;
        while(isset($postdata['contact_name'.$i]))
        {
            if($postdata['contact_name'.$i]!="")
            {
                $tblValues=array('customer_id'=>$customer_id,
                                'contact_name'=>$postdata['contact_name'.$i],
                                'contact_mobile'=>$postdata['contact_mobile'.$i],
                                'contact_email'=>$postdata['contact_email'.$i],
                                'contact_phone'=>$postdata['contact_phone'.$i],
                                'contact_designation'=>$postdata['contact_designation'.$i]);
                $this->db->insert('payroll_customer_contacts', $tblValues);

            }
            $i++;
        }
       

        $data['resfunction']='search_customer';
        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_purchase_order($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $customer=explode('-', $postdata['customer_name']);
        $customer_id=$this->mastermodel->getSingleFieldValue('payroll_customer','customer_id','customer_code',$customer[count($customer)-1]);
        $tblValues=array(
                'job_code'=>$postdata['job_code'],
                'customer_id'=>$customer_id,
                'purchase_order_company_name'=>$postdata['purchase_order_company_name'],
                'customer_purchase_order_no'=>$postdata['customer_purchase_order_no'],
                'purchase_order_subject'=>$postdata['purchase_order_subject'],
                'purchase_order_start_date'=>$this->mastermodel->convertdateformat($postdata['purchase_order_start_date']),
                'purchase_order_end_date'=>$this->mastermodel->convertdateformat($postdata['purchase_order_end_date']),
                'purchase_order_site_id'=>$postdata['purchase_order_site_id'],
                'purchase_order_date'=>$this->mastermodel->convertdateformat($postdata['purchase_order_date']),
                'purchase_order_hours_basic'=>$postdata['purchase_order_hours_basic'],
                'purchase_order_transportation'=>$postdata['purchase_order_transportation'],
                'purchase_order_food_allowance'=>$postdata['purchase_order_food_allowance'],
                'purchase_order_accomodation'=>$postdata['purchase_order_accomodation'],
                'purchase_order_status'=>$postdata['purchase_order_status'],
                'purchase_order_payment_terms'=>$postdata['purchase_order_payment_terms'],
                'purchase_order_remarks'=>$postdata['purchase_order_remarks'],
                'purchase_order_document_name'=>$postdata['purchase_order_document_name'],
                'purchase_order_document_doc'=>$postdata['purchase_order_document_doc'],
                        );
        $this->db->insert('payroll_purchase_order', $tblValues);
        $purchase_order_id=mysql_insert_id();
        $i=1;
        while (isset($postdata['purchase_order_trade_id'.$i]))
        {
            if($postdata['purchase_order_trade_id'.$i]!='')
            {
                $tblValues=array('purchase_order_id'=>$purchase_order_id,
                                'purchase_order_trade_id'=>$postdata['purchase_order_trade_id'.$i],
                                'purchase_order_manpower_no'=>$postdata['purchase_order_manpower_no'.$i],
                                'purchase_order_rate'=>$postdata['purchase_order_rate'.$i],
                                'purchase_order_ot_type'=>$postdata['purchase_order_ot_type'.$i],
                                'purchase_order_ot_rate'=>$postdata['purchase_order_ot_rate'.$i],
                                'purchase_order_hot_rate'=>$postdata['purchase_order_hot_rate'.$i]);
                $this->db->insert('payroll_purchase_order_details', $tblValues);
            }
            $i++;
        }

        $data['resfunction']='search_purchase_order';
        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_client_timesheet($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        if(isset($postdata['client_timesheet_reviewed']))
            $client_timesheet_reviewed=1;
        else
            $client_timesheet_reviewed=0;
        $tblValues=array(
                        'purchase_order_id'=>$postdata['purchase_order_id'],
                        'client_timesheet_received_date'=>$this->mastermodel->convertdateformat($postdata['client_timesheet_received_date']),
                        'client_timesheet_reviewed'=>$client_timesheet_reviewed,
                        'client_timesheet_submission_date'=>$this->mastermodel->convertdateformat($postdata['client_timesheet_submission_date']),
                        'client_timesheet_start_date'=>$this->mastermodel->convertdateformat($postdata['client_timesheet_start_date']),
                        'client_timesheet_end_date'=>$this->mastermodel->convertdateformat($postdata['client_timesheet_end_date']),
                        'client_timesheet_remarks'=>$postdata['client_timesheet_remarks'],
                        'client_timesheet_document'=>$postdata['client_timesheet_document']);
        $this->db->insert('payroll_client_timesheet', $tblValues);
       
        $data['resfunction']='search_client_timesheet';
        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_manpower_timesheet($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        if(isset($postdata['manpower_timesheet_reviewed']))
            $manpower_timesheet_reviewed=1;
        else
            $manpower_timesheet_reviewed=0;
        $tblValues=array(
                        'manpower_timesheet_company_id'=>$postdata['manpower_timesheet_company_id'],
                        'purchase_order_id'=>$postdata['purchase_order_id'],
                        'manpower_timesheet_issued_date'=>$this->mastermodel->convertdateformat($postdata['manpower_timesheet_issued_date']),
                        'manpower_timesheet_reviewed'=>$manpower_timesheet_reviewed,
                        'manpower_timesheet_submission_date'=>$this->mastermodel->convertdateformat($postdata['manpower_timesheet_submission_date']),
                        'manpower_timesheet_start_date'=>$this->mastermodel->convertdateformat($postdata['manpower_timesheet_start_date']),
                        'manpower_timesheet_end_date'=>$this->mastermodel->convertdateformat($postdata['manpower_timesheet_end_date']),
                        'manpower_timesheet_remarks'=>$postdata['manpower_timesheet_remarks'],
                        'manpower_timesheet_document'=>$postdata['manpower_timesheet_document']);
        $this->db->insert('payroll_manpower_timesheet', $tblValues);

        $data['resfunction']='search_manpower_timesheet';
        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_invoice($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        
        $tblValues=array(
                        'invoice_date'=>$this->mastermodel->convertdateformat($postdata['invoice_date']),
                        'invoice_status'=>$postdata['invoice_status'],
                        'invoice_no'=>$postdata['invoice_no'],
                        'invoice_remarks'=>$postdata['invoice_remarks'],
                        'invoice_heading'=>$postdata['invoice_heading'],
                        'invoice_start_date'=>$this->mastermodel->convertdateformat($postdata['invoice_start_date']),
                        'invoice_end_date'=>$this->mastermodel->convertdateformat($postdata['invoice_end_date']),
                        'invoice_document'=>$postdata['invoice_document']);
        $this->db->insert('payroll_invoice', $tblValues);
        $i=1;
        $invoice_id=mysql_insert_id();
        while(isset($postdata['id_customer_po_no'.$i]))
        {
            if($postdata['id_customer_po_no'.$i]!='')
            {
                            $tbl_Values=array('invoice_id'=>$invoice_id,
                                'po_id'=>$postdata['id_customer_po_no'.$i]);
                $this->db->insert('payroll_invoice_purchase_order', $tbl_Values);
            }
            $i++;
        }
        $i=1;
        while(isset($postdata['label_text'.$i]))
        {
            if($postdata['label_text'.$i]!='')
            {
                            $tbl_Values=array('invoice_id'=>$invoice_id,
                                'label_text'=>$postdata['label_text'.$i],
                                'label_value'=>$postdata['label_value'.$i]);
                $this->db->insert('payroll_invoice_field', $tbl_Values);
            }
            $i++;
        }
        $data['resfunction']='search_invoice';
        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_internal_job($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        
        
        $tblValues=array(
                'internal_job_code'=>$postdata['internal_job_code'],
                'internal_job_camp_id'=>$postdata['internal_job_camp_id'],
                'internal_job_subject'=>$postdata['internal_job_subject'],
                'internal_job_remarks'=>$postdata['internal_job_remarks'],
                'internal_job_start_date'=>$this->mastermodel->convertdateformat($postdata['internal_job_start_date']),
                'internal_job_end_date'=>$this->mastermodel->convertdateformat($postdata['internal_job_end_date']),
                'internal_job_document_name'=>$postdata['internal_job_document_name'],
                'internal_job_document_doc'=>$postdata['internal_job_document_doc'],
                'internal_job_status'=>$postdata['internal_job_status']);
        $this->db->insert('payroll_internal_job', $tblValues);
       

        $data['resfunction']='search_internal_job';
        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_internal_timesheet($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $tblValues=array(
                        'internal_job_id'=>$postdata['internal_job_id'],
                        'internal_timesheet_submission_date'=>$this->mastermodel->convertdateformat($postdata['internal_timesheet_submission_date']),
                        'internal_timesheet_start_date'=>$this->mastermodel->convertdateformat($postdata['internal_timesheet_start_date']),
                        'internal_timesheet_end_date'=>$this->mastermodel->convertdateformat($postdata['internal_timesheet_end_date']),
                        'internal_timesheet_remarks'=>$postdata['internal_timesheet_remarks']);
        $this->db->insert('payroll_internal_timesheet', $tblValues);

        $data['resfunction']='search_internal_timesheet';
        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;
        }
    }
    

        /*Add function ends here*/


         /*Update function starts here*/


    function update_manpower_hiring($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $tblValues=array(
                        'company_name'=>$postdata['company_name'],
                        'company_cordinator'=>$postdata['company_cordinator'],
                        'company_address'=>$postdata['company_address'],
                        'company_pobox'=>$postdata['company_pobox'],
                        'company_city'=>$postdata['company_city'],
                        'company_country'=>$postdata['company_country'],
                        'company_email'=>$postdata['company_email'],
                        'company_contact_no'=>$postdata['company_contact_no']);
        $this->db->where('company_id', $postdata['company_id']);
        $this->db->update('payroll_manpower_hiring', $tblValues);
        $i=1;
        while(isset ($postdata['manpower_hiring_employee_id_'.$i]))
        {
           if($postdata['manpower_hiring_employee_no_'.$i]!='')
           {
               $tblValues=array(
                        'manpower_hiring_employee_no'=>$postdata['manpower_hiring_employee_no_'.$i],
                        'manpower_hiring_employee_name'=>$postdata['manpower_hiring_employee_name_'.$i],
                        'manpower_hiring_employee_active'=>$postdata['manpower_hiring_employee_active_'.$i],
                        'manpower_hiring_employee_contact_no'=>$postdata['manpower_hiring_employee_contact_no_'.$i]);
                $this->db->where('manpower_hiring_employee_id', $postdata['manpower_hiring_employee_id_'.$i]);
                $this->db->update('payroll_manpower_hiring_employee', $tblValues);
           }
            $i++;
        }
        $i=1;
        while (isset($postdata['manpower_hiring_employee_no'.$i]))
        {
            if($postdata['manpower_hiring_employee_no'.$i]!='')
            {
                $tblValues=array('manpower_hiring_employee_company_id'=>$postdata['company_id'],
                                'manpower_hiring_employee_no'=>$postdata['manpower_hiring_employee_no'.$i],
                                'manpower_hiring_employee_name'=>$postdata['manpower_hiring_employee_name'.$i],
                                'manpower_hiring_employee_trade_id'=>$postdata['manpower_hiring_employee_trade_id'.$i],
                                'manpower_hiring_employee_active'=>$postdata['manpower_hiring_employee_active'.$i],
                                'manpower_hiring_employee_contact_no'=>$postdata['manpower_hiring_employee_contact_no'.$i],);
                $this->db->insert('payroll_manpower_hiring_employee', $tblValues);
                $employee_id=  mysql_insert_id();
                $tblValues=array(
                        'manpower_hiring_trade_change_employee_id'=>$employee_id,
                        'manpower_hiring_trade_change_date'=>$this->mastermodel->convertdateformat($postdata['manpower_hiring_trade_change_date']),
                        'manpower_hiring_trade_change_trade_id'=>$postdata['manpower_hiring_employee_trade_id'.$i],
                        'manpower_hiring_trade_change_status'=>0,
                        );
                $this->db->insert('payroll_manpower_hiring_trade_change', $tblValues);
            }
            $i++;
        }
        $data['resfunction']='search_manpower_hiring';
        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_manpower_trade_change($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $i=1;
        while(isset ($postdata['manpower_hiring_employee_id_'.$i]))
        {
           if(($postdata['manpower_hiring_employee_old_trade_id_'.$i]!=$postdata['manpower_hiring_employee_new_trade_id_'.$i]) ||($postdata['manpower_hiring_trade_change_status_'.$i]!=$postdata['manpower_hiring_trade_change_current_status_'.$i]))
           {
               $tblValues=array(
                        'manpower_hiring_employee_trade_id'=>$postdata['manpower_hiring_employee_new_trade_id_'.$i],
                        );
                $this->db->where('manpower_hiring_employee_id', $postdata['manpower_hiring_employee_id_'.$i]);
                $this->db->update('payroll_manpower_hiring_employee', $tblValues);
                $tblValues=array(
                        'manpower_hiring_trade_change_employee_id'=>$postdata['manpower_hiring_employee_id_'.$i],
                        'manpower_hiring_trade_change_date'=>$this->mastermodel->convertdateformat($postdata['manpower_hiring_trade_change_date']),
                        'manpower_hiring_trade_change_trade_id'=>$postdata['manpower_hiring_employee_new_trade_id_'.$i],
                        'manpower_hiring_trade_change_status'=>$postdata['manpower_hiring_trade_change_status_'.$i],
                        );
                $this->db->insert('payroll_manpower_hiring_trade_change', $tblValues);
           }
            $i++;
        }
       
        $data['resfunction']='search_manpower_hiring';
        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_purchase_order($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $tblValues=array(
                'job_code'=>$postdata['job_code'],
                'customer_purchase_order_no'=>$postdata['customer_purchase_order_no'],
                'purchase_order_company_name'=>$postdata['purchase_order_company_name'],
                'purchase_order_subject'=>$postdata['purchase_order_subject'],
                'purchase_order_start_date'=>$this->mastermodel->convertdateformat($postdata['purchase_order_start_date']),
                'purchase_order_end_date'=>$this->mastermodel->convertdateformat($postdata['purchase_order_end_date']),
                'purchase_order_site_id'=>$postdata['purchase_order_site_id'],
                'purchase_order_date'=>$this->mastermodel->convertdateformat($postdata['purchase_order_date']),
                'purchase_order_hours_basic'=>$postdata['purchase_order_hours_basic'],
                'purchase_order_transportation'=>$postdata['purchase_order_transportation'],
                'purchase_order_food_allowance'=>$postdata['purchase_order_food_allowance'],
                'purchase_order_accomodation'=>$postdata['purchase_order_accomodation'],
                'purchase_order_status'=>$postdata['purchase_order_status'],
                'purchase_order_payment_terms'=>$postdata['purchase_order_payment_terms'],
                'purchase_order_remarks'=>$postdata['purchase_order_remarks'],
                'purchase_order_document_name'=>$postdata['purchase_order_document_name']
                        );
        $this->db->where('purchase_order_id', $postdata['purchase_order_id']);
        $this->db->update('payroll_purchase_order', $tblValues);

        if($postdata['purchase_order_document_doc']!='')
        {
            $tblValues=array(
            'purchase_order_document_doc'=>$postdata['purchase_order_document_doc']);
            $this->db->where('purchase_order_id', $postdata['purchase_order_id']);
        $this->db->update('payroll_purchase_order', $tblValues);
        }
        $this->db->where('purchase_order_id', $postdata['purchase_order_id']);
        $this->db->delete('payroll_purchase_order_details');
        $i=1;
        while (isset($postdata['purchase_order_trade_id'.$i]))
        {
            if($postdata['purchase_order_trade_id'.$i]!='')
            {
                $tblValues=array('purchase_order_id'=>$postdata['purchase_order_id'],
                                'purchase_order_trade_id'=>$postdata['purchase_order_trade_id'.$i],
                                'purchase_order_manpower_no'=>$postdata['purchase_order_manpower_no'.$i],
                                'purchase_order_rate'=>$postdata['purchase_order_rate'.$i],
                                'purchase_order_ot_type'=>$postdata['purchase_order_ot_type'.$i],
                                'purchase_order_ot_rate'=>$postdata['purchase_order_ot_rate'.$i],
                                'purchase_order_hot_rate'=>$postdata['purchase_order_hot_rate'.$i]);
                $this->db->insert('payroll_purchase_order_details', $tblValues);
            }
            $i++;
        }

        $data['resfunction']='search_purchase_order';
        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 update_customer($postdata)
    {
        $data=array();
        $this->db->trans_begin();
             $tblValues=array(
                        'customer_name'=>$postdata['customer_name'],
                        'customer_code'=>$postdata['customer_code'],
                        'customer_address'=>$postdata['customer_address'],
                        'customer_pobox'=>$postdata['customer_pobox'],
                        'customer_city'=>$postdata['customer_city'],
                        'customer_state'=>$postdata['customer_state'],
                        'customer_country'=>$postdata['customer_country'],
                        'customer_email'=>$postdata['customer_email'],
                        'customer_contact_no'=>$postdata['customer_contact_no'],
                        'customer_status'=>$postdata['customer_status'],
                        'customer_status'=>$postdata['customer_marketing_executve']);
        $this->db->where('customer_id',$postdata['customer_id']);
        $this->db->update('payroll_customer', $tblValues);

            $i=1;
            $this->db->where('customer_id',$postdata['customer_id']);
            $this->db->delete('payroll_customer_contacts');

            while(isset($postdata['contact_name'.$i]))
            {
                if($postdata['contact_name'.$i]!='')
                {
                    $tbl_Values=array('customer_id'=>$postdata['customer_id'],
                                    'contact_name'=>$postdata['contact_name'.$i],
                                    'contact_mobile'=>$postdata['contact_mobile'.$i],
                                    'contact_email'=>$postdata['contact_email'.$i],
                                    'contact_phone'=>$postdata['contact_phone'.$i],
                                    'contact_designation'=>$postdata['contact_designation'.$i]);
                    $this->db->insert('payroll_customer_contacts', $tbl_Values);
                }
                $i++;
            }
        
        $data['resfunction']='search_customer';
        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_internal_job($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $tblValues=array(
                'internal_job_code'=>$postdata['internal_job_code'],
                'internal_job_camp_id'=>$postdata['internal_job_camp_id'],
                'internal_job_subject'=>$postdata['internal_job_subject'],
                'internal_job_remarks'=>$postdata['internal_job_remarks'],
                'internal_job_start_date'=>$this->mastermodel->convertdateformat($postdata['internal_job_start_date']),
                'internal_job_end_date'=>$this->mastermodel->convertdateformat($postdata['internal_job_end_date']),
                'internal_job_document_name'=>$postdata['internal_job_document_name'],
                'internal_job_status'=>$postdata['internal_job_status']);
        $this->db->where('internal_job_id', $postdata['internal_job_id']);
        $this->db->update('payroll_internal_job', $tblValues);

        if($postdata['internal_job_document_doc']!='')
        {
            $tblValues=array(
            'internal_job_document_doc'=>$postdata['internal_job_document_doc']);
            $this->db->where('internal_job_id', $postdata['internal_job_id']);
            $this->db->update('payroll_internal_job', $tblValues);
        }
        $data['resfunction']='search_internal_job';
        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 update_client_timesheet($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        if(isset($postdata['client_timesheet_reviewed']))
            $client_timesheet_reviewed=1;
        else
            $client_timesheet_reviewed=0;
        $tblValues=array(
                        'client_timesheet_received_date'=>$this->mastermodel->convertdateformat($postdata['client_timesheet_received_date']),
                        'client_timesheet_reviewed'=>$client_timesheet_reviewed,
                        'client_timesheet_submission_date'=>$this->mastermodel->convertdateformat($postdata['client_timesheet_submission_date']),
                        'client_timesheet_start_date'=>$this->mastermodel->convertdateformat($postdata['client_timesheet_start_date']),
                        'client_timesheet_end_date'=>$this->mastermodel->convertdateformat($postdata['client_timesheet_end_date']),
                        'client_timesheet_remarks'=>$postdata['client_timesheet_remarks']);
        $this->db->where('client_timesheet_id', $postdata['client_timesheet_id']);
        $this->db->update('payroll_client_timesheet', $tblValues);
        
        if($postdata['client_timesheet_document']!='')
        {
            $tblValues=array('client_timesheet_document'=>$postdata['client_timesheet_document']);
            $this->db->where('client_timesheet_id', $postdata['client_timesheet_id']);
            $this->db->update('payroll_client_timesheet', $tblValues);
        }
       
       
        $data['resfunction']='search_client_timesheet';
        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 update_manpower_timesheet($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        if(isset($postdata['manpower_timesheet_reviewed']))
            $manpower_timesheet_reviewed=1;
        else
            $manpower_timesheet_reviewed=0;
        $tblValues=array(
                        'manpower_timesheet_issued_date'=>$this->mastermodel->convertdateformat($postdata['manpower_timesheet_issued_date']),
                        'manpower_timesheet_reviewed'=>$manpower_timesheet_reviewed,
                        'manpower_timesheet_submission_date'=>$this->mastermodel->convertdateformat($postdata['manpower_timesheet_submission_date']),
                        'manpower_timesheet_start_date'=>$this->mastermodel->convertdateformat($postdata['manpower_timesheet_start_date']),
                        'manpower_timesheet_end_date'=>$this->mastermodel->convertdateformat($postdata['manpower_timesheet_end_date']),
                        'manpower_timesheet_remarks'=>$postdata['manpower_timesheet_remarks']);
        $this->db->where('manpower_timesheet_id', $postdata['manpower_timesheet_id']);
        $this->db->update('payroll_manpower_timesheet', $tblValues);

        if($postdata['manpower_timesheet_document']!='')
        {
            $tblValues=array('manpower_timesheet_document'=>$postdata['manpower_timesheet_document']);
            $this->db->where('manpower_timesheet_id', $postdata['manpower_timesheet_id']);
            $this->db->update('payroll_manpower_timesheet', $tblValues);
        }


        $data['resfunction']='search_manpower_timesheet';
        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 update_invoice($postdata)
    {
        $data=array();
        $this->db->trans_begin();
        $tblValues=array(
                        'invoice_date'=>$this->mastermodel->convertdateformat($postdata['invoice_date']),
                        'invoice_status'=>$postdata['invoice_status'],
                        'invoice_no'=>$postdata['invoice_no'],
                        'invoice_remarks'=>$postdata['invoice_remarks'],
                        'invoice_heading'=>$postdata['invoice_heading'],
                        );
        $this->db->where('invoice_id', $postdata['invoice_id']);
        $this->db->update('payroll_invoice', $tblValues);
        $i=1;
        $this->db->where('invoice_id',$postdata['invoice_id']);
        $this->db->delete('payroll_invoice_field');

        while(isset($postdata['label_text'.$i]))
        {
            if($postdata['label_text'.$i]!='')
            {
                $tbl_Values=array('invoice_id'=>$postdata['invoice_id'],
                                'label_text'=>$postdata['label_text'.$i],
                                'label_value'=>$postdata['label_value'.$i]);
                $this->db->insert('payroll_invoice_field', $tbl_Values);
            }
            $i++;
        }
        
        if($postdata['invoice_document']!='')
        {
            $tblValues=array(
            'invoice_document'=>$postdata['invoice_document']);
            $this->db->where('invoice_id', $postdata['invoice_id']);
        $this->db->update('payroll_invoice', $tblValues);
        }
        

        $data['resfunction']='search_invoice';
        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 update_internal_timesheet($postdata)
    {
        $data=array();
        $this->db->trans_begin();
       
        $tblValues=array(
                        'internal_timesheet_submission_date'=>$this->mastermodel->convertdateformat($postdata['internal_timesheet_submission_date']),
                        'internal_timesheet_start_date'=>$this->mastermodel->convertdateformat($postdata['internal_timesheet_start_date']),
                        'internal_timesheet_end_date'=>$this->mastermodel->convertdateformat($postdata['internal_timesheet_end_date']),
                        'internal_timesheet_remarks'=>$postdata['internal_timesheet_remarks']);
        $this->db->where('internal_timesheet_id', $postdata['internal_timesheet_id']);
        $this->db->update('payroll_internal_timesheet', $tblValues);



        $data['resfunction']='search_internal_timesheet';
        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_external_job_info()
    {
        $data = array();
        $this->db->join('payroll_customer',"payroll_purchase_order.customer_id=payroll_customer.customer_id");
        $this->db->join('site',"payroll_purchase_order.purchase_order_site_id=site.site_id");
        $this->db->where('purchase_order_status',"Active");
        $this->db->order_by('purchase_order_id','desc');
        $Q= $this->db->get('payroll_purchase_order');
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }

    function get_job_info_single($customer_purchase_order_id)
    {
        $this->db->join('payroll_customer',"payroll_purchase_order.customer_id=payroll_customer.customer_id");
        $this->db->join('site',"payroll_purchase_order.purchase_order_site_id=site.site_id");
        $this->db->where('purchase_order_status',"Active");
        $this->db->where('purchase_order_id',$customer_purchase_order_id);
        $Q = $this->db->get('payroll_purchase_order');
        return $Q->row();
    }

    function get_jobs_info($customer_purchase_order_id)
    {
        $purchase_orders = explode('~', $customer_purchase_order_id);
        array_pop($purchase_orders);
        $this->db->join('payroll_customer',"payroll_purchase_order.customer_id=payroll_customer.customer_id");
        $this->db->join('site',"payroll_purchase_order.purchase_order_site_id=site.site_id");
        $this->db->where('purchase_order_status',"Active");
        $this->db->where_in('purchase_order_id',$purchase_orders);
        $Q = $this->db->get('payroll_purchase_order');
        
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }

    function enter_client_timesheet_details($postdata)
    {
        $this->db->trans_begin();
        
        $this->db->where('client_timesheet_id',$postdata['client_timesheet_id']);
        $this->db->delete('payroll_client_timesheet_main');
        $j=0;
        $x=0;
        while(isset($postdata['employee_id'][$x]))
        {
            $i=1;
            if(strpos($postdata['employee_id'][$x], '~')===false)
                    $employee_id=$postdata['employee_id'][$x];
            else
            {
                    $employee_array =  explode('~', $postdata['employee_id'][$x]);
                    $employee_id = $employee_array[0];
            }
            $tblValues=array(
                        'client_timesheet_id'=>$postdata['client_timesheet_id'],
                        'client_timesheet_employee_id'=>$employee_id,
                        'client_timesheet_company_id'=>$postdata['company_id'][$x],
                        'client_timesheet_trade_id'=>$postdata['trade_id'][$x],
                        'trade_code'=>$postdata['trade_code'][$x],
                        'timesheet_status'=>$postdata['timesheet_status'][$x],
                        'normal_hours'=>$postdata['normal_hours'][$x],
                        'ot_hours'=>$postdata['ot_hours'][$x],
                        'hot_hours'=>$postdata['hot_hours'][$x],
                        'basic_rate'=>$postdata['basic_rate'][$x],
                        'ot_rate'=>$postdata['ot_rate'][$x],
                        'hot_rate'=>$postdata['hot_rate'][$x],
                        );
            $this->db->insert('payroll_client_timesheet_main', $tblValues);
            $main_id=mysql_insert_id();
            while(isset ($postdata['attendance_day'.$i]))
            {
                if(is_numeric($postdata['attendance_day'.$i][$j]))
                {
                    $postdata['attendance_day'.$i][$j]=number_format($postdata['attendance_day'.$i][$j],2);
                }
                $tblValues=array(
                        'client_timesheet_main_id'=>$main_id,
                        'client_timesheet_day'=>$i,
                        'client_timesheet_date'=>$postdata['attendance_date'.$i][$j],
                        'client_timesheet_no_hours'=>$postdata['attendance_day'.$i][$j]);
                 $this->db->insert('payroll_client_timesheet_details', $tblValues);
                 $i++;
            }
            $j++;
            $x++;
        }
        
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
        }
        else
        {
            $this->db->trans_commit();
        }
    }

    function get_timesheet_employees($client_timesheet_id)
    {
        $data = array();
        $this->db->where('client_timesheet_id',$client_timesheet_id);
        $this->db->group_by('client_timesheet_employee_id');
        $this->db->order_by('client_timesheet_id','asc');
        $Q= $this->db->get('payroll_client_timesheet_details');
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }
    function get_attendance_days($client_timesheet_id,$employee_id)
    {
        $data = array();
        $this->db->where('client_timesheet_id',$client_timesheet_id);
        $this->db->where('client_timesheet_employee_id',$employee_id);
        $this->db->order_by('id','asc');
        $Q= $this->db->get('payroll_client_timesheet_details');
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }
    function get_attendance_days_manpower($client_timesheet_main_id,$start_date,$end_date)
    {
        $data = array();
        $this->db->where('client_timesheet_main_id',$client_timesheet_main_id);
        $this->db->where('client_timesheet_date >=',$start_date);
        $this->db->where('client_timesheet_date <=',$end_date);
        $this->db->order_by('id','asc');
        $Q= $this->db->get('payroll_client_timesheet_details');
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }
    function get_employees_not_marked($client_timesheet_id,$trade_string,$end_date)
    {
        $trade_string=  rtrim($trade_string,',');
        $exit_date = date('Y-m-d',strtotime($end_date. ' -60 days'));
        $data = array();
        $sql = "(select employee_non_main.emp_non_id, emp_non_full_name,   CONCAT(`employee_non_main`.`emp_non_no_old`,','
            ,GROUP_CONCAT(DISTINCT(`visa_processing`.`employee_no`) order by visa_processing_id)) AS emp_non_no,0 from employee_non_main
             JOIN visa_processing ON (`visa_processing`.`employee_id` = `employee_non_main`.`emp_non_id`)
             JOIN trade_change ON (`trade_change`.`employee_id` = `employee_non_main`.`emp_non_id`)
             LEFT JOIN `leave` ON (`leave`.`employee_id` = `employee_non_main`.`emp_non_id`)
             LEFT JOIN `exit` ON (`exit`.`leave_id` = `leave`.`leave_id`)
             where (active =1 || exit_date > '$exit_date') 
             and `employee_non_main`.`emp_non_id` not in (select client_timesheet_employee_id from payroll_client_timesheet_main where client_timesheet_id=$client_timesheet_id and client_timesheet_company_id = 0)
             and `employee_non_main`.`emp_non_trade_id` in ($trade_string)
             and trade_change_date <= '$end_date'
             GROUP BY emp_non_id order by emp_non_trade_id,emp_non_full_name)";

        $sql .= "UNION 
                (select payroll_manpower_hiring_employee.manpower_hiring_employee_id as emp_non_id, manpower_hiring_employee_name as emp_non_full_name,  manpower_hiring_employee_no AS emp_non_no,
                company_id from payroll_manpower_hiring_employee 
                JOIN payroll_manpower_hiring ON `payroll_manpower_hiring`.`company_id` = `payroll_manpower_hiring_employee`.`manpower_hiring_employee_company_id`
                JOIN payroll_manpower_hiring_trade_change ON (`payroll_manpower_hiring_trade_change`.`manpower_hiring_trade_change_employee_id` = `manpower_hiring_employee_id`)
                where manpower_hiring_employee_active =1 and `payroll_manpower_hiring_employee`.`manpower_hiring_employee_id` not in (select client_timesheet_employee_id from payroll_client_timesheet_main where client_timesheet_id=$client_timesheet_id  and client_timesheet_company_id != 0)
                and `payroll_manpower_hiring_employee`.`manpower_hiring_employee_trade_id` in ($trade_string)
                and manpower_hiring_trade_change_status <= '$end_date'
                GROUP BY manpower_hiring_employee_id order by manpower_hiring_employee_trade_id,manpower_hiring_employee_name)";
        
       $Q = $this->db->query($sql);
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }

    function get_timesheet_employees_internal($internal_timesheet_id)
    {
        $data = array();
        $this->db->where('internal_timesheet_id',$internal_timesheet_id);
        $this->db->group_by('internal_timesheet_employee_id');
        $this->db->order_by('internal_timesheet_id','asc');
        $Q= $this->db->get('payroll_internal_timesheet_details');
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }
    function get_attendance_days_internal($internal_timesheet_id,$employee_id)
    {
        $data = array();
        $this->db->where('internal_timesheet_id',$internal_timesheet_id);
        $this->db->where('internal_timesheet_employee_id',$employee_id);
        $this->db->order_by('id','asc');
        $Q= $this->db->get('payroll_internal_timesheet_details');
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }

    function get_employees_not_marked_internal($internal_timesheet_id,$end_date)
    {
        $data = array();
        $exit_date = date('Y-m-d',strtotime($end_date. ' -60 days'));
        $sql = "select employee_non_main.emp_non_id, emp_non_full_name, emp_non_date_joining, batch_code,
            trade_name, CONCAT(`employee_non_main`.`emp_non_no_old`,',',
            GROUP_CONCAT(DISTINCT(`visa_processing`.`employee_no`) order by visa_processing_id)) AS emp_non_no
            from employee_non_main
            JOIN visa_processing ON `visa_processing`.`employee_id` = `employee_non_main`.`emp_non_id`
            inner join emp_non_personal on emp_non_personal.emp_non_id=employee_non_main.emp_non_id
            inner join batch on id=batch_id inner join trade on emp_non_trade_id=trade_id
            LEFT JOIN `leave` ON (`leave`.`employee_id` = `employee_non_main`.`emp_non_id`)
            LEFT JOIN `exit` ON (`exit`.`leave_id` = `leave`.`leave_id`)
            where (active =1 || exit_date > '$exit_date') 
            and `employee_non_main`.`emp_non_id`
            not in (select internal_timesheet_employee_id from payroll_internal_timesheet_details where
            internal_timesheet_id=$internal_timesheet_id) 
            GROUP BY emp_non_id order by batch_code, emp_non_full_name";
        $Q = $this->db->query($sql);
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }

    function enter_internal_timesheet_details($postdata)
    {
        $this->db->trans_begin();
        $this->db->where('internal_timesheet_id',$postdata['internal_timesheet_id']);
        $this->db->delete('payroll_internal_timesheet_details');
        $j=0;
        $x=0;
        while(isset($postdata['employee_id'][$x]))
        {
            $i=1;
            if(strpos($postdata['employee_id'][$x], '~')===false)
                    $employee_id=$postdata['employee_id'][$x];
            else
            {
                    $employee_array =  explode('~', $postdata['employee_id'][$x]);
                    $employee_id = $employee_array[0];
            }
            while(isset ($postdata['attendance_day'.$i]))
            {
                if(is_numeric($postdata['attendance_day'.$i][$j]))
                {
                    $postdata['attendance_day'.$i][$j]=number_format($postdata['attendance_day'.$i][$j],2);
                }
                $tblValues=array(
                        'internal_timesheet_id'=>$postdata['internal_timesheet_id'],
                        'internal_timesheet_employee_id'=>$employee_id,
                        'internal_timesheet_trade_id'=>$postdata['trade_id'][$x],
                        'internal_timesheet_day'=>$i,
                        'internal_timesheet_date'=>$postdata['attendance_date'.$i][$j],
                        'internal_timesheet_no_hours'=>$postdata['attendance_day'.$i][$j]);
                 $this->db->insert('payroll_internal_timesheet_details', $tblValues);
                 $i++;
            }
            $j++;
            $x++;
        }

        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
        }
        else
        {
            $this->db->trans_commit();
        }
    }

    function get_timesheet_employee_details($client_timesheet_id)
    {
        $data = array();
        $sql = "(select payroll_client_timesheet_main.*,emp_non_full_name,emp_non_no,client_timesheet_employee_id,purchase_order_hours_basic,
            CONCAT(trade_name,'-',trade_code) as trade_name,payroll_purchase_order.purchase_order_id,trade_id,0,client_timesheet_company_id from payroll_client_timesheet_main
            join employee_non_main on employee_non_main.emp_non_id=client_timesheet_employee_id
            join trade on client_timesheet_trade_id=trade_id
            join payroll_client_timesheet on payroll_client_timesheet.client_timesheet_id=payroll_client_timesheet_main.client_timesheet_id
            join payroll_purchase_order on payroll_purchase_order.purchase_order_id=payroll_client_timesheet.purchase_order_id
            where payroll_client_timesheet_main.client_timesheet_id = '$client_timesheet_id' and client_timesheet_company_id = '0'
            group by payroll_client_timesheet_main.client_timesheet_employee_id order by trade_id asc)";
        $sql .= " UNION (select payroll_client_timesheet_main.*,manpower_hiring_employee_name as emp_non_full_name,manpower_hiring_employee_no as emp_non_no,client_timesheet_employee_id,purchase_order_hours_basic,
            CONCAT(trade_name,'-',trade_code) as trade_name,payroll_purchase_order.purchase_order_id,trade_id,company_name,client_timesheet_company_id from payroll_client_timesheet_main
            join payroll_manpower_hiring_employee on payroll_manpower_hiring_employee.manpower_hiring_employee_id=client_timesheet_employee_id
            join payroll_manpower_hiring on payroll_manpower_hiring.company_id=payroll_manpower_hiring_employee.manpower_hiring_employee_company_id
            join trade on client_timesheet_trade_id=trade_id
            join payroll_client_timesheet on payroll_client_timesheet.client_timesheet_id=payroll_client_timesheet_main.client_timesheet_id
            join payroll_purchase_order on payroll_purchase_order.purchase_order_id=payroll_client_timesheet.purchase_order_id
            where payroll_client_timesheet_main.client_timesheet_id = '$client_timesheet_id' and client_timesheet_company_id != '0'
            group by payroll_client_timesheet_main.client_timesheet_employee_id order by trade_id asc)";
        $Q= $this->db->query($sql);
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }

    function get_po_rates($purchase_order_id,$trade_id)
    {
        $data = array();
        $this->db->where('purchase_order_id',$purchase_order_id);
        $this->db->where('purchase_order_trade_id',$trade_id);
        $Q= $this->db->get('payroll_purchase_order_details');
        $row=$Q->row();
        return $row;
    }
    
      
    function get_timesheet_employee_details_manpower($manpower_timesheet_company_id,$purchase_order_id,$manpower_timesheet_start_date,$manpower_timesheet_end_date)
    {
        $data = array();
            $sql = "select *,CONCAT(trade_name,'-',trade_code) as trade_name from payroll_client_timesheet_main
            inner join payroll_manpower_hiring_employee on payroll_manpower_hiring_employee.manpower_hiring_employee_id=payroll_client_timesheet_main.client_timesheet_employee_id
            inner join payroll_manpower_hiring on payroll_manpower_hiring.company_id=payroll_manpower_hiring_employee.manpower_hiring_employee_company_id
            inner join trade on trade.trade_id = client_timesheet_trade_id
            inner join payroll_client_timesheet on payroll_client_timesheet.client_timesheet_id = payroll_client_timesheet_main.client_timesheet_id
            inner join payroll_client_timesheet_details on payroll_client_timesheet_details.client_timesheet_main_id = payroll_client_timesheet_main.client_timesheet_main_id
            where client_timesheet_company_id = '$manpower_timesheet_company_id'
            and payroll_client_timesheet.purchase_order_id = '$purchase_order_id'
            and payroll_client_timesheet_details.client_timesheet_date >= '$manpower_timesheet_start_date'
            and payroll_client_timesheet_details.client_timesheet_date <= '$manpower_timesheet_start_date'
            order by trade_name asc";
        $Q= $this->db->query($sql);
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }

    function get_client_timesheet_invoice_info($customer_purchase_order_id,$timesheet_start_date,$timesheet_end_date)
    {
        $data=array();
        $res=array();
        $purchase_orders = explode('~', $customer_purchase_order_id);
        $po_list='';
        foreach($purchase_orders as $po)
            $po_list.=$po.',';
        $po_list=rtrim($po_list,',');
        $timesheet_start_date=$this->mastermodel->convertdateformat($timesheet_start_date);
        $timesheet_end_date=$this->mastermodel->convertdateformat($timesheet_end_date);
        $sql="SELECT  `payroll_client_timesheet_main`.`client_timesheet_company_id`, 
            CONCAT (`trade`.`trade_name`,' - ',trade_code) as trade_name,
            normal_hours, ot_hours, hot_hours, basic_rate, ot_rate, hot_rate, `trade`.`trade_id`
            FROM `payroll_client_timesheet_main` 
            RIGHT JOIN `payroll_client_timesheet` 
                ON (`payroll_client_timesheet`.`client_timesheet_id` = `payroll_client_timesheet_main`.`client_timesheet_id`)    
            JOIN `trade` 
                ON (`client_timesheet_trade_id` = `trade`.`trade_id`)
            LEFT JOIN `employee_non_main`
                ON (`employee_non_main`.`emp_non_id` = `payroll_client_timesheet_main`.`client_timesheet_employee_id`)
            LEFT JOIN `payroll_manpower_hiring_employee` 
                ON (`payroll_client_timesheet_main`.`client_timesheet_employee_id` = `payroll_manpower_hiring_employee`.`manpower_hiring_employee_id`)
             WHERE `payroll_client_timesheet`.`client_timesheet_start_date` >= '$timesheet_start_date' AND  `payroll_client_timesheet`.`client_timesheet_end_date` <=  '$timesheet_end_date' and `payroll_client_timesheet`.`purchase_order_id` in ($po_list)";
        $Q=$this->db->query($sql);
        //echo $sql;
        
        $this->db->query("TRUNCATE TABLE payroll_invoice_temp");
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $tblValues=array(
                        'trade_name'=>$row['trade_name'],
                        'normal_hours'=>$row['normal_hours'],
                        'ot_hours'=>$row['ot_hours'],
                        'hot_hours'=>$row['hot_hours'],
                        'basic_rate'=>$row['basic_rate'],
                        'ot_rate'=>$row['ot_rate'],
                        'hot_rate'=>$row['hot_rate']);
                $this->db->insert('payroll_invoice_temp', $tblValues);
            }
        }
        
        $Q->free_result();
        
        $sql = "select trade_name, count(trade_name) as manpower, SUM(normal_hours) as normal_hours, SUM(ot_hours) as ot_hours,
                SUM(hot_hours) as hot_hours,  basic_rate as normal_rate,  ot_rate, `hot_rate`
                from payroll_invoice_temp group by `trade_name`,`basic_rate`,`ot_rate`,`hot_rate`";
        $Q= $this->db->query($sql);
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
        $Q->free_result();
     //   $this->db->query("UNLOCK TABLES payroll_invoice_temp READ");
        return $data;
    }

    function get_manpower_timesheet_invoice_info($customer_purchase_order_id,$timesheet_start_date,$timesheet_end_date)
    {
        $purchase_orders = explode('~', $customer_purchase_order_id);
        $po_list='';
        foreach($purchase_orders as $po)
            $po_list.=$po.',';
        $po_list=rtrim($po_list,',');
        $data=array();
        $timesheet_start_date=$this->mastermodel->convertdateformat($timesheet_start_date);
        $timesheet_end_date=$this->mastermodel->convertdateformat($timesheet_end_date);
        $sql="SELECT
            `payroll_purchase_order`.`purchase_order_id`, `trade`.`trade_name`, `trade`.`trade_id`,
            `payroll_client_timesheet_main`.`trade_code` as `trade_code`,
            `payroll_client_timesheet_main`.`basic_rate` AS 'normal_rate',
            `payroll_client_timesheet_main`.`ot_rate` AS 'ot_rate',
            `payroll_client_timesheet_main`.`hot_rate` AS 'hot_rate',
             SUM(`payroll_client_timesheet_main`.`ot_hours`) AS ot_hours,
             SUM(`payroll_client_timesheet_main`.`hot_hours`) AS hot_hours,
             SUM(`payroll_client_timesheet_main`.`normal_hours`)  AS normal_hours,
             COUNT(DISTINCT(`payroll_client_timesheet_main`.`client_timesheet_employee_id`)) AS 'manpower'
             FROM
                `payroll_client_timesheet_main`
                INNER JOIN `payroll_client_timesheet`
                    ON (`payroll_client_timesheet`.`client_timesheet_id` = `payroll_client_timesheet_main`.`client_timesheet_id`)
                INNER JOIN `payroll_purchase_order`
                    ON (`payroll_purchase_order`.`purchase_order_id` = `payroll_client_timesheet`.`purchase_order_id`)
                INNER JOIN `payroll_purchase_order_details`
                    ON (`payroll_purchase_order_details`.`purchase_order_id` = `payroll_purchase_order`.`purchase_order_id`)
                INNER JOIN `trade`
                    ON (`payroll_purchase_order_details`.`purchase_order_trade_id` = `trade`.`trade_id`)
                INNER JOIN `payroll_manpower_hiring_employee`
                    ON (`payroll_manpower_hiring_employee`.`manpower_hiring_employee_trade_id` = `trade`.`trade_id`) AND (`payroll_client_timesheet_main`.`client_timesheet_employee_id` = `payroll_manpower_hiring_employee`.`manpower_hiring_employee_id`)
            WHERE `payroll_client_timesheet`.`client_timesheet_start_date` >= '$timesheet_start_date' AND  `payroll_client_timesheet`.`client_timesheet_end_date` <=  '$timesheet_end_date' and `payroll_purchase_order_details`.`purchase_order_id` in ($po_list)
            GROUP BY CONCAT(`trade`.`trade_id`, `payroll_client_timesheet_main`.`trade_code`)";

        $Q= $this->db->query($sql);
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }

    function get_timesheet_employee_details_internal($internal_timesheet_id)
    {
        $data = array();
        $this->db->join('employee_non_main','employee_non_main.emp_non_id=internal_timesheet_employee_id');
        $this->db->join('trade','internal_timesheet_trade_id=trade_id');
        $this->db->join('payroll_internal_timesheet','payroll_internal_timesheet.internal_timesheet_id=payroll_internal_timesheet_details.internal_timesheet_id');
        $this->db->join('payroll_internal_job','payroll_internal_job.internal_job_id=payroll_internal_timesheet.internal_job_id');
        $this->db->where('payroll_internal_timesheet_details.internal_timesheet_id',$internal_timesheet_id);
        $this->db->group_by('payroll_internal_timesheet_details.internal_timesheet_employee_id');
        $this->db->order_by('emp_non_trade_id','asc');
        $Q= $this->db->get('payroll_internal_timesheet_details');
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }

    function get_basic_hours($date)
    {
        $this->db->where('working_hour_effective_date <=',$date);
        $this->db->order_by('working_hour_effective_date','desc');
        $Q= $this->db->get('payroll_working_hour');
        $row=$Q->row();
        return $row->working_hours;
    }

    function get_purchaseorderreport($postdata)
    {
        $data=array();
        $i = 1;
        $searchstr='';
        while($i<=$postdata['field_count'])
        {
            if(isset($postdata['field_name_'.$i]))
            {
                $column_name = explode('~',$postdata['field_name_'.$i]);
                $searchstr .= $column_name[0]." as ".$column_name[1].",";
            }
            $i++;

        }
        $searchstr = substr($searchstr, 0, -1);

        $qry="SELECT ".$searchstr." FROM `payroll_purchase_order`
        JOIN `site` ON `site`.`site_id` = `payroll_purchase_order`.`purchase_order_site_id`
        JOIN `payroll_customer` ON `payroll_customer`.`customer_id` = `payroll_purchase_order`.`customer_id`
        JOIN `payroll_purchase_order_details`  on `payroll_purchase_order_details`.`purchase_order_id`=`payroll_purchase_order`.`purchase_order_id`
        JOIN `trade` ON `trade`.`trade_id` = `payroll_purchase_order_details`.`purchase_order_trade_id`
        where `payroll_purchase_order`.`purchase_order_id` !=''";

        if($postdata['customer']!="")
        {
            
            $customer=explode('-',$postdata['customer']);
            $qry.=" and `payroll_customer`.`customer_code` = '".$customer[count($customer)-1]."'";
        }

        
        if($postdata['purchase_order_start_date']!="" && $postdata['purchase_order_end_date']!="")
        {
            $fromdate=  $this->mastermodel->convdatformat($postdata['purchase_order_start_date']);
            $todate=$this->mastermodel->convdatformat($postdata['purchase_order_end_date']);
            $qry.=" and purchase_order_date between '$fromdate' and '$todate'";
        }

        $qry.=" group by `payroll_purchase_order`.`purchase_order_id` order by `payroll_purchase_order`.`purchase_order_id` desc";
        $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_invoicereport($postdata)
    {
        $data=array();
        $i = 1;
        $searchstr='';
        while($i<=$postdata['field_count'])
        {
            if(isset($postdata['field_name_'.$i]))
            {
                $column_name = explode('~',$postdata['field_name_'.$i]);
                $searchstr .= $column_name[0]." as ".$column_name[1].",";
            }
            $i++;

        }
        $searchstr = substr($searchstr, 0, -1);
        if(isset ($postdata['total_amount']))
            $searchstr .= ',`payroll_invoice`.`invoice_id` as Total';
        if(isset ($postdata['customer_purchase_order_no']))
            $searchstr .= ', GROUP_CONCAT(`payroll_purchase_order`.`customer_purchase_order_no`) as PO_No';
        if(isset ($postdata['job_code']))
            $searchstr .= ', GROUP_CONCAT(`payroll_purchase_order`.`job_code`) as Job_Code';
        if(isset ($postdata['site_name']))
            $searchstr .= ', GROUP_CONCAT(`site`.`site_name`) as Site_Name';
        $qry="SELECT ".$searchstr." from `payroll_invoice_purchase_order`
            INNER JOIN `payroll_invoice` 
                ON (`payroll_invoice_purchase_order`.`invoice_id` = `payroll_invoice`.`invoice_id`)
            INNER JOIN `payroll_purchase_order` 
                ON (`payroll_invoice_purchase_order`.`po_id` = `payroll_purchase_order`.`purchase_order_id`)
            INNER JOIN `payroll_customer` 
                ON (`payroll_purchase_order`.`customer_id` = `payroll_customer`.`customer_id`)
            INNER JOIN `site` 
                ON (`site`.`site_id` = `payroll_purchase_order`.`purchase_order_site_id`)    
            where `payroll_invoice`.`invoice_id` !=''";
        if($postdata['customer']!="")
        {

            $customer=explode('-',$postdata['customer']);
            $qry.=" and `payroll_customer`.`customer_code` = '".$customer[count($customer)-1]."'";
        }


        if($postdata['invoice_start_date']!="" && $postdata['invoice_end_date']!="")
        {
            $fromdate=  $this->mastermodel->convdatformat($postdata['invoice_start_date']);
            $todate=$this->mastermodel->convdatformat($postdata['invoice_end_date']);
            $qry.=" and invoice_date between '$fromdate' and '$todate'";
        }

        $qry.=" group by `payroll_invoice`.`invoice_id` order by `payroll_invoice`.`invoice_id` desc";
//        echo $qry;
//        die();
        $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_ot_type($purchase_order_id,$trade_id)
    {
        $data=$this->db->query("select purchase_order_ot_type from payroll_purchase_order_details where purchase_order_trade_id ='".$trade_id."' and purchase_order_id ='".$purchase_order_id."'");
        $result= $data->row();
        if($result)
        {
            return $result->purchase_order_ot_type;
        }
        else
        {
            return 0;
        }
    }

    function get_rates_po($trade_id,$po_id)
    {
        $data = array();
        $this->db->where('purchase_order_trade_id',$trade_id);
        $this->db->where('purchase_order_id',$po_id);
        $Q= $this->db->get('payroll_purchase_order_details');
        $row=$Q->row();
        return $row;
    }
    
    function check_employee_hours_client($employee_id,$calendar_date,$client_timesheet_id)
    {
        $employee_array=  explode('~', $employee_id);
        $employee_id=$employee_array[0];
        $company_id=$employee_array[1];
        $this->db->from('payroll_client_timesheet_details');
        $this->db->join('payroll_client_timesheet_main','payroll_client_timesheet_main.client_timesheet_main_id=payroll_client_timesheet_details.client_timesheet_main_id');
        $this->db->where('payroll_client_timesheet_main.client_timesheet_id !=',$client_timesheet_id);
        $this->db->where('payroll_client_timesheet_main.client_timesheet_employee_id',$employee_id);
        $this->db->where('payroll_client_timesheet_main.client_timesheet_company_id',$company_id);
        $this->db->where('payroll_client_timesheet_details.client_timesheet_date',$calendar_date);
        $this->db->where('payroll_client_timesheet_details.client_timesheet_no_hours >',0);
        if($this->db->count_all_results()==1)
            return 1;
        
        $this->db->from('payroll_internal_timesheet_details');
        $this->db->where('payroll_internal_timesheet_details.internal_timesheet_employee_id',$employee_id);
        $this->db->where('payroll_internal_timesheet_details.internal_timesheet_date',$calendar_date);
        $this->db->where('payroll_internal_timesheet_details.internal_timesheet_no_hours >',0);
        if($this->db->count_all_results()==2)
            return 1;
        
        return 0;
    }  
    
    function check_employee_hours_internal($employee_id,$calendar_date,$internal_timesheet_id)
    {
        $this->db->from('payroll_client_timesheet_details');
        $this->db->join('payroll_client_timesheet_main','payroll_client_timesheet_main.client_timesheet_main_id=payroll_client_timesheet_details.client_timesheet_main_id');
        $this->db->where('payroll_client_timesheet_main.client_timesheet_employee_id',$employee_id);
        $this->db->where('payroll_client_timesheet_main.client_timesheet_company_id','0');
        $this->db->where('payroll_client_timesheet_details.client_timesheet_date',$calendar_date);
        $this->db->where('payroll_client_timesheet_details.client_timesheet_no_hours !=','');
        if($this->db->count_all_results()==1)
            $num = 1;
        
        $this->db->from('payroll_internal_timesheet_details');
        $this->db->where('payroll_internal_timesheet_details.internal_timesheet_employee_id',$employee_id);
        $this->db->where('payroll_internal_timesheet_details.internal_timesheet_id !=',$internal_timesheet_id);
        $this->db->where('payroll_internal_timesheet_details.internal_timesheet_date',$calendar_date);
        $this->db->where('payroll_internal_timesheet_details.internal_timesheet_no_hours !=','');
        $count = $this->db->count_all_results();
        if($num==1 && $count==1)
            return 1;
        if($count==2)
            return 1;
        
        return 0;
    }  
    
    function get_client_timesheet_details($client_timesheet_id)
    {
        $data = array();
        $this->db->join('payroll_client_timesheet',"payroll_client_timesheet.client_timesheet_id=payroll_client_timesheet_main.client_timesheet_id");
        $this->db->where('payroll_client_timesheet_main.client_timesheet_id',$client_timesheet_id);
        $this->db->order_by('client_timesheet_main_id','asc');
        $Q= $this->db->get('payroll_client_timesheet_main');
        
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }
    
    function get_po_ot_type($po_id,$trade_id)
    {
        $data=$this->db->query("select purchase_order_ot_type from payroll_purchase_order_details  where
             purchase_order_id ='". $po_id ."' and purchase_order_trade_id ='". $trade_id ."'");
        $result= $data->row();
        if($result)
        {
            return $result->purchase_order_ot_type;
        }
        else
        {
            return "";
        }
    }
    
    function get_current_manpower_trade($employee_id)
    {
        $this->db->where('manpower_hiring_trade_change_employee_id',$employee_id);
        $this->db->order_by('manpower_hiring_trade_change_date','desc');
        $this->db->limit('1');
        $Q = $this->db->get('payroll_manpower_hiring_trade_change');
        $row=$Q->row('manpower_hiring_trade_change_status');
        return $row;
    }

}

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