delphi – precise control of balances

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

New contributor

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật