<?php

/**
 * This is the model class for table "{{sales_invoice}}".
 *
 * The followings are the available columns in table '{{sales_invoice}}':
 * @property integer $id
 * @property string $invoice_number
 * @property string $customer_name
 * @property string $invoice_date
 * @property double $tax_amount
 * @property double $total_amount
 * @property double $grand_total
 * @property integer $created_by
 * @property string $created_at
 * @property string $updated_at
 *
 * @property SalesInvoiceItems[] $items
 */
class SalesInvoice extends CActiveRecord
{
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'crm_sales_invoice';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
{
    return [
        ['invoice_number, customer_name, invoice_date', 'required'],
        ['total_amount, tax_amount, grand_total', 'numerical'],
        ['invoice_number, customer_name, status', 'length', 'max'=>255],
        ['customer_address, notes', 'safe'],
    ];
}

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

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'invoice_number' => 'Invoice Number',
            'customer_name' => 'Customer Name',
            'invoice_date' => 'Invoice Date',
            'tax_amount' => 'Tax Amount',
            'total_amount' => 'Total Amount',
            'grand_total' => 'Grand Total',
            'created_by' => 'Created By',
            '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_number', $this->invoice_number, true);
        $criteria->compare('customer_name', $this->customer_name, true);
        $criteria->compare('invoice_date', $this->invoice_date, true);
        $criteria->compare('tax_amount', $this->tax_amount);
        $criteria->compare('total_amount', $this->total_amount);
        $criteria->compare('grand_total', $this->grand_total);
        $criteria->compare('created_by', $this->created_by);

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

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