Exemple #1
0
    def to_json(self,
                include_logistic=False,
                replace_entries_to_refunded=False):
        if not self.is_paid:
            self.update_amount()
            self.reload()

        entries_json = []
        if replace_entries_to_refunded and self.has_refund_entries():
            for e in self.refund_entries:
                entries_json.append(e.to_json())
        else:
            for e in self.entries:
                entries_json.append(e.to_json())

        refund_entries_json = []
        for e in self.refund_entries:
            refund_entries_json.append(e.to_json())

        result = dict(
            id=str(self.id),
            short_id=str(self.sid),
            status=self.status,
            customer_id=str(self.customer_id),
            amount=self.amount,
            cn_shipping=self.cn_shipping,
            coin=self.coin,
            hongbao=self.hongbao,
            discount=self.discount,
            final=self.final,
            estimated_tax=self.estimated_tax,
            payment_status='PAID' if self.is_paid else 'UNPAID',
            payment_ref_number=[
                p.ref_number for p in Payment.objects(order=self)
            ],
            created_at=format_date(self.created_at),
            entries=entries_json,
            refund_entries=refund_entries_json,
            refund_amount=self.refund_amount,
            real_tax=self.real_tax,
        )

        if self.address:
            result.update({"address": self.address.to_json()})

        if include_logistic:
            result.update(dict(logistics=[l.to_json()
                                          for l in self.logistics]))

        return result
Exemple #2
0
 def shipping_history(self):
     res = []
     status = self.detail.status
     for st in LOG_STATS:
         field = self.detail.attr_by_log_stat[st]
         val = getattr(self.detail, field)
         if not val:
             continue
         res.append({'time': format_date(val),
                     'desc': SHIPPING_HISTORY[st],
                     'status': st})
         if st == status:
             break
     return res
Exemple #3
0
def get_board(board_id):
    board = Models.Board.get_board(board_id)
    if not board:
        return jsonify(message='Failed')

    items = Models.Item.objects(web_id__in=board.items)

    board.view_count += 1
    board.save()

    return jsonify(message='OK',
                   board=dict(
                       date=format_date(board.published_at),
                       image=board.image,
                       desc=board.description,
                       title=board.title,
                       items=[Json.item_json_in_list(item) for item in items],
                   ))
Exemple #4
0
def simple_order_json(order):
    if not order.is_paid:
        order.update_amount()
        order.reload()

    entries_json = []
    for e in order.entries:
        if order.order_type == ORDER_TYPE.TRANSFER:
            entries_json.append(transfer_entry_json(e))
        else:
            entries_json.append(entry_json(e))

    refund_entries_json = []
    for e in order.refund_entries:
        refund_entries_json.append(e.to_json())

    result = dict(
        id=str(order.id),
        short_id=str(order.sid),
        current_status=ORDER_STATUS_DESC.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,
        hongbao=order.hongbao,
        discount=order.discount,
        final=order.final,
        payment_status='PAID' if order.is_paid else 'UNPAID',
        payment_ref_number=[
            p.ref_number for p in Models.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
Exemple #5
0
def transfer_order_price_json(order):

    entries_json = []
    for e in order.entries:
        entries_json.append(transfer_entry_json(e))

    provider_json = Models.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,
                  hongbao=order.hongbao,
                  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(l, order.order_type) for l in order.logistics
            ]))

    return result
Exemple #6
0
def logistic_json(logistic, order_type):

    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': st,
        'desc': ORDER_STATUS_DESC.get(st, '')
    } for st in ROUTES.get(logistic.detail.route, 'DEFAULT')]
    current_status = logistic.detail.status

    # Combined history
    history = []
    for st in LOG_STATS:

        dt_field = logistic.detail.attr_by_log_stat[st]
        val = getattr(logistic.detail, dt_field)
        if not val:
            continue
        history.append(
            dict(
                desc=SHIPPING_HISTORY[st],
                time=(format_date(val) if val else ''),
            ))
        if st == 'TRANSFER_APPROVED':
            history.append(
                dict(
                    desc=u'美比仓库地址:广东省深圳市 南山区 粤海路四达大厦B座19B \t邮编:518000',
                    time=u'收货人:唐耀星 \t手机号码:13822327121',
                ))
        if st == 'WAREHOUSE_IN' and logistic.detail.real_weight > 0:
            history.append(
                dict(
                    desc=u'称得包裹总重量:%skg' %
                    str(logistic.detail.real_weight / 1000),
                    time='',
                ))

        # tracking information
        tracking = None
        if st == LOG_STATS.SHIPPING and logistic.detail.cn_logistic_name:
            history.append(
                dict(
                    desc=u'国际快递公司: %s' %
                    logistic.detail.cn_logistic_name.upper(),
                    time=u'国际快递单号: %s' % logistic.detail.cn_tracking_no,
                ))
            tracking = Models.ExpressTracking.find(
                company=logistic.detail.cn_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 st == 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,
    }
Exemple #7
0
class MBModelView(PermissionModelView):
    column_type_formatters = {datetime.datetime:
                              lambda view, value: format_date(value)}
Exemple #8
0
    def download(self):
        FIELDS = [
            u"包裹ID", u'IMG No', u'CTN', u"下单日期", u"订单ID", u'订单短号', u'收件人',
            u'手机号', u'合作物流商', u'remark', u"下单备注", u"估重", u"渠道"
        ]

        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:
            los = Models.Logistic.objects(is_closed=False, **query)
            if status:
                los = los.order_by(
                    'detail__%s' %
                    Models.LogisticDetail.attr_by_log_stat[status])
        except:
            pass

        if delay_export:

            date_field = Models.LogisticDetail.attr_by_log_stat[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,
            }
            los = los.filter(**query).order_by('detail__%s' % date_field)
            if delay_type:
                los = los.filter(
                    detail__delay_details__reason__contains=delay_type)

        if query.get('receiver'):
            addrs = Models.Address.objects(
                receiver=query.get('receiver')).distinct('id')
            orders = Models.Order.commodities(address__in=addrs)
            los = list(chain.from_iterable(order.logistics
                                           for order in orders))

        if query.get('order_id'):
            orders = Models.Order.commodities(
                short_id=int(query.get('order_id')))
            los = list(chain.from_iterable(order.logistics
                                           for order in orders))

        def generate():
            yield ','.join(st for st in FIELDS) + '\n'
            for log in los:
                yield ','.join([
                    str(log.id),
                    log.detail.partner_tracking_no,
                    log.detail.carrier_tracking_no,
                    log.detail.cn_tracking_no,
                    log.detail.cn_logistic_name,
                    format_date(log.detail.payment_received_date),
                    str(log.order.id),
                    str(log.order.short_id),
                    log.order.address.receiver,
                    log.order.address.mobile_number,
                    format_date(log.detail.processing_date),
                    format_date(log.detail.shipping_date),
                    format_date(log.detail.port_arrived_date),
                    format_date(log.detail.received_date),
                    format_date(log.detail.modified),
                    log.detail.partner.name if log.detail.partner else '',
                    '; '.join([r.content for r in log.detail.remarks]),
                    log.detail.extra or '',
                    str(log.estimated_weight),
                    log.detail.channel,
                ]) + '\n'

        return Response(generate(),
                        mimetype="text/csv",
                        headers={
                            "Content-Disposition":
                            "attachment;filename=%s %s.csv" %
                            (format_date(now, '%Y-%m-%d'), 'dumps_file')
                        })