<?php

class department_model extends CI_Model{

    function __construct()

    {
       

        $this->load->library('table');

        // Call the Model constructor

        parent::__construct();
       //$this->load->library('session');
        
		
    }

     

    function buisness_type(){

                $b_type='';

               $user_id=$this->session->userdata('user_id');

               $bus_type=$this->db->query("select * from buisness_type  where user_id='$user_id'");

               $data='';

              $row = $bus_type->row();

             return $b_type=$row->buisness_id;

    }

    function get_bankcount(){

        $b_type= $this->buisness_type();

         $query = $this->db->query("select * from bank_name  where   status=1 

                                     and record=1 and buisness_id='$b_type'  ");

        $row= $query->result();

         return $row;

     }

    function cash_bank(){

        $b_type= $this->buisness_type();

          

    $bus_type=$this->db->query("select * from bank_name  where buisness_id='$b_type'

    and type='Cash' ");

       $data='';

      $row = $bus_type->row();

    $bank=$row->id;

    return $bank;

    }

    function get_bankamount_mark($allbank=''){

         $b_type= $this->buisness_type();

         $sql=$this->db->query("select * from bank_name  where buisness_id='$b_type' and status=1 ");

         $cat=$sql->result();

        foreach ($cat as $key=> $type){

        $totalamount=0.0;

        $totalpay=0.0;

        $idbank=$type->id;

       
         $sql="SELECT sum(amount) as income ,sum(pur_amount) as expense FROM sale_purchases   where  buisness_id='$b_type'  and status=1 and bank_id='$idbank'";
         $query=$this->db->query($sql);
         $result=$query->row();

           //foreach ($result as $key=> $value){

               $totalamount=$result->amount;

                $totalpay=$result->pur_amount;

          // }
          if($allbank){
              $mark[$type->type]=$totalamount-$totalpay;  
          }
            else{
                $mark[$type->id]=$totalamount-$totalpay; 
            }
           

        }

       //print_r($mark);

       

        return $mark;

    }

    function purtabmark(){

        

         $b_type= $this->buisness_type();

         $sql=$this->db->query("select * from purchases_type  where buisness_type='$b_type' and status=1 ");

         $cat=$sql->result();

        foreach ($cat as $key=> $type){

        $totalorder=0.0;

        $totalpay=0.0;

        $ptype=$type->id;

        $catvalue=$this->db->query("select * from purchases  where buisness_type='$b_type' and status=1 and type='$ptype'");

         $result=  $catvalue->result();

           foreach ($result as $key=> $value){

               $totalorder+=round($value->total_order,2);

              

                  $totalpay+=round($value->pay_total,2);

                

           }

            

           $mark[$type->id]=round($totalpay,2)-round($totalorder,2);  

        }

       

       // print_r($mark);

        return $mark;

        

    }

    function wholesaletabmark(){

        

         $b_type= $this->buisness_type();

         $sql=$this->db->query("select * from wholesale_category  where buisness_type='$b_type' and status=1 ");

         $cat=$sql->result();

        foreach ($cat as $key=> $type){

        $totalorder=0.0;

        $totalpay=0.0;

        $ptype=$type->id;

        $catvalue=$this->db->query("select * from wholesale  where buisness_type='$b_type' and status=1 and type='$ptype'");

         $result=  $catvalue->result();

           foreach ($result as $key=> $value){

               $totalorder+=round($value->total_order,2);

                $totalpay+=round($value->pay_total,2);

           }

          $mark[$type->id]=round($totalpay,2)-round($totalorder,2);  

        }

       // print_r($mark);

        return $mark;

        

    }

    function tr_bank(){

        $b_type= $this->buisness_type();

         

    $bus_type=$this->db->query("select * from bank_name  where buisness_id='$b_type'

   and type='Ticket Resto' ");

  $data='';

 $row = $bus_type->row();

 $bank=$row->id;

    return $bank;

    }

    function get_saletype_list(){

         $b_type= $this->buisness_type();

    $sql="SELECT * FROM sale_type

WHERE  buisness_type='$b_type' and status=1 ORDER BY type ASC ";

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

        return $query->result();

    }

    function get_totalwholesale($filter,$active=''){

      

          $b_type= $this->buisness_type(); 

       

           if($filter=='Last Year'){

             

            $sql="SELECT * FROM wholesale   WHERE YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR)  and buisness_type='$b_type'  and status=1";

             

        

        }

        else if($filter=='Last Month'){

             

            $sql="SELECT * FROM wholesale   WHERE MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'   and status=1";

            

        

        }

        

        else if ($filter=='Last week'){

            $sql="SELECT * FROM wholesale

            WHERE date BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE()  and buisness_type='$b_type'  and status=1"; 

           

        }

            else if($filter=='This Month'){

                $sql="SELECT * FROM wholesale

            WHERE MONTH(date) = MONTH(CURRENT_DATE()) AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'  and status=1"; 

               

            }

         else if($filter=='This Year'){

                $sql="SELECT * FROM wholesale

            WHERE YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'  and status=1";

             

            }

        else if($filter=='Total Wohlesale'){

            $sql = "select * from wholesale where status=1 and buisness_type='$b_type' " ;  

        }

       if($active){
           $sql .= " AND type='".$active."' " ;
      }

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

        

        $totalorder=0.0; $totalpay=0.0; $unpaid=0.0;

                foreach ($query->result() as $key=>$result) {

                  

                    $totalorder+=round($result->total_order,2); $totalpay+=round($result->pay_total,2); $unpaid+=round($result->due_balance,2);

                }
               $un=  $totalorder-$totalpay;
              
         return  array(

    'unpaid' => round($un,2),

          

    'totalpay' => round($totalpay,2),

           'totalorder'=>round($totalorder,2),

);

    }

    function get_purchases_total($filter,$active=''){

        $b_type= $this->buisness_type();

       

           if($filter=='Last Year'){

             

            $sql="SELECT * FROM purchases   WHERE YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR)  and buisness_type='$b_type'  and status=1";

             

        

        }

        else if($filter=='Last Month'){

             

            $sql="SELECT * FROM purchases   WHERE MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'   and status=1";

            

        

        }

        

        else if ($filter=='Last week'){

            $sql="SELECT * FROM purchases

            WHERE date BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE() and buisness_type='$b_type'  and status=1"; 

           

        }

            else if($filter=='This Month'){

                $sql="SELECT * FROM purchases

            WHERE MONTH(date) = MONTH(CURRENT_DATE()) AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'  and status=1"; 

               

            }

         else if($filter=='This Year'){

                $sql="SELECT * FROM purchases

            WHERE YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'  and status=1";

             

            }

        else if($filter=='Total Purchases'){

            $sql = "select * from purchases where status=1 and buisness_type='$b_type' " ;  

        }

      if($active){
           $sql .= " AND type='".$active."' " ;
      }

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

        

        $totalorder=0.0; $totalpay=0.0; $unpaid=0.0;

                foreach ($query->result() as $key=>$result) {

                    $totalorder+=round($result->total_order,2); $totalpay+=round($result->pay_total,2); $unpaid+=round($result->due_balance,2);

                }

        
$un=$totalorder-$totalpay;
            
         return  array(

    'unpaid' => round($un,2),

          

    'totalpay' => round($totalpay,2),

           'totalorder'=>round($totalorder,2),

);

        

    }

    function get_tab_total($tabid,$text){

     

          $b_type= $this->buisness_type();

       

           if($text=='Last Year'){

             

            $sql="SELECT * FROM sale_purchases   WHERE YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR)  and buisness_id='$b_type'  and status=1 and bank_id='$tabid'";

             

        

        }

        else if($text=='Last Month'){

             

            $sql="SELECT * FROM sale_purchases   WHERE MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)  AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_id='$b_type'   and status=1 and bank_id='$tabid'";

            

        

        }

        

        else if ($text=='Last week'){

            $sql="SELECT * FROM sale_purchases

            WHERE date BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE() and buisness_id='$b_type' and bank_id='$tabid' and status=1"; 

           

        }

            else if($text=='This Month'){

                $sql="SELECT * FROM sale_purchases

            WHERE MONTH(date) = MONTH(CURRENT_DATE()) AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_id='$b_type' and bank_id='$tabid'  and status=1"; 

               

            }

         else if($text=='This Year'){

                $sql="SELECT * FROM sale_purchases

            WHERE YEAR(date) = YEAR(CURRENT_DATE()) and buisness_id='$b_type'  and status=1 and bank_id='$tabid'";

             

            }

        else if($text=='Total'){

            $sql = "select * from sale_purchases where status=1 and buisness_id='$b_type' and bank_id='$tabid' " ;  

        }

      

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

        

        $totalcradit=0.0; $totaldebit=0.0; $totalprofit=0.0;

                foreach ($query->result() as $key=>$result) {

                    $totalcradit+=$result->amount; $totaldebit+=$result->pur_amount; 

                }

        $totalprofit=$totalcradit-$totaldebit;

         return  array(

    'totalprofit' => $totalprofit,

          

    'totaldebit' => $totaldebit,

           'totalcradit'=>$totalcradit,

);

        

    }
function payloanfilter($text){
      $b_type= $this->buisness_type();

      if($text=='Last Year'){

            $sql="SELECT * FROM pay_loan   WHERE YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR)  and buisness_type='$b_type'  and status=1 ";


        }
        else if($text=='Last Month'){
             
            $sql="SELECT * FROM pay_loan   WHERE MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'   and status=1 ";


        }

        else if ($text=='Last week'){

            $sql="SELECT * FROM pay_loan

            WHERE date BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE() and buisness_type='$b_type'  and status=1"; 

           

        }

            else if($text=='This Month'){

                $sql="SELECT * FROM pay_loan

            WHERE MONTH(date) = MONTH(CURRENT_DATE()) AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'   and status=1"; 

               

            }

         else if($text=='This Year'){

                $sql="SELECT * FROM pay_loan

            WHERE YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'  and status=1 ";

             

            }

        else if($text=='Total'){

             



            $sql = "select * from pay_loan where status=1 and buisness_type='$b_type'  " ;  

           

        }

      

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

        

        $totalexpense=0.0; 

                foreach ($query->result() as $key=>$result) {

                    $totalexpense+=round($result->total,2);  

                }

      

       return  array(

    'totalexpense' => round($totalexpense,2),

);

}
    function expensefilter($text){

         $b_type= $this->buisness_type();

     

           if($text=='Last Year'){

             

            $sql="SELECT * FROM expense   WHERE YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR)  and buisness_type='$b_type'  and status=1 ";

             

        

        }

        else if($text=='Last Month'){

             

            $sql="SELECT * FROM expense   WHERE MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'   and status=1 ";

            

        

        }

        

        else if ($text=='Last week'){

            $sql="SELECT * FROM expense

            WHERE date BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE() and buisness_type='$b_type'  and status=1"; 

           

        }

            else if($text=='This Month'){

                $sql="SELECT * FROM expense

            WHERE MONTH(date) = MONTH(CURRENT_DATE()) AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'   and status=1"; 

               

            }

         else if($text=='This Year'){

                $sql="SELECT * FROM expense

            WHERE YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'  and status=1 ";

             

            }

        else if($text=='Total'){

             



            $sql = "select * from expense where status=1 and buisness_type='$b_type'  " ;  

           

        }

      

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

        

        $totalexpense=0.0; 

                foreach ($query->result() as $key=>$result) {

                    $totalexpense+=round($result->total,2);  

                }

      

       return  array(

    'totalexpense' => round($totalexpense,2),

          

   

);

        

        

    }

    function workerfilter($text){

         $b_type= $this->buisness_type();

     

           if($text=='Last Year'){

             

            $sql="SELECT * FROM worker  WHERE YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR)  and buisness_type='$b_type'  and status=1 ";

             

        

        }

        else if($text=='Last Month'){

             

            $sql="SELECT * FROM worker   WHERE MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) AND YEAR(date) = YEAR(CURRENT_DATE())  and buisness_type='$b_type'   and status=1 ";

            

        

        }

        

        else if ($text=='Last week'){

            $sql="SELECT * FROM worker

            WHERE date BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE() and buisness_type='$b_type'  and status=1"; 

           

        }

            else if($text=='This Month'){

                $sql="SELECT * FROM worker

            WHERE MONTH(date) = MONTH(CURRENT_DATE())  AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'   and status=1"; 

               

            }

         else if($text=='This Year'){

                $sql="SELECT * FROM worker

            WHERE YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'  and status=1 ";

             

            }

        else if($text=='Total'){

            



            $sql = "select * from worker where status=1 and buisness_type='$b_type'  " ;  

           

        }

      

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

        

        $totalpay=0.0; 

                foreach ($query->result() as $key=>$result) {

                    $totalpay+=$result->salary;  

                }

      

       return  array(

    'totalpay' => $totalpay,

          

   

);

    }

      function get_sale_total($filter,$active=''){

          $tab1=0.0;

          $tab2=0.0;

          $tab3=0.0;

          $totalsale=0.0;

        $b_type= $this->buisness_type();

       $saletype= $this->get_saletype_list();

          

          foreach ($saletype as $key=>$result) {

           if($filter=='Last Year'){

             

            $sql="SELECT * FROM sale   WHERE YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR)  and buisness_type='$b_type'  and status=1";

                $sql2[$result->id]="SELECT * FROM sale   WHERE YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR)  and buisness_type='$b_type'  and status=1 and type='$result->id' ";
           
               //$sql3="SELECT * FROM sale   WHERE YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR)  and buisness_type='$b_type'  and status=1 and type='Cosmetiques' ";

              // $sql4="SELECT * FROM sale   WHERE YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR)  and buisness_type='$b_type'  and status=1 and type='Calling Card' ";

             

        

        }

        else if($filter=='Last Month'){

             

            $sql="SELECT * FROM sale   WHERE MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'   and status=1";

            $sql2[$result->id]="SELECT * FROM sale    WHERE MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'  and status=1 and type='$result->id' ";

              // $sql3="SELECT * FROM sale   WHERE MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)  and buisness_type='$b_type'  and status=1 and type='Cosmetiques' ";

               //$sql4="SELECT * FROM sale    WHERE MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)  and buisness_type='$b_type'  and status=1 and type='Calling Card' ";

        

        }

        

        else if ($filter=='Last week'){

            $sql="SELECT * FROM sale

            WHERE date BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE() and buisness_type='$b_type'  and status=1";

             $sql2[$result->id]="SELECT * FROM sale    WHERE date BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE()  and buisness_type='$b_type'  and status=1 and type='$result->id' ";

              // $sql3="SELECT * FROM sale   WHERE date BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE()  and buisness_type='$b_type'  and status=1 and type='Cosmetiques' ";

              // $sql4="SELECT * FROM sale    WHERE date BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE()  and buisness_type='$b_type'  and status=1 and type='Calling Card' ";

           

        }

            else if($filter=='This Month'){

                $sql="SELECT * FROM sale

               WHERE MONTH(date) = MONTH(CURRENT_DATE()) AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'  and status=1"; 

                 $sql2[$result->id]="SELECT * FROM sale    WHERE MONTH(date) = MONTH(CURRENT_DATE())   and buisness_type='$b_type'  and status=1 and type='$result->id' ";

              // $sql3="SELECT * FROM sale   WHERE MONTH(date) = MONTH(CURRENT_DATE())   and buisness_type='$b_type'  and status=1 and type='Cosmetiques' ";

              // $sql4="SELECT * FROM sale    WHERE MONTH(date) = MONTH(CURRENT_DATE())   and buisness_type='$b_type'  and status=1 and type='Calling Card' ";

               

            }

         else if($filter=='This Year'){

                $sql="SELECT * FROM sale

            WHERE YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'  and status=1";

             $sql2[$result->id]="SELECT * FROM sale   WHERE YEAR(date) = YEAR(CURRENT_DATE())  and buisness_type='$b_type'  and status=1 and type='$result->id' ";

             //  $sql3="SELECT * FROM sale   WHERE YEAR(date) = YEAR(CURRENT_DATE())  and buisness_type='$b_type'  and status=1 and type='Cosmetiques' ";

              // $sql4="SELECT * FROM sale   WHERE YEAR(date) = YEAR(CURRENT_DATE())  and buisness_type='$b_type'  and status=1 and type='Calling Card' ";

             

            }

        else if($filter=='Total Sale'){

            $sql= "select * from sale where status=1 and buisness_type='$b_type' " ;

            

            $sql2[$result->id]="SELECT * FROM sale   WHERE buisness_type='$b_type'  and status=1 and type='$result->id' ";

              // $sql3="SELECT * FROM sale   WHERE  buisness_type='$b_type'  and status=1 and type='Cosmetiques' ";

              // $sql4="SELECT * FROM sale   WHERE buisness_type='$b_type'  and status=1 and type='Calling Card' ";

        }

          }

      foreach ($saletype as $key=>$result2) {

          $tab[$result2->id]=0;

          $tabquery[$result2->id] = $this->db->query($sql2[$result2->id]);

          foreach ($tabquery[$result2->id]->result() as $key=>$tabresult) {

                     $tab[$result2->id]+=$tabresult->total; 

                }

      }

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

          foreach ($query->result() as $key=>$result3) {

                     $totalsale+=$result3->total; 

                }

        

    return  array(

    'tab' => $tab,

          

    'totalsale' => number_format($totalsale,2),

           

);

        

    }

    function get_sale_filterprofit($from,$to){

      

      $b_type= $this->buisness_type();

        

             

            $sql="SELECT * FROM sale_purchases   where (date BETWEEN '".$from."' AND '".$to."' )  and buisness_id='$b_type' and sale_id and status=1";

             $sql11="SELECT * FROM sale_purchases  where (date BETWEEN '".$from."' AND '".$to."' )  and buisness_id='$b_type' and wholesale_id and status=1";

            $sql2="SELECT * FROM sale_purchases

          where (date BETWEEN '".$from."' AND '".$to."' )   and buisness_id='$b_type' and purchases_id and status=1";

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

         $query2 = $this->db->query($sql2);

        $query11= $this->db->query($sql11);

       $sale=0.0;

        $totalsale=0.0;

        $wholesale=0.0;

        $purchases=0.0;

        $netincome=0.0;

        foreach ($query->result() as $key=> $amount){

             $totalsale+=$amount->amount;

              

               }

        foreach ($query11->result() as $key=> $amount3){

             $wholesale+=$amount3->amount;

              

               }

        foreach ($query2->result() as $key=> $amount2){

             $purchases+=$amount2->pur_amount;

              

               }

        $sale=$wholesale+$totalsale;

        $netincome=$sale-$purchases;

       return  array(

    'sale' => $sale,

          

    'purchases' => $purchases,

           'netincome'=>$netincome,

);

        

    }

   function get_sale_profit($profit,$from,$to){

         $b_type= $this->buisness_type();

        if($profit=='Last Year'){

             

            $sql="SELECT * FROM sale   WHERE YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR)  and buisness_type='$b_type' and status=1";

             $sql11="SELECT * FROM wholesale   WHERE YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR)  and buisness_type='$b_type'  and status=1";

            $sql2="SELECT * FROM purchases

           WHERE YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR)  and buisness_type='$b_type'  and status=1";

        

        }

        else if($profit=='Last Month'){

             

            $sql="SELECT * FROM sale  WHERE MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'   and status=1";

            $sql11="SELECT * FROM wholesale   WHERE MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'  and status=1 ";

            $sql2="SELECT * FROM purchases

        WHERE MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'  and status=1 ";

        

        }

        

        else if ($profit=='Last week'){

            $sql="SELECT * FROM sale

            WHERE date BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE() and buisness_type='$b_type'  and status=1"; 

            $sql11="SELECT * FROM wholesale

            WHERE date BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE() and buisness_type='$b_type'  and status=1"; 

            $sql2="SELECT * FROM purchases

           WHERE date BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE() and buisness_type='$b_type'  and status=1";

        }

            else if($profit=='This Month'){

                 $sql="SELECT * FROM sale

            WHERE MONTH(date) = MONTH(CURRENT_DATE()) AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'  and status=1"; 

                $sql11="SELECT * FROM wholesale

            WHERE MONTH(date) = MONTH(CURRENT_DATE()) AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'  and status=1"; 

            $sql2="SELECT * FROM purchases

           WHERE MONTH(date) = MONTH(CURRENT_DATE()) AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'  and status=1";

            }

         else if($profit=='This Year'){

                $sql="SELECT * FROM sale

            WHERE YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'  and status=1";

              $sql11="SELECT * FROM wholesale

            WHERE YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'  and status=1";

            $sql2="SELECT * FROM purchases

           WHERE YEAR(date) = YEAR(CURRENT_DATE()) and buisness_type='$b_type'  and status=1";

            }
       

        else if($from and $to){

           $sql="SELECT * FROM sale

             where (date BETWEEN '".$from."' AND '".$to."' ) and buisness_type='$b_type'  and status=1";

              $sql11="SELECT * FROM wholesale

             where (date BETWEEN '".$from."' AND '".$to."' ) and buisness_type='$b_type'  and status=1";

            $sql2="SELECT * FROM purchases

           where (date BETWEEN '".$from."' AND '".$to."' ) and buisness_type='$b_type'  and status=1"; 

        }

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

         $query2 = $this->db->query($sql2);

        $query11= $this->db->query($sql11);

       $sale=0.0;

        $totalsale=0.0;

        $wholesale=0.0;

        $purchases=0.0;

        $netincome=0.0;
        $paid=0.0;
        foreach ($query->result() as $key=> $amount){

             $totalsale+=round($amount->total,2);

              

               }

        foreach ($query11->result() as $key=> $amount3){

             $wholesale+=round($amount3->pay_total,2);

              

               }

        foreach ($query2->result() as $key=> $amount2){

             $paid+=round($amount2->pay_total,2);
            $purchases+=round($amount2->total_order,2);
              

               }

        $sale=$wholesale+$totalsale;

        $netincome=$sale-$paid;

       return  array(

    'sale' => round($sale,2),

          

    'purchases' => round($purchases,2),
    'paid' => round($paid,2),
    'netincome'=>round($netincome,2),

);

    }


    

       function get_expense_profit($expenseprofit,$from,$to){

         $b_type= $this->buisness_type();

        if($expenseprofit=='Last Year'){

             

            $sql="SELECT * FROM sale_purchases   WHERE YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR)  and buisness_id='$b_type' and expense_id and status=1 ";

            $sql2="SELECT * FROM sale_purchases

     WHERE YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR)  and buisness_id='$b_type' and worker_id and status=1";

        

        }

        else if($expenseprofit=='Last Month'){

             

            $sql="SELECT * FROM sale_purchases   WHERE MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_id='$b_type' and expense_id and status=1";

            $sql2="SELECT * FROM sale_purchases

        WHERE MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_id='$b_type' and worker_id and status=1";

        

        }

        

        else if ($expenseprofit=='Last week'){

            $sql="SELECT * FROM sale_purchases

            WHERE date BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE() and buisness_id='$b_type' and expense_id and status=1 "; 

            $sql2="SELECT * FROM sale_purchases

           WHERE date BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE() and buisness_id='$b_type' and worker_id and status=1 ";

        }

            else if($expenseprofit=='This Month'){

                $sql="SELECT * FROM sale_purchases

            WHERE MONTH(date) = MONTH(CURRENT_DATE()) AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_id='$b_type' and expense_id and status=1"; 

            $sql2="SELECT * FROM sale_purchases

           WHERE MONTH(date) = MONTH(CURRENT_DATE()) AND YEAR(date) = YEAR(CURRENT_DATE()) and buisness_id='$b_type' and worker_id and status=1";

            }

         else if($expenseprofit=='This Year'){

                $sql="SELECT * FROM sale_purchases

            WHERE YEAR(date) = YEAR(CURRENT_DATE()) and buisness_id='$b_type' and expense_id and status=1"; 

            $sql2="SELECT * FROM sale_purchases

           WHERE YEAR(date) = YEAR(CURRENT_DATE()) and buisness_id='$b_type' and worker_id and status=1";

            }

            else if($from and $to){

                $sql="SELECT * FROM sale_purchases

           where (date BETWEEN '".$from."' AND '".$to."' ) and buisness_id='$b_type' and expense_id and status=1"; 

            $sql2="SELECT * FROM sale_purchases

          where (date BETWEEN '".$from."' AND '".$to."' ) and buisness_id='$b_type' and worker_id and status=1"; 

            }

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

         $query2 = $this->db->query($sql2);

       $sale=0.0;

        $purchases=0.0;

        $netincome=0.0;

        foreach ($query->result() as $key=> $amount){

             $sale+=$amount->pur_amount;

              

               }

        foreach ($query2->result() as $key=> $amount2){

             $purchases+=$amount2->pur_amount;

              

               }

        $netincome=($sale+$purchases);

       return  array(

    'expense' => $sale,

    'worker' => $purchases,

           'netincome'=>$netincome,

);

    }
     function get_netexpense_profit($profit,$from,$to){

         $b_type= $this->buisness_type();
           $b_type= $this->buisness_type();
            $sql="SELECT sum(amount) as income ,sum(pur_amount) as expense FROM sale_purchases   where buisness_id='$b_type'  and status=1 ";
            

       

       if($profit=='Last Year'){

             

            $sql .=" AND YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR)  ";

          
        

        }

        else if($profit=='Last Month'){

             

            $sql.=" AND MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)  ";

          
        }

        

        else if ($profit=='Last week'){

            $sql.=" AND date BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE() ";
          

        }

            else if($profit=='This Month'){

                $sql.=" AND MONTH(date) = MONTH(CURRENT_DATE()) AND YEAR(date) = YEAR(CURRENT_DATE())"; 

             
            }

         else if($profit=='This Year'){

                $sql.=" AND YEAR(date) = YEAR(CURRENT_DATE())  ";

            

            }
       

        else if($from and $to){

           $sql .=" AND  (date BETWEEN '".$from."' AND '".$to."' ) ";

            

        }

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

        $result=$query->row();
        $income=$result->income;
        $expense=$result->expense;
       $profit=$income-$expense;
     
        

       return  array(

    'totalsale' => ($income),

    'totalexpense' => ($expense),

    'netincome'=>($profit),

);

    }

function get_amount_details(){

      $b_type= $this->buisness_type();

    $sql="SELECT * FROM sale_purchases

WHERE date BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE() and buisness_id='$b_type' and sale_id ";

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

        return $query->result();

}

    function get_bankamount(){

       $b_type= $this->buisness_type();

        $today = date('Y-m-d');

       
        $sale=0.0;

        $purchases=0.0;

        $totalbankamount=0.0;

       
       $sql="SELECT sum(amount) as income ,sum(pur_amount) as expense FROM sale_purchases   where buisness_id='$b_type'  and status=1 ";
            

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

        $result=$query->row();
        $income=$result->income;
        $expense=$result->expense;
       $profit=$income-$expense;
        /*$query = $this->db->query($sql);

        foreach ($query->result() as $key=> $amount){

             $sale+=$amount->amount;

             $purchases+=$amount->pur_amount;

        }

        $totalbankamount=$sale-$purchases;
*/
        return $profit ;

    }

    function get_cashbankamount($id){

        $sale=0.0;

        $purchases=0.0;

        $totalbankamount=0.0;

           $b_type='';

         $user_id=$this->session->userdata('user_id');

              $b_type= $this->buisness_type();

         
         $sql="SELECT sum(amount) as income ,sum(pur_amount) as expense FROM sale_purchases   where buisness_id='$b_type'  and status=1 and bank_id='$id' ";

         $income=$result->income;
        $expense=$result->expense;
       $profit=$income-$expense;
 
        return $profit ;

        

    }

      function invoice(){

        

        $b_type= $this->buisness_type();

       

         $sql = "SELECT *  FROM invoice where status=1 and buisness_type='$b_type' order by id ASC" ;

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

        foreach ($query->result() as $key=> $invoice){

             $id=$invoice->id;

            

        }

       

        return $id ;

        

    }
function invoice_create(){

        

        $b_type= $this->buisness_type();

       $id='';

         $sql = "SELECT *  FROM invoice where status=1 and buisness_type='$b_type' order by id ASC" ;

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

        foreach ($query->result() as $key=> $invoice){

             $id=$invoice->invoice_id;

            

        }

       

        return $id ;

        

    }

    function get_edit_invoice($id){

        $b_type= $this->buisness_type();

           $sql = "SELECT *  FROM invoice where buisness_type='$b_type'  and id='$id' " ;

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

      

       

        return $query->result() ;

        

    }

     function get_invoice(){

        

        $b_type= $this->buisness_type();

       

         $sql = "SELECT *  FROM invoice where buisness_type='$b_type' and status=1 " ;

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

      

       

        return $query->result() ;

        

    }

    function get_trashinvoice(){

        $b_type= $this->buisness_type();

       

         $sql = "SELECT *  FROM invoice where buisness_type='$b_type' and status=0 " ;

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

      

       

        return $query->result() ;

    }

    function fetch_single_details($invoice_id)

	{
         ini_set('memory_limit', '64M');
         $user_id=$this->session->userdata('user_id');

        $output='';

         $b_type= $this->buisness_type();

		  $sql = "SELECT *  FROM invoice where id='$invoice_id' and buisness_type='$b_type' " ;

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

        $logo='';

    $type=$this->db->query("select image  from invoice 

      where id=$invoice_id");

    foreach ($type->result() as $key => $logo)

      $logo=$logo->image; 

    $defult="index.jpg";

    if($logo){

      $img= '<img src='.base_url().'invoice-uploads/'.$logo.' alt="Smiley face" class="profile"  style="height: 104px;margin: 0;float: right;width: 104px;background: #fff none repeat scroll 0 0;border: 5px solid #7fd000; border-radius: 50%;float: right;margin-top: -151px;margin-right: 62px;">';
        

    }

    else{

      $img='<img src='.base_url().'invoice-uploads/'.$defult.' alt="Smiley face" class="profile" style="height: 104px;margin: 0;float: right;width: 104px;background: #fff none repeat scroll 0 0;border: 5px solid #7fd000; border-radius: 50%;float: right;margin-top: -151px;margin-right: 62px;"  >'; 

    }

		$frombilltext[1]=0;

      $senten=0;

		foreach ($query->result() as $key=> $invoice){
          $custom_unit=($invoice->custom_unit);
            $qty=unserialize($invoice->invoice_qty_info);

     $unit=unserialize($invoice->invoice_unit_info);

     $des=unserialize($invoice->invoice_des_info);

     $amount=unserialize($invoice->invoice_amount_info);

     $tax=unserialize($invoice->unit_invoice_tax_info);

            $taxprice=unserialize($invoice->invoice_tax_price);

             $total=($invoice->total);
            $sub_total=($invoice->sub_total);

            $condition=($invoice->condition);

             $invoice_n=($invoice->invoice_name);

            $paytype=($invoice->pay_type);

         

            $text = trim($invoice->bill_from);

             $frombillcompany = trim($invoice->from_company);

            $frombilltocompany = trim($invoice->bill_to);

            $pieces = explode(' ', $frombillcompany);

              

          

           $billtext= explode("\n", $text);

             $frombilltext= explode("\n",  $frombillcompany);

             $frombillto= explode("\n",   $frombilltocompany);

            

            if(isset($frombilltext[1])){

                $senten=$frombilltext[1];

            }

            $ro = preg_replace('/\s\s+/', ' ',$senten);

            $bold=explode(" ",$ro);

            $cun= array_filter($bold);

           

        

           $keys = array_keys($cun);



            

            $loop= (count($bold));

             $lastindex=end($keys);

           

 

      

       

           

             $output .= '<div class="iv-main-section"><div class="header-invoice">

             <div class="company">

             <div class="from-company">';

             foreach( $frombilltext as $index => $line1 ){

             if($index==1){

                 

                 $last=$this->lastWord($bold[$lastindex]);

                   $subject = $line1 ;

                    $search = $last ;

                    $line1 = str_replace($search, '', $subject) ;

               

                    }

                 else{

                    $last=' ';  

                 }

               $output .='<p>'.$line1.' <strong>'.$last.'</strong></p> ';

             }

             

           $output .='</div>

             </div>

             

             <div class="facture">

             <h3>'.$invoice_n.'</h3>

             <span>Date: '.date('d F Y',strtotime($invoice->invoice_date)).'</span>

             <p>'.($invoice->company_name).'</p>

             ';

            foreach( $frombillto as $index => $line2 ){

              $output .='<p>'.$line2.'</p>';

            }

            $output .=' 

             </div>

             <div class="invoice"> 

             <p>N°:  '.($invoice->invoice_id).'</p> 

             </div>

             </div>

             <div class="iv-main-content">

               <table  style=" border-collapse: collapse; border:0px solid #000;border-width: .5px .5px .5px 0;width: 100%;" class="table table-striped table-bordered bootstrap-datatable datatable">

                                	<thead>

                                    	<tr>

                                        <th style="border-left: .5px solid #000;border-top: .5px solid #000;line-height: 20px;padding: 8px;">N°.</th>

                                        	<th style="border-left: .5px solid #000;border-top: .5px solid #000;line-height: 20px;padding: 8px;">Description</th>

											<th style="border-left: .5px solid #000;border-top: .5px solid #000">Quantite</th>

											

											<th style="border-left: .5px solid #000;border-top: .5px solid #000">Unit '.$custom_unit.'</th>

                                            <th style="border-left: .5px solid #000;border-top: .5px solid #000">Total  '.$custom_unit.'</th>

                                           												

                                        </tr>

                                    </thead>

                                    <tbody>';

                                   

									$sum=0.0;

                                     $sl=1;
                                      $newpage='';
                                      $pdfrow=(count($unit));
                                   
                                       $tt_rowcount=(count($unit));
                                    
                                     if($tt_rowcount <= 22){
                                           $pdfrow=21;
                                           
                                       }
                                     if ($tt_rowcount >=22  ){
                                     $pdfrow=33; 
                                       } 
                                       if($tt_rowcount >=33 ){
                                             $pdfrow=53;
                                             
                                       }
                                      
                                      
                                      if($tt_rowcount >=53){
                                           $pdfrow=73;
                                           
                                       }
                                     if($tt_rowcount >=73){
                                           $pdfrow=83;
                                           
                                       }
            
           
										for ($q=0;$q<=$pdfrow;$q++) {

                                            

                                            if(isset($amount[$q]) ){

                                                $slno=$sl++;

                                                $sum+=$amount[$q];

                                            }

                                           else{

                                               $slno=' ';

                                             

                                           }

                                           if(isset($des[$q]) ){

                                               

                                                $des1=substr($des[$q], 0, 50) ;

                                            }

                                           else{

                                               $des1=' ';

                                             

                                           }

                                           if(isset($amount[$q]) ){

                                               $amount1=number_format($amount[$q],2).'€';

                                           }

                                           else{

                                              $amount1=' ';  

                                           }

                                            if(isset($qty[$q]) ){

                                               $qty1=$qty[$q];

                                           }

                                           else{

                                              $qty1=' ';  

                                           }

                                            if(isset($unit[$q]) ){

                                               $unit1=number_format($unit[$q],2).'€';

                                           }

                                           else{

                                              $unit1=' ';  

                                           }

                                         

                                            

											$output .='<tr>

                                            <td class="first" style="border-left: .5px solid #000;border-top: .5px solid #000;">'.$slno.'</td>

												<td class="second" style="border-left: .5px solid #000;border-top: .5px solid #000;line-height: 20px;padding:0 8px;">'.$des1.'</td>

												<td class="third" style="border-left: .5px solid #000;border-top: .5px solid #000">'.$qty1.'</td>

                                               

												<td class="forth" style="border-left: .5px solid #000;border-top: .5px solid #000">'.$unit1.'</td>

												<td class="fifth" style="border-left: .5px solid #000;border-top: .5px solid #000">'.$amount1.'</td>

												</tr>';

                                        }

											

											

                              

            $pcheck=" ";$escheck=" ";$trcheck=" ";$chcheck=" ";$vicheck='';

            if($paytype =="CB"){

                $pcheck="checked";

            }

            if($paytype=="Checque"){

                $chcheck="checked";

            }

            if($paytype=="TR"){

                $trcheck="checked";

            }

           if($paytype=="ESPECES"){

                $escheck="checked";

            }
             if($paytype=="VIREMENT"){

                $vicheck="checked";

            }

         

            

                               $output .=' 

                              

                               </tbody>

                                </table> </div>';

                               $output .='<div class="iv-footer">

                               <div class="footer-left">

                               <div class="footer-paymeny">

                               <h4>Mode Paiment</h4>

                               <div><input type="checkbox" value="CB" '.$pcheck.'> CB</div>
                                <div><input type="checkbox" value="VIREMENT" '.$vicheck.'> VIREMENT </div>
                               <div><input type="checkbox" value="checque" '.$chcheck.'> CHEQUE</div>

                             <div><input type="checkbox" value="ESPECES" '.$escheck.'> ESPECES</div>

                               <div><input type="checkbox" value="TR" '.$trcheck.'> TR</div>

                               </div>

                               <div class="footer-address">';

              $check=" ";$escheck=" ";$trcheck=" ";$chcheck=" ";

                               foreach( $billtext as $index => $line ){

                             $output .=  '<p>'.$line.'</p>';

                                    

                                   }

                               

                             $output.=' </div>';

                              $output.= ' </div>

                               <div class="footer-right">

                               <div class="footer-intotal">

                               <table style=" border-collapse: collapse;" class="table table-striped table-bordered bootstrap-datatable datatable">

                               <tbody>

                               <tr><td>Total H.T</td> <td>'.number_format($sub_total, 2).'€</td></tr>

                               ';

                                $commontax='xx';

                                $taxrate=0.0;

                                

                                
                                        

                                    

                                   for ($t=0;$t<count($tax);$t++) {

                                     

                              $output .='<tr><td>T.V.A '.$tax[$t].'% </td><td>'.number_format($taxprice[$t], 2).'€</td></tr>

                               ';  

                             

                                       $commontax=$tax[$t];

                                       $taxrate=0;

                                   }

                                   $output .='

                               <tr><td>Total T.T.C</td><td>'.number_format($total, 2).'€</td></tr>

                               </tbody>

                               </table>

                               </div>

                            <div class="footer-condition">'.$condition.'</div>

                               </div>

                               </div>

                               </div>

<style>

/*.iv-main-section {

    width: 750px;

    margin: 50px auto;

}*/
@page teacher {
  size: A4 portrait;
  margin: 2cm;
}

.teacherPage {
   page: teacher;
   page-break-after: always;
}
.iv-main-section {

    font-family: Arial,Helvetica Neue,Helvetica,sans-serif;

    color: #000;

    font-size: 16px;

    font-weight: normal;

    line-height: 16px;

}

p {

    color: #000;

    font-size: 16px;

    font-weight: normal;

    line-height: 16px;

    margin: 0;

}

ul {

  list-style: none;

  margin: 0;

  padding: 0;

}

.header-invoice {

    float: left;

    width: 100%;

    clear: both;

}

.company {

    float: left;

    width: 62%;

    padding-right: 3%;

}

.from-company p:nth-child(2) {

    font-size: 25px;

    line-height: 36px;

    font-weight: 700;

    color: #626262;

}

.from-company p:nth-child(2) strong {

    font-size: 25px;

    line-height: 33px;

    font-weight: 700;

    color: #010B2E;



}

.facture {

    float: right;
    min-height: 175px;
    width: 35%;

    text-align: center;

    margin-top: 23px;

}

.facture h3 {

    text-transform: uppercase;

    margin: 7px 0;

    font-size: 20px;

    font-weight: 700;

    line-height: 20px;

}

.facture span {

    font-size: 13px;

    display: block;

    margin-bottom: 6px;

}

.facture p {

    border-bottom: 1px solid #969696;

    padding-bottom: 6px;

    margin-bottom: 6px;

    font-weight: bold;

}

.invoice {

    width: 100%;

    font-weight: 700;

    clear: both;

    display: block;

}

.invoice p {

    margin: 3px 0 8px;

    font-size: 20px;

    font-weight: bold;

}

th {

    background: #e1004a;

    color: #fff;

    text-align: center;

    font-size: 13px;

    font-weight: 700;

}

.iv-main-content td {

    height: 21px;

    line-height: 21px;

    font-size: 16px;

}

.first, .third {

    text-align: center;

}

.forth, .fifth {

    padding: 0 20px;

    text-align: right;

}

.iv-main-content {

    clear: both;

}

.iv-footer {

    clear: both;

    padding-top: 20px;

    float: left;
  
    width: 100%;

    height: 210px;

    position: relative;

}

.footer-left {

    display: inline-block;

    width: 60%;

    vertical-align: top;

}

.footer-right {

    display: inline-block;

    width: 39%;

    text-align: right;

    vertical-align: top;

}

.footer-address {

    background: #e1004a;

    color: #fff;

    width: 85%;

    padding: 6px 10px 5px;

    font-size: 13px;

    margin-top: 5px;

    margin-bottom: 5px;

}

.footer-address p {

    color: #fff;

    font-size: 13px;

    padding-left: 20px;

    margin-bottom: 2px;

}

.footer-address p:first-child {

    background: url(http://thelivedemo.amaderparis.com/php/finance/invoice-uploads/home.png) no-repeat scroll left center;

    background-size: 15px;

    background-color: inherit;

}

.footer-address p:nth-child(3) {

    background: url(http://thelivedemo.amaderparis.com/php/finance/invoice-uploads/phone.png) no-repeat scroll left center;

    background-size: 15px;

    background-color: inherit;

}

.footer-address p:last-child {

    font-weight: bold;

    background: url(http://thelivedemo.amaderparis.com/php/finance/invoice-uploads/mail.png) no-repeat scroll left center;

    background-size: 15px;

    background-color: inherit;

}

.footer-paymeny h4 {

    margin: 0px 10px 10px;

    font-size: 13px;

    font-weight: bold;

}

.footer-paymeny input {

    display: inline-block;

    vertical-align: top;

    margin: 3px 6px 3px 10px;

}

.footer-right table td {

    font-size: 13px;

    font-weight: bold;

    height: 23px;

    line-height: 23px;

}

.footer-right table td + td {

    border: 1px solid #000;

    text-align: right;

    padding: 0 15px;

    font-size: 19px;

    line-height: 23px;

    font-weight: bold;

    height: 23px;

}

.footer-right table {

    line-height: 31px;

    width: 100%;

    vertical-align: middle;

}

.footer-condition {

    margin-top: 55px;

    display: block;

    text-align: right;

    width: 80%;

    float: left;

    font-size: 13px;

}

.footer-paymeny div {

    line-height: 20px;

    vertical-align: middle;

    font-size: 13px;

    text-transform: uppercase;

    font-weight: bold;

}



</style>';		

									



		}

		

	

		return $output;

	}

 function lastWord($sentence) {

// Break the sentence into its component words:

$words = explode(' ', $sentence);

// Get the last word and trim any punctuation:

$result = trim($words[count($words) - 1], '.?![](){}*');

// Return a result:

return $result;

}

    
function get_trbankamount($id){

        $sale=0.0;

        $purchases=0.0;

        $totalbankamount=0.0;

          $b_type='';

         $user_id=$this->session->userdata('user_id');

             $b_type= $this->buisness_type();

         $sql = "SELECT *  FROM sale_purchases where buisness_id='$b_type' and bank_id='$id' and status=1" ;

         $sql="SELECT sum(amount) as income ,sum(pur_amount) as expense FROM sale_purchases   where buisness_id='$b_type'  and status=1 and bank_id='$id' ";
            

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

        $result=$query->row();
        $income=$result->income;
        $expense=$result->expense;
       $profit=$income-$expense;
   
        return $profit ;

    }
    
 function get_totalprofitamount(){
           $b_type= $this->buisness_type();
            $sql="SELECT sum(amount) as income ,sum(pur_amount) as expense FROM sale_purchases   where buisness_id='$b_type'  and status=1 ";
            

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

        $result=$query->row();
        $income=$result->income;
        $expense=$result->expense;
       $profit=$income-$expense;
     
    return $profit;
    
}
    
 function get_unpaidpurchasesbankamount(){

        $sale=0.0;

        $purchases=0.0;
        $totalorder=0.0;
        $totalpay=0.0;
        $totalbankamount=0.0;

          $b_type='';

         $user_id=$this->session->userdata('user_id');

        $b_type= $this->buisness_type();

          $sql = "SELECT sum(total_order) as total_order,sum(pay_total) as pay_total,sum(due_balance) as due_balance   FROM purchases where buisness_type='$b_type' and status=1" ;

        $query = $this->db->query($sql);
       $total=($query->row());
        //foreach ($query->result() as $key=> $amount){

             
            $totalorder+=round($total->total_order,2); 
            $totalpay+=round($total->pay_total,2);
             $purchases+=round($total->due_balance,2);

        //}

        $unpaid=round($totalorder,2)-round($totalpay,2);
        $un=round($unpaid,2);    

        return $unpaid ;

    }

 function get_unpaidwholesalebankamount(){

          $b_type='';

        $sale=0.0;
        $totalorder=0.0;
        $totalpay=0.0;
         $user_id=$this->session->userdata('user_id');

              $b_type= $this->buisness_type();

         $sql = "SELECT *  FROM wholesale where buisness_type='$b_type' and status=1" ;

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

        foreach ($query->result() as $key=> $amount){
             $totalorder+=round($amount->total_order,2); $totalpay+=round($amount->pay_total,2);
             $sale+=round($amount->due_balance,2);

             

        }

       $unpaid=round($totalorder,2)-round($totalpay,2);
//$unpaid=$totalorder-$totalpay;
        return $unpaid ;

    }
    
    function get_expense_list($limit, $start,$active){

        $b_type='';

          $today = date('Y-m-d');

         $user_id=$this->session->userdata('user_id');

        $b_type= $this->buisness_type();


             $sql = "select a.*,b.id as expenseid,b.type as expensetype from expense a left join expense_type b on(a.type=b.id ) where a.status=1 and b.status=1 and a.buisness_type='$b_type' and b.buisness_type='$b_type' ";

              if($active){

                   $sql .=" and a.type='$active' ";

                }
        $from=$this->input->post('from');
        $to=$this->input->post('to');
        if(($this->input->post('from')) and ($this->input->post('to')) ){
           
            $sql .=" AND (date BETWEEN '".$from."' AND '".$to."' )";
        }
      if(!$from and !$to){
          $sql .="order by CONVERT(DATE, date) desc   limit " . $start . ', ' . $limit;
      }

        

        

    

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

        return $query->result(); 

}

    

    function get_department_list($limit, $start,$active)

    {
 
      $from='';$to='';
        $b_type='';

         $user_id=$this->session->userdata('user_id');

              $b_type= $this->buisness_type();   

        $today = date('Y-m-d');

        // $sql = "select * from sale where order by id  limit " . $start . ", " . $limit;
         $sql = "select a.*,b.id as saleid,b.type as saletype from sale a left join sale_type b on(a.type=b.id) where a.status=1 and b.status=1 and  a.buisness_type='$b_type'  and b.buisness_type='$b_type' ";   
                if($active){

                   $sql .=" and a.type='$active' ";

                }
         $from=$this->input->post('from');
        $to=$this->input->post('to');
        if(($this->input->post('from')) and ($this->input->post('to')) ){
           
            $sql .=" AND (date BETWEEN '".$from."' AND '".$to."' )";
        }
      if(!$from and !$to){
          $sql .="order by CONVERT(DATE, date) desc   limit " . $start . ', ' . $limit;
      }
        
        

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

        return $query->result();

    }

   function get_purchase_list($limit, $start,$active)

    {

            $b_type='';

              $user_id=$this->session->userdata('user_id');

              $b_type= $this->buisness_type(); 

    

        

           $sql = "select a.*,b.id as purtypeid,b.type as purtype from purchases a left join purchases_type b on(a.type=b.id) where a.status=1 and b.status=1 and a.buisness_type='$b_type' and b.buisness_type='$b_type' " ;

              if($active){

                   $sql .=" and a.type='$active' ";

                }
       $from=$this->input->post('from');
        $to=$this->input->post('to');
        if(($this->input->post('from')) and ($this->input->post('to')) ){
            
            $sql .=" AND (date BETWEEN '".$from."' AND '".$to."' )";
        }
      if(!$from and !$to){
          $sql .="order by CONVERT(DATE, date) desc   limit " . $start . ', ' . $limit;
      }

        

        

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

        return $query->result();

    }

    

       function get_wholesale_list($limit, $start,$active)

    {

            $b_type='';

             $user_id=$this->session->userdata('user_id');

              $b_type= $this->buisness_type(); 


                $sql = "select a.*,b.id as wholetypeid,b.category as wholesaletype from wholesale a left join wholesale_category b on(a.type=b.id) where a.status=1 and b.status=1 and a.buisness_type='$b_type' and b.buisness_type='$b_type'  "   ;

           
            if($active){

                   $sql .=" and a.type='$active' ";

                }
            $from=$this->input->post('from');
            $to=$this->input->post('to');
        if(($this->input->post('from')) and ($this->input->post('to')) ){
           
            $sql .=" AND (date BETWEEN '".$from."' AND '".$to."' )";
        }
      if(!$from and !$to){
          $sql .="order by CONVERT(DATE, date) desc   limit " . $start . ', ' . $limit;
      }
       

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

        return $query->result();

    }

       function get_bank_list()

     {

        $b_type='';

        $user_id=$this->session->userdata('user_id');

        $b_type= $this->buisness_type();  

        $sql = "select * from bank_name where status=1 and record=1 and buisness_id='$b_type'" ;

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

        return $query->result();

    }

    


    function get_buisness_type(){

        $user_id=$this->session->userdata('user_id');

    $bus_type=$this->db->query("select * from buisness_type  where user_id='$user_id'

    ");

  $data='';

  foreach ($bus_type->result() as $key => $bus_type){

    $b_type=$bus_type->buisness_id;



  }

    return $b_type;    

    }

function get_buisness_name($id_post){

    $user_id=$this->session->userdata('user_id');

   $bus_type=$this->db->query("select * from type  where id='$id_post' 

   and user_id='$user_id' ");

  $data='';

  foreach ($bus_type->result() as $key => $bus_type){

    $b_type=$bus_type->type;



  }

    return $b_type;    

    }

    

 function get_wholesale_type($id_post=''){

         $b_type='';

         $user_id=$this->session->userdata('user_id');

              $b_type= $this->buisness_type(); 

         $type=$this->db->query("select * from wholesale_category b  where b.id  = '$id_post' and buisness_type='$b_type' ",false);

          foreach ($type->result() as $key => $value){

           

              $type=$value->category;

              

              

          }

        return $type;

    }

    function get_wholesaletrash($limit, $start,$active){ 

         $b_type='';

         $user_id=$this->session->userdata('user_id');

              $b_type= $this->buisness_type(); 

          $sql = "select a.*,b.id as wholetypeid,b.category as wholesaletype from wholesale a left join wholesale_category b on(a.type=b.id) where a.status=0  and a.buisness_type='$b_type' and b.buisness_type='$b_type'  "   ;
          if($active){

                   $sql .=" and a.type='$active' ";

                }
        if(($this->input->post('from')) and ($this->input->post('to')) ){
            $from=$this->input->post('from');
            $to=$this->input->post('to');
            $sql .=" AND (date BETWEEN '".$from."' AND '".$to."' )";
        }
      if(!$from and !$to){
          $sql .="order by CONVERT(DATE, date) desc   limit " . $start . ', ' . $limit;
      }
        $query = $this->db->query($sql);

        return $query->result();

    }

    

    function get_transfer_list($limit, $start){

        $b_type='';

         $user_id=$this->session->userdata('user_id');

              $b_type= $this->buisness_type(); 

        $sql = "select * from transfer where status=1 and buisness_type='$b_type' order by CONVERT(DATE, date) desc  limit " .$start. ', ' . $limit ;

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

        return $query->result();

    }

    function get_transfer_trash($limit, $start){

        $b_type='';

         $user_id=$this->session->userdata('user_id');

              $b_type= $this->buisness_type();  

        $sql = "select * from transfer where status=0 and buisness_type='$b_type' order by CONVERT(DATE, date) desc  limit " .$start. ', ' . $limit ;

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

        return $query->result();

    }

     function get_wholesale_category()

    {

            $b_type='';

         $user_id=$this->session->userdata('user_id');

             $b_type= $this->buisness_type();

        $sql = "select * from wholesale_category where status=1 and buisness_type='$b_type'   ORDER BY category ASC" ;

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

        return $query->result();

    }

    

    function get_value($id_post,$id_post1){

        $sql = 'select * from purchases type='.$id_post .' and pay_type='.$id_post1.' ';

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

        return $query->result();

    }

    function get_purchase_category($limit, $start){

           $b_type='';

        $user_id=$this->session->userdata('user_id');

              $b_type= $this->buisness_type();

        $sql = "select * from purchases_type where status=1 and buisness_type='$b_type' ORDER BY id DESC limit " . $start . ', ' . $limit ;

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

        return $query->result();

        

    }

      function get_sale_type($limit, $start){

           $b_type='';

         $user_id=$this->session->userdata('user_id');

               $b_type= $this->buisness_type();

           $sql = "select * from sale_type where status=1 and buisness_type='$b_type'  ORDER BY id DESC limit " . $start . ', ' . $limit ;

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

        return $query->result();

          

      }
    

   /* function get_saletab_type($tab){

           $b_type='';

        

               $b_type= $this->buisness_type();

           $sql = "select * from sale_type where status=1 and buisness_type='$b_type' and id='$tab' ";

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

         foreach ($query->result() as $key => $value){

           

              $type=$value->type;

              

              

          } 

        return $type;

          

      }*/

    
    /*function get_purtab_type($tab){

           $b_type='';

        

               $b_type= $this->buisness_type();

           $sql = "select * from purchases_type where status=1 and buisness_type='$b_type' and id='$tab' ";

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

         foreach ($query->result() as $key => $value){

           

              $type=$value->type;

              

              

          } 

        return $type;

          

      }*/

    

      function get_trash($limit, $start,$active){

          $b_type='';

         $user_id=$this->session->userdata('user_id');

              $b_type= $this->buisness_type();

           $sql = "select a.*,b.type as saletype from sale a left join sale_type b on(a.type=b.id)  where a.status=0 and a.buisness_type='$b_type'  " ;
           if($active){

                   $sql .=" and a.type='$active' ";

                }
        if(($this->input->post('from')) and ($this->input->post('to')) ){
            $from=$this->input->post('from');
            $to=$this->input->post('to');
            $sql .=" AND (date BETWEEN '".$from."' AND '".$to."' )";
        }
      if(!$from and !$to){
          $sql .="order by CONVERT(DATE, date) desc   limit " . $start . ', ' . $limit;
      }
        $query = $this->db->query($sql);

        return $query->result();

     }

      function get_ptrash($limit, $start,$active){

          $b_type='';

         $user_id=$this->session->userdata('user_id');

               $b_type= $this->buisness_type();

          $sql = "select a.*,b.type as purchases_type from purchases a left join purchases_type b on(a.type=b.id) where a.status=0  and a.buisness_type='$b_type'   "  ;
               if($active){

                   $sql .=" and a.type='$active' ";

                }
        if(($this->input->post('from')) and ($this->input->post('to')) ){
            $from=$this->input->post('from');
            $to=$this->input->post('to');
            $sql .=" AND (date BETWEEN '".$from."' AND '".$to."' )";
        }
      if(!$from and !$to ){
          $sql .="order by CONVERT(DATE, date) desc   limit " . $start . ', ' . $limit;
      }
         
         
        $query = $this->db->query($sql);

        return $query->result();

     }

 function get_worker_list($limit, $start,$active){

            $b_type='';
             $from=$this->input->post('from');
             $to=$this->input->post('to');
             $user_id=$this->session->userdata('user_id');

               $b_type= $this->buisness_type();


                  $sql = "select a.*,b.id as workerid,b.name as workername from worker a left join worker_name b on(a.name=b.id) where a.status=1  and b.status=1 and a.buisness_type='$b_type'  and b.buisness_type='$b_type'  " ;   

        
            if($active){

                   $sql .=" and a.name='$active' ";

                }
        if(($from) and ($to) ){
            $from=$this->input->post('from');
            $to=$this->input->post('to');
            $sql .=" AND (date BETWEEN '".$from."' AND '".$to."' )";
        }
      if(!$from and !$to){
          $sql .="order by CONVERT(DATE, date) desc   limit " . $start . ', ' . $limit;
      }

        

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

        return $query->result(); 

    }

function get_workertrash($limit, $start,$active){

          $b_type='';

         $user_id=$this->session->userdata('user_id');

               $b_type= $this->buisness_type();

             $sql = "select a.*,b.name as workername from worker a left join worker_name b on(a.name=b.id)  where a.status=0  and a.buisness_type='$b_type'   "  ;
    
           if($active){

                   $sql .=" and a.name='$active' ";

                }
        if(($this->input->post('from')) and ($this->input->post('to')) ){
            $from=$this->input->post('from');
            $to=$this->input->post('to');
            $sql .=" AND (date BETWEEN '".$from."' AND '".$to."' )";
        }
      if(!$from and !$to){
          $sql .="order by CONVERT(DATE, date) desc   limit " . $start . ', ' . $limit;
      }
        $query = $this->db->query($sql);

        return $query->result();

     }

     function get_worker_name($limit, $start){

           $b_type='';

         $user_id=$this->session->userdata('user_id');

              $b_type= $this->buisness_type();

        $sql = "select * from worker_name where status=1 and buisness_type='$b_type' ORDER BY id DESC limit " . $start . ', ' . $limit ;

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

        return $query->result();

     }

      function get_expense_trash($limit, $start,$active){

        $b_type='';

        $user_id=$this->session->userdata('user_id');

        $b_type= $this->buisness_type();

           $sql = "select a.*,b.type as expensetype from expense a left join expense_type b on(a.type=b.id) where a.status=0 and a.buisness_type='$b_type'   "  ;
          if($active){

                   $sql .=" and a.type='$active' ";

                }
        if(($this->input->post('from')) and ($this->input->post('to')) ){
            $from=$this->input->post('from');
            $to=$this->input->post('to');
            $sql .=" AND (date BETWEEN '".$from."' AND '".$to."' )";
        }
      if(!$from and !$to){
          $sql .="order by CONVERT(DATE, date) desc   limit " . $start . ', ' . $limit;
      }
        $query = $this->db->query($sql);

        return $query->result();

     }

     function get_expense_type($limit, $start){

           $b_type='';

         $user_id=$this->session->userdata('user_id');

               $b_type= $this->buisness_type();

        $sql = "select * from expense_type where status=1 and buisness_type='$b_type'  ORDER BY id DESC limit " . $start . ', ' . $limit ;

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

        return $query->result();

     }

      function get_bank_name($limit, $start){

        $b_type='';

        $user_id=$this->session->userdata('user_id');

        $b_type= $this->buisness_type();

         $sql = "select * from bank_name where status=1 and buisness_id='$b_type' ORDER BY id DESC limit " . $start . ', ' . $limit ;

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

        return $query->result();

     }
   function get_loan_category($limit='', $start=''){
        $b_type='';

        $user_id=$this->session->userdata('user_id');

        $b_type= $this->buisness_type();
         if($limit){
            $sql = "select * from loan_category where status=1 and buisness_id='$b_type' ORDER BY id DESC limit " . $start . ', ' . $limit ; 
         }
       else{
           $sql = "select * from loan_category where status=1 and buisness_id='$b_type' ORDER BY category ASC  "  ;
       }
         

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

        return $query->result();
   }
   
    function get_payloantab_type($id){
        $tb_type='';

    $user_id=$this->session->userdata('user_id');

    $b_type= $this->buisness_type();

    $tab_type=$this->db->query("select * from pay_loan_category  where id='$id' ");

  $data='';

  foreach ($tab_type->result() as $key => $tabtype){

    $tb_type=$tabtype->category;



  }

    return $tb_type;    
    }
     function get_loan_option_name($id){

    $tb_type='';

    $user_id=$this->session->userdata('user_id');

    $b_type= $this->buisness_type();

    $tab_type=$this->db->query("select * from loan_category  where id='$id' ");

  $data='';

  foreach ($tab_type->result() as $key => $tabtype){

    $tb_type=$tabtype->category;



  }

    return $tb_type;    

    }
    function bank_row(){

          $b_type='';

        $user_id=$this->session->userdata('user_id');

               $b_type= $this->buisness_type();

        $query = "SELECT * FROM bank_name   ";

        $query2 = $this->db->query($query);

 $bankrow= $query2->num_rows();

        return $bankrow;

    }

      function get_bankname(){

        $b_type='';

        $user_id=$this->session->userdata('user_id');

              $b_type= $this->buisness_type();

        $sql = "select * from bank_name where status=1 and buisness_id='$b_type'  " ;

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

        return $query->result();

     }

    function get_banktotransfer($toid){

        $b_type='';

        $user_id=$this->session->userdata('user_id');

              $b_type= $this->buisness_type();

           $bus_type=$this->db->query("select * from bank_name where type='$toid' and buisness_id='$b_type' 

    ");

  $data='';

  foreach ($bus_type->result() as $key => $bus_type){

    $b_type=$bus_type->id;



  }

    return $b_type;    

    }

function get_tab_type($id){

        $tb_type='';

        $user_id=$this->session->userdata('user_id');

        $b_type= $this->buisness_type();

        $tab_type=$this->db->query("select * from sale_type where id='$id' and buisness_type='$b_type' and status=1 ");

            $data='';

  foreach ($tab_type->result() as $key => $tabtype){

    $tb_type=$tabtype->type;



  }

    return $tb_type;    

    }

     function get_purtab_type($id){

$tb_type='';

$user_id=$this->session->userdata('user_id');

 $b_type= $this->buisness_type();

$tab_type=$this->db->query("select * from purchases_type where id='$id' and buisness_type='$b_type' and status=1");

  $data='';

  foreach ($tab_type->result() as $key => $tabtype){

    $tb_type=$tabtype->type;



  }

    return $tb_type;    

    }

     function get_wholetab_type($id){

    $tb_type='';

        $user_id=$this->session->userdata('user_id');

               $b_type= $this->buisness_type();

           $tab_type=$this->db->query("select * from wholesale_category where id='$id' and buisness_type='$b_type' and status=1

    ");

  $data='';

  foreach ($tab_type->result() as $key => $tabtype){

    $tb_type=$tabtype->category;



  }

    return $tb_type;    

    }

    function get_workertab_type($id){

    $tb_type='';

    $user_id=$this->session->userdata('user_id');

    $b_type= $this->buisness_type();

    $tab_type=$this->db->query("select * from worker_name where id='$id' and buisness_type='$b_type' and status=1");

  $data='';

  foreach ($tab_type->result() as $key => $tabtype){

    $tb_type=$tabtype->name;



  }

    return $tb_type;    

    }

function get_expensetab_type($id){

    $tb_type='';

        $user_id=$this->session->userdata('user_id');

               $b_type= $this->buisness_type();

           $tab_type=$this->db->query("select * from expense_type where id='$id' and buisness_type='$b_type' and status=1

    ");

  $data='';

  foreach ($tab_type->result() as $key => $tabtype){

    $tb_type=$tabtype->type;



  }

    return $tb_type;    

    }
function buisness_list(){
   $b_type= $this->buisness_type();
      $user_id=$this->session->userdata('user_id');

    $bus_type=$this->db->query("select * from type  where user_id='$user_id' ");
    return $bus_type->result();
}
    function address_list($b_type){ 
        $data=array();
        $b_type= $this->buisness_type();
    $bus_type=$this->db->query("select * from address_setting where buisness_id='$b_type' ");
   
    return $bus_type->result();
}
    
function graph($fyear=''){
    $b_type= $this->buisness_type();
    $year=date('Y');
    if($fyear){
        $year=$fyear;
    }
    $dataPoints=array();
    $months = array(1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December');
    for($i=1;$i<13;$i++){
    $sql=$this->db->query("select sum(total) as total from sale where year(date) = '".$year."' and month(date) = '".$i."' AND buisness_type=".$b_type." ");
    $result=$sql->row();
    $sql2=$this->db->query("select sum(pay_total) as total,sum(total_order) as totalorder from purchases where year(date) = '".$year."' and month(date) = '".$i."' AND buisness_type=".$b_type." ");
    $result2=$sql2->row();
    if($result2->total > $result->total){
        $redsignal='redsignal';
    }
    else{
           $redsignal=''; 
        }
     $dataPoints[] = 
	array("label"=> "".$months[$i]."", "sale"=> isset($result->total)?number_format($result->total,2):'0.00',"paid"=> isset($result2->total)?number_format($result2->total,2):'0.00',"purchases"=> isset($result2->totalorder)?number_format($result2->totalorder,2):'0.00','signal'=>$redsignal);
	
      //$data[$i]= $result->total; 
    }
   // print_r( $dataPoints);
    return $dataPoints ;
}
    
    public function get_totalloanamount(){
    $b_type= $this->buisness_type();
    $loancat=$this->get_loan_category();
    $totalloan=0.0;
     $allloancat=array();
        foreach ($loancat as $loancatval){
            $sql2=$this->db->query("select sum(total_loan) as total from loan where  buisness_type=".$b_type."  AND status=1 AND type=".$loancatval->id."");
            $result2=$sql2->row();
            $totalloan+=$result2->total;
            $allloancat[$loancatval->category]=isset($result2->total)?number_format($result2->total,2):'0.00';
        }
     
     
    $loan['totalloan']=number_format($totalloan,2);
    $loan['totalloancat']=$allloancat;
    return $loan;
     }
     public function get_totalpayloanamount(){
    $b_type= $this->buisness_type();
    $allpayloancat=$this->get_payloan_category();
     $totalloan=0.0;
     $allloancat=array();
        foreach ($allpayloancat as $loancatval){
            $sql2=$this->db->query("select sum(total) as total from pay_loan where  buisness_type=".$b_type."  AND status=1 AND type=".$loancatval->id."");
            $result2=$sql2->row();
            $totalloan+=$result2->total;
            $allloancat[$loancatval->category]=isset($result2->total)?number_format($result2->total,2):'0.00';
        }
     
    
    $loan['totalpayloan']=number_format($totalloan,2);
    $loan['totalpayloancat']=$allloancat;
 
    return $loan;
     }
     function get_payloan_category($limit='', $start=''){
        $b_type='';

        $user_id=$this->session->userdata('user_id');

        $b_type= $this->buisness_type();
         if($limit){
            $sql = "select * from pay_loan_category where status=1 and buisness_id='$b_type' ORDER BY id DESC limit " . $start . ', ' . $limit ; 
         }
       else{
           $sql = "select * from pay_loan_category where status=1 and buisness_id='$b_type' ORDER BY category ASC  "  ;
       }
         

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

        return $query->result();
   }
 public function get_loan_list($limit, $start,$active,$status){
             $b_type='';
             $from=$this->input->post('from');
             $to=$this->input->post('to');
             $user_id=$this->session->userdata('user_id');

               $b_type= $this->buisness_type();


                  $sql = "select a.*,b.category as loantype,b.id as loantypeid from loan a left join loan_category b on(a.type=b.id) where a.status=".(int)$status."  and  a.buisness_type='$b_type'     " ;   

        
            if($active){

                   $sql .=" and a.type='$active' ";

                }
           $from=$this->input->post('from');
            $to=$this->input->post('to');
        if(($from) and ($to) ){
            
            $sql .=" AND (date BETWEEN '".$from."' AND '".$to."' )";
        }
      if(!$from and !$to){
          $sql .="order by CONVERT(DATE, date) desc   limit " . $start . ', ' . $limit;
      }

        

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

        return $query->result(); 

}  
    
 public function get_payloan_list($limit, $start,$active,$status){
             $b_type='';
             $from=$this->input->post('from');
             $to=$this->input->post('to');
             $user_id=$this->session->userdata('user_id');

               $b_type= $this->buisness_type();


                  $sql = "select a.*,b.category as loantype,b.id as loantypeid from pay_loan a left join pay_loan_category b on(a.type=b.id) where a.status=".(int)$status."  and  a.buisness_type='$b_type'     " ;   

        
            if($active){

                   $sql .=" and a.type='$active' ";

                }
           $from=$this->input->post('from');
            $to=$this->input->post('to');
        if(($from) and ($to) ){
            
            $sql .=" AND (date BETWEEN '".$from."' AND '".$to."' )";
        }
      if(!$from and !$to){
          $sql .="order by CONVERT(DATE, date) desc   limit " . $start . ', ' . $limit;
      }

        

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

        return $query->result(); 

} 
}

?>