Nueva forma de liquidar a través de las compras. Busca todas las ventas
confirmadas en sale.order y en pos.order de los productos contenidos en la compra y genera una factura con esas ventas.
This commit is contained in:
parent
24c782c3aa
commit
446814d0c8
5 changed files with 162 additions and 3 deletions
|
@ -1,4 +1,4 @@
|
||||||
# Copyright 2021 Criptomart
|
# Copyright 2021-2024 Criptomart
|
||||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
|
||||||
{
|
{
|
||||||
"name": "Stock Picking Depósito",
|
"name": "Stock Picking Depósito",
|
||||||
|
@ -15,11 +15,14 @@
|
||||||
"stock",
|
"stock",
|
||||||
"account",
|
"account",
|
||||||
"sale_management",
|
"sale_management",
|
||||||
|
"purchase",
|
||||||
|
"point_of_sale",
|
||||||
],
|
],
|
||||||
"data": [
|
"data": [
|
||||||
|
"data/data.xml",
|
||||||
"views/stock_picking.xml",
|
"views/stock_picking.xml",
|
||||||
"views/view_res_partner.xml",
|
"views/view_res_partner.xml",
|
||||||
"data/data.xml",
|
"views/purchase_order.xml",
|
||||||
"views/res_config_settings_views.xml",
|
"views/res_config_settings_views.xml",
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,3 +2,4 @@ from . import stock_picking
|
||||||
from . import res_partner
|
from . import res_partner
|
||||||
from . import res_config_settings
|
from . import res_config_settings
|
||||||
from . import res_company
|
from . import res_company
|
||||||
|
from . import purchase_order
|
127
stock_picking_deposito/models/purchase_order.py
Normal file
127
stock_picking_deposito/models/purchase_order.py
Normal file
|
@ -0,0 +1,127 @@
|
||||||
|
# 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')],
|
||||||
|
}
|
|
@ -40,7 +40,7 @@ class Picking(models.Model):
|
||||||
for picking in self:
|
for picking in self:
|
||||||
if not picking.partner_id:
|
if not picking.partner_id:
|
||||||
return
|
return
|
||||||
_logger.debug("onchange %s", picking.location_dest_id.name)
|
_logger.debug("onchange %s", picking.location_dest_id.name) # web cliebt does not update without this, wtf?
|
||||||
if picking.picking_type_id.is_deposit:
|
if picking.picking_type_id.is_deposit:
|
||||||
if picking.picking_type_id.code == 'internal':
|
if picking.picking_type_id.code == 'internal':
|
||||||
if not picking.partner_id.deposit_sale_accept:
|
if not picking.partner_id.deposit_sale_accept:
|
||||||
|
|
28
stock_picking_deposito/views/purchase_order.xml
Normal file
28
stock_picking_deposito/views/purchase_order.xml
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
<odoo>
|
||||||
|
<record model="ir.actions.server" id="action_purchase_order_liquidation">
|
||||||
|
<field name="name">Liquidación Depósito Compra</field>
|
||||||
|
<field name="type">ir.actions.server</field>
|
||||||
|
<field name="model_id" ref="purchase.model_purchase_order" />
|
||||||
|
<field name="state">code</field>
|
||||||
|
<field name="code">action =record.make_liquidation_invoice()</field>
|
||||||
|
</record>
|
||||||
|
|
||||||
|
<record id="view_purchase_order_form_deposito" model="ir.ui.view">
|
||||||
|
<field name="model">purchase.order</field>
|
||||||
|
<field name="inherit_id" ref="purchase.purchase_order_form"/>
|
||||||
|
<field name="priority">200</field>
|
||||||
|
<field name="arch" type="xml">
|
||||||
|
<div name="button_box" position="inside">
|
||||||
|
<button name="%(stock_picking_deposito.action_purchase_order_liquidation)d"
|
||||||
|
string="Liquida Compra"
|
||||||
|
type="action"
|
||||||
|
class="oe_stat_button"
|
||||||
|
icon="fa-calendar"
|
||||||
|
help="Realiza el informe de liquidación para éste proveedor."
|
||||||
|
groups="stock.group_stock_manager"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</field>
|
||||||
|
</record>
|
||||||
|
|
||||||
|
</odoo>
|
Loading…
Add table
Reference in a new issue