def has_bought_newcomer_product(self): """判断是否购买了新手产品 暂时适用于2016年5月1日之后购买第一笔订单的用户""" from core.models.hoarder.order import HoarderOrder if ZhiwangOrder.has_wrapped_product(self.user_id): return True start_time = datetime(2016, 05, 01, 0, 0, 0) xm_orders = XMOrder.get_multi_by_user(self.user_id) xm_orders = [ o for o in xm_orders if o.status in [ XMOrder.Status.committed, XMOrder.Status.shelved, XMOrder.Status.paying, XMOrder.Status.success ] ] xm_order = xm_orders[-1] if len(xm_orders) > 0 else None condition_xm_orders = bool(xm_order and xm_order.creation_time < start_time) orders = HoarderOrder.get_multi_by_user(self.user_id) orders = [ o for o in orders if o.status in [ HoarderOrder.Status.committed, HoarderOrder.Status.shelved, HoarderOrder.Status.paying, HoarderOrder.Status.success ] ] order = orders[-1] if len(orders) > 0 else None condition_orders = bool(order and order.creation_time < start_time) if any([ HoardOrder.get_total_orders(self.user_id) > 0, ZhiwangOrder.get_total_orders(self.user_id) > 0, condition_xm_orders, condition_orders ]): return True return False
def get_order(order_id): order = ZhiwangOrder.get(order_id) if not order: abort(404) if not order.is_owner(g.user): abort(403) return order
def total_orders(self): from core.models.hoarder.order import HoarderOrder yx_orders = HoardOrder.get_total_orders(self.user_id) zw_orders = ZhiwangOrder.get_total_orders(self.user_id) xm_orders = XMOrder.get_total_orders(self.user_id) sxb_orders = HoarderOrder.get_order_amount_by_user(self.user_id) return yx_orders + zw_orders + xm_orders + sxb_orders
def genernate_report(filename, time_str): csv_file = open(filename, 'wb') writer = csv.writer(csv_file, delimiter=',') header = ['订单编号', '资产编号', '订单金额', '支付金额', '基础利率', '实际利率', '下单时间'] writer.writerow(header) year, month = time_str.split('-') sql = ('select id from hoard_zhiwang_order where status=%s' 'and year(creation_time)=%s and month(creation_time)=%s') params = (ZhiwangOrder.Status.success.value, year, month) rs = db.execute(sql, params) orders = [ZhiwangOrder.get(r[0]) for r in rs] for order in orders: line = [0] * len(header) asset = ZhiwangAsset.get_by_order_code(order.order_code) if not asset: # asset not found for specific order code line[0] = order.order_code line[1] = '[故障单]' writer.writerow(line) continue line = [ order.order_code, asset.asset_no, '%.2f' % order.amount, '%.2f' % (order.pay_amount or order.amount), # compatible '%.2f' % asset.annual_rate, '%.2f' % asset.actual_annual_rate, order.creation_time ] writer.writerow([str(o) for o in line]) csv_file.close()
def obtain_zw_order(order_id): order = ZhiwangOrder.get(order_id) if not order: warning('用户访问不存在的订单', order_id=order_id) abort(404, u'该订单不存在') if not order.is_owner(request.oauth.user): warning('用户访问他人的订单', order_id=order_id) abort(403, u'该订单不属于当前账号,无法继续查看') return order
def zhiwang_payment_tracking(order_id): """同步指旺用户订单支付状态""" from core.models.hoard.zhiwang import ZhiwangOrder, ZhiwangAccount expected_status_set = frozenset([ ZhiwangOrder.Status.success, ZhiwangOrder.Status.failure, ]) order = ZhiwangOrder.get(order_id) origin_status = order.status if origin_status in expected_status_set: return zw_ucode = ZhiwangAccount.get_by_local(order.user_id).zhiwang_id response = zhiwang.order_status(zw_ucode, order.order_code) assert response.order_code == order.order_code assert int(response.pay_amount) == int(order.amount) new_status = ZhiwangOrder.MUTUAL_STATUS_MAP.get(response.order_status) if new_status is not origin_status: # 当订单属于自然失败时不再更新状态 if new_status is ZhiwangOrder.Status.failure and origin_status in ( ZhiwangOrder.Status.unpaid, ZhiwangOrder.Status.committed): return # 只有当订单状态发生变化时才更新状态 order.update_status(new_status) if new_status in expected_status_set: return if bearychat.configured: # 长时间未被结算中心处理的订单将发往BC提醒 since_delta = datetime.datetime.now() - order.creation_time if since_delta > datetime.timedelta(hours=6): message = (u'已经**{}**了, 指旺订单 {}({}) 状态仍为 {}. ' u'[[查看详情]](http://earth.guihua.com/hoard/user/{})') bearychat.say( message.format(format_timedelta(since_delta, locale='zh_CN'), order.id_, order.order_code, new_status.name, order.user_id)) error_msg = (u'tube:{0}, order_id:{1}, order code:{2}, ' u'new status:{3},user_id:{4}') raise WorkerTaskError( error_msg.format(zhiwang_payment_tracking.tube, order.id_, order.order_code, new_status.name, order.user_id))
def zhiwang_asset_fetching(order_id): """为指旺资产找到匹配订单""" from core.models.hoard.zhiwang import ZhiwangOrder, ZhiwangAccount, ZhiwangAsset order = ZhiwangOrder.get(order_id) asset = ZhiwangAsset.get_by_order_code(order.order_code) if asset: return user_id = order.user_id zw_ucode = ZhiwangAccount.get_by_local(user_id).zhiwang_id all_assets = sorted(zhiwang.asset_list(zw_ucode), key=operator.attrgetter('created_at'), reverse=True) for asset_info in all_assets: detail = zhiwang.asset_details(zw_ucode, asset_info.asset_code) if detail.order_code == order.order_code: # find the matcher ZhiwangAsset.add( asset_info.asset_code, detail.order_code, order.bankcard_id, detail.user_bank_account, detail.product_id, user_id, ZhiwangAsset.MUTUAL_STATUS_MAP.get(detail.status), detail.annual_rate, detail.actual_annual_rate, detail.create_amount, detail.current_amount, detail.base_interest, detail.actual_interest, # actual = expect detail.current_interest, detail.interest_start_date, detail.interest_end_date, detail.expected_payback_date, detail.created_at.naive) return raise WorkerTaskError(zhiwang_asset_fetching.tube)
def zhiwang_contract(order_id): """指旺购买合同. reqheader Authorization: OAuth 2.0 Bearer Token :status 200: 返回 :class:`.ZhiwangContractSchema` :status 403: 获取合同失败 """ contract_schema = ZhiwangContractSchema(strict=True) order = ZhiwangOrder.get(order_id) asset = ZhiwangAsset.get_by_order_code(order.order_code) if not asset: abort(403, u'资产合同正在准备中') if not asset.contract: try: content = fetch_asset_contract(request.oauth.user.id_, asset) except ContractFetchingError as e: abort(403, e.args[0]) else: content = asset.contract data = {'contract': content} return jsonify(success=True, data=contract_schema.dump(data).data)
def commit(self): # 确认所记录礼券已被成功使用 if self.provider is zhiwang: from core.models.hoard.zhiwang import ZhiwangOrder order = ZhiwangOrder.get(self.order_id) if order.status is not ZhiwangOrder.Status.success: raise ValueError('bound order %s has not succeeded' % self.order_id) sql = 'update {.table_name} set status=%s where id=%s'.format(self) params = (self.Status.settled.value, self.id_) db.execute(sql, params) db.commit() rsyslog.send('%s\t%s\t%s' % (self.provider.shortcut, self.order_id, self.id_), tag='coupon_usage') self.clear_cache(self.id_) self.clear_cache_record_ids_by_coupon_key(self.coupon_id) self.clear_cache_record_ids_by_user_key(self.user_id) self.clear_cache_record_by_partner_order(self.provider_id, self.order_id)
def pay(order_code, pay_code): """支付订单""" sms_code = request.form.get('sms_code').strip() order = ZhiwangOrder.get_by_order_code(order_code) or abort(404) if not order.is_owner(g.user): abort(403, u'该订单不属于此用户') if order.status is ZhiwangOrder.Status.committed: abort(403, u'您的订单已在处理中,请勿重复提交') if not sms_code: abort(400) # 向指旺发起支付请求 try: pay_order(order, pay_code, sms_code) except (ProfitHikeLockedError, FirewoodBusinessError, CouponBusinessError, DealingError, PayTerminatedError) as e: # 优惠被冻结、抵扣金使用错误、礼券无法使用、订单正在被处理或支付被告知失败 abort(403, unicode(e)) redirect_url = url_for('savings.zhiwang.order_complete', order_id=order.id_) return jsonify(r=True, redirect_url=redirect_url)