<?php 

namespace backend\models;

use Yii;

/**
 * This is the model class for table "crm_withdraw".
 *
 * @property integer $id
 * @property integer $type
 * @property integer $member_id
 * @property string $amount
 * @property string $fee
 * @property string $memo
 * @property integer $in_time
 * @property integer $out_time
 * @property string $true_name
 * @property string $bank_name
 * @property string $bank_sub_name
 * @property string $bank_card_no
 * @property string $bank_province
 * @property string $bank_city
 * @property string $mobile
 * @property integer $login
 * @property string $rate
 * @property string $swift
 * @property string $admin_name
 */
class Withdraw extends \yii\db\ActiveRecord
{

    /**
     * @var array
     */
    public static $typeTextMap = [
        0 => '等待审核',
        1 => '不通过',
        2 => '已出金',
        3 => '管理员撤销',
        5 => '暂不处理',
        6 => '处理中',
    ];
    
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'crm_withdraw';
    }

    /**
     * @return \yii\db\Connection the database connection used by this AR class.  
     */
    public static function getDb()
    {
        return Yii::$app->get('dbXcrm');
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['type', 'member_id', 'amount', 'in_time', 'true_name', 'bank_name', 'bank_card_no', 'bank_province', 'bank_city', 'mobile', 'login', 'rate'], 'required'],
            [['type', 'member_id', 'in_time', 'out_time', 'login','mt4_status'], 'integer'],
            [['amount', 'fee', 'rate'], 'number'],
            [['memo', 'true_name', 'bank_name', 'bank_sub_name', 'bank_card_no', 'bank_province', 'bank_city', 'mobile', 'swift', 'admin_name'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '主键ID',
            'type' => '出金状态（0：等待审核；1：不通过；2：已出金；3：管理员撤销；5：暂不处理；6：处理中）',
            'member_id' => '代理商ID',
            'amount' => '出金金额',
            'fee' => '手续费',
            'memo' => '备注',
            'in_time' => '申请时间',
            'out_time' => '出金时间',
            'true_name' => '姓名',
            'bank_name' => '银行名称',
            'bank_sub_name' => '分行名称',
            'bank_card_no' => '银行卡号',
            'bank_province' => '银行省份',
            'bank_city' => '银行城市',
            'mobile' => '电话号码',
            'login' => 'mt4登录ID',
            'rate' => '汇率',
            'swift' => 'Swift',
            'mt4_status' => 'mt4出金状态',
            'admin_name' => '操作人',
        ];
    }

    /**
     * 获取未处理的出金记录
     * @param int $memberId
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getUnDealList($memberId)
    {
        return static::find()->where(['member_id' => $memberId, 'type' => 6])->asArray()->all();
		//return static::find()->where(['and','member_id' => $memberId,['or','type=0','type=6']] )->asArray()->all();
    }

    /**
     * 获取当月出金记录
     * @param int $memberId
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getMonthWithdraw($memberId)
    {
        $inTimeStart = strtotime(date('Y-m-01 00:00:00')) * 1000;
        $inTimeEnd = (strtotime(date('Y-m-t 00:00:00')) + 86400) * 1000;
        return static::find()->where(['member_id' => $memberId])
            ->andWhere(['type' => 2])
            ->andWhere(['>=', 'in_time', $inTimeStart])
            ->andWhere(['<', 'in_time', $inTimeEnd])
            ->asArray()->all();
    }

    /**
     * 统计出金金额 amount+fee
     * @param int $type
     * @return mixed
     */
    public static function sumWithdrawByType($type)
    {
        return static::find()->andFilterWhere(['type' => $type])->sum('amount+fee');
    }

    /**
     * 统计出金金额 amount
     * @param int $type
     * @return mixed
     */
    public static function sumNotFeeByType($type)
    {
        return static::find()->andFilterWhere(['type' => $type])->sum('amount');
    }

    /**
     * 统计出金金额 人民币 amount*rate
     * @param int $type
     * @return mixed
     */
    public static function sumRmbByType($type)
    {
        return static::find()->andFilterWhere(['type' => $type])->sum('amount*rate');
    }
    
    /**
     * 收取手续费，单位美元
     * @param float $amount
     * @param array $member
     * @return float
     */
    public static function getFee($amount, $member)
    {
        // 原来的规则，XBroker当月第二次以后出金固定收取25美元，这个规则已废弃
//        $monthWithdraw = static::getMonthWithdraw($member['id']);
//        $fee = $monthWithdraw ? 25 : 0;

        return 5;

        // XBroker和XTrader都要收取手续费
        $percent = 0.005;
        if (function_exists('bcmul')) {
            $fee = bcmul($amount, $percent, 3);    
        } else {
            $fee = $amount * $percent;
        }
        $fee = number_format($fee, 2, '.', '');
        
        return $fee;
    }
    
}


