|
@@ -0,0 +1,191 @@
|
|
|
+from datetime import datetime
|
|
|
+from antrag import SampleAntrag as Antrag
|
|
|
+from pathlib import Path
|
|
|
+from os import getcwd
|
|
|
+from dataclasses import dataclass
|
|
|
+import json
|
|
|
+
|
|
|
+@dataclass
|
|
|
+class Field:
|
|
|
+ name: str
|
|
|
+ fieldType: int
|
|
|
+ isGroupField: bool
|
|
|
+ group: str
|
|
|
+ valueChosenOrEnteredOutput: object
|
|
|
+ kurzbeschreibung: str
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+class AntragDrucken():
|
|
|
+
|
|
|
+ def __init__(self, antrag: Antrag):
|
|
|
+ self.antrag = antrag
|
|
|
+ self.pathAndFileName:Path = Path()
|
|
|
+ self.activityFields = []
|
|
|
+
|
|
|
+
|
|
|
+ def executeActivity(self) -> bool:
|
|
|
+ # Build field-list for printout
|
|
|
+ for fieldGroup in self.antrag.instance["field_groups"]:
|
|
|
+ self.activityFields.append(Field(name=fieldGroup["name"],
|
|
|
+ fieldType=fieldGroup["fieldType"],
|
|
|
+ isGroupField=True, group="", valueChosenOrEnteredOutput=None,
|
|
|
+ kurzbeschreibung=fieldGroup["brief"]))
|
|
|
+ for field in self.antrag.instance[fieldGroup["name"]]:
|
|
|
+ self.activityFields.append(Field(name=field["name"],
|
|
|
+ fieldType=field["fieldType"],
|
|
|
+ isGroupField=False, group=fieldGroup["name"],
|
|
|
+ valueChosenOrEnteredOutput=field["valueChosenOrEntered"],
|
|
|
+ kurzbeschreibung=field["brief"]))
|
|
|
+ self._printPDF()
|
|
|
+ return True
|
|
|
+
|
|
|
+ def getFolderAndPath(self) -> str:
|
|
|
+ if self.pathAndFileName:
|
|
|
+ # 28.10.20: Return only filename
|
|
|
+ return str(self.pathAndFileName.name)
|
|
|
+
|
|
|
+ def _printPDF(self):
|
|
|
+ html_start = """
|
|
|
+ <html>
|
|
|
+ <head>
|
|
|
+ </head>
|
|
|
+ <body>
|
|
|
+ <font face="arial">
|
|
|
+ <p align="center"><font face="arial" size="20">Prämienindikation "$(Produktname)"</font></p>
|
|
|
+ <p align="center"><font face="arial" size="6">$(Datum)</font></p>
|
|
|
+ <br>
|
|
|
+ <p><font face="arial" size="9">Thank you for your interest in $(Produktname).
|
|
|
+Based on the given parameters we are happy to provide a premium indication as follows:</font></p>
|
|
|
+ <br>
|
|
|
+ <table>
|
|
|
+ <tr>
|
|
|
+ <th width=30% align="right">Parameter/Statement</th>
|
|
|
+ <th width=70% align="left">Base for Indication</th>
|
|
|
+ </tr>"""
|
|
|
+
|
|
|
+ html_end = """</table>
|
|
|
+ <p><font face="arial" size="9">Please contact our sales staff for a complete and binding offer!</font></p>
|
|
|
+ </font>
|
|
|
+ </body>
|
|
|
+ </html>
|
|
|
+ """
|
|
|
+
|
|
|
+ html_start = html_start.replace("$(Produktname)", self.antrag.product_name)
|
|
|
+ html_start = html_start.replace("$(Datum)", datetime.now().strftime("%Y-%M-%D"))
|
|
|
+
|
|
|
+ html = ""
|
|
|
+
|
|
|
+ # Output of field groups and their fields
|
|
|
+ for groupField in [x for x in self.activityFields if x.isGroupField]:
|
|
|
+ template = f"""<tr><font face="arial" size="10">
|
|
|
+ <td align="right"><br><b>{str(groupField.kurzbeschreibung)}</b>
|
|
|
+ <p style="font-size:1px"> </p>
|
|
|
+ </td></font>
|
|
|
+ <td></td>
|
|
|
+ </tr>"""
|
|
|
+ html += template
|
|
|
+
|
|
|
+ for field in [x for x in self.activityFields if not x.isGroupField and x.group == groupField.name]:
|
|
|
+ if isinstance(field.valueChosenOrEnteredOutput, datetime):
|
|
|
+ field.valueChosenOrEnteredOutput = field.valueChosenOrEnteredOutput.strftime("%Y-%M-%D")
|
|
|
+ if field.fieldType == 3: # Don' print hidden fields!
|
|
|
+ continue
|
|
|
+ template = f"""<tr><font face="arial" size="8"><b>
|
|
|
+ <td align="right">{str(field.kurzbeschreibung)}</td></b></font>
|
|
|
+ <td><font face="arial" size="9">{str(field.valueChosenOrEnteredOutput)}</font></td>
|
|
|
+ </tr>"""
|
|
|
+ html += template
|
|
|
+
|
|
|
+ # Output of fields without group
|
|
|
+ for field in [x for x in self.activityFields if not x.isGroupField and not x.group]:
|
|
|
+ if isinstance(field.valueChosenOrEnteredOutput, datetime):
|
|
|
+ field.valueChosenOrEnteredOutput = field.valueChosenOrEnteredOutput.strftime("%Y-%M-%D")
|
|
|
+ if field.fieldType == 3: # Don' print hidden fields!
|
|
|
+ continue
|
|
|
+ if field.name == "premium":
|
|
|
+ template = f"""<tr><font face="arial" size="9"><b>
|
|
|
+ <td align="right"><br>{str(field.kurzbeschreibung)} in Euro</td></b></font>
|
|
|
+ <td align="left"><b>{str(field.valueChosenOrEnteredOutput)}</b></td>
|
|
|
+ </tr>"""
|
|
|
+ html += template
|
|
|
+ else:
|
|
|
+ template = f"""<tr><font face="arial" size="9"><b>
|
|
|
+ <td align="right">{str(field.kurzbeschreibung)} </td></b></font>
|
|
|
+ <td>{str(field.valueChosenOrEnteredOutput)}</td>
|
|
|
+ </tr>"""
|
|
|
+ html += template
|
|
|
+
|
|
|
+ html = html_start + html + html_end
|
|
|
+
|
|
|
+ # html = self._replaceVariable(html)
|
|
|
+ html = html.replace("€", "EURO")
|
|
|
+ self.outputPDFfromHTML(html)
|
|
|
+ # self.writePDFfromHTML(html)
|
|
|
+
|
|
|
+ def outputPDFfromHTML(self, html):
|
|
|
+ from fpdf import FPDF, HTMLMixin
|
|
|
+
|
|
|
+ class HTML2PDF(FPDF, HTMLMixin):
|
|
|
+ pass
|
|
|
+
|
|
|
+ pdf = HTML2PDF()
|
|
|
+ pdf.add_page()
|
|
|
+ pdf.write_html(html)
|
|
|
+
|
|
|
+ self.pathAndFileName = Path(getcwd()).joinpath("pdfoutput").\
|
|
|
+ joinpath(f"{self.antrag.product_name}_{datetime.now().strftime('%Y%m%d_%H%M%S%f')}.pdf")
|
|
|
+ if not self.pathAndFileName.parent.exists():
|
|
|
+ print(f"filename: {self.pathAndFileName}")
|
|
|
+ self.pathAndFileName.parent.mkdir(parents=False)
|
|
|
+
|
|
|
+ pdf.output(str(self.pathAndFileName))
|
|
|
+
|
|
|
+ # Moved from Activity class in fasifu.
|
|
|
+ def _replaceVariable(self, expression):
|
|
|
+ if not "$(" in expression:
|
|
|
+ return expression
|
|
|
+
|
|
|
+ antrag = self.antrag
|
|
|
+
|
|
|
+ while "$(" in expression:
|
|
|
+ if expression[0:2] == "$(":
|
|
|
+ left_part = ""
|
|
|
+ else:
|
|
|
+ left_part = expression.split("$(")[0]
|
|
|
+
|
|
|
+ center = expression[len(left_part) + 2:]
|
|
|
+ center = center.split(")")[0]
|
|
|
+
|
|
|
+ try:
|
|
|
+ right_part = expression[len(left_part) + len(center) + 3:]
|
|
|
+ except Exception:
|
|
|
+ right_part = ""
|
|
|
+
|
|
|
+ # Left = part of payload before $( starts
|
|
|
+ # right_part = part of payload after $(variable) ends
|
|
|
+ # center = the <variable>-part inside $(<variable>)
|
|
|
+
|
|
|
+ # center might be polizze.polizzenNummer or self.wirksamkeitsdatum
|
|
|
+ if "." in center:
|
|
|
+ lClassForAttribute = locals().get(center.split(".")[0])
|
|
|
+ center = center.split(".")[1]
|
|
|
+ try:
|
|
|
+ centerValue = getattr(lClassForAttribute, center)
|
|
|
+ except Exception as e:
|
|
|
+ raise BaseException("Fehler in _replaceVariable. Guckst Du Logs.")
|
|
|
+ else:
|
|
|
+ lClassForAttribute = self
|
|
|
+ try:
|
|
|
+ lField = lClassForAttribute.activityFields.getField(name=center)
|
|
|
+ if lField.valueChosenOrEnteredTech:
|
|
|
+ centerValue = lField.valueChosenOrEnteredTech
|
|
|
+ else:
|
|
|
+ centerValue = lField.valueChosenOrEntered
|
|
|
+ except:
|
|
|
+ centerValue = getattr(lClassForAttribute, center)
|
|
|
+
|
|
|
+ expression = "".join([left_part, str(centerValue), right_part])
|
|
|
+
|
|
|
+ return expression
|
|
|
+
|