41 from gncinvoicefkt
import *
42 from gnucash
import SessionOpenMode
43 except ImportError
as import_error:
44 print(
"Problem importing modules.")
50 def __init__(self, msg):
61 filename_template =
None 62 filename_output =
None 67 filename_from_invoice =
False 72 opts, args = getopt.getopt(argv[1:],
"fhliI:t:o:OP:", [
"help"])
73 except getopt.error
as msg:
78 print(
"ignoring lock")
80 if opt[0]
in [
"-h",
"--help"]:
84 print(
"using invoice ID '" + str(invoice_id) +
"'.")
86 print(
"Using ipshell")
89 filename_output = opt[1]
90 print(
"using output file", filename_output)
93 print(
"given output filename will be overwritten,")
94 print(
"creating output filename from Invoice data.")
95 filename_from_invoice =
True 97 filename_template = opt[1]
98 print(
"using template file", filename_template)
101 print(
"listing invoices")
104 print(
"output path is", output_path +
".")
108 print(
"opts:", opts,
"args:", args)
109 raise Usage(
"Only one input possible !")
111 raise Usage(
"No input given !")
115 if not filename_template:
117 if not list_invoices:
118 raise Usage(
"No template given !")
121 if not (filename_output
or filename_from_invoice):
122 if filename_template:
123 filename_output = filename_template +
".out" 124 print(
"no output filename given, will be:", filename_output)
127 if err.msg ==
"Help:":
130 print(
"Error:", err.msg, file=sys.stderr)
131 print(
"for help use --help", file=sys.stderr)
137 print(
"Invoke with", prog_name,
"gnucash_url.")
138 print(
"where input is")
140 print(
"or file://filename")
141 print(
"or mysql://user:password@host/databasename")
143 print(
"-f force open = ignore lock (read only)")
144 print(
"-l list all invoices")
145 print(
"-h or --help for this help")
146 print(
"-I ID use invoice ID")
147 print(
"-t filename use filename as template file")
148 print(
"-o filename use filename as output file")
149 print(
"-O create output filename by date, owner and invoice number")
150 print(
"-P path path for output file. Overwrites path in -o option")
157 "Opening", input_url,
" (ignore-lock = read-only)." if ignore_lock
else "." 159 session = gnucash.Session(
161 SessionOpenMode.SESSION_READ_ONLY
163 else SessionOpenMode.SESSION_NORMAL_OPEN,
165 except Exception
as exception:
166 print(
"Problem opening input.")
171 root_account = book.get_root_account()
172 comm_table = book.get_table()
173 EUR = comm_table.lookup(
"CURRENCY",
"EUR")
175 invoice_list = get_all_invoices(book)
178 for number, invoice
in enumerate(invoice_list):
179 print(str(number) +
")")
185 invoice = book.InvoiceLookupByID(invoice_id)
187 print(
"ID not found.")
191 invoice = invoice_list[invoice_number]
193 print(
"Using the following invoice:")
196 path_template = os.path.dirname(filename_template)
197 filename_template_basename = os.path.basename(filename_template)
199 loader = jinja2.FileSystemLoader(path_template)
200 env = jinja2.Environment(loader=loader)
201 template = env.get_template(filename_template_basename)
205 output = template.render(invoice=invoice, locale=locale)
207 if filename_from_invoice:
208 filename_date = invoice.GetDatePosted().strftime(
211 filename_owner_name = str(invoice.GetOwner().GetName())
212 filename_invoice_id = str(invoice.GetID())
216 + filename_owner_name
218 + filename_invoice_id
223 filename_output = os.path.join(
224 output_path, os.path.basename(filename_output)
227 print(
"Writing output", filename_output,
".")
228 with open(filename_output,
"w")
as f:
237 if __name__ ==
"__main__":