class Reporter( object ): def __init__( self, reportname, data, tipo ): self.doc = SimpleDocTemplate(reportname, pagesize=A4) self.data = data self.elements = list() self.logo = 'logo.png' self.DBM = DBManager() self.tipo = tipo def __addHead( self ): if self.tipo == "stock": styles = getSampleStyleSheet() ptext = '<font size=14>Reporte estado de Stock</font>' styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY)) self.elements.append( Paragraph(ptext, styles["Normal"]) ) self.elements.append(Spacer(1, 12)) elif self.tipo == "ganancia": styles = getSampleStyleSheet() ptext = '<font size=14>Reporte de ganancias de Tifosi (%s, %s)</font>' % ( self.data[0], self.data[1] ) styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY)) self.elements.append( Paragraph(ptext, styles["Normal"]) ) self.elements.append(Spacer(1, 12)) def __addTable( self ): tt = list() if self.tipo == "ganancia": data = self.DBM.getCuentas(self.data[0], self.data[1]) total = 0 tt.append( ['Fecha', 'Descripción', 'Cantidad', 'Precio'] ) for producto in data: tt.append( [producto[0], producto[1], producto[2], "$ %.2f" % float( producto[4] ) ] ) total += float( producto[4] ) tt.append( ["TOTAL" , "", "", "$ %.2f" % total] ) elif self.tipo == "stock": total = 0 tt.append( ['Código', 'Descripción', 'Marca', 'Precio', 'Punto Pedido', 'Stock'] ) for producto in self.data: if producto[1] != '1000': tt.append( [ producto[1], producto[2], producto[3], "$ %.2f" % float( producto[4] ), producto[5], producto[6] ] ) total += float( producto[4] * producto[6] ) tabla = Table(tt) if self.tipo == "ganancia": tabla.setStyle( TableStyle([ ('ALIGN', (1,1), (-2, -2), 'RIGHT'), ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), ('BOX', (0,0), (-1,-1), 0.55, colors.black), ('TEXTCOLOR',(0,-1),(-1,-1),colors.yellow), ('BACKGROUND',(0,-1),(-1,-1),colors.red), ('BACKGROUND',(0,0),(-1,0),colors.gray) ])) elif self.tipo == "stock": tabla.setStyle( TableStyle([ ('ALIGN', (1,1), (-1, -1), 'RIGHT'), ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), ('BOX', (0,0), (-1,-1), 0.55, colors.black), ('BACKGROUND',(0,0),(-1,0),colors.gray) ])) self.elements.append( tabla ) if self.tipo == "ganancia": styles = getSampleStyleSheet() ptext = '<font size=10>Total de ventas: %s </font>' % len( data ) self.elements.append(Spacer(1, 12)) styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY)) self.elements.append( Paragraph(ptext, styles["Normal"]) ) elif self.tipo == "stock": styles = getSampleStyleSheet() ptext = '<font size=10>Productos en Stock: %s </font>' % len( self.data ) self.elements.append(Spacer(1, 12)) styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY)) self.elements.append( Paragraph(ptext, styles["Normal"]) ) styles = getSampleStyleSheet() ptext = '<font size=10>Dinero en Stock: $ %.2f </font>' % total styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY)) self.elements.append( Paragraph(ptext, styles["Normal"]) ) def doReport( self ): self.__addHead() self.__addTable() self.doc.build( self.elements )
class CanchasTabFinanzas(wx.Panel): def __init__(self, parent): wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY) self.DBM = DBManager() self.vbox = wx.BoxSizer( wx.VERTICAL ) hbox1 = wx.BoxSizer( wx.HORIZONTAL ) image1 = wx.Image('calendar.png', wx.BITMAP_TYPE_ANY).ConvertToBitmap() btn_cal = wx.BitmapButton(self, id=-1, bitmap=image1, size=(40,30)) image2 = wx.Image('print.png', wx.BITMAP_TYPE_ANY).ConvertToBitmap() btn_prt = wx.BitmapButton(self, id=-1, bitmap=image2, size=(40,30)) image3 = wx.Image('money.png', wx.BITMAP_TYPE_ANY).ConvertToBitmap() btn_inicio = wx.BitmapButton(self, id=-1, bitmap=image3, size=(40,30)) image4 = wx.Image('cashRegister.png', wx.BITMAP_TYPE_ANY).ConvertToBitmap() btn_gasto = wx.BitmapButton(self, id=-1, bitmap=image4, size=(40,30)) #image3 = wx.Image('venta.png', wx.BITMAP_TYPE_ANY).ConvertToBitmap() #btn_vta = wx.BitmapButton(self, id=-1, bitmap=image3, size=(40,30)) self.desde = None self.hasta = None self.Bind(wx.EVT_BUTTON, self.OnCalendar, btn_cal) #self.Bind(wx.EVT_BUTTON, self.OnVenta, btn_vta) self.Bind(wx.EVT_BUTTON, self.OnPrint, btn_prt) self.Bind(wx.EVT_BUTTON, self.OnInicio, btn_inicio) self.Bind(wx.EVT_BUTTON, self.OnGasto, btn_gasto) hbox1.Add(btn_cal) hbox1.Add(btn_prt) hbox1.Add(btn_inicio) hbox1.Add(btn_gasto) #hbox1.Add(btn_vta) self.vbox.Add(hbox1) self.hbox2 = wx.BoxSizer( wx.HORIZONTAL ) self.list_ctrl = wx.ListCtrl(self, style=wx.LC_REPORT) self.hbox2.Add(self.list_ctrl, 2, flag=wx.EXPAND) self.vbox.Add(self.hbox2, 2, wx.EXPAND) self.TOTAL = 0 self.SetSizer(self.vbox) self.__generateContent() Publisher().subscribe(self.OnExternalCalSelected, ("dates")) Publisher().subscribe(self.OnTabSelected , ("tab_cuentas_update")) def __generateContent( self, desde=None, hasta=None): cuentas = self.DBM.getCuentas(desde, hasta) egresos = self.DBM.getEgresos(desde, hasta) index = 0 self.list_ctrl.InsertColumn(0, "Hora") self.list_ctrl.InsertColumn(1, "Producto") self.list_ctrl.InsertColumn(2, "Cantidad") self.list_ctrl.InsertColumn(3, "Precio") self.list_ctrl.InsertColumn(4, "Total") for cuenta in cuentas+egresos: self.list_ctrl.InsertStringItem(index, cuenta[0]) self.list_ctrl.SetStringItem(index, 1, cuenta[1]) if cuenta[1] == "Inicio de caja": self.list_ctrl.SetStringItem(index, 2, '-') self.list_ctrl.SetStringItem(index, 3, '-') else: self.list_ctrl.SetStringItem(index, 3, "$ %s" % str(cuenta[3])) self.list_ctrl.SetStringItem(index, 2, str(cuenta[2])) self.list_ctrl.SetStringItem(index, 4, "$ %s" % str(cuenta[4])) self.TOTAL += cuenta[4] if index % 2: self.list_ctrl.SetItemBackgroundColour(index, "white") else: self.list_ctrl.SetItemBackgroundColour(index, "gray") index += 1 self.list_ctrl.InsertStringItem(index, 'TOTAL') self.list_ctrl.SetStringItem(index, 1, '') self.list_ctrl.SetStringItem(index, 2, '') self.list_ctrl.SetStringItem(index, 3, '') self.list_ctrl.SetStringItem(index, 4, '$ %s' % str(self.TOTAL)) self.list_ctrl.SetItemBackgroundColour(index, "red") self.list_ctrl.SetItemTextColour(index, "yellow") def OnExternalCalSelected( self, evt ): desde = evt.data[0].__str__().split(' ')[0] hasta = evt.data[1].__str__().split(' ')[0] self.desde = desde self.hasta = hasta self.TOTAL = 0 self.list_ctrl.ClearAll() self.__generateContent( desde, hasta ) def OnGasto( self, evt ): Gasto(self, -1, "Egreso de caja") self.list_ctrl.ClearAll() self.__generateContent() def OnCalendar( self, evt ): MyCalendar(self, -1, "Seleccion rango de fecha") def OnVenta( self, evt ): SeleccionarProducto(self, -1, "Seleccionar producto") def OnInicio( self, evt ): IniciarCaja(self, -1, "Inicio de caja") def OnPrint( self, evt ): this_time = datetime.datetime.now().strftime("%d/%m/%y") nombre = "ventas"+this_time+".pdf " #import pdb;pdb.set_trace() if self.desde == None or self.hasta == None: wx.MessageBox('Primero debe seleccionar un rango para el reporte', 'Error al generar el reporte', wx.OK | wx.ICON_ERROR) else: dlg = wx.FileDialog( self, message="Choose a file", defaultFile=nombre, wildcard="*.pdf", style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR ) if dlg.ShowModal() == wx.ID_OK: paths = dlg.GetPaths() path = None for path in paths: path = path dlg.Destroy() RPTR = Reporter( path, (self.desde, self.hasta), "ganancia" ) RPTR.doReport() def OnTabSelected( self, evt): self.list_ctrl.ClearAll() self.__generateContent()