<?php

declare(strict_types=1);

namespace common\models;

use yii\db\ActiveRecord;

/**
 * InvoiceItem — line item on an invoice.
 *
 * @property int    $id
 * @property int    $invoice_id
 * @property string $description
 * @property float  $quantity
 * @property float  $unit_price
 * @property float  $total_price
 * @property int    $sort_order
 *
 * @property Invoice $invoice
 */
class InvoiceItem extends ActiveRecord
{
    public static function tableName(): string
    {
        return '{{%invoice_items}}';
    }

    public function rules(): array
    {
        return [
            [['invoice_id', 'description'], 'required'],
            [['invoice_id', 'sort_order'], 'integer'],
            [['description'], 'string', 'max' => 255],
            [['quantity', 'unit_price', 'total_price'], 'number', 'min' => 0],
            [['quantity'], 'default', 'value' => 1],
            [['unit_price', 'total_price'], 'default', 'value' => 0],
        ];
    }

    public function getInvoice(): \yii\db\ActiveQuery
    {
        return $this->hasOne(Invoice::class, ['id' => 'invoice_id']);
    }
}
