diff options
| author | Azka Nathan <darizkyfaz@gmail.com> | 2025-07-12 10:20:18 +0700 |
|---|---|---|
| committer | Azka Nathan <darizkyfaz@gmail.com> | 2025-07-12 10:20:18 +0700 |
| commit | f6d21f2f6a8bbc597e70aa07a4f6c2fc43b4c176 (patch) | |
| tree | 83054fd2b019272ea0f5e95a9d049c03ff8143c4 | |
| parent | b298a37963027a08e0046629bbcb795effa58e3a (diff) | |
add change log widya only
| -rw-r--r-- | indoteknik_custom/models/approval_payment_term.py | 62 | ||||
| -rw-r--r-- | indoteknik_custom/views/approval_payment_term.xml | 1 |
2 files changed, 63 insertions, 0 deletions
diff --git a/indoteknik_custom/models/approval_payment_term.py b/indoteknik_custom/models/approval_payment_term.py index cd54b53a..025f9ed4 100644 --- a/indoteknik_custom/models/approval_payment_term.py +++ b/indoteknik_custom/models/approval_payment_term.py @@ -53,6 +53,68 @@ class ApprovalPaymentTerm(models.Model): grand_total = fields.Float(string='Grand Total', compute="_compute_grand_total") + change_log_688 = fields.Text(string="Change Log", readonly=True, copy=False) + + def write(self, vals): + # Ambil nilai lama sebelum perubahan + old_values_dict = { + rec.id: rec.read(vals.keys())[0] + for rec in self + } + + res = super().write(vals) + + self._track_changes_for_user_688(vals, old_values_dict) + return res + + def _track_changes_for_user_688(self, vals, old_values_dict): + if self.env.user.id != 688: + return + + for rec in self: + changes = [] + old_values = old_values_dict.get(rec.id, {}) + + for field_name, new_value in vals.items(): + if field_name not in rec._fields or field_name == 'change_log_688': + continue + + field = rec._fields[field_name] + old_value = old_values.get(field_name) + + field_label = field.string # Ambil label user-friendly + + # Relational field + if field.type == 'many2one': + old_id = old_value[0] if old_value else False + is_different = old_id != new_value + if is_different: + old_display = old_value[1] if old_value else 'False' + new_display = rec.env[field.comodel_name].browse(new_value).display_name if new_value else 'False' + changes.append(f"[{field_label}] dari '{old_display}' ke '{new_display}'") + + else: + # Float khusus + if field.type == 'float': + is_different = not self._float_equal(old_value, new_value) + else: + is_different = old_value != new_value + + if is_different: + changes.append(f"[{field_label}] dari '{old_value}' ke '{new_value}'") + + if changes: + timestamp = fields.Datetime.now().strftime('%Y-%m-%d %H:%M:%S') + rec.change_log_688 = f"{timestamp} - Perubahan oleh Widya:\n" + "\n".join(changes) + + + @staticmethod + def _float_equal(val1, val2, eps=1e-6): + try: + return abs(float(val1 or 0.0) - float(val2 or 0.0)) < eps + except Exception: + return False + def _compute_grand_total(self): for rec in self: grand_total = sum(order.amount_total for order in rec.sale_order_ids) diff --git a/indoteknik_custom/views/approval_payment_term.xml b/indoteknik_custom/views/approval_payment_term.xml index 373637a2..cc9db914 100644 --- a/indoteknik_custom/views/approval_payment_term.xml +++ b/indoteknik_custom/views/approval_payment_term.xml @@ -21,6 +21,7 @@ <field name="state" widget="badge" decoration-danger="state == 'rejected'" decoration-success="state == 'approved'" decoration-info="state in ['waiting_approval_sales_manager', 'waiting_approval_finance', 'waiting_approval_leader']"/> + <field name="change_log_688" optional="hide"/> </tree> </field> </record> |
