<?php

/**
 * This is the model class for table "{{sales_invoice_items}}".
 *
 * The followings are the available columns in table '{{sales_invoice_items}}':
 * @property integer $id
 * @property integer $invoice_id
 * @property integer $product_id
 * @property string $sku
 * @property string $product_name
 * @property double $quantity
 * @property double $unit_price
 * @property double $total_price
 * @property string $created_at
 * @property string $updated_at
 *
 * @property SalesInvoice $invoice
 */
class SalesInvoiceItems extends CActiveRecord
{
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'crm_sales_invoice_items';
    }

    /**
     * @return array validation rules for model attributes.
     */
   public function rules()
{
    return [
        ['invoice_id, product_id, sku, product_name, quantity, unit_price, total_price', 'required'],
        ['quantity, unit_price, total_price', 'numerical'],
        ['sku, product_name', 'length', 'max'=>255],
    ];
}

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        return [
            'invoice' => [self::BELONGS_TO, 'SalesInvoice', 'invoice_id'],
        ];
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'invoice_id' => 'Invoice ID',
            'product_id' => 'Product ID',
            'sku' => 'SKU',
            'product_name' => 'Product Name',
            'quantity' => 'Quantity',
            'unit_price' => 'Unit Price',
            'total_price' => 'Total Price',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    /**
     * 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('invoice_id', $this->invoice_id);
        $criteria->compare('product_id', $this->product_id);
        $criteria->compare('sku', $this->sku, true);
        $criteria->compare('product_name', $this->product_name, true);
        $criteria->compare('quantity', $this->quantity);
        $criteria->compare('unit_price', $this->unit_price);
        $criteria->compare('total_price', $this->total_price);

        return new CActiveDataProvider($this, [
            'criteria' => $criteria,
        ]);
    }

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return SalesInvoiceItems the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }
}