<?php

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

class Suppliers_model extends CI_Model {

    function getSupplierByString($string) {
        return $this->db->query("SELECT NAME FROM SUPPLIERS WHERE NAME LIKE '%$string%' LIMIT 10")->result();
    }

//GET SUPPLIERS
    function getsuppliers() {
        //Fetch data
        $this->db->select("SUPPLIERS.ID,SUPPLIERS.SEARCHKEY, SUPPLIERS.NAME, SUPPLIERS.CARD, SUPPLIERS.MAXDEBT, SUPPLIERS.CURDEBT, T.NAME AS TAXCATEGORY, SUPPLIERS.ADDRESS,SUPPLIERS.PHONE,SUPPLIERS.EMAIL,SUPPLIERS.FAX,SUPPLIERS.NOTES,SUPPLIERS.TAXCATEGORY AS TAXCATID");
        $this->db->join("TAXCUSTCATEGORIES T", 'SUPPLIERS.TAXCATEGORY=T.ID' ,'LEFT');
        $this->db->order_by("SUPPLIERS.NAME", "asc");
        $rows = $this->db->get("SUPPLIERS")->result_array();
//        echo $this->db->last_query();exit;
        return $rows;
    }
    
    
    
    function getsupplierrsautocomplete($term) {
        $this->db->select('S.ID as id, S.NAME AS value');
        $where = "S.NAME LIKE '". $term . "%'";
        $this->db->where($where);
        return $this->db->get("SUPPLIERS S")->result();
    }

   function getSupplierById($id) {
        $this->db->where('ID', $id);
        return $this->db->get('SUPPLIERS')->result();
    }

   function getSupplierCategories() {
        $this->db->select('ID, NAME');
        return $this->db->get('TAXCUSTCATEGORIES')->result();
    }
   function getsupplierautocomplete($term) {
        $this->db->select('ID,TAXID, NAME,ADDRESS,POSTAL,CITY,REGION,COUNTRY,PHONE,EMAIL');
        $where = "NAME LIKE '". $term. "%'";
        $this->db->where($where);
        return $this->db->get("SUPPLIERS")->result();
   }
   function getsuppliercardautocomplete($term) {
        $this->db->select('ID, CARD');
        $where = "CARD LIKE '". $term. "%'";
        $this->db->where($where);
        return $this->db->get("SUPPLIERS")->result();
   }
   function getsuppliercategoriesautocomplete($term) {
        $this->db->select('TAXCUSTCATEGORIES.NAME AS TAXCATEGORY');
        $where = "TAXCUSTCATEGORIES.NAME LIKE '". $term. "%'";
        $this->db->where($where);
        return $this->db->get("TAXCUSTCATEGORIES")->result();
    }     
    function getsuppliersautocomplete($term) {
        $this->db->select('ID, NAME');
        $this->db->like('NAME', $term);
        return $this->db->get("SUPPLIERS")->result();
    }
    function getsearchkeyautocomplete($term) {
        $this->db->select('ID, SEARCHKEY');
        $this->db->from('SUPPLIERS');
        $where = "SEARCHKEY LIKE '". $term. "%'";
        $this->db->where($where);
        return $this->db->get()->result();       
    }
    function getuomidbn($name) {
        $this->db->where('NAME', $name);
        $q = $this->db->get('UNITS')->result();
        return $q[0]->ID;
    }

    function getsupplieridbn($name) {
        $this->db->where('NAME', $name);
        $q = $this->db->get('SUPPLIERS')->result();
        return $q[0]->ID;
    }
    function getsuppliernamebid($id) {
        $this->db->where('ID', $id);
        $q = $this->db->get('SUPPLIERS')->result();
        return $q[0]->NAME;
    }
    function getsidbn($name) {
        $this->db->where('NAME', $name);
        $q = $this->db->get('SUPPLIERS')->result();
        return $q[0]->ID;
    }
    function getcustcat($name) {
        $this->db->where('NAME', $name);
        $q = $this->db->get('TAXCUSTCATEGORIES')->result();
        return $q[0]->ID;
    }
    function add($form_data){
        var_dump($form_data);
        $this->db->insert('SUPPLIERS', $form_data);
		if ($this->db->affected_rows() == '1')
		{
			return TRUE;
		}
        return FALSE;
    }
    function update($id, $form_data){
        $this->db->where('ID', $id);
        $this->db->update('SUPPLIERS', $form_data);
		if ($this->db->affected_rows() == '1')
		{
			return TRUE;
		}
        return FALSE;
    }
     function delete($id) {
        return ($this->db->delete('SUPPLIERS', array('ID' => $id))) ? true : false;
    }
  
}
