<?php

/**
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司
 * @license   http://www.u-bo.com/license/
 */

namespace home\modules\contract\models;

use Yii;
use yii\base\ErrorException;

/**
 * This is the model class for table "qc_payment_instruction".
 *
 * 付款方式说明
 *
 * @property string $contract_no  (合同编号  关联qc_contract表)
 * @property integer $sort  (付款批次顺序)
 * @property float $percentage  (付款比例)
 * @property float $sum  (付款金额)
 * @property string $remark  (备注)
 * @property integer $update_time  (更新时间)
 * @property integer $user_id  (更新人)
 * @property string $ip  (更新人IP)
 *
 * @author HuLimin <http://blog.u-bo.com>
 * @since 0.1
 */
class PaymentInstruction extends \yii\db\ActiveRecord
{

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

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['sort', 'update_time', 'user_id'], 'integer'],
            [['percentage', 'sum'], 'number'],
            [['contract_no'], 'string', 'max' => 64],
            [['remark'], 'string', 'max' => 255],
            [['ip'], 'string', 'max' => 15]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'contract_no' => '合同编号',
            'sort' => '付款批次',
            'percentage' => '付款比例',
            'sum' => '付款金额',
            'remark' => '备注',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
            'user_id' => '更新人',
            'ip' => '更新人IP'
        ];
    }

    /**
     * 更新合同付款说明
     *
     */
    public static function saveInstruction($instructionArr, $contract_no)
    {
        $transaction = Yii::$app->db->beginTransaction(); //创建事务
        try {
            if(is_array($instructionArr) && !empty($contract_no)){
                $user_id = \Yii::$app->user->id;
                $ip = Yii::$app->getRequest()->getUserIP();
                self::deleteAll('contract_no=:contract_no', [':contract_no' => $contract_no]);
                //新增项目资源信息
                foreach ($instructionArr as $key => $value) {
                    $model = new PaymentInstruction();
                    $model->contract_no = $contract_no;
                    $model->sort = $value['sort'];
                    $model->percentage = $value['percentage'];
                    $model->sum = $value['sum'];
                    $model->remark = $value['remark'];
                    $model->update_time = time();
                    $model->user_id = $user_id;
                    $model->ip = $ip;
                    if(!$model->validate()){
                        throw new ErrorException('付款方式说明更新失败，参数有误！');
                    }
                    $model->save();
                }
            } else {
                throw new ErrorException('付款方式说明更新失败，参数有误！');
            }
            $transaction->commit();
            return true;
        } catch (Exception $e) {
            $transaction->rollBack();
            throw new ErrorException('付款方式说明更新失败！');
        }
    }

}
