|
| 1 | +# Copyright (c) 2026, BrainWise and contributors |
| 2 | +# For license information, please see license.txt |
| 3 | + |
| 4 | +import frappe |
| 5 | +from frappe import _ |
| 6 | +from frappe.model.document import Document |
| 7 | +from frappe.utils import flt, today |
| 8 | + |
| 9 | + |
| 10 | +class BankDeposits(Document): |
| 11 | + def validate(self): |
| 12 | + if not self.posting_date: |
| 13 | + self.posting_date = today() |
| 14 | + |
| 15 | + self.validate_pos_profile() |
| 16 | + self.validate_pos_closing_shift() |
| 17 | + self.validate_deposit_amount() |
| 18 | + self.validate_bank_receipt() |
| 19 | + |
| 20 | + def validate_pos_profile(self): |
| 21 | + if frappe.db.get_value("POS Profile", self.pos_profile, "disabled"): |
| 22 | + frappe.throw( |
| 23 | + _("POS Profile {0} is not active").format(frappe.bold(self.pos_profile)), |
| 24 | + title=_("Invalid POS Profile"), |
| 25 | + ) |
| 26 | + |
| 27 | + def validate_pos_closing_shift(self): |
| 28 | + shift = frappe.db.get_value( |
| 29 | + "POS Closing Shift", |
| 30 | + self.pos_closing_shift, |
| 31 | + ["docstatus", "pos_profile", "pos_opening_shift"], |
| 32 | + as_dict=True, |
| 33 | + ) |
| 34 | + if not shift: |
| 35 | + frappe.throw(_("POS Closing Shift {0} does not exist").format(self.pos_closing_shift)) |
| 36 | + |
| 37 | + if shift.docstatus != 1: |
| 38 | + frappe.throw(_("POS Closing Shift must be submitted")) |
| 39 | + |
| 40 | + opening_status = frappe.db.get_value("POS Opening Shift", shift.pos_opening_shift, "status") |
| 41 | + if opening_status != "Closed": |
| 42 | + frappe.throw(_("POS Closing Shift must be closed")) |
| 43 | + |
| 44 | + if shift.pos_profile != self.pos_profile: |
| 45 | + frappe.throw(_("POS Closing Shift does not belong to the selected POS Profile")) |
| 46 | + |
| 47 | + existing = frappe.db.exists( |
| 48 | + "Bank Deposits", |
| 49 | + { |
| 50 | + "pos_closing_shift": self.pos_closing_shift, |
| 51 | + "name": ["!=", self.name], |
| 52 | + "docstatus": ["!=", 2], |
| 53 | + }, |
| 54 | + ) |
| 55 | + if existing: |
| 56 | + frappe.throw( |
| 57 | + _("Bank Deposit {0} already exists for this shift").format(frappe.bold(existing)), |
| 58 | + title=_("Duplicate Bank Deposit"), |
| 59 | + ) |
| 60 | + |
| 61 | + def validate_deposit_amount(self): |
| 62 | + if flt(self.deposit_amount) <= 0: |
| 63 | + frappe.throw(_("Deposit Amount must be greater than zero")) |
| 64 | + |
| 65 | + def validate_bank_receipt(self): |
| 66 | + if not self.bank_transaction_doc: |
| 67 | + frappe.throw(_("Bank Transaction Document is required")) |
| 68 | + |
| 69 | + def on_submit(self): |
| 70 | + frappe.db.set_value( |
| 71 | + "POS Closing Shift", |
| 72 | + self.pos_closing_shift, |
| 73 | + "custom_bank_deposit", |
| 74 | + self.name, |
| 75 | + update_modified=False, |
| 76 | + ) |
| 77 | + |
| 78 | + def on_cancel(self): |
| 79 | + frappe.db.set_value( |
| 80 | + "POS Closing Shift", |
| 81 | + self.pos_closing_shift, |
| 82 | + "custom_bank_deposit", |
| 83 | + None, |
| 84 | + update_modified=False, |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +@frappe.whitelist() |
| 89 | +@frappe.validate_and_sanitize_search_inputs |
| 90 | +def get_pos_closing_shifts(doctype, txt, searchfield, start, page_len, filters): |
| 91 | + """Return submitted, closed POS Closing Shifts without an existing Bank Deposit.""" |
| 92 | + filters = filters or {} |
| 93 | + pos_profile = filters.get("pos_profile") |
| 94 | + profile_condition = "AND pcs.pos_profile = %(pos_profile)s" if pos_profile else "" |
| 95 | + |
| 96 | + return frappe.db.sql( |
| 97 | + f""" |
| 98 | + SELECT pcs.name |
| 99 | + FROM `tabPOS Closing Shift` pcs |
| 100 | + INNER JOIN `tabPOS Opening Shift` pos ON pos.name = pcs.pos_opening_shift |
| 101 | + WHERE pcs.docstatus = 1 |
| 102 | + AND pos.status = 'Closed' |
| 103 | + AND pcs.name NOT IN ( |
| 104 | + SELECT bd.pos_closing_shift |
| 105 | + FROM `tabBank Deposits` bd |
| 106 | + WHERE bd.docstatus = 1 |
| 107 | + AND bd.pos_closing_shift IS NOT NULL |
| 108 | + ) |
| 109 | + {profile_condition} |
| 110 | + AND pcs.name LIKE %(txt)s |
| 111 | + ORDER BY pcs.period_end_date DESC |
| 112 | + LIMIT %(page_len)s OFFSET %(start)s |
| 113 | + """, |
| 114 | + { |
| 115 | + "txt": f"%{txt}%", |
| 116 | + "start": start, |
| 117 | + "page_len": page_len, |
| 118 | + "pos_profile": pos_profile, |
| 119 | + }, |
| 120 | + ) |
0 commit comments