barredterra | 81ad90c | 2020-09-29 11:54:57 +0200 | [diff] [blame] | 1 | import frappe |
| 2 | from frappe import _ |
| 3 | from frappe import msgprint |
| 4 | |
| 5 | |
| 6 | REQUIRED_FIELDS = { |
| 7 | "Sales Invoice": [ |
| 8 | { |
| 9 | "field_name": "company_address", |
| 10 | "regulation": "§ 14 Abs. 4 Nr. 1 UStG" |
| 11 | }, |
| 12 | { |
| 13 | "field_name": "company_tax_id", |
| 14 | "regulation": "§ 14 Abs. 4 Nr. 2 UStG" |
| 15 | }, |
| 16 | { |
| 17 | "field_name": "taxes", |
Raffael Meyer | 57ce046 | 2020-11-09 19:04:46 +0100 | [diff] [blame] | 18 | "regulation": "§ 14 Abs. 4 Nr. 8 UStG" |
barredterra | 81ad90c | 2020-09-29 11:54:57 +0200 | [diff] [blame] | 19 | }, |
| 20 | { |
| 21 | "field_name": "customer_address", |
| 22 | "regulation": "§ 14 Abs. 4 Nr. 1 UStG", |
| 23 | "condition": "base_grand_total > 250" |
| 24 | } |
| 25 | ] |
| 26 | } |
| 27 | |
| 28 | |
| 29 | def validate_regional(doc): |
| 30 | """Check if required fields for this document are present.""" |
| 31 | required_fields = REQUIRED_FIELDS.get(doc.doctype) |
| 32 | if not required_fields: |
| 33 | return |
| 34 | |
| 35 | meta = frappe.get_meta(doc.doctype) |
| 36 | field_map = {field.fieldname: field.label for field in meta.fields} |
| 37 | |
| 38 | for field in required_fields: |
| 39 | condition = field.get("condition") |
barredterra | 511be64 | 2020-12-22 11:43:33 +0100 | [diff] [blame] | 40 | if condition and not frappe.safe_eval(condition, doc.as_dict()): |
barredterra | 81ad90c | 2020-09-29 11:54:57 +0200 | [diff] [blame] | 41 | continue |
| 42 | |
| 43 | field_name = field.get("field_name") |
| 44 | regulation = field.get("regulation") |
| 45 | if field_name and not doc.get(field_name): |
| 46 | missing(field_map.get(field_name), regulation) |
| 47 | |
| 48 | |
| 49 | def missing(field_label, regulation): |
| 50 | """Notify the user that a required field is missing.""" |
barredterra | 2acd8cb | 2020-12-22 17:34:22 +0100 | [diff] [blame^] | 51 | translated_msg = _('Remember to set {field_label}. It is required by {regulation}.', context='Specific for Germany. Example: Remember to set Company Tax ID. It is required by § 14 Abs. 4 Nr. 2 UStG.') # noqa: E501 |
barredterra | 5adbe49 | 2020-12-22 11:37:43 +0100 | [diff] [blame] | 52 | formatted_msg = translated_msg.format(field_label=frappe.bold(_(field_label)), regulation=regulation) |
| 53 | msgprint(formatted_msg) |