<?php
class Attribute_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    function getGroupList()
    {
        $sql = 'SELECT group_row_id,
                       group_name,
                       is_required
                FROM tbl_product_group
                WHERE merchant_row_id = ' . getCurrentMerchantId().' ORDER BY group_name';
        $query = $this->db->query($sql);

        return $query->result_array();
    }

    function deleteGroup($groupRowId)
    {
        if(!empty($groupRowId))
        {
            $this->db->where('group_row_id', $groupRowId);
            $this->db->delete('tbl_product_group');
            $attributes = $this->getAttributeByGroupId($groupRowId);
            foreach($attributes as $item)
            {
                $this->deleteAttribute($item['attribute_row_id']);
            }
        }
    }

    function getGroupById($groupRowId)
    {
        if(!empty($groupRowId))
        {
            $sql = "SELECT * FROM tbl_product_group WHERE group_row_id = {$groupRowId}";
            $query = $this->db->query($sql);
            return $query->row_array();
        }
        return false;
    }

    function getAttributeById($attributeRowId)
    {
        if(!empty($attributeRowId))
        {
            $sql = "SELECT * FROM tbl_product_attribute WHERE attribute_row_id = {$attributeRowId}";
            $query = $this->db->query($sql);
            return $query->row_array();
        }
        return false;
    }

    function saveGroup($info)
    {
        if($info['group_row_id'])
        {
            $this->db->where('group_row_id', $info['group_row_id']);
            return $this->db->update('tbl_product_group', $info);
        }
        else
        {
            $this->db->insert('tbl_product_group', $info);
            return $this->db->insert_id();
        }
    }

    function getAttributeByGroupId($groupRowId)
    {
        if(!empty($groupRowId))
        {
            $sql = "SELECT *FROM tbl_product_attribute
                    WHERE group_row_id = {$groupRowId} ORDER BY weight";
            $query = $this->db->query($sql);
            return $query->result_array();
        }
        return false;
    }

    function saveAttribute($info)
    {
        if($info['attribute_row_id'])
        {
            $this->db->where('attribute_row_id', $info['attribute_row_id']);
            return $this->db->update('tbl_product_attribute', $info);
        }
        else
        {
            $this->db->insert('tbl_product_attribute', $info);
            return $this->db->insert_id();
        }
    }

    function deleteAttribute($attributeRowId)
    {
        if(!empty($attributeRowId))
        {
            $this->db->where('attribute_row_id', $attributeRowId);
            $this->db->delete('tbl_product_attribute');
            $sql = "DELETE FROM tbl_product_attribute_group WHERE
                           attribute_row_id LIKE '%,{$attributeRowId},%' OR
                           attribute_row_id LIKE '{$attributeRowId},%' OR
                           attribute_row_id LIKE '%,{$attributeRowId}'";

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

    function getOptionByProductId($productRowId, $fields = null)
    {
        if(!empty($productRowId))
        {
            $select = $fields ? $fields : '*';
            $sql = "SELECT {$select} FROM tbl_product_attribute_group
                    WHERE product_row_id = " . $productRowId . "
                    ORDER BY attribute_group_row_id";
            $query = $this->db->query($sql);
            return $query->result_array();
        }
        return false;
    }

    function getOptionByAttributeGroupId($attributeGroupRowId)
    {
        if(!empty($attributeGroupRowId))
        {
            $sql = "SELECT *FROM tbl_product_attribute_group
                    WHERE attribute_group_row_id = " . $attributeGroupRowId;
            $query = $this->db->query($sql);
            return $query->row_array();
        }
        return false;
    }


    function getGroupAttribute($merchant_row_id)
    {
        if(!empty($merchant_row_id))
        {
            /*$sql = "SELECT pa.attribute_row_id,
                           pa.attribute_name,
                           pg.group_row_id,
                           pg.group_name,
                           pg.is_required
                           FROM tbl_product_attribute AS pa
                           JOIN tbl_product_group AS pg ON(pa.group_row_id = pg.group_row_id)
                           WHERE pg.merchant_row_id = " . $merchant_row_id . " ORDER BY pg.group_row_id, pa.attribute_name ASC";*/

            $sql = "SELECT pa.attribute_row_id,
                        pa.attribute_name,
                        pa.group_row_id,
                        pg.group_name,
                        pg.is_required
                        FROM tbl_product_attribute AS pa
                        INNER JOIN tbl_product_group AS pg ON pa.group_row_id = pg.group_row_id
                        WHERE pa.group_row_id IN (  SELECT group_row_id
                                                    FROM tbl_product_group
                                                    WHERE merchant_row_id = $merchant_row_id)
                        ORDER BY pg.group_name, pa.weight ASC";

            $query = $this->db->query($sql);
            return $query->result_array();
        }
        return false;
    }

    function deleteProductAttribute($attributeGroupRowId)
    {
        if(!empty($attributeGroupRowId))
        {
            $this->db->where('attribute_group_row_id', $attributeGroupRowId);
            return $this->db->delete('tbl_product_attribute_group');
        }
        return false;
    }


    function saveProductAttribute($data)
    {
        if($data['attribute_group_row_id'])
        {
            $this->db->where('attribute_group_row_id', $data['attribute_group_row_id']);
            return $this->db->update('tbl_product_attribute_group', $data);
        }
        return $this->db->insert('tbl_product_attribute_group', $data);
    }

    function getGroupByAttributeRowIds($attributeRowIds)
    {
        if($attributeRowIds)
        {
            $sql = "SELECT pg.group_row_id,
                           pg.group_name,
                           pg.is_required,
                           pa.attribute_name,
                           pa.attribute_row_id
                           FROM tbl_product_group AS pg JOIN
                                tbl_product_attribute AS pa ON(pa.group_row_id = pg.group_row_id)
                                WHERE pa.attribute_row_id IN({$attributeRowIds}) ORDER BY pg.group_name, pa.weight ASC";
            $query = $this->db->query($sql);
            return $query->result_array();
        }
        return false;
    }

    function getOptionInfo ($attribute_row_ids, $product_row_id)
    {
        if($attribute_row_ids && $product_row_id)
        {
            $sql = "SELECT *FROM tbl_product_attribute_group WHERE attribute_row_id = '{$attribute_row_ids}' AND product_row_id = " . $product_row_id;
            $query = $this->db->query($sql);
            return $query->row_array();
        }
        return false;
    }

    function saveDefaultAttribute($product_row_id, $default_attribute)
    {
        if(!empty($product_row_id))
        {
            $sql = "UPDATE tbl_product SET default_attribute = '{$default_attribute}' WHERE product_row_id = {$product_row_id}";
            return $this->db->query($sql);
        }
        return false;
    }

    function updateTotalInventory($product_row_id)
    {
        if(!empty($product_row_id))
        {
            $sql = "UPDATE tbl_product SET current_inventory = (SELECT SUM(quantity) FROM tbl_product_attribute_group WHERE product_row_id = {$product_row_id} AND quantity != -9999 )
                           WHERE product_row_id = {$product_row_id}";
            return $this->db->query($sql);
        }
        return false;
    }

    function removeDefaultAttribute($product_row_id)
    {
        if(!empty($product_row_id))
        {
            $sql = "UPDATE tbl_product SET default_attribute = '' WHERE product_row_id = {$product_row_id}";
            return $this->db->query($sql);
        }
        return false;
    }

    function getAttributeByProductId($product_row_id)
    {
        if(!empty($product_row_id))
        {
            $sql = "SELECT attribute_group_row_id FROM tbl_product_attribute_group WHERE product_row_id = {$product_row_id}";
            $query = $this->db->query($sql);
            return $query->result_array();
        }
        return false;
    }

    function toggleUsedAttrubuteSystem($product_row_id)
    {
        if(!empty($product_row_id))
        {
            return $this->db->query("UPDATE tbl_product SET is_mass_attribute = !is_mass_attribute WHERE product_row_id={$product_row_id}");
        }
        return false;
    }

    function priceChangeTotal($attribute_row_ids, $product_row_id)
    {
        if(!empty($attribute_row_ids) && !empty($product_row_id))
        {
            $sql = "SELECT SUM(price_change) AS price_change
                    FROM tbl_product_attribute_group
                    WHERE attribute_row_id IN({$attribute_row_ids})
                    AND product_row_id = " . $product_row_id;
            $query = $this->db->query($sql);
            return $query->row_array();
        }
        return false;
    }
}
?>