|
| 1 | +from dateutil.relativedelta import relativedelta |
| 2 | +import datetime |
| 3 | + |
| 4 | +from odoo import models, api, fields |
| 5 | + |
| 6 | + |
| 7 | +class ProductProduct(models.Model): |
| 8 | + _inherit = "product.product" |
| 9 | + |
| 10 | + last_order = fields.Datetime(compute="_compute_last_order") |
| 11 | + last_invoice_date = fields.Date(compute="_compute_last_invoice_date") |
| 12 | + last_invoice_time = fields.Char(compute="_compute_invoice_time") |
| 13 | + |
| 14 | + @api.depends_context("customer", "formatted_display_name") |
| 15 | + def _compute_display_name(self): |
| 16 | + res = super()._compute_display_name() |
| 17 | + if not self.env.context.get("customer") or not self.env.context.get( |
| 18 | + "formatted_display_name" |
| 19 | + ): |
| 20 | + return res |
| 21 | + |
| 22 | + for product in self: |
| 23 | + if not product.last_order: |
| 24 | + continue |
| 25 | + ago = self.compute_agotime(fields.Datetime.now(),product.last_order) |
| 26 | + current_product_name = product.display_name or "" |
| 27 | + if self.env.context.get("formatted_display_name"): |
| 28 | + if ago: |
| 29 | + time_postfix = f"\t--{ago}--" |
| 30 | + else: |
| 31 | + time_postfix = "" |
| 32 | + product.display_name = f"{current_product_name}{time_postfix}" |
| 33 | + else: |
| 34 | + product.display_name = f"{current_product_name}" |
| 35 | + |
| 36 | + def _compute_last_invoice_date(self): |
| 37 | + print("invoice", self.env.context.get("customer")) |
| 38 | + customer_id = self.env.context.get("customer") |
| 39 | + vendor_id = self.env.context.get("vendor") |
| 40 | + domain = [ |
| 41 | + ("product_id", "in", self.ids), |
| 42 | + ("parent_state", "=", "posted"), |
| 43 | + ] |
| 44 | + if customer_id: |
| 45 | + domain.append(("partner_id", "=", customer_id)) |
| 46 | + else: |
| 47 | + domain.append(("move_id.move_type", "=", "in_invoice")) |
| 48 | + domain.append(("partner_id", "=", vendor_id)) |
| 49 | + |
| 50 | + last_invoice_dates = self.env["account.move.line"].search( |
| 51 | + domain, order="invoice_date desc" |
| 52 | + ) |
| 53 | + invoice_dates = {} |
| 54 | + for invoice in last_invoice_dates: |
| 55 | + if invoice.product_id.id not in invoice_dates: |
| 56 | + invoice_dates[invoice.product_id.id] = invoice.invoice_date |
| 57 | + for product in self: |
| 58 | + product.last_invoice_date = invoice_dates.get(product.id, False) |
| 59 | + |
| 60 | + def _compute_last_order(self): |
| 61 | + print("order") |
| 62 | + last_orders = self.env["sale.order.line"].search( |
| 63 | + [ |
| 64 | + ("order_id.partner_id", "=", self.env.context.get("customer")), |
| 65 | + ("product_id", "in", self.ids), |
| 66 | + ("state", "=", "sale"), |
| 67 | + ], |
| 68 | + order="order_id desc", |
| 69 | + ) |
| 70 | + order_dates = {} |
| 71 | + for order in last_orders: |
| 72 | + if order.product_id.id not in order_dates: |
| 73 | + order_dates[order.product_id.id] = order.order_id.date_order |
| 74 | + for product in self: |
| 75 | + product.last_order = order_dates.get(product.id, False) |
| 76 | + |
| 77 | + @api.depends('last_invoice_date') |
| 78 | + def _compute_invoice_time(self): |
| 79 | + for product in self: |
| 80 | + if product.last_invoice_date: |
| 81 | + ago = self.compute_agotime(fields.Date.today(),product.last_invoice_date) |
| 82 | + if ago == "": |
| 83 | + ago = "Today" |
| 84 | + product.last_invoice_time = ago |
| 85 | + else: |
| 86 | + product.last_invoice_time = False |
| 87 | + |
| 88 | + @api.model |
| 89 | + def name_search(self, name="", args=None, operator="ilike", limit=100): |
| 90 | + print("name search") |
| 91 | + if not self.env.context.get("customer") and not self.env.context.get("vendor"): |
| 92 | + return super().name_search(name, args, operator, limit) |
| 93 | + res = super().name_search(name, args, operator, limit=100) |
| 94 | + ids = [r[0] for r in res] |
| 95 | + records = self.browse(ids) |
| 96 | + records.mapped("last_invoice_date") |
| 97 | + sorted_records = records.sorted( |
| 98 | + key=(lambda r: r.last_invoice_date or datetime.date.min), reverse=True |
| 99 | + ) |
| 100 | + return [(r.id, r.display_name) for r in sorted_records][:limit] |
| 101 | + |
| 102 | + def compute_agotime(self,current, datetime_field): |
| 103 | + rd = relativedelta(current, datetime_field) |
| 104 | + if rd.years: |
| 105 | + ago = f"{rd.years}y" |
| 106 | + elif rd.months: |
| 107 | + ago = f"{rd.months}mo" |
| 108 | + elif rd.days: |
| 109 | + ago = f"{rd.days}d" |
| 110 | + elif rd.hours: |
| 111 | + ago = f"{rd.hours}h" |
| 112 | + elif rd.minutes: |
| 113 | + ago = f"{rd.minutes}m" |
| 114 | + elif rd.seconds: |
| 115 | + ago = f"{rd.seconds}s" |
| 116 | + else: |
| 117 | + ago = "" |
| 118 | + |
| 119 | + return ago |
0 commit comments