def invoice_to_lco(invoice): """returns a string which forms a lco-file for use with LaTeX""" lco_out = u"\ProvidesFile{data.lco}[]\n" def write_variable(ukey, uvalue, replace_linebreak=True): outstr = u"" if uvalue.endswith("\n"): uvalue = uvalue[0:len(uvalue) - 1] if not ukey in [u"fromaddress", u"toaddress", u"date"]: outstr += u'\\newkomavar{' outstr += ukey outstr += u"}\n" outstr += u"\\setkomavar{" outstr += ukey outstr += u"}{" if replace_linebreak: outstr += uvalue.replace(u"\n", u"\\\\") + "}" return outstr # Write owners address add_str = u"" owner = invoice.GetOwner() if owner.GetName() != "": add_str += owner.GetName().decode("UTF-8") + "\n" addr = owner.GetAddr() if addr.GetName() != "": add_str += addr.GetName().decode("UTF-8") + "\n" if addr.GetAddr1() != "": add_str += addr.GetAddr1().decode("UTF-8") + "\n" if addr.GetAddr2() != "": add_str += addr.GetAddr2().decode("UTF-8") + "\n" if addr.GetAddr3() != "": add_str += addr.GetAddr3().decode("UTF-8") + "\n" if addr.GetAddr4() != "": add_str += addr.GetAddr4().decode("UTF-8") + "\n" lco_out += write_variable("toaddress2", add_str) # Invoice number inr_str = invoice.GetID() lco_out += write_variable("rechnungsnummer", inr_str) # date date = invoice.GetDatePosted() udate = date.strftime("%d.%m.%Y") lco_out += write_variable("date", udate) + "\n" # date due date_due = invoice.GetDateDue() udate_due = date_due.strftime("%d.%m.%Y") lco_out += write_variable("date_due", udate_due) + "\n" # Write the entries ent_str = u"" locale.setlocale(locale.LC_ALL, "de_DE") for n, ent in enumerate(invoice.GetEntries()): line_str = u"" if type(ent) != Entry: ent = Entry(instance=ent) # Add to method_returns_list descr = ent.GetDescription() price = ent.GetInvPrice().to_double() n = ent.GetQuantity() uprice = locale.currency(price).rstrip(" EUR") un = unicode(int(float(n.num()) / n.denom()) ) # choose best way to format numbers according to locale line_str = u"\Artikel{" line_str += un line_str += u"}{" line_str += descr.decode("UTF-8") line_str += u"}{" line_str += uprice line_str += u"}" #print(line_str) ent_str += line_str lco_out += write_variable("entries", ent_str) return lco_out
def invoice_to_lco(invoice): """returns a string which forms a lco-file for use with LaTeX""" lco_out=u"\ProvidesFile{data.lco}[]\n\\usepackage[utf8]{inputenc}\n" def write_variable(ukey, uvalue, replace_linebreak=True): outstr = u"" if uvalue.endswith("\n"): uvalue=uvalue[0:len(uvalue)-1] if not ukey in [u"fromaddress",u"toaddress",u"date"]: outstr += u'\\newkomavar{' outstr += ukey outstr += u"}\n" outstr += u"\\setkomavar{" outstr += ukey outstr += u"}{" if replace_linebreak: outstr += uvalue.replace(u"\n",u"\\\\")+"}" return outstr # Write owners address add_str=u"" owner = invoice.GetOwner() if owner.GetName() != "": add_str += owner.GetName()+"\n" ### MWE: 2014-10-08 double? owner.GetName() vs. addr.GetName() # if addr.GetName() != "": # add_str += addr.GetName().decode("UTF-8")+"\n" def mkaddr(addr): result = u"" if addr == None: return u"" if addr.GetAddr1() != "": result += addr.GetAddr1().decode("UTF-8")+"\n" if addr.GetAddr2() != "": result += addr.GetAddr2().decode("UTF-8")+"\n" if addr.GetAddr3() != "": result += addr.GetAddr3().decode("UTF-8")+"\n" if addr.GetAddr4() != "": result += addr.GetAddr4().decode("UTF-8")+"\n" return result add_str += mkaddr(owner.GetAddr()) lco_out += write_variable("toaddress2",add_str) # Invoice number inr_str = invoice.GetID() lco_out += write_variable("rechnungsnummer",inr_str) # date date = invoice.GetDatePosted() udate = date.strftime("%d.%m.%Y") lco_out += write_variable("date",udate)+"\n" # date due date_due = invoice.GetDateDue() udate_due = date_due.strftime("%d.%m.%Y") lco_out += write_variable("date_due",udate_due)+"\n" lco_out += write_variable("description", str(invoice.GetNotes())) # Write the entries ent_str = u"" ### MWE: locale.setlocale(locale.LC_ALL,"de_DE") taxes = 0.0 bruto = 0.0 total = 0.0 for n,ent in enumerate(invoice.GetEntries()): line_str = u"" if type(ent) != Entry: ent=Entry(instance=ent) # Add to method_returns_list descr = ent.GetDescription() ### MWE: 2014-10-08 type error when using 'gnucash.GncNumeric' price = gnucash.gnucash_core_c._gnc_numeric() price = ent.GetInvPrice().to_double() taxval = ent.GetDocTaxValue(True,True,False) n = gnucash.gnucash_core_c._gnc_numeric() n = instance=ent.GetQuantity() # change gncucash_core.py locale.setlocale( locale.LC_ALL, '' ) uprice = locale.currency(price/1.21).rstrip(" EUR").rstrip(" €").strip("€ ") ### MWE: 2014-10-08 use float instead of int. ### Otherwise decimal places will be cut and nobody want to spend 0.75 h for nothing but stupid software. quantity = float(n.num())/n.denom() un = unicode(quantity) # choose best way to format numbers according to locale curtaxes = GncNumeric(taxval.num, taxval.denom).to_double() curprice = price*quantity taxes += curtaxes total += curprice bruto += curprice - curtaxes line_str = u"\Artikel{" line_str += un line_str += u"}{" line_str += descr.decode("UTF-8") line_str += u"}{" line_str += uprice line_str += u"}" print(ent.GetAction()) # Perhaps to add the action table?? line_str += u"{" line_str += "uur" if ent.GetAction() == "Uren" else "art." line_str += u"}" #print line_str ent_str += line_str lco_out += write_variable("taxes", str(round(taxes,2))) lco_out += write_variable("bruto", str(round(bruto,2))) lco_out += write_variable("total", str(round(total,2))) lco_out += write_variable("entries",ent_str) return lco_out