コード例 #1
0
ファイル: bag_utils.py プロジェクト: tazjel/ctrlpyme4
def add_bag_item(bag, item, quantity=None, sale_price=None):
    """ """

    db = current.db

    sale_price = sale_price if sale_price else item.base_price

    bag_item = db((db.bag_item.id_item == item.id)
                  & (db.bag_item.id_bag == bag.id)).select().first()

    # when quantity is not specified, avoid stock checking (the API user knows what he is doing)
    if not quantity:
        stock_qty = item_utils.item_stock_qty(item,
                                              bag.id_store,
                                              id_bag=bag.id)
        if item.has_inventory:
            stock_qty = DQ(stock_qty)

        base_qty = base_qty = 1 if stock_qty >= 1 or ALLOW_OUT_OF_STOCK else stock_qty % 1  # modulo to consider fractionary items
        # if there is no stock notify the user
        if base_qty <= 0:
            raise CP_OutOfStockError()
        quantity = base_qty

    # create item taxes string, the string contains the tax name and its percentage, see db.py > bag_item table for more info
    if not bag_item:
        item_taxes_str = ''
        for tax in item.taxes:
            item_taxes_str += '%s:%s' % (tax.name, tax.percentage)
            if tax != item.taxes[-1]:
                item_taxes_str += ','
        discounts = item_utils.item_discounts(item)
        sale_price = item_utils.discount_data(discounts, sale_price)[0]
        discount = item.base_price - sale_price
        id_bag_item = db.bag_item.insert(id_bag=bag.id,
                                         id_item=item.id,
                                         quantity=quantity,
                                         sale_price=sale_price,
                                         discount=discount,
                                         product_name=item.name,
                                         item_taxes=item_taxes_str,
                                         sale_taxes=item_utils.item_taxes(
                                             item, sale_price))
        bag_item = db.bag_item(id_bag_item)
    else:
        bag_item.quantity += base_qty
        bag_item.update_record()

    return bag_item
コード例 #2
0
def add_bag_item(bag, item, quantity=None, sale_price=None):
    """ """

    db = current.db

    sale_price = sale_price if sale_price else item.base_price

    bag_item = db(
          (db.bag_item.id_item == item.id)
        & (db.bag_item.id_bag == bag.id)
    ).select().first()

    # when quantity is not specified, avoid stock checking (the API user knows what he is doing)
    if not quantity:
        stock_qty = item_utils.item_stock_qty(item, bag.id_store, id_bag=bag.id)
        if item.has_inventory:
            stock_qty = DQ(stock_qty)

        base_qty = base_qty = 1 if stock_qty >= 1 or ALLOW_OUT_OF_STOCK else stock_qty % 1 # modulo to consider fractionary items
        # if there is no stock notify the user
        if base_qty <= 0:
            raise CP_OutOfStockError()
        quantity = base_qty

    # create item taxes string, the string contains the tax name and its percentage, see db.py > bag_item table for more info
    if not bag_item:
        item_taxes_str = ''
        for tax in item.taxes:
            item_taxes_str += '%s:%s' % (tax.name, tax.percentage)
            if tax != item.taxes[-1]:
                item_taxes_str += ','
        discounts = item_utils.item_discounts(item)
        sale_price = item_utils.discount_data(discounts, sale_price)[0]
        discount = item.base_price - sale_price
        id_bag_item = db.bag_item.insert(
            id_bag=bag.id, id_item=item.id, quantity=quantity,
            sale_price=sale_price, discount=discount,
            product_name=item.name, item_taxes=item_taxes_str,
            sale_taxes=item_utils.item_taxes(item, sale_price),
            reward_points=item.reward_points or 0
        )
        bag_item = db.bag_item(id_bag_item)
    else:
        bag_item.quantity += base_qty
        bag_item.update_record()

    return bag_item
コード例 #3
0
def new(bag, id_store, now, user):
    """ """
    db = current.db

    bag_items = db(db.bag_item.id_bag == bag.id).iterselect()
    #TODO check discounts coherence
    for bag_item in bag_items:
        discounts = item_utils.item_discounts(bag_item.id_item)
        original_price = bag_item.sale_price + bag_item.discount

        # this is the discount coherence check, for now it does nothing
        discounted_price = item_utils.apply_discount(discounts, original_price)
        if bag_item.sale_price == discounted_price:
            pass

    # bag was created by a client
    id_store = bag.id_store.id if bag.id_store else id_store
    id_client = bag.created_by.id if bag.created_by.is_client else None

    new_sale_id = db.sale.insert(id_bag=bag.id,
                                 subtotal=bag.subtotal,
                                 taxes=bag.taxes,
                                 total=bag.total,
                                 quantity=bag.quantity,
                                 reward_points=bag.reward_points,
                                 id_store=id_store,
                                 id_client=id_client,
                                 created_by=user.id,
                                 created_on=now,
                                 modified_on=now)

    bag.created_by = user.id
    bag.id_store = id_store
    bag.is_sold = True
    bag.status = BAG_COMPLETE
    bag.update_record()

    if bag.is_paid:
        stripe_payment_opt = db(
            db.payment_opt.name == 'stripe').select().first()
        db.payment.insert(id_payment_opt=stripe_payment_opt.id,
                          id_sale=new_sale,
                          amount=bag.total,
                          stripe_charge_id=bag.stripe_charge_id,
                          is_updatable=False)

    return new_sale_id
コード例 #4
0
def new(bag, id_store, now, user):
    """ """
    db = current.db

    bag_items = db(db.bag_item.id_bag == bag.id).iterselect()
    #TODO check discounts coherence
    for bag_item in bag_items:
        discounts = item_utils.item_discounts(bag_item.id_item)
        original_price = bag_item.sale_price + bag_item.discount

        # this is the discount coherence check, for now it does nothing
        discounted_price = item_utils.apply_discount(discounts, original_price)
        if bag_item.sale_price == discounted_price:
            pass

    # bag was created by a client
    id_store = bag.id_store.id if bag.id_store else id_store
    id_client = bag.created_by.id if bag.created_by.is_client else None

    new_sale_id = db.sale.insert(
        id_bag=bag.id, subtotal=bag.subtotal, taxes=bag.taxes,
        total=bag.total, quantity=bag.quantity,
        reward_points=bag.reward_points, id_store=id_store, id_client=id_client,
        created_by=user.id, created_on=now, modified_on=now
    )

    bag.created_by = user.id
    bag.id_store = id_store
    bag.is_sold = True
    bag.status = BAG_COMPLETE
    bag.update_record()

    if bag.is_paid:
        stripe_payment_opt = db(
            db.payment_opt.name == 'stripe'
        ).select().first()
        db.payment.insert(
            id_payment_opt=stripe_payment_opt.id, id_sale=new_sale,
            amount=bag.total, stripe_charge_id=bag.stripe_charge_id,
            is_updatable=False
        )

    return new_sale_id