#!/usr/bin/python # -*- coding:latin1 -*- # # Copyright (C) 2015-2019 Nico Latzer nl@mnet-online.de # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # $Id: invgen.py,v 1.3 2019/11/07 04:21:54 nl Exp nl $ from InvoiceGenerator.api import Invoice, Item, Client, Provider, Creator provider = Provider('Praxis Dr. Deimel', bank_account='2600420569/2010', #address = 'Steinweg 7, DE-93047 Regensburg', email='sales@deimel.at', vat_id = 'DE3290398-3382') providerExclVat=u'' #u'kUr §19' creator = Creator('Nico Latzer, Assistant') from InvoiceGenerator.pdf import SimpleInvoice, ProformaInvoice, CorrectingInvoice import os def ensure_dir(fn): path,tail = os.path.split(fn) if not os.path.exists( path ): #not perfect.. #see http://stackoverflow.com/questions/273192/check-if-a-directory-exists-and-create-it-if-necessary #os.makedirs(path) os.mkdir(path) #,0755) conn=None def query(sql, *args ): c = conn.cursor() #print sql,args c.execute(sql, args ) return c def fmt3Lines(s): parts= s.split(',',2) return "\n".join(parts) from mx.DateTime.ISO import ParseDate def produce( c, inv , inv_lines, fn): client = Client('%s' % c['fn'], address = fmt3Lines(c['po'] or '') ) invoice = Invoice(client, provider, creator) #invoice.currency_locale = 'de_DE.UTF-8' invoice.title = 'Re Behandlung' #@@ inv.details #inv.ref or '' invoice.paytype=u'innerhalb von %u Tagen' % c['toc'] #@@skonto invoice.currency='EUR' try: inv['stmt_date'].strip invoice.date = ParseDate(inv['stmt_date']).pydate() except AttributeError: invoice.date = inv['stmt_date'] #already a datetime invoice.number=inv['id'] #.invoice_id v=1 if providerExclVat or 0: #inv.excl_vat: v=0 #if inv.excl_vat: client.note='Ust befreit nach %s' % inv.excl_vat if providerExclVat: provider.note='keine USt ausgewiesen nach §19 kUr' invoice.use_tax=v #20191013 for idl in inv_lines: invoice.add_item( Item(idl['cnt'],idl['price_excl'], description=idl['descr'], tax=v* idl['vat'] ) ) if inv['amount'] >= 0: #total pdf = SimpleInvoice(invoice) pdf.gen(fn, generate_qr_code=True) else: invoice.reason = 'Gutschrift Beh.' #inv.ref or '' invoice.paytype='' pdf = CorrectingInvoice(invoice) pdf.gen(fn) #os.chmod(fn,0550) if __name__=='__main__': import sqlite3 conn=sqlite3.connect('data/gnub.db') conn.row_factory=sqlite3.Row #import kinterbasdb #conn=kinterbasdb.connect('data/GNUVETF.FDB', 'SYSDBA','masterkey') #close pending inv for pend_inv in query("""select cli from v_pending_liab """).fetchall(): with conn: c= query("""insert into cb ( cli, valdate, amount, details) select cli,stmt_date,total_incl,details from v_pending_liab where cli = ? """ , pend_inv['cli'] ) if c.rowcount >0: query("update emr set inv=? where pat in ( select id from pat where cli=?) and inv is null and encdate<=CURRENT_DATE ", c.lastrowid, pend_inv['cli'] ) print "closed inv %u for client %u" % ( c.lastrowid,pend_inv['cli'] ) #produce missing pdfs for inv in query("select id,valdate as stmt_date,amount,details,cli from cb where details like 'Forderung%' /* and valdate>=CURRENT_DATE */ ").fetchall(): #if inv.isOpen(): # continue fn='silo/%u_%u.pdf' % ( inv['cli'],inv['id'] ) # inv.getFilename() print fn if os.path.exists( fn ): continue print "producing %s" % fn with conn: c,=query("select fn,po,2 as toc from cli where id =?", inv['cli']).fetchall() inv_lines = query("""select ci.code,ci.descr, ci.price as price_excl,ci.vat, emr.factor as cnt from emr join ci on ( emr.lofch=ci.code ) where emr.inv=? """, inv['id'] ).fetchall() produce(c, inv,inv_lines, fn )