diff --git a/erpnext/utilities/transaction_base.py b/erpnext/utilities/transaction_base.py index 6ca3d5bf889..4845e0b960e 100644 --- a/erpnext/utilities/transaction_base.py +++ b/erpnext/utilities/transaction_base.py @@ -37,11 +37,18 @@ class TransactionBase(StatusUpdater): self._add_calendar_event(opts) def delete_events(self): - events = frappe.db.sql_list("""select name from `tabEvent` - where ref_type=%s and ref_name=%s""", (self.doctype, self.name)) - if events: - frappe.db.sql("delete from `tabEvent` where name in ({0})" - .format(", ".join(['%s']*len(events))), tuple(events)) + participations = frappe.get_all("Event Participants", filters={"reference_doctype": self.doctype, "reference_docname": self.name, + "parenttype": "Event"}, fields=["name", "parent"]) + + if participations: + for participation in participations: + total_participants = frappe.get_all("Event Participants", filters={"parenttype": "Event", "parent": participation.parent}) + + if len(total_participants) <= 1: + frappe.db.sql("delete from `tabEvent` where name='%s'" % participation.parent) + + frappe.db.sql("delete from `tabEvent Participants` where name='%s'" % participation.name) + def _add_calendar_event(self, opts): opts = frappe._dict(opts) @@ -54,11 +61,15 @@ class TransactionBase(StatusUpdater): "description": opts.description, "starts_on": self.contact_date, "ends_on": opts.ends_on, - "event_type": "Private", - "ref_type": self.doctype, - "ref_name": self.name + "event_type": "Private" }) + event.append('event_participants', { + "reference_doctype": self.doctype, + "reference_docname": self.name + } + ) + event.insert(ignore_permissions=True) if frappe.db.exists("User", self.contact_by):