unit ViewSetTrackingNumber;

interface

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

type
  TfrmViewSetTrackingNumber = class(TForm)
    Timer1: TTimer;
    lvTrackingNumber: TListView;
    rightclick: TPopupMenu;
    popEdit: TMenuItem;
    BitBtn1: TBitBtn;
    GroupBox2: TGroupBox;
    Panel1: TPanel;
    N1: TMenuItem;
    Delete1: TMenuItem;
    GroupBox1: TGroupBox;
    lb_pname: TLabel;
    lb_pnum: TLabel;
    cmbLoadTrrackType: TComboBox;
    btnSetTo: TBitBtn;
    Panel2: TPanel;
    SetTrackingNumber1: TMenuItem;
    lb_ttno: TLabel;
    procedure FormShow(Sender: TObject);
    procedure btnSetNewClick(Sender: TObject);
    procedure CheckTrackIfP_NUMExist();
    procedure popEditClick(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure LoadTrackCMB();
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure SetThisTo();
    procedure loadTrackContain();
    procedure btnSetToClick(Sender: TObject);
    procedure cmbLoadTrrackTypeKeyPress(Sender: TObject; var Key: Char);
    procedure AutoPrime();
    procedure SetTrackingNumber1Click(Sender: TObject);
    procedure cmbLoadTrrackTypeClick(Sender: TObject);
    procedure comboboxTTNo();
    procedure countColumns;
    

  private
    { Private declarations }
  public
    { Public declarations }
    p_num:Integer;
    setType:String;
    partname:String;
    sample:Integer;
    ttno:Integer;
    count:Integer;
  end;

var
  frmViewSetTrackingNumber: TfrmViewSetTrackingNumber;

implementation

uses DM, SetTrackNum, Main, Math, Particulars, jvuiblib, jvuib,
  SetTracking, SetTrackName;
{$R *.dfm}

procedure TfrmViewSetTrackingNumber.FormShow(Sender: TObject);
begin
          LoadTrackCMB();
          loadTrackContain();
end;

procedure TfrmViewSetTrackingNumber.btnSetNewClick(Sender: TObject);
begin
      frmUpdateTrackNum.ShowModal;
end;


//.................tricks, magics in here
procedure TfrmViewSetTrackingNumber.countColumns();
begin
      if DModule.TR.InTransaction then
        DModule.TR.Commit;

          DModule.QR.SQL.Clear;
          DModule.QR.SQL.Add('SELECT COUNT(SETCODE)');
          DModule.QR.SQL.Add('FROM SETTTRACKINGNUM');
          DModule.QR.Prepare;
          DModule.QR.Open();
            count:=DModule.QR.Fields.AsInteger[0];
      if DModule.TR.InTransaction then
        DModule.TR.Commit;
end;

procedure TfrmViewSetTrackingNumber.LoadTrackCMB();
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();
         cmbLoadTrrackType.Items.BeginUpdate;
         cmbLoadTrrackType.Items.Clear;
            if DModule.QR.Fields.IsNull[0] then Exit;
               while not DModule.QR.Eof do begin
                  cmbLoadTrrackType.Items.Add(DModule.QR.Fields.AsString[0]);
              DModule.QR.Next;
            end;
      if DModule.TR.InTransaction then
        DModule.TR.Commit;
        cmbLoadTrrackType.Items.EndUpdate;
end;
procedure TfrmViewSetTrackingNumber.LoadTrackContain();
    var newState:TListItem;
begin
     if DModule.TR.InTransaction then
        DModule.TR.Commit;

        DModule.TR.StartTransaction;
        DModule.QR.SQL.Clear();
        DModule.QR.SQL.Add('SELECT a.SETCODE, a.TRACKTYPE,a.TRACKNUM_FROM,a.TRACKNUM_TO,b.P_NUM');
        DModule.QR.SQL.Add('FROM SETTRACKINGNUM a, PARTICULARS b where a.P_NUM=b.P_NUM');
        DModule.QR.Prepare;
        DModule.QR.Open();

    lvTrackingNumber.Clear;
    lvTrackingNumber.Items.BeginUpdate;

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

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

    lvTrackingNumber.Items.EndUpdate;
end;

procedure TfrmViewSetTrackingNumber.CheckTrackIfP_NUMExist();
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 SETTRACKINGNUM');
        DModule.QR.Prepare;
        DModule.QR.Open();
          p_num:=DModule.QR.Fields.AsInteger[6];
    if DModule.TR.InTransaction then
        DModule.TR.Commit;
end;

procedure TfrmViewSetTrackingNumber.popEditClick(Sender: TObject);
begin
      frmUpdateTrackNum.ShowModal;
      frmUpdateTrackNum.btnSet.Caption:='&Edit';
end;

procedure TfrmViewSetTrackingNumber.BitBtn1Click(Sender: TObject);
begin
      frmSetTrackName.ShowModal;
end;

procedure TfrmViewSetTrackingNumber.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
      if MessageDlg('Leave this form?',mtConfirmation,[mbYes, mbNo], 0)= mrYes then begin
        Exit
      end
      else
          Action:=caNone; //do nothing

end;

procedure TfrmViewSetTrackingNumber.SetThisTo();
begin
CheckTrackIfP_NUMExist();
      if (StrToInt(lb_pnum.Caption) <> p_num)then begin
            if DModule.TR.InTransaction then
                DModule.TR.Commit;
                  DModule.QR.SQL.Clear;
                  DModule.QR.SQL.Add('INSERT INTO SETTRACKINGNUM ');
                  DModule.QR.SQL.Add('(SETCODE,TRACKTYPE, SETDATE, TTNO, P_NUM)');
                  DModule.QR.SQL.Add(' VALUES (:a, :b, :c, :d, :e) ');
                    DModule.QR.Params.AsInteger[0] := sample;
                    DModule.QR.Params.AsString[1] := cmbLoadTrrackType.Text;
                    DModule.QR.Params.AsString[2] := DateToStr(Now);
                    DModule.QR.Params.AsInteger[3] := StrToInt(lb_ttno.Caption);
                    DModule.QR.Params.AsInteger[4] := StrToInt(lb_pnum.Caption);
                  DModule.QR.Prepare;
                  DModule.QR.Open();
                    MessageDlg('Set successfully',mtInformation,[mbOK],0);

            if DModule.TR.InTransaction then
                DModule.TR.Commit;
                loadTrackContain();
      end
      else begin
            if DModule.TR.InTransaction then
                DModule.TR.Commit;
                  DModule.QR.SQL.Clear;
                  DModule.QR.SQL.Add('UPDATE SETTRACKINGNUM SET TRACKTYPE=:A,SETDATE=:b,P_NUM=:c where Setcode=:d');
                  DModule.QR.Params.AsString[0] := cmbLoadTrrackType.Text;
                  DModule.QR.Params.AsString[1] := DateToStr(Now);
                  DModule.QR.Params.AsInteger[2] := StrToInt(lb_pnum.Caption);
                  DModule.QR.Prepare;
                  DModule.QR.Open();
                    MessageDlg('Set successfully',mtInformation,[mbOK],0);
                    loadTrackContain();
            if DModule.TR.InTransaction then
                DModule.TR.Commit;
      end

end;

procedure TfrmViewSetTrackingNumber.btnSetToClick(Sender: TObject);
begin
        SetThisTo();
end;

procedure TfrmViewSetTrackingNumber.cmbLoadTrrackTypeKeyPress(
  Sender: TObject; var Key: Char);
begin
      if Key = #13 then begin
        SelectNext(Sender as tWinControl, True, True );
      if Sender = cmbLoadTrrackType then
        btnSetTo.Click; // let the button click itself
        Key := #0;
      end;
end;

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

procedure TfrmViewSetTrackingNumber.SetTrackingNumber1Click(
  Sender: TObject);
begin
    if(lvTrackingNumber.ItemFocused <> nil)then begin
        frmSetTracking.ShowModal;
    end
    else
        MessageDlg('Please select item(s) to set',mtError,[mbOK],0);
end;

procedure TfrmViewSetTrackingNumber.cmbLoadTrrackTypeClick(
  Sender: TObject);
begin
      comboboxTTNo();
end;

procedure TfrmViewSetTrackingNumber.comboboxTTNo();
begin
      if DModule.TR.InTransaction then
          DModule.TR.Commit;
            DModule.QR.SQL.Clear;
            DModule.QR.SQL.Add('SELECT TTNO FROM TRACKINGTYPE WHERE TT_TYPE=:a');
              DModule.QR.Params.AsString[0]:=cmbLoadTrrackType.Text;
            DModule.QR.Prepare;
            DModule.QR.Open();
              ttno:=DModule.QR.Fields.AsInteger[0];
              lb_ttno.Caption:=IntToStr(ttno);
      if DModule.TR.InTransaction then
          DModule.TR.Commit;
end;

end.
