Debian Codegraph @

addons/delivery_stock_picking_batch/models/stock_picking.py

raw
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from odoo import api, fields, models
4 from odoo.fields import Domain
5
6
7 class StockPickingType(models.Model):
8 _inherit = "stock.picking.type"
9
10 def _get_default_weight_uom(self):
11 return self.env['product.template']._get_weight_uom_name_from_ir_config_parameter()
12
13 batch_group_by_carrier = fields.Boolean('Carrier', help="Automatically group batches by carriers")
14 batch_max_weight = fields.Integer("Maximum weight",
15 help="A transfer will not be automatically added to batches that will exceed this weight if the transfer is added to it.\n"
16 "Leave this value as '0' if no weight limit.")
17 weight_uom_name = fields.Char(string='Weight unit of measure label', compute='_compute_weight_uom_name', readonly=True, default=_get_default_weight_uom)
18
19 def _compute_weight_uom_name(self):
20 for picking_type in self:
21 picking_type.weight_uom_name = self.env['product.template']._get_weight_uom_name_from_ir_config_parameter()
22
23 @api.model
24 def _get_batch_group_by_keys(self):
25 return super()._get_batch_group_by_keys() + ['batch_group_by_carrier']
26
27
28 class StockPicking(models.Model):
29 _inherit = "stock.picking"
30
31 def _get_possible_pickings_domain(self):
32 domain = super()._get_possible_pickings_domain()
33 if self.picking_type_id.batch_group_by_carrier:
34 domain &= Domain('carrier_id', '=', self.carrier_id.id if self.carrier_id else False)
35
36 return domain
37
38 def _get_possible_batches_domain(self):
39 domain = super()._get_possible_batches_domain()
40 if self.picking_type_id.batch_group_by_carrier:
41 domain &= Domain('picking_ids.carrier_id', '=', self.carrier_id.id if self.carrier_id else False)
42
43 return domain
44
45 def _get_auto_batch_description(self):
46 description = super()._get_auto_batch_description()
47 if self.picking_type_id.batch_group_by_carrier and self.carrier_id:
48 description = f"{description}, {self.carrier_id.name}" if description else self.carrier_id.name
49 return description
50
51 def _is_auto_batchable(self, picking=None):
52 """ Verifies if a picking can be put in a batch with another picking without violating auto_batch constrains.
53 """
54 res = super()._is_auto_batchable(picking)
55 if not picking:
56 picking = self.env['stock.picking']
57 if self.picking_type_id.batch_max_weight:
58 res = res and (self.weight + picking.weight <= self.picking_type_id.batch_max_weight)
59 return res