#!/usr/bin/env python

import MySQLdb
import xlwt
import os

db = MySQLdb.connect("localhost","asterisk","1GGLfeE6*LL$","asteriskcdr")
cursor = db.cursor()
cursor.execute('select userfield as dst,date_format(start, "%h:%i:%s") hora, date_format(start,"%Y-%m-%d") as fecha ,duration,src from cdr where dcontext="outboundfax" and date_format(start,"%Y-%m-%d")  between date_sub(curdate(),interval day(date_sub(curdate(), interval 1 day))day) and date_sub(curdate(), interval 1 day)')
myresults =  cursor.fetchall()

workbook = xlwt.Workbook()
worksheet = workbook.add_sheet("Fax Salientes")
#bold = workbook.add_format({'bold': 1})
worksheet.write(1,0,'destino')
worksheet.write(1,1,'hora')
worksheet.write(1,2,'fecha')
worksheet.write(1,3,'duracion')
worksheet.write(1,4,'origen')
row_index = 2
for dst, hora, fecha, duration, src in (myresults):
  worksheet.write(row_index,0,dst)
  worksheet.write(row_index,1,hora)
  worksheet.write(row_index,2,fecha)
  worksheet.write(row_index,3,duration)
  worksheet.write(row_index,4,src)
  row_index +=1
workbook.save('faxEnviados.xls')

os.system("./envioCDRFax.py faxEnviados.xls vicente.falco@gmail.com 'CDR FAX'")
