<?php
class Service_CustomerBalanceProcess
{
	/**
	 * 订单预扣费
	 * @author solar
	 * @param string $order_code
	 * @param int $customer_id
	 * @return true|string
	 */
    public function freezeFee($order_code, $customer_id) {
    	$customerRow = Service_Customer::getByField($customer_id);
    	$currencyRow = Service_Currency::getByField($customerRow['customer_currency'], 'currency_code');
        $orderRow = Service_Orders::getByField($order_code, 'order_code');
    	//if($customerRow['cash_type']==2) return true;	//月结，不用冻结
        //集货转运类的订单在确认到提交时，无须冻结金额，在日本仓出货的时候也无须扣款，只在目的仓出货的时候发生扣款动作
        //if($orderRow['warehouse_id']=="1"&&$orderRow['order_mode_type']=="1") return true;
    	$addressRow = Service_OrderAddressBook::getByField($order_code, 'order_code');
    	$weight = Service_Orders::calculateWeight($order_code);
    	//$orderProducts = Service_OrderProduct::getByCondition();
    	//$orderProducts = Service_OrderProduct::getByField($order_code,'order_code');
    	$objCalculate = new Common_CalculateShipping();
    	$keys = array(
    		'warehouseId' => $orderRow['warehouse_id'],
    		'countryId' => $addressRow['oab_country_id'],
    		'smCode' => $orderRow['sm_code'],
    		'weight' => $weight,
    		'order_code'=>$order_code,
        'customerCode' => $orderRow['customer_code'],
    	);
        if($addressRow['oab_country_id']=='253'){
            //美国的要加入城市
            $conditionCity = array(
                'country_id'=>$addressRow['oab_country_id'],
                'city_name'=>$addressRow['oab_city']
            );
            $citys = Service_City::getByCondition($conditionCity,'*');
            if(!empty($citys)){
                $keys['cityId'] = $citys[0]['city_id'];
            }
        }
    	$objCalculate->setParams($keys);
    	$result = $objCalculate->getRate();
    	if($result['state'] == 0)
    		return is_array($result['error']) ? implode(',', $result['error']) : $result['error'];
        $doresert = true;
        if(isset($result['data']['cost']['sm_channel']) && ($result['data']['cost']['sm_channel']=='1')){
            //只有渠道
           $doresert = false;
        }
    	$fee = $result['data']['cost']['totalCost'];
    	$convert_fee = round($fee / $currencyRow['currency_rate'], 2);
    	//转成客户的币种
    	$convert_fee = Service_Currency::converByCode($result['data']['cost']['currency_code'], $customerRow['customer_currency'], $fee);

    	//冻结金额
    	$balanceRow = Service_CustomerBalance::getByField($customer_id, 'customer_id');
    	/* print_r($balanceRow);
    	echo $convert_fee; */
    	$able_fee = floatval($balanceRow['cb_value'] - $convert_fee);
    	//echo $able_fee;
        if($customerRow['cash_type']==1){
            if($able_fee < 0) return '账户余额不足，无法冻结金额';
        }
    	$hold_fee = $balanceRow['cb_hold_value'] + $convert_fee;

    	//if(!$uResult) return '更新余额数据失败';
    	//记录余额日记
    	$aBalanceLog['customer_code'] = $customerRow['customer_code'];
    	$aBalanceLog['customer_id'] = $customer_id;
    	$aBalanceLog['cbl_type'] = 0;
    	$aBalanceLog['cbl_transaction_value'] = $convert_fee;
    	$aBalanceLog['cbl_value'] = $fee;
    	$aBalanceLog['currency_rate'] = $currencyRow['currency_rate'];
    	$aBalanceLog['currency_code'] = $result['data']['cost']['currency_code'];

    	$aBalanceLog['cbl_note'] = '提交订单，冻结余额';
    	$aBalanceLog['user_id'] = -1;
    	$aBalanceLog['cbl_current_value'] = $able_fee;
    	$aBalanceLog['cbl_current_hold_value'] = $hold_fee;
    	$aBalanceLog['application_code'] = 1;
    	$aBalanceLog['cbl_refer_code'] = $order_code;
    	$aBalanceLog['cbl_add_time'] = date('Y-m-d H:i:s');
        if($doresert){
            if($convert_fee>0){
                $aResult = Service_CustomerBalanceLog::add($aBalanceLog);
                if(!$aResult) return '记录余额日志失败';
            }
        }

    	$uOrder['shipping_fee_estimate'] = $convert_fee;
    	$uOrder['update_time'] = date('Y-m-d H:i:s');

    	//扣其它费用
    	$objOtherFeeCalculate = new Common_CalculateOtherFee();
    	$keys = array(
    			'warehouseId' => $orderRow['warehouse_id'],
    			'countryId' => $addressRow['oab_country_id'],
    			'smCode' => $orderRow['sm_code'],
    			'weight' => $weight,
    			'order_code'=>$order_code,
    	);
    	$objOtherFeeCalculate->setParams($keys);
    	$otherfeeResult = $objOtherFeeCalculate->getOtherFee('CAOZUO'); //预扣操作费
    	$otherfeeResult = $objOtherFeeCalculate->getOtherFee('BAOGUAN');
    	$otherfeeResult = $objOtherFeeCalculate->getOtherFee('YUNSHU');
        $otherfeeResult = $objOtherFeeCalculate->getOtherFee("RUKU");
        $otherfeeResult = $objOtherFeeCalculate->getOtherFee("WULIUCAOZUO");
        $otherfeeResult = $objOtherFeeCalculate->getOtherFee("PIANYUAN");
        $otherfeeResult = $objOtherFeeCalculate->getOtherFee("JIANGUANYUNSHU");
    	//print_r($otherfeeResult);
    	$caozuofee = $otherfeeResult['data']['otherFee']['caozuo']['price'];
    	$baoguanfee = $otherfeeResult['data']['otherFee']['baoguan']['price'];
    	$yunshufee = $otherfeeResult['data']['otherFee']['yunshu']['price'];
        $rukuFee = $otherfeeResult['data']['otherFee']['ruku']['price'];
        $wuliucaozuoFee = $otherfeeResult['data']['otherFee']['wuliucaozuo']['price'];
        $jgysFee = isset($otherfeeResult['data']['otherFee']['jianguanyunshu']['price'])?$otherfeeResult['data']['otherFee']['jianguanyunshu']['price']:'0';
    	if($caozuofee>0){
    		//$convert_caozuo_fee = round($caozuofee / $currencyRow['currency_rate'], 2);
    		$convert_caozuo_fee = Service_Currency::converByCode($otherfeeResult['data']['otherFee']['caozuo']['currency_code'], $customerRow['customer_currency'], $caozuofee);
    		$able_fee = $able_fee-$convert_caozuo_fee;
    			
    		$hold_fee+=$convert_caozuo_fee;
    		
    		$aBalanceLog['customer_code'] = $customerRow['customer_code'];
    		$aBalanceLog['customer_id'] = $customer_id;
    		$aBalanceLog['cbl_type'] = 0;
    		$aBalanceLog['cbl_transaction_value'] = $convert_caozuo_fee;
    		$aBalanceLog['cbl_value'] = $caozuofee;
    		$aBalanceLog['currency_rate'] = $currencyRow['currency_rate'];
    		$aBalanceLog['currency_code'] = $otherfeeResult['data']['otherFee']['caozuo']['currency_code'];
    		$aBalanceLog['cbl_note'] = '提交订单扣操作费，冻结余额';
    		$aBalanceLog['user_id'] = -1;
    		$aBalanceLog['cbl_current_value'] = $able_fee;
    		$aBalanceLog['cbl_current_hold_value'] = $hold_fee;
    		$aBalanceLog['application_code'] = 1;
    		$aBalanceLog['cbl_refer_code'] = $order_code;
    		$aBalanceLog['cbl_add_time'] = date('Y-m-d H:i:s');
    		$otherfee_caozuo=Service_OtherFee::getByField('caozuo','fee_code');
    		if(!empty($otherfee_caozuo)){
    			$aBalanceLog['fee_id'] = $otherfee_caozuo['fee_id'];
    		}
    		
    		//print_r($aBalanceLog);
    		$aResult = Service_CustomerBalanceLog::add($aBalanceLog);
    	}
    	if($baoguanfee>0){
    		//$convert_caozuo_fee = round($caozuofee / $currencyRow['currency_rate'], 2);
    		$convert_baoguan_fee = Service_Currency::converByCode($otherfeeResult['data']['otherFee']['baoguan']['currency_code'], $customerRow['customer_currency'], $baoguanfee);
    		$able_fee = $able_fee-$convert_baoguan_fee;
    		 
    		$hold_fee+=$convert_baoguan_fee;
    	
    		$aBalanceLog['customer_code'] = $customerRow['customer_code'];
    		$aBalanceLog['customer_id'] = $customer_id;
    		$aBalanceLog['cbl_type'] = 0;
    		$aBalanceLog['cbl_transaction_value'] = $convert_baoguan_fee;
    		$aBalanceLog['cbl_value'] = $baoguanfee;
    		$aBalanceLog['currency_rate'] = $currencyRow['currency_rate'];
    		$aBalanceLog['currency_code'] = $otherfeeResult['data']['otherFee']['baoguan']['currency_code'];
    		$aBalanceLog['cbl_note'] = '提交订单扣报关费，冻结余额';
    		$aBalanceLog['user_id'] = -1;
    		$aBalanceLog['cbl_current_value'] = $able_fee;
    		$aBalanceLog['cbl_current_hold_value'] = $hold_fee;
    		$aBalanceLog['application_code'] = 1;
    		$aBalanceLog['cbl_refer_code'] = $order_code;
    		$aBalanceLog['cbl_add_time'] = date('Y-m-d H:i:s');
    		//$otherfee_caozuo=Service_OtherFee::getByField('caozuo','fee_code');
    		if($otherfeeResult['data']['otherFee']['baoguan']['fee_id']>0){
    			$aBalanceLog['fee_id'] = $otherfeeResult['data']['otherFee']['baoguan']['fee_id'];
    		}
    	
    		//print_r($aBalanceLog);
    		$aResult = Service_CustomerBalanceLog::add($aBalanceLog);
    	}
    	if($yunshufee>0){
    		//$convert_caozuo_fee = round($caozuofee / $currencyRow['currency_rate'], 2);
    		$convert_yunshu_fee = Service_Currency::converByCode($otherfeeResult['data']['otherFee']['yunshu']['currency_code'], $customerRow['customer_currency'], $yunshufee);
    		$able_fee = $able_fee-$convert_yunshu_fee;
    		 
    		$hold_fee+=$convert_yunshu_fee;
    		 
    		$aBalanceLog['customer_code'] = $customerRow['customer_code'];
    		$aBalanceLog['customer_id'] = $customer_id;
    		$aBalanceLog['cbl_type'] = 0;
    		$aBalanceLog['cbl_transaction_value'] = $convert_yunshu_fee;
    		$aBalanceLog['cbl_value'] = $yunshufee;
    		$aBalanceLog['currency_rate'] = $currencyRow['currency_rate'];
    		$aBalanceLog['currency_code'] = $otherfeeResult['data']['otherFee']['yunshu']['currency_code'];
    		$aBalanceLog['cbl_note'] = '提交订单扣中港运输费，冻结余额';
    		$aBalanceLog['user_id'] = -1;
    		$aBalanceLog['cbl_current_value'] = $able_fee;
    		$aBalanceLog['cbl_current_hold_value'] = $hold_fee;
    		$aBalanceLog['application_code'] = 1;
    		$aBalanceLog['cbl_refer_code'] = $order_code;
    		$aBalanceLog['cbl_add_time'] = date('Y-m-d H:i:s');
    		//$otherfee_caozuo=Service_OtherFee::getByField('caozuo','fee_code');
    		if($otherfeeResult['data']['otherFee']['yunshu']['fee_id']>0){
    			$aBalanceLog['fee_id'] = $otherfeeResult['data']['otherFee']['yunshu']['fee_id'];
    		}
    		 
    		//print_r($aBalanceLog);
    		$aResult = Service_CustomerBalanceLog::add($aBalanceLog);
    	}
        if($rukuFee>0){
            $convert_ruku_fee = Service_Currency::converByCode($otherfeeResult['data']['otherFee']['ruku']['currency_code'], $customerRow['customer_currency'], $rukuFee);
            $able_fee = $able_fee-$convert_ruku_fee;

            $hold_fee+=$convert_ruku_fee;
            $aBalanceLog['customer_code'] = $customerRow['customer_code'];
            $aBalanceLog['customer_id'] = $customer_id;
            $aBalanceLog['cbl_type'] = 0;
            $aBalanceLog['cbl_transaction_value'] = $convert_ruku_fee;
            $aBalanceLog['cbl_value'] = $rukuFee;
            $aBalanceLog['currency_rate'] = $currencyRow['currency_rate'];
            $aBalanceLog['currency_code'] = $otherfeeResult['data']['otherFee']['ruku']['currency_code'];
            $aBalanceLog['cbl_note'] = '提交订单扣入库费，冻结余额';
            $aBalanceLog['user_id'] = -1;
            $aBalanceLog['cbl_current_value'] = $able_fee;
            $aBalanceLog['cbl_current_hold_value'] = $hold_fee;
            $aBalanceLog['application_code'] = 1;
            $aBalanceLog['cbl_refer_code'] = $order_code;
            $aBalanceLog['cbl_add_time'] = date('Y-m-d H:i:s');
            //$otherfee_caozuo=Service_OtherFee::getByField('caozuo','fee_code');
            if($otherfeeResult['data']['otherFee']['ruku']['fee_id']>0){
                $aBalanceLog['fee_id'] = $otherfeeResult['data']['otherFee']['ruku']['fee_id'];
            }

            //print_r($aBalanceLog);
            $aResult = Service_CustomerBalanceLog::add($aBalanceLog);
        }
        if($wuliucaozuoFee>0){
            $convert_wuliucaozuo_fee = Service_Currency::converByCode($otherfeeResult['data']['otherFee']['wuliucaozuo']['currency_code'], $customerRow['customer_currency'], $wuliucaozuoFee);
            $able_fee = $able_fee-$convert_wuliucaozuo_fee;

            $hold_fee+=$convert_wuliucaozuo_fee;
            $aBalanceLog['customer_code'] = $customerRow['customer_code'];
            $aBalanceLog['customer_id'] = $customer_id;
            $aBalanceLog['cbl_type'] = 0;
            $aBalanceLog['cbl_transaction_value'] = $convert_wuliucaozuo_fee;
            $aBalanceLog['cbl_value'] = $wuliucaozuoFee;
            $aBalanceLog['currency_rate'] = $currencyRow['currency_rate'];
            $aBalanceLog['currency_code'] = $otherfeeResult['data']['otherFee']['wuliucaozuo']['currency_code'];
            $aBalanceLog['cbl_note'] = '提交订单扣入库费，冻结余额';
            $aBalanceLog['user_id'] = -1;
            $aBalanceLog['cbl_current_value'] = $able_fee;
            $aBalanceLog['cbl_current_hold_value'] = $hold_fee;
            $aBalanceLog['application_code'] = 1;
            $aBalanceLog['cbl_refer_code'] = $order_code;
            $aBalanceLog['cbl_add_time'] = date('Y-m-d H:i:s');
            //$otherfee_caozuo=Service_OtherFee::getByField('caozuo','fee_code');
            if($otherfeeResult['data']['otherFee']['wuliucaozuo']['fee_id']>0){
                $aBalanceLog['fee_id'] = $otherfeeResult['data']['otherFee']['wuliucaozuo']['fee_id'];
            }

            //print_r($aBalanceLog);
            $aResult = Service_CustomerBalanceLog::add($aBalanceLog);
        }
        if($jgysFee>0){
            $convert_jgys_fee = Service_Currency::converByCode($otherfeeResult['data']['otherFee']['jianguanyunshu']['currency_code'], $customerRow['customer_currency'], $jgysFee);
            $able_fee = $able_fee-$convert_jgys_fee;
            $able_fee=sprintf('%.2f',$able_fee);

            $hold_fee+=$convert_jgys_fee;
            $hold_fee=sprintf('%.2f',$hold_fee);

            $aBalanceLog['customer_code'] = $customerRow['customer_code'];
            $aBalanceLog['customer_id'] = $customer_id;
            $aBalanceLog['cbl_type'] = 0;
            $aBalanceLog['cbl_transaction_value'] = $convert_jgys_fee;
            $aBalanceLog['cbl_value'] = $jgysFee;
            $aBalanceLog['currency_rate'] = $currencyRow['currency_rate'];
            $aBalanceLog['currency_code'] = $otherfeeResult['data']['otherFee']['jianguanyunshu']['currency_code'];
            $aBalanceLog['cbl_note'] = '提交订单扣监管运输费，冻结余额';
            $aBalanceLog['user_id'] = -1;
            $aBalanceLog['cbl_current_value'] = $able_fee;
            $aBalanceLog['cbl_current_hold_value'] = $hold_fee;
            $aBalanceLog['application_code'] = 1;
            $aBalanceLog['cbl_refer_code'] = $order_code;
            $aBalanceLog['cbl_add_time'] = date('Y-m-d H:i:s');
            //$otherfee_caozuo=Service_OtherFee::getByField('caozuo','fee_code');
            if($otherfeeResult['data']['otherFee']['jianguanyunshu']['fee_id']>0){
                $aBalanceLog['fee_id'] = $otherfeeResult['data']['otherFee']['jianguanyunshu']['fee_id'];
            }
            $aResult = Service_CustomerBalanceLog::add($aBalanceLog);
        }

    	$uBalance['cb_value'] = $able_fee;
    	$uBalance['cb_hold_value'] = $hold_fee;
    	$uBalance['cb_update_time'] = date('Y-m-d H:i:s');
    	$uResult = Service_CustomerBalance::update($uBalance, $balanceRow['cb_id']);
        if($doresert){
            Service_Orders::update($uOrder, $order_code, 'order_code');
        }
    	return true;
    }

	/**
     * 解冻订单金额
     * @author solar
     * @param string $order_code
     * @return true|string
     */
    public static function unfreezeFee($order_code) {
    	$aOrder = Service_Orders::getByField($order_code, 'order_code');
    	if(empty($aOrder)) return '找不到订单'.$order_code;

      // 解冻其他杂费
      self::unfreezeAllOtherFee($aOrder);

    	if($aOrder['shipping_fee_estimate'] == 0) return true;
    	$aCustomer = Service_Customer::getByField($aOrder['customer_id']);
    	//if($aCustomer['cash_type']==2) return true;	//月结
    	$aBalance = Service_CustomerBalance::getByField($aOrder['customer_id'], 'customer_id');
		if(empty($aBalance)) return '找不到客户账户余额记录';
    	//处理
		$balanceRow = Service_CustomerBalance::getForUpdate($aBalance['cb_id']);
		$hold_value = $balanceRow['cb_hold_value'] - $aOrder['shipping_fee_estimate'];
        //允许冻结金额小于0
    	//if($hold_value < 0) return '账户金额异常，冻结余额不足';
    	$uBalance['cb_value'] = $balanceRow['cb_value'] + $aOrder['shipping_fee_estimate'];
    	$uBalance['cb_hold_value'] = $hold_value;
    	$uBalance['cb_update_time'] = date('Y-m-d H:i:s');
    	$uResult = Service_CustomerBalance::update($uBalance, $balanceRow['cb_id']);
    	//if(!$uResult) return '更新余额数据失败';
    	$balanceShipCondition = array(
    		'customer_id'=>$aOrder['customer_id'],
    		'cbl_type'=>'0',
    		'fee_id' => '0',
    		'cbl_refer_code'=>$aOrder['order_code']	
    	);
    	$shipfees = Service_CustomerBalanceLog::getByCondition($balanceShipCondition,'*',1,1);
    	if(empty($shipfees)){
            return true;
    		return '不存在冻结运费记录';
    	}
    	$shipfee = $shipfees[0];
    	//解冻日志
        $currencyRow= Service_Currency::getByField($aCustomer['customer_currency'],"currency_code","*");
    	$aBalanceLog['customer_code'] = $aCustomer['customer_code'];
    	$aBalanceLog['customer_id'] = $aCustomer['customer_id'];
    	$aBalanceLog['cbl_type'] = 1;
    	$aBalanceLog['cbl_transaction_value'] = $aOrder['shipping_fee_estimate'];
    	$aBalanceLog['cbl_value'] = $shipfee['cbl_value'];
    	$aBalanceLog['currency_rate'] = $currencyRow['currency_rate'];
    	$aBalanceLog['currency_code'] = $shipfee['currency_code'];
    	$aBalanceLog['cbl_note'] = '订单出货，解冻余额';
    	$aBalanceLog['user_id'] = Service_User::getUserId();
    	$aBalanceLog['cbl_current_value'] = $uBalance['cb_value'];
    	$aBalanceLog['cbl_current_hold_value'] = $uBalance['cb_hold_value'];
    	$aBalanceLog['application_code'] = 1;
    	$aBalanceLog['cbl_refer_code'] = $order_code;
    	$aBalanceLog['cbl_add_time'] = date('Y-m-d H:i:s');
        if($aOrder['shipping_fee_estimate']>0){
            $aResult = Service_CustomerBalanceLog::add($aBalanceLog);
            //self::unfreezeAllOtherFee($aOrder); // 在这里解冻杂费，逻辑上有误，集货模式不用扣运费，也谈不上解冻运费
            return $aResult ? true : '记录余额日志失败';
        }
        return true;
    }

    /**
     * 解冻区外仓订单金额
     * @author solar
     * @param string $order_code
     * @return true|string
     */
    public static function logisticsUnFreezeFee($order_code,$customer_id){
        $condition = array(
            'customer_id'=>$customer_id,
            'cbl_refer_code'=>$order_code,
            'cbl_type'=>'1'
        );
        $unfeeRow = $feeCustomerBalanceLog = Service_CustomerBalanceLog::getByCondition($condition,"*");
        //已经解冻过的务须再解冻
        if(!empty($unfeeRow)){
            return true;
        }

        $condition = array(
            'customer_id'=>$customer_id,
            'cbl_refer_code'=>$order_code,
            'cbl_type'=>'0'
        );
        $feeCustomerBalanceLog = Service_CustomerBalanceLog::getByCondition($condition,"*");
        $aCustomer = Service_Customer::getByField($customer_id);
        $aBalance = Service_CustomerBalance::getByField($customer_id, 'customer_id');
        if(empty($aBalance)) return '找不到客户账户余额记录';
        $balanceRow = Service_CustomerBalance::getForUpdate($aBalance['cb_id']);
        $unfeeValue=0;
         $hold_value = $balanceRow['cb_hold_value'];
         $balance=$balanceRow['cb_value'];
        foreach($feeCustomerBalanceLog as $log){
             $hold_value = $hold_value - $log['cbl_transaction_value'];
        //允许冻结金额小于0
        //if($hold_value < 0) return '账户金额异常，冻结余额不足';
             $balance=$balance + $log['cbl_transaction_value'];
        $uBalance['cb_value'] = $balance;
        $uBalance['cb_hold_value'] = $hold_value;
        $uBalance['cb_update_time'] = date('Y-m-d H:i:s');
        $uResult = Service_CustomerBalance::update($uBalance, $balanceRow['cb_id']);

        //$currencyRow= Service_Currency::getByField($feeCustomerBalanceLog[0]['currency_code'],"currency_code","*");
        $aBalanceLog['customer_code'] = $aCustomer['customer_code'];
        $aBalanceLog['customer_id'] = $aCustomer['customer_id'];
        $aBalanceLog['cbl_type'] = 1;
        $aBalanceLog['cbl_transaction_value'] = $log['cbl_transaction_value'];
        $aBalanceLog['cbl_value'] = $log['cbl_value'];
        $aBalanceLog['currency_rate'] = $log['currency_rate'];
        $aBalanceLog['currency_code'] = $log['currency_code'];
        $aBalanceLog['cbl_note'] = '区外仓订单收货，解冻余额';
        $aBalanceLog['user_id'] = Service_User::getUserId();
        $aBalanceLog['cbl_current_value'] =$balance;
        $aBalanceLog['cbl_current_hold_value'] = $hold_value;
        $aBalanceLog['application_code'] = 'logistics';
        $aBalanceLog['cbl_refer_code'] = $order_code;
        $aBalanceLog['cbl_add_time'] = date('Y-m-d H:i:s');
        if($log['fee_id']){
            $aBalanceLog['fee_id']=$log['fee_id'];
        }
         $aResult = Service_CustomerBalanceLog::add($aBalanceLog);
          if(!$aResult){
             return  '记录余额日志失败';
          }
        
        }
       
        return true;
    }
    
    /**
     * @author william-fan
     * @todo 用于解冻其它费用
     */
    public static function unfreezeOtherFee($aOrder,$balanceLog,$log=''){
    	$aCustomer = Service_Customer::getByField($aOrder['customer_id']);
    	//if($aCustomer['cash_type']==2) return true;	//月结
    	$aBalance = Service_CustomerBalance::getByField($aOrder['customer_id'], 'customer_id');
    	if(empty($aBalance)) return '找不到客户账户余额记录';
    	//处理
    	$balanceRow = Service_CustomerBalance::getForUpdate($aBalance['cb_id']);
    	$hold_value = $balanceRow['cb_hold_value'] - $balanceLog['cbl_transaction_value'];
        //允许冻结金额小于0
    	//if($hold_value < 0) return '账户金额异常，冻结余额不足';
    	$uBalance['cb_value'] = $balanceRow['cb_value'] + $balanceLog['cbl_transaction_value'];
    	$uBalance['cb_hold_value'] = $hold_value;
    	$uBalance['cb_update_time'] = date('Y-m-d H:i:s');
    	$uResult = Service_CustomerBalance::update($uBalance, $balanceRow['cb_id']);
    	//if(!$uResult) return '更新余额数据失败';
    	//解冻日志
    	$currencyRow= Service_Currency::getByField($aCustomer['customer_currency'],"currency_code","*");
    	$aBalanceLog['customer_code'] = $aCustomer['customer_code'];
    	$aBalanceLog['customer_id'] = $aCustomer['customer_id'];
    	$aBalanceLog['cbl_type'] = 1;
    	$aBalanceLog['cbl_transaction_value'] = $balanceLog['cbl_transaction_value'];
    	$aBalanceLog['cbl_value'] = $balanceLog['cbl_value'];
    	$aBalanceLog['currency_rate'] = $currencyRow['currency_rate'];
    	$aBalanceLog['currency_code'] = $balanceLog['currency_code'];
    	$aBalanceLog['cbl_note'] = $log?$log:'订单出货，解冻其它费用余额';
    	$aBalanceLog['user_id'] = Service_User::getUserId();
    	$aBalanceLog['cbl_current_value'] = $uBalance['cb_value'];
    	$aBalanceLog['cbl_current_hold_value'] = $uBalance['cb_hold_value'];
    	$aBalanceLog['application_code'] = 1;
    	$aBalanceLog['cbl_refer_code'] = $aOrder['order_code'];
    	$aBalanceLog['cbl_add_time'] = date('Y-m-d H:i:s');
    	$aBalanceLog['fee_id'] = $balanceLog['fee_id'];
        if($balanceLog['cbl_transaction_value']>0){
            $aResult = Service_CustomerBalanceLog::add($aBalanceLog);
            if($aResult){
                $uLogRow = array(
                    'un_cbl_id'=>$aResult
                );
                Service_CustomerBalanceLog::update($uLogRow,$balanceLog['cbl_id']);
            }            
            return $aResult ? true : '记录其它费用余额日志失败';
        }
        return true;
    }
    /**
     * @author william-fan
     * @todo 解冻所以的其它费用
     */
    public static function unfreezeAllOtherFee($aOrder){
    	if(empty($aOrder) || $aOrder['customer_id']<=0){
    		return '数据有误';
    	}
    	$otherfeeArr = array(
    		'customer_id'=>$aOrder['customer_id'],
    		'cbl_type'=>'0',
    		'fee_id_gt' => '0',
    		'cbl_refer_code'=>$aOrder['order_code']	
    	);

    	//查询是其它费用的情况
    	$otherFees = Service_CustomerBalanceLog::getByCondition($otherfeeArr);
        $hasUnfreeze = array();
    	if(!empty($otherFees)){
    		foreach ($otherFees as $key=>$balanceLog){
    			if(!in_array($balanceLog['fee_id'],$hasUnfreeze)){
                    self::unfreezeOtherFee($aOrder,$balanceLog);
                    $hasUnfreeze[] = $balanceLog['fee_id'];
                }

    		}
    	}
    }
    /**
     * @author william-fan
     * @todo 用于解冻单个费用通过费用id
     */
    public static function unfreezeOtherFeeById($aOrder,$fee_id='',$log=''){
    	if(empty($aOrder) || $aOrder['customer_id']<=0){
    		return '数据有误';
    	}
    	$otherfee = Service_OtherFee::getByField($fee_id);
    	if(empty($otherfee)){
    		return '费用不存在';
    	}
    	$otherfeeArr = array(
    			'customer_id'=>$aOrder['customer_id'],
    			'cbl_type'=>'0',
    			'fee_id_gt' => '0',
    			'cbl_refer_code'=>$aOrder['order_code'],
    			'fee_id'=>$fee_id,
    	);
    	//查询是其它费用的情况
    	$otherFees = Service_CustomerBalanceLog::getByCondition($otherfeeArr);
    	if(!empty($otherFees)){
    		foreach ($otherFees as $key=>$balanceLog){
    			 
    			self::unfreezeOtherFee($aOrder,$balanceLog,$log);
    		}
    	}
    }
    /**
     * 扣除费用
     * @author solar
     * @param int $customer_id
     * @param $fee array('currency_code'=>'','price'=>'','convert_fee'='')
     * @param string $note
     * @param string $refer_code
     * @param string $application_code
     * @return array('ask'=>0, 'fee'=>0, 'error'=>'')
     */
    public static function deductFee($customer_id, $fee, $note, $refer_code, $application_code='',$fee_id='',$otherParam=array()) {
        $addTime=isset($otherParam['add_time'])?$otherParam['add_time']:date('Y-m-d H;i:s');
    	$result = array('ask'=>0, 'fee'=>0, 'error'=>'');
    	$aCustomer = Service_Customer::getByField($customer_id);
    	$aCurrency = Service_Currency::getByField($aCustomer['customer_currency'], 'currency_code');
    	
    	//$convert_fee = round($fee / $aCurrency['currency_rate'], 2);
        //针对于订单重新计费对客户扣费，因汇率需要取历史汇率，所以不能直接以Service_Currency::converByCode来转换
        if(isset($fee['convert_fee'])){
            $convert_fee = $fee['convert_fee'];
        } else {
            $convert_fee = Service_Currency::converByCode($fee['currency_code'], $aCustomer['customer_currency'], $fee['price']);
        }
    	
    	$balanceRow = Service_CustomerBalance::getByField($customer_id, 'customer_id');
    	$aBalance = Service_CustomerBalance::getForUpdate($balanceRow['cb_id']);
    	$able_fee = floatval($aBalance['cb_value'] - $convert_fee);
    	if($aCustomer['cash_type']==1 && $able_fee < 0) {
    		$result['error'] = '账户余额不足，无法扣费';
    		return $result;
    	}
    	$uBalance['cb_value'] = $able_fee;
    	$uBalance['cb_update_time'] = date('Y-m-d H:i:s');
    	$uResult = Service_CustomerBalance::update($uBalance, $aBalance['cb_id']);
    	/* if(!$uResult) {
    		$result['error'] = '更新余额数据失败';
    		return $result;
    	} */
    	//记录余额日记
    	$aBalanceLog['customer_code'] = $aCustomer['customer_code'];
    	$aBalanceLog['customer_id'] = $customer_id;
    	$aBalanceLog['cbl_type'] = 2;	//扣费
    	$aBalanceLog['cbl_transaction_value'] = $convert_fee;
    	$aBalanceLog['cbl_value'] = $fee['price'];
    	$aBalanceLog['currency_rate'] = $aCurrency['currency_rate'];
    	$aBalanceLog['currency_code'] = $fee['currency_code'];
    	$aBalanceLog['cbl_note'] = $note;
    	$aBalanceLog['user_id'] = Service_User::getUserId();
    	$aBalanceLog['cbl_current_value'] = $able_fee;
    	$aBalanceLog['cbl_current_hold_value'] = $aBalance['cb_hold_value'];
    	$aBalanceLog['application_code'] = $application_code;
    	$aBalanceLog['cbl_refer_code'] = $refer_code;
    	$aBalanceLog['cbl_add_time'] = $addTime;
    	if(!empty($fee_id) && $fee_id>0){
    		$otherfee = Service_OtherFee::getByField($fee_id);
    		if(!empty($otherfee)){
    			$aBalanceLog['fee_id'] = $otherfee['fee_id'];
    			$aBalanceLog['application_code'] = $otherfee['fee_code'];
    		}
    	}
        if($convert_fee>0){
            $aResult = Service_CustomerBalanceLog::add($aBalanceLog);
            if(!$aResult) $result['error'] = '记录扣费日志失败';
            else {
                $result['fee'] = $convert_fee;
                $result['ask'] = 1;
            }
        }elseif($convert_fee==0){
            $result['ask'] = 1;
        }
    	return $result;
    }
    
    public static function valid($params){
    	$errors = array();
    	if($params['customer_code']!=''){
    		$customer = Service_Customer::getByField($params['customer_code'],'customer_code');
    		if(!empty($customer)){
    			if($customer['customer_status']!='2'){
    				$errors[] = "客户{$params['customer_code']}未激活";
    			}
    		}else{
    			$errors[] = "客户{$params['customer_code']}不存在";
    		}
    	}else{
    		$errors[] = "客户编码必填";
    	}
    	if($params['cbl_value']=='' || $params['cbl_value']==0){
    		$errors[] = "充值金额必须大于0";
    	}
    	if($params['transaction_number']==''){
    		$errors[] = "交易号必须填写";
    	}
    	return $errors;
    }
    /**
     * @author william-fan
     * @todo 客户入库扣款
     */
    public static function chongzhi($params){
    	$db = Service_Customer::getModelInstance()->getAdapter();
        $db->beginTransaction();
        try {
        	$result = array('state' => 0, 'message' => '充值失败', 'errorMessage' => array());
        	$errors=self::valid($params);
        	if(!empty($errors)){
        		$result['errorMessage'] = $errors;
        		$db->rollBack();
        		return $result;
        	}
        	$customer_code = $params['customer_code'];
        	/*
        	* @params $customerCode 客户代码 $options=>数组 支持多维
        	* $options['cbl_type'] 费用类型:0:冻结; 1:解冻; 2:扣款; 3:入款
        	* $options['cbl_value'] 费用金额;
        	* $options['currency_code'] 费用币种;
        	* $options['release_value'] 解冻费用（只支持费用类型为2，先回退再扣款）;
        	* $options['user_id'] 操作人员;后台操作人员ID/0系统操作/-1客户操作;
        	* $options['cbl_refer_code'] 参考号:orderCode ASNCode;
        	* $options['application_code'] 操作类型;
        	* $options['cbl_note'] 备注信息; */
        	
        	$balanceInfo = array(
        		'cbl_type'=>$params['cbl_type'],
        		'cbl_value'=>$params['cbl_value'],
        		'currency_code'=>$params['currency_code'],
        		'cbl_note'=>$params['cbl_note'],
        		'arrive_time'=>$params['arrive_time'],
        		'transaction_number'=>$params['transaction_number'],					
        	);
        	
        	$return = Common_CustomerFeeProcess::payment($customer_code,$balanceInfo);
        	if($return['state']=='1'){
        		$result['state'] = '1';
        		$result['message'] = "充值成功";
        		$result['data'] = $return['data'];
        	}else{
        		$result['message'] = $return['message'];
        	}
        	//$db->rollBack();
        	$db->commit();
        } catch (Exception $e) {
            $db->rollBack();
            $result['errorMessage'][] = $e->getMessage();
        }
        return $result;
    }
	
	
	/**
	 * 订单预扣费
	 * @author colin
	 * @param string $order_code
	 * @param int $customer_id
	 * @return true|string
	 */
   public function logisticsFreezeFee($order_code, $customer_id) {
   		//print_r($order_code);exit;
		
    	$customerRow = Service_Customer::getByField($customer_id);
    	$currencyRow = Service_Currency::getByField($customerRow['customer_currency'], 'currency_code');
		
        $orderRow = Service_LogisticsOrder::getByField($order_code, 'logistics_code');
		
    	//if($customerRow['cash_type']==2) return true;	//月结，不用冻结
        //集货转运类的订单在确认到提交时，无须冻结金额，在日本仓出货的时候也无须扣款，只在目的仓出货的时候发生扣款动作
        //if($orderRow['warehouse_id']=="1"&&$orderRow['order_mode_type']=="1") return true;
    	$addressRow = Service_LogisticsOrderAddress::getByField($order_code, 'logistics_code');
    	
    	$objCalculate = new Common_CalculateShipping();
    	$keys = array(
    		'warehouseId' => $orderRow['warehouse_id'],
    		'countryId' => $addressRow['loa_country'],
    		'smCode' => $orderRow['sm_code'],
    		'weight' => $orderRow['lo_weight']
    	);
     if($addressRow['loa_country']=='253'){
           //美国的要加入城市
            $conditionCity = array(
                'country_id'=>$addressRow['loa_country'],
                'city_name'=>$addressRow['loa_city']
            );
            $citys = Service_City::getByCondition($conditionCity,'*');
            if(!empty($citys)){
                $keys['cityId'] = $citys[0]['city_id'];
            }
        }		
    	$objCalculate->setParams($keys);
    	$result = $objCalculate->getRate();
    	if($result['state'] == 0)
    		return is_array($result['error']) ? implode(',', $result['error']) : $result['error'];
    	$fee = $result['data']['cost']['totalCost'];
        $cureencyCode = $result['data']['cost']['currency_code'];
    	$convert_fee = round($fee / $currencyRow['currency_rate'], 2);
    	//冻结金额
    	$balanceRow = Service_CustomerBalance::getByField($customer_id, 'customer_id');
    	/* print_r($balanceRow);
    	echo $convert_fee; */
    	$able_fee = floatval($balanceRow['cb_value'] - $convert_fee);
		
    	//echo $able_fee;
        if($customerRow['cash_type']==1){
            if($able_fee < 0) return '账户余额不足，无法冻结金额';
        }
    	$hold_fee = $balanceRow['cb_hold_value'] + $convert_fee;
    	//if(!$uResult) return '更新余额数据失败';
    	//记录余额日记
    	$aBalanceLog['customer_code'] = $customerRow['customer_code'];
    	$aBalanceLog['customer_id'] = $customer_id;
    	$aBalanceLog['cbl_type'] = 0;
    	$aBalanceLog['cbl_transaction_value'] = $convert_fee;
    	$aBalanceLog['cbl_value'] = $fee;
    	$aBalanceLog['currency_rate'] = $currencyRow['currency_rate'];
    	$aBalanceLog['currency_code'] = $cureencyCode ;//$customerRow['customer_currency'];
    	$aBalanceLog['cbl_note'] = '提交订单，冻结余额';
    	$aBalanceLog['user_id'] = -1;
    	$aBalanceLog['cbl_current_value'] = $able_fee;
    	$aBalanceLog['cbl_current_hold_value'] = $hold_fee;
    	$aBalanceLog['application_code'] = 1;
    	$aBalanceLog['cbl_refer_code'] = $order_code;
    	$aBalanceLog['cbl_add_time'] = date('Y-m-d H:i:s');
       if($convert_fee>0){
           $aResult = Service_CustomerBalanceLog::add($aBalanceLog);
           if(!$aResult) return '记录余额日志失败';
       }
       if($orderRow['sm_code']=='HKDHL'){
              $objOtherCalculte           = new Service_CalculateLogisticOtherFee();
            $otherFee= $objOtherCalculte->calculateLogisticOrderFrezRemoteFee($orderRow,$addressRow);
            $currencyCacheRows=Common_DataCache::getCurrency();
            if($otherFee&&$otherFee['price']>0){
            $convert_pianyuan_fee = Service_Currency::converByCode($otherFee['currency_code'], $customerRow['customer_currency'], $otherFee['price']);
           $able_fee = floatval($able_fee - $convert_pianyuan_fee);
           $hold_fee=  floatval($hold_fee+$convert_pianyuan_fee);
               	$aBalanceLog['customer_code'] = $customerRow['customer_code'];
                $aBalanceLog['customer_id'] = $customer_id;
                $aBalanceLog['cbl_type'] = 0;
                $aBalanceLog['cbl_transaction_value'] = $convert_pianyuan_fee;
                $aBalanceLog['cbl_value'] = $otherFee['price'];
                $aBalanceLog['currency_rate'] = $currencyRow['currency_rate'];
                $aBalanceLog['currency_code'] = $otherFee['currency_code'] ;//$customerRow['customer_currency'];
                $aBalanceLog['cbl_note'] = '提交订单，冻结余额';
                $aBalanceLog['user_id'] = -1;
                $aBalanceLog['cbl_current_value'] = $able_fee;
                $aBalanceLog['cbl_current_hold_value'] = $hold_fee;
                $aBalanceLog['application_code'] = 'logistics';
                 $aBalanceLog['fee_id'] = $otherFee['fee_id'];
                $aBalanceLog['cbl_refer_code'] = $order_code;
                $aBalanceLog['cbl_add_time'] = date('Y-m-d H:i:s');
                $aResult = Service_CustomerBalanceLog::add($aBalanceLog);
                if(!$aResult) return '记录余额日志失败';
            }
       }
       	$uBalance['cb_value'] = $able_fee;
    	$uBalance['cb_hold_value'] = $hold_fee;
    	$uBalance['cb_update_time'] = date('Y-m-d H:i:s');
    	$uResult = Service_CustomerBalance::update($uBalance, $balanceRow['cb_id']);
    	$uOrder['shipping_fee_estimate'] = $convert_fee;
    	$uOrder['update_time'] = date('Y-m-d H:i:s');
    	Service_Orders::update($uOrder, $order_code, 'order_code');
    	return true;
    }

   public function logisticsRefund($order_code,$customer_id){
       $condition = array(
           'customer_id'=>$customer_id,
           'cbl_refer_code'=>$order_code,
           'cbl_type'=>'2'
       );
       $feeCustomerBalanceLog = Service_CustomerBalanceLog::getByCondition($condition,"*",0,0,"cbl_id desc");
       $aCustomer = Service_Customer::getByField($customer_id);
       $aBalance = Service_CustomerBalance::getByField($customer_id, 'customer_id');
       if(empty($aBalance)) return '找不到客户账户余额记录';
       $balanceRow = Service_CustomerBalance::getForUpdate($aBalance['cb_id']);
       $refund = $feeCustomerBalanceLog[0]['cbl_transaction_value'];
       $hold_value = $balanceRow['cb_hold_value'] + $refund;
       $uBalance['cb_value'] = $balanceRow['cb_value'] + $refund;
       $uBalance['cb_hold_value'] = $hold_value;
       $uBalance['cb_update_time'] = date('Y-m-d H:i:s');
       $uResult = Service_CustomerBalance::update($uBalance, $balanceRow['cb_id']);

       //$currencyRow= Service_Currency::getByField($feeCustomerBalanceLog[0]['currency_code'],"currency_code","*");
       $aBalanceLog['customer_code'] = $aCustomer['customer_code'];
       $aBalanceLog['customer_id'] = $aCustomer['customer_id'];
       $aBalanceLog['cbl_type'] = 4;
       $aBalanceLog['cbl_transaction_value'] = $refund;
       $aBalanceLog['cbl_value'] = $feeCustomerBalanceLog[0]['cbl_value'];
       $aBalanceLog['currency_rate'] = $feeCustomerBalanceLog[0]['currency_rate'];
       $aBalanceLog['currency_code'] = $feeCustomerBalanceLog[0]['currency_code'];
       $aBalanceLog['cbl_note'] = '区外仓订单收货，退款';
       $aBalanceLog['user_id'] = Service_User::getUserId();
       $aBalanceLog['cbl_current_value'] = $uBalance['cb_value'];
       $aBalanceLog['cbl_current_hold_value'] = $uBalance['cb_hold_value'];
       $aBalanceLog['application_code'] = 1;
       $aBalanceLog['cbl_refer_code'] = $order_code;
       $aBalanceLog['cbl_add_time'] = date('Y-m-d H:i:s');
       $aResult = Service_CustomerBalanceLog::add($aBalanceLog);
       //删除成本记录
       Service_CostRecording::delete($order_code,"order_code");
       //删除运费记录
       Service_LogisticsCost::delete($order_code,"order_code");
       return $aResult ? true : '记录余额日志失败';
   }
   
   
   /**
     * ASN预扣费
     * @author william-fan
     */
    public function freezeASNFee($receiving_code, $customer_id,$asnRow = array()) {
    	$customerRow = Service_Customer::getByField($customer_id);
    	$currencyRow = Service_Currency::getByField($customerRow['customer_currency'], 'currency_code');
    	
		if(empty($asnRow)){
			$asnRow = Service_Receiving::getByField($receiving_code,'receiving_code');	
		}	
		
    	$objOtherFeeCalculate = new Common_CalculateOtherFee();
    	$keys = array(
    			'warehouseId' => $asnRow['warehouse_id'],
    			'receiving_code'=>$receiving_code,
    	);
    	$objOtherFeeCalculate->setParams($keys);
    	$otherfeeResult = $objOtherFeeCalculate->getOtherFee('TIHUO'); //预扣提货费
    	$tihuofee = $otherfeeResult['data']['otherFee']['tihuo']['price'];
    	
    	//冻结金额
    	$balanceRow = Service_CustomerBalance::getByField($customer_id, 'customer_id');
    	/* print_r($balanceRow);
    	 echo $convert_fee; */
		
    	$able_fee = floatval($balanceRow['cb_value']);
    	//echo $able_fee;
    	if($customerRow['cash_type']==1){
    		if($able_fee < 0) return '账户余额不足，无法冻结金额';
    	}
    	$hold_fee = $balanceRow['cb_hold_value'];
    	if($tihuofee>0){
    		//$convert_caozuo_fee = round($caozuofee / $currencyRow['currency_rate'], 2);
    		$convert_tihuuo_fee = Service_Currency::converByCode($otherfeeResult['data']['otherFee']['tihuo']['currency_code'], $customerRow['customer_currency'], $tihuofee);
    		$able_fee = $able_fee-$convert_tihuuo_fee;
    		 
    		$hold_fee+=$convert_tihuuo_fee;
    		 
    		$aBalanceLog['customer_code'] = $customerRow['customer_code'];
    		$aBalanceLog['customer_id'] = $customer_id;
    		$aBalanceLog['cbl_type'] = 0;
    		$aBalanceLog['cbl_transaction_value'] = $convert_tihuuo_fee;
    		$aBalanceLog['cbl_value'] = $tihuofee;
    		$aBalanceLog['currency_rate'] = $currencyRow['currency_rate'];
    		$aBalanceLog['currency_code'] = $otherfeeResult['data']['otherFee']['tihuo']['currency_code'];
    		$aBalanceLog['cbl_note'] = 'ASN提交扣提货费，冻结余额';
    		$aBalanceLog['user_id'] = -1;
    		$aBalanceLog['cbl_current_value'] = $able_fee;
    		$aBalanceLog['cbl_current_hold_value'] = $hold_fee;
    		$aBalanceLog['application_code'] = 1;
    		$aBalanceLog['cbl_refer_code'] = $receiving_code;
    		$aBalanceLog['cbl_add_time'] = date('Y-m-d H:i:s');
    		//$otherfee_caozuo=Service_OtherFee::getByField('caozuo','fee_code');
			
    		if($otherfeeResult['data']['otherFee']['tihuo']['fee_id']>0){
    			$aBalanceLog['fee_id'] = $otherfeeResult['data']['otherFee']['tihuo']['fee_id'];
    		}
    		 
    		$aResult = Service_CustomerBalanceLog::add($aBalanceLog);
    	}
    	$uBalance['cb_value'] = $able_fee;
    	$uBalance['cb_hold_value'] = $hold_fee;
    	$uResult = Service_CustomerBalance::update($uBalance, $balanceRow['cb_id']);
    	return true;
    }

    public static function freezeOtherFee($money,$currencyCode,$referCode,$otherFeeId,$note,$customerId) {
        $customerRow = Service_Customer::getByField($customerId,'customer_id',"*");
        $currencyRow = Service_Currency::getByField($customerRow['customer_currency'], 'currency_code');
        $customerBalance = Service_CustomerBalance::getByField($customerId,'customer_id',"*");
        $able_fee = floatval($customerBalance['cb_value']);
        //echo $able_fee;
        if($customerRow['cash_type']==1){
            if($able_fee < 0) {
                throw new Exception('账户余额不足，无法冻结金额');
            }
        }
        $hold_fee = $customerBalance['cb_hold_value'];
        if($money>0){
            $convertMoney = Service_Currency::converByCode($currencyCode,$customerRow['customer_currency'], $money);
            $able_fee = $able_fee-$convertMoney;
            $hold_fee+=$convertMoney;
            $aBalanceLog['customer_code'] = $customerRow['customer_code'];
            $aBalanceLog['customer_id'] = $customerId;
            $aBalanceLog['cbl_type'] = 0;
            $aBalanceLog['cbl_transaction_value'] = $convertMoney;
            $aBalanceLog['cbl_value'] = $money;
            $aBalanceLog['currency_rate'] = $currencyRow['currency_rate'];
            $aBalanceLog['currency_code'] =$currencyCode;
            $aBalanceLog['cbl_note'] = $note;
            $aBalanceLog['user_id'] = -1;
            $aBalanceLog['cbl_current_value'] = $able_fee;
            $aBalanceLog['cbl_current_hold_value'] = $hold_fee;
            $aBalanceLog['application_code'] = 1;
            $aBalanceLog['cbl_refer_code'] = $referCode;
            $aBalanceLog['cbl_add_time'] = date('Y-m-d H:i:s');
            //$otherfee_caozuo=Service_OtherFee::getByField('caozuo','fee_code');
            if($otherFeeId>0){
                $aBalanceLog['fee_id'] = $otherFeeId;
            }

            $aResult = Service_CustomerBalanceLog::add($aBalanceLog);
        }
        $uBalance['cb_value'] = $able_fee;
        $uBalance['cb_hold_value'] = $hold_fee;
        $uResult = Service_CustomerBalance::update($uBalance, $customerBalance['cb_id']);
        if(!$uResult){
            throw new Exception('冻结费用失败');
        }
        return true;
    }
		
    
}