? Fallagassrini

Fallagassrini Bypass Shell

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

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');?>
<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class policiesmodel extends Model
{
     function policiesmodel()
     {
         parent::Model();

     }
function norecords($tabname)
        {
            $data = array();
            $sql = "select MAX(id) as maxid from $tabname";
              $res = mysql_query($sql);
             $row = mysql_fetch_array($res);
             $numcode =  $row['maxid'];
             if ($numcode != '')
             {
            return $numcode;
             }
             else {
                 return 0;
             }
        }
      function insertrenewal()
    {
          $renreno = $_POST['renref']."/".$_POST['rensrefno'];
       $query=$this->db->query("insert into renewal (policyid,renrefno) values('$_POST[policyid]','".$renreno."')") ;
     $renewalid=mysql_insert_id();
		
         
        if($query>0)
       {
           return 1;
       }
       else
           {
           return 0;
       }
    }
      
      function insertendorsement($oldcrereferenceno="",$olddebreferenceno="")
         {
           $this->db->trans_begin();
           $endreno = $_POST['endref'];
           $policyid=$_POST['policyid'];
           $endno = $this->db->query("select `endrefno` from endorsement where id in (select max(id) from endorsement where `policyid` = '$policyid')");
           $result= $endno->row();
           $code=$result->endrefno;
           $arr=explode('/', $code);

           $endrefno=$arr[4];
           $endno=substr($endrefno, 1);

           $endreno .= "/E".($endno+1);
           $endpolicyno=$_POST['endpolicyno'];
           $date=$this->mastermodel->convdatformat($_POST['date']);
           $mode=$_POST['mode'];
           $premiumchange=$_POST['premiumchange'];
           $policyfeeschange=$_POST['policyfeeschange'];
           $remarks=$_POST['remarks'];
           if(isset($_POST['paymethod']))
           {
           $payoption=$_POST['paymethod'];
           }
           else
           {
           $payoption='Nil';
           }
			

                     
           $enddate=$this->mastermodel->convdatformat($_POST['enddate']);
           $type="";


              // $type = $_POST['Txt_Pay_accno'];
         $query=$this->db->query("insert into endorsement values('','$policyid','$endpolicyno','$date','$endreno','$mode','$type','$premiumchange','$policyfeeschange','$remarks','$payoption','$enddate')") ;
         

         $endorsementid=mysql_insert_id();

         $premdet=$this->db->query("SELECT p.insurance_company_id,p.brokerage,pl.id as plid,pl.cover_id,s.staffid,s.percentage,pl.customer_id,pl.quotation_ref from placing_slip pl left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join placing_slip_stakeholders s on
         s.placing_slip_id=pl.id  join policynote pn on pn.quotation_ref=pl.quotation_ref where pn.id='$policyid'");      
         foreach ($premdet->result_array() as $row)
             {
                 $brokerage = $row['brokerage'];
                 $customerid= $row['customer_id'];
                 $quotation_ref= $row['quotation_ref'];
                 $staffid[]=$row['staffid'];
                 $percentage[]=$row['percentage'];
                 $coverid=$row['cover_id'];
                 $inscompid=$row['insurance_company_id'];
                 $plid=$row['plid'];
             }

         if($mode=='Addition')
         {

          
            

         /*********************** Add Agent and their commission percentage*****************************/
              $tot_brokerage=$brokerage;
              $tot_brokerage_cover=$tot_brokerage;
              //$trans_date=$acctype_val= $this->customermodel->getSingleFieldValue('placing_slip','quotation_date','id',$_POST['placing_slip_id']);
              $tdate=date('Y-m-d');
              if (!in_array(0,$staffid))
                 {
              $tra_cod_c= $this->customermodel->getSingleFieldValue('transaction_code','code','trans',5);
                        if($tra_cod_c!='')
                             {
                               $next_ref= $this->accountingsmodel->get_reference_code(5);
                              $refc=$tra_cod_c.'/'.date('Y').'/'.$next_ref;
                             }
                       else
                             {
                              $refc="0";
                             }
            /************ Insert into refs table and take inserted id **************/
                             if($oldcrereferenceno)
                                 $refc=$oldcrereferenceno;
                             
                            $refdatae=array(
                                    'type'=>'5',
                                     'reference'=>$refc
                                   );
                                  $this->db->insert('refs', $refdatae);
                                  $refid=$this->db->insert_id() ;
             /************ Insert into placing slip refs table  **************/
                          $refpdata=array(
                                      'id'=>$refid,
                                      'pslip_id'=>$endorsementid,
                                      'type'=>'endorsement'
                               );
                              $this->db->insert('placing_slip_refs', $refpdata);


                              $fyear=$this->accountingsmodel->fiscalyear();

                       $insValuesA=array(
                                  'trans_no'=>$refid,
                                  'user'=>$_SESSION['userid'],
                                  'date_trans'=>$tdate,

                                  'fiscal_year'=>$fyear,
                                  'gl_date'=>$tdate

                             );
                         $this->db->insert('audit_trail', $insValuesA);



              
                 
                 $comm_pay_amt=0;
                 for($sff=0;$sff<sizeof($staffid);$sff++)
                      {
                            $preamt1=$_POST['premiumchange'];
                           
                            $agentamt= $preamt1 *($percentage[$sff]/100);

                            $staffname=$this->mastermodel->get_data_srow('staffpersonaldetails',$staffid[$sff],'id');
                            $agent_acc=$staffname->employeeid;
                           
                            $trans_date=$acctype_val= $this->customermodel->getSingleFieldValue('placing_slip','quotation_date','id',$plid);
                            if($agentamt!=0)
                            {
                            
                            $insValuesS=array(
                                    'type_no'=>$refid,
                                    'tran_date'=>$tdate,
                                    'account'=>$agent_acc,
                                    'memo'=>'',
                                    'amount'=>-number_format($agentamt,2, '.', '')
                            );
                            $insValuesAt=array(
                                    'type_no'=>$refid,
                                    'tran_date'=>$tdate,
                                    'account'=>$agent_acc,
                                    'memo'=>'',
                                    'amount'=>-number_format($agentamt,2, '.', ''),
                                    'pid'=>$plid
                            );


                             $this->db->insert('gl_trans', $insValuesS);
                             $this->db->insert('agent_trans', $insValuesAt);
                            }
                            $tot_brokerage=$tot_brokerage-$percentage[$sff];
                            $comm_pay_amt+=$agentamt;
                      }
                      
                     $commission_acc=$this->documentsmodel->get_commission_pay_acc();
                     if($comm_pay_amt!=0)
                     {
                     $insValuesCP=array(
                                    'type_no'=>$refid,
                                    'tran_date'=>$tdate,
                                    'account'=>$commission_acc,
                                    'memo'=>'',
                                    'amount'=>number_format($comm_pay_amt,2, '.', '')
                            );
                        $this->db->insert('gl_trans', $insValuesCP);
                     }

              }
              /********************************insert total premium as customer debit into gl trans************************************/
              $preamt=$_POST['premiumchange']+$_POST['policyfeeschange'];
              $cust_acc=$this->accountingsmodel->get_acc_id('customers','code',$customerid);
             




                        $tra_cod_d= $this->customermodel->getSingleFieldValue('transaction_code','code','trans',6);
                        if($tra_cod_d!='')
                             {
                               $next_ref1= $this->accountingsmodel->get_reference_code(6);
                              $refd=$tra_cod_d.'/'.date('Y').'/'.$next_ref1;
                             }
                       else
                             {
                              $refd="0";
                            }
            /************ Insert into refs table and take inserted id **************/
                            if($olddebreferenceno)
                                $refd=$olddebreferenceno;
                            
                            $refdata=array(
                                    'type'=>'6',
                                     'reference'=>$refd
                                   );
                                  $this->db->insert('refs', $refdata);
                                  $refid1=$this->db->insert_id() ;
             /************ Insert into placing slip refs table  **************/
                          $refpdata=array(
                                      'id'=>$refid1,
                                      'pslip_id'=>$endorsementid,
                                       'type'=>'endorsement'
                               );
                              $this->db->insert('placing_slip_refs', $refpdata);


                              $fyear=$this->accountingsmodel->fiscalyear();

                       $insValuesA1=array(
                                  'trans_no'=>$refid1,
                                  'user'=>$_SESSION['userid'],
                                  'date_trans'=>$tdate,

                                  'fiscal_year'=>$fyear,
                                  'gl_date'=>$tdate

                             );
                         $this->db->insert('audit_trail', $insValuesA1);






             $trans_date=$acctype_val= $this->customermodel->getSingleFieldValue('placing_slip','quotation_date','id',$plid);
                $insValues=array(

                                    'type_no'=>$refid1,
                                    'tran_date'=>$tdate,
                                    'account'=>$cust_acc,
                                    'memo'=>'',
                                    'amount'=>number_format($preamt,2, '.', '')

                                 );
                  $this->db->insert('gl_trans', $insValues);


                   /********************************insert into customer transaction table*************************************************/
                $insValue=array(

                                    'type_no'=>$refid1,
                                    'tran_date'=>$tdate,
                                    'account'=>$cust_acc,
                                    'memo'=>'',
                                    'amount'=>number_format($preamt,2, '.', ''),
                                    'alloc'=>'',
                                    'pid'=>$plid

                                 );
                   $this->db->insert('customer_trans', $insValue);

         /*******************************insert brokerage amount into cover acc in gl trans*************************************/
                  $brokamount=$brokerage;

                  $totbrotamt= $_POST['premiumchange'] *($brokamount/100);

                  $coveracc= $this->accountingsmodel->get_mainaccount_setting('cover_start');
                  $covrmain_acc=$coveracc+$coverid;

                
                  $trans_date=$acctype_val= $this->customermodel->getSingleFieldValue('placing_slip','quotation_date','id',$plid);
                  $insValues1=array(

                                    'type_no'=>$refid1,
                                    'tran_date'=>$tdate,
                                    'account'=>$covrmain_acc,
                                    'memo'=>'',
                                    'amount'=>-number_format($totbrotamt,2, '.', '')

                                );
                  $this->db->insert('gl_trans', $insValues1);
        /****************************************insert insurer amount into credit as company acc*********************************************/
                  $insurer= 100 - $brokerage;
                  $compamt= $premiumchange * ($insurer/100) +$policyfeeschange;
                 

                $comp_acc=$this->accountingsmodel->get_acc_id('insurance_company','company_code',$inscompid);

                $insValuesC=array(
                                  'type_no'=>$refid1,
                                  'tran_date'=>$tdate,
                                  'account'=>$comp_acc,
                                  'memo'=>'',
                                  'amount'=>-number_format($compamt,2, '.', '')

                  );

            $insValuesC1=array(
                                  'type_no'=>$refid1,
                                  'tran_date'=>$tdate,
                                  'account'=>$comp_acc,
                                  'memo'=>'',
                                  'amount'=>-number_format($compamt,2, '.', ''),
                                  'pid'=>$plid

                  );
                $this->db->insert('gl_trans', $insValuesC);
                $this->db->insert('company_trans', $insValuesC1);


         }
         else if($mode=='Deduction')
         {
          /*********************** Add Agent and their commission percentage*****************************/
              $tot_brokerage=$brokerage;
              $tot_brokerage_cover=$tot_brokerage;
              //$trans_date=$acctype_val= $this->customermodel->getSingleFieldValue('placing_slip','quotation_date','id',$_POST['placing_slip_id']);
              $tdate=date('Y-m-d');
              if (!in_array(0,$staffid))
                 {
              $tra_cod_c= $this->customermodel->getSingleFieldValue('transaction_code','code','trans',6);
                        if($tra_cod_c!='')
                             {
                               $next_ref= $this->accountingsmodel->get_reference_code(6);
                              $refc=$tra_cod_c.'/'.date('Y').'/'.$next_ref;
                             }
                       else
                             {
                              $refc="0";
                             }
            /************ Insert into refs table and take inserted id **************/
                            $refdatae=array(
                                    'type'=>'6',
                                     'reference'=>$refc
                                   );
                                  $this->db->insert('refs', $refdatae);
                                  $refid=$this->db->insert_id() ;
             /************ Insert into placing slip refs table  **************/
                          $refpdata=array(
                                      'id'=>$refid,
                                      'pslip_id'=>$endorsementid,
                                       'type'=>'endorsement'
                               );
                              $this->db->insert('placing_slip_refs', $refpdata);


                              $fyear=$this->accountingsmodel->fiscalyear();

                       $insValuesA=array(
                                  'trans_no'=>$refid,
                                  'user'=>$_SESSION['userid'],
                                  'date_trans'=>$tdate,

                                  'fiscal_year'=>$fyear,
                                  'gl_date'=>$tdate

                             );
                         $this->db->insert('audit_trail', $insValuesA);



              

                 $comm_pay_amt=0;
                 for($sff=0;$sff<sizeof($staffid);$sff++)
                      {
                            $preamt1=$_POST['premiumchange'];

                            $agentamt= $preamt1 *($percentage[$sff]/100);

                            $staffname=$this->mastermodel->get_data_srow('staffpersonaldetails',$staffid[$sff],'id');
                            $agent_acc=$staffname->employeeid;

                            $trans_date=$acctype_val= $this->customermodel->getSingleFieldValue('placing_slip','quotation_date','id',$plid);

                            $insValuesS=array(
                                    'type_no'=>$refid,
                                    'tran_date'=>$tdate,
                                    'account'=>$agent_acc,
                                    'memo'=>'',
                                    'amount'=>number_format($agentamt,2, '.', '')
                            );
                            $insValuesAt=array(
                                    'type_no'=>$refid,
                                    'tran_date'=>$tdate,
                                    'account'=>$agent_acc,
                                    'memo'=>'',
                                    'amount'=>number_format($agentamt,2, '.', ''),
                                    'pid'=>$plid
                            );


                             $this->db->insert('gl_trans', $insValuesS);
                             $this->db->insert('agent_trans', $insValuesAt);
                            $tot_brokerage=$tot_brokerage-$percentage[$sff];
                            $comm_pay_amt+=$agentamt;
                      }
                     $commission_acc=$this->documentsmodel->get_commission_pay_acc();
                     $insValuesCP=array(
                                    'type_no'=>$refid,
                                    'tran_date'=>$tdate,
                                    'account'=>$commission_acc,
                                    'memo'=>'',
                                    'amount'=>-number_format($comm_pay_amt,2, '.', '')
                            );
                        $this->db->insert('gl_trans', $insValuesCP);

              }
              /********************************insert total premium as customer debit into gl trans************************************/
              $preamt=$_POST['premiumchange']+$_POST['policyfeeschange'];
              $cust_acc=$this->accountingsmodel->get_acc_id('customers','code',$customerid);





                        $tra_cod_d= $this->customermodel->getSingleFieldValue('transaction_code','code','trans',5);
                        if($tra_cod_d!='')
                             {
                               $next_ref1= $this->accountingsmodel->get_reference_code(5);
                              $refd=$tra_cod_d.'/'.date('Y').'/'.$next_ref1;
                             }
                       else
                             {
                              $refd="0";
                            }
            /************ Insert into refs table and take inserted id **************/
                            $refdata=array(
                                    'type'=>'5',
                                     'reference'=>$refd
                                   );
                                  $this->db->insert('refs', $refdata);
                                  $refid1=$this->db->insert_id() ;
             /************ Insert into placing slip refs table  **************/
                          $refpdata=array(
                                      'id'=>$refid1,
                                      'pslip_id'=>$endorsementid,
                                       'type'=>'endorsement'
                               );
                              $this->db->insert('placing_slip_refs', $refpdata);


                              $fyear=$this->accountingsmodel->fiscalyear();

                       $insValuesA1=array(
                                  'trans_no'=>$refid1,
                                  'user'=>$_SESSION['userid'],
                                  'date_trans'=>$tdate,

                                  'fiscal_year'=>$fyear,
                                  'gl_date'=>$tdate

                             );
                         $this->db->insert('audit_trail', $insValuesA1);






                $trans_date=$acctype_val= $this->customermodel->getSingleFieldValue('placing_slip','quotation_date','id',$plid);
                $insValues=array(

                                    'type_no'=>$refid1,
                                    'tran_date'=>$tdate,
                                    'account'=>$cust_acc,
                                    'memo'=>'',
                                    'amount'=>-number_format($preamt,2, '.', '')

                                 );
                  $this->db->insert('gl_trans', $insValues);


                   /********************************insert into customer transaction table*************************************************/
                $insValue=array(

                                    'type_no'=>$refid1,
                                    'tran_date'=>$tdate,
                                    'account'=>$cust_acc,
                                    'memo'=>'',
                                    'amount'=>-number_format($preamt,2, '.', ''),
                                    'alloc'=>'',
                                    'pid'=>$plid

                                 );
                   $this->db->insert('customer_trans', $insValue);

         /*******************************insert brokerage amount into cover acc in gl trans*************************************/
                  $brokamount=$brokerage;

                  $totbrotamt= $_POST['premiumchange'] *($brokamount/100);

                  $coveracc= $this->accountingsmodel->get_mainaccount_setting('cover_start');
                  $covrmain_acc=$coveracc+$coverid;


                  $trans_date=$acctype_val= $this->customermodel->getSingleFieldValue('placing_slip','quotation_date','id',$plid);
                  $insValues1=array(

                                    'type_no'=>$refid1,
                                    'tran_date'=>$tdate,
                                    'account'=>$covrmain_acc,
                                    'memo'=>'',
                                    'amount'=>number_format($totbrotamt,2, '.', '')

                                );
                  $this->db->insert('gl_trans', $insValues1);
        /****************************************insert insurer amount into credit as company acc*********************************************/
                  $insurer= 100 - $brokerage;
                  $compamt= $premiumchange * ($insurer/100) +$policyfeeschange;


                $comp_acc=$this->accountingsmodel->get_acc_id('insurance_company','company_code',$inscompid);

                $insValuesC=array(
                                  'type_no'=>$refid1,
                                  'tran_date'=>$tdate,
                                  'account'=>$comp_acc,
                                  'memo'=>'',
                                  'amount'=>number_format($compamt,2, '.', '')

                  );

            $insValuesC1=array(
                                  'type_no'=>$refid1,
                                  'tran_date'=>$tdate,
                                  'account'=>$comp_acc,
                                  'memo'=>'',
                                  'amount'=>number_format($compamt,2, '.', ''),
                                  'pid'=>$plid

                  );
                $this->db->insert('gl_trans', $insValuesC);
                $this->db->insert('company_trans', $insValuesC1);
         }
         else
         {



         /*********************** Add Agent and their commission percentage*****************************/
             // $tot_brokerage=$brokerage;
            //  $tot_brokerage_cover=$tot_brokerage;
            
              //$trans_date=$acctype_val= $this->customermodel->getSingleFieldValue('placing_slip','quotation_date','id',$_POST['placing_slip_id']);
              $tdate=date('Y-m-d');

              $tra_cod_c= $this->customermodel->getSingleFieldValue('transaction_code','code','trans',5);
                        if($tra_cod_c!='')
                             {
                               $next_ref= $this->accountingsmodel->get_reference_code(5);
                              $refc=$tra_cod_c.'/'.date('Y').'/'.$next_ref;
                             }
                       else
                             {
                              $refc="0";
                             }
            /************ Insert into refs table and take inserted id **************/
                            $refdatae=array(
                                    'type'=>'5',
                                     'reference'=>$refc
                                   );
                                  $this->db->insert('refs', $refdatae);
                                  $refid=$this->db->insert_id() ;
             /************ Insert into placing slip refs table  **************/
                          $refpdata=array(
                                      'id'=>$refid,
                                      'pslip_id'=>$endorsementid,
                                      'type'=>'endorsement'
                               );
                              $this->db->insert('placing_slip_refs', $refpdata);


                              $fyear=$this->accountingsmodel->fiscalyear();

                       $insValuesA=array(
                                  'trans_no'=>$refid,
                                  'user'=>$_SESSION['userid'],
                                  'date_trans'=>$tdate,

                                  'fiscal_year'=>$fyear,
                                  'gl_date'=>$tdate

                             );
                         $this->db->insert('audit_trail', $insValuesA);



             
              /********************************insert total premium as customer debit into gl trans************************************/
          




                        $tra_cod_d= $this->customermodel->getSingleFieldValue('transaction_code','code','trans',6);
                        if($tra_cod_d!='')
                             {
                               $next_ref1= $this->accountingsmodel->get_reference_code(6);
                              $refd=$tra_cod_d.'/'.date('Y').'/'.$next_ref1;
                             }
                       else
                             {
                              $refd="0";
                            }
            /************ Insert into refs table and take inserted id **************/
                            $refdata=array(
                                    'type'=>'6',
                                     'reference'=>$refd
                                   );
                                  $this->db->insert('refs', $refdata);
                                  $refid1=$this->db->insert_id() ;
             /************ Insert into placing slip refs table  **************/
                          $refpdata=array(
                                      'id'=>$refid1,
                                      'pslip_id'=>$endorsementid,
                                       'type'=>'endorsement'
                               );
                              $this->db->insert('placing_slip_refs', $refpdata);


                              $fyear=$this->accountingsmodel->fiscalyear();

                       $insValuesA1=array(
                                  'trans_no'=>$refid1,
                                  'user'=>$_SESSION['userid'],
                                  'date_trans'=>$tdate,

                                  'fiscal_year'=>$fyear,
                                  'gl_date'=>$tdate

                             );
                         $this->db->insert('audit_trail', $insValuesA1);

         
         }




          

        if ($this->db->trans_status() === FALSE)
	     {
		 $this->db->trans_rollback();
                 return 0;

	     }
	     else
	     {
		 $this->db->trans_commit();
                 return 1;

	     }


    }

function deletetable($endorsementid)
{
 $this->db->query("delete from endorsement_netbank where endorsment_id='$endorsementid' ");
 $this->db->query("delete from endorsement_bank where endorsment_id='$endorsementid' ");
 $this->db->query("delete from endorsement_creditmode where endorsment_id='$endorsementid' ");

}
     
    function insertcancellation($oldcrereferenceno="",$olddebreferenceno="")
         {

        $this->db->trans_begin();
       
      
         $date=$_POST['date'];
         $policyid=$_POST['policyid'];
         $date= $this->mastermodel->convdatformat($date);
         $premiumchange=$_POST['premiumchange'];

         $canreno = $_POST['canref'];
        // $canno = $this->policiesmodel->get_cover_num_policy($policyid,'cancellation');
         $canreno .= "/CL";

         $query=$this->db->query("insert into cancellation values('','$_POST[policyid]','$_POST[canpolicyno]','$date','$canreno','$_POST[remarks]','$_POST[premiumchange]')") ;
         $cancellationid=mysql_insert_id();


         $premdet=$this->db->query("SELECT p.insurance_company_id,p.brokerage,pl.id as plid,pl.cover_id,ifnull(s.staffid,0) as staffid,s.percentage,pl.customer_id,pl.quotation_ref from placing_slip pl left join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join placing_slip_stakeholders s on
         s.placing_slip_id=pl.id  join policynote pn on pn.quotation_ref=pl.quotation_ref where pn.id='$policyid'");

    
         foreach ($premdet->result_array() as $row)
             {
                 $brokerage = $row['brokerage'];
                 $customerid= $row['customer_id'];
                 $quotation_ref= $row['quotation_ref'];
                 $staffid[]=$row['staffid'];
                 
                 $percentage[]=$row['percentage'];
                 $coverid=$row['cover_id'];
                 $inscompid=$row['insurance_company_id'];
                 $plid=$row['plid'];
             }
       
              $tot_brokerage=$brokerage;
              $tot_brokerage_cover=$tot_brokerage;
              //$trans_date=$acctype_val= $this->customermodel->getSingleFieldValue('placing_slip','quotation_date','id',$_POST['placing_slip_id']);
              $tdate=date('Y-m-d');

                                  
                        
              if (!in_array(0,$staffid))
                 {
                  $tra_cod_c= $this->customermodel->getSingleFieldValue('transaction_code','code','trans',6);
                        if($tra_cod_c!='')
                             {
                               $next_ref= $this->accountingsmodel->get_reference_code(6);
                              $refc=$tra_cod_c.'/'.date('Y').'/'.$next_ref;
                             }
                       else
                             {
                              $refc="0";
                             }
            /************ Insert into refs table and take inserted id **************/
                             if($olddebreferenceno)
                                 $refc=$olddebreferenceno;
                             
                            $refdatae=array(
                                    'type'=>'6',
                                     'reference'=>$refc
                                   );
                                  $this->db->insert('refs', $refdatae);
                                  $refid=$this->db->insert_id() ;
             /************ Insert into placing slip refs table  **************/
                          $refpdata=array(
                                      'id'=>$refid,
                                      'pslip_id'=>$cancellationid,
                                       'type'=>'cancellation'
                               );
                              $this->db->insert('placing_slip_refs', $refpdata);


                              $fyear=$this->accountingsmodel->fiscalyear();

                       $insValuesA=array(
                                  'trans_no'=>$refid,
                                  'user'=>$_SESSION['userid'],
                                  'date_trans'=>$tdate,

                                  'fiscal_year'=>$fyear,
                                  'gl_date'=>$tdate

                             );
                         $this->db->insert('audit_trail', $insValuesA);

                 $comm_pay_amt=0;
                 for($sff=0;$sff<sizeof($staffid);$sff++)
                      {
                            $preamt1=$_POST['premiumchange'];

                            $agentamt= $preamt1 *($percentage[$sff]/100);

                            $staffname=$this->mastermodel->get_data_srow('staffpersonaldetails',$staffid[$sff],'id');
                            $agent_acc=$staffname->employeeid;

                            $trans_date=$acctype_val= $this->customermodel->getSingleFieldValue('placing_slip','quotation_date','id',$plid);

                            if($agentamt!=0)
                            {
                            $insValuesS=array(
                                    'type_no'=>$refid,
                                    'tran_date'=>$tdate,
                                    'account'=>$agent_acc,
                                    'memo'=>'',
                                    'amount'=>number_format($agentamt,2, '.', '')
                            );
                            $insValuesAt=array(
                                    'type_no'=>$refid,
                                    'tran_date'=>$tdate,
                                    'account'=>$agent_acc,
                                    'memo'=>'',
                                    'amount'=>number_format($agentamt,2, '.', ''),
                                    'pid'=>$plid
                            );


                             $this->db->insert('gl_trans', $insValuesS);
                             $this->db->insert('agent_trans', $insValuesAt);
                            }
                            $tot_brokerage=$tot_brokerage-$percentage[$sff];
                            $comm_pay_amt+=$agentamt;
                      }
                     $commission_acc=$this->documentsmodel->get_commission_pay_acc();
                     if($comm_pay_amt!=0)
                     {
                     $insValuesCP=array(
                                    'type_no'=>$refid,
                                    'tran_date'=>$tdate,
                                    'account'=>$commission_acc,
                                    'memo'=>'',
                                    'amount'=>-number_format($comm_pay_amt,2, '.', '')
                            );
                        $this->db->insert('gl_trans', $insValuesCP);
                     }

              }
              /********************************insert total premium as customer debit into gl trans************************************/
              $preamt=$_POST['premiumchange'];
              $cust_acc=$this->accountingsmodel->get_acc_id('customers','code',$customerid);





                        $tra_cod_d= $this->customermodel->getSingleFieldValue('transaction_code','code','trans',5);
                        if($tra_cod_d!='')
                             {
                               $next_ref1= $this->accountingsmodel->get_reference_code(5);
                              $refd=$tra_cod_d.'/'.date('Y').'/'.$next_ref1;
                             }
                       else
                             {
                              $refd="0";
                            }
            /************ Insert into refs table and take inserted id **************/
                            if($oldcrereferenceno)
                                $refd=$oldcrereferenceno;
                            
                            $refdata=array(
                                    'type'=>'5',
                                     'reference'=>$refd
                                   );
                                  $this->db->insert('refs', $refdata);
                                  $refid1=$this->db->insert_id() ;
             /************ Insert into placing slip refs table  **************/
                          $refpdata=array(
                                      'id'=>$refid1,
                                      'pslip_id'=>$cancellationid,
                                       'type'=>'cancellation'
                               );
                              $this->db->insert('placing_slip_refs', $refpdata);


                              $fyear=$this->accountingsmodel->fiscalyear();

                       $insValuesA1=array(
                                  'trans_no'=>$refid1,
                                  'user'=>$_SESSION['userid'],
                                  'date_trans'=>$tdate,

                                  'fiscal_year'=>$fyear,
                                  'gl_date'=>$tdate

                             );
                         $this->db->insert('audit_trail', $insValuesA1);






                $trans_date=$acctype_val= $this->customermodel->getSingleFieldValue('placing_slip','quotation_date','id',$plid);
                $insValues=array(

                                    'type_no'=>$refid1,
                                    'tran_date'=>$tdate,
                                    'account'=>$cust_acc,
                                    'memo'=>'',
                                    'amount'=>-number_format($preamt,2, '.', '')

                                 );
                  $this->db->insert('gl_trans', $insValues);


                   /********************************insert into customer transaction table*************************************************/
                $insValue=array(

                                    'type_no'=>$refid1,
                                    'tran_date'=>$tdate,
                                    'account'=>$cust_acc,
                                    'memo'=>'',
                                    'amount'=>-number_format($preamt,2, '.', ''),
                                    'alloc'=>'',
                                    'pid'=>$plid

                                 );
                   $this->db->insert('customer_trans', $insValue);

         /*******************************insert brokerage amount into cover acc in gl trans*************************************/
                  $brokamount=$brokerage;

                  $totbrotamt= $_POST['premiumchange'] *($brokamount/100);

                  $coveracc= $this->accountingsmodel->get_mainaccount_setting('cover_start');
                  $covrmain_acc=$coveracc+$coverid;


                  $trans_date=$acctype_val= $this->customermodel->getSingleFieldValue('placing_slip','quotation_date','id',$plid);
                  $insValues1=array(

                                    'type_no'=>$refid1,
                                    'tran_date'=>$tdate,
                                    'account'=>$covrmain_acc,
                                    'memo'=>'',
                                    'amount'=>number_format($totbrotamt,2, '.', '')

                                );
                  $this->db->insert('gl_trans', $insValues1);
        /****************************************insert insurer amount into credit as company acc*********************************************/
                  $insurer= 100 - $brokerage;
                  $compamt= $premiumchange * ($insurer/100) ;


                $comp_acc=$this->accountingsmodel->get_acc_id('insurance_company','company_code',$inscompid);

                $insValuesC=array(
                                  'type_no'=>$refid1,
                                  'tran_date'=>$tdate,
                                  'account'=>$comp_acc,
                                  'memo'=>'',
                                  'amount'=>number_format($compamt,2, '.', '')

                  );

            $insValuesC1=array(
                                  'type_no'=>$refid1,
                                  'tran_date'=>$tdate,
                                  'account'=>$comp_acc,
                                  'memo'=>'',
                                  'amount'=>number_format($compamt,2, '.', ''),
                                  'pid'=>$plid

                  );
                $this->db->insert('gl_trans', $insValuesC);
                $this->db->insert('company_trans', $insValuesC1);






	
        if ($this->db->trans_status() === FALSE)
	     {
		 $this->db->trans_rollback();
                 return 0;

	     }
	     else
	     {
		 $this->db->trans_commit();
                 return 1;

	     }
    }
      function insertpolicynote()
         {
        $query=$this->db->query("insert into policynote values('','$_POST[date]','$_POST[quotation_ref]','$_POST[policyno]','$_POST[recdate]')") ;
         $policynoteid=mysql_insert_id();

       
        if($query>0)
       {
           return 1;
       }
       else
           {
           return 0;
       }
    }
    function updatepolicynote($table)
    {
      $this->db->query("update $table set quotation_ref='$_POST[quotation_ref]', policyno = '$_POST[policyno]', date = '$_POST[date]', recdate = '$_POST[recdate]' where id='$_POST[policynoteid]'");
	//  echo $query;
	  

    }
    function updaterenewal($table)
    {
      $query=$this->db->query("update $table set policyid='$_POST[policyid]'  where id='$_POST[renewalid]'");
	  
	

    }
    function updateendorsement($table)
    {		
         
         
        $type = '';
        $endorsementid=$_POST['endorsementid'];
        $query=$this->db->query("delete from  $table where id='$endorsementid'");
        
        
        $reff_id=$this->documentsmodel->getendorsement_refs($endorsementid);
        $oldcrereferenceno=  $this->documentsmodel->getplacingslip_referenceno($endorsementid,5);
        $olddebreferenceno=  $this->documentsmodel->getplacingslip_referenceno($endorsementid,6);

             if(count($reff_id)!=0)
               {

                 foreach($reff_id as $refval)
                    {
                     $a=$refval['id'];
                     $this->db->query('delete from gl_trans where type_no ='.$a);
                     $this->db->query('delete from audit_trail where trans_no ='.$a);
                     $this->db->query('delete from refs where id ='.$a);

                    }

                      $this->db->where('pid', $endorsementid);
                      $this->db->delete('customer_trans');
                      $this->db->where('pid', $endorsementid);
                      $this->db->delete('agent_trans');
                      $this->db->where('pid', $endorsementid);
                      $this->db->delete('company_trans');

                      $this->db->query("delete from placing_slip_refs where pslip_id='.$endorsementid.'and type='endorsement'" );

                }

                $this->insertendorsement($oldcrereferenceno,$olddebreferenceno);

      
         

    }

    function addendpolicyno()
    {
        $endid=$_POST['endid'];
        $endpolicyno=$_POST['endpolicyno'];
        $query=$this->db->query("update endorsement set  endpolicyno = '$endpolicyno' where id='$endid'");
        return $query;

    }

     function adddeclcertno()
    {
        $decid=$_POST['decid'];
        $certno=$_POST['certno'];
        $query=$this->db->query("update declaration set  certificate_no = '$certno' where id='$decid'");
        return $query;

    }
    function addcanpolicyno()
    {
        $canid=$_POST['canid'];
        $canpolicyno=$_POST['canpolicyno'];
        $query=$this->db->query("update cancellation set  canpolicyno = '$canpolicyno' where id='$canid'");
        return $query;

    }
    function updatecancellation($table)
    {
			
        $type = '';
        $cancellationid=$_POST['cancellationid'];
        
        $this->db->query("delete from  $table where id='$cancellationid'");

        $reff_id=$this->documentsmodel->getcancellation_refs($cancellationid);
        
         $oldcrereferenceno=  $this->getcancellation_referenceno($payslip,5);
         $olddebreferenceno=  $this->getcancellation_referenceno($payslip,6);

             if(count($reff_id)!=0)
               {

                 foreach($reff_id as $refval)
                    {
                     $a=$refval['id'];
                     $this->db->query('delete from gl_trans where type_no ='.$a);
                     $this->db->query('delete from audit_trail where trans_no ='.$a);
                     $this->db->query('delete from refs where id ='.$a);

                    }

                      $this->db->where('pid', $cancellationid);
                      $this->db->delete('customer_trans');
                      $this->db->where('pid', $cancellationid);
                      $this->db->delete('agent_trans');
                      $this->db->where('pid', $cancellationid);
                      $this->db->delete('company_trans');

                      $this->db->query("delete from placing_slip_refs where pslip_id='.$cancellationid.'and type='cancellation'" );

                }

                $this->insertcancellation($oldcrereferenceno,$olddebreferenceno);



	
    }
function updatedeclaration()
    {
           $this->db->trans_begin();
           $input=0;
           $area=0;
           $combo=0;
          if(isset($_POST['estimatedturnover']))
          {
             $suminsuredtot= $_POST['estimatedturnover']; 
          }
          else
          {
             $suminsuredtot= $_POST['estimatedsuminsured'];    
          }
           if (isset($_POST['currencytype']))
               {
               $currencytype=$this->db->escape_str($_POST['currencytype']);
               }
               else
               {
                 $ctype=$this->mastermodel->getdatas('default_currency');
                                
                 $currencytype=$ctype[0]['currency_id'];
                    
               }

               if (!isset($_POST['brokerage']))
                {
                $_POST['brokerage']=0;
                }

                if(!isset($_POST['policyfees']))
                {
                    $_POST['policyfees']=0;
                }
          $declaration_id=$_POST['declarationid'];
          $created_date= date("Y-m-d H:i:s");
          $policyno=$_POST['policyid'];
           
             if(isset($_POST['totalsumtitle'])&& isset($_POST['total_sumtitle_amount']))
               {
            $total_sumtitle=$_POST['totalsumtitle'];
            $total_sumtitle_amount=$_POST['total_sumtitle_amount'];
               }
               else
               {
                  $total_sumtitle="";
                  $total_sumtitle_amount="";
               }
               if(isset($_POST['desctitle'])&& isset($_POST['suminsuredtitle']))
               {
                $desctitle=$this->db->escape_str($_POST['desctitle']);
                $suminsuredtitle=$this->db->escape_str($_POST['suminsuredtitle']);
               }
               else
               {
                $desctitle="Description";
                $suminsuredtitle="Sum Insured";
               }
         
          $declarationdata=array('template_header'=>$_POST['template_header'],
                                   'cover_id'=>$_POST['coverid'],
                                   'customer_id'=>$_POST['custid'],
                                   'quotation_ref'=>$_POST['quotation_ref'],
                                   'est_suminsured'=>$suminsuredtot,
                                   
                                   'certificate_no'=>$_POST['certificateno'],
                                   'policyid'=>$_POST['policyid'],
                                   'currencytype'=>$currencytype,
                                   'declarationdate'=>$_POST['declarationdate'],
                                   'createddate'=>$created_date,
                                   'suminsured_desctitle'=>$desctitle,
                                  'suminsured_sumtitle'=>$suminsuredtitle,
                                  'total_sumtitle'=>$total_sumtitle,
                                  'total_sumtitle_amount'=>$total_sumtitle_amount
                                   );
          $this->db->where('id',$declaration_id);
          $this->db->update('declaration',$declarationdata);
          
          
         // $declaration_id=mysql_insert_id();
          $fieldtype = $this->db->escape_str($_POST['fieldtype']);
           
           
          $labelname=$this->db->escape_str($_POST['labelname']);
     if(isset($_POST['labelname'])&& isset($_POST['fieldtype']))
        {
     $query=$this->db->query("delete from declaration_form where declaration_id='$declaration_id'");
       for($i=0;$i<sizeof($fieldtype);$i++)
               {      $value="";
                  if($fieldtype[$i]=='text')
                  {   $textOrder=$this->db->escape_str($_POST['textorder']);
                      $value=$this->db->escape_str($_POST['myInputs'. $textOrder[$input]]);
                      $input++;
         
                  }
                  else if($fieldtype[$i]=='textarea')
                   {    $areaOrder=$this->db->escape_str($_POST['areaorder']);
                       $value=$this->db->escape_str($_POST['myTextAreas'.$areaOrder[$area]]);
                       $area++;
                  }
                  else if($fieldtype[$i]=='combobox')
                  {
                   $comboOrder=$_POST['comboorder'];
                  
                        if(isset($_POST['myComboBox'.$comboOrder[$combo]]))
                             {
                           
                           $comboval=$_POST['myComboBox'.$comboOrder[$combo]];
                             if(is_array($comboval))
                             {
                             $value=implode('/',$comboval);
                               
                             }
                    
                   
                       
                             }
                             $combo++;
                  }
                  
                  $query=$this->db->query("insert into declaration_form (`declaration_id`,`labelname`,`value`,`fieldtype`) values('$declaration_id','$labelname[$i]','$value','$fieldtype[$i]')");                  
                  
                }
                
                
               
          }
          
          
         
             if(isset($_POST['section']) && isset($_POST['section_title']))
        {
           $section=$this->db->escape_str($_POST['section']);
           $section_title=$this->db->escape_str($_POST['section_title']);

  $query=$this->db->query("delete from declaration_section where declaration_id='$declaration_id'");
  $query=$this->db->query("delete from declaration_sum_insured where declaration_id='$declaration_id'");
           $secorder=0;
            $k=0;
     
            for($j=0;$j<sizeof($_POST['section']);$j++)
           {
               
                 if(isset($_POST["totamt".$j]))
                {
                $totsuminsured=$this->db->escape_str($_POST["totamt".$j]);
                }
                else
                {
                    $totsuminsured=0;
                }
              
               
       $query=$this->db->query("insert into declaration_section (`declaration_id`,`section`,`section_title`,`total_suminsured`) values('$declaration_id','$section[$j]','$section_title[$j]','$totsuminsured')");
       $section_id=mysql_insert_id();
        if(isset($_POST['desc']) && isset($_POST['sumamt']))
       {
            $amt=$this->db->escape_str($_POST['sumamt']);
            $desc=$this->db->escape_str($_POST['desc']);
          
             $f=0;
           
            while($f<= $_POST['seccount'.$secorder])
          
           {  
               
           
        $query=$this->db->query("insert into declaration_sum_insured (`declaration_id`,`description`,`amount`,`section_id`) values('$declaration_id','$desc[$k]','$amt[$k]','$section_id')");
           
            $k++;
            $f++;
           }
           $secorder++;
           }
           }
        }
        
        $query=$this->db->query("delete from declaration_rate where declaration_id='$declaration_id'");

if(isset($_POST['ratesection']) && isset($_POST['rate']))
    {
        $ratesection=$_POST['ratesection'];
        $rate=$_POST['rate'];

        for($n=0;$n<sizeof($_POST['ratesection']);$n++)
           {
       $tblValues=array('declaration_id'=>$declaration_id,'ratesection'=>$ratesection[$n],'rate'=>$rate[$n]);
       $this->db->insert('declaration_rate', $tblValues);


          }

    }
         
          if(isset($_POST['premium']))
         {
          
          
          $premiumdata=array('declaration_id'=>$declaration_id,
                             'premium'=>$_POST['premium'],
                            'brokerage'=>$_POST['brokerage'],
                            'policy_fees'=>$_POST['policyfees']);
          $this->db->where('id',$declaration_id);
          $this->db->update('declaration_premiumdetails',$premiumdata);
          
         }
         
          if(isset($_POST['modeofconveyance']))
         {
          $moof=$_POST['modeofconveyance'];
          $this->db->where('declaration_id',$declaration_id);
           $this->db->delete('declaration_mofconveyance');
          for($jj=0;$jj<sizeof($moof);$jj++)
           {
               
          $mdata=array('declaration_id'=>$declaration_id,
                             'description'=>$moof[$jj]);
          $this->db->insert('declaration_mofconveyance ',$mdata);
           }
          
          
         }
         
          if(isset($_POST['voyage']))
         {
          $voyage=$_POST['voyage'];
           $this->db->where('declaration_id',$declaration_id);
           $this->db->delete('declaration_voyage');

          for($jv=0;$jv<sizeof($voyage);$jv++)
           {
               
          $mdata=array('declaration_id'=>$declaration_id,
                             'description'=>$voyage[$jv]);
          $this->db->insert('declaration_voyage',$mdata);
           }
          
          
         }
         
         if(isset($_POST['frmprojdate'])&&isset($_POST['toprojdate']))
         {
          
          
          $premdata=array('declaration_id'=>$declaration_id,
                             'fromdate'=>$_POST['frmprojdate'],
                             'todate'=>$_POST['toprojdate']);
           $this->db->where('id',$declaration_id);
          $this->db->update('declaration_project_period',$premdata);
          
         }
        
        
          if ($this->db->trans_status() === FALSE)
	     {
		 $this->db->trans_rollback();
		 return 0;
	     }
	     else
	     {
		 $this->db->trans_commit();
		 return $declaration_id;
	     }


    
          
          
     }

   
     function get_data_searchnew($table,$id="",$field="",$count="",$page="",$per_page="")
    {

        
         $staffids=$this->mastermodel->tracestaff();
                    $staffids=implode(',', $staffids);



              if ($this->mastermodel->ret_private())
              {
                 
                  $where=' where s.staffid in ('.$staffids.') and';
              }
              else
              {
                 
                   $where=' where ';
              }

              if($table=='cancellation')
                         $get='canrefno';
                     elseif($table=='declaration')
                         $get='certificate_refno';
                     elseif($table=='endorsement')
                         $get='endrefno';
                     else
                          $get='renrefno';

         $serption=explode("~",$field);

             $field=$serption[0];

    if($page)
        {
        $start = ($page-1)*$per_page;
        }
        else
        {
        $start=0;
         }
         if($per_page=="")
             $limit = "";
         else
             $limit = "limit  $start,$per_page";

       
         if($field == "policyno")
        {

                     
                      
            
             if($table=='policynote')
                 $sql= "select distinct p.id,p.quotation_ref,p.recdate,p.policyno from policynote  p  join broking_slip b on p.quotation_ref=b.quotation_ref join staffcustomer s on b.customer_id=s.customerid  $where policyno like '".$id."%' $limit";
             else
             {              
            $sql = "select distinct e.id,e.$get,e.policyid,e.mode from $table e join policynote p on e.policyid=p.id  join broking_slip b on p.quotation_ref=b.quotation_ref join staffcustomer s on b.customer_id=s.customerid $where p.policyno like '".$id."%' $limit";
             }
        }
        else if($field == "covername")
        {

             if($table=='policynote')
                 $sql= "select distinct p.id,p.quotation_ref,p.recdate,p.policyno from policynote  p  join broking_slip b on p.quotation_ref=b.quotation_ref join staffcustomer s on b.customer_id=s.customerid join covers cv on cv.id=b.cover_id  $where cv.cover like '".$id."%' $limit";
             else
             {
             $sql = "select distinct e.id,e.$get,e.policyid,e.mode from $table e join policynote p on e.policyid=p.id  join broking_slip b on p.quotation_ref=b.quotation_ref join staffcustomer s on b.customer_id=s.customerid join covers cv on cv.id=b.cover_id  $where cv.cover like '".$id."%' $limit";
             }

        }


          else if($field == "customername")
        {

             

                 if($table=='policynote')
                 $sql = "select distinct p.id,p.quotation_ref,p.recdate,p.policyno from policynote  p join broking_slip b on p.quotation_ref=b.quotation_ref join staffcustomer s on b.customer_id=s.customerid LEFT JOIN customer_corp_info as cc ON cc.customer_id = s.customerid LEFT JOIN customer_personal_ind as cp ON cp.customer_id = s.customerid $where  (cp.insured like '".$id."%' or cc.insured like '".$id."%')  $limit";
             else
             {
                 $sql = "select distinct e.id,e.$get,e.policyid,e.mode from $table e join policynote p on e.policyid=p.id join broking_slip b on p.quotation_ref=b.quotation_ref join staffcustomer s on b.customer_id=s.customerid LEFT JOIN customer_corp_info as cc ON cc.customer_id = s.customerid LEFT JOIN customer_personal_ind as cp ON cp.customer_id = s.customerid $where  (cp.insured like '".$id."%' or cc.insured like '".$id."%')  $limit";
             }


        }
        else
            {



             if($table=='policynote')
                 $sql= "select distinct p.id,p.quotation_ref,p.recdate,p.policyno from policynote  p  join broking_slip b on p.quotation_ref=b.quotation_ref join staffcustomer s on b.customer_id=s.customerid  $where p.$field like '".$id."%' $limit";
             else
             {
             $sql = "select distinct e.id,e.$get,e.policyid from $table e join policynote p on e.policyid=p.id  join broking_slip b on p.quotation_ref=b.quotation_ref join staffcustomer s on b.customer_id=s.customerid $where e.$field like '".$id."%' $limit";
             }
       // $sql = "select * from $table where $field like '".$id."%' $limit";
       //echo $sql;
             }
       if($count=='true')
       {
            $res = mysql_query($sql);
           return mysql_num_rows($res);
       }
       else
       {

        return $sql;
       }


    }
    
    
    function get_data_search($table,$id,$field,$page="",$per_page="")
    {
       // echo $field;
       // echo $id;
      //  die();
         $serption=explode("~",$field);
         
             $field=$serption[0];
         
    if($page)
        {
        $start = ($page-1)*$per_page;
        }
        else
        {
        $start=0;
         }
         if($per_page=="")
             $limit = "";
         else
             $limit = "limit  $start,$per_page";
        if($field == "policyid")
        {
             $qry = "select id from policynote where policyno like '".$id."%'";
             $res = mysql_query($qry);
             $ro = mysql_fetch_array($res);
             $sql = "select * from $table where $field in ($qry) $limit";
             
        }
        else if($field == "covername")
        {
            
            $qry = "select id from covers where cover like '".$id."%'";
         
              $qry1 = "select quotation_ref from broking_slip where cover_id in ($qry)";

if($serption[1] != "")
             {
             $sql1 = "select id from policynote where quotation_ref in ($qry1) ";
             if($table!="policynote")
             $sql = "select * from $table where policyid in ($sql1) $limit";
             else
                  $sql = "select * from $table where id in ($sql1) $limit";
           //  echo $sql;
            // die();
     }
     else
             $sql = "select * from $table where quotation_ref in ($qry1) $limit";
         
        

        }
          else if($field == "customername")
        {

             $query1 = "select customer_id as id from customer_personal_ind where insured like '" . $id . "%'";
		    
	     $query2 ="select customer_id as id from customer_corp_info where insured like '" . $id . "%'";
		    

              $qry1 = "select quotation_ref from broking_slip where customer_id in ($query1) or customer_id in ($query2)";
              if($serption[1] != "")
             {
             $sql1 = "select id from policynote where quotation_ref in ($qry1) ";
             if($table!="policynote")
             $sql = "select * from $table where policyid in ($sql1) $limit";
             else
                  $sql = "select * from $table where id in ($sql1) $limit";
     }
     else

             $sql = "select * from $table where quotation_ref in ($qry1) $limit";


        }
        else
            {
        $sql = "select * from $table where $field like '".$id."%' $limit";
        
             }
        //echo $sql;
        return $sql;
           

    }
     
     function quotationlist($searchoption="",$searchtext="")
     {

                   $data=array();
                if($searchoption!='insured')
                {
                    $serption=explode("~",$searchoption);
                    $qry="select " . $serption[1] . " from " . $serption[0] . " where ". $serption[2] . " like '%" . $searchtext . "%'";
                   
                    $Q=$this->db->query($qry);
                    $rows=$Q->result_array();
                    if($serption[0]=="customers")
                    {
                         $serption[1]='customer_id';
                         $val='id';
                    }
                    else
                    {
                         $val=$serption[1];
                    }
                }
                else
                {
                    $qry="select customer_id from customer_personal_ind where insured like '%" . $searchtext . "%'";
                    $qry1="select customer_id from customer_corp_info where insured like '%" . $searchtext . "%'";
                    $Q=$this->db->query($qry);
                    $tmprow=$Q->result_array();
                    foreach ($tmprow as $r1)
                    {
                        $rows[] = $r1;
                    }

                    $Q=$this->db->query($qry1);
                    $tmprow=$Q->result_array();
                    foreach ($tmprow as $r1)
                    {
                        $rows[] = $r1;
                    }

                    $serption[1]='customer_id';
                    $val=$serption[1];
                }

		foreach ($rows as $rw)
		{
        

                    $newRow=$this->mastermodel->get_data('placing_slip',$rw[$val],$serption[1]);
                    if (count($newRow) > 0) {
                    foreach ($newRow as $r)
                    {
                       $info['id']=$r['id'];
                       $info['cover_id']=$r['cover_id'];
                       
                       $info['quotation_date']=$r['quotation_date'];
                      
                       $info['qref']=$r['quotation_ref'];
                    
                       $cname=$this->customermodel->getSingleFieldValue('customers','type','id',$r['customer_id']);
                          if($cname=='Individual')
                          {
                            $info['cname']=$this->customermodel->getSingleFieldValue('customer_personal_ind','insured','customer_id',$r['customer_id']);

                          }
                          else
                          {
                             $info['cname']=$this->customermodel->getSingleFieldValue('customer_corp_info','insured','customer_id',$r['customer_id']);
                          }
                      $info['ccode']=$this->customermodel->getSingleFieldValue('customers','code','id',$r['customer_id']);
                      $info['cover']=$this->customermodel->getSingleFieldValue('covers','cover','id',$r['cover_id']);

                    }
                    $data[]=$info;
                    
                    $info="";
		}
                }
		return $data;

     }
     function get_cover_policy($policyid)
     {
            $sql = "select cover from covers join broking_slip join policynote on policynote.quotation_ref =  broking_slip.quotation_ref and broking_slip.cover_id = covers.id and policynote.id = '$policyid'";
            $res = mysql_query($sql);
            $row =  mysql_fetch_array($res);
            return $row['cover'];
     }
      function get_customer_policy($policyid)
     {
            $sql = "select type,customer_id from customers join broking_slip join policynote on policynote.quotation_ref =  broking_slip.quotation_ref and broking_slip.customer_id = customers.id and policynote.id = '$policyid'";
            $res = mysql_query($sql);
            
            $row =  mysql_fetch_array($res);
            if ($row['type'] == "Individual")
                {
                    $table = 'customer_personal_ind';
            }
            else {
                 $table = 'customer_corp_info';
            }
            return $this->policiesmodel->getSingleFieldValue($table,'insured','customer_id',$row['customer_id']);


     }
     function getSingleFieldValue($table,$field,$condition,$value)
       {
	   $data=$this->db->query("select ". $field. " from ". $table . " where ". $condition . "='". $value ."'");
	   $result= $data->row();
	   if($result)
	   {
	      return $result->$field;
	   }
	   else
	   {
	      return "";
	   }
       }
       
    function policylist($searchoption="",$searchtext="")
     {
                   $data=array();

                if($searchoption!='insured')
                {
                    $serption=explode("~",$searchoption);
                    $qry="select " . $serption[1] . " from " . $serption[0] . " where ". $serption[2] . " like '%" . $searchtext . "%'";
             //  echo $qry;
                    $Q=$this->db->query($qry);
                    $rows=$Q->result_array();
                    if($serption[0]=="customers")
                    {
                         $serption[1]='customer_id';
                         $val='id';
                    }
                    else if($serption[0]=="covers")
                    {
                         $serption[1]='cover_id';
                         $val='id';

                    }
                    else
                    {
                         $val=$serption[1];

                      //  echo $val;
                    }
                }
                else
                {
                    $qry="select customer_id from customer_personal_ind where insured like '%" . $searchtext . "%'";
                    $qry1="select customer_id from customer_corp_info where insured like '%" . $searchtext . "%'";
                    $Q=$this->db->query($qry);
                    $tmprow=$Q->result_array();
                    foreach ($tmprow as $r1)
                    {
                        $rows[] = $r1;
                    }

                    $Q=$this->db->query($qry1);
                    $tmprow=$Q->result_array();
                    foreach ($tmprow as $r1)
                    {
                        $rows[] = $r1;
                    }

                    $serption[1]='customer_id';
                    $val=$serption[1];
                }

		foreach ($rows as $rw)
		{
                   $query = "select * from broking_slip where $serption[1] = '$rw[$val]' and quotation_ref in (select quotation_ref from policynote)";
                    $Q=$this->db->query($query);
                   // echo $query."<br>";
                   $newRow= $Q->result_array();
               // echo count($newRow);
                      //$newRow=$this->mastermodel->get_data('broking_slip',$rw[$val],$serption[1]);
                     if (count($newRow) > 0) {
                    foreach ($newRow as $r)
                    {
                    //  if(($info['poliid']=$this->customermodel->getSingleFieldValue('policynote','id','quotation_ref',$r['quotation_ref']))!="")
                    //          {
                       $info['id']=$r['id'];
                       $info['cover_id']=$r['cover_id'];
                       $info['template_header']=$r['template_header'];
                       $info['quotation_date']=$r['quotation_date'];
                       $info['qref']=$r['quotation_ref'];
                      // echo $info['qref']."shan<br>";
                       $cname=$this->customermodel->getSingleFieldValue('customers','type','id',$r['customer_id']);
                          if($cname=='Individual')
                          {
                            $info['cname']=$this->customermodel->getSingleFieldValue('customer_personal_ind','insured','customer_id',$r['customer_id']);

                          }
                          else
                          {
                             $info['cname']=$this->customermodel->getSingleFieldValue('customer_corp_info','insured','customer_id',$r['customer_id']);
                          }
                      $info['ccode']=$this->customermodel->getSingleFieldValue('customers','code','id',$r['customer_id']);
                      $info['cover']=$this->customermodel->getSingleFieldValue('covers','cover','id',$r['cover_id']);


               $info['polino']=$this->customermodel->getSingleFieldValue('policynote','policyno','quotation_ref',$r['quotation_ref']);
               $info['poliid']=$this->customermodel->getSingleFieldValue('policynote','id','quotation_ref',$r['quotation_ref']);

                    $data[]=$info;
                    $info="";
                    }
		}
                }
		return $data;

     }
     function policylist1($searchoption="",$searchtext="")
     {
                   $data=array();
                  
           $staffids=$this->mastermodel->tracestaff();
           $staffids=implode(',', $staffids);
          // echo $staffids;
                if($this->mastermodel->ret_private())
                    {

                   $condition=' p  join broking_slip b on p.quotation_ref=b.quotation_ref join staffcustomer s on b.customer_id=s.customerid  where s.staffid in ('.$staffids.') and';
                    $condition1='b join staffcustomer s on b.customer_id=s.customerid  where s.staffid in ('.$staffids.') and';
                    }
                    else
                    {

                     $condition="where";
                      $condition1="where";

                    }

                   if($searchoption!='insured')
                {
                    $serption=explode("~",$searchoption);
                    $qry="select  * from " . $serption[0]. " " . $condition." ". $serption[2] . " like '%" . $searchtext . "%'";



                    echo $qry;
                    $Q=$this->db->query($qry);
                    $rows=$Q->result_array();
                    if($serption[0]=="customers")
                    {
                         $serption[1]='customer_id';
                         $val='id';
                    }
                    else if($serption[0]=="covers")
                    {
                         $serption[1]='cover_id';
                         $val='id';
                      
                    }
                    else
                    {
                         $val=$serption[1];
                   
                      //  echo $val;
                    }
                }
                else
                {
                    $qry="select customer_id from customer_personal_ind where insured like '%" . $searchtext . "%'";
                    $qry1="select customer_id from customer_corp_info where insured like '%" . $searchtext . "%'";
                    $Q=$this->db->query($qry);
                    $tmprow=$Q->result_array();
                    foreach ($tmprow as $r1)
                    {
                        $rows[] = $r1;
                    }

                    $Q=$this->db->query($qry1);
                    $tmprow=$Q->result_array();
                    foreach ($tmprow as $r1)
                    {
                        $rows[] = $r1;
                    }

                    $serption[1]='customer_id';
                    $val=$serption[1];
                }

		foreach ($rows as $rw)
		{
                   $query = "select * from broking_slip  $condition1  $serption[1] = '$rw[$val]' and quotation_ref in (select quotation_ref from policynote)";
                    $Q=$this->db->query($query);
                   // echo $query."<br>";
                   $newRow= $Q->result_array();
               // echo count($newRow);
                      //$newRow=$this->mastermodel->get_data('broking_slip',$rw[$val],$serption[1]);
                     if (count($newRow) > 0) {
                    foreach ($newRow as $r)
                    {
                    //  if(($info['poliid']=$this->customermodel->getSingleFieldValue('policynote','id','quotation_ref',$r['quotation_ref']))!="")
                    //          {
                       $info['id']=$r['id'];
                       $info['cover_id']=$r['cover_id'];
                       $info['template_header']=$r['template_header'];
                       $info['quotation_date']=$r['quotation_date'];
                       $info['qref']=$r['quotation_ref'];
                      // echo $info['qref']."shan<br>";
                       $cname=$this->customermodel->getSingleFieldValue('customers','type','id',$r['customer_id']);
                          if($cname=='Individual')
                          {
                            $info['cname']=$this->customermodel->getSingleFieldValue('customer_personal_ind','insured','customer_id',$r['customer_id']);

                          }
                          else
                          {
                             $info['cname']=$this->customermodel->getSingleFieldValue('customer_corp_info','insured','customer_id',$r['customer_id']);
                          }
                      $info['ccode']=$this->customermodel->getSingleFieldValue('customers','code','id',$r['customer_id']);
                      $info['cover']=$this->customermodel->getSingleFieldValue('covers','cover','id',$r['cover_id']);
                     
                      
               $info['polino']=$this->customermodel->getSingleFieldValue('policynote','policyno','quotation_ref',$r['quotation_ref']);
               $info['poliid']=$this->customermodel->getSingleFieldValue('policynote','id','quotation_ref',$r['quotation_ref']);
                   
                    $data[]=$info;
                    $info="";
                    }
		}
                }
		return $data;

     }
 function policylistdec($searchoption="",$searchtext="")
     {
                   $data=array();
                  
                if($searchoption!='insured')
                {
                    $serption=explode("~",$searchoption);
                    $qry="select " . $serption[1] . " from " . $serption[0] . " where ". $serption[2] . " like '%" . $searchtext . "%'";
              // echo $qry;
                    $Q=$this->db->query($qry);
                    $rows=$Q->result_array();
                    if($serption[0]=="customers")
                    {
                         $serption[1]='customer_id';
                         $val='id';
                    }
                     else if($serption[0]=="covers")
                    {
                         $serption[1]='cover_id';
                         $val='id';

                    }
                    else
                    {
                         $val=$serption[1];
                   
                        
                    }
                }
                else
                {
                    $qry="select customer_id from customer_personal_ind where insured like '%" . $searchtext . "%'";
                    $qry1="select customer_id from customer_corp_info where insured like '%" . $searchtext . "%'";
                    $Q=$this->db->query($qry);
                    $tmprow=$Q->result_array();
                    foreach ($tmprow as $r1)
                    {
                        $rows[] = $r1;
                    }

                    $Q=$this->db->query($qry1);
                    $tmprow=$Q->result_array();
                    foreach ($tmprow as $r1)
                    {
                        $rows[] = $r1;
                    }

                    $serption[1]='customer_id';
                    $val=$serption[1];
                }

		foreach ($rows as $rw)
		{
                 
                    $sql = "select * from placing_slip where $serption[1] = '$rw[$val]' and cover_id in(select cover_id from template where template_type = '3') and  quotation_ref in (select quotation_ref from policynote)";
                    //echo $sql;
          //die();
                         
                     $Q21=$this->db->query($sql);
                     $newRow=$Q21->result_array();
                    
                     if (count($newRow) > 0) {
                    foreach ($newRow as $r)
                    {
                        
                       $info['id']=$r['id'];
                       $info['cover_id']=$r['cover_id'];
                       $info['template_header']=$r['template_header'];
                       $info['quotation_date']=$r['quotation_date'];
                       $info['qref']=$r['quotation_ref'];
                       $info['custid']=$r['customer_id'];
                       $cname=$this->customermodel->getSingleFieldValue('customers','type','id',$r['customer_id']);
                          if( $cname=='Individual')
                          {
                            $info['cname']=$this->customermodel->getSingleFieldValue('customer_personal_ind','insured','customer_id',$r['customer_id']);

                          }
                          else
                          {
                             $info['cname']=$this->customermodel->getSingleFieldValue('customer_corp_info','insured','customer_id',$r['customer_id']);
                          }
                      $info['ccode']=$this->customermodel->getSingleFieldValue('customers','code','id',$r['customer_id']);
                      $info['cover']=$this->customermodel->getSingleFieldValue('covers','cover','id',$r['cover_id']);
                     
                      
               $info['polino']=$this->customermodel->getSingleFieldValue('policynote','policyno','quotation_ref',$r['quotation_ref']);
               $info['poliid']=$this->customermodel->getSingleFieldValue('policynote','id','quotation_ref',$r['quotation_ref']);
               $placingslip_id= $this->customermodel->getSingleFieldValue('placing_slip','id','quotation_ref',$r['quotation_ref']);
               $info['fdatepolicy']=$this->customermodel->getSingleFieldValue('placing_slip_premiumdetails','fromdate','placing_slip_id',$placingslip_id);
               $info['todatepolicy']=$this->customermodel->getSingleFieldValue('placing_slip_premiumdetails','todate','placing_slip_id',$placingslip_id);
               
               $info['insurance_company_id']=$this->customermodel->getSingleFieldValue('placing_slip_premiumdetails','insurance_company_id','placing_slip_id',$placingslip_id);
               $info['brokerage']=$this->customermodel->getSingleFieldValue('placing_slip_premiumdetails','brokerage','placing_slip_id',$placingslip_id);
              
               $sqry=$this->db->query("select sum(amount) as amt from placing_slip_sum_insured where placing_slip_id='$placingslip_id'");
                   if ($sqry->num_rows() > 0)
                         {
                         $row = $sqry->row();
                         $info['estsuminsured']=$row->amt;
                         }
                          $data[]=$info;
                          $info="";
                    }
                    }
                   
		}
                
		return $data;

     }
     
     
     function adddeclaration()
     {
         $this->db->trans_begin();//MANUAL TRANSACTION BEGIN HERE
           $input=0;
           $area=0;
           $combo=0;
          if(isset($_POST['estimatedturnover']))
          {
             $suminsuredtot= $_POST['estimatedturnover']; 
          }
          else if(isset($_POST['estimatedsuminsured']))
          {
             $suminsuredtot= $_POST['estimatedsuminsured'];    
          }
          else
          {
              $suminsuredtot=0;
          }
           if (isset($_POST['currencytype']))
               {
               $currencytype=$this->db->escape_str($_POST['currencytype']);
               }
               else
               {
                 $ctype=$this->mastermodel->getdatas('default_currency');
                                
                 $currencytype=$ctype[0]['currency_id'];
                    
               }

               if(isset($_POST['desctitle'])&& isset($_POST['suminsuredtitle']))
               {
                $desctitle=$this->db->escape_str($_POST['desctitle']);
                $suminsuredtitle=$this->db->escape_str($_POST['suminsuredtitle']);
               }
               else
               {
                $desctitle="Description";
                $suminsuredtitle="Sum Insured";
               }
                if(isset($_POST['totalsumtitle'])&& isset($_POST['total_sumtitle_amount']))
               {
            $total_sumtitle=$_POST['totalsumtitle'];
            $total_sumtitle_amount=$_POST['total_sumtitle_amount'];
               }
               else
               {
                 $total_sumtitle="";
                 $total_sumtitle_amount="";
               }
          $created_date= date("Y-m-d H:i:s");
          $policyno=$_POST['policyno'];
          $policyid=$_POST['policyid'];
          
          $maxid=$this->customermodel->getSingleFieldValue('declaration ','max(id)','cover_id',$_POST['coverid']);
          $maxid=$maxid+1;
          $cover_id=$_POST['coverid'];
          $cyear=date('Y');

         $decreno = $_POST['quotation_ref'];
         $decno = $this->policiesmodel->get_cover_num_policy($policyid,'declaration');
         $decreno .= "/C".($decno+1);


        
         
          $declarationdata=array('template_header'=>$_POST['template_header'],
                                   'cover_id'=>$_POST['coverid'],
                                   'customer_id'=>$_POST['custid'],
                                   'quotation_ref'=>$_POST['quotation_ref'],
                                   'est_suminsured'=>$suminsuredtot,
                                   'certificate_refno'=>$decreno,
                                   'certificate_no'=>$_POST['certificateno'],
                                   'policyid'=>$_POST['policyid'],
                                   'currencytype'=>$currencytype,
                                   'declarationdate'=>$_POST['declarationdate'],
                                   'createddate'=>$created_date  ,
                                    'suminsured_desctitle'=>$desctitle,
                                    'suminsured_sumtitle'=>$suminsuredtitle,
                                    'total_sumtitle'=>$total_sumtitle,
                                   'total_sumtitle_amount'=>$total_sumtitle_amount
                                   );
          $this->db->insert('declaration',$declarationdata);
          
          
          $declaration_id=mysql_insert_id();
          $fieldtype = $this->db->escape_str($_POST['fieldtype']);
           
           
          $labelname=$this->db->escape_str($_POST['labelname']);
     if(isset($_POST['labelname'])&& isset($_POST['fieldtype']))
        {
       for($i=0;$i<sizeof($fieldtype);$i++)
               {      $value="";
                  if($fieldtype[$i]=='text')
                  {   $textOrder=$this->db->escape_str($_POST['textorder']);
                      $value=$this->db->escape_str($_POST['myInputs'. $textOrder[$input]]);
                      $input++;
         
                  }
                  else if($fieldtype[$i]=='textarea')
                   {    $areaOrder=$this->db->escape_str($_POST['areaorder']);
                       $value=$this->db->escape_str($_POST['myTextAreas'.$areaOrder[$area]]);
                       $area++;
                  }
                  else if($fieldtype[$i]=='combobox')
                  {
                   $comboOrder=$_POST['comboorder'];
                  
                        if(isset($_POST['myComboBox'.$comboOrder[$combo]]))
                             {
                           
                           $comboval=$_POST['myComboBox'.$comboOrder[$combo]];
                             if(is_array($comboval))
                             {
                             $value=implode('/',$comboval);
                               
                             }
                    
                   
                       
                             }
                             $combo++;
                  }
                  
                  $query=$this->db->query("insert into declaration_form (`declaration_id`,`labelname`,`value`,`fieldtype`) values('$declaration_id','$labelname[$i]','$value','$fieldtype[$i]')");                  
                  
                }
                
                
               
          }
          
          
         
             if(isset($_POST['section']) && isset($_POST['section_title']))
        {
           $section=$this->db->escape_str($_POST['section']);
           $section_title=$this->db->escape_str($_POST['section_title']);


          
           $secorder=0;
            $k=0;
     
            for($j=0;$j<sizeof($_POST['section']);$j++)
           {
               
               if(isset($_POST["totamt".$j]))
                {
                $totsuminsured=$this->db->escape_str($_POST["totamt".$j]);
                }
                else
                {
                    $totsuminsured=0;
                }
              
               
       $query=$this->db->query("insert into declaration_section (`declaration_id`,`section`,`section_title`,`total_suminsured`) values('$declaration_id','$section[$j]','$section_title[$j]','$totsuminsured')");
       $section_id=mysql_insert_id();
        if(isset($_POST['desc']) && isset($_POST['sumamt']))
     {
            $amt=$this->db->escape_str($_POST['sumamt']);
            $desc=$this->db->escape_str($_POST['desc']);
          
             $f=0;
           
            while($f<= $_POST['seccount'.$secorder])
          
           {  
               
           
        $query=$this->db->query("insert into declaration_sum_insured (`declaration_id`,`description`,`amount`,`section_id`) values('$declaration_id','$desc[$k]','$amt[$k]','$section_id')");
           
            $k++;
            $f++;
           }
           $secorder++;
     }
           }
        }
        
           if (!isset($_POST['brokerage']))
                {
                $_POST['brokerage']=0;
                }

                if(!isset($_POST['policyfees']))
                {
                    $_POST['policyfees']=0;
                }
         
          if(isset($_POST['premium']))
         {
          
          
       
          $tblValues=array('declaration_id'=>$declaration_id,
                'premium'=>$_POST['premium'],

                'brokerage'=>$_POST['brokerage'],
                'policy_fees'=>$_POST['policyfees'],
                'insurance_company_id'=>$_POST['insurance_company_id']);
                $this->db->insert('declaration_premiumdetails', $tblValues);
          
         }
         
          if(isset($_POST['modeofconveyance']))
         {
          $moof=$_POST['modeofconveyance'];
          for($jj=0;$jj<sizeof($moof);$jj++)
           {
               
          $mdata=array('declaration_id'=>$declaration_id,
                             'description'=>$moof[$jj]);
          $this->db->insert('declaration_mofconveyance ',$mdata);
           }
          
          
         }
         
          if(isset($_POST['voyage']))
         {
          $voyage=$_POST['voyage'];
          for($jv=0;$jv<sizeof($voyage);$jv++)
           {
               
          $mdata=array('declaration_id'=>$declaration_id,
                             'description'=>$voyage[$jv]);
        
          $this->db->insert('declaration_voyage',$mdata);
           }
          
          
         }
         
         if(isset($_POST['frmprojdate'])&&isset($_POST['toprojdate']))
         {
          
          
          $premdata=array('declaration_id'=>$declaration_id,
                             'fromdate'=>$this->mastermodel->convdatformat($_POST['frmprojdate']),
                             'todate'=>$this->mastermodel->convdatformat($_POST['toprojdate']));
          $this->db->insert('declaration_project_period',$premdata);
          
         }

          if ($this->db->trans_status() === FALSE)
	     {
		 $this->db->trans_rollback();
		 return 0;
	     }
	     else
	     {
		 $this->db->trans_commit();
		 return $declaration_id;
	     }
       
        
         
     }
     
     
     function insertdeclstatus()
     {
          
          
          
          $statusdata=array('declaration_id'=>$this->input->post('declarationid'),
               'statusdate'=>$this->input->post('statusdate'),
                'statusmode'=>$this->input->post('documentmode'),
                 'address'=>$this->input->post('documentaddress'),
                 
                   'statusresponse'=>$this->input->post('docresponse'),
                    'statusremarks'=>$this->input->post('docremarks')
                  
               );
          
          $this->db->insert('declaration_status',$statusdata);
     }
     
     function updatedeclstatus()
     {
          
          $data = array('status'=>$this->input->post('brokstatus'));
          $this->db->where('id', $this->input->post('declarationid'));
          $res=$this->db->update('declaration',$data);
          if($res==1)
          {
             echo  $this->input->post('brokstatus');
          }
          
     }
     
     
     function updatedeclarationstatus()
     {
         $statusdata=array('declaration_id'=>$this->input->post('declarationid'),
               'statusdate'=>$this->input->post('statusdate'),
                'statusmode'=>$this->input->post('documentmode'),
                 'address'=>$this->input->post('documentaddress'),
                  
                   'statusresponse'=>$this->input->post('docresponse'),
                    'statusremarks'=>$this->input->post('docremarks')
                  
               );
          $this->db->where('id',$this->input->post('declarationstatusid'));
          $this->db->update('declaration_status',$statusdata);  
     }
      function getinsurer($qref)
     {
        $sql = "select company_name from insurance_company inner join placing_slip_premiumdetails inner join placing_slip on insurance_company.id = placing_slip_premiumdetails.insurance_company_id and placing_slip_premiumdetails.placing_slip_id = placing_slip.id and placing_slip.quotation_ref='$qref'";
         $res = mysql_query($sql);
          $row =  mysql_fetch_array($res);
            return $row['company_name'];
     }
  function get_insurer_name($policyid)
     {
            $sql = "select company_name from insurance_company inner join placing_slip_premiumdetails inner join placing_slip join policynote on insurance_company.id = placing_slip_premiumdetails.insurance_company_id and placing_slip_premiumdetails.placing_slip_id = placing_slip.id and policynote.quotation_ref = placing_slip.quotation_ref and policynote.id = '$policyid'";
            $res = mysql_query($sql);
            $row =  mysql_fetch_array($res);
            return $row['company_name'];
     }

 function cancel_count($pid)
   {
        $this->db->select('*');
         $this->db->from('cancellation');
         $this->db->where('policyid', $pid);
         echo $this->db->count_all_results();

   }
  function check_cancel_date($pid,$date)
         {
            $res=$this->db->query("select * from policynote pn
                                    INNER JOIN placing_slip ps ON pn.quotation_ref=ps.quotation_ref
                                    INNER JOIN placing_slip_premiumdetails pspd ON ps.id=pspd.placing_slip_id
                                    where pn.id=$pid and '$date' between pspd.fromdate and pspd.todate");
//            echo "select * from policynote pn
//                                    INNER JOIN placing_slip ps ON pn.quotation_ref=ps.quotation_ref
//                                    INNER JOIN placing_slip_premiumdetails pspd ON ps.id=pspd.placing_slip_id
//                                    where pn.id=$pid and '$date' between pspd.fromdate and pspd.todate";
//                           
            return $res->num_rows();
         }

         function getplacingslipid($policyid)
         {
          $res=$this->db->query(" SELECT pl.id as plid from placing_slip pl join policynote pn on pn.quotation_ref=pl.quotation_ref where pn.id='$policyid'");
          $result= $res->row();
          return $result->plid;
          }

          function getendorsementpayment($endid)
          {  $data=array();
             $res=$this->db->query("SELECT e.endrefno,e.endpolicyno,e.enddate,e.mode,e.premiumchange,e.policyfeeschange,e.paymentoptions,covers.cover,inc.company_name,
                     p.insurance_company_id,pn.policyno,p.brokerage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype
                     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 e on e.policyid=pn.id left join covers on covers.id=pl.cover_id left join
                     insurance_company inc on inc.id=p.insurance_company_id where e.id='$endid'");
               
             foreach ($res->result_array() as $row)
             {
                 $data[] = $row;
             }
            return $data;

         }


         function getcancellationpayment($cancid)
         {
            $data=array();
             $res=$this->db->query("SELECT c.canpolicyno,c.canrefno,c.date as cancdate,c.premiumchange,p.insurance_company_id,pn.policyno,covers.cover,inc.company_name,
              p.brokerage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype  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 c on c.policyid=pn.id left join covers on covers.id=pl.cover_id left join
                     insurance_company inc on inc.id=p.insurance_company_id where c.id='$cancid' ");

             foreach ($res->result_array() as $row)
             {
                 $data[] = $row;
             }
            return $data;
         }

          function getplacingpayment($plid)
         {
            $data=array();
             $res=$this->db->query("SELECT c.cover,p.premium,pl.customer_id,pl.cover_id,pl.quotation_date,p.insurance_company_id,pn.policyno,
              p.brokerage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype  from placing_slip pl
              join placing_slip_premiumdetails p on p.placing_slip_id=pl.id left join policynote pn on pn.quotation_ref=pl.quotation_ref
              join covers c on c.id=pl.cover_id where  pl.id='$plid'");

           
             foreach ($res->result_array() as $row)
             {
                 $data[] = $row;
             }
                return $data;
         }

         function getdeclarationpayment($plid)
         {
            $data=array();
             $res=$this->db->query("SELECT c.cover,p.premium,pl.customer_id,pl.cover_id,pl.certificate_refno,pl.declarationdate,p.insurance_company_id,pn.policyno,
              p.brokerage,pl.id as plid,pl.cover_id,pl.customer_id,pl.quotation_ref,pl.currencytype  from declaration pl
              join declaration_premiumdetails p on p.declaration_id=pl.id left join policynote pn on pn.quotation_ref=pl.quotation_ref
              join covers c on c.id=pl.cover_id where  pl.id='$plid'");
              

             foreach ($res->result_array() as $row)
             {
                 $data[] = $row;
             }
                return $data;
         }


         function checkpremium($policyid)
         {
          $res=$this->db->query(" SELECT pm.premium from placing_slip pl join placing_slip_premiumdetails pm on pm.placing_slip_id=pl.id join policynote pn on pn.quotation_ref=pl.quotation_ref where pn.id='$policyid'");
          $result= $res->row();
          return $result->premium;
         }
         function get_cover_num_policy($policyid,$table)
         {
             $sql = "select $table.id from $table join policynote on policynote.id = $table.policyid join broking_slip on broking_slip.quotation_ref = policynote.quotation_ref where broking_slip.cover_id in (select cover_id from broking_slip join policynote on policynote.quotation_ref=broking_slip.quotation_ref and policynote.id = '$policyid')";
            
             $res=mysql_query($sql);
             return mysql_num_rows($res);
             
         }
         function insertdeclarationpayment()
     {



         $pay_mode=$_POST['paymethod'];

         $this->db->trans_begin();
         $premiumdata = array(
               'insurance_company_id' => $_POST['insurance_company_id'],'payment_method' => $pay_mode,

            );

                $this->db->where('declaration_id', $_POST['declaration_id']);
                $this->db->update('declaration_premiumdetails', $premiumdata);
                $payslip=$_POST['declaration_id'];
                $this->db->where('declaration_id', $_POST['declaration_id']);
                $this->db->delete('declaration_stakeholders');
                //$reff_id=$this->customermodel->getSingleFieldValue('placing_slip_refs','id','pslip_id',$_POST['placing_slip_id']);
                 $reff_id=$this->getdeclaration_refs($payslip);
                
                 $oldcrereferenceno=  $this->getdeclaration_referenceno($payslip,5);
                 $olddebreferenceno=  $this->getdeclaration_referenceno($payslip,6);

             if(count($reff_id)!=0)
               {

                 foreach($reff_id as $refval)
                    {
                     $a=$refval['id'];
                     $this->db->query('delete from gl_trans where type_no ='.$a);
                     $this->db->query('delete from audit_trail where trans_no ='.$a);
                     $this->db->query('delete from refs where id ='.$a);

                    }

                      $this->db->where('pid', $payslip);
                      $this->db->delete('customer_trans');
                      $this->db->where('pid', $payslip);
                      $this->db->delete('agent_trans');
                      $this->db->where('pid', $payslip);
                      $this->db->delete('company_trans');

                      $this->db->query("delete from placing_slip_refs where pslip_id='.$payslip.'and type='declaration'" );

                }

                /*********************** Add Agent and their commission percentage*****************************/
              $tot_brokerage=$_POST['brokerage'];
              $tot_brokerage_cover=$tot_brokerage;
             
              $tdate=date('Y-m-d');

               if (isset($_POST['staffid']) && isset($_POST['brockamt']))
                 {


              $tra_cod_c= $this->customermodel->getSingleFieldValue('transaction_code','code','trans',5);
                        if($tra_cod_c!='')
                             {
                               $next_ref= $this->accountingsmodel->get_reference_code(5);
                              $refc=$tra_cod_c.'/'.date('Y').'/'.$next_ref;
                             }
                       else
                             {
                              $refc="0";
                             }
            /************ Insert into refs table and take inserted id **************/
                              if($oldcrereferenceno)
                                 $refc=$oldcrereferenceno;
                              
                            $refdatae=array(
                                    'type'=>'5',
                                     'reference'=>$refc
                                   );
                                  $this->db->insert('refs', $refdatae);
                                  $refid=$this->db->insert_id() ;
             /************ Insert into placing slip refs table  **************/
                          $refpdata=array(
                                      'id'=>$refid,
                                      'pslip_id'=>$_POST['declaration_id'],
                                      'type'=>'declaration'
                               );
                              $this->db->insert('placing_slip_refs', $refpdata);


                              $fyear=$this->accountingsmodel->fiscalyear();

                       $insValuesA=array(
                                  'trans_no'=>$refid,
                                  'user'=>$_SESSION['userid'],
                                  'date_trans'=>$tdate,

                                  'fiscal_year'=>$fyear,
                                  'gl_date'=>$tdate

                             );
                         $this->db->insert('audit_trail', $insValuesA);


                  $staffid=$_POST['staffid'];
                  $pecentageamt=$_POST['brockamt'];
                 $comm_pay_amt=0;
                 for($sff=0;$sff<sizeof($staffid);$sff++)
                      {
                            $preamt1=$_POST['preamt'];
                            $stakedata=array(
                                    'declaration_id'=>$_POST['declaration_id'],
                                    'staffid'=>$staffid[$sff],
                                    'percentage'=>$pecentageamt[$sff]

                            );
                            $this->db->insert('declaration_stakeholders', $stakedata);
                            $agentamt= $preamt1 *($pecentageamt[$sff]/100);


                            $agent_acc=$_POST['agent_acc'][$sff];
                        //    $trans_date=$acctype_val= $this->customermodel->getSingleFieldValue('placing_slip','quotation_date','id',$_POST['placing_slip_id']);
                            $insValuesS=array(
                                    'type_no'=>$refid,
                                    'tran_date'=>$tdate,
                                    'account'=>$agent_acc,
                                    'memo'=>'',
                                    'amount'=>-number_format($agentamt,2, '.', '')
                            );
                            $insValuesAt=array(
                                    'type_no'=>$refid,
                                    'tran_date'=>$tdate,
                                    'account'=>$agent_acc,
                                    'memo'=>'',
                                    'amount'=>-number_format($agentamt,2, '.', ''),
                                    'pid'=>$_POST['declaration_id']
                            );


                             $this->db->insert('gl_trans', $insValuesS);
                             $this->db->insert('agent_trans', $insValuesAt);
                            $tot_brokerage=$tot_brokerage-$pecentageamt[$sff];
                            $comm_pay_amt+=$agentamt;
                      }
                     $commission_acc=$this->documentsmodel->get_commission_pay_acc();
                     $insValuesCP=array(
                                    'type_no'=>$refid,
                                    'tran_date'=>$tdate,
                                    'account'=>$commission_acc,
                                    'memo'=>'',
                                    'amount'=>number_format($comm_pay_amt,2, '.', '')
                            );
                        $this->db->insert('gl_trans', $insValuesCP);

              }
              /********************************insert total premium as customer debit into gl trans************************************/
              $preamt=$_POST['preamt']+$_POST['policyfees'];
              $cust_acc=$_POST['cust_acc'];




                        $tra_cod_d= $this->customermodel->getSingleFieldValue('transaction_code','code','trans',6);
                        if($tra_cod_d!='')
                             {
                               $next_ref1= $this->accountingsmodel->get_reference_code(6);
                              $refd=$tra_cod_d.'/'.date('Y').'/'.$next_ref1;
                             }
                       else
                             {
                              $refd="0";
                            }
            /************ Insert into refs table and take inserted id **************/
                            
                            if($olddebreferenceno)
                                 $refd=$olddebreferenceno;
                            
                            $refdata=array(
                                    'type'=>'6',
                                     'reference'=>$refd
                                   );
                                  $this->db->insert('refs', $refdata);
                                  $refid1=$this->db->insert_id() ;
             /************ Insert into placing slip refs table  **************/
                          $refpdata=array(
                                      'id'=>$refid1,
                                      'pslip_id'=>$_POST['declaration_id'],
                                      'type'=>'declaration'
                               );
                              $this->db->insert('placing_slip_refs', $refpdata);


                              $fyear=$this->accountingsmodel->fiscalyear();

                       $insValuesA1=array(
                                  'trans_no'=>$refid1,
                                  'user'=>$_SESSION['userid'],
                                  'date_trans'=>$tdate,

                                  'fiscal_year'=>$fyear,
                                  'gl_date'=>$tdate

                             );
                         $this->db->insert('audit_trail', $insValuesA1);






            // $trans_date=$acctype_val= $this->customermodel->getSingleFieldValue('placing_slip','quotation_date','id',$_POST['placing_slip_id']);
                $insValues=array(

                                    'type_no'=>$refid1,
                                    'tran_date'=>$tdate,
                                    'account'=>$cust_acc,
                                    'memo'=>'',
                                    'amount'=>number_format($preamt,2, '.', '')

                                 );
                  $this->db->insert('gl_trans', $insValues);


                   /********************************insert into customer transaction table*************************************************/
                $insValue=array(

                                    'type_no'=>$refid1,
                                    'tran_date'=>$tdate,
                                    'account'=>$cust_acc,
                                    'memo'=>'',
                                    'amount'=>number_format($preamt,2, '.', ''),
                                    'alloc'=>'',
                                    'pid'=>$_POST['declaration_id']

                                 );
                   $this->db->insert('customer_trans', $insValue);

         /*******************************insert brokerage amount into cover acc in gl trans*************************************/
                  $brokamount=$_POST['brokerage'];

                  $totbrotamt= $_POST['preamt'] *($brokamount/100);
                  $covr_acc=$_POST['covr_acc'];
                  $covrmain_acc=$_POST['covrmain_acc'];
                 // $trans_date=$acctype_val= $this->customermodel->getSingleFieldValue('placing_slip','quotation_date','id',$_POST['placing_slip_id']);
                  $insValues1=array(

                                    'type_no'=>$refid1,
                                    'tran_date'=>$tdate,
                                    'account'=>$covrmain_acc,
                                    'memo'=>'',
                                    'amount'=>-number_format($totbrotamt,2, '.', '')

                                );
                  $this->db->insert('gl_trans', $insValues1);
        /****************************************insert insurer amount into credit as company acc*********************************************/
                $compamt=$_POST['comp_amt'];

                $comp_acc=$this->accountingsmodel->get_acc_id('insurance_company','company_code',$_POST['insurance_company_id']);

              $insValuesC=array(
                                  'type_no'=>$refid1,
                                  'tran_date'=>$tdate,
                                  'account'=>$comp_acc,
                                  'memo'=>'',
                                  'amount'=>-number_format($compamt,2, '.', '')

                  );

            $insValuesC1=array(
                                  'type_no'=>$refid1,
                                  'tran_date'=>$tdate,
                                  'account'=>$comp_acc,
                                  'memo'=>'',
                                  'amount'=>-number_format($compamt,2, '.', ''),
                                  'pid'=>$_POST['declaration_id']

                  );
                $this->db->insert('gl_trans', $insValuesC);
                 $this->db->insert('company_trans', $insValuesC1);




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

	     }
	     else
	     {
		 $this->db->trans_commit();
                 return true;

	     }




     }

     function getdeclaration_refs($plid)
    {
        $data = array();
        $Q=$this->db->query("select * from placing_slip_refs where pslip_id ='$plid' and type='declaration'");

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

    }
    
    function getdeclaration_referenceno($plid,$reftype)
    {
       
         $Q=$this->db->query("select reference from refs r join placing_slip_refs ps on ps.id=r.id where ps.pslip_id ='$plid' and ps.type='declaration' and r.type='$reftype'");
         $data=$Q->row()->reference ;
         return $data;
    }

    function getdpolicyfeesname($pid)
    {

             $qry="select labelname from declaration_form where declaration_id='$pid' and fieldtype='policyfees'";
             $res=$this->db->query($qry);
             if(isset($res->row()->labelname))
             $data=$res->row()->labelname ;
             else
             $data="Policy Fees";
             return $data;
    }


     function addquickpolicynoteno()
         {
         $pdate=$this->mastermodel->convdatformat($_POST['date']);
         $rdate=$this->mastermodel->convdatformat($_POST['recdate']);

        $query=$this->db->query("insert into policynote values('','$pdate','$_POST[qref]','$_POST[policyno]','$rdate')") ;
        $policynoteid=mysql_insert_id();


        if($query>0)
       {
           return 1;
       }
       else
           {
           return 0;
       }
         }

         function findpolicybyno($code)
         {
           $res=$this->db->query("SELECT p.policyno,p.id,p.quotation_ref,c.cover,ifnull(cp.insured,ci.insured) as insured FROM `policynote` p join
                 placing_slip ps on p.`quotation_ref` =ps.`quotation_ref` join covers c on c.id=ps.cover_id left join customer_corp_info cp on
                 cp.customer_id=ps.customer_id left join customer_personal_ind ci on ci.customer_id=ps.customer_id
                 where p.policyno='$code'");
          $result=$res->row();

           if($res->num_rows() > 0)
           {
             $policyno= $result->policyno;
             $policyid=$result->id;
             $quotation_ref=$result->quotation_ref;
             $cover=$result->cover;
             $insured=$result->insured;

             $resultset=$policyno.'~'.$policyid.'~'.$insured.'~'.$quotation_ref.'~'.$cover;
           }
           else
           {
               $resultset="";
           }

           return $resultset;
             
         }


 }

?>

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