Skip to content

[ADD] products_orderby_invoice: Imp dropdown in SO/RFQ Based on Invo… #910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: 18.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions products_orderby_invoice/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from . import models
12 changes: 12 additions & 0 deletions products_orderby_invoice/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

{
'name': 'Products Orderby Invoice',
'version': '1.0',
'depends': ['sale_management', 'account', 'stock', 'purchase'],
'data': [
'views/product_views.xml',
],
'installable': True,
'license': 'LGPL-3',
}
4 changes: 4 additions & 0 deletions products_orderby_invoice/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from . import product_template
from . import product_product
63 changes: 63 additions & 0 deletions products_orderby_invoice/models/product_product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models


class ProductProduct(models.Model):
_inherit = 'product.product'

surplus_qty = fields.Float(string='Surplus Quantity', compute='_compute_surplus_qty')
invoice_date = fields.Datetime(string='Last Invoice Date', compute='_compute_invoice_date')

@api.depends('invoice_date')
def _compute_invoice_date(self):
for rec in self:
lines = self.env['account.move.line'].search([
('product_id', '=', rec.id),
('move_id.move_type', '=', 'out_invoice'),
('move_id.state', '=', 'posted'),
], limit=1, order='date desc')
rec.invoice_date = lines.move_id.date if lines else False

@api.depends('virtual_available', 'qty_available')
def _compute_surplus_qty(self):
for rec in self:
rec.surplus_qty = rec.qty_available - rec.virtual_available

@api.model
def name_search(self, name, args=None, operator='ilike', limit=100):
args = list(args) if args else []
partner_id = self.env.context.get('partner_id')
results = []
matched_ids = set()

if partner_id:
lines = self.env['account.move.line'].search([
('move_id.move_type', '=', 'out_invoice'),
('move_id.partner_id', '=', partner_id),
('move_id.state', '=', 'posted'),
('product_id', '!=', False),
])

lines = sorted(lines, key=lambda l: l.move_id.invoice_date or l.create_date, reverse=True)

for line in lines:
product = line.product_id
if product.id in matched_ids:
continue
results.append((product.id, product.display_name))
matched_ids.add(product.id)
if len(results) >= limit:
break

remaining = limit - len(results)
if remaining > 0:
domain = args[:]
if name:
domain.append(('name', operator, name))
if matched_ids:
domain.append(('id', 'not in', list(matched_ids)))
others = super().name_search(name, domain, operator=operator, limit=remaining)
results.extend(others)

return results
46 changes: 46 additions & 0 deletions products_orderby_invoice/models/product_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import models, fields, api


class ProductTemplate(models.Model):
_inherit = "product.template"

@api.model
def name_search(self, name, args=None, operator="ilike", limit=100):
args = list(args) if args else []
partner_id = self.env.context.get("partner_id")
results = []
seen_ids = set()

if partner_id:
lines = self.env["account.move.line"].search([
("move_id.move_type", "=", "out_invoice"),
("move_id.partner_id", "=", partner_id),
("move_id.state", "=", "posted"),
("product_id.product_tmpl_id", "!=", False),
])
lines = sorted(lines, key=lambda l: l.move_id.invoice_date or fields.Date.today(), reverse=True)

for line in lines:
tmpl = line.product_id.product_tmpl_id
if tmpl.id in seen_ids:
continue
days = line.product_id.product_tmpl_id.sale_delay
display = f"{tmpl.display_name} (Order Lead time {days} days)"
results.append((tmpl.id, display))
seen_ids.add(tmpl.id)
if len(results) >= limit:
break

remaining = limit - len(results)
if remaining > 0:
domain = args[:]
if name:
domain.append(("name", operator, name))
if seen_ids:
domain.append(("id", "not in", list(seen_ids)))
others = super().name_search(name, args=domain, operator=operator, limit=remaining)
results.extend(others)

return results
29 changes: 29 additions & 0 deletions products_orderby_invoice/views/product_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="product_view_kanban_catalog_surplus_qty" model="ir.ui.view">
<field name="name">product.view.kanban.catalog.inherit.surplus.qty</field>
<field name="model">product.product</field>
<field name="inherit_id" ref="product.product_view_kanban_catalog" />
<field name="arch" type="xml">
<xpath expr="//div[@name='o_kanban_qty_available']/field[@name='uom_id']"
position="after">
<span> (</span>
<t t-if="record.surplus_qty.raw_value &gt; 0">
<span class="text-success">+<field name="surplus_qty" /></span>
</t>
<t t-elif="record.surplus_qty.raw_value &lt; 0">
<span class="text-danger">
<field name="surplus_qty" />
</span>
</t>
<t t-else="">
<span class="text-muted">0.00</span>
</t>
<span>)</span>
<div>
Last invoice date :<field name="invoice_date" widget="remaining_days"/>
</div>
</xpath>
</field>
</record>
</odoo>