confirmadas en sale.order y en pos.order de los productos contenidos en la compra y genera una factura con esas ventas.
127 lines
5.7 KiB
Python
127 lines
5.7 KiB
Python
# Copyright (C) 2024: Criptomart (<https://criptomart.net/>)
|
|
# 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
|
|
|
|
import pdb
|
|
import logging
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
class PurchaseOrde(models.Model):
|
|
_inherit = 'purchase.order'
|
|
|
|
is_deposit = fields.Boolean(
|
|
help='Éste proveedor nos deja material a depósito.',
|
|
string='Es a depósito',
|
|
)
|
|
deposit_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."
|
|
)
|
|
|
|
@api.model
|
|
def create(self, vals):
|
|
if self.is_deposit:
|
|
if not self.partner_id.deposit_buy_accept:
|
|
raise ValidationError("Este proveedor no suministra material en depósito, configúralo antes de crear un depósito para él.")
|
|
vals['deposit_last_liquidation_date'] = fields.datetime.now()
|
|
return super(PurchaseOrde, self).create(vals)
|
|
|
|
def make_liquidation_invoice(self, context = None):
|
|
invoice_obj = self.env['account.move']
|
|
if self.deposit_last_liquidation_date:
|
|
sale_orders = self.env['sale.order'].search([
|
|
('date_order', '>', self.deposit_last_liquidation_date),
|
|
('order_line.product_id', 'in', self.order_line.mapped('product_id').ids),
|
|
('state', 'in', ['sale', 'done'])
|
|
])
|
|
else:
|
|
sale_orders = self.env['sale.order'].search([
|
|
('order_line.product_id', 'in', self.order_line.mapped('product_id').ids),
|
|
('state', 'in', ['sale', 'done'])
|
|
])
|
|
|
|
product_dict = {}
|
|
if sale_orders:
|
|
sale_lines = self.env['sale.order.line'].search([
|
|
('order_id', 'in', sale_orders.ids),
|
|
('product_id', 'in', self.order_line.mapped('product_id').ids)
|
|
])
|
|
for line in sale_lines:
|
|
product_id = line.product_id.id
|
|
if product_id in product_dict:
|
|
product_dict[product_id]['debit'] += line.price_total
|
|
product_dict[product_id]['quantity'] += line.product_uom_qty
|
|
else:
|
|
product_dict[product_id] = {
|
|
'product_id': product_id,
|
|
'debit': line.price_total,
|
|
'name': line.product_id.name,
|
|
'price_unit': line.product_id.lst_price,
|
|
'tax_ids': [(6, 0, line.product_id.supplier_taxes_id.ids)],
|
|
'quantity': line.product_uom_qty
|
|
}
|
|
_logger.warning("product_dict %s", product_dict)
|
|
pdb.set_trace()
|
|
pos_orders = False
|
|
if self.deposit_last_liquidation_date:
|
|
pos_orders = self.env['pos.order'].search([
|
|
('date_order', '>', self.deposit_last_liquidation_date),
|
|
('lines.product_id', 'in', self.order_line.mapped('product_id').ids),
|
|
('state', 'in', ['invoiced', 'done'])
|
|
])
|
|
else:
|
|
pos_orders = self.env['pos.order'].search([
|
|
('lines.product_id', 'in', self.order_line.mapped('product_id').ids),
|
|
('state', 'in', ['invoiced', 'done'])
|
|
])
|
|
|
|
if pos_orders:
|
|
pos_lines = self.env['pos.order.line'].search([
|
|
('order_id', 'in', pos_orders.ids),
|
|
('product_id', 'in', self.order_line.mapped('product_id').ids)
|
|
])
|
|
for line in pos_lines:
|
|
product_id = line.product_id.id
|
|
if product_id in product_dict:
|
|
product_dict[product_id]['debit'] += line.price_subtotal_incl
|
|
product_dict[product_id]['quantity'] += line.qty
|
|
else:
|
|
product_dict[product_id] = {
|
|
'product_id': product_id,
|
|
'debit': line.price_subtotal_incl,
|
|
'name': line.product_id.name,
|
|
'price_unit': line.product_id.lst_price,
|
|
'tax_ids': [(6, 0, line.product_id.supplier_taxes_id.ids)],
|
|
'quantity': line.qty
|
|
}
|
|
|
|
if not product_dict:
|
|
msg = "No se ha vendido ningún producto en depósito de éste proveedor desde la última liquidación.\n\n"
|
|
if self.deposit_last_liquidation_date:
|
|
msg += "Fecha última liquidación de compras: %s" % self.deposit_last_liquidation_date.strftime("%d-%m-%Y, %H:%M:%S")
|
|
else:
|
|
msg += "Todavía no se ha realizado ninguna liquidación de compras a éste proveedor."
|
|
raise ValidationError(msg)
|
|
_logger.warning("product_dict %s", product_dict)
|
|
invoice_vals = invoice_obj.default_get(invoice_obj._fields.keys())
|
|
invoice_vals.update({
|
|
'ref': "Liquidación Compras " + self.name,
|
|
'move_type': 'in_invoice',
|
|
'financial_type': 'payable',
|
|
'journal_id': self.env.user.company_id.deposit_journal_id.id,
|
|
'partner_id': self.partner_id.id,
|
|
'invoice_line_ids': [(0, 0, vals) for vals in product_dict.values()],
|
|
'company_id': self.company_id.id,
|
|
'purchase_id': self.id,
|
|
})
|
|
invoice = invoice_obj.create(invoice_vals)
|
|
self.deposit_last_liquidation_date = fields.datetime.now()
|
|
return {
|
|
'type': 'ir.actions.act_window',
|
|
'res_model': 'account.move',
|
|
'res_id': invoice.id,
|
|
'target': 'current',
|
|
'views': [(self.env.ref('account.view_move_form').id, 'form')],
|
|
}
|