def _send_fax(self, cr, uid, datas, context): # Number of sent FAX nbOk = 0 nbError = 0 logger = netsvc.Logger() pool = pooler.get_pool(cr.dbname) # Get file attachement and prepare for sending if datas['form']['attachment']: fFax = "/tmp/hfx_attach" + str(random.randint(0,100)) try: fc = open(fFax, "w") fc.write(base64.decodestring(datas['form']['attachment'])) fc.close() except IOError: raise wizard.except_wizard('[HeoFAX - Error]', 'Unable to open temporary file for file attachement: %s !!!' % fFax) cmd['listFile'].append(fFax) if debug: logger.notifyChannel("[HeoFAX - Debug]", netsvc.LOG_INFO, " File attached: %s" % " ".join(cmd['listFile'])) partners = pool.get('res.partner').browse(cr, uid, datas['ids'], context) for partner in partners: for adr in partner.address: if adr.name: name = adr.name + ' (' + partner.name + ')' else: name = partner.name if adr.fax: cmd['to_number'] = adr.fax cmd['to_company'] = partner.name cmd['to_name'] = name # Build FAX command line cmdFax = hnm_heofax_lib.build_fax_cmd(self, cr, uid, datas, cmd) cmdFax = cmdFax.encode('utf8') if debug: # For trace only logger.notifyChannel("[HeoFAX - Debug]", netsvc.LOG_INFO, " Fax for: %s with fax nb: %s" % (name,cmd['to_number'])) logger.notifyChannel("[HeoFAX - Debug]",netsvc.LOG_INFO, " Command used: %s" % cmdFax) pipe = subprocess.Popen(cmdFax, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) sts = pipe.wait() out = pipe.stdout.read() + pipe.stderr.read() if debug: logger.notifyChannel("[HeoFAX - Debug]",netsvc.LOG_INFO, " ID: %d:%s" % (sts,out)) # increment number of fax nbOk += 1 # Record event if selected if datas['form']['recordEvent']: ret = pool.get('res.partner.event').create(cr, uid, {'name':'Sent to FAX server: '+ datas['form']['subject'], 'partner_id':partner.id,'user_id':cmd['user'].id}) # Test if sending to first contact only if datas['form']['firstonly']: break else: nbError += 1 logger.notifyChannel("[HeoFAX - Info]",netsvc.LOG_INFO, " No fax number for name: %s" % name) if len(partners) < 1: raise wizard.except_wizard('[HeoFAX - Error]', ' No fax number for name: %s' % name) # Remove working files for f in cmd['listFile']: os.remove(f) if debug: logger.notifyChannel("[HeoFAX - Debug]",netsvc.LOG_INFO, " Removed file: %s" % f) cmd['listFile'] = [] return {'fax_sent': nbOk, 'fax_error': nbError}
def _mass_fax_send(self, cr, uid, data, context): # Number of sent FAX nbOk = 0 nbError = 0 logger = netsvc.Logger() pool = pooler.get_pool(cr.dbname) sale_orders = pool.get('account.invoice').browse(cr,uid,data['ids']) if debug: logger.notifyChannel("[HeoFAX - Debug]", netsvc.LOG_INFO, " ID(s) selected: %s" % data['ids']) for so in sale_orders: # Get recipient name and/or company name if so.address_contact_id.name: name = so.address_contact_id.name + ' (' + so.partner_id.name + ')' else: name = so.partner_id.name if so.address_contact_id.fax: # Make sale order and prepare for sending print data['form']['model'] service = netsvc.LocalService('report.' + data['form']['model']) (result, format) = service.create(cr, uid, [so.id], data, context) fFax = "/tmp/hfx_attach" + str(random.randint(0,100)) try: fc = open(fFax, "w") fc.write(result) fc.close() except IOError: raise wizard.except_wizard('[HeoFAX - Error]', 'Unable to open temporary file to prepare file attachement: %s !!!' % fFax) cmd['listFile'].append(fFax) if debug: logger.notifyChannel("[HeoFAX - Debug]", netsvc.LOG_INFO, " File attached: %s" % " ".join(cmd['listFile'])) # Preparing other information for sending cmd['to_number'] = so.address_contact_id.fax cmd['to_company'] = so.partner_id.name cmd['to_name'] = name # Build FAX command line cmdFax = hnm_heofax_lib.build_fax_cmd(self, cr, uid, data, cmd) cmdFax = cmdFax.encode('utf8') if cmdFax: if debug: # For trace only logger.notifyChannel("[HeoFAX - Debug]", netsvc.LOG_INFO, " Fax for: %s with fax nb: %s" % (name,cmd['to_number'])) logger.notifyChannel("[HeoFAX - Debug]",netsvc.LOG_INFO, " Command used: %s" % cmdFax) pipe = subprocess.Popen(cmdFax, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) sts = pipe.wait() out = pipe.stdout.read() + pipe.stderr.read() if debug: logger.notifyChannel("[HeoFAX - Debug]",netsvc.LOG_INFO, " ID: %d:%s" % (sts,out)) # increment number of fax nbOk += 1 # Record event if selected if data['form']['recordEvent']: ret = pool.get('res.partner.event').create(cr, uid, {'name':'Sent to FAX server \''+ so.name + '\'.', 'description': 'With report model \'' + data['form']['model'] + '\'.', 'partner_id':so.partner_id.id,'user_id':cmd['user'].id}) # Remove working files for f in cmd['listFile']: os.remove(f) if debug: logger.notifyChannel("[HeoFAX - Debug]",netsvc.LOG_INFO, " Removed file: %s" % f) cmd['listFile'] = [] else: nbError += 1 logger.notifyChannel("[HeoFAX - Info]",netsvc.LOG_INFO, " No fax number for name: %s" % name) return {'fax_sent': nbOk, 'fax_error': nbError}