unit U_ProjectRev;

interface

uses
  Windows, U_LNG, Messages, SysUtils, Variants, Classes, Graphics, Controls, Contnrs, Forms,
  Dialogs, U_BaseCommon, U_BaseConstants, U_Common, U_SCSComponent, U_SCSLists, Math, ExtCtrls, RzPanel, RzButton,
  cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, kbmMemTable, StdCtrls, Mask, RzEdit,
  cxCurrencyEdit, cxCheckBox, cxColorComboBox, cxSpinEdit, cxDropDownEdit,
  cxDBLookupComboBox, cxTextEdit, ActnList, ActnMan, Menus, ComCtrls,
  ToolWin, XPMenu, siComp, siLngLnk, cxMemo, cxImageComboBox, Buttons,
  cxCalendar, FIBDatabase, pFIBDatabase, FIBDataSet, pFIBDataSet,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  PlatformDefaultStyleActnCtrls;

//const
  //Colmn Indexes
  //ciTransToUOM = 10;
const
  cntWorkDayHours = 8;

type
  TF_ProjectRev = class(TForm)
    RzGroupBox1: TRzGroupBox;
    gbOkCancel: TRzGroupBox;
    btOK: TRzBitBtn;
    btCancel: TRzBitBtn;
    RzPanel1: TRzPanel;
    GT_Revs: TcxGridDBTableView;
    GL_Revs: TcxGridLevel;
    gridRevs: TcxGrid;
    GT_RevsDiff: TcxGridDBColumn;
    GT_RevsNormCost: TcxGridDBColumn;
    ActionManager: TActionManager;
    PopupMenu: TPopupMenu;
    N2: TMenuItem;
    XPMenu1: TXPMenu;
    btApply: TRzBitBtn;
    lng_Forms: TsiLangLinked;
    GT_RevsRevision: TcxGridDBColumn;
    GT_RevsMaterialCost: TcxGridDBColumn;
    GT_RevsComment: TcxGridDBColumn;
    GT_RevsDate: TcxGridDBColumn;
    GT_RevsTotalCost: TcxGridDBColumn;
    GT_RevsBaseLine: TcxGridDBColumn;
    GT_RevsID: TcxGridDBColumn;
    dsProjRevs: TpFIBDataSet;
    transactProjRevs: TpFIBTransaction;
    dsrcProjRevs: TDataSource;
    btSetAsBaseline: TRzButton;
    aSetAsBaseLine: TAction;
    btApplyRevision: TRzButton;
    aApplyRev: TAction;
    Setasbaseline1: TMenuItem;
    procedure gbOkCancelResize(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure aSetAsBaseLineExecute(Sender: TObject);
    procedure aApplyRevExecute(Sender: TObject);
    procedure GT_RevsCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
  private
    GForm: TForm;

    procedure PrepareForm;
    procedure SetActions;
    procedure OnDrawCellAsChanged(ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo);
  public
    constructor Create(AOwner: TComponent; AForm: TForm); overload;
    destructor Destroy; override;

    function Execute: Boolean;
  end;

//var
  //F_ProjectRev: TF_ProjectRev;

implementation
Uses U_Main, Unit_DM_SCS, U_DMCommon;
{$R *.dfm}

{ TF_ProjectRev }

constructor TF_ProjectRev.Create(AOwner: TComponent; AForm: TForm);
begin
  GForm := AForm;
  inherited Create(AOwner);
end;

destructor TF_ProjectRev.Destroy;
begin

  inherited;
end;

function TF_ProjectRev.Execute: Boolean;
begin
  Result := false;
  PrepareForm;
  try
    if ShowModal = mrOk then
    begin
      Result := true;
    end;
  finally
    dsProjRevs.Active := False;
  end;
end;

procedure TF_ProjectRev.gbOkCancelResize(Sender: TObject);
begin
  SetMiddleControlChilds(TControl(Sender), TControl(Self));
end;

procedure TF_ProjectRev.FormCreate(Sender: TObject);
var
  i: Integer;
  Action: TAction;
begin
  for i := 0 to ActionManager.ActionCount - 1 do
  begin
    Action := TAction(ActionManager.Actions[i]);
    Action.Hint := Action.Caption;
  end;
end;

procedure TF_ProjectRev.SetActions;
begin
  aSetAsBaseLine.Enabled := dsProjRevs.RecNo <> 0;
  aApplyRev.Enabled := dsProjRevs.RecNo <> 0;
end;

procedure TF_ProjectRev.PrepareForm;
var
  DisplayFormatM: String;
begin
  gbOkCancelResize(gbOkCancel);
  //SetCxCurrencyEditProperties(GT_RevsMaterialCost.Properties);
  //SetCxCurrencyEditProperties(GT_RevsNormCost.Properties);
  //SetCxCurrencyEditProperties(GT_RevsTotalCost.Properties);
  //SetCxCurrencyEditProperties(GT_RevsDiff.Properties);

  DisplayFormatM := GetDisplayFormat(TF_Main(GForm).GCurrencyM.NameBrief);
  TcxCurrencyEditProperties(GT_RevsMaterialCost.Properties).DisplayFormat := DisplayFormatM;
  TcxCurrencyEditProperties(GT_RevsMaterialCost.Properties).DecimalPlaces := FloatPrecision;

  TcxCurrencyEditProperties(GT_RevsNormCost.Properties).DisplayFormat := DisplayFormatM;
  TcxCurrencyEditProperties(GT_RevsNormCost.Properties).DecimalPlaces := FloatPrecision;

  TcxCurrencyEditProperties(GT_RevsTotalCost.Properties).DisplayFormat := DisplayFormatM;
  TcxCurrencyEditProperties(GT_RevsTotalCost.Properties).DecimalPlaces := FloatPrecision;

  TcxCurrencyEditProperties(GT_RevsDiff.Properties).DisplayFormat := DisplayFormatM;
  TcxCurrencyEditProperties(GT_RevsDiff.Properties).DecimalPlaces := FloatPrecision;

  try
    dsProjRevs.DataBase := TF_Main(GForm).DM.Database_SCS;
    dsProjRevs.Active := False;
    dsProjRevs.DisableControls;
    try
      //dsProjRevs.SQLs.SelectSQL.Text := 'SELECT * FROM '+tnProjectRev+' WHERE ID_CATALOG = :ID_CATALOG'; //, 0 AS DIFF
      dsProjRevs.SQLs.SelectSQL.Text :=
          'SELECT PR.*, (PR.TOTAL_COST - BL.TOTAL_COST) as DIFF FROM PROJECT_REV PR '+
          'LEFT JOIN PROJECT_REV BL ON (BL.ID_CATALOG = PR.ID_CATALOG AND BL.baseline=1) '+
          'WHERE PR.ID_CATALOG = :ID_CATALOG';
      dsProjRevs.Prepare;
      dsProjRevs.ParamByName(fnIDCatalog).AsInteger := TF_Main(GForm).GSCSBase.CurrProject.ID;
    finally
      dsProjRevs.EnableControls;
      dsProjRevs.Active := true;
    end;
    SetActions;
  except
    on E: Exception do AddExceptionToLogExt(ClassName, 'PrepareForm', E.Message);
  end;
end;

procedure TF_ProjectRev.OnDrawCellAsChanged(ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo);
var
  RecID, CellColor: Integer;
begin
  {RecID := AViewInfo.GridRecord.Values[GT_NormsID.Index];
  CellColor := clBlue;
  if dsrcNorms.DataSet.RecNo > 0 then
    if RecID = dsrcNorms.DataSet.FieldByName(fnID).AsInteger then
      CellColor := clYellow;
  ACanvas.Font.Color := CellColor;}
end;

procedure TF_ProjectRev.FormDestroy(Sender: TObject);
begin
  //
end;

procedure TF_ProjectRev.aSetAsBaseLineExecute(Sender: TObject);
begin
  with TF_Main(GForm) do
  begin
    //SetSQLToFIBQuery(DM.Query_Operat, GetSQLByParams(qtUpdate, tnProjectRev, fnIDCatalog+'='+IntToStr(GSCSBase.CurrProject.ID), nil, fnBaseline), false);
    //DM.Query_Operat.Params[0].AsInteger := biFalse;
    //DM.Query_Operat.ExecQuery;
    DM.UpdateTableField(tnProjectRev, fnBaseline, fnIDCatalog, GSCSBase.CurrProject.ID, biFalse, qmPhisical);
    DM.UpdateTableField(tnProjectRev, fnBaseline, fnID, dsProjRevs.FN(fnID).AsInteger, biTrue, qmPhisical);
    dsProjRevs.ReopenLocate(fnID);
  end;
end;

procedure TF_ProjectRev.aApplyRevExecute(Sender: TObject);
var
  ProjStream: TMemoryStream;
  Node: TTreeNode;
begin
  try
    BeginProgress;
    ProjStream := TMemoryStream.Create;
    try
      with TF_Main(GForm) do
      begin
        Node := GSCSBase.CurrProject.TreeViewNode;
        //GSCSBase.CurrProject.ReadOnly := true;
        //try
        //  GSCSBase.CurrProject.Close;
        //finally
        //  GSCSBase.CurrProject.ReadOnly := false;
        //end;
        //SetProjectChanged(false);

        TBlobField(dsProjRevs.FN(fnPMBLock)).SaveToStream(ProjStream);
        ProjStream.Position := 0;
        GSCSBase.CurrProject.LoadFromStreamOrFile(ProjStream, '', false);

        //if Tree_Catalog.Items.In
        //ReloadNodes(Node);
      end;
    finally
      EndProgress;
    end;
  except
    on E: Exception do AddExceptionToLogExt(ClassName, 'aApplyRevExecute', E.Message);
  end;
end;

procedure TF_ProjectRev.GT_RevsCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if AViewInfo.GridRecord.Values[GT_RevsBaseLine.Index] = biTrue then
    ACanvas.Font.Style := ACanvas.Font.Style + [fsBold];
end;

end.
