Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to get value from function field and use it in calculation ?

By
Dr Obx
on 12/22/15, 7:38 AM 365 views

Hello,

'nod' is a function field, I need value of this field to use in calculation. How to do it ?

ex:

   'nod': fields.function(_days_between, type='integer', string='NoD'),# counting days between dates

...      mwosres = (rec[1] / 'value from nod') ...


0
Dr Obx
On 1/4/16, 3:58 AM

Thank you Axel, everything works fine ;)

Happy new year :)

0

Axel Mendoza

--Axel Mendoza--
10318
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

Axel Mendoza
On 12/22/15, 3:02 PM

Use it as a normal field, you need to have a browse record of the model record that contains the 2 dates and you may use it as:

model_id = self.browse(idx)
mwosres = (rec[1] / model_id.nod)

Hi Axel, actually this field just contain a result of the definition which calculates number of days ;)

class stock_check(osv.osv):
	_name = 'stock.check'

	def _days_between(self, cr, uid, ids, field_name, arg, context=None):
		res = {}
		for cur_data in self.browse(cr, uid, ids, context=context):
			st_date = datetime.strptime(cur_data.st_date, "%Y-%m-%d")
			en_date = datetime.strptime(cur_data.en_date, "%Y-%m-%d")
			res[cur_data.id] = abs((en_date - st_date).days)
		return res

	def _weeks_between(self, cr, uid, ids, field_name, arg, context=None):
		res = {}
		for cur_data in self.browse(cr, uid, ids, context=context):
			st_date = datetime.strptime(cur_data.st_date, "%Y-%m-%d")
			en_date = datetime.strptime(cur_data.en_date, "%Y-%m-%d")
			week_day1 = (st_date - timedelta(days=st_date.weekday()))
			week_day2 = (en_date - timedelta(days=en_date.weekday()))
			res[cur_data.id] = (week_day2 - week_day1).days / 7
		return res

	_columns = {
			'product': fields.one2many('product.data','check_id', string="Product"),
			'location_id': fields.many2one('stock.location','location'),
			'state': fields.boolean('checked'),
			'st_date': fields.date('Start date'),
			'en_date': fields.date('End date'),
			'days': fields.function(_days_between, type='char', string='NoD'),
			'weeks': fields.function(_weeks_between, type='char', string='NoW'),
			'months': fields.function(_weeks_between, type='char', string='NoM')
			}

	def _get_def_loc(self, cr, uid, context=None):
		dest_loc = self.pool.get('stock.location').search(cr, uid, [('usage', '=', 'production')], context=context)
		dest_loc_obj = self.pool.get('stock.location').browse(cr, uid, dest_loc[0])
		return dest_loc_obj.id

	_defaults = {
		'location_id': _get_def_loc,
		}			
	def moves_get(self, cr, uid, ids, context=None):
		a = 0
		res=[]
		total = 0
		lsrv = 0
		hsrv = 0
		asrv = 0
		mwosres = 0
		tot_qty = 0
		product_id = 0
		product_qty = 0

		if context.has_key('prod_id'):
			product_id = context.get('prod_id', 0)

		for obj in self.browse(cr, uid, ids):
			cr.execute(""" select product_id,sum(qty) from stock_quant where in_date ::date>=to_date('%s', 'YYYY-MM-DD') and
					in_date ::date
Dr Obx
on 12/22/15, 4:28 PM

......

			tot_qty = cr.fetchall()
			if product_id == True:
				print 'Product ', product_id, ' removed'
			else:
				if tot_qty:
					for rec in tot_qty:
						a = a + 1
						total += rec[1]
						lsrv = min(str,rec[1])
						hsrv = rec[1]
						asrv = (total / a)
						mwosres = (rec[1]/  !!!!!  i need it here !!!!!)
Dr Obx
on 12/22/15, 4:30 PM

Like in the answer, use it like:
mwosres = (rec[1]/ obj.days)

Axel Mendoza
on 12/23/15, 6:44 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 12/22/15, 7:38 AM
Seen: 365 times
Last updated: 1/4/16, 3:58 AM