unit U_ZKJL;

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, 
   cxProgressBar, FrameDateSel, Clipbrd;

type
  TfrmZKJL = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    CDS_HZ: TClientDataSet;
    PopupMenu1: TPopupMenu;
    Label3: TLabel;
    Label4: TLabel;
    WBCID: TcxTextEdit;
    Filler: TcxTextEdit;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    frmFrameDateSel1: TfrmFrameDateSel;
    ToolButton1: TToolButton;
    ToolButton3: TToolButton;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    TV1Column4: TcxGridDBColumn;
    TV1Column5: TcxGridDBColumn;
    TV1Column6: TcxGridDBColumn;
    TV1Column7: TcxGridDBColumn;
    TV1Column8: TcxGridDBColumn;
    TV1Column9: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel2: TcxGridLevel;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Tv2Column1: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    Tv2Column3: TcxGridDBColumn;
    Tv2Column4: TcxGridDBColumn;
    Tv2Column5: TcxGridDBColumn;
    Tv2Column6: TcxGridDBColumn;
    TV1Column10: TcxGridDBColumn;
    TV1Column11: TcxGridDBColumn;
    TV1Column12: TcxGridDBColumn;
    TV1Column13: TcxGridDBColumn;
    TV1Column14: TcxGridDBColumn;
    TV1Column15: TcxGridDBColumn;
    TV1Column16: TcxGridDBColumn;
    TV1Column17: TcxGridDBColumn;
    TV1Column18: 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);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure TV1Column3CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
    procedure TV1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
  private
    canshu1, FStkName, canshu3: string;
    procedure InitGrid();

    { Private declarations }
  public

    { Public declarations }
  end;

var
  frmZKJL: TfrmZKJL;

implementation

uses
  U_DataLink, U_RTFun, U_LabelPrint;

{$R *.dfm}

procedure TfrmZKJL.FormDestroy(Sender: TObject);
begin
  inherited;
  frmZKJL := nil;
end;

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

procedure TfrmZKJL.FormCreate(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.EndDate.Date := SGetServerDate(ADOQueryTemp);
  frmFrameDateSel1.BegDate.Date := frmFrameDateSel1.EndDate.Date - 30;

  canshu1 := Trim(self.fParameters1);
  FStkName := Trim(self.fParameters2);
  canshu3 := Trim(self.fParameters3);
end;

procedure TfrmZKJL.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;

      sql.Add(' Select A.RowNo,D.PMID                                                                               ');
      sql.Add(' ,case when (select count(*) from Tat_WB_Card_Yarn X where X.WBCNO =D.PMID and X.RowNo=A.RowNo) > 0       ');
      sql.Add('      then (select BYIOID from Tat_WB_Card_Yarn X where X.WBCNO =D.PMID 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 =D.PMID and X.RowNo=A.RowNo) > 0     ');
      sql.Add('       then (select PH from Tat_WB_Card_Yarn X where X.WBCNO =D.PMID and X.RowNo=A.RowNo)               ');
      sql.Add('       else ''            '' end as PH                                                                      ');
      sql.Add(' ,A.Y_Name                                                                                               ');
      sql.Add(' ,PCID =(select top 1 PCID from Tat_Plan_OutPut X where X.TaskId=D.PMID ORDER BY Filltime desc   )       ');
      sql.Add(' into #V_RWPH                                                                                            ');
      sql.Add(' from Tat_Plan_Yarn A                                                                                     ');
      sql.Add(' INNER JOIN Tat_Plan_Main B on A.MainId =B.MainId                                                         ');
      sql.Add(' INNER JOIN Tat_Plan_Sub C on B.MainId =C.MainId                                                          ');
      sql.Add(' INNER JOIN Tat_Plan_Mach D on B.OrderNo =D.OrderNo                                                       ');

      sql.Add(' select AA.*,SL=round((1-1.00*JYQty/Qty)*100,2) from (');
      sql.Add(' select A.WBCID,A.WB_Code,A.WB_Name,A.WB_YarnQty,D.C_Code,D.C_Name,C.OrderNo');
      sql.Add(' ,B.Qty,B.Filler,B.Filltime');
      sql.Add(' ,JYQty=(select Sum(Meter) from Tat_Cloth_Inspect X INNER JOIN Tat_Plan_Card Y on X.PCID =Y.PCID where  Y.WBCID =A.WBCID)  ');
      sql.Add(' ,XBQty=(select Sum(ClothQty) from Tat_Plan_Card Y  where Y.WBCID =A.WBCID)  ');
      sql.Add(' ,JTH=cast((SELECT  DISTINCT Loom +'';'' from Tat_Plan_Card  X where X.WBCID=A.WBCID  FOR XML PATH(''''))as VARCHAR(600)  )  ');

      sql.Add(' ,JSPH=cast((SELECT  DISTINCT PH +'';'' from V_JSPH  X where X.WBCID=A.WBCID and isnull(PH,'''') <> '''' FOR XML PATH(''''))as VARCHAR(600)  )  ');
      sql.Add(' ,JSGG=cast((SELECT  DISTINCT Y_Name +'';'' from V_JSPH  X where X.WBCID=A.WBCID and isnull(PH,'''') <> '''' FOR XML PATH(''''))as VARCHAR(600)  )  ');

      sql.Add(' ,WSPH=cast((SELECT  DISTINCT PH +'';'' from #V_RWPH  X where X.PCID=A.WBCID and isnull(PH,'''') <> '''' FOR XML PATH(''''))as VARCHAR(600)  )  ');
      sql.Add(' ,WSGG=cast((SELECT  DISTINCT Y_Name +'';'' from #V_RWPH  X where X.PCID=A.WBCID and isnull(PH,'''') <> '''' FOR XML PATH(''''))as VARCHAR(600)  )  ');

      sql.Add(' ,WB_ProcessType=cast((SELECT  DISTINCT WB_ProcessType +'''' from V_JSPH  X where X.WBCID=A.WBCID  FOR XML PATH(''''))as VARCHAR(600)  )  ');
      sql.Add(' ,WBFtyName=cast((SELECT  DISTINCT WBFtyName +'''' from V_JSPH  X where X.WBCID=A.WBCID  FOR XML PATH(''''))as VARCHAR(600)  )  ');

      sql.Add(' from Tat_WB_Card A   left JOIN Tat_Plan_Main C on A.OrderNo=C.OrderNo  left JOIN Tat_Plan_Sub D on C.MainID=D.MainID                         ');
      sql.Add(' left JOIN Tat_Plan_OutPut B on A.WBCID=PCID and GlideName=''整经''     ');
      sql.add(' where A.Filltime>=''' + Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.BegDate.Date)) + '''');
      sql.Add(' and A.Filltime<''' + Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.enddate.Date + 1)) + '''');
      sql.Add(' )AA');

      sql.Add(' drop table #V_RWPH');
//      showmessage(Sql.text);
      Open;
    end;

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

procedure TfrmZKJL.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  InitGrid();
end;

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

procedure TfrmZKJL.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmZKJL.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(trim(Self.Caption), Tv1, '染色坯布仓库');
  InitGrid();
  frmFrameDateSel1.lbl1.caption := '轴卡安排时间';
end;

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

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

procedure TfrmZKJL.N1Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));

end;

procedure TfrmZKJL.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmZKJL.N3Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

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

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

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

procedure TfrmZKJL.ToolButton1Click(Sender: TObject);
var
  WSql: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  WSql := '';
  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while not Eof do
    begin
      if CDS_Main.fieldbyname('SSel').AsBoolean then
      begin
        if WSql <> '' then
        begin
          WSql := WSql + ',' + Trim(Trim(CDS_Main.fieldbyname('CIFID').AsString));
        end
        else
        begin
          WSql := Trim(Trim(CDS_Main.fieldbyname('CIFID').AsString));
        end;
      end;
      Next;
    end;
  end;
  CDS_Main.Locate('SSel', True, []);
  CDS_Main.EnableControls;
//  ShowMessage(WSql);

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'TatInsp2';
      FFiltration1 := WSql;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmLabelPrint.Free;
  end;

  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;

end;

procedure TfrmZKJL.ToolButton2Click(Sender: TObject);
begin
  WriteCxGrid(trim(Self.Caption), Tv1, '染色坯布仓库');
end;

procedure TfrmZKJL.ToolButton3Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

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

procedure TfrmZKJL.TV1Column3CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
begin
  if ARow1.Values[TV1Column3.Index] = ARow2.Values[TV1Column3.Index] then
    AAreEqual := True
  else
    AAreEqual := False;
end;

procedure TfrmZKJL.TV1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    Filtered := False;
    Close;
    SQL.Clear;
    sql.Add(' select X.*,DCG=cast((SELECT  knitter +'';'' from Tat_Cloth_knitter XX where knitter <> '''' and  XX.CIID=X.CIID FOR XML PATH(''''))as VARCHAR(600)  )   ');
    sql.Add(' from Tat_Cloth_Inspect X INNER JOIN Tat_Plan_Card Y on X.PCID =Y.PCID where 1=1');
    sql.Add(' and Y.WBCID=' + quotedstr(Trim(CDS_Main.FieldByName('WBCID').AsString)));
//   ShowMessage(sql.text);
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_2);
  SInitCDSData(ADOQueryTemp, CDS_2);
end;

end.

