unit unitCreateSched;

interface

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

type
  TfrmCreateSched = class(TForm)
    Label3: TLabel;
    StatusBar1: TStatusBar;
    btnCancel: TBitBtn;
    btnSave: TBitBtn;
    GroupBox1: TGroupBox;
    Label2: TLabel;
    Label12: TLabel;
    Label9: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label11: TLabel;
    Label8: TLabel;
    Label10: TLabel;
    Label1: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    txtSubj: TEdit;
    txtTitle: TEdit;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    CheckBox4: TCheckBox;
    CheckBox5: TCheckBox;
    CheckBox7: TCheckBox;
    CheckBox6: TCheckBox;
    dtpTime: TDateTimePicker;
    dtpSched: TDateTimePicker;
    txtRoom: TEdit;
    txtBuil: TEdit;
    txtAcad: TEdit;
    cmbSem: TComboBox;
    procedure btnSaveClick(Sender: TObject);
    procedure txtSubjKeyPress(Sender: TObject; var Key: Char);
    procedure txtTitleKeyPress(Sender: TObject; var Key: Char);
    procedure txtAcadKeyPress(Sender: TObject; var Key: Char);
    procedure cmbSemKeyPress(Sender: TObject; var Key: Char);
    procedure txtBuilKeyPress(Sender: TObject; var Key: Char);
    procedure txtRoomKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
      classSched : String;
      schedidPK : Integer;
      procedure clear;
      procedure saveSched;
      procedure getPrimaryKey;
  end;

var
  frmCreateSched: TfrmCreateSched;

implementation

uses DM;

{$R *.dfm}

procedure TfrmCreateSched.btnSaveClick(Sender: TObject);
begin
      saveSched;
end;

procedure TfrmCreateSched.clear;
begin
     txtSubj.Text := '';
     txtTitle.Text := '';
     txtBuil.Text :='';
     txtRoom.Text := '';
     CheckBox1.Checked := false;
     CheckBox2.Checked := false;
     CheckBox3.Checked := false;
     CheckBox4.Checked := false;
     CheckBox5.Checked := false;
     CheckBox7.Checked := false;
     CheckBox6.Checked := false;
end;

procedure TfrmCreateSched.getPrimaryKey;
var schedID : Integer;
begin
      schedID := 1;
        if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;
          DataModule1.query.SQL.Clear();
          DataModule1.query.SQL.Add('SELECT SCHEDID');
          DataModule1.query.SQL.Add('FROM CLASS_SCHED');
          DataModule1.query.Prepare;
          DataModule1.query.Open();

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

end;

procedure TfrmCreateSched.saveSched;
begin
    getPrimaryKey;
        if (CheckBox1.Checked = true) And (CheckBox4.Checked = true)then
            begin
                classSched := 'MH ';
            end
                else if (CheckBox2.Checked = true) And (CheckBox5.Checked = true) then
                begin
                      classSched := 'TF ';
                end
                    else if (CheckBox1.Checked = true) And (CheckBox3.Checked = true) And (CheckBox5.Checked = true) then
                    begin
                          classSched := 'MWF ';
                    end
                        else if (CheckBox6.Checked = true ) And (CheckBox7.Checked = true) then
                        begin
                            classSched := 'Sat And Sun ';
                        end
                          else if (CheckBox1.Checked = true) And (CheckBox3.Checked = true) then
                          begin
                              classSched := 'MW ';
                          end
                              else if (CheckBox1.Checked = true) And (CheckBox4.Checked = true) And (CheckBox5.Checked = true) then
                              begin
                                classSched := 'MTHF ';
                              end
                                else if (CheckBox3.Checked = true) then
                                  begin
                                    classSched := 'W';
                                  end

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

                                        DataModule1.query.SQL.Clear();
                                        DataModule1.query.SQL.Add('INSERT INTO CLASS_SCHED');
                                        DataModule1.query.SQL.Add('(SCHEDID, OCC_ID,AYEAR, SEMESTER, SUBJECT, DES_TITLE, SCHED_DAY, CLS_SCHEDTIME, CLS_ENDTIME, CLASS_BUIL, CLASS_ROOM, DATEADD)');
                                        DataModule1.query.SQL.Add('VALUES (:a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l)');
                                        DataModule1.query.Params.AsInteger[0] := schedidPK;
                                        DataModule1.query.Params.AsString[1] := Label3.Caption;
                                        DataModule1.query.Params.AsString[2] := txtAcad.Text;
                                        DataModule1.query.Params.AsString[3] := cmbSem.Text;
                                        DataModule1.query.Params.AsString[4] := trim(txtSubj.Text);
                                        DataModule1.query.Params.AsString[5] := trim(txtTitle.Text);
                                        DataModule1.query.Params.AsString[6] := classSched;
                                        Datamodule1.query.Params.AsString[7] := (TimeToStr(dtpSched.Time));
                                        DataModule1.query.Params.AsString[8] := (TimeToStr(dtpTime.Time));
                                        DataModule1.query.Params.AsString[9] := trim(txtBuil.Text);
                                        DataModule1.query.Params.AsString[10] := trim(txtRoom.Text);
                                        DataModule1.query.Params.AsString[11] := DateToStr(Now());
                                        DataModule1.query.Prepare;
                                        DataModule1.query.Execute;

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

                                        ShowMessage('Record has been successfully saved.');
                                        clear();
end;

procedure TfrmCreateSched.txtSubjKeyPress(Sender: TObject; var Key: Char);
begin
      if key = #13 then begin
        SelectNext(Sender as tWinControl, True, True);
          if Sender = txtSubj then
            key := #0;
end;
end;
procedure TfrmCreateSched.txtTitleKeyPress(Sender: TObject; var Key: Char);
begin
      if key = #13 then begin
        SelectNext(Sender as tWinControl, True, True);
          if Sender = txtSubj then
            key := #0;
end;
end;

procedure TfrmCreateSched.txtAcadKeyPress(Sender: TObject; var Key: Char);
begin
      if key = #13 then begin
        SelectNext(Sender as tWinControl, True, True);
          if Sender = txtSubj then
            key := #0;
end;
end;

procedure TfrmCreateSched.cmbSemKeyPress(Sender: TObject; var Key: Char);
begin
      if key = #13 then begin
        SelectNext(Sender as tWinControl, True, True);
          if Sender = txtSubj then
            key := #0;
end;
end;
procedure TfrmCreateSched.txtBuilKeyPress(Sender: TObject; var Key: Char);
begin
      if key = #13 then begin
        SelectNext(Sender as tWinControl, True, True);
          if Sender = txtSubj then
            key := #0;
end;
end;

procedure TfrmCreateSched.txtRoomKeyPress(Sender: TObject; var Key: Char);

    begin
      if key = #13 then begin
        SelectNext(Sender as tWinControl, True, True);
          if Sender = txtSubj then
            key := #0;
end;
end;

end.
