<?php

namespace portalpeople\invoicing\models;

use portalpeople\invoicing\traits\InvoiceRulesTrait;
use portalpeople\invoicing\traits\CreditNoteTrait;
use Yii;

/**
 * This is the model class for table "invoices".
 *
 * @property int $id
 * @property int $source_cart_id
 * @property string $source_centrearrddate
 * @property string $generated_at
 * @property string $generation_key
 * @property string $invoice_number
 * @property string $cartid
 * @property string $email
 * @property string $name
 * @property string $namefirstname
 * @property string $namelastname
 * @property string $telephone
 * @property string $fulladdress
 * @property string $postcode
 * @property string $centername
 * @property string $centrearrddate
 * @property string $centredepdate
 * @property float $subtotal
 * @property float $total
 * @property float $tax
 * @property float $shipping
 * @property float $amountpaid
 * @property string $ordernumber
 * @property string $orderref
 * @property string $status
 * @property string $billingstatus
 * @property string $paymentmethod
 * @property string $source_snapshot_json
 *
 * @property CreditNote[] $creditNotes
 */
class Invoicesbe extends \common\models\Base
{
    use InvoiceRulesTrait;
    use HasCreditNotesTrait;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'invoices';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        $cartRules = (new \backend\models\Cartsbe())->rules();
        
        $invoiceRules = [
            [['source_cart_id', 'source_centrearrddate'], 'required'],
            [['source_cart_id'], 'integer'],
            [['source_centrearrddate'], 'safe'],
            [['generated_at', 'generation_key'], 'safe'],
            [['invoice_number'], 'required', 'message' => 'Invoice number is required'],
            [['generation_key'], 'unique', 'message' => 'This invoice was already generated'],
            [['cartid', 'email', 'name', 'namefirstname', 'namelastname', 'telephone', 
              'fulladdress', 'postcode', 'centername', 'ordernumber', 'orderref', 
              'status', 'billingstatus', 'paymentmethod', 'centrearrddate', 'centredepdate', 
              'invoice_number'], 'string'],
            [['subtotal', 'total', 'tax', 'shipping', 'amountpaid'], 'number'],
            [['source_snapshot_json'], 'string'],
        ];

        return array_merge($cartRules, $invoiceRules);
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'source_cart_id' => 'Source Cart ID',
            'source_centrearrddate' => 'Center Arrival Date',
            'generated_at' => 'Generated At',
            'generation_key' => 'Generation Key',
            'invoice_number' => 'Invoice Number',
            'cartid' => 'Cart ID',
            'email' => 'Email',
            'name' => 'Name',
            'namefirstname' => 'First Name',
            'namelastname' => 'Last Name',
            'telephone' => 'Telephone',
            'fulladdress' => 'Address',
            'postcode' => 'Postcode',
            'centername' => 'Center Name',
            'centrearrddate' => 'Center Arrival Date',
            'centredepdate' => 'Center Departure Date',
            'subtotal' => 'Subtotal',
            'total' => 'Total',
            'tax' => 'Tax',
            'shipping' => 'Shipping',
            'amountpaid' => 'Amount Paid',
            'ordernumber' => 'Order Number',
            'orderref' => 'Order Reference',
            'status' => 'Status',
            'billingstatus' => 'Billing Status',
            'paymentmethod' => 'Payment Method',
        ];
    }

    /**
     * Get the source cart
     *
     * @return \yii\db\ActiveQuery
     */
    public function getSourceCart()
    {
        return $this->hasOne(\backend\models\Cartsbe::class, ['id' => 'source_cart_id']);
    }

    /**
     * Check if invoice is immutable (already issued)
     *
     * @return bool
     */
    public function isImmutable(): bool
    {
        return !empty($this->generated_at) && !empty($this->invoice_number);
    }
}
