<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Product_controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Your own constructor code
        $this->load->model('product_model');
         $this->load->model('transfernotes_model');
        $this->load->model('accounting_model');
        $this->load->model('service_model');
        $this->load->model('supplier_model');
        $this->load->model('business_model');
        if ($this->session->userdata('role') == '') {
            redirect('logout');
        }
    }

    public function getproducts($businessid="", $storeid="", $showprofessional='No') {
        //get the posted values
        $showinhouse="No";
        
        //$showprofessional="No";
        
        $business = $this->business_model->getbusinessdetails();
//
        if($this->session->userdata('common_products')=='No'){
            $businessid=$this->session->userdata('businessid');            
        }
        
        if(null !== $this->input->get('showprofessional', TRUE)){
            $showprofessional=$this->input->get('showprofessional');   
        } else if($showprofessional=='No'){
            if($business[0]['show_professional']=='Yes'){
                $showprofessional="Yes";            
            }
        }        
        //$data = $this->product_model->get_instock_products($showinhouse);
        $data = $this->product_model->get_searched_products($showinhouse, $showprofessional,"",$businessid, $storeid);

        echo(json_encode($data));
    }

    public function getinhouseproducts() {
        //get the posted values
        $business = $this->business_model->getbusinessdetails();
        $data = $this->product_model->get_inhouse_products();
        echo(json_encode($data));
    }

    public function getProductsByService() {
        $service_id = htmlspecialchars($this->input->post('service_id', TRUE));
        $data = $this->product_model->get_inhouse_products_by_service($service_id);
        echo(json_encode($data));
    }

    public function set_business_brands() {
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            //storemanager,hr,reception...user serial
            checkroles(1, 0, 0);

            $data['nav'] = 'my_business';
            $data['subnav'] = 'business_brands';

            $data['brands'] = $this->product_model->get_all_brands();


            $this->load->view('includes/header', $data);
            $this->load->view('setting/brands_view');
            $this->load->view('includes/footer');
        }
    }

    public function add_brand() {
        $result = $this->product_model->add_brand();
        echo('success|' . $result);
    }

    public function update_brand() {
        $result = $this->product_model->update_brand();
        echo('success|' . $result);
    }

    public function delete_brand() {

        $result = $this->product_model->delete_brand();
        echo('success|' . $result);
    }

    public function set_products() {
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            $data['nav'] = 'my_business';
            $data['subnav'] = 'business_brands';

            $data['business'] = $this->business_model->getbusinessdetails();
            $scid = $this->input->post('id_business_brands', TRUE);
            $data['products'] = $this->product_model->get_all_products($scid);
            $data['business_brands'] = $this->product_model->get_brand_byid($scid);
            $data['brands'] = $this->product_model->get_all_brands();
            $data['servicecategories'] = $this->service_model->getservice_categories();
            $data['unit_types']=$this->product_model->get_unittypes();

            $data['measurement_unit'] = $this->product_model->get_measurement_unit();

            $data['product_types'] = $this->product_model->get_product_types();
            //var_dump($data['product_types']);
            $this->load->view('includes/header', $data);
            $this->load->view('setting/products_view');
            $this->load->view('includes/footer');
        }
    }

    public function add_product() {

        $result = $this->product_model->add_product();
        echo('success|' . $result);
    }

    public function update_product() {

        $result = $this->product_model->update_product();
        echo('success|' . $result);
    }

    public function delete_product() {

        $result = $this->product_model->delete_product();
        echo('success|' . $result);
    }

    
    public function set_product_list($tagged=null, $brandid=0, $store_id='') {
        
        
        if(null !== $this->input->post('tagged') && $this->input->post('tagged')!==''){
            $tagged=$this->input->post('tagged');
        }
        if(null !== $this->input->post('brandid') && $this->input->post('brandid')!==''){
            $brandid=$this->input->post('brandid');
        }
        if(null !== $this->input->post('store_id') && $this->input->post('store_id')!==''){
            $store_id=$this->input->post('store_id');
        }
        
        
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            //storemanager,hr,reception...user serial
            checkroles(1, 0, 0);

            $data['nav'] = 'my_business';

            $data['business'] = $this->business_model->getbusinessdetails();
            
            $this->db->select('product_sales_tax');
            $this->db->where('product_sales_tax >', 0);
            $this->db->where('brand_id', $brandid);
            $this->db->limit(1);
            $query = $this->db->get('business_products');
            $data['taxes'] = $query->result_array();         
            //print_r($data['taxes']); exit();
            
            $data['brands'] = $this->product_model->get_all_brands();
            $data['measurement_unit'] = $this->product_model->get_measurement_unit();
            
            if($this->session->userdata('ho')=='No'){
                 $data['stores'] = $this->product_model->get_business_stores();
                 if($store_id=='' && sizeof($data['stores'])>0){
                     $store_id=$data['stores'][0]['id_business_stores'];
                 }
            } else if($this->session->userdata('common_products')=='No'){
                 $data['stores'] = $this->product_model->get_business_stores();
                 if($store_id=='' && sizeof($data['stores'])>0){
                     $store_id=$data['stores'][0]['id_business_stores'];
                 }
            } else {
                $data['stores'] = $this->product_model->get_stores();
            } 
            
            $data['storeselected'] = $store_id;
            
            if($brandid>0){
                $data['business_brands'] =$this->product_model->get_brand_byid($brandid);
            }
            if ($tagged && $tagged === "professional") {
                $data['subnav'] = 'product_list';
                $data['products'] = $this->product_model->get_all_products($brandid,$tagged, '', '', $store_id);
                $data['title']='Professional Products';
                $data['unit_types']=$this->product_model->get_unittypes();
                $data['product_types'] = $this->product_model->get_product_types();
            
            } else if ($tagged && $tagged === "retail") {
                $data['subnav'] = 'product_list_retail';
                $data['products'] = $this->product_model->get_all_products($brandid,$tagged, '', '', $store_id);
                $data['title']='Retail Products';
                $data['unit_types']=$this->product_model->get_unittypes();
                $data['product_types'] = $this->product_model->get_product_types();
              
            } else if ($tagged && $tagged === "threshold") {
                $data['subnav'] = 'product_list_retail';
                $data['products'] = $this->product_model->get_all_products($brandid,$tagged, '', '', $store_id);
                $data['title']='Products Low In Stock';
                $data['unit_types']=$this->product_model->get_unittypes();
                $data['product_types'] = $this->product_model->get_product_types();

              
            } else if ($tagged && $tagged === "expiring") {
                $data['subnav'] = 'product_list_retail';
                $data['products'] = $this->product_model->get_all_products($brandid,$tagged, '', '', $store_id);
                $data['title']='Products With Batches that are Expired or will Expire soon';
                $data['unit_types']=$this->product_model->get_unittypes();
                $data['product_types'] = $this->product_model->get_product_types();

              
            } else {
                $data['subnav'] = 'product_list';
                $data['products'] = $this->product_model->get_all_products($brandid,$tagged, '', '', $store_id);
                $data['title']='All Products';
                $data['unit_types']=$this->product_model->get_unittypes();
                $data['product_types'] = $this->product_model->get_product_types();
                
            }
            $this->load->view('includes/header', $data);
            $this->load->view('setting/products_view');
            $this->load->view('includes/footer');
        }
    }

    
        
    public function all_product_list(){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            checkroles(1, 1, 1);

            $data['nav'] = 'my_business';

            $data['business'] = $this->business_model->getbusinessdetails();
            $data['brands'] = $this->product_model->get_all_brands();
            $data['measurement_unit'] = $this->product_model->get_measurement_unit();
            $data['subnav'] = 'product_list';
               // $data['products'] = $this->product_model->get_all_products($brandid,$tagged);
                $data['title']='All Products';
                $data['unit_types']=$this->product_model->get_unittypes();
                $data['product_types'] = $this->product_model->get_product_types();

                $this->load->view('includes/header', $data);
                $this->load->view('setting/products_view_server');
                $this->load->view('includes/footer');
        }
    }
    function get_productsbysearch(){
        
         //** get all table definitions **//
        $data['draw']= $this->input->post('draw');
        $data['$start'] = $this->input->post('start');
        $data['$length'] = $this->input->post('length');
        
        $data['brand']= $this->input->post('brand');
        $data['product']= $this->input->post('product');
        $data['category']= $this->input->post('category');
        $data['professoinal']= $this->input->post('professional');
        $data['business_product_active']= $this->input->post('business_product_active');
        $data['sku']= $this->input->post('sku');
        
        
        
        
        $total= $this->product_model->get_countofproducts();
        $data['data'] = $this->product_model->get_productsbysearch();
        
//        for($x=0; $x<sizeof($data['data']);$x++){
//            $data['data'][$x]['button'] = "<button id=".$x." type='sumbit'>Add</button>";
//        }
        
        $filtered=  sizeof($data['data']);
        
        $data['recordsTotal'] = $total;
        $data['recordsFiltered'] = $total;
        
        echo(json_encode($data));
        
    }
    
    
    public function add_listproduct() {
        $result = $this->product_model->add_listproduct();
        echo('success|' . $result);
    }

    public function update_listproduct() {
        $result = $this->product_model->update_listproduct();
        echo('success|' . $result);
    }
    
    public function edit_brand(){
        $data = $this->product_model->edit_brand();
        echo (json_encode($data));
        die;
    }
    
    public function edit_product(){
        $data = $this->product_model->edit_product();
        echo (json_encode($data));
        die;
    }
    
    public function get_brands(){
        $data = $this->product_model->get_all_brands();
        echo (json_encode($data));
    }
    
    public function get_all_products(){
        $brandid=$this->input->post('brand_id', TRUE);
        $data = $this->product_model->get_all_products($brandid,'all','yes');
        echo (json_encode($data));
    }

    public function batches($product_id='', $store_id=''){
        
        if(null !== $this->input->post('product_id') && $this->input->post('product_id')!=""){
            $product_id=$this->input->post('product_id');
        }
        
        if(null !== $this->input->post('store_id') && $this->input->post('store_id')!=""){
            $store_id=$this->input->post('store_id');
        }
        
        
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            //storemanager,hr,reception...user serial
            checkroles(1, 0, 0);

            $data['nav'] = 'my_business';
            $data['subnav'] = 'business_brands';

            $data['business'] = $this->business_model->getbusinessdetails();
            $data['product'] = $this->product_model->get_product_by_id($product_id);
            
            
            if($this->session->userdata('ho')=='No') {                
                $data['stores'] = $this->product_model->get_business_stores();                
            } else if($this->session->userdata('common_products')=='No'){
                $data['stores'] = $this->product_model->get_business_store();
            } else {
                $data['stores'] = $this->product_model->get_stores();
            }
            
            $data['fortransfers']= $this->product_model->get_stores();
            
            
            $data['batches'] = $this->product_model->get_all_batches($product_id,'', $store_id);
            
            $this->load->model('productkits_model');
            $data['kitconfig'] = $this->productkits_model->get_kit_config($product_id);
            
            
            $data['storeselected']=$store_id;

            $this->load->view('includes/header', $data);
            $this->load->view('setting/batches_view');
            $this->load->view('includes/footer');
        }
        
    }

    
    
    public function check_batch_number() {
        $productid = $this->input->post('product_id', TRUE);
        $batch_number= $this->input->post('batch_number', TRUE);
        
        $data = $this->product_model->check_batch_number($productid, $batch_number);
        echo (json_encode($data));
        
    }
    
    public function add_batch(){
        //echo('Here'); exit();
        $result  = $this->product_model->add_batch();
        $adjustments=[];
        if((float)$this->input->post('batch_qty')>0){
            $data = array(
                'batch_id' => $result,
                'product_id' => $this->input->post('product_id', TRUE),
                'adjustment_qty' => $this->input->post('batch_qty', TRUE),
                'unit_price' => $this->input->post('unit_price', TRUE),              
                'remarks' => 'Openging Qty',
                 'adjustment_date' => date('Y-m-d H:i:s'),      
                'expiry_date'=> $this->input->post('expiry_date',TRUE),
                'created_on' => date('Y-m-d H:i:s'),
                'created_by' => $this->session->userdata('username')
            );
            
           array_push($adjustments, $data);
         // var_dump(json_encode($adjustments));
            
           $this->product_model->add_adjustment_note($adjustments);
        }
                
        echo('success|' . $result);
        
    }
    
    public function open_edit_batch(){
        
        $data = $this->product_model->open_edit_batch();
        echo (json_encode($data));
        
    }
    
    public function update_batch(){
        
        $result  = $this->product_model->update_batch();
                  
        $a = $this->product_model->add_adjustment_note(); 
        
        
        echo('success|' . $result);
        
    }

    public function get_next_batchno(){
        
        $data = $this->product_model->get_next_batchno();
        echo (json_encode($data));
        
        
    }
    
    public function bulk_transfer_qty(){
        // exit();
         // Unescape the string values in the JSON array
        $tableData = stripcslashes($_POST['orderdata']);

        // Decode the JSON array
        $tableData = json_decode($tableData, TRUE);
        
        foreach ($tableData as $row) {
            ///check the inventory in batch before proceeding
            $stopproduct='';

            $qty=0;
            $batch = $this->product_model->get_a_batch($row['batch_id'], date("Y-m-d"), $this->session->userdata('businessid'));

            if($batch){
                $qty=$batch->total_stock;
                if($qty < $row['transfer_qty'] || $qty<=0){
                    $stopproduct=$batch->product;
                    echo 'error|Stock Qty is lower in batch '.$row['batch_id'].' then the Transfer Qty';
                    die();
                }
            } else {

                echo 'error|Stock Qty is lower in batch '.$row['batch_id'].' then the Transfer Qty';
                die();
            }
            ////////////
        }        
        $count=0;
         foreach ($tableData as $row) {
            $reqid='';
            if(isset($row['requisition_id'])){$reqid=$row['requisition_id'];}
             $this->transfer_qty($row['batch_id'],$row['tostore'],$row['transfer_qty'],$reqid);
             $count++;
         }
        echo "success|".$count;
        
    }
    
    public function transfer_qty($frombatch="", $tostore="", $transfer_Qty="", $requisition_id=""){
       
        if($frombatch==""){
            $frombatch=$this->input->post('frombatch');
        }
        if($tostore==""){
            $tostore=$this->input->post('tostore');
        }
        if($transfer_Qty==""){
            $transfer_Qty=$this->input->post('transfer_qty');
        }
      
        
        ///check the inventory in batch before proceeding
        $stopproduct='';
        
        $qty=0;
        $batch = $this->product_model->get_a_batch($frombatch, date("Y-m-d"), $this->session->userdata('businessid'));
        
        if($batch){
            $qty=$batch->total_stock;
            
            if($qty < $transfer_Qty || $qty<=0){
                $stopproduct=$batch->product;
                
                echo 'error|Stock Qty is lower in batch '.$frombatch.' then the Transfer Qty';
                die();
            }

        } else {
            
            echo 'error|Stock Qty is lower in batch '.$frombatch.' then the Transfer Qty';
            die();
        }

        ////////////
        
        $batch = $this->product_model->getbatch($frombatch);
        
        $datetime =date("Y-m-d H:i:s");
        //$date = $datetime->format('Y-m-d H:i:s');
        //add a gtn and getid
        $gtn_id = $this->transfernotes_model->create_gtn($tostore);
        
        
        $transferout = array(
            'gtn_id' => $gtn_id,
            'business_id' => $this->session->userdata('businessid'),
            'product_id' => $batch->product_id,
            'tranfer_out_qty'=> $transfer_Qty,
            'tranfer_in_qty' => 0,
            'transfer_date' => $datetime,
            'created_by' => $this->session->userdata('username'),
            'batch' => $batch->batch_number,
            'batch_id' => $batch->id_batch,
            'requisition_id' => $requisition_id
        );
        
        $newtransferout=$this->transfernotes_model->createtransfernote($transferout);
        $inventoryout=$this->transfernotes_model->createinventorydecrease($transferout, $batch->store_id, $tostore, $batch->batch_amount);
        
        ///create new batch in transferto store and get newbatchid
        $newbatchno= $this->product_model->get_next_batchno($batch->product_id);
        $newbatch=array(
            'batch_number' => $newbatchno->new_number,
            'product_id' => $batch->product_id,
            'batch_date' => $datetime,
            'expiry_date' => $batch->expiry_date,
            'batch_qty' => 0,
            'store_id' => $tostore,
            'batch_amount' => $batch->batch_amount
            
        );
        $newbatchid=$this->product_model->add_batch($newbatch);
        
        
        $transferin = array(
            'gtn_id' => $gtn_id,
            'business_id' => $this->session->userdata('businessid'),
            'product_id' => $batch->product_id,
            'tranfer_out_qty'=> 0,
            'tranfer_in_qty' => $transfer_Qty,
            'transfer_date' => $datetime,
            'created_by' => $this->session->userdata('username'),
            'batch' => $newbatchno->new_number,
            'batch_id' => $newbatchid,
            'requisition_id' => $requisition_id
        );
        $newtransferin=$this->transfernotes_model->createtransfernote($transferin);
        $inventoryin=$this->transfernotes_model->createinventoryincrease($transferin, $batch->store_id, $tostore, $batch->batch_amount);
         
        if($requisition_id!=""){
            $sql = "update branch_requisition set transfered_qty = transfered_qty+".$transfer_Qty;
            $sql .= " where id_branch_requisition = ".$requisition_id;
            $this->db->query($sql);

            $this->db->select('required_qty, transfered_qty');
            $this->db->where("id_branch_requisition", $requisition_id);
            $query = $this->db->get('branch_requisition');
            $req=$query->row();
            if($req->required_qty <= $req->transfered_qty){

                $this->db->set('requisition_status','Transferred');
                $this->db->where('id_branch_requisition', $requisition_id);
                $this->db->update('branch_requisition');
               
            }

        }


        echo 'success|'.$newbatchid;
        
        
    }
    
    function product_notes(){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            checkroles(1, 0, 0);

            $data['nav'] = 'my_business';
            $data['subnav'] = 'business_brands';

            $data['notes'] = $this->product_model->get_all_notes($this->input->post('id_business_products'));


            $this->load->view('includes/header', $data);
            $this->load->view('setting/product_notes');
            $this->load->view('includes/footer');
        
        }
    }
    
    function getadjustmentnotes(){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            checkroles(1, 0, 0);

            $data['nav'] = 'my_business';
            $data['subnav'] = 'business_brands';

            $startdate= $this->input->post('startdate');
            $enddate= $this->input->post('enddate');

            if($startdate==null){$startdate=date('Y-m-d 00:00');}
            if($enddate==null){$enddate=date('Y-m-d 23:59');}
            
            $data['adjnotes'] = $this->product_model->get_adjustment_notes($startdate, $enddate);
                        
            $data['startdate']=$startdate;
            $data['enddate']=$enddate;

            $this->load->view('includes/header', $data);
            $this->load->view('setting/adjustment_notes');
            $this->load->view('includes/footer');
        
        }
        
        
    }
    
    function getbatchadjustmentnotes(){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            checkroles(1, 0, 0);

            $data['nav'] = 'my_business';
            $data['subnav'] = 'business_brands';

            $data['adjnotes'] = $this->product_model->getbatchadjustmentnotes($this->input->post('batchid'));


            $this->load->view('includes/header', $data);
            $this->load->view('setting/adjustment_notes');
            $this->load->view('includes/footer');
        
        }
    }


    

    // =====================>  New Code jun-14-2024 Start  <================== 
    public function add_product_images($product_id='', $store_id=''){ 

        if(null !== $this->input->get('product_id') && $this->input->get('product_id')!==""){
            $product_id=$this->input->get('product_id');
        }
        
        $data['nav'] = 'procurement';
        $data['subnav'] = 'business_brands';

        $data['business'] = $this->business_model->getbusinessdetails();
        $data['product'] = $this->product_model->get_product_by_id($product_id);  
        $data['get_product_images'] = $this->product_model->get_product_images($product_id);
         
        $this->load->view('includes/header', $data);
        $this->load->view('setting/add_product_images');
        $this->load->view('includes/footer'); 
     }  

    public function store_product_images(){      
        $product_id = $this->input->post('id_business_products');
        $uploaded_images = $_FILES;   
        $allowed_extensions = array('png', 'jpg', 'jpeg');
        $is_valid = true;
        $error_messages = [];
    
        // Validation
        foreach ($uploaded_images['product_image']['name'] as $key => $name) {
            $filename = $uploaded_images['product_image']['name'][$key];
            $file_extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
            
            if (!in_array($file_extension, $allowed_extensions)) {
                $is_valid = false;
                $error_messages[] = "Error: Only PNG, JPG, and JPEG files are allowed.";
                break;  
            }
        }
    
        if (!$is_valid) {
            $this->session->set_flashdata('error_msg', implode(' ', $error_messages));
            redirect("addproductimage/$product_id"); 
            exit;
        }
          $checking_first_image=0;  
        foreach ($uploaded_images['product_image']['name'] as $key => $name) {
            $filename = $uploaded_images['product_image']['name'][$key];
            $tmp_name = $uploaded_images['product_image']['tmp_name'][$key];
            $file_path = time().'.'.$filename;
            $destination = 'assets/images/productImages/'.$file_path;
        
            if (move_uploaded_file($tmp_name, $destination)) {  
                $sql="SELECT  `business_products`.`product_image` FROM `business_products` where business_id = ? and id_business_products =?";
                $resp = $this->db->query($sql, array($this->session->userdata('businessid'),$product_id));
                $img_d = $resp->row(); 
                 if($img_d->product_image !="" || $img_d->product_image != null){
                    $checking_first_image++;
                 }
                if($checking_first_image==0){
                    $data = array(
                        'product_id' => $product_id,
                        'business_id' => $this->session->userdata('businessid'),
                        'created_by' => $this->session->userdata('username'),
                        'created_at' => date('Y-m-d H:i:s'),
                        'status' => 'main',
                        'image_name' => $filename,
                        'image_path' => $file_path,
                    );  

                    $sql = "UPDATE `business_products` SET `product_image` = ? WHERE `id_business_products` = ?";
                    $resp = $this->db->query($sql, array($file_path,$product_id));    

                    $checking_first_image++;
                }else{ 

                    $data = array(
                        'product_id' => $product_id,
                        'business_id' => $this->session->userdata('businessid'),
                        'created_by' => $this->session->userdata('username'),
                        'created_at' => date('Y-m-d H:i:s'),
                        'status' => 'ads',
                        'image_name' => $filename,
                        'image_path' => $file_path,
                    );

                }   
                
     
                if ($this->db->insert('product_image', $data)) {
                    $this->session->set_flashdata('success_msg', 'Data inserted successfully.');
                } else {
                    $this->session->set_flashdata('error_msg', 'Failed to insert data.');
                    redirect("addproductimage/$product_id");
                    exit;
                }
            } else {
                $this->session->set_flashdata('error_msg', 'Failed to move uploaded file.');
                redirect("addproductimage/$product_id");
                exit;
            }
        }
       
        $this->session->set_flashdata('success_msg', ' Images uploaded and data inserted successfully.');
        redirect("addproductimage/$product_id"); 
        exit;
    }  

    public function edit_product_image(){
        $data = $this->product_model->edit_product_image_modal();
        echo (json_encode($data));
        die;
    }
    public function update_product_image(){
        $data = $this->product_model->update_product_image_model();
        echo (json_encode($data));
        die;
    } 

    public function delete_product_image(){
        $data = $this->product_model->delete_product_image_modal();
        echo (json_encode($data));
        die;
    }  
    
    public function product_categories(){  
          $data['nav'] = 'procurement';
          $data['subnav'] = 'product_category';
          $data['business'] = $this->business_model->getbusinessdetails(); 
          $sql = "SELECT 
                    business_products.category
                FROM 
                    business_products 
                WHERE 
                    business_products.business_product_active = 'Yes' 
                    AND business_products.business_id = ? 
                    AND business_products.category IS NOT NULL 
                    AND business_products.category != ''
                GROUP BY 
                    business_products.category;
                ";
 
         $resp = $this->db->query($sql, array($this->session->userdata('businessid')));
         $data = $resp->result();
         $data['products_category']=$data;
         $this->load->view('includes/header', $data);
         $this->load->view('setting/products_category');
         $this->load->view('includes/footer'); 
    }  

    public function start_advertisement($category ='',$product_id=''){ 
        $data['chart']='nochart';
        $data['nodatatable']='nodatatable';
        $data['menu'] = 'hidden';  

        if($product_id != 0){ 
          $sql="SELECT  product_image.image_path FROM `business_products` INNER join product_image on product_image.product_id = `business_products`.`id_business_products`
            where product_image.status='ads' and product_image.deleted_at is NULL and product_image.product_id =? and `business_products`.business_id =?";
           $resp = $this->db->query($sql, array($product_id, $this->session->userdata('businessid')));
           $ads_images = $resp->result(); 
           $data['ads_images'] = $ads_images; 
        }elseif($product_id == 0){ 
            $sql="SELECT  product_image.image_path FROM `business_products` INNER join product_image on product_image.product_id = `business_products`.`id_business_products`
            where product_image.status='ads' and product_image.deleted_at is NULL and business_products.category =? and `business_products`.business_id =?";
           $resp = $this->db->query($sql, array($category, $this->session->userdata('businessid')));
           $ads_images = $resp->result(); 
           $data['ads_images'] = $ads_images; 
        }
    
        // $this->load->view('includes/header1', $data);
        $this->load->view('restaurant/advertisement_view',$data);
        // $this->load->view('includes/footer');
     }  
    // =====================>  New Code jun-14-2024 end  <==================
}
