58 from gncinvoicefkt
import *
59 from IPython
import version_info
as IPython_version_info
61 if IPython_version_info[0] >= 1:
62 from IPython.terminal.ipapp
import TerminalIPythonApp
64 from IPython.frontend.terminal.ipapp
import TerminalIPythonApp
65 from gnucash.gnucash_business
import (
78 from gnucash
import SessionOpenMode
80 except ImportError
as import_error:
81 print(
"Problem importing modules.")
87 def __init__(self, msg):
91 def invoice_to_lco(invoice):
92 """returns a string which forms a lco-file for use with LaTeX""" 94 lco_out =
u"\ProvidesFile{data.lco}[]\n" 96 def write_variable(ukey, uvalue, replace_linebreak=True):
99 if uvalue.endswith(
"\n"):
100 uvalue = uvalue[0 : len(uvalue) - 1]
102 if not ukey
in [
u"fromaddress",
u"toaddress",
u"date"]:
103 outstr +=
u"\\newkomavar{" 107 outstr +=
u"\\setkomavar{" 110 if replace_linebreak:
111 outstr += uvalue.replace(
u"\n",
u"\\\\") +
"}" 116 owner = invoice.GetOwner()
117 if owner.GetName() !=
"":
118 add_str += owner.GetName() +
"\n" 120 addr = owner.GetAddr()
121 if addr.GetName() !=
"":
122 add_str += addr.GetName() +
"\n" 123 if addr.GetAddr1() !=
"":
124 add_str += addr.GetAddr1() +
"\n" 125 if addr.GetAddr2() !=
"":
126 add_str += addr.GetAddr2() +
"\n" 127 if addr.GetAddr3() !=
"":
128 add_str += addr.GetAddr3() +
"\n" 129 if addr.GetAddr4() !=
"":
130 add_str += addr.GetAddr4() +
"\n" 132 lco_out += write_variable(
"toaddress2", add_str)
135 inr_str = invoice.GetID()
136 lco_out += write_variable(
"rechnungsnummer", inr_str)
139 date = invoice.GetDatePosted()
140 udate = date.strftime(
"%d.%m.%Y")
141 lco_out += write_variable(
"date", udate) +
"\n" 144 date_due = invoice.GetDateDue()
145 udate_due = date_due.strftime(
"%d.%m.%Y")
146 lco_out += write_variable(
"date_due", udate_due) +
"\n" 150 locale.setlocale(locale.LC_ALL,
"")
151 for n, ent
in enumerate(invoice.GetEntries()):
155 if type(ent) != Entry:
156 ent = Entry(instance=ent)
158 descr = ent.GetDescription()
159 price = ent.GetInvPrice().to_double()
160 n = ent.GetQuantity()
162 uprice = locale.currency(price).rstrip(
" EUR")
164 int(float(n.num()) / n.denom())
167 line_str =
u"\Artikel{" 178 lco_out += write_variable(
"entries", ent_str)
190 no_latex_output =
True 191 list_invoices =
False 192 output_file_name =
"data.lco" 193 invoice_number =
None 196 opts, args = getopt.getopt(argv[1:],
"fhiln:po:", [
"help"])
197 except getopt.error
as msg:
202 print(
"ignoring lock")
204 if opt[0]
in [
"-h",
"--help"]:
207 print(
"Using ipshell")
210 print(
"listing all invoices")
213 invoice_number = int(opt[1])
214 print(
"using invoice number", invoice_number)
215 no_latex_output =
False 217 output_file_name = opt[1]
218 print(
"using output file", output_file_name)
220 print(
"opts:", opts,
"args:", args)
221 raise Usage(
"Only one input can be accepted !")
223 raise Usage(
"No input given !")
226 if err.msg ==
"Help:":
229 print(
"Error:", err.msg, file=sys.stderr)
230 print(
"for help use --help", file=sys.stderr)
233 print(
"Generate a LaTeX invoice or print out all invoices.")
237 print(
"Invoke with", prog_name,
"input.")
238 print(
"where input is")
240 print(
"or file://filename")
241 print(
"or mysql://user:password@host/databasename")
243 print(
"-f force open = ignore lock")
244 print(
"-h or --help for this help")
245 print(
"-i for ipython shell")
246 print(
"-l list all invoices")
247 print(
"-n number use invoice number (no. from previous run with -l)")
248 print(
"-o name use name as outputfile. default: data.lco")
254 session = gnucash.Session(
256 SessionOpenMode.SESSION_READ_ONLY
258 else SessionOpenMode.SESSION_NORMAL_OPEN,
260 except Exception
as exception:
261 print(
"Problem opening input.")
266 root_account = book.get_root_account()
267 comm_table = book.get_table()
268 EUR = comm_table.lookup(
"CURRENCY",
"EUR")
270 invoice_list = get_all_invoices(book)
273 for number, invoice
in enumerate(invoice_list):
274 print(str(number) +
")")
277 if not (no_latex_output):
279 if invoice_number ==
None:
280 print(
"Using the first invoice:")
283 invoice = invoice_list[invoice_number]
284 print(
"Using the following invoice:")
287 lco_str = invoice_to_lco(invoice)
290 f = open(output_file_name,
"w")
295 app = TerminalIPythonApp.instance()
296 app.initialize(argv=[])
303 if __name__ ==
"__main__":