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

class Popularproducts extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('sales/popularproducts_model');
    }

    public function index() {
      
          $data['template'] = 'sales/popularproducts_view';
          $this->load->view('template',$data);
    }

    //  COLUNM NAMES
    function getcolumns() {

        $params = $this->uri->segment(3);
        if ($params == 1 || $params == 0) {
            $data = '{"cnames": ["","",""], "url": "popularproducts/getpopularproducts",
                "colModel": [
{"name":"NAME","index":"NAME", "width":"100"},
{"name":"AMOUNT","index":"AMOUNT", "width":"100","align":"right"},
{"name":"PERCENTAGE","index":"PERCENTAGE","width":"100","align":"right"}
]}';
        } else {
            $data = '{"cnames": ["","",""], "url": "popularproducts/getpopularproducts"}';
        }
        echo $data;
    }

    // GET FOOD COSTING
    function getpopularproducts() {
        $datefrom = $this->input->post('datefrom');
        $dateto = $this->input->post('dateto');
        $data['data'] = $this->popularproducts_model->getpopularproducts($datefrom, $dateto);
          echo json_encode($data);
        //print_r($data['productsale'] ); die();
     /*?>
         <table class="table table-striped table-hover table-bordered" id="simpledatatable">
                                        <thead>
                                            <tr role="row">
                                                <th>
                                                    ID
                                                </th>
                                                <th>
                                                    PRODUCT
                                                </th>
                                                <th>
                                                    QUANTITY
                                                </th>
                                    
                                                   <th>
                                                %
                                                </th>
                                                   <th>
                                                AMOUNT
                                                </th>
                                                   <th>
                                                %
                                                </th>
                                            </tr>
                                        </thead>
                                                              
          <tbody>
            <?php
         $i = 1;
        $totalqty = 0.0;
        $totalamount = 0.0; 
          foreach ($data['productsale'] as $row) { ?>
          <tr>
          <?php
            $totalqty = $totalqty + $row->QTY;
            $totalamount = $totalamount + $row->AMOUNT;

            if ($row->QTY != 0 && $row->AMOUNT != 0) 
                {
                $qtypct = number_format(($row->QTY / $totalqty) * 100, 2, '.', ',') . '%';
                $marginpct = (($row->AMOUNT - $row->QTY) / $row->AMOUNT) * 100;
                $amountpct = number_format(($row->AMOUNT / $totalamount) * 100, 2, '.', ',') . '%';
                $margin = number_format(($row->AMOUNT - $row->QTY), 2, '.', ',');
            } else {
                $qtypct = 0;
                $marginpct = 0;
                $amountpct = 0;
                $margin = 0;
            }?>

                   <td><?php echo $i;?></td>
                   <td><?php echo $row->NAME;?></td>
                   <td><?php echo $row->QTY;?></td>
                   <td><?php echo $qtypct;?></td>
                     <td><?php echo $row->AMOUNT;?></td>
                       <td><?php echo $amountpct;?></td>
          <?php 
         $i++;
        }

       ?>
       </tr> </tbody>
    </table>
  <?php*/
    }

}