58 from gncinvoicefkt
import *
59 from IPython
import version_info
as IPython_version_info
60 if IPython_version_info[0]>=1:
61 from IPython.terminal.ipapp
import TerminalIPythonApp
63 from IPython.frontend.terminal.ipapp
import TerminalIPythonApp
64 from gnucash.gnucash_business
import Customer, Employee, Vendor, Job, \
65 Address, Invoice, Entry, TaxTable, TaxTableEntry, GNC_AMT_TYPE_PERCENT, \
68 except ImportError
as import_error:
69 print "Problem importing modules."
74 def __init__(self, msg):
77 def invoice_to_lco(invoice):
78 """returns a string which forms a lco-file for use with LaTeX"""
80 lco_out=
u"\ProvidesFile{data.lco}[]\n"
82 def write_variable(ukey, uvalue, replace_linebreak=True):
85 if uvalue.endswith(
"\n"):
86 uvalue=uvalue[0:len(uvalue)-1]
88 if not ukey
in [
u"fromaddress",
u"toaddress",
u"date"]:
89 outstr +=
u'\\newkomavar{'
93 outstr +=
u"\\setkomavar{"
97 outstr += uvalue.replace(
u"\n",
u"\\\\")+
"}"
102 owner = invoice.GetOwner()
103 if owner.GetName() !=
"":
104 add_str += owner.GetName().decode(
"UTF-8")+
"\n"
106 addr = owner.GetAddr()
107 if addr.GetName() !=
"":
108 add_str += addr.GetName().decode(
"UTF-8")+
"\n"
109 if addr.GetAddr1() !=
"":
110 add_str += addr.GetAddr1().decode(
"UTF-8")+
"\n"
111 if addr.GetAddr2() !=
"":
112 add_str += addr.GetAddr2().decode(
"UTF-8")+
"\n"
113 if addr.GetAddr3() !=
"":
114 add_str += addr.GetAddr3().decode(
"UTF-8")+
"\n"
115 if addr.GetAddr4() !=
"":
116 add_str += addr.GetAddr4().decode(
"UTF-8")+
"\n"
118 lco_out += write_variable(
"toaddress2",add_str)
121 inr_str = invoice.GetID()
122 lco_out += write_variable(
"rechnungsnummer",inr_str)
125 date = invoice.GetDatePosted()
126 udate = date.strftime(
"%d.%m.%Y")
127 lco_out += write_variable(
"date",udate)+
"\n"
130 date_due = invoice.GetDateDue()
131 udate_due = date_due.strftime(
"%d.%m.%Y")
132 lco_out += write_variable(
"date_due",udate_due)+
"\n"
137 locale.setlocale(locale.LC_ALL,
"de_DE")
138 for n,ent
in enumerate(invoice.GetEntries()):
142 if type(ent) != Entry:
143 ent=Entry(instance=ent)
145 descr = ent.GetDescription()
146 price = ent.GetInvPrice().to_double()
147 n = ent.GetQuantity()
149 uprice = locale.currency(price).rstrip(
" EUR")
150 un = unicode(int(float(n.num())/n.denom()))
152 line_str =
u"\Artikel{"
155 line_str += descr.decode(
"UTF-8")
163 lco_out += write_variable(
"entries",ent_str)
175 no_latex_output =
True
176 list_invoices =
False
177 output_file_name =
"data.lco"
178 invoice_number =
None
181 opts, args = getopt.getopt(argv[1:],
"fhiln:po:", [
"help"])
182 except getopt.error, msg:
187 print "ignoring lock"
189 if opt[0]
in [
"-h",
"--help"]:
192 print "Using ipshell"
195 print "listing all invoices"
198 invoice_number = int(opt[1])
199 print "using invoice number", invoice_number
200 no_latex_output =
False
202 output_file_name = opt[1]
203 print "using output file", output_file_name
205 print "opts:",opts,
"args:",args
206 raise Usage(
"Only one input can be accepted !")
208 raise Usage(
"No input given !")
211 if err.msg ==
"Help:":
214 print >>sys.stderr,
"Error:",err.msg
215 print >>sys.stderr,
"for help use --help"
218 print "Generate a LaTeX invoice or print out all invoices."
222 print "Invoke with",prog_name,
"input."
223 print "where input is"
225 print "or file://filename"
226 print "or mysql://user:password@host/databasename"
228 print "-f force open = ignore lock"
229 print "-h or --help for this help"
230 print "-i for ipython shell"
231 print "-l list all invoices"
232 print "-n number use invoice number (no. from previous run with -l)"
233 print "-o name use name as outputfile. default: data.lco"
239 session = gnucash.Session(input_url,ignore_lock=ignore_lock)
240 except Exception
as exception:
241 print "Problem opening input."
246 root_account = book.get_root_account()
247 comm_table = book.get_table()
248 EUR = comm_table.lookup(
"CURRENCY",
"EUR")
250 invoice_list=get_all_invoices(book)
253 for number,invoice
in enumerate(invoice_list):
254 print str(number)+
")"
257 if not (no_latex_output):
259 if invoice_number ==
None:
260 print "Using the first invoice:"
263 invoice=invoice_list[invoice_number]
264 print "Using the following invoice:"
267 lco_str=invoice_to_lco(invoice)
270 f=open(output_file_name,
"w")
271 lco_str=lco_str.encode(
"latin1")
276 app = TerminalIPythonApp.instance()
277 app.initialize(argv=[])
283 if __name__ ==
"__main__":