Merge PR #588 into 14.0

Signed-off-by dreispt
This commit is contained in:
OCA-git-bot
2021-02-08 13:43:31 +00:00
7 changed files with 78 additions and 52 deletions

View File

@@ -10,7 +10,7 @@
"author": "Open Source Integrators, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/manufacture",
"license": "AGPL-3",
"depends": ["mrp_analytic", "account"],
"depends": ["mrp_analytic"],
"data": ["views/account_analytic_line_view.xml", "views/mrp_production_views.xml"],
"maintainer": "dreispt",
"development_status": "Alpha",

View File

@@ -3,3 +3,4 @@
from . import account_analytic_line
from . import mrp_production
from . import stock_move

View File

@@ -15,8 +15,3 @@ class AccountAnalyticLine(models.Model):
"mrp.production",
string="Related Manufacturing Order",
)
product_category_id = fields.Many2one(
related="product_id.categ_id",
string="Product Category",
store=True,
)

View File

@@ -7,38 +7,6 @@ from odoo import models
class MrpProduction(models.Model):
_inherit = "mrp.production"
def _prepare_material_analytic_line(self):
self.ensure_one()
order = self.raw_material_production_id
return {
"name": self.product_id.default_code or "",
"account_id": order.analytic_account_id.id or False,
"ref": order.name,
"unit_amount": self.product_uom_qty,
"company_id": order.company_id.id,
"manufacturing_order_id": order.id,
"product_id": self.product_id.id or False,
"stock_move_id": self.id,
}
def generate_analytic_line(self):
"""Generate Analytic Lines Manually."""
# FIXME: this is a placeholder for final logic
# TODO: when should Analytic Items generation be triggered?
# TODO: what to do if prevous items were already generated?
AnalyticLine = self.env["account.analytic.line"].sudo()
order_raw_moves = self.mapped("move_raw_ids")
existing_items = AnalyticLine.search(
[("stock_move_id ", "in", order_raw_moves.ids)]
)
for order in self.filtered("analytic_account_id"):
for line in order.move_raw_ids:
line_vals = line._prepare_material_analytic_line()
if line in existing_items:
analytic_line = existing_items.filter(
lambda x: x.stock_move_id == line
)
analytic_line.write(line_vals)
else:
analytic_line = AnalyticLine.create(line_vals)
analytic_line.on_change_unit_amount()
def generate_analytic_lines(self):
""" Generate Analytic Lines for a MO """
return self.mapped("move_raw_ids").generate_mrp_raw_analytic_line()

View File

@@ -0,0 +1,68 @@
# Copyright (C) 2021 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, models
class StockMove(models.Model):
_inherit = "stock.move"
def _prepare_mrp_raw_material_analytic_line(self):
self.ensure_one()
move = self
mrp_order = move.raw_material_production_id
return {
"date": move.date,
"name": "{} / {}".format(mrp_order.name, move.product_id.display_name),
"ref": mrp_order.name,
"account_id": mrp_order.analytic_account_id.id,
"manufacturing_order_id": mrp_order.id,
"company_id": mrp_order.company_id.id,
"stock_move_id": move.id,
"product_id": move.product_id.id,
"unit_amount": move.quantity_done,
}
def generate_mrp_raw_analytic_line(self):
"""Generate Analytic Lines"""
# FIXME: consumed products coming from child MOs
# should not generate Analytic Items, to avoid duplicating costs!
AnalyticLine = self.env["account.analytic.line"].sudo()
existing_items = AnalyticLine.search([("stock_move_id", "in", self.ids)])
for move in self.filtered("raw_material_production_id.analytic_account_id"):
line_vals = move._prepare_mrp_raw_material_analytic_line()
if move in existing_items.mapped("stock_move_id"):
analytic_line = existing_items.filtered(
lambda x: x.stock_move_id == move
)
analytic_line.write(line_vals)
analytic_line.on_change_unit_amount()
elif line_vals.get("unit_amount"):
analytic_line = AnalyticLine.create(line_vals)
analytic_line.on_change_unit_amount()
def write(self, vals):
""" When material is consumed, generate Analytic Items """
res = super().write(vals)
if vals.get("qty_done"):
self.generate_mrp_raw_analytic_line()
return res
class StockMoveLine(models.Model):
_inherit = "stock.move.line"
def write(self, vals):
qty_done = vals.get("qty_done")
res = super().write(vals)
if qty_done:
self.mapped("move_id").generate_mrp_raw_analytic_line()
return res
@api.model
def create(self, vals):
qty_done = vals.get("qty_done")
res = super().create(vals)
if qty_done:
res.mapped("move_id").generate_mrp_raw_analytic_line()
return res

View File

@@ -9,7 +9,6 @@
<group name="manufacture" string="Manufacture">
<field name="stock_move_id" />
<field name="manufacturing_order_id" />
<field name="product_category_id" />
</group>
</group>
</field>
@@ -31,12 +30,6 @@
domain="[]"
context="{'group_by': 'manufacturing_order_id'}"
/>
<filter
string="Product Category"
name="product_category"
domain="[]"
context="{'group_by': 'product_category_id'}"
/>
</filter>
</field>
</record>

View File

@@ -5,12 +5,13 @@
<field name="model">mrp.production</field>
<field name="inherit_id" ref="mrp.mrp_production_form_view" />
<field name="arch" type="xml">
<xpath expr='//button[@name="button_mark_done"]' position='before'>
<xpath expr="//button[@name='action_cancel']" position="before">
<button
name="generate_analytic_line"
class="btn-primary"
name="generate_analytic_lines"
type="object"
string="Generate Material Analytic Items"
icon="fa-cog"
string="Material Costs"
groups="base.group_no_one"
/>
</xpath>
</field>