change_production_qty.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models, _
  4. from odoo.exceptions import UserError
  5. from odoo.tools import float_is_zero
  6. class ChangeProductionQty(models.TransientModel):
  7. _name = 'change.production.qty'
  8. _description = 'Change Production Qty'
  9. mo_id = fields.Many2one('mrp.production', 'Manufacturing Order',
  10. required=True, ondelete='cascade')
  11. product_qty = fields.Float(
  12. 'Quantity To Produce',
  13. digits='Product Unit of Measure', required=True)
  14. @api.model
  15. def default_get(self, fields):
  16. res = super(ChangeProductionQty, self).default_get(fields)
  17. if 'mo_id' in fields and not res.get('mo_id') and self._context.get('active_model') == 'mrp.production' and self._context.get('active_id'):
  18. res['mo_id'] = self._context['active_id']
  19. if 'product_qty' in fields and not res.get('product_qty') and res.get('mo_id'):
  20. res['product_qty'] = self.env['mrp.production'].browse(res['mo_id']).product_qty
  21. return res
  22. @api.model
  23. def _update_finished_moves(self, production, new_qty, old_qty):
  24. """ Update finished product and its byproducts. This method only update
  25. the finished moves not done or cancel and just increase or decrease
  26. their quantity according the unit_ratio. It does not use the BoM, BoM
  27. modification during production would not be taken into consideration.
  28. """
  29. modification = {}
  30. push_moves = self.env['stock.move']
  31. for move in production.move_finished_ids:
  32. if move.state in ('done', 'cancel'):
  33. continue
  34. qty = (new_qty - old_qty) * move.unit_factor
  35. modification[move] = (move.product_uom_qty + qty, move.product_uom_qty)
  36. if self._need_quantity_propagation(move, qty):
  37. push_moves |= move.copy({'product_uom_qty': qty})
  38. else:
  39. self._update_product_qty(move, qty)
  40. if push_moves:
  41. push_moves._action_confirm()._action_assign()
  42. return modification
  43. @api.model
  44. def _need_quantity_propagation(self, move, qty):
  45. return move.move_dest_ids and not float_is_zero(qty, precision_rounding=move.product_uom.rounding)
  46. @api.model
  47. def _update_product_qty(self, move, qty):
  48. move.write({'product_uom_qty': move.product_uom_qty + qty})
  49. def change_prod_qty(self):
  50. precision = self.env['decimal.precision'].precision_get('Product Unit of Measure')
  51. for wizard in self:
  52. production = wizard.mo_id
  53. produced = sum(production.move_finished_ids.filtered(lambda m: m.product_id == production.product_id).mapped('quantity_done'))
  54. if wizard.product_qty < produced:
  55. format_qty = '%.{precision}f'.format(precision=precision)
  56. raise UserError(_(
  57. "You have already processed %(quantity)s. Please input a quantity higher than %(minimum)s ",
  58. quantity=format_qty % produced,
  59. minimum=format_qty % produced
  60. ))
  61. old_production_qty = production.product_qty
  62. new_production_qty = wizard.product_qty
  63. factor = new_production_qty / old_production_qty
  64. update_info = production._update_raw_moves(factor)
  65. documents = {}
  66. for move, old_qty, new_qty in update_info:
  67. iterate_key = production._get_document_iterate_key(move)
  68. if iterate_key:
  69. document = self.env['stock.picking']._log_activity_get_documents({move: (new_qty, old_qty)}, iterate_key, 'UP')
  70. for key, value in document.items():
  71. if documents.get(key):
  72. documents[key] += [value]
  73. else:
  74. documents[key] = [value]
  75. production._log_manufacture_exception(documents)
  76. self._update_finished_moves(production, new_production_qty, old_production_qty)
  77. production.write({'product_qty': new_production_qty})
  78. if not float_is_zero(production.qty_producing, precision_rounding=production.product_uom_id.rounding) and\
  79. not production.workorder_ids:
  80. production.qty_producing = new_production_qty
  81. production._set_qty_producing()
  82. for wo in production.workorder_ids:
  83. operation = wo.operation_id
  84. wo.duration_expected = wo._get_duration_expected(ratio=new_production_qty / old_production_qty)
  85. quantity = wo.qty_production - wo.qty_produced
  86. if production.product_id.tracking == 'serial':
  87. quantity = 1.0 if not float_is_zero(quantity, precision_digits=precision) else 0.0
  88. else:
  89. quantity = quantity if (quantity > 0 and not float_is_zero(quantity, precision_digits=precision)) else 0
  90. wo._update_qty_producing(quantity)
  91. if wo.qty_produced < wo.qty_production and wo.state == 'done':
  92. wo.state = 'progress'
  93. if wo.qty_produced == wo.qty_production and wo.state == 'progress':
  94. wo.state = 'done'
  95. # assign moves; last operation receive all unassigned moves
  96. # TODO: following could be put in a function as it is similar as code in _workorders_create
  97. # TODO: only needed when creating new moves
  98. moves_raw = production.move_raw_ids.filtered(lambda move: move.operation_id == operation and move.state not in ('done', 'cancel'))
  99. if wo == production.workorder_ids[-1]:
  100. moves_raw |= production.move_raw_ids.filtered(lambda move: not move.operation_id)
  101. moves_finished = production.move_finished_ids.filtered(lambda move: move.operation_id == operation) #TODO: code does nothing, unless maybe by_products?
  102. moves_raw.mapped('move_line_ids').write({'workorder_id': wo.id})
  103. (moves_finished + moves_raw).write({'workorder_id': wo.id})
  104. # run scheduler for moves forecasted to not have enough in stock
  105. self.mo_id.filtered(lambda mo: mo.state in ['confirmed', 'progress']).move_raw_ids._trigger_scheduler()
  106. return {}