unit U_ItemTestList;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.ComCtrls, Vcl.ToolWin, cxGraphics, cxControls, cxLookAndFeels, cxContainer,
  cxEdit, dxLayoutcxEditAdapters, dxBarBuiltInMenu, cxPC, dxLayoutContainer,
  cxClasses, cxTextEdit, dxLayoutControl, cxStyles, cxCustomData, cxFilter,
  cxData, cxDataStorage, cxNavigator, dxDateRanges, dxScrollbarAnnotations,
  Data.DB, cxDBData, cxCalendar, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGridCustomView, cxGrid, U_BaseList,
  Data.Win.ADODB, cxGridCustomPopupMenu, cxGridPopupMenu, Datasnap.DBClient,
  cxCheckBox, cxButtonEdit, cxSplitter, dxCore, cxDateUtils, cxMaskEdit,
  cxDropDownEdit, cxProgressBar, Vcl.Menus, RM_Common, RM_Class, RM_GridReport,
  RM_Dataset, cxLookAndFeelPainters, Vcl.ExtCtrls;

type
  TfrmItemTestList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBAdd: TToolButton;
    btnCopy: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    Tdjsz: TToolButton;
    TBPrint: TToolButton;
    TBCK: TToolButton;
    ToolButton4: TToolButton;
    TWC: TToolButton;
    TNowc: TToolButton;
    Tbcgs: TToolButton;
    TBClose: TToolButton;
    dxLayoutControl_query: TdxLayoutControl;
    ConNo: TcxTextEdit;
    BuyConNo: TcxTextEdit;
    dxLayoutControl_queryGroup_Root: TdxLayoutGroup;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabControl1: TcxTabControl;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    Tv1ConNo: TcxGridDBColumn;
    Tv1PB_Spec: TcxGridDBColumn;
    Tv1Color: TcxGridDBColumn;
    Tv1Con_Width: TcxGridDBColumn;
    Tv1Con_GramWeight: TcxGridDBColumn;
    Tv1Con_DSLight: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    dxLayoutItem_ywy: TdxLayoutItem;
    dxLayoutItem_pm: TdxLayoutItem;
    ywy: TcxTextEdit;
    C_Name: TcxTextEdit;
    GPM_1: TcxGridPopupMenu;
    ADOQueryMain: TADOQuery;
    DataSource1: TDataSource;
    Order_Main: TClientDataSet;
    cxGrid5: TcxGrid;
    TV5: TcxGridDBTableView;
    TV5SSel: TcxGridDBColumn;
    V2BuyConNo: TcxGridDBColumn;
    V2C_StyleNo: TcxGridDBColumn;
    V2C_Name: TcxGridDBColumn;
    V2C_EName: TcxGridDBColumn;
    v1C_ColorNo: TcxGridDBColumn;
    v1C_Color: TcxGridDBColumn;
    V2PRTCode: TcxGridDBColumn;
    TV5C_EColor: TcxGridDBColumn;
    v1OrdQty: TcxGridDBColumn;
    v1OrdUnit: TcxGridDBColumn;
    TV5OrdSNote: TcxGridDBColumn;
    v1C_Pattern: TcxGridDBColumn;
    V2C_Width: TcxGridDBColumn;
    V2C_GramWeight: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    v1SOrddefstr2: TcxGridDBColumn;
    V2C_Spec: TcxGridDBColumn;
    V2JYRoll: TcxGridDBColumn;
    V2Column8: TcxGridDBColumn;
    V2Column9: TcxGridDBColumn;
    V2Column10: TcxGridDBColumn;
    V2Column11: TcxGridDBColumn;
    V2Column12: TcxGridDBColumn;
    V2Column13: TcxGridDBColumn;
    V2Column14: TcxGridDBColumn;
    TV5Column1: TcxGridDBColumn;
    TV5Column2: TcxGridDBColumn;
    TV5Column3: TcxGridDBColumn;
    TV5Column4: TcxGridDBColumn;
    TV5Column5: TcxGridDBColumn;
    TV5Column6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxSplitter1: TcxSplitter;
    endDate: TcxDateEdit;
    begdate: TcxDateEdit;
    dxLayoutItem_beg: TdxLayoutItem;
    dxLayoutItem_end: TdxLayoutItem;
    dxLayoutItem_khdh: TdxLayoutItem;
    dxLayoutItem_hth: TdxLayoutItem;
    DataSource2: TDataSource;
    ADOQuerySub: TADOQuery;
    ORDER_SUB: TClientDataSet;
    GPM_2: TcxGridPopupMenu;
    Tcdgl: TToolButton;
    Ttoexcel: TToolButton;
    DataSource3: TDataSource;
    ADOQueryMxList: TADOQuery;
    cxProgressBar1: TcxProgressBar;
    GPM_3: TcxGridPopupMenu;
    cds_mxlist: TClientDataSet;
    Tfilter: TToolButton;
    Tbqyl: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ADOQueryPrint: TADOQuery;
    RM1: TRMGridReport;
    RMDB_1: TRMDBDataSet;
    Tupdateto: TToolButton;
    dxLayoutItem_batchno: TdxLayoutItem;
    batchNo: TcxTextEdit;
    FtyPCId: TcxTextEdit;
    dxLayoutItem_FtyPCId: TdxLayoutItem;
    TV5FtyPCId: TcxGridDBColumn;
    TV5batchNo: TcxGridDBColumn;
    TV5rKRoll: TcxGridDBColumn;
    TV5rKJZ: TcxGridDBColumn;
    TV5rKQty: TcxGridDBColumn;
    TV5rKMZ: TcxGridDBColumn;
    TV5Column7: TcxGridDBColumn;
    TV5Column8: TcxGridDBColumn;
    TV5Column9: TcxGridDBColumn;
    TV5Column10: TcxGridDBColumn;
    TV5Column11: TcxGridDBColumn;
    TV5Column12: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    Tv1SSel: TcxGridDBColumn;
    Tv1RollNo: TcxGridDBColumn;
    Tv1Quantity: TcxGridDBColumn;
    v1C_Code: TcxGridDBColumn;
    Tv1GramWeight: TcxGridDBColumn;
    Tv1FullWidth: TcxGridDBColumn;
    Tv1CutttableWidth: TcxGridDBColumn;
    Tv1DryColor: TcxGridDBColumn;
    Tv1WetColor: TcxGridDBColumn;
    Tv1WarpDensity: TcxGridDBColumn;
    Tv1WeftDensity: TcxGridDBColumn;
    Tv1WarpTaer: TcxGridDBColumn;
    Tv1WeftTear: TcxGridDBColumn;
    Tv1WarpTensile: TcxGridDBColumn;
    Tv1WeftTensile: TcxGridDBColumn;
    Tv1WarpShrink: TcxGridDBColumn;
    Tv1WeftShrink: TcxGridDBColumn;
    Tv1Skew: TcxGridDBColumn;
    Tv1WaterProof: TcxGridDBColumn;
    Tv1WarpDamage: TcxGridDBColumn;
    v1C_Spec: TcxGridDBColumn;
    v1AttName: TcxGridDBColumn;
    Tv1WeftDamage: TcxGridDBColumn;
    Tv1PH: TcxGridDBColumn;
    Tv1SLabVolume: TcxGridDBColumn;
    Tv1SLabPackage: TcxGridDBColumn;
    Tv1PToP: TcxGridDBColumn;
    Tv1WaterFast: TcxGridDBColumn;
    Tv1PillResist: TcxGridDBColumn;
    Tv1AbsorpWater: TcxGridDBColumn;
    Tv1DripDiff: TcxGridDBColumn;
    Tv1JointBubble: TcxGridDBColumn;
    Tv1StirFloat: TcxGridDBColumn;
    Tv1Di84: TcxGridDBColumn;
    Tv1Pao84: TcxGridDBColumn;
    Tv1JointDye: TcxGridDBColumn;
    Tv1SeamSlip: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    procedure TBCloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure TBAddClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TbcgsClick(Sender: TObject);
    procedure btnCopyClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TdjszClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBCKClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure TWCClick(Sender: TObject);
    procedure TNowcClick(Sender: TObject);
    procedure TcdglClick(Sender: TObject);
    procedure TtoexcelClick(Sender: TObject);
    procedure cxPageControl1Click(Sender: TObject);
    procedure OrderNoPropertiesEditValueChanged(Sender: TObject);
    procedure TfilterClick(Sender: TObject);
    procedure OrderNoPropertiesChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TupdatetoClick(Sender: TObject);
    procedure ButtonEdit(Sender: TObject; AButtonIndex: Integer);
  private
    canshu1, canshu2: string;
    fIsLoad: Boolean;
    procedure InitGrid();
    procedure InitMxGrid();
    procedure InitForm();
    procedure InitSub();
    function DelData(): Boolean;
    function DelSubData(): Boolean;
    procedure threadLoadform();
  public
  end;

var
  frmItemTestList: TfrmItemTestList;

implementation

uses
  U_DataLink, U_RTFun, U_TradePlanInPut, U_globalVar, U_FormLayOutDesign,
  U_TradeInspSet, U_AttachmentUpload, U_LabelPrint, U_ZDYHelp, U_iniParam,
  U_UptPlanDataToRoll, U_PictureUpload, U_ItemTestInPut;
{$R *.dfm}

procedure TfrmItemTestList.btnCopyClick(Sender: TObject);
begin
  if cxPageControl1.ActivePageIndex = 0 then
  begin
    if Order_Main.IsEmpty then
      Exit;

    try
      frmItemTestInPut := TfrmItemTestInPut.Create(Application);
      with frmItemTestInPut do
      begin
        PState := 1;
        CopyInt := 99;
        FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
        fCopyMainId := FMainId;
        canshu1 := Self.canshu1;
        if ShowModal = 1 then
        begin

        end;
      end;
    finally
      frmItemTestInPut.Free;
    end;
  end
  else
  begin
    if cds_mxlist.IsEmpty then
      Exit;

    try
      frmItemTestInPut := TfrmItemTestInPut.Create(Application);
      with frmItemTestInPut do
      begin
        PState := 1;
        CopyInt := 99;
        FMainId := Trim(Self.cds_mxlist.fieldbyname('MainId').AsString);
        fCopyMainId := FMainId;
        canshu1 := Self.canshu1;
        if ShowModal = 1 then
        begin

        end;
      end;
    finally
      frmItemTestInPut.Free;
    end;
  end;

end;

procedure TfrmItemTestList.cxPageControl1Click(Sender: TObject);
begin
  if cxPageControl1.ActivePageIndex = 0 then
  begin
    dxLayoutItem_khdh.Visible := false;
    dxLayoutItem_pm.Visible := false;
    dxLayoutItem_batchno.Visible := false;
    dxLayoutItem_FtyPCId.Visible := false;
  end
  else
  begin
    dxLayoutItem_khdh.Visible := true;
    dxLayoutItem_pm.Visible := true;
    dxLayoutItem_batchno.Visible := true;
    dxLayoutItem_FtyPCId.Visible := true;
  end;
  TBRafresh.Click
end;

procedure TfrmItemTestList.cxTabControl1Change(Sender: TObject);
begin
  TBRafresh.Click

end;

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

procedure TfrmItemTestList.FormCreate(Sender: TObject);
begin
  inherited;
  canshu1 := Trim(Self.fParameters1);
  cxGrid2.Height := 250;
  cxGrid1.Align := alClient;
  cxPageControl1.Align := alClient;
  cxPageControl1.ActivePageIndex := 0;
//  cxGrid3.Align := alClient;

//  if cxPageControl1.ActivePageIndex = 0 then begin
//    dxLayoutItem_khdh.Visible := false;
//    dxLayoutItem_pm.Visible := false;
//    dxLayoutItem_batchno.Visible:=false;
//    dxLayoutItem_FtyPCId.Visible:=false;
//  end
//  else begin dxLayoutItem_khdh.Visible := true;
//    dxLayoutItem_pm.Visible := true;
//    dxLayoutItem_batchno.Visible:=true;
//    dxLayoutItem_FtyPCId.Visible:=true;
//  end;
end;

procedure TfrmItemTestList.FormDestroy(Sender: TObject);
begin
  inherited;
  frmItemTestList := nil;
end;

procedure TfrmItemTestList.FormShow(Sender: TObject);
//var
 // mdesignCode :string;
begin
  inherited;
  Tbcgs.Visible := gIsCanDesign;
  endDate.Date := SGetServerDate(ADOQueryBaseTemp);
  begdate.Date := endDate.Date - 10;
  // Scaleby(Screen.Width,1024);
  InitForm();

//  addQryContionByLay(ADOQueryBaseTemp,fformId,'cxgrid1',dxLayoutControl_query,7);
//  addQryContionByLay(ADOQueryBaseTemp,fformId,'cxgrid3',dxLayoutControl_query,7);
 //threadLoadform();
//  mdesignCode := fDllFileName +'|'+self.name+ '|' + intTostr(fformId) + '|' + 'cxgrid1';
 // addQryContionByLay(ADOQueryBaseTemp,fformId,'cxgrid1',dxLayoutControl_query,7);

 //application.ProcessMessages;

end;

procedure TfrmItemTestList.TBAddClick(Sender: TObject);
begin
  try
    frmItemTestInPut := TfrmItemTestInPut.Create(Application);
    with frmItemTestInPut do
    begin
      PState := 0;
      FMainId := '';
      canshu1 := Self.canshu1;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmItemTestInPut.Free;
  end;

end;

procedure TfrmItemTestList.TBCKClick(Sender: TObject);
var
  mMainId: string;
begin
  if cxPageControl1.ActivePageIndex = 0 then
  begin
    if Order_Main.IsEmpty then
      Exit;
    mMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
  end
  else
  begin
    if cds_mxlist.IsEmpty then
      Exit;
    mMainId := Trim(Self.cds_mxlist.fieldbyname('MainId').AsString);
  end;

  try
    frmTradePlanInPut := TfrmTradePlanInPut.Create(Application);
    with frmTradePlanInPut do
    begin
      PState := 5;
      FMainId := Trim(mMainId);
      ToolBar2.Visible := false;
      TBSave.Visible := false;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmTradePlanInPut.Free;
  end;

end;

procedure TfrmItemTestList.TBCloseClick(Sender: TObject);
begin
  inherited;
  close;
end;

procedure TfrmItemTestList.TBDelClick(Sender: TObject);
begin
  if cxPageControl1.ActivePageIndex = 0 then
  begin
    if Order_Main.IsEmpty then
      Exit;
    if (canshu1 <> '管理') and (canshu1 <> '高权限') then
    begin
      if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
      begin
        Application.MessageBox('不能操作他人的数据!', '提示', 0);
        Exit;
      end;
    end;

    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;

//    with ADOQueryBaseTemp do
//    begin
//      close;
//      sql.Clear;
//      sql.Add('select * from Trade_Cloth_Inspect where isnull(mainID,'''')<>'''' and mainID=''' + Trim(Order_Main.fieldbyname('mainID').AsString) + '''');
//      Open;
//    end;
//    if not ADOQueryBaseTemp.IsEmpty then
//    begin
//      Application.MessageBox('已检验不能删除数据!', '提示', 0);
//      Exit;
//    end;

    if Trim(Order_Main.fieldbyname('MainId').AsString) <> '' then
    begin
      DelData()
    end;
    Order_Main.Delete;
  end
  else
  begin
    if cds_mxlist.IsEmpty then
      Exit;
    if (canshu1 <> '管理') and (canshu1 <> '高权限') then
    begin
      if Trim(cds_mxlist.fieldbyname('Filler').AsString) <> Trim(DName) then
      begin
        Application.MessageBox('不能操作他人的数据!', '提示', 0);
        Exit;
      end;
    end;

    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;

//    with ADOQueryBaseTemp do
//    begin
//      close;
//      sql.Clear;
//      sql.Add('select * from Trade_Cloth_Inspect where isnull(mainID,'''')<>'''' and mainID=''' + Trim(cds_mxlist.fieldbyname('mainID').AsString) + '''');
//      Open;
//    end;
//    if not ADOQueryBaseTemp.IsEmpty then
//    begin
//      Application.MessageBox('已检验不能删除数据!', '提示', 0);
//      Exit;
//    end;

    if Trim(cds_mxlist.fieldbyname('subId').AsString) <> '' then
    begin
      DelSubData()
    end;
//    cds_mxlist.Delete;

  end;

end;

procedure TfrmItemTestList.TBEditClick(Sender: TObject);
begin
  if cxPageControl1.ActivePageIndex = 0 then
  begin
    if Order_Main.IsEmpty then
      Exit;

    if (canshu1 <> '管理') and (canshu1 <> '高权限') then
    begin
      if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
      begin
        Application.MessageBox('不能操作他人的数据!', '提示', 0);
        Exit;
      end;
    end;

    try
      frmItemTestInPut := TfrmItemTestInPut.Create(Application);
      with frmItemTestInPut do
      begin
        PState := 1;
        FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
//        FOrderNo := Trim(Self.Order_Main.fieldbyname('OrderNo').AsString);
        canshu1 := Self.canshu1;
        if ShowModal = 1 then
        begin

        end;
      end;
    finally
      frmItemTestInPut.Free;
    end;
  end
  else
  begin
    if cds_mxlist.IsEmpty then
      Exit;

    if (canshu1 <> '管理') and (canshu1 <> '高权限') then
    begin
      if Trim(cds_mxlist.fieldbyname('Filler').AsString) <> Trim(DName) then
      begin
        Application.MessageBox('不能操作他人的数据!', '提示', 0);
        Exit;
      end;
    end;

    try
      frmItemTestInPut := TfrmItemTestInPut.Create(Application);
      with frmItemTestInPut do
      begin
        PState := 1;
        FMainId := Trim(Self.cds_mxlist.fieldbyname('MainId').AsString);
        FOrderNo := Trim(Self.cds_mxlist.fieldbyname('OrderNo').AsString);
        canshu1 := Self.canshu1;
        if ShowModal = 1 then
        begin

        end;
      end;
    finally
      frmItemTestInPut.Free;
    end;
  end;

end;

procedure TfrmItemTestList.TBPrintClick(Sender: TObject);
var
  WSql: string;
  mxIds: string;
begin
  mxIds := '';
  if cxPageControl1.ActivePageIndex = 0 then
  begin
    if Order_Main.IsEmpty then
      Exit;
    WSql := (Trim(Order_Main.fieldbyname('MainId').AsString));
    ORDER_SUB.First;
    while not ORDER_SUB.eof do
    begin
      if ORDER_SUB.FieldByName('SSel').AsBoolean then
        mxIds := trim(ORDER_SUB.FieldByName('subId').AsString) + ',' + mxIds;
      ORDER_SUB.Next;
    end;
  end
  else
  begin
    if cds_mxlist.IsEmpty then
      Exit;
    WSql := (Trim(cds_mxlist.fieldbyname('MainId').AsString));
  end;

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'ItemTestPrint';
      FFiltration1 := WSql;
      FFiltrationSubs := mxIds;
      if ShowModal = 1 then
      begin
        // Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;

end;

procedure TfrmItemTestList.TBRafreshClick(Sender: TObject);
begin
  if cxPageControl1.ActivePageIndex = 0 then
    InitGrid()
  else
//    InitMxGrid();
end;

procedure TfrmItemTestList.TcdglClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'WFBCD';
      flagname := '疵点';
      fnote := true;
      V1Note.Caption := '英文名称';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmItemTestList.TdjszClick(Sender: TObject);
var
  mOrderNo, mMainId: string;
begin
  if cxPageControl1.ActivePageIndex = 0 then
  begin
    if Order_Main.IsEmpty then
      Exit;
    mOrderNo := Trim(Order_Main.fieldbyname('orderNo').AsString);
    mMainId := Trim(Order_Main.fieldbyname('mainId').AsString);
  end
  else
  begin
    if cds_mxlist.IsEmpty then
      Exit;
    mOrderNo := Trim(cds_mxlist.fieldbyname('orderNo').AsString);
    mMainId := Trim(cds_mxlist.fieldbyname('mainId').AsString);
  end;
  try
    frmTradeInspSet := TfrmTradeInspSet.Create(Application);
    with frmTradeInspSet do
    begin
     // caption:=mOrderNo+'标签设置'
      FDataID := mMainId;

      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmTradeInspSet.Free;
  end;

end;

procedure TfrmItemTestList.TNowcClick(Sender: TObject);
var
  mOrderNo, mMainId: string;
begin
  if cxPageControl1.ActivePageIndex = 0 then
  begin
    if Order_Main.IsEmpty then
      Exit;
    if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
    begin
      Application.MessageBox('不能操作他人的数据!', '提示', 0);
      Exit;
    end;
    mOrderNo := Trim(Order_Main.fieldbyname('orderNo').AsString);
    mMainId := Trim(Order_Main.fieldbyname('mainId').AsString);
  end
  else
  begin
    if cds_mxlist.IsEmpty then
      Exit;
    if Trim(cds_mxlist.fieldbyname('Filler').AsString) <> Trim(DName) then
    begin
      Application.MessageBox('不能操作他人的数据!', '提示', 0);
      Exit;
    end;
    mOrderNo := Trim(cds_mxlist.fieldbyname('orderNo').AsString);
    mMainId := Trim(cds_mxlist.fieldbyname('mainId').AsString);
  end;
  if Application.MessageBox('是否核实好数据？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with ADOQueryBaseCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Trade_Plan_Main SET status=''9'' ');
      sql.Add('where mainID=' + quotedstr(mMainId));
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(Trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(Trim(Self.Caption)));
      sql.Add(',' + quotedstr(Trim('计划单撤销完成')));
      sql.Add(',' + quotedstr(Trim('计划单号：' + Trim(mOrderNo))));
      sql.Add(',' + quotedstr(Trim('成功')));
      sql.Add(')');
      execsql;
    end;
    Application.MessageBox('计划单撤销完成成功！', '提示信息');
    TBRafresh.Click;
  except
    Application.MessageBox('计划单撤销完成失败！', '提示信息', 0);
  end;

end;

procedure TfrmItemTestList.TbcgsClick(Sender: TObject);
begin
//  if cxPageControl1.ActivePageIndex = 1 then
//  begin
//    WriteCxGrid('物测报告明细列表T3', tv3, gDllFileCaption);
//  end
//  else
//  begin
    WriteCxGrid('物测报告列表T1', Tv1, gDllFileCaption);
    WriteCxGrid('物测报告列表T2', TV2, gDllFileCaption);
//  end;
  if gIsCanDesign then
  begin
    saveLayOut(application, dxLayoutControl_query, ADOQueryBaseCmd, pwidechar(fDllFileName + '|' + Self.name + '|' + dxLayoutControl_query.name + '.ini'));
  end;
end;

procedure TfrmItemTestList.TtoexcelClick(Sender: TObject);
begin
  if (cxPageControl1.ActivePageIndex = 0) then
  begin
    if cxGrid2.Focused then
    begin
      TcxGridToExcel(Self.Caption, cxGrid2);
    end
    else
    begin
      TcxGridToExcel(Self.Caption + '-明细', cxGrid1);
    end;
  end
//  else
//  begin
//    TcxGridToExcel(Self.Caption + '-订单明细', cxGrid3);
//  end;
end;

procedure TfrmItemTestList.TfilterClick(Sender: TObject);
begin
  inherited;
  conno.Tag := 2;
  if cxPageControl1.ActivePageIndex = 0 then
  begin
    try
      Tv1.BeginUpdate();
      if ADOQueryMain.Active then
      begin
        SDofilter(ADOQueryMain, SLGetFilters(dxLayoutControl_query, 1, 2));
        Order_Main.EmptyDataSet;
        SInitCDSData(ADOQueryMain, Order_Main);
      end;
    finally
      Tv1.EndUpdate;
    end;
  end
  else
  begin
//    try
//      tv3.BeginUpdate();
//      if ADOQueryMxList.Active then
//      begin
//        SDofilter(ADOQueryMxList, SLGetFilters(dxLayoutControl_query, 1, 2));
//        cds_mxlist.EmptyDataSet;
//        SInitCDSData(ADOQueryMxList, cds_mxlist);
//      end;
//    finally
//      tv3.EndUpdate;
//    end;
  end;
end;

procedure TfrmItemTestList.TupdatetoClick(Sender: TObject);
var
  mOrderNo, mMainId: string;
begin
  if cxPageControl1.ActivePageIndex = 0 then
  begin
    if Order_Main.IsEmpty then
      Exit;
    mOrderNo := Trim(Order_Main.fieldbyname('orderNo').AsString);
    mMainId := Trim(Order_Main.fieldbyname('mainId').AsString);
  end
  else
  begin
    if cds_mxlist.IsEmpty then
      Exit;
    mOrderNo := Trim(cds_mxlist.fieldbyname('orderNo').AsString);
    mMainId := Trim(cds_mxlist.fieldbyname('mainId').AsString);
  end;
  frmUptPlanDataToRoll := TfrmUptPlanDataToRoll.create(Application);
  with frmUptPlanDataToRoll do
  begin
    fMainId := mMainId;
    cxTextEdit1.Text := trim(mOrderNo);
    showModal;
    free;
  end;

end;

procedure TfrmItemTestList.ToolButton4Click(Sender: TObject);
var
  mMainId: string;
begin
  if cxPageControl1.ActivePageIndex = 0 then
  begin
    if Order_Main.IsEmpty then
      Exit;
    mMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
  end
  else
  begin
    if cds_mxlist.IsEmpty then
      Exit;
    mMainId := Trim(Self.cds_mxlist.fieldbyname('MainId').AsString);
  end;
  try
    frmAttachmentUpload := TfrmAttachmentUpload.Create(Application);
    with frmAttachmentUpload do
    begin
      if Trim(canshu1) <> '查询' then
        FEditAuthority := true;
      fkeyNO := Trim(mMainId);
      fType := '指示单';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmAttachmentUpload.Free;
  end;

end;

procedure TfrmItemTestList.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  if fIsLoad then
    InitSub();

end;

procedure TfrmItemTestList.TWCClick(Sender: TObject);
begin
  if cxPageControl1.ActivePageIndex = 0 then
  begin
    if Order_Main.IsEmpty then
      Exit;
    if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
    begin
      Application.MessageBox('不能操作他人的数据!', '提示', 0);
      Exit;
    end;

    if Application.MessageBox('是否核实好数据？', '提示', 32 + 4) <> IDYES then
      Exit;
    try
      with ADOQueryBaseCmd do
      begin
        close;
        sql.Clear;
        sql.Add('update  Trade_Plan_Main SET status=''10'' ');
        sql.Add('where mainID=' + quotedstr(Trim(Order_Main.fieldbyname('mainID').AsString)));

        sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
        sql.Add(' ' + quotedstr(Trim(DName)));
        sql.Add(',getdate() ');
        sql.Add(',' + quotedstr(Trim(Self.Caption)));
        sql.Add(',' + quotedstr(Trim('计划单完成')));
        sql.Add(',' + quotedstr(Trim('计划单号：' + Trim(Order_Main.fieldbyname('orderNO').AsString))));
        sql.Add(',' + quotedstr(Trim('成功')));
        sql.Add(')');
        execsql;
      end;

      Application.MessageBox('计划单完成成功！', '提示信息');
      TBRafresh.Click;
    except
      Application.MessageBox('计划单完成失败！', '提示信息', 0);
    end;
  end
  else
  begin
    if cds_mxlist.IsEmpty then
      Exit;
    if Trim(cds_mxlist.fieldbyname('Filler').AsString) <> Trim(DName) then
    begin
      Application.MessageBox('不能操作他人的数据!', '提示', 0);
      Exit;
    end;

    if Application.MessageBox('是否核实好数据？', '提示', 32 + 4) <> IDYES then
      Exit;
    try
      with ADOQueryBaseCmd do
      begin
        close;
        sql.Clear;
        sql.Add('update  Trade_Plan_Main SET status=''10'' ');
        sql.Add('where mainID=' + quotedstr(Trim(cds_mxlist.fieldbyname('mainID').AsString)));

        sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
        sql.Add(' ' + quotedstr(Trim(DName)));
        sql.Add(',getdate() ');
        sql.Add(',' + quotedstr(Trim(Self.Caption)));
        sql.Add(',' + quotedstr(Trim('计划单完成')));
        sql.Add(',' + quotedstr(Trim('计划单号：' + Trim(cds_mxlist.fieldbyname('orderNO').AsString))));
        sql.Add(',' + quotedstr(Trim('成功')));
        sql.Add(')');
        execsql;
      end;

      Application.MessageBox('计划单完成成功！', '提示信息');
      TBRafresh.Click;
    except
      Application.MessageBox('计划单完成失败！', '提示信息', 0);
    end;
  end;

end;

procedure TfrmItemTestList.ButtonEdit(Sender: TObject; AButtonIndex: Integer);
begin
  inherited;
  try
    frmPictureUpload := TfrmPictureUpload.Create(Application);
    with frmPictureUpload do
    begin
      fFlileFlag := UserDataFlag + 'HX';
      FWidth := 250;
      FHeight := 300;
      FTFType := 'HX';
      FDataId := Trim(Self.ORDER_SUB.fieldbyname('subId').AsString);
      if ShowModal = 1 then
      begin
      end;
    end;
  finally
    frmPictureUpload.Free;
  end;
end;

procedure TfrmItemTestList.InitGrid();
var
  strwhere: string;
  mStatus: string;
begin
  strwhere := '';
  mStatus := cxTabControl1.Tabs[cxTabControl1.TabIndex].Caption;
  ConNo.Tag := 99;
  batchNo.Tag := 99;
  FtyPCId.Tag := 99;
  strwhere := SLGetFilters(dxLayoutControl_query, 1, 2);
  if Trim(strwhere) <> '' then
  begin
    strwhere := ' and ' + Trim(strwhere);
  end;

  if (Trim(DParameters1) <> '高权限') then
    strwhere := strwhere + ' and A.Filler=' + quotedstr(Trim(DName));

  try
    Tv1.BeginUpdate();
    fIsLoad := false;
    loadProcess.Visible := true;
    loadProcess.Refresh;

    Order_Main.DisableControls;
    with ADOQueryMain do
    begin

      close;
      Filtered := false;
      sql.Clear;
      sql.Add('exec P_View_ItemTestList :begdate,:enddate,:WSql');
      Parameters.ParamByName('begdate').Value := FormatDateTime('yyyy-MM-dd', begdate.Date);
      Parameters.ParamByName('enddate').Value := FormatDateTime('yyyy-MM-dd', endDate.Date + 1);
//      Parameters.ParamByName('mStatus').Value := mStatus;
      Parameters.ParamByName('WSql').Value := strwhere;
      Open;
    end;
    // if Order_Main.Fields.Count<=1 then
    SCreateCDS(ADOQueryMain, Order_Main);
    SInitCDSData(ADOQueryMain, Order_Main);
  finally
    Tv1.DataController.Filter.Clear;
    Order_Main.EnableControls;

    Tv1.EndUpdate;
    TV1.Controller.GoToFirst();
    fIsLoad := true;
    InitSub();

    loadProcess.Visible := false;

  end;

end;

procedure TfrmItemTestList.InitForm();
begin
  addQryContionByLay(ADOQueryBaseTemp, fformId, 'cxgrid1', dxLayoutControl_query, 7);
//  addQryContionByLay(ADOQueryBaseTemp, fformId, 'cxgrid3', dxLayoutControl_query, 7);
  ReadCxGrid('物测报告列表T1', Tv1, gDllFileCaption);
  ReadCxGrid('物测报告列表T2', TV2, gDllFileCaption);
//  ReadCxGrid('物测报告明细列表T3', tv3, gDllFileCaption);

  InitGrid();

end;

procedure TfrmItemTestList.InitSub();
begin
  ADOQuerySub.close;
//  if Order_Main.IsEmpty then
//    Exit;
  try
    TV2.BeginUpdate();
    ORDER_SUB.DisableControls;
    with ADOQuerySub do
    begin
      close;
      sql.Clear;
      sql.Add('exec P_View_ItemTestMxList ');
      sql.Add('@mainId =' + quotedstr((Order_Main.fieldbyname('mainID').AsString)));
      Open;
    end;
    // if ORDER_SUB.Fields.Count<=1 then
    SCreateCDS(ADOQuerySub, ORDER_SUB);
    SInitCDSData(ADOQuerySub, ORDER_SUB);
  finally
    ORDER_SUB.EnableControls;
    TV2.EndUpdate;
  end;

  //
end;

procedure TfrmItemTestList.N1Click(Sender: TObject);
var
  FLabVolume, fPrintFile, mSubId: string;
begin
  if cxPageControl1.ActivePageIndex = 0 then
  begin
    if ORDER_SUB.IsEmpty then
      Exit;
    mSubId := Trim(ORDER_SUB.fieldbyname('SUBID').AsString);
  end
  else
  begin
    if cds_mxlist.IsEmpty then
      Exit;
    mSubId := Trim(cds_mxlist.fieldbyname('SUBID').AsString);
  end;
  //
  with ADOQueryPrint do
  begin
    close;
    sql.Clear;
    sql.Add(' EXEC P_Trade_Print_djd_Lab ');
    sql.Add(' @CIID=''' + mSubId + '''');
    sql.Add(',@isTest =1');
    Open;
  end;

  if Trim(ADOQueryPrint.fieldbyname('SLabVolume').AsString) <> '' then
    FLabVolume := Trim(ADOQueryPrint.fieldbyname('SLabVolume').AsString)
  else
    FLabVolume := Trim(ADOQueryPrint.fieldbyname('LabVolume').AsString);

  if FLabVolume = '' then
    FLabVolume := trim(gDefaultLableFile);
  if FLabVolume = '' then
  begin
    Application.MessageBox('还未设置卷标签！', '提示信息', MB_ICONERROR);
    Exit;
  end;
  ExportFtErpFile(FLabVolume + '.rmf', ADOQueryBaseTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + FLabVolume + '.rmf';

  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    if Trim(ADOQueryPrint.fieldbyname('LabVolume').AsString) <> '次品标签' then
      RM1.DefaultCopies := 2
    else
      RM1.DefaultCopies := 1;
    RM1.ShowErrorMsg := false;
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;

end;

procedure TfrmItemTestList.N2Click(Sender: TObject);
var
  MLabPackage, fPrintFile, mSubId: string;
begin
  if cxPageControl1.ActivePageIndex = 0 then
  begin
    if ORDER_SUB.IsEmpty then
      Exit;
    mSubId := Trim(ORDER_SUB.fieldbyname('SUBID').AsString);
  end
  else
  begin
    if cds_mxlist.IsEmpty then
      Exit;
    mSubId := Trim(cds_mxlist.fieldbyname('SUBID').AsString);
  end;
  with ADOQueryPrint do
  begin
    close;
    sql.Clear;
    sql.Add('exec P_Trade_Insp_Prt_djd_PackLab_test ');
    sql.Add('@subId =' + quotedstr(mSubId));
    Open;
  end;
  if Trim(ADOQueryPrint.fieldbyname('SLabPackage').AsString) <> '' then
    MLabPackage := Trim(ADOQueryPrint.fieldbyname('SLabPackage').AsString)
  else
    MLabPackage := Trim(ADOQueryPrint.fieldbyname('LabPackage').AsString);
  if MLabPackage = '' then
    MLabPackage := trim(gDefaultPackLableFile);
  if MLabPackage = '' then
  begin
    Application.MessageBox('还未设置包标签！', '提示信息', MB_ICONERROR);
    Exit;
  end;
  ExportFtErpFile(Trim(MLabPackage) + '.rmf', ADOQueryBaseTemp);

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(MLabPackage) + '.rmf';
  if not FileExists(fPrintFile) then
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
    Exit;
  end;
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.DefaultCopies := 1;
    RM1.ShowErrorMsg := false;
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;

end;

procedure TfrmItemTestList.OrderNoPropertiesChange(Sender: TObject);
begin
  inherited;
  Tfilter.Click;
end;

procedure TfrmItemTestList.OrderNoPropertiesEditValueChanged(Sender: TObject);
begin
  inherited;
  InitGrid();
end;

procedure TfrmItemTestList.InitMxGrid();
var
  strwhere: string;
  mStatus: string;
begin
  strwhere := '';
  try
    loadProcess.Visible := true;
    loadProcess.Refresh;
//    tv3.BeginUpdate();
    ConNo.Tag := 2;
    batchNo.Tag := 2;
    FtyPCId.Tag := 2;
    strwhere := SLGetFilters(dxLayoutControl_query, 1, 2);
    if Trim(strwhere) <> '' then
    begin
      strwhere := ' and ' + Trim(strwhere);
    end;
    mStatus := cxTabControl1.Tabs[cxTabControl1.TabIndex].Caption;
    if (Trim(DParameters1) <> '高权限') then
      strwhere := strwhere + ' and A.Filler=' + quotedstr(Trim(DName));

    cds_mxlist.DisableControls;
    with ADOQueryMxList do
    begin
      close;
      Filtered := false;
      sql.Clear;
      sql.Add('exec P_View_TradePlanMxList_for_pack :timeType, :begdate,:enddate,:mStatus,:WSql');
      Parameters.ParamByName('timeType').Value := '制单日期';
      Parameters.ParamByName('begdate').Value := FormatDateTime('yyyy-MM-dd', begdate.Date);
      Parameters.ParamByName('enddate').Value := FormatDateTime('yyyy-MM-dd', endDate.Date + 1);
      Parameters.ParamByName('mStatus').Value := mStatus;
    //  if strwhere<> then
      Parameters.ParamByName('WSql').Value := ' ' + strwhere;
      Open;
    end;

    // if cds_mxlist.Fields.Count<=1 then
    SCreateCDS(ADOQueryMxList, cds_mxlist);
    SInitCDSData(ADOQueryMxList, cds_mxlist);

    cds_mxlist.EnableControls;

//    tv3.DataController.Filter.Clear;
  finally
//    TV3.Controller.GoToFirst();
//    tv3.EndUpdate;
    loadProcess.Visible := false;
  end;
end;

function TfrmItemTestList.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryBaseCmd.Connection.BeginTrans;
    with ADOQueryBaseCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete Item_Test_Main where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
      sql.Add('delete Item_Test_sub where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
//      sql.Add('delete Trade_Plan_Fty where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');

      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(Trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(Trim(Self.Caption)));
      sql.Add(',' + quotedstr(Trim('物测报告删除')));
      sql.Add(',' + quotedstr(Trim('生产合同号：' + Trim(Order_Main.fieldbyname('ConNo').AsString))));
      sql.Add(',' + quotedstr(Trim('成功')));
      sql.Add(')');
      execsql;
    end;

    ADOQueryBaseCmd.Connection.CommitTrans;
    Result := true;
  except
    ADOQueryBaseCmd.Connection.RollbackTrans;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;
/////////////////////////////////////////////////

function TfrmItemTestList.DelSubData(): Boolean;
var
  mSubId: string;
begin
  try
    Result := false;
    mSubId := Trim(cds_mxlist.fieldbyname('subId').AsString);
    ADOQueryBaseCmd.Connection.BeginTrans;
    with ADOQueryBaseCmd do
    begin
      close;
      sql.Clear;
     // sql.Add('delete Trade_Plan_Main where MainId=''' +
      //  Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
      sql.Add('delete Trade_Plan_Sub where MainId=' + quotedstr(Trim(cds_mxlist.fieldbyname('MainId').AsString)));
      sql.Add('and subId=' + quotedstr(mSubId));
     // sql.Add('delete Trade_Plan_Fty where MainId=''' +
      //  Trim(Order_Main.fieldbyname('MainId').AsString) + '''');

      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(Trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(Trim(Self.Caption)));
      sql.Add(',' + quotedstr(Trim('指示单删除')));
      sql.Add(',' + quotedstr(Trim('指示单号：' + Trim(Order_Main.fieldbyname('OrderNO').AsString) + ';明细id=' + mSubId)));
      sql.Add(',' + quotedstr(Trim('成功')));
      sql.Add(')');
      execsql;
    end;

    ADOQueryBaseCmd.Connection.CommitTrans;
    Result := true;
  except
    ADOQueryBaseCmd.Connection.RollbackTrans;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;
////////////////////////////////////////////////

procedure TfrmItemTestList.threadLoadform();
var
  thread: TThread;
begin

  thread := TThread.CreateAnonymousThread(
    procedure
    begin

      with ADOQueryMain do
      begin

        close;
        Filtered := false;
        sql.Clear;
        sql.Add('exec P_View_TradePlanList_for_pack :timeType, :begdate,:enddate,:mStatus,:WSql');
        Parameters.ParamByName('timeType').Value := '制单日期';
        Parameters.ParamByName('begdate').Value := FormatDateTime('yyyy-MM-dd', begdate.Date);
        Parameters.ParamByName('enddate').Value := FormatDateTime('yyyy-MM-dd', endDate.Date + 1);
        Parameters.ParamByName('mStatus').Value := '';
        Parameters.ParamByName('WSql').Value := '';
        Open;
      end;
    end);

  thread.FreeOnTerminate := True;
  thread.Start;

end;

end.

