| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from odoo import fields, models, api, _ |
| 5 |
from odoo.exceptions import UserError |
| 6 |
|
| 7 |
|
| 8 |
class StockWarehouse(models.Model): |
| 9 |
_inherit = 'stock.warehouse' |
| 10 |
|
| 11 |
repair_type_id = fields.Many2one('stock.picking.type', 'Repair Operation Type', check_company=True, copy=False) |
| 12 |
repair_mto_pull_id = fields.Many2one( |
| 13 |
'stock.rule', 'Repair MTO Rule', copy=False) |
| 14 |
|
| 15 |
def _get_sequence_values(self, name=False, code=False): |
| 16 |
values = super(StockWarehouse, self)._get_sequence_values(name=name, code=code) |
| 17 |
values.update({ |
| 18 |
'repair_type_id': { |
| 19 |
'name': _('%(name)s Sequence repair', name=self.name), |
| 20 |
'prefix': self.code + '/' + (self.repair_type_id.sequence_code or 'RO') + '/', |
| 21 |
'padding': 5, |
| 22 |
'company_id': self.company_id.id |
| 23 |
}, |
| 24 |
}) |
| 25 |
return values |
| 26 |
|
| 27 |
def _get_picking_type_create_values(self, max_sequence): |
| 28 |
data, next_sequence = super(StockWarehouse, self)._get_picking_type_create_values(max_sequence) |
| 29 |
prod_location = self._get_production_location() |
| 30 |
scrap_location_id = self.env['stock.location'].search_read([('usage', '=', 'inventory'), ('company_id', 'in', [self.company_id.id, False])], fields=['id'], limit=1)[0].get('id') |
| 31 |
data.update({ |
| 32 |
'repair_type_id': { |
| 33 |
'name': _('Repairs'), |
| 34 |
'code': 'repair_operation', |
| 35 |
'default_location_src_id': self.lot_stock_id.id, |
| 36 |
'default_location_dest_id': prod_location.id, |
| 37 |
'default_remove_location_dest_id': scrap_location_id, |
| 38 |
'default_recycle_location_dest_id': self.lot_stock_id.id, |
| 39 |
'sequence': next_sequence + 1, |
| 40 |
'sequence_code': 'RO', |
| 41 |
'company_id': self.company_id.id, |
| 42 |
'use_create_lots': True, |
| 43 |
'use_existing_lots': True, |
| 44 |
}, |
| 45 |
}) |
| 46 |
return data, max_sequence + 2 |
| 47 |
|
| 48 |
def _get_picking_type_update_values(self): |
| 49 |
data = super(StockWarehouse, self)._get_picking_type_update_values() |
| 50 |
data.update({ |
| 51 |
'repair_type_id': { |
| 52 |
'active': self.active, |
| 53 |
'barcode': self.code.replace(" ", "").upper() + "RO", |
| 54 |
}, |
| 55 |
}) |
| 56 |
return data |
| 57 |
|
| 58 |
@api.model |
| 59 |
def _get_production_location(self): |
| 60 |
location = self.env['stock.location'].search([('usage', '=', 'production'), ('company_id', '=', self.company_id.id)], limit=1) |
| 61 |
if not location: |
| 62 |
raise UserError(_("Can't find any production location.")) |
| 63 |
return location |
| 64 |
|
| 65 |
def _create_missing_locations(self, vals): |
| 66 |
super()._create_missing_locations(vals) |
| 67 |
for company_id in self.company_id: |
| 68 |
location = self.env['stock.location'].search([('usage', '=', 'production'), ('company_id', '=', company_id.id)], limit=1) |
| 69 |
if not location: |
| 70 |
company_id._create_production_location() |
| 71 |
|
| 72 |
def _generate_global_route_rules_values(self): |
| 73 |
rules = super()._generate_global_route_rules_values() |
| 74 |
production_location = self._get_production_location() |
| 75 |
rules.update({ |
| 76 |
'repair_mto_pull_id': { |
| 77 |
'depends': ['repair_type_id'], |
| 78 |
'create_values': { |
| 79 |
'procure_method': 'make_to_order', |
| 80 |
'company_id': self.company_id.id, |
| 81 |
'action': 'pull', |
| 82 |
'auto': 'manual', |
| 83 |
'route_id': self._find_or_create_global_route('stock.route_warehouse0_mto', _('Replenish on Order (MTO)')).id, |
| 84 |
'location_dest_id': self.repair_type_id.default_location_dest_id.id, |
| 85 |
'location_src_id': self.repair_type_id.default_location_src_id.id, |
| 86 |
'picking_type_id': self.repair_type_id.id |
| 87 |
}, |
| 88 |
'update_values': { |
| 89 |
'name': self._format_rulename(self.lot_stock_id, production_location, 'MTO'), |
| 90 |
'active': True, |
| 91 |
}, |
| 92 |
}, |
| 93 |
}) |
| 94 |
return rules |