unit unitRegistration;

interface

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

type
  TRegistration = class(TForm)
    Label1: TLabel;
    Label17: TLabel;
    StatusBar2: TStatusBar;
    PageControl2: TPageControl;
    TabSheet3: TTabSheet;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label10: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    txtFName: TEdit;
    txtLName: TEdit;
    txtMI: TEdit;
    cmbGender: TComboBox;
    dtpDate: TDateTimePicker;
    txtYr: TEdit;
    txtScholarship: TEdit;
    txtID: TEdit;
    txtStat: TEdit;
    txtAcad: TEdit;
    cmbSem: TComboBox;
    TabSheet4: TTabSheet;
    btnSave: TBitBtn;
    btnCancel: TBitBtn;
    Memo1: TMemo;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label18: TLabel;
    txtName: TEdit;
    txtMobileNo: TEdit;
    txtRel: TEdit;
    Memo2: TMemo;
    Label9: TLabel;
    txtParent: TEdit;
    procedure btnSaveClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
      id : String;
     systemDate : TDateTime;
     DateTimeformat : string;
     code : Integer;
     conid  : Integer;
     appid : Integer;
     procedure getParID;
     procedure Aid;
     procedure getConID;
     procedure saveApplicants;
     procedure clear;
  end;

var
  Registration: TRegistration;

implementation

uses DM, unitLogin, unitApplicants;

{$R *.dfm}

procedure TRegistration.btnSaveClick(Sender: TObject);
begin
      saveApplicants;
end;

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

procedure TRegistration.clear;
begin
     txtID.Text := '';
     txtLName.Text := '';
     txtFName.Text := '';
     txtMI.Text := '';
     cmbGender.Text := '';
     txtYr.Text := '';
     Memo1.Text := '';
     txtScholarship.Text := '';
     txtparent.Text := '';
     txtName.Text := '';
    Memo2.Text := '';
     txtMobileNo.Text :='';
     txtRel.Text := '';

end;

procedure TRegistration.Aid;
var acode : Integer;
begin
      acode :=1;
        if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;
          DataModule1.query.SQL.Clear();
          DataModule1.query.SQL.Add('SELECT ACODE');
          DataModule1.query.SQL.Add('FROM ADDRESS');
          DataModule1.query.Prepare;
          DataModule1.query.Open();

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

end;

procedure TRegistration.getParID;
var applicantsID : Integer;
begin
      applicantsID :=1;
        if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;
          DataModule1.query.SQL.Clear();
          DataModule1.query.SQL.Add('SELECT APPID');
          DataModule1.query.SQL.Add('FROM APPLICANTS');
          DataModule1.query.Prepare;
          DataModule1.query.Open();

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

end;

procedure TRegistration.getConID;
var ID : Integer;
begin
      ID :=1;
        if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;
          DataModule1.query.SQL.Clear();
          DataModule1.query.SQL.Add('SELECT conid');
          DataModule1.query.SQL.Add('FROM contact_info');
          DataModule1.query.Prepare;
          DataModule1.query.Open();

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

end;
procedure TRegistration.saveApplicants;
begin
      getParID;
          if (txtID.Text = '') then
          begin
              MessageDlg('Please fill in the required fields',mtError,[mbOK],0);
              txtID.SetFocus;
          end
          else if (txtLName.Text = '') then
          begin
              MessageDlg('Please fill in the required fields',mtError,[mbOK],0);
              txtLName.SetFocus;
            end
            else if (txtFName.Text = '') then
            begin
                MessageDlg('Please fill in the required fields',mtError,[mbOK],0);
                txtFName.SetFocus;
            end
            else if (txtMI.Text = '') then
            begin
                MessageDlg('Please fill in the required fields',mtError,[mbOK],0);
                txtMI.SetFocus;
            end
            else if (cmbGender.Text = '') then
            begin
                MessageDlg('Please fill in the required fields',mtError,[mbOK],0);
                cmbGender.SetFocus;
            end
            else  if (txtYr.Text = '') then
            begin
                MessageDlg('Please fill in the required fields',mtError,[mbOK],0);
                txtYr.SetFocus;
            end
            else if (Memo2.Text = '') then
            begin
                MessageDlg('Please fill in the required fields',mtError,[mbOK],0);
                Memo2.SetFocus;
            end
            else if (txtScholarship.Text = '') then
            begin
                MessageDlg('Please fill in the required fields',mtError,[mbOK],0);
                txtScholarship.SetFocus;
            end
            else if (txtparent.Text = '') then
            begin
                MessageDlg('Please fill in the required fields',mtError,[mbOK],0);
                txtparent.SetFocus;
            end
            else if (Memo1.Text = '') then
            begin
                MessageDlg('Please fill in the required fields',mtError,[mbOK],0);
                Memo1.SetFocus;
            end
            else if (txtName.Text = '') then
            begin
                MessageDlg('Please fill in the required fields',mtError,[mbOK],0);
                txtName.SetFocus;
            end
            else if (Memo2.Text = '') then
            begin
                 MessageDlg('Please fill in the required fields',mtError,[mbOK],0);
                 Memo2.SetFocus;
            end
            else if (txtMobileNo.Text = '') then
            begin
                 MessageDlg('Please fill in the required fields',mtError,[mbOK],0);
                 txtMobileNo.SetFocus;
            end
            else if (txtRel.Text = '') then
            begin
                 MessageDlg('Please fill in the required fields',mtError,[mbOK],0);
                 txtRel.SetFocus;
            end
            else begin
              if DataModule1.tr.InTransaction then
              DataModule1.tr.Commit;

              DataModule1.query.SQL.Clear();
            DataModule1.query.SQL.Add('INSERT INTO APPLICANTS');
            DataModule1.query.SQL.Add('(APPID,STUDENT_ID,APP_FNAME,APP_LNAME,MIDDLE_NAME,GENDER,APP_BDAY,YEAR_COURSE,APP_SCHOLARSHIP,APP_STATUS,PARENT, ACADYR,SEM,DATEAPPLIED,ADDEDBY)');
            DataModule1.query.SQl.Add('VALUES (:a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l, :m, :n, :o)');
            DataModule1.query.Params.AsInteger[0] := appid;                                         
            DataModule1.query.Params.AsString[1] := trim(txtID.Text);
            DataModule1.query.Params.AsString[2] := trim(txtFName.Text);
            DataModule1.query.Params.AsString[3] := trim(txtLName.Text);
            DataModule1.query.Params.AsString[4] := trim(txtMI.Text);
            Datamodule1.query.Params.AsString[5] := cmbGender.Text;
            DataModule1.query.Params.AsString[6] := DateToStr(dtpDate.Date);
            DataModule1.query.Params.AsString[7] := trim(txtYr.Text);
            DataModule1.query.Params.AsString[8] := trim(txtScholarship.Text);
            DataModule1.query.Params.AsString[9]:= trim(txtStat.Text);
            DataModule1.query.Params.AsString[10] := trim(txtParent.Text);
            DataModule1.query.Params.AsString[11] := trim(txtAcad.Text);
            DataModule1.query.Params.AsString[12] := cmbSem.Text;
            DataModule1.query.Params.AsString[13] := DateToStr(Now());
            DataModule1.query.Params.AsString[14] := frmLogin.fullname;
            DataModule1.query.Prepare;
            DataModule1.query.Open();

            Aid;
            DataModule1.query.SQL.Clear();
            DataModule1.query.SQL.Add('INSERT INTO ADDRESS');
            DataModule1.query.SQL.Add('(acode, PERMANENTADD, CURADD)');
            DataModule1.query.SQl.Add('VALUES (:a, :b, :c)');
            DataModule1.query.Params.AsInteger[0] := code;
            DataModule1.query.Params.AsString[1] := Memo1.Text;
            DataModule1.query.Params.AsString[2] := 'MSU-NAAWAN, DORMITORY NAAWAN MIS.OR.';
            DataModule1.query.Prepare;
            DataModule1.query.Open();

            DataModule1.query.SQL.Clear;
        DataModule1.query.SQL.Add('UPDATE applicants set acode = :a where appid = :c');
        DataModule1.query.Params.AsInteger[0] := code;
        DataModule1.query.Params.AsInteger[1] := appid;
        DataModule1.query.Prepare;
        DataModule1.query.ExecSQL;

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

            getConID;
            DataModule1.query.SQL.Clear();
            DataModule1.query.SQL.Add('INSERT INTO CONTACT_INFO');
            DataModule1.query.SQL.Add('(conid,NAME, NUMBER, RELATIONSHIP)');
            DataModule1.query.SQl.Add('VALUES (:a, :b, :c, :d)');
            DataModule1.query.Params.AsInteger[0] := conID;
            DataModule1.query.Params.AsString[1] := txtName.Text;
            DataModule1.query.Params.AsString[2] := txtMobileNo.Text;
            DataModule1.query.Params.AsString[3] := txtRel.Text;
            DataModule1.query.Prepare;
            DataModule1.query.Open();


             DataModule1.query.SQL.Clear;
        DataModule1.query.SQL.Add('UPDATE applicants set conid = :a where appid = :c');
        DataModule1.query.Params.AsInteger[0] := conID;
        DataModule1.query.Params.AsInteger[1] := appid;
        DataModule1.query.Prepare;
        DataModule1.query.ExecSQL;


         Aid;
            DataModule1.query.SQL.Clear();
            DataModule1.query.SQL.Add('INSERT INTO ADDRESS');
            DataModule1.query.SQL.Add('(acode, PERMANENTADD, CURADD)');
            DataModule1.query.SQl.Add('VALUES (:a, :b, :c)');
            DataModule1.query.Params.AsInteger[0] := code;
            DataModule1.query.Params.AsString[1] := Memo2.Text;
            DataModule1.query.Params.AsString[2] := Memo2.Text;;
            DataModule1.query.Prepare;
            DataModule1.query.Open();

            DataModule1.query.SQL.Clear;
        DataModule1.query.SQL.Add('UPDATE contact_info set acode = :a where conid = :c');
        DataModule1.query.Params.AsInteger[0] := code;
        DataModule1.query.Params.AsInteger[1] := conID;
        DataModule1.query.Prepare;
        DataModule1.query.ExecSQL;

                if DataModule1.tr.InTransaction then
                  DataModule1.tr.Commit;
                      ShowMessage('Record has been successfully saved.');
                      //frmApplicants.gApplicants;
                      //frmApplicants.bApplicants;
                        clear();

                        {getParID;
                        Aid;
                        getConID;

                                 }
                       

end;
end;

procedure TRegistration.FormShow(Sender: TObject);
begin
       DateTimeToString(DateTimeformat, 'dddddd', now());
      Label17.Caption := DateTimeformat;
      StatusBar2.Panels.Items[1].Text := 'Date -- ' + label17.Caption;
end;

end.
