? Fallagassrini

Fallagassrini Bypass Shell

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

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

 class reportmodel extends Model
{
     function reportmodel()
     {
         parent::Model();

     }
      function get_claimdetails($table,$id)
    {
        $data = array();
        $sql = "select * from policynote inner join broking_slip on policynote.quotation_ref = broking_slip.quotation_ref and policynote.id = '$id'";
        //echo $sql;
        
        $Q=$this->db->query($sql);
         $row=$Q->row();
        return $row;
    }
     function generatereport($table,$selectsearch,$search,$fromdate,$todate,$cover_id)
     {
            // echo $table.'--'.$selectsearch.'--'.$search.'--'.$fromdate.'--'.$todate.'--'.$cover_id;
         $data = array();

    $condition="";
         
                   if($selectsearch != "" && $fromdate != "" && $todate != "")
              {
             $fromdate = $this->mastermodel->convdatformat($fromdate);
        $todate = $this->mastermodel->convdatformat($todate);

             $condition=" and claim.$selectsearch between '$fromdate' and '$todate'";
              }
          if($cover_id != "cover_id")
              $condition=$condition." and placing_slip.cover_id = '$cover_id'";
            




         $sql = "
SELECT
    IFNULL(customer_corp_info.insured
    , customer_personal_ind.insured) as insured
    , staffpersonaldetails.name
    , covers.cover
    ,max(staffcustomer.entrydate)
    , customers.id 
    , placing_slip.cover_id
    , policynote.policyno
    , claim.date
    , placing_slip_premiumdetails.brokerage
    , placing_slip_premiumdetails.ispercentage
    , placing_slip_premiumdetails.premium,
     placing_slip_premiumdetails.policy_fees
    , insurance_company.company_name
    , customer_account.branch_id
    , customers.code
    , country.currencycode
    , placing_slip_premiumdetails.fromdate
    , placing_slip_premiumdetails.todate
    , IFNULL((SELECT sum(amount) from placing_slip_payamount where placing_slip_payamount.placing_slip_id=placing_slip.id ),0) as amount
    , IFNULL((SELECT sum(amount) from claimsettlement where claimsettlement.claimid=claim.id),0) as settlement
FROM
    customers
    LEFT JOIN customer_personal_ind 
        ON (customers.id = customer_personal_ind.customer_id)
    LEFT JOIN customer_corp_info 
        ON (customers.id = customer_corp_info.customer_id)
    INNER JOIN placing_slip 
        ON (placing_slip.customer_id = customers.id)
    INNER JOIN covers 
        ON (placing_slip.cover_id = covers.id)
    INNER JOIN country
        ON (placing_slip.currencytype = country.id)
    INNER JOIN policynote 
        ON (placing_slip.quotation_ref = policynote.quotation_ref)
    INNER JOIN claim 
        ON (claim.policyid = policynote.id)
    INNER JOIN claimsettlement 
        ON (claimsettlement.claimid = claim.id)
    INNER JOIN placing_slip_premiumdetails 
        ON (placing_slip_premiumdetails.placing_slip_id = placing_slip.id)
    LEFT JOIN placing_slip_payamount 
        ON (placing_slip.id = placing_slip_payamount.placing_slip_id)
    INNER JOIN insurance_company 
        ON (insurance_company.id = placing_slip_premiumdetails.insurance_company_id)
    INNER JOIN customer_account 
        ON (customer_account.customer_id = customers.id)
    INNER JOIN staffcustomer 
        ON (staffcustomer.customerid = customers.id)
    INNER JOIN staffpersonaldetails 
        ON (staffcustomer.staffid = staffpersonaldetails.id)  where  customers.id = '".$search."' $condition GROUP BY staffcustomer.customerid order by placing_slip.cover_id";
         
            //echo $sql;


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

	
     }


     function generateclaiminsureport($table,$fromdate,$todate,$search,$cover_id,$branch_id)
     {
            // echo $table.'--'.$selectsearch.'--'.$search.'--'.$fromdate.'--'.$todate.'--'.$cover_id;
        if($branch_id=='')
            $branch_id='branch_id';
         $data = array();

               $condition="";

                   if(($fromdate != "" && $todate != ""))
              {
             $fromdate = $this->mastermodel->convdatformat($fromdate);
        $todate = $this->mastermodel->convdatformat($todate);

             $condition=" and claim.date between '$fromdate' and '$todate'";
              }
          if($cover_id != "cover_id")
              $condition=$condition." and placing_slip.cover_id = '$cover_id'";
               if($branch_id!='branch_id')
         {
              $condition=$condition." and customer_account.branch_id = '$branch_id'";
         }




         $sql = "
    SELECT
    IFNULL(customer_corp_info.insured
    , customer_personal_ind.insured) as insured
    , staffpersonaldetails.name
    , covers.cover
    ,max(staffcustomer.entrydate)
    , customers.id
    , placing_slip.cover_id
    , policynote.policyno
    , claim.date
    , placing_slip_premiumdetails.brokerage
    , placing_slip_premiumdetails.ispercentage
    , placing_slip_premiumdetails.premium,
    placing_slip_premiumdetails.policy_fees
    , insurance_company.company_name
    , customer_account.branch_id
    , customers.code
    , country.currencycode
    , placing_slip_premiumdetails.fromdate
    , placing_slip_premiumdetails.todate
    , IFNULL((SELECT sum(amount) from placing_slip_payamount where placing_slip_payamount.placing_slip_id=placing_slip.id ),0) as amount
    , IFNULL((SELECT sum(amount) from claimsettlement where claimsettlement.claimid=claim.id),0) as settlement
    FROM
    customers
    LEFT JOIN customer_personal_ind
        ON (customers.id = customer_personal_ind.customer_id)
    LEFT JOIN customer_corp_info
        ON (customers.id = customer_corp_info.customer_id)
    INNER JOIN placing_slip
        ON (placing_slip.customer_id = customers.id)
    INNER JOIN covers
        ON (placing_slip.cover_id = covers.id)
    INNER JOIN country
        ON (placing_slip.currencytype = country.id)
    INNER JOIN policynote
        ON (placing_slip.quotation_ref = policynote.quotation_ref)
    INNER JOIN claim
        ON (claim.policyid = policynote.id)
    INNER JOIN claimsettlement
        ON (claimsettlement.claimid = claim.id)
    INNER JOIN placing_slip_premiumdetails
        ON (placing_slip_premiumdetails.placing_slip_id = placing_slip.id)
    LEFT JOIN placing_slip_payamount
        ON (placing_slip.id = placing_slip_payamount.placing_slip_id)
    INNER JOIN insurance_company
        ON (insurance_company.id = placing_slip_premiumdetails.insurance_company_id)
    INNER JOIN customer_account
        ON (customer_account.customer_id = customers.id)
    INNER JOIN staffcustomer
        ON (staffcustomer.customerid = customers.id)
    INNER JOIN staffpersonaldetails
        ON (staffcustomer.staffid = staffpersonaldetails.id)  where  placing_slip_premiumdetails.insurance_company_id  = '".$search."' $condition GROUP BY staffcustomer.customerid order by placing_slip.cover_id";

         //   echo $sql;


         $Q=$this->db->query($sql);

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


     }

     function generatesalesclientreport($table,$search,$fromdate,$todate,$cover_id)
     {
           
         $data = array();

         if($fromdate != "" && $todate != "")
                 {
         $fromdate = $this->mastermodel->convdatformat($fromdate);
         $todate = $this->mastermodel->convdatformat($todate);
                 }

         $sql="select * from placing_slip_refs pr  group by type,pslip_id ";

         $res=$this->db->query($sql);

         $i=0;
         foreach ($res->result_array()  as $restype)
         {  
             $trtype=$restype['type'];
             $qid=$restype['pslip_id'];
             
                if($trtype=='issue')
                 {

                            $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,p.fromdate as qrefdate,p.todate as renewdate,p.insurance_company_id,pn.policyno as policy_no,'null' as endmode,
                      p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref as qref,plc.currencytype ,p.policy_fees,sc.staffid,sc.date as effectivedate from placing_slip plc
                      join placing_slip_premiumdetails p on p.placing_slip_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                      join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                      ON plc.customer_id = sc.customerid INNER JOIN country ct
                      ON plc.currencytype = ct.id  ";



                 }
                else if($trtype=='endorsement')
                {

                    $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.endrefno as qref,plc.endpolicyno as policy_no,plc.enddate as qrefdate,p.todate as renewdate,plc.premiumchange as premium,plc.policyfeeschange as policy_fees,covers.cover,plc.mode as endmode,
                     p.insurance_company_id,pn.policyno,plc.brokerage,plc.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate
                     from placing_slip pl left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on
                     pn.quotation_ref=pl.quotation_ref join endorsement plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id  INNER JOIN staffcustomer sc
                     ON pl.customer_id = sc.customerid INNER JOIN country ct
                     ON pl.currencytype = ct.id ";
                    


                }
                else  if($trtype=='cancellation')
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.canpolicyno as policy_no,plc.canrefno as qref,p.fromdate as qrefdate,p.todate as renewdate,0-plc.premiumchange as premium,p.insurance_company_id,pn.policyno,covers.cover,'0' as policy_fees,'null' as endmode,
                  p.brokerage,p.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate  from placing_slip pl
                  left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on pn.quotation_ref=pl.quotation_ref
                  join cancellation plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id INNER JOIN staffcustomer sc
                  ON pl.customer_id = sc.customerid INNER JOIN country ct
                  ON pl.currencytype = ct.id  ";
                  

                }

                else
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from declaration_stakeholders where declaration_stakeholders.declaration_id=p.declaration_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,plc.certificate_refno as qref,p.todate as renewdate,plc.certificate_no as policy_no, p.fromdate as qrefdate,p.insurance_company_id,pn.policyno,'null' as endmode,
                  p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref,plc.currencytype,p.policy_fees,sc.staffid,sc.date as effectivedate  from declaration plc
                  join declaration_premiumdetails p on p.declaration_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                  join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                  ON plc.customer_id = sc.customerid INNER JOIN country ct
                  ON plc.currencytype = ct.id  ";
                  


                   

                }

                $sql=$sql."where  plc.id='$qid' and customer_id='$search'";

                if($fromdate != "" && $todate != "")
                 {
                     
                     $sql=$sql." having qrefdate between '$fromdate' and '$todate' ";
                 }
                 if($cover_id!="cover_id")
                 {
                     $sql=$sql." and cover_id = '$cover_id'";
                 }
                      $sql=$sql." order by effectivedate desc limit 1";
                 //echo $sql.'<br><br><br>';


                 $res1=$this->db->query($sql);

                 $result=$res1->row();

                

//                 foreach ($res1->result_array() as $row)
//                 {
//                     $resultset[] = $row;
//                 }
              
                if(!isset($result->qref))
                {
                    continue;
                }
                $data[$i]['qtnref']=$result->qref;

                $data[$i]['qdate']=$result->qrefdate;
                $data[$i]['renewdate']=$result->renewdate;
                $data[$i]['plid']=$result->plid;
                $data[$i]['customerid']=$result->customer_id;
                $data[$i]['covertype']=$result->cover;
                $data[$i]['premium']=$result->premium;
                $data[$i]['brokerage']=$result->brokerage;
                $data[$i]['policy_no']=$result->policy_no;
                $data[$i]['staffid']=$result->staffid;
                $data[$i]['currencycode']=$result->currencycode;
                $data[$i]['cover_id']=$result->cover_id;
                $data[$i]['policy_fees']=$result->policy_fees;
                $data[$i]['insurance_company_id']=$result->insurance_company_id;
                $data[$i]['percentage']=$result->percentage;
                $data[$i]['ispercentage']=$result->ispercentage;
                $data[$i]['endmode']=$result->endmode;
                

                $i++;

               
         }
               // var_dump($data);
                
                return $data;

                
     }

        function generaterenewclientreport($table,$search,$fromdate,$todate,$cover_id)
     {
            // echo $table.'--'.$selectsearch.'--'.$search.'--'.$fromdate.'--'.$todate.'--'.$cover_id;
          $data = array();

         if($fromdate != "" && $todate != "")
                 {
         $fromdate = $this->mastermodel->convdatformat($fromdate);
         $todate = $this->mastermodel->convdatformat($todate);
                 }

         $sql="select * from placing_slip_refs pr  group by type,pslip_id ";

         $res=$this->db->query($sql);

         $i=0;
         foreach ($res->result_array()  as $restype)
         {
             $trtype=$restype['type'];
             $qid=$restype['pslip_id'];

                if($trtype=='issue')
                 {

                            $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,p.todate as qrefdate,p.todate as renewdate,p.insurance_company_id,pn.policyno as policy_no,
                      p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref as qref,plc.currencytype ,p.policy_fees,sc.staffid,sc.date as effectivedate from placing_slip plc
                      join placing_slip_premiumdetails p on p.placing_slip_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                      join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                      ON plc.customer_id = sc.customerid INNER JOIN country ct
                      ON plc.currencytype = ct.id  ";



                 }
                else if($trtype=='endorsement')
                {

                    $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.endrefno as qref,plc.endpolicyno as policy_no,p.todate as qrefdate,p.todate as renewdate,plc.premiumchange as premium,plc.policyfeeschange as policy_fees,covers.cover,
                     p.insurance_company_id,pn.policyno,plc.brokerage,plc.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate
                     from placing_slip pl left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on
                     pn.quotation_ref=pl.quotation_ref join endorsement plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id  INNER JOIN staffcustomer sc
                     ON pl.customer_id = sc.customerid INNER JOIN country ct
                     ON pl.currencytype = ct.id ";



                }
                else  if($trtype=='cancellation')
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.canpolicyno as policy_no,plc.canrefno as qref,p.todate as qrefdate,p.todate as renewdate,0-plc.premiumchange as premium,p.insurance_company_id,pn.policyno,covers.cover,'0' as policy_fees,
                  p.brokerage,p.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate  from placing_slip pl
                  left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on pn.quotation_ref=pl.quotation_ref
                  join cancellation plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id INNER JOIN staffcustomer sc
                  ON pl.customer_id = sc.customerid INNER JOIN country ct
                  ON pl.currencytype = ct.id  ";


                }

                else
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from declaration_stakeholders where declaration_stakeholders.declaration_id=p.declaration_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,plc.certificate_refno as qref,p.todate as renewdate,plc.certificate_no as policy_no, p.todate as qrefdate,p.insurance_company_id,pn.policyno,
                  p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref,plc.currencytype,p.policy_fees,sc.staffid,sc.date as effectivedate  from declaration plc
                  join declaration_premiumdetails p on p.declaration_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                  join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                  ON plc.customer_id = sc.customerid INNER JOIN country ct
                  ON plc.currencytype = ct.id  ";





                }

                $sql=$sql."where  plc.id='$qid' and customer_id='$search'";

                if($fromdate != "" && $todate != "")
                 {

                     $sql=$sql." having qrefdate between '$fromdate' and '$todate' ";
                 }
                 if($cover_id!="cover_id")
                 {
                     $sql=$sql." and cover_id = '$cover_id'";
                 }
                    $sql=$sql." order by effectivedate desc limit 1";
                 //echo $sql.'<br><br><br>';


                 $res1=$this->db->query($sql);

                 $result=$res1->row();



//                 foreach ($res1->result_array() as $row)
//                 {
//                     $resultset[] = $row;
//                 }

                if(!isset($result->qref))
                {
                    continue;
                }
                $data[$i]['qtnref']=$result->qref;

                $data[$i]['qdate']=$result->qrefdate;
                $data[$i]['renewdate']=$result->renewdate;
                $data[$i]['plid']=$result->plid;
                $data[$i]['customerid']=$result->customer_id;
                $data[$i]['covertype']=$result->cover;
                $data[$i]['premium']=$result->premium;
                $data[$i]['brokerage']=$result->brokerage;
                $data[$i]['policy_no']=$result->policy_no;
                $data[$i]['staffid']=$result->staffid;
                $data[$i]['currencycode']=$result->currencycode;
                $data[$i]['cover_id']=$result->cover_id;
                $data[$i]['policy_fees']=$result->policy_fees;
                $data[$i]['insurance_company_id']=$result->insurance_company_id;
                $data[$i]['percentage']=$result->percentage;
                $data[$i]['ispercentage']=$result->ispercentage;


                $i++;


         }
               // var_dump($data);

                return $data;

     }
function generatereportlossclient($table,$selectsearch,$search,$fromdate,$todate,$cover_id)
     {

         $data = array();
         $sql = "select placing_slip.customer_id, placing_slip.quotation_ref,placing_slip.cover_id,placing_slip_premiumdetails.premium,policynote.policyno,claim.claimsref, sum(claimsettlement.amount) as totalamount from placing_slip inner join placing_slip_premiumdetails inner join policynote inner join claim inner join claimsettlement on placing_slip.id = placing_slip_premiumdetails.placing_slip_id and placing_slip.quotation_ref = policynote.quotation_ref and policynote.id = claim.policyid and claim.id = claimsettlement.claimid and placing_slip.customer_id = '$search'";
          /*if($selectsearch != "" && $fromdate != "" && $todate != "")
              {
             $fromdate = $this->mastermodel->convdatformat($fromdate);
        $todate = $this->mastermodel->convdatformat($todate);

             $sql=$sql." and claim.$selectsearch between '$fromdate' and '$todate'";
              }*/
         
          if($cover_id != "")
              $sql=$sql." and placing_slip.cover_id = '$cover_id'";
                //echo $sql;

         $Q=$this->db->query($sql);

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


     }

function generatereportlossclaimcov($table,$cover_id)
     {
         $data = array();
         $sql = "select placing_slip.customer_id, placing_slip.quotation_ref,placing_slip.cover_id,placing_slip_premiumdetails.premium,policynote.policyno,claim.claimsref, sum(claimsettlement.amount) as totalamount from placing_slip inner join placing_slip_premiumdetails inner join policynote inner join claim inner join claimsettlement on placing_slip.id = placing_slip_premiumdetails.placing_slip_id and placing_slip.quotation_ref = policynote.quotation_ref and policynote.id = claim.policyid and claim.id = claimsettlement.claimid and placing_slip.cover_id = '$cover_id'";
         $Q=$this->db->query($sql);
        // echo $sql;
         if ($Q->num_rows() > 0)
         {
             foreach ($Q->result_array() as $row)
             {
                 $data[] = $row;
             }
         }
         $Q->free_result();
         return $data;
      }
    function generatereportclaimcov($table,$selectsearch,$fromdate,$todate,$cover_id)
             {


        

        $data = array();

        $condition="";

                           if($selectsearch != "" && $fromdate != "" && $todate != "")
                      {
                     $fromdate = $this->mastermodel->convdatformat($fromdate);
                $todate = $this->mastermodel->convdatformat($todate);

                     $condition=" and claim.$selectsearch between '$fromdate' and '$todate'";
                      }
                  if($cover_id != "")
                      $condition=$condition." and placing_slip.cover_id = '$cover_id'";





                 $sql = "
       SELECT
    IFNULL(customer_corp_info.insured
    , customer_personal_ind.insured) as insured
    , staffpersonaldetails.name
    , covers.cover
    ,max(staffcustomer.entrydate)
    , customers.id
    , customers.code
    , placing_slip.cover_id
    , policynote.policyno
    , claim.date
    , placing_slip_premiumdetails.brokerage
    , placing_slip_premiumdetails.ispercentage
    , placing_slip_premiumdetails.premium,
                  placing_slip_premiumdetails.policy_fees
    , insurance_company.company_name
    , customer_account.branch_id
    , placing_slip_premiumdetails.fromdate
    , placing_slip_premiumdetails.todate
    , country.currencycode
    , IFNULL((SELECT sum(amount) from placing_slip_payamount where placing_slip_payamount.placing_slip_id=placing_slip.id ),0) as amount
    , IFNULL((SELECT sum(amount) from claimsettlement where claimsettlement.claimid=claim.id),0) as settlement
FROM
    customers
    LEFT JOIN customer_personal_ind
        ON (customers.id = customer_personal_ind.customer_id)
    LEFT JOIN customer_corp_info
        ON (customers.id = customer_corp_info.customer_id)
    INNER JOIN placing_slip
        ON (placing_slip.customer_id = customers.id)
   INNER JOIN country
        ON (placing_slip.currencytype = country.id)
    INNER JOIN covers
        ON (placing_slip.cover_id = covers.id)
    INNER JOIN policynote
        ON (placing_slip.quotation_ref = policynote.quotation_ref)
    INNER JOIN claim
        ON (claim.policyid = policynote.id)
    INNER JOIN claimsettlement
        ON (claimsettlement.claimid = claim.id)
    INNER JOIN placing_slip_premiumdetails
        ON (placing_slip_premiumdetails.placing_slip_id = placing_slip.id)
    LEFT JOIN placing_slip_payamount
        ON (placing_slip.id = placing_slip_payamount.placing_slip_id)
    INNER JOIN insurance_company
        ON (insurance_company.id = placing_slip_premiumdetails.insurance_company_id)
    INNER JOIN customer_account
        ON (customer_account.customer_id = customers.id)
    INNER JOIN staffcustomer
        ON (staffcustomer.customerid = customers.id)
    INNER JOIN staffpersonaldetails
        ON (staffcustomer.staffid = staffpersonaldetails.id)  where customers.id like '%' $condition group by placing_slip.id";

                   
                 $Q=$this->db->query($sql);

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


     }
      function generatereportclaimbra($table,$selectsearch,$fromdate,$todate,$branch,$cover_id)
             {


          $data = array();
      $condition="";
if($selectsearch != "" && $fromdate != "" && $todate != "")
              {
             $fromdate = $this->mastermodel->convdatformat($fromdate);
        $todate = $this->mastermodel->convdatformat($todate);

             $condition=" and claim.$selectsearch between '$fromdate' and '$todate'";
              }
               if($cover_id != "cover_id")
                      $condition=$condition." and placing_slip.cover_id = '$cover_id'";
        $sql = "
         SELECT
    IFNULL(customer_corp_info.insured
    , customer_personal_ind.insured) as insured
    , staffpersonaldetails.name
    , covers.cover
    ,max(staffcustomer.entrydate)
    , customers.id
    , customers.code
    , placing_slip.cover_id
    , policynote.policyno
    , claim.date
    , placing_slip_premiumdetails.brokerage
    , placing_slip_premiumdetails.ispercentage
    , placing_slip_premiumdetails.premium,
               placing_slip_premiumdetails.policy_fees
    , placing_slip_premiumdetails.fromdate
    , placing_slip_premiumdetails.todate
    , insurance_company.company_name
    , country.currencycode
    , customer_account.branch_id

    , IFNULL((SELECT sum(amount) from placing_slip_payamount where placing_slip_payamount.placing_slip_id=placing_slip.id ),0) as amount
    , IFNULL((SELECT sum(amount) from claimsettlement where claimsettlement.claimid=claim.id),0) as settlement
FROM
    customers
    LEFT JOIN customer_personal_ind
        ON (customers.id = customer_personal_ind.customer_id)
    LEFT JOIN customer_corp_info
        ON (customers.id = customer_corp_info.customer_id)
    INNER JOIN placing_slip
        ON (placing_slip.customer_id = customers.id)
    INNER JOIN covers
        ON (placing_slip.cover_id = covers.id)
     INNER JOIN country
        ON (placing_slip.currencytype = country.id)
    INNER JOIN policynote
        ON (placing_slip.quotation_ref = policynote.quotation_ref)
    INNER JOIN claim
        ON (claim.policyid = policynote.id)
    INNER JOIN claimsettlement
        ON (claimsettlement.claimid = claim.id)
    INNER JOIN placing_slip_premiumdetails
        ON (placing_slip_premiumdetails.placing_slip_id = placing_slip.id)
    LEFT JOIN placing_slip_payamount
        ON (placing_slip.id = placing_slip_payamount.placing_slip_id)
    INNER JOIN insurance_company
        ON (insurance_company.id = placing_slip_premiumdetails.insurance_company_id)
    INNER JOIN customer_account
        ON (customer_account.customer_id = customers.id)
    INNER JOIN staffcustomer
        ON (staffcustomer.customerid = customers.id)
    INNER JOIN staffpersonaldetails
        ON (staffcustomer.staffid = staffpersonaldetails.id)
    INNER JOIN staffemployeedetails
         ON (staffcustomer.staffid = staffemployeedetails.staffid)

               where   customer_account.branch_id = '$branch' $condition group by placing_slip.id order by placing_slip.cover_id";



         
          
         $Q=$this->db->query($sql);

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


     }


     function generatereportbratarget($table,$fromdate,$todate,$branch,$cover_id)
             {


          $data = array();
      $condition="";
if($fromdate != "" && $todate != "")
              {
             $fromdate = $this->mastermodel->convdatformat($fromdate);
        $todate = $this->mastermodel->convdatformat($todate);

             $condition=" and (branchtarget.startdate >= '$fromdate' and branchtarget.startdate<='$todate') and  (placing_slip_premiumdetails.fromdate >= '$fromdate' and placing_slip_premiumdetails.todate<='$todate')";
              }
               if($cover_id != "cover_id")
                      $condition=$condition." and placing_slip.cover_id = '$cover_id'";
        $sql = "
         SELECT
    IFNULL(customer_corp_info.insured
    , customer_personal_ind.insured) as insured
    , staffpersonaldetails.name
    , covers.cover
    ,max(staffcustomer.entrydate)
    , customers.id
    , customers.code
    , placing_slip.cover_id
    , policynote.policyno
    , (branchtarget.target
    , branchtarget.curr_id as bt_currency
    , placing_slip_premiumdetails.brokerage
    , placing_slip_premiumdetails.ispercentage
    , placing_slip_premiumdetails.premium,
               placing_slip_premiumdetails.policy_fees
    , placing_slip_premiumdetails.fromdate
    , placing_slip_premiumdetails.todate
    , insurance_company.company_name
    , country.currencycode
    , customer_account.branch_id

    , IFNULL((SELECT sum(amount) from placing_slip_payamount where placing_slip_payamount.placing_slip_id=placing_slip.id ),0) as amount
   
FROM
    customers
    LEFT JOIN customer_personal_ind
        ON (customers.id = customer_personal_ind.customer_id)
    LEFT JOIN customer_corp_info
        ON (customers.id = customer_corp_info.customer_id)
    INNER JOIN placing_slip
        ON (placing_slip.customer_id = customers.id)
    INNER JOIN covers
        ON (placing_slip.cover_id = covers.id)
     INNER JOIN country
        ON (placing_slip.currencytype = country.id)
    INNER JOIN policynote
        ON (placing_slip.quotation_ref = policynote.quotation_ref)
   
    INNER JOIN placing_slip_premiumdetails
        ON (placing_slip_premiumdetails.placing_slip_id = placing_slip.id)
    LEFT JOIN placing_slip_payamount
        ON (placing_slip.id = placing_slip_payamount.placing_slip_id)
    LEFT JOIN insurance_company
        ON (insurance_company.id = placing_slip_premiumdetails.insurance_company_id)
    INNER JOIN customer_account
        ON (customer_account.customer_id = customers.id)
    INNER JOIN staffcustomer
        ON (staffcustomer.customerid = customers.id)
    INNER JOIN staffpersonaldetails
        ON (staffcustomer.staffid = staffpersonaldetails.id)
    INNER JOIN staffemployeedetails
         ON (staffcustomer.staffid = staffemployeedetails.staffid)
    INNER JOIN branchtarget
         ON (branchtarget.branch = customer_account.branch_id)

               where   customer_account.branch_id = '$branch' $condition group by placing_slip.id order by placing_slip.cover_id";


//echo $sql;


         $Q=$this->db->query($sql);

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


     }



 function generatereportsalesbra($table,$fromdate,$todate,$branch,$selectedcovers="")
     {
          $data = array();

          if($fromdate != "" && $todate != "")
                 {
         $fromdate = $this->mastermodel->convdatformat($fromdate);
         $todate = $this->mastermodel->convdatformat($todate);
                 }
        $sql="select * from placing_slip_refs pr  group by type,pslip_id ";

         $res=$this->db->query($sql);

         $i=0;
         
         foreach ($res->result_array()  as $restype)
         {
             $trtype=$restype['type'];
             $qid=$restype['pslip_id'];

                if($trtype=='issue')
                 {

                            $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,p.fromdate as qrefdate,p.insurance_company_id,pn.policyno as policy_no,'null' as endmode,
                      p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref as qref,plc.currencytype ,p.policy_fees,sc.staffid,sc.date as effectivedate from placing_slip plc
                      join placing_slip_premiumdetails p on p.placing_slip_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                      join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                      ON plc.customer_id = sc.customerid INNER JOIN country ct
                      ON plc.currencytype = ct.id  LEFT JOIN customer_account
                      ON (customer_account.customer_id = plc.customer_id)  ";



                 }
                else if($trtype=='endorsement')
                {

                    $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.endrefno as qref,plc.endpolicyno as policy_no,plc.enddate as qrefdate,plc.premiumchange as premium,plc.policyfeeschange as policy_fees,covers.cover,plc.mode as endmode,
                     p.insurance_company_id,pn.policyno,plc.brokerage,plc.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate
                     from placing_slip pl left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on
                     pn.quotation_ref=pl.quotation_ref join endorsement plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id  INNER JOIN staffcustomer sc
                     ON pl.customer_id = sc.customerid INNER JOIN country ct
                     ON pl.currencytype = ct.id LEFT JOIN customer_account
                      ON (customer_account.customer_id = pl.customer_id)  ";



                }
                else  if($trtype=='cancellation')
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.canpolicyno as policy_no,plc.canrefno as qref,p.fromdate as qrefdate,0-plc.premiumchange as premium,p.insurance_company_id,pn.policyno,covers.cover,'0' as policy_fees,'null' as endmode,
                  p.brokerage,p.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate  from placing_slip pl
                  left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on pn.quotation_ref=pl.quotation_ref
                  join cancellation plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id INNER JOIN staffcustomer sc
                  ON pl.customer_id = sc.customerid INNER JOIN country ct
                  ON pl.currencytype = ct.id LEFT JOIN customer_account
                  ON (customer_account.customer_id = pl.customer_id)  ";


                }

                else
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from declaration_stakeholders where declaration_stakeholders.declaration_id=p.declaration_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,plc.certificate_refno as qref,plc.certificate_no as policy_no, p.fromdate as qrefdate,p.insurance_company_id,pn.policyno,'null' as endmode,
                  p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref,plc.currencytype,p.policy_fees,sc.staffid,sc.date as effectivedate  from declaration plc
                  join declaration_premiumdetails p on p.declaration_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                  join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                  ON plc.customer_id = sc.customerid INNER JOIN country ct
                  ON plc.currencytype = ct.id  LEFT JOIN customer_account
                  ON (customer_account.customer_id = plc.customer_id)  ";





                }
                if($selectedcovers!='')
                {
                    $cvrs  = str_replace('_', ',',$selectedcovers);
                    $sql=$sql."where  plc.id='$qid' and customer_account.branch_id = '$branch' and (cover_id IN (  $cvrs ))";
                }
                else
                   $sql=$sql."where  plc.id='$qid' and customer_account.branch_id = '$branch'";



                
                  if($fromdate != "" && $todate != "")
                 {
                     

                     $sql=$sql." having qrefdate between '$fromdate' and '$todate' ";
                 }
                 
                    $sql=$sql." order by effectivedate desc limit 1";
               //  echo $sql.'<br><br><br>';


                 $res1=$this->db->query($sql);

                 $result=$res1->row();



                if(!isset($result->qref))
                {
                    continue;
                }
                $data[$i]['qtnref']=$result->qref;

                $data[$i]['qdate']=$result->qrefdate;
                $data[$i]['plid']=$result->plid;
                $data[$i]['customerid']=$result->customer_id;
                $data[$i]['covertype']=$result->cover;
                $data[$i]['premium']=$result->premium;
                $data[$i]['brokerage']=$result->brokerage;
                $data[$i]['policy_no']=$result->policy_no;
                $data[$i]['staffid']=$result->staffid;
                $data[$i]['currencycode']=$result->currencycode;
                $data[$i]['cover_id']=$result->cover_id;
                $data[$i]['policy_fees']=$result->policy_fees;
                $data[$i]['insurance_company_id']=$result->insurance_company_id;
                $data[$i]['percentage']=$result->percentage;
                $data[$i]['endmode']=$result->endmode;
                $data[$i]['ispercentage']=$result->ispercentage;

                $i++;


         }
               // var_dump($data);

                return $data;
     }
      function generatereportrenewbra($table,$fromdate,$todate,$branch)
     {
          $data = array();

          if($fromdate != "" && $todate != "")
                 {
         $fromdate = $this->mastermodel->convdatformat($fromdate);
         $todate = $this->mastermodel->convdatformat($todate);
                 }
        $sql="select * from placing_slip_refs pr  group by type,pslip_id ";

         $res=$this->db->query($sql);

         $i=0;

         foreach ($res->result_array()  as $restype)
         {
             $trtype=$restype['type'];
             $qid=$restype['pslip_id'];

                if($trtype=='issue')
                 {

                            $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,p.todate as qrefdate,p.insurance_company_id,pn.policyno as policy_no,
                      p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref as qref,plc.currencytype ,p.policy_fees,sc.staffid,sc.date as effectivedate,plc.remarks as description from placing_slip plc
                      join placing_slip_premiumdetails p on p.placing_slip_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                      join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                      ON plc.customer_id = sc.customerid INNER JOIN country ct
                      ON plc.currencytype = ct.id  LEFT JOIN customer_account
                      ON (customer_account.customer_id = plc.customer_id)  ";



                 }
                else if($trtype=='endorsement')
                {

                    $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.endrefno as qref,plc.endpolicyno as policy_no,p.todate as qrefdate,plc.premiumchange as premium,plc.policyfeeschange as policy_fees,covers.cover,
                     p.insurance_company_id,pn.policyno,plc.brokerage,plc.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate,
                     plc.remarks as description from placing_slip pl left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on
                     pn.quotation_ref=pl.quotation_ref join endorsement plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id  INNER JOIN staffcustomer sc
                     ON pl.customer_id = sc.customerid INNER JOIN country ct
                     ON pl.currencytype = ct.id LEFT JOIN customer_account
                      ON (customer_account.customer_id = pl.customer_id)  ";



                }
                else  if($trtype=='cancellation')
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.canpolicyno as policy_no,plc.canrefno as qref,p.todate as qrefdate,0-plc.premiumchange as premium,p.insurance_company_id,pn.policyno,covers.cover,'0' as policy_fees,
                  p.brokerage,p.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate, plc.remarks as description from placing_slip pl
                  left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on pn.quotation_ref=pl.quotation_ref
                  join cancellation plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id INNER JOIN staffcustomer sc
                  ON pl.customer_id = sc.customerid INNER JOIN country ct
                  ON pl.currencytype = ct.id LEFT JOIN customer_account
                  ON (customer_account.customer_id = pl.customer_id)  ";


                }

                else
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from declaration_stakeholders where declaration_stakeholders.declaration_id=p.declaration_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,plc.certificate_refno as qref,plc.certificate_no as policy_no, p.todate as qrefdate,p.insurance_company_id,pn.policyno,
                  p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref,plc.currencytype,p.policy_fees,sc.staffid,sc.date as effectivedate, '' as description  from declaration plc
                  join declaration_premiumdetails p on p.declaration_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                  join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                  ON plc.customer_id = sc.customerid INNER JOIN country ct
                  ON plc.currencytype = ct.id  LEFT JOIN customer_account
                  ON (customer_account.customer_id = plc.customer_id)  ";





                }

                $sql=$sql."where  plc.id='$qid' and customer_account.branch_id = '$branch'";




                  if($fromdate != "" && $todate != "")
                 {


                     $sql=$sql." having qrefdate between '$fromdate' and '$todate' ";
                 }
                    $sql=$sql." order by effectivedate desc limit 1";
               //  echo $sql.'<br><br><br>';


                 $res1=$this->db->query($sql);

                 $result=$res1->row();



                if(!isset($result->qref))
                {
                    continue;
                }
                $data[$i]['qtnref']=$result->qref;

                $data[$i]['qdate']=$result->qrefdate;
                $data[$i]['plid']=$result->plid;
                $data[$i]['customerid']=$result->customer_id;
                $data[$i]['covertype']=$result->cover;
                $data[$i]['premium']=$result->premium;
                $data[$i]['brokerage']=$result->brokerage;
                $data[$i]['policy_no']=$result->policy_no;
                $data[$i]['staffid']=$result->staffid;
                $data[$i]['currencycode']=$result->currencycode;
                $data[$i]['cover_id']=$result->cover_id;
                $data[$i]['policy_fees']=$result->policy_fees;
                $data[$i]['insurance_company_id']=$result->insurance_company_id;
                $data[$i]['percentage']=$result->percentage;
                $data[$i]['ispercentage']=$result->ispercentage;
                $data[$i]['remarks']=$result->description;


                $i++;


         }
               // var_dump($data);

                return $data;
     }

     function generatereportsalescover($table,$fromdate,$todate,$cover_id,$branch_id)
     {

          $data = array();

          if($fromdate != "" && $todate != "")
                 {
         $fromdate = $this->mastermodel->convdatformat($fromdate);
         $todate = $this->mastermodel->convdatformat($todate);
                 }
        $sql="select * from placing_slip_refs pr  group by type,pslip_id ";

         $res=$this->db->query($sql);

         $i=0;
         foreach ($res->result_array()  as $restype)
         {
             $trtype=$restype['type'];
             $qid=$restype['pslip_id'];

                if($trtype=='issue')
                 {

                            $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,p.fromdate as qrefdate,p.insurance_company_id,pn.policyno as policy_no, 'null' as endmode,
                      p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref as qref,plc.currencytype ,p.policy_fees,sc.staffid,sc.date as effectivedate from placing_slip plc
                      join placing_slip_premiumdetails p on p.placing_slip_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                      join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                      ON plc.customer_id = sc.customerid INNER JOIN country ct
                      ON plc.currencytype = ct.id  LEFT JOIN customer_account
                      ON (customer_account.customer_id = plc.customer_id)  ";



                 }
                else if($trtype=='endorsement')
                {

                    $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.endrefno as qref,plc.endpolicyno as policy_no,plc.enddate as qrefdate,plc.premiumchange as premium,plc.policyfeeschange as policy_fees,covers.cover, plc.mode as endmode,
                     p.insurance_company_id,pn.policyno,plc.brokerage,plc.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate
                     from placing_slip pl left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on
                     pn.quotation_ref=pl.quotation_ref join endorsement plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id  INNER JOIN staffcustomer sc
                     ON pl.customer_id = sc.customerid INNER JOIN country ct
                     ON pl.currencytype = ct.id LEFT JOIN customer_account
                      ON (customer_account.customer_id = pl.customer_id)  ";



                }
                else  if($trtype=='cancellation')
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.canpolicyno as policy_no,plc.canrefno as qref,p.fromdate as qrefdate,0-plc.premiumchange as premium,p.insurance_company_id,pn.policyno,covers.cover,'0' as policy_fees, 'null' as endmode,
                  p.brokerage,p.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate  from placing_slip pl
                  left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on pn.quotation_ref=pl.quotation_ref
                  join cancellation plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id INNER JOIN staffcustomer sc
                  ON pl.customer_id = sc.customerid INNER JOIN country ct
                  ON pl.currencytype = ct.id LEFT JOIN customer_account
                  ON (customer_account.customer_id = pl.customer_id)  ";


                }

                else
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from declaration_stakeholders where declaration_stakeholders.declaration_id=p.declaration_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,plc.certificate_refno as qref,plc.certificate_no as policy_no, p.fromdate as qrefdate,p.insurance_company_id,pn.policyno, 'null' as endmode,
                  p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref,plc.currencytype,p.policy_fees,sc.staffid,sc.date as effectivedate  from declaration plc
                  join declaration_premiumdetails p on p.declaration_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                  join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                  ON plc.customer_id = sc.customerid INNER JOIN country ct
                  ON plc.currencytype = ct.id  LEFT JOIN customer_account
                  ON (customer_account.customer_id = plc.customer_id)  ";





                }

                $sql=$sql."where  plc.id='$qid' and cover_id='$cover_id'";


                
                  if($branch_id!='branch_id')
                 {
                     $sql=$sql." and customer_account.branch_id = '$branch_id'";
                 }

                  if($fromdate != "" && $todate != "")
                 {
                     

                     $sql=$sql." having qrefdate between '$fromdate' and '$todate' ";
                 }
                 
                    $sql=$sql." order by effectivedate desc limit 1";
//                 echo $sql.'<br><br><br>';


                 $res1=$this->db->query($sql);

                 $result=$res1->row();



                if(!isset($result->qref))
                {
                    continue;
                }
                $data[$i]['qtnref']=$result->qref;

                $data[$i]['qdate']=$result->qrefdate;
                $data[$i]['plid']=$result->plid;
                $data[$i]['customerid']=$result->customer_id;
                $data[$i]['covertype']=$result->cover;
                $data[$i]['premium']=$result->premium;
                $data[$i]['brokerage']=$result->brokerage;
                $data[$i]['policy_no']=$result->policy_no;
                $data[$i]['staffid']=$result->staffid;
                $data[$i]['currencycode']=$result->currencycode;
                $data[$i]['cover_id']=$result->cover_id;
                $data[$i]['policy_fees']=$result->policy_fees;
                $data[$i]['insurance_company_id']=$result->insurance_company_id;
                $data[$i]['percentage']=$result->percentage;
                 $data[$i]['ispercentage']=$result->ispercentage;
                 $data[$i]['endmode']=$result->endmode;


                $i++;


         }
               // var_dump($data);

                return $data;


     }
     
     function generatereportsalesinsclass($table,$fromdate,$todate,$segment_id,$branch_id)
     {

          $data = array();

          if($fromdate != "" && $todate != "")
                 {
         $fromdate = $this->mastermodel->convdatformat($fromdate);
         $todate = $this->mastermodel->convdatformat($todate);
                 }
        $sql="select * from placing_slip_refs pr  group by type,pslip_id ";

         $res=$this->db->query($sql);

         $i=0;
         foreach ($res->result_array()  as $restype)
         {
             $trtype=$restype['type'];
             $qid=$restype['pslip_id'];

                if($trtype=='issue')
                 {

                            $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,p.fromdate as qrefdate,p.insurance_company_id,pn.policyno as policy_no, 'null' as endmode,
                      p.brokerage,p.ispercentage,segment,plc.id as plid,plc.cover_id,plc.quotation_ref as qref,plc.currencytype ,p.policy_fees,sc.staffid,sc.date as effectivedate from placing_slip plc
                      join placing_slip_premiumdetails p on p.placing_slip_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                      join covers c on c.id=plc.cover_id 
                      join segment_insurances si on si.id = c.segment_id
                      INNER JOIN staffcustomer sc
                      ON plc.customer_id = sc.customerid INNER JOIN country ct
                      ON plc.currencytype = ct.id  LEFT JOIN customer_account
                      ON (customer_account.customer_id = plc.customer_id)  ";



                 }
                else if($trtype=='endorsement')
                {

                    $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.endrefno as qref,plc.endpolicyno as policy_no,plc.enddate as qrefdate,plc.premiumchange as premium,plc.policyfeeschange as policy_fees,covers.cover, plc.mode as endmode,
                     p.insurance_company_id,pn.policyno,segment,plc.brokerage,plc.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate
                     from placing_slip pl left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on
                     pn.quotation_ref=pl.quotation_ref join endorsement plc on plc.policyid=pn.id left 
                     join covers on covers.id=pl.cover_id  
                    join segment_insurances si on si.id = covers.segment_id 
                    INNER JOIN staffcustomer sc
                     ON pl.customer_id = sc.customerid INNER JOIN country ct
                     ON pl.currencytype = ct.id LEFT JOIN customer_account
                      ON (customer_account.customer_id = pl.customer_id)  ";



                }
                else  if($trtype=='cancellation')
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.canpolicyno as policy_no,plc.canrefno as qref,p.fromdate as qrefdate,0-plc.premiumchange as premium,p.insurance_company_id,pn.policyno,covers.cover,'0' as policy_fees, 'null' as endmode,
                  p.brokerage,p.ispercentage,pl.id as plid,segment,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate  from placing_slip pl
                  left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on pn.quotation_ref=pl.quotation_ref
                  join cancellation plc on plc.policyid=pn.id left 
                  join covers on covers.id=pl.cover_id 
                  join segment_insurances si on si.id = covers.segment_id 
                    INNER JOIN staffcustomer sc
                  ON pl.customer_id = sc.customerid INNER JOIN country ct
                  ON pl.currencytype = ct.id LEFT JOIN customer_account
                  ON (customer_account.customer_id = pl.customer_id)  ";


                }

                else
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from declaration_stakeholders where declaration_stakeholders.declaration_id=p.declaration_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,plc.certificate_refno as qref,plc.certificate_no as policy_no, p.fromdate as qrefdate,p.insurance_company_id,pn.policyno, 'null' as endmode,
                  p.brokerage,p.ispercentage,plc.id as plid,segment,plc.cover_id,plc.quotation_ref,plc.currencytype,p.policy_fees,sc.staffid,sc.date as effectivedate  from declaration plc
                  join declaration_premiumdetails p on p.declaration_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                  join covers c on c.id=plc.cover_id 
                  join segment_insurances si on si.id = covers.segment_id 
                    INNER JOIN staffcustomer sc
                  ON plc.customer_id = sc.customerid INNER JOIN country ct
                  ON plc.currencytype = ct.id  LEFT JOIN customer_account
                  ON (customer_account.customer_id = plc.customer_id)  ";





                }

                $sql=$sql."where  plc.id='$qid' and si.id='$segment_id'";


                
                  if($branch_id!='branch_id')
                 {
                     $sql=$sql." and customer_account.branch_id = '$branch_id'";
                 }

                  if($fromdate != "" && $todate != "")
                 {
                     

                     $sql=$sql." having qrefdate between '$fromdate' and '$todate' ";
                 }
                 
                    $sql=$sql." order by effectivedate desc limit 1";
                 
                 $res1=$this->db->query($sql);
                 
                 $result=$res1->row();


                 if(!isset($result->qref))
                {
                    continue;
                }
                
                $data[$i]['qtnref']=$result->qref;

                $data[$i]['qdate']=$result->qrefdate;
                $data[$i]['plid']=$result->plid;
                $data[$i]['customerid']=$result->customer_id;
                $data[$i]['covertype']=$result->cover;
                $data[$i]['segment']=$result->segment;
                $data[$i]['premium']=$result->premium;
                $data[$i]['brokerage']=$result->brokerage;
                $data[$i]['policy_no']=$result->policy_no;
                $data[$i]['staffid']=$result->staffid;
                $data[$i]['currencycode']=$result->currencycode;
                $data[$i]['cover_id']=$result->cover_id;
                $data[$i]['policy_fees']=$result->policy_fees;
                $data[$i]['insurance_company_id']=$result->insurance_company_id;
                $data[$i]['percentage']=$result->percentage;
                $data[$i]['ispercentage']=$result->ispercentage;
                $data[$i]['endmode']=$result->endmode;


                $i++;


         }
               // var_dump($data);

                return $data;


     }
     
     function generatereportrenewcover($table,$fromdate,$todate,$cover_id,$branch_id)
     {
         $data = array();

          if($fromdate != "" && $todate != "")
                 {
         $fromdate = $this->mastermodel->convdatformat($fromdate);
         $todate = $this->mastermodel->convdatformat($todate);
                 }
        $sql="select * from placing_slip_refs pr  group by type,pslip_id ";

         $res=$this->db->query($sql);

         $i=0;
         foreach ($res->result_array()  as $restype)
         {
             $trtype=$restype['type'];
             $qid=$restype['pslip_id'];

                if($trtype=='issue')
                 {

                            $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,p.todate as qrefdate,p.insurance_company_id,pn.policyno as policy_no,
                      p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref as qref,plc.currencytype ,p.policy_fees,sc.staffid,sc.date as effectivedate from placing_slip plc
                      join placing_slip_premiumdetails p on p.placing_slip_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                      join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                      ON plc.customer_id = sc.customerid INNER JOIN country ct
                      ON plc.currencytype = ct.id  LEFT JOIN customer_account
                      ON (customer_account.customer_id = plc.customer_id)  ";



                 }
                else if($trtype=='endorsement')
                {

                    $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.endrefno as qref,plc.endpolicyno as policy_no,p.todate as qrefdate,plc.premiumchange as premium,plc.policyfeeschange as policy_fees,covers.cover,
                     p.insurance_company_id,pn.policyno,plc.brokerage,plc.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate
                     from placing_slip pl left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on
                     pn.quotation_ref=pl.quotation_ref join endorsement plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id  INNER JOIN staffcustomer sc
                     ON pl.customer_id = sc.customerid INNER JOIN country ct
                     ON pl.currencytype = ct.id LEFT JOIN customer_account
                      ON (customer_account.customer_id = pl.customer_id)  ";



                }
                else  if($trtype=='cancellation')
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.canpolicyno as policy_no,plc.canrefno as qref,p.todate as qrefdate,0-plc.premiumchange as premium,p.insurance_company_id,pn.policyno,covers.cover,'0' as policy_fees,
                  p.brokerage,p.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate  from placing_slip pl
                  left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on pn.quotation_ref=pl.quotation_ref
                  join cancellation plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id INNER JOIN staffcustomer sc
                  ON pl.customer_id = sc.customerid INNER JOIN country ct
                  ON pl.currencytype = ct.id LEFT JOIN customer_account
                  ON (customer_account.customer_id = pl.customer_id)  ";


                }

                else
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from declaration_stakeholders where declaration_stakeholders.declaration_id=p.declaration_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,plc.certificate_refno as qref,plc.certificate_no as policy_no, p.todate as qrefdate,p.insurance_company_id,pn.policyno,
                  p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref,plc.currencytype,p.policy_fees,sc.staffid,sc.date as effectivedate  from declaration plc
                  join declaration_premiumdetails p on p.declaration_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                  join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                  ON plc.customer_id = sc.customerid INNER JOIN country ct
                  ON plc.currencytype = ct.id  LEFT JOIN customer_account
                  ON (customer_account.customer_id = plc.customer_id)  ";





                }

                $sql=$sql."where  plc.id='$qid' and cover_id='$cover_id'";



                  if($branch_id!='branch_id')
                 {
                     $sql=$sql." and customer_account.branch_id = '$branch_id'";
                 }

                  if($fromdate != "" && $todate != "")
                 {


                     $sql=$sql." having qrefdate between '$fromdate' and '$todate' ";
                 }
                 
                    $sql=$sql." order by effectivedate desc limit 1";
                // echo $sql.'<br><br><br>';


                 $res1=$this->db->query($sql);

                 $result=$res1->row();



                if(!isset($result->qref))
                {
                    continue;
                }
                $data[$i]['qtnref']=$result->qref;

                $data[$i]['qdate']=$result->qrefdate;
                $data[$i]['plid']=$result->plid;
                $data[$i]['customerid']=$result->customer_id;
                $data[$i]['covertype']=$result->cover;
                $data[$i]['premium']=$result->premium;
                $data[$i]['brokerage']=$result->brokerage;
                $data[$i]['policy_no']=$result->policy_no;
                $data[$i]['staffid']=$result->staffid;
                $data[$i]['currencycode']=$result->currencycode;
                $data[$i]['cover_id']=$result->cover_id;
                $data[$i]['policy_fees']=$result->policy_fees;
                $data[$i]['insurance_company_id']=$result->insurance_company_id;
                $data[$i]['percentage']=$result->percentage;
                $data[$i]['ispercentage']=$result->ispercentage;        

                $i++;


         }
               // var_dump($data);

                return $data;

     }
     
     function generatereportrenewinsclass($table,$fromdate,$todate,$segment_id,$branch_id)
     {
         $data = array();

          if($fromdate != "" && $todate != "")
                 {
         $fromdate = $this->mastermodel->convdatformat($fromdate);
         $todate = $this->mastermodel->convdatformat($todate);
                 }
        $sql="select * from placing_slip_refs pr  group by type,pslip_id ";

         $res=$this->db->query($sql);

         $i=0;
         foreach ($res->result_array()  as $restype)
         {
             $trtype=$restype['type'];
             $qid=$restype['pslip_id'];

                if($trtype=='issue')
                 {

                            $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,p.todate as qrefdate,p.insurance_company_id,pn.policyno as policy_no,
                      p.brokerage,segment,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref as qref,plc.currencytype ,p.policy_fees,sc.staffid,sc.date as effectivedate from placing_slip plc
                      join placing_slip_premiumdetails p on p.placing_slip_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                      join covers c on c.id=plc.cover_id 
                      join segment_insurances si on si.id = c.segment_id
                      INNER JOIN staffcustomer sc
                      ON plc.customer_id = sc.customerid INNER JOIN country ct
                      ON plc.currencytype = ct.id  LEFT JOIN customer_account
                      ON (customer_account.customer_id = plc.customer_id)  ";



                 }
                else if($trtype=='endorsement')
                {

                    $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.endrefno as qref,plc.endpolicyno as policy_no,p.todate as qrefdate,plc.premiumchange as premium,plc.policyfeeschange as policy_fees,covers.cover,
                     p.insurance_company_id,segment,pn.policyno,plc.brokerage,plc.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate
                     from placing_slip pl left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on
                     pn.quotation_ref=pl.quotation_ref join endorsement plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id
                    join segment_insurances si on si.id = covers.segment_id 
                    INNER JOIN staffcustomer sc
                     ON pl.customer_id = sc.customerid INNER JOIN country ct
                     ON pl.currencytype = ct.id LEFT JOIN customer_account
                      ON (customer_account.customer_id = pl.customer_id)  ";



                }
                else  if($trtype=='cancellation')
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.canpolicyno as policy_no,plc.canrefno as qref,p.todate as qrefdate,0-plc.premiumchange as premium,p.insurance_company_id,pn.policyno,covers.cover,'0' as policy_fees,
                  p.brokerage,p.ispercentage,segment,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate  from placing_slip pl
                  left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on pn.quotation_ref=pl.quotation_ref
                  join cancellation plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id
                  join segment_insurances si on si.id = covers.segment_id
                    INNER JOIN staffcustomer sc
                  ON pl.customer_id = sc.customerid INNER JOIN country ct
                  ON pl.currencytype = ct.id LEFT JOIN customer_account
                  ON (customer_account.customer_id = pl.customer_id)  ";


                }

                else
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from declaration_stakeholders where declaration_stakeholders.declaration_id=p.declaration_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,plc.certificate_refno as qref,plc.certificate_no as policy_no, p.todate as qrefdate,p.insurance_company_id,pn.policyno,
                  p.brokerage,p.ispercentage,segment,plc.id as plid,plc.cover_id,plc.quotation_ref,plc.currencytype,p.policy_fees,sc.staffid,sc.date as effectivedate  from declaration plc
                  join declaration_premiumdetails p on p.declaration_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                  join covers c on c.id=plc.cover_id 
                  join segment_insurances si on si.id = c.segment_id
                    INNER JOIN staffcustomer sc
                  ON plc.customer_id = sc.customerid INNER JOIN country ct
                  ON plc.currencytype = ct.id  LEFT JOIN customer_account
                  ON (customer_account.customer_id = plc.customer_id)  ";





                }

                $sql=$sql."where  plc.id='$qid' and si.id='$segment_id'";



                  if($branch_id!='branch_id')
                 {
                     $sql=$sql." and customer_account.branch_id = '$branch_id'";
                 }

                  if($fromdate != "" && $todate != "")
                 {


                     $sql=$sql." having qrefdate between '$fromdate' and '$todate' ";
                 }
                 
                    $sql=$sql." order by effectivedate desc limit 1";
//                 echo $sql.'<br><br><br>';


                 $res1=$this->db->query($sql);

                 $result=$res1->row();



                if(!isset($result->qref))
                {
                    continue;
                }
                $data[$i]['qtnref']=$result->qref;

                $data[$i]['qdate']=$result->qrefdate;
                $data[$i]['plid']=$result->plid;
                $data[$i]['segment']=$result->segment;
                $data[$i]['customerid']=$result->customer_id;
                $data[$i]['covertype']=$result->cover;
                $data[$i]['premium']=$result->premium;
                $data[$i]['brokerage']=$result->brokerage;
                $data[$i]['policy_no']=$result->policy_no;
                $data[$i]['staffid']=$result->staffid;
                $data[$i]['currencycode']=$result->currencycode;
                $data[$i]['cover_id']=$result->cover_id;
                $data[$i]['policy_fees']=$result->policy_fees;
                $data[$i]['insurance_company_id']=$result->insurance_company_id;
                $data[$i]['percentage']=$result->percentage;
                $data[$i]['ispercentage']=$result->ispercentage;        

                $i++;


         }
               // var_dump($data);

                return $data;

     }

     function generatereportsalesstf($table,$fromdate,$todate,$staffid,$cover_id,$branch_id,$selectedcovers="")
     {
            $data = array();
            if($fromdate != "" && $todate != "")
                 {
         $fromdate = $this->mastermodel->convdatformat($fromdate);
         $todate = $this->mastermodel->convdatformat($todate);
                 }

        $sql="select * from placing_slip_refs pr  group by type,pslip_id ";

         $res=$this->db->query($sql);

         $i=0;
         foreach ($res->result_array()  as $restype)
         {
             $trtype=$restype['type'];
             $qid=$restype['pslip_id'];

                if($trtype=='issue')
                 {

                            $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,p.fromdate as qrefdate,p.insurance_company_id,pn.policyno as policy_no,'null' as endmode,
                      p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref as qref,plc.currencytype ,p.policy_fees,sc.staffid,sc.date as effectivedate from placing_slip plc
                      join placing_slip_premiumdetails p on p.placing_slip_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                      join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                      ON plc.customer_id = sc.customerid INNER JOIN country ct
                      ON plc.currencytype = ct.id  LEFT JOIN customer_account
                      ON (customer_account.customer_id = plc.customer_id)  ";



                 }
                else if($trtype=='endorsement')
                {

                    $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.endrefno as qref,plc.endpolicyno as policy_no,plc.enddate as qrefdate,plc.premiumchange as premium,plc.policyfeeschange as policy_fees,covers.cover,plc.mode as endmode,
                     p.insurance_company_id,pn.policyno,plc.brokerage,plc.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate
                     from placing_slip pl left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on
                     pn.quotation_ref=pl.quotation_ref join endorsement plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id  INNER JOIN staffcustomer sc
                     ON pl.customer_id = sc.customerid INNER JOIN country ct
                     ON pl.currencytype = ct.id LEFT JOIN customer_account
                      ON (customer_account.customer_id = pl.customer_id)  ";



                }
                else  if($trtype=='cancellation')
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.canpolicyno as policy_no,plc.canrefno as qref,p.fromdate as qrefdate,0-plc.premiumchange as premium,p.insurance_company_id,pn.policyno,covers.cover,'0' as policy_fees,'null' as endmode,
                  p.brokerage,p.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate  from placing_slip pl
                  left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on pn.quotation_ref=pl.quotation_ref
                  join cancellation plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id INNER JOIN staffcustomer sc
                  ON pl.customer_id = sc.customerid INNER JOIN country ct
                  ON pl.currencytype = ct.id LEFT JOIN customer_account
                  ON (customer_account.customer_id = pl.customer_id)  ";


                }

                else
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from declaration_stakeholders where declaration_stakeholders.declaration_id=p.declaration_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,plc.certificate_refno as qref,plc.certificate_no as policy_no, p.fromdate as qrefdate,p.insurance_company_id,pn.policyno,'null' as endmode,
                  p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref,plc.currencytype,p.policy_fees,sc.staffid,sc.date as effectivedate  from declaration plc
                  join declaration_premiumdetails p on p.declaration_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                  join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                  ON plc.customer_id = sc.customerid INNER JOIN country ct
                  ON plc.currencytype = ct.id  LEFT JOIN customer_account
                  ON (customer_account.customer_id = plc.customer_id)  ";
                        
                }


               if($selectedcovers!='')
                {
                    $cvrs  = str_replace('_', ',',$selectedcovers);
                    $sql=$sql."where  plc.id='$qid' and staffid='$staffid' and (cover_id IN (  $cvrs ))";
                }
                else
                     $sql=$sql."where  plc.id='$qid' and staffid='$staffid'";


                 if($cover_id!="cover_id")
                 {
                     $sql=$sql." and cover_id = '$cover_id'";
                 }

                 if($branch_id!='branch_id')
                 {
                     $sql=$sql." and customer_account.branch_id = '$branch_id'";
                 }

                  if($fromdate != "" && $todate != "")
                 {
                    
                     $sql=$sql." having qrefdate between '$fromdate' and '$todate' ";
                 }
                 
                 $sql=$sql." order by effectivedate desc limit 1";
                 //echo $sql.'<br><br><br>';
                

                 $res1=$this->db->query($sql);
                 

                 $result=$res1->row();



                if(!isset($result->qref))
                {
                    continue;
                }
                $data[$i]['qtnref']=$result->qref;

                $data[$i]['qdate']=$result->qrefdate;
                $data[$i]['plid']=$result->plid;
                $data[$i]['customerid']=$result->customer_id;
                $data[$i]['covertype']=$result->cover;
                $data[$i]['premium']=$result->premium;
                $data[$i]['brokerage']=$result->brokerage;
                $data[$i]['policy_no']=$result->policy_no;
                $data[$i]['staffid']=$result->staffid;
                $data[$i]['currencycode']=$result->currencycode;
                $data[$i]['cover_id']=$result->cover_id;
                $data[$i]['policy_fees']=$result->policy_fees;
                $data[$i]['insurance_company_id']=$result->insurance_company_id;
                $data[$i]['percentage']=$result->percentage;
                $data[$i]['endmode']=$result->endmode;
                $data[$i]['ispercentage']=$result->ispercentage;
                
                $i++;


         }
               // var_dump($data);

                return $data;





     }
     function generatereportrenewstf($table,$fromdate,$todate,$staffid,$cover_id,$branch_id)
     {

         $data = array();
            if($fromdate != "" && $todate != "")
                 {
         $fromdate = $this->mastermodel->convdatformat($fromdate);
         $todate = $this->mastermodel->convdatformat($todate);
                 }

        $sql="select * from placing_slip_refs pr  group by type,pslip_id ";

         $res=$this->db->query($sql);

         $i=0;
         foreach ($res->result_array()  as $restype)
         {
             $trtype=$restype['type'];
             $qid=$restype['pslip_id'];

                if($trtype=='issue')
                 {

                            $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,p.todate as qrefdate,p.insurance_company_id,pn.policyno as policy_no,
                      p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref as qref,plc.currencytype ,p.policy_fees,sc.staffid,sc.date as effectivedate from placing_slip plc
                      join placing_slip_premiumdetails p on p.placing_slip_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                      join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                      ON plc.customer_id = sc.customerid 
                      INNER JOIN
                        (
                            SELECT customerid, MAX(date) date
                            FROM staffcustomer
                            GROUP BY customerid
                        ) b ON sc.customerid = b.customerid AND
                                sc.date = b.date

                      INNER JOIN country ct
                      ON plc.currencytype = ct.id  LEFT JOIN customer_account
                      ON (customer_account.customer_id = plc.customer_id)  ";



                 }
                else if($trtype=='endorsement')
                {

                    $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.endrefno as qref,plc.endpolicyno as policy_no,p.todate as qrefdate,plc.premiumchange as premium,plc.policyfeeschange as policy_fees,covers.cover,
                     p.insurance_company_id,pn.policyno,plc.brokerage,plc.ispercentage,p.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate
                     from placing_slip pl left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on
                     pn.quotation_ref=pl.quotation_ref join endorsement plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id  INNER JOIN staffcustomer sc
                     ON pl.customer_id = sc.customerid INNER JOIN country ct
                     ON pl.currencytype = ct.id LEFT JOIN customer_account
                      ON (customer_account.customer_id = pl.customer_id)  ";



                }
                else  if($trtype=='cancellation')
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.canpolicyno as policy_no,plc.canrefno as qref,p.todate as qrefdate,0-plc.premiumchange as premium,p.insurance_company_id,pn.policyno,covers.cover,'0' as policy_fees,
                  p.brokerage,p.ispercentage,p.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate  from placing_slip pl
                  left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on pn.quotation_ref=pl.quotation_ref
                  join cancellation plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id INNER JOIN staffcustomer sc
                  ON pl.customer_id = sc.customerid INNER JOIN country ct
                  ON pl.currencytype = ct.id LEFT JOIN customer_account
                  ON (customer_account.customer_id = pl.customer_id)  ";


                }

                else
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from declaration_stakeholders where declaration_stakeholders.declaration_id=p.declaration_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,plc.certificate_refno as qref,plc.certificate_no as policy_no, p.todate as qrefdate,p.insurance_company_id,pn.policyno,
                  p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref,plc.currencytype,p.policy_fees,sc.staffid,sc.date as effectivedate  from declaration plc
                  join declaration_premiumdetails p on p.declaration_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                  join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                  ON plc.customer_id = sc.customerid INNER JOIN country ct
                  ON plc.currencytype = ct.id  LEFT JOIN customer_account
                  ON (customer_account.customer_id = plc.customer_id)  ";





                }

                $sql=$sql."where  plc.id='$qid' and staffid='$staffid'";


                 if($cover_id!="cover_id")
                 {
                     $sql=$sql." and cover_id = '$cover_id'";
                 }

                  if($branch_id!='branch_id')
                 {
                     $sql=$sql." and customer_account.branch_id = '$branch_id'";
                 }

                  if($fromdate != "" && $todate != "")
                 {

                     $sql=$sql." having qrefdate between '$fromdate' and '$todate' ";
                 }
                 
                 $sql=$sql." order by effectivedate desc limit 1";
                // echo $sql.'<br><br><br>';


                 $res1=$this->db->query($sql);

                 $result=$res1->row();



                if(!isset($result->qref))
                {
                    continue;
                }
                $data[$i]['qtnref']=$result->qref;

                $data[$i]['qdate']=$result->qrefdate;
                $data[$i]['plid']=$result->plid;
                $data[$i]['customerid']=$result->customer_id;
                $data[$i]['covertype']=$result->cover;
                $data[$i]['premium']=$result->premium;
                $data[$i]['brokerage']=$result->brokerage;
                $data[$i]['policy_no']=$result->policy_no;
                $data[$i]['staffid']=$result->staffid;
                $data[$i]['currencycode']=$result->currencycode;
                $data[$i]['cover_id']=$result->cover_id;
                $data[$i]['policy_fees']=$result->policy_fees;
                $data[$i]['insurance_company_id']=$result->insurance_company_id;
                $data[$i]['percentage']=$result->percentage;
                $data[$i]['ispercentage']=$result->ispercentage;


                $i++;


         }
               // var_dump($data);

                return $data;


     }
     function generatereportbrostf($table,$fromdate,$todate,$staffid)
     {
         $data = array();

         $sql = "select distinct broking_slip.id as brid, broking_slip.customer_id,broking_slip.cover_id, broking_slip.quotation_ref,broking_slip.quotation_date,broking_slip.status, staffcustomer.staffid, staffcustomer.customerid from broking_slip inner join staffcustomer on broking_slip.customer_id = staffcustomer.customerid and staffcustomer.staffid = '$staffid'";


         if($fromdate != "" && $todate != "")
         {
             $fromdate = $this->mastermodel->convdatformat($fromdate);
        $todate = $this->mastermodel->convdatformat($todate);

             $sql=$sql." and broking_slip.quotation_date between '$fromdate' and '$todate'";
         }


         $Q=$this->db->query($sql);

        if ($Q->num_rows() > 0)
         {
             foreach ($Q->result_array() as $row)
             {
                 $data[] = $row;
             }
         }
         $Q->free_result();
         return $data;
     }
     function generatereportplastf($table,$fromdate,$todate,$staffid)
     {
         $data = array();

         $sql = "select placing_slip.id as plid, placing_slip.customer_id,placing_slip.cover_id, placing_slip.quotation_ref,placing_slip.quotation_date,placing_slip.status, staffcustomer.staffid, staffcustomer.customerid,placing_slip_premiumdetails.premium,placing_slip_premiumdetails.brokerage,placing_slip_premiumdetails.ispercentage,placing_slip_premiumdetails.insurance_company_id,insurance_company.company_name from placing_slip inner join staffcustomer inner join placing_slip_premiumdetails inner join insurance_company on placing_slip_premiumdetails.placing_slip_id = placing_slip.id and placing_slip.customer_id = staffcustomer.customerid and insurance_company.id=placing_slip_premiumdetails.insurance_company_id and staffcustomer.staffid = '$staffid'";


         if($fromdate != "" && $todate != "")
         {
             $fromdate = $this->mastermodel->convdatformat($fromdate);
        $todate = $this->mastermodel->convdatformat($todate);

             $sql=$sql." and placing_slip.quotation_date between '$fromdate' and '$todate'";
         }


         $Q=$this->db->query($sql);

        if ($Q->num_rows() > 0)
         {
             foreach ($Q->result_array() as $row)
             {
                 $data[] = $row;
             }
         }
         $Q->free_result();
         return $data;
     }
     function generatereportbroinscomp($table,$fromdate,$todate,$insurance_company_id)
        {
            $data = array();

         $sql = "select distinct broking_slip.id, broking_slip.quotation_ref,broking_slip.cover_id,broking_slip.customer_id,broking_slip.quotation_date,broking_slip.status from broking_slip inner join broking_slip_status on broking_slip_status.broking_slip_id = broking_slip.id  where broking_slip_status.companyid=$insurance_company_id";


         if($fromdate != "" && $todate != "")
         {
             $fromdate = $this->mastermodel->convdatformat($fromdate);
            $todate = $this->mastermodel->convdatformat($todate);

             $sql=$sql." and broking_slip.quotation_date between '$fromdate' and '$todate'";
         }

         $Q=$this->db->query($sql);

        if ($Q->num_rows() > 0)
         {
             foreach ($Q->result_array() as $row)
             {
                 $data[] = $row;
             }
         }
         $Q->free_result();
         return $data;
     }
     function generatereportbrobranch($table,$fromdate,$todate,$branch_id)
     {
        $data = array();
        $sql = "select distinct broking_slip.id, broking_slip.quotation_ref,broking_slip.cover_id,broking_slip.customer_id,broking_slip.quotation_date,broking_slip.status from broking_slip inner join customer_account on customer_account.customer_id = broking_slip.customer_id  where customer_account.branch_id=$branch_id";
        if($fromdate != "" && $todate != "")
        {
            $fromdate = $this->mastermodel->convdatformat($fromdate);
            $todate = $this->mastermodel->convdatformat($todate);
            $sql=$sql." and broking_slip.quotation_date between '$fromdate' and '$todate'";
        }
        $Q=$this->db->query($sql);
        if ($Q->num_rows() > 0)
        {
             foreach ($Q->result_array() as $row)
             {
                 $data[] = $row;
             }
        }
        $Q->free_result();
        return $data;
     }
     function generatereportplainscomp($table,$fromdate,$todate,$insurance_company_id)
     {
         $data = array();

         $sql = "select placing_slip.id as plid, placing_slip.customer_id,placing_slip.cover_id, placing_slip.quotation_ref,placing_slip.quotation_date,placing_slip.status, staffcustomer.staffid, staffcustomer.customerid,placing_slip_premiumdetails.premium,placing_slip_premiumdetails.brokerage,placing_slip_premiumdetails.ispercentage,placing_slip_premiumdetails.insurance_company_id,insurance_company.company_name from placing_slip inner join staffcustomer inner join placing_slip_premiumdetails inner join insurance_company on placing_slip_premiumdetails.placing_slip_id = placing_slip.id and placing_slip.customer_id = staffcustomer.customerid and insurance_company.id=placing_slip_premiumdetails.insurance_company_id and placing_slip_premiumdetails.insurance_company_id  = '$insurance_company_id'";


         if($fromdate != "" && $todate != "")
         {
             $fromdate = $this->mastermodel->convdatformat($fromdate);
        $todate = $this->mastermodel->convdatformat($todate);

             $sql=$sql." and placing_slip.quotation_date between '$fromdate' and '$todate'";
         }


         $Q=$this->db->query($sql);

        if ($Q->num_rows() > 0)
         {
             foreach ($Q->result_array() as $row)
             {
                 $data[] = $row;
             }
         }
         $Q->free_result();
         return $data;
     }
     function generatereportsalesinscomp($table,$fromdate,$todate,$inscompid,$cover_id,$branch_id)
     {
         $data = array();
         
         if($fromdate != "" && $todate != "")
                 {
         $fromdate = $this->mastermodel->convdatformat($fromdate);
         $todate = $this->mastermodel->convdatformat($todate);
                 }
        $sql="select * from placing_slip_refs pr  group by type,pslip_id ";

         $res=$this->db->query($sql);

         $i=0;
         foreach ($res->result_array()  as $restype)
         {
             $trtype=$restype['type'];
             $qid=$restype['pslip_id'];

                if($trtype=='issue')
                 {

                            $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,p.fromdate as qrefdate,p.insurance_company_id,pn.policyno as policy_no,'null' as endmode,
                      p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref as qref,plc.currencytype ,p.policy_fees,sc.staffid,sc.date as effectivedate from placing_slip plc
                      join placing_slip_premiumdetails p on p.placing_slip_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                      join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                      ON plc.customer_id = sc.customerid INNER JOIN country ct
                      ON plc.currencytype = ct.id  LEFT JOIN customer_account
                      ON (customer_account.customer_id = plc.customer_id)  ";



                 }
                else if($trtype=='endorsement')
                {

                    $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.endrefno as qref,plc.endpolicyno as policy_no,plc.enddate as qrefdate,plc.premiumchange as premium,plc.policyfeeschange as policy_fees,covers.cover,plc.mode as endmode,
                     p.insurance_company_id,pn.policyno,plc.brokerage,plc.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate
                     from placing_slip pl left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on
                     pn.quotation_ref=pl.quotation_ref join endorsement plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id  INNER JOIN staffcustomer sc
                     ON pl.customer_id = sc.customerid INNER JOIN country ct
                     ON pl.currencytype = ct.id LEFT JOIN customer_account
                      ON (customer_account.customer_id = pl.customer_id)  ";



                }
                else  if($trtype=='cancellation')
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.canpolicyno as policy_no,plc.canrefno as qref,p.fromdate as qrefdate,0-plc.premiumchange as premium,p.insurance_company_id,pn.policyno,covers.cover,'0' as policy_fees,'null' as endmode,
                  p.brokerage,p.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate  from placing_slip pl
                  left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on pn.quotation_ref=pl.quotation_ref
                  join cancellation plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id INNER JOIN staffcustomer sc
                  ON pl.customer_id = sc.customerid INNER JOIN country ct
                  ON pl.currencytype = ct.id LEFT JOIN customer_account
                  ON (customer_account.customer_id = pl.customer_id)  ";


                }

                else
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from declaration_stakeholders where declaration_stakeholders.declaration_id=p.declaration_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,plc.certificate_refno as qref,plc.certificate_no as policy_no, p.fromdate as qrefdate,p.insurance_company_id,pn.policyno,'null' as endmode,
                  p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref,plc.currencytype,p.policy_fees,sc.staffid,sc.date as effectivedate  from declaration plc
                  join declaration_premiumdetails p on p.declaration_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                  join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                  ON plc.customer_id = sc.customerid INNER JOIN country ct
                  ON plc.currencytype = ct.id  LEFT JOIN customer_account
                  ON (customer_account.customer_id = plc.customer_id)  ";





                }
               
                $sql=$sql."where  plc.id='$qid' and insurance_company_id='$inscompid'";

               
                 if($cover_id!="cover_id")
                 {
                     $sql=$sql." and cover_id = '$cover_id'";
                 }

                  if($branch_id!='branch_id')
                 {
                     $sql=$sql." and customer_account.branch_id = '$branch_id'";
                 }

                  if($fromdate != "" && $todate != "")
                 {
                     

                     $sql=$sql." having qrefdate between '$fromdate' and '$todate' ";
                 }
                    
                    $sql=$sql." order by effectivedate desc limit 1";

                
                // echo $sql.'<br><br><br>';


                 $res1=$this->db->query($sql);

                 $result=$res1->row();



                if(!isset($result->qref))
                {
                    continue;
                }
                $data[$i]['qtnref']=$result->qref;

                $data[$i]['qdate']=$result->qrefdate;
                $data[$i]['plid']=$result->plid;
                $data[$i]['customerid']=$result->customer_id;
                $data[$i]['covertype']=$result->cover;
                $data[$i]['premium']=$result->premium;
                $data[$i]['brokerage']=$result->brokerage;
                $data[$i]['policy_no']=$result->policy_no;
                $data[$i]['staffid']=$result->staffid;
                $data[$i]['currencycode']=$result->currencycode;
                $data[$i]['cover_id']=$result->cover_id;
                $data[$i]['policy_fees']=$result->policy_fees;
                $data[$i]['insurance_company_id']=$result->insurance_company_id;
                $data[$i]['percentage']=$result->percentage;
                $data[$i]['ispercentage']=$result->ispercentage;
                $data[$i]['endmode']=$result->endmode;


                $i++;


         }
               // var_dump($data);

                return $data;





     }
     
     
     function generatereportcommissioninscomp($table,$fromdate,$todate,$inscompid,$cover_id,$branch_id)
     {
         $data = array();
         
         if($fromdate != "" && $todate != "")
                 {
         $fromdate = $this->mastermodel->convdatformat($fromdate);
         $todate = $this->mastermodel->convdatformat($todate);
                 }
        $sql="select * from placing_slip_refs pr where type = 'issue' OR type= 'endorsement' group by type,pslip_id ";

         $res=$this->db->query($sql);

         $i=0;
         foreach ($res->result_array()  as $restype)
         {
             $trtype=$restype['type'];
             $qid=$restype['pslip_id'];
             $flag = 0;
            if ($trtype == 'issue') {
                if ($this->mastermodel->getSingleFieldValue('placing_slip_premiumdetails','payment_method','placing_slip_id',$qid) == 'insurance_company') {
                    $flag = 1;
                }
            } else if ($trtype == 'endorsement') {
                if ($this->mastermodel->getSingleFieldValue('endorsement','paymentoptions','id',$qid) == 'insurance_company') {
                    $flag = 1;
                }
            }
            
            if ($flag) {
                if($trtype=='issue')
                 {

                            $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,p.fromdate as qrefdate,p.insurance_company_id,pn.policyno as policy_no,
                      SUM(ca.amount) as received_commission, SUM(customer_receipt_allocation.cr_allocation_amount) as received_premium,p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref as qref,plc.currencytype ,p.policy_fees,sc.staffid,sc.date as effectivedate from placing_slip plc
                      join placing_slip_premiumdetails p on p.placing_slip_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                      join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                      ON plc.customer_id = sc.customerid INNER JOIN country ct
                      ON plc.currencytype = ct.id  LEFT JOIN customer_account
                      ON (customer_account.customer_id = plc.customer_id)
                      LEFT JOIN company_allocate ca on ca.pid=plc.id AND ca.type = 'issue'
                      LEFT JOIN customer_receipt_allocation on cr_allocation_pid=plc.id AND cr_allocation_type = 'issue'  ";



                 }
                else if($trtype=='endorsement')
                {

                    $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.endrefno as qref,plc.endpolicyno as policy_no,plc.enddate as qrefdate,plc.premiumchange as premium,plc.policyfeeschange as policy_fees,covers.cover,
                     SUM(ca.amount) as received_commission, SUM(customer_receipt_allocation.cr_allocation_amount) as received_premium,p.insurance_company_id,pn.policyno,plc.brokerage,plc.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate
                     from placing_slip pl left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on
                     pn.quotation_ref=pl.quotation_ref join endorsement plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id  INNER JOIN staffcustomer sc
                     ON pl.customer_id = sc.customerid INNER JOIN country ct
                     ON pl.currencytype = ct.id LEFT JOIN customer_account
                      ON (customer_account.customer_id = pl.customer_id) 
                      LEFT JOIN company_allocate ca on ca.pid=plc.id AND ca.type = 'endorsement'
                      LEFT JOIN customer_receipt_allocation on cr_allocation_pid=plc.id AND cr_allocation_type = 'endorsement'";



                }
                else  if($trtype=='cancellation')
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.canpolicyno as policy_no,plc.canrefno as qref,p.fromdate as qrefdate,0-plc.premiumchange as premium,p.insurance_company_id,pn.policyno,covers.cover,'0' as policy_fees,
                  SUM(ca.amount) as received_commission,p.brokerage,p.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate  from placing_slip pl
                  left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on pn.quotation_ref=pl.quotation_ref
                  join cancellation plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id INNER JOIN staffcustomer sc
                  ON pl.customer_id = sc.customerid INNER JOIN country ct
                  ON pl.currencytype = ct.id LEFT JOIN customer_account
                  ON (customer_account.customer_id = pl.customer_id) 
                  LEFT JOIN company_allocate ca on ca.pid=plc.id AND ca.type = 'cancellation'";


                }

                else
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from declaration_stakeholders where declaration_stakeholders.declaration_id=p.declaration_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,plc.certificate_refno as qref,plc.certificate_no as policy_no, p.fromdate as qrefdate,p.insurance_company_id,pn.policyno,
                  SUM(ca.amount) as received_commission,p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref,plc.currencytype,p.policy_fees,sc.staffid,sc.date as effectivedate  from declaration plc
                  join declaration_premiumdetails p on p.declaration_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                  join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                  ON plc.customer_id = sc.customerid INNER JOIN country ct
                  ON plc.currencytype = ct.id  LEFT JOIN customer_account
                  ON (customer_account.customer_id = plc.customer_id) 
                  LEFT JOIN company_allocate ca on ca.pid=plc.id";

                }
               
                $sql=$sql."where  plc.id='$qid' and insurance_company_id='$inscompid'";

               
                 if($cover_id!="cover_id")
                 {
                     $sql=$sql." and cover_id = '$cover_id'";
                 }

                  if($branch_id!='branch_id')
                 {
                     $sql=$sql." and customer_account.branch_id = '$branch_id'";
                 }
                    $sql.=" GROUP BY ca.pid";

                  if($fromdate != "" && $todate != "")
                 {
                     

                     $sql=$sql." having qrefdate between '$fromdate' and '$todate' ";
                 }
                    $sql=$sql." order by effectivedate desc limit 1";

                 $res1=$this->db->query($sql);

                 $result=$res1->row();



                if(!isset($result->qref))
                {
                    continue;
                }
                $data[$i]['qtnref']=$result->qref;

                $data[$i]['qdate']=$result->qrefdate;
                $data[$i]['plid']=$result->plid;
                $data[$i]['customerid']=$result->customer_id;
                $data[$i]['covertype']=$result->cover;
                $data[$i]['premium']=$result->premium;
                $data[$i]['brokerage']=$result->brokerage;
                $data[$i]['policy_no']=$result->policy_no;
                $data[$i]['staffid']=$result->staffid;
                $data[$i]['currencycode']=$result->currencycode;
                $data[$i]['cover_id']=$result->cover_id;
                $data[$i]['policy_fees']=$result->policy_fees;
                $data[$i]['insurance_company_id']=$result->insurance_company_id;
                $data[$i]['percentage']=$result->percentage;
                $data[$i]['ispercentage']=$result->ispercentage;
                $data[$i]['received_commission']=$result->received_commission;
                $data[$i]['received_premium']=$result->received_premium;


                $i++;
            }

         }
               // var_dump($data);

                return $data;





     }
     
     function generatereportcustomersoa($fromdate,$todate,$customer_id,$payment_status, $payment_mode)
     {
        $data = array();
        
        if($fromdate != "" && $todate != "")
        {
            $fromdate = $this->mastermodel->convdatformat($fromdate);
            $todate = $this->mastermodel->convdatformat($todate);
        }
        $sql="select * from placing_slip_refs pr where type = 'issue' OR type= 'endorsement' "
                . "group by type,pslip_id order by pslip_id desc";
        $res=$this->db->query($sql);
        $i=0;
        $broker = $this->mastermodel->getSingleFieldValue('mycompany','company','id','1');
       
        foreach ($res->result_array()  as $restype)
        {
            $trtype=$restype['type'];
            $qid=$restype['pslip_id'];
            $flag = false;
            if ($payment_mode == 'all') {
                $flag = true;
            } else if ($payment_mode == 'insurance_company') {
                
                if ($trtype == 'issue') {
                    if ($this->mastermodel->getSingleFieldValue('placing_slip_premiumdetails','payment_method','placing_slip_id',$qid) == 'insurance_company') {
                        $flag = true;
                    }
                } else if ($trtype == 'endorsement') {
                    if ($this->mastermodel->getSingleFieldValue('endorsement','paymentoptions','id',$qid) == 'insurance_company') {
                        $flag = true;
                    }
                }
            } else {
                if ($trtype == 'issue') {
                    if ($this->mastermodel->getSingleFieldValue('placing_slip_premiumdetails','payment_method','placing_slip_id',$qid) != 'insurance_company') {
                        $flag = true;
                    }
                } else if ($trtype == 'endorsement') {
                    if ($this->mastermodel->getSingleFieldValue('endorsement','paymentoptions','id',$qid) != 'insurance_company') {
                        $flag = true;
                    }
                }
            }
            
            if ($flag) {
                if($trtype=='issue')
                {
                    $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id as customer_id,plc.cover_id,p.fromdate as qrefdate,p.insurance_company_id,pn.policyno as policy_no,
                        p.brokerage,company_name,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref as qref,plc.currencytype ,p.policy_fees, 
                        plc.remarks as description ,'' as endorsementmode, p.payment_method as payment_method,
                        IFNULL(SUM(customer_receipt_allocation.cr_allocation_amount),0) as received_premium,
                        IFNULL(SUM(cust_allocate.amount),0) as received_premium_to_broker,
                        plc.quotation_date as issue_date
                        from placing_slip plc
                        join placing_slip_premiumdetails p on p.placing_slip_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                        join covers c on c.id=plc.cover_id  INNER JOIN country ct
                        ON plc.currencytype = ct.id  
                        INNER JOIN insurance_company  ON insurance_company.id = insurance_company_id
                        LEFT JOIN customer_receipt_allocation on cr_allocation_pid=plc.id AND cr_allocation_type = 'issue' 
                        LEFT JOIN cust_allocate on cust_allocate.pid=plc.id AND cust_allocate.type = 'issue' ";



                }
                else if($trtype=='endorsement')
                {

                    $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.endrefno as qref,plc.endpolicyno as policy_no,plc.enddate as qrefdate,plc.premiumchange as premium,plc.policyfeeschange as policy_fees,covers.cover,
                        p.insurance_company_id,company_name,pn.policyno,plc.brokerage,plc.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id as customer_id,pl.quotation_ref,pl.currencytype,
                        plc.remarks as description,plc.mode as endorsementmode, plc.paymentoptions as payment_method, 
                        IFNULL(SUM(customer_receipt_allocation.cr_allocation_amount),0) as received_premium,
                        IFNULL(SUM(cust_allocate.amount),0) as received_premium_to_broker,
                        plc.date as issue_date
                        from placing_slip pl left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on
                        pn.quotation_ref=pl.quotation_ref join endorsement plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id  
                        INNER JOIN country ct
                        ON pl.currencytype = ct.id 
                        INNER JOIN insurance_company  ON insurance_company.id = insurance_company_id
                        LEFT JOIN customer_receipt_allocation on cr_allocation_pid=plc.id AND cr_allocation_type = 'endorsement'
                        LEFT JOIN cust_allocate on cust_allocate.pid=plc.id AND cust_allocate.type = 'endorsement'";
                }
                $sql=$sql."where  plc.id='$qid'";
                
                if ($customer_id != '') {
                    $sql .= " and customer_id='$customer_id'";
                }
                
                $sql.=" GROUP BY plc.id";
                if($fromdate != "" && $todate != "")
                {
                    $sql=$sql." having qrefdate between '$fromdate' and '$todate' ";
                }
                $sql=$sql." order by qrefdate asc ";
                $res1=$this->db->query($sql);
                $result=$res1->row();

                if (!isset($result->qref))
                {
                    continue;
                }
                
                // for payment to Savoy Insurance broker
                if ($result->payment_method != 'insurance_company') {
                    $data[$result->customer_id][0][$i]['qtnref']=$result->qref;
                    $data[$result->customer_id][0][$i]['customer_id']=$result->customer_id;
                    $data[$result->customer_id][0][$i]['qdate']=$result->qrefdate;
                    $data[$result->customer_id][0][$i]['issue_date']=$result->issue_date;
                    $data[$result->customer_id][0][$i]['cover']=$result->cover;
                    $data[$result->customer_id][0][$i]['plid']=$result->plid;
                    $data[$result->customer_id][0][$i]['customerid']=$result->customer_id;
                    $data[$result->customer_id][0][$i]['description']=$result->description;
                    $data[$result->customer_id][0][$i]['covertype']=$result->cover;
                    if ($result->endorsementmode == 'Deduction') {
                        $data[$result->customer_id][0][$i]['premium']= -1 * abs($result->premium);
                        $data[$result->customer_id][0][$i]['received_premium']=-1 * abs($result->received_premium+$result->received_premium_to_broker);
                    } else {
                        $data[$result->customer_id][0][$i]['premium']=$result->premium;
                        $data[$result->customer_id][0][$i]['received_premium']=$result->received_premium+$result->received_premium_to_broker;
                    }
                    $data[$result->customer_id][0][$i]['policy_no']=$result->policy_no;
                    $data[$result->customer_id][0][$i]['currencycode']=$result->currencycode;
                    $data[$result->customer_id][0][$i]['cover_id']=$result->cover_id;
                    $data[$result->customer_id][0][$i]['policy_fees']=$result->policy_fees;
                    $data[$result->customer_id][0][$i]['insurance_company_id']=0;
                    $data[$result->customer_id][0][$i]['company_name']=$result->company_name;
                    $data[$result->customer_id][0][$i]['paid_to']=$broker;

                } else {
                    $data[$result->customer_id][$result->insurance_company_id][$i]['qtnref']=$result->qref;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['customer_id']=$result->customer_id;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['qdate']=$result->qrefdate;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['issue_date']=$result->issue_date;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['cover']=$result->cover;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['plid']=$result->plid;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['customerid']=$result->customer_id;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['description']=$result->description;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['covertype']=$result->cover;
                    if ($result->endorsementmode == 'Deduction') {
                        $data[$result->customer_id][$result->insurance_company_id][$i]['premium']= -1 * abs($result->premium);
                        $data[$result->customer_id][$result->insurance_company_id][$i]['received_premium']=-1 * abs($result->received_premium+$result->received_premium_to_broker);
                    } else {
                        $data[$result->customer_id][$result->insurance_company_id][$i]['premium']=$result->premium;
                        $data[$result->customer_id][$result->insurance_company_id][$i]['received_premium']=$result->received_premium+$result->received_premium_to_broker;
                    }
                    $data[$result->customer_id][$result->insurance_company_id][$i]['policy_no']=$result->policy_no;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['currencycode']=$result->currencycode;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['cover_id']=$result->cover_id;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['policy_fees']=$result->policy_fees;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['insurance_company_id']=$result->insurance_company_id;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['company_name']=$result->company_name;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['paid_to']=$result->company_name;

                }

                $i++;
            }
        }
        return $data;
    }
     
     function generatereportqcbsummary($fromdate,$todate,$customer_id,$payment_status, $payment_mode)
     {
        $data = array();
        
        if($fromdate != "" && $todate != "")
        {
            $fromdate = $this->mastermodel->convdatformat($fromdate);
            $todate = $this->mastermodel->convdatformat($todate);
        }
        $sql="select * from placing_slip_refs pr where type = 'issue' OR type= 'endorsement' "
                . "group by type,pslip_id order by pslip_id desc";
        $res=$this->db->query($sql);
        $i=0;
        $broker = $this->mastermodel->getSingleFieldValue('mycompany','company','id','1');
       
        foreach ($res->result_array()  as $restype)
        {
            $trtype=$restype['type'];
            $qid=$restype['pslip_id'];
            $flag = false;
            if ($payment_mode == 'all') {
                $flag = true;
            } else if ($payment_mode == 'insurance_company') {
                
                if ($trtype == 'issue') {
                    if ($this->mastermodel->getSingleFieldValue('placing_slip_premiumdetails','payment_method','placing_slip_id',$qid) == 'insurance_company') {
                        $flag = true;
                    }
                } else if ($trtype == 'endorsement') {
                    if ($this->mastermodel->getSingleFieldValue('endorsement','paymentoptions','id',$qid) == 'insurance_company') {
                        $flag = true;
                    }
                }
            } else {
                if ($trtype == 'issue') {
                    if ($this->mastermodel->getSingleFieldValue('placing_slip_premiumdetails','payment_method','placing_slip_id',$qid) != 'insurance_company') {
                        $flag = true;
                    }
                } else if ($trtype == 'endorsement') {
                    if ($this->mastermodel->getSingleFieldValue('endorsement','paymentoptions','id',$qid) != 'insurance_company') {
                        $flag = true;
                    }
                }
            }
            
            if ($flag) {
                if($trtype=='issue')
                {
                    $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,c.cover,p.premium,plc.customer_id as customer_id,plc.cover_id,p.fromdate as qrefdate,p.insurance_company_id,pn.policyno as policy_no,
                        p.brokerage,p.ispercentage,p.fromdate,p.todate,company_name,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref as qref,p.policy_fees, 
                        plc.remarks as description ,'' as endorsementmode, p.payment_method as payment_method,
                        IFNULL(SUM(customer_receipt_allocation.cr_allocation_amount),0) as received_premium,
                        IFNULL(SUM(cust_allocate.amount),0) as received_premium_to_broker, p.sum_insured as sum_insured
                        from placing_slip plc
                        join placing_slip_premiumdetails p on p.placing_slip_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                        join covers c on c.id=plc.cover_id  
                        INNER JOIN insurance_company  ON insurance_company.id = insurance_company_id
                        LEFT JOIN customer_receipt_allocation on cr_allocation_pid=customer_receipt_allocation.id AND cr_allocation_type = 'issue' 
                        LEFT JOIN cust_allocate on cust_allocate.pid=plc.id AND cust_allocate.type = 'issue' ";
                }
                else if($trtype=='endorsement')
                {

                    $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,plc.endrefno as qref,plc.endpolicyno as policy_no,plc.enddate as qrefdate,plc.premiumchange as premium,plc.policyfeeschange as policy_fees,covers.cover,
                        p.insurance_company_id,company_name,pn.policyno,plc.brokerage,plc.ispercentage,plc.endorsement_start_date as fromdate,plc.endorsement_end_date as todate,plc.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id as customer_id,pl.quotation_ref,
                        plc.remarks as description,plc.mode as endorsementmode, plc.paymentoptions as payment_method, 
                        IFNULL(SUM(customer_receipt_allocation.cr_allocation_amount),0) as received_premium,
                        IFNULL(SUM(cust_allocate.amount),0) as received_premium_to_broker, plc.sum_insured as sum_insured
                        from placing_slip pl left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on
                        pn.quotation_ref=pl.quotation_ref join endorsement plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id  
                        INNER JOIN insurance_company  ON insurance_company.id = insurance_company_id
                        LEFT JOIN customer_receipt_allocation on cr_allocation_pid=plc.id AND cr_allocation_type = 'endorsement'
                        LEFT JOIN cust_allocate on cust_allocate.pid=plc.id AND cust_allocate.type = 'endorsement'";
                }
                $sql=$sql."where  plc.id='$qid'";
                
                if ($customer_id != '') {
                    $sql .= " and customer_id='$customer_id'";
                }
                
                $sql.=" GROUP BY plc.id";
                if($fromdate != "" && $todate != "")
                {
                    $sql=$sql." having qrefdate between '$fromdate' and '$todate' ";
                }
                $sql=$sql." order by qrefdate asc ";
                $res1=$this->db->query($sql);
                $result=$res1->row();

                if (!isset($result->qref))
                {
                    continue;
                }
                $period = '';
                $fromdate = $this->mastermodel->convertdateformat($result->fromdate);
                $todate = $this->mastermodel->convertdateformat($result->todate);
                if (!empty($fromdate) && !empty($todate)) {
                    $period = $this->mastermodel->convdatformat($result->fromdate).' to '.$this->mastermodel->convdatformat($result->todate);
                }
                if ($result->ispercentage) {
                    $brokerageamount = round($result->brokerage/100*$result->premium,2);
                    $percentage = $result->brokerage;
                } else {
                    $percentage = $result->brokerage/$result->premium*100;
                    $brokerageamount = $result->brokerage;
                }
                // for payment to Savoy Insurance broker
                if ($result->payment_method != 'insurance_company') {
                    $data[$result->customer_id][0][$i]['qtnref']=$result->qref;
                    $data[$result->customer_id][0][$i]['customer_id']=$result->customer_id;
                    $data[$result->customer_id][0][$i]['qdate']=$result->qrefdate;
                    $data[$result->customer_id][0][$i]['cover']=$result->cover;
                    $data[$result->customer_id][0][$i]['plid']=$result->plid;
                    $data[$result->customer_id][0][$i]['customerid']=$result->customer_id;
                    $data[$result->customer_id][0][$i]['description']=$result->description;
                    $data[$result->customer_id][0][$i]['covertype']=$result->cover;
                    if ($result->endorsementmode == 'Deduction') {
                        $data[$result->customer_id][0][$i]['premium']= -1 * abs($result->premium);
                        $data[$result->customer_id][0][$i]['received_premium']=-1 * abs($result->received_premium+$result->received_premium_to_broker);
                        $data[$result->customer_id][0][$i]['brokerage']= -1 * abs($brokerageamount);
                    } else {
                        $data[$result->customer_id][0][$i]['premium']=$result->premium;
                        $data[$result->customer_id][0][$i]['received_premium']=$result->received_premium+$result->received_premium_to_broker;
                        $data[$result->customer_id][0][$i]['brokerage']=$brokerageamount;
                    }
                    $data[$result->customer_id][0][$i]['brokerage_percentage']=$percentage;
                    $data[$result->customer_id][0][$i]['period']=$period;
                    $data[$result->customer_id][0][$i]['policy_no']=$result->policy_no;
                    $data[$result->customer_id][0][$i]['cover_id']=$result->cover_id;
                    $data[$result->customer_id][0][$i]['policy_fees']=$result->policy_fees;
                    $data[$result->customer_id][0][$i]['insurance_company_id']=0;
                    $data[$result->customer_id][0][$i]['company_name']=$result->company_name;
                    $data[$result->customer_id][0][$i]['paid_to']=$broker;
                    $data[$result->customer_id][0][$i]['sum_insured']=$result->sum_insured;

                } else {
                    $data[$result->customer_id][$result->insurance_company_id][$i]['qtnref']=$result->qref;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['customer_id']=$result->customer_id;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['qdate']=$result->qrefdate;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['cover']=$result->cover;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['plid']=$result->plid;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['customerid']=$result->customer_id;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['description']=$result->description;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['covertype']=$result->cover;
                    if ($result->endorsementmode == 'Deduction') {
                        $data[$result->customer_id][$result->insurance_company_id][$i]['premium']= -1 * abs($result->premium);
                        $data[$result->customer_id][$result->insurance_company_id][$i]['received_premium']=-1 * abs($result->received_premium+$result->received_premium_to_broker);
                        $data[$result->customer_id][$result->insurance_company_id][$i]['brokerage']= -1 * abs($brokerageamount);
                    } else {
                        $data[$result->customer_id][$result->insurance_company_id][$i]['premium']=$result->premium;
                        $data[$result->customer_id][$result->insurance_company_id][$i]['received_premium']=$result->received_premium+$result->received_premium_to_broker;
                        $data[$result->customer_id][$result->insurance_company_id][$i]['brokerage']=$brokerageamount;
                    }
                    
                    $data[$result->customer_id][$result->insurance_company_id][$i]['brokerage_percentage']=$percentage;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['period']=$period;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['policy_no']=$result->policy_no;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['cover_id']=$result->cover_id;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['policy_fees']=$result->policy_fees;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['insurance_company_id']=$result->insurance_company_id;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['company_name']=$result->company_name;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['paid_to']=$result->company_name;
                    $data[$result->customer_id][$result->insurance_company_id][$i]['sum_insured']=$result->sum_insured;

                }

                $i++;
            }
        }
        return $data;
    }
     
     function generatereportcoversummary($year)
     {
        $data['brokerage'] = array();
        $data['premium'] = array();
        $fromdate = $year . '-01-01';
        $todate = $year . '-12-31';
        $sql="select * from placing_slip_refs pr where type = 'issue' OR type= 'endorsement' "
                . "group by type,pslip_id";
        $res=$this->db->query($sql);
        
        foreach ($res->result_array()  as $restype)
        {
            $trtype=$restype['type'];
            $qid=$restype['pslip_id'];
            
            if($trtype=='issue')
            {
                $sql="SELECT IFNULL((select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,c.cover,p.premium,p.fromdate as qrefdate,
                    p.brokerage,p.ispercentage,p.ispercentage,plc.quotation_ref as qref,p.policy_fees, 
                    '' as endorsementmode
                    from placing_slip plc
                    join placing_slip_premiumdetails p on p.placing_slip_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                    join covers c on c.id=plc.cover_id";
            }
            else if($trtype=='endorsement')
            {

                $sql="SELECT IFNULL((select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,plc.endrefno as qref,plc.endpolicyno as policy_no,plc.enddate as qrefdate,plc.premiumchange as premium,plc.policyfeeschange as policy_fees,covers.cover,
                    p.insurance_company_id,pn.policyno,plc.brokerage,plc.ispercentage,plc.ispercentage,pl.quotation_ref,plc.mode as endorsementmode
                    from placing_slip pl 
                    left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id 
                    left join policynote pn on pn.quotation_ref=pl.quotation_ref 
                    join endorsement plc on plc.policyid=pn.id 
                    join covers on covers.id=pl.cover_id";
            }
            $sql=$sql." where  plc.id='$qid'";


            $sql.=" GROUP BY plc.id";
            if($fromdate != "" && $todate != "")
            {
                $sql=$sql." having qrefdate between '$fromdate' and '$todate' ";
            }
            $res1=$this->db->query($sql);
            $result=$res1->row();

            if (!isset($result->qref))
            {
                continue;
            }
            if ($result->ispercentage) {
                $brokerageamount = round($result->brokerage/100*$result->premium,2);
                $percentage = $result->brokerage;
            } else {
                $percentage = $result->brokerage/$result->premium*100;
                $brokerageamount = $result->brokerage;
            }
            $month = date("F", strtotime($result->qrefdate));    
            if (!isset($data['brokerage'][$result->cover][$month])) {
                $data['brokerage'][$result->cover][$month] = 0;
            }
            if (!isset($data['premium'][$result->cover][$month])) {
                $data['premium'][$result->cover][$month] = 0;
            }

            if ($result->endorsementmode == 'Deduction') {
                $data['premium'][$result->cover][$month] += -1 * abs($result->premium);
                $data['brokerage'][$result->cover][$month] += -1 * abs($brokerageamount);
            } else {
                $data['premium'][$result->cover][$month] += $result->premium;
                $data['brokerage'][$result->cover][$month] += $brokerageamount;
            }
        }
        return $data;
    }
    
     
    function generatereportinsurancecosummary($year)
    {
        $data['brokerage'] = array();
        $data['premium'] = array();
        $fromdate = $year . '-01-01';
        $todate = $year . '-12-31';
        $sql="select * from placing_slip_refs pr where type = 'issue' OR type= 'endorsement' "
                . "group by type,pslip_id";
        $res=$this->db->query($sql);
        
        foreach ($res->result_array()  as $restype)
        {
            $trtype=$restype['type'];
            $qid=$restype['pslip_id'];
            
            if($trtype=='issue')
            {
                $sql="SELECT IFNULL((select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,p.premium,plc.customer_id as customer_id,p.fromdate as qrefdate,
                    p.brokerage,p.ispercentage,company_name,p.ispercentage,plc.id as plid,plc.quotation_ref as qref,p.policy_fees, 
                    '' as endorsementmode
                    from placing_slip plc
                    join placing_slip_premiumdetails p on p.placing_slip_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                    INNER JOIN insurance_company  ON insurance_company.id = insurance_company_id";
            }
            else if($trtype=='endorsement')
            {

                $sql="SELECT IFNULL((select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,plc.endrefno as qref,plc.enddate as qrefdate,plc.premiumchange as premium,plc.policyfeeschange as policy_fees,
                    company_name,plc.brokerage,plc.ispercentage,plc.ispercentage,pl.id as plid,pl.quotation_ref,
                    plc.remarks as description,plc.mode as endorsementmode
                    from placing_slip pl left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id 
                    left join policynote pn on pn.quotation_ref=pl.quotation_ref 
                    join endorsement plc on plc.policyid=pn.id 
                    INNER JOIN insurance_company  ON insurance_company.id = insurance_company_id";
            }
            $sql=$sql." where  plc.id='$qid'";


            $sql.=" GROUP BY plc.id";
            if($fromdate != "" && $todate != "")
            {
                $sql=$sql." having qrefdate between '$fromdate' and '$todate' ";
            }
            $res1=$this->db->query($sql);
            $result=$res1->row();

            if (!isset($result->qref))
            {
                continue;
            }
            if ($result->ispercentage) {
                $brokerageamount = round($result->brokerage/100*$result->premium,2);
                $percentage = $result->brokerage;
            } else {
                $percentage = $result->brokerage/$result->premium*100;
                $brokerageamount = $result->brokerage;
            }
            $month = date("F", strtotime($result->qrefdate));    
            if (!isset($data['brokerage'][$result->company_name][$month])) {
                $data['brokerage'][$result->company_name][$month] = 0;
            }
            if (!isset($data['premium'][$result->company_name][$month])) {
                $data['premium'][$result->company_name][$month] = 0;
            }

            if ($result->endorsementmode == 'Deduction') {
                $data['premium'][$result->company_name][$month] += -1 * abs($result->premium);
                $data['brokerage'][$result->company_name][$month] += -1 * abs($brokerageamount);
            } else {
                $data['premium'][$result->company_name][$month] += $result->premium;
                $data['brokerage'][$result->company_name][$month] += $brokerageamount;
            }
        }
        return $data;
    }
    
    
    
    function generatereportcustomersummary($fromdate,$todate)
    {
        $data = array();
        $data['insurance_companies'] = array();
        $data['customers'] = array();
        if($fromdate != "" && $todate != "")
        {
            $fromdate = $this->mastermodel->convdatformat($fromdate);
            $todate = $this->mastermodel->convdatformat($todate);
        }
        
        // for broker balance
        $sub_query = '';
        $sub_query1 = '';
        if($fromdate != "" && $todate != "")
        {
            $sub_query = "between '$fromdate' and '$todate' ";
            $sub_query1 = "where tran_date between '$fromdate' and '$todate'";
        }
        $qry = "SELECT customers.id,
            SUM(if(ct.tran_date $sub_query,ct.amount,0)) as balance
            FROM
            (select sum(amount) as amount,type_no,tran_date,account,memo,method,id from gl_trans $sub_query1 group by type_no,account) as ct   
            inner join customers ON (customers.code = ct.account)
            group by code having balance > 0.5";
        $res = $this->db->query($qry);
        $broker_balances = $res->result_array();
       
        // for insurance company balance
//        $sql="select * from placing_slip_refs pr where type = 'issue' OR type= 'endorsement' "
//                . "group by type,pslip_id order by pslip_id desc";
//        $res=$this->db->query($sql);
//     
//        foreach ($res->result_array()  as $restype)
//        {
//            $trtype=$restype['type'];
//            $qid=$restype['pslip_id'];
//
//            if ($trtype=='issue')
//            {
                $sql ="SELECT p.premium
                        ,plc.customer_id as customer_id
                        ,p.fromdate as qrefdate
                        ,p.insurance_company_id
                        ,pn.policyno as policy_no
                        , SUM(customer_receipt_allocation.cr_allocation_amount) as received_premium
                        ,p.brokerage
                        ,company_name
                        ,p.ispercentage
                        ,plc.id as plid
                        ,plc.quotation_ref as qref
                        ,p.policy_fees,
                        plc.remarks as description
                        , p.payment_method
                        ,'' as endorsementmode
                    from placing_slip plc
                    join placing_slip_premiumdetails p on p.placing_slip_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                    join covers c on c.id=plc.cover_id  INNER JOIN country ct
                    ON plc.currencytype = ct.id  
                    INNER JOIN insurance_company  ON insurance_company.id = insurance_company_id
                    LEFT JOIN customer_receipt_allocation on cr_allocation_pid=plc.id AND cr_allocation_type = 'issue'";
                
                if($fromdate != "" && $todate != "")
                {
                    $sql=$sql." where p.fromdate >= '$fromdate' and p.fromdate <= '$todate' ";
                }
                $sql .=" GROUP BY plc.id";

//            }
//            else if($trtype=='endorsement')
//            {

                $sql .= " UNION SELECT plc.premiumchange as premium,
                    pl.customer_id as customer_id,
                    plc.enddate as qrefdate,
                    p.insurance_company_id,
                    plc.endpolicyno as policy_no,
                    SUM(customer_receipt_allocation.cr_allocation_amount) as received_premium,
                    plc.brokerage,
                    company_name,
                    plc.ispercentage,
                    pl.id as plid,
                    plc.endrefno as qref,
                    plc.policyfeeschange as policy_fees,
                    plc.remarks as description,
                    plc.paymentoptions as payment_method ,
                    plc.mode as endorsementmode
                     
                    from placing_slip pl left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on
                    pn.quotation_ref=pl.quotation_ref join endorsement plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id  
                    INNER JOIN country ct
                    ON pl.currencytype = ct.id 
                    INNER JOIN insurance_company  ON insurance_company.id = insurance_company_id
                    LEFT JOIN customer_receipt_allocation on cr_allocation_pid=plc.id AND cr_allocation_type = 'endorsement'";
//            }
//            $sql=$sql." where  plc.id='$qid'";


            if($fromdate != "" && $todate != "")
                {
                    $sql=$sql." where plc.enddate >= '$fromdate' and plc.enddate <= '$todate' ";
                }
                $sql .=" GROUP BY plc.id";
                
//                echo $sql;die();
         //   $sql=$sql." order by qrefdate asc ";

            $res1=$this->db->query($sql);
//     
            foreach ($res1->result_array()  as $result) {
                if (!isset($result['qref']))
                {
                    continue;
                }
                if ($result['payment_method'] !== 'insurance_company') {
                    continue;
                }

                if ($result['endorsementmode'] == 'Deduction') {
                    $premium= -1 * abs($result['premium']);
                    $received_premium =-1 * abs($result['received_premium']);
                } else {
                    $premium=$result['premium'];
                    $received_premium=$result['received_premium'];
                }

                if (!isset($data['customers'][$result['customer_id']]['total_balance'])) {
                    $data['customers'][$result['customer_id']]['total_balance'] = 0;
                }
                if (!isset($data['customers'][$result['customer_id']]['broker_balance'])) {

                    // check if customer is there in broker transactons
                    if (array_search($result['customer_id'], $this->array_column($broker_balances, 'id')) !== false) {
                        $key = array_search($result['customer_id'], $this->array_column($broker_balances, 'id'));

                        $data['customers'][$result['customer_id']]['broker_balance'] = $broker_balances[$key]['balance'];
                        $data['customers'][$result['customer_id']]['total_balance'] += $broker_balances[$key]['balance'];
                    }
                }

                if (!isset($data['customers'][$result['customer_id']]['insurace_companies_balance'][$result['insurance_company_id']])) {
                    $data['customers'][$result['customer_id']]['insurace_companies_balance'][$result['insurance_company_id']] = 0;
                }
                if (!in_array($result['insurance_company_id'], $data['insurance_companies'])) {
                    $data['insurance_companies'][] = $result['insurance_company_id'];
                }
                $data['customers'][$result['customer_id']]['total_balance'] += $premium + $result['policy_fees'] - $received_premium;
                $data['customers'][$result['customer_id']]['insurace_companies_balance'][$result['insurance_company_id']] += $premium + $result['policy_fees'] - $received_premium;

            }

            
        // add all remaining broker balances to data
        foreach ($broker_balances as $balance) {
            
            if (isset($data['customers'][$balance['id']])) {
                continue;
            } else {
                $data['customers'][$balance['id']]['total_balance'] = $balance['balance'];
                $data['customers'][$balance['id']]['broker_balance'] = $balance['balance'];
            }
        }
        
        return $data;
    }
    
    
    function genrepcustomerreceipt($fromdate, $todate, $payment_method)
    {
//        $qry = "SELECT customer_receipt.customer_id, customer_receipt_amount, customer_receipt_date, transaction_code, customer_payment_method,
//            cr_allocation_amount, endpolicyno, endrefno, policynote.policyno, policynote.quotation_ref FROM customer_receipt 
//            join customer_receipt_allocation on customer_receipt_allocation.customer_receipt_id = customer_receipt.customer_receipt_id
//            left join placing_slip on  cr_allocation_pid = placing_slip.id and cr_allocation_type = 'issue'
//            left join endorsement on  cr_allocation_pid = endorsement.id and cr_allocation_type = 'endorsement'
//            left join policynote on policynote.quotation_ref = placing_slip.quotation_ref";
        $data = array();
        if ($fromdate != "" && $todate != "")
        {
            $fromdate = $this->mastermodel->convdatformat($fromdate);
            $todate = $this->mastermodel->convdatformat($todate);
        }
        
        $qry = "SELECT customer_receipt_amount, customer_receipt_date, transaction_code, customer_payment_method,
            customer_corp_info.insured as individual_name, customer_personal_ind.insured as corporate_name FROM customer_receipt 
            join customers on customers.id = customer_receipt.customer_id
            left join customer_personal_ind on  customer_personal_ind.customer_id = customers.id and customers.type = 'Individual'
            left join customer_corp_info on  customer_corp_info.customer_id = customers.id and customers.type = 'Corporate'";
        if ($fromdate != "" && $todate != "")
        {
            $qry .= " where customer_receipt_date between '$fromdate' and '$todate'";
        }
        if (!empty($payment_method))
        {
            $qry .= " and customer_payment_method = '$payment_method'";
        }
        
        $qry .= " UNION SELECT bank_trans.amount as customer_receipt_amount, bank_trans.trans_date as customer_receipt_date, bank_trans.ref as transaction_code, unposted_pay_dep.method as customer_payment_method,
            customer_corp_info.insured as individual_name, customer_personal_ind.insured as corporate_name FROM customer_trans 
            join bank_trans on bank_trans.trans_no = customer_trans.type_no
            join unposted_pay_dep on unposted_pay_dep.reference = customer_trans.type_no
            join customers on customers.code = customer_trans.account
            left join customer_personal_ind on  customer_personal_ind.customer_id = customers.id and customers.type = 'Individual'
            left join customer_corp_info on  customer_corp_info.customer_id = customers.id and customers.type = 'Corporate'";
        if ($fromdate != "" && $todate != "")
        {
            $qry .= " where customer_trans.tran_date between '$fromdate' and '$todate'";
        }
        if (!empty($payment_method))
        {
            $qry .= " and unposted_pay_dep.method = '$payment_method'";
        }
        $qry .= " order by customer_receipt_date";
        
        $res=$this->db->query($qry);
     
        return $res->result_array();
    }
    
    
    
    
    function generatereportageingsummary()
    { 

        $data = array();
        $data['dates'] = array('30_days', '60_days', '90_days', '120_days', '180_days', '365_days', '>365_days');
        
        $data['customers'] = array();
        $today = date("Y-m-d");
        $days_30 = date('Y-m-d', strtotime('-30 days', strtotime($today))); 
        $days_60 = date('Y-m-d', strtotime('-60 days', strtotime($today))); 
        $days_90 = date('Y-m-d', strtotime('-90 days', strtotime($today))); 
        $days_120 = date('Y-m-d', strtotime('-120 days', strtotime($today))); 
        $days_180 = date('Y-m-d', strtotime('-180 days', strtotime($today))); 
        $days_365 = date('Y-m-d', strtotime('-365 days', strtotime($today))); 
        $date = $today;

//        $mixedqry = "sum(if(ct.amount<0,ct.amount+alloc,ct.amount-alloc),0)) as total_balance,
//                    sum(if(DATEDIFF('$date',ct.tran_date) > '0' AND DATEDIFF('$date',ct.tran_date)  <='30' , if(ct.amount<0,ct.amount+alloc,ct.amount-alloc),0)) as amount30,
//                    sum(if(DATEDIFF('$date',ct.tran_date) > '30' AND DATEDIFF('$date',ct.tran_date)  <='60' , if(ct.amount<0,ct.amount+alloc,ct.amount-alloc),0)) as amount60,
//                    sum(if(DATEDIFF('$date',ct.tran_date) > '60' AND DATEDIFF('$date',ct.tran_date)  <='90' , if(ct.amount<0,ct.amount+alloc,ct.amount-alloc),0)) as amount90,
//                    sum(if(DATEDIFF('$date',ct.tran_date) > '90' AND DATEDIFF('$date',ct.tran_date)  <='120' , if(ct.amount<0,ct.amount+alloc,ct.amount-alloc),0)) as amount120,
//                    sum(if(DATEDIFF('$date',ct.tran_date) > '120' AND DATEDIFF('$date',ct.tran_date)  <='180' , if(ct.amount<0,ct.amount+alloc,ct.amount-alloc),0)) as amount180,
//                    sum(if(DATEDIFF('$date',ct.tran_date) > '180' AND DATEDIFF('$date',ct.tran_date)  <='365' , if(ct.amount<0,ct.amount+alloc,ct.amount-alloc),0)) as amount365,
//                    sum(if(DATEDIFF('$date',ct.tran_date) > '365' , if(ct.amount<0,ct.amount+alloc,ct.amount-alloc),0)) as amount365greater";
//
//        $qry = "select customers.id,
//            $mixedqry
//            FROM
//            (select sum(amount) as amount,type_no,tran_date,account,memo,method,id from gl_trans group by type_no,account) as ct   
//            inner join customers ON (customers.code = ct.account)
//            inner join refs on refs.id=ct.type_no
//            left JOIN  (select sum(amount) as amount,type_no,tran_date,account,memo,alloc,pid,id from customer_trans group by type_no,account) as g on ct.type_no=g.type_no and ct.account=g.account                     
//            group by code having total_balance != 0";
//        
//        $broker_balances = array();
        
        $qry = "SELECT customers.id,
            SUM(ct.amount) as balance
            FROM
            (select sum(amount) as amount,tran_date,account from gl_trans  group by type_no,account) as ct   
            inner join customers ON (customers.code = ct.account)
            group by code having balance > 0.5";
        
        $res = $this->db->query($qry);
        $broker_balances['total_balance'] = $res->result_array();
        
        $sub_query = "ct.tran_date >= '$days_30'";
        $sub_query1 = "where tran_date >= '$days_30'";        
        $qry = "SELECT customers.id,
            SUM(if($sub_query,ct.amount,0)) as balance
            FROM
            (select sum(amount) as amount,type_no,tran_date,account,memo,method,id from gl_trans $sub_query1 group by type_no,account) as ct   
            inner join customers ON (customers.code = ct.account)
            group by code having balance > 0.5";
        $res = $this->db->query($qry);
        $broker_balances['30_days'] = $res->result_array();
        
        $sub_query = "ct.tran_date < '$days_30' and ct.tran_date >= '$days_60'";
        $sub_query1 = "where tran_date < '$days_30' and tran_date >= '$days_60'";        
        $qry = "SELECT customers.id,
            SUM(if($sub_query,ct.amount,0)) as balance
            FROM
            (select sum(amount) as amount,type_no,tran_date,account,memo,method,id from gl_trans $sub_query1 group by type_no,account) as ct   
            inner join customers ON (customers.code = ct.account)
            group by code having balance > 0.5";
        $res = $this->db->query($qry);
        $broker_balances['60_days'] = $res->result_array();
        
        $sub_query = "ct.tran_date < '$days_60' and ct.tran_date >= '$days_90'";
        $sub_query1 = "where tran_date < '$days_60' and tran_date >= '$days_90'";        
        $qry = "SELECT customers.id,
            SUM(if($sub_query,ct.amount,0)) as balance
            FROM
            (select sum(amount) as amount,type_no,tran_date,account,memo,method,id from gl_trans $sub_query1 group by type_no,account) as ct   
            inner join customers ON (customers.code = ct.account)
            group by code having balance > 0.5";
        $res = $this->db->query($qry);
        $broker_balances['90_days'] = $res->result_array();
        
        $sub_query = "ct.tran_date < '$days_90' and ct.tran_date >= '$days_120'";
        $sub_query1 = "where tran_date < '$days_90' and tran_date >= '$days_120'";        
        $qry = "SELECT customers.id,
            SUM(if($sub_query,ct.amount,0)) as balance
            FROM
            (select sum(amount) as amount,type_no,tran_date,account,memo,method,id from gl_trans $sub_query1 group by type_no,account) as ct   
            inner join customers ON (customers.code = ct.account)
            group by code having balance > 0.5";

        $res = $this->db->query($qry);
        $broker_balances['120_days'] = $res->result_array();
        
        $sub_query = "ct.tran_date < '$days_120' and ct.tran_date >= '$days_180'";
        $sub_query1 = "where tran_date < '$days_120' and tran_date >= '$days_180'";        
        $qry = "SELECT customers.id,
            SUM(if($sub_query,ct.amount,0)) as balance
            FROM
            (select sum(amount) as amount,type_no,tran_date,account,memo,method,id from gl_trans $sub_query1 group by type_no,account) as ct   
            inner join customers ON (customers.code = ct.account)
            group by code having balance > 0.5";
        $res = $this->db->query($qry);
        $broker_balances['180_days'] = $res->result_array();
        
        $sub_query = "ct.tran_date < '$days_180' and ct.tran_date >= '$days_365'";
        $sub_query1 = "where tran_date < '$days_180' and tran_date >= '$days_365'";        
        $qry = "SELECT customers.id,
            SUM(if($sub_query,ct.amount,0)) as balance
            FROM
            (select sum(amount) as amount,type_no,tran_date,account,memo,method,id from gl_trans $sub_query1 group by type_no,account) as ct   
            inner join customers ON (customers.code = ct.account)
            group by code having balance > 0.5";
        $res = $this->db->query($qry);
        $broker_balances['365_days'] = $res->result_array();
        
        $sub_query = "ct.tran_date < '$days_365'";
        $sub_query1 = "where tran_date < '$days_365'";        
        $qry = "SELECT customers.id,
            SUM(if($sub_query,ct.amount,0)) as balance
            FROM
            (select sum(amount) as amount,type_no,tran_date,account,memo,method,id from gl_trans $sub_query1 group by type_no,account) as ct   
            inner join customers ON (customers.code = ct.account)
            group by code having balance > 0.5";
        $res = $this->db->query($qry);
        $broker_balances['>365_days'] = $res->result_array();
        
        $sql="select pslip_id, type from placing_slip_refs pr where type = 'issue' "
                . "OR type= 'endorsement' group by type,pslip_id";
        $Q = $this->db->query($sql);
        $limit = $Q->num_rows();
        $offset = 0;
        while ($offset <= $limit) {
            $sql="select pslip_id, type from placing_slip_refs pr where type = 'issue' "
                . "OR type= 'endorsement' group by type,pslip_id limit 100 offset $offset";
            
            $res=$this->db->query($sql);

            foreach ($res->result_array() as $restype)
            {
                $trtype=$restype['type'];
                $qid=$restype['pslip_id'];

                if ($trtype=='issue')
                {
                    $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,p.premium,plc.customer_id as customer_id,p.fromdate as qrefdate,
                        SUM(customer_receipt_allocation.cr_allocation_amount) as received_premium,p.brokerage,p.ispercentage,plc.quotation_ref as qref,p.policy_fees, 
                        '' as endorsementmode, p.payment_method from placing_slip plc
                        join placing_slip_premiumdetails p on p.placing_slip_id=plc.id 
                        LEFT JOIN customer_receipt_allocation on cr_allocation_pid=plc.id AND cr_allocation_type = 'issue'";

                } else if($trtype=='endorsement') {

                    $sql="SELECT IFNULL((select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,plc.enddate as qrefdate,plc.premiumchange as premium,plc.policyfeeschange as policy_fees,
                        SUM(customer_receipt_allocation.cr_allocation_amount) as received_premium,plc.endrefno as qref,plc.brokerage,plc.ispercentage,pl.customer_id as customer_id,
                        plc.mode as endorsementmode, plc.paymentoptions as payment_method from placing_slip pl
                        left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id 
                        join policynote pn on pn.quotation_ref=pl.quotation_ref 
                        join endorsement plc on plc.policyid=pn.id 
                        LEFT JOIN customer_receipt_allocation on cr_allocation_pid=plc.id AND cr_allocation_type = 'endorsement'";
                }
                $sql=$sql." where  plc.id='$qid'";

                $res1=$this->db->query($sql);
                $result=$res1->row();

                if (!isset($result->qref)) {
                    continue;
                }
                if ($result->payment_method !== 'insurance_company') {
                    continue;
                }

                if ($result->endorsementmode == 'Deduction') {
                    $premium= -1 * abs($result->premium);
                    $received_premium =-1 * abs($result->received_premium);
                } else {
                    $premium=$result->premium;
                    $received_premium=$result->received_premium;
                }

                if (!isset($data['customers'][$result->customer_id])) {
                    $data['customers'][$result->customer_id]['total_balance'] = 0;
                    $data['customers'][$result->customer_id]['30_days'] = 0;
                    $data['customers'][$result->customer_id]['60_days'] = 0;
                    $data['customers'][$result->customer_id]['90_days'] = 0;
                    $data['customers'][$result->customer_id]['120_days'] = 0;
                    $data['customers'][$result->customer_id]['180_days'] = 0;
                    $data['customers'][$result->customer_id]['365_days'] = 0;
                    $data['customers'][$result->customer_id]['>365_days'] = 0;
                }

                $balance_amount = $premium + $result->policy_fees - $received_premium;
                $data['customers'][$result->customer_id]['total_balance'] += $balance_amount;
                if ($result->qrefdate >= $days_30) {
                    $data['customers'][$result->customer_id]['30_days'] += $balance_amount;
                } elseif ($result->qrefdate < $days_30 && $result->qrefdate >= $days_60) {
                    $data['customers'][$result->customer_id]['60_days'] += $balance_amount;
                } elseif ($result->qrefdate < $days_60 && $result->qrefdate >= $days_90) {
                    $data['customers'][$result->customer_id]['90_days'] += $balance_amount;
                } elseif ($result->qrefdate < $days_90 && $result->qrefdate >= $days_120) {
                    $data['customers'][$result->customer_id]['120_days'] += $balance_amount;
                } elseif ($result->qrefdate < $days_120 && $result->qrefdate >= $days_180) {
                    $data['customers'][$result->customer_id]['180_days'] += $balance_amount;
                } elseif ($result->qrefdate < $days_180 && $result->qrefdate >= $days_365) {
                    $data['customers'][$result->customer_id]['365_days'] += $balance_amount;
                } elseif ($result->qrefdate < $days_365) {
                    $data['customers'][$result->customer_id]['>365_days'] += $balance_amount;
                }
            }
            unset($result, $sql, $res);
            $offset += 100;
        }
        $customers = array();
        foreach ($broker_balances as $key => $balance) {
            
            
            
            foreach ($balance as $balance) {
                // if a customer does not have a balancet then dont consider him at all
                if ($key === 'total_balance') {
                    $customers[] = $balance['id'];
                } else {
                    if (!in_array($balance['id'], $customers)) {
                        continue;
                    }
                }
                if (isset($data['customers'][$balance['id']][$key])) {
                    $data['customers'][$balance['id']][$key] += $balance['balance'];
                } else {
                    $data['customers'][$balance['id']][$key] = $balance['balance'];
                }
            }
        }
        return $data;
    }
    
     function generatereportcompanypayment($fromdate='',$todate='',$date_filter='',$customer_id='',$insurance_company_id='',$payment_status='', $payment_completed='')
     {
        $data = array();
        $sql = "select transaction_code, customer_id, customer_receipt_amount, customer_receipt_date, company_name,"
                . " insurance_company_payment_date, insurance_company_receipt_amount,"
                . " insurance_company_payment_completed, customer_receipt_remarks, insurance_company_payment_remarks,"
                . " IFNULL(customer_receipt_amount - insurance_company_receipt_amount, customer_receipt_amount) AS balance"
                . " from customer_receipt "
                . " inner join insurance_company on insurance_company.id = customer_receipt.insurance_company_id  where customer_receipt_id !=''";
        
        if($fromdate != "" && $todate != "")
        {
            $fromdate = $this->mastermodel->convdatformat($fromdate);
            $todate = $this->mastermodel->convdatformat($todate);
            $sql .= " and $date_filter between '$fromdate' and '$todate'";
        }
        if(!empty($customer_id))
        {
            $sql .= " and customer_id ='$customer_id'";
        }
        if(!empty($insurance_company_id))
        {
            $sql .= " and insurance_company_id ='$insurance_company_id'";
        }
        if(!empty($payment_completed))
        {
            $sql .= " and insurance_company_payment_completed ='$payment_completed'";
        }
        if($payment_status != "all")
        {
            if ($payment_status == 'completed') {
                $sql .= " having balance = '0'";
            } elseif ($payment_status == 'pending') {
                $sql .= " having balance != '0'";
            }
        }
        $Q=$this->db->query($sql);
        if ($Q->num_rows() > 0)
        {
             foreach ($Q->result_array() as $row)
             {
                 $data[] = $row;
             }
        }
        $Q->free_result();
        return $data;
     }

     function generatereportrenewinscomp($table,$fromdate,$todate,$inscompid,$cover_id,$branch_id)
     {
         $data = array();

         if($fromdate != "" && $todate != "")
                 {
         $fromdate = $this->mastermodel->convdatformat($fromdate);
         $todate = $this->mastermodel->convdatformat($todate);
                 }
        $sql="select * from placing_slip_refs pr  group by type,pslip_id ";

         $res=$this->db->query($sql);

         $i=0;
         foreach ($res->result_array()  as $restype)
         {
             $trtype=$restype['type'];
             $qid=$restype['pslip_id'];

                if($trtype=='issue')
                 {

                            $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,p.todate as qrefdate,p.insurance_company_id,pn.policyno as policy_no,
                      p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref as qref,plc.currencytype ,p.policy_fees,sc.staffid,sc.date as effectivedate from placing_slip plc
                      join placing_slip_premiumdetails p on p.placing_slip_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                      join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                      ON plc.customer_id = sc.customerid INNER JOIN country ct
                      ON plc.currencytype = ct.id  LEFT JOIN customer_account
                      ON (customer_account.customer_id = plc.customer_id)  ";



                 }
                else if($trtype=='endorsement')
                {

                    $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.endrefno as qref,plc.endpolicyno as policy_no,p.todate as qrefdate,plc.premiumchange as premium,plc.policyfeeschange as policy_fees,covers.cover,
                     p.insurance_company_id,pn.policyno,plc.brokerage,plc.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate
                     from placing_slip pl left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on
                     pn.quotation_ref=pl.quotation_ref join endorsement plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id  INNER JOIN staffcustomer sc
                     ON pl.customer_id = sc.customerid INNER JOIN country ct
                     ON pl.currencytype = ct.id LEFT JOIN customer_account
                      ON (customer_account.customer_id = pl.customer_id)  ";



                }
                else  if($trtype=='cancellation')
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.canpolicyno as policy_no,plc.canrefno as qref,p.todate as qrefdate,0-plc.premiumchange as premium,p.insurance_company_id,pn.policyno,covers.cover,'0' as policy_fees,
                  p.brokerage,p.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate  from placing_slip pl
                  left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on pn.quotation_ref=pl.quotation_ref
                  join cancellation plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id INNER JOIN staffcustomer sc
                  ON pl.customer_id = sc.customerid INNER JOIN country ct
                  ON pl.currencytype = ct.id LEFT JOIN customer_account
                  ON (customer_account.customer_id = pl.customer_id)  ";


                }

                else
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from declaration_stakeholders where declaration_stakeholders.declaration_id=p.declaration_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,plc.certificate_refno as qref,plc.certificate_no as policy_no, p.todate as qrefdate,p.insurance_company_id,pn.policyno,
                  p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref,plc.currencytype,p.policy_fees,sc.staffid,sc.date as effectivedate  from declaration plc
                  join declaration_premiumdetails p on p.declaration_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                  join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                  ON plc.customer_id = sc.customerid INNER JOIN country ct
                  ON plc.currencytype = ct.id  LEFT JOIN customer_account
                  ON (customer_account.customer_id = plc.customer_id)  ";





                }

                $sql=$sql."where  plc.id='$qid' and insurance_company_id='$inscompid'";


                 if($cover_id!="cover_id")
                 {
                     $sql=$sql." and cover_id = '$cover_id'";
                 }

                  if($branch_id!='branch_id')
                 {
                     $sql=$sql." and customer_account.branch_id = '$branch_id'";
                 }

                  if($fromdate != "" && $todate != "")
                 {


                     $sql=$sql." having qrefdate between '$fromdate' and '$todate' ";
                 }

                    $sql=$sql." order by effectivedate desc limit 1";

                // echo $sql.'<br><br><br>';


                 $res1=$this->db->query($sql);

                 $result=$res1->row();



                if(!isset($result->qref))
                {
                    continue;
                }
                $data[$i]['qtnref']=$result->qref;

                $data[$i]['qdate']=$result->qrefdate;
                $data[$i]['plid']=$result->plid;
                $data[$i]['customerid']=$result->customer_id;
                $data[$i]['covertype']=$result->cover;
                $data[$i]['premium']=$result->premium;
                $data[$i]['brokerage']=$result->brokerage;
                $data[$i]['policy_no']=$result->policy_no;
                $data[$i]['staffid']=$result->staffid;
                $data[$i]['currencycode']=$result->currencycode;
                $data[$i]['cover_id']=$result->cover_id;
                $data[$i]['policy_fees']=$result->policy_fees;
                $data[$i]['insurance_company_id']=$result->insurance_company_id;
                $data[$i]['percentage']=$result->percentage;
                $data[$i]['ispercentage']=$result->ispercentage;


                $i++;


         }
               // var_dump($data);

                return $data;

     }
     function generatereportpaymentclient($search,$fromdate,$todate,$cover_id)
     {

         $data = array();

         $sql = "select placing_slip.customer_id, placing_slip.quotation_ref, placing_slip.status,placing_slip.cover_id, placing_slip.quotation_date,  placing_slip_payamount.amount,  placing_slip_payamount.paydate from   placing_slip inner join placing_slip_payamount where placing_slip.id =  placing_slip_payamount.placing_slip_id and placing_slip.customer_id = '$search'";
        
         if($fromdate != "" && $todate != "")
              {
             $fromdate = $this->mastermodel->convdatformat($fromdate);
        $todate = $this->mastermodel->convdatformat($todate);

             $sql=$sql." and placing_slip_payamount.paydate between '$fromdate' and '$todate'";
              }
          if($cover_id != "")
              $sql=$sql." and placing_slip.cover_id = '$cover_id'";

         $Q=$this->db->query($sql);

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


     }
     function generatereportpaymentcover($search,$fromdate,$todate)
     {

         $data = array();

         $sql = "select placing_slip.customer_id, placing_slip.quotation_ref, placing_slip.status,placing_slip.cover_id, placing_slip.quotation_date,  placing_slip_payamount.amount,  placing_slip_payamount.paydate from   placing_slip inner join placing_slip_payamount where placing_slip.id =  placing_slip_payamount.placing_slip_id and placing_slip.cover_id = '$search'";

         if($fromdate != "" && $todate != "")
              {
             $fromdate = $this->mastermodel->convdatformat($fromdate);
        $todate = $this->mastermodel->convdatformat($todate);

             $sql=$sql." and placing_slip_payamount.paydate between '$fromdate' and '$todate'";
              }
       

         $Q=$this->db->query($sql);

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


     }
     function generatereportpaymentstf($search,$fromdate,$todate)
     {

         $data = array();

         $sql = "select placing_slip.customer_id, placing_slip.quotation_ref, placing_slip.status,placing_slip.cover_id, placing_slip.quotation_date,  placing_slip_payamount.amount,  placing_slip_payamount.paydate from   placing_slip inner join placing_slip_payamount inner join staffcustomer  on placing_slip.id =  placing_slip_payamount.placing_slip_id and placing_slip.customer_id = staffcustomer.customerid and staffcustomer.staffid = '$search'";

         if($fromdate != "" && $todate != "")
              {
             $fromdate = $this->mastermodel->convdatformat($fromdate);
        $todate = $this->mastermodel->convdatformat($todate);

             $sql=$sql." and placing_slip_payamount.paydate between '$fromdate' and '$todate'";
              }

         $Q=$this->db->query($sql);

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


     }
     function generatereportpaymentinscomp($search,$fromdate,$todate)
     {

         $data = array();

         $sql = "select placing_slip.customer_id, placing_slip.quotation_ref, placing_slip.status,placing_slip.cover_id, placing_slip.quotation_date,  placing_slip_payamount.amount,  placing_slip_payamount.paydate from   placing_slip inner join placing_slip_payamount inner join placing_slip_premiumdetails  on placing_slip.id =  placing_slip_payamount.placing_slip_id and placing_slip.id = placing_slip_premiumdetails.placing_slip_id and placing_slip_premiumdetails.insurance_company_id = '$search'";

         if($fromdate != "" && $todate != "")
              {
             $fromdate = $this->mastermodel->convdatformat($fromdate);
        $todate = $this->mastermodel->convdatformat($todate);

             $sql=$sql." and placing_slip_payamount.paydate between '$fromdate' and '$todate'";
              }
//echo $sql;
         $Q=$this->db->query($sql);

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


     }
     function generatereportoutstandingclient($search,$fromdate,$todate,$cover_id)
     {

         $data = array();

         $sql = "select placing_slip.customer_id, placing_slip.quotation_ref, placing_slip.status,placing_slip.cover_id, placing_slip.quotation_date,  sum(placing_slip_payamount.amount) as totalamount,  placing_slip_payamount.paydate, placing_slip_premiumdetails.premium from  placing_slip inner join placing_slip_payamount inner join placing_slip_premiumdetails where placing_slip.id =  placing_slip_payamount.placing_slip_id and placing_slip.id=placing_slip_premiumdetails.placing_slip_id and placing_slip.customer_id = '$search'";

         if($fromdate != "" && $todate != "")
              {
             $fromdate = $this->mastermodel->convdatformat($fromdate);
        $todate = $this->mastermodel->convdatformat($todate);

             $sql=$sql." and placing_slip_payamount.paydate between '$fromdate' and '$todate'";
              }
          if($cover_id != "")
              $sql=$sql." and placing_slip.cover_id = '$cover_id'";


         $Q=$this->db->query($sql);

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


     }
      function generatereportoutstandingcover($search,$fromdate,$todate)
     {

         $data = array();

         $sql = "select placing_slip.customer_id, placing_slip.quotation_ref, placing_slip.status,placing_slip.cover_id, placing_slip.quotation_date,  sum(placing_slip_payamount.amount) as totalamount,  placing_slip_payamount.paydate, placing_slip_premiumdetails.premium from  placing_slip inner join placing_slip_payamount inner join placing_slip_premiumdetails where placing_slip.id =  placing_slip_payamount.placing_slip_id and placing_slip.id=placing_slip_premiumdetails.placing_slip_id and placing_slip.cover_id = '$search'";

         if($fromdate != "" && $todate != "")
              {
             $fromdate = $this->mastermodel->convdatformat($fromdate);
        $todate = $this->mastermodel->convdatformat($todate);

             $sql=$sql." and placing_slip_payamount.paydate between '$fromdate' and '$todate'";
              }
          


         $Q=$this->db->query($sql);

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


     }
     function generatereportoutstandingstaff($search,$fromdate,$todate)
     {

         $data = array();

         $sql = "select placing_slip.customer_id, placing_slip.quotation_ref, placing_slip.status,placing_slip.cover_id, placing_slip.quotation_date,  sum(placing_slip_payamount.amount) as totalamount,  placing_slip_payamount.paydate, placing_slip_premiumdetails.premium from  placing_slip inner join placing_slip_payamount inner join placing_slip_premiumdetails inner join staffcustomer on placing_slip.id =  placing_slip_payamount.placing_slip_id and placing_slip.id=placing_slip_premiumdetails.placing_slip_id and placing_slip.customer_id = staffcustomer.customerid and staffcustomer.staffid = '$search'";

         if($fromdate != "" && $todate != "")
              {
             $fromdate = $this->mastermodel->convdatformat($fromdate);
        $todate = $this->mastermodel->convdatformat($todate);

             $sql=$sql." and placing_slip_payamount.paydate between '$fromdate' and '$todate'";
              }

              
         $Q=$this->db->query($sql);

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


     }
     function generatereportoutstandinginscomp($search,$fromdate,$todate)
     {

         $data = array();

         $sql = "select placing_slip.customer_id, placing_slip.quotation_ref, placing_slip.status,placing_slip.cover_id, placing_slip.quotation_date,  sum(placing_slip_payamount.amount) as totalamount,  placing_slip_payamount.paydate, placing_slip_premiumdetails.premium from  placing_slip inner join placing_slip_payamount inner join placing_slip_premiumdetails  on placing_slip.id =  placing_slip_payamount.placing_slip_id and placing_slip.id=placing_slip_premiumdetails.placing_slip_id and placing_slip_premiumdetails.insurance_company_id = '$search'";

         if($fromdate != "" && $todate != "")
              {
             $fromdate = $this->mastermodel->convdatformat($fromdate);
        $todate = $this->mastermodel->convdatformat($todate);

             $sql=$sql." and placing_slip_payamount.paydate between '$fromdate' and '$todate'";
              }
//echo $sql;

         $Q=$this->db->query($sql);

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


     }
     function generatereportleadsstf($table,$fromdate,$todate,$staffid,$cover_id,$branch_id)
     {

         $data = array();

         $sql = "SELECT
    salesleads.leadname
    , salesleads.remarks
    , salesleads.rendate as date
    , staffpersonaldetails.name
    , covers.cover
FROM
    salesleads
    INNER JOIN staffpersonaldetails
        ON (salesleads.staffid = staffpersonaldetails.id)
    INNER JOIN covers
        ON (salesleads.cover_id = covers.id)
    INNER JOIN staffemployeedetails
        ON (staffemployeedetails.staffid = staffpersonaldetails.id)
WHERE staffpersonaldetails.id='$staffid'";


         if($fromdate != "" && $todate != "")
         {
        $fromdate = $this->mastermodel->convdatformat($fromdate);
        $todate = $this->mastermodel->convdatformat($todate);

             $sql=$sql." and salesleads.rendate between '$fromdate' and '$todate'";
         }
         if($cover_id!="cover_id")
         {
             $sql=$sql."and covers.id = '$cover_id'";
         }
          if($branch_id!='branch_id')
         {
              $sql=$sql." and branchid = '$branch_id'";
         }
//echo $sql;
         $Q=$this->db->query($sql);

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

     function generatereportleadsbra($table,$fromdate,$todate,$branch_id)
     {

         $data = array();

         $sql = "SELECT
        salesleads.leadname
    , salesleads.remarks
    , salesleads.rendate as date
    , staffpersonaldetails.name
    , covers.cover
FROM
    salesleads
    INNER JOIN staffpersonaldetails
        ON (salesleads.staffid = staffpersonaldetails.id)
    INNER JOIN covers
        ON (salesleads.cover_id = covers.id)
    INNER JOIN staffemployeedetails
        ON (staffemployeedetails.staffid = staffpersonaldetails.id)
WHERE branchid = '$branch_id'";


         if($fromdate != "" && $todate != "")
         {
        $fromdate = $this->mastermodel->convdatformat($fromdate);
        $todate = $this->mastermodel->convdatformat($todate);

             $sql=$sql." and salesleads.rendate between '$fromdate' and '$todate'";
         }
         
        
//echo $sql;
         $Q=$this->db->query($sql);

        if ($Q->num_rows() > 0)
         {
             foreach ($Q->result_array() as $row)
             {
                 $data[] = $row;
             }
         }
         $Q->free_result();
         return $data;
     }
     function generatereporttargetbra($table,$fromdate,$todate,$branch_id)
     {

         $data = array();

         $sql = "SELECT
   *
FROM
    branchtarget
    where branch = $branch_id

    ";


         if($fromdate != "" && $todate != "")
         {
             $fromdate = $this->mastermodel->convdatformat($fromdate);
        $todate = $this->mastermodel->convdatformat($todate);

             $sql=$sql." and branchtarget.startdate between '$fromdate' and '$todate'";
         }

         $Q=$this->db->query($sql);

        if ($Q->num_rows() > 0)
         {
             foreach ($Q->result_array() as $row)
             {
                 $data[] = $row;
             }
         }
         $Q->free_result();
         return $data;
     }
     function generatereporttargetempwise($table,$fromdate,$todate,$staffid)
     {

         $data = array();

         $sql = "SELECT
   * 
FROM
    settarget
    INNER JOIN staffpersonaldetails
        ON (settarget.staffid = staffpersonaldetails.id)  and settarget.staffid = $staffid
    
    ";


         if($fromdate != "" && $todate != "")
         {
             $fromdate = $this->mastermodel->convdatformat($fromdate);
        $todate = $this->mastermodel->convdatformat($todate);

             $sql=$sql." and settarget.startdate between '$fromdate' and '$todate'";
         }

         $Q=$this->db->query($sql);

        if ($Q->num_rows() > 0)
         {
             foreach ($Q->result_array() as $row)
             {
                 $data[] = $row;
             }
         }
         $Q->free_result();
         return $data;
     }
     function generatereportdailyactivity($table,$fromdate,$todate,$staffid,$branch_id)
     {
        $data = array();
        $sql = " SELECT dailyactivitydetails.name as name, dailyactivitydetails.time as time,
        dailyactivitydetails.contact_person as contact_person,dailyactivitydetails.designation as designation,dailyactivitydetails.tel_no as tel_no, dailyactivitydetails.remarks as remarks,
        dailyactivity.date as date FROM dailyactivity INNER JOIN staffpersonaldetails ON (dailyactivity.staffid = staffpersonaldetails.id)
        INNER JOIN staffemployeedetails ON (staffemployeedetails.staffid = staffpersonaldetails.id)
        INNER JOIN dailyactivitydetails ON (dailyactivitydetails.activityid = dailyactivity.id) where staffpersonaldetails.id='$staffid'";
        if($fromdate != "" && $todate != "")
        {
            $fromdate = $this->mastermodel->convdatformat($fromdate);
            $todate = $this->mastermodel->convdatformat($todate);
            $sql=$sql." and dailyactivity.date between '$fromdate' and '$todate'";
        }
        if($branch_id!='branch_id')
        {
              $sql=$sql." and branchid = '$branch_id'";
        }
//echo $sql;
        $Q=$this->db->query($sql);
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                 $data[] = $row;
            }
        }
         $Q->free_result();
         return $data;

     }
     function generatereportdailyactivitybranch($table,$fromdate,$todate,$branch_id)
     {
        $data = array();
        $sql = " SELECT dailyactivitydetails.name as name, dailyactivitydetails.time as time,staffpersonaldetails.name as employee_name,
        dailyactivitydetails.contact_person as contact_person,dailyactivitydetails.designation as designation,dailyactivitydetails.tel_no as tel_no, dailyactivitydetails.remarks as remarks,
        dailyactivity.date as date FROM dailyactivity INNER JOIN staffpersonaldetails ON (dailyactivity.staffid = staffpersonaldetails.id)
        INNER JOIN staffemployeedetails ON (staffemployeedetails.staffid = staffpersonaldetails.id)
        INNER JOIN dailyactivitydetails ON (dailyactivitydetails.activityid = dailyactivity.id) ";
        if($fromdate != "" && $todate != "")
        {
            $fromdate = $this->mastermodel->convdatformat($fromdate);
            $todate = $this->mastermodel->convdatformat($todate);
            $sql=$sql." and dailyactivity.date between '$fromdate' and '$todate'";
        }
        if($branch_id!='branch_id')
        {
              $sql=$sql." and branchid = '$branch_id'";
        }

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

     }
     function getfootervalue()
     {
          $sql="select footer from ourbranch join staffemployeedetails on staffemployeedetails.branchid = ourbranch.id join users on users.staff_id = staffemployeedetails.staffid where users.id= '".$_SESSION['userid']."'";
          $data=$this->db->query($sql);
	  $result = $data->row();
	   if($data->num_rows()>0)
	   {
	      return $result->footer;
	   }
	   else
	   {
                $sql="select footer from ourbranch  limit 1";
                $data=$this->db->query($sql);
                $result = $data->row();
                return $result->footer;
	   }
     }

     function generatereporthrsalaryemp($fromdate,$todate,$staffid)
     {
            $data = array();
            if($fromdate != "" && $todate != "")
                 {
         $fromdate = $this->mastermodel->convdatformat($fromdate);
         $todate = $this->mastermodel->convdatformat($todate);

         $dt="and  salary_date  between '$fromdate'  and  '$todate'";

                 }
                 else
                 {
                     $dt="";
                 }

                 $Q=$this->db->query("select es.salary_date,es.pay_date,es.basic_salary,es.allowance,es.deduction,es.netpayment,es.paidamount,
                         es.commission, sp.employeeid,sp.name from  emp_salary_slip es  join staffpersonaldetails sp on sp.id=es.employee_id  where employee_id='$staffid' and unpost='0'  $dt ");
                   if ($Q->num_rows() > 0)
                    {
                        foreach ($Q->result_array() as $row)
                        {
                             $data[] = $row;
                        }
                    }
                     $Q->free_result();
                     return $data;
     }

     function generatereporthrsalarybranch($fromdate,$todate,$branchid)
     {
            $data = array();
            if($fromdate != "" && $todate != "")
                 {
         $fromdate = $this->mastermodel->convdatformat($fromdate);
         $todate = $this->mastermodel->convdatformat($todate);

         $dt="and  salary_date  between '$fromdate'  and  '$todate'";

                 }
                 else
                 {
                     $dt="";
                 }

                 $Q=$this->db->query("select es.employee_id,es.salary_date,es.pay_date,es.basic_salary,es.allowance,es.deduction,es.netpayment,es.paidamount,
                         es.commission, sp.employeeid,sp.name from  emp_salary_slip es  join staffpersonaldetails sp on sp.id=es.employee_id
                         join staffemployeedetails se on se.staffid=es.employee_id  where se.branchid='$branchid' and unpost='0'  $dt ");
                   if ($Q->num_rows() > 0)
                    {
                        foreach ($Q->result_array() as $row)
                        {
                             $data[] = $row;
                        }
                    }
                     $Q->free_result();
                     return $data;
     }

     function generatereporthrleave($table,$fromdate,$todate,$staffid)
     {
            $data = array();
            if($fromdate != "" && $todate != "")
                 {
         $fromdate = $this->mastermodel->convdatformat($fromdate);
         $todate = $this->mastermodel->convdatformat($todate);

         $dt="and  fromdate >= '$fromdate'  and  todate <='$todate'";
                 }
                 else
                 {
                     $dt="";
                 }

                 $Q=$this->db->query("select a.fromdate,a.todate,a.leavestatus,a.remarks,l.leavename from  assignleave a join leavetype l on l.id=a.type where staffid='$staffid' $dt ");
           

                 if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                 $data[] = $row;
            }
        }
         $Q->free_result();
         return $data;
     }
     function generatereporthrleavebranch($table,$fromdate,$todate,$branchid)
     {
            $data = array();
            if($fromdate != "" && $todate != "")
                 {
         $fromdate = $this->mastermodel->convdatformat($fromdate);
         $todate = $this->mastermodel->convdatformat($todate);

      $dt="and  fromdate >= '$fromdate'  and  todate <='$todate'";
                 }
                 else
                 {
                     $dt="";
                 }

                 $Q=$this->db->query("select a.staffid, a.fromdate,a.todate,a.leavestatus,a.remarks,l.leavename from
                         assignleave a join leavetype l on l.id=a.type
                         join staffemployeedetails se on se.staffid=a.staffid where se.branchid='$branchid' $dt ");
                  if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                 $data[] = $row;
            }
        }
         $Q->free_result();
         return $data;
     }

     function generarereporthrcommissionemp($fromdate,$todate,$staffid)
     {
          $data = array();
          $comdet=array();
            if($fromdate != "" && $todate != "")
                 {
         $fromdate = $this->mastermodel->convdatformat($fromdate);
         $todate = $this->mastermodel->convdatformat($todate);
         $dt="and  salary_date  between '$fromdate'  and  '$todate'";
                 }
                 else
                 {
                     $dt="";
                 }

         $sql="select * from placing_slip_refs pr  group by type,pslip_id ";

         $res=$this->db->query($sql);

        // $i=0;
         $data['premium']=0;
         $data['brokerage']=0;
         $data['percentage']=0;
         $commis=0;
         foreach ($res->result_array()  as $restype)
         {
             $trtype=$restype['type'];
             $qid=$restype['pslip_id'];

                 if($trtype=='issue')
                 {

                            $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,p.fromdate as qrefdate,p.insurance_company_id,pn.policyno as policy_no,
                      p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref as qref,plc.currencytype ,p.policy_fees,sc.staffid,sc.date as effectivedate from placing_slip plc
                      join placing_slip_premiumdetails p on p.placing_slip_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                      join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                      ON plc.customer_id = sc.customerid INNER JOIN country ct
                      ON plc.currencytype = ct.id  LEFT JOIN customer_account
                      ON (customer_account.customer_id = plc.customer_id)  ";



                 }
                else if($trtype=='endorsement')
                {

                    $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.endrefno as qref,plc.endpolicyno as policy_no,plc.enddate as qrefdate,plc.premiumchange as premium,plc.policyfeeschange as policy_fees,covers.cover,
                     p.insurance_company_id,pn.policyno,plc.brokerage,plc.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate
                     from placing_slip pl left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on
                     pn.quotation_ref=pl.quotation_ref join endorsement plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id  INNER JOIN staffcustomer sc
                     ON pl.customer_id = sc.customerid INNER JOIN country ct
                     ON pl.currencytype = ct.id LEFT JOIN customer_account
                      ON (customer_account.customer_id = pl.customer_id)  ";



                }
                else  if($trtype=='cancellation')
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.canpolicyno as policy_no,plc.canrefno as qref,p.fromdate as qrefdate,0-plc.premiumchange as premium,p.insurance_company_id,pn.policyno,covers.cover,'0' as policy_fees,
                  p.brokerage,p.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate  from placing_slip pl
                  left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on pn.quotation_ref=pl.quotation_ref
                  join cancellation plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id INNER JOIN staffcustomer sc
                  ON pl.customer_id = sc.customerid INNER JOIN country ct
                  ON pl.currencytype = ct.id LEFT JOIN customer_account
                  ON (customer_account.customer_id = pl.customer_id)  ";


                }

                else
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from declaration_stakeholders where declaration_stakeholders.declaration_id=p.declaration_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,plc.certificate_refno as qref,plc.certificate_no as policy_no, p.fromdate as qrefdate,p.insurance_company_id,pn.policyno,
                  p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref,plc.currencytype,p.policy_fees,sc.staffid,sc.date as effectivedate  from declaration plc
                  join declaration_premiumdetails p on p.declaration_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                  join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                  ON plc.customer_id = sc.customerid INNER JOIN country ct
                  ON plc.currencytype = ct.id  LEFT JOIN customer_account
                  ON (customer_account.customer_id = plc.customer_id)  ";





                }

                $sql=$sql."where  plc.id='$qid' and staffid='$staffid'";


                 
                  if($fromdate != "" && $todate != "")
                 {

                     $sql=$sql." having qrefdate between '$fromdate' and '$todate' ";
                 }
                 
                 $sql=$sql." order by effectivedate desc limit 1";
               //echo $sql.'<br><br><br>';


                 $res1=$this->db->query($sql);

                 $result=$res1->row();



                if(!isset($result->qref))
                {
                    continue;
                }
                
          
               
             
                
                $data['premium']+=$result->premium;

                $data['brokerage']+=$result->brokerage;
                
                $data['percentage']+=$result->percentage;
                if($result->ispercentage)
                $commis+=($result->premium*$result->percentage)/100;
                else
                $commis+=$result->percentage;
               

               // $i++;


         }
         
//         echo $data['premium'].'<br>';
//         echo $data['percentage'];

        

         $comdet[0]['commissionamt']=$commis;

         $Q=$this->db->query("select sum(es.commission) as commpaid from  emp_salary_slip es  join staffpersonaldetails sp on sp.id=es.employee_id  where employee_id='$staffid' and unpost='0'  $dt ");
         $resl= $Q->row();
         $comdet[0]['commissionpaid']=$resl->commpaid;
         

         return $comdet;



         }


         function generatereporthrcommissionbranch($fromdate,$todate,$branchid)
         {

           $data = array();
           $comm=array();
            if($fromdate != "" && $todate != "")
                 {
         $fromdate = $this->mastermodel->convdatformat($fromdate);
         $todate = $this->mastermodel->convdatformat($todate);
         $dt="and  salary_date  between '$fromdate'  and  '$todate'";
                 }
                 else
                 {
                     $dt="";
                 }

         $sql="select * from placing_slip_refs pr  group by type,pslip_id ";

         $res=$this->db->query($sql);

         $i=0;
        // $data['premium']=0;
        // $data['brokerage']=0;
        // $data['percentage']=0;
        $commis=0;
         foreach ($res->result_array()  as $restype)
         {
             $trtype=$restype['type'];
             $qid=$restype['pslip_id'];

                 if($trtype=='issue')
                 {

                            $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,p.fromdate as qrefdate,p.insurance_company_id,pn.policyno as policy_no,
                      p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref as qref,plc.currencytype ,p.policy_fees,sc.staffid,sc.date as effectivedate from placing_slip plc
                      join placing_slip_premiumdetails p on p.placing_slip_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                      join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                      ON plc.customer_id = sc.customerid INNER JOIN country ct
                      ON plc.currencytype = ct.id  LEFT JOIN customer_account
                      ON (customer_account.customer_id = plc.customer_id) join staffemployeedetails se on se.staffid=sc.staffid     ";



                 }
                else if($trtype=='endorsement')
                {

                    $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.endrefno as qref,plc.endpolicyno as policy_no,plc.enddate as qrefdate,plc.premiumchange as premium,plc.policyfeeschange as policy_fees,covers.cover,
                     p.insurance_company_id,pn.policyno,plc.brokerage,plc.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate
                     from placing_slip pl left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on
                     pn.quotation_ref=pl.quotation_ref join endorsement plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id  INNER JOIN staffcustomer sc
                     ON pl.customer_id = sc.customerid INNER JOIN country ct
                     ON pl.currencytype = ct.id LEFT JOIN customer_account
                      ON (customer_account.customer_id = pl.customer_id)  join staffemployeedetails se on se.staffid=sc.staffid   ";



                }
                else  if($trtype=='cancellation')
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from placing_slip_stakeholders where placing_slip_stakeholders.placing_slip_id=p.placing_slip_id ),'0') as percentage,ct.currencycode,plc.canpolicyno as policy_no,plc.canrefno as qref,p.fromdate as qrefdate,0-plc.premiumchange as premium,p.insurance_company_id,pn.policyno,covers.cover,'0' as policy_fees,
                  p.brokerage,p.ispercentage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype,sc.staffid,sc.date as effectivedate  from placing_slip pl
                  left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on pn.quotation_ref=pl.quotation_ref
                  join cancellation plc on plc.policyid=pn.id left join covers on covers.id=pl.cover_id INNER JOIN staffcustomer sc
                  ON pl.customer_id = sc.customerid INNER JOIN country ct
                  ON pl.currencytype = ct.id LEFT JOIN customer_account
                  ON (customer_account.customer_id = pl.customer_id) join staffemployeedetails se on se.staffid=sc.staffid   ";


                }

                else
                {
                        $sql="SELECT IFNULL( (select sum(percentage) from declaration_stakeholders where declaration_stakeholders.declaration_id=p.declaration_id ),'0') as percentage,ct.currencycode,c.cover,p.premium,plc.customer_id,plc.cover_id,plc.certificate_refno as qref,plc.certificate_no as policy_no, p.fromdate as qrefdate,p.insurance_company_id,pn.policyno,
                  p.brokerage,p.ispercentage,plc.id as plid,plc.cover_id,plc.quotation_ref,plc.currencytype,p.policy_fees,sc.staffid,sc.date as effectivedate  from declaration plc
                  join declaration_premiumdetails p on p.declaration_id=plc.id left join policynote pn on pn.quotation_ref=plc.quotation_ref
                  join covers c on c.id=plc.cover_id INNER JOIN staffcustomer sc
                  ON plc.customer_id = sc.customerid INNER JOIN country ct
                  ON plc.currencytype = ct.id  LEFT JOIN customer_account
                  ON (customer_account.customer_id = plc.customer_id) join staffemployeedetails se on se.staffid=sc.staffid   ";





                }

                $sql=$sql."where  plc.id='$qid' and se.branchid='$branchid' ";



                  if($fromdate != "" && $todate != "")
                 {

                     $sql=$sql." having qrefdate between '$fromdate' and '$todate' ";
                 }
                 
                    $sql=$sql." order by effectivedate desc limit 1";
                //echo $sql.'<br><br><br>';


                 $res1=$this->db->query($sql);

                 $result=$res1->row();



                if(!isset($result->qref))
                {
                    continue;
                }





                $data[$i]['premium']=$result->premium;

                $data[$i]['brokerage']=$result->brokerage;

                $data[$i]['percentage']=$result->percentage;
                $data[$i]['ispercentage']=$result->ispercentage;
                $data[$i]['staffid']=$result->staffid;


               

                $i++;


         }

            $staffidsort=array();
            foreach($data as $key => $row)
            {
            $staffidsort[$key]=$row['staffid'];

            }



         array_multisort($staffidsort, SORT_DESC, $data);
         $stid='unknown';
         $k=-1;
        
         foreach($data as $updata)
            {

                if($updata['staffid']!=$stid)
                    {
                        $k++;
                        $comm[$k]['premium']=0;

                        $comm[$k]['brokerage']=0;

                        $comm[$k]['commission']=0;
                        
                        $stid=$updata['staffid'];
                        
                    }
                        $comm[$k]['staffid']=$updata['staffid'];
                        $comm[$k]['premium']+=$updata['premium'];

                        $comm[$k]['brokerage']+=$updata['brokerage'];

                        if($result->ispercentage)
                        $comm[$k]['commission']+=($updata['premium']*$updata['percentage'])/100;
                        else
                        $comm[$k]['commission']+=$updata['percentage'];     






            }

           

        $z=0;
        foreach($comm as $stf)
        {
            $empid=$stf['staffid'];
            $Q=$this->db->query("select sum(commission) as commpaid,name from  emp_salary_slip  es right join staffpersonaldetails sp on sp.id=es.employee_id where es.employee_id='$empid' and unpost='0'  $dt ");
            $resl= $Q->row();
            $comm[$z]['commissionpaid']=$resl->commpaid;
            $comm[$z]['name']=$resl->name;

         $z++;
        }

        return $comm;
    }
         
         
    function load_report_list($report_type)
    {
         $this->db->select('*')
                 ->from('report')
                 ->join('report_list_permission','report_list_permission.report_id=report.id')
                 ->where('report_list_permission.user_id',$_SESSION['userid'])
                 ->where('report.report_for',$report_type);

         $result = $this->db->get();

         return $result->result_array();

    }
    
    function array_column($input = null, $columnKey = null, $indexKey = null)
    {
        // Using func_get_args() in order to check for proper number of
        // parameters and trigger errors exactly as the built-in array_column()
        // does in PHP 5.5.
        $argc = func_num_args();
        $params = func_get_args();

        if ($argc < 2) {
            trigger_error("array_column() expects at least 2 parameters, {$argc} given", E_USER_WARNING);
            return null;
        }

        if (!is_array($params[0])) {
            trigger_error(
                'array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given',
                E_USER_WARNING
            );
            return null;
        }

        if (!is_int($params[1])
            && !is_float($params[1])
            && !is_string($params[1])
            && $params[1] !== null
            && !(is_object($params[1]) && method_exists($params[1], '__toString'))
        ) {
            trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING);
            return false;
        }

        if (isset($params[2])
            && !is_int($params[2])
            && !is_float($params[2])
            && !is_string($params[2])
            && !(is_object($params[2]) && method_exists($params[2], '__toString'))
        ) {
            trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING);
            return false;
        }

        $paramsInput = $params[0];
        $paramsColumnKey = ($params[1] !== null) ? (string) $params[1] : null;

        $paramsIndexKey = null;
        if (isset($params[2])) {
            if (is_float($params[2]) || is_int($params[2])) {
                $paramsIndexKey = (int) $params[2];
            } else {
                $paramsIndexKey = (string) $params[2];
            }
        }

        $resultArray = array();

        foreach ($paramsInput as $row) {
            $key = $value = null;
            $keySet = $valueSet = false;

            if ($paramsIndexKey !== null && array_key_exists($paramsIndexKey, $row)) {
                $keySet = true;
                $key = (string) $row[$paramsIndexKey];
            }

            if ($paramsColumnKey === null) {
                $valueSet = true;
                $value = $row;
            } elseif (is_array($row) && array_key_exists($paramsColumnKey, $row)) {
                $valueSet = true;
                $value = $row[$paramsColumnKey];
            }

            if ($valueSet) {
                if ($keySet) {
                    $resultArray[$key] = $value;
                } else {
                    $resultArray[] = $value;
                }
            }

        }

        return $resultArray;
    }
}
?>

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