unit unitTimeSched;

interface

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

type
  TfrmTimeSched = class(TForm)
    StatusBar1: TStatusBar;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    GroupBox1: TGroupBox;
    Label9: TLabel;
    Label7: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    dtpSched: TDateTimePicker;
    dtpTime: TDateTimePicker;
    txtLetter: TEdit;
    Panel1: TPanel;
    btnSave: TBitBtn;
    btnCancel: TBitBtn;
    procedure btnSaveClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
  private
    { Private declarations }
  public
      primKey : Integer;
      procedure saveSched;
      procedure clear;
      procedure loadPrimKey;
  end;

var
  frmTimeSched: TfrmTimeSched;
  systemDate : TDateTime;
  DateTimeformat : string;
implementation

uses DM, unitLogin;

{$R *.dfm}

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

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

procedure TfrmTimeSched.loadPrimKey;
var pk : Integer;
begin
      pk :=1;
        if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;
          DataModule1.query.SQL.Clear();
          DataModule1.query.SQL.Add('SELECT TIME_ID');
          DataModule1.query.SQL.Add('FROM TIME_SCHED');
          DataModule1.query.Prepare;
          DataModule1.query.Open();

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

end;

procedure TfrmTimeSched.saveSched;
begin
      loadPrimKey;
    if DataModule1.tr.InTransaction then
      DataModule1.tr.Commit;

      DataModule1.query.SQL.Clear;
      DataModule1.query.SQL.Add('INSERT INTO TIME_SCHED (TIME_ID, FR_TIME, END_TIME, LETTER, ADDEDBY, DATEADDED) VALUES (:a, :b, :c, :d, :e, :f)');
      DataModule1.query.Params.AsInteger[0] := primKey;
      DataModule1.query.Params.AsString[1] := TimeToStr(dtpSched.Time);
      DataModule1.query.Params.AsString[2] := TimeToStr(dtpTime.Time);
      DataModule1.query.Params.AsString[3] := trim(txtLetter.Text);
      DataModule1.query.Params.AsString[4] := frmLogin.fullname;
      DataModule1.query.Params.AsString[5] := DateToStr(Now());
      DataModule1.query.Prepare;
      DataModule1.query.Open();

      if DataModule1.tr.InTransaction then
         DataModule1.tr.Commit;
         ShowMessage('Time schedule has been successfully saved !');
        // frmParentInfo.loadTimeSched;
end;

procedure TfrmTimeSched.clear;
begin
      txtLetter.Text :='';
end;

procedure TfrmTimeSched.btnCancelClick(Sender: TObject);
begin
      Close;
end;

end.
