arregla el depósito de entrada, no probado con salida, pero debería
funcionar igual. Cambia el location dst en el onchange partner_id. También se asegura que si se han creado stock moves se cambie su location dst. El create y el write no sé si son necesarios, a lo mejor para importaciones, duplicados y así. El escribir el stock location directamnete no es muy elegante, lo suyo sería sobreescribir el onchange_partner_id de stock.move y dejar que se enargue de location dst ahí.
This commit is contained in:
parent
beaee63e89
commit
260cd2a394
1 changed files with 49 additions and 30 deletions
|
@ -1,53 +1,72 @@
|
|||
# Copyright 2021-2024 Criptomart
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
|
||||
|
||||
import logging
|
||||
|
||||
from odoo import api, models, fields
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
class PickingType(models.Model):
|
||||
_inherit = 'stock.picking.type'
|
||||
|
||||
is_deposit = fields.Boolean(
|
||||
string='Depósito',
|
||||
help='Éste albarán es un depósito, no es una venta final.',
|
||||
help='Los albaranes creados con éste tipo son depósitos, no son compras/ventas finales.',
|
||||
default= False,
|
||||
)
|
||||
|
||||
|
||||
class Picking(models.Model):
|
||||
_inherit = 'stock.picking'
|
||||
|
||||
@api.model
|
||||
def create(self, vals):
|
||||
pick = super().create(vals)
|
||||
if pick.picking_type_id.is_deposit:
|
||||
pick.change_dest_location()
|
||||
return pick
|
||||
if vals.get('picking_type_id'):
|
||||
picking_type = self.env['stock.picking.type'].browse(vals.get('picking_type_id'))
|
||||
if picking_type.is_deposit:
|
||||
self.change_dest_location(vals, picking_type.code)
|
||||
return super().create(vals)
|
||||
|
||||
@api.onchange("partner_id")
|
||||
def write(self, vals):
|
||||
if vals.get('picking_type_id'):
|
||||
picking_type = self.env['stock.picking.type'].browse(vals.get('picking_type_id'))
|
||||
if picking_type.is_deposit:
|
||||
self.change_dest_location(vals, picking_type.code)
|
||||
return super().write(vals)
|
||||
|
||||
@api.onchange('partner_id')
|
||||
def onchange_partner_id(self):
|
||||
if self.partner_id and self.picking_type_id.is_deposit:
|
||||
if self.picking_type_id.code == 'internal':
|
||||
self.location_dest_id = self.partner_id.deposit_sale_location_id
|
||||
elif self.picking_type_id.code == 'incoming':
|
||||
self.location_dest_id = self.partner_id.deposit_buy_location_id
|
||||
_logger.warning("location id: %s %s", self.location_dest_id, self.partner_id.deposit_sale_location_id)
|
||||
for picking in self:
|
||||
if not self.partner_id:
|
||||
return
|
||||
if self.picking_type_id.is_deposit:
|
||||
if self.picking_type_id.code == 'internal':
|
||||
if not self.partner_id.deposit_sale_accept:
|
||||
raise ValidationError("Éste cliente no acepta material en depósito, configúralo antes de crear un depósito para él.")
|
||||
return
|
||||
self.location_dest_id = self.partner_id.deposit_sale_location_id.id,
|
||||
elif self.picking_type_id.code == 'incoming':
|
||||
if not self.partner_id.deposit_buy_accept:
|
||||
raise ValidationError("Éste proveedor no suministra material en depósito, configúralo antes de crear un depósito para él.")
|
||||
return
|
||||
self.location_dest_id = self.partner_id.deposit_buy_location_id.id
|
||||
moves = self.env['stock.move'].search([('picking_id', '=', self.id)])
|
||||
for move in moves:
|
||||
move.write({'partner_id': self.partner_id.id}, {'location_dest_id': self.location_dest_id})
|
||||
|
||||
def change_dest_location(self):
|
||||
if self.picking_type_id.code == 'internal':
|
||||
if not self.partner_id.deposit_sale_accept:
|
||||
raise ValidationError("Éste cliente no acepta material en depósito, configúralo antes de crear un depósito para él.")
|
||||
return
|
||||
self.update({
|
||||
'location_dest_id': self.partner_id.deposit_sale_location_id.id,
|
||||
})
|
||||
elif self.picking_type_id.code == 'incoming':
|
||||
if not self.partner_id.deposit_buy_accept:
|
||||
raise ValidationError("Éste proveedor no suministra material en depósito, configúralo antes de crear un depósito para él.")
|
||||
return
|
||||
self.update({
|
||||
'location_dest_id': self.partner_id.deposit_buy_location_id.id,
|
||||
})
|
||||
def change_dest_location(self, vals, code):
|
||||
if vals.get('partner_id'):
|
||||
partner = self.env['res.partner'].browse(vals.get('partner_id'))
|
||||
if code == 'internal':
|
||||
if not partner.deposit_sale_accept:
|
||||
raise ValidationError("Éste cliente no acepta material en depósito, configúralo antes de crear un depósito para él.")
|
||||
return
|
||||
vals.update({
|
||||
'location_dest_id': partner.deposit_sale_location_id.id,
|
||||
})
|
||||
elif code == 'incoming':
|
||||
if not partner.deposit_buy_accept:
|
||||
raise ValidationError("Éste proveedor no suministra material en depósito, configúralo antes de crear un depósito para él.")
|
||||
return
|
||||
vals.update({
|
||||
'location_dest_id': partner.deposit_buy_location_id.id,
|
||||
})
|
||||
|
|
Loading…
Add table
Reference in a new issue