Skip to content

Commit

Permalink
Automated report
Browse files Browse the repository at this point in the history
  • Loading branch information
gsampallo committed Jul 5, 2019
1 parent a2d7c8e commit f02c004
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.xls
*.pyc
13 changes: 13 additions & 0 deletions Movimiento.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Movimiento(object):

def __init__(self,lista):

self.fecha = lista[0]
self.producto_id = lista[1]
self.producto = lista[2]
self.tipo_movimiento = lista[3]
self.cantidad = str(lista[4])


def listar(self):
print (self.fecha,self.producto_id,self.producto,self.tipo_movimiento,self.cantidad)
37 changes: 37 additions & 0 deletions MovimientosToExcel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import xlwt
from datetime import datetime

class MovimientosToExcel:

def __init__(self,name):
self.wb = xlwt.Workbook()
self.ws = self.wb.add_sheet(name,cell_overwrite_ok=True)

self.ws.write(0, 0, name)

columnas = ["Fecha",
"Producto ID",
"Producto",
"Tipo Movimiento",
"Cantidad"]

c = 0
for columna in columnas:
self.ws.write(1, c, columna)
c = c + 1

self.fila = 2

def agregarItem(self,item):
self.ws.write(self.fila, 0, item.fecha)
self.ws.write(self.fila, 1, item.producto_id)
self.ws.write(self.fila, 2, item.producto)
self.ws.write(self.fila, 3, item.tipo_movimiento)
self.ws.write(self.fila, 4, item.cantidad)

self.fila = self.fila + 1

def guardarPlanilla(self,archivo):
self.wb.save(archivo)
print ("Generado")

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# automated_report_python
An easy way to generate automated report with Python. Create spreadsheet and email them.
An easy way to generate automated report with Python. Create spreadsheet and email them.
53 changes: 53 additions & 0 deletions reporteSemanal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from datetime import datetime, timedelta
import mysql.connector
from mysql.connector import errorcode
from Movimiento import Movimiento
from MovimientosToExcel import MovimientosToExcel

config = {
'user': 'root',
'password': '',
'host': 'localhost',
'database': 'stock',
'raise_on_warnings': True,

}

cursor = 0
try:
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor()

query = "SELECT \
DATE_FORMAT(movimientos.fecha,'%d-%m-%Y') as fecha, \
productos.producto_id, \
productos.descripcion, \
movimientos.tipo_movimiento, \
movimientos.cantidad \
FROM \
movimientos \
INNER JOIN productos ON movimientos.producto_id = productos.producto_id \
WHERE WEEKOFYEAR(movimientos.fecha) = (WEEKOFYEAR(curdate())-1) \
ORDER BY productos.descripcion,movimientos.fecha"

cursor.execute(query)
m2e = MovimientosToExcel('Weekly Report')

for fila in cursor:
print(fila)
movimiento = Movimiento(fila)
movimiento.listar()

m2e.agregarItem(movimiento)

m2e.guardarPlanilla("reporte_semanal.xls")

except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
else:
cnx.close()
53 changes: 53 additions & 0 deletions sql/stock.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Navicat MySQL Data Transfer
Source Server : Localhost
Source Server Version : 50717
Source Host : localhost:3306
Source Database : stock
Target Server Type : MYSQL
Target Server Version : 50717
File Encoding : 65001
Date: 2019-07-04 21:20:51
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `movimientos`
-- ----------------------------
DROP TABLE IF EXISTS `movimientos`;
CREATE TABLE `movimientos` (
`mov_id` int(4) NOT NULL AUTO_INCREMENT,
`fecha` date NOT NULL,
`producto_id` int(3) NOT NULL,
`tipo_movimiento` varchar(1) NOT NULL,
`cantidad` int(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`mov_id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of movimientos
-- ----------------------------
INSERT INTO `movimientos` VALUES ('1', '2019-06-24', '1', 'A', '3');

-- ----------------------------
-- Table structure for `productos`
-- ----------------------------
DROP TABLE IF EXISTS `productos`;
CREATE TABLE `productos` (
`producto_id` int(3) NOT NULL AUTO_INCREMENT,
`descripcion` varchar(20) NOT NULL,
`stock` int(3) NOT NULL,
PRIMARY KEY (`producto_id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of productos
-- ----------------------------
INSERT INTO `productos` VALUES ('1', 'Producto A', '1');
INSERT INTO `productos` VALUES ('2', 'Producto B', '4');
INSERT INTO `productos` VALUES ('3', 'Producto C', '5');
INSERT INTO `productos` VALUES ('4', 'Producto D', '0');

0 comments on commit f02c004

Please sign in to comment.