Files
manufacture/mrp_account_analytic/models/mrp_workorder.py
Daniel Reis 8695ca8897 [ADD] mrp_account_analytic: generate Analytic Items for manufacturing consumptions.
Extracted from the original proposed code for mrp_account_analytic_wip
2021-08-30 15:12:34 +01:00

47 lines
1.6 KiB
Python

# Copyright (C) 2021 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, fields, models
class MrpWorkcenterProductivity(models.Model):
_inherit = "mrp.workcenter.productivity"
def _prepare_mrp_workorder_analytic_item(self):
"""
Prepare additional values for Analytic Items created.
For compatibility with analytic_activity_cost
"""
self.ensure_one()
return {
"name": "{} / {}".format(self.production_id.name, self.workorder_id.name),
"account_id": self.production_id.analytic_account_id.id,
"date": fields.Date.today(),
"company_id": self.company_id.id,
"manufacturing_order_id": self.production_id.id,
"workorder_id": self.workorder_id.id,
"unit_amount": self.duration / 60, # convert minutes to hours
"amount": -self.duration / 60 * self.workcenter_id.costs_hour,
}
def generate_mrp_work_analytic_line(self):
AnalyticLine = self.env["account.analytic.line"].sudo()
for timelog in self:
line_vals = timelog._prepare_mrp_workorder_analytic_item()
analytic_line = AnalyticLine.create(line_vals)
analytic_line.on_change_unit_amount()
@api.model
def create(self, vals):
timelog = super().create(vals)
if vals.get("date_end"):
timelog.generate_mrp_work_analytic_line()
return timelog
def write(self, vals):
if vals.get("date_end"):
self.generate_mrp_work_analytic_line()
res = super().write(vals)
return res