<?php
class Manufacturer_model extends CI_Model
{

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

    function isDuplicate($data)
    {
        $sql = 'SELECT mfg_id
                FROM tbl_manufacturer
                WHERE merchant_row_id = ' . $this->db->escape($data['merchant_row_id']) . '
                AND mfg_name = ' . $this->db->escape($data['mfg_name']);

        if(!empty($data['mfg_id']))
        {
            $sql .= ' AND mfg_id != ' . $this->db->escape($data['mfg_id']);
        }

        $query=$this->db->query($sql);

        if($query->num_rows())
        {
            return true;
        }

        return false;
    }

	function addManufacturer ($data)
	{
        $sql= 'INSERT INTO tbl_manufacturer(merchant_row_id,mfg_name,creation_date,manufacturer_info)
              VALUES(' . $this->db->escape($data['merchant_row_id']) . ',
                     ' . $this->db->escape($data['mfg_name']) . ',
                     ' . $this->db->escape($data['creation_date']) . ',
                     ' . $this->db->escape($data['manufacturer_info']) . ')';

        $this->db->query($sql);
        return $this->db->insert_id();
	}

	function getAllManufacturer ()
	{
        $sql="SELECT *
              FROM tbl_manufacturer
              WHERE merchant_row_id=".$this->db->escape(getCurrentMerchantId());
        $query=$this->db->query($sql);
		$result = $query->result_array();

		return $result;
	}
    function getManufacturerWithId ($mfg_id=0,$merchant_row_id=0)
    {
        $sql="SELECT *
              FROM tbl_manufacturer
              WHERE merchant_row_id=".$this->db->escape($merchant_row_id)." AND mfg_id=".$this->db->escape($mfg_id);
        $query=$this->db->query($sql);
        $result = $query->row();

        return $result;
    }

    function updateManufacturer($data)
    {
        $sql = 'UPDATE tbl_manufacturer
                SET mfg_name = '. $this->db->escape($data['mfg_name']) . ',
                modify_date = ' . $this->db->escape($data['modify_date']) . ',
                manufacturer_info = ' . $this->db->escape($data['manufacturer_info']) . '
                WHERE merchant_row_id = ' . $this->db->escape($data['merchant_row_id']) . '
                AND mfg_id = ' . $this->db->escape($data['mfg_id']);

        $this->db->query($sql);
    }

    function delete_manufacturer($mfg_id, $merchant_row_id)
    {
        $sql = 'DELETE FROM tbl_manufacturer
                WHERE merchant_row_id = ' . $this->db->escape($merchant_row_id) . '
                AND mfg_id = ' . $this->db->escape($mfg_id);

        $this->db->query($sql);
    }
    function getAllProductsNotIn($merchant_row_id=0,$mfg_id=0)
    {
        $condition='';
        if($mfg_id!=0)
        {
            $condition=" AND prod.manufacturer_id!=".$this->db->escape($mfg_id);
        }
        $sql="SELECT prod.product_row_id,
                     prod.product_description,
                     prod.product_price,
                     man.mfg_name";

        if($mfg_id!=0){
        $sql .=     ", CONCAT(\"<input type=checkbox name=checkbox[] type=checkbox value= \",prod.product_row_id,\" />\") AS select_product";
       }


        $sql.= " FROM tbl_product prod
              LEFT JOIN tbl_manufacturer man ON man.mfg_id=prod.manufacturer_id
              WHERE prod.merchant_row_id=".$this->db->escape($merchant_row_id).$condition."
              AND prod.active_status = 1 AND prod.is_editable = 1 AND  prod.is_gift_certificate = 0 ORDER BY prod.product_description";

              $sql = '('.$sql.' ) AS abc';

        $this->load->library ( 'rapyd' );
        $this->rapyd->load('datagrid');

        $grid = new DataGrid('', $sql);
        $grid->per_page = $this->config->item('rows_per_page');
        if($mfg_id!=null){
        $grid->column("<input type='checkbox' class='checkbox_checkall' />", "<#select_product#>", 'align="left" width="1%"');
        }
        $grid->column('Product Name', 'product_description', 'align="left" width="45%"');
        $grid->column('Product Manufacturer', 'mfg_name', 'align="left" width="30%"');
        $grid->column('Product Price', 'product_price', 'align="left" width="15%"');

        if($mfg_id!=0){
        $grid->column_detail('Action', 'Assign', 'products/product_manufacturer/assign/3/<#product_row_id#>/'.$mfg_id , 'align="left" class="link_style" width="10%" ', false , true);
        }else{
        $grid->column('Action', 'Assign');
        }
        $grid->build();
        return $grid->output;


        $query=$this->db->query($sql);
        $result=$query->result_array();

        return $result;
    }
    function getAllProductsIn($merchant_row_id=0,$mfg_id=0)
    {

        $sql='SELECT prod.product_row_id,
                     prod.product_description,
                     prod.product_price,
                     man.mfg_name';
        if($mfg_id!=0){
        $sql .=     ', CONCAT(\'<input type=checkbox name=checkbox[] type=checkbox value= \',prod.product_row_id,\' />\') AS select_product ';
       }

         $sql .= '  FROM tbl_product prod
              LEFT JOIN tbl_manufacturer man ON man.mfg_id=prod.manufacturer_id WHERE prod.merchant_row_id='.$this->db->escape($merchant_row_id).' AND prod.manufacturer_id='.$this->db->escape($mfg_id).'
              AND prod.active_status = 1 AND prod.is_editable = 1 AND  prod.is_gift_certificate = 0 ORDER BY prod.product_description';


        $sql = '('.$sql.' ) AS abc';

        $this->load->library ( 'rapyd' );
        $this->rapyd->load('datagrid');

        $grid = new DataGrid('', $sql);

        if($mfg_id!=0){
        $grid->column("<input type='checkbox' class='checkbox_checkall' />", "<#select_product#>", 'align="left" width="1%"');
        }
        $grid->column('Product Name', 'product_description', 'align="left" width="45%"');
        $grid->column('Product Manufacturer', 'mfg_name', 'align="left" width="30%"');
        $grid->column('Product Price', 'product_price', 'align="left" width="15%"');
        if($mfg_id!=0){
        $grid->column_detail('Unassign', 'Unassign', 'products/product_manufacturer/assign/2/<#product_row_id#>/'.$mfg_id , 'align="left" class="link_style" width="10%" ', false , true);
        }else{
        $grid->column('Action', 'Assign');
        }
        $grid->build();
        return $grid->output;

    }
    function assignProd($prod_id=0, $mfg_id=0, $merchant_row_id=0)
    {
        $sql="UPDATE tbl_product SET manufacturer_id=".$this->db->escape($mfg_id)."
              WHERE merchant_row_id=".$this->db->escape($merchant_row_id)." AND product_id=".$this->db->escape($prod_id);
        $this->db->query($sql);
        return $this->db->affected_rows();
    }
    function unassignProd($prod_id=0, $mfg_id=0, $merchant_row_id=0)
    {
       $sql="UPDATE tbl_product SET manufacturer_id=''
              WHERE merchant_row_id=".$this->db->escape($merchant_row_id)." AND product_id=".$this->db->escape($prod_id);
        $this->db->query($sql);
        return $this->db->affected_rows();
    }

    function getManufacturerId($merchant_row_id=0,$product_row_id=0)
    {
        $sql="SELECT manufacturer_id FROM tbl_product WHERE merchant_row_id=".$this->db->escape($merchant_row_id)."
              AND product_row_id=".$this->db->escape($product_row_id);
        $query=$this->db->query($sql);

        return $query->row()->manufacturer_id;
    }

    function getManufacturerIdCSV($manufacturer)
    {
        $manufacturer_id=0;
        $sql="SELECT mfg_id
              FROM tbl_manufacturer
              WHERE merchant_row_id=".$this->db->escape($manufacturer['merchant_row_id'])."
              AND mfg_name=".$this->db->escape($manufacturer['manufacturer']);
        $query=$this->db->query($sql);
        $result=$query->row();
        $manufacturer_id=$result->mfg_id;
        if(!$manufacturer_id && !empty($manufacturer['manufacturer']))
        {
            $current_date=date('Y-m-d H:i:s');

            $sql="INSERT INTO tbl_manufacturer(merchant_row_id,mfg_name,creation_date)
                  VALUES(".$this->db->escape($manufacturer['merchant_row_id']).",
                         ".$this->db->escape($manufacturer['manufacturer']).",
                         ".$this->db->escape($current_date)."
                  )";
            $this->db->query($sql);
            $manufacturer_id = $this->db->insert_id();
        }
        if(!$manufacturer_id)
        {
            $manufacturer_id=0;
        }
        return $manufacturer_id;
    }

    function getAllManufacturersForAPI($merchant_row_id)
    {
        $sql = 'SELECT  mfg_id,
                        mfg_name,
                        creation_date,
                        modify_date
                FROM tbl_manufacturer
                WHERE merchant_row_id = ' . $this->db->escape($merchant_row_id);
        $query=$this->db->query($sql);

        return $query->result_array();
    }

  function getProdByManGrid($search){

        if(!empty($search['under_product_id']))
        {
            $search_sql .= ' AND p.product_id = \''.$search['under_product_id'].'\'';
        }
        if(!empty($search['under_Product_desc']))
        {
            $search_sql .= ' AND p.product_description LIKE \'%'.$search['under_Product_desc'].'%\'';
        }
        if(!empty($search['under_category_id']))
        {
            $search_sql .= ' AND p.category_id = \''.$search['under_category_id'].'\'';
        }
        if(!empty($search['mainCatCombo']))
        {
            $mfg_id  =  $search['mainCatCombo'];
        }
        $currency_symbol = $_SESSION['currency_symbol'];

        $sql = 'SELECT p.product_id AS product_id_g,
                    p.product_description AS product_description_g,
                    (SELECT mfg_name FROM tbl_manufacturer WHERE mfg_id = p.manufacturer_id) AS manufacturer_name,
                    CONCAT(\''.$currency_symbol.'\',p.product_price) AS product_price_g ';
        if($mfg_id!=0){
        $sql .= ', CONCAT(\'<input type=checkbox name=checkbox[] type=checkbox value= \',p.product_id,\' />\') AS select_product ';
        }
        $sql .= ' FROM tbl_product p
                LEFT OUTER JOIN tbl_manufacturer man ON p.manufacturer_id = man.mfg_id
                WHERE p.merchant_row_id = '.getCurrentMerchantId() .'
                AND p.active_status = 1'.$search_sql.
                ' AND p.is_editable = 1';
        if($mfg_id!=0)
        {
        $sql .= ' AND p.manufacturer_id = ' . $mfg_id;
        }
        $sql .=    ' AND p.is_gift_certificate = 0 ORDER BY p.product_description ASC';


        $sql = '('.$sql.' ) AS abc';

        $this->load->library ( 'rapyd' );
        $this->rapyd->load('datagrid');

        $grid = new DataGrid('', $sql);

        if($mfg_id!=0){
        $grid->column("<input type='checkbox' class='checkbox_checkall' />", "<#select_product#>", 'align="left" width="1%"');
        }
        $grid->column('Product Name', 'product_description_g', 'align="left" width="45%"');
        $grid->column('Product Manufacturer', 'manufacturer_name', 'align="left" width="30%"');
        $grid->column('Product Price', 'product_price_g', 'align="left" width="15%"');
        if($mfg_id!=0){
        $grid->column_detail('Unassign', 'Unassign', 'products/product_manufacturer/assign/2/<#product_id_g#>/'.$mfg_id , 'align="left" class="link_style" width="10%" ', false , true);
        }else{
        $grid->column('Action', 'Assign');
        }
        $grid->build();
        return $grid->output;
    }

    function getProdNotByManGrid($search)
    {
        if(!empty($search['product_id']))
        {
            $search_sql .= ' AND p.product_id = \''.$search['product_id'].'\'';
        }
        if(!empty($search['Product_desc']))
        {
            $search_sql .= ' AND p.product_description LIKE \'%'.$search['Product_desc'].'%\'';
        }
        if(!empty($search['category_id']))
        {
            $search_sql .= ' AND p.category_id = \''.$search['category_id'].'\'';
        }
        if(!empty($search['mainCatCombo']))
        {
            $mfg_id  =  $search['mainCatCombo'];
        }

        $currency_symbol = $_SESSION['currency_symbol'];

        $sql = 'SELECT p.product_id AS product_id_g,
                    p.product_description AS product_description_g,
                    (SELECT mfg_name FROM tbl_manufacturer WHERE mfg_id = p.manufacturer_id) AS manufacturer_name,
                    CONCAT(\''.$currency_symbol.'\',p.product_price) AS product_price_g ';
        if($mfg_id!=0){
        $sql .=     ', CONCAT(\'<input type=checkbox name=checkbox[] type=checkbox value= \',p.product_id,\' />\') AS select_product ';
        }
        $sql .=   ' FROM tbl_product p
                    LEFT OUTER JOIN tbl_category c ON p.category_id = c.category_id
                    WHERE p.merchant_row_id = '.getCurrentMerchantId() .'
                    AND p.active_status = 1'.$search_sql.
                    ' AND p.is_editable = 1';
        if($mfg_id!=0)
        {
        $sql .= ' AND p.manufacturer_id != ' . $mfg_id;
        }
        $sql .=    ' AND p.is_gift_certificate = 0 ORDER BY p.product_description ASC';

        $sql = '('.$sql.' ) AS abc';

        $this->load->library ( 'rapyd' );
        $this->rapyd->load('datagrid');
        $grid = new DataGrid('', $sql);
        $grid->per_page = $this->config->item('rows_per_page');
        if($mfg_id!=0){
        $grid->column("<input type='checkbox' class='checkbox_checkall' />", "<#select_product#>", 'align="left" width="1%"');
        }
        $grid->column('Product Name', 'product_description_g', 'align="left" width="55%"');
        $grid->column('Product Manufacturer', 'manufacturer_name', 'align="left" width="20%"');
        $grid->column('Product Price', 'product_price_g', 'align="left" width="15%"');
        if($mfg_id!=0){
        $grid->column_detail('Action', 'Assign', 'products/product_manufacturer/assign/3/<#product_id_g#>/'.$mfg_id , 'align="left" class="link_style" width="10%" ', false , true);
        }else{
        $grid->column('Action', 'Assign');
        }
        $grid->build();
        return $grid->output;
    }


}
?>