blob: 7f76493608e5a48f61480defc669d0b5a68a5f72 [file] [log] [blame]
barredterra81ad90c2020-09-29 11:54:57 +02001import frappe
2from frappe import _
3from frappe import msgprint
4
5
6REQUIRED_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 Meyer57ce0462020-11-09 19:04:46 +010018 "regulation": "§ 14 Abs. 4 Nr. 8 UStG"
barredterra81ad90c2020-09-29 11:54:57 +020019 },
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
29def 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")
barredterra511be642020-12-22 11:43:33 +010040 if condition and not frappe.safe_eval(condition, doc.as_dict()):
barredterra81ad90c2020-09-29 11:54:57 +020041 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
49def missing(field_label, regulation):
50 """Notify the user that a required field is missing."""
barredterra2acd8cb2020-12-22 17:34:22 +010051 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
barredterra5adbe492020-12-22 11:37:43 +010052 formatted_msg = translated_msg.format(field_label=frappe.bold(_(field_label)), regulation=regulation)
53 msgprint(formatted_msg)