unit Particulars;

interface

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

type
  TfrmParticularss = class(TForm)
    gbParticulars: TGroupBox;
    lbPartName: TLabel;
    txtPartname: TEdit;
    Label1: TLabel;
    txtPartFee: TEdit;
    gbPartlistview: TGroupBox;
    txtSearch: TEdit;
    lvParticulars: TListView;
    rightClick: TPopupMenu;
    Label2: TLabel;
    cmbType: TComboBox;
    Label3: TLabel;
    lbTime: TLabel;
    popDelete: TMenuItem;
    cmbSearch: TComboBox;
    btnNew: TBitBtn;
    btnCancel: TBitBtn;
    btnSave: TBitBtn;
    btnDelete: TBitBtn;
    btnEdit: TBitBtn;
    N1: TMenuItem;
    Refresh: TMenuItem;
    btnRefresh: TBitBtn;
    AddReq: TMenuItem;
    PartStatusbar: TStatusBar;
    Timer1: TTimer;
    btnSetTo: TBitBtn;
    Setthisto: TMenuItem;
    //procedure FormActivate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure lvParticularsDblClick(Sender: TObject);
    procedure Save();
    procedure Edited();
    procedure btnSaveKeyPress(Sender: TObject; var Key: Char);
    procedure btnSaveEnter(Sender: TObject);
    procedure popDeleteClick(Sender: TObject);
    procedure btnbGoClick(Sender: TObject);
    procedure Search();
    procedure DoubleClick();
    procedure Clear();
    procedure Deleted();
    procedure AutoPrime();
    procedure VerifyPartName();
    procedure RefreshLoad();
    procedure btnRefreshClick(Sender: TObject);
    procedure txtPartnameKeyPress(Sender: TObject; var Key: Char);
    procedure txtPartFeeKeyPress(Sender: TObject; var Key: Char);
    procedure cmbTypeKeyPress(Sender: TObject; var Key: Char);
    procedure btnNewClick(Sender: TObject);
    procedure btnGoClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure btnEditClick(Sender: TObject);
    procedure btnDeleteClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure loadParticulars();
    procedure RefreshClick(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure CheckStatus();
    procedure AddReqClick(Sender: TObject);
    procedure popUpdateTrackNumClick(Sender: TObject);
    procedure btnSetToClick(Sender: TObject);
    procedure countColumns();
    procedure FormActivate(Sender: TObject);
    procedure txtSearchChange(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }


  end;

var
  frmParticularss: TfrmParticularss;

  sysDate : TDateTime;
  DateTime : string;
  updateme:integer;
  primary:String;
  sample:integer;
  Pname:string;
  partname:String;
  partid:String;
  count:Integer;


implementation

uses LogIn, Main, AddParticularRequirements, SetTrackNum,
  ViewSetTrackingNumber;

{$R *.dfm}

procedure TfrmParticularss.btnCancelClick(Sender: TObject);
begin
      Clear();
end;
procedure TfrmParticularss.btnGoClick(Sender: TObject);
begin
     Search();
end;

procedure TfrmParticularss.btnRefreshClick(Sender: TObject);
begin
      RefreshLoad();
end;

procedure TfrmParticularss.RefreshClick(Sender: TObject);
begin
        RefreshLoad();
end;

procedure TfrmParticularss.btnSaveClick(Sender: TObject);
begin
        if (btnSave.Caption='&Save') then begin
        Save();
        end
       else if (btnSave.Caption='&Update')  then begin
         Edited();
       end
end;
procedure TfrmParticularss.btnNewClick(Sender: TObject);
begin
      gbParticulars.Enabled:=true;
      gbPartlistview.Enabled:=true;
      txtPartname.SetFocus;
      btnSave.Caption:='&Save';
      Clear();
end;
procedure TfrmParticularss.Timer1Timer(Sender: TObject);
begin
      PartStatusbar.Panels.Items[2].Text:='Time: '+TimeToStr(Now);
end;
procedure TfrmParticularss.btnSaveKeyPress(Sender: TObject; var Key: Char);
begin
    Save();
end;
procedure TfrmParticularss.btnSaveEnter(Sender: TObject);
begin
     Save();
end;
procedure TfrmParticularss.popDeleteClick(Sender: TObject);
begin
     Deleted();
end;

procedure TfrmParticularss.btnbGoClick(Sender: TObject);
begin
    Search();
end;

procedure TfrmParticularss.lvParticularsDblClick(Sender: TObject);
begin
    DoubleClick();
end;

procedure TfrmParticularss.btnDeleteClick(Sender: TObject);
begin
    Deleted();
end;

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



//.................dirty works here
procedure TfrmParticularss.countColumns();
begin
      if DModule.TR.InTransaction then
        DModule.TR.Commit;

          DModule.QR.SQL.Clear;
          DModule.QR.SQL.Add('SELECT COUNT(P_NUM)');
          DModule.QR.SQL.Add('FROM PARTICULARS');
          DModule.QR.Prepare;
          DModule.QR.Open();
            count:=DModule.QR.Fields.AsInteger[0];
          PartStatusbar.Panels.Items[0].Text:='Columns: '+IntToStr(count);
      if DModule.TR.InTransaction then
        DModule.TR.Commit;
end;
procedure TfrmParticularss.Clear();
begin
    txtPartname.Clear;
    txtPartFee.Clear;
    cmbType.ItemIndex := -1;
end;

procedure TfrmParticularss.loadParticulars();
var
  newState: TListItem;
begin

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

    DModule.TR.StartTransaction;
    DModule.QR.SQL.Clear();
    DModule.QR.SQL.Add('SELECT P_Num, Description, PartType, Amount,DateCreated,UI_ID');
    DModule.QR.SQL.Add('FROM Particulars');
    DModule.QR.Prepare;
    DModule.QR.Open();


    lvParticulars.Clear;
    lvParticulars.Items.BeginUpdate;

    if DModule.QR.Fields.IsNull[0] then Exit;
    while not DModule.QR.Eof do begin
        newState := lvParticulars.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]);

        DModule.QR.Next;
    end;

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

    lvParticulars.Items.EndUpdate;

end;

procedure TfrmParticularss.Save();
var amount:Extended;
begin
VerifyPartName();
AutoPrime();
amount:=StrToInt(txtPartFee.Text);

FloatToStrF(amount,ffFixed,4,2);
ShowMessage(FloatToStr(amount));

        if(txtPartname.Text='') then begin
          MessageDlg('Field(s) required',mtConfirmation,[mbOK],0);
           txtPartname.SetFocus;
        end
        else if(Pname = UpperCase(trim(txtPartname.Text)))then begin
              MessageDlg('Particular name already exist!',mtError,[mbOK],0);
              txtPartname.SetFocus;
        end
        else if (txtPartFee.Text='') then begin
          MessageDlg('Field(s) required',mtConfirmation,[mbOK],0);
          txtPartFee.SetFocus;
        end
        else if(cmbType.Text='')then begin
              MessageDlg('Field(s) required',mtConfirmation,[mbOK],0);
              cmbType.SetFocus;
        end
    else
    if MessageDlg('Save particular?',mtConfirmation,[mbYes, mbNo], 0)= mrYes then begin
        MessageDlg('Particular has been save',mtInformation,[mbOK],0);

        DModule.QR.SQL.Clear();
        DModule.QR.SQL.Add('INSERT INTO particulars');
        DModule.QR.SQL.Add('(P_NUM,Description, Amount, PartType,DateCreated,UI_ID)');
        DModule.QR.SQL.Add(' VALUES (:a, :b, :c, :d, :e, :f) ');

        DModule.QR.Params.AsInteger[0] := sample;
        DModule.QR.Params.AsString[1] := trim(UpperCase(txtPartname.Text));
        DModule.QR.Params.AsString[2] := trim(txtPartFee.Text);
        DModule.QR.Params.AsString[3] := UpperCase(cmbType.Text);
        DModule.QR.Params.AsString[4] := DateToStr(now());
        DModule.QR.Params.AsString[5] := frmMain.pk;

        DModule.QR.Prepare;
        DModule.QR.open();

           if DModule.TR.InTransaction then
          DModule.TR.Commit;
        gbPartlistview.Enabled:=true;
        loadParticulars();
        gbParticulars.Enabled:=false;
        Clear();
     end
     else begin
      MessageDlg('Nothing saved',mtInformation,[mbOK],0);
      Clear();
     end
end;

procedure TfrmParticularss.Edited();
begin
    if(txtPartname.Text ='')or(txtPartFee.Text ='')or (cmbType.Text ='') then begin
        ShowMessage('Field(s) required');
        if(txtPartname.Text='') then begin
           txtPartname.SetFocus;
        end
        else if (txtPartFee.Text='') then begin
          txtPartFee.SetFocus;
        end
        else if(cmbType.Text='')then begin
              cmbType.SetFocus;
        end
    end
    else
      if MessageDlg('Do you want to save changes?',mtConfirmation,[mbYes, mbNo], 0)= mrYes then begin

        primary:= lvParticulars.ItemFocused.Caption;
            if DModule.TR.InTransaction then
                DModule.TR.Commit;
            DModule.QR.SQL.Clear;
            DModule.QR.SQL.Add('UPDATE PARTICULARS SET Description=:A, Amount=:B, PartType=:C WHERE P_NUM =:F');
            DModule.QR.Params.AsString[0] := trim(UpperCase(txtPartname.Text));
            DModule.QR.Params.AsString[1] := trim(txtPartFee.Text);
            DModule.QR.Params.AsString[2] := UpperCase(cmbType.Text);
            DModule.QR.Params.AsString[3] := primary;
            DModule.QR.Prepare;
            DModule.QR.Open();

            MessageDlg('Changes saved',mtInformation,[mbOK],0);

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

              gbPartlistview.Enabled:=true;
              loadParticulars();
              Clear();
              gbParticulars.Enabled:=false;
      end
     else begin
      MessageDlg('Nothing updated',mtInformation,[mbOK],0);
      Clear();
     end
end;

procedure TfrmParticularss.DoubleClick();
begin
    if lvParticulars.ItemFocused  <> nil then begin
      btnNew.Enabled:=true;
      gbParticulars.Enabled:=true;
    btnSave.Caption:='&Update';
         if MessageDlg('Doyou want to edit?',mtConfirmation,[mbYes,mbNo],0)=mrYes then begin
            txtPartname.Text := lvParticulars.ItemFocused.SubItems[0];
            txtPartFee.Text := lvParticulars.ItemFocused.SubItems[2];
            cmbType.Text := lvParticulars.ItemFocused.SubItems[1];
            gbPartlistview.Enabled:=false;
         end
         else begin
         end
     end
     else
        ShowMessage('Nothing selected');
end;

procedure TfrmParticularss.Deleted();
begin

CheckStatus();//....if already use in transaction

  if lvParticulars.ItemFocused <> nil then begin
      if MessageDlg('Are you sure you want to delete?',mtConfirmation,[mbYes,mbNo],0)=mrYes then begin
            if (lvParticulars.ItemFocused.Caption = partid)then begin
                MessageDlg('This item is already used, this cannot be deleted.',mtError,[mbOK],0);
            end
            else begin
                if DModule.TR.InTransaction then
                    DModule.TR.Commit;
                  DModule.TR.StartTransaction;
                  DModule.QR.SQL.Clear();
                  DModule.QR.SQL.Add('DELETE FROM Particulars where P_Num =:a');
                    DModule.QR.Params.AsInteger[0]:=StrToInt(lvParticulars.ItemFocused.Caption);
                  DModule.QR.Prepare;
                  DModule.QR.Open();
                    if DModule.TR.InTransaction then
                        DModule.TR.Commit;
                        loadParticulars();
                          MessageDlg('Successfully deleted',mtConfirmation,[mbOK],0);
                end
            end
      else begin
            MessageDlg('Nothing deleted',mtConfirmation,[mbOK],0);
      end
  end
  else begin
      MessageDlg('Nothing focused/selected',mtConfirmation,[mbOK],0);
  end
end;

procedure TfrmParticularss.CheckStatus(); //....if it is already use and found in the collections table
begin
      If DModule.TR.InTransaction then
       DModule.TR.Commit;

        DModule.QR.SQL.Clear();
        DModule.QR.SQL.Add(' SELECT * ');
        DModule.QR.SQL.Add('FROM Collections where P_NUM=:a');
            DModule.QR.Params.AsInteger[0]:=StrToInt(lvParticulars.ItemFocused.Caption);
        DModule.QR.Prepare;
        DModule.QR.open();

       partid := DModule.QR.Fields.AsString[7];
        if DModule.TR.InTransaction then
          DModule.TR.Commit;

end;

procedure TfrmParticularss.Search();
var newState:TListItem;
begin
  if(cmbSearch.Text='Particular Name')then begin
        if DModule.TR.InTransaction then
                DModule.TR.Commit;

       DModule.TR.InTransaction;
       DModule.QR.SQL.Clear();

       DModule.QR.SQL.Add('SELECT * FROM Particulars  where Description like(:a)');
          DModule.QR.Params.AsString[0] :=Trim('%'+UpperCase(txtSearch.Text)+'%');
       DModule.QR.Prepare;
       DModule.QR.Open();

    lvParticulars.Items.BeginUpdate;
    lvParticulars.Items.Clear;

    if DModule.QR.Fields.IsNull[0] then Exit;
    if DModule.QR.Eof then begin
      MessageDlg('No record(s) found',mtError,[mbOK],0);
   end;

    while not DModule.QR.Eof do begin
        newState := lvParticulars.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]);

        DModule.QR.Next;
    end;
    if DModule.TR.InTransaction then
        DModule.TR.Commit;
    lvParticulars.Items.EndUpdate;
  end
//----------------------------------------------------
  else if(cmbSearch.Text='Type') then begin
      if DModule.TR.InTransaction then
                DModule.TR.Commit;

       DModule.TR.InTransaction;
       DModule.QR.SQL.Clear();

       DModule.QR.SQL.Add('SELECT * FROM Particulars  where PartType like(:a) ');
       DModule.QR.Params.AsString[0] :=Trim('%'+UpperCase(txtSearch.Text)+'%');
       DModule.QR.Prepare;
       DModule.QR.Open();

    lvParticulars.Items.BeginUpdate;
    lvParticulars.Items.Clear;

    if DModule.QR.Fields.IsNull[0] then Exit;
    if DModule.QR.Eof then begin
      MessageDlg('Search not found',mtError,[mbOK],0);
   end;

    while not DModule.QR.Eof do begin
        newState := lvParticulars.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]);

        DModule.QR.Next;
    end;
    if DModule.TR.InTransaction then
        DModule.TR.Commit;
    lvParticulars.Items.EndUpdate;
  end
//------------------------------------------------
  else if(cmbSearch.Text='All') then begin
  //loadParticulars;
      if DModule.TR.InTransaction then
                DModule.TR.Commit;

       DModule.TR.InTransaction;
       DModule.QR.SQL.Clear();

       DModule.QR.SQL.Add('SELECT * FROM Particulars like(:a) ');
       DModule.QR.Params.AsString[0] :=Trim('%'+UpperCase(txtSearch.Text)+'%');
       DModule.QR.Prepare;
       DModule.QR.Open();

    lvParticulars.Items.BeginUpdate;
    lvParticulars.Items.Clear;

    if DModule.QR.Fields.IsNull[0] then Exit;
    if DModule.QR.Eof then begin
      MessageDlg('Search not found',mtInformation,[mbOK],0);
   end;

    while not DModule.QR.Eof do begin
        newState := lvParticulars.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]);

        DModule.QR.Next;
    end;
    if DModule.TR.InTransaction then
        DModule.TR.Commit;
    lvParticulars.Items.EndUpdate;
  end
end;

procedure TfrmParticularss.VerifyPartName();
begin
     if DModule.TR.InTransaction then
            DModule.TR.Commit;

    DModule.TR.StartTransaction;
    DModule.QR.SQL.Clear;
    DModule.QR.SQL.Add('SELECT * ');
    DModule.QR.SQL.Add('FROM PARTICULARS WHERE Description =:a');

    DModule.QR.Params.AsString[0] := UpperCase(trim(txtPartname.Text));

    DModule.QR.Prepare;
    DModule.QR.Open();

    Pname:=DModule.QR.Fields.AsString[1];
end;

procedure TfrmParticularss.AutoPrime();
    var a:integer;
begin
a:=1;
 if DModule.TR.InTransaction then
        DModule.TR.Commit;
    DModule.QR.SQL.Clear();
    DModule.QR.SQL.Add('SELECT P_NUM');
    DModule.QR.SQL.Add('FROM PARTICULARS');
    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(P_NUM) FROM PARTICULARS');
        DModule.QR.Prepare;
        DModule.QR.Open();
        sample := DModule.QR.Fields.AsInteger[0] + 1;
        end;
         if DModule.TR.InTransaction then
       DModule.TR.Commit;
end;

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

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

procedure TfrmParticularss.cmbTypeKeyPress(Sender: TObject; var Key: Char);
begin
      if Key = #13 then begin
        SelectNext(Sender as tWinControl, True, True );
      if Sender = cmbType then
        btnSave.Click; // let the button click itself
        Key := #0;
      end;
end;
procedure TfrmParticularss.btnEditClick(Sender: TObject);
begin
      if lvParticulars.ItemFocused  <> nil then begin
        btnNew.Enabled:=true;
        gbParticulars.Enabled:=true;
        btnSave.Caption:='&Update';

         if MessageDlg('Doyou want to edit?',mtConfirmation,[mbYes,mbNo],0)=mrYes then begin
            txtPartname.Text := lvParticulars.ItemFocused.SubItems[0];
            txtPartFee.Text := lvParticulars.ItemFocused.SubItems[2];
            cmbType.Text := lvParticulars.ItemFocused.SubItems[1];
            gbPartlistview.Enabled:=false;

            Edited();
         end

         else begin
         end
     end
     else
        MessageDlg('Nothing selected', mtInformation,[mbOK],0);

end;

procedure TfrmParticularss.btnSetToClick(Sender: TObject);
begin
       if(lvParticulars.ItemFocused <> nil)then begin
              frmViewSetTrackingNumber.lb_pnum.Caption:=lvParticulars.ItemFocused.Caption;
              frmViewSetTrackingNumber.lb_pname.Caption:=lvParticulars.ItemFocused.SubItems[0];
              frmViewSetTrackingNumber.Show();
              frmViewSetTrackingNumber.btnSetTo.Enabled:=True;
          end
          else
            MessageDlg('Please select item to set',mtError,[mbOK],0);
end;


procedure TfrmParticularss.FormShow(Sender: TObject);
begin
    Clear;
    PartStatusbar.Panels.Items[0].Text:='Columns: '+IntToStr(count);
    PartStatusbar.Panels.Items[2].Text:='Time: '+TimeToStr(Now);
end;

procedure TfrmParticularss.RefreshLoad();
var
  newState: TListItem;
begin

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

    DModule.TR.StartTransaction;
    DModule.QR.SQL.Clear();
    DModule.QR.SQL.Add('SELECT *');
    DModule.QR.SQL.Add('FROM Particulars');
    DModule.QR.Prepare;
    DModule.QR.Open();


    lvParticulars.Clear;
    lvParticulars.Items.BeginUpdate;

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

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

    lvParticulars.Items.EndUpdate;
end;

procedure TfrmParticularss.BitBtn1Click(Sender: TObject);
begin
        //DModule.rep
end;

procedure TfrmParticularss.AddReqClick(Sender: TObject);
begin
      frmAddPartReq.ShowModal;
end;

procedure TfrmParticularss.popUpdateTrackNumClick(Sender: TObject);
begin
        frmUpdateTrackNum.ShowModal;
end;

procedure TfrmParticularss.FormActivate(Sender: TObject);
begin
    PartStatusbar.Panels.Items[0].Text:='Columns: '+IntToStr(count);
end;

procedure TfrmParticularss.txtSearchChange(Sender: TObject);
begin
      Search();
end;

end.
