Пример #1
0
    def _get_shipping_list(self, conn, id_shipment):
        r = []

        shipping_list = get_shipping_list(conn, id_shipment)
        for shipping in shipping_list:
            sel_variant = shipping['sel_variant']
            sel_weight_type = shipping['sel_weight_type']
            if sel_variant:
                sel_variant = ujson.loads(sel_variant)
            if sel_weight_type:
                sel_weight_type = ujson.loads(sel_weight_type)

            cur_packed = order_item_packing_quantity(conn, shipping['id_item'])
            item_quantity = order_item_quantity(conn, shipping['id_item'])
            remaining = item_quantity - cur_packed
            item = self._gen_shipping_sale_item(
                shipping['item_detail'], shipping['weight'],
                shipping['weight_unit'], sel_variant, sel_weight_type,
                shipping['external_id'], shipping['currency'], item_quantity,
                shipping['packing_quantity'], remaining)

            shipping['sale_item'] = item
            r.append(shipping)

        return r
Пример #2
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)
Пример #3
0
    def _get_weight_and_address(self):
        shipping_list = get_shipping_list(self.conn, self.id_shipment)
        spm_weight = 0
        spm_weight_with_free = 0
        id_address = None

        total_amount = 0
        total_amount_with_free = 0
        for shipping in shipping_list:
            if (shipping['free_shipping'] and not self.with_free_shipping_fee):
                continue
            id_sale = shipping['id_sale']
            sale = CachedSale(id_sale).sale
            weight = sale.standard_weight or None
            id_weight_type = shipping['id_weight_type']
            quantity = shipping['quantity']

            if weight is None:
                sel_weight_type = sale.get_weight_attr(id_weight_type)
                weight = sel_weight_type.weight.value

            weight = weight_convert(sale.weight_unit, float(weight)) * quantity
            if not shipping['free_shipping']:
                spm_weight += weight
            if self.with_free_shipping_fee:
                spm_weight_with_free += weight

            # All shipping list have same orig address
            if id_address is None:
                id_address = self._get_sale_address(sale, shipping['id_shop'])

            amount = sale.final_price(shipping['id_variant'],
                                      shipping['id_price_type'])
            if not shipping['free_shipping']:
                total_amount += amount * quantity
            if self.with_free_shipping_fee:
                total_amount_with_free += amount * quantity

        return spm_weight, spm_weight_with_free, \
               total_amount, total_amount_with_free, id_address
Пример #4
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']
Пример #5
0
    def shipment_content(self, conn, id_shipment):
        shipping_list = get_shipping_list(conn, id_shipment)

        normal_items = []
        discount_lines = {}

        def _item_key(item):
            return ujson.dumps({
                'id_sale': item['id_sale'],
                "id_variant": item["id_variant"],
                "id_price_type": item["id_price_type"],
                "id_type": item["id_type"],
            })

        for shipping in shipping_list:
            promo = shipping["modified_by_coupon"] is not None
            promo_type = None
            manufacturer_promo = False
            if promo:
                coupons = db_utils.select_dict(
                    conn,
                    'coupons',
                    'id',
                    where={
                        'id': shipping['modified_by_coupon']
                    }).values()
                if len(coupons) > 0:
                    promo_type = COUPON_REWARD_TYPE.toReverseDict()[
                        coupons[0]['coupon_type']]
                    manufacturer_promo = coupons[0]['manufacturer']
            detail = ujson.loads(shipping['item_detail'])
            item = {
                "id_item": shipping["id_item"],
                "id_sale": shipping["id_sale"],
                "id_variant": shipping["id_variant"],
                "quantity": shipping["quantity"],
                "id_price_type": shipping["id_price_type"],
                "id_type": shipping["id_type"],
                "name": shipping["name"] or '',
                "type_name": shipping["type_name"] or '',
                "external_id": shipping["external_id"] or '',
                'promo': promo,
                'promo_type': promo_type,
                'manufacturer_promo': manufacturer_promo,
                'price': shipping['price'],
                'desc': shipping["description"],
                'redeemable_credits': detail.get('redeemable_credits'),
            }
            if shipping['id_sale'] and shipping['price'] < 0:
                if _item_key(item) not in discount_lines:
                    discount_lines[_item_key(item)] = []
                discount_lines[_item_key(item)].append(item)
            else:
                normal_items.append(item)

        content = []
        for item in normal_items:
            content.append(item)
            if _item_key(item) in discount_lines:
                content += discount_lines.pop(_item_key(item))
        for items in discount_lines.values():
            content += items
        return ujson.dumps(content)