lundi 25 avril 2016

Creation of trigger in web2py using sqlite databse

I'm developing a project in DatabaseManagement using web2py. I have stock,item and customer tables as follows

Item:

db = DAL("sqlite://storage.sqlite")
db.define_table('item',
Field('name', notnull = True,unique=True),
Field('price'),
Field('quantity'),
format = '%(name)s')

Customer:

db.define_table('customer',
Field('item_name','reference item'),
Field('name', notnull = True,requires=IS_NOT_EMPTY()),
Field('unit_price','double',requires=IS_NOT_EMPTY()),
Field('email',requires = IS_EMAIL(error_message='invalid    email!'),unique='True'),
 Field('adress',requires=IS_NOT_EMPTY()),
 Field('city',requires=IS_NOT_EMPTY()),
 Field('quantity','integer',requires=IS_NOT_EMPTY()),
 Field('state',default='KARNATAKA'),
 Field('contact_number',requires=IS_LENGTH(10,10)),
 Field('total_price',
 compute=lambda r: r['unit_price'] * r['quantity']),
 format='%(name)s')

 db.item.name.requires = IS_NOT_IN_DB(db, db.item.name)
 db.customer.item_name.requires = IS_IN_DB(db, db.item.id, '%(name)s')

Stock:

db.define_table('stock',
Field('Date','datetime'),
Field('particulars'),
Field('opening_stock','double'),
Field('purchases','double'),
Field('total_price',
compute=lambda r: r['opening_stock'] + r['purchases']),
Field('sales','double'),
Field('closing_stock',
compute=lambda r: r['total_price'] - r['sales']))

Stock table maintains the particulars i.e item name and opening stock i.e number of stock available of that particulars,closing stock i.e stock available at the end of the day. Now when customer purchases item it should automatically deduct in stock table but according to my tables mentioned i'm manually entering the sales data and by this it gonna calculate closing stock. But i wanted to write trigger for that when customer purchases it should deduct in stock table means sales attribute value should gonna increase. How could i do using web2py? What is the trigger syntax and function call for trigger in default.py? Please let me know the soultion...

Aucun commentaire:

Enregistrer un commentaire