unit taxPayerForm;

interface

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

type
  TfrmtaxPayer = class(TForm)
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    txtName: TEdit;
    cmbGender: TComboBox;
    cmbCivilStatus: TComboBox;
    mmoAddress: TMemo;
    btnSave: TBitBtn;
    btnCancel: TBitBtn;
    lb_age: TLabel;
    GroupBox2: TGroupBox;
    lvTaxPayer: TListView;
    txtSearch: TEdit;
    txtDate: TEdit;
    cmbMonth: TComboBox;
    txtYear: TEdit;
    btnViewAll: TBitBtn;
    PopupMenu1: TPopupMenu;
    popEdit: TMenuItem;
    lb_TP_ID: TLabel;
    btnNew1: TBitBtn;
    Panel1: TPanel;
    procedure btnCancelClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure Save();
    procedure AutoPrime();
    procedure Clear();
    procedure calcAge();
    procedure txtNameKeyPress(Sender: TObject; var Key: Char);
    procedure cmbGenderKeyPress(Sender: TObject; var Key: Char);
    procedure cmbCivilStatusKeyPress(Sender: TObject; var Key: Char);
    procedure cmbMonthKeyPress(Sender: TObject; var Key: Char);
    procedure txtDateKeyPress(Sender: TObject; var Key: Char);
    procedure txtYearKeyPress(Sender: TObject; var Key: Char);
    procedure mmoAddressKeyPress(Sender: TObject; var Key: Char);
    procedure txtSearchChange(Sender: TObject);
    procedure SearchTaxPayer();
    procedure SearchTaxPayer2();
    procedure btnViewAllClick(Sender: TObject);
    procedure popEditClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    //procedure btnNewClick(Sender: TObject);
    procedure btnNew1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    sample:integer;
    month:String;
    day:String;
    year:String;
    age:integer;
  end;

var
  frmtaxPayer: TfrmtaxPayer;

implementation

uses DM, LogIn, Main,jvuiblib ;
{$R *.dfm}

procedure TfrmtaxPayer.btnCancelClick(Sender: TObject);
begin
      if MessageDlg('Do you want to cancel?',mtConfirmation,[mbYes,mbNo],0)=mrYes then begin
          Clear;
          Close;
      end
      else

end;

procedure TfrmtaxPayer.btnSaveClick(Sender: TObject);
begin
      Save();
end;
procedure TfrmtaxPayer.Clear();
begin
     txtName.Text:='';
end;

procedure TfrmtaxPayer.Save();
begin
AutoPrime;
calcAge;
//month := FormatDateTime('mmmm',Now());
//day := FormatDateTime('dd', now());
year:= FormatDateTime('yyyy', Now());

    if (txtName.Text='')then begin
       MessageDlg('Name required',mtError,[mbOK],0);
       txtName.SetFocus;
    end
    else if(cmbGender.Text='')then begin
       MessageDlg('Gender required',mtError,[mbOK],0);
       cmbGender.SetFocus;
    end
    else if(cmbCivilStatus.Text='')then begin
       MessageDlg('Civil Status required',mtError,[mbOK],0);
       cmbCivilStatus.SetFocus;
    end
    else if(cmbMonth.Text='')then begin
       MessageDlg('Birth month required',mtError,[mbOK],0);
       cmbMonth.SetFocus;
    end
    else if(txtDate.Text='')then begin
       MessageDlg('Birth date required',mtError,[mbOK],0);
       txtDate.SetFocus;
    end
    else if(txtYear.Text='')then begin
       MessageDlg('Birth year required',mtError,[mbOK],0);
       txtYear.SetFocus;
    end
    else if(StrToInt(year) < StrToInt(txtYear.Text) ) then begin
            MessageDlg('Wrong birth year'+#13#10+'It cannot be set to future time.',mtError,[mbOK],0);
    end
    else if(mmoAddress.Text='')then begin
       MessageDlg('Address required',mtError,[mbOK],0);
       mmoAddress.SetFocus;
    end
    else
      if MessageDlg('Save taxpayer?',mtConfirmation,[mbYes,mbNo],0)=mrYes then begin
          if DModule.TR.InTransaction then
            DModule.TR.Commit;
              DModule.QR.SQL.Clear;
              DModule.QR.SQL.Add('INSERT INTO TaxPayer ');
              DModule.QR.SQL.Add('(TP_ID,NAME,Gender,CivilStatus,Birthdate,Age,Address,DateAdded,UI_ID)');
              DModule.QR.SQL.Add(' VALUES (:a, :b, :c, :d, :e, :f, :g, :h, :i) ');
                DModule.QR.Params.AsInteger[0] := sample;
                DModule.QR.Params.AsString[1] := Trim(UpperCase(txtName.Text));
                DModule.QR.Params.AsString[2] := cmbGender.Text;
                DModule.QR.Params.AsString[3] := cmbCivilStatus.Text;
                DModule.QR.Params.AsString[4] := cmbMonth.Text+' ' + txtDate.Text+','+ txtYear.Text ;
                DModule.QR.Params.AsInteger[5] := StrToInt(lb_age.Caption);
                DModule.QR.Params.AsString[6] := Trim(UpperCase(mmoAddress.Text));
                DModule.QR.Params.AsString[7] := DateToStr(Now);
                DModule.QR.Params.AsString[8] := frmMain.pk;
                  DModule.QR.Prepare;
                  DModule.QR.Open();
                    MessageDlg('Successfully saved',mtInformation,[mbOK],0);
          if DModule.TR.InTransaction then
            DModule.TR.Commit;
            Clear();
            //Close;

      end
end;

procedure TfrmtaxPayer.AutoPrime();
    var a:integer;
begin
a:=1;
 if DModule.TR.InTransaction then
        DModule.TR.Commit;
    DModule.QR.SQL.Clear();
    DModule.QR.SQL.Add('SELECT TP_ID');
    DModule.QR.SQL.Add('FROM TAXPAYER');
    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(TP_ID) FROM TAXPAYER');
        DModule.QR.Prepare;
        DModule.QR.Open();
        sample := DModule.QR.Fields.AsInteger[0] + 1;
        end;
         if DModule.TR.InTransaction then
       DModule.TR.Commit;
end;
{
procedure TfrmtaxPayer.dtpBDateChange(Sender: TObject);
var
  Month, Day, Year, CurrentMonth, CurrentDay, CurrentYear: word;
  Age: integer;
begin
  DecodeDate(dtpBDate.Date, Year, Month, Day);
  DecodeDate(Date,CurrentYear, CurrentMonth, CurrentDay);
  if (Year = CurrentYear) and (Month = CurrentMonth) and (Day = CurrentDay) then
    Age := 0
  else
  begin
    Age := CurrentYear - Year;
    if (Month > CurrentMonth) then
      dec(Age)
    else if Month = CurrentMonth then
      if (Day > CurrentDay) then
        dec(Age);
  end;
  lb_age.Caption := IntToStr(Age);
end; }

{procedure TfrmtaxPayer.DateTimePicker1Change(Sender: TObject);
var
  Month, Day, Year,CurrentMonth, CurrentDay, CurrentYear:Word;
  Age: integer;
begin
  DecodeDate(DateTimePicker1.Date, Year, Month, Day);
  DecodeDate(Date,CurrentYear, CurrentMonth, CurrentDay);
  if (Year = CurrentYear) and (Month = CurrentMonth) and (Day = CurrentDay) then
    Age := 0
  else
  begin
    Age := CurrentYear - Year;
    if (Month > CurrentMonth) then
      dec(Age)
    else if Month = CurrentMonth then
      if (Day > CurrentDay) then
        dec(Age);
  end;
  lb_age.Caption := IntToStr(Age);

end;}
procedure TfrmtaxPayer.calcAge();
begin
     year:= FormatDateTime('yyyy', Now());
     age:=StrToInt(year) - StrToInt(txtYear.Text);
     lb_age.Caption:=IntToStr(age);
end;

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

procedure TfrmtaxPayer.txtDateKeyPress(Sender: TObject; var Key: Char);
begin
      if Key = #13 then begin
        SelectNext(Sender as tWinControl, True, True );
      if Sender = txtDate 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('.',txtDate.text)<> 0 then begin
              key:=#0;
              windows.Beep(1000,200);
        end;
      //backspace
      if key =#8 then begin
          txtDate.Text:=Copy(txtDate.Text,1,length(txtDate.Text)-1);
          txtDate.selStart:=length(txtDate.Text);
          key:=#0;
          end;
      end
      else begin
          key:=#0;
          windows.Beep(1000,200);
      end;
end;

procedure TfrmtaxPayer.txtYearKeyPress(Sender: TObject; var Key: Char);
begin
      if Key = #13 then begin
        SelectNext(Sender as tWinControl, True, True );
      if Sender = txtYear 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('.',txtYear.text)<> 0 then begin
              key:=#0;
              windows.Beep(1000,200);
        end;
      //backspace
      if key =#8 then begin
          txtYear.Text:=Copy(txtYear.Text,1,length(txtYear.Text)-1);
          txtYear.selStart:=length(txtYear.Text);
          key:=#0;
          end;
      end
      else begin
          key:=#0;
          windows.Beep(1000,200);
      end;
end;

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

procedure TfrmtaxPayer.txtSearchChange(Sender: TObject);
begin
        SearchTaxPayer2();
end;
procedure TfrmtaxPayer.SearchTaxPayer();
var
  newState: TListItem;
begin

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

    DModule.TR.StartTransaction;
    DModule.QR.SQL.Clear();
    DModule.QR.SQL.Add('SELECT TP_ID,NAME,GENDER,CIVILSTATUS,BIRTHDATE,ADDRESS,DATEADDED,UI_ID');
    DModule.QR.SQL.Add('FROM TaxPayer');
    DModule.QR.Prepare;
    DModule.QR.Open();


    lvTaxPayer.Clear;
    lvTaxPayer.Items.BeginUpdate;

    if DModule.QR.Fields.IsNull[0] then Exit;
    while not DModule.QR.Eof do begin
        newState := lvTaxPayer.Items.Add;
        newState.Caption := DModule.QR.Fields.AsString[0];
        newState.SubItems.Add(DModule.QR.Fields.AsString[1]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[2]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[3]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[4]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[5]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[6]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[7]);
        DModule.QR.Next;
    end;

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

    lvTaxPayer.Items.EndUpdate;
end;
procedure TfrmtaxPayer.SearchTaxPayer2();
var
  newState: TListItem;
begin

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

    DModule.TR.StartTransaction;
    DModule.QR.SQL.Clear();
    DModule.QR.SQL.Add('SELECT TP_ID,NAME,GENDER,CIVILSTATUS,BIRTHDATE,ADDRESS,DATEADDED,UI_ID');
    DModule.QR.SQL.Add('FROM TaxPayer where Name like(:a) or Address like(:a)');
      DModule.QR.Params.AsString[0]:=Trim('%'+UpperCase(txtSearch.Text)+'%');
    DModule.QR.Prepare;
    DModule.QR.Open();


    lvTaxPayer.Clear;
    lvTaxPayer.Items.BeginUpdate;

    if DModule.QR.Fields.IsNull[0] then Exit;
    while not DModule.QR.Eof do begin
        newState := lvTaxPayer.Items.Add;
        newState.Caption := DModule.QR.Fields.AsString[0];
        newState.SubItems.Add(DModule.QR.Fields.AsString[1]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[2]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[3]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[4]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[5]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[6]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[7]);
        DModule.QR.Next;
    end;

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

    lvTaxPayer.Items.EndUpdate;
end;

procedure TfrmtaxPayer.btnViewAllClick(Sender: TObject);
begin
    SearchTaxPayer();
end;

procedure TfrmtaxPayer.popEditClick(Sender: TObject);
begin
      if lvTaxPayer.ItemFocused  <> nil then begin
          if MessageDlg('Do you want to edit?',mtConfirmation,[mbYes,mbNo],0) = mrYes then begin
              GroupBox1.Enabled:=True;
              
              lb_TP_ID.Caption:=lvTaxPayer.ItemFocused.Caption;
              txtName.Text:=lvTaxPayer.ItemFocused.SubItems[0];
              cmbGender.Text:=lvTaxPayer.ItemFocused.SubItems[1];
              cmbCivilStatus.Text:=lvTaxPayer.ItemFocused.SubItems[2];
              mmoAddress.Text:=lvTaxPayer.ItemFocused.SubItems[4];

              if (txtName.Text='')then
              begin
                  MessageDlg('Field required',mtError,[mbOk],0);
                  txtName.SetFocus;
              end
              else if (cmbGender.Text='')then
              begin
                  MessageDlg('Field required',mtError,[mbOk],0);
                  cmbGender.SetFocus;
              end
              else if (cmbCivilStatus.Text='')then
              begin
                  MessageDlg('Field required',mtError,[mbOk],0);
                  cmbCivilStatus.SetFocus;
              end
              else if (cmbMonth.Text='')then
              begin
                  MessageDlg('Field required',mtError,[mbOk],0);
                  txtName.SetFocus;
              end
              else if (txtDate.Text='')then
              begin
                  MessageDlg('Field required',mtError,[mbOk],0);
                  txtDate.SetFocus;
              end
              else if (txtYear.Text='')then
              begin
                  MessageDlg('Field required',mtError,[mbOk],0);
                  txtYear.SetFocus;
              end
              else if (mmoAddress.Text='')then
              begin
                  MessageDlg('Field required',mtError,[mbOk],0);
                  mmoAddress.SetFocus;
              end
              else begin
              if MessageDlg('Do you want to save changes?',mtConfirmation,[mbYes,mbNo],0)=mrYes then begin
                  if DModule.TR.InTransaction then
                      DModule.TR.Commit;
                      DModule.QR.SQL.Clear;
                      DModule.QR.SQL.Add('UPDATE TAXPAYER SET TP_ID=:A,NAME=:B,GENDER=:C,CIVILSTATUS=:D,BIRTHDATE=:E,AGE=:F,ADDRESS=:G,DATEADDED,UI_ID=:H WHERE TP_ID=:I ');
                      DModule.QR.Params.AsInteger[0] := StrToInt(lb_TP_ID.Caption);
                      DModule.QR.Params.AsString[1] := trim(UpperCase(txtName.Text));
                      DModule.QR.Params.AsString[2] := cmbGender.Text;
                      DModule.QR.Params.AsString[3] := cmbCivilStatus.Text;
                      DModule.QR.Params.AsString[4] := cmbMonth.Text+' '+txtDate.Text+','+txtYear.Text;
                      DModule.QR.Params.AsInteger[5] := StrToInt(lb_age.Caption);
                      DModule.QR.Params.AsString[6] := Trim(UpperCase(mmoAddress.Text));
                      DModule.QR.Params.AsString[7] := DateToStr(now);
                      DModule.QR.Params.AsString[8] := frmMain.pk;
                      MessageDlg('Information has been updated',mtInformation,[mbOK],0);
                      DModule.QR.Prepare;
                       DModule.QR.Open();
              if DModule.TR.InTransaction then
                      DModule.TR.Commit;
              end
              else
                MessageDlg('Changes not save',mtInformation,[mbOK],0);
            end
          end
          else
             MessageDlg('Nothing change',mtInformation,[mbOK],0);
      end
      else
          MessageDlg('Please select row to edit',mtError,[mbOK],0);
end;

procedure TfrmtaxPayer.FormShow(Sender: TObject);
begin
        GroupBox1.Enabled:=False;
end;

{procedure TfrmtaxPayer.btnNewClick(Sender: TObject);
begin
      GroupBox1.Enabled:=True;
end;}

procedure TfrmtaxPayer.btnNew1Click(Sender: TObject);
begin
      GroupBox1.Enabled:=True;
end;

end.
