How to relate fields in Odoo 11 CE - odoo

I added a custom field in "account.payment" model, I also added it in "account.move.line" model (aka: Journal Item).
The value of the custom field is entered from account.payment and since the journal items are generated from a payment creation I want to do the following:
If account.move.line.id is created by account.payment.id
let account.move.line.custom_field = account.payment.custom_field
I appreciate your help

You have to find in account.move.line that represent the relation(foreign_key) with account.payment, let says payment_id, this field allows you to access to account.payment model, and for your case you just need to do:
field_in_account_move_line = payment_id.field_in_account_payment
I hope this answer can be helpful for you.

Related

How to programatically create a paid invoice from sale order [duplicate]

I am trying to make a single action on a sales order in Odoo v15 CE that creates an invoice for the sales order and immediately posts it and registers a payment for it. The way I'm doing so is through a wizard method that looks like this:
def create_invoices(self):
sale_orders = self.env['sale.order'].browse(self._context.get('active_ids', []))
for order in sale_orders:
order._create_invoices()
for inv in order.invoice_ids:
# make invoice:
inv.action_post()
# register payment
# ????
Currently, it creates the invoice and posts it. I would like to add something like inv.register_payment() to the final line in order to register the payment.
I've fount the action_register_payment method of the account.move model and also looked into using the account.payment.register wizard but neither worked. I've also found this question, which is trying to do something similar, but through an XML-RPC call (from what I can tell).
Can anyone please explain how to do this? Thanks!
You have to post the invoices using action_post then register payment by creating a payment like in
https://github.com/odoo/odoo/blob/15.0/addons/account/wizard/account_payment_register.py#L496
account.payment.register is a wizard, that's why you need to create values for it, otherwise you can action_register_payment which is a window action that will trigger the wizard and then it's for the user to register the payment (which is the standard behavior of Odoo)
You can create an account.payment.register record and pass the model and invoice ids in context then call action_create_payments to create the payment with the default values.
Example:
payment_register.with_context(
{'active_model': 'account.move',
'active_ids': inv.ids}
).create({}).action_create_payments()

Create an invoice from a sales order using XML-RPC (Python)

I'm following the external APIs documentation : https://www.odoo.com/documentation/13.0/webservices/odoo.html
to implement our companies requirements. I'm required to create a sales order and automatically create an invoice after that. The sales order part is done but I cant seem to be able to attach the Invoice to the Sales order
I've tried linking it via the 'invoice_ids' field but the documentation does not mention how to provide a many2many field in it. here is the code:
many2manyInvoice = [(4, invoice_id)]
common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url))
#Admin user Id
uid = common.authenticate(db, username, password, {})
models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url))
models.execute_kw(db, uid, password, 'sale.order', 'write', [[sales_order_id], {'invoice_ids':many2manyInvoice}])
The response returned is 200 , but nothing is happening on the sales order level. I think its the way that I defined the field that might be incorrect.
Can someone help with this issue ? Thanks in advance
Nothing is happening on the sales order level because you are not creating a sales record, writing to it without doing anything. Not sure if this would work in your specific case but here is what I would do.
Use the Pro-forma invoice https://www.odoo.com/documentation/user/13.0/sales/invoicing/proforma.html
Then when a sales record is created run the "Send pro-forma invoice" method using the web api. This takes care of the db linking, as it can get very complicated.

Correct way to make comparison based on other models fields

for line in invoice.invoice_line_ids:
if line.sale_line_ids:
if line.sale_line_ids.qty_avl_stock >= line.sale_line_ids.product_uom_qty:
term = _('At warehouse')
in a proforma invoice, I want to check sale_order_line fields that is related to invoice_line.
In account.invoice.line I found a relation to sale.order.line it's sale_line_ids field. my goal is to make some checking in related sale_order_line but is this a good way to do? or maybe I should do a search on sale.order.line model?
Because sale_line_ids field is many2many and probably there can be more than one record and if that is the case i will get an error here.
UPDATE for bounty
Basically, I need check order_line qty_avl_stock and product_uom_qantity that is related to invoice_line. But I don't know how is the best way to relate sale_order_line to invoice_line, or maybe there are other solutions?
class SaleOrerLine(self)
qty_avl_stock = fields.float("Quantity availible stock")

related attributes , related fields on inherited view odoo

what is related attribute , what it can be used for ? and how to add a related attribute .
I'm trying to add a related field for total amount.
Related Field
In such case we need to take the value from any other table but we already have the reference of that table in our model, in that case we can utilize one functionality with that we can add the multiple fields from the reference table by just having the only one reference field in our model.
One relational field (Many2one) of the source model is mandatory to
have in the destination model to create relational field.
Consider the company_currency_id field from the res.currency model is there in the destination model then you can get the current rate of that currency in your target model.
syntax: v7
_columns = {
'current_rate': fields.related('company_currency_id','rate_silent', type='float', relation='res.currency',digits_compute=dp.get_precision( 'Account'), string='Current Rate', readonly=True),
}
Here,
company_currency_id => the field in the same model through which the new field will be relate,
rate_silent => is the field which you are going to relate with new field, means the field from source model,
relation => is the source model name,
type => is the datatype of the source field
Syntax: v8 and newer version
current_rate = fields.Float(string='Current Rate', related='company_currency_id.rate_silent', store=True, readonly=True)
Note :
Value is available in that field only after you save the record.
When you update value in your newly defined related field it
will be updated in source field as well, though it's always advisable
to set readonly in newly defined field.
In context of Odoo related field means, that its value will be
read from another table (related table) --> store=False
read from another table, but stored in the table its defined on --> store=True
Examples (Odoo V8):
sale.order currency_id: it is persisted in product_pricelist
stock_quant packaging_type_id: it is persisted in product_packaging and stock_quant. Everytime you change the value on product_packinging it will be updated on stock_quant, too, not vice versa.
**Related Field* is used when you wanted to pull a value from another model you can use related field on fields.
Here is an example for you.
order_id = fields.Many2one(comodel_name='sale.order', 'Sale Order')
order_amount = fields.Monetary(string='Order Amount',
store=True,
related='order_id.amount_total')
you must add a Many2one field in the model which relates to the model you want to access a field. in our case model is sale.order.
with the related kwarg you can relate field of the related model defined in the Many2one field. in our case order_id.
Setting the store kwarg will automatically store the value in database. With new API the value of the related field will be automatically updated.(Reference)
Hope this helps!

Shall I record the user name who modify a certain field by Odoo?

.py file:
….
namex=fields.Text()
moifier=fields.Many2one(‘res.users’, string=”Modifier”)
…
When some user modify “namex”, his/her name should be recorded on field “modifier” automatically; what code should I make? I try “onchange/depends”, but failed; maybe modifier could be a “text field/ char field”?
in addition, shall I set "access_rule" to set users just see the records created by the members in his/her own group?
Odoo already has that for you. Every model has those fields, which are automatically created and updated each time you create, or write:
create_date (datetime): when record is created
create_uid (many2one): user who created this record
write_date (datetime): last time record is updated
write_uid (many2one): last user updated this record
Go to Settings > Technical > Database Structure > Models for more details.
While Odoo will keep for you a track of the last user which has modified a record, a modifier per field is not kept. I can see the interest of such a functionality in many cases.
To do that for a particular model one possibility is to redefine the write method of this model. In your .py file you may want to add something like this:
#api.model
def write(self):
if self.namex in values:
values.update({'modifier': uid})
super().write(cr, uid, ids, values, context)
Another way to do that in a more flexible way is to use the #onchange decorator:
#onchange('your_sensible_field_name'):
def set_modifier(self):
self.modifer = self.env.user
You may also want to take a look at the #depends decorator.