Hi,
I've created a module that creates a relation one2many between Purchase Order and Sales Order. So in Purchase order we can choose a Sales Order that is the origin to this Purchase Order, like this:
In mymodule.py
class purchase_order(osv.osv):
_inherit = 'purchase.order'
_name = 'purchase.order'
_columns = {
'asd_pquotation_id': fields.many2one('sale.order','Sale Quotation', domain=[('state','in',('draft','sent'))]),
}
_defaults = {
'asd_pquotation_id': lambda self, cr, uid, context: context.get('asd_pquotation_id', False),
}
purchase_order()
class sale_order(osv.osv):
_inherit = "sale.order"
_name = "sale.order"
_columns = {
'asd_squotation': fields.one2many('purchase.order','asd_pquotation_id', 'Purchase Quotation'),
}
sale_order()
In mymodule_view.xml
purchase.order.form.changepurchase.order
The result is like this:

But now I wanted to by selecting the sale quotation (in red) to fill in the purchase.order.line (in green) with the information from sale.order.line. I've tried this code, but I've an error:
In the file mymodule.py I added this function:
class purchase_order(osv.osv):
_inherit = 'purchase.order'
_name = 'purchase.order'
STATE_SELECTION = [
('draft', 'Draft PO'),
('sent', 'RFQ Sent'),
('received', 'RFQ Received'),
('confirmed', 'Waiting Approval'),
('approved', 'Purchase Order'),
('except_picking', 'Shipping Exception'),
('except_invoice', 'Invoice Exception'),
('done', 'Done'),
('cancel', 'Cancelled')
]
_columns = {
'state': fields.selection(STATE_SELECTION, 'Status', readonly=True, help="The status of the purchase order or the quotation request. A quotation is a purchase order in a 'Draft' status. Then the order has to be confirmed by the user, the status switch to 'Confirmed'. Then the supplier must confirm the order to change the status to 'Approved'. When the purchase order is paid and received, the status becomes 'Done'. If a cancel action occurs in the invoice or in the reception of goods, the status becomes in exception.", select=True),
'vi_coa': fields.boolean('COA', help="Check this box if you received and attach the COA"),
'vi_msds': fields.boolean('MSDS', help="Check this box if you received and attach the MSDS"),
'asd_pquotation_id': fields.many2one('sale.order','Sale Quotation', domain=[('state','in',('draft','sent'))]),
}
_defaults = {
'asd_pquotation_id': lambda self, cr, uid, context: context.get('asd_pquotation_id', False),
}
def onchange_sales_order(self, cr, uid, ids, asd_pquotation_id, partner_id, pricelist_id):
domain = []
value = []
if asd_pquotation_id:
so = self.pool.get('sale.order').browse(cr,uid,asd_pquotation_id)
for sol in so.order_line:
vals = self.pool.get('purchase.order.line').onchange_product_id(cr, uid, ids, pricelist_id, sol.product_id.id, sol.product_uom_qty, sol.product_uom.id, partner_id)
vals['value'].update({
'product_id': sol.product_id.id,
})
value.append(vals['value'])
domain.append(vals['domain'])
return {'value': {'order_line': value}, 'domain': {'order_line': domain}}
purchase_order()
and in the view:
purchase.order.form.changepurchase.order
**When I try to save I get this error:**
Integrity Error
The operation cannot be completed, probably due to the following:
- deletion: you may be trying to delete a record while other records still reference it
- creation/update: a mandatory field is not correctly set
[object with reference: state - state]
**I if I change any line of purchase order line, all the buttons stays disabled.**
Someone know how can I do what I want?
I've already check this :
http://doc.openerp.com/trunk/developers/server/06_misc_on_change_tips/
http://doc.openerp.com/v6.0/developer/2_6_views_events/events/events.html
but this doesn't help me...