diff --git a/i18n/es_AR.po b/i18n/es_AR.po index 12bd1685f9def35b7adb7a996282b14908a5e4c9..c81519ebf3fe0f677435c756788305cbf7da8ed8 100644 --- a/i18n/es_AR.po +++ b/i18n/es_AR.po @@ -186,7 +186,7 @@ msgstr "Última actualización" #. module: premin_purchase_custom #: model:ir.model.fields,field_description:premin_purchase_custom.field_supplies_cost__last_update_date msgid "Last Update Date" -msgstr "Ultima Actualización" +msgstr "Ult. Act." #. module: premin_purchase_custom #: model:ir.model.fields,field_description:premin_purchase_custom.field_supplies_cost__last_update_price @@ -196,7 +196,7 @@ msgstr "$Ant" #. module: premin_purchase_custom #: model:ir.model.fields,field_description:premin_purchase_custom.field_product_supplierinfo__last_updated_date msgid "Last Updated" -msgstr "Ultima Actualizaicon" +msgstr "Ult. Act." #. module: premin_purchase_custom #: model:ir.model.fields,field_description:premin_purchase_custom.field_freight_types__write_uid diff --git a/models/supplies_cost.py b/models/supplies_cost.py index 9e68a7fb8af62419d19e4d6c5ccd8cb8c31736ae..fe22cbf3ec966a09c8de3a38989f663e5a5e4fe3 100644 --- a/models/supplies_cost.py +++ b/models/supplies_cost.py @@ -24,34 +24,53 @@ class SuppliesCost(models.Model): calculated_cost = fields.Float( "$/Kg o Un", compute="_compute_calculated_cost", store=True ) + price = fields.Float(required=True) - uom_id = fields.Many2one("uom.uom") + uom_id = fields.Many2one("uom.uom", related="product_id.uom_id", readonly=True) currency_id = fields.Many2one( "res.currency", required=True, default=lambda self: self.env.ref("base.ARS") ) observations = fields.Text() - category_id = fields.Many2one("product.category") - freight_id = fields.Many2one("freight.types") + category_id = fields.Many2one("product.category", related="product_id.categ_id") + freight_id = fields.Many2one( + "freight.types", related="product_id.freight_type", store=True + ) freight_plus_others = fields.Float() - cost_lost = fields.Float() - partner_ids = fields.Many2one("res.partner") + cost_lost = fields.Float(compute="_compute_cost_lost", store=True) + partner_ids = fields.Many2many("res.partner") last_update_price = fields.Float() backup_last_price = fields.Float() variation_percentage = fields.Float() - @api.depends( - "price", "cost_lost", "currency_id", "freight_plus_others", "freight_id" - ) + @api.depends("product_id") + def _compute_cost_lost(self): + if self.product_id: + self.cost_lost = self.product_id.lost_percentage * 100 + + @api.depends("price", "cost_lost", "currency_id", "freight_plus_others") def _compute_calculated_cost(self): + """Computes calculated_cost field. + Through converted_price it takes the inputted price and converts it to + ARS currency before doing any other operation. + + Same case with freight type. Freight can be expressed in USD currency, so before + is used in any calculations it's converted to ARS. + + Finally it updates product_id.standard_price and product_id_latest_cost_update. + """ for record in self: - record.last_update_price = record.backup_last_price if record.product_id: converted_price = record.price / record.currency_id.rate price_per_kg = (converted_price / record.uom_id.factor_inv) + ( record.freight_plus_others ) * (1 + record.cost_lost / 100) + if ( + record.calculated_cost != price_per_kg + and record.calculated_cost > 0 + ): + record.last_update_price = record.calculated_cost record.calculated_cost = price_per_kg - record.backup_last_price = price_per_kg + # record.backup_last_price = price_per_kg if record.last_update_price > 0: variation = ( (price_per_kg - record.last_update_price) @@ -70,8 +89,8 @@ class SuppliesCost(models.Model): @api.onchange("freight_id") def onchange_freight_type(self): - """Get the value of freight type.""" - if self.product_id: + """Get the value of freight type and converts it to ARS.""" + if self.product_id and self.product_id.freight_type: # Price is transformed to company currency before setting it. self.freight_plus_others = ( self.freight_id.price / self.freight_id.currency_id.rate @@ -79,11 +98,11 @@ class SuppliesCost(models.Model): @api.onchange("product_id") def onchange_product_id(self): + """On product selection, freight_plus_other is populated with + product_id.freight_type if it exists. Before populating it, it's converted + to ARS currency. + """ if self.product_id: - self.uom_id = self.product_id.uom_id.id - self.category_id = self.product_id.categ_id.id - self.freight_id = self.product_id.freight_type.id - self.cost_lost = self.product_id.lost_percentage * 100 if not self.product_id.freight_type: self.freight_plus_others = 0 else: diff --git a/views/supplies_cost.xml b/views/supplies_cost.xml index 3dd5ba2f2ccd71c75d732fde39b3c360eb5d3523..8027a7707328dff2b5673a3b1a405c5a744ca589 100644 --- a/views/supplies_cost.xml +++ b/views/supplies_cost.xml @@ -18,14 +18,14 @@ <field name="calculated_cost"/> <field name="price" widget="monetary" options="{'currency_field': 'currency_id'}"/> <field name="uom_id"/> - <field name="observations"/> - <field name="category_id"/> + <field name="observations" optional="show"/> + <field name="category_id" optional="show"/> <field name="freight_id"/> - <field name="freight_plus_others"/> - <field name="cost_lost"/> - <field name="partner_ids"/> - <field name="last_update_price"/> - <field name="variation_percentage"/> + <field name="freight_plus_others" optional="show"/> + <field name="cost_lost" optional="show"/> + <field name="partner_ids" widget="many2many_tags" optional="show"/> + <field name="last_update_price" optional="show" readonly="1"/> + <field name="variation_percentage" optional="show" readonly="1"/> </tree> </field> </record>