unit CollectionDetails;

interface

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

type
  TfrmTPDetails = class(TForm)
    GroupBox1: TGroupBox;
    txtAge: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    cmbGender: TComboBox;
    cmbCivilStatus: TComboBox;
    mmoAddress: TMemo;
    mmoRemarks: TMemo;
    txtPurpose: TEdit;
    Label7: TLabel;
    pkHide: TLabel;
    txtName: TEdit;
    Label8: TLabel;
    btnSave: TBitBtn;
    btnCancel: TBitBtn;
    CTCNO: TLabel;
    ISSUEDDATE: TLabel;
    ISSUEDAT: TLabel;
    lb_tpID: TLabel;
    lb_ctcnum: TLabel;


    procedure Save();
    procedure Report();
    procedure Clear();
    procedure AutoPrime();
    procedure Delete();
    procedure txtNameKeyPress(Sender: TObject; var Key: Char);
    procedure A(Sender: TObject; var Key: Char);
    procedure cmbGenderKeyPress(Sender: TObject; var Key: Char);
    procedure cmbCivilStatusKeyPress(Sender: TObject; var Key: Char);
    procedure mmoAddressKeyPress(Sender: TObject; var Key: Char);
    procedure mmoRemarksKeyPress(Sender: TObject; var Key: Char);
    procedure txtPurposeKeyPress(Sender: TObject; var Key: Char);
    procedure btnSaveClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure MaxID();
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
    sample:integer;
  end;

var
  frmTPDetails: TfrmTPDetails;

implementation

uses DM, Services;

{$R *.dfm}

procedure TfrmTPDetails.btnSaveClick(Sender: TObject);
begin
        Save();
end;

procedure TfrmTPDetails.btnCancelClick(Sender: TObject);
begin
        Delete();
end;


//methods.....................................
procedure TfrmTPDetails.Save();
begin
  AutoPrime();

if(txtAge.Text='')or(cmbGender.Text ='')or(cmbCivilStatus.Text ='')or (mmoAddress.Text ='')or(mmoRemarks.Text='')or(txtPurpose.Text='') then
     begin
        MessageDlg('Field(s) required',mtInformation,[mbOK],0);

        if(txtAge.Text='') then begin
            txtAge.SetFocus;
        end
        else if(StrToInt(txtAge.Text) < 18) then begin
            MessageDlg('Age must be 18 and above.',mtInformation,[mbOK],0);
            txtAge.SetFocus;
        end
        else if(cmbGender.Text='') then begin
            cmbGender.SetFocus;
        end
        else if(cmbCivilStatus.Text='') then begin
            cmbCivilStatus.SetFocus;
        end
        else if(mmoAddress.Text='') then begin
             mmoAddress.SetFocus;
        end
        else if (mmoRemarks.Text='') then begin
              mmoRemarks.SetFocus;
        end
        else if(txtPurpose.Text='')then begin
              txtPurpose.SetFocus;
        end
    end
    else
   if MessageDlg('Do you want to save?',mtConfirmation,[mbYes, mbNo], 0)= mrYes then begin

        DModule.QR.SQL.Clear();
        DModule.QR.SQL.Add('INSERT INTO ClearanceDetails');
        DModule.QR.SQL.Add('(ClearanceID,Remarks,Purpose,CTCNUM,TP_ID,ColID )');
        DModule.QR.SQL.Add(' VALUES (:a, :b, :c, :d, :e, :f) ');

        DModule.QR.Params.AsInteger[0] :=sample;
        DModule.QR.Params.AsString[1] := trim(UpperCase(mmoRemarks.Text));
        DModule.QR.Params.AsString[2] := trim(UpperCase(txtPurpose.Text));
        DModule.QR.Params.AsString[3] := lb_ctcnum.Caption;
        DModule.QR.Params.AsString[4] := lb_tpID.Caption;
        DModule.QR.Params.AsString[5] := pkHide.Caption;
          MessageDlg('Information has been save',mtInformation,[mbOK],0);
        DModule.QR.Prepare;
        DModule.QR.open();

           if DModule.TR.InTransaction then
          DModule.TR.Commit;

          frmServices.Show;
          frmServices.Clear;
          Clear();
          Close;
          Report();
   end
   else
        MessageDlg('Nothing saved',mtInformation,[mbOK],0);
end;

procedure TfrmTPDetails.Report();
begin
MaxID();
      DModule.rep.Filename := 'reports/brgyclearance.rep';
      DModule.rep.Report.Params.Items[0].Value :=sample;
      DModule.rep.Preview := True;
      DModule.rep.Execute();
      Close;
end;


procedure TfrmTPDetails.Delete();
begin
      if MessageDlg('Are you sure you want to cancel?',mtConfirmation,[mbYes,mbNo],0)=mrYes then begin

        if DModule.TR.InTransaction then
                DModule.TR.Commit;
       DModule.TR.StartTransaction;
       DModule.QR.SQL.Clear();

       DModule.QR.SQL.Add('DELETE FROM Collections where ColID=:A');

       DModule.QR.Params.AsString[0]:=pkHide.Caption;
       DModule.QR.Prepare;
       DModule.QR.Open();

         if DModule.TR.InTransaction then
            DModule.TR.Commit;

          Close;
           frmServices.Clear;
           frmServices.show;
           frmServices.txtCTCNum.Enabled:=False;
  end
  else begin
  end
end;

procedure TfrmTPDetails.Clear();
begin
    pkHide.Caption:='';
    txtName.Clear;
    txtAge.Clear;
    cmbGender.ItemIndex:=-1;
    cmbCivilStatus.ItemIndex:=-1;
    mmoAddress.Clear;
    txtPurpose.Clear;
end;

procedure TfrmTPDetails.MaxID();
    var sample:integer;
begin
 if DModule.TR.InTransaction then
        DModule.TR.Commit;
    DModule.QR.SQL.Clear();
    DModule.QR.SQL.Add('SELECT MAX(ClearanceID) ClearanceID');
    DModule.QR.SQL.Add('FROM ClearanceDetails');
    DModule.QR.Prepare;
    DModule.QR.Open();
        sample := DModule.QR.Fields.AsInteger[0];

    if DModule.QR.Fields.Eof then
      else begin
        DModule.QR.SQL.Clear;
        DModule.QR.SQL.Add('SELECT MAX(ClearanceID) FROM ClearanceDetails');
        DModule.QR.Prepare;
        DModule.QR.Open();
            sample := DModule.QR.Fields.AsInteger[0] + 1;
        end;

         if DModule.TR.InTransaction then
       DModule.TR.Commit;

end;

procedure TfrmTPDetails.AutoPrime();
    var a:integer;
begin
a:=1;
 if DModule.TR.InTransaction then
        DModule.TR.Commit;
    DModule.QR.SQL.Clear();
    DModule.QR.SQL.Add('SELECT ClearanceID');
    DModule.QR.SQL.Add('FROM ClearanceDetails');
    DModule.QR.Prepare;
    DModule.QR.Open();
   if DModule.QR.Fields.Eof then
    sample:=a
      else begin
       DModule.QR.SQL.Clear;
        DModule.QR.SQL.Add('SELECT MAX(ClearanceID) FROM ClearanceDetails');
        DModule.QR.Prepare;
        DModule.QR.Open();
        sample := DModule.QR.Fields.AsInteger[0] + 1;
        end;
         if DModule.TR.InTransaction then
       DModule.TR.Commit;
end;

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

procedure TfrmTPDetails.A(Sender: TObject; var Key: Char);
begin
      if Key = #13 then begin
        SelectNext(Sender as tWinControl, True, True );
      if Sender = txtAge then
        btnSave.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 TfrmTPDetails.cmbGenderKeyPress(Sender: TObject; var Key: Char);
begin
      if Key = #13 then begin
        SelectNext(Sender as tWinControl, True, True );
      if Sender = cmbGender then
        btnSave.Click; // let the button click itself
        Key := #0;
      end;
end;

procedure TfrmTPDetails.cmbCivilStatusKeyPress(Sender: TObject;
  var Key: Char);
begin
      if Key = #13 then begin
        SelectNext(Sender as tWinControl, True, True );
      if Sender = cmbCivilStatus then
        btnSave.Click; // let the button click itself
        Key := #0;
      end;
end;

procedure TfrmTPDetails.mmoAddressKeyPress(Sender: TObject;
  var Key: Char);
begin
      if Key = #13 then begin
        SelectNext(Sender as tWinControl, True, True );
      if Sender = mmoAddress then
        btnSave.Click; // let the button click itself
        Key := #0;
      end;
end;

procedure TfrmTPDetails.mmoRemarksKeyPress(Sender: TObject;
  var Key: Char);
begin
      if Key = #13 then begin
        SelectNext(Sender as tWinControl, True, True );
      if Sender = mmoRemarks then
        btnSave.Click; // let the button click itself
        Key := #0;
      end;
end;

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

procedure TfrmTPDetails.FormShow(Sender: TObject);
begin
      frmServices.displayTName();
      //ebutang and primary ni taxpayer
      pkHide.Caption:=frmServices.pk;
      Label8.Caption:=frmServices.txtORNum.Text;
      CTCNO.Caption:=frmServices.txtCTCNum.Text;
      ISSUEDDATE.Caption:=frmServices.txtIssuedDate.Text;
      ISSUEDAT.Caption:=frmServices.mmoIssuedAt.Text;
end;

procedure TfrmTPDetails.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
      Clear;
end;

end.

