diff --git a/tax_mate/hooks.py b/tax_mate/hooks.py index 9ea0fb7..6c14653 100644 --- a/tax_mate/hooks.py +++ b/tax_mate/hooks.py @@ -136,14 +136,12 @@ # Document Events # --------------- # Hook on document methods and events - -# doc_events = { -# "*": { -# "on_update": "method", -# "on_cancel": "method", -# "on_trash": "method" -# } -# } +# +doc_events = { + "Sales Order": { + "before_insert": "tax_mate.tax_mate.doc_events.sales_order.ensure_taxes_applied" + } +} # Scheduled Tasks # --------------- @@ -241,4 +239,3 @@ # default_log_clearing_doctypes = { # "Logging DocType Name": 30 # days to retain logs # } - diff --git a/tax_mate/tax_mate/doc_events/sales_order.py b/tax_mate/tax_mate/doc_events/sales_order.py new file mode 100644 index 0000000..221677b --- /dev/null +++ b/tax_mate/tax_mate/doc_events/sales_order.py @@ -0,0 +1,18 @@ +import frappe + +def ensure_taxes_applied(doc, method): + """ + Ensure taxes are calculated for all Sales Orders, including those created programmatically. + """ + if not doc.taxes: + default_template = frappe.db.get_value("Sales Taxes and Charges Template", { + "is_default": 1, + "company": doc.company + }) + + if default_template: + doc.taxes_and_charges = default_template + doc.set_taxes(default_template) + + doc.set_missing_values() + doc.calculate_taxes_and_totals()