unit unitDiactivate;

interface

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

type
  TfrmDiactivate = class(TForm)
    Label3: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    txtID: TEdit;
    txtBuil: TEdit;
    txtRMNo: TEdit;
    rbDiactivate: TRadioButton;
    BitBtn1: TBitBtn;
    btnSave: TBitBtn;
    procedure btnSaveClick(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure updateRoom(roomnum : integer);
  private
    { Private declarations }
  public
      stat : String;
      primary : String;
      builID : Integer;
      RMNo : Integer;
      procedure save;

  end;

var
  frmDiactivate: TfrmDiactivate;

implementation

uses DM, unitLogin, unitOcc;

{$R *.dfm}

procedure TfrmDiactivate.btnSaveClick(Sender: TObject);
var rn : Integer;
begin
    rn := StrToInt(txtRMNo.Text);
    save;
    updateRoom(rn);

end;
procedure TfrmDiactivate.BitBtn1Click(Sender: TObject);
begin
    Close;
end;

procedure TfrmDiactivate.save;
begin
     if (rbDiactivate.Checked = true) then
      begin
        stat := '0';
        end
        else
    if DataModule1.tr.InTransaction then
       DataModule1.tr.Commit;

       DataModule1.query.SQL.Clear;
        DataModule1.query.SQL.Add('UPDATE OCCUPANTS set OCC_STATUS = :A, DIACBY = :B, MODDATE = :C WHERE OCC_ID = :D');
        DataModule1.query.Params.AsString[0] := stat;
        DataModule1.query.Params.AsString[1] := frmLogin.fullname;
         DataModule1.query.Params.AsString[2] := DateToStr(Now());
        DataModule1.query.Params.AsString[3] := Label2.Caption;
        DataModule1.query.Prepare;
        DataModule1.query.Open();

            if DataModule1.tr.InTransaction then
              DataModule1.tr.Commit;
                ShowMessage('Occupants has been diactivated.');
                frmOcc.loadLv;
                Close();
end;

procedure TfrmDiactivate.updateRoom(roomnum : integer);
var count : Integer;
begin
    If DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

         DataModule1.query.SQL.Clear;
         DataModule1.query.SQL.Add('SELECT  a.CURRENTNO FROM ROOM a WHERE a.RM_NO = :a');
         DataModule1.query.Params.AsInteger[0] := roomnum;
         DataModule1.query.prepare;
         DataModule1.query.Open;
         count := DataModule1.query.Fields.AsInteger[0];
         count := count - 1;
         If DataModule1.tr.InTransaction then
            DataModule1.tr.Commit;

         DataModule1.query.SQL.Clear;
         DataModule1.query.SQL.Add('update room set currentno =:b where rm_no = :c');
         DataModule1.query.Params.AsInteger[0] := count;
         DataModule1.query.Params.AsInteger[1] := roomnum;
         DataModule1.query.prepare;
         DataModule1.query.Execute;

end;
end.
