I have heat_degree_day and hw_degree_day set to readonly so user can't modify them directly.
I have an on_change event that modifies them based on other field the user enters.
The degree day fields are being modified correctly when the user enters the high and low temps for the day; however, when I click the save button the degree day fields revert back to their original values.
I am assuming this is because I have them set to read only in the view.
I would assume there has to be a common way to work around this, but I am not finding it in the documentation.
degree_day.py
from openerp.osv import osv, fields
from dateutil.parser import *
from dateutil.tz import *
from datetime import *
class degree_day(osv.osv):
_name = "degree.day"
_columns={
'date': fields.date('Date'),
'high_temp': fields.integer('High Temp'),
'low_temp': fields.integer('Low Temp'),
'heat_degree_day': fields.integer('Heat Degree Day' ),
'hw_degree_day': fields.integer('Hot Water Degree Day' ),
'debug':fields.text('Debug text'),
}
def write(self, cr, uid, ids, vals, context=None):
dd_recs = self.pool.get('degree.day')
low_temp = dd_recs.read(cr, uid, ids[0], ["low_temp"])["low_temp"]
high_temp = dd_recs.read(cr, uid, ids[0], ["high_temp"])["high_temp"]
a = 65 - ((high_temp + low_temp)/2)
if a < 0:
a = 0
vals['heat_degree_day'] = a
vals['hw_degree_day'] = a + 5
return super(degree_day, self).write(cr, uid, ids, vals, context=context)
def generate_degree_day(self, cr, uid, ids, high_temp, low_temp, date, debug, context=None):
""" calculates heat degree day and hot water degree day when high or low temp is changed
@param high_temp: The day's high temperature
@param low_temp: The day's low temperature
"""
v={}
debug = "... "
# check to see that args are not null
if (high_temp and low_temp and date):
debug += "args present:"
adate = parse(date)
day_before = adate - timedelta(days=1)
dd_recs = self.pool.get('degree.day')
# were any degree.day records retrieved?
if dd_recs:
debug += " dd_recs not null:"
ids = dd_recs.search(cr, uid, [("date", "=", day_before)])
# is there a record for yesterday?
if ids:
debug += " ids not null:"
last_hdd = dd_recs.read(cr, uid, ids[0], ["heat_degree_day"])
last_hwdd = dd_recs.read(cr, uid, ids[0], ["hw_degree_day"])
# degree day calculation
a = 65 - ((high_temp + low_temp)/2)
if a < 0:
debug += " a<0:"
a = 0
v['heat_degree_day'] = last_hdd["heat_degree_day"] + a
v['hw_degree_day'] = last_hwdd["hw_degree_day"] + a + 5
v['debug'] = debug
return {'value':v}
degree_day_view.xml
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="degree_day_tree" model="ir.ui.view">
<field name="name">degree.day.tree</field>
<field name="model">degree.day</field>
<field name="arch" type="xml">
<tree string="Degree Day List">
<field name="date" />
<field name="high_temp" />
<field name="low_temp" />
<field name="heat_degree_day" />
<field name="hw_degree_day" />
<field name="debug" />
</tree>
</field>
</record>
<record id="degree_day_form" model="ir.ui.view">
<field name="name">degree.day.form</field>
<field name="model">degree.day</field>
<field name="arch" type="xml">
<form string="Degree Day" version="7.0">
<sheet string="Degree Day">
<h1>Day <field name="date" class="oe_inline"/></h1>
<group>
<field name="low_temp" on_change="generate_degree_day(high_temp, low_temp, date, debug)" />
<field name="high_temp" on_change="generate_degree_day(high_temp, low_temp, date, debug)" />
</group>
<group>
<field name="heat_degree_day" readonly="True" />
<field name="hw_degree_day" readonly="True" />
</group>
<field name="debug" />
</sheet>
</form>
</field>
</record>
<record id="show_degree_day" model="ir.actions.act_window">
<field name="name">Degree Day</field>
<field name="res_model">degree.day</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem name="Degree Day" id="menu_degree_day" action="show_degree_day"/>
</data>
</openerp>