From aca89f41cf980e2ea9b7f266d4792be52d2e4aa1 Mon Sep 17 00:00:00 2001 From: AndriFP Date: Thu, 8 May 2025 22:12:07 +0700 Subject: (andri) add log note pada purchase list untuk catat perubahan tiap fields --- indoteknik_custom/models/purchase_pricelist.py | 67 +++++++++++++++++++++++++- indoteknik_custom/views/purchase_pricelist.xml | 7 +++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/indoteknik_custom/models/purchase_pricelist.py b/indoteknik_custom/models/purchase_pricelist.py index e5b35d7f..6ac74c69 100755 --- a/indoteknik_custom/models/purchase_pricelist.py +++ b/indoteknik_custom/models/purchase_pricelist.py @@ -6,6 +6,7 @@ from pytz import timezone class PurchasePricelist(models.Model): _name = 'purchase.pricelist' _rec_name = 'product_id' + _inherit = ['mail.thread', 'mail.activity.mixin'] name = fields.Char(string='Name', compute="_compute_name") product_id = fields.Many2one('product.product', string="Product", required=True) @@ -120,4 +121,68 @@ class PurchasePricelist(models.Model): rec.sync_pricelist_tier() rec.product_id.product_tmpl_id._create_solr_queue('_sync_price_to_solr') - \ No newline at end of file + + def _collect_old_values(self, vals): + return { + record.id: { + field_name: record[field_name] + for field_name in vals.keys() + if field_name in record._fields + } + for record in self + } + + def _log_field_changes(self, vals, old_values): + exclude_fields = ['solr_flag', 'desc_update_solr', 'last_update_solr', 'is_edited'] + + for record in self: + changes = [] + for field_name in vals: + if field_name not in record._fields or field_name in exclude_fields: + continue + + field = record._fields[field_name] + field_label = field.string or field_name + old_value = old_values.get(record.id, {}).get(field_name) + new_value = record[field_name] + + def stringify(val): + if val in [None, False]: + return 'None' + if isinstance(field, fields.Selection): + selection = field.selection + if callable(selection): + selection = selection(record) + return dict(selection).get(val, str(val)) + if isinstance(field, fields.Boolean): + return 'Yes' if val else 'No' + if isinstance(field, fields.Many2one): + return val.name if hasattr(val, 'name') else str(val) + if isinstance(field, fields.Many2many): + records = val if isinstance(val, models.Model) else val + names = [] + if records: + for attr in ['name', 'x_name', 'display_name']: + if hasattr(records[0], attr): + names = records.mapped(attr) + break + if not names: + names = [str(r.id) for r in records] + return ", ".join(names) if names else 'None' + return str(val) + + old_val_str = stringify(old_value) + new_val_str = stringify(new_value) + + if old_val_str != new_val_str: + changes.append(f"
  • {field_label}: '{old_val_str}' → '{new_val_str}'
  • ") + + if changes: + message = "Updated:" % "".join(changes) + record.message_post(body=message) + + def write(self, vals): + old_values = self._collect_old_values(vals) + result = super(PurchasePricelist, self).write(vals) + self._log_field_changes(vals, old_values) + return result \ No newline at end of file diff --git a/indoteknik_custom/views/purchase_pricelist.xml b/indoteknik_custom/views/purchase_pricelist.xml index ca5cd416..409e3b6a 100755 --- a/indoteknik_custom/views/purchase_pricelist.xml +++ b/indoteknik_custom/views/purchase_pricelist.xml @@ -20,6 +20,8 @@ + + @@ -52,6 +54,11 @@ +
    + + + +
    -- cgit v1.2.3