Skip to content

Instantly share code, notes, and snippets.

@blaggacao
Last active November 19, 2017 20:29
Show Gist options
  • Save blaggacao/c89b5b6119c1bc400ceff05f69e71765 to your computer and use it in GitHub Desktop.
Save blaggacao/c89b5b6119c1bc400ceff05f69e71765 to your computer and use it in GitHub Desktop.
Maquila for third party
# -*- coding: utf-8 -*-
# Copyright 2017, XOE Corp.
# XOE Enterprise Edition License v1.0.
from odoo import models, fields, api, _ # noqa
class MrpSettings(models.TransientModel):
_inherit = 'mrp.config.settings'
group_mrp_maquila = fields.Boolean('Manage maquila for third parties', implied_group='mrp_maquila.group_mrp_maquila')
class ProductTemplate(models.Model):
_inherit = 'product.template'
property_stock_maquila = fields.Many2one(
'stock.location', "Maquila Location",
company_dependent=True, domain=[('usage', 'like', 'production')],
help="This stock location will be used, instead of the default one, as the source location for stock moves generated by maquila type manufacturing orders.")
class MrpProduction(models.Model):
_inherit = 'mrp.production'
owner_id = fields.Many2one('res.partner', 'Maquila for', help="This will be the owner of the primary finished product (used in maquila for third parties)")
def _generate_finished_moves(self):
if self.owner_id and self.owner_id.commercial_partner_id != self.company_id.accounting_company_id.partner_id:
production_location = self.product_id.property_stock_maquila
else:
production_location = self.product_id.property_stock_production
move = self.env['stock.move'].create({
'name': self.name,
'date': self.date_planned_start,
'date_expected': self.date_planned_start,
'product_id': self.product_id.id,
'product_uom': self.product_uom_id.id,
'product_uom_qty': self.product_qty,
'location_id': production_location.id,
'location_dest_id': self.location_dest_id.id,
'move_dest_id': self.procurement_ids and self.procurement_ids[0].move_dest_id.id or False,
'procurement_id': self.procurement_ids and self.procurement_ids[0].id or False,
'company_id': self.company_id.id,
'production_id': self.id,
'origin': self.name,
'group_id': self.procurement_group_id.id,
'propagate': self.propagate,
'restrict_partner_id': self.owner_id.id
})
move.action_confirm()
return move
def _generate_raw_move(self, bom_line, line_data):
quantity = line_data['qty']
# alt_op needed for the case when you explode phantom bom and all the lines will be consumed in the operation given by the parent bom line
alt_op = line_data['parent_line'] and line_data['parent_line'].operation_id.id or False
if bom_line.child_bom_id and bom_line.child_bom_id.type == 'phantom':
return self.env['stock.move']
if bom_line.product_id.type not in ['product', 'consu']:
return self.env['stock.move']
if self.routing_id:
routing = self.routing_id
else:
routing = self.bom_id.routing_id
if routing and routing.location_id:
source_location = routing.location_id
else:
source_location = self.location_src_id
original_quantity = self.product_qty - self.qty_produced
if original_quantity == 0:
return self.env['stock.move']
if self.owner_id and self.owner_id.commercial_partner_id != self.company_id.accounting_company_id.partner_id:
production_location = self.product_id.property_stock_maquila
else:
production_location = self.product_id.property_stock_production
data = {
'sequence': bom_line.sequence,
'name': self.name,
'date': self.date_planned_start,
'date_expected': self.date_planned_start,
'bom_line_id': bom_line.id,
'product_id': bom_line.product_id.id,
'product_uom_qty': quantity,
'product_uom': bom_line.product_uom_id.id,
'location_id': source_location.id,
'location_dest_id': production_location.id,
'raw_material_production_id': self.id,
'company_id': self.company_id.id,
'operation_id': bom_line.operation_id.id or alt_op,
'price_unit': bom_line.product_id.standard_price,
'procure_method': 'make_to_stock',
'origin': self.name,
'warehouse_id': source_location.get_warehouse().id,
'group_id': self.procurement_group_id.id,
'propagate': self.propagate,
'unit_factor': quantity / original_quantity,
}
return self.env['stock.move'].create(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment