product_replenish.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import datetime
  4. from odoo import _, api, fields, models
  5. from odoo.exceptions import UserError
  6. from odoo.tools.misc import clean_context
  7. class ProductReplenish(models.TransientModel):
  8. _name = 'product.replenish'
  9. _description = 'Product Replenish'
  10. product_id = fields.Many2one('product.product', string='Product', required=True)
  11. product_tmpl_id = fields.Many2one('product.template', string='Product Template', required=True)
  12. product_has_variants = fields.Boolean('Has variants', default=False, required=True)
  13. product_uom_category_id = fields.Many2one('uom.category', related='product_id.uom_id.category_id', readonly=True, required=True)
  14. product_uom_id = fields.Many2one('uom.uom', string='Unit of measure', required=True)
  15. quantity = fields.Float('Quantity', default=1, required=True)
  16. date_planned = fields.Datetime('Scheduled Date', required=True, help="Date at which the replenishment should take place.")
  17. warehouse_id = fields.Many2one(
  18. 'stock.warehouse', string='Warehouse', required=True,
  19. domain="[('company_id', '=', company_id)]")
  20. route_ids = fields.Many2many(
  21. 'stock.route', string='Preferred Routes',
  22. help="Apply specific route(s) for the replenishment instead of product's default routes.",
  23. domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]")
  24. company_id = fields.Many2one('res.company')
  25. @api.onchange('product_id')
  26. def _onchange_product_id(self):
  27. self.quantity = abs(self.product_id.virtual_available) if self.product_id.virtual_available < 0 else 1
  28. @api.model
  29. def default_get(self, fields):
  30. res = super(ProductReplenish, self).default_get(fields)
  31. product_tmpl_id = self.env['product.template']
  32. if self.env.context.get('default_product_id'):
  33. product_id = self.env['product.product'].browse(self.env.context['default_product_id'])
  34. product_tmpl_id = product_id.product_tmpl_id
  35. if 'product_id' in fields:
  36. res['product_tmpl_id'] = product_id.product_tmpl_id.id
  37. res['product_id'] = product_id.id
  38. elif self.env.context.get('default_product_tmpl_id'):
  39. product_tmpl_id = self.env['product.template'].browse(self.env.context['default_product_tmpl_id'])
  40. if 'product_id' in fields:
  41. res['product_tmpl_id'] = product_tmpl_id.id
  42. res['product_id'] = product_tmpl_id.product_variant_id.id
  43. if len(product_tmpl_id.product_variant_ids) > 1:
  44. res['product_has_variants'] = True
  45. company = product_tmpl_id.company_id or self.env.company
  46. if 'product_uom_id' in fields:
  47. res['product_uom_id'] = product_tmpl_id.uom_id.id
  48. if 'company_id' in fields:
  49. res['company_id'] = company.id
  50. if 'warehouse_id' in fields and 'warehouse_id' not in res:
  51. warehouse = self.env['stock.warehouse'].search([('company_id', '=', company.id)], limit=1)
  52. res['warehouse_id'] = warehouse.id
  53. if 'date_planned' in fields:
  54. res['date_planned'] = datetime.datetime.now()
  55. return res
  56. def launch_replenishment(self):
  57. uom_reference = self.product_id.uom_id
  58. self.quantity = self.product_uom_id._compute_quantity(self.quantity, uom_reference, rounding_method='HALF-UP')
  59. try:
  60. self.env['procurement.group'].with_context(clean_context(self.env.context)).run([
  61. self.env['procurement.group'].Procurement(
  62. self.product_id,
  63. self.quantity,
  64. uom_reference,
  65. self.warehouse_id.lot_stock_id, # Location
  66. _("Manual Replenishment"), # Name
  67. _("Manual Replenishment"), # Origin
  68. self.warehouse_id.company_id,
  69. self._prepare_run_values() # Values
  70. )
  71. ])
  72. except UserError as error:
  73. raise UserError(error)
  74. def _prepare_run_values(self):
  75. replenishment = self.env['procurement.group'].create({})
  76. values = {
  77. 'warehouse_id': self.warehouse_id,
  78. 'route_ids': self.route_ids,
  79. 'date_planned': self.date_planned,
  80. 'group_id': replenishment,
  81. }
  82. return values