Hi all,
How to get the value from that record order by date descending to get the last value of my field "nextweek"?
in my table test_prod I have the following data http://i60.tinypic.com/w2nd3d.png
what I want is when I create new record I want to get the 70.00 value which is the last create_date of my prod_code and store to my begvalue field.
The idea is to get that value which is the totalending of specific product and use as the beginning value of same product
could someone have a simple working code please post.
something like this:
In create method, check the class if already any record exists order by id or not
2) if records exists, then browse the value from searched record and update it to your newly creating record.
3) if record does not exist, then you can give your own values to the initialbeginning and initialending fields.
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
from osv import osv, fields
from openerp import tools
import time
class test_product(osv.Model):
_name = "test.product"_columns = {
'prodcode_id': fields.many2one("abs.prod.desc", 'Product Code'),
'description_id': fields.many2one('abs.desc', 'Description'),
'description': fields.related('description_id', type='many2one', relation='abs.desc', string='Description',),
'price_id': fields.many2one('abs.price', 'Price', ondelete='cascade'),
'price': fields.related('price_id','price_value', type='float', string='Price', ondelete='cascade'),
'abs_weeklyperiod_id': fields.many2one('abs.weeklyperiod', 'Periods Covered', ondelete='cascade'),
'newval': fields.float('New Val', digits=(12,2)),
}
def onchange_prodcode(self, cr, uid, ids, prodcode_id, context=None):
if prodcode_id:
description_id = self.pool.get('abs.prod.desc').browse(cr, uid, prodcode_id, context).description_id.id
price_id = self.pool.get('abs.prod.desc').browse(cr, uid, prodcode_id, context).price_id.id
return {'value':{'description_id':description_id, 'price_id':price_id}}
return {}def onchange_prodcode1(self,cr,uid,ids,prodcode,context=None):
if prodcode:
test_prod_ids = self.search(cr,uid,[('prodcode','=',prodcode)],order="id desc",limit=1,context=context)
if test_prod_ids:
rd_test_prod = self.read(cr,uid,test_prod_uids[0],['nextweek'],context=conetxt)
return {'value':{'newval':rd_test_prod[0].get('nextweek')}}
return {'value':{}}class descriptionproduct(osv.osv):
_description="Description Product"
_name = 'abs.prod.desc'
_columns = {
'name': fields.char('Code', size=64, required=True,),
'description_id': fields.many2one('abs.desc', 'Description', required=True),
'price_id': fields.many2one('abs.price', 'Price', required=True),
}
class description(osv.osv):
_name = 'abs.desc'
_description = 'Description'
_columns = {
'name': fields.char('Description Name', size=64, required=True,),
}class productprice(osv.osv):
_description="Product Price"
_name = 'abs.prod.price'
_columns = {
'price_id': fields.many2one('abs.price', 'Price', required=True),
'price_value': fields.float('Price', digits=(12,2)),
}class price(osv.osv):
_name = 'abs.price'
_description = 'Price'
_columns = {
'name': fields.float('Price', digits=(12,2)),
}class abs_weeklyperiod(osv.Model):
_name = "abs.weeklyperiod"_columns = {
'start_date': fields.date('Start Date'),
'end_date': fields.date('End Date'),
'name': fields.char('Periods', size=32, required=True),
'initialbeginning': fields.integer('Initial Beginning', size=32, ondelete='cascade'),
'initialending': fields.integer('Initial Ending', size=32, ondelete='cascade'),
}_defaults = {
'start_date': lambda *a: time.strftime('%Y-%m-%d'),
'end_date': lambda *a: (datetime.today() + relativedelta(days=6)).strftime('%Y-%m-%d'),
}
Very Thanks