| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from odoo import fields, models, api, _ |
| 5 |
|
| 6 |
|
| 7 |
class StockPicking(models.Model): |
| 8 |
_inherit = "stock.picking" |
| 9 |
|
| 10 |
l10n_it_transport_reason = fields.Selection([('sale', 'Sale'), |
| 11 |
('outsourcing', 'Outsourcing'), |
| 12 |
('evaluation', 'Evaluation'), |
| 13 |
('gift', 'Gift'), |
| 14 |
('transfer', 'Transfer'), |
| 15 |
('substitution', 'Returned goods'), |
| 16 |
('attemped_sale', 'Attempted Sale'), |
| 17 |
('loaned_use', 'Loaned for Use'), |
| 18 |
('repair', 'Repair')], default="sale", tracking=True, string='Transport Reason') |
| 19 |
l10n_it_transport_method = fields.Selection([('sender', 'Sender'), ('recipient', 'Recipient'), ('courier', 'Courier service')], |
| 20 |
default="sender", string='Transport Method') |
| 21 |
l10n_it_transport_method_details = fields.Char('Transport Note') |
| 22 |
l10n_it_parcels = fields.Integer(string="Parcels", default=1) |
| 23 |
l10n_it_ddt_number = fields.Char('DDT Number', readonly=True) |
| 24 |
l10n_it_show_print_ddt_button = fields.Boolean(compute="_compute_l10n_it_show_print_ddt_button") |
| 25 |
|
| 26 |
@api.depends('country_code', |
| 27 |
'picking_type_code', |
| 28 |
'state', |
| 29 |
'is_locked', |
| 30 |
'move_ids', |
| 31 |
'location_id', |
| 32 |
'location_dest_id') |
| 33 |
def _compute_l10n_it_show_print_ddt_button(self): |
| 34 |
|
| 35 |
|
| 36 |
for picking in self: |
| 37 |
picking.l10n_it_show_print_ddt_button = ( |
| 38 |
picking.country_code == 'IT' |
| 39 |
and picking.state == 'done' |
| 40 |
and picking.is_locked |
| 41 |
and (picking.picking_type_code == 'outgoing' |
| 42 |
or ( |
| 43 |
picking.move_ids |
| 44 |
and picking.move_ids[0].partner_id |
| 45 |
and picking.location_id.usage == 'supplier' |
| 46 |
and picking.location_dest_id.usage == 'customer' |
| 47 |
) |
| 48 |
) |
| 49 |
) |
| 50 |
|
| 51 |
def _action_done(self): |
| 52 |
for picking in self.filtered(lambda p: p.picking_type_id.l10n_it_ddt_sequence_id): |
| 53 |
picking.l10n_it_ddt_number = picking.picking_type_id.l10n_it_ddt_sequence_id.next_by_id() |
| 54 |
super()._action_done() |
| 55 |
|
| 56 |
|
| 57 |
class StockPickingType(models.Model): |
| 58 |
_inherit = 'stock.picking.type' |
| 59 |
|
| 60 |
l10n_it_ddt_sequence_id = fields.Many2one('ir.sequence') |
| 61 |
|
| 62 |
def _get_dtt_ir_seq_vals(self, warehouse_id, sequence_code): |
| 63 |
if warehouse_id: |
| 64 |
wh = self.env['stock.warehouse'].browse(warehouse_id) |
| 65 |
ir_seq_name = _('%(warehouse)s Sequence %(code)s', warehouse=wh.name, code=sequence_code) |
| 66 |
ir_seq_prefix = wh.code + '/' + sequence_code + '/DDT' |
| 67 |
else: |
| 68 |
ir_seq_name = _('Sequence %(code)s', code=sequence_code) |
| 69 |
ir_seq_prefix = sequence_code + '/DDT' |
| 70 |
return ir_seq_name, ir_seq_prefix |
| 71 |
|
| 72 |
@api.model_create_multi |
| 73 |
def create(self, vals_list): |
| 74 |
for vals in vals_list: |
| 75 |
company = self.env['res.company'].browse(vals.get('company_id', False)) or self.env.company |
| 76 |
if company.country_id.code == 'IT' and vals.get('code') == 'outgoing' and ('l10n_it_ddt_sequence_id' not in vals or not vals['l10n_it_ddt_sequence_id']): |
| 77 |
ir_seq_name, ir_seq_prefix = self._get_dtt_ir_seq_vals(vals.get('warehouse_id'), vals['sequence_code']) |
| 78 |
vals['l10n_it_ddt_sequence_id'] = self.env['ir.sequence'].create({ |
| 79 |
'name': ir_seq_name, |
| 80 |
'prefix': ir_seq_prefix, |
| 81 |
'padding': 5, |
| 82 |
'company_id': company.id, |
| 83 |
'implementation': 'no_gap', |
| 84 |
}).id |
| 85 |
return super().create(vals_list) |
| 86 |
|
| 87 |
def write(self, vals): |
| 88 |
if 'sequence_code' in vals: |
| 89 |
for picking_type in self.filtered(lambda p: p.l10n_it_ddt_sequence_id): |
| 90 |
warehouse = picking_type.warehouse_id.id if 'warehouse_id' not in vals else vals['warehouse_ids'] |
| 91 |
ir_seq_name, ir_seq_prefix = self._get_dtt_ir_seq_vals(warehouse, vals['sequence_code']) |
| 92 |
picking_type.l10n_it_ddt_sequence_id.write({ |
| 93 |
'name': ir_seq_name, |
| 94 |
'prefix': ir_seq_prefix, |
| 95 |
}) |
| 96 |
return super(StockPickingType, self).write(vals) |