take PricePerUnit from customer if not set in invoice position

This commit is contained in:
Torsten Ueberschar
2024-02-19 10:12:52 +01:00
parent 6f1120c8b4
commit b8bd3f288b
4 changed files with 24 additions and 15 deletions

View File

@@ -32,8 +32,7 @@ class HtmlTemplate:
@staticmethod @staticmethod
def calculate_total(invoice_data): def calculate_total(invoice_data):
return sum( return sum((pos['Quantity'] * pos['PricePerUnit']) for pos in invoice_data.Positions)
(pos['Quantity'] * (pos['PricePerUnit'] or invoice_data.PricePerUnit)) for pos in invoice_data.Positions)
@staticmethod @staticmethod
def named_replace(value, **replacements): def named_replace(value, **replacements):

View File

@@ -86,13 +86,16 @@ def main():
print('Envelope data:') print('Envelope data:')
envelope_data = DataObject(**envelope) envelope_data = DataObject(**envelope)
print(envelope_data.__dict__) print('<--->')
print(yaml.dump(envelope_data))
print('</--->')
print('Invoice data:') print('Invoice data:')
invoice_data = DataObject(**invoice) invoice_data = DataObject(**invoice)
merge_envelope_data_into_invoice_data(invoice_data, envelope_data.Invoice) merge_envelope_data_into_invoice_data(invoice_data, envelope_data.Invoice)
selected_customer = next((x for x in envelope_data.Customers if x['CustomerId'] == invoice_data.CustomerId), None) selected_customer = next((x for x in envelope_data.Customers if x['CustomerId'] == invoice_data.CustomerId),
None)
merge_envelope_data_into_invoice_data(invoice_data, selected_customer) merge_envelope_data_into_invoice_data(invoice_data, selected_customer)
if not hasattr(invoice_data, 'InvoiceDate'): if not hasattr(invoice_data, 'InvoiceDate'):
@@ -103,8 +106,17 @@ def main():
if not hasattr(invoice_data, 'Id'): if not hasattr(invoice_data, 'Id'):
invoice_data.Id = None invoice_data.Id = None
if not hasattr(invoice_data, 'Positions'):
invoice_data.Positions = []
for position in invoice_data.Positions:
if 'PricePerUnit' not in position or position['PricePerUnit'] is None:
position['PricePerUnit'] = invoice_data.PricePerUnit
invoice_data.Id = invoice_data.Id or invoice_file.stem invoice_data.Id = invoice_data.Id or invoice_file.stem
print(invoice_data.__dict__) print('<--->')
print(yaml.dump(invoice_data))
print('</--->')
except FileNotFoundError as e: except FileNotFoundError as e:
print(f'Error: {e}') print(f'Error: {e}')
@@ -119,8 +131,6 @@ def main():
generator = html_generator.HtmlTemplate(args.template) generator = html_generator.HtmlTemplate(args.template)
template = generator.prepare_template(invoice_data, envelope_data) template = generator.prepare_template(invoice_data, envelope_data)
print(template)
try: try:
print('Generating invoice...') print('Generating invoice...')
invoice_pdf = Path(invoice_data.Id).with_suffix('.pdf') invoice_pdf = Path(invoice_data.Id).with_suffix('.pdf')

View File

@@ -15,7 +15,6 @@ Positions:
- Title: "Aschkriechen" - Title: "Aschkriechen"
SubTitle: "Leistungszeitraum: 10/2022" SubTitle: "Leistungszeitraum: 10/2022"
PricePerUnit: 77.88
Quantity: 3 Quantity: 3

View File

@@ -112,6 +112,10 @@
border-top: .1pt solid black; border-top: .1pt solid black;
} }
.center {
text-align: center;
}
.head_data { .head_data {
width: 100%; width: 100%;
border-collapse: collapse; border-collapse: collapse;
@@ -136,7 +140,7 @@
<body> <body>
<!-- Content for Static Frame 'header_frame' --> <!-- Content for Static Frame 'header_frame' -->
<div id="address_frame_content"> <div id="address_frame_content">
<p class="underline">{{ envelope.AddressContent.AddressBoxSender }}</p> <p class="underline small center">{{ envelope.AddressContent.AddressBoxSender }}</p>
<address> <address>
{{ invoice.AddressField | markdown_to_html }} {{ invoice.AddressField | markdown_to_html }}
</address> </address>
@@ -188,20 +192,17 @@
{% endfor %} {% endfor %}
<tr class="summe"> <tr class="summe">
<td></td> <td></td>
<td></td> <td colspan="2" class="rechts">Nettosumme:</td>
<td class="rechts">Nettosumme:</td>
<td class="rechts">{{ format_float(calculate_total(invoice)) }}</td> <td class="rechts">{{ format_float(calculate_total(invoice)) }}</td>
</tr> </tr>
<tr> <tr>
<td></td> <td></td>
<td></td> <td colspan="2" class="rechts">USt. ({{ format_float(invoice.Vat) }}%):</td>
<td class="rechts">USt. ({{ format_float(invoice.Vat) }}%):</td>
<td class="rechts">{{ format_float(calculate_total(invoice) * ((invoice.Vat / 100))) }}</td> <td class="rechts">{{ format_float(calculate_total(invoice) * ((invoice.Vat / 100))) }}</td>
</tr> </tr>
<tr> <tr>
<td></td> <td></td>
<td></td> <td colspan="2" class="rechts bold overline">Gesamt Summe:</td>
<td class="rechts bold overline">Gesamt Summe:</td>
<td class="rechts bold overline">{{ format_float(calculate_total(invoice) * ((invoice.Vat / 100)+1)) }}</td> <td class="rechts bold overline">{{ format_float(calculate_total(invoice) * ((invoice.Vat / 100)+1)) }}</td>
</tr> </tr>
</table> </table>