blob: 0ab027b4d6e05ec25c828b721d2d73b17ca89b88 [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")
barredterra6900a792020-12-22 11:37:13 +010040 condition_true = True
41 try:
42 condition_true = frappe.safe_eval(condition, doc.as_dict())
43 except:
44 # invalid condition should not result in an error
45 pass
46
47 if condition and not condition_true:
barredterra81ad90c2020-09-29 11:54:57 +020048 continue
49
50 field_name = field.get("field_name")
51 regulation = field.get("regulation")
52 if field_name and not doc.get(field_name):
53 missing(field_map.get(field_name), regulation)
54
55
56def missing(field_label, regulation):
57 """Notify the user that a required field is missing."""
58 context = 'Specific for Germany. Example: Remember to set Company Tax ID. It is required by § 14 Abs. 4 Nr. 2 UStG.'
59 msgprint(_('Remember to set {field_label}. It is required by {regulation}.', context=context).format(
60 field_label=frappe.bold(_(field_label)),
61 regulation=regulation
62 )
63 )