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

class Supplierpricelist_model extends CI_Model {

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

  //GET TARIFF AUTOCOMPLETE
  function gettariffautocomplete($term)
    {

        $this->db->select('ID, NAME');
        $this->db->like('NAME', $term);
        return $this->db->get("TARIFFAREAS")->result();
    }
        //GET TARIFF
function getsupplierpricelist()
        {
$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 = 'id';
if (!$sord) $sord = 'asc';

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

//Total Returned Rows Count
$this->db->from('SUPPLIER_PRICELISTS');
$count = $this->db->count_all_results();

//Total Pages
if( $count > 0 && $limit > 0) {
$total_pages = ceil($count/$limit);
} else {
$total_pages = 0;
}

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

//Set start record number
$start = $limit*$page - $limit;

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



$this->db->select("SUPPLIER_PRICELISTS.ID,SUPPLIER_PRICELISTS.PRICELISTNAME,SUPPLIERS.NAME AS SUPPLIER,SUPPLIER_PRICELISTS.VALIDFROM,SUPPLIER_PRICELISTS.ACTIVE");
$this->db->join("SUPPLIERS","SUPPLIER_PRICELISTS.SUPPLIER = SUPPLIERS.ID","LEFT");
$this->db->order_by($sidx, $sord);
$this->db->limit($limit, $start);

$rows = $this->db->get("SUPPLIER_PRICELISTS")->result_array();

$i=0;
foreach($rows as $row){
if ($row['ACTIVE'] == "0") {
                $row['ACTIVE'] = "Inactive";
            } else {
                $row['ACTIVE'] = "Active";
            }

$data['rows'][$i]['id']=$i+1;
$data['rows'][$i]['cell']=array($row['ID'],$row['PRICELISTNAME'],$row['SUPPLIER'],$row['VALIDFROM'],$row['ACTIVE']);

$i++;
}

return json_encode($data);
        }
function addsupplierprice($arr) {
		return ($this->db->insert('SUPPLIER_PRICELISTS', $arr)) ? true : false;
	}
 function addsupplierprice_prod($arr) {
		return ($this->db->insert('SUPPLIER_PRICELISTS_PRODUCTS', $arr)) ? true : false;
	}
function getGroupById($id) {
        $this->db->where('ID', $id);
        return $this->db->get('SUPPLIER_PRICELISTS')->result();
    }
 public function getlines($id) {
        $table_name = "SUPPLIER_PRICELISTS_PRODUCTS";
        $this->db->select("SUPPLIER_PRICELISTS.PRICELISTNAME ,PRODUCTS.NAME AS MATERIAL,SUPPLIER_PRICELISTS_PRODUCTS.LISTPRICE AS PRICE")->from($table_name);
        $this->db->where('SUPPLIER_PRICELISTS_PRODUCTS.PRICELIST', $id);
        $this->db->join("PRODUCTS","PRODUCTS.ID = SUPPLIER_PRICELISTS_PRODUCTS.PRODUCT","LEFT");
                $this->db->join("SUPPLIER_PRICELISTS","SUPPLIER_PRICELISTS.ID = SUPPLIER_PRICELISTS_PRODUCTS.PRICELIST","LEFT");
                return $this->db->get()->result();
    }
      function updatesupplierprice($id, $LISTNAME, $SUPPLIER, $VALIDFROM, $ACTIVE) {
        $this->db->where('ID', $id);
        $update = array(
            'PRICELISTNAME' => $LISTNAME,
            'SUPPLIER'=> $SUPPLIER,
            'VALIDFROM'=> $VALIDFROM,
            'ACTIVE'=> $ACTIVE
                    );
        return ($this->db->update('SUPPLIER_PRICELISTS', $update)) ? true : false;
    }
 function deleteLines($id) {
        return ($this->db->delete('SUPPLIER_PRICELISTS_PRODUCTS', array('PRICELIST' => $id))) ? true : false;
    }
   function deletepricelist($id) {
		return ($this->db->delete('SUPPLIER_PRICELISTS', array('ID' => $id))) ? true : false;
	}
//        function tariffData($id) {
//		$this->db->where('ID', $id);
//		return $this->db->get('TARIFFAREAS')->result();
//	}
}