Compare txt data with database data

1

Follow the code:

procedure TfrmGrid.btnLoadClick(Sender: TObject);
var
 txt: TextFile;
 l,treg,treg2, coo, ccf: integer;
 valortxt, valorbd : double;
 lTemp, valor, dtcompratxt: String;
 dtcompra: TDateTime;
begin
DModuleGrid.ZQuery1.Close;
DModuleGrid.ZQuery1.SQL.Clear;
//Add tdcupant
//numcupom = coo
//Cupons
DModuleGrid.ZQuery1.SQL.Add('SELECT dtcompra, numnf, numcupom, ccf, valor FROM tdcupant');

DModuleGrid.ZQuery1.Open;

DModuleGrid.ClientDataSet1.SetProvider(DModuleGrid.DataSetProvider1);
DModuleGrid.ClientDataSet1.Open;

  l:= 1;
  treg:= 0;
  treg2:= 0;

  AssignFile(txt, frmSelection.FileListBox1.FileName);
  Reset(txt);
    while not eof(txt) do
    begin
     Readln(txt, lTemp);
     if (copy(lTemp, 1, 3) = 'E14') then
     begin
      inc(treg2);
      dtcompratxt := copy(lTemp,65,2)+'/'+copy(lTemp,63,2)+'/'+copy(lTemp,59,4);
      TryStrToDateTime(dtcompratxt, dtcompra);

      //Comparando valores (Valor do Cupom)

      ShowMessage(DModuleGrid.ZQuery1.FieldByName('numcupom').AsString +' '+
        copy(lTemp,53, 6));
       if  (DModuleGrid.ZQuery1.FieldByName('numcupom').AsString = copy(lTemp,53, 6))
       and (DModuleGrid.ZQuery1.FieldByName('ccf').AsString = copy(lTemp,47,6))
       and (SameDate(DModuleGrid.ZQuery1.ParamByName('dtcompra').AsDate,
         dtcompra))
       then
       begin
        inc(treg);
        StringGrid1.RowCount := treg+1;
        inc(l);
        //Valor no BD
        StringGrid1.Cells[0,l] := DModuleGrid.ZQuery1.FieldByName('valor').AsString;
        valorbd := DModuleGrid.ZQuery1.FieldByName('valor').AsFloat;
        //Valor no TXT
        StringGrid1.Cells[1,l] := FloatToStr(StrToFloat(copy(lTemp, 109, 14))/100);
        valortxt := StrToFloat(copy(lTemp, 109, 14))/100;

        ShowMessage(FloatToStr(valortxt) + ' ' + FloatToStr(valorbd));
       end;
        //Diferença nos valores
       if not (StringGrid1.Cells[0,l] = StringGrid1.Cells[1,l]) then
        begin
            valor := FloatToStr(valorbd - valortxt);
         if (valor = '') then
           begin
            valor := IntToStr(0);
            StringGrid1.Cells[2,l] := valor;
           end
            else
              StringGrid1.Cells[2,l] := valor;
        end;
     end;
    end;
    ShowMessage('Existem '+ IntToStr(treg2) + ' linhas -E14');
    ShowMessage('Existem '+ IntToStr(treg) + ' linhas');
    CloseFile(txt);
end;

This code does not reach the second if , it follows the code of the second if , which is also above:

dtcompratxt := copy(lTemp,65,2)+'/'+copy(lTemp,63,2)+'/'+copy(lTemp,59,4);
      TryStrToDateTime(dtcompratxt, dtcompra);

      //Comparando valores (Valor do Cupom)

      ShowMessage(DModuleGrid.ZQuery1.FieldByName('numcupom').AsString +' '+
        copy(lTemp,53, 6));
       if  (DModuleGrid.ZQuery1.FieldByName('numcupom').AsString = copy(lTemp,53, 6))
       and (DModuleGrid.ZQuery1.FieldByName('ccf').AsString = copy(lTemp,47,6))
       and (SameDate(DModuleGrid.ZQuery1.ParamByName('dtcompra').AsDate,
         dtcompra))
       then
       begin
        inc(treg);
        StringGrid1.RowCount := treg+1;
        inc(l);
        //Valor no BD
        StringGrid1.Cells[0,l] := DModuleGrid.ZQuery1.FieldByName('valor').AsString;
        valorbd := DModuleGrid.ZQuery1.FieldByName('valor').AsFloat;
        //Valor no TXT
        StringGrid1.Cells[1,l] := FloatToStr(StrToFloat(copy(lTemp, 109, 14))/100);
        valortxt := StrToFloat(copy(lTemp, 109, 14))/100;

        ShowMessage(FloatToStr(valortxt) + ' ' + FloatToStr(valorbd));
       end;

This code can not compare the data from one to another.

What am I doing wrong?

    
asked by anonymous 12.05.2014 / 21:08

2 answers

1

Follow the code I developed in response.

I hope it helps.

procedure TfrmGrid.btnLoadClick(Sender: TObject);
var
 txt: TextFile;
 treg, lreg: integer;
 valortxt, valorbd : double;
 lTemp, valor, dtcompratxt: String;
 dtcompra: TDateTime;
begin
lreg:= 1;
treg:= 0;

AssignFile(txt, frmSelection.FileListBox1.FileName);
Reset(txt);
while not eof(txt) do
begin
 Readln(txt, lTemp);
 if  (copy(lTemp, 1, 3) = 'E14') then
 begin
   dtcompratxt := copy(lTemp,65,2)+'/'+copy(lTemp,63,2)+'/'+copy(lTemp,59,4);

   DModuleGrid.ZQuery1.Close;
   DModuleGrid.ZQuery1.SQL.Clear;
   DModuleGrid.ZQuery1.SQL.Add('SELECT dtcompra, numnf, numcupom, ccf, valor FROM tdcupant');
   //DModuleGrid.ZQuery1.SQL.Add('WHERE dtcompra = ' + dtcompratxt + ' AND numcupom = ' + copy(lTemp,53, 6) + ' AND numnf = ' + copy(lTemp, 4, 20) + ' AND ccf = '+ copy(lTemp,47,6) + ';');
   DModuleGrid.ZQuery1.Open;
   DModuleGrid.ClientDataSet1.SetProvider(DModuleGrid.DataSetProvider1);
   DModuleGrid.ClientDataSet1.Open;

   if not (DModuleGrid.ZQuery1.IsEmpty) then
   begin
     DModuleGrid.ZQuery1.First;
     while not DModuleGrid.ZQuery1.Eof do
     begin
       if (copy(lTemp,53, 6) = DModuleGrid.ZQuery1.FieldByName('numcupom').AsString)
       and (copy(lTemp,47,6) = DModuleGrid.ZQuery1.FieldByName('ccf').AsString)
       and (StrToDateTime(dtcompratxt) = DModuleGrid.ZQuery1.FieldByName('dtcompra').AsDateTime)
       then
       begin
        inc(lreg);
        //Valor no BD
        valorbd := DModuleGrid.ZQuery1.FieldByName('valor').AsFloat;
        //Valor no TXT
        valortxt := StrToFloat(copy(lTemp, 109, 14))/100;
        //Diferença nos valores
        if (valorbd <> valortxt) then
        begin
          inc(treg);
         if (valor = '') then
           begin
            valor := IntToStr(0);
           end
            else
            valor := IntToStr(treg);
            Label1.Caption:='Divergências entre valores: '+ valor;
        end;

       end;
       DModuleGrid.ZQuery1.Next;
     end;
   end;
 end;
end;
ShowMessage('Existem '+ IntToStr(lreg) + ' linhas de Cupons(E14)');
CloseFile(txt);
CloseQuery;
end;
    
13.05.2014 / 16:19
0

Hitting the indentation of your (very important) code I came to some conclusions. Following:

First the code:

var
  txt: TextFile;
  l,treg,treg2, coo, ccf: integer;
  valortxt, valorbd : double;
  lTemp, valor, dtcompratxt: String;
  dtcompra: TDateTime;
begin
  DModuleGrid.ZQuery1.Close;
  DModuleGrid.ZQuery1.SQL.Clear;
  //Add tdcupant
  //numcupom = coo
  //Cupons
  DModuleGrid.ZQuery1.SQL.Add('SELECT dtcompra, numnf, numcupom, ccf, valor FROM tdcupant');
  DModuleGrid.ZQuery1.Open;
  DModuleGrid.ClientDataSet1.SetProvider(DModuleGrid.DataSetProvider1);
  DModuleGrid.ClientDataSet1.Open;
  l:= 1;
  treg:= 0;
  treg2:= 0;
  AssignFile(txt,frmSelection.FileListBox1.FileName);
  Reset(txt);
  while not eof(txt) do
  begin
    Readln(txt, lTemp);
    if (copy(lTemp, 1, 3) = 'E14') then
    begin
      inc(treg2);
      dtcompratxt := copy(lTemp,65,2)+'/'+copy(lTemp,63,2)+'/'+copy(lTemp,59,4);
      TryStrToDateTime(dtcompratxt, dtcompra);
      //Comparando valores (Valor do Cupom)
      ShowMessage(DModuleGrid.ZQuery1.FieldByName('numcupom').AsString +' '+
      copy(lTemp,53, 6));
      if  (DModuleGrid.ZQuery1.FieldByName('numcupom').AsString = copy(lTemp,53, 6))
           and (DModuleGrid.ZQuery1.FieldByName('ccf').AsString = copy(lTemp,47,6))
           and (SameDate(DModuleGrid.ZQuery1.ParamByName('dtcompra').AsDate,dtcompra)) then
      begin
        inc(treg);
        StringGrid1.RowCount := treg+1;
        inc(l);
        //Valor no BD
        StringGrid1.Cells[0,l] := DModuleGrid.ZQuery1.FieldByName('valor').AsString;
        valorbd := DModuleGrid.ZQuery1.FieldByName('valor').AsFloat;
        //Valor no TXT
        StringGrid1.Cells[1,l] := FloatToStr(StrToFloat(copy(lTemp, 109, 14))/100);
        valortxt := StrToFloat(copy(lTemp, 109, 14))/100;
        ShowMessage(FloatToStr(valortxt) + ' ' + FloatToStr(valorbd));
      end;
      //Diferença nos valores
      if not (StringGrid1.Cells[0,l] = StringGrid1.Cells[1,l]) then
      begin
        valor := FloatToStr(valorbd - valortxt);
        if (valor = '') then
        begin
          valor := IntToStr(0);
          StringGrid1.Cells[2,l] := valor;
        end
        else
          StringGrid1.Cells[2,l] := valor;
      end;
    end;
  end;
  ShowMessage('Existem '+ IntToStr(treg2) + ' linhas -E14');
  ShowMessage('Existem '+ IntToStr(treg) + ' linhas');
  CloseFile(txt);

As we see, the second and third ifs are within the first. If the condition of the first is not satisfied, we will not reach the others.

As for what else may be done wrong, it depends on your logic, the question becomes a bit subjective.

    
13.05.2014 / 16:18