? Fallagassrini

Fallagassrini Bypass Shell

echo"
Fallagassrini
";
Current Path : /home1/savoy/public_html/savoyglobal.net/sibs_draft/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/sibs_draft/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;
        foreach ($res->result_array()  as $restype)
        {
            $trtype=$restype['type'];
            $qid=$restype['pslip_id'];
            $flag = 0;
            if ($payment_mode == 'all') {
                $flag = 1;
            } 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 = 1;
                    }
                } else if ($trtype == 'endorsement') {
                    if ($this->mastermodel->getSingleFieldValue('endorsement','paymentoptions','id',$qid) == 'insurance_company') {
                        $flag = 1;
                    }
                }
            } else {
                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 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,
                        IFNULL(SUM(customer_receipt_allocation.cr_allocation_amount),0) as received_premium,
                        IFNULL(SUM(cust_allocate.amount),0) as received_premium_to_broker
                        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, 
                        IFNULL(SUM(customer_receipt_allocation.cr_allocation_amount),0) as received_premium,
                        IFNULL(SUM(cust_allocate.amount),0) as received_premium_to_broker
                        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 customer_receipt_allocation.cr_allocation_pid";
                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;
                }
                $balance = abs($result->premium+$result->policy_fees-$result->received_premium);
                if ($payment_status == 'pending' && $balance == 0) {
                    continue;
                } else if ($payment_status == 'completed' && $balance != 0) {
                    continue;
                }
                
                
                
                $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']= -($result->premium);
                    $data[$result->customer_id][$result->insurance_company_id][$i]['received_premium']=-($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;


                $i++;
            }
        }
        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);
        }
        $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;
        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 as customer_id,plc.cover_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.cover_id,plc.quotation_ref as qref,plc.currencytype ,p.policy_fees, 
                        plc.remarks as description ,'' 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'  ";



                }
                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(customer_receipt_allocation.cr_allocation_amount) as received_premium,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 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'";
                
               
                $sql.=" GROUP BY customer_receipt_allocation.cr_allocation_pid";
                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;
                }
                $balance = abs($result->premium+$result->policy_fees-$result->received_premium);
                if ($balance == 0) {
                    continue;
                } 
                
                
                
                if ($result->endorsementmode == 'Deduction') {
                    $premium= -($result->premium);
                    $received_premium =-($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]['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;
                
                $i++;
            }
        }
        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'];     






            }

           


//         $commission=(($data['premium']*$data['percentage'])/100);
//
//         $comdet[0]['commissionamt']=$commission;
//
        $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();
             
         }


}
?>

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