diff --git a/church/church_finances/doctype/church_expense/church_expense.json b/church/church_finances/doctype/church_expense/church_expense.json index 938abb3..82bd537 100644 --- a/church/church_finances/doctype/church_expense/church_expense.json +++ b/church/church_finances/doctype/church_expense/church_expense.json @@ -1,6 +1,7 @@ { "actions": [], "allow_rename": 1, + "autoname": "format:{type} - {date}", "creation": "2025-10-09 02:25:29.401455", "description": "A financial transaction that reduces the balance of a `Church Fund`", "doctype": "DocType", @@ -8,36 +9,70 @@ "field_order": [ "type", "amount", - "notes" + "date", + "notes", + "amended_from" ], "fields": [ { "fieldname": "amount", - "fieldtype": "Float", + "fieldtype": "Currency", "in_list_view": 1, + "in_preview": 1, "label": "Amount", "reqd": 1 }, { "fieldname": "type", "fieldtype": "Link", + "in_filter": 1, + "in_list_view": 1, + "in_preview": 1, + "in_standard_filter": 1, "label": "Type", "options": "Church Expense Type", - "reqd": 1 + "reqd": 1, + "search_index": 1 }, { "fieldname": "notes", "fieldtype": "Small Text", + "in_list_view": 1, + "in_preview": 1, "label": "Notes" + }, + { + "fieldname": "amended_from", + "fieldtype": "Link", + "label": "Amended From", + "no_copy": 1, + "options": "Church Expense", + "print_hide": 1, + "read_only": 1, + "search_index": 1 + }, + { + "default": "Now", + "fieldname": "date", + "fieldtype": "Datetime", + "in_preview": 1, + "label": "Date" } ], "grid_page_length": 50, "index_web_pages_for_search": 1, - "links": [], - "modified": "2025-10-09 02:43:04.572585", + "is_submittable": 1, + "links": [ + { + "link_doctype": "Church Fund", + "link_fieldname": "source" + } + ], + "modified": "2025-10-09 23:46:57.471825", "modified_by": "Administrator", "module": "Church Finances", "name": "Church Expense", + "naming_rule": "Expression", "owner": "Administrator", "permissions": [ { diff --git a/church/church_finances/doctype/church_expense/church_expense.py b/church/church_finances/doctype/church_expense/church_expense.py index c544eff..674a1ca 100644 --- a/church/church_finances/doctype/church_expense/church_expense.py +++ b/church/church_finances/doctype/church_expense/church_expense.py @@ -1,9 +1,61 @@ # Copyright (c) 2025, meichthys and contributors # For license information, please see license.txt -# import frappe +import frappe from frappe.model.document import Document +from frappe.utils import get_link_to_form class ChurchExpense(Document): - pass + def before_delete(self): + # This probably should never get called since frappe prevents the deletion + # of submitted documents by default, but just to be sure we'll provide our own warning. + # Prevent deletion if the document is not cancelled + if not self.docstatus == 2: # 2 is Cancelled + frappe.throw("❌ You must cancel this Church Expense before deleting it.") + + def on_cancel(self): + fund_name = frappe.db.get_value("Church Expense Type", self.type, "fund") + if not fund_name: + frappe.throw("⚠️ No fund linked to the selected Church Expense Type.") + + fund = frappe.get_doc("Church Fund", fund_name) + + # Remove transaction that matches this expense + updated_transactions = [] + for transaction in fund.transactions: + if not (transaction.source_type == "Church Expense" and transaction.source == self.name): + updated_transactions.append(transaction) + else: + frappe.msgprint( + f"💰 Associated {get_link_to_form('Church Fund', fund_name)} fund has been increased by ${-transaction.amount}" + ) + fund.transactions = updated_transactions + fund.save(ignore_permissions=True) + fund.reload() + + def on_submit(self): + # Get related Church Fund via Expense Type + fund_name = frappe.db.get_value("Church Expense Type", self.type, "fund") + + if not fund_name: + frappe.throw("⚠️ No fund linked to the selected Church Expense Type.") + + fund = frappe.get_doc("Church Fund", fund_name) + + # Add new row to fund's transactions table + fund.append( + "transactions", + { + "amount": -self.amount, + "source_type": "Church Expense", + "source": self.name, + "date": self.date, + "notes": self.notes, + }, + ) + fund.save(ignore_permissions=True) + fund.reload() + frappe.msgprint( + f"💸 Associated {get_link_to_form('Church Fund', fund_name)} fund has been reduced by ${self.amount}" + )