2024-11-26 10:57:25 -03:00
|
|
|
import mysql.connector
|
|
|
|
|
from mysql.connector import Error
|
2025-01-08 00:19:49 -03:00
|
|
|
# from config.db_config import db_config
|
|
|
|
|
from sqlalchemy import create_engine, Column, Integer, String, Boolean, Numeric, Date, ForeignKey
|
|
|
|
|
from sqlalchemy.orm import relationship
|
|
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
2024-11-26 10:57:25 -03:00
|
|
|
|
2025-01-08 00:19:49 -03:00
|
|
|
Base = declarative_base()
|
|
|
|
|
|
|
|
|
|
engine = create_engine('sqlite:///database.db', echo=True)
|
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
|
|
|
|
def get_db_connection(db_config):
|
2024-11-26 10:57:25 -03:00
|
|
|
try:
|
|
|
|
|
connection = mysql.connector.connect(**db_config)
|
|
|
|
|
return connection
|
|
|
|
|
except Error as e:
|
|
|
|
|
print(f"Error connecting to database: {e}")
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def execute_query(query, params=None):
|
2025-01-08 00:19:49 -03:00
|
|
|
connection = get_db_connection('')
|
2024-11-26 10:57:25 -03:00
|
|
|
if connection is None:
|
|
|
|
|
return None
|
|
|
|
|
try:
|
|
|
|
|
cursor = connection.cursor(dictionary=True)
|
|
|
|
|
cursor.execute(query, params)
|
|
|
|
|
connection.commit()
|
|
|
|
|
return cursor
|
|
|
|
|
except Error as e:
|
|
|
|
|
print(f"Error executing query: {e}")
|
|
|
|
|
return None
|
|
|
|
|
finally:
|
|
|
|
|
if connection.is_connected():
|
|
|
|
|
cursor.close()
|
2025-01-08 00:19:49 -03:00
|
|
|
connection.close()
|
|
|
|
|
|
|
|
|
|
class Militante(Base):
|
|
|
|
|
__tablename__ = 'militantes'
|
|
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
|
|
|
nome = Column(String(100), nullable=False)
|
|
|
|
|
cpf = Column(String(14), unique=True)
|
|
|
|
|
email = Column(String(100), unique=True)
|
|
|
|
|
telefone = Column(String(15))
|
|
|
|
|
endereco = Column(String(255))
|
|
|
|
|
filiado = Column(Boolean, default=False)
|
|
|
|
|
|
|
|
|
|
cotas_mensais = relationship("CotaMensal", back_populates="militante")
|
|
|
|
|
pagamentos = relationship("Pagamento", back_populates="militante")
|
|
|
|
|
materiais_vendidos = relationship("MaterialVendido", back_populates="militante")
|
|
|
|
|
vendas_jornais = relationship("VendaJornalAvulso", back_populates="militante")
|
|
|
|
|
assinaturas = relationship("AssinaturaAnual", back_populates="militante")
|
|
|
|
|
|
|
|
|
|
class CotaMensal(Base):
|
|
|
|
|
__tablename__ = 'cotas_mensais'
|
|
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
|
|
|
militante_id = Column(Integer, ForeignKey('militantes.id'))
|
|
|
|
|
valor_antigo = Column(Numeric(10, 2), nullable=False)
|
|
|
|
|
valor_novo = Column(Numeric(10, 2), nullable=False)
|
|
|
|
|
data_alteracao = Column(Date, nullable=False)
|
|
|
|
|
|
|
|
|
|
militante = relationship("Militante", back_populates="cotas_mensais")
|
|
|
|
|
|
|
|
|
|
class TipoPagamento(Base):
|
|
|
|
|
__tablename__ = 'tipos_pagamento'
|
|
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
|
|
|
descricao = Column(String(100), nullable=False)
|
|
|
|
|
|
|
|
|
|
pagamentos = relationship("Pagamento", back_populates="tipo_pagamento")
|
|
|
|
|
|
|
|
|
|
class Pagamento(Base):
|
|
|
|
|
__tablename__ = 'pagamentos'
|
|
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
|
|
|
militante_id = Column(Integer, ForeignKey('militantes.id'))
|
|
|
|
|
tipo_pagamento_id = Column(Integer, ForeignKey('tipos_pagamento.id'))
|
|
|
|
|
valor = Column(Numeric(10, 2), nullable=False)
|
|
|
|
|
data_pagamento = Column(Date, nullable=False)
|
|
|
|
|
|
|
|
|
|
militante = relationship("Militante", back_populates="pagamentos")
|
|
|
|
|
tipo_pagamento = relationship("TipoPagamento", back_populates="pagamentos")
|
|
|
|
|
|
|
|
|
|
class TipoMaterial(Base):
|
|
|
|
|
__tablename__ = 'tipos_materiais'
|
|
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
|
|
|
descricao = Column(String(100), nullable=False)
|
|
|
|
|
|
|
|
|
|
materiais_vendidos = relationship("MaterialVendido", back_populates="tipo_material")
|
|
|
|
|
assinaturas = relationship("AssinaturaAnual", back_populates="tipo_material")
|
|
|
|
|
|
|
|
|
|
class MaterialVendido(Base):
|
|
|
|
|
__tablename__ = 'materiais_vendidos'
|
|
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
|
|
|
militante_id = Column(Integer, ForeignKey('militantes.id'))
|
|
|
|
|
tipo_material_id = Column(Integer, ForeignKey('tipos_materiais.id'))
|
|
|
|
|
descricao = Column(String(255), nullable=False)
|
|
|
|
|
valor = Column(Numeric(10, 2), nullable=False)
|
|
|
|
|
data_venda = Column(Date, nullable=False)
|
|
|
|
|
|
|
|
|
|
militante = relationship("Militante", back_populates="materiais_vendidos")
|
|
|
|
|
tipo_material = relationship("TipoMaterial", back_populates="materiais_vendidos")
|
|
|
|
|
|
|
|
|
|
class VendaJornalAvulso(Base):
|
|
|
|
|
__tablename__ = 'vendas_jornais_avulsos'
|
|
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
|
|
|
militante_id = Column(Integer, ForeignKey('militantes.id'))
|
|
|
|
|
quantidade = Column(Integer, nullable=False)
|
|
|
|
|
valor_total = Column(Numeric(10, 2), nullable=False)
|
|
|
|
|
data_venda = Column(Date, nullable=False)
|
|
|
|
|
|
|
|
|
|
militante = relationship("Militante", back_populates="vendas_jornais")
|
|
|
|
|
|
|
|
|
|
class AssinaturaAnual(Base):
|
|
|
|
|
__tablename__ = 'assinaturas_anuais'
|
|
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
|
|
|
militante_id = Column(Integer, ForeignKey('militantes.id'))
|
|
|
|
|
tipo_material_id = Column(Integer, ForeignKey('tipos_materiais.id'))
|
|
|
|
|
quantidade = Column(Integer, nullable=False)
|
|
|
|
|
valor_total = Column(Numeric(10, 2), nullable=False)
|
|
|
|
|
data_inicio = Column(Date, nullable=False)
|
|
|
|
|
data_fim = Column(Date, nullable=False)
|
|
|
|
|
|
|
|
|
|
militante = relationship("Militante", back_populates="assinaturas")
|
|
|
|
|
tipo_material = relationship("TipoMaterial", back_populates="assinaturas")
|
|
|
|
|
|
|
|
|
|
class Setor(Base):
|
|
|
|
|
__tablename__ = 'setores'
|
|
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
|
|
|
nome = Column(String(100), nullable=False)
|
|
|
|
|
|
|
|
|
|
relatorios_cotas = relationship("RelatorioCotasMensais", back_populates="setor")
|
|
|
|
|
relatorios_vendas = relationship("RelatorioVendasMateriais", back_populates="setor")
|
|
|
|
|
|
|
|
|
|
class ComiteCentral(Base):
|
|
|
|
|
__tablename__ = 'comites_centrais'
|
|
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
|
|
|
nome = Column(String(100), nullable=False)
|
|
|
|
|
|
|
|
|
|
relatorios_cotas = relationship("RelatorioCotasMensais", back_populates="comite")
|
|
|
|
|
relatorios_vendas = relationship("RelatorioVendasMateriais", back_populates="comite")
|
|
|
|
|
|
|
|
|
|
class RelatorioCotasMensais(Base):
|
|
|
|
|
__tablename__ = 'relatorio_cotas_mensais'
|
|
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
|
|
|
setor_id = Column(Integer, ForeignKey('setores.id'))
|
|
|
|
|
comite_id = Column(Integer, ForeignKey('comites_centrais.id'))
|
|
|
|
|
total_cotas = Column(Numeric(10, 2), nullable=False)
|
|
|
|
|
data_relatorio = Column(Date, nullable=False)
|
|
|
|
|
|
|
|
|
|
setor = relationship("Setor", back_populates="relatorios_cotas")
|
|
|
|
|
comite = relationship("ComiteCentral", back_populates="relatorios_cotas")
|
|
|
|
|
|
|
|
|
|
class RelatorioVendasMateriais(Base):
|
|
|
|
|
__tablename__ = 'relatorio_vendas_materiais'
|
|
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
|
|
|
setor_id = Column(Integer, ForeignKey('setores.id'))
|
|
|
|
|
comite_id = Column(Integer, ForeignKey('comites_centrais.id'))
|
|
|
|
|
total_vendas = Column(Numeric(10, 2), nullable=False)
|
|
|
|
|
data_relatorio = Column(Date, nullable=False)
|
|
|
|
|
|
|
|
|
|
setor = relationship("Setor", back_populates="relatorios_vendas")
|
|
|
|
|
comite = relationship("ComiteCentral", back_populates="relatorios_vendas")
|
|
|
|
|
|
|
|
|
|
Base.metadata.create_all(engine)
|