i have rotines
procedure ProcessRecordsLoop(cdsOrigem, cdsDestino: TClientDataSet);
var
origens, destinos: array of Currency;
i, j: Integer;
begin
// Preencher os arrays com os valores dos conjuntos de dados
cdsOrigem.First;
cdsDestino.First;
if cdsOrigem.RecordCount >= cdsDestino.RecordCount then
begin
while not cdsOrigem.Eof do
begin
while not cdsDestino.Eof do
begin
if cdsOrigem.FieldByName('VALORTOTAL').AsCurrency > cdsDestino.FieldByName('VALORTOTAL').AsCurrency then
begin
AdicionarRegistro();
fSaldoParcelaOrigem := cdsOrigem.FieldByName('VALORTOTAL').AsCurrency - cdsDestino.FieldByName('VALORTOTAL').AsCurrency;
cdsDestino.Next;
while fSaldoParcelaOrigem > 0 do
begin
AdicionarRegistro();
fSaldoParcelaOrigem := fSaldoParcelaOrigem - cdsDestino.FieldByName('VALORTOTAL').AsCurrency;
cdsDestino.Next;
AdicionarRegistro();
end;
cdsOrigem.Next;
AdicionarRegistro();
end;
if cdsDestino.FieldByName('VALORTOTAL').AsCurrency > cdsOrigem.FieldByName('VALORTOTAL').AsCurrency then
begin
AdicionarRegistro();
fSaldoParcelaDestino := cdsDestino.FieldByName('VALORTOTAL').AsCurrency - cdsOrigem.FieldByName('VALORTOTAL').AsCurrency;
cdsOrigem.Next;
while fSaldoParcelaDestino > 0 do
begin
AdicionarRegistro();
fSaldoParcelaDestino := fSaldoParcelaDestino - cdsOrigem.FieldByName('VALORTOTAL').AsCurrency;
cdsOrigem.Next;
AdicionarRegistro();
end;
cdsDestino.Next;
AdicionarRegistro();
end;
if cdsDestino.FieldByName('VALORTOTAL').AsCurrency = cdsOrigem.FieldByName('VALORTOTAL').AsCurrency then
begin
AdicionarRegistro();
cdsOrigem.Next;
cdsDestino.Next;
fSaldoParcelaDestino := 0;
fSaldoParcelaOrigem := 0;
end;
cdsDestino.Next;
end;
cdsOrigem.Next;
end;
end;
if cdsOrigem.RecordCount < cdsDestino.RecordCount then
begin
while not cdsDestino.Eof do
begin
while not cdsOrigem.Eof do
begin
if cdsDestino.FieldByName('VALORTOTAL').AsCurrency < cdsOrigem.FieldByName('VALORTOTAL').AsCurrency then
begin
AdicionarRegistro();
fSaldoParcelaOrigem := origens[i] - cdsDestino.FieldByName('VALORTOTAL').AsCurrency;
cdsDestino.Next;
while fSaldoParcelaOrigem > 0 do
begin
AdicionarRegistro();
fSaldoParcelaOrigem := fSaldoParcelaOrigem - cdsDestino.FieldByName('VALORTOTAL').AsCurrency;
cdsDestino.Next;
AdicionarRegistro();
end;
cdsOrigem.Next;
AdicionarRegistro();
end;
if cdsDestino.FieldByName('VALORTOTAL').AsCurrency > cdsOrigem.FieldByName('VALORTOTAL').AsCurrency then
begin
AdicionarRegistro();
fSaldoParcelaDestino := cdsDestino.FieldByName('VALORTOTAL').AsCurrency - cdsOrigem.FieldByName('VALORTOTAL').AsCurrency;
cdsOrigem.Next;
while fSaldoParcelaDestino > 0 do
begin
AdicionarRegistro();
fSaldoParcelaDestino := fSaldoParcelaDestino - cdsOrigem.FieldByName('VALORTOTAL').AsCurrency;
cdsOrigem.Next;
AdicionarRegistro();
end;
cdsDestino.Next;
AdicionarRegistro();
end;
if cdsDestino.FieldByName('VALORTOTAL').AsCurrency = cdsOrigem.FieldByName('VALORTOTAL').AsCurrency then
begin
AdicionarRegistro();
cdsOrigem.Next;
cdsDestino.Next;
fSaldoParcelaDestino := 0;
fSaldoParcelaOrigem := 0;
end;
cdsOrigem.Next;
end;
cdsDestino.Next;
end;
end;
end;
procedure AdicionarRegistro;
begin
cdsReceberOrigem.Append;
cdsReceberOrigemFILIAL.AsInteger := cdsValorOrigemCODFILIAL.AsInteger;
cdsReceberOrigemCODIGO.AsInteger := cdsValorOrigemCODPARCELA.AsInteger;
cdsReceberOrigemFILIALDEST.AsInteger := cdsValoresParcelasCODFILIAL.AsInteger;
cdsReceberOrigemCODIGODEST.AsInteger := cdsValoresParcelasCODPARCELA.AsInteger;
cdsReceberOrigem.Post;
end;
UNDERSTAND THAT
Source values: 400, 200, 100 Destination values: 350, 350
Here are the correct steps:
The first source of value 400 must be distributed to the two destinations of 350. This results in a call to AddRecord. The second source of value 200 is not enough to completely cover the remaining destinations, so it is distributed as follows: First destination: 200 Second destination: 150 (350 – 200) This results in a second call to AddRecord. The third source of value 100 is smaller than any of the remaining destinations, so it is distributed as follows: First destination: 200 Second destination: 250 (150 + 100) This results in a third call to AddRecord. Therefore, the total number of calls to AddRecord should be 3.
500 fits in how many destinations, fits in 2, right, there would be 2 records, with the first origin, in the first record the first origin and first destination, in the second record the first origin and the second destination and one more record with the 2nd origin and the 2nd destination and one more record with the third origin and the second destination
I need to have more precise control of balances so that the AdicionarRegistro routine is executed in the correct way and quantity,
destination and origin balance control must be correct
I need to have more precise control of balances so that the AdicionarRegistro routine is executed in the correct way and quantity,
destination and origin balance control must be correct
Rodrigo Ernandes de Paiva is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.