blob: 193c8e14a3d1aa53df417fa45aba5b07a0b2a575 [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",
18 "regulation": "§ 14 Abs. 4 Nr. 8 UStG",
19 "condition": "not exempt_from_sales_tax"
20 },
21 {
22 "field_name": "customer_address",
23 "regulation": "§ 14 Abs. 4 Nr. 1 UStG",
24 "condition": "base_grand_total > 250"
25 }
26 ]
27}
28
29
30def validate_regional(doc):
31 """Check if required fields for this document are present."""
32 required_fields = REQUIRED_FIELDS.get(doc.doctype)
33 if not required_fields:
34 return
35
36 meta = frappe.get_meta(doc.doctype)
37 field_map = {field.fieldname: field.label for field in meta.fields}
38
39 for field in required_fields:
40 condition = field.get("condition")
41 if condition and not frappe.safe_eval(condition, doc.as_dict()):
42 continue
43
44 field_name = field.get("field_name")
45 regulation = field.get("regulation")
46 if field_name and not doc.get(field_name):
47 missing(field_map.get(field_name), regulation)
48
49
50def missing(field_label, regulation):
51 """Notify the user that a required field is missing."""
52 context = 'Specific for Germany. Example: Remember to set Company Tax ID. It is required by § 14 Abs. 4 Nr. 2 UStG.'
53 msgprint(_('Remember to set {field_label}. It is required by {regulation}.', context=context).format(
54 field_label=frappe.bold(_(field_label)),
55 regulation=regulation
56 )
57 )