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

class Productsearch_model extends CI_Model {

	public function __construct() {
		parent::__construct();
	}

                function getproductsbyparam($barcode, $product, $buyparam, $buyprice, $sellparam, $sellprice, $cp, $cm, $ca){


            $page = $this->input->post('page');
        $limit = $this->input->post('rows'); // get how many rows we want to have into the grid
        $sidx = $this->input->post('sidx'); // get index row - i.e. user click to sort
        $sord = $this->input->post('sord'); // get the direction
//Default values
        if (!$sidx)
            $sidx = 'P.NAME';
        if (!$sord)
            $sord = 'ASC';

        if (!$page)
            $page = 1;
        if (!$limit)
            $limit = 25;

//Total Returned Rows
        if($barcode)
            $this->db->like('P.CODE', $barcode);
        if($product)
            $this->db->like('P.NAME', $product);
        if($cp)
            $this->db->like('P.CATEGORY', $cp);
        if($cm)
            $this->db->like('P.CATEGORY', $cm);
        if($ca)
            $this->db->like('P.CATEGORY', $ca);
        if($buyparam && $buyprice)
            $this->db->where('P.PRICEBUY '.$buyparam , $buyprice);
        if($sellparam && $sellprice)
            $this->db->where('P.PRICESELL '.$sellparam , $sellprice);

        $this->db->join('STOCKCURRENT AS SC', 'P.ID = SC.PRODUCT AND SC.LOCATION = "0"', 'left');
        $this->db->from('PRODUCTS AS P');
        $count = $this->db->count_all_results();

//Total Pages
        if ($count > 0 && $limit > 0) {
            $total_pages = ceil($count / $limit);
            //Set start record number
        $start = $limit * $page - $limit;
        } else {
            $total_pages = 0;
            //Set start record number
        $start = 0;

        }

//Check requested page
        if ($page > $total_pages)
            $page = $total_pages;


//Format data for jqGrid output
        $data['page'] = "" . $page . "";
        $data['total'] = "" . $total_pages . "";
        $data['records'] = "" . $count . "";


//Fetch data
        if($barcode)
            $this->db->like('P.CODE', $barcode);
        if($product)
            $this->db->like('P.NAME', $product);
        if($cp)
            $this->db->like('P.CATEGORY', $cp);
         if($cm)
            $this->db->like('P.CATEGORY', $cm);
            if($ca)
            $this->db->like('P.CATEGORY', $ca);
        if($buyparam && $buyprice)
            $this->db->where('P.PRICEBUY '.$buyparam , $buyprice);
        if($sellparam && $sellprice)
            $this->db->where('P.PRICESELL '.$sellparam , $sellprice);

        $this->db->select('P.NAME AS PRODUCT, P.PRICEBUY, P.PRICESELL, SC.UNITS');
        $this->db->join('STOCKCURRENT AS SC', 'P.ID = SC.PRODUCT AND SC.LOCATION = "0"', 'left');
        $this->db->order_by($sidx, $sord);
        $this->db->limit($limit, $start);
        $rows = $this->db->get('PRODUCTS AS P')->result_array();

//Format data
        $i = 0;
        foreach ($rows as $row) {
            $data['rows'][$i]['id'] = $i + 1;
            $data['rows'][$i]['cell'] = array($row['PRODUCT'], $row['PRICEBUY'], $row['PRICESELL'], $row['UNITS']);
            $i++;
        }
        return json_encode($data);


        }
}
