<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "caiwu_t".
 *
 * @property integer $id
 * @property integer $user_id
 * @property string $user_name
 * @property integer $order_id
 * @property string $insurer_no
 * @property integer $insurer_type
 * @property string $car_no
 * @property string $factory_model
 * @property string $id_man
 * @property string $total
 * @property string $total_clear
 * @property string $total_tax
 * @property string $total_rate
 * @property string $total_clear_real
 * @property string $total_tax_real
 * @property integer $status
 * @property string $process_date
 * @property string $r_total
 * @property string $r_date
 * @property string $b1_total
 * @property string $b1_date
 * @property string $b2_total
 * @property string $b2_date
 * @property string $b3_total
 * @property string $b3_date
 * @property string $b4_total
 * @property string $b4_date
 * @property string $company
 * @property string $created_at
 * @property string $updated_at
 */
class CaiwuT extends \common\models\Base
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'caiwu_t';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'order_id', 'total', 'total_clear', 'total_tax', 'total_rate', 'total_clear_real', 'total_tax_real', 'status'], 'required'],
            [['user_id', 'order_id', 'status', 'insurer_type','dif'], 'integer'],
            [['total', 'total_clear', 'total_tax', 'total_rate', 'total_clear_real', 'total_tax_real','r_total','b1_total','b2_total','b3_total','b4_total', 'total_dis'], 'number'],
            [['process_date','r_date','b1_date','b2_date','b3_date','b4_date', 'created_at', 'updated_at'], 'safe'],
            [['user_name', 'insurer_no', 'factory_model', 'id_man', 'company'], 'string', 'max' => 100],
            [['car_no'], 'string', 'max' => 50],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => 'User ID',
            'user_name' => 'User Name',
            'order_id' => 'Order ID',
            'insurer_no' => 'Insurer No',
            'insurer_type' => 'Insurer Type',
            'car_no' => 'Car No',
            'factory_model' => 'Factory Model',
            'id_man' => 'Id Man',
            'total' => 'Total',
            'total_clear' => 'Total Clear',
            'total_tax' => 'Total Tax',
            'total_rate' => 'Total Rate',
            'total_clear_real' => 'Total Clear Real',
            'total_tax_real' => 'Total Tax Real',
            'total_dis' => 'Total Dis',
            'status' => 'Status',
            'process_date' => 'Process Date',
            'r_total' => 'R Total',
            'r_date' => 'R Date',
            'b1_total' => 'B1 Total',
            'b1_date' => 'B1 Date',
            'b2_total' => 'B2 Total',
            'b2_date' => 'B2 Date',
            'b3_total' => 'B3 Total',
            'b3_date' => 'B3 Date',
            'b4_total' => 'B4 Total',
            'b4_date' => 'B4 Date',
            'company' => 'Company',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    public function getUser()
    {
        return $this->hasOne(UserT::className(),['id'=>'user_id']);
    }

    public function getReceiveTotal()
    {
        $query = CaiwuExcelT::find()
            ->where('field1="'.trim($this->insurer_no).'" and field4>0 and field11<="'.$this->process_date.'"');
        $total = $query->sum('field8');
        return $total;
    }

    public function getOrder()
    {
        return $this->hasOne(OrderT::className(),['id'=>'order_id']);
    }

    public function getOrderCaiwu()
    {
        return $this->hasOne(OrderCaiwuT::className(),['order_id'=>'order_id']);
    }
}