unit U_TradePlanMgeList;

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, cxLookAndFeelPainters,  
  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, dxSkinBasic;

type
  TfrmTradePlanMgeList = 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;
    custName: TcxTextEdit;
    ConNo: TcxTextEdit;
    BuyConNo: TcxTextEdit;
    OrderNo: TcxTextEdit;
    dxLayoutControl_queryGroup_Root: TdxLayoutGroup;
    dxLayoutItem1: TdxLayoutItem;
    dxLayoutItem2: TdxLayoutItem;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    cxTabControl1: TcxTabControl;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1custName: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1DlyDate: TcxGridDBColumn;
    v1ZQty: TcxGridDBColumn;
    v1OrderUnit3: TcxGridDBColumn;
    v1MPRTTeBieNote: TcxGridDBColumn;
    v1MPRTBZNote: TcxGridDBColumn;
    v1MPRTSCTeBieNote: TcxGridDBColumn;
    v1filler: TcxGridDBColumn;
    v1OrdPerson2: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    dxLayoutItem_ywy: TdxLayoutItem;
    dxLayoutItem_pm: TdxLayoutItem;
    ywy: TcxTextEdit;
    C_Name: TcxTextEdit;
    GPM_1: TcxGridPopupMenu;
    ADOQueryMain: TADOQuery;
    DataSource1: TDataSource;
    Order_Main: TClientDataSet;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    TV2SSel: TcxGridDBColumn;
    V2BuyConNo: TcxGridDBColumn;
    V2C_StyleNo: TcxGridDBColumn;
    V2C_Name: TcxGridDBColumn;
    V2C_EName: TcxGridDBColumn;
    v1C_ColorNo: TcxGridDBColumn;
    v1C_Color: TcxGridDBColumn;
    V2PRTCode: TcxGridDBColumn;
    TV2C_EColor: TcxGridDBColumn;
    v1OrdQty: TcxGridDBColumn;
    v1OrdUnit: TcxGridDBColumn;
    Tv2OrdSNote: 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;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    TV2Column6: 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;
    cxGrid3: TcxGrid;
    tv3: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridDBColumn26: TcxGridDBColumn;
    cxGridDBColumn27: TcxGridDBColumn;
    cxGridDBColumn28: TcxGridDBColumn;
    cxGridDBColumn29: TcxGridDBColumn;
    cxGridDBColumn30: TcxGridDBColumn;
    cxGridDBColumn31: TcxGridDBColumn;
    cxGridDBColumn32: TcxGridDBColumn;
    cxGridDBColumn33: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    tv3OrderNo: TcxGridDBColumn;
    tv3custName: TcxGridDBColumn;
    tv3ordDate: TcxGridDBColumn;
    DataSource3: TDataSource;
    ADOQueryMxList: TADOQuery;
    cxProgressBar1: TcxProgressBar;
    GPM_3: TcxGridPopupMenu;
    tv3ConNo: TcxGridDBColumn;
    cds_mxlist: TClientDataSet;
    Tfilter: TToolButton;
    Tbqyl: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ADOQueryPrint: TADOQuery;
    RM1: TRMGridReport;
    RMDB_1: TRMDBDataSet;
    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);
  private
    canshu1, canshu2: string;
    fIsLoad: Boolean;
    procedure InitGrid();
    procedure InitMxGrid();
    procedure InitForm();
    procedure InitSub();
    function DelData(): Boolean;
  public

  end;

var
  frmTradePlanMgeList: TfrmTradePlanMgeList;

implementation

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

procedure TfrmTradePlanMgeList.btnCopyClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;

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

      end;
    end;
  finally
    frmTradePlanInPut.Free;
  end;

end;

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

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

end;

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

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

end;

procedure TfrmTradePlanMgeList.FormDestroy(Sender: TObject);
begin inherited;
  frmTradePlanMgeList := nil;
end;

procedure TfrmTradePlanMgeList.FormShow(Sender: TObject);
var
  mdesignCode :string;
begin inherited;
  // Scaleby(Screen.Width,1024);
  InitForm();
//  mdesignCode := fDllFileName +'|'+self.name+ '|' + intTostr(fformId) + '|' + 'cxgrid1';
 // addQryContionByLay(ADOQueryBaseTemp,fformId,'cxgrid1',dxLayoutControl_query,7);

end;

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

end;

procedure TfrmTradePlanMgeList.TBCKClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;

  try
    frmTradePlanInPut := TfrmTradePlanInPut.Create(Application);
    with frmTradePlanInPut do begin PState := 5;
      FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      ToolBar2.Visible := false;
      TBSave.Visible := false;
      if ShowModal = 1 then begin

      end;
    end;
  finally
    frmTradePlanInPut.Free;
  end;

end;

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

procedure TfrmTradePlanMgeList.TBDelClick(Sender: TObject);
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;

  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;

  // while Order_Main.Locate('SSel', true, []) do
  // begin
  if Trim(Order_Main.fieldbyname('MainId').AsString) <> '' then begin DelData()
  end;
  Order_Main.Delete;
  // end;

end;

procedure TfrmTradePlanMgeList.TBEditClick(Sender: TObject);
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
    frmTradePlanInPut := TfrmTradePlanInPut.Create(Application);
    with frmTradePlanInPut 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
    frmTradePlanInPut.Free;
  end;

end;

procedure TfrmTradePlanMgeList.TBPrintClick(Sender: TObject);
var
  WSql: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  WSql := (Trim(Order_Main.fieldbyname('MainId').AsString));

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

end;

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

procedure TfrmTradePlanMgeList.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 TfrmTradePlanMgeList.TdjszClick(Sender: TObject);
begin
  try
    frmTradeInspSet := TfrmTradeInspSet.Create(Application);
    with frmTradeInspSet do begin

        FDataID := Trim(Self.Order_Main.fieldbyname('MainId').AsString);

      if ShowModal = 1 then begin

      end;
    end;
  finally
    frmTradeInspSet.Free;
  end;

end;

procedure TfrmTradePlanMgeList.TNowcClick(Sender: TObject);
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=''9'' ');
      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;

procedure TfrmTradePlanMgeList.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 TfrmTradePlanMgeList.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+'-明细',cxGrid1);
  end;
end;

procedure TfrmTradePlanMgeList.TfilterClick(Sender: TObject);
begin
 inherited;
  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 TfrmTradePlanMgeList.ToolButton4Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmAttachmentUpload := TfrmAttachmentUpload.Create(Application);
    with frmAttachmentUpload do begin
      if Trim(canshu1) <> '查询' then
        FEditAuthority := true;
      fkeyNO := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      fType := '指示单';
      if ShowModal = 1 then begin

      end;
    end;
  finally
    frmAttachmentUpload.Free;
  end;

end;

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

end;

procedure TfrmTradePlanMgeList.TWCClick(Sender: TObject);
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;

procedure TfrmTradePlanMgeList.InitGrid();
var
  strwhere: string;
  mStatus: string;
begin strwhere := '';
  mStatus := cxTabControl1.Tabs[cxTabControl1.TabIndex].Caption;
  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;
    cxProgressBar1.Visible := true;
    cxProgressBar1.Refresh;


    Order_Main.DisableControls;
    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 := 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;
    fIsLoad := true;
    InitSub();

    cxProgressBar1.Visible := false;

  end;

end;

procedure TfrmTradePlanMgeList.InitForm();
begin
  ReadCxGrid('包装指示单列表T1', Tv1, gDllFileCaption);
  ReadCxGrid('包装指示单列表T2', TV2, gDllFileCaption);
  ReadCxGrid('包装指示单明细列表T3', tv3, gDllFileCaption);
  endDate.Date := SGetServerDate(ADOQueryBaseTemp);
  begdate.Date := endDate.Date - 15;

  InitGrid();
end;

procedure TfrmTradePlanMgeList.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_TradePlanMxList_for_pack ');
      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 TfrmTradePlanMgeList.N1Click(Sender: TObject);
var
  FLabVolume, fPrintFile: string;
begin
  if ORDER_SUB.IsEmpty then
    Exit;

  with ADOQueryPrint do begin close;
    sql.Clear;
    sql.Add(' EXEC P_Trade_Print_djd_Lab ');
    sql.Add(' @CIID=''' + Trim(ORDER_SUB.fieldbyname('SUBID').AsString) + '''');
    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 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 TfrmTradePlanMgeList.N2Click(Sender: TObject);
var
  MLabPackage, fPrintFile: string;
begin
  if ORDER_SUB.IsEmpty then
    Exit;

  with ADOQueryPrint do begin close;
    sql.Clear;
    sql.Add('exec P_Trade_Insp_Prt_djd_PackLab_test ');
    sql.Add('@subId =' + quotedstr(Trim(ORDER_SUB.fieldbyname('SUBID')
      .AsString)));
    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 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 TfrmTradePlanMgeList.OrderNoPropertiesChange(Sender: TObject);
begin inherited;
  Tfilter.Click;
end;

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

procedure TfrmTradePlanMgeList.InitMxGrid();
var
  strwhere: string;
  mStatus: string;
begin strwhere := '';
  try
    cxProgressBar1.Visible := true;
    cxProgressBar1.Refresh;
    cxProgressBar1.Reset;
    strwhere := SLGetFilters(dxLayoutControl_query, 1, 2);
    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;
      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
    cxProgressBar1.Visible := false;
  end;
end;

function TfrmTradePlanMgeList.DelData(): Boolean;
begin
  try
    Result := false;
    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=''' +
        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('OrderNO').AsString))));
      sql.Add(',' + quotedstr(Trim('成功')));
      sql.Add(')');
      execsql;
    end;

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

end.
