I want this module to calculate expiry date from two fields i.e mfg date and bbm(best before in months).The expiry field should add both mfg date,bbm and show it's value as readonly. Below is the code i'm using.
class omfed_items(osv.Model):
"""
items or products profilling
"""
_name = 'omfed.items'
_description = 'omfed items'
_order = 'code asc'
_columns = {
'name': fields.char('Name', required=True),
'code': fields.integer('Code', required=True),
'category_id':fields.many2one('omfed.category','Category',required=True),
'mfg_date':fields.date('Manufacturing Date'),
'p_manager':fields.many2one('hr.employee','Product Manager'),
'bbm':fields.integer('Best before use (in Months)'),
'exp_date':fields.date(compute='addmonths', string="Expiry Date"),
}
def addmonths(self):
try:
targetdate = omfed_items.mfg_date
targetmonths = omfed_items.bbm+targetdate.month
if targetmonths%12 == 0:
targetmonth = 12
else:
targetmonth = targetmonths%12
if favorEoM == True:
testdate = date+datetime.timedelta(days=1)
if testdate.day == 1:
targetdate.replace(year=targetdate.year+int((targetmonths+1)/12),month=(targetmonth%12+1),day=1)
targetdate+=datetime.timedelta(days=-1)
else:
targetdate.replace(year=targetdate.year+int(targetmonths/12),month=(targetmonth))
else:
targetdate.replace(year=targetdate.year+int(targetmonths/12),month=(targetmonth))
return targetdate
except:
targetdate.replace(year=targetdate.year+int((targetmonths+1)/12),month=(targetmonth%12+1),day=1)
targetdate+=datetime.timedelta(days=-1)
return targetdate
class omfed_category(osv.Model):
""" category """
_name = 'omfed.category'
_description = 'omfed category'
_columns={
'name': fields.char('Category Name',required=True),
'code': fields.integer('Category Code',required=True),
'items_id':fields.one2many('omfed.items','category_id','Items'),
}
XML File
<?xml version="1.0"?>
<openerp>
<data>
<menuitem id='menu_omfed' name='OMFED'/>
<record model="ir.ui.view" id="omfed_items_search_view">
<field name="name">Search</field>
<field name="model">omfed.items</field>
<field name="arch" type="xml">
<search string="Search">
<field name="name" filter_domain="['|', ('name', 'ilike', self), ('note', 'ilike', self)]"/>
<field name="code"/>
<group expand="0" string="Group By">
<filter name="name" string="PRODUCT" context="{'group_by':'name'}" help="product"/>
<filter name="code" string="CODE" context="{'group_by':'code'}" help="product code"/>
<filter name="p_manager" string="PRODUCT MANAGER" context="{'group_by':'p_manager'}" help="product manager"/>
</group>
</search>
</field>
</record>
<record model="ir.ui.view" id="omfed_items_tree_view">
<field name="name">omfed_items_tree</field>
<field name="model">omfed.items</field>
<field name="arch" type="xml">
<tree string="Products or Items">
<field name="name"/>
<field name="code"/>
<field name="category_id"/>
<field name="mfg_date"/>
<field name="bbm"/>
<field name="exp_date"/>
<field name="p_manager"/>
</tree>
</field>
</record>
<record model="ir.ui.view" id="omfed_items_form_view">
<field name="name">omfed_items_form</field>
<field name="model">omfed.items</field>
<field name="arch" type="xml">
<form string="Products or Items">
<group col="4" colspan="1">
<field name="name"/>
<field name="code"/>
<field name="category_id"/>
<field name="mfg_date" on_change="addmonths(mfg_date,bbm)"/>
<field name="bbm"/>
<field name="exp_date"/>
<field name="p_manager"/>
</group>
</form>
</field>
</record>
<record model="ir.actions.act_window" id="action_omfed_items_form">
<field name="name">omfed_items</field>
<field name="res_model">omfed.items</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem name="Items" parent="menu_omfed" id="menu_omfed_items" action="action_omfed_items_form" />
<menuitem name="Dairy_Items" parent="menu_omfed_items" id="menu_omfed_dairy_items" action="action_omfed_items_form" />
<record model="ir.ui.view" id="category_tree_view">
<field name="name">category_tree</field>
<field name="model">omfed.category</field>
<field name="arch" type="xml">
<tree string="Category of Items">
<field name="name"/>
<field name="code"/>
<field name="items_id"/>
</tree>
</field>
</record>
<record model="ir.ui.view" id="category_form_view">
<field name="name">category_form</field>
<field name="model">omfed.category</field>
<field name="arch" type="xml">
<form sting="Category of Items">
<group col="4" colspan="1">
<field name="name"/>
<field name="code"/>
<field name="items_id"/>
</group>
</form>
</field>
</record>
<record model="ir.ui.view" id="category_search_view">
<field name="name">category_search</field>
<field name="model">omfed.category</field>
<field name="arch" type="xml">
<search string="Search">
<field name="name"/>
<field name="code"/>
<field name="items_id"/>
<group expand="0" string="Group By">
<filter name="cname" string="CATEGORY NAME" context="{'group_by':'cname'}" help="category name"/>
<filter name="ccode" string="CATEGORY CODE" context="{'group_by':'ccode'}" help="category code"/>
</group>
</search>
</field>
</record>
<record model="ir.actions.act_window" id="action_omfed_category">
<field name="name">omfed_categry</field>
<field name="res_model">omfed.category</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem name="Categories" parent="menu_omfed_items" id="menu_omfed_category" action="action_omfed_category" />
</data>
Thanks in advance for solution.
You can use the following code: