How to change many2many field route_ids value in odoo? - odoo

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])]})

Your code should work, but you can try to remove them with (5, ) before adding the new routes:
self.update({'route_ids': [(5, ), (6, 0, [route_manufacture, route_mto])]})

Actually, I found out my original works after save. Althought previous selected value doesn't change in the "form", it does change after press "Save".

Related

Fold or hide Kanban-stage dependent on parameter in context

Does anybody have experience with the following problem or any idea how to solve it:
I want to fold or hide specific Kanban-stages dependent on a Boolean parameter in the context.
You can try with invisible attribute and get value from the context.
invisible="context.get('boolean_field', False)"
JFI, I haven't tried it in kanban fold / hide but this logic works elsewhere. For example, field, span, t, div, qweb template etc.
Stage field is a selection field of Odoo. So you can not display its value base on condition but you can display default at some stage like statusbar_visible="stage1,stage2,stage5".
I found a working solution to completely hide kanban stages depending on the context (here: job_id). This function from hr.applicant does the trick:
stage_id = fields.Many2one(
group_expand='_read_group_stage_ids')
#api.model
def _read_group_stage_ids(self, stages, domain, order):
# retrieve job_id from the context and write the domain: ids + contextual columns (job or default)
job_id = self._context.get('default_job_id')
search_domain = [('job_ids', '=', False)]
if job_id:
search_domain = ['|', ('job_ids', '=', job_id)] + search_domain
if stages:
search_domain = ['|', ('id', 'in', stages.ids)] + search_domain
stage_ids = stages._search(search_domain, order=order, access_rights_uid=SUPERUSER_ID)
return stages.browse(stage_ids)

Change many-to-one domain based on selection field

I want to change the domain of many-to-one field based on user selection, selection field have to options (code, phone), but the domain does not change and get only the default value:
#api.onchange('search_by')
def _get_partner(self):
partners = self.env['customer.regist'].search([('name','!=','New')])
partner_list = []
partner_list2 = []
for rec in partners:
partner_list.append(rec.name)
partner_list2.append(rec.phone)
res = {}
if self.search_by == 'code':
res['domain'] = {'search_value': [('name', 'in', partner_list)]}
if self.search_by == 'phone':
res['domain'] = {'search_value': [('phone', 'in', partner_list2)]}
return res
I didn't test the code but from the look of it the domain will always contain all customer.regist records (except the ones with the name 'New' of course). You are going through all the records and adding their phones / names to a list and then say that the phone / name must be included in that list. So you will always have all of them.

How to use get default method on a selection field based on condition odoo 12?

What im trying to do is fetching a element from the selection field based on the state of the record.
#api.model
def _get_next_step(self):
for rec in self:
if rec.state == 'draft':
return rec.write({'next_step': 'waiting_room'})
elif rec.state == 'waiting_room':
return rec.write({'next_step': 'start_consultation'})
elif rec.state == 'start_consultation':
return rec.write({'next_step': 'finish_consultation'})
next_step = fields.Selection([
('waiting_room', 'To Waiting Room'),
('start_consultation', 'Start Consultation'),
('finish_consultation', 'Finish Consultation'),
('follow_up', 'Follow-Up'),
], string='Next Step', copy=False, index=True, track_visibility='onchange', defult='_get_next_step')
what i tried to do here is that,applying default in the selection field and wrote a function for the default method,But the field next_step is not getting updated.
The default execution environment will never have records, self is always an empty recordset. The api.model decorator is telling you that already.
You could just change the field next_step to a computed field and trigger the recomputation on state. When you store the computed field, everything like searches/grouping will work like on normal fields.

How to creat records in odoo tree view onclick button?

please help
I need when I click Enregistrer Button to create those fields in the tree view on the bottom
for this example, I have quantity equal 12 so I need 12 lines to be created on the tree view with the values on the wizard view
the wizard code :
class LinesWizard(models.Model):
_name = 'bons.wizard'
w_contrat_name = fields.Many2one('contrat.contrat', string='Contrat')
w_contrat_line = fields.Many2one('contrat.lignes', string='Ligne contrat')
w_product_name = fields.Many2one('product.product', string='Produit')
w_po_number = fields.Char(string='Numero PO')
w_qtt = fields.Float('quantite', related='w_contrat_line.quantity')
w_prix = fields.Float(string='Prix unitaire', related='w_contrat_line.unit_price')
#api.onchange('w_contrat_name')
def on_change_contrat_name(self):
if self.w_contrat_name:
self.w_contrat_line = False
return {'domain': {'w_contrat_line' : [('ligne_ids', '=', self.w_contrat_name.id)]}}
else:
return {'domain': {'w_contrat_line': []}}
In your function for the Enregistrer button, you can use below code to get the active sale.order ID.
session_id = self.env['sale.order'].browse(self._context.get('active_id'))
Then in the same function, simply create and add your rows.
session_id.write({
'your_tree_ids': [(0, False,
{
'w_contrat_name': self.w_contrat_name,
'w_product_name': self.w_product_name,
'etc': 'etc...'}
)] * int(self.w_qtt) # assuming rows to be added are the same, create a list of w_qtt quantity of (0, _, values), since your qty is a float, need to convert to int first
})

Save array in DB when checked more than one checkbox

I have a problem greatest!! I guess that really want Array, look my console when I checked just one:
{"value_solve"=>["", "", "333", ""], "contract_number"=>["33"]}
-----
SQL (317.5ms) UPDATE "authorizations" SET "value_solve" = '', "situation" = 2 WHERE "authorizations"."contract_number" = ? [["contract_number", "33"]]
After, when I checked just one, the first:
{"value_solve"=>["111", "", "", ""], "contract_number"=>["11"]}
-----
SQL (317.5ms) UPDATE "authorizations" SET "value_solve" = '111 ', "situation" = 2 WHERE "authorizations"."contract_number" = ? [["contract_number", "11"]]
And, for last, when I just more then one:
{"contract_number"=>["11", "44"], "value_solve"=>["111", "", "", "444"]}
-----
SQL (297.7ms) UPDATE "authorizations" SET "value_solve" = '111', "situation" = 2 WHERE "authorizations"."contract_number" = ? [["contract_number", "11"]]
SQL (121.9ms) UPDATE "authorizations" SET "value_solve" = '', "situation" = 2 WHERE "authorizations"."contract_number" = ? [["contract_number", "44"]]
And this is my controller:
#selected_ids = params[:authorization][:contract_number]
#authorizations = Authorization.where("contract_number in (?)", #selected_ids)
auth_params = params[:authorization]
auth_params[:contract_number].zip(auth_params[:value_solve]).each do |contract_number, value_solve|
Authorization.where(contract_number: contract_number).update_all(value_solve: value_solve, situation: 2)
end
Just save the first value on DB, how I can save more then one value? Thanks!
As I understood, you want the contract_number with id 44 to be “associated” with value_solve == "444". If this is correct, you should remove blanks from your value_solve array:
auth_params[:contract_number].zip(auth_params[:value_solve].reject(&:blank?))...
Now 44 is being updated with the second element of value_solve, which is apparently an empty string.
See Array#zip for more details.