diff --git a/mrp_bom_price_and_qty/models/__init__.py b/mrp_bom_price_and_qty/models/__init__.py index f67479175..ea9f9bb3f 100644 --- a/mrp_bom_price_and_qty/models/__init__.py +++ b/mrp_bom_price_and_qty/models/__init__.py @@ -1 +1,2 @@ -from . import mrp_bom_line_extension +from . import mrp_report_bom_structure +from . import uom_uom diff --git a/mrp_bom_price_and_qty/models/mrp_bom_line_extension.py b/mrp_bom_price_and_qty/models/mrp_bom_line_extension.py deleted file mode 100644 index 39400d327..000000000 --- a/mrp_bom_price_and_qty/models/mrp_bom_line_extension.py +++ /dev/null @@ -1,16 +0,0 @@ -from odoo import models, fields, api -import requests - -class MrpBomLineExtension(models.Model): - _inherit = 'mrp.bom.line' - - prod_price = fields.Float(string='Product Price', compute='_compute_prod_price') - - @api.depends('product_id') - def _compute_prod_price(self): - for line in self: - if line.product_id: - response = requests.get(f'http://example.com/api/product_price/{line.product_id.id}') - if response.status_code == 200: - price = response.json().get('price', 0.0) - line.prod_price = round(price, 4) diff --git a/mrp_bom_price_and_qty/models/mrp_report_bom_structure.py b/mrp_bom_price_and_qty/models/mrp_report_bom_structure.py new file mode 100644 index 000000000..3446bc29b --- /dev/null +++ b/mrp_bom_price_and_qty/models/mrp_report_bom_structure.py @@ -0,0 +1,81 @@ +from odoo import models +from odoo.tools import float_round + +class ReportBomStructurePrecision(models.AbstractModel): + _inherit = 'report.mrp.report_bom_structure' + + def _get_bom(self, bom_id=False, product_id=False, line_qty=False, line_id=False, level=False): + bom = self.env['mrp.bom'].browse(bom_id) + company = bom.company_id or self.env.company + bom_quantity = line_qty + if line_id: + current_line = self.env['mrp.bom.line'].browse(int(line_id)) + bom_quantity = current_line.product_uom_id._compute_quantity(line_qty, bom.product_uom_id) or 0 + # Display bom components for current selected product variant + if product_id: + product = self.env['product.product'].browse(int(product_id)) + else: + product = bom.product_id or bom.product_tmpl_id.product_variant_id + if product: + price = product.uom_id._compute_price(product.with_company(company).standard_price, bom.product_uom_id) * bom_quantity + attachments = self.env['mrp.document'].search(['|', '&', ('res_model', '=', 'product.product'), + ('res_id', '=', product.id), '&', ('res_model', '=', 'product.template'), ('res_id', '=', product.product_tmpl_id.id)]) + else: + # Use the product template instead of the variant + price = bom.product_tmpl_id.uom_id._compute_price(bom.product_tmpl_id.with_company(company).standard_price, bom.product_uom_id) * bom_quantity + attachments = self.env['mrp.document'].search([('res_model', '=', 'product.template'), ('res_id', '=', bom.product_tmpl_id.id)]) + operations = self._get_operation_line(bom, float_round(bom_quantity, precision_rounding=1, rounding_method='UP'), 0) + lines = { + 'bom': bom, + 'bom_qty': bom_quantity, + 'bom_prod_name': product.display_name, + 'currency': company.currency_id, + 'product': product, + 'code': bom and bom.display_name or '', + 'price': round(price, 4), # Set precision to 4 decimals for price + 'total': sum([op['total'] for op in operations]), + 'level': level or 0, + 'operations': operations, + 'operations_cost': sum([op['total'] for op in operations]), + 'attachments': attachments, + 'operations_time': sum([op['duration_expected'] for op in operations]) + } + components, total = self._get_bom_lines(bom, bom_quantity, product, line_id, level) + lines['components'] = components + lines['total'] += total + return lines + + def _get_bom_lines(self, bom, bom_quantity, product, line_id, level): + components = [] + total = 0 + for line in bom.bom_line_ids: + line_quantity = (bom_quantity / (bom.product_qty or 1.0)) * line.product_qty + if line._skip_bom_line(product): + continue + company = bom.company_id or self.env.company + price = line.product_id.uom_id._compute_price(line.product_id.with_company(company).standard_price, line.product_uom_id) * line_quantity + if line.child_bom_id: + factor = line.product_uom_id._compute_quantity(line_quantity, line.child_bom_id.product_uom_id) + sub_total = self._get_price(line.child_bom_id, factor, line.product_id) + else: + sub_total = price + sub_total = self.env.company.currency_id.round(sub_total) + components.append({ + 'prod_id': line.product_id.id, + 'prod_name': line.product_id.display_name, + 'code': line.child_bom_id and line.child_bom_id.display_name or '', + 'prod_qty': line_quantity, + 'prod_uom': line.product_uom_id.name, + 'prod_cost': round(price, 4), # Set precision to 4 decimals for price + 'parent_id': bom.id, + 'line_id': line.id, + 'level': level or 0, + 'total': sub_total, + 'child_bom': line.child_bom_id.id, + 'phantom_bom': line.child_bom_id and line.child_bom_id.type == 'phantom' or False, + 'attachments': self.env['mrp.document'].search(['|', '&', + ('res_model', '=', 'product.product'), ('res_id', '=', line.product_id.id), '&', ('res_model', '=', 'product.template'), ('res_id', '=', line.product_id.product_tmpl_id.id)]), + + }) + total += sub_total + return components, total diff --git a/mrp_bom_price_and_qty/models/uom_uom.py b/mrp_bom_price_and_qty/models/uom_uom.py new file mode 100644 index 000000000..f41729552 --- /dev/null +++ b/mrp_bom_price_and_qty/models/uom_uom.py @@ -0,0 +1,23 @@ +from odoo import models + +class UomUom(models.Model): + _inherit = 'uom.uom' + + def _compute_price(self, price, to_unit): + self.ensure_one() + + # Establecer el atributo rounding a 0.001 + self.rounding = 0.001 + to_unit.rounding = 0.001 + + + if not self or not price or not to_unit or self == to_unit: + return price + if self.category_id.id != to_unit.category_id.id: + return price + + + amount = price * self.factor + if to_unit: + amount = amount / to_unit.factor + return amount diff --git a/mrp_bom_price_and_qty/views/mrp_bom_line_templates.xml b/mrp_bom_price_and_qty/views/mrp_bom_line_templates.xml index eef7070a7..90c1ada62 100644 --- a/mrp_bom_price_and_qty/views/mrp_bom_line_templates.xml +++ b/mrp_bom_price_and_qty/views/mrp_bom_line_templates.xml @@ -1,16 +1,128 @@ -