2121## For QR bill and svg -> pdf
2222from qrbill import QRBill
2323from reportlab .graphics import renderPDF
24+ from requests import HTTPError
2425from stdnum .ch import esr
2526from svglib .svglib import svg2rlg
2627
@@ -98,9 +99,14 @@ def create_invoices(
9899 all_placeholder_invoices = []
99100 all_email_messages = []
100101
101- with AccountingManager (messages ) as book :
102+ book_messages = []
103+ with AccountingManager (book_messages ) as book :
102104 if not book :
103- return messages
105+ contracts = [] # Can't proceed without an accounting book
106+ for msg in book_messages :
107+ messages .append (
108+ CohivaAdminViewMixin .make_response_item (msg , variant = ResponseVariant .ERROR )
109+ )
104110 for contract in contracts :
105111 ## Create monthly invoices starting from the reference date
106112 options = {
@@ -124,7 +130,7 @@ def create_invoices(
124130 )
125131 messages .append (
126132 CohivaAdminViewMixin .make_response_item (
127- _ ("VERARBEITUNG ABGEBROCHEN!" ), variant = ResponseVariant .ERROR
133+ _ ("VERARBEITUNG ABGEBROCHEN!" ), variant = ResponseVariant .WARNING
128134 )
129135 )
130136 break
@@ -780,6 +786,20 @@ def add_invoice_obj(
780786 raise InvoiceCreationError (f"Invoice type { invoice_type } is not implemented." )
781787
782788 if dry_run :
789+ # Just check if the accounts exist
790+ try :
791+ if not book .account_exists (acc_debit ):
792+ raise InvoiceCreationError (
793+ _ ("Account {account} does not exist." ).format (account = acc_debit )
794+ )
795+ if not book .account_exists (acc_credit ):
796+ raise InvoiceCreationError (
797+ _ ("Account {account} does not exist." ).format (account = acc_credit )
798+ )
799+ except HTTPError as e :
800+ raise InvoiceCreationError (
801+ _ ("Can't access accounting backend: {error}" ).format (error = e )
802+ )
783803 return None
784804
785805 if transaction_id :
0 commit comments