# -*- coding: utf-8 -*- # !/usr/bin/env python import logging from apilib.monetary import RMB from apps.web.agent.proxy import record_agent_withdraw_fee from apps.web.common.transaction.withdraw import WithdrawService, WithdrawRetryService from apps.web.common.transaction import WithdrawHandler from apps.web.dealer.models import Dealer logger = logging.getLogger(__name__) class DealerWithdrawHandler(WithdrawHandler): def on_approve(self): dealer = self.payee # type: Dealer try: _map = {} for item in self.record.partition: agent_id = item['id'] if agent_id in _map: _map[agent_id]['earned'] += RMB(item['earned']) else: _map[agent_id] = { 'earned': RMB(item['earned']) } for agent_id, item in _map.iteritems(): earned = item['earned'] if earned <= RMB(0): continue detail = { 'recharge_record_id': self.record.id, 'name': dealer.nickname, 'sum_of_price': self.record.serviceFee, 'withdrawAmount': self.record.amount, 'withdrawFeeRatio': self.record.withdrawFeeRatio } record_agent_withdraw_fee(agentId = agent_id, source_key = self.record.withdrawSourceKey, amount = earned, detail = detail) except Exception as e: logging.exception(e) class DealerWithdrawService(WithdrawService): pass class DealerWithdrawRetryService(WithdrawRetryService): def get_payee(self): return Dealer.objects(id = self.record.ownerId).get()