unit unitNewHp;

interface

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

type
  TfrmNewHP = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    txtlname: TEdit;
    txtfname: TEdit;
    txtcp: TEdit;
    StatusBar1: TStatusBar;
    btnCancel: TBitBtn;
    btnNew: TBitBtn;
    txtstatus: TEdit;
    Label5: TLabel;
    Memo1: TMemo;
    procedure btnNewClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
         code : Integer;
         appid : Integer;
         systemDate : TDateTime;
        DateTimeformat : string;
        procedure save;
        procedure Aid;
        procedure getParID;
  end;

var
  frmNewHP: TfrmNewHP;

implementation

uses DM, unitLogin;

{$R *.dfm}

procedure TfrmNewHP.getParID;
var applicantsID : Integer;
begin
      applicantsID :=1;
        if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;
          DataModule1.query.SQL.Clear();
          DataModule1.query.SQL.Add('SELECT PARENTID');
          DataModule1.query.SQL.Add('FROM HPARENT');
          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(PARENTID) FROM HPARENT');
                        DataModule1.query.Prepare;
                        DataModule1.query.Open();
                        appid := DataModule1.query.Fields.AsInteger[0] + 1;
                end;
                      if  DataModule1.tr.InTransaction then
                          DataModule1.tr.Commit;

end;

procedure TfrmNewHP.btnNewClick(Sender: TObject);
begin
      save;
end;
procedure TfrmNewHP.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 TfrmNewHP.save;
begin
      getParID;
      if DataModule1.tr.InTransaction then
        DataModule1.tr.Commit;

        DataModule1.query.SQL.Clear;
        DataModule1.query.SQL.Add('INSERT INTO HPARENT (PARENTID, PAR_LNAME, PAR_FNAME, CONTACTNO, STATUS,ADDEDBY, DATEADD) VALUES (:A, :B, :C, :D, :E, :F, :g)');
        DataModule1.query.Params.AsInteger[0] := appid;
        DataModule1.query.Params.AsString[1] := UpperCase(trim(txtlname.Text));
        DataModule1.query.params.AsString[2] := UpperCase(trim(txtfname.Text));
        DataModule1.query.Params.AsString[3] := UpperCase(trim(txtcp.Text));
        DataModule1.query.Params.AsString[4] := trim(txtstatus.Text);
        DataModule1.query.Params.AsString[5] := frmLogin.fullname;
        DataModule1.query.Params.AsString[6] := DateToStr(Now());
        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] := Memo1.Text;;
            DataModule1.query.Prepare;
            DataModule1.query.Open();

             DataModule1.query.SQL.Clear;
        DataModule1.query.SQL.Add('UPDATE HPARENT set acode = :a where PARENTID = :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;
            ShowMessage('Data successfully saved.');
              Close;

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

end.
