unit TaxPayerDetails;

interface

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

type
  TfrmTaxPayerDetails = class(TForm)
    gbTaxPayerDetails: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    txtName: TEdit;
    txtAge: TEdit;
    txtAddress: TMemo;
    txtRemarks: TMemo;
    txtPurpose: TEdit;
    btnOK: TBitBtn;
    btnEdit: TBitBtn;
    btnCancel: TBitBtn;
    lbpkHide: TLabel;
    lbPrimary: TLabel;
    txtGender: TEdit;
    txtCivilStat: TEdit;
    procedure Clear();
    procedure FormActivate(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure Edit();
    procedure btnEditClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure txtAgeKeyPress(Sender: TObject; var Key: Char);
    //procedure txtNameChange(Sender: TObject);
    procedure txtNameKeyPress(Sender: TObject; var Key: Char);
    procedure txtAddressKeyPress(Sender: TObject; var Key: Char);
    procedure txtRemarksKeyPress(Sender: TObject; var Key: Char);
    procedure txtPurposeKeyPress(Sender: TObject; var Key: Char);
    procedure cmbGenderKeyPress(Sender: TObject; var Key: Char);
    procedure cmbCivilStatKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
    primary :string;
    genderF:string;
    genderM:string;
    civilStat_S:string;
    civilStat_M:string;
    civilStat_Sep:string;
    civilStat_W:string;
  end;

var
  frmTaxPayerDetails: TfrmTaxPayerDetails;

implementation

uses History, DM, Services;

{$R *.dfm}


procedure TfrmTaxPayerDetails.Clear();
begin
         txtName.Clear;
         txtAge.Clear;
         txtGender.Clear;
         txtCivilStat.Clear;
         txtAddress.Clear;
         txtRemarks.Clear;
         txtPurpose.Clear;
end;

procedure TfrmTaxPayerDetails.FormActivate(Sender: TObject);
begin
      {frmHistory.View();
      lbpkHide.Caption:=frmHistory.lvSearch.ItemFocused.Caption;
      //lbPrimary.Caption:=frmHistory.primary;  }
end;

procedure TfrmTaxPayerDetails.btnOKClick(Sender: TObject);
begin
      Close;
      Clear;
end;
procedure TfrmTaxPayerDetails.btnEditClick(Sender: TObject);
begin
      gbTaxPayerDetails.Enabled:=True;
      btnEdit.Caption:='&Save';
      Edit();
end;

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

//methods...............................
procedure TfrmTaxPayerDetails.Edit();
begin
genderF:='Female';
genderM:='Male';
civilStat_S:='Single';
civilStat_M:='Married';
civilStat_Sep:='Separated';
civilStat_W:='Widow';

        if(txtAge.Text='') then begin
            MessageDlg('Field(s) required',mtError,[mbOK],0);
            txtAge.SetFocus;
        end
        else if((StrToInt(txtAge.Text) < 18)) then begin
            MessageDlg('Age must be 18 and above',mtError,[mbOK],0);
            txtAge.SetFocus;
        end
        else if(txtGender.Text ='') then begin
            MessageDlg('Field(s) required',mtError,[mbOK],0);
            txtGender.SetFocus;
        end
        else if((txtGender.Text) <> genderF) OR ((txtGender.Text) <> genderM)  then begin
            MessageDlg('Invalid input. Gender must be "Male" or "Female"',mtError,[mbOK],0);
            txtGender.SetFocus;
        end
                else if(txtCivilStat.Text ='') then begin
            MessageDlg('Field(s) required',mtError,[mbOK],0);
            txtCivilStat.SetFocus;
        end
        else if(UpperCase(txtCivilStat.Text) <> civilStat_S) OR (UpperCase(txtCivilStat.Text) <> civilStat_M)  OR (UpperCase(txtCivilStat.Text) <> civilStat_Sep)  OR (UpperCase(txtCivilStat.Text) <> civilStat_W) then begin
            MessageDlg('Invalid input please check the spelling. Civil must be "Single" or "Married" or "Separated" or "Widow"',mtError,[mbOK],0);
            txtCivilStat.SetFocus;
        end
        else if(txtAddress.Text='') then begin
             MessageDlg('Field(s) required',mtError,[mbOK],0);
             txtAddress.SetFocus;
        end
        else if (txtRemarks.Text='') then begin
              MessageDlg('Field(s) required',mtError,[mbOK],0);
              txtRemarks.SetFocus;
        end
        else if(txtPurpose.Text='')then begin
              MessageDlg('Field(s) required',mtError,[mbOK],0);
              txtPurpose.SetFocus;
        end
    else
   if MessageDlg('Do you want to edit?',mtConfirmation,[mbYes, mbNo], 0)= mrYes then begin

      primary:=lbpkHide.Caption;
        if DModule.TR.InTransaction then
            DModule.TR.Commit;
        DModule.QR.SQL.Clear();
        DModule.QR.SQL.Add('UPDATE ClearanceDetails SET Age=:A, Gender=:B,CivilStatus=:C,Address=:D,Remarks=:E,Purpose=:F WHERE ColID=:G');
           DModule.QR.Params.AsString[0] := trim(UpperCase(txtAge.Text));
           DModule.QR.Params.AsString[1] := trim(UpperCase(txtGender.Text));
           DModule.QR.Params.AsString[2] := trim(UpperCase(txtCivilStat.Text));
           DModule.QR.Params.AsString[3] := trim(UpperCase(txtAddress.Text));
           DModule.QR.Params.AsString[4] := trim(UpperCase(txtRemarks.Text));
           DModule.QR.Params.AsString[5] := trim(UpperCase(txtPurpose.Text));
           DModule.QR.Params.AsString[6] := primary;
        DModule.QR.Prepare;
        DModule.QR.Open();

        MessageDlg('Information has been successfully edited',mtInformation,[mbOK],0);

           if DModule.TR.InTransaction then
              DModule.TR.Commit;
        Close;
   end
   else begin
        MessageDlg('Nothing edited',mtInformation,[mbOK],0);
   end
end;

procedure TfrmTaxPayerDetails.FormShow(Sender: TObject);
begin
        frmHistory.View();
        lbpkHide.Caption:=frmHistory.lvSearch.ItemFocused.Caption;
      //lbPrimary.Caption:=frmHistory.primary;
end;

procedure TfrmTaxPayerDetails.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
        //Close;
         Clear;
end;

procedure TfrmTaxPayerDetails.txtNameKeyPress(Sender: TObject;
  var Key: Char);
begin
      if Key = #13 then begin
        SelectNext(Sender as tWinControl, True, True );
      if Sender = txtName then
        btnEdit.Click; // let the button click itself
        Key := #0;
      end;
end;

procedure TfrmTaxPayerDetails.txtAgeKeyPress(Sender: TObject;
  var Key: Char);
begin
       if Key = #13 then begin
        SelectNext(Sender as tWinControl, True, True );
       if Sender = txtAge then
        btnEdit.Click; // let the button click itself
        Key := #0;
      end;

      if key in['0'..'9']+['+']+['-'..'.']+[#8]then
        begin
          //dec point
          if key = #46 then
            if Pos('.',txtAge.text)<> 0 then begin
              key:=#0;
              windows.Beep(1000,200);
        end;
      //backspace
      if key =#8 then begin
          txtAge.Text:=Copy(txtAge.Text,1,length(txtAge.Text)-1);
          txtAge.selStart:=length(txtAge.Text);
          key:=#0;
          end;
      end                                                                                                                   
      else begin
          key:=#0;
          windows.Beep(1000,200);
      end;
end;

procedure TfrmTaxPayerDetails.txtAddressKeyPress(Sender: TObject;
  var Key: Char);
begin
      if Key = #13 then begin
        SelectNext(Sender as tWinControl, True, True );
      if Sender = txtAddress then
        btnEdit.Click; // let the button click itself
        Key := #0;
      end;
end;

procedure TfrmTaxPayerDetails.txtRemarksKeyPress(Sender: TObject;
  var Key: Char);
begin
      if Key = #13 then begin
        SelectNext(Sender as tWinControl, True, True );
      if Sender = txtRemarks then
        btnEdit.Click; // let the button click itself
        Key := #0;
      end;
end;

procedure TfrmTaxPayerDetails.txtPurposeKeyPress(Sender: TObject;
  var Key: Char);
begin
      if Key = #13 then begin
        SelectNext(Sender as tWinControl, True, True );
      if Sender = txtPurpose then
        btnEdit.Click; // let the button click itself
        Key := #0;
      end;
end;

procedure TfrmTaxPayerDetails.cmbGenderKeyPress(Sender: TObject;
  var Key: Char);
begin
      if Key = #13 then begin
        SelectNext(Sender as tWinControl, True, True );
      if Sender = txtGender then
        btnEdit.Click; // let the button click itself
        Key := #0;
      end;
end;

procedure TfrmTaxPayerDetails.cmbCivilStatKeyPress(Sender: TObject;
  var Key: Char);
begin
      if Key = #13 then begin
        SelectNext(Sender as tWinControl, True, True );
      if Sender = txtCivilStat then
        btnEdit.Click; // let the button click itself
        Key := #0;
      end;
end;

end.
