Calculate freight total in a computed field on Invoice - Odoo - sum

I am trying to get a freight total to show on the bottom of my invoices. I created a new field in the Accounting Module called "Freight Total" (x_studio_freight_total). I want to get the sum of the "Subtotal" (price_subtotal) for any instance where the "Product" (product_id) = "FREIGHT".
For the field, x_studio_freight_total, I set the Dependencies as: "invoice_line_ids.price_subtotal, invoice_line_ids.product_id"
I set the Compute property as:
for record in self:
if(record.invoice_line_ids.product_id == "FREIGHT):
record['x_studio_freight_total'] = sum(record.invoice_line_ids.price_subtotal)
If an invoice shows a line with a 'Product' named "FREIGHT", and a 'Subtotal' of "12.75" I expect my 'Freight Total' field to display "12.75".
If an invoice has two lines with 'Product' named "FREIGHT, one with a 'Subtotal' of "12.75" and the other with a 'Subtotal' of "7.50", I expect the 'Freight Total' field to display "20.25".
But it currently is not displaying anything just "0.00"

Try using this code
for record in self:
record['x_studio_freight_total'] = 0
for line in record.invoice_line_ids:
if(line.product_id.name == "FREIGHT): #product_id.name to search product name
record['x_studio_freight_total'] += line.price_subtotal`

Related

How to set unit_price automatically as I select product in odoo 15?

I have made one model which is listed below, I want to set the price automatically as I select the product.
class JobCardLine(models.Model):
_name = "job.card.line"
product_id = fields.Many2one('product.template', string="Product", tracking=True)
price = fields.Many2one('product.template.',string="Price", tracking=True)
I think it can be done using depends on onchange but not able to do that.
You can use on_change to automatically set the price to the product list_price
Example:
#api.onchange('product_id')
def _change_price(self):
self.price = self.product_id.list_price
You will need to change the price field type to Float
You can do it using a computed field but you will need to implement the inverse function to allow setting values on the field and the unit price should depend on product price

Odoo - field from sales order to purchase order

I am using Odoo 10. I have a custom field call linear_units in Sales Order. I have make to order ticked and it creates an automatic Purchase order. I would like to include the field linear_units from Sales order to the purchase order. With below code I can select the Sales order but I cant figure out how to add a field.
class PurchaseOrder(models.Model):
_inherit = 'purchase.order'
sale_order_id = fields.Many2one(
'sale.order',
"Sale Order",
help="Reference to Sale Order")
The above code works for selecting a sales order in purchase order. I have a float field in Sales order called linear_units. I need this field to copy to purchase order. I tried below but does not work
class PurchaseOrder(models.Model):
_inherit = 'purchase.order'
linear_units2 = fields.Float("Linear Units")
#api.onchange('product_id','linear_units')
def _onchange_product_qty(self):
if self.product_id:
self.linear_units2 = self.sale.order.linear_units
you can add a related field in the purchase order for linear_units like below
class PurchaseOrder(models.Model):
_inherit = 'purchase.order'
sale_order_id = fields.Many2one('sale.order', "Sale Order", help="Reference to Sale Order")
linear_units = fields.Float(related='sale_order_id.linear_units')
It will fetch the related linear_units value from the selected sale_order_id
hope this helps!
What is the purpose of this field. Is it supposed to be on each order line or is it supposed to be on the sale order as a whole. With the setup you have, you two options: First
sale_order_lines = fields.One2many('sale.order.line', 'Sale Order Lines')
Then from there you can reference your order number and your linear units.
sale_order_id = fields.Many2one('sale.order', related='sale_order_lines.order_id', string='Sale Order')
linear_units2 = fields.Float(related='sale_order_lines.linear_units', string='Linear Units')
and Second:
sale_order_id = fields.Many2one('sale.order', string='Sale Order')
linear_units = fields.Float(related='sale_order_id.sale_order_lines.linear_units', string='Linear units')
Though I'm not entirely certain that the second option will work. If this is the same value on all order lines then I would suggest putting linear_units on sale.order, then if you need it on the order lines you can put a related field on the order lines and then your fields will look like below
class SaleOrder(model.Models):
_inherit='sale.order'
linear_units = fields.Float(string='Linear Units')
class SaleOrderLines(model.Models):
_inherit='sale.order.lines'
linear_units = fields.Float(related='order_id.linear_units', string='Linear Units', readonly=True)
class PurchaseOrder(models.Models):
_inherit='purchase.order'
sale_order_id = fields.Many2one('sale.order', string='Sale Order')
linear_units = fields.Float(related='sale_order_id.linear_units', string='Linear Units', readonly=True)
(I suggest putting the read only on your related fields because if they are changed on your inherited view it will change it for that sale order and all of its relations.)

Odoo v10 display amount in company currency in purchase tree and form view

I am using Odoo v10 .
As we know in purchase.order, there is a base (Monetary) field amount_total which contains value of total amount of a Purchase Order based on (self) currency_id .
Now, I create a new float field home_currency_amount_total in purchase.order .
home_currency_amount_total = fields.Float(string='Total Amount in company currency', store=True)
How can i have a value in this field based on company currency? i.e. I want to have a corresponding value in company base currency and can be used in my tree & form views.
I am new to Odoo and I wonder if there is a "shortcut" (e.g. a built-in compute method) instead of I have to write up related codes.
There is a built-in method for conversion of currency.
Eg.
#api.model
def _compute(self, from_currency, to_currency, from_amount, round=True):
if (to_currency == from_currency):
amount = to_currency.round(from_amount) if round else from_amount
else:
rate = self._get_conversion_rate(from_currency, to_currency)
amount = to_currency.round(from_amount * rate) if round else from_amount * rate
return amount
So, if you want to calculate the conversion you can use this method.
This method takes 3 arguments, first from currency, second to currency and amount which you want to convert as a third argument.
Eg.
self.env['res.currency']._compute(order.currency_id,order.company_id.currency_id,order.amount_total)
Update :
Create you field like this.
home_currency_amount_total = fields.Float(string='Total Amount in company currency', compute="_compute", store=True)
#api.depends('order_lines.price_subtotal','company_id','currency_id')
def _compute(self);
for order in self:
home_currency_amount_total = self.env['res.currency']._compute(order.currency_id,order.company_id.currency_id,order.amount_total)
You can do it using following method.
class PurchaseOrder(models.Model):
_inherit = "purchase.order"
#api.multi
#api.depends('amount_total')
def get_amount_in_company_currency(self):
for purchase_order in self:
if purchase_order.currency_id.id!=purchase_order.company_id.currency_id.id:
currency_id = purchase_order.currency_id.with_context(date=purchase_order.date_order)
purchase_order.home_currency_amount_total = currency_id.compute(purchase_order.amount_total, purchase_order.company_id.currency_id)
else:
purchase_order.home_currency_amount_total=purchase_order.amount_total
home_currency_amount_total = fields.Float(string='Total Amount in company currency',compute="get_amount_in_company_currency",store=True)
In above code we have create one compute field store True, it means value will be store in the database.
When amount_total will change at that time system will calculate home currency amount.
In method we have checked if company currency & purchase order currency is different then system will compute currency amount.
In odoo base module on method is available for compute currency, in which you can pass date in the context.
purchase_order.currency_id.with_context(date=purchase_order.date_order)
Based on context date system will take currency rate, if you not pass
any date then system will take current date rate.
This will help you.

Odoo- How load products with multiple parents

I am working on Odoo 10e . I have situation which i am unable to solve in here.
I have a relation like following
Customer 1-------* Shipments 1-------* Shipment Detail 1-----* Products
Now i have a separate form in which i want to show products which are associated against a specific Customer in dropdown . How can i do this in Odoo
Do you mean you want to sort all products that have related to the Customer who selected in form view?
You can do this way:
#api.depends('customer')
def get_related_product(self):
res = []
#compute to get your product id here
return res
customer = fields.Many2one(....)
related_product = fields.Many2many(......., compute='get_related_product')

Display Product Combinations in Prestashop Customer View

I am trying to get the product combinations displayed in Prestashop admin Customer detail view in addition to the products that are displayed for the customer.
This seems to be the relevant call from AdminCustomersController.php in public form renderForm():
$products = $customer->getBoughtProducts();
Then in the Customer class I found the method:
public function getBoughtProducts()
{
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT * FROM `'._DB_PREFIX_.'orders` o
LEFT JOIN `'._DB_PREFIX_.'order_detail` od ON o.id_order = od.id_order
WHERE o.valid = 1 AND o.`id_customer` = '.(int)$this->id);
}
How can I modify this method to show the product combination alongside the product name?
I am using Prestashop version 1.6.0.9.
you can get it using 2 ways:
order_detail table already have field 'product_name' that contains value like 'Product Name - Combination', so you can use $products['product_name'] in that case.
or
if for some reason it is not good for you, same table contains also product_attribute_id field, it is combination id, so:
$combination = new Combination($product['product_attribute_id']);
$attributes = $combination->getAttributesName($id_lang);
var_dump($attributes);
will give you array of attributes that current combination contains.