65 lines
2.6 KiB
Python
65 lines
2.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
from odoo import tools
|
|
from odoo import models, fields
|
|
|
|
class MrpComponentsReport(models.Model):
|
|
_name = 'mrp.components.report'
|
|
_description = 'MRP Components Report'
|
|
_auto = False
|
|
|
|
id = fields.Id(readonly=True)
|
|
date = fields.Date('Date Needed', readonly=True)
|
|
production_id = fields.Many2one('mrp.production', 'Manufacturing Order', readonly=True)
|
|
product_id = fields.Many2one('product.product', 'Parent Product', readonly=True)
|
|
component_id = fields.Many2one('product.product', 'Component', readonly=True)
|
|
workcenter_id = fields.Many2one('mrp.workcenter', 'Work Center', readonly=True)
|
|
product_qty = fields.Float('Quantity Needed', readonly=True)
|
|
product_uom_id = fields.Many2one('uom.uom', 'Unit of Measure', readonly=True)
|
|
state = fields.Selection([
|
|
('draft', 'Draft'),
|
|
('confirmed', 'Confirmed'),
|
|
('progress', 'In Progress'),
|
|
('to_close', 'To Close'),
|
|
('done', 'Done'),
|
|
('cancel', 'Cancelled')], string='MO State', readonly=True)
|
|
|
|
def _select(self):
|
|
return """
|
|
SELECT
|
|
sm.id AS id,
|
|
CAST(mp.date_start AS DATE) AS date,
|
|
mp.id AS production_id,
|
|
mp.product_id AS product_id,
|
|
sm.product_id AS component_id,
|
|
COALESCE(mw.id, (SELECT workcenter_id FROM mrp_workorder WHERE production_id = mp.id ORDER BY id ASC LIMIT 1)) AS workcenter_id,
|
|
sm.product_uom_qty AS product_qty,
|
|
sm.product_uom AS product_uom_id,
|
|
mp.state AS state
|
|
"""
|
|
|
|
def _from(self):
|
|
return """
|
|
FROM stock_move sm
|
|
JOIN mrp_production mp ON sm.raw_material_production_id = mp.id
|
|
LEFT JOIN mrp_bom_line mbl ON sm.bom_line_id = mbl.id
|
|
LEFT JOIN mrp_routing_workcenter mrw ON sm.operation_id = mrw.id
|
|
LEFT JOIN mrp_routing_workcenter mrw_bom ON mbl.operation_id = mrw_bom.id
|
|
LEFT JOIN mrp_workorder mwo ON sm.workorder_id = mwo.id
|
|
LEFT JOIN mrp_workcenter mw ON COALESCE(mrw.workcenter_id, mwo.workcenter_id, mrw_bom.workcenter_id) = mw.id
|
|
"""
|
|
|
|
def _where(self):
|
|
return """
|
|
WHERE sm.raw_material_production_id IS NOT NULL
|
|
AND sm.state != 'cancel'
|
|
AND mp.state != 'cancel'
|
|
"""
|
|
|
|
def init(self):
|
|
tools.drop_view_if_exists(self.env.cr, self._table)
|
|
self.env.cr.execute("""CREATE or REPLACE VIEW %s as (
|
|
%s
|
|
%s
|
|
%s
|
|
)""" % (self._table, self._select(), self._from(), self._where()))
|