Skip to Content
Menu
This question has been flagged
4 Replies
6857 Views

Hi Guys,

I have been working for my below code for few days, but still can't get it work. What I intended to do is when one select raw-material product type (customized field) in Product Create section, Buy route will be selected. On the other hand, select "Finish Good", Manufacture and MTO route will be selected. The below codes does the half because it doesn't clear the previous selected value when switch from Raw-Material to Finish Good. The previous filled up value still remain. Please help! Thx so much.

@ api.onchange ('custom_product_type')

    def _onchange_custom_product_type (self):

        if self.custom_product_type:

            self.warehouse = self.env.ref ('stock.warehouse0')

            route_manufacture = self.env.ref ('stock.warehouse0'). manufacture_pull_id.route_id.id

            route_mto = self.env.ref ('stock.warehouse0'). mto_pull_id.route_id.id

            buy_route = self.env.ref ('stock.warehouse0'). buy_pull_id.route_id.id

             if self.custom_product_type == 'RM':

                self.sale_ok = False

                self.purchase_ok = True

                self.update ({'route_ids': [(6, 0, [buy_route])]})

            elif self.custom_product_type == 'FG' or self.custom_product_type == 'HG':

                self.sale_ok = True

                self.purchase_ok = False

                self.update ({'route_ids': [(6, 0, [route_manufacture, route_mto])]})

Avatar
Discard
Best Answer

Just a few comments regarding.

1. Why have you chosen to use @api.onchange for that purpose? It is triggered only when the field is manually changed on the form. Moreover, it is not ideal to change many2many fields.

2. Basically, in onchange you are not obliged to work with m2m/o2m operators (at least at Odoo version > 11). So, if I’m not mistaken you may do self.route_ids = self.env.ref('stock.warehouse0').manufacture_pull_id.route_id

3. Continuing the point 1: it is much more efficient to work with inverse of the field ‘custom_product_type’ since in inverse the record is already saved. I would offer something like:

def _inverse_custom_product_type(self):
    warehouse = self.env.ref('stock.warehouse0')
    fhg_routes = []
    buy_routes = []
    if warehouse:
        fhg_routes = (warehouse.manufacture_pull_id.route_id + warehouse.mto_pull_id.route_id).ids
        buy_routes = warehouse.buy_pull_id.route_id.ids
    for product in self:
        res_route_ids = []
        if product.custom_product_type ==  'RM':
            res_route_ids = buy_routes
        elif product.custom_product_type in  ['FG', 'HG']:
            res_route_ids = fhg_routes
        product.route_ids = [(6, 0, res_route_ids)]
custom_product_type = fields.Selection(
    [
        ("RM", "Raw MAterial"),
        ("FG", "Finished Good"),
        ("HG", "whatever it means")
    ],
    inverse=_inverse_custom_product_type,
)


Avatar
Discard
Best Answer

Hi,

Try the following

self.update({'route_ids': [(5,), (6, 0, [route_manufacture, route_mto])]})

Hope it helps

Avatar
Discard
Best Answer

Hi, did you try: self.route_ids = [(6, 0, [route_manufacture, route_mto])]

Avatar
Discard
Author Best Answer

@Adnier, thx for your response, but it doesn't work. I tried.


@Odoo Tools, thx for your response, your code is much better than mine. I will have a try and let you know if it works. Yes, you are correct, api.change won't be able to change many2many vaule. Actually, I found out my original work after save. Althought previous selected value doesn't change in the "form" only, it does change after press "Save". Your code looks more reasonable and sophisticate.
Avatar
Discard
Related Posts Replies Views Activity
1
Aug 19
1265
3
Dec 24
11849
2
Oct 24
2660
2
Aug 21
4035
2
Mar 24
1431