unit U_JZYarnSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, ToolWin, ComCtrls, U_BaseHelp, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGrid, DBClient, ADODB, ImgList, StdCtrls, ExtCtrls,
  cxTextEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxLookAndFeels,
  cxLookAndFeelPainters,   cxNavigator,
  dxDateRanges, dxBarBuiltInMenu, System.ImageList, U_BaseInput, cxCheckBox,
  cxCalendar,    
     
    
     
     
    
    
    
    
    
    
     
      
     
   
     
   dxScrollbarAnnotations, FrameDateSel, cxContainer,
  cxButtonEdit, cxMemo;

type
  TfrmJZYarnSel = class(TfrmBaseHelp)
    ToolBar1: TToolBar;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ToolButton1: TToolButton;
    ADOConnection1: TADOConnection;
    Panel1: TPanel;
    GPM_1: TcxGridPopupMenu;
    ImageList1: TImageList;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label3: TLabel;
    OrderNo: TEdit;
    Label1: TLabel;
    CustName: TEdit;
    Label4: TLabel;
    Y_Code: TEdit;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    TV1Column8: TcxGridDBColumn;
    TV1Column1: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    TV1Column4: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    TV1Column5: TcxGridDBColumn;
    TV1Column7: TcxGridDBColumn;
    TV1Column9: TcxGridDBColumn;
    TV1Column10: TcxGridDBColumn;
    TV1Column11: TcxGridDBColumn;
    TV1Column12: TcxGridDBColumn;
    TV1Column13: TcxGridDBColumn;
    TV1Column14: TcxGridDBColumn;
    TV1Column16: TcxGridDBColumn;
    TV1Column15: TcxGridDBColumn;
    TV1Column23: TcxGridDBColumn;
    TV1Column17: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column6: TcxGridDBColumn;
    TV1Column18: TcxGridDBColumn;
    TV1Column19: TcxGridDBColumn;
    TV1Column20: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Frame11: TfrmFrameDateSel;
    ToolButton2: TToolButton;
    Label2: TLabel;
    Label6: TLabel;
    WBCNO: TcxTextEdit;
    cxTextEdit1: TcxTextEdit;
    CDS_2: TClientDataSet;
    GPM_2: TcxGridPopupMenu;
    DS_2: TDataSource;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    v1Column18: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    v1SPName: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    TV2Column6: TcxGridDBColumn;
    TV2Column7: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column1: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    TV2Column8: TcxGridDBColumn;
    TV2Column9: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ADO_2: TADOQuery;
    TV2Column10: TcxGridDBColumn;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure TV1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
  private
    procedure InitGrid();
    { Private declarations }
  public
    FCoType, FAuthority: string;
    { Public declarations }
  end;

var
  frmJZYarnSel: TfrmJZYarnSel;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

procedure TfrmJZYarnSel.FormCreate(Sender: TObject);
begin
  inherited;
  try
    with ADOConnection1 do
    begin
      Connected := false;
      ConnectionString := DConString;
      Connected := true;
    end;
    ADOQueryBaseCmd.Connection := ADOConnection1;
    ADOQueryBaseTemp.Connection := ADOConnection1;
  except
    application.MessageBox('网络连接失败！', '提示信息');
  end;
end;

procedure TfrmJZYarnSel.FormDestroy(Sender: TObject);
begin
  inherited;
  frmJZYarnSel := nil;
end;

procedure TfrmJZYarnSel.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := cahide;
end;

procedure TfrmJZYarnSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;

      sql.Add('select A.*,PZQty=(select count(*) from [dbo].[F_Tool_SplitString]( A.WBCIDS, '';'' ) )  ');
      sql.Add(',ZJQty=(select count(*) from [dbo].[F_Tool_SplitString]( A.WBCIDS, '';'' ) X  INNER JOIN Tat_Plan_OutPut Y on X.RTValue = Y.PCID where Y.GlideName =''整经'')  ');
      sql.Add(',RKQty=(select count(*) from [dbo].[F_Tool_SplitString]( A.WBCIDS, '';'' ) X  INNER JOIN Tat_WB_IO Y on X.RTValue = Y.WBCID where Y.IOFlag =''入库'' and  isnull(StkName,'''')=''经轴'')  ');
      sql.Add(',C_CodeName=(select Y.C_Name from Tat_Plan_Main X INNER JOIN Tat_Plan_Sub Y on X.MainId = Y.MainId where X.OrderNo=A.OrderNo )  ');
      sql.Add(',OrderNoS=SUBSTRING(CAST((select distinct '','' +Y.OrderNo from Tat_WB_Card_Link X inner join Tat_plan_Main Y on X.mainid=Y.mainid where X.WBCNO=A.WBCNO for xml path('''') ) AS VARCHAR(100)) , 2, 100)         ');
      sql.Add(',ConNoS=SUBSTRING(CAST((select distinct '','' +Y.ConNo from Tat_WB_Card_Link X inner join Tat_plan_Sub Y on X.Subid=Y.Subid where X.WBCNO=A.WBCNO for xml path('''') ) AS VARCHAR(100)) , 2, 100)         ');
      sql.Add(',C_NameS=SUBSTRING(CAST((select distinct '','' +Y.C_Name from Tat_WB_Card_Link X inner join Tat_plan_Sub Y on X.Subid=Y.Subid where X.WBCNO=A.WBCNO for xml path('''') ) AS VARCHAR(100)) , 2, 100)         ');
      sql.Add(',C_CodeoS=SUBSTRING(CAST((select distinct '','' +Y.C_Code from Tat_WB_Card_Link X inner join Tat_plan_Sub Y on X.Subid=Y.Subid where X.WBCNO=A.WBCNO for xml path('''') ) AS VARCHAR(100)) , 2, 100)         ');
      sql.Add(',HZWB_YarnLen=(select Sum(WB_YarnLen) from Tat_WB_Card X where A.WBCNO =X.WBCNO) ');
      sql.Add(' from  V_Tat_WB_CardGrop A ');
      sql.Add(' where  1=1');
      sql.add(' and A.WB_Date>=''' + Trim(FormatDateTime('yyyy-MM-dd', Frame11.BegDate.Date)) + '''');
      sql.Add(' and A.WB_Date<''' + Trim(FormatDateTime('yyyy-MM-dd', Frame11.enddate.Date + 1)) + '''');

//      sql.Add(' select Y_Spec=(select Y_Spec from Bs_Yarn_Info X where X.Y_Code =C.Y_Code),C.*,A.*,B.*');
//      sql.Add(' from Tat_Plan_Main A  ');
//      sql.Add(' inner join Tat_Plan_Sub B on A.MainId=B.MainId ');
//      sql.Add(' INNER JOIN Tat_Plan_Yarn C on A.MainId =C.MainId ');
//      sql.Add(' where isnull(C.Y_Code,'''') <> '''' ');
//      sql.Add(' and  isnull(A.status,''0'') in (''0'',''9'') ');
//      ShowMessage(sql.Text);
      Open;
    end;
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmJZYarnSel.OrderNoChange(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 TfrmJZYarnSel.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid('纱线选择1' + Trim(FCoType), TV1, '自定义数据');
  ReadCxGrid('纱线选择2' + Trim(FCoType), TV2, '自定义数据');

  Frame11.EndDate.Date := SGetServerDate(ADOQueryTemp);
  Frame11.BegDate.Date := Frame11.EndDate.Date - 30;
  InitGrid();
end;

procedure TfrmJZYarnSel.TBCloseClick(Sender: TObject);
begin
  inherited;
  Close;
end;

procedure TfrmJZYarnSel.TBSaveClick(Sender: TObject);
begin
  WriteCxGrid('纱线选择1' + Trim(FCoType), TV1, '自定义数据');
  WriteCxGrid('纱线选择2' + Trim(FCoType), TV2, '自定义数据');
end;

procedure TfrmJZYarnSel.ToolButton1Click(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmJZYarnSel.ToolButton2Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmJZYarnSel.Tv1DblClick(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmJZYarnSel.TV1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADO_2 do
  begin
    Close;
    sql.Clear;

    sql.Add(' select Y_Spec=(select Y_Spec from Bs_Yarn_Info X where X.Y_Code = A.Y_Code),A.* ');

    sql.Add(' ,case when (select count(*) from Tat_WB_Card_Yarn X where X.WBCNO =''' + Trim(CDS_1.fieldbyname('WBCNO').AsString) + ''' and X.RowNo=A.RowNo) > 0 ');
    sql.Add('       then (select BYIOID from Tat_WB_Card_Yarn X where X.WBCNO =''' + Trim(CDS_1.fieldbyname('WBCNO').AsString) + ''' and X.RowNo=A.RowNo)  ');
    sql.Add('       else ''            '' end as BYIOID');

    sql.Add(' ,case when (select count(*) from Tat_WB_Card_Yarn X where X.WBCNO =''' + Trim(CDS_1.fieldbyname('WBCNO').AsString) + '''and X.RowNo=A.RowNo) > 0 ');
    sql.Add('       then (select PH from Tat_WB_Card_Yarn X where X.WBCNO =''' + Trim(CDS_1.fieldbyname('WBCNO').AsString) + ''' and X.RowNo=A.RowNo)  ');
    sql.Add('       else ''            '' end as PH');

    sql.Add(' from Tat_Plan_Yarn A INNER JOIN Tat_Plan_Main B on A.MainId =B.MainId  ');
    sql.Add(' where A.YRType=''经丝'' and  isnull(A.Y_Code,'''') <>''''  and B.OrderNo = ''' + Trim(CDS_1.fieldbyname('OrderNo').AsString) + '''');

//    showmessage(sql.text);
    Open;
  end;
  SCreateCDS(ADO_2, CDS_2);
  SInitCDSData(ADO_2, CDS_2);
end;

end.

