Fix liquidaciones. Cambia los creates a multi para evitar el warning.
Añade campos is desit y última fecha de liquidación a purchase.order. Se asegura de linkar la línea de invoice con la línea de purchase order.
This commit is contained in:
parent
4b1cae0b9e
commit
5a3b3772aa
5 changed files with 138 additions and 101 deletions
|
|
@ -2,71 +2,76 @@
|
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
|
||||
from odoo import models, fields, api
|
||||
from odoo.exceptions import ValidationError, Warning
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
|
||||
class ResPartner(models.Model):
|
||||
_inherit = 'res.partner'
|
||||
_inherit = 'res.partner'
|
||||
|
||||
deposit_sale_accept = fields.Boolean(
|
||||
deposit_sale_accept = fields.Boolean(
|
||||
string='Acepta depósitos de venta',
|
||||
help='Éste cliente acepta nuestro material en depósito.',
|
||||
)
|
||||
deposit_buy_accept = fields.Boolean(
|
||||
)
|
||||
deposit_buy_accept = fields.Boolean(
|
||||
string='Acepta depósitos de compra',
|
||||
help='Éste proveedor nos deja material a depósito.',
|
||||
)
|
||||
deposit_sale_location_id = fields.Many2one(
|
||||
comodel_name='stock.location',
|
||||
string='Ubicación de depósito de ventas',
|
||||
help="Ubicación usada para gestionar el depósito que dejamos a éste proveedor."
|
||||
)
|
||||
deposit_buy_location_id = fields.Many2one(
|
||||
comodel_name='stock.location',
|
||||
string='Ubicación de depósito de compras',
|
||||
help="Ubicación usada para gestionar el material que éste proveedor nos deja en depósito."
|
||||
)
|
||||
deposit_buy_last_liquidation_date = fields.Datetime(
|
||||
string='Fecha última liquidación compras',
|
||||
help="Cuándo se realizó la última liquidación de compras con éste proveedor."
|
||||
)
|
||||
deposit_sale_last_liquidation_date = fields.Datetime(
|
||||
string='Fecha última liquidación ventas',
|
||||
help="Cuándo se realizó la última liquidación de ventas con éste proveedor."
|
||||
)
|
||||
)
|
||||
deposit_sale_location_id = fields.Many2one(
|
||||
comodel_name='stock.location',
|
||||
string='Ubicación de depósito de ventas',
|
||||
help="Ubicación usada para gestionar el depósito que dejamos a éste proveedor."
|
||||
)
|
||||
deposit_buy_location_id = fields.Many2one(
|
||||
comodel_name='stock.location',
|
||||
string='Ubicación de depósito de compras',
|
||||
help="Ubicación usada para gestionar el material que éste proveedor nos deja en depósito."
|
||||
)
|
||||
deposit_buy_last_liquidation_date = fields.Datetime(
|
||||
string='Fecha última liquidación compras',
|
||||
help="Cuándo se realizó la última liquidación de compras con éste proveedor."
|
||||
)
|
||||
deposit_sale_last_liquidation_date = fields.Datetime(
|
||||
string='Fecha última liquidación ventas',
|
||||
help="Cuándo se realizó la última liquidación de ventas con éste proveedor."
|
||||
)
|
||||
|
||||
@api.model
|
||||
def create(self, vals):
|
||||
if vals.get('deposit_sale_accept', False) == True:
|
||||
vals['deposit_sale_location_id'] = self.env['stock.location'].create({
|
||||
'usage': 'transit',
|
||||
'name': vals.get('name'),
|
||||
'location_id': self.env.ref('stock_picking_deposito.stock_location_deposits_stock').id
|
||||
}).id
|
||||
if vals.get('deposit_buy_accept', False) == True:
|
||||
vals['deposit_buy_location_id'] = self.env['stock.location'].create({
|
||||
'usage': 'transit',
|
||||
'name': vals.get('name'),
|
||||
'location_id': self.env.ref('stock_picking_deposito.location_deposit_buy').id
|
||||
}).id
|
||||
return super().create(vals)
|
||||
@api.model_create_multi
|
||||
def create(self, vals_list):
|
||||
for vals in vals_list:
|
||||
vals = self.create_single(vals)
|
||||
return super().create(vals_list)
|
||||
|
||||
def create_single(self, vals):
|
||||
if vals.get('deposit_sale_accept', False) == True:
|
||||
vals['deposit_sale_location_id'] = self.env['stock.location'].create({
|
||||
'usage': 'transit',
|
||||
'name': vals.get('name'),
|
||||
'location_id': self.env.ref('stock_picking_deposito.stock_location_deposits_stock').id
|
||||
}).id
|
||||
if vals.get('deposit_buy_accept', False) == True:
|
||||
vals['deposit_buy_location_id'] = self.env['stock.location'].create({
|
||||
'usage': 'transit',
|
||||
'name': vals.get('name'),
|
||||
'location_id': self.env.ref('stock_picking_deposito.location_deposit_buy').id
|
||||
}).id
|
||||
return vals
|
||||
|
||||
def write(self, vals):
|
||||
if ( vals.get('deposit_sale_accept', False) == True ) and not self.deposit_sale_location_id:
|
||||
vals['deposit_sale_location_id'] = self.env['stock.location'].create({
|
||||
'usage': 'transit',
|
||||
'name': self.name,
|
||||
'location_id': self.env.ref('stock_picking_deposito.stock_location_deposits_stock').id
|
||||
}).id
|
||||
if ( vals.get('deposit_buy_accept', False) == True ) and not self.deposit_buy_location_id:
|
||||
vals['deposit_buy_location_id'] = self.env['stock.location'].create({
|
||||
'usage': 'transit',
|
||||
'name': self.name,
|
||||
'location_id': self.env.ref('stock_picking_deposito.location_deposit_buy').id
|
||||
}).id
|
||||
return super().write(vals)
|
||||
def write(self, vals):
|
||||
if ( vals.get('deposit_sale_accept', False) == True ) and not self.deposit_sale_location_id:
|
||||
vals['deposit_sale_location_id'] = self.env['stock.location'].create({
|
||||
'usage': 'transit',
|
||||
'name': self.name,
|
||||
'location_id': self.env.ref('stock_picking_deposito.stock_location_deposits_stock').id
|
||||
}).id
|
||||
if (vals.get('deposit_buy_accept', False) == True ) and not self.deposit_buy_location_id:
|
||||
vals['deposit_buy_location_id'] = self.env['stock.location'].create({
|
||||
'usage': 'transit',
|
||||
'name': self.name,
|
||||
'location_id': self.env.ref('stock_picking_deposito.location_deposit_buy').id
|
||||
}).id
|
||||
return super().write(vals)
|
||||
|
||||
def make_liquidation_buy(self, context = None):
|
||||
def make_liquidation_buy(self, context = None):
|
||||
invoice_obj = self.env['account.move']
|
||||
search_vals = [
|
||||
('location_id', '=', self.deposit_buy_location_id.id),'|',
|
||||
|
|
@ -78,26 +83,27 @@ class ResPartner(models.Model):
|
|||
move_lines = self.env['stock.move.line'].search(search_vals)
|
||||
product_list = []
|
||||
for mv in move_lines:
|
||||
new_prod = True
|
||||
for p in product_list:
|
||||
if p[2]['product_id'] == mv.product_id.id:
|
||||
p[2]['debit'] += mv.qty_done * mv.product_id.standard_price
|
||||
new_prod = False
|
||||
break
|
||||
if new_prod:
|
||||
product_list.append([0, False, {
|
||||
'product_id': mv.product_id.id,
|
||||
'debit': mv.qty_done * mv.product_id.standard_price,
|
||||
'name': mv.product_id.name,
|
||||
'price_unit': mv.product_id.lst_price,
|
||||
'tax_ids' : mv.product_id.supplier_taxes_id,
|
||||
}
|
||||
])
|
||||
new_prod = True
|
||||
for p in product_list:
|
||||
if p[2]['product_id'] == mv.product_id.id:
|
||||
p[2]['debit'] += mv.qty_done * mv.product_id.standard_price
|
||||
new_prod = False
|
||||
break
|
||||
if new_prod:
|
||||
product_list.append([0, False, {
|
||||
'product_id': mv.product_id.id,
|
||||
'debit': mv.qty_done * mv.product_id.standard_price,
|
||||
'name': mv.product_id.name,
|
||||
'price_unit': mv.product_id.lst_price,
|
||||
'tax_ids' : mv.product_id.supplier_taxes_id,
|
||||
}])
|
||||
if len(product_list):
|
||||
invoice_vals = invoice_obj.default_get(invoice_obj._fields.keys())
|
||||
inv_lines = []
|
||||
if self.deposit_buy_last_liquidation_date:
|
||||
invoice_vals['ref'] = "Liquidación de compras desde " + self.deposit_buy_last_liquidation_date.strftime("%Y-%m-%d %H:%M:%S")
|
||||
else:
|
||||
invoice_vals['ref'] = "Primera Liquidación de Compras."
|
||||
invoice_vals.update({
|
||||
'ref': "Liquidación Compras " + self.deposit_buy_last_liquidation_date.strftime("%d-%m-%Y"),
|
||||
'financial_type': 'payable',
|
||||
'journal_id': self.env.user.company_id.deposit_journal_id.id,
|
||||
'partner_id': self.id,
|
||||
|
|
@ -106,6 +112,7 @@ class ResPartner(models.Model):
|
|||
})
|
||||
invoice = invoice_obj.create(invoice_vals)
|
||||
self.deposit_buy_last_liquidation_date = fields.datetime.now()
|
||||
self.message_post(body="Se ha creado la liquidación de compras %s" %invoice.name)
|
||||
views = [(self.env.ref('account.view_move_form').id, 'form')]
|
||||
return {
|
||||
'type': 'ir.actions.act_window',
|
||||
|
|
@ -122,7 +129,7 @@ class ResPartner(models.Model):
|
|||
msg += "Todavía no se ha realizado ninguna liquidación de compras a éste proveedor."
|
||||
raise ValidationError(msg)
|
||||
|
||||
def make_liquidation_sale(self, context = None):
|
||||
def make_liquidation_sale(self, context = None):
|
||||
self.deposit_sale_location_id
|
||||
sale_order_obj = self.env['sale.order']
|
||||
search_vals = [('location_id', '=', self.deposit_sale_location_id.id)]
|
||||
|
|
@ -149,6 +156,7 @@ class ResPartner(models.Model):
|
|||
|
||||
})
|
||||
sale_order = self.env['sale.order'].sudo().create(so_vals)
|
||||
self.message_post(body="Se ha creado la liquidación de ventas %s" %sale_order.name)
|
||||
views = [(self.env.ref('sale.view_order_form').id, 'form')]
|
||||
return {
|
||||
'type': 'ir.actions.act_window',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue