I have a database of items sold, I am updating the quantity of an item by
subtracting the amount purchased from the current available Quantity. How
do i replace the old value with the difference between the current value
and the purchased value?
In the controller below i have an *itemCount *for loop (highlighted in red)
that attempts to capture the selected item and its quantity, draw from the
database the similar item and subtract its specified quantity
(session.value) from the database Quantity value then update the database
Quantity column withe new value which is the difference between Quantity
and session.value
CONTROLLER
def buy():
if not session.cart:
session.flash = 'Add something to shopping cart'
redirect(URL('index'))
invoice = session.invoiceNo
total = sum(db.product(id).price*qty for id,qty in session.cart.items())
for key, value in session.cart.items():
db.sales.insert(invoice=invoice,buyer=auth.user.id,product =
key,quantity = value,price=db.product(session.key).price)
*itemCount=db(db.product.name==key).select(db.product.ALL) for item in
itemCount: quantity=item.Quantity-value
item.update_record(Quantity=quantity)*
session.cart.clear()
session.flash = 'Sale Complete'
redirect(URL('invoice',args=invoice))
return dict(cart=session.cart,form=form,total=total)
I am hoping that the itemCount for loop will do the calculations and update
the Quantity column with the new value when the sale is being made. I
expect to find a new Quantity value when i look at the purchased items in
the database table, but that is not happening, how cn i fix this?
Mostwanted
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.