<?php

/**
 * This is the model class for table "{{inventory_products}}".
 *
 * The followings are the available columns in table '{{inventory_products}}':
 * @property integer $id
 * @property string $product_name
 * @property string $sku
 * @property string $category
 * @property integer $quantity
 * @property double $price
 * @property string $isActive
 * @property string $created_at
 * @property string $updated_at
 */
class InventoryProducts extends CActiveRecord
{
    
 
    /**
     * @return string the associated database table name
     */
   public function tableName()
{
    return 'crm_inventory_products';
}

    /**
     * @return array validation rules for model attributes.
     */
public function rules()
{
    return array(

        array('sku, product_name, brand, unit, tax_code, machine_spare, warehouse, rack', 'required'),

        array('quantity', 'numerical', 'integerOnly'=>true),

        array('price, total_value, length_cm, width_cm, height_cm', 'numerical'),

       array('alias, category, machine_spare_remarks, bin, material, finish, hs_code, bar_code, item_type, image, have_expiry_date, have_serial_no', 'safe'),
    );
}
    /**
     * @return array relational rules.
     */
    public function relations()
    {
        return array(
            // Example relation if category becomes foreign key later
            // 'categoryData' => array(self::BELONGS_TO, 'Category', 'category_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
       public function attributeLabels()
{
    return [
        'product_name' => 'Name',
        'sku' => 'Part Number',
        'quantity' => 'Total Quantity',
        'price' => 'Rate',
        'isActive' => 'Status',
        'alias' => 'Alias',
        'brand' => 'Brand',
        'category' => 'Category',
        'unit' => 'Unit',
        'tax_code' => 'Tax Code',
        'machine_spare' => 'Machine / Spare',
        'machine_spare_remarks' => 'Machine/Spare Remarks',
        'warehouse' => 'Warehouse',
        'rack' => 'Rack',
        'bin' => 'Bin',
        'have_expiry_date' => 'Have Expiry Date',
        'have_serial_no' => 'Have Serial No',
        'total_value' => 'Total Value',
        'volume' => 'Volume',
        'weight' => 'Weight',
        'thickness' => 'Thickness',
        'length_cm' => 'Length (CM)',
        'width_cm' => 'Width (CM)',
        'height_cm' => 'Height (CM)',
        'material' => 'Material',
        'finish' => 'Finish',
        'hs_code' => 'HS Code',
        'bar_code' => 'Bar Code',
        'item_type' => 'Item Type',
        'status' => 'Status',
    ];
}

  

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider
     */
 public function search()
{
    $criteria = new CDbCriteria;

    $criteria->compare('id', $this->id);
    $criteria->compare('sku', $this->sku, true);
    $criteria->compare('product_name', $this->product_name, true);
    $criteria->compare('brand', $this->brand, true);
    $criteria->compare('category', $this->category, true);
    $criteria->compare('quantity', $this->quantity);
    $criteria->compare('price', $this->price);
    $criteria->compare('isActive', $this->isActive, true);

    return new CActiveDataProvider($this, array(
        'criteria' => $criteria,
    ));
}
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return InventoryProducts the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * Get All Active Products (Dropdown Helper)
     */
   public static function getAllProducts()
{
    $condition = 'status = "Y"';

    $model = self::model()->findAll(array(
        'condition' => $condition,
        'order' => 'name ASC'
    ));

    return CHtml::listData($model, 'id', 'name');
}
}


