I have a selection field (many to one division field) if I select one division need to load its' employees on the 'notebook section'. how to implement this in openerp 7?
if i selected the relevant estate id then need to load relevant divisions.if division select then need to load relevant workers in following phase
<record model="ir.ui.view" id="bpl_work_offer_form">
<field name="name">bpl.work.offer.form</field>
<field name="model">bpl.work.offer</field>
<field name="arch" type="xml">
<form string='bpl_work_offer' version='7.0'>
<sheet>
<group>
<group>
<field name='bpl_estate_id' />
<field name='bpl_division_id' />
<field name='user_id' />
</group>
<group>
<field name='date_of_offer' />
<field name='payment_type' />
<field name='select_by' />
<field name='no_of_workers' attrs="{'invisible':[('select_by','=','by_name')]}" />
<field name='work_type' />
</group>
</group>
<div name="Worker Selection"></div>
<separator string='Select workers' />
<notebook attrs="{'invisible':[('select_by','=','by_count')]}">
<page string="Tea Workers" attrs="{'readonly':[('work_type','!=','tea')]}">
<field name='selected_tea_workers_line_ids' nolabel='1'>
<tree string='List' editable='bottom'>
<field name='worker_id' />
<field name='is_selected' />
</tree>
</field>
<group class='oe_subtotal_footer oe_right'>
<field name='total_workers' />
</group>
<br />
<br />
</page>
<page string="Rubber Workers" attrs="{'readonly':[('work_type','!=','rubber')]}">
<field name='selected_rubber_workers_line_ids' nolabel='1'>
<tree string='List' editable='bottom'>
<field name='worker_id' />
<field name='is_selected' />
</tree>
</field>
<group class='oe_subtotal_footer oe_right'>
<field name='total_workers' />
</group>
<br />
<br />
</page>
<page string="Sundry Workers" attrs="{'readonly':[('work_type','!=','sundry')]}">
<field name='selected_sundry_workers_line_ids' nolabel='1'>
<tree string='List' editable='bottom'>
<field name='worker_id' />
<field name='is_selected' />
</tree>
</field>
<group class='oe_subtotal_footer oe_right'>
<field name='total_workers' />
</group>
<br />
<br />
</page>
</notebook>
</sheet>
</form>
</field>
</record>
<record model="ir.actions.act_window" id="bpl_work_offer_action">
<field name="name">bpl.work.offer.action</field>
<field name="res_model">bpl.work.offer</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
</record>
model code
class company_new_registration(osv.osv):
_name = "bpl.company.n.registration"
_description = "Company"
_columns = {
'name': fields.char('Company Name', size=128, required=True),
'estates': fields.one2many('bpl.estate.n.registration', 'company_id', 'Estate')
}
# Bill of Material
company_new_registration()
class estate_new_registration(osv.osv):
_name = "bpl.estate.n.registration"
_description = "Estates"
_columns = {
'name': fields.char('Estate Name', size=128, required=True),
'company_id': fields.many2one('bpl.company.n.registration', 'Company Name', select=True),
'divisions': fields.one2many('bpl.division.n.registration', 'estate_id', 'Division')
}
estate_new_registration()
class division_new_registration(osv.osv):
_name = "bpl.division.n.registration"
_description = "Divisions"
_columns = {
'name': fields.char('Division Name', size=128, required=True),
'estate_id': fields.many2one('bpl.estate.n.registration', 'Estate Name', select=True),
}
division_new_registration()
and needed mapping for that work offer (plantation project)
class bpl_work_offer(osv.osv):
_name = "bpl.work.offer"
_description = "BPL Work Offer"
_columns = {
'user_id': fields.many2one('res.users', 'User Name'),
'date_of_offer': fields.date('Date'),
'bpl_estate_id':fields.many2one('bpl.estate.n.registration', 'Estate', help='Estate'),
'bpl_division_id':fields.many2one('bpl.division.n.registration', 'Division', help='Division'),
'payment_type': fields.selection([('normal_work', 'Normal Work'), ('cash_work', 'Cash Work')], 'Payment Type'),
'select_by': fields.selection([('by_name', 'By Names'), ('by_count', 'By Count')], 'Select By'),
'no_of_workers': fields.integer('No of Workers'),
'work_type': fields.selection([('tea', 'Tea'), ('rubber', 'Rubber'), ('sundry', 'Sundry')], 'Work Type'),
'total_workers': fields.integer('Total Workers'),
'selected_tea_workers_line_ids': fields.one2many('bpl.selected.tea.workers.line', 'worker_id', 'Tea Workers', ondelete="cascade"),
'selected_rubber_workers_line_ids': fields.one2many('bpl.selected.rubber.workers.line', 'worker_id', 'Rubber Workers', ondelete="cascade"),
'selected_sundry_workers_line_ids': fields.one2many('bpl.selected.sundry.workers.line', 'worker_id', 'Sundry Workers', ondelete="cascade"),
}
bpl_work_offer()
class selected_tea_workers_line_ids(osv.osv):
_name = 'bpl.selected.tea.workers.line'
_description = 'BPL Selected Tea Workers line'
_columns = {
'worker_id':fields.many2one('bpl.worker', 'Tea Worker', ondelete='cascade', help='Worker'),
'is_selected': fields.boolean('Select', help="Selected or not"),
}
selected_tea_workers_line_ids()
class selected_rubber_workers_line_ids(osv.osv):
_name = 'bpl.selected.rubber.workers.line'
_description = 'BPL Selected Rubber Workers line'
_columns = {
'worker_id':fields.many2one('bpl.worker', 'Rubber Worker', ondelete='cascade', help='Worker'),
'is_selected': fields.boolean('Select', help="Selected or not"),
}
selected_rubber_workers_line_ids()
class selected_sundry_workers_line_ids(osv.osv):
_name = 'bpl.selected.sundry.workers.line'
_description = 'BPL Selected Sundry Workers line'
_columns = {
'worker_id':fields.many2one('bpl.worker', 'Sundry Worker', ondelete='cascade', help='Worker'),
'is_selected': fields.boolean('Select', help="Selected or not"),
}
selected_sundry_workers_line_ids()
You can use the following example to create your onchange function. This is an onchange function I have created for invoice(just an example)
def onchange_partner_id(self, cr, uid, ids, type, partner_id,\
date_invoice=False, payment_term=False, partner_bank_id=False, company_id=False):
res = super(account_invoice, self).onchange_partner_id(cr, uid, ids, type, partner_id,
date_invoice=date_invoice, payment_term=payment_term, partner_bank_id=partner_bank_id,
company_id=company_id)
invoice_lines = []
product_ids = self.pool.get('product.product').search(cr, uid, [],limit=5)
for p in self.pool.get('product.product').browse(cr, uid, product_ids):
invoice_lines.append((0,0,{'product_id':p.id,'name':p.name,
'account_id':p.categ_id.property_account_income_categ.id,
}))#this dict contain keys which are fields of one2many field
res['value']['invoice_line']=invoice_lines
return res
if there is only single line to be added then res['value'].update({ 'one2many_fieldmname':[(0,0,{'field1':value1,'field2':value2,'fieldn':valuen})] })
In the above example I am searching 5 products and these products are added in the invoiceline. So in your onchange function also you have to search the employee which comes under the division.
In your code,in the form "bpl_work_offer_form" you have add the onchange function at the field bpl_division_id.(in my example partner_id is the many2one field and invoice_line is the one2many field.)
You need to more careful when using onchange to load the one2many field. In the onchange function you have to first unlink all the records loaded in your many2one field. because if once the record is saved then if the onchange is again loaded then a new line will be created. so you need to manage it correctly.
Reference : http://doc.openerp.com/trunk/developers/server/06_misc_on_change_tips/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With