make church collections submittable and handle fund updates when cancelling

This commit is contained in:
meichthys 2025-10-10 04:13:30 +00:00
parent d4a39c7373
commit acf9f6ac38
2 changed files with 58 additions and 21 deletions

View File

@ -9,11 +9,13 @@
"field_order": [
"date",
"event",
"notes",
"column_break_ijpy",
"total_amount",
"fund_totals",
"section_break_izta",
"donations"
"donations",
"amended_from"
],
"fields": [
{
@ -23,7 +25,8 @@
"in_list_view": 1,
"in_standard_filter": 1,
"label": "Event",
"options": "Church Event"
"options": "Church Event",
"search_index": 1
},
{
"default": "now",
@ -64,12 +67,28 @@
"label": "Fund Totals",
"options": "Church Collection Fund Total",
"read_only": 1
},
{
"fieldname": "notes",
"fieldtype": "Small Text",
"label": "Notes"
},
{
"fieldname": "amended_from",
"fieldtype": "Link",
"label": "Amended From",
"no_copy": 1,
"options": "Church Collection",
"print_hide": 1,
"read_only": 1,
"search_index": 1
}
],
"grid_page_length": 50,
"index_web_pages_for_search": 1,
"is_submittable": 1,
"links": [],
"modified": "2025-10-01 22:52:37.422727",
"modified": "2025-10-09 22:56:43.397335",
"modified_by": "Administrator",
"module": "Church Finances",
"name": "Church Collection",

View File

@ -6,30 +6,48 @@ from frappe.model.document import Document
class ChurchCollection(Document):
def after_insert(self):
self.update_church_funds()
def on_submit(self):
self.update_church_funds(reverse=False)
def update_church_funds(self):
# Group donations by fund
def on_cancel(self):
self.update_church_funds(reverse=True)
def update_church_funds(self, reverse=False):
fund_data = {}
for donation in self.donations:
if donation.fund and donation.amount:
if donation.fund not in fund_data:
fund_data[donation.fund] = 0
fund_data.setdefault(donation.fund, 0)
fund_data[donation.fund] += donation.amount
# Update each Church Fund
messages = []
for fund_name, fund_total in fund_data.items():
fund_doc = frappe.get_doc("Church Fund", fund_name)
# Add financial transaction
transaction = fund_doc.append(
"transactions",
{"amount": fund_total, "source_type": "Church Collection", "source": self.name},
)
transaction.creation = frappe.utils.now()
# Update balance (assuming you want to add to existing balance)
current_balance = fund_doc.get("balance") or 0
fund_doc.balance = current_balance + fund_total
fund_doc.save()
if reverse:
fund_doc.transactions = [
txn
for txn in fund_doc.transactions
if not (txn.source_type == "Church Collection" and txn.source == self.name)
]
fund_doc.balance = (fund_doc.balance or 0) - fund_total
fund_doc.save(ignore_permissions=True)
messages.append(f"💸 {fund_name} fund decreased by ${fund_total}")
else:
fund_doc.append(
"transactions",
{
"amount": fund_total,
"source_type": "Church Collection",
"source": self.name,
"date": frappe.utils.nowdate(),
},
)
fund_doc.balance = (fund_doc.balance or 0) + fund_total
fund_doc.save(ignore_permissions=True)
messages.append(f"💰 {fund_name} fund increased by ${fund_total}")
if messages:
frappe.msgprint("<br>".join(messages))
# Warn if funds are now negative
if fund_doc.balance < 0:
frappe.msgprint(f"⚠️ {fund_name} fund balance is negative: {fund_doc.balance}")