Account greek fiscal: Feed cups data back into print status.

bzr revid: p_christ@hol.gr-20101028122411-pu42c9acjp60e9ph
This commit is contained in:
P. Christeas
2010-10-28 15:24:11 +03:00
parent 9e39342d6b
commit 31dc0244cb
2 changed files with 100 additions and 5 deletions
@@ -9,16 +9,30 @@
<form string="Prints">
<field name="cups_jobid" />
<field name="name" colspan="2"/>
<field name="state" />
<field name="report" />
<field name="cups_msg" />
<field name="hash" />
<field name="date" />
<field name="cups_msg" colspan="4"/>
<field name="hash" />
<field name="machine_id" />
<field name="day_no" />
<field name="total_no" />
</form>
</field>
</record>
<record model="ir.ui.view" id="view_account_ficalgr_print_tree">
<field name="name">account.fiscalgr.print.tree</field>
<field name="model">account.fiscalgr.print</field>
<field name="type">tree</field>
<field name="arch" type="xml">
<tree string="Prints">
<field name="cups_jobid" />
<field name="name" colspan="2"/>
<field name="state" />
<field name="date" />
</tree>
</field>
</record>
<record model="ir.actions.act_window" id="view_account_fiscalgr_print_tree_form">
<field name="name">Fiscal Prints</field>
<field name="res_model">account.fiscalgr.print</field>
@@ -53,6 +67,19 @@
</xpath>
</field>
</record>
<record forcecreate="True" id="ir_cron_scheduler_action" model="ir.cron">
<field name="name">Check fiscal print result</field>
<field eval="False" name="active"/>
<field name="user_id" ref="base.user_root"/>
<field name="interval_number">1</field>
<field name="interval_type">minutes</field>
<field name="numbercall">-1</field>
<field eval="False" name="doall"/>
<field name="model">account.fiscalgr.print</field>
<field name="function">check_results</field>
<field name="args"></field>
</record>
</data>
</openerp>
+71 -3
View File
@@ -28,18 +28,22 @@ class fiscal_print(osv.osv):
_columns = {
'hash': fields.char('Secure hash', size=40, readonly=True,),
'date': fields.char('Print date', size=10, readonly=True,),
'state': fields.selection([('unknown','Unknown'),
('queued','Queued'), ('printed','Printed'), ('error','Failed')],
'State', readonly = True, required = True),
'machine_id': fields.char('Machine ID', size=12, readonly=True,),
'day_no': fields.integer('Daily sequence', readonly=True),
'total_no': fields.integer('Total sequence', readonly=True),
'cups_jobid': fields.integer('Job ID', readonly=True, required=True, help="CUPS job id"),
'name': fields.char('Title',size=200,required=True, readonly=True,help="Title of the CUPS job, typically the invoice reference."),
'cups_msg': fields.text('CUPS message',help="This is the message returned by cups, if the printing fails."),
'cups_msg': fields.text('CUPS message',help="This is the message returned by cups, if the printing fails.", readonly=True),
'report': fields.many2one('ir.actions.report.xml', 'Report', required=True,readonly=True),
}
_defaults = {
'state' : lambda *a: 'unknown',
}
def _print_fiscal(self,cr,uid,report_id,title,format,content,printer='Forol',copies=1,context=None):
def _print_fiscal(self,cr,uid,report_id,title,format,content,printer=False,copies=1,context=None):
import tempfile
import os
if not printer:
@@ -64,7 +68,8 @@ class fiscal_print(osv.osv):
os.unlink(fp_name)
if job:
print 'Created job %d'% job
fprn=self.create(cr,uid,{'cups_jobid':job,'name':title,'report':report_id},context)
fprn=self.create(cr,uid,{'cups_jobid':job,'name':title,'report':report_id, 'state': 'queued'},context)
self._set_pooler_active(cr,uid,True)
return True
else:
raise Exception(_('Cannot print at printer %s')%printer)
@@ -107,6 +112,69 @@ class fiscal_print(osv.osv):
#return False
return False
def check_results(self,cr,uid,context=None):
logger = netsvc.Logger()
logger.notifyChannel('fiscalgr-prints', netsvc.LOG_DEBUG,
'Checking for pending fiscal prints.')
if not context:
context={}
try:
import cups
except:
raise Exception(_('Cannot talk to cups, please install pycups'))
ccon = cups.Connection()
open_prints=self.search(cr,uid,[('state','=','queued')])
if not open_prints:
self._set_pooler_active(cr,uid,False)
return
logger.notifyChannel('fiscalgr-prints', netsvc.LOG_DEBUG,
'Found %d pending fiscal prints.'%len(open_prints))
prints = self.read(cr,uid,open_prints,['state','title','cups_jobid'])
pending_jobs = len(prints)
for pjob in prints:
jats = ccon.getJobAttributes(pjob['cups_jobid'])
ndata = {}
print "Job %d attributes" % pjob['cups_jobid'],jats
if 'job-printer-state-message' in jats:
jpsm = jats['job-printer-state-message']
else:
jpsm = ''
if jats['job-state'] == cups.IPP_OK:
pending_jobs -= 1
if jpsm.startswith('eafdSigns-1:'):
ndata['hash'] = jpsm[12:]
ndata['state'] = 'printed'
#TODO more fields
else:
# job is still pending
ndata['cups_msg'] = jpsm
if ndata:
self.write(cr,uid,pjob['id'],ndata)
self._set_pooler_active(cr,uid,(pending_jobs > 0))
return True
def _set_pooler_active(self,cr,uid,active=True):
'''We ask the pooler to periodically check our jobs
'''
logger = netsvc.Logger()
obj = self.pool.get('ir.cron')
pids = obj.search(cr,uid,[('model','=',self._name),('function','=','check_results')])
if len(pids) != 1 :
logger.notifyChannel('fiscalgr-prints', netsvc.LOG_WARNING,
'Found %d pooler jobs instead of 1.'%len(pids))
print "trying to update the state"
obj.write(cr,uid,pids[0],{'active': active})
return True
fiscal_print()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: