unit U_WBNeedLook;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridBandedTableView, cxGridDBBandedTableView, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridDBTableView, cxGrid, StdCtrls, ComCtrls,
  ExtCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB, DBClient,
  cxDropDownEdit, cxCheckBox, RM_Common, RM_Class, RM_e_Xls, RM_Dataset,
  RM_System, RM_GridReport, Menus, cxCalendar, cxButtonEdit, cxTextEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxDateRanges,
  dxBarBuiltInMenu, U_BaseList, dxScrollbarAnnotations, cxContainer, dxSkinsCore,
  dxSkinsDefaultPainters, cxProgressBar, cxGroupBox;

type
  TfrmWBNeedLook = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    PopupMenu1: TPopupMenu;
    Label3: TLabel;
    Label4: TLabel;
    CustName: TcxTextEdit;
    OrderNo: TcxTextEdit;
    ToolButton2: TToolButton;
    Panel2: TPanel;
    Panel3: TPanel;
    TV1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    TV1Column4: TcxGridDBColumn;
    TV1Column5: TcxGridDBColumn;
    TV1Column6: TcxGridDBColumn;
    TV1Column7: TcxGridDBColumn;
    TV1Column8: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    DS_2: TDataSource;
    CDS_3: TClientDataSet;
    CDS_1: TClientDataSet;
    CDS_2: TClientDataSet;
    DS_3: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGridPopupMenu3: TcxGridPopupMenu;
    cxGroupBox1: TcxGroupBox;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    TV2Column6: TcxGridDBColumn;
    TV2Column7: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    cxGroupBox2: TcxGroupBox;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    TV3Column1: TcxGridDBColumn;
    TV3Column2: TcxGridDBColumn;
    TV3Column3: TcxGridDBColumn;
    TV3Column4: TcxGridDBColumn;
    TV3Column5: TcxGridDBColumn;
    TV3Column6: TcxGridDBColumn;
    cxGrid3Level1: TcxGridLevel;
    TV1Column9: TcxGridDBColumn;
    TV1Column10: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure SXBatchNOChange(Sender: TObject);
    procedure FromCoNameChange(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure FromCoNamePropertiesChange(Sender: TObject);
  private
    canshu1, FStkName, canshu3: string;
    procedure InitGrid();
    procedure InitSUB();
    { Private declarations }
  public
    FWB_Code: string;
    { Public declarations }
  end;

var
  frmWBNeedLook: TfrmWBNeedLook;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

procedure TfrmWBNeedLook.FormDestroy(Sender: TObject);
begin
  inherited;
  frmWBNeedLook := nil;
end;

procedure TfrmWBNeedLook.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := caFree;
end;

procedure TfrmWBNeedLook.FormCreate(Sender: TObject);
begin
  inherited;
  canshu1 := Trim(self.fParameters1);
  FStkName := Trim(self.fParameters2);
  canshu3 := Trim(self.fParameters3);
end;

procedure TfrmWBNeedLook.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.*,B.*,OrdQty2=round(OrdQty*(1+ZS/100),2) ');
      sql.Add(',apqty=(select sum(WB_YarnLen) FROM Tat_WB_Card E WHERE E.GYSubid=B.SUBID AND WB_Type<>''稀轴'')');
      sql.Add(',Wapqty=round(OrdQty*(1+ZS/100),2)-ISNULL((select sum(WB_YarnLen) FROM Tat_WB_Card E WHERE E.GYSubid=B.SUBID AND WB_Type<>''稀轴''),0)');
      sql.Add(',SZQty=( SELECT SUM(WB_YarnLen) from Tat_WB_Card X WHERE  (WB_Type = ''经轴'' or  WB_Type = ''浆轴'' ) )');
      SQL.ADD('from Tat_Plan_Main A inner join Tat_Plan_Sub B on A.Mainid=B.MainId');
      SQL.Add('WHERE WB_Code=' + QuotedStr(FWB_Code));

      SQL.Add('ORDER BY FILLTIME DESC');

//      sql.Add('  where A.STKName =''经轴''  and A.IOFlag =''出库'' and A.WBNo like ''%JZ%'' and isnull(CLFlag,0)=0  ');
//      sql.Add('  and not EXISTS (select * from Tat_WB_IO X where X.STKName =''空轴''  and X.IOFlag =''入库'' and A.WBNO =X.WBNO)   ');
//      ShowMessage(Sql.text);
      Open;
    end;

    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmWBNeedLook.InitSUB();
begin
  try
    ADOQueryCmd.DisableControls;
    with ADOQueryCmd do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' EXEC P_TatWB_StkALL ');

      sql.Add(' select A.WBNo ORGWBNo,B.*,C.*,WB_YarnQty2= cast (WB_YarnQty AS Varchar)   ');
      sql.Add(' ,JSName=cast((SELECT  Y_Name +'';'' from  Tat_WB_Formula X inner join Tat_WB_Spec Y on X.WBSID=Y.WBSID where C.WB_Code=Y.WB_Code FOR XML PATH(''''))as VARCHAR(6000)  )');
      sql.Add(' from Tat_WB_Stk A inner join Tat_WB_IO B on A.STKTWIOID=B.TWIOID ');
      sql.Add(' inner join Tat_WB_Card C on C.WBCID=B.WBCID ');
      sql.Add(' where   B.IOFlag =''入库''  and isnull(B.stkName,'''')=''' + Trim('经轴') + '''');
      sql.Add('and WB_Code=''' + Trim(FWB_Code) + '''');
      SQL.Add('AND WB_Type<>''稀轴''');
//      sql.Add('  where A.STKName =''经轴''  and A.IOFlag =''出库'' and A.WBNo like ''%JZ%'' and isnull(CLFlag,0)=0  ');
//      sql.Add('  and not EXISTS (select * from Tat_WB_IO X where X.STKName =''空轴''  and X.IOFlag =''入库'' and A.WBNO =X.WBNO)   ');

      Open;
    end;

    SCreateCDS(ADOQueryCmd, CDS_2);
    SInitCDSData(ADOQueryCmd, CDS_2);
  finally
    ADOQueryCmd.EnableControls;
  end;
  try
    ADOQueryCmd.DisableControls;
    with ADOQueryCmd do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.*   ');
      sql.add(' ,jgType=(SELECT top 1 jgType from Tat_Wb_IO X where X.WBCID=A.WBCID and X.IOFlag =''入库'' and X.STKName =''经轴'' )');
      sql.add(' from Tat_WB_Card A ');
      sql.Add(' where 1=1');
      sql.Add('and WB_Code=''' + Trim(FWB_Code) + '''');
      SQL.Add('AND WB_Type<>''稀轴''');
      Open;
    end;

    SCreateCDS(ADOQueryCmd, CDS_3);
    SInitCDSData(ADOQueryCmd, CDS_3);
  finally
    ADOQueryCmd.EnableControls;
  end;

end;

procedure TfrmWBNeedLook.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
  INITSUB();
end;

procedure TfrmWBNeedLook.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  end;
end;

procedure TfrmWBNeedLook.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmWBNeedLook.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(trim(Self.Caption), Tv1, '经轴需求明细');
  ReadCxGrid(trim(Self.Caption), Tv2, '经轴需求明细');
  ReadCxGrid(trim(Self.Caption), Tv3, '经轴需求明细');
  InitGrid();
  Initsub();
end;

procedure TfrmWBNeedLook.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  end;
end;

procedure TfrmWBNeedLook.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmWBNeedLook.SXBatchNOChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmWBNeedLook.FromCoNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmWBNeedLook.FromCoNamePropertiesChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmWBNeedLook.ToolButton2Click(Sender: TObject);
begin

  WriteCxGrid(trim(Self.Caption), Tv1, '经轴需求明细');
  WriteCxGrid(trim(Self.Caption), Tv2, '经轴需求明细');
  WriteCxGrid(trim(Self.Caption), Tv3, '经轴需求明细');
end;

procedure TfrmWBNeedLook.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  ModalResult := 1;
end;

end.

