Beispiel #1
0
    def valid_check(self, conn, id_order, id_invoices):
        try:
            assert id_order is not None, "Miss order in request"
            assert id_invoices is not None, "Miss invoices in request"
            id_invoices = ujson.loads(id_invoices)

            oi = get_invoice_by_order(conn, id_order)
            id_oi = [item['id'] for item in oi]

            ex_invoices = set(id_invoices) - set(id_oi)
            assert len(ex_invoices) == 0, ("invoices %s not belongs "
                                           "to order %s" %
                                           (ex_invoices, id_order))
        except (AssertionError, TypeError, ValueError), e:
            logging.error("pm_init_req_err: %s", e, exc_info=True)
            raise UserError(ErrorCode.PM_ERR_INVALID_REQ[0],
                            ErrorCode.PM_ERR_INVALID_REQ[1])
Beispiel #2
0
    def mark_order_paid(self, req, resp, conn, id_order, id_brand):
        invoices = get_invoice_by_order(conn, id_order, id_brand)
        for iv in invoices:
            values = {
                'amount_paid': iv['amount_due'],
                'status': INVOICE_STATUS.INVOICE_PAID
            }
            update_invoice(conn, iv['id'], values, iv=iv)

        conn.commit()

        # need update trans ??

        for iv in invoices:
            log_incomes(conn, iv['id'])

        try:
            send_order_email_to_service()
        except Exception, e:
            logging.error("Failed to send order email: %s", e, exc_info=True)
Beispiel #3
0
def push_order_confirmed_event(conn, id_order, id_brand, id_shops=None):
    id_user, confirmation_time = db_utils.select(conn,
                                                 "orders",
                                                 columns=("id_user",
                                                          "confirmation_time"),
                                                 where={'id': id_order})[0]
    user_info = get_user_info(conn, id_user)
    month, day = confirmation_time.strftime('%b|%d').split('|')
    params = {
        'email': user_info['fo_user_email'],
        'service_email': settings.SERVICE_EMAIL,
        'id_brand': id_brand,
        'id_order': id_order,
        'fo_user_name': user_info['fo_user_name'],
        'order_created_month': month,
        'order_created_day': day,
        'order_url': os.path.join(settings.FRONT_ROOT_URI,
                                  'orders/%s' % id_order),
    }

    from models.invoice import get_invoice_by_order
    from models.shipments import get_shipments_by_order
    shipments = get_shipments_by_order(conn, id_order)
    spm_dict = dict([(spm['id'], spm) for spm in shipments])
    invoices = get_invoice_by_order(conn, id_order)
    _xslts = []
    for iv in invoices:
        id_shipment = iv['id_shipment']
        spm = spm_dict.get(id_shipment)
        if not spm:
            continue
        if str(spm['id_brand']) != id_brand:
            continue
        if id_shops and str(spm['id_shop']) not in id_shops:
            continue
        _xslts.append(invoice_xslt(iv['invoice_xml']))

    params['invoices_xslt'] = ''.join(_xslts)
    _push_specific_event('USR_ORDER_CONFIRMED', **params)
Beispiel #4
0
    def _get_invoices(self, conn, id_order, id_brand, id_shops):
        try:
            shipments = get_shipments_by_order(conn, id_order)
            invoices = get_invoice_by_order(conn, id_order)
            spm_dict = dict([(spm['id'], spm) for spm in shipments])
            content = []

            for iv in invoices:
                id_shipment = iv['id_shipment']
                spm = spm_dict.get(id_shipment)
                if not spm:
                    continue
                if spm['id_brand'] != id_brand:
                    continue
                if id_shops and spm['id_shop'] not in id_shops:
                    continue

                content.append({
                    'id': iv['id'],
                    'id_shipment': iv['id_shipment'],
                    'html': self.invoice_xslt(iv['invoice_xml'])
                })

            order_iv_status = order_iv_sent_status(conn, id_order, id_brand,
                                                   id_shops)
            order_status = get_order_status(conn, id_order)
            to_sent_qty = iv_to_sent_qty(conn, id_order, id_brand, id_shops)
            return {
                'res': SUCCESS,
                'iv_sent_status': order_iv_status,
                'iv_to_sent_qty': to_sent_qty,
                'order_status': order_status,
                'content': content
            }
        except Exception, e:
            conn.rollback()
            logging.error("get_invoice_server_err: %s", str(e), exc_info=True)
            return {'res': FAILURE, 'reason': str(e), 'err': "SERVER_ERROR"}
Beispiel #5
0
    def _on_post(self, req, resp, conn, **kwargs):
        try:
            params = req.stream.read()
            params = ujson.loads(params)
            req._params.update(params)

            id_order = params.get('order')
            id_shipments = params.get('shipment') or '[]'
            id_brand = params.get('brand')
            id_shops = params.get('shops')

            if not id_brand or not id_shops or not id_order:
                raise ValidationError("iv_send_req_err: miss params")
            id_brand = int(id_brand)
            id_shops = [int(id_shop) for id_shop in ujson.loads(id_shops)]
            id_shipments = [
                int(id_spm) for id_spm in ujson.loads(id_shipments)
            ]
            orig_id_spms = id_shipments

            shipments = get_shipments_by_order(conn, id_order)
            shipments = dict([(sp['id'], sp) for sp in shipments])
            valid_shipments = shipments.keys()
            order = get_order(conn, id_order)

            if id_shipments:
                invalid_shipments = set(id_shipments) - set(valid_shipments)
                if len(invalid_shipments) > 0:
                    raise ValidationError("shipments %s not belongs to "
                                          "order %s" %
                                          (invalid_shipments, id_order))

                for id_spm in id_shipments:
                    spm_brand = shipments[id_spm]['id_brand']
                    spm_shop = shipments[id_spm]['id_shop']

                    if spm_brand != id_brand:
                        raise ValidationError(
                            "iv_send_req_err: spm brand %s is not "
                            "same with given %s" % (spm_brand, id_brand))
                    if spm_shop not in id_shops:
                        raise ValidationError(
                            "iv_send_req_err: spm shop %s is not "
                            "in given shops %s" % (spm_shop, id_shops))
            else:
                for id_spm, spm in shipments.iteritems():
                    if (spm['id_brand'] == id_brand
                            and spm['id_shop'] in id_shops):
                        id_shipments.append(id_spm)

            self.get_and_save_invoices(conn,
                                       req,
                                       order['id_user'],
                                       id_shipments=id_shipments)
            order_invoices = get_invoice_by_order(conn, id_order)

            invoices = dict([(oi['id_shipment'], oi) for oi in order_invoices])
            users_mail = get_user_email(conn, order['id_user'])
            if orig_id_spms:
                for id_spm in id_shipments:
                    if invoices.get(id_spm) is None:
                        continue
                    content = invoices[id_spm]['invoice_xml']
                    content_html = self.invoice_xslt(content)
                    send_html_email(users_mail, INVOICE_MAIL_SUB, content_html)
            else:
                content_list = []
                for id_spm in id_shipments:
                    if invoices.get(id_spm) is None:
                        continue
                    content = invoices[id_spm]['invoice_xml']
                    content = self.parse_invoices_content(content)
                    content_list.append(content)
                content = self.unitary_content(content_list)
                content_html = self.invoice_xslt(content)
                send_html_email(users_mail, INVOICE_MAIL_SUB, content_html)

            order_iv_status = order_iv_sent_status(conn, id_order, id_brand,
                                                   id_shops)
            return {'res': SUCCESS, 'order_iv_status': order_iv_status}
        except ValidationError, e:
            conn.rollback()
            logging.error("send_invoice_invalidate: %s", str(e), exc_info=True)
            return {'res': FAILURE, 'err': str(e)}