# -*- coding: utf-8 -*- #!/usr/bin/env python """ 后记 最后一笔收入发生在 2019年6月26日10点5分59秒 订单号 20190626100559Q00000000037501545 (在此之前的所有的订单收入 - 经销商已经提走的钱 ) * (1 - 0.006 (微信和支付宝的手续费) ) (9449.93 - 6070.14 )* 0.994 = 3359.51 * 首先从支付宝和微信账单取出6月所有的外部订单号 (取出所有订单较为麻烦和耗时) * 合并两者得到集合 -> orders_paid_to_us * 得到该厂商下所有6月支付成功的充值记录,包含recharge, chargeCard -> langxin_orders_in_june * 筛选一次,[ x | x <- filter(\x -> x.orderNo, orders_paid_to_us) ] * 取到6月没有支付给我们的订单号 * 取出该厂家从一开始到6月的所有订单 - 6月没有付给我们的订单 * 算出数据 - 所有提现的金额 * 相关数据可以查看 /var/archive """ import datetime from apilib.monetary import RMB, sum_rmb from apps.web.management.models import Manager from apps.web.dealer.models import Dealer, WithdrawRecord from apps.web.agent.models import Agent from apps.web.user.models import RechargeRecord WECHAT_OUT_TRADE_ORDER_FIELD_NAME = ALIPAY_OUT_TRADE_ORDER_FIELD_NAME = u'商户订单号' WECHAT_REMARK_FIELD_NAME = u'商户数据包' ALIPAY_REMARK_FIELD_NAME = u'备注' WECHAT_TRADE_STATUS_FIELD_NAME = u'交易状态' ALIPAY_TRADE_STATUS_FIELD_NAME = u'' langxin_manager = Manager.objects(username='13738357542').get() agentIds = [ str(_.id) for _ in Agent.objects(managerId=str(langxin_manager.id)) ] dealerIds = [ str(_.id) for _ in Dealer.objects(agentId__in=agentIds) ] started_datetime = datetime.datetime.strptime('2019-06-26', '%Y-%m-%d') langxin_agent = Agent.objects(username='13738357542').get() langxin_wechat_gateway = get_wechat_payment_gateway_for_user(langxin_agent) langxin_alipay_gateway = get_alipay_payment_gateway(langxin_agent) langxin_raw = { 'appid': langxin_wechat_gateway.appid, 'mch_id': langxin_wechat_gateway.mchid } our_agent = Agent.objects().first() our_wechat_gateway = get_wechat_payment_gateway_for_user(our_agent) our_alipay_gateway = get_alipay_payment_gateway(our_agent) our_raw = { 'appid': langxin_wechat_gateway.appid, 'mch_id': langxin_wechat_gateway.mchid } #: 此列表通过下载支付宝和微信的订单,然后 langxin_orders_havent_paid_to_us_in_june = [] after_june = datetime.datetime.now().replace(month=6, day=1, hour=0, second=0) before_july = datetime.datetime.now().replace(month=7, day=1, hour=0, second=0) langxin_orders_in_june = RechargeRecord.objects(result='success', via__in=['recharge', 'chargeCard'], dateTimeAdded__lt=before_july, dateTimeAdded__gt=after_june, ownerId__in=dealerIds).order_by('dateTimeAdded') langxin_orders_till_june = RechargeRecord.objects(result='success', via__in=['recharge', 'chargeCard'], dateTimeAdded__lt=before_july, ownerId__in=dealerIds).order_by('dateTimeAdded') langxin_orders_till_june_paid_to_us = list(RechargeRecord.objects(result='success', via__in=['recharge', 'chargeCard'], dateTimeAdded__lt=before_july, orderNo__nin=langxin_orders_havent_paid_to_us_in_june, ownerId__in=dealerIds).order_by('dateTimeAdded')) def sum_previous_orders(records): return sum_rmb( _.money for _ in records) confirmed_paid_to_us = set() def confirm_third_party(records): for record in records: if record.id in confirmed_paid_to_us: continue if record.gateway == 'wechat': result = our_wechat_gateway.order_query(record.orderNo) if result.get('trade_state_desc') == u'支付成功': confirmed_paid_to_us.add(record.id) elif record.gateway == 'alipay': result = our_alipay_gateway.api_alipay_trade_query(record.orderNo) if result.get('trade_status') == 'TRADE_SUCCESS' or result.get('trade_status') == 'TRADE_FINISHED': confirmed_paid_to_us.add(record.id) else: raise ValueError() def sum_previous_withdraw(): return RMB(WithdrawRecord.get_succeeded_records(ownerId__in=dealerIds, postTime__lt=before_july).sum('amount')) if __name__ == '__main__': assert len(langxin_orders_havent_paid_to_us_in_june), 'get data from pickle or download bills from wechat/alipay' confirm_third_party(langxin_orders_till_june_paid_to_us) how_much_we_got_paid = sum_previous_orders( _ for _ in langxin_orders_till_june_paid_to_us if _.id in confirmed_paid_to_us ) print('how_much_we_got_paid=%s' % (how_much_we_got_paid,)) how_much_we_paid = sum_previous_withdraw() print('how_much_we_paid=%s' % (how_much_we_paid,)) how_much_we_should_pay_to_langxin = how_much_we_paid - how_much_we_got_paid print('how_much_we_should_pay_to_langxin=%s' % (how_much_we_should_pay_to_langxin,))