Exemplo n.º 1
0
 def test_01_FiltrosOVR_temtg(self):
     setor1 = Setor()
     setor1.id = 567567
     setor1.nome = 'Setor 567'
     session.add(setor1)
     ovr1 = self.create_OVR_campos('R1', 'U1', 'C1', '2020-05-01', 'teste1',
                                   setor1)
     try:
         session.refresh(ovr1)
         ovrs = get_ovr_filtro(session, {})
         assert isinstance(ovrs, list)
         assert len(ovrs) == 1
         ovrs = get_ovr_filtro(session, {'temtg': True})
         assert isinstance(ovrs, list)
         assert len(ovrs) == 0
         tgovr = TGOVR()
         tgovr.qtde = 10
         tgovr.valor = 100
         tgovr.afrfb = ovr1.user_name
         tgovr.numerolote = '1234'
         tgovr.descricao = 'nonono'
         tgovr.ovr_id = ovr1.id
         session.add(tgovr)
         session.commit()
         ovrs = get_ovr_filtro(session, {'temtg': True})
         assert isinstance(ovrs, list)
         assert len(ovrs) == 1
     finally:
         session.delete(tgovr)
         session.delete(ovr1)
         session.commit()
Exemplo n.º 2
0
 def test_8itemtg_tg_ovr_get(self):
     self.login()
     self._case('GET',
                '/api/itenstg/0',
                status_code=404,
                query_dict={},
                headers=self.headers)
     ovr1 = self.create_ovr('1')
     tg = TGOVR()
     tg.ovr_id = 1
     tg.numerolote = 'abcde'
     tg.descricao = 'teste'
     self.db_session.add(tg)
     self.db_session.commit()
     self.db_session.refresh(tg)
     itemtg = ItemTG()
     itemtg.numero = 1
     itemtg.tg_id = tg.id
     itemtg.descricao = 'teste'
     self.db_session.add(itemtg)
     self.db_session.commit()
     self.db_session.refresh(itemtg)
     r = self._case('GET',
                    '/api/itenstg/%s' % tg.id,
                    status_code=200,
                    headers=self.headers)
     assert len(r) == 1
     assert r[0]['id'] == itemtg.id
     assert r[0]['tg_id'] == tg.id
Exemplo n.º 3
0
 def test_7itemtg_get(self):
     self.login()
     self._case('GET',
                '/api/itemtg/0',
                status_code=404,
                query_dict={},
                headers=self.headers)
     self._case('POST',
                '/api/itenstg',
                status_code=404,
                query_dict={'id': 0},
                headers=self.headers)
     tg = TGOVR()
     tg.ovr_id = 1
     tg.numerolote = 'abcde'
     tg.descricao = 'teste'
     self.db_session.add(tg)
     self.db_session.commit()
     self.db_session.refresh(tg)
     itemtg = ItemTG()
     itemtg.numero = 1
     itemtg.tg_id = tg.id
     itemtg.descricao = 'teste'
     self.db_session.add(itemtg)
     self.db_session.commit()
     self.db_session.refresh(itemtg)
     r = self._case('GET',
                    '/api/itemtg/%s' % itemtg.id,
                    status_code=200,
                    headers=self.headers)
     assert r['id'] == itemtg.id
     assert r['descricao'] == itemtg.descricao
     self._case('POST',
                '/api/itenstg',
                status_code=200,
                query_dict={'id': itemtg.id},
                headers=self.headers)
     self._case('POST',
                '/api/itenstg',
                status_code=200,
                query_dict={'numero': itemtg.numero},
                headers=self.headers)
     self._case('POST',
                '/api/itenstg',
                status_code=200,
                query_dict={'descricao': itemtg.descricao},
                headers=self.headers)
Exemplo n.º 4
0
 def test_6tg_ovr_get(self):
     self.login()
     self._case('GET',
                '/api/tgs/0',
                status_code=404,
                query_dict={},
                headers=self.headers)
     ovr1 = self.create_ovr('1')
     tg = TGOVR()
     tg.ovr_id = ovr1.id
     tg.numerolote = 'abcd'
     tg.descricao = 'teste'
     self.db_session.add(tg)
     self.db_session.commit()
     self.db_session.refresh(tg)
     r = self._case('GET',
                    '/api/tgs/%s' % ovr1.id,
                    status_code=200,
                    headers=self.headers)
     assert len(r) == 1
     assert r[0]['id'] == tg.id
     assert r[0]['ovr_id'] == tg.ovr_id
Exemplo n.º 5
0
 def test_OVR_Evento(self):
     ovr = self.create_OVR_valido()
     session.refresh(ovr)
     tg = TGOVR()
     tg.descricao = 'teste10'
     tg.qtde = 10
     tg.ovr_id = ovr.id
     tg.numerolote = 'CCNU1234567'
     session.add(tg)
     session.commit()
     # test xls file
     file_name = os.path.join(os.path.dirname(__file__), 'teste.xls')
     alert = importa_planilha_tg(session, tg, file_name)
     assert 'Campo procedencia(País Procedência, *****) não encontrado' in alert
     # test csv file
     file_name = os.path.join(os.path.dirname(__file__), 'teste_csv')
     alert = importa_planilha_tg(session, tg, file_name)
     assert 'Campo procedencia(País Procedência, *****) não encontrado' in alert
     # test ods file
     file_name = os.path.join(os.path.dirname(__file__), 'teste.ods')
     alert = importa_planilha_tg(session, tg, file_name)
     assert 'Campo procedencia(País Procedência, *****) não encontrado' in alert
     # test Exception file reading
     file_name_wrong = os.path.join(os.path.dirname(__file__), '123.abc')
     with self.assertRaises(Exception):
         alert = importa_planilha_tg(session, tg, file_name_wrong)
     # test broken file
     file_name = os.path.join(os.path.dirname(__file__),
                              'teste_sem_coluna_ncm.ods')
     alert = importa_planilha_tg(session, tg, file_name)
     assert alert.find("ncm") > 0
     # test broken file
     file_name = os.path.join(os.path.dirname(__file__),
                              'teste_sem_coluna_valor.ods')
     alert = importa_planilha_tg(session, tg, file_name)
     assert alert.find("valor") > 0
Exemplo n.º 6
0
def processa_dirs():
    listadir = os.listdir(RAIZ)
    # print(listadir)
    validos = 0
    invalidos = 0
    for arq in listadir:
        if arq[:2] == 'TG':
            # print(caminho)
            caminho_tgs = os.path.join(RAIZ, arq)
            for subarq in sorted(os.listdir(caminho_tgs)):
                caminho_tg = os.path.join(caminho_tgs, subarq)
                if os.path.isdir(caminho_tg):
                    pdf = pegaarquivo_dirTG(caminho_tg, ['pdf'], 'CE')
                    planilha = pegaarquivo_dirTG(caminho_tg, ['ods', 'xls', 'xlsx'])
                    print(caminho_tg, pdf, planilha)
                    if not pdf:
                        continue
                    ce = ''
                    ce, cnpj = processa_PDF_CE(os.path.join(caminho_tg, pdf))
                    print(ce, cnpj)
                    if not ce.isnumeric():
                        print(f'Erro para processar CE no caminho: {caminho_tg} CE: {ce}')
                        invalidos += 1
                        ce = f'Inválido {invalidos}'
                    # Cria Ficha e TG
                    tgovr = db_session.query(TGOVR).filter(TGOVR.numerolote == ce).one_or_none()
                    if not tgovr:
                        ovr = db_session.query(OVR).filter(OVR.numeroCEmercante == ce). \
                            one_or_none()
                        if not ovr:
                            ovr = OVR()
                            ovr.numeroCEmercante = ce
                            ovr.cnpj_fiscalizado = ''.join([s for s in cnpj if s.isnumeric()])
                            db_session.add(ovr)
                            create_time = datetime.fromtimestamp(os.path.getmtime(
                                os.path.join(caminho_tg, pdf)))
                            ovr.datahora = create_time
                            ovr.create_date = create_time
                            ovr.setor_id = '1'
                            db_session.commit()
                            db_session.refresh(ovr)
                        tgovr = TGOVR()
                        tgovr.ovr_id = ovr.id
                        tgovr.numerolote = ce
                        create_time = datetime.fromtimestamp(os.path.getmtime(
                            os.path.join(caminho_tg, planilha)))
                        tgovr.create_date = create_time
                        tgovr.descricao = 'Importado automaticamente'
                        db_session.add(tgovr)
                        db_session.commit()
                        db_session.refresh(tgovr)
                    if len(tgovr.itenstg) > 0:
                        print(f'Pulando TG {tgovr.id} OVR {tgovr.ovr_id} porque já possui itens')
                        continue
                    if planilha:
                        alertas = importa_planilha_tg(db_session, tgovr,
                                                      os.path.join(caminho_tg, planilha))
                        if alertas:
                            print(alertas)
                            invalidos += 1
                        else:
                            print(f'Criou ItensTG no OVR id {tgovr.ovr_id}')
                            validos += 1
    print(validos, invalidos)