def build_aditional_filters(filters): init_date = filters.get('init_date', False) last_date = filters.get('last_date', False) delivered_route_id = filters.get('delivered_route_id', False) presale_route_id = filters.get('presale_route_id', False) strategic_channel = filters.get('strategic_channel', False) gec = filters.get('gec', False) description_channel = filters.get('description_channel', False) filters = "" if (delivered_route_id): filters = filters + """ AND R.id =""" + str(delivered_route_id) + """ """ if (presale_route_id): filters = filters + """ AND R.id =""" + str(presale_route_id) + """ """ if (strategic_channel): filters = filters + """ AND CH.id =""" + str(strategic_channel) + """ """ if (init_date and last_date): date_start = utils.format_date(init_date, '01:00') date_end = utils.format_date(last_date, '23:59') filters = filters + """ AND ordered_at BETWEEN ('""" + date_start + """') AND ('""" + date_end + """') """ return filters
def to_json(self): return dict( coupon_type=self.coupon_type, value=self.value, code=self.code, effective_date=format_date(self.effective_date, '%Y-%m-%d'), expire_date=format_date(self.expire_date, '%Y-%m-%d'), is_expired=self.is_expired, description=self.description, )
def to_json(self): return { 'type': self.banner_type, 'target': self.target, 'img': self.img, 'created_at': format_date(self.created_at) }
def order_json(order): """ :param order: :return: """ if not order.is_paid: order.update_amount() order.reload() entries_json = [] for entry in order.entries: if order.order_type == ORDER_TYPE.TRANSFER: entries_json.append(transfer_entry_json(entry)) else: entries_json.append(entry_json(entry)) refund_entries_json = [] for entry in order.refund_entries: refund_entries_json.append(entry.to_json()) provider_json = LogisticProvider.objects.get( name=order.logistic_provider, country=order.address.country).to_json() result = dict( id=str(order.id), short_id=str(order.id), status=order.status, customer_id=str(order.customer_id), amount=order.amount, amount_usd=order.amount_usd, cn_shipping=order.cn_shipping, coin=order.coin, lucky_money=order.lucky_money, discount=order.discount, final=order.final, estimated_tax=order.estimated_tax, payment_status='PAID' if order.is_paid else 'UNPAID', payment_ref_number=[ p.ref_number for p in Payment.objects(order=order) ], created_at=format_date(order.created_at), entries=entries_json, refund_entries=refund_entries_json, refund_amount=order.refund_amount, real_tax=order.real_tax, provider=provider_json, address='', ) if order.address: result.update(dict(address=order.address.to_json())) if order.logistics: result.update(logistics=[ logistic_json(logistic, order.order_type) for logistic in order.logistics ]) return result
def restruct_query(data): """ :param data: :return: """ format_date = lambda d: datetime.datetime.strptime(d, '%Y-%m-%d %H:%M:%S.%f') status = data.get('status') query = {} for k, v in data.items(): if v in [None, 'None', '', 'null']: continue if k[-3:] == '_no': query.update({'detail__%s' % k: v}) elif k in ['status']: query.update({'detail__%s' % k: v}) elif k == 'start': if status: date_field = LogisticDetail.attr_by_log_status[status] query.update({'detail__%s__gte' % date_field: format_date(v)}) else: query.update({'created_at__gte': format_date(v)}) elif k == 'end': if status: date_field = LogisticDetail.attr_by_log_status[status] query.update({'detail__%s__lt' % date_field: format_date(v)}) else: query.update({'created_at__lt': format_date(v)}) elif k == 'query': if v.startswith('MB'): query.update({'detail__partner_tracking_no': v}) elif ObjectId.is_vaild(v): query.update({'id': v}) else: query.update({'tracking_no': v}) elif k == 'partner': partner = Partner.objects(name=v).first() query.update({'detail__partner': partner}) elif k == 'channel': query.update({'detail__channel': v}) else: query.update({'%s' % k: v}) return query
def to_json(self): return { 'amount': self.amount, 'coin_type': self.coin_type, 'time': format_date(self.time), 'kind': self.kind, 'type': self.kind, 'reason': self.reason, 'reason_id': self.reason_id, 'description': self.description or self.descs.get(self.reason, ''), 'is_hold': False, }
def generate(): yield ','.join(st for st in FIELDS) + '\n' for logistic in logistics: yield ','.join([ str(logistic.id), logistic.detail.partner_tracking_no, logistic.detail.carrier_tracking_no, logistic.detail.cn_tracking_no, logistic.detail.cn_logistic_name, format_date(logistic.detail_payment_received_date), str(logistic.order.id), str(logistic.order.short_id), logistic.order.address.receiver, logistic.order.address.mobile_number, format_date(logistic.detail.processing_date), format_date(logistic.detail.shipping_date), format_date(logistic.detail.port_arrived_date), format_date(logistic.detail.received_date), format_date(logistic.detail.modified), logistic.detail.partner.name if logistic.detail.partner else '', ';'.join([r.content for r in logistic.detail.remarks]), logistic.detail.extra or '', str(logistic.estimated_weight), logistic.detail.channel, ]) + '\n'
def build_ranking_filters(filters): init_date = filters.get('init_date', False) last_date = filters.get('last_date', False) presale_route = filters.get('presale_route', False) delivery_route = filters.get('delivery_route', False) product = filters.get('product', False) group_product = filters.get('group_product', False) filters = "" if (presale_route): if presale_route["presale_route_id"] != 0: filters = filters + """ AND o.route_id =""" + str(presale_route["presale_route_id"]) + """ """ if (delivery_route): if delivery_route["delivery_route_id"] != 0: filters = filters + """ AND o.route_delivery_id =""" + str( delivery_route["delivery_route_id"]) + """ """ if (product): if product["product_id"] != 0: filters = filters + """ AND p.id =""" + str(product["product_id"]) + """ """ if (group_product): if group_product["group_product_id"] != 0: filters = filters + """ AND p.product_group_id =""" + str(group_product["group_product_id"]) + """ """ if (init_date and last_date): date_start = utils.format_date(init_date, '00:00:00') date_end = utils.format_date(last_date, '23:59:59') filters = filters + """ AND o.ordered_at BETWEEN ('""" + date_start + """') AND ('""" + date_end + """') """ return filters
def simple_order_json(order): """ :param order: :return: """ if not order.is_paid: order.update_amount() order.reload() entries_json = [] for entry in order.entries: if order.order_type == ORDER_TYPE.TRANSFER: entries_json.append(transfer_entry_json(entry)) else: entries_json.append(entry_json(entry)) refund_entries_json = [] for entry in order.refund_entries: refund_entries_json.append(entry.to_json()) result = dict( id=str(order.id), short_id=str(order.id), current_status=ORDER_STATUS_DESCRIPTION.get(order.status, ''), status=order.status, customer_id=str(order.customer_id), amount=order.amount, amount_usd=order.amount_usd, cn_shipping=order.cn_shipping, coin=order.coin, lucky_money=order.lucky_money, discount=order.discount, final=order.final, payment_status='PAID' if order.is_paid else 'UNPAID', payment_ref_number=[ p.ref_number for p in Payment.objects(order=order) ], created_at=format_date(order.created_at), entries=entries_json, refund_entries=refund_entries_json, refund_amount=order.refund_amount, ) return result
def transfer_order_price_json(order): entries_json = [] for entry in order.entries: entries_json.append(transfer_entry_json(entry)) provider_json = LogisticProvider.objects.get( name=order.logistic_provider, country=order.address.country).to_json() result = dict( id=str(order.id), short_id=str(order.sid), status=order.status, customer_id=str(order.customer_id), amount=order.amount, amount_usd=order.amount_usd, cn_shipping=order.cn_shipping, coin=order.coin, lucky_money=order.lucky_money, discount=order.discount, final=order.final, estimated_tax=order.estimated_tax, payment_status='PAID' if order.is_paid else 'UNPAID', created_at=format_date(order.created_at), entries=entries_json, refund_amount=order.refund_amount, real_tax=order.real_tax, provider=provider_json, address='', ) if order.address: result.update(dict(address=order.address.to_json())) if order.logistics: result.update( dict(logistics=[ logistic_json(logistic, order.order_type) for logistic in order.logistics ])) return result
def logistic_json(logistic, order_type): """ :param logistic: :param order_type: :return: """ if order_type == ORDER_TYPE.TRANSFER: entries = [transfer_entry_json(entry) for entry in logistic.entries] else: entries = [entry_json(entry) for entry in logistic.entries] all_status = [{ 'status': status, 'desc': ORDER_STATUS_DESCRIPTION.get(status, '') } for status in ROUTES.get(logistic.detail.route, 'DEFAULT')] current_status = logistic.detail.status history = [] for status in LOG_STATUS: detail_field = logistic.detail.attr_by_log_status[status] value = getattr(logistic.detail, detail_field) if not value: continue history.append( dict( desc=SHIPPING_HISTORY[status], time=format_date(value) if value else '', )) if status == 'TRANSFER_APPROVED': history.append(dict(desc='', time='')) if status == 'WAREHOUSE_IN' and logistic.detail.real_weight > 0: history.append( dict( desc='包裹总总量: %s kg' % str(logistic.detail.real_weight / 1000), time='', )) # tracking information tracking = None if status == LOG_STATUS.SHIPPING and logistic.detail.cn_logistic_name: history.append( dict( desc='国际快递公司: %s' % logistic.detail.cn_logistic_name.upper(), time='国际快递单号: %s' % logistic.detail.cn_tracking_no, )) tracking = ExpressTracking.find( company=logistic.detail.n_logistic_name, number=logistic.detail.cn_tracking_no) for d in (reversed(tracking.data) if tracking else []): desc = re.sub(r'\s{2, }', ' ', d.get('context', '')) history.append(dict( desc=desc, time=d.get('time', ''), )) if status == current_status: break if current_status in ['PENDING_RETURN', 'RETURNING', 'RETURNED']: current_status = 'PAYMENT_RECEIVED' return { 'id': str(logistic.id), 'entries': entries, 'all_status': all_status, 'current_status': current_status, 'history': history, 'partner_tracking_no': logistic.detail.partner_tracking_no, }
def build_orders_filters(filters): init_date = filters.get('init_date', False) last_date = filters.get('last_date', False) presale_route = filters.get('presale_route', False) delivery_route = filters.get('delivery_route', False) branch = filters.get('branch', False) canal_giro = filters.get('canal_giro', False) canal_est = filters.get('canal_est', False) business_name = filters.get('business_name',False) population = filters.get('population',False) filters = "" if (presale_route): if presale_route["presale_route_id"] != 0: filters = filters + """ AND o.route_id ="""+str(presale_route["presale_route_id"])+""" """ if (delivery_route): if delivery_route["delivery_route_id"] != 0: filters = filters + """ AND o.route_delivery_id ="""+str(delivery_route["delivery_route_id"])+""" """ if (branch): if branch["branch_id"] != 0: filters = filters + """ AND B.id ="""+str(branch["branch_id"])+""" """ if (init_date and last_date): date_start = utils.format_date(init_date, '00:00:00') date_end = utils.format_date(last_date, '23:59:59') filters = filters + """ AND o.ordered_at BETWEEN ('"""+date_start+"""') AND ('"""+date_end+"""') """ if (canal_giro or canal_est or (business_name and business_name!="") or cities_name): filters += """ AND o.client_id IN (SELECT C.id FROM clients as C LEFT JOIN addresses A ON C.address_id = A.id LEFT JOIN cities CI ON A.city_id = CI.id LEFT JOIN client_types CL ON C.client_type_id=Cl.id """ condicionales = "" if (canal_giro): if canal_giro["canal_giro_id"] != 0: condicionales = " AND " if condicionales != "" else " WHERE " filters += condicionales + " Cl.id ="+str(canal_giro["canal_giro_id"]) if (canal_est): if canal_est["canal_est_id"] != 0: condicionales = " AND " if condicionales != "" else " WHERE " filters += condicionales + " C.channel_id ="+str(canal_est["canal_est_id"]) if (business_name): if(business_name!=""): condicionales = " AND " if condicionales != "" else " WHERE " filters += condicionales + " C.business_name LIKE '%"+business_name+"%'" if (population): if population["population_id"] != 0: condicionales = " AND " if condicionales != "" else " WHERE " filters += condicionales + " Cl.id ="+str(population["population_id"]) filters += ") " return filters
class MBModelView(PermissionModelView): """""" column_type_formatters = { datetime.datetime: lambda view, value: format_date(value) }
def download(self): """ :return: """ FIELDS = [ '包裹ID', 'IMG NO', 'CTN', '下单日期', '订单ID', '订单短号', '收件人', '手机号', '合作物流商', 'remark', '下单备注', '估重', '渠道' ] now = datetime.datetime.now() status = request.args.get('status') query = restruct_query(request.args) delay_export = query.get('delay_export') and query.pop('delay_export') delay_type = query.get('delay_type') and query.pop('delay_type') try: logistics = Logistic.objects(is_closed=False, **query) if status: logistics = logistics.order_by( 'detail__%s' % LogisticDetail.attr_by_log_status[status]) except: pass if delay_export: date_field = LogisticDetail.attr_by_log_status[status] delay_days = datetime.timedelta(days=delay_status_by_date[status]) query = { 'detail__%s__lt' % date_field: datetime.datetime.utcnow() - delay_days, 'detail__status': status, } logistics = logistics.filter(**query).order_by('detail__%s' % date_field) if delay_type: logistics = logistics.filter( detail__delay_details__reason__contains=delay_type) if query.get('receiver'): addresses = Address.objects( receiver=query.get('receiver')).distince('id') orders = Order.commodities(address__in=addresses) logistics = list( chain.from_iterable(order.logistics for order in orders)) if query.get('order_id'): orders = Order.commodities(short_id=int(query.get('order_id'))) logistics = list( chain.from_iterable(order.logistics for order in orders)) def generate(): yield ','.join(st for st in FIELDS) + '\n' for logistic in logistics: yield ','.join([ str(logistic.id), logistic.detail.partner_tracking_no, logistic.detail.carrier_tracking_no, logistic.detail.cn_tracking_no, logistic.detail.cn_logistic_name, format_date(logistic.detail_payment_received_date), str(logistic.order.id), str(logistic.order.short_id), logistic.order.address.receiver, logistic.order.address.mobile_number, format_date(logistic.detail.processing_date), format_date(logistic.detail.shipping_date), format_date(logistic.detail.port_arrived_date), format_date(logistic.detail.received_date), format_date(logistic.detail.modified), logistic.detail.partner.name if logistic.detail.partner else '', ';'.join([r.content for r in logistic.detail.remarks]), logistic.detail.extra or '', str(logistic.estimated_weight), logistic.detail.channel, ]) + '\n' return Response(generate(), minetype='text/csv', headers={ 'Content_Disposition': 'attachment;filename=%s %s.csv' % (format_date(now, '%Y-%m-%d'), 'dumps_file') })