summaryrefslogtreecommitdiff
path: root/addons/purchase_mrp/models
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/purchase_mrp/models
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/purchase_mrp/models')
-rw-r--r--addons/purchase_mrp/models/__init__.py6
-rw-r--r--addons/purchase_mrp/models/mrp_production.py45
-rw-r--r--addons/purchase_mrp/models/purchase.py62
-rw-r--r--addons/purchase_mrp/models/stock_move.py14
4 files changed, 127 insertions, 0 deletions
diff --git a/addons/purchase_mrp/models/__init__.py b/addons/purchase_mrp/models/__init__.py
new file mode 100644
index 00000000..dd92304f
--- /dev/null
+++ b/addons/purchase_mrp/models/__init__.py
@@ -0,0 +1,6 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from . import purchase
+from . import mrp_production
+from . import stock_move
diff --git a/addons/purchase_mrp/models/mrp_production.py b/addons/purchase_mrp/models/mrp_production.py
new file mode 100644
index 00000000..c979c4ad
--- /dev/null
+++ b/addons/purchase_mrp/models/mrp_production.py
@@ -0,0 +1,45 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import api, fields, models, _
+
+
+class MrpProduction(models.Model):
+ _inherit = 'mrp.production'
+
+ purchase_order_count = fields.Integer(
+ "Count of generated PO",
+ compute='_compute_purchase_order_count',
+ groups='purchase.group_purchase_user')
+
+ @api.depends('procurement_group_id.stock_move_ids.created_purchase_line_id.order_id', 'procurement_group_id.stock_move_ids.move_orig_ids.purchase_line_id.order_id')
+ def _compute_purchase_order_count(self):
+ for production in self:
+ production.purchase_order_count = len(production.procurement_group_id.stock_move_ids.created_purchase_line_id.order_id |
+ production.procurement_group_id.stock_move_ids.move_orig_ids.purchase_line_id.order_id)
+
+ def action_view_purchase_orders(self):
+ self.ensure_one()
+ purchase_order_ids = (self.procurement_group_id.stock_move_ids.created_purchase_line_id.order_id | self.procurement_group_id.stock_move_ids.move_orig_ids.purchase_line_id.order_id).ids
+ action = {
+ 'res_model': 'purchase.order',
+ 'type': 'ir.actions.act_window',
+ }
+ if len(purchase_order_ids) == 1:
+ action.update({
+ 'view_mode': 'form',
+ 'res_id': purchase_order_ids[0],
+ })
+ else:
+ action.update({
+ 'name': _("Purchase Order generated from %s", self.name),
+ 'domain': [('id', 'in', purchase_order_ids)],
+ 'view_mode': 'tree,form',
+ })
+ return action
+
+ def _get_document_iterate_key(self, move_raw_id):
+ iterate_key = super(MrpProduction, self)._get_document_iterate_key(move_raw_id)
+ if not iterate_key and move_raw_id.created_purchase_line_id:
+ iterate_key = 'created_purchase_line_id'
+ return iterate_key
diff --git a/addons/purchase_mrp/models/purchase.py b/addons/purchase_mrp/models/purchase.py
new file mode 100644
index 00000000..6256516c
--- /dev/null
+++ b/addons/purchase_mrp/models/purchase.py
@@ -0,0 +1,62 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import api, fields, models, _
+
+
+class PurchaseOrder(models.Model):
+ _inherit = 'purchase.order'
+
+ mrp_production_count = fields.Integer(
+ "Count of MO Source",
+ compute='_compute_mrp_production_count',
+ groups='mrp.group_mrp_user')
+
+ @api.depends('order_line.move_dest_ids.group_id.mrp_production_ids')
+ def _compute_mrp_production_count(self):
+ for purchase in self:
+ purchase.mrp_production_count = len(purchase.order_line.move_dest_ids.group_id.mrp_production_ids |
+ purchase.order_line.move_ids.move_dest_ids.group_id.mrp_production_ids)
+
+ def action_view_mrp_productions(self):
+ self.ensure_one()
+ mrp_production_ids = (self.order_line.move_dest_ids.group_id.mrp_production_ids | self.order_line.move_ids.move_dest_ids.group_id.mrp_production_ids).ids
+ action = {
+ 'res_model': 'mrp.production',
+ 'type': 'ir.actions.act_window',
+ }
+ if len(mrp_production_ids) == 1:
+ action.update({
+ 'view_mode': 'form',
+ 'res_id': mrp_production_ids[0],
+ })
+ else:
+ action.update({
+ 'name': _("Manufacturing Source of %s", self.name),
+ 'domain': [('id', 'in', mrp_production_ids)],
+ 'view_mode': 'tree,form',
+ })
+ return action
+
+
+class PurchaseOrderLine(models.Model):
+ _inherit = 'purchase.order.line'
+
+ def _compute_qty_received(self):
+ kit_lines = self.env['purchase.order.line']
+ for line in self:
+ if line.qty_received_method == 'stock_moves' and line.move_ids:
+ kit_bom = self.env['mrp.bom']._bom_find(product=line.product_id, company_id=line.company_id.id, bom_type='phantom')
+ if kit_bom:
+ moves = line.move_ids.filtered(lambda m: m.state == 'done' and not m.scrapped)
+ order_qty = line.product_uom._compute_quantity(line.product_uom_qty, kit_bom.product_uom_id)
+ filters = {
+ 'incoming_moves': lambda m: m.location_id.usage == 'supplier' and (not m.origin_returned_move_id or (m.origin_returned_move_id and m.to_refund)),
+ 'outgoing_moves': lambda m: m.location_id.usage != 'supplier' and m.to_refund
+ }
+ line.qty_received = moves._compute_kit_quantities(line.product_id, order_qty, kit_bom, filters)
+ kit_lines += line
+ super(PurchaseOrderLine, self - kit_lines)._compute_qty_received()
+
+ def _get_upstream_documents_and_responsibles(self, visited):
+ return [(self.order_id, self.order_id.user_id, visited)]
diff --git a/addons/purchase_mrp/models/stock_move.py b/addons/purchase_mrp/models/stock_move.py
new file mode 100644
index 00000000..849a2354
--- /dev/null
+++ b/addons/purchase_mrp/models/stock_move.py
@@ -0,0 +1,14 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import models
+
+
+class StockMove(models.Model):
+ _inherit = 'stock.move'
+
+ def _prepare_phantom_move_values(self, bom_line, product_qty, quantity_done):
+ vals = super(StockMove, self)._prepare_phantom_move_values(bom_line, product_qty, quantity_done)
+ if self.purchase_line_id:
+ vals['purchase_line_id'] = self.purchase_line_id.id
+ return vals