unit unitRMFacility;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ComCtrls;

type
  TfrmRmFac = class(TForm)
    GroupBox1: TGroupBox;
    txtType: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    sb: TCheckBox;
    dd: TCheckBox;
    cr: TCheckBox;
    BitBtn1: TBitBtn;
    btnSave: TBitBtn;
    Memo1: TMemo;
    air: TCheckBox;
    StatusBar1: TStatusBar;
    Label1: TLabel;
    procedure btnSaveClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
        id : Integer;
        airCon : String;
        hascr : String;
        bed : String;
         systemDate : TDateTime;
     DateTimeformat : string;
        procedure saveFac;
        procedure bedDesc;
        procedure aircond;
        procedure Cr1;
        procedure getID;

  end;

var
  frmRmFac: TfrmRmFac;

implementation

uses DM, unitLogin;

{$R *.dfm}

procedure TfrmRmFac.aircond;
begin
     if (air.Checked = true) then
    begin
        airCon := '1';
    end
        else if (air.Checked = false) then
             begin
                  airCon := '0';
              end
end;
procedure TfrmRmFac.Cr1;
begin
     if (cr.Checked = true) then
    begin
        hascr := '1';
    end
        else if (cr.Checked = false) then
             begin
                  hascr := '0';
              end
end;

procedure TfrmRmFac.getID;
var idtyp : Integer;
begin
      idtyp :=1;
        if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;
          DataModule1.query.SQL.Clear();
          DataModule1.query.SQL.Add('SELECT type_id');
          DataModule1.query.SQL.Add('FROM rm_type');
          DataModule1.query.Prepare;
          DataModule1.query.Open();

              if DataModule1.query.Fields.Eof then
                  id := idtyp
                    else begin
                        DataModule1.query.SQL.Clear;
                        DataModule1.query.SQL.Add('SELECT MAX(type_id) FROM rm_type');
                        DataModule1.query.Prepare;
                        DataModule1.query.Open();
                        id := DataModule1.query.Fields.AsInteger[0] + 1;
                end;
                      if  DataModule1.tr.InTransaction then
                          DataModule1.tr.Commit;

end;


procedure TfrmRmFac.bedDesc;
begin
    if (sb.Checked = true) And (dd.Checked = true) then
      begin
          bed := 'Single Bed and Double Deck';
      end
        else if (sb.Checked = true) Then
        begin
            bed := 'Single Bed';
        end
            else if (dd.Checked = true) then
            begin
                bed := 'Double Deck';
            end
end;

procedure TfrmRmFac.saveFac;
begin
       getID;
       aircond;
       Cr1;
       bedDesc;
      if DataModule1.tr.InTransaction then
        DataModule1.tr.Commit;

        DataModule1.query.SQL.Clear;
       DataModule1.query.SQL.Add('INSERT INTO RM_TYPE (TYPE_ID, DESC, BEDDESC, ISAIRCONDITIONED, HASCR, AMENITIES, ADDEDBY, DATEADD) VALUES (:a, :b, :d, :e, :f, :g, :h, :i)');
        DataModule1.query.Params.AsInteger[0] := id;
        DataModule1.query.Params.AsString[1] := txtType.Text;
        DataModule1.query.Params.AsString[2] := bed;
        DataModule1.query.Params.AsString[3] := airCon;
        DataModule1.query.Params.AsString[4] := hascr;
        DataModule1.query.Params.AsString[5] := Memo1.Text;
        DataModule1.query.Params.AsString[6] := frmLogin.fullname;
        DataModule1.query.Params.AsString[7] := DateToStr(Now());
        DataModule1.query.Prepare;
        DataModule1.query.Open();

        if DataModule1.tr.InTransaction then
        DataModule1.tr.Commit;

        ShowMessage('Successfully saved');
        Close();
end;

procedure TfrmRmFac.btnSaveClick(Sender: TObject);
begin
      saveFac;
end;

procedure TfrmRmFac.FormShow(Sender: TObject);
begin
      DateTimeToString(DateTimeformat, 'dddddd', now());
      Label1.Caption := DateTimeformat;
      StatusBar1.Panels.Items[1].Text := 'Date -- ' + label1.Caption;
end;

end.
