mirror of
https://github.com/meichthys/church.git
synced 2026-02-08 18:03:42 +00:00
make church collections submittable and handle fund updates when cancelling
This commit is contained in:
parent
d4a39c7373
commit
acf9f6ac38
@ -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",
|
||||
|
||||
@ -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}")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user