OpenERP one2many issue - odoo

I have the following:
shipment_obj = self.pool.get('stock.picking.in').browse(cr, uid, context.get('active_id'))
This returns correct object, I can access properties, but when I access the O2m field "move_lines", it returns None object and cannot iterate.
Do I need special way to access the products for the specific incoming shipment?
This is in existing OpenERP:
'move_lines': fields.one2many('stock.move', 'picking_id', 'Internal Moves', states={'done': [('readonly', True)], 'cancel': [('readonly', True)]}),
'product_id': fields.related('move_lines', 'product_id', type='many2one', relation='product.product', string='Product'),

stock.picking.in , stock.picking.out and stock.picking are actually the same table stock_picking and stock.picking is considered as the base of both stock.picking.in and stock.picking.out. I think this functionality still have some issues and it is solved in openerp v8.So when you need to browse the object, use the stock.picking whether you are browsing for stock.picking.in nor stock.picking.out

when using
shipment_obj = self.pool.get('stock.picking.in').browse(cr, uid, context.get('active_id'))
here you will get a list of browse records, you can access the one2many field move_lines like
for shipment_id in shipment_obj:
move_lines = shipment_id.move_lines
here you will get the move_lines of each records of the list...

Needed to add this which solves the problem:
if context is not None and context.get('active_id'):
it is the way I thought the method was called that didn't need to check context and active_id since there would always be a context and active_id, I was wrong.

Related

Restrict write permissions for a field - Odoo 15

How can I restrict the write permissions for a field to a specific group ?
I want to check if a user is in a specific group with id 46. If the user is in this group, he should be allowed to write in this field. If he is not in this group, he should not be allowed to write.
The field is a custom field, editing the domain with the studio app I think I should avoid.
My field:
<field name="customer_codename" placeholder="Codename" attrs="{'invisible':['|',('customer_rank','=', 0),('is_company','=', False)]}"/>
I tried the following, but it did not work:
I created a new field using the studio app. Field type is boolean.
In the advanced properties I wanted to define the compute for the field. In dependencies I gave "user_id" and in the compute field I gave
for record in self:
user_id.has_group('__export__.res_groups_46_eff9dc52')
The boolean field should be set to true if the user is in a certain group.
Not sure if I can give you the best answer there is.
But for me, I'd personally create a Boolean field in the view's associated model, with its default field a lambda function checking if the user belongs to the groups you mentioned.
Assuming groups_id is the name of the user groups in model res.users, we have:
class ResUsers(models.Model):
_inherit = "res.users"
can_write_codename = fields.Boolean(default=lambda self: self.groups_id in ("model_name.group_name"))
Then in your xml file, you can include can_write_codename inside attrs, like this:
<field name="customer_codename" placeholder="Codename" attrs="{'invisible':['|',('customer_rank','=', 0),('is_company','=', False)], 'readonly': [('can_write_codename', '=', 'True')]}"}"/>

How to move field in sale order to stock picking

I have a problem, I made two custom fields in
Sale.order
Stock.picking
How to do when the sale order is confirmed, the field in stock.picking also filled? and the data was picked up from the field at sale.order I've made before.
I'm using odoo 10
Thanks
Yeah, first, depend in your module of sale_stock, then, inherit the sales confirm button and search for the pickings associated:
#api.multi
def action_confirm(self):
result = super(SaleOrder, self).action_confirm()
for order in self:
order.picking_ids.write({'your_field_in_picking': order.your_field_in_sale})
return result
Put it in a class that inherits from sale.order

How can I modify a selection field from another class in odoo 9?

I want to modify the "invoice_status" field from the "SaleOrder" class asociated to an invoice after the validation of that invoice.
The validation of an invoice is defined in the "AccountInvoice" class, inside the account module:
#api.multi
def invoice_validate(self):
...
I realised that the "name" field from "SaleOrder" class is related with the "origin" field from "AccountInvoice" class.
So, i modified the invoice_validate function like this:
#api.multi
def invoice_validate(self):
for invoice in self:
...
origin = self.origin
sale_order_id = self.env['sale.order'].search([('name', '=', origin)])[0].id
sale_order_obj = self.env['sale.order'].browse(sale_order_id)
sale_order_obj.write({'invoice_status': 'invoiced'})
return self.write({'state': 'open'})
For some reason, the write parte doesn't work.
That is the official definition of the "invoice_status" field from SaleOrder class:
invoice_status = fields.Selection([
('upselling', 'Upselling Opportunity'),
('invoiced', 'Fully Invoiced'),
('to invoice', 'To Invoice'),
('no', 'Nothing to Invoice')
], string='Invoice Status', compute='_get_invoiced', store=True, readonly=True, default='no')
You cannot set the value of invoice_status because it is a compute field. Even if you set it's value, it will be recomputed again when the field it depends on is changed and will eventually find the value it is supposed to have --and write that value instead of yours.
Odoo made it so that it work (it will say invoiced when the order is invoiced). So I don't think you need to do it maually. If you badly need to have your value stored, you should change that field so that it is not computed anymore, or create another field.
Check the selection_add attribute of the Selection class.
If you want to add some items to a selection field you have to redefine it in another class that inherits from the same model and declare it like this:
invoice_status = fields.Selection(selection_add=[("state", "open")])
Check the Selection class documentation and search for selection_add in your codebase to see some examples.

How to retrieve the current user in OpenERP module

I'm developing an OpenERP 7 module and I need to add a field that logs the user who created each record. How do I retrieve the current user object?
this kind of field is already available in openerp, as create_uid and write_uid.
In OpenERP Python code, functions generally take cr, the database pointer, and uid, the user id, as arguments. If all you need is the id of the current res.users object (for instance, to write into the one2many field), you can use uid as is. If you need to access the object (to see fields, etc.), something like:
current_user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
should work.

OpenERP: Add new selection on 'state' field by inheriting Sale Order

I really need to add an additional 'state' value on my Sale Order object. Since version 7.0, the 'sale_stock' module does exactly that already. When you try to do the same thing from your own module, your key,value just gets ignored. Is there any other alternative to achieve this?
As I found out, this seems to be an old time issue from two years ago as explained in this thread. A suggested workaround there was to do something like this:
_inherit = 'sale.order'
def __init__(self, pool, cr):
super(sale_order, self)._columns['state'].selection.append(('keyx', 'valuex'))
I found this approach logical, but it resulted in the following error:
`File "/home/nicolas/Eclipse/OpenERP/7.0/src/openerp/osv/orm.py", line 2958, in _auto_init
self._field_create(cr, context=context)
File "/home/nicolas/Eclipse/OpenERP/7.0/src/openerp/osv/orm.py", line 764, in _field_create
ir_model_fields_obj = self.pool.get('ir.model.fields')
AttributeError: 'sale.order' object has no attribute 'pool'`
Should this bug be reported at launchpad or is it an unintended use? What other possible solutions can you suggest? Thanks in advance.
try this
from openerp.osv import osv, fields
class sale_order(osv.osv):
_inherit = 'sale.order'
selection_list = [];#add your selection list here.
_columns = {
'state': fields.selection(selection_list,'State');#add necessary arguments
}
sale_order()
simply inherit the sale.order model and add your state field as it is which define in existing model , add the external state which you are required to add additionaly
for eg:
class sale_order(osv.osv)
_inherit ='sale.order'
_columns = {
'state': fields.selection([
('draft', 'Quotation'),
('waiting_date', 'Waiting Schedule'),
('manual', 'To Invoice'),
('progress', 'In Progress'),
('shipping_except', 'Shipping Exception'),
('invoice_except', 'Invoice Exception'),
('done', 'Done'),
('cancel', 'Cancelled'),
**('key','value')**,
This above would be your newly added selection value sequence dosen't matter.
], 'Order State', readonly=True, help="Gives the state of the quotation or sales order. \nThe exception state is automatically set when a cancel operation occurs in the invoice validation (Invoice Exception) or in the picking list process (Shipping Exception). \nThe 'Waiting Schedule' state is set when the invoice is confirmed but waiting for the scheduler to run on the order date.", select=True),
}
sale_order()
than abouve key value will be your additional selection field and you can set it any where in the squence as per your requirements.
having the same problem, I had a look at the thread, you noticed.
I guess that the problem comes from the fact that our modules and sale_stock are "in conflict" because they modify the same field ('state') in the sale.order object and are not depending of each other.
One solution is to modify your own module and adding 'sale_stock" in the 'depends' list of openerp.py :
depends : ['sale_stock',...]
You can see an example in this module which had another (key,value) in state field : http://bazaar.launchpad.net/~agaplan/agaplan-addons/7.0/files/head:/sale_double_validation/
Hope it helps