<?php

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

namespace home\modules\receivable\models;

use Yii;
use yii\helpers\Url;
use qcbase\models\Auth;
use home\modules\contract\models\Contract;
use home\modules\customer\models\Customer;
use home\modules\receivable\models\Bank;
use home\models\PaymentMethod;
use home\modules\statement\models\StatementLinkContract;

/**
 * 回款表 ({%receivable})
 *
 * @property integer $rece_id  (ID)
 * @property integer $contract_no  (合同编号（关联表qc_contract）)
 * @property string $rece_no  (回款编号)
 * @property decimal $rece_money  (回款金额)
 * @property string $rece_bank  (回款开户行)
 * @property string $rece_account  (回款账号)
 * @property string $receiving_bank  (收款银行)
 * @property string $receiving_account  (收款账户)
 * @property mediumint $method_id  (付款方式，关联qc_payment_method)
 * @property integer $to_date  (到款日期)
 * @property string $remark  (备注)
 * @property integer $create_time  (创建时间)
 * @property integer $update_time  (最后更新时间)
 * @property smallint $status  (状态：99逻辑删除)
 * @property integer $user_id  (创建人ID)
 * @property string $ip  (创建人IP)
 *
 * @author Strief http://blog.u-bo.com
 * @since 0.1
 */
class Receivable extends \yii\db\ActiveRecord
{

    public $contract_name;  //合同名称
    public $customer_name;  //客户名称
    public $project_name;  //项目名称

    /**
     * @inheritdoc
     */

    public static function tableName()
    {
        return '{{%receivable}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['contract_no', 'contract_name', 'rece_no', 'rece_money', 'to_date'], 'required'],
            [['create_time', 'update_time', 'status', 'receiving_bank', 'method_id'], 'integer'],
            [['rece_money'], 'number', 'max' => 99999999.99],
            [['rece_no'], 'string', 'max' => 64],
            [['rece_no'], 'unique'],
            [['rece_bank', 'rece_account', 'receiving_account'], 'string', 'max' => 100],
            [['remark'], 'string', 'max' => 255],
            [['ip'], 'string', 'max' => 15],
            [['receiving_bank', 'receiving_account'], 'safe']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'rece_id' => 'ID',
            'contract_no' => '合同名称',
            'rece_no' => '回款编号',
            'rece_money' => '汇款金额(元)',
            'rece_bank' => '汇款开户行',
            'rece_account' => '汇款账号',
            'receiving_bank' => '收款银行',
            'receiving_account' => '收款账号',
            'method_id' => '付款方式',
            'to_date' => '到款日期',
            'remark' => '备注',
            'create_time' => '创建时间',
            'update_time' => '最后更新时间',
            'status' => '状态',
            'user_id' => '回款登记人',
            'ip' => '创建人IP',
            'contract_name' => '合同名称',
            'customer_name' => '客户名称',
            'project_name' => '项目名称',
        ];
    }

    public static function getNo()
    {
        $prefix = 'HK';
        return $prefix . date('Ymd') . mt_rand(1000, 9999);
    }

    /**
     * 公共搜索所需信息
     *
     * 这里返回公共搜索的信息，我们希望所有的搜索走统一的配置
     * 返回值里的键名为keywords的数组为搜索里关键字搜索所支持的字段
     */
    public function search()
    {
        return [
            'keywords' => [
                'contract_name' => '合同名称',
                'rece_no' => '回款编号'
            ],
        ];
    }

    /**
     * 关联获取合同信息
     */
    public function getContract()
    {
        return $this->hasOne(Contract::className(), ['contract_no' => 'contract_no']);
    }

    /**
     * 获取合同的付款方式，关联qc_payment_method
     */
    public function getMethod()
    {
        return $this->hasOne(PaymentMethod::className(), ['method_id' => 'method_id']);
    }

    /**
     * 关联获取银行信息
     */
    public function getBank()
    {
        return $this->hasOne(Bank::className(), ['bank_id' => 'receiving_bank']); //procession
    }

    /**
     * 插入更新前置操作
     */
    public function beforeSave($insert)
    {
        if(parent::beforeSave($insert)){
            //判断是否是插入操作
            if($this->isNewRecord){
                $this->user_id = Yii::$app->user->identity->user_id;
                $this->ip = Yii::$app->getRequest()->getUserIP();
                $this->create_time = time();
            }
            $this->update_time = time();
            $this->to_date = strtotime($this->to_date);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 删除进账单关联关系
     */
    /*public function beforeDelete()
    {
        if(parent::beforeDelete()){
            return StatementLinkContract::deleteByReceId($this->rece_id);
        }
        return false;
    }*/
    

    /**
     * 权限审核
     * @param  string  $url     操作URL
     * @param  integer $user_id 用户ID
     */
    public static function readAccess($url = null, $user_id = 0)
    {
        if(empty($url)){
            return false;
        }
        empty($user_id) ? $user_id = \Yii::$app->user->id : $user_id;
        //非超级管理员，需要检测权限
        if(Yii::$app->session->get('isSuperAdmin')){
            return true;
        }
        //检测是否具有某一特定权限
        if(Auth::checkAccess($url, $user_id)){
            return true;
        }
        return false;
    }

    /**
     * 新增回款后的消息操作
     */
    /* public function afterSave($insert, $changedAttributes)
      {
      parent::afterSave($insert, $changedAttributes);
      //若该回款为新增操作则发送系统信息，否则无操作
      if ($this->create_time == $this->update_time) {
      $contract = Contract::findOne(['contract_no' => $this->contract_no]);
      if (!empty($contract)) {
      $content = '你所拥有的合同："' . $contract->name . '" 于' . date('Y-m-d H:m:s', time()) . '收到了<font color="green">' . $this->rece_money . '</font>元的回款 <a href="'.Url::to(['/receivable/receivable/detail', 'id' => $this->rece_id]).'">点击查看</a>';
      \home\models\Announcement::Send('回款通知', $content, $contract->owner_id, 2);
      }
      }
      } */
}
