{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "318c9652", "metadata": {}, "outputs": [], "source": [ "import smtplib\n", "import ssl\n", "import pyodbc\n", "import configparser\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import matplotlib.dates as mdates\n", "import matplotlib.ticker as mtick\n", "import seaborn as sns\n", "from email.message import EmailMessage\n", "from email.utils import make_msgid\n", "from email.mime.image import MIMEImage\n", "from pathlib import Path\n", "from datetime import datetime, time" ] }, { "cell_type": "code", "execution_count": 2, "id": "b0d5aacc", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\joao.herculano\\AppData\\Local\\Temp\\ipykernel_15612\\775955969.py:22: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.\n", " datas = pd.read_sql(query, conn)\n" ] } ], "source": [ "config = configparser.ConfigParser()\n", "config.read(r\"C:\\Users\\joao.herculano\\GRUPO GINSENG\\Assistência Suprimentos - 2025\\CODIGOS\\relatório_improdutivo\\credenciais.ini\")\n", "\n", "# Conexão com o banco\n", "conn = pyodbc.connect(\n", " f\"DRIVER={{SQL Server}};\"\n", " f\"SERVER={config['banco']['host']},1433;\"\n", " f\"DATABASE=GINSENG;\"\n", " f\"UID={config['banco']['user']};\"\n", " f\"PWD={config['banco']['password']}\")\n", "\n", "# 1. Criar dados fictícios e gerar Excel\n", "query = '''\n", "SELECT \n", " cd.Ciclo,\n", " min(emh.data_estoque)\n", "FROM estoque_mar_historico emh\n", "INNER JOIN ciclos_data_2025 cd \n", " ON cd.[Date] = emh.data_estoque and emh.data_estoque > '2025-06-19'\n", "GROUP BY cd.Ciclo\n", "'''\n", "datas = pd.read_sql(query, conn)\n", "\n", "conn.close()" ] }, { "cell_type": "code", "execution_count": 3, "id": "1e6373af", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['2025-06-20', '2025-06-26', '2025-07-16', '2025-08-06']" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lista = datas.iloc[:, 1].tolist()\n", "lista" ] }, { "cell_type": "code", "execution_count": 4, "id": "071290d7", "metadata": {}, "outputs": [], "source": [ "datas_lista = datas.iloc[:, 1].tolist() # sua lista de datas\n", "\n", "# monta os placeholders\n", "placeholders = \", \".join([\"?\"] * len(datas_lista))\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "004a9497", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\joao.herculano\\AppData\\Local\\Temp\\ipykernel_15612\\485056913.py:25: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.\n", " estoque_data = pd.read_sql(query, conn, params=datas_lista)\n" ] } ], "source": [ "# Conexão com o banco\n", "conn = pyodbc.connect(\n", " f\"DRIVER={{SQL Server}};\"\n", " f\"SERVER={config['banco']['host']},1433;\"\n", " f\"DATABASE=GINSENG;\"\n", " f\"UID={config['banco']['user']};\"\n", " f\"PWD={config['banco']['password']}\")\n", "\n", "query = f\"\"\"\n", "SELECT \n", " emh.PDV,\n", " emh.SKU,\n", " emh.[ESTOQUE ATUAL],\n", " emh.[ESTOQUE EM TRANSITO],\n", " emh.[PEDIDO PENDENTE],\n", " emh.[DDV PREVISTO],\n", " cd.Ciclo,\n", " emh.data_estoque\n", "FROM estoque_mar_historico emh\n", "INNER JOIN ciclos_data_2025 cd \n", " ON cd.[Date] = emh.data_estoque and cd.MARCA ='BOT'\n", "WHERE emh.data_estoque IN ({placeholders})\n", "\"\"\"\n", "\n", "estoque_data = pd.read_sql(query, conn, params=datas_lista)\n", "conn.close()" ] }, { "cell_type": "code", "execution_count": 6, "id": "9a4ddcad", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
PDVSKUESTOQUE ATUALESTOQUE EM TRANSITOPEDIDO PENDENTEDDV PREVISTOCiclodata_estoque
02096894394000NoneC2025092025-06-26
12096994394000NoneC2025092025-06-26
22097094394000NoneC2025092025-06-26
32098694394000NoneC2025092025-06-26
42098894394000NoneC2025092025-06-26
\n", "
" ], "text/plain": [ " PDV SKU ESTOQUE ATUAL ESTOQUE EM TRANSITO PEDIDO PENDENTE \\\n", "0 20968 94394 0 0 0 \n", "1 20969 94394 0 0 0 \n", "2 20970 94394 0 0 0 \n", "3 20986 94394 0 0 0 \n", "4 20988 94394 0 0 0 \n", "\n", " DDV PREVISTO Ciclo data_estoque \n", "0 None C202509 2025-06-26 \n", "1 None C202509 2025-06-26 \n", "2 None C202509 2025-06-26 \n", "3 None C202509 2025-06-26 \n", "4 None C202509 2025-06-26 " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "estoque_data.head()" ] }, { "cell_type": "code", "execution_count": 7, "id": "88bd3679", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\joao.herculano\\AppData\\Local\\Temp\\ipykernel_15612\\731793387.py:22: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.\n", " draft_data = pd.read_sql(query, conn, params=datas_lista)\n" ] } ], "source": [ "# Conexão com o banco\n", "conn = pyodbc.connect(\n", " f\"DRIVER={{SQL Server}};\"\n", " f\"SERVER={config['banco']['host']},1433;\"\n", " f\"DATABASE=GINSENG;\"\n", " f\"UID={config['banco']['user']};\"\n", " f\"PWD={config['banco']['password']}\")\n", "\n", "query = f\"\"\"\n", "SELECT \n", " d.loja_id AS PDV,\n", " cast(d.code as int) as SKU,\n", " d.codcategory AS CATEGORIA,\n", " d.salescurve AS Curva,\n", " d.isproductdeactivated AS Desativado,\n", " d.criticalitem_iscritical AS Item_Critico,\n", " d.[data] as data_estoque\n", "from draft_historico d\n", "where d.[data] in ({placeholders})\n", "\"\"\"\n", "\n", "draft_data = pd.read_sql(query, conn, params=datas_lista)\n", "conn.close()" ] }, { "cell_type": "code", "execution_count": 8, "id": "4590cc77", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
PDVSKUCATEGORIACurvaDesativadoItem_Criticodata_estoque
02098651470CUIDADOS FACIAISC002025-06-20
11282686340MAQUIAGEME002025-06-20
22127858334MAQUIAGEMB002025-06-20
32099557761SOLARE002025-06-20
42425475568MAQUIAGEMC002025-06-20
\n", "
" ], "text/plain": [ " PDV SKU CATEGORIA Curva Desativado Item_Critico data_estoque\n", "0 20986 51470 CUIDADOS FACIAIS C 0 0 2025-06-20\n", "1 12826 86340 MAQUIAGEM E 0 0 2025-06-20\n", "2 21278 58334 MAQUIAGEM B 0 0 2025-06-20\n", "3 20995 57761 SOLAR E 0 0 2025-06-20\n", "4 24254 75568 MAQUIAGEM C 0 0 2025-06-20" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "draft_data.head()" ] }, { "cell_type": "code", "execution_count": 9, "id": "edcfa1bf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 51470\n", "1 86340\n", "2 58334\n", "3 57761\n", "4 75568\n", "Name: SKU, dtype: object" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "draft_data['SKU'] = draft_data['SKU'].astype(str)\n", "\n", "draft_data['SKU'].head()" ] }, { "cell_type": "code", "execution_count": 10, "id": "300e5866", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
PDVSKUESTOQUE ATUALESTOQUE EM TRANSITOPEDIDO PENDENTEDDV PREVISTOCiclodata_estoqueCATEGORIACurvaDesativadoItem_Critico
021277881460000C2025092025-06-20MAQUIAGEME00
13546879608000.07C2025092025-06-20MAQUIAGEMC00
24560879603000.07C2025092025-06-20MAQUIAGEMC00
35699879604000.05C2025092025-06-20MAQUIAGEMC00
412522879600100.04C2025092025-06-20MAQUIAGEMC00
\n", "
" ], "text/plain": [ " PDV SKU ESTOQUE ATUAL ESTOQUE EM TRANSITO PEDIDO PENDENTE \\\n", "0 21277 88146 0 0 0 \n", "1 3546 87960 8 0 0 \n", "2 4560 87960 3 0 0 \n", "3 5699 87960 4 0 0 \n", "4 12522 87960 0 1 0 \n", "\n", " DDV PREVISTO Ciclo data_estoque CATEGORIA Curva Desativado Item_Critico \n", "0 0 C202509 2025-06-20 MAQUIAGEM E 0 0 \n", "1 0.07 C202509 2025-06-20 MAQUIAGEM C 0 0 \n", "2 0.07 C202509 2025-06-20 MAQUIAGEM C 0 0 \n", "3 0.05 C202509 2025-06-20 MAQUIAGEM C 0 0 \n", "4 0.04 C202509 2025-06-20 MAQUIAGEM C 0 0 " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_junto = pd.merge(estoque_data,draft_data,on =['PDV',\t'SKU','data_estoque'],how='inner')\n", "\n", "df_junto.head()" ] }, { "cell_type": "code", "execution_count": 11, "id": "fbc812db", "metadata": {}, "outputs": [], "source": [ "from datetime import datetime\n", "from dateutil.relativedelta import relativedelta\n", "\n", "# Pegar a primeira data\n", "primeira_data = datetime.strptime(datas_lista[0], \"%Y-%m-%d\")\n", "\n", "# Subtrair 1 mês\n", "data_inicio = primeira_data - relativedelta(months=1)\n", "\n", "# Formatar para string no formato SQL Server (YYYY-MM-DD)\n", "data_inicio_str = data_inicio.strftime(\"%Y-%m-%d\")" ] }, { "cell_type": "code", "execution_count": 12, "id": "dffcbc1d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2025-05-20'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_inicio_str" ] }, { "cell_type": "code", "execution_count": 13, "id": "1c7e4715", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\joao.herculano\\AppData\\Local\\Temp\\ipykernel_15612\\1525897645.py:46: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.\n", " pedidos = pd.read_sql(query, conn, params=[data_inicio_str])\n" ] } ], "source": [ "# Conexão com o banco\n", "conn = pyodbc.connect(\n", " f\"DRIVER={{SQL Server}};\"\n", " f\"SERVER={config['banco']['host']},1433;\"\n", " f\"DATABASE=GINSENG;\"\n", " f\"UID={config['banco']['user']};\"\n", " f\"PWD={config['banco']['password']}\")\n", "\n", "query = \"\"\"\n", "SELECT\n", " fne.cnpj_destinatario,\n", " fne.cnf AS Nota_Fiscal,\n", " fne.data_emissao,\n", " fnei.codigo_pedido,\n", " fnei.n_item,\n", " fnei.cod_produto,\n", " fnei.produto,\n", " fnei.quantidade,\n", " fnei.valor_unitario,\n", " fnei.valor_total_produtos,\n", " b.PDV,\n", " p.quantity_accepted as qtd_aceita_industria,\n", " p.status \n", "FROM fato_notas_entrada fne\n", "INNER JOIN fato_notas_entrada_itens fnei ON fne.chave = fnei.chave\n", "INNER JOIN base_pdvs b ON fne.cnpj_destinatario = b.CNPJ\n", "LEFT JOIN (\n", " SELECT\n", " sellOrders,\n", " sku,\n", " storeCode,\n", " MAX(quantity_accepted) AS quantity_accepted,\n", " MAX(status) AS status,\n", " MAX(pedido_id) AS pedido_id,\n", " MAX([date]) AS [date],\n", " MAX(deliveryDate) AS deliveryDate\n", " FROM produtos_pedidos\n", " GROUP BY sellOrders, sku, storeCode\n", ") p \n", " ON p.sellOrders = fnei.codigo_pedido \n", " AND p.sku = fnei.cod_produto \n", " AND p.storeCode = b.PDV \n", "WHERE fne.data_emissao >= ?\n", "\"\"\"\n", "\n", "pedidos = pd.read_sql(query, conn, params=[data_inicio_str])\n", "conn.close()\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 14, "id": "2a594a3a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2025-06-20'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "datas_lista[0]" ] }, { "cell_type": "code", "execution_count": 15, "id": "d6617c7b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
cnpj_destinatarioNota_Fiscaldata_emissaocodigo_pedidon_itemcod_produtoprodutoquantidadevalor_unitariovalor_total_produtosPDVqtd_aceita_industriastatus
0084896430055450008060492025-05-240163731443386019ESTJ ARBO AMOR 25459.1150236.4620986NaNNone
1084896430061930008069132025-05-270163731450385294ESTJ QUASAR CLASSIC AMOR/251055.6330556.3320991NaNNone
2084896430061930008069132025-05-270163731450485294ESTJ QUASAR CLASSIC AMOR/25555.6320278.1620991NaNNone
3084896430046540008065332025-05-2701637314521086019ESTJ ARBO AMOR 252459.11631418.7920993NaNNone
4084896430058980008069952025-05-270163731459285293ESTJ HER CODE AMOR/2524121.98212927.5720997NaNNone
\n", "
" ], "text/plain": [ " cnpj_destinatario Nota_Fiscal data_emissao codigo_pedido n_item \\\n", "0 08489643005545 000806049 2025-05-24 0163731443 3 \n", "1 08489643006193 000806913 2025-05-27 0163731450 3 \n", "2 08489643006193 000806913 2025-05-27 0163731450 4 \n", "3 08489643004654 000806533 2025-05-27 0163731452 10 \n", "4 08489643005898 000806995 2025-05-27 0163731459 2 \n", "\n", " cod_produto produto quantidade valor_unitario \\\n", "0 86019 ESTJ ARBO AMOR 25 4 59.1150 \n", "1 85294 ESTJ QUASAR CLASSIC AMOR/25 10 55.6330 \n", "2 85294 ESTJ QUASAR CLASSIC AMOR/25 5 55.6320 \n", "3 86019 ESTJ ARBO AMOR 25 24 59.1163 \n", "4 85293 ESTJ HER CODE AMOR/25 24 121.9821 \n", "\n", " valor_total_produtos PDV qtd_aceita_industria status \n", "0 236.46 20986 NaN None \n", "1 556.33 20991 NaN None \n", "2 278.16 20991 NaN None \n", "3 1418.79 20993 NaN None \n", "4 2927.57 20997 NaN None " ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pedidos.head()" ] }, { "cell_type": "code", "execution_count": null, "id": "4e64bc1d", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.2" } }, "nbformat": 4, "nbformat_minor": 5 }