def create_from_order(): """ Given a sale order, creates a purchase and adds all the order missing items to the purchase args: [id_order] """ from bag_utils import get_ordered_items_count order = db.sale_order(request.args(0)) if not order: raise HTTP(404) missing_items = [] # check if the order items are in stock for bag_item in db(db.bag_item.id_bag == order.id_bag.id).iterselect(): quantity = item_stock_qty(bag_item.id_item, session.store) order_items_qty = get_ordered_items_count(order.id, bag_item.id_item.id) # the needed quantity will be the total amount of required items to satisfy the specified order and the previous orders. needed_qty = bag_item.quantity + order_items_qty item_ready = (quantity >= needed_qty) if not item_ready: # if the item is a bundle add the contained items to the purchase if bag_item.id_item.is_bundle: for bundle_item in db( (db.bundle_item.id_bundle == bag_item.id_item.id )).select(): qty = item_stock_qty(bundle_item.id_item, session.store) item_ready = (quantity >= needed_qty * bundle_item.quantity) if not item_ready: missing_items.append( dict(qty=(bag_item.quantity * bundle_item.quantity) - qty, item=bundle_item.id_item)) else: missing_items.append( dict(item=bag_item.id_item, qty=needed_qty - quantity)) if missing_items: new_purchase = purchase_utils.new(session.store, request.now, auth.user) for missing_item in missing_items: db.stock_item.insert(id_purchase=new_purchase, id_credit_note=None, id_inventory=None, id_store=session.store, purchase_qty=missing_item['qty'], id_item=missing_item['item'].id, base_price=missing_item['item'].base_price, price2=missing_item['item'].price2, price3=missing_item['item'].price3) else: session.info = T('No missing items') redirect(URL('sale_order', 'ready', args=order.id)) redirect(URL('fill', args=new_purchase, vars=dict(is_xml=False)))
def create(): """ vars: is_xml: if true, then the form will accept an xml file """ is_xml = request.vars.is_xml == 'True' new_purchase_id = purchase_utils.new(session.store, request.now, auth.user) if not is_xml: redirect(URL('fill', args=new_purchase_id)) else: form = SQLFORM(db.purchase, fields=['purchase_xml']) return dict(form=form)
def create_from_order(): """ Given a sale order, creates a purchase and adds all the order missing items to the purchase args: [id_order] """ from bag_utils import get_ordered_items_count order = db.sale_order(request.args(0)) if not order: raise HTTP(404) missing_items = [] # check if the order items are in stock for bag_item in db(db.bag_item.id_bag == order.id_bag.id).iterselect(): quantity = item_stock_qty(bag_item.id_item, session.store) order_items_qty = get_ordered_items_count(order.id, bag_item.id_item.id) # the needed quantity will be the total amount of required items to satisfy the specified order and the previous orders. needed_qty = bag_item.quantity + order_items_qty item_ready = (quantity >= needed_qty) if not item_ready: # if the item is a bundle add the contained items to the purchase if bag_item.id_item.is_bundle: for bundle_item in db((db.bundle_item.id_bundle == bag_item.id_item.id)).select(): qty = item_stock_qty(bundle_item.id_item, session.store) item_ready = (quantity >= needed_qty * bundle_item.quantity) if not item_ready: missing_items.append( dict( qty=(bag_item.quantity * bundle_item.quantity) - qty , item=bundle_item.id_item ) ) else: missing_items.append(dict(item=bag_item.id_item, qty=needed_qty - quantity)) if missing_items: new_purchase = purchase_utils.new(session.store, request.now, auth.user) for missing_item in missing_items: db.stock_item.insert(id_purchase=new_purchase, id_credit_note=None, id_inventory=None, id_store=session.store, purchase_qty=missing_item['qty'], id_item=missing_item['item'].id, base_price=missing_item['item'].base_price, price2=missing_item['item'].price2, price3=missing_item['item'].price3) else: session.info = T('No missing items') redirect(URL('sale_order', 'ready', args=order.id)) redirect(URL('fill', args=new_purchase, vars=dict(is_xml=False)))