unit PhaseCompareDm;


interface

uses
  SysUtils, Classes, sdDB, sdIDTree, DB, DBClient, Provider, ADODB,
  sdProvider;

{const
  PhaseSelect0 = 'Select R.BillsID, R.GatherQuantity As Quantity0, R.GatherTotalPrice As TotalPrice0' +
                 '  From Refer As R';

  PhaseSelect1 = 'Select A1.BillsID, A1.GatherQuantity As Quantity1, A1.GatherTotalPrice As TotalPrice1,' +
                 '       R.GatherQuantity As Quantity0, R.GatherTotalPrice As TotalPrice0' +
                 '  From Audit1 As A1 Left Join Refer As R on A1.BillsID = R.BillsID';

  PhaseSelect2 = 'Select A2.BillsID, A2.GatherQuantity As Quantity2, A2.GatherTotalPrice As TotalPrice2,' +
                 '       A1.GatherQuantity As Quantity1, A1.GatherTotalPrice As TotalPrice1' +
                 '       R.GatherQuantity As Quantity0, R.GatherTotalPrice As TotalPrice0' +
                 '  From' +
                 '    (Audit2 As A2 Left Join Audit1 As A1 on A2.BillsID = A1.BillsID)'+
                 '    Left Join Refer As R on A2.BillsID = R.BillsID';

  PhaseSelect3 = 'Select A3.BillsID, A3.GatherQuantity As Quantity3, A3.GatherTotalPrice As TotalPrice3,' +
                 '       A2.GatherQuantity As Quantity2, A2.GatherTotalPrice As TotalPrice2' +
                 '       A1.GatherQuantity As Quantity1, A1.GatherTotalPrice As TotalPrice1' +
                 '       R.GatherQuantity As Quantity0, R.GatherTotalPrice As TotalPrice0' +
                 '  From' +
                 '    ((Audit3 As A3 Left Join Audit2 As A2 on A3.BillsID = A2.BillsID)'+
                 '     Left Join Audit1 As A1 on A3.BillsID = A1.BillsID)'+
                 '    Left Join Refer As R on A3.BillsID = R.BillsID';

  PhaseSelect4 = 'Select A4.BillsID, A4.GatherQuantity As Quantity4, A4.GatherTotalPrice As TotalPrice4,' +
                 '       A3.GatherQuantity As Quantity3, A3.GatherTotalPrice As TotalPrice3' +
                 '       A2.GatherQuantity As Quantity2, A2.GatherTotalPrice As TotalPrice2' +
                 '       A1.GatherQuantity As Quantity1, A1.GatherTotalPrice As TotalPrice1' +
                 '       R.GatherQuantity As Quantity0, R.GatherTotalPrice As TotalPrice0' +
                 '  From' +
                 '    (((Audit4 As A4 Left Join Audit3 As A3 on A4.BillsID = A3.BillsID)'+
                 '      Left Join Audit2 As A2 on A4.BillsID = A2.BillsID)'+
                 '     Left Join Audit1 As A1 on A4.BillsID = A1.BillsID)'+
                 '    Left Join Refer As R on A4.BillsID = R.BillsID';

  PhaseSelect14 = 'Select A14.BillsID, A14.GatherQuantity As Quantity14, A14.GatherTotalPrice As TotalPrice14,' +
                  '       A13.GatherQuantity As Quantity13, A13.GatherTotalPrice As TotalPrice13' +
                  '       A12.GatherQuantity As Quantity12, A12.GatherTotalPrice As TotalPrice12' +
                  '       A11.GatherQuantity As Quantity11, A11.GatherTotalPrice As TotalPrice11' +
                  '       A10.GatherQuantity As Quantity10, A10.GatherTotalPrice As TotalPrice10' +
                  '       A9.GatherQuantity As Quantity9,   A9.GatherTotalPrice As TotalPrice9' +
                  '       A8.GatherQuantity As Quantity8,   A8.GatherTotalPrice As TotalPrice8' +
                  '       A7.GatherQuantity As Quantity7,   A7.GatherTotalPrice As TotalPrice7' +
                  '       A6.GatherQuantity As Quantity6,   A6.GatherTotalPrice As TotalPrice6' +
                  '       A5.GatherQuantity As Quantity5,   A5.GatherTotalPrice As TotalPrice5' +
                  '       A4.GatherQuantity As Quantity4,   A4.GatherTotalPrice As TotalPrice4' +
                  '       A3.GatherQuantity As Quantity3,   A3.GatherTotalPrice As TotalPrice3' +
                  '       A2.GatherQuantity As Quantity2,   A2.GatherTotalPrice As TotalPrice2' +
                  '       A1.GatherQuantity As Quantity1,   A1.GatherTotalPrice As TotalPrice1' +
                  '       R.GatherQuantity As Quantity0,    R.GatherTotalPrice As TotalPrice0' +
                  '  From' +
                  '    (((((((((((((Audit14 As A14 Left Join Audit13 As A13 on A14.BillsID = A13.BillsID)'+
                  '                Left Join Audit12 As A12 on A14.BillsID = A12.BillsID)'+
                  '               Left Join Audit11 As A11 on A14.BillsID = A11.BillsID)'+
                  '              Left Join Audit20 As A10 on A14.BillsID = A10.BillsID)'+
                  '             Left Join Audit9 As A9 on A14.BillsID = A9.BillsID)'+
                  '            Left Join Audit8 As A8 on A14.BillsID = A8.BillsID)'+
                  '           Left Join Audit7 As A7 on A14.BillsID = A7.BillsID)'+
                  '          Left Join Audit6 As A6 on A14.BillsID = A6.BillsID)'+
                  '         Left Join Audit5 As A5 on A14.BillsID = A5.BillsID)'+
                  '        Left Join Audit4 As A4 on A14.BillsID = A4.BillsID)'+
                  '       Left Join Audit3 As A3 on A14.BillsID = A3.BillsID)'+
                  '      Left Join Audit2 As A2 on A4.BillsID = A2.BillsID)'+
                  '     Left Join Audit1 As A1 on A4.BillsID = A1.BillsID)'+
                  '    Left Join Refer As R on A4.BillsID = R.BillsID'; }


type
  TPhaseCompareData = class(TDataModule)
    sdvPhaseCompare: TsdDataView;
    procedure sdvPhaseCompareGetText(var Text: String;
      ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
      DisplayText: Boolean);
  private
    FProjectData: TObject;
    FCompareTree: TsdIDTree;
    FStageCount: Integer;

    procedure AddStageColumn(AStageIndex: Integer);
    procedure ClearStageColumn;
    procedure InitCompareColumns;

    procedure CreateCompareTree;
  public
    constructor Create(AProjectData: TObject);
    destructor Destroy; override;

    procedure Open;
    procedure Close;

    procedure ReConnectTree;

    procedure ExpandNodeTo(ALevel: Integer);
    procedure ExpandXmjNode;
    procedure ExpandCurPhase;

    property ProjectData: TObject read FProjectData;
    property CompareTree: TsdIDTree read FCompareTree;
  end;

implementation

uses
  ProjectData, PhaseData, StageDm, StageCompareDm, UtilMethods,
  ProjectProperty;

{$R *.dfm}

{ TPhaseCompareData }

procedure TPhaseCompareData.AddStageColumn(AStageIndex: Integer);
var
  vColumn: TsdViewColumn;
begin
  vColumn := sdvPhaseCompare.Columns.Add;
  vColumn.FieldName := Format('Quantity%d', [AStageIndex]);
  vColumn.KeyFields := 'ID';
  vColumn.LookupKeyFields := 'BillsID';
  vColumn.LookupResultField := Format('Quantity%d', [AStageIndex]);
  vColumn.LookupDataSet := TProjectData(FProjectData).PhaseData.StageCompareData.sddStageCompare;
  vColumn.DisplayFormat := TProjectData(FProjectData).ProjProperties.QuantityFormat;

  vColumn := sdvPhaseCompare.Columns.Add;
  vColumn.FieldName := Format('TotalPrice%d', [AStageIndex]);
  vColumn.KeyFields := 'ID';
  vColumn.LookupKeyFields := 'BillsID';
  vColumn.LookupResultField := Format('TotalPrice%d', [AStageIndex]);
  vColumn.LookupDataSet := TProjectData(FProjectData).PhaseData.StageCompareData.sddStageCompare;
  vColumn.DisplayFormat := TProjectData(FProjectData).ProjProperties.TotalPriceFormat;
end;

procedure TPhaseCompareData.ClearStageColumn;
var
  iStage: Integer;
  vCol: TsdViewColumn;
begin
  for iStage := 0 to FStageCount - 1 do
  begin
    vCol := sdvPhaseCompare.Columns.FindColumn('Quantity' + IntToStr(iStage));
    if Assigned(vCol) then
      sdvPhaseCompare.Columns.Delete(vCol.Index);
    vCol := sdvPhaseCompare.Columns.FindColumn('TotalPrice' + IntToStr(iStage));
    if Assigned(vCol) then
      sdvPhaseCompare.Columns.Delete(vCol.Index);
  end;
end;

procedure TPhaseCompareData.Close;
begin
  sdvPhaseCompare.Close;
end;

constructor TPhaseCompareData.Create(AProjectData: TObject);
begin
  inherited Create(nil);
  FProjectData := AProjectData;
  FStageCount := 0;
end;

procedure TPhaseCompareData.CreateCompareTree;
begin
  FCompareTree := TsdIDTree.Create;
  FCompareTree.KeyFieldName := 'ID';
  FCompareTree.ParentFieldName := 'ParentID';
  FCompareTree.NextSiblingFieldName := 'NextSiblingID';
  FCompareTree.AutoExpand := True;
  FCompareTree.DataView := sdvPhaseCompare;
end;

destructor TPhaseCompareData.Destroy;
begin
  FCompareTree.Free;
  inherited;
end;

procedure TPhaseCompareData.ExpandCurPhase;
var
  iIndex: Integer;
  stnNode: TsdIDTreeNode;
begin
  for iIndex := 0 to CompareTree.Count - 1 do
  begin
    stnNode := CompareTree.Items[iIndex];
    with TProjectData(FProjectData).PhaseData.StageCompareData do
      if (stnNode.ParentID <> -1) then
        stnNode.Expanded := HasPhaseData(stnNode.ID);
  end;
end;

procedure TPhaseCompareData.ExpandNodeTo(ALevel: Integer);
begin
  CompareTree.ExpandLevel := ALevel;
end;

procedure TPhaseCompareData.ExpandXmjNode;
var
  iIndex: Integer;
  stnNode: TsdIDTreeNode;
begin
  for iIndex := 0 to CompareTree.Count - 1 do
  begin
    stnNode := CompareTree.Items[iIndex];
    if (stnNode.ParentID <> -1) then
      stnNode.Parent.Expanded := stnNode.Rec.ValueByName('B_Code').AsString = '';
  end;
end;

procedure TPhaseCompareData.InitCompareColumns;
var
  iStage: Integer;
begin
  ClearStageColumn;
  if TProjectData(FProjectData).PhaseData.Active then
    for iStage := 0 to TProjectData(FProjectData).PhaseData.AuditCount do
      AddStageColumn(iStage);
  FStageCount := TProjectData(FProjectData).PhaseData.StageCount;
end;

procedure TPhaseCompareData.Open;
begin
  sdvPhaseCompare.DataSet := TProjectData(FProjectData).BillsData.sddBills;
  InitCompareColumns;
  sdvPhaseCompare.Open;
  CreateCompareTree;
end;

procedure TPhaseCompareData.ReConnectTree;
begin
  FCompareTree.DataView := nil;
  FCompareTree.DataView := sdvPhaseCompare;
  FCompareTree.Link(TProjectData(FProjectData).BillsCompileData.BillsCompileTree, True);
end;

procedure TPhaseCompareData.sdvPhaseCompareGetText(var Text: String;
  ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
  DisplayText: Boolean);
begin
  if Assigned(AValue) and (AValue.DataType = ftFloat) and (AValue.AsFloat = 0) then
    Text := '';
end;

end.
