diff options
| author | Indoteknik . <andrifebriyadiputra@gmail.com> | 2025-05-08 09:41:15 +0700 |
|---|---|---|
| committer | Indoteknik . <andrifebriyadiputra@gmail.com> | 2025-05-08 09:41:15 +0700 |
| commit | 75bc2e5226862b8401424942ba464f6e70a03604 (patch) | |
| tree | 11d3cd3ff3ea72fd37f4ea917efbafb176589412 | |
| parent | 8923005630ca26e9863f71c031a28183eb590727 (diff) | |
(andri) add log note update field pada product variant
| -rwxr-xr-x | indoteknik_custom/models/product_template.py | 83 |
1 files changed, 82 insertions, 1 deletions
diff --git a/indoteknik_custom/models/product_template.py b/indoteknik_custom/models/product_template.py index 8e475f95..99ae73d3 100755 --- a/indoteknik_custom/models/product_template.py +++ b/indoteknik_custom/models/product_template.py @@ -418,7 +418,10 @@ class ProductTemplate(models.Model): if val in [None, False]: return 'None' if isinstance(field, fields.Selection): - return dict(field.selection).get(val, str(val)) + 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): @@ -777,6 +780,84 @@ class ProductProduct(models.Model): ], limit=1) return pricelist + # simpan data lama + 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 + } + + # log perubahan field + 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): + if isinstance(val, int): + rec = self.env[field.comodel_name].browse(val) + return rec.display_name if rec.exists() else str(val) + elif isinstance(val, models.BaseModel): + return val.display_name + return str(val) + + if isinstance(field, fields.Many2many): + records = record[field_name] + 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"<li><b>{field_label}</b>: '{old_val_str}' → '{new_val_str}'</li>") + + if changes: + message = "<b>Updated:</b><ul>%s</ul>" % "".join(changes) + record.message_post(body=message) + + # simpan data lama dan log perubahan field + def write(self, vals): + old_values = self._collect_old_values(vals) + result = super().write(vals) + self._log_field_changes(vals, old_values) + return result class OutstandingMove(models.Model): _name = 'v.move.outstanding' |
