How to pass new field value from sale order line to invoice lines while using down payment in odoo 15 - odoo-15

How to pass new field value from sale order line to invoice lines while using down payment in odoo 15

Usefull basics:
The model sale.order.line is related to the model account.move.line through this field:
invoice_lines: list of invoice lines (account.move.line) linked to this sale.order.line
And the corresponding relational field in account.move.line to sale.order.line is:
sale_line_ids: list of sale order lines (sale.order.line) linked to this account.move.line
1. Create a new field
To create a new field in the model you want: (App) Configuration > (Menu) Technicals > Fields
Create new field in the model you want (account.invoice.line ?)
Example : mynewfield
2. Create an automated Action
Create an automated action using (App) Configuration > (Menu) Technicals > automated Actions
Or: can be found here : /web?debug=1#id=&action=435&model=base.automation)
Create a new one, on the model sale.order.line
Action: execute python code
#Update invoice lines corresponding to the current sale.order.line-record:
for invoice_line_record in record.invoice_lines:
invoice_line_record['mynewfield'] = record['invoiced']

Related

How to store relational fields to the database?

I've been searching for this for a week now, and still don't find any satisfying answer. If we make any type of relationship to other model in Odoo (in any framework as well) it won't store the data inside it, it will only store its id. Now when the related model get changed, the change will also true for all the child models that's inheriting it.
I want just like in the Sale module, when I change any product in Products model, it doesn't change the same product that's already stored in the orders. Please, any help, I'm very new here to Odoo I used to develop with Java.
An order correspond to the model named sale.order which is in one2many relationship with the model sale.order.line (SOL). one SOL has it s OWN fields for price, vat... which are computed bases on the current state of product at the time of the customer order. That's why the order and its SOL are not updated when the corresponding product attributes (price...) are changed...
In Odoo : an inherited python class EXTENDS the original class (similar to java extends) : in the sale_coupon module, the model located at src/odoo/addons/sale_coupon/models/sale_order.py is :
class SaleOrder(models.Model):
_inherit = "sale.order"
extends the source class SaleOrder defined in the module sale : src/odoo/addons/sale/models/sale.py:
class SaleOrder(models.Model):
name = "sale.order"
That's mean that the inherited class SaleOrder (_inherit) gets the specific attributes (fields) and methods (def) provided and defined in its source(s) class(es) : saleOrder :
origin = fields.Char(string='Source Document', help="Reference of the document that generated this sales order request.")
def _amount_all(self):
""" Compute the total amounts of the SO."""
for order in self:
amount_untaxed = amount_tax = 0.0
for line in order.order_line:
amount_untaxed += line.price_subtotal
amount_tax += line.price_tax
...
And you can add new fields and new methods in your inherited class SaleOrder scope :
reward_amount = fields.Float(compute='_compute_reward_total')
def action_confirm(self):
self.generated_coupon_ids.write({'state': 'new'})
...
return super(SaleOrder, self).action_confirm()
But, you don t need to instantiate theses Classes in your code (you don t need to create yourself Objects).
Theses classes have the basic CRUD-methods provided from models.Model: def create(), def read(), def write(), def unlink()
The source and the inherited class(es) both are connected to the same database-table : sale_order
So one Class-record (python in Odoo) :
self.env['sale.order'].browse(12)
corresponds to one record in the database-table : sale_order
Theses classes (SaleOrder) have the CRUD-methods from model.Model : def create(), def read(), def write(), def unlink() and they can override them in their own scope, where you can optionally call the "parent"-def : super(SaleOrder, self).unlink() :
def unlink(self):
for order in self:
if order.state not in ('draft', 'cancel'):
raise UserError(_('You can not delete a sent quotation or a confirmed sales order. You must first cancel it.'))
return super(SaleOrder, self).unlink()
Relations betweens models are defined using fields :
A relation between the sale.order model and the res.partner model (contact) is created using fields.Many2one:
class SaleOrder(models.Model):
...
partner_id = fields.Many2one('res.partner')
which is reflected in the corresponding database-table.
First of all, If you change the product details It will these details will change accordingly in the sale order after you refresh, about store the id of relation field its the standard way to use,
but there Is another way I think this is the way you looking for which Is store data as a JSON in column with type JSON as an example for sale order line table if you want to store the product as a JSON column it will be like below:
[{"id": 1, "name": "product 1"},]
of course, there Is a way to read and update and create the data into a JSON column you can read about It.
one more way as an example if you want to store the product to the current record of relation table not just store the Id and query to get the result you can just create new fields for data you want to store like if you want to store the name and price with name and id then you must add fields for this date and when you create in order line rather than add just product Id as a relation just add the product data to the new fields you have added before but this is not a good way.
I hope I understand your question and this answer helps you.

how to get only child name not with parent name in many2many field with many2many_tags widget odoo?

I have the Customer field in the Sale Order form.
I have added a new Many2many field with many2many_tags for Child Contacts of Customers in the Sale order form.
Now when I change Customer this new field fills with its child contacts.
But it comes with a Customer name.
I want to display only the name of child contacts.
Thanks in Advance.
You should inherit the name_get method like below.
#api.multi
def name_get(self):
if not self._context.get('ADD_CONTEXT_ON_SALEORDER_ACTION_AND_ALSO_ON_FIELD'):
return super(ResPartner, self).name_get()
res = []
for partner in self:
res.append((partner.id, partner.name))
return res
as per above code you also you need to add context in sale order action and on that many2many field too.
The values are coming from js file of many2many_tags
In this it shows values of display_name.
You can change the code as per your need.

Odoo How to create a new model for Product Master with all the data in the product master

I want to create a separate view for Product Master.I created a new model and tried like this.But when I checked in database no data is present in my new model.
Code
class QuotationCreation(models.Model):
_name='quotation.creation'
xn_product_id = fields.Many2one('product.template')
product=fields.Char(related = 'xn_product_id.name',string='Product')
How can I tranfer all the data from product master to this model.
I want to create a new model with existing data.How can I do that ?
Thanks in Advance
For populating your new model with your existing product.template table records, you have to run a for loop in your odoo shell, because this are existing data that you cannot fire any event on create method. For example:
ProductTemplates = env['product.template'].search([])
for pt in ProductTemplates:
env['quotation.creation'].create({'xn_product_id': pt.id})
env.cr.commit()
OR you can even export all database id from product template list view and import that on quotation.creation list view with no other field which will create all the records in your new table.
For future records, you can just inherit product.template models create() method and create a corresponding quotation.creation record in it.

How to add unique Item Code every product in open erp

I'm adding a new product to the open erp. However, I'm unable to add a unique number to each product. But there should be a unique product number for every product.
You can use #api.constrains decorator.
#api.one
#api.constrains('code')
def _unique_code(self):
if len(self.search([('code', '=', self.code)])) > 1:
raise ValidationError("Product code must be unique!")
Id is the always unique for all model in openerp.
And another way,
you can also add your custome field for unique number usind "_sql_constraints"
eg: _sql_constraints = [
('seq_uniq', 'unique (item_code)', _("The Item Code must be \
unique per Stage!"))]

Openerp 7 many2one dropdown should display field of related record

If you install Openerp 7 with recruitment module. And create a simple entry with following values e.g.
Subject (internal field name = 'name') = 10 Year Experience
Applicant Name = Jhon Smith
Then if you create a custom module with following columns
_columns = {
'applicant_id': fields.many2one('hr.applicant', 'Applicant', required=True),
}
The view widget by default will show a drop-down with the Subject (internal field name ='name') field but i want to show applicant name (internal field name='partner_name') field in drop down, when creating a new record in my custom module.
In Summary how can I display Applicant's Name instead of Subject in drop-down widget in my custom module.
In openerp there is a function called name_get().This function returns a list of tuples containing ID of the record and name tobe displayed. So override this function and return list of tuples containing ID of the record and applicant name
You need to define applicant_id in _rec_name in your custom module.
Try this:
_rec_name = 'applicant_id'
Have a look at Predefined fields.