<?php

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

class salereport extends MY_Controller {

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

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

    //  COLUNM NAMES
   

    // GET FOOD COSTING
    function getproductsale() {
         $category = $this->input->post('category');
         $datefrom = $this->input->post('datefrom');
         $dateto = $this->input->post('dateto');
        
         //$data['productsale'] = $this->salereport_model->getproductsale($category,$datefrom, $dateto);
       //  print_r($data['productsale']); die();

            $data['data'] = $this->salereport_model->getproductsale($category,$datefrom, $dateto);;
        echo json_encode($data);

        /* ?>

           <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>
                                                 COST
                                                </th>
                                                   <th>
                                                 TOTAL
                                                </th>
                                                   <th>
                                                 PROFIT
                                                </th>
                                            </tr>
                                        </thead>
                                                              
          <tbody>
         <tr><td colspan="6">Item: <?php echo count($data['productsale']);?></td></tr>
          <?php $i=1; 
          $totalcount = 0;$costcount=0;$profitcount = 0;
          foreach($data['productsale'] as $productsale1) { ?>            
               <tr>
                   <td><?php echo $i;?></td>
                   <td><?php echo $productsale1->PRODUCT;?></td>
                   <td><?php echo $productsale1->UNITS;?></td>
                   <td><?php echo $productsale1->COST;?></td>
                   <td><?php echo $productsale1->TOTAL;?></td>
                   <td><?php echo ($productsale1->TOTAL - $productsale1->COST);?></td>
                 </tr>
          <?php $i++; 
          $totalcount = $totalcount + $productsale1->TOTAL;
          $costcount = $costcount + $productsale1->COST;
           $profitcount = $profitcount + ($productsale1->TOTAL - $productsale1->COST);
        }  ?>
        <tr>
                   <td colspan="3">Total:</td>
                   <td><?php echo $costcount;?></td>
                   <td><?php echo $totalcount;?></td>
                   <td><?php echo $profitcount;?></td>
        </tr>
             </tbody>
                                    </table>
        

            <?php*/
    }

 private function _getProdCategories($id = NULL) {
        $this->load->model('inventory/categories_model');
        $catarr = $this->categories_model->getproductcategories();

        $data = '<option value="">Select</option>';

        for ($i = 0; $i < count($catarr); $i++) {
            if ($id != NULL && $id == $catarr[$i]->ID)
                $data .= "<option id=\"option_custom\" selected=\"yes\" value=\"" . $catarr[$i]->ID . "\">" . $catarr[$i]->NAME . "</option>\n";
            else
                $data .= "<option id=\"option_custom\" value=\"" . $catarr[$i]->ID . "\">" . $catarr[$i]->NAME . "</option>\n";
        }
        return $data;
    }
    function exportcsv()
{


        $file="salereport.csv";
            header("Content-Disposition: attachment; filename=" . urlencode($file));
            header("Content-Type: application/force-download");
            header("Content-Type: application/octet-stream");
            header("Content-Type: application/download");
            header("Content-Description: File Transfer");
            header("Content-type: application/csv");
            header("Pragma: no-cache");
            header("Expires: 0");

            $export= $this->input->post('exportdata');


$this->load->helper('download');
force_download($file, $export);
}

 function getcolumns() {

        $params = $this->uri->segment(3);
        if ($params == 1 || $params == 0) {
            $data = '{"cnames": ["PRODUCT GROUP","CATEGORY","PRODUCT","PRICE","QUANTITY","TOTAL"], "url": "sales/salereport/getproductsale",
                "colModel": [
{"name":"REFERENCE","index":"REFERENCE","hidden":true, "width":"100", "sorttype":"date", "datefmt":"Y-mm-d"},
{"name":"CATEGORY","index":"CATEGORY", "width":"100", "sorttype":"date", "formatter":"date", "formatoptions": {"newformat":"d-M-Y"}},
{"name":"PRODUCT","index":"PRODUCT", "width":"100"},
{"name":"PRICE","index":"PRICE", "width":"100"},
{"name":"QUANTITY","index":"QUANTITY", "width":"100", "align":"center"},

{"name":"TOTAL","index":"TOTAL", "hidden":true,"width":"100", "align":"right"}
]}';
        } else if ($params ==42) {

            $data = '{"cnames": ["REQUIREDON","CATEGORY","PRODUCT","RQTY","QOH","UOM","POQTY","QTYSTS","TOTAL"], "url": "inventory/materials/getlinesbycategory",
                "colModel": [
{"name":"REQUIREDON","index":"REQUIREDON", "width":"50", "sorttype":"date", "formatter":"date", "formatoptions": {"newformat":"d-M-Y"}},
{"name":"CATEGORY","index":"CATEGORY", "width":"70"},
{"name":"PRODUCT","index":"PRODUCT", "width":"100"},
{"name":"RQTY","index":"APPROVEDQUANTITY", "width":"25"},
{"name":"QOH","index":"QOH", "width":"25"},
{"name":"UOM","index":"UOM", "width":"35"},
{"name":"POQTY","index":"POQTY", "width":"25"},
{"name":"QTYSTS","index":"QTYSTS", "width":"35"},
{"name":"TOTAL","index":"TOTAL", "width":"70", "align":"right", "formatter":"currency", "formatoptions": {"thousandsSeparator":","}}
]}';
        } else if ($params == 93) {
            $data = '{"cnames": ["REQUIREDON","LOCATION","TOTAL"], "url": "inventory/materialplanning/getlinesbylocation",
                "colModel": [
{"name":"REQUIREDON","index":"REQUIREDON", "width":"100", "sorttype":"date", "formatter":"date", "formatoptions": {"newformat":"d-M-Y"}},
{"name":"CATEGORY","index":"LOCATIONNAME", "width":"100"},
{"name":"TOTAL","index":"TOTAL", "width":"100", "align":"right", "formatter":"currency", "formatoptions": {"thousandsSeparator":","}}
]}';
        } else {
            $data = '{"cnames": ["PRODUCT GROUP","CATEGORY","PRODUCT","PRICE","QUANTITY","TOTAL"], "url": "sales/salereport/getproductsale"}';
        }
      //  echo $data;
        var_dump($data);
    }
}
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


