Example #1
0
def _add_shipping_fee(conn, id_shipment, handling_fee, shipping_fee):
    values = {
        "id_shipment": id_shipment,
        'handling_fee': to_round(handling_fee),
        'shipping_fee': to_round(shipping_fee)
    }
    id = insert(conn, 'shipping_fee', values=values, returning='id')
    logging.info('shipment fee added: id: %s, values: %s', id[0], values)
Example #2
0
def update_shipping_fee(conn, id_shipment, values):
    where = {'id_shipment': id_shipment}
    if 'handling_fee' in values:
        values['handling_fee'] = to_round(values['handling_fee'])
    if 'shipping_fee' in values:
        values['shipping_fee'] = to_round(values['shipping_fee'])
    r = update(conn,
               "shipping_fee",
               values=values,
               where=where,
               returning="id")
    return r and r[0] or None
Example #3
0
def update_shipping_fee(conn, id_shipment, id_postage, shipping_fee):
    try:
        shipping_fee = to_round(shipping_fee)
        update(conn,
               'shipping_supported_services',
               values={'id_postage': id_postage},
               where={'id_shipment': id_shipment})
        update(conn,
               'shipping_fee',
               values={'shipping_fee': shipping_fee},
               where={'id_shipment': id_shipment})
        conn.commit()
        logging.info('update_shipping_fee:'
                     'id_postage: %s, '
                     'shipping_fee: %s, '
                     'for shipment: %s,' %
                     (id_postage, shipping_fee, id_shipment))
    except Exception, e:
        conn.rollback()
        logging.error('update_shipping_fee err: %s, '
                      'args: id_shipment: %s,'
                      'id_postage: %s, '
                      'shipping_fee: %s' %
                      (e, id_shipment, id_postage, shipping_fee),
                      exc_info=True)
        raise
Example #4
0
def _store_credit_redeemed(conn, id_coupon, id_order, id_invoice, amount):
    results = db_utils.select(conn,
                              'store_credit_redeemed',
                              where={
                                  'id_coupon': id_coupon,
                                  'id_order': id_order,
                              },
                              order=('-id_invoice', ))
    if not results or results[0]['id_invoice']:
        return

    update_values = {
        'id_invoice': id_invoice,
        'order_status': ORDER_STATUS_FOR_COUPON.PAID,
    }
    total = to_round(results[0]['redeemed_amount'])
    if total <= amount:
        db_utils.update(conn,
                        'store_credit_redeemed',
                        values=update_values,
                        where={'id': results[0]['id']})
    else:
        values = {
            'id_coupon': results[0]['id_coupon'],
            'id_user': results[0]['id_user'],
            'id_order': results[0]['id_order'],
            'currency': results[0]['currency'],
            'redeemed_amount': amount,
        }
        values.update(update_values)
        db_utils.insert(conn, 'store_credit_redeemed', values=values)

        db_utils.update(conn,
                        'store_credit_redeemed',
                        values={'redeemed_amount': to_round(total - amount)},
                        where={'id': results[0]['id']})

    # check if need to mark redeemed_in_full
    credit_value = _get_store_credit(conn, id_coupon)
    redeemed_amount = _get_store_redeemed_amount(conn, id_coupon)
    if credit_value and credit_value['amount'] <= to_round(redeemed_amount):
        db_utils.update(conn,
                        'store_credit',
                        values={'redeemed_in_full': True},
                        where={'id_coupon': id_coupon})
Example #5
0
    def testSameProvince(self):
        # update user address to US AK
        self.b.update_account_address(self.users_id, "US", "AL", "ALABAMA")

        qty = 2
        item = {
            'id_sale': 1000031,
            'id_variant': 1000012,
            'quantity': qty,
            'id_shop': 1000002,
            'id_type': 1000003,
            'id_price_type': 1000003,
            'id_weight_type': 1000003
        }

        # conf data in backoffice
        type_attr_price = 3 * (1 + 0.02)  # + premium
        weight = 2
        shipping_fee = 2.0 * weight * qty
        handling_fee = 6.0
        tax1 = 1 / 100.0
        tax2 = 0.5 / 100.0
        tax3 = 1 / 100.0  # taxable
        expect_amount = (type_attr_price * qty + handling_fee + shipping_fee +
                         (to_round(type_attr_price *
                                   (1 + tax1)) - type_attr_price) * qty +
                         (to_round(type_attr_price *
                                   (1 + tax2)) - type_attr_price) * qty +
                         (to_round(type_attr_price *
                                   (1 + tax3)) - type_attr_price) * qty +
                         to_round((handling_fee + shipping_fee) *
                                  (1 + tax3)) - (handling_fee + shipping_fee))

        wwwOrder = [item]
        id_order = self.success_wwwOrder(self.telephone, self.shipaddr,
                                         self.billaddr, wwwOrder)
        id_shp = self._shipmentsCountCheck(id_order, 1)[0]
        self._shipping_conf(id_shp)

        self.b.post_invoices(id_order)
        self.expect_one_item_result(id_order, expect_amount)
Example #6
0
def create_invoice(conn, id_order, id_shipment, amount_due,
                   currency,
                   invoice_xml, invoice_number,
                   due_within=None,
                   shipping_within=None,
                   amount_paid=None,
                   invoice_file=None,
                   invoice_items=None,
                   status=INVOICE_STATUS.INVOICE_OPEN):
    values = {
        'id_order': id_order,
        'id_shipment': id_shipment,
        'creation_time': datetime.now(),
        'update_time': datetime.now(),
        'amount_due': to_round(amount_due),
        'amount_paid': to_round(amount_paid or 0),
        'currency': currency,
        'invoice_xml': invoice_xml,
        'invoice_number': invoice_number,
        'status': status
        }

    if due_within:
        values['due_within'] = due_within
    if shipping_within:
        values['shipping_within'] = shipping_within
    if invoice_items:
        values['invoice_items'] = invoice_items

    if invoice_file:
        values['invoice_file'] = invoice_file

    invoice_id = insert(conn, 'invoices', values=values, returning='id')
    logging.info('invoice created: id: %s, values: %s',
                 invoice_id[0], values)

    seller = get_seller(conn, id_shipment)

    from models.order import up_order_log
    up_order_log(conn, id_order, seller)
    return invoice_id[0]
Example #7
0
    def testDiffCountry(self):

        # update user address to China BJ
        self.b.update_account_address(self.users_id, "CN", "BJ", "BeiJing")

        qty = 2
        item = {
            'id_sale': 1000031,
            'id_variant': 1000012,
            'quantity': qty,
            'id_shop': 1000002,
            'id_type': 1000003,
            'id_price_type': 1000003,
            'id_weight_type': 1000003
        }

        # conf data in backtoshops
        type_attr_price = 3 * (1 + 0.02)  # + premium
        weight = 2
        shipping_fee = 2.0 * weight * qty
        handling_fee = 6.0
        tax1 = 3 / 100.0
        tax2 = 4 / 100.0
        expect_amount = (type_attr_price * qty + handling_fee + shipping_fee +
                         (to_round(type_attr_price *
                                   (1 + tax1)) - type_attr_price) * qty +
                         (to_round(type_attr_price *
                                   (1 + tax2)) - type_attr_price) * qty)

        wwwOrder = [item]
        id_order = self.success_wwwOrder(self.telephone, self.shipaddr,
                                         self.billaddr, wwwOrder)
        id_shp = self._shipmentsCountCheck(id_order, 1)[0]
        self._shipping_conf(id_shp)

        self.b.post_invoices(id_order)
        self.expect_one_item_result(id_order, expect_amount)
Example #8
0
def format_amount(amount, decimal_digits=2):
    try:
        result = ('%%.%sf' % decimal_digits) % to_round(amount, decimal_digits)
        if decimal_digits > 2:
            count = decimal_digits - 2
            while count > 0:
                if result[-1] == '0':
                    result = result[:-1]
                    count -= 1
                    continue
                else:
                    break
        return result
    except:
        return amount
Example #9
0
def redeem_coupon_for_shipment(conn, id_order, id_shipment, id_invoice):
    shipping_list = get_shipping_list(conn, id_shipment)
    coupon_items = [
        item for item in shipping_list if item['modified_by_coupon']
    ]
    items_group_by_coupon = {}
    for item in shipping_list:
        if not item['modified_by_coupon'] or item['price'] > 0:
            continue
        if item['modified_by_coupon'] not in items_group_by_coupon:
            items_group_by_coupon[item['modified_by_coupon']] = []
        items_group_by_coupon[item['modified_by_coupon']].append(item)

    for id_coupon, items in items_group_by_coupon.iteritems():
        _coupon_redeemed(conn, id_coupon, id_order, id_invoice)
        amount = to_round(
            sum([item['quantity'] * item['price'] for item in items]))
        _store_credit_redeemed(conn, id_coupon, id_order, id_invoice, amount)
Example #10
0
def cancel_coupon_for_order(conn, id_order):
    db_utils.update(conn,
                    'coupon_redeemed',
                    values={'order_status': ORDER_STATUS_FOR_COUPON.CANCELLED},
                    where={'id_order': id_order})
    db_utils.update(conn,
                    'store_credit_redeemed',
                    values={'order_status': ORDER_STATUS_FOR_COUPON.CANCELLED},
                    where={'id_order': id_order})

    id_coupons = [
        item['modified_by_coupon'] for item in get_order_items(conn, id_order)
        if item['modified_by_coupon']
    ]
    for id_coupon in id_coupons:
        # check if need to mark redeemed_in_full False
        credit_value = _get_store_credit(conn, id_coupon)
        redeemed_amount = _get_store_redeemed_amount(conn, id_coupon)
        if credit_value and credit_value['amount'] > to_round(redeemed_amount):
            db_utils.update(conn,
                            'store_credit',
                            values={'redeemed_in_full': False},
                            where={'id_coupon': id_coupon})
Example #11
0
def _calc_discount(price, discount):
    return to_round(price * discount * 0.01)
Example #12
0
def _calc_currency_credit(conn, id_coupon, coupon, id_order, id_user,
                          match_order_items):
    credit_value = db_utils.select_dict(conn,
                                        'store_credit',
                                        'id_coupon',
                                        where={
                                            'id_coupon': id_coupon
                                        }).values()[0]
    if credit_value['redeemed_in_full']:
        raise ValidationError('COUPON_ERR_REDEEMED_FULL')

    redeemed_amount = db_utils.query(
        conn,
        "select COALESCE(sum(redeemed_amount), 0) from store_credit_redeemed "
        "where id_coupon=%s and order_status in (%s, %s) ", [
            id_coupon, ORDER_STATUS_FOR_COUPON.PENDING,
            ORDER_STATUS_FOR_COUPON.PAID
        ])[0][0]
    left_amount = credit_value['amount'] - redeemed_amount
    total_redeemable_amount = 0
    match_id_items = [item['item_id'] for item in match_order_items]

    shipments = get_shipments_by_order(conn, id_order)
    for shipment in shipments:
        if left_amount <= 0:
            continue
        shipping_list = get_shipping_list(conn, shipment['id'])
        price = sum([
            o['price'] * o['quantity'] for o in shipping_list
            if o['id_item'] in match_id_items
            and o['currency'] == credit_value['currency']
        ])
        redeemed_amount = abs(
            sum([
                o['price'] * o['quantity'] for o in shipping_list if
                o['id_sale'] == 0 and o['currency'] == credit_value['currency']
            ]))

        taxes = get_apply_before_coupons_taxes(conn, id_user,
                                               shipment['id_shop'],
                                               shipping_list[0]['id_sale'])
        price_with_tax = price + sum(
            [(price + redeemed_amount) * float(tax['rate']) / 100.0
             for tax in taxes])
        redeemable_amount = min(price_with_tax, left_amount)

        fake_item = {
            'id_sale':
            0,
            'id_variant':
            0,
            'id_brand':
            match_order_items[0]['brand_id'],
            'id_shop':
            match_order_items[0]['shop_id'],
            'name':
            '',
            'price':
            -min(price, left_amount),
            'currency':
            credit_value['currency'],
            'description':
            coupon['description'],
            'weight':
            0,
            'weight_unit':
            match_order_items[0]['weight_unit'],
            'barcode':
            '',
            'external_id':
            '',
            'item_detail':
            ujson.dumps({
                'name': '',
                'redeemable_credits': redeemable_amount,
            }),
            'type_name':
            '',
            'modified_by_coupon':
            id_coupon,
        }
        _create_fake_order_item(conn, id_order, match_id_items[0], fake_item)
        left_amount -= redeemable_amount
        total_redeemable_amount += redeemable_amount

    return to_round(total_redeemable_amount), credit_value['currency']