unit SetTracking;

interface

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

type
  TfrmSetTracking = class(TForm)
    Timer1: TTimer;
    txtTrackNum: TEdit;
    GroupBox1: TGroupBox;
    btnSet: TBitBtn;
    cmbSelectTrackingType: TComboBox;
    txtTo: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    procedure btnSetClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure loadCmb();
    procedure txtTrackNumKeyPress(Sender: TObject; var Key: Char);
    procedure cmbSelectTrackingTypeKeyPress(Sender: TObject;
      var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
    tracktype:String;
  end;

var
  frmSetTracking: TfrmSetTracking;

implementation

{$R *.dfm}
uses DM, jvuib, ViewSetTrackingNumber;

procedure TfrmSetTracking.btnSetClick(Sender: TObject);
begin
      if (txtTrackNum.Text='')then begin
         MessageDlg('Field required',mtError,[mbOK],0);
         txtTrackNum.SetFocus;
      end
      else if (txtTo.Text='')then begin
         MessageDlg('Field required',mtError,[mbOK],0);
         txtTo.SetFocus;
      end
      else if(cmbSelectTrackingType.Text='')then begin
         MessageDlg('Select Tracking type',mtError,[mbOK],0);
         cmbSelectTrackingType.SetFocus;
      end
      else begin
          if DModule.TR.InTransaction then
            DModule.TR.Commit;

            DModule.QR.SQL.Clear;
            DModule.QR.SQL.Add('UPDATE SETTRACKINGNUM SET TRACKNUM_FROM=:A,TRACKNUM_TO=:B WHERE TRACKTYPE=:C');
            DModule.QR.Params.AsString[0] := Trim(txtTrackNum.Text);
            DModule.QR.Params.AsString[1] := Trim(txtTo.Text);
            DModule.QR.Params.AsString[2] := cmbSelectTrackingType.Text;
            DModule.QR.Prepare;
            DModule.QR.Open();
                MessageDlg('Tracking number has been set',mtInformation,[mbOK],0);

         if DModule.TR.InTransaction then
            DModule.TR.Commit;
              txtTrackNum.Text:='';
              txtTo.Text:='';
              cmbSelectTrackingType.ItemIndex:=-1;
              frmViewSetTrackingNumber.Show;
              frmViewSetTrackingNumber.loadTrackContain;

      end
end;

procedure TfrmSetTracking.FormShow(Sender: TObject);
begin
      loadCmb();
end;
procedure TfrmSetTracking.loadCmb();
begin
      if DModule.TR.InTransaction then
        DModule.TR.Commit;

        DModule.TR.StartTransaction;
        DModule.QR.SQL.Clear();
        DModule.QR.SQL.Add('SELECT TT_TYPE');
        DModule.QR.SQL.Add('FROM TRACKINGTYPE');
        DModule.QR.Prepare;
        DModule.QR.Open();
         cmbSelectTrackingType.Items.BeginUpdate;
         cmbSelectTrackingType.Items.Clear;
            if DModule.QR.Fields.IsNull[0] then Exit;
               while not DModule.QR.Eof do begin
                  cmbSelectTrackingType.Items.Add(DModule.QR.Fields.AsString[0]);
              DModule.QR.Next;
            end;
      if DModule.TR.InTransaction then
        DModule.TR.Commit;
        cmbSelectTrackingType.Items.EndUpdate;
end;
procedure TfrmSetTracking.txtTrackNumKeyPress(Sender: TObject;
  var Key: Char);
begin
      if Key = #13 then begin
        SelectNext(Sender as tWinControl, True, True );
      if Sender = txtTrackNum then
        btnSet.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('.',txtTrackNum.text)<> 0 then begin
              key:=#0;
              windows.Beep(1000,200);
        end;
      //backspace
      if key =#8 then begin
          txtTrackNum.Text:=Copy(txtTrackNum.Text,1,length(txtTrackNum.Text)-1);
          txtTrackNum.selStart:=length(txtTrackNum.Text);
          key:=#0;
          end;
      end
      else begin
          key:=#0;
          windows.Beep(1000,200);
      end;
end;

procedure TfrmSetTracking.cmbSelectTrackingTypeKeyPress(Sender: TObject;
  var Key: Char);
begin
      if Key = #13 then begin
        SelectNext(Sender as tWinControl, True, True );
      if Sender = txtTrackNum then
        btnSet.Click; // let the button click itself
        Key := #0;
      end;
end;

end.
