@@ -46,7 +46,7 @@ def _compute_last_order_time(self):
46
46
last_date = last_ordered_products .get (record .id )
47
47
48
48
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
50
50
51
51
def _get_last_sold_products (self , partner_id ):
52
52
'''Fetch products last sold to the given customer'''
@@ -100,28 +100,6 @@ def _get_last_purchased_products(self, partner_id):
100
100
101
101
return last_purchased_order_products
102
102
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
-
125
103
@api .model
126
104
def name_search (self , name = '' , args = None , operator = 'ilike' , limit = 100 ):
127
105
'''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):
130
108
partner_id = self .env .context .get ('partner_id' )
131
109
order_type = self .env .context .get ('order_type' )
132
110
active_id = self .env .context .get ('active_id' )
133
- if active_id :
111
+ if not order_type and active_id :
134
112
order_type = self .env ['account.journal' ].browse (active_id ).type
135
113
136
114
if partner_id :
@@ -147,8 +125,8 @@ def name_search(self, name='', args=None, operator='ilike', limit=100):
147
125
if limit_rest is None or limit_rest > 0 :
148
126
products |= self .search_fetch (expression .AND ([domain , [('id' , 'not in' , product_ids )], [("name" , operator , name )]]), ['display_name' ], limit = limit_rest )
149
127
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 )
151
129
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 ]
153
131
154
132
return super ().name_search (name , args , operator , limit )
0 commit comments