hi,

is it possible to add custom form field that reference to another table?
i've tried both form, sqlform, sqlform.factory and crud it seems that it 
can't handle it or maybe there is something i missed.

my goal is to show only id in the form sale header which is refer to 
customer table and when it submitted it will also insert the invoice no 
(link to the sale detail) that is generate by uuid.

thank you so much before

the code :
*#####*
*model*
# create table : product
db.define_table('product',
    Field('product_name', label=T('Product Name')),
    Field('quantity', 'integer', label=T('Quantity')),
    Field('unit_price', 'double', label=T('Unit Price')),
    auth.signature,
    format='%(product_name)s')

# create table : customer
db.define_table('customer',
    Field('customer_name', label=T('Customer Name')),
    Field('first_name', label=T('First Name')),
    Field('last_name', label=T('Last Name')),
    Field('email', label=T('Email')),
    Field('address', label=T('Address')),
    Field('city', label=T('City')),
    Field('zip', label=T('Zip')),
    Field('phone', label=T('Phone')),
    Field('mobile_phone', label=T('Mobile Phone')),
    auth.signature,
    format='%(customer_name)s')

# create table : sale header
db.define_table('sale_header',
    Field('invoice_no', label=T('Invoice No.')),
    Field('customer_id', 'reference customer', label=T('Customer ID')),
    auth.signature),

# create table : sale detail
db.define_table('sale_detail',
    Field('invoice_no', label=T('Invoice No.')),
    Field('product_id', 'reference product', label=T('Product ID')),
    Field('quantity', 'integer', label=T('Quantity')),
    Field('unit_price', 'double', label=T('Unit Price')),
    Field('total_price', 'double', label=T('Total Price')),
    auth.signature)

*########*
*controller*

def bill():
    if not session.order:
        session.flash='Add something to order'
        redirect(URL('index'))
    import datetime
    import uuid
    invoice_no=str(uuid.uuid4())
    total_price=sum(db.product(id).unit_price*qty for id, qty in 
session.order.items())
#    form=FORM('Customer ID:', INPUT(_id='sale_header_customer_id', 
_name='customer_id'), INPUT(_type='submit'))
#    form=SQLFORM.factory(Field('customer_id', db.customer))
    form=SQLFORM(db.sale_header, fields=['customer_id'])
    if form.accepts(request,session):
        db.sale_header.insert(invoice_no=invoice_no, 
customer_id=form.vars.customer_id)
        for key, value in session.order.items():
            db.sale_detail.insert(invoice_no=invoice_no,
                                  product_id=key,
                                  quantity=value,
                                  unit_price=db.product(key).unit_price,
                                  total_price=total_price)
            db(db.product.id==key).update(quantity=db.product.quantity - 
value)
        session.order.clear()          
        session.flash='Thank you for your order'
        redirect(URL('invoice',args=invoice_no))
    return dict(order=session.order,form=form,total_price=total_price)

-- 

--- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to