unit U_WeaveSel;

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,
  dxSkinsCore, dxSkinsDefaultPainters, dxSkinOffice2013White, dxSkinSharpPlus,
  dxSkinWXI, cxPC;

type
  TfrmWeaveSel = 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;
    Label8: TLabel;
    Label1: TLabel;
    OrderNo: TEdit;
    ConNo: TEdit;
    CustName: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    v1Column31: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    cxStyle2: TcxStyle;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column10: 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 cxTabControl1Change(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    FCoType, FAuthority: string;
    FISSJ: string;
    { Public declarations }
  end;

var
  frmWeaveSel: TfrmWeaveSel;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

procedure TfrmWeaveSel.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 TfrmWeaveSel.FormDestroy(Sender: TObject);
begin
  inherited;
  frmWeaveSel := nil;
end;

procedure TfrmWeaveSel.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

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

procedure TfrmWeaveSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add(' select *,PZQty=(select count(*) from Tat_WB_Card X where X.OrderNo =A.OrderNo)');
      SQL.Add(',apqty=(select sum(WB_YarnLen) from Tat_WB_Card X where X.OrderNo =A.OrderNo)');
      sql.Add(',ZS=(select ZS from Bs_Cloth_Info X where X.C_Code =B.C_Code)  ');
      SQL.Add(',ORDQTY2=ROUND(ORDQTY*(100+isnull(zs,0))/100,2)');
      sql.Add(',ISSizing=(select ISSizing from Tat_WB_Spec E where E.WB_Code =B.WB_Code)  ');
      sql.Add(' from Tat_Plan_Main A inner join Tat_Plan_Sub B on A.MainId=B.MainId');

      //SQL.Add(' where  isnull(A.status,''0'')=''9'' ');
      sql.add('where 1=1');
      if FISSJ = '否' then   //整经
      begin
        sql.add('AND  (select ISSizing from Tat_WB_Spec E where E.WB_Code =B.WB_Code)=''否''');
        sql.add('AND  (select WB_TYPE from Tat_WB_Spec E where E.WB_Code =B.WB_Code)=''分条''');
        case cxTabControl1.TabIndex of
          0:
            begin

              sql.Add('and not exists (select 1 from Tat_WB_Card X where X.gyno = A.OrderNo)'); //未安排

//              sql.Add('and not exists (select 1 from Tat_WB_Card where gyno = A.OrderNo)'); //未安排
            end;
          1:
            begin

              sql.Add('and  exists (select 1 from Tat_WB_Card X where X.gyno = A.OrderNo)');    //已安排

//              sql.Add('and not exists (select 1 from Tat_WB_Card where gyno = A.OrderNo)');    //已安排
            end;
          2:
            begin
              sql.Add('and isnull(Status,''0'') not in (''11'',''22'') ');   //全部
//            sql.Add(' and A.FillTIme>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
//            sql.Add(' and A.FillTIme<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
            end;
        end;
      end
      else
      begin
        sql.add('AND  (select WB_TYPE from Tat_WB_Spec E where E.WB_Code =B.WB_Code)=''整浆并''');
        case cxTabControl1.TabIndex of
          0:
            begin

//              sql.Add('and not exists (select 1 from Tat_WB_Plan where gyno = A.OrderNo)'); //未安排

              sql.Add('and not exists (select 1 from Tat_WB_Card where gyno = A.OrderNo)'); //未安排
            end;
          1:
            begin

//              sql.Add('and not exists (select 1 from Tat_WB_Plan where gyno = A.OrderNo)');    //已安排

              sql.Add('and exists (select 1 from Tat_WB_Card where gyno = A.OrderNo)');    //已安排
            end;
          2:
            begin
              sql.Add('and isnull(Status,''0'') not in (''11'',''22'') ');   //全部
//            sql.Add(' and A.FillTIme>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
//            sql.Add(' and A.FillTIme<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
            end;
        end;
      end;
      sql.Add('and isnull(Status,''0'') =''9'' ');

      sql.add('order by A.FILLTIME DESC');
      Open;
    end;
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmWeaveSel.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 TfrmWeaveSel.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid('单位名称1' + Trim(FCoType), TV1, '自定义数据');
  InitGrid();
end;

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

procedure TfrmWeaveSel.TBSaveClick(Sender: TObject);
begin
  WriteCxGrid('单位名称1' + Trim(FCoType), TV1, '自定义数据');

end;

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

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

end.

