unit loginForm;

interface

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

type
  TfrmOccLogin = class(TForm)
    GroupBox1: TGroupBox;
    rbHome: TRadioButton;
    rbSchool: TRadioButton;
    rbOvernyt: TRadioButton;
    StatusBar1: TStatusBar;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    txtNamePerson: TEdit;
    txtAdd: TEdit;
    txtMobileNum: TEdit;
    Label4: TLabel;
    Timer1: TTimer;
    Label5: TLabel;
    txtReason: TEdit;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    per: TRadioButton;
    Label9: TLabel;
    Label10: TLabel;
    procedure Timer1Timer(Sender: TObject);
    procedure rbSchoolClick(Sender: TObject);
    procedure rbHomeClick(Sender: TObject);
    procedure rbOvernytClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure perClick(Sender: TObject);
   
  private
    { Private declarations }
  public
        systemDate : TDateTime;
        DateTimeformat : string;
        logid : Integer;
        occupantID : Integer;
        procedure getprimaryKey;
        procedure statSchool;
        procedure statHome;
        procedure overnyt;
        procedure clear;
        procedure statPer;
  end;

var
  statusToken : Integer;
  frmOccLogin: TfrmOccLogin;

implementation

uses DM, unitStudentLog;

{$R *.dfm}

procedure TfrmOccLogin.clear;
begin
      StudentLog.txtID.Text := '';
      StudentLog.rbIN.Checked := false;
      StudentLog.rbOUT.Checked := false;
      StudentLog.Label4.Caption := '';
end;

procedure TfrmOccLogin.statPer;
begin
 getprimaryKey;
        StudentLog.loadid;
      if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

              DataModule1.query.SQL.Clear;
              DataModule1.query.SQL.Add('INSERT INTO LOGGED');
              DataModule1.query.SQL.Add('(LOGID,OCC_ID,DATELOG,TIMELOG,STATUSID)');
              DataModule1.query.SQL.Add('VALUES(:a, :b, :c, :d,:e)');
              DataModule1.query.Params.AsInteger[0] := logid;
              DataModule1.query.Params.AsInteger[1] := StudentLog.occupantID;
              DataModule1.query.Params.AsString[2] := DateToStr(Now());
              DataModule1.query.Params.AsString[3] := TimeToStr(Now());
              DataModule1.query.Params.AsInteger[4] := StrToInt(Label10.Caption);
              DataModule1.query.Prepare;
              DataModule1.query.Open();

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

                ShowMessage('You have successfully logged.');
                Close();
end;

procedure TfrmOccLogin.getprimaryKey;
var maximum:integer;
begin
      maximum := 1;
        if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;
          DataModule1.query.SQL.Clear();
          DataModule1.query.SQL.Add('SELECT LOGID FROM LOGGED');
          DataModule1.query.Prepare;
          DataModule1.query.Open();

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

end;

procedure  TfrmOccLogin.statSchool;
begin
      getprimaryKey;
      StudentLog.loadid;
      if DataModule1.tr.InTransaction then
              DataModule1.tr.Commit;

              DataModule1.query.SQL.Clear;
              DataModule1.query.SQL.Add('INSERT INTO LOGGED');
              DataModule1.query.SQL.Add('(LOGID,OCC_ID,DATELOG,TIMELOG,STATUSID)');
              DataModule1.query.SQL.Add('VALUES(:a, :b, :c, :d,:e)');
              DataModule1.query.Params.AsInteger[0] := logid;
              DataModule1.query.Params.AsInteger[1] := StudentLog.occupantID;
              DataModule1.query.Params.AsString[2] := DateToStr(Now());
              DataModule1.query.Params.AsString[3] := TimeToStr(Now());
              DataModule1.query.Params.AsInteger[4] := StrToInt(Label6.Caption);
              DataModule1.query.Prepare;
              DataModule1.query.Open();

              if DataModule1.tr.InTransaction then
                DataModule1.tr.Commit;
                ShowMessage('You have successfully logged.');
               Close();
end;

procedure TfrmOccLogin.statHome;
begin
        getprimaryKey;
        StudentLog.loadid;
      if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

              DataModule1.query.SQL.Clear;
              DataModule1.query.SQL.Add('INSERT INTO LOGGED');
              DataModule1.query.SQL.Add('(LOGID,OCC_ID,DATELOG,TIMELOG,STATUSID)');
              DataModule1.query.SQL.Add('VALUES(:a, :b, :c, :d,:e)');
              DataModule1.query.Params.AsInteger[0] := logid;
              DataModule1.query.Params.AsInteger[1] := StudentLog.occupantID;
              DataModule1.query.Params.AsString[2] := DateToStr(Now());
              DataModule1.query.Params.AsString[3] := TimeToStr(Now());
              DataModule1.query.Params.AsInteger[4] := StrToInt(Label7.Caption);
              DataModule1.query.Prepare;
              DataModule1.query.Open();

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

                ShowMessage('You have successfully logged.');
                Close();
end;

procedure TfrmOccLogin.overnyt;
begin
      getprimaryKey;
      StudentLog.loadid;
      if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

          DataModule1.query.SQL.Clear;
          DataModule1.query.SQL.Add('INSERT INTO LOGGED');
          DataModule1.query.SQL.Add('(LOGID,OCC_ID, DATELOG, TIMELOG, STATUSID, NAME, ADDRESS, CONTACTNO, REASON_OVERNYT)');
          DataModule1.query.SQL.Add('VALUES (:a, :b, :c, :d, :e, :f, :g,:h, :i)');
          DataModule1.query.Params.AsInteger[0] := logid;
          DataModule1.query.Params.AsInteger[1] := StudentLog.occupantID;
          DataModule1.query.Params.AsString[2] := DateToStr(Now());
          DataModule1.query.Params.AsString[3] := TimeToStr(Now());
          DataModule1.query.Params.AsInteger[4] := StrToInt(Label8.Caption);
          DataModule1.query.Params.AsString[5] := txtNamePerson.Text;
          DataModule1.query.Params.AsString[6] := txtAdd.Text;
          DataModule1.query.Params.AsString[7] := txtMobileNum.Text;
          DataModule1.query.Params.AsString[8] := txtReason.Text;
          DataModule1.query.Prepare;
          DataModule1.query.Open();

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

              ShowMessage('You have successfully logged.');
end;

procedure TfrmOccLogin.Timer1Timer(Sender: TObject);
begin
        StatusBar1.Panels.Items[2].Text := 'Time : ' +timetostr(Now);
end;

procedure TfrmOccLogin.rbSchoolClick(Sender: TObject);
begin
    if (rbSchool.Checked = true) then
          begin
            statSchool;
            StudentLog.rbOUT.Checked := false;
            StudentLog.txtID.Clear;
            Close;
        end
end;

procedure TfrmOccLogin.rbHomeClick(Sender: TObject);
begin
    if (rbHome.Checked = true) then
    begin
      statHome;
      StudentLog.rbOUT.Checked := false;
      StudentLog.txtID.Clear;
      Close;
    end;
end;

procedure TfrmOccLogin.rbOvernytClick(Sender: TObject);
begin
    if (txtNamePerson.Text = '') then
    begin
      MessageDlg('Please fill in the required fields when having an overnight.',mtInformation,[mbOK],0);
        txtNamePerson.Enabled := true;
        txtAdd.Enabled := true;
        txtMobileNum.Enabled := true;
        txtReason.Enabled := true;
        txtNamePerson.SetFocus;
        rbOvernyt.Checked := false;
    end
        else if (rbOvernyt.Checked = true) then
        begin
            overnyt;
            StudentLog.rbOUT.Checked := false;
            StudentLog.txtID.Clear;
            Close;
        end;
end;

procedure TfrmOccLogin.FormActivate(Sender: TObject);
begin
       DateTimeToString(DateTimeformat, 'dddddd', now());
       Label4.Caption := DateTimeformat;
       StatusBar1.Panels.Items[1].Text := label4.Caption;
       txtNamePerson.Enabled := false;
       txtAdd.Enabled := False;
       txtMobileNum.Enabled := false;
       txtReason.Enabled := false;
       rbHome.Checked := false;
       rbSchool.Checked := false;
       rbOvernyt.Checked := false;
end;

procedure TfrmOccLogin.perClick(Sender: TObject);
begin
if (per.Checked = true) then
    begin
      statPer;
      StudentLog.rbOUT.Checked := false;
      StudentLog.txtID.Clear;
      Close;
    end;
end;

end.
