unit U_TatWBPlan;

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
  TfrmTatWBPlan = 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;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    V2Column4: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    DS_2: TDataSource;
    CDS_2: TClientDataSet;
    GPM_2: TcxGridPopupMenu;
    Label3: TLabel;
    WB_Spec: TEdit;
    ToolButton4: TToolButton;
    cxTabControl2: TcxTabControl;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    v2Column10: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    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;
    Tv1WB_Name: TcxGridDBColumn;
    Tv1WB_Qty_YJJZZS: TcxGridDBColumn;
    Tv1WB_Qty: TcxGridDBColumn;
    Tv1C_CODE: TcxGridDBColumn;
    TV2XZZJ: TcxGridDBColumn;
    TV2XZSJ: TcxGridDBColumn;
    TV2Group: TcxGridDBColumn;
    TV2JZSJ: TcxGridDBColumn;
    TV2JZSZ: TcxGridDBColumn;
    TbLogLook: TToolButton;
    Tv1GYNo: TcxGridDBColumn;
    Tv1ConNo: TcxGridDBColumn;
    Label4: TLabel;
    Label5: TLabel;
    ConNo: TEdit;
    GYNo: TEdit;
    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_TatWBCardInPut2, U_RPFun,U_LogInput; //U_TatWBPlanCardInPut

{$R *.dfm}

procedure TfrmTatWBPlan.InitCard();
begin
  with ADO_2 do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*  ');
    sql.Add(',cast((CASE  WHEN EXISTS (SELECT 1 from Tat_WB_IO WHERE WBCID=A.WBCID  AND StkName=''经轴''  AND ioFlag=''入库''  AND ioType=''整经完成'' ) THEN 1 ELSE 0 END) as bit) AS XZZJ');
    sql.Add(',cast((CASE  WHEN EXISTS (SELECT 1 from Tat_WB_IO WHERE WBCID=A.WBCID  AND StkName=''经轴''  AND ioFlag=''出库''  AND ioType=''加工出库'' ) THEN 1 ELSE 0 END) as bit) AS XZSJ');

    sql.Add(',cast((CASE  WHEN EXISTS (SELECT 1 from Tat_Plan_OutPut WHERE PCID=A.WBCID  AND GlideName=''上浆'' ) THEN 1 ELSE 0 END) as bit) AS JZSJ');
    sql.Add(',cast((CASE  WHEN EXISTS (SELECT 1 from Tat_Plan_OutPut WHERE PCID=A.WBCID  AND GlideName=''上轴'' ) THEN 1 ELSE 0 END) as bit) AS JZSZ');
    sql.Add(' from Tat_WB_Card A  ');
    sql.Add('where TWPId=' + quotedstr(trim(CDS_1.fieldbyname('TWPId').AsString)));
    case cxTabControl2.TabIndex of
      0:
        begin
          sql.Add('and WB_Type =''稀轴'' ');
        end;
      1:
        begin
          sql.Add('and WB_Type =''浆轴'' ');
        end;

    end;

    Open;
  end;
  SCreateCDS(ADO_2, CDS_2);
  SInitCDSData(ADO_2, CDS_2);
end;

procedure TfrmTatWBPlan.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 TfrmTatWBPlan.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := caFree;
end;

procedure TfrmTatWBPlan.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 TfrmTatWBPlan.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmTatWBPlan.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select A.*  ');
      sql.Add(' ,ConNo = (select ConNo from Tat_Plan_Sub X where X.MainId = A.gyno) ');
      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 TfrmTatWBPlan.N5Click(Sender: TObject);
begin
  SelOKNo(CDS_2, True);
end;

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

procedure TfrmTatWBPlan.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 TfrmTatWBPlan.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 TfrmTatWBPlan.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 TfrmTatWBPlan.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 TfrmTatWBPlan.TBEditClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;

  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
    frmTatWBPlanInPut := TfrmTatWBPlanInPut.Create(Application);
    with frmTatWBPlanInPut do
    begin
      PState := 1;
      FTWPId := Trim(Self.CDS_1.fieldbyname('TWPId').AsString);
      if ShowModal = 1 then
      begin
        InitGrid();
        Self.CDS_1.Locate('TWPId', frmTatWBPlanInPut.FTWPId, []);
      end;
    end;
  finally
    frmTatWBPlanInPut.Free;
  end;
end;

procedure TfrmTatWBPlan.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 TfrmTatWBPlan.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 TfrmTatWBPlan.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 TfrmTatWBPlan.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel('坯布生产单', cxGrid1);
end;

procedure TfrmTatWBPlan.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 := 'TatWBPlan';
      FFiltration1 := WSql;
      FFiltration2 := WSql;
      FFiltration3 := WSql;
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;

end;

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

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

procedure TfrmTatWBPlan.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 TfrmTatWBPlan.FormShow(Sender: TObject);
begin
  inherited;
  canshu1 := Self.fParameters1;
  InitForm();

end;

procedure TfrmTatWBPlan.TBViewClick(Sender: TObject);
begin
  try
    frmTatWBPlanInPut := TfrmTatWBPlanInPut.Create(Application);
    with frmTatWBPlanInPut 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
    frmTatWBPlanInPut.Free;
  end;
end;

procedure TfrmTatWBPlan.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '坯布生产管理');
  WriteCxGrid(trim(self.Caption) + 'Tv2', Tv2, '坯布生产管理');
  WriteCxGrid(trim(self.Caption) + 'Tv3', Tv3, '坯布生产管理');
end;

procedure TfrmTatWBPlan.ToolButton2Click(Sender: TObject);
begin
  try
    frmTatWBCardInPut2 := TfrmTatWBCardInPut2.Create(Application);
    with frmTatWBCardInPut2 do
    begin
      FTWPId := Trim(self.CDS_1.fieldbyname('TWPId').AsString);
      case cxTabControl2.TabIndex of
        0:
          begin
            FWB_Type := '稀轴';
          end;
        1:
          begin
            FWB_Type := '浆轴';
          end;

      end;
      if ShowModal = 1 then
      begin
        InitCard();
      end;
    end;
  finally
    frmTatWBCardInPut2.Free;
  end;

end;

procedure TfrmTatWBPlan.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 TfrmTatWBPlan.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;
      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 TfrmTatWBPlan.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 TfrmTatWBPlan.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitCard();
end;

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

procedure TfrmTatWBPlan.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 TfrmTatWBPlan.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 TfrmTatWBPlan.OrdTypeChange(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmTatWBPlan.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 TfrmTatWBPlan.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 TfrmTatWBPlan.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 TfrmTatWBPlan.cxTabControl1Change(Sender: TObject);
begin
  InitButton();
  InitGrid();
end;

procedure TfrmTatWBPlan.cxTabControl2Change(Sender: TObject);
begin

  case cxTabControl2.TabIndex of
    0:
      begin
        TV2XZZJ.Visible := True;
        TV2XZSJ.Visible := True;

        TV2JZSJ.Visible := False;
        TV2JZSZ.Visible := False;
      end;
    1:
      begin
        TV2XZZJ.Visible := False;
        TV2XZSJ.Visible := False;

        TV2JZSJ.Visible := True;
        TV2JZSZ.Visible := True;
      end;

  end;

  InitCard();

end;

procedure TfrmTatWBPlan.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.

