unit DealBillsDm;

interface

uses
  SysUtils, Classes, sdDB, sdProvider, ADODB;

type
  TDealBillsData = class(TDataModule)
    sdpDealBills: TsdADOProvider;
    sddDealBills: TsdDataSet;
    sdvDealBills: TsdDataView;
    procedure sddDealBillsBeforeAddRecord(ARecord: TsdDataRecord;
      var Allow: Boolean);
    procedure sdvDealBillsSetText(var Text: String; ARecord: TsdDataRecord;
      AValue: TsdValue; AColumn: TsdViewColumn; var Allow: Boolean);
  private
    FProjectData: TObject;

    function GetNewID: Integer;
  public
    constructor Create(AProjectData: TObject);
    destructor Destroy; override;

    procedure Open(AConnection: TADOConnection);
    procedure Close;
    procedure Save;
    procedure Clear;

    procedure DisableEvent;
    procedure EnableEvent;

    function DealRecord(const AB_Code: string): TsdDataRecord;

    property ProjectData: TObject read FProjectData;
  end;

implementation

uses
  UtilMethods;

{$R *.dfm}

{ TDealBillsData }

procedure TDealBillsData.Clear;
begin
  sddDealBills.DeleteAll;
end;

procedure TDealBillsData.Close;
begin
  sddDealBills.Close;
end;

constructor TDealBillsData.Create(AProjectData: TObject);
begin
  inherited Create(nil);
  FProjectData := AProjectData;
end;

function TDealBillsData.DealRecord(const AB_Code: string): TsdDataRecord;
begin
  Result := sddDealBills.Locate('B_Code', AB_Code);
end;

destructor TDealBillsData.Destroy;
begin
  inherited;
end;

procedure TDealBillsData.DisableEvent;
begin
  sddDealBills.BeforeAddRecord := nil;
end;

procedure TDealBillsData.EnableEvent;
begin
  sddDealBills.BeforeAddRecord := sddDealBillsBeforeAddRecord;
end;

function TDealBillsData.GetNewID: Integer;
var
  idx: TsdIndex;
begin
  Result := 1;
  if sddDealBills.RecordCount = 0 then Exit;
  idx := sddDealBills.FindIndex('idxID');
  Result := idx.Records[sddDealBills.RecordCount - 1].ValueByName('ID').AsInteger + 1;
end;

procedure TDealBillsData.Open(AConnection: TADOConnection);
begin
  sdpDealBills.Connection := AConnection;
  sddDealBills.Open;
  if not Assigned(sddDealBills.IndexList.FindByName('idxID')) then
    sddDealBills.AddIndex('idxID', 'ID');
  sdvDealBills.Open;
  sdvDealBills.IndexName := 'idxID';
end;

procedure TDealBillsData.Save;

  procedure CheckIndexCode;
  var
    i: Integer;
    Rec: TsdDataRecord;
    sIndexCode: string;
  begin
    for i := 0 to sddDealBills.RecordCount - 1 do
    begin
      Rec := sddDealBills.Records[i];
      sIndexCode := B_CodeToIndexCode(Rec.ValueByName('B_Code').AsString);
      if sIndexCode <> Rec.ValueByName('IndexCode').AsString then
        Rec.ValueByName('IndexCode').AsString := sIndexCode;
    end;
  end;

begin
  CheckIndexCode;
  sddDealBills.Save;
end;

procedure TDealBillsData.sddDealBillsBeforeAddRecord(
  ARecord: TsdDataRecord; var Allow: Boolean);
begin
  ARecord.ValueByName('ID').AsInteger := GetNewID;
end;

procedure TDealBillsData.sdvDealBillsSetText(var Text: String;
  ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
  var Allow: Boolean);
begin
  if SameText(AColumn.FieldName, 'Price') then
    Text := FloatToStr(PriceRoundTo(StrToFloatDef(Text, 0)))
  else if SameText(AColumn.FieldName, 'Quantity') then
    Text := FloatToStr(QuantityRoundTo(StrToFloatDef(Text, 0)))
  else if SameText(AColumn.FieldName, 'TotalPrice') then
    Text := FloatToStr(TotalPriceRoundTo(StrToFloatDef(Text, 0)));
end;

end.
