unit P3Q_TypyUkolu;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, P3KvaFormQ, System.ImageList,
  Vcl.ImgList, System.Actions, Vcl.ActnList, KvaActionSource, Vcl.Menus,
  Vcl.ExtCtrls, AdvGlowButton, AdvSmoothLabel, Vcl.StdCtrls, GradientLabel,
  AdvPanel, AdvSplitter, AdvUtil, Vcl.Grids, AdvObj, BaseGrid, AdvGrid, P3Types,
  P3ObjPPC, P3ObjSfr, P3Global, P3Sys001, P3SysTxt, P3VizTyp, P3TxtErr;

type
  TQ_TypyUkolu = class(TKvaFormQ)
    OQTypyUkolu: TAdvStringGrid;
    BQucosnovyInsert: TAdvGlowButton;
    BQucosnovyEdit: TAdvGlowButton;
    BQucosnovyDelete: TAdvGlowButton;
    ACEdit: TKvaAction;
    ACDelete: TKvaAction;
    procedure OQTypyUkoluCanEditCell(Sender: TObject; ARow, ACol: Integer;
      var CanEdit: Boolean);
    procedure OQTypyUkoluCanSort(Sender: TObject; ACol: Integer;
      var DoSort: Boolean);
    procedure OQTypyUkoluDblClickCell(Sender: TObject; ARow, ACol: Integer);
    procedure OQTypyUkoluGetAlignment(Sender: TObject; ARow, ACol: Integer;
      var HAlign: TAlignment; var VAlign: TVAlignment);
    procedure OQTypyUkoluGetCellColor(Sender: TObject; ARow, ACol: Integer;
      AState: TGridDrawState; ABrush: TBrush; AFont: TFont);
    procedure OQTypyUkoluGetDisplText(Sender: TObject; ACol, ARow: Integer;
      var Value: string);
    procedure ACinsertExecute(Sender: TObject);
    procedure ACEditExecute(Sender: TObject);
    procedure ACDeleteExecute(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    function CanDelete(ATypUko:Integer):Boolean;
  public
    { Public declarations }
    AktMes:Integer;
    AktRok:Integer;
    TypyUkolu:TKvaTypUkoluList;
    SetFormularTypyUkoluEdit:TKvaSetFormular;
    SetFormularQuest:TKvaSetFormular;
    SetFormularWarn:TKvaSetFormular;
    procedure InitGrid(Which:Smallint);
    procedure ReFillGrid(Which:Smallint);
    procedure InitFirstGo; override;
    procedure InitForm(NewSectiom:TKvaTypeSection); override;
    procedure TopExit; override;
    function CorrectForm:integer; override;
    constructor CreateNew(AOwner:TComponent; Dummy: Integer=0); override;
  end;

var
  Q_TypyUkolu: TQ_TypyUkolu;

implementation

{$R *.dfm}

uses P3E_TypyUkolu, P3ObjSQL;

{ TQ_TypyUkolu }

procedure TQ_TypyUkolu.ACDeleteExecute(Sender: TObject);
var pomint:integer;
    PomActive:Integer;
    Modified,Sta:Boolean;
    PomStr:String;
begin
  inherited;
   if (ACDelete.Enabled)and(TypyUkolu<>nil) then
   begin
      if(OQTypyUkolu.Enabled) then OQTypyUkolu.SetFocus;
      PomActive:=OQTypyUkolu.Row;
      Modified:=false;
      if (TypyUkolu.NumRows>0) then
      begin
         var deTypyUkolu:=TypyUkolu.TypUkolu[OQTypyUkolu.Row-1];
         if (deTypyUkolu<>nil) and (CanDelete(deTypyUkolu.typuko)) then
           begin
            if ShowQuestion(AktUzivatel,Txt(QTxtSmazatZaznam),true,SetFormularQuest) then
              begin
              TypyUkolu.DeleteRow(OQTypyUkolu.Row-1);
              PomActive:=PomActive-1;
              Modified:=true;
              end;
           end
         else
          begin
             ShowWarning(AktUzivatel,'Daný typ úkolu je používán!',0,5,SetFormularWarn);
          end;
         if Modified then
           begin
              ReFillGrid(0);
              if not(DataSet.Modified) then DataSet.Modified:=true;
              if PomActive>0 then OQTypyUkolu.Row:=PomActive;
           end;
      end;
      if OQTypyUkolu.Enabled then OQTypyUkolu.SetFocus;
   end;
end;

procedure TQ_TypyUkolu.ACEditExecute(Sender: TObject);
var
   pomint:integer;
   PomActive:Integer;
   Modified:Boolean;
begin
  inherited;
   if (ACEdit.Enabled)and(TypyUkolu<>nil) then
   begin
      if(OQTypyUkolu.Enabled) then OQTypyUkolu.SetFocus;
      PomActive:=OQTypyUkolu.Row;
      Modified:=false;
      if (TypyUkolu<>nil)and(TypyUkolu.NumRows>0)and(OQTypyUkolu.Row>0) then
      begin
         var deTypyUkolu:=TypyUkolu.TypUkolu[OQTypyUkolu.Row-1];
         if E_TypyUkolu=nil then E_TypyUkolu:=TE_TypyUkolu.CreateNew(Application,0);
         E_TypyUkolu.Visible:=false;
         E_TypyUkolu.IfInsert:=false;
         E_TypyUkolu.Obdobi:=Obdobi;
         E_TypyUkolu.EditObj:=deTypyUkolu;
         E_TypyUkolu.AktUzivatel:=AktUzivatel;
         E_TypyUkolu.ConfigList:=ConfigList;
         E_TypyUkolu.SetFormular:=SetFormularTypyUkoluEdit;
         E_TypyUkolu.SetFormularEditPoloz:=SetFormularEditPoloz;
         E_TypyUkolu.InitForm(tsDefault);
         E_TypyUkolu.ShowModal;
         if E_TypyUkolu.Potvrzeno then
           begin
             OldSection:=AktSection;
             Modified:=true;
           end;
         E_TypyUkolu.Destroy;
         E_TypyUkolu:=nil;
         if Modified then
           begin
             ReFillGrid(0);
             DataSet.Modified:=true;
           end;
      end;
      if OQTypyUkolu.Enabled then OQTypyUkolu.SetFocus;
   end;
end;

procedure TQ_TypyUkolu.ACinsertExecute(Sender: TObject);
var
   pomint:integer;
   PomActive:Integer;
   Modified:Boolean;
begin
   if (ACinsert.Enabled)and(TypyUkolu<>nil) then
   begin
      if(OQTypyUkolu.Enabled) then OQTypyUkolu.SetFocus;
      PomActive:=OQTypyUkolu.Row;
      Modified:=false;
      pomint:=TypyUkolu.InsertRow(TypyUkolu.Count);
      var inTypyUkolu:=TypyUkolu.TypUkolu[pomint];
      if E_TypyUkolu=nil then E_TypyUkolu:=TE_TypyUkolu.CreateNew(Application,0);
      E_TypyUkolu.Visible:=false;
      E_TypyUkolu.IfInsert:=true;
      E_TypyUkolu.EditObj:=inTypyUkolu;
      E_TypyUkolu.AktUzivatel:=AktUzivatel;
      E_TypyUkolu.Obdobi:=Obdobi;
      E_TypyUkolu.ConfigList:=ConfigList;
      E_TypyUkolu.SetFormular:=SetFormularTypyUkoluEdit;
      E_TypyUkolu.SetFormularEditPoloz:=SetFormularEditPoloz;
      E_TypyUkolu.InitForm(tsDefault);
      E_TypyUkolu.ETypyUkolu.typuko:=TypyUkolu.MaxKodDleKodu+1;
      E_TypyUkolu.ShowModal;
      if E_TypyUkolu.Potvrzeno then
      begin
         OldSection:=AktSection;
         Modified:=true;
         PomActive:=pomint+1;
      end else
      begin
         inTypyUkolu:=nil;
         TypyUkolu.DeleteRow(pomint);
      end;
      E_TypyUkolu.Destroy;
      E_TypyUkolu:=nil;
      if Modified then
      begin
         ReFillGrid(0);
         DataSet.Modified:=true;
      end;
      if OQTypyUkolu.Enabled then OQTypyUkolu.SetFocus;
   end;
end;

function TQ_TypyUkolu.CanDelete(ATypUko: Integer): Boolean;
var Sel:TKvaSelecter;
begin
  Result:=True;
  Sel:=TKvaSelecter.Create(self);
  Sel.Database:=TypyUkolu.SQL.Connect.DbUsr;
  Sel.Sloupce:='COUNT(*)';
  Sel.Tabulka:='csukol';
  Sel.Podminka:='typuko=:typuko';
  Sel.OrderBy:='';
  Sel.Hodnoty.ParamByName('typuko').AsInteger:=ATypUko;
  Sel.OpenCursor;
  if Sel.SQLError<>100 then
     Result:=(Sel.Fields[0].AsInteger=0);
  Sel.CloseCursor;
  Sel.Destroy;
end;

function TQ_TypyUkolu.CorrectForm: integer;
begin
  Result:=inherited CorrectForm;
end;

constructor TQ_TypyUkolu.CreateNew(AOwner: TComponent; Dummy: Integer);
begin
  inherited;
   ReadComponentResFile(GUnitsDir+'\P3Q_TypyUkolu.dfm',self);
end;

procedure TQ_TypyUkolu.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
   TopExit;
end;

procedure TQ_TypyUkolu.InitFirstGo;
begin
  inherited;
   FDefTop:=5;
   FDefLeft:=5;
   Constraints.MinHeight:= 350;
   Constraints.MinWidth:= 750;
   Constraints.MaxHeight:=GMaxHeight;
   Constraints.MaxWidth:=GMaxWidth;
  inherited;
   SetParamDateW8;
   AktMes:=Obdobi.Month;
   AktRok:=Obdobi.Year;
   Caption:=AktUzivatel.CaptionPERM(Width,'5TypyUkolu',GUserMan);
   XEheadtext.Caption:=TxtCaption('5TypyUkolu');
   AktTypeForm:=tfTypyUkolu;
   OQTypyUkolu.Visible:=true;
   OQTypyUkolu.RowCount:=2;
   OQTypyUkolu.FixedRows:=1;
   OQTypyUkolu.Options := OQTypyUkolu.Options + [goRowSelect];
   OQTypyUkolu.ColCount:=4;
   OQTypyUkolu.ColWidths[0]:=20;
   OQTypyUkolu.ColWidths[1]:=30;
   OQTypyUkolu.ColWidths[2]:=80;
   OQTypyUkolu.ColWidths[3]:=200;
   ACfiltrn.Visible:=False;
   XEtopfiltr.Visible:=False;
   FirstGo:=false;
   FindGo(OQTypyUkolu);
end;

procedure TQ_TypyUkolu.InitForm(NewSectiom: TKvaTypeSection);
begin
  inherited;
   if FirstGo then InitFirstGo;
   InitGrid(0);
   ReFillGrid(0);
end;

procedure TQ_TypyUkolu.InitGrid(Which: Smallint);
begin
   OQTypyUkolu.DefaultRowHeight:=12+(Lsf_BigFormy*4);
   OQTypyUkolu.RowHeights[0]:=30+(Lsf_BigFormy*4);
end;

procedure TQ_TypyUkolu.OQTypyUkoluCanEditCell(Sender: TObject; ARow,
  ACol: Integer; var CanEdit: Boolean);
begin
  inherited;
   CanEdit:=false;
end;

procedure TQ_TypyUkolu.OQTypyUkoluCanSort(Sender: TObject; ACol: Integer;
  var DoSort: Boolean);
begin
  inherited;
   DoSort:=false;
end;

procedure TQ_TypyUkolu.OQTypyUkoluDblClickCell(Sender: TObject; ARow,
  ACol: Integer);
begin
  inherited;
  if(OQTypyUkolu.Row>0)then
    ACEdit.Execute;
end;

procedure TQ_TypyUkolu.OQTypyUkoluGetAlignment(Sender: TObject; ARow,
  ACol: Integer; var HAlign: TAlignment; var VAlign: TVAlignment);
begin
  inherited;
  if ARow=0 then
   HAlign:=taCenter
  else
    case ACol of
      2,3: HAlign:=taLeftJustify;
      else HAlign:=taCenter;
    end;
end;

procedure TQ_TypyUkolu.OQTypyUkoluGetCellColor(Sender: TObject; ARow,
  ACol: Integer; AState: TGridDrawState; ABrush: TBrush; AFont: TFont);
begin
  inherited;
   AFont.Size:=6+(Lsf_BigFormy*2);
end;

procedure TQ_TypyUkolu.OQTypyUkoluGetDisplText(Sender: TObject; ACol,
  ARow: Integer; var Value: string);
begin
  inherited;
   if ARow>0 then
     begin
       var shTypyUkolu:=TypyUkolu.TypUkolu[ARow-1];
       if shTypyUkolu<>nil then
         case ACol of
         1:  Value:=shTypyUkolu.typuko.ToString;
         2:  Value:=shTypyUkolu.zkrtyp;
         3:  Value:=shTypyUkolu.naztyp;
         end;
     end;
end;

procedure TQ_TypyUkolu.ReFillGrid(Which: Smallint);
var
   i:integer;
   NumRecords:integer;
   SomeRecord:boolean;
   myAllow:boolean;
begin
  NumRecords:=0;
  SomeRecord:=false;
  if (TypyUkolu<>nil) then
  begin
     ACInsert.Enabled:=true;
     NumRecords:=TypyUkolu.NumRows;
     if (NumRecords>0) then OQTypyUkolu.RowCount:=NumRecords+1
     else OQTypyUkolu.RowCount:=1;
     SomeRecord:=NumRecords>0;
     ACEdit.enabled:=SomeRecord;
     ACDelete.enabled:=SomeRecord;
  end else
  begin
     OQTypyUkolu.RowCount:=1;
     ACInsert.enabled:=false;
     ACEdit.enabled:=false;
     ACDelete.enabled:=false;
  end;
  OQTypyUkolu.Invalidate;
end;

procedure TQ_TypyUkolu.TopExit;
begin
  inherited;
   ChangeForm(AktTypeForm,tsDefault,tfStart,tsDefault);
end;

end.
