Enviar e-mail con datos de el Orden de venta (SQL)
A DWU ofrece un implarnte para uso de los clientes y socios. Este implante, puede ser modificado de forma libre por el desarrollador.
1. CREACIÓN DE LA PROCEDURE
Creación de la PROCEDURE para generación de los datos (ejemplo solamente SQL):
CREATE PROCEDURE [dbo].[EnviaPedidoPorEmail]
@list_of_cols_val_tab_del INT
AS
BEGIN
DECLARE @NomeEmpresa VARCHAR(250) = (SELECT OADM.CompnyName FROM OADM)
DECLARE @DocNum VARCHAR(250)
DECLARE @Cliente VARCHAR(250)
DECLARE @TelefoneX VARCHAR(250)
DECLARE @EmailX VARCHAR(250)
DECLARE @EnderecoEntrega VARCHAR(250)
DECLARE @DetalhesEntrega VARCHAR(MAX)
DECLARE @CEPX VARCHAR(250)
DECLARE @BairroX VARCHAR(250)
DECLARE @CidadeX VARCHAR(250)
DECLARE @EstadoX VARCHAR(250)
DECLARE @CNPJX VARCHAR(250)
DECLARE @CPFX VARCHAR(250)
DECLARE @IEX VARCHAR(250)
DECLARE @FormaPagamento VARCHAR(250)
DECLARE @Prazo VARCHAR(250)
DECLARE @PesoBruto VARCHAR(250)
DECLARE @PesoLiquido VARCHAR(250)
DECLARE @CodProduto VARCHAR(250)
DECLARE @NomeProduto VARCHAR(250)
DECLARE @Quantidade VARCHAR(250)
DECLARE @Preco VARCHAR(250)
DECLARE @TotalProduto VARCHAR(250)
DECLARE @DespesasAdicionais VARCHAR(250)
DECLARE @TotalDocumento VARCHAR(250)
DECLARE @Vendedor VARCHAR(250)
DECLARE @VendedorId VARCHAR(250)
DECLARE @Observacoes VARCHAR(250)
DECLARE @CabecalhoHTML VARCHAR(MAX) = ''
DECLARE @LinhasHTML VARCHAR(MAX) = ''
DECLARE @HTML VARCHAR(MAX) = ''
DECLARE @DestinatariosEmail VARCHAR(MAX)
DECLARE @DestinatariosCopiaEmail VARCHAR(MAX)
---------------------------------------------------------------------------------------------
-- Dados do cabeçalho do documento
---------------------------------------------------------------------------------------------
SELECT
@DocNum = T0.DocNum,
@Cliente = T0.CardCode + ' - ' + ISNULL(T0.CardName,'') ,--'Cliente',
@TelefoneX = ISNULL(T3.Phone2,'') + ' ' + ISNULL(T3.Phone1,'') ,--'Telefone',
@EmailX = ISNULL(T3.E_Mail,'') ,--'Email',
@EnderecoEntrega = ISNULL(T5.AddrTypeS,'') +' '+ ISNULL(T5.StreetS,'') +', '+ ISNULL(T5.StreetNoS,'') +' - '+ ISNULL(CAST(T5.BuildingS AS VARCHAR(250)),'') ,--'EnderecoEntrega',
@DetalhesEntrega = CAST(ISNULL(T9.U_DWU_Obs,'') AS VARCHAR(MAX)),
@CEPX = ISNULL(T5.ZipCodeS,'') ,--'CEP',
@BairroX = ISNULL(T5.BlockS,'') ,--'Bairro',
@CidadeX = ISNULL(T5.CityS,'') ,--'Cidade',
@EstadoX = ISNULL(T5.StateS,'') ,--'Estado',
@CNPJX = ISNULL(T4.TaxId0,'') ,--'CNPJ',
@CPFX = ISNULL(T4.TaxId4,'') ,--'CPF',
@IEX = ISNULL(T4.TaxId1,'') ,--'IE',
@FormaPagamento = ISNULL(T6.Descript,'') ,--'FormaPagamento',
@Prazo = ISNULL(T7.PymntGroup,'') ,--'Prazo',
@PesoBruto = ISNULL(FORMAT(T5.GrsWeight,'N','pt-BR'),0),--'Peso Bruto',
@PesoLiquido = ISNULL(FORMAT(T5.NetWeight,'N','pt-BR'),0),--'PesoLiquido',
@TotalDocumento = REPLACE(FORMAT(T0.DocTotal,'C','pt-BR'), 'R$', '') ,--'TotalDocumento',
@Vendedor = ISNULL(T2.SlpName,'') ,--'Vendedor',
@VendedorId = T2.SlpCode ,--'VendedorId',
@Observacoes = ISNULL(CAST(T0.Comments AS VARCHAR(250)),'') ,--'Observacoes',
@DespesasAdicionais = REPLACE(FORMAT(ISNULL(SUM(T8.LineTotal),0),'C','pt-BR'), 'R$', '') --'DespesasAdicionais'
FROM
ORDR T0
LEFT JOIN RDR1 T1 ON T0.DocEntry = T1.DocEntry
LEFT JOIN OSLP T2 ON T2.SlpCode = T0.SlpCode
LEFT JOIN OCRD T3 ON T3.CardCode = T0.CardCode
LEFT JOIN CRD7 T4 ON T4.CardCode = T0.CardCode AND T4.Address = T0.ShipToCode AND T4.AddrType = 'S'
LEFT JOIN RDR12 T5 ON T5.DocEntry = T0.DocEntry
LEFT JOIN OPYM T6 ON T6.PayMethCod = T0.PeyMethod
LEFT JOIN OCTG T7 ON T7.GroupNum = T0.GroupNum
LEFT JOIN RDR3 T8 ON T8.DocEntry = T0.DocEntry
LEFT JOIN CRD1 T9 ON T9.CardCode = T0.CardCode AND T9.Address = T0.ShipToCode
WHERE T0.DocEntry = @list_of_cols_val_tab_del
GROUP BY
T0.DocNum,
T0.CardCode + ' - ' + ISNULL(T0.CardName,''),
ISNULL(T3.Phone2,'') + ' ' + ISNULL(T3.Phone1,''),
ISNULL(T3.E_Mail,''),
ISNULL(T5.AddrTypeS,'') +' '+ ISNULL(T5.StreetS,'') +', '+ ISNULL(T5.StreetNoS,'') +' - '+ ISNULL(CAST(T5.BuildingS AS VARCHAR(250)),''),
CAST(ISNULL(T9.U_DWU_Obs,'') AS VARCHAR(MAX)),
ISNULL(T5.ZipCodeS,''),
ISNULL(T5.BlockS,''),
ISNULL(T5.CityS,''),
ISNULL(T5.StateS,''),
ISNULL(T4.TaxId0,''),
ISNULL(T4.TaxId4,''),
ISNULL(T4.TaxId1,''),
ISNULL(T6.Descript,''),
ISNULL(T7.PymntGroup,''),
ISNULL(FORMAT(T5.GrsWeight,'N','pt-BR'),0),
ISNULL(FORMAT(T5.NetWeight,'N','pt-BR'),0),
REPLACE(FORMAT(T0.DocTotal,'C','pt-BR'), 'R$', ''),
ISNULL(T2.SlpName,''),
T2.SlpCode,
ISNULL(CAST(T0.Comments AS VARCHAR(250)),'')
---------------------------------------------------------------------------------------------
-- Definição de destinatários do e-mail (e-mail principal do PN, e-mails do vendedor e e-mail do gerente do vendedor)
---------------------------------------------------------------------------------------------
SET @DestinatariosEmail =
(SELECT ISNULL(E_Mail,'') FROM OCRD WHERE CardCode = (SELECT CardCode FROM ORDR WHERE DocEntry = @list_of_cols_val_tab_del)) +';'+
(SELECT ISNULL(E_Mail,'') FROM OUSR WHERE USERID = (SELECT userId FROM OHEM WHERE salesPrson = @VendedorId))
SET @DestinatariosCopiaEmail = (SELECT ISNULL(E_Mail,'vazio') FROM OUSR WHERE USERID = (SELECT userId FROM OHEM WHERE EmpId = (SELECT manager FROM OHEM WHERE salesPrson = @VendedorId)))
----------------------------------------------------------------------------------------------------------------------------------
-- Caso email do cliente não esteja preenchido, definir e-mail da empresa
----------------------------------------------------------------------------------------------------------------------------------
IF @DestinatariosEmail IS NULL
BEGIN
SET @DestinatariosEmail = 'email@empresa.com.br'
END
----------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
-- Inicia HTML do e-mail com valores do cabeçalho
---------------------------------------------------------------------------------------------
SET @CabecalhoHTML =
'
<table style="height: 186px;" border="5" width="892" rules="none" cellpadding="10">
<tbody>
<tr>
<td style="width: 881.758px;">
<table style="width: 880px;">
<tbody>
<tr>
<td style="width: 244.91px;"><img src="http://www.dwu.com.br/wp-content/uploads/2020/07/DWU-logo-1.png" alt="" width="228" height="63" /></td>
<td style="width: 30.0898px;"> </td>
<td style="width: 593px;">
<h1 style="text-align: center;">'+@NomeEmpresa+'</h1>
</td>
</tr>
</tbody>
</table>
<h2><br />
O documento <span style="color: #808080;">'+@DocNum+'</span> foi gerado no valor total de <span style="color: #808080;">R$ '+@TotalDocumento+'</h2>
<p> </p>
<table style="width: 875px;" cellpadding="5">
<tbody>
<tr>
<td style="width: 139px;"><strong>Cliente: </strong></td>
<td style="width: 1501.13px;">'+@Cliente+'</td>
<td style="width: 204.867px;"><strong>Telefone: </strong></td>
<td style="width: 173px;">'+@TelefoneX+'</td>
</tr>
<tr>
<td style="width: 139px;"><strong>Endereço: </strong></td>
<td style="width: 1501.13px;">'+@EnderecoEntrega+'</td>
<td style="width: 204.867px;"><strong>CEP: </strong></td>
<td style="width: 173px;">'+@CEPX+'</td>
</tr>
<tr>
<td style="width: 139px;"><strong>Cidade: </strong></td>
<td style="width: 1501.13px;">'+@CidadeX+'</td>
<td style="width: 204.867px;"><strong>UF: </strong></td>
<td style="width: 173px;">'+@EstadoX+'</td>
</tr>
<tr>
<td style="width: 139px;"><strong>CNPJ: </strong></td>
<td style="width: 1501.13px;">'+@CNPJX+@CPFX+'</td>
<td style="width: 204.867px;"><strong>IE: </strong></td>
<td style="width: 173px;">'+@IEX+'</td>
</tr>
<tr>
<td style="width: 139px;"><strong>Forma de pagamento:
</strong></td>
<td style="width: 1501.13px;"><strong>'+@FormaPagamento+'</strong></td>
<td style="width: 204.867px;"><strong>Prazo: </strong></td>
<td style="width: 173px;">'+@Prazo+'</td>
</tr>
<tr>
<td style="width: 139px;"><strong>Peso Bruto: </strong></td>
<td style="width: 1501.13px;">'+@PesoBruto+' KG</td>
<td style="width: 204.867px;"><strong>Peso Líquido: </strong></td>
<td style="width: 173px;">'+@PesoLiquido+' KG</td>
</tr>
</tbody>
</table>
<p> </p>
<h2> Itens do documento:</h2>
<table style="width: 875px;" border="1" rules="rows"
cellpadding="5">
<thead>
<tr>
<td style="width: 70px;"><strong>Código</strong></td>
<td style="width: 342.832px;"><strong>Descrição</strong></td>
<td style="width: 64.168px; text-align: right;"><strong>Qtd</strong></td>
<td style="width: 139px; text-align: right;"><strong>Preço</strong></td>
<td style="width: 220px; text-align: right;"><strong>Total</strong></td>
</tr>
</thead>
<tbody>
'
---------------------------------------------------------------------------------------------
-- Consulta linhas do documento
---------------------------------------------------------------------------------------------
--Declaração do cursor
DECLARE carrCursor CURSOR FOR
SELECT
--Campos que tu quer verificar
T1.ItemCode 'CodProduto',
T1.Dscription 'NomeProduto',
CAST(T1.Quantity AS INT) 'Quantidade',
REPLACE(FORMAT(T1.PriceBefDi,'C','pt-BR'), 'R$', '') 'Preco',
REPLACE(FORMAT(T1.LineTotal,'C','pt-BR'), 'R$', '') 'TotalProduto'
FROM
ORDR T0
INNER JOIN RDR1 T1 ON T0.DocEntry = T1.DocEntry
WHERE T0.DocEntry = @list_of_cols_val_tab_del
-- Abre cursos
OPEN carrCursor
FETCH NEXT FROM carrCursor
INTO
@CodProduto,
@NomeProduto,
@Quantidade,
@Preco,
@TotalProduto
WHILE (@@FETCH_STATUS=0)
BEGIN
----------------------------------------------------------------------------------------------------------------------------------
-- Insere linhas da tabela do documento
----------------------------------------------------------------------------------------------------------------------------------
SET @LinhasHTML = @LinhasHTML + '<tr>'
SET @LinhasHTML = @LinhasHTML + '<td style="width: 70px;">'+@CodProduto+'</td>'
SET @LinhasHTML = @LinhasHTML + '<td style="width: 342.832px;">'+@NomeProduto+'</td>'
SET @LinhasHTML = @LinhasHTML + '<td style="width: 64.168px; text-align: right;">'+@Quantidade+'</td>'
SET @LinhasHTML = @LinhasHTML + '<td style="width: 139px; text-align: right;">R$ '+@Preco+'</td>'
SET @LinhasHTML = @LinhasHTML + '<td style="width: 220px; text-align: right;">R$ '+@TotalProduto+'</td>'
SET @LinhasHTML = @LinhasHTML + '</tr>'
----------------------------------------------------------------------------------------------------------------------------------
FETCH NEXT FROM carrCursor
INTO
@CodProduto,
@NomeProduto,
@Quantidade,
@Preco,
@TotalProduto
END
CLOSE carrCursor
DEALLOCATE carrCursor
SET @LinhasHTML = @LinhasHTML +
'
</tbody>
</table>
<table style="width: 875px;" cellpadding="5">
<thead>
<tr>
<td style="width: 640.422px; text-align: right;"><strong>Despesas adicionais:</strong></td>
<td style="width: 219.578px; text-align: right;">R$ '+@DespesasAdicionais+'</td>
</tr>
</thead>
<tbody>
<tr>
<td style="width: 640.422px; text-align: right;"><strong>Total:</strong></td>
<td style="width: 219.578px; text-align: right;"><strong>R$ '+@TotalDocumento+'</strong></td>
</tr>
</tbody>
</table>
<table style="width: 876px;" cellpadding="5">
<thead>
<tr>
<td style="width: 124.902px;"><strong>Obs. de entrega:</strong></td>
<td style="width: 736.098px;">'+@DetalhesEntrega+'</td>
</tr>
<tr>
<td style="width: 124.902px;"><strong>Vendedor:</strong></td>
<td style="width: 736.098px;">'+@Vendedor+'</td>
</tr>
<tr>
<td style="width: 124.902px;"><strong>Data de envio:</strong></td>
<td style="width: 736.098px;">'+(SELECT CONVERT(VARCHAR(10),GETDATE(),103))+' às '+ (SELECT LEFT(CAST(getdate() AS TIME),8)) +'</td>
</tr>
</thead>
</table>
<p> </p>
<p><strong>Atenciosamente</strong></p>
<p><strong>'+@NomeEmpresa+'</strong></p>
<p> </p>
<p> </p>
<p><em>Enviado por </em></p>
<p><a href="http://www.dwu.com.br"><strong><img src="http://www.dwu.com.br/wp-content/uploads/2020/06/Logo_CRMOne_Horizontal.png" alt="" width="165" height="35" /></strong></a></p>
</td>
</tr>
</tbody>
</table>
'
SET @HTML = @CabecalhoHTML + @LinhasHTML
----------------------------------------------------------------------------------------------------------------------------------
-- Insere dados na tabela de envio de e-mail
----------------------------------------------------------------------------------------------------------------------------------
INSERT INTO [dbo].[@DWU_MAIL_SER]
(
[Code]
,[Name]
,[U_ObjType]
,[U_ObjEntry]
,[U_Assunto]
,[U_Destinatario]
,[U_Copia]
,[U_CopiaOculta]
,[U_CaminhoAnexo]
,[U_TextoHTML]
,[U_AssinaturaHTML]
,[U_Status]
,[U_Obs1]
,[U_Obs2]
,[U_Obs3]
,[U_DataProg]
,[U_HoraProg])
VALUES
((select REPLACE(convert(varchar, getdate(), 23),'-','')+REPLACE(convert(varchar, getdate(), 14),':',''))
,(select REPLACE(convert(varchar, getdate(), 23),'-','')+REPLACE(convert(varchar, getdate(), 14),':',''))
,'17'
,@list_of_cols_val_tab_del
,'[CRM One] Olá! Seu documento número ' + CAST(@list_of_cols_val_tab_del AS VARCHAR(100)) + ' foi gerado e já está sendo processado.'
,@DestinatariosEmail
,@DestinatariosCopiaEmail
,''
,''
,@HTML
,''
,'N'
,''
,''
,''
,''
,''
)
END
---------------------------------------------------------------------------------------------------------------------------
2. TRANSACTION
Despues la cración de PROCEDURE encima, basta implementar la llamada de ella en la transaction notification de el SAP Business One conforme abajo:
----------------------------------------------------------------------------------------------------------------------------------
-- ENVIO DE EMAIL COM PEDIDO DE VENDA PARA CLIENTE, VENDEDOR E GERENTE
----------------------------------------------------------------------------------------------------------------------------------
IF (@object_type in ('17') and @transaction_type in ('A'))
BEGIN
EXEC EnviaPedidoPorEmail @list_of_cols_val_tab_del
END
3. RESULTADO FINAL
El resultado FINAL sera un e-mail en el siguiente formato:
![](https://www.dwu.com.br/wp-content/uploads/2023/12/25.png)
IMPORTRANTE
IMPORTATE: Para uso en el HANA, basta desarrollar en el formato/sintaxe del HANA que funcionara de la misma manera ejemplificada encima.