unit unitHouseparent;

interface

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

type
  TfrmHouseparent = class(TForm)
    PageControl2: TPageControl;
    TabSheet2: TTabSheet;
    GroupBox3: TGroupBox;
    txtSearch1: TEdit;
    btnSearch: TBitBtn;
    btnNew: TBitBtn;
    lvHouseparent: TListView;
    TabSheet4: TTabSheet;
    GroupBox2: TGroupBox;
    txtSearch: TEdit;
    btnGo: TBitBtn;
    lvSched: TListView;
    PageControl3: TPageControl;
    TabSheet3: TTabSheet;
    Label4: TLabel;
    Label3: TLabel;
    Label6: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label5: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    cmbBldg: TComboBox;
    txtLName: TEdit;
    txtFName: TEdit;
    cboMon: TComboBox;
    cboTue: TComboBox;
    cboWed: TComboBox;
    cboThu: TComboBox;
    cboFri: TComboBox;
    cboSat: TComboBox;
    cboSun: TComboBox;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    lvTimeSched: TListView;
    btnCreate: TBitBtn;
    StatusBar2: TStatusBar;
    btnSave: TBitBtn;
    btnCancel: TBitBtn;
    txtid: TEdit;
    Label11: TLabel;
    Label12: TLabel;
    txtacad: TEdit;
    cmbSem: TComboBox;
    cboSearch: TComboBox;
    ListView1: TListView;
    PopupMenu1: TPopupMenu;
    Viewprofile1: TMenuItem;
    Label13: TLabel;
    procedure btnCreateClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure lvHouseparentDblClick(Sender: TObject);
    procedure btnGoClick(Sender: TObject);
    procedure ListView1Click(Sender: TObject);
    procedure lvTimeSchedCustomDrawItem(Sender: TCustomListView;
      Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
    procedure lvSchedCustomDrawItem(Sender: TCustomListView;
      Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
    procedure btnNewClick(Sender: TObject);
    procedure btnSearchClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);

  private
    { Private declarations }
  public
        occID : Integer;
        mon : Integer;
        tue : Integer;
        wed : Integer;
        thu : Integer;
        fri : Integer;
        sat : Integer;
        sun : Integer;
        parID : Integer;
        procedure loadTimeSched;
        procedure loadHouseparent;
        procedure loadBuilding;
        procedure loadID;
        procedure loadMon;
        procedure loadTue;
        procedure loadWed;
        procedure loadThu;
        procedure loadFri;
        procedure loadSat;
        procedure loadSun;
        procedure saveSched;
        procedure getMonID;
        procedure getTueID;
        procedure getWedID;
        procedure getThuID;
        procedure getFriID;
        procedure getSatID;
        procedure getSunID;
        procedure getparID;
        procedure loadFullname;
        procedure loadHP;
        procedure schedule;
        procedure loadSearch;
        function hpStatus(status : Integer) : String;
  end;

var
  frmHouseparent: TfrmHouseparent;
  systemDate : TDateTime;
        DateTimeformat : string;
implementation

uses unitTimeSched, DM, unitLogin, jvuib, jvuiblib, unitNewHp;

{$R *.dfm}

procedure TfrmHouseparent.btnCreateClick(Sender: TObject);
begin
      frmTimeSched.ShowModal;
end;

procedure TfrmHouseparent.getparID;
var ID : Integer;
begin
      ID :=1;
        if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;
          DataModule1.query.SQL.Clear();
          DataModule1.query.SQL.Add('SELECT HP_ID');
          DataModule1.query.SQL.Add('FROM HP_SCHEDULE');
          DataModule1.query.Prepare;
          DataModule1.query.Open();

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

end;

procedure TfrmHouseparent.loadTimeSched;
var lv : TListItem;
begin
      if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

          DataModule1.query.SQL.Clear;
          DataModule1.query.SQL.Add('SELECT * FROM TIME_SCHEDULING');
          DataModule1.query.Prepare;
          DataModule1.query.Open();
          lvTimeSched.Items.Clear;
          lvTimeSched.Items.BeginUpdate;

         if DataModule1.query.Fields.IsNull[0] then Exit;
            while not DataModule1.query.Eof do begin

            lv := lvTimeSched.Items.Add;
            lv.Caption := DataModule1.query.Fields.AsString[0];
            lv.SubItems.Add(DataModule1.query.Fields.AsString[1]);
            lv.SubItems.Add(DataModule1.query.Fields.AsString[2]);
            lv.SubItems.Add(DataModule1.query.Fields.AsString[3]);
            lv.SubItems.Add(DataModule1.query.Fields.AsString[4]);
            lv.SubItems.Add(DataModule1.query.Fields.AsString[5]);
            DataModule1.query.Next;
            end;

              if DataModule1.tr.InTransaction then
                DataModule1.tr.Commit;
                lvTimeSched.Items.EndUpdate;
end;

procedure TfrmHouseparent.loadHouseparent;
var
  newState : TListItem;
  hpStat : Integer;
  status : String;
begin
       if DataModule1.tr.InTransaction then
           DataModule1.tr.Commit;

           DataModule1.query.SQL.Clear;
           DataModule1.query.SQL.Add('SELECT a.PARENTID, a.PAR_LNAME, a.PAR_FNAME, a.CONTACTNO, b.PERMANENTADD, a.STATUS, a.ACADYR, a.SEM, a.ADDEDBY, a.DATEADD,a.acode FROM HPARENT a, address b where b.acode = a.acode');
           DataModule1.query.Prepare;
           DataModule1.query.Open();
           lvHouseparent.Items.BeginUpdate;
           lvHouseparent.Items.Clear;

                if DataModule1.query.Fields.IsNull[0] then Exit;
                    while not DataModule1.query.Eof do begin

                    hpStat:=StrToInt(DataModule1.query.Fields.AsString[5]);
                    status:= hpStatus(hpStat);

                    newState := lvHouseparent.Items.Add;
                    newState.Caption := DataModule1.query.Fields.AsString[0];
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[1]);
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[2]);
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[3]);
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[4]);
                    newState.SubItems.Add(status);
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[6]);
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[7]);
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[8]);
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[9]);
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[10]);
                   
                    DataModule1.query.Next;
                  end;
                      if DataModule1.tr.InTransaction then
                        DataModule1.tr.Commit;
                          lvHouseparent.Items.EndUpdate;
 end;

procedure TfrmHouseparent.loadSearch;
var
  newState : TListItem;
  hpStat : Integer;
  status : String;
begin
       if DataModule1.tr.InTransaction then
           DataModule1.tr.Commit;

           DataModule1.query.SQL.Clear;
           DataModule1.query.SQL.Add('SELECT PARENTID, PAR_LNAME, PAR_FNAME, CONTACTNO, ADDRESS, STATUS, ACADYR, SEM, ADDEDBY, DATEADD, MODBY, MODDATE FROM HOUSEPARENT where PAR_LNAME LIKE :A OR PAR_FNAME LIKE :B');
           DataModule1.query.Params.AsString[0] := ('%' + UpperCase(txtSearch1.Text) + '%');
           DataModule1.query.Params.AsString[1] := ('%' + UpperCase(txtSearch1.Text) + '%');
           DataModule1.query.Prepare;
           DataModule1.query.Open();
           lvHouseparent.Items.BeginUpdate;
           lvHouseparent.Items.Clear;

                if DataModule1.query.Fields.IsNull[0] then Exit;
                    while not DataModule1.query.Eof do begin

                    hpStat:=StrToInt(DataModule1.query.Fields.AsString[5]);
                    status:= hpStatus(hpStat);

                    newState := lvHouseparent.Items.Add;
                    newState.Caption := DataModule1.query.Fields.AsString[0];
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[1]);
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[2]);
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[3]);
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[4]);
                    newState.SubItems.Add(status);
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[6]);
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[7]);
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[8]);
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[9]);
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[10]);
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[11]);
                    DataModule1.query.Next;
                  end;
                      if DataModule1.tr.InTransaction then
                        DataModule1.tr.Commit;
                          lvHouseparent.Items.EndUpdate;
 end;

function TfrmHouseparent.hpStatus(status : Integer) : String;
begin
      case status of
          1 : hpStatus := 'Active';
          0 : hpStatus := 'Inactive';
     end;
end;

procedure TfrmHouseparent.loadID;
begin
    if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

          DataModule1.query.SQL.Clear;
          DataModule1.query.SQL.Add('SELECT BUIL_ID FROM BUILDING WHERE DESCRIPTION = :a') ;
          DataModule1.query.Params.AsString[0] := trim(cmbBldg.Text);
          DataModule1.query.Prepare;
          DataModule1.query.Open();
          occid := DataModule1.query.Fields.AsInteger[0];

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

procedure TfrmHouseparent.loadBuilding;
begin
      If DataModule1.tr.InTransaction then
        DataModule1.tr.Commit;

         DataModule1.tr.StartTransaction;
         DataModule1.query.SQL.Clear;
         DataModule1.query.SQL.Add('Select DESCRIPTION');
         DataModule1.query.SQL.Add('From BUILDING');
         DataModule1.query.prepare;
         DataModule1.query.Open;
         cmbBldg.Items.BeginUpdate;
         cmbBldg.Clear;

         If DataModule1.query.Fields.IsNull[0] then Exit
            else begin
            while not DataModule1.query.Eof do begin
            cmbBldg.Items.Add(DataModule1.query.Fields.AsString[0]);
            DataModule1.query.Next;
         end;
        end;

            If DataModule1.tr.InTransaction then
              DataModule1.tr.Commit;
              cmbBldg.Items.EndUpdate
end;

procedure TfrmHouseparent.loadMon;
begin
      getMonID;
      If DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

         DataModule1.query.SQL.Clear;
         DataModule1.query.SQL.Add('Select TIMEDESC');
         DataModule1.query.SQL.Add('From TIME_SCHEDULING');
         DataModule1.query.prepare;
         DataModule1.query.Open;
         cboMon.Items.BeginUpdate;
         cboMon.Clear;

         If DataModule1.query.Fields.IsNull[0] then Exit
            else begin
            while not DataModule1.query.Eof do begin
            cboMon.Items.Add(DataModule1.query.Fields.AsString[0]);
            DataModule1.query.Next;
         end;
        end;

            If DataModule1.tr.InTransaction then
              DataModule1.tr.Commit;
              cboMon.Items.EndUpdate;
end;

procedure TfrmHouseparent.loadTue;
begin
      getTueID;
      If DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

         DataModule1.query.SQL.Clear;
         DataModule1.query.SQL.Add('Select TIMEDESC');
         DataModule1.query.SQL.Add('From TIME_SCHEDULING');
         DataModule1.query.prepare;
         DataModule1.query.Open;
         cboTue.Items.BeginUpdate;
         cboTue.Clear;

         If DataModule1.query.Fields.IsNull[0] then Exit
            else begin
            while not DataModule1.query.Eof do begin
            cboTue.Items.Add(DataModule1.query.Fields.AsString[0]);
            DataModule1.query.Next;
         end;
        end;

           If DataModule1.tr.InTransaction then
              DataModule1.tr.Commit;
              cboTue.Items.EndUpdate;
end;

procedure TfrmHouseparent.loadWed;
begin
      getWedID;
      If DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

         DataModule1.query.SQL.Clear;
         DataModule1.query.SQL.Add('Select TIMEDESC');
         DataModule1.query.SQL.Add('From TIME_SCHEDULING');
         DataModule1.query.prepare;
         DataModule1.query.Open;
         cboWed.Items.BeginUpdate;
         cboWed.Clear;

         If DataModule1.query.Fields.IsNull[0] then Exit
            else begin
            while not DataModule1.query.Eof do begin
            cboWed.Items.Add(DataModule1.query.Fields.AsString[0]);
            DataModule1.query.Next;
         end;
        end;

           If DataModule1.tr.InTransaction then
              DataModule1.tr.Commit;
              cboWed.Items.EndUpdate;
end;

procedure TfrmHouseparent.loadThu;
begin
      getThuID;
      If DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

         DataModule1.query.SQL.Clear;
         DataModule1.query.SQL.Add('Select TIMEDESC');
         DataModule1.query.SQL.Add('From TIME_SCHEDULING');
         DataModule1.query.prepare;
         DataModule1.query.Open;
         cboThu.Items.BeginUpdate;
         cboThu.Clear;

         If DataModule1.query.Fields.IsNull[0] then Exit
            else begin
            while not DataModule1.query.Eof do begin
            cboThu.Items.Add(DataModule1.query.Fields.AsString[0]);
            DataModule1.query.Next;
         end;
        end;

           If DataModule1.tr.InTransaction then
              DataModule1.tr.Commit;
              cboThu.Items.EndUpdate;
end;

procedure TfrmHouseparent.loadFri;
begin
     getFriID;
      If DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

         DataModule1.query.SQL.Clear;
         DataModule1.query.SQL.Add('Select TIMEDESC');
         DataModule1.query.SQL.Add('From TIME_SCHEDULING');
         DataModule1.query.prepare;
         DataModule1.query.Open;
         cboFri.Items.BeginUpdate;
         cboFri.Clear;

         If DataModule1.query.Fields.IsNull[0] then Exit
            else begin
            while not DataModule1.query.Eof do begin
            cboFri.Items.Add(DataModule1.query.Fields.AsString[0]);
            DataModule1.query.Next;
         end;
        end;

           If DataModule1.tr.InTransaction then
              DataModule1.tr.Commit;
              cboFri.Items.EndUpdate;
end;

procedure TfrmHouseparent.loadSat;
begin
      getSatID;
      If DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

         DataModule1.query.SQL.Clear;
         DataModule1.query.SQL.Add('Select TIMEDESC');
         DataModule1.query.SQL.Add('From TIME_SCHEDULING');
         DataModule1.query.prepare;
         DataModule1.query.Open;
         cboSat.Items.BeginUpdate;
         cboSat.Clear;

         If DataModule1.query.Fields.IsNull[0] then Exit
            else begin
            while not DataModule1.query.Eof do begin
            cboSat.Items.Add(DataModule1.query.Fields.AsString[0]);
            DataModule1.query.Next;
         end;
        end;

           If DataModule1.tr.InTransaction then
              DataModule1.tr.Commit;
              cboSat.Items.EndUpdate;
end;

procedure TfrmHouseparent.loadSun;
begin
      getSunID;
      If DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

         DataModule1.query.SQL.Clear;
         DataModule1.query.SQL.Add('Select TIMEDESC');
         DataModule1.query.SQL.Add('From TIME_SCHEDULING');
         DataModule1.query.prepare;
         DataModule1.query.Open;
         cboSun.Items.BeginUpdate;
         cboSun.Clear;

         If DataModule1.query.Fields.IsNull[0] then Exit
            else begin
            while not DataModule1.query.Eof do begin
            cboSun.Items.Add(DataModule1.query.Fields.AsString[0]);
            DataModule1.query.Next;
         end;
        end;

           If DataModule1.tr.InTransaction then
              DataModule1.tr.Commit;
              cboSun.Items.EndUpdate;
end;

procedure TfrmHouseparent.getMonID;
begin
    if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

          DataModule1.query.SQL.Clear;
          DataModule1.query.SQL.Add('SELECT TIME_ID FROM TIME_SCHEDULING WHERE timedesc = :a') ;
          DataModule1.query.Params.AsString[0] := trim(cboMon.Text);
          DataModule1.query.Prepare;
          DataModule1.query.Open();
          mon := DataModule1.query.Fields.AsInteger[0];

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

procedure TfrmHouseparent.getTueID;
begin
    if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

          DataModule1.query.SQL.Clear;
          DataModule1.query.SQL.Add('SELECT TIME_ID FROM TIME_SCHEDULING WHERE timedesc = :a') ;
          DataModule1.query.Params.AsString[0] := trim(cboTue.Text);
          DataModule1.query.Prepare;
          DataModule1.query.Open();
          tue := DataModule1.query.Fields.AsInteger[0];

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

procedure TfrmHouseparent.getWedID;
begin
    if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

          DataModule1.query.SQL.Clear;
          DataModule1.query.SQL.Add('SELECT TIME_ID FROM TIME_SCHEDULING WHERE timedesc = :a') ;
          DataModule1.query.Params.AsString[0] := trim(cboWed.Text);
          DataModule1.query.Prepare;
          DataModule1.query.Open();
          wed := DataModule1.query.Fields.AsInteger[0];

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

procedure TfrmHouseparent.getThuID;
begin
    if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

          DataModule1.query.SQL.Clear;
          DataModule1.query.SQL.Add('SELECT TIME_ID FROM TIME_SCHEDULING WHERE timedesc = :a') ;
          DataModule1.query.Params.AsString[0] := trim(cboThu.Text);
          DataModule1.query.Prepare;
          DataModule1.query.Open();
          thu := DataModule1.query.Fields.AsInteger[0];

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

procedure TfrmHouseparent.getFriID;
begin
    if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

          DataModule1.query.SQL.Clear;
          DataModule1.query.SQL.Add('SELECT TIME_ID FROM TIME_SCHEDULING WHERE timedesc= :a') ;
          DataModule1.query.Params.AsString[0] := trim(cboFri.Text);
          DataModule1.query.Prepare;
          DataModule1.query.Open();
          fri := DataModule1.query.Fields.AsInteger[0];

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

procedure TfrmHouseparent.getSatID;
begin
    if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

          DataModule1.query.SQL.Clear;
          DataModule1.query.SQL.Add('SELECT TIME_ID FROM TIME_SCHEDULING WHERE timedesc = :a') ;
          DataModule1.query.Params.AsString[0] := trim(cboSat.Text);
          DataModule1.query.Prepare;
          DataModule1.query.Open();
          sat := DataModule1.query.Fields.AsInteger[0];

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

procedure TfrmHouseparent.getSunID;
begin
    if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

          DataModule1.query.SQL.Clear;
          DataModule1.query.SQL.Add('SELECT TIME_ID FROM TIME_SCHEDULING WHERE timedesc = :a') ;
          DataModule1.query.Params.AsString[0] := trim(cboSun.Text);
          DataModule1.query.Prepare;
          DataModule1.query.Open();
          sun := DataModule1.query.Fields.AsInteger[0];

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

procedure TfrmHouseparent.saveSched;
begin
      getparID;
      getMonID;
      loadID;
      if DataModule1.tr.InTransaction then
        DataModule1.tr.Commit;

        DataModule1.query.SQL.Clear;
        DataModule1.query.SQL.Add('INSERT INTO HP_SCHEDULE (HP_ID, PARENTID, SCHEDDAY, TIME_ID, BUIL_ID) VALUES (:a, :b, :c, :d, :e)');
        DataModule1.query.params.AsInteger[0] := parID;
        DataModule1.query.Params.AsInteger[1] := StrToInt(txtid.Text);
        DataModule1.query.Params.AsString[2] := Label1.Caption;
        DataModule1.query.Params.AsInteger[3] := mon;
        DataModule1.query.Params.AsInteger[4] := occID;
        DataModule1.query.Prepare;
        DataModule1.query.Open();

         getparID;
         getTueID;
         loadID;
        DataModule1.query.SQL.Clear;
        DataModule1.query.SQL.Add('INSERT INTO HP_SCHEDULE (HP_ID, PARENTID, SCHEDDAY, TIME_ID, BUIL_ID) VALUES (:a, :b, :c, :d, :e)');
        DataModule1.query.params.AsInteger[0] := parID;
        DataModule1.query.Params.AsInteger[1] := StrToInt(txtid.Text);
        DataModule1.query.Params.AsString[2] := Label8.Caption;
        DataModule1.query.Params.AsInteger[3] := tue;
        DataModule1.query.Params.AsInteger[4] := occID;
        DataModule1.query.Prepare;
        DataModule1.query.Open();

         getparID;
         getWedID;
         loadID;
        DataModule1.query.SQL.Clear;
        DataModule1.query.SQL.Add('INSERT INTO HP_SCHEDULE (HP_ID, PARENTID, SCHEDDAY, TIME_ID, BUIL_ID) VALUES (:a, :b, :c, :d, :e)');
        DataModule1.query.params.AsInteger[0] := parID;
        DataModule1.query.Params.AsInteger[1] := StrToInt(txtid.Text);
        DataModule1.query.Params.AsString[2] := Label7.Caption;
        DataModule1.query.Params.AsInteger[3] := wed;
        DataModule1.query.Params.AsInteger[4] := occID;
        DataModule1.query.Prepare;
        DataModule1.query.Open();

         getparID;
         getThuID;
         loadID;
        DataModule1.query.SQL.Clear;
        DataModule1.query.SQL.Add('INSERT INTO HP_SCHEDULE (HP_ID, PARENTID, SCHEDDAY, TIME_ID, BUIL_ID) VALUES (:a, :b, :c, :d, :e)');
        DataModule1.query.params.AsInteger[0] := parID;
        DataModule1.query.Params.AsInteger[1] := StrToInt(txtid.Text);
        DataModule1.query.Params.AsString[2] := Label5.Caption;
        DataModule1.query.Params.AsInteger[3] := thu;
        DataModule1.query.Params.AsInteger[4] := occID;
        DataModule1.query.Prepare;
        DataModule1.query.Open();

         getparID;
         getFriID;
         loadID;
        DataModule1.query.SQL.Clear;
        DataModule1.query.SQL.Add('INSERT INTO HP_SCHEDULE (HP_ID, PARENTID, SCHEDDAY, TIME_ID, BUIL_ID) VALUES (:a, :b, :c, :d, :e)');
        DataModule1.query.params.AsInteger[0] := parID;
        DataModule1.query.Params.AsInteger[1] := StrToInt(txtid.Text);
        DataModule1.query.Params.AsString[2] := Label2.Caption;
        DataModule1.query.Params.AsInteger[3] := fri;
        DataModule1.query.Params.AsInteger[4] := occID;
        DataModule1.query.Prepare;
        DataModule1.query.Open();

         getparID;
         getSatID;
         loadID;
        DataModule1.query.SQL.Clear;
       DataModule1.query.SQL.Add('INSERT INTO HP_SCHEDULE (HP_ID, PARENTID, SCHEDDAY, TIME_ID, BUIL_ID) VALUES (:a, :b, :c, :d, :e)');
        DataModule1.query.params.AsInteger[0] := parID;
        DataModule1.query.Params.AsInteger[1] := StrToInt(txtid.Text);
        DataModule1.query.Params.AsString[2] := Label9.Caption;
        DataModule1.query.Params.AsInteger[3] := sat;
        DataModule1.query.Params.AsInteger[4] := occID;
        DataModule1.query.Prepare;
        DataModule1.query.Open();

         getparID;
         getSunID;
         loadID;
        DataModule1.query.SQL.Clear;
        DataModule1.query.SQL.Add('INSERT INTO HP_SCHEDULE (HP_ID, PARENTID, SCHEDDAY, TIME_ID, BUIL_ID) VALUES (:a, :b, :c, :d, :e)');
        DataModule1.query.params.AsInteger[0] := parID;
        DataModule1.query.Params.AsInteger[1] := StrToInt(txtid.Text);
        DataModule1.query.Params.AsString[2] := Label10.Caption;
        DataModule1.query.Params.AsInteger[3] := sun;
        DataModule1.query.Params.AsInteger[4] := occID;
        DataModule1.query.Prepare;
        DataModule1.query.Open();



        if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;
            ShowMessage('Successfully saved.');

        DataModule1.query.SQL.Clear;
        DataModule1.query.SQL.Add('UPDATE HPARENT SET ACADYR = :A, SEM = :B WHERE PARENTID = :E');
        DataModule1.query.Params.AsString[0] := trim(txtacad.Text);
        DataModule1.query.Params.AsString[1] := cmbSem.Text;
       // DataModule1.query.Params.AsString[2] := frmLogin.fullname;
        //DataModule1.query.Params.AsString[3] := DateToStr(Now());
        //DataModule1.query.Params.AsInteger[4] := StrToInt(txtid.Text);
        DataModule1.query.Prepare;
        DataModule1.query.ExecSQL;

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

procedure TfrmHouseparent.loadFullname;
var name : TListItem;
begin
      if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

          DataModule1.query.SQL.Clear;
          DataModule1.query.SQL.Add('SELECT fullname from sp_houseparent');
          DataModule1.query.Prepare;
          DataModule1.query.Open();
          lvSched.Items.BeginUpdate;
          lvSched.Items.Clear;


              if DataModule1.query.Fields.IsNull[0] then Exit;
                while not DataModule1.query.Eof do begin

                    name := lvSched.Items.Add;
                    name.Caption := DataModule1.query.Fields.AsString[0];

                   DataModule1.query.Next;
                   end;
                    if DataModule1.tr.InTransaction then
                      DataModule1.tr.Commit;
                      lvSched.Items.EndUpdate;

end;

procedure TfrmHouseparent.FormShow(Sender: TObject);
begin
      DateTimeToString(DateTimeformat, 'dddddd', now());
      Label13.Caption := DateTimeformat;
      StatusBar2.Panels.Items[2].Text := 'Date --' + label13.Caption;

      loadTimeSched;
      loadHouseparent;
      loadBuilding;
      loadMon;
      loadTue;
      loadWed;
      loadThu;
      loadFri;
      loadSat;
      loadSun;
end;

procedure TfrmHouseparent.lvHouseparentDblClick(Sender: TObject);
begin
       txtid.Text := lvHouseparent.ItemFocused.Caption;
       txtLName.Text := lvHouseparent.ItemFocused.SubItems[0];
       txtFName.Text := lvHouseparent.ItemFocused.SubItems[1];
end;

procedure TfrmHouseparent.loadHP;
var
  newState : TListItem;
begin
       if DataModule1.tr.InTransaction then
           DataModule1.tr.Commit;

           DataModule1.query.SQL.Clear;
           DataModule1.query.SQL.Add('SELECT DISTINCT PARENTID, FULLNAME, ACADYR, SEM FROM SCHEDULE WHERE SEM = :A and acadyr = :b');
           DataModule1.query.Params.AsString[0] := 'Second Semester';
           DataModule1.query.Params.AsString[1] := trim(txtSearch.Text);
           DataModule1.query.Prepare;
           DataModule1.query.Open();
           ListView1.Items.BeginUpdate;
           ListView1.Items.Clear;

                if DataModule1.query.Fields.IsNull[0] then Exit;
                    while not DataModule1.query.Eof do begin

                    newState := ListView1.Items.Add;
                    newState.Caption := DataModule1.query.Fields.AsString[0];
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[1]);
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[2]);
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[3]);
                    DataModule1.query.Next;
                  end;
                      if DataModule1.tr.InTransaction then
                        DataModule1.tr.Commit;
                          ListView1.Items.EndUpdate;
 end;

procedure TfrmHouseparent.btnGoClick(Sender: TObject);
begin
      if (cboSearch.Text = 'Second Semester') then
      begin
          loadHP;
      end
        else if (cboSearch.Text = 'First Semester') then
        begin
         //   loadFirstSem;
        end;
end;


procedure TfrmHouseparent.schedule;
var newState : TListItem;
begin
      if DataModule1.tr.InTransaction then
      DataModule1.tr.Commit;

      DataModule1.query.SQL.Clear;
      DataModule1.query.SQL.Add('SELECT * FROM SCHEDULE WHERE PARENTID = :A');
      DataModule1.query.Params.AsString[0] := ListView1.Selected.Caption;
      DataModule1.query.Prepare;
      DataModule1.query.Open();
      lvSched.Items.Clear;
      lvSched.Items.BeginUpdate;
      


                if DataModule1.query.Fields.IsNull[0] then Exit;
                    while not DataModule1.query.Eof do begin

                    newState := lvSched.Items.Add;
                    newState.Caption := DataModule1.query.Fields.AsString[4];
                    newState.SubItems.Add(DataModule1.query.Fields.AsString[3]);
                     newState.SubItems.Add(DataModule1.query.Fields.AsString[6]);
                    DataModule1.query.Next;
                  end;
                      if DataModule1.tr.InTransaction then
                        DataModule1.tr.Commit;
                          lvSched.Items.EndUpdate;
      
end;

procedure TfrmHouseparent.ListView1Click(Sender: TObject);
begin
      schedule;
end;

procedure TfrmHouseparent.lvTimeSchedCustomDrawItem(
  Sender: TCustomListView; Item: TListItem; State: TCustomDrawState;
  var DefaultDraw: Boolean);
begin
       if (Item.SubItems[1] = 'X') then
        lvTimeSched.Canvas.Font.Color := clGray;
end;

procedure TfrmHouseparent.lvSchedCustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
     if (Item.SubItems[0] = 'X') then
        lvSched.Canvas.Font.Color := clGray;
end;

procedure TfrmHouseparent.btnNewClick(Sender: TObject);
begin
      frmNewHP.ShowModal;
end;

procedure TfrmHouseparent.btnSearchClick(Sender: TObject);
begin
      loadSearch;
end;

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

end.
