unit AddParticularRequirements;

interface

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

type
  TfrmAddPartReq = class(TForm)
    GroupBox1: TGroupBox;
    txtAddReq: TEdit;
    btnCancel: TBitBtn;
    lbP_Num: TLabel;
    lvPartReq: TListView;
    rightclick: TPopupMenu;
    Add1: TMenuItem;
    Delete1: TMenuItem;
    btnAdd: TBitBtn;

    procedure btnCancelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure LoadReq();
    procedure CheckReq();
    procedure btnAddClick(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
     checkRequirements:String;
  end;

var
  frmAddPartReq: TfrmAddPartReq;

implementation

uses Particulars, DM, Math;

{$R *.dfm}

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

procedure TfrmAddPartReq.FormShow(Sender: TObject);
begin
      LoadReq();
      GroupBox1.Caption:= frmParticularss.lvParticulars.ItemFocused.SubItems[0];
      lbP_Num.Caption:= frmParticularss.lvParticulars.ItemFocused.Caption;
end;

procedure TfrmAddPartReq.LoadReq();
var newState : TListItem;
begin
     If DModule.TR.InTransaction then
        DModule.TR.Commit;

       DModule.TR.StartTransaction;
       DModule.QR.SQL.Clear();
       DModule.QR.SQL.Add('SELECT PARTID,REQNAME');
       DModule.QR.SQL.Add('FROM PARTREQUIREMENTS');
       DModule.QR.Prepare;
       DModule.QR.Open();
          lvPartReq.Items.BeginUpdate;
          lvPartReq.Items.Clear;
    if DModule.QR.Fields.IsNull[0] then Exit;
    while not DModule.QR.Eof do begin
        newState := lvPartReq.Items.Add;
        newState.Caption := DModule.QR.Fields.AsString[0];
        newState.SubItems.Add(DModule.QR.Fields.AsString[1]);
        DModule.QR.Next;
    end;
    if DModule.TR.InTransaction then
        DModule.TR.Commit;
    lvPartReq.Items.EndUpdate;
end;

procedure TfrmAddPartReq.CheckReq();
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 PartRequirements WHERE PARTID =:a');

    DModule.QR.Params.AsString[0] := Trim(UpperCase(txtAddReq.Text));

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

    checkRequirements:=DModule.QR.Fields.AsString[1];

end;

procedure TfrmAddPartReq.btnAddClick(Sender: TObject);
begin
CheckReq();
      if (txtAddReq.Text='') then begin
           MessageDlg('Fields require',mtError,[mbOK],0);
           txtAddReq.SetFocus;
        end
        else if (Trim(UpperCase(txtAddReq.Text)) = checkRequirements) then begin
           MessageDlg('Fields require',mtError,[mbOK],0);
           txtAddReq.SetFocus;
        end
        else
            if MessageDlg('Save this requirement?',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 partrequirements');
                DModule.QR.SQL.Add('(REQNAME,P_NUM)');
                DModule.QR.SQL.Add('VALUES(:b, :c)');
                DModule.QR.Params.AsString[0] := trim(UpperCase(txtAddReq.Text));
                DModule.QR.Params.AsString[1] := lbP_Num.Caption;
                    MessageDlg('Requirement has been save',mtInformation,[mbOK],0);
                DModule.QR.Prepare;
                DModule.QR.open();

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

end.
