Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9e01f9e

Browse files
committedMar 25, 2025·
[IMP] last_ordered_products: code improvement
- Improved code by removing unnecessary code
1 parent 8f87feb commit 9e01f9e

File tree

5 files changed

+7
-34
lines changed

5 files changed

+7
-34
lines changed
 

Diff for: ‎last_ordered_products/models/product_product.py

+4-26
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def _compute_last_order_time(self):
4646
last_date = last_ordered_products.get(record.id)
4747

4848
record.last_order_time = last_date if last_date else False
49-
record.last_date_str = self._get_time_ago_string(last_date) if last_date else False
49+
record.last_date_str = self.env['product.template']._get_time_ago_string(last_date) if last_date else False
5050

5151
def _get_last_sold_products(self, partner_id):
5252
'''Fetch products last sold to the given customer'''
@@ -100,28 +100,6 @@ def _get_last_purchased_products(self, partner_id):
100100

101101
return last_purchased_order_products
102102

103-
def _get_time_ago_string(self, last_date):
104-
'''Convert datetime to human-readable time difference (e.g., "1d", "4h", "4mo")'''
105-
106-
if not last_date:
107-
return ""
108-
109-
now = fields.Datetime.now()
110-
diff = now - last_date
111-
112-
if diff.days > 365:
113-
return f"{diff.days // 365}y"
114-
elif diff.days > 30:
115-
return f"{diff.days // 30}mo"
116-
elif diff.days > 0:
117-
return f"{diff.days}d"
118-
elif diff.seconds >= 3600:
119-
return f"{diff.seconds // 3600}h"
120-
elif diff.seconds >= 60:
121-
return f"{diff.seconds // 60}m"
122-
else:
123-
return f"{diff.seconds}s"
124-
125103
@api.model
126104
def name_search(self, name='', args=None, operator='ilike', limit=100):
127105
'''Modify product dropdown in sale order line to show last sold date'''
@@ -130,7 +108,7 @@ def name_search(self, name='', args=None, operator='ilike', limit=100):
130108
partner_id = self.env.context.get('partner_id')
131109
order_type = self.env.context.get('order_type')
132110
active_id = self.env.context.get('active_id')
133-
if active_id:
111+
if not order_type and active_id:
134112
order_type = self.env['account.journal'].browse(active_id).type
135113

136114
if partner_id:
@@ -147,8 +125,8 @@ def name_search(self, name='', args=None, operator='ilike', limit=100):
147125
if limit_rest is None or limit_rest > 0:
148126
products |= self.search_fetch(expression.AND([domain, [('id', 'not in', product_ids)], [("name", operator, name)]]), ['display_name'], limit=limit_rest)
149127

150-
products = sorted(products, key=lambda p: p.last_order_time if p.last_order_time else datetime.min, reverse=True)
128+
products = sorted(products, key=lambda p: last_ordered_products.get(p.id, datetime.min), reverse=True)
151129

152-
return [(product.id, product.display_name, product.last_date_str if product.last_date_str else False) for product in products]
130+
return [(product.id, product.display_name, self.env['product.template']._get_time_ago_string(last_ordered_products.get(product.id, False))) for product in products]
153131

154132
return super().name_search(name, args, operator, limit)

Diff for: ‎last_ordered_products/models/product_template.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def name_search(self, name='', args=None, operator='ilike', limit=100):
8989
partner_id = self.env.context.get('partner_id')
9090
order_type = self.env.context.get('order_type')
9191
active_id = self.env.context.get('active_id')
92-
if active_id:
92+
if not order_type and active_id:
9393
order_type = self.env['account.journal'].browse(active_id).type
9494

9595
if partner_id:

Diff for: ‎last_ordered_products/static/src/core/autocomplete/autocomplete.js

-5
This file was deleted.

Diff for: ‎last_ordered_products/static/src/core/autocomplete/autocomplete.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<templates xml:space="preserve">
3-
<t t-name="last_ordered_products.OrderedProductsAutoComplete" t-inherit="web.AutoComplete" t-inherit-mode="extension">
3+
<t t-inherit="web.AutoComplete" t-inherit-mode="extension">
44
<xpath expr="//t[@t-esc='option.label']" position="replace">
55
<t t-if="option.time_str">
66
<div class="d-flex justify-content-between">

Diff for: ‎last_ordered_products/views/product_views.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</xpath>
1313

1414
<xpath expr="//div[@name='o_kanban_qty_available']" position="replace">
15-
<div t-if="record.is_storable.raw_value" js_class="product_catalog_kanban_record"
15+
<div t-if="record.is_storable.raw_value"
1616
name="o_kanban_qty_available">
1717
<field name="qty_available"/> On Hand
1818
<field name="virtual_available" invisible="1"/>

0 commit comments

Comments
 (0)
Please sign in to comment.