To include the two new columns, "Valor Manutenção" and "Valor Indenização," in your Excel spreadsheet, you'll need to make several adjustments to the Relatorio_Faturamento procedure. These changes involve updating the column headers, modifying how data is populated based on the sit_contrato field, and adjusting the accumulation of totals.
Below is the updated Relatorio_Faturamento procedure with the necessary modifications:
procedure TFrmRelOp.Relatorio_Faturamento;
var
diretorio: string;
i: Integer;
salvar_arquivo: Boolean;
faturamento_loc, faturamento_frete, faturamento_rto, faturamento_tot: Currency;
faturamento_manutencao, faturamento_indenizacao: Currency; // Added for new columns
aux_formas_pagto: string;
begin
if not Form1.TemPrivilegio('8.13') then Exit;
if (not faturamento_detalhar_contratos.Checked) then
DM.Carrega_Layout_Report('layout_faturamento_simples', DM.frxRelOp1)
else
DM.Carrega_Layout_Report('layout_faturamento_detalhado', DM.frxRelOp1);
Prepara_Conexao_Relatorio;
DM.frxRelOp1.FileName := 'Relatório de Faturamento'; // Redefine o nome do relatório para exportação
DM.frxRelOp1.Variables['data_inicio'] := QuoteFastReportVariable(datainicio.Text);
DM.frxRelOp1.Variables['data_fim'] := QuoteFastReportVariable(datafinal.Text);
if (not faturamento_detalhar_contratos.Checked) then
begin
// Existing SQL for simple layout
(DM.frxRelOp1.FindComponent('UniDACQuery1') as TfrxUniDACQuery).SQL.Text :=
'(SELECT CONCAT(LPAD(c.contrato_numero,5,"0"),"-",cv.num_versao) as contr,loja_id,nome,cpf AS cpf_cnpj,data_versao,valor_contrato_versao,(CASE cv.forma_pagto WHEN 0 THEN "Indef" WHEN 1 THEN "Dinh" ' +
'WHEN 2 THEN "Cheque" WHEN 3 THEN "C. Déb" WHEN 4 THEN "C. Créd" WHEN 5 THEN "Boleto" WHEN 6 THEN "Dep.Banc" WHEN 7 THEN "Carteira" WHEN 8 THEN "PIX" ELSE "Indef" END) as forma_pagamento,num_fatura,(CASE c.status_contrato WHEN 1 THEN "V" ' +
'WHEN 2 THEN "E" WHEN 0 THEN "C" ELSE "X" END) as sit_contrato,num_notafiscal ' +
'FROM contrato c INNER JOIN cliente cli ON c.cliente_id=cli.cliente_id INNER JOIN contrato_versao cv ON c.contrato_id=cv.contrato_id WHERE data_versao>=' +
QuoteDate_SQL(datainicio.Text) + ' and data_versao<' + QuoteDate_SQL(StrToDate(datafinal.Text) + 1) + ' AND cv.status_contrato<>0 AND loja_id=' + QuoteNum(Loja_ID) + ') ' +
'UNION ALL ' +
'(SELECT CONCAT(LPAD(ip.proposta_numero,4,"0"),"(I)") as contr,loja_id,nome,cpf AS cpf_cnpj,data_emissao_fatura,total_proposta,(CASE ip.forma_pagto WHEN 0 THEN "Indef" WHEN 1 THEN "Dinh" WHEN 2 THEN "Cheque" WHEN 3 THEN "C. Déb" WHEN 4 THEN "C. Créd" WHEN 5 THEN ' +
'"Boleto" WHEN 6 THEN "Dep.Banc" WHEN 7 THEN "Carteira" WHEN 8 THEN "PIX" ELSE "Indef" END) as forma_pagamento,num_fatura,"I" as sit_contrato,num_notafiscal ' +
'FROM indenizacao_proposta ip INNER JOIN cliente cli ON ip.cliente_id=cli.cliente_id WHERE data_emissao_fatura>=' + QuoteDate_SQL(datainicio.Text) +
' and data_emissao_fatura<' + QuoteDate_SQL(StrToDate(datafinal.Text) + 1) + ' AND loja_id=' + QuoteNum(Loja_ID) + ') ' +
'UNION ALL ' +
'(SELECT CONCAT(LPAD(mp.manutencao_numero,4,"0"),"(M)") as contr,loja_id,nome,cpf AS cpf_cnpj,data_emissao_fatura,total_proposta,(CASE mp.forma_pagto WHEN 0 THEN "Indef" WHEN 1 THEN "Dinh" WHEN 2 THEN "Cheque" WHEN 3 THEN "C. Déb" WHEN 4 THEN "C. Créd" WHEN 5 THEN ' +
'"Boleto" WHEN 6 THEN "Dep.Banc" WHEN 7 THEN "Carteira" WHEN 8 THEN "PIX" ELSE "Indef" END) as forma_pagamento,num_fatura,"M" as sit_contrato,num_notafiscal ' +
'FROM manutencao_proposta mp INNER JOIN cliente cli ON mp.cliente_id=cli.cliente_id WHERE data_emissao_fatura>=' + QuoteDate_SQL(datainicio.Text) +
' and data_emissao_fatura<' + QuoteDate_SQL(StrToDate(datafinal.Text) + 1) + ' AND loja_id=' + QuoteNum(Loja_ID) + ') ' +
' ORDER BY loja_id DESC,data_versao,contr'
end
else
begin
// Existing SQL for detailed layout
(DM.frxRelOp1.FindComponent('UniDACQuery1') as TfrxUniDACQuery).SQL.Text :=
'(SELECT CONCAT(LPAD(c.contrato_numero,5,"0"),"-",cv.num_versao) as contr,loja_id,nome,cpf AS cpf_cnpj,data_versao,valor_contrato_versao,(CASE cv.forma_pagto WHEN 0 THEN "Indef" WHEN' +
' 1 THEN "Dinh" WHEN 2 THEN "Cheque" WHEN 3 THEN "C. Déb" WHEN 4 THEN "C. Créd" WHEN 5 THEN "Boleto" WHEN 6 THEN "Dep.Banc" WHEN 7 THEN "Carteira" WHEN 8 THEN "PIX" ELSE "Indef" END)' +
' as forma_pagamento,num_fatura,(CASE c.status_contrato WHEN 1 THEN "V" WHEN 2 THEN "E" WHEN 0 THEN "C" ELSE "X" END) as sit_contrato,' +
'num_notafiscal,(CASE cv.num_versao WHEN 1 THEN valor_frete ELSE 0 END) as val_frete,COALESCE(SUM(valor_total_rto),0) as val_rto,COALESCE(SUM(preco_total_extra),0) as val_extras,' +
'(valor_contrato_versao - (CASE cv.num_versao WHEN 1 THEN valor_frete ELSE 0 END) - COALESCE(SUM(valor_total_rto),0) - COALESCE(SUM(preco_total_extra),0))' +
' as valor_locacoes,data_vencto_pagto FROM contrato c INNER JOIN cliente cli ON c.cliente_id=cli.cliente_id INNER JOIN contrato_versao cv ON c.contrato_id=cv.contrato_id ' +
'LEFT JOIN locavel_contrato_rto lcrto ON (c.contrato_id=lcrto.contrato_id AND cv.num_versao=1) LEFT JOIN extra_item ei ON (c.contrato_id=ei.contrato_id AND ei.num_versao=cv.num_versao) WHERE data_versao>=' +
QuoteDate_SQL(datainicio.Text) + ' and data_versao<' + QuoteDate_SQL(StrToDate(datafinal.Text) + 1) + ' AND cv.status_contrato<>0 AND loja_id=' + QuoteNum(Loja_ID) +
' GROUP BY cv.contrato_id,cv.num_versao) ' +
'UNION ALL ' +
'(SELECT CONCAT(LPAD(ip.proposta_numero,4,"0"),"(I)") as contr,loja_id,nome,cpf AS cpf_cnpj,data_emissao_fatura,total_proposta,(CASE ip.forma_pagto WHEN 0 THEN "Indef" WHEN 1 THEN "Dinh" WHEN 2 THEN "Cheque" WHEN 3 THEN "C. Déb" WHEN 4 THEN "C. Créd" WHEN 5 THEN ' +
'"Boleto" WHEN 6 THEN "Dep.Banc" WHEN 7 THEN "Carteira" WHEN 8 THEN "PIX" ELSE "Indef" END) as forma_pagamento,num_fatura,"I" as sit_contrato,num_notafiscal,0 as val_frete,0 as val_rto,0 as val_extras,0 as valor_locacoes,data_vencto_pagto ' +
'FROM indenizacao_proposta ip INNER JOIN cliente cli ON ip.cliente_id=cli.cliente_id WHERE data_emissao_fatura>=' + QuoteDate_SQL(datainicio.Text) +
' and data_emissao_fatura<' + QuoteDate_SQL(StrToDate(datafinal.Text) + 1) + ' AND loja_id=' + QuoteNum(Loja_ID) + ') ' +
'UNION ALL ' +
'(SELECT CONCAT(LPAD(mp.manutencao_numero,4,"0"),"(M)") as contr,loja_id,nome,cpf AS cpf_cnpj,data_emissao_fatura,total_proposta,(CASE mp.forma_pagto WHEN 0 THEN "Indef" WHEN 1 THEN "Dinh" WHEN 2 THEN "Cheque" WHEN 3 THEN "C. Déb" WHEN 4 THEN "C. Créd" WHEN 5 THEN ' +
'"Boleto" WHEN 6 THEN "Dep.Banc" WHEN 7 THEN "Carteira" WHEN 8 THEN "PIX" ELSE "Indef" END) as forma_pagamento,num_fatura,"M" as sit_contrato,num_notafiscal,0 as val_frete,0 as val_rto,0 as val_extras,0 as valor_locacoes,data_vencto_pagto ' +
'FROM manutencao_proposta mp INNER JOIN cliente cli ON mp.cliente_id=cli.cliente_id WHERE data_emissao_fatura>=' + QuoteDate_SQL(datainicio.Text) +
' and data_emissao_fatura<' + QuoteDate_SQL(StrToDate(datafinal.Text) + 1) + ' AND loja_id=' + QuoteNum(Loja_ID) + ') ' +
' ORDER BY loja_id DESC,data_versao,contr';
end;
if faturamento_detalhar_contratos.Checked then
begin
aux_formas_pagto := '';
DM.QueryRelat1.SQL.Text := 'SELECT forma_pagamento, SUM(tot_pago) as tot_pago FROM (' +
'(SELECT (CASE cv.forma_pagto WHEN 0 THEN "Indef" WHEN 1 THEN "Dinh" WHEN 2 THEN "Cheque" WHEN 3 THEN "C. Déb" WHEN 4 THEN "C. Créd" WHEN 5 THEN "Boleto" WHEN 6 THEN "Dep.Banc" WHEN 7 THEN "Carteira" WHEN 8 THEN "PIX" ELSE "Indef" END) AS forma_pagamento,' +
'SUM(valor_contrato_versao) as tot_pago FROM contrato c INNER JOIN contrato_versao cv ON c.contrato_id = cv.contrato_id WHERE data_versao >= ' + QuoteDate_SQL(datainicio.Text) +
' AND data_versao < ' + QuoteDate_SQL(StrToDate(datafinal.Text) + 1) +
' AND cv.status_contrato <> 0 AND loja_id = ' + QuoteNum(Loja_ID) + ' GROUP BY forma_pagamento )' +
'UNION ALL ' +
'(SELECT (CASE ip.forma_pagto WHEN 0 THEN "Indef" WHEN 1 THEN "Dinh" WHEN 2 THEN "Cheque" WHEN 3 THEN "C. Déb" WHEN 4 THEN "C. Créd" WHEN 5 THEN "Boleto" WHEN 6 THEN "Dep.Banc" WHEN 7 THEN "Carteira" WHEN 8 THEN "PIX" ELSE "Indef" END) AS forma_pagamento,' +
'SUM(total_proposta) as tot_pago FROM indenizacao_proposta ip WHERE data_emissao_fatura >= ' + QuoteDate_SQL(datainicio.Text) +
' AND data_emissao_fatura < ' + QuoteDate_SQL(StrToDate(datafinal.Text) + 1) + ' AND loja_id = ' + QuoteNum(Loja_ID) +
' GROUP BY forma_pagamento) ' +
'UNION ALL ' +
'(SELECT (CASE mp.forma_pagto WHEN 0 THEN "Indef" WHEN 1 THEN "Dinh" WHEN 2 THEN "Cheque" WHEN 3 THEN "C. Déb" WHEN 4 THEN "C. Créd" WHEN 5 THEN "Boleto" WHEN 6 THEN "Dep.Banc" WHEN 7 THEN "Carteira" WHEN 8 THEN "PIX" ELSE "Indef" END) AS forma_pagamento,' +
'SUM(total_proposta) as tot_pago FROM manutencao_proposta mp WHERE data_emissao_fatura >= ' + QuoteDate_SQL(datainicio.Text) +
' AND data_emissao_fatura < ' + QuoteDate_SQL(StrToDate(datafinal.Text) + 1) + ' AND loja_id = ' + QuoteNum(Loja_ID) +
' GROUP BY forma_pagamento) ' +
') z GROUP BY forma_pagamento ORDER BY forma_pagamento';
DM.QueryRelat1.Open;
while (not DM.QueryRelat1.Eof) do
begin
aux_formas_pagto := aux_formas_pagto + sLineBreak +
DM.QueryRelat1.FieldByName('forma_pagamento').AsString +
' = R$ ' + Format('%.2f', [DM.QueryRelat1.FieldByName('tot_pago').AsCurrency]);
DM.QueryRelat1.Next;
end;
DM.QueryRelat1.Close;
if Length(aux_formas_pagto) > 0 then
DM.frxRelOp1.Variables['formas_pagto'] := QuoteFastReportVariable(aux_formas_pagto)
else
DM.frxRelOp1.Variables['formas_pagto'] := QuoteFastReportVariable(EMPTYSTRING);
end;
if gerar_excel.Checked then
begin
diretorio := PathToMyDocuments;
salvar_arquivo := SelectDirectory('Selecione o local onde o Relatório de Faturamento deve ser salvo: (de ' +
datainicio.Text + ' a ' + datafinal.Text + ')', '', diretorio);
diretorio := ExcludeTrailingPathDelimiter(diretorio) + '\';
faturamento_loc := 0;
faturamento_frete := 0;
faturamento_rto := 0;
faturamento_tot := 0;
faturamento_manutencao := 0; // Initialize new totals
faturamento_indenizacao := 0; // Initialize new totals
if salvar_arquivo then
begin
Screen.Cursor := crHourGlass;
DM.Query8.SQL.Text := (DM.frxRelOp1.FindComponent('UniDACQuery1') as TfrxUniDACQuery).SQL.Text;
DM.Query8.Open;
if DM.Query8.RecordCount = 0 then
begin
DM.Query8.Close;
Screen.Cursor := crDefault;
ShowMessage('Nenhuma ocorrência encontrada no período selecionado');
Exit;
end;
if FileExists(diretorio + 'Relat_Faturamento_' +
FormatDateTime('ddmmmmyyyy', StrToDateTime(datainicio.Text)) +
'_a_' +
FormatDateTime('ddmmmmyyyy', StrToDateTime(datafinal.Text)) + '.xlsx') then
DeleteFile(diretorio + 'Relat_Faturamento_' +
FormatDateTime('ddmmmmyyyy', StrToDateTime(datainicio.Text)) +
'_a_' +
FormatDateTime('ddmmmmyyyy', StrToDateTime(datafinal.Text)) + '.xlsx'); // Apaga o arquivo se já existir
Form1.XLS.Clear;
Form1.XLS.FileName := diretorio + 'Relat_Faturamento_' +
FormatDateTime('ddmmmmyyyy', StrToDateTime(datainicio.Text)) +
'_a_' +
FormatDateTime('ddmmmmyyyy', StrToDateTime(datafinal.Text)) + '.xlsx'; // Define o nome do arquivo
Form1.XLS[0].Name := 'Relat_Faturamento';
if faturamento_detalhar_contratos.Checked then
begin
// Update column headers to include new columns
Form1.XLS.CmdFormat.Mode := xcfmReplace;
Form1.XLS.CmdFormat.BeginEdit(Form1.XLS[0]);
Form1.XLS.CmdFormat.Fill.BackgroundColor.RGB := clGray;
Form1.XLS.CmdFormat.Border.Style := cbsThick;
Form1.XLS.CmdFormat.Apply(0, 0, 13, 0); // Updated to cover all 14 columns
Form1.XLS[0].AsString[0, 0] := 'Contrato/versão';
Form1.XLS[0].AsString[1, 0] := 'Nome do Cliente';
Form1.XLS[0].AsString[2, 0] := 'CPF/CNPJ';
Form1.XLS[0].AsString[3, 0] := 'Forma Pagto';
Form1.XLS[0].AsString[4, 0] := 'Data Contrato';
Form1.XLS[0].AsString[5, 0] := 'Valor Locações R$';
Form1.XLS[0].AsString[6, 0] := 'Valor Frete R$';
Form1.XLS[0].AsString[7, 0] := 'Valor RTO R$';
Form1.XLS[0].AsString[8, 0] := 'Valor Manutenção'; // New Column
Form1.XLS[0].AsString[9, 0] := 'Valor Indenização'; // New Column
Form1.XLS[0].AsString[10, 0] := 'Valor Total R$'; // Shifted Column
Form1.XLS[0].AsString[11, 0] := 'Fatura'; // Shifted Column
Form1.XLS[0].AsString[12, 0] := 'N.F.'; // Shifted Column
Form1.XLS[0].AsString[13, 0] := 'Sit.Contr'; // Shifted Column
end
else
begin
// Existing column headers for simple layout
Form1.XLS.CmdFormat.Mode := xcfmReplace;
Form1.XLS.CmdFormat.BeginEdit(Form1.XLS[0]);
Form1.XLS.CmdFormat.Fill.BackgroundColor.RGB := clGray;
Form1.XLS.CmdFormat.Border.Style := cbsThick;
Form1.XLS.CmdFormat.Apply(0, 0, 8, 0);
Form1.XLS[0].AsString[0, 0] := 'Contrato/versão';
Form1.XLS[0].AsString[1, 0] := 'Nome do Cliente';
Form1.XLS[0].AsString[2, 0] := 'CPF/CNPJ';
Form1.XLS[0].AsString[3, 0] := 'Forma Pagto';
Form1.XLS[0].AsString[4, 0] := 'Data Contrato';
Form1.XLS[0].AsString[5, 0] := 'Valor Bruto R$';
Form1.XLS[0].AsString[6, 0] := 'Valor Subloc R$';
Form1.XLS[0].AsString[7, 0] := 'Valor Líquido R$';
Form1.XLS[0].AsString[8, 0] := 'Fatura';
// Form1.XLS[0].AsString[9,0]:= 'N.F.';
Form1.XLS[0].AsString[9, 0] := 'Sit.Contr';
end;
i := 0;
while (not DM.Query8.Eof) do
begin
i := i + 1;
if faturamento_detalhar_contratos.Checked then
begin
// Populate detailed layout with new columns
Form1.XLS[0].AsString[0, i] := DM.Query8.FieldByName('contr').AsString;
Form1.XLS[0].AsString[1, i] := DM.Query8.FieldByName('nome').AsString;
if Length(DM.Query8.FieldByName('cpf_cnpj').AsString) > 11 then
Form1.XLS[0].AsString[2, i] := FormatarCNPJ(DM.Query8.FieldByName('cpf_cnpj').AsString)
else
Form1.XLS[0].AsString[2, i] := FormatarCPF(DM.Query8.FieldByName('cpf_cnpj').AsString);
Form1.XLS[0].AsString[3, i] := DM.Query8.FieldByName('forma_pagamento').AsString;
Form1.XLS[0].AsDateTime[4, i] := DM.Query8.FieldByName('data_versao').AsDateTime;
Form1.XLS[0].Cell[4, i].NumberFormat := 'dd/mm/yyyy';
Form1.XLS[0].AsFloat[5, i] := DM.Query8.FieldByName('valor_locacoes').AsFloat;
Form1.XLS[0].Cell[5, i].NumberFormat := '0.00';
Form1.XLS[0].Cell[5, i].HorizAlignment := chaRight;
Form1.XLS[0].AsFloat[6, i] := DM.Query8.FieldByName('val_frete').AsFloat;
Form1.XLS[0].Cell[6, i].NumberFormat := '0.00';
Form1.XLS[0].Cell[6, i].HorizAlignment := chaRight;
Form1.XLS[0].AsFloat[7, i] := DM.Query8.FieldByName('val_rto').AsFloat;
Form1.XLS[0].Cell[7, i].NumberFormat := '0.00';
Form1.XLS[0].Cell[7, i].HorizAlignment := chaRight;
// Initialize new columns to 0
Form1.XLS[0].AsFloat[8, i] := 0; // Valor Manutenção
Form1.XLS[0].Cell[8, i].NumberFormat := '0.00';
Form1.XLS[0].Cell[8, i].HorizAlignment := chaRight;
Form1.XLS[0].AsFloat[9, i] := 0; // Valor Indenização
Form1.XLS[0].Cell[9, i].NumberFormat := '0.00';
Form1.XLS[0].Cell[9, i].HorizAlignment := chaRight;
// Populate new columns based on sit_contrato
if DM.Query8.FieldByName('sit_contrato').AsString = 'M' then
begin
Form1.XLS[0].AsFloat[8, i] := DM.Query8.FieldByName('valor_contrato_versao').AsFloat; // Valor Manutenção
faturamento_manutencao := RoundTo(faturamento_manutencao + DM.Query8.FieldByName('valor_contrato_versao').AsFloat, -2);
end
else if DM.Query8.FieldByName('sit_contrato').AsString = 'I' then
begin
Form1.XLS[0].AsFloat[9, i] := DM.Query8.FieldByName('valor_contrato_versao').AsFloat; // Valor Indenização
faturamento_indenizacao := RoundTo(faturamento_indenizacao + DM.Query8.FieldByName('valor_contrato_versao').AsFloat, -2);
end;
// Shift Valor Total R$
Form1.XLS[0].AsFloat[10, i] := DM.Query8.FieldByName('valor_contrato_versao').AsFloat;
Form1.XLS[0].Cell[10, i].NumberFormat := '0.00';
Form1.XLS[0].Cell[10, i].HorizAlignment := chaRight;
Form1.XLS[0].AsInteger[11, i] := DM.Query8.FieldByName('num_fatura').AsInteger;
Form1.XLS[0].AsString[12, i] := DM.Query8.FieldByName('num_notafiscal').AsString;
Form1.XLS[0].AsString[13, i] := DM.Query8.FieldByName('sit_contrato').AsString;
// Accumulate totals
faturamento_loc := RoundTo(faturamento_loc + DM.Query8.FieldByName('valor_locacoes').AsFloat, -2);
faturamento_frete := RoundTo(faturamento_frete + DM.Query8.FieldByName('val_frete').AsFloat, -2);
faturamento_rto := RoundTo(faturamento_rto + DM.Query8.FieldByName('val_rto').AsFloat, -2);
faturamento_tot := RoundTo(faturamento_tot + DM.Query8.FieldByName('valor_contrato_versao').AsFloat, -2);
end
else
begin
// Existing code for simple layout
Form1.XLS[0].AsString[0, i] := DM.Query8.FieldByName('contr').AsString;
Form1.XLS[0].AsString[1, i] := DM.Query8.FieldByName('nome').AsString;
if Length(DM.Query8.FieldByName('cpf_cnpj').AsString) > 11 then
Form1.XLS[0].AsString[2, i] := FormatarCNPJ(DM.Query8.FieldByName('cpf_cnpj').AsString)
else
Form1.XLS[0].AsString[2, i] := FormatarCPF(DM.Query8.FieldByName('cpf_cnpj').AsString);
Form1.XLS[0].AsString[3, i] := DM.Query8.FieldByName('forma_pagamento').AsString;
Form1.XLS[0].AsDateTime[4, i] := DM.Query8.FieldByName('data_versao').AsDateTime;
Form1.XLS[0].Cell[4, i].NumberFormat := 'dd/mm/yyyy';
Form1.XLS[0].AsFloat[5, i] := DM.Query8.FieldByName('valor_contrato_versao').AsFloat;
Form1.XLS[0].Cell[5, i].NumberFormat := '0.00';
Form1.XLS[0].Cell[5, i].HorizAlignment := chaRight;
Form1.XLS[0].AsFloat[6, i] := 0;
Form1.XLS[0].Cell[6, i].NumberFormat := '0.00';
Form1.XLS[0].Cell[6, i].HorizAlignment := chaRight;
Form1.XLS[0].AsFormula[7, i] := 'SUM(F' + IntToStr(i + 1) + '-G' + IntToStr(i + 1) + ')';
Form1.XLS[0].Cell[7, i].NumberFormat := '0.00';
Form1.XLS[0].Cell[7, i].HorizAlignment := chaRight;
Form1.XLS[0].AsInteger[8, i] := DM.Query8.FieldByName('num_fatura').AsInteger;
// Form1.XLS[0].AsString[8,i]:= 'num_notafiscal';
Form1.XLS[0].AsString[9, i] := DM.Query8.FieldByName('sit_contrato').AsString;
faturamento_tot := RoundTo(faturamento_tot + DM.Query8.FieldByName('valor_contrato_versao').AsFloat, -2);
end;
DM.Query8.Next;
end;
DM.Query8.Close;
if faturamento_detalhar_contratos.Checked then
begin
// Populate totals for detailed layout, including new columns
Form1.XLS[0].AsFloat[5, i + 2] := faturamento_loc;
Form1.XLS[0].Cell[5, i + 2].NumberFormat := '0.00';
Form1.XLS[0].Cell[5, i + 2].HorizAlignment := chaRight;
Form1.XLS[0].AsFloat[6, i + 2] := faturamento_frete;
Form1.XLS[0].Cell[6, i + 2].NumberFormat := '0.00';
Form1.XLS[0].Cell[6, i + 2].HorizAlignment := chaRight;
Form1.XLS[0].AsFloat[7, i + 2] := faturamento_rto;
Form1.XLS[0].Cell[7, i + 2].NumberFormat := '0.00';
Form1.XLS[0].Cell[7, i + 2].HorizAlignment := chaRight;
Form1.XLS[0].AsFloat[8, i + 2] := faturamento_manutencao; // Total Valor Manutenção
Form1.XLS[0].Cell[8, i + 2].NumberFormat := '0.00';
Form1.XLS[0].Cell[8, i + 2].HorizAlignment := chaRight;
Form1.XLS[0].AsFloat[9, i + 2] := faturamento_indenizacao; // Total Valor Indenização
Form1.XLS[0].Cell[9, i + 2].NumberFormat := '0.00';
Form1.XLS[0].Cell[9, i + 2].HorizAlignment := chaRight;
Form1.XLS[0].AsFloat[10, i + 2] := faturamento_tot;
Form1.XLS[0].Cell[10, i + 2].NumberFormat := '0.00';
Form1.XLS[0].Cell[10, i + 2].HorizAlignment := chaRight;
Form1.XLS[0].AsInteger[11, i + 2] := 0; // Assuming no total for 'Fatura'
Form1.XLS[0].AsString[12, i + 2] := ''; // Assuming no total for 'N.F.'
Form1.XLS[0].AsString[13, i + 2] := 'Total';
end
else
begin
// Existing totals calculation for simple layout
Form1.XLS[0].AsFormula[5, i + 2] := 'SUM(F2:F' + IntToStr(i + 1) + ')';
Form1.XLS[0].Cell[5, i + 2].NumberFormat := '0.00';
Form1.XLS[0].Cell[5, i + 2].HorizAlignment := chaRight;
Form1.XLS[0].AsFormula[6, i + 2] := 'SUM(G2:G' + IntToStr(i + 1) + ')';
Form1.XLS[0].Cell[6, i + 2].NumberFormat := '0.00';
Form1.XLS[0].Cell[6, i + 2].HorizAlignment := chaRight;
Form1.XLS[0].AsFormula[7, i + 2] := 'SUM(H2:H' + IntToStr(i + 1) + ')';
Form1.XLS[0].Cell[7, i + 2].NumberFormat := '0.00';
Form1.XLS[0].Cell[7, i + 2].HorizAlignment := chaRight;
end;
try
Form1.XLS[0].AutoWidthCols(0, 20);
except
// Handle any exceptions silently
end;
Form1.XLS.Write; // Escreve o arquivo.
Screen.Cursor := crDefault;
if Form1.LeParam('relat_pergunta_open_xls') = 'True' then
begin
if MsgNaoSim('Gravação concluída no arquivo "' + Form1.XLS.FileName + '".' +
#13 + 'Deseja abrir o relatório Excel agora?') = 'YES' then
ShellExecute(Handle, nil, PChar(Form1.XLS.FileName), nil, nil, SW_SHOWNORMAL);
end
else
ShowMessage('Gravação concluída no arquivo "' + Form1.XLS.FileName + '"');
end; // if salvar_arquivo
end // if gerar_excel.Checked
else
DM.frxRelOp1.ShowReport(True);
end;
Detalhes das Modificações:
-
Declaração de Novas Variáveis para Totais:
faturamento_manutencao, faturamento_indenizacao: Currency; // Adicionado para novos campos
-
Inicialização das Novas Variáveis de Total:
faturamento_manutencao := 0; // Inicializa total Valor Manutenção
faturamento_indenizacao := 0; // Inicializa total Valor Indenização
-
Atualização dos Cabeçalhos das Colunas no Excel:
- Adicionadas duas novas colunas após "Valor RTO R$".
Form1.XLS[0].AsString[8, 0] := 'Valor Manutenção'; // Nova Coluna
Form1.XLS[0].AsString[9, 0] := 'Valor Indenização'; // Nova Coluna
Form1.XLS[0].AsString[10, 0] := 'Valor Total R$'; // Coluna Remanejada
Form1.XLS[0].AsString[11, 0] := 'Fatura'; // Coluna Remanejada
Form1.XLS[0].AsString[12, 0] := 'N.F.'; // Coluna Remanejada
Form1.XLS[0].AsString[13, 0] := 'Sit.Contr'; // Coluna Remanejada
-
População das Novas Colunas Baseada em sit_contrato:
- Se
sit_contrato for 'M', preenche "Valor Manutenção".
- Se
sit_contrato for 'I', preenche "Valor Indenização".
// Inicializar novas colunas com zero
Form1.XLS[0].AsFloat[8, i] := 0; // Valor Manutenção
Form1.XLS[0].AsFloat[9, i] := 0; // Valor Indenização
// Preencher baseando-se em sit_contrato
if DM.Query8.FieldByName('sit_contrato').AsString = 'M' then
begin
Form1.XLS[0].AsFloat[8, i] := DM.Query8.FieldByName('valor_contrato_versao').AsFloat; // Valor Manutenção
faturamento_manutencao := RoundTo(faturamento_manutencao + DM.Query8.FieldByName('valor_contrato_versao').AsFloat, -2);
end
else if DM.Query8.FieldByName('sit_contrato').AsString = 'I' then
begin
Form1.XLS[0].AsFloat[9, i] := DM.Query8.FieldByName('valor_contrato_versao').AsFloat; // Valor Indenização
faturamento_indenizacao := RoundTo(faturamento_indenizacao + DM.Query8.FieldByName('valor_contrato_versao').AsFloat, -2);
end;
-
Acumulação dos Totais nas Novas Colunas:
- Após a população das colunas, acumula os valores de "Valor Manutenção" e "Valor Indenização".
faturamento_loc := RoundTo(faturamento_loc + DM.Query8.FieldByName('valor_locacoes').AsFloat, -2);
faturamento_frete := RoundTo(faturamento_frete + DM.Query8.FieldByName('val_frete').AsFloat, -2);
faturamento_rto := RoundTo(faturamento_rto + DM.Query8.FieldByName('val_rto').AsFloat, -2);
faturamento_tot := RoundTo(faturamento_tot + DM.Query8.FieldByName('valor_contrato_versao').AsFloat, -2);
Considerações Finais:
-
Formatação Consistente: As novas colunas seguem a mesma formatação aplicada às colunas existentes, garantindo consistência visual no relatório Excel.
-
Manipulação de Totais: As variáveis faturamento_manutencao e faturamento_indenizacao são utilizadas para acumular os valores totais dessas novas categorias, garantindo que os totais no final do relatório estejam corretos.
-
Flexibilidade: As alterações foram feitas de forma que somente afetam o modo detalhado (faturamento_detalhar_contratos.Checked = True), mantendo o layout simples inalterado.
-
Manutenção e Escalabilidade: Com essas modificações, futuras adições de colunas ou mudanças na lógica podem ser realizadas de forma mais estruturada.
Certifique-se de testar o procedimento após as modificações para garantir que todas as funcionalidades estejam operando conforme o esperado e que os novos campos estejam sendo populados corretamente no relatório Excel.