unit U_TatWBPlanZJZK;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, DBClient, cxCalendar, cxButtonEdit, cxSplitter, RM_Common, RM_Class,
  RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, cxTextEdit, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxDateRanges, dxBarBuiltInMenu, U_BaseList,
  cxPC, cxCheckBox, dxScrollbarAnnotations, Vcl.Menus, cxContainer,
  cxProgressBar, dxSkinsCore, dxSkinsDefaultPainters, dxSkinOffice2013White,
  dxSkinSharpPlus, dxSkinWXI, frxOLE, frxRich, frxBarcode, frxClass, frxDBSet,
  frxTableObject, frxExportPPTX, frxExportXLSX, frxExportBaseDialog,
  frxExportXLS;

type
  TfrmTatWBPlanZJZK = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Label2: TLabel;
    GPM_1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    TBExport: TToolButton;
    CDS_1: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    TBView: TToolButton;
    v1Column19: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column31: TcxGridDBColumn;
    CustName: TEdit;
    Label7: TLabel;
    cxTabControl1: TcxTabControl;
    Tv1Column2: TcxGridDBColumn;
    TBEnd: TToolButton;
    TBNoEnd: TToolButton;
    ToolButton1: TToolButton;
    btnChk: TToolButton;
    btnReChk: TToolButton;
    Panel2: TPanel;
    ToolBar2: TToolBar;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    DS_2: TDataSource;
    CDS_2: TClientDataSet;
    GPM_2: TcxGridPopupMenu;
    Label3: TLabel;
    WB_Spec: TEdit;
    ToolButton4: TToolButton;
    DS_3: TDataSource;
    CDS_3: TClientDataSet;
    GPM_3: TcxGridPopupMenu;
    ADO_2: TADOQuery;
    ADO_3: TADOQuery;
    PM_1: TPopupMenu;
    N5: TMenuItem;
    N6: TMenuItem;
    ToolButton5: TToolButton;
    frxXLSExport1: TfrxXLSExport;
    frxXLSXExport1: TfrxXLSXExport;
    frxPPTXExport1: TfrxPPTXExport;
    frxReportTableObject1: TfrxReportTableObject;
    frxReport1: TfrxReport;
    fRMDB_1: TfrxDBDataset;
    frxBarCodeObject1: TfrxBarCodeObject;
    frxRichObject1: TfrxRichObject;
    frxOLEObject1: TfrxOLEObject;
    cxGrid4: TcxGrid;
    Tv4: TcxGridDBTableView;
    Tv4Column1: TcxGridDBColumn;
    Tv4Column2: TcxGridDBColumn;
    cxGridDBColumn34: TcxGridDBColumn;
    cxGridDBColumn36: TcxGridDBColumn;
    cxGridDBColumn37: TcxGridDBColumn;
    cxGridDBColumn38: TcxGridDBColumn;
    cxGridDBColumn39: TcxGridDBColumn;
    cxGridDBColumn40: TcxGridDBColumn;
    cxGridDBColumn41: TcxGridDBColumn;
    cxGridDBColumn42: TcxGridDBColumn;
    cxGridDBColumn43: TcxGridDBColumn;
    cxGridDBColumn44: TcxGridDBColumn;
    Tv4Column3: TcxGridDBColumn;
    cxGridDBColumn35: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    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;
    cxGridLevel1: TcxGridLevel;
    TbLogLook: TToolButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure ConNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBViewClick(Sender: TObject);
    procedure OrdTypeChange(Sender: TObject);
    procedure C_CodeKeyPress(Sender: TObject; var Key: Char);
    procedure BatchNoHZKeyPress(Sender: TObject; var Key: Char);
    procedure CustNameKeyPress(Sender: TObject; var Key: Char);
    procedure C_NameKeyPress(Sender: TObject; var Key: Char);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TBEndClick(Sender: TObject);
    procedure TBNoEndClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure btnChkClick(Sender: TObject);
    procedure btnReChkClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure cxTabControl2Change(Sender: TObject);
    procedure TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton3Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure TbLogLookClick(Sender: TObject);

  private
    FInt, PFInt: Integer;
    procedure InitGrid();
    procedure InitCard();
    procedure InitCardYarn();
    procedure InitForm();
//    procedure PrintCar(MPCID: string);
    function DelData(): Boolean;
    procedure InitButton();
    { Private declarations }
  public
    canshu1, FCheJian: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_LabelPrint, U_TatWBPlanInPut,
  U_TatWBCardInPut, U_TatWBPlanZJZKInPut, U_TatWBPlanZJZKKaiKa,
  U_TatWBCardInPut2, U_RPFun, U_LogInput; //U_TatWBPlanCardInPut

{$R *.dfm}

procedure TfrmTatWBPlanZJZK.InitCard();
begin
  with ADO_2 do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*  ');
    sql.Add(' from Tat_WB_Card A  ');
    sql.Add('where TWPId=' + quotedstr(trim(CDS_1.fieldbyname('TWPId').AsString)));
    sql.Add(' and WB_Type <> ''浆轴'' and WB_Type <> ''稀轴'' ');
    Open;
  end;
  SCreateCDS(ADO_2, CDS_2);
  SInitCDSData(ADO_2, CDS_2);
end;

procedure TfrmTatWBPlanZJZK.InitCardYarn();
begin

  with ADO_3 do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*  ');
    sql.Add(' from Tat_WB_Card_Yarn A  ');
    sql.Add('where WBCID=' + quotedstr(trim(CDS_2.fieldbyname('WBCID').AsString)));

    Open;
  end;
  SCreateCDS(ADO_3, CDS_3);
  SInitCDSData(ADO_3, CDS_3);
end;

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

procedure TfrmTatWBPlanZJZK.InitButton();
begin

  TBAdd.Enabled := False;
  TBEdit.Enabled := False;
  TBDel.Enabled := False;
  TBEnd.Enabled := False;
  TBNoEnd.Enabled := False;
  btnChk.Enabled := False;
  btnReChk.Enabled := False;
  if Trim(canshu1) = '录入' then
  begin
    case cxTabControl1.TabIndex of
      0:
        begin
          TBAdd.Enabled := true;
          TBDel.Enabled := true;
          TBEdit.Enabled := true;
          btnChk.Enabled := true;

        end;
      1:
        begin
          TBAdd.Enabled := true;
          TBEnd.Enabled := true;
          TBEdit.Enabled := true;
          btnReChk.Enabled := true;
        end;
      2:
        begin
          TBNoEnd.Enabled := true;
        end;
    end;
  end;

  if Trim(canshu1) = '管理' then
  begin
    case cxTabControl1.TabIndex of
      0:
        begin
          TBAdd.Enabled := true;
          TBDel.Enabled := true;
          TBEdit.Enabled := true;
          btnChk.Enabled := true;
        end;
      1:
        begin
          TBAdd.Enabled := true;
          TBEnd.Enabled := true;
          TBEdit.Enabled := true;
          btnReChk.Enabled := true;
        end;
      2:
        begin
          TBNoEnd.Enabled := true;
        end;
    end;
  end;

end;

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

procedure TfrmTatWBPlanZJZK.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select A.*  ');
      sql.Add(' from Tat_WB_Plan A  ');
      sql.Add('where 1=1 ');
      sql.Add(' and EXISTS (SELECT * FROM Tat_WB_Spec X where X.WB_Code = A.WB_Code and X.WB_TYPE = ''分条'' )');
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add('and isnull(Status,''0'') =''0'' ');
          end;
        1:
          begin
            sql.Add('and isnull(Status,''0'') =''9'' ');
          end;
        2:
          begin
            sql.Add('and isnull(Status,''0'') =''10'' ');
            sql.Add(' and A.FillTIme>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
            sql.Add(' and A.FillTIme<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1)) + '''');
          end;
      end;

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

procedure TfrmTatWBPlanZJZK.N5Click(Sender: TObject);
begin
  SelOKNo(CDS_2, True);
end;

procedure TfrmTatWBPlanZJZK.N6Click(Sender: TObject);
begin
  SelOKNo(CDS_2, False);
end;

procedure TfrmTatWBPlanZJZK.InitForm();
begin
  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '整经轴卡安排');
  ReadCxGrid(trim(self.Caption) + 'Tv2', Tv2, '整经轴卡安排');
//  ReadCxGrid(trim(self.Caption) + 'Tv3', Tv3, '坯布生产管理');
  BegDate.DateTime := SGetServerDate(ADOQueryTemp) - 30;
  EndDate.DateTime := SGetServerDate(ADOQueryTemp);
  InitButton();
  InitGrid();
end;

procedure TfrmTatWBPlanZJZK.TBFindClick(Sender: TObject);
begin
  TBRafresh.Click;
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

procedure TfrmTatWBPlanZJZK.TbLogLookClick(Sender: TObject);
begin
  frmLogInput := TfrmLogInput.Create(Application);
  with frmLogInput do
  begin
    fWB_Code := Self.CDS_1.FieldByName('TWPId').AsString;
    fFlag := '查看';
    flogFlag := '整经计划信息修改';
    if ShowModal = 1 then
    begin

    end;
  end;

end;

procedure TfrmTatWBPlanZJZK.TBNoEndClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Tat_WB_Plan SET status=''0'',Comper=null,Comptime=null ');
      sql.Add('where TWPId=' + quotedstr(trim(CDS_1.fieldbyname('TWPId').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_1.FieldByName('TWPId').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('撤销结束成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('撤销结束失败！', '提示信息', 0);
  end;
end;

procedure TfrmTatWBPlanZJZK.TBEditClick(Sender: TObject);
var
  SavedID: string;
begin
  if CDS_1.IsEmpty then
    Exit;
  SavedID := Trim(CDS_1.FieldByName('TWPId').AsString);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    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_1.FieldByName('TWPId').AsString))));
    sql.Add(',' + quotedstr(trim('成功')));
    sql.Add(')');
    ExecSQL;
  end;
  try
    frmTatWBPlanZJZKInPut := TfrmTatWBPlanZJZKInPut.Create(Application);
    with frmTatWBPlanZJZKInPut do
    begin
      PState := 1;
      FTWPId := Trim(Self.CDS_1.fieldbyname('TWPId').AsString);
      if ShowModal = 1 then
      begin
        InitGrid();
        if not Self.CDS_1.Locate('TWPId', SavedID, [loCaseInsensitive]) then
        begin
          // 如果没找到，可能是记录已被删除，或者数据异常，可以给出提示
          Application.MessageBox('未能在列表中找到刚才编辑的记录，请手动刷新。', '提示', MB_OK);
        end;
//        Self.CDS_1.Locate('TWPId', frmTatWBPlanInPut.FTWPId, []);
      end;
    end;
  finally
    frmTatWBPlanZJZKInPut.Free;
  end;

end;

procedure TfrmTatWBPlanZJZK.TBEndClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Tat_WB_Plan SET status=''10'' ,Comper=''' + trim(DName) + ''',Comptime=getdate() ');
      sql.Add('where TWPId=' + quotedstr(trim(CDS_1.fieldbyname('TWPId').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_1.FieldByName('TWPId').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('结束成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('结束失败！', '提示信息', 0);
  end;
end;

procedure TfrmTatWBPlanZJZK.TBDelClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*');
    sql.Add('from Tat_WB_CARD A');
    sql.Add(' where A.WBCNO=''' + Trim(CDS_1.fieldbyname('TWPId').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已产生安排数据不能删除!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if DelData() then
  begin
    CDS_1.Delete;
  end;
end;

function TfrmTatWBPlanZJZK.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Tat_WB_Plan where TWPId=''' + Trim(CDS_1.fieldbyname('TWPId').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_1.FieldByName('TWPId').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      ExecSQL;
    end;

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

procedure TfrmTatWBPlanZJZK.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel('坯布生产单', cxGrid1);
end;

procedure TfrmTatWBPlanZJZK.TBPrintClick(Sender: TObject);
var
  WSql: string;
begin
  if CDS_1.IsEmpty then
    Exit;
  WSql := Trim(CDS_1.fieldbyname('TWPId').AsString);

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

end;

procedure TfrmTatWBPlanZJZK.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmTatWBPlanZJZK.TBAddClick(Sender: TObject);
begin
  try
    frmTatWBPlanZJZKInPut := TfrmTatWBPlanZJZKInPut.Create(Application);
    with frmTatWBPlanZJZKInPut do
    begin
      PState := 0;
      FTWPId := '';
      FSubId := '';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmTatWBPlanZJZKInPut.Free;
  end;
end;

procedure TfrmTatWBPlanZJZK.ConNoChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

procedure TfrmTatWBPlanZJZK.FormShow(Sender: TObject);
begin
  inherited;
  canshu1 := Self.fParameters1;
  InitForm();

end;

procedure TfrmTatWBPlanZJZK.TBViewClick(Sender: TObject);
begin
  try
    frmTatWBPlanZJZKInPut := TfrmTatWBPlanZJZKInPut.Create(Application);
    with frmTatWBPlanZJZKInPut do
    begin
      PState := 1;
      FTWPId := Trim(Self.CDS_1.fieldbyname('TWPId').AsString);
      TBSave.Visible := False;
      ToolBar3.Visible := False;
      ScrollBox1.Enabled := False;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmTatWBPlanZJZKInPut.Free;
  end;
end;

procedure TfrmTatWBPlanZJZK.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '整经轴卡安排');
  WriteCxGrid(trim(self.Caption) + 'Tv2', Tv2, '整经轴卡安排');
//  WriteCxGrid(trim(self.Caption) + 'Tv3', Tv3, '坯布生产管理');
end;

procedure TfrmTatWBPlanZJZK.ToolButton2Click(Sender: TObject);
begin

  try
    frmTatWBPlanZJZKKaiKa := TfrmTatWBPlanZJZKKaiKa.Create(Application);
    with frmTatWBPlanZJZKKaiKa do
    begin
      FTWPId := Trim(self.CDS_1.fieldbyname('TWPId').AsString);
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmTatWBPlanZJZKKaiKa.Free;
  end;
end;

procedure TfrmTatWBPlanZJZK.ToolButton3Click(Sender: TObject);
var
  rtvalues: TArray<string>;
begin
  if CDS_2.IsEmpty then
    Exit;
  Tv2.OnFocusedRecordChanged := nil;
  if CDS_2.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  rtvalues := SelCDSKey(CDS_2, ['WBCID']);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('exec P_TatWB_Card_Del ');
      Sql.Add('@WBCIDS=' + quotedstr(Trim(rtvalues[0])));
      Sql.Add(',@DCode=' + Quotedstr(DCode));
      Sql.Add(',@DName=' + Quotedstr(DName));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitCard();
    application.MessageBox('删除成功！', '提示信息');
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmTatWBPlanZJZK.ToolButton4Click(Sender: TObject);
var
  RTValues: TArray<string>;
begin
  if CDS_2.IsEmpty then
    Exit;

  RTValues := SelCDSKey(CDS_2, ['WBCID']);

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FPreviewPrint := False;
//      case cxTabControl2.TabIndex of
//        0:
//          begin
//            FLMType := 'TatWBCard1';
//          end;
//        1:
//          begin
//            FLMType := 'TatWBCard2';
//          end;
//      end;      、
//      FLMType := 'TatWBCard3';
      FFiltration1 := RTValues[0];
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
//        with ADOQueryCmd do
//        begin
//          Close;
//          sql.Clear;
//          sql.Add('update Tat_WB_Card         ');
//          sql.Add(' set PRTer=' + quotedstr(DName));
//          sql.Add(', PRTCount=PRTCount+1');
//          sql.Add(', PRTDate=getdate()');
//          sql.Add(' where WBCID in (' + WSql + ')');
//          ExecSQL;
//        end;
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;

end;

procedure TfrmTatWBPlanZJZK.ToolButton5Click(Sender: TObject);
var
  i: integer;
  MPCID: string;
  MPrintJson: string;
begin

  if CDS_2.IsEmpty then
    Exit;
  if CDS_2.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  i := 0;
  with CDS_2 do
  begin
    DisableControls;
    First;
    while not Eof do
    begin
      if Fieldbyname('Ssel').AsBoolean then
      begin
        if i = 0 then
          MPCID := trim(CDS_2.fieldbyname('WBCID').AsString);
//        PrintCar(trim(CDS_2.fieldbyname('PCID').AsString));
        MPrintJson := '{ "LBName": "上浆轴卡","IsPreview": false, "PrtArgs": [ {"SqlStr": "EXEC P_TatWB_JZCard_Prt @Filtration=' + QuotedStr(MPCID) + ' " }, {"SqlStr": "EXEC P_TatWB_JZCard_Prt2 @Filtration=' + QuotedStr(MPCID) + '" } ] }';
//        ShowMessage(MPrintJson);
        FunPrintRmf(Application, PChar(DConString), PChar(MPrintJson));
        i := i + 1;
      end;
      Next;
    end;
    EnableControls;
  end;
  SelOKNoFiler(Tv2, False);
  CDS_2.Locate('WBCID', MPCID, []);
end;

procedure TfrmTatWBPlanZJZK.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitCard();
end;

procedure TfrmTatWBPlanZJZK.TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitCardYarn();
end;

procedure TfrmTatWBPlanZJZK.btnChkClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Tat_WB_Plan SET status=''9'',chker=''' + trim(DName) + ''',Chktime=getdate() ');
      sql.Add('where TWPId=' + quotedstr(trim(CDS_1.fieldbyname('TWPId').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_1.FieldByName('TWPId').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('开始成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('开始失败！', '提示信息', 0);
  end;
end;

procedure TfrmTatWBPlanZJZK.btnReChkClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;

  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Tat_WB_Plan SET status=''0'',chker=null,Chktime=null ');
      sql.Add('where TWPId=' + quotedstr(trim(CDS_1.fieldbyname('TWPId').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_1.FieldByName('TWPId').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    TBRafresh.Click;
  except
    application.MessageBox('计划单撤销开始失败！', '提示信息', 0);
  end;
end;

procedure TfrmTatWBPlanZJZK.OrdTypeChange(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmTatWBPlanZJZK.C_CodeKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    TBRafresh.Click();
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

procedure TfrmTatWBPlanZJZK.BatchNoHZKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    TBRafresh.Click();
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

procedure TfrmTatWBPlanZJZK.CustNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    TBRafresh.Click();
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

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

procedure TfrmTatWBPlanZJZK.cxTabControl2Change(Sender: TObject);
begin
  InitCard();
end;

procedure TfrmTatWBPlanZJZK.C_NameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    TBRafresh.Click();
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

end.

