stock_rule.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from collections import defaultdict
  4. from dateutil.relativedelta import relativedelta
  5. from odoo import api, fields, models, SUPERUSER_ID, _
  6. from odoo.osv import expression
  7. from odoo.addons.stock.models.stock_rule import ProcurementException
  8. from odoo.tools import float_compare, OrderedSet
  9. class StockRule(models.Model):
  10. _inherit = 'stock.rule'
  11. action = fields.Selection(selection_add=[
  12. ('manufacture', 'Manufacture')
  13. ], ondelete={'manufacture': 'cascade'})
  14. def _get_message_dict(self):
  15. message_dict = super(StockRule, self)._get_message_dict()
  16. source, destination, operation = self._get_message_values()
  17. manufacture_message = _('When products are needed in <b>%s</b>, <br/> a manufacturing order is created to fulfill the need.') % (destination)
  18. if self.location_src_id:
  19. manufacture_message += _(' <br/><br/> The components will be taken from <b>%s</b>.') % (source)
  20. message_dict.update({
  21. 'manufacture': manufacture_message
  22. })
  23. return message_dict
  24. @api.depends('action')
  25. def _compute_picking_type_code_domain(self):
  26. remaining = self.browse()
  27. for rule in self:
  28. if rule.action == 'manufacture':
  29. rule.picking_type_code_domain = 'mrp_operation'
  30. else:
  31. remaining |= rule
  32. super(StockRule, remaining)._compute_picking_type_code_domain()
  33. def _should_auto_confirm_procurement_mo(self, p):
  34. return (not p.orderpoint_id and p.move_raw_ids) or (p.move_dest_ids.procure_method != 'make_to_order' and not p.move_raw_ids and not p.workorder_ids)
  35. @api.model
  36. def _run_manufacture(self, procurements):
  37. productions_values_by_company = defaultdict(list)
  38. for procurement, rule in procurements:
  39. if float_compare(procurement.product_qty, 0, precision_rounding=procurement.product_uom.rounding) <= 0:
  40. # If procurement contains negative quantity, don't create a MO that would be for a negative value.
  41. continue
  42. bom = rule._get_matching_bom(procurement.product_id, procurement.company_id, procurement.values)
  43. productions_values_by_company[procurement.company_id.id].append(rule._prepare_mo_vals(*procurement, bom))
  44. for company_id, productions_values in productions_values_by_company.items():
  45. # create the MO as SUPERUSER because the current user may not have the rights to do it (mto product launched by a sale for example)
  46. productions = self.env['mrp.production'].with_user(SUPERUSER_ID).sudo().with_company(company_id).create(productions_values)
  47. productions.filtered(self._should_auto_confirm_procurement_mo).action_confirm()
  48. for production in productions:
  49. origin_production = production.move_dest_ids and production.move_dest_ids[0].raw_material_production_id or False
  50. orderpoint = production.orderpoint_id
  51. if orderpoint and orderpoint.create_uid.id == SUPERUSER_ID and orderpoint.trigger == 'manual':
  52. production.message_post(
  53. body=_('This production order has been created from Replenishment Report.'),
  54. message_type='comment',
  55. subtype_xmlid='mail.mt_note')
  56. elif orderpoint:
  57. production.message_post_with_view(
  58. 'mail.message_origin_link',
  59. values={'self': production, 'origin': orderpoint},
  60. subtype_id=self.env.ref('mail.mt_note').id)
  61. elif origin_production:
  62. production.message_post_with_view(
  63. 'mail.message_origin_link',
  64. values={'self': production, 'origin': origin_production},
  65. subtype_id=self.env.ref('mail.mt_note').id)
  66. return True
  67. @api.model
  68. def _run_pull(self, procurements):
  69. # Override to correctly assign the move generated from the pull
  70. # in its production order (pbm_sam only)
  71. for procurement, rule in procurements:
  72. warehouse_id = rule.warehouse_id
  73. if not warehouse_id:
  74. warehouse_id = rule.location_dest_id.warehouse_id
  75. if rule.picking_type_id == warehouse_id.sam_type_id:
  76. if float_compare(procurement.product_qty, 0, precision_rounding=procurement.product_uom.rounding) < 0:
  77. procurement.values['group_id'] = procurement.values['group_id'].stock_move_ids.filtered(
  78. lambda m: m.state not in ['done', 'cancel']).move_orig_ids.group_id[:1]
  79. continue
  80. manu_type_id = warehouse_id.manu_type_id
  81. if manu_type_id:
  82. name = manu_type_id.sequence_id.next_by_id()
  83. else:
  84. name = self.env['ir.sequence'].next_by_code('mrp.production') or _('New')
  85. # Create now the procurement group that will be assigned to the new MO
  86. # This ensure that the outgoing move PostProduction -> Stock is linked to its MO
  87. # rather than the original record (MO or SO)
  88. group = procurement.values.get('group_id')
  89. if group:
  90. procurement.values['group_id'] = group.copy({'name': name})
  91. else:
  92. procurement.values['group_id'] = self.env["procurement.group"].create({'name': name})
  93. return super()._run_pull(procurements)
  94. def _get_custom_move_fields(self):
  95. fields = super(StockRule, self)._get_custom_move_fields()
  96. fields += ['bom_line_id']
  97. return fields
  98. def _get_matching_bom(self, product_id, company_id, values):
  99. if values.get('bom_id', False):
  100. return values['bom_id']
  101. if values.get('orderpoint_id', False) and values['orderpoint_id'].bom_id:
  102. return values['orderpoint_id'].bom_id
  103. return self.env['mrp.bom']._bom_find(product_id, picking_type=self.picking_type_id, bom_type='normal', company_id=company_id.id)[product_id]
  104. def _prepare_mo_vals(self, product_id, product_qty, product_uom, location_dest_id, name, origin, company_id, values, bom):
  105. date_planned = self._get_date_planned(product_id, company_id, values)
  106. date_deadline = values.get('date_deadline') or date_planned + relativedelta(days=product_id.produce_delay)
  107. mo_values = {
  108. 'origin': origin,
  109. 'product_id': product_id.id,
  110. 'product_description_variants': values.get('product_description_variants'),
  111. 'product_qty': product_uom._compute_quantity(product_qty, bom.product_uom_id) if bom else product_qty,
  112. 'product_uom_id': bom.product_uom_id.id if bom else product_uom.id,
  113. 'location_src_id': self.location_src_id.id or self.picking_type_id.default_location_src_id.id or location_dest_id.id,
  114. 'location_dest_id': location_dest_id.id,
  115. 'bom_id': bom.id,
  116. 'date_deadline': date_deadline,
  117. 'date_planned_start': date_planned,
  118. 'date_planned_finished': fields.Datetime.from_string(values['date_planned']),
  119. 'procurement_group_id': False,
  120. 'propagate_cancel': self.propagate_cancel,
  121. 'orderpoint_id': values.get('orderpoint_id', False) and values.get('orderpoint_id').id,
  122. 'picking_type_id': self.picking_type_id.id or values['warehouse_id'].manu_type_id.id,
  123. 'company_id': company_id.id,
  124. 'move_dest_ids': values.get('move_dest_ids') and [(4, x.id) for x in values['move_dest_ids']] or False,
  125. 'user_id': False,
  126. }
  127. # Use the procurement group created in _run_pull mrp override
  128. # Preserve the origin from the original stock move, if available
  129. if location_dest_id.warehouse_id.manufacture_steps == 'pbm_sam' and values.get('move_dest_ids') and values.get('group_id') and values['move_dest_ids'][0].origin != values['group_id'].name:
  130. origin = values['move_dest_ids'][0].origin
  131. mo_values.update({
  132. 'name': values['group_id'].name,
  133. 'procurement_group_id': values['group_id'].id,
  134. 'origin': origin,
  135. })
  136. return mo_values
  137. def _get_date_planned(self, product_id, company_id, values):
  138. format_date_planned = fields.Datetime.from_string(values['date_planned'])
  139. date_planned = format_date_planned - relativedelta(days=product_id.produce_delay)
  140. if date_planned == format_date_planned:
  141. date_planned = date_planned - relativedelta(hours=1)
  142. return date_planned
  143. def _get_lead_days(self, product, **values):
  144. """Add the product and company manufacture delay to the cumulative delay
  145. and cumulative description.
  146. """
  147. delay, delay_description = super()._get_lead_days(product, **values)
  148. bypass_delay_description = self.env.context.get('bypass_delay_description')
  149. manufacture_rule = self.filtered(lambda r: r.action == 'manufacture')
  150. if not manufacture_rule:
  151. return delay, delay_description
  152. manufacture_rule.ensure_one()
  153. manufacture_delay = product.produce_delay
  154. delay += manufacture_delay
  155. if not bypass_delay_description:
  156. delay_description.append((_('Manufacturing Lead Time'), _('+ %d day(s)', manufacture_delay)))
  157. security_delay = manufacture_rule.picking_type_id.company_id.manufacturing_lead
  158. delay += security_delay
  159. if not bypass_delay_description:
  160. delay_description.append((_('Manufacture Security Lead Time'), _('+ %d day(s)', security_delay)))
  161. days_to_order = values.get('days_to_order', product.product_tmpl_id.days_to_prepare_mo)
  162. if not bypass_delay_description:
  163. delay_description.append((_('Days to Supply Components'), _('+ %d day(s)', days_to_order)))
  164. return delay + days_to_order, delay_description
  165. def _push_prepare_move_copy_values(self, move_to_copy, new_date):
  166. new_move_vals = super(StockRule, self)._push_prepare_move_copy_values(move_to_copy, new_date)
  167. new_move_vals['production_id'] = False
  168. return new_move_vals
  169. class ProcurementGroup(models.Model):
  170. _inherit = 'procurement.group'
  171. mrp_production_ids = fields.One2many('mrp.production', 'procurement_group_id')
  172. @api.model
  173. def run(self, procurements, raise_user_error=True):
  174. """ If 'run' is called on a kit, this override is made in order to call
  175. the original 'run' method with the values of the components of that kit.
  176. """
  177. procurements_without_kit = []
  178. product_by_company = defaultdict(OrderedSet)
  179. for procurement in procurements:
  180. product_by_company[procurement.company_id].add(procurement.product_id.id)
  181. kits_by_company = {
  182. company: self.env['mrp.bom']._bom_find(self.env['product.product'].browse(product_ids), company_id=company.id, bom_type='phantom')
  183. for company, product_ids in product_by_company.items()
  184. }
  185. for procurement in procurements:
  186. bom_kit = kits_by_company[procurement.company_id].get(procurement.product_id)
  187. if bom_kit:
  188. order_qty = procurement.product_uom._compute_quantity(procurement.product_qty, bom_kit.product_uom_id, round=False)
  189. qty_to_produce = (order_qty / bom_kit.product_qty)
  190. boms, bom_sub_lines = bom_kit.explode(procurement.product_id, qty_to_produce)
  191. for bom_line, bom_line_data in bom_sub_lines:
  192. bom_line_uom = bom_line.product_uom_id
  193. quant_uom = bom_line.product_id.uom_id
  194. # recreate dict of values since each child has its own bom_line_id
  195. values = dict(procurement.values, bom_line_id=bom_line.id)
  196. component_qty, procurement_uom = bom_line_uom._adjust_uom_quantities(bom_line_data['qty'], quant_uom)
  197. procurements_without_kit.append(self.env['procurement.group'].Procurement(
  198. bom_line.product_id, component_qty, procurement_uom,
  199. procurement.location_id, procurement.name,
  200. procurement.origin, procurement.company_id, values))
  201. else:
  202. procurements_without_kit.append(procurement)
  203. return super(ProcurementGroup, self).run(procurements_without_kit, raise_user_error=raise_user_error)
  204. def _get_moves_to_assign_domain(self, company_id):
  205. domain = super(ProcurementGroup, self)._get_moves_to_assign_domain(company_id)
  206. domain = expression.AND([domain, [('production_id', '=', False)]])
  207. return domain