unit U_ClothSCListAnPaiJiTaiAP;

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, cxCheckBox,
  BtnEdit, cxPC, MovePanel, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu;

type
  TfrmClothSCListAnPaiJiTaiAP = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    ConNo: TEdit;
    Order_Main: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Qty1: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    v1Column20: TcxGridDBColumn;
    Label5: TLabel;
    Label4: TLabel;
    C_CodeName: TEdit;
    CarType: TEdit;
    Label2: TLabel;
    C_Code: TEdit;
    Panel2: TPanel;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Panel3: TPanel;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    cxSplitter1: TcxSplitter;
    DSAnPai: TDataSource;
    CDS_AnPai: TClientDataSet;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    ToolButton10: TToolButton;
    v1Column25: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v3Column2: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    v1Column29: TcxGridDBColumn;
    v1Column26: TcxGridDBColumn;
    Label6: TLabel;
    BatchNoHZ: TEdit;
    v1Column30: TcxGridDBColumn;
    Label7: TLabel;
    GCCode: TEdit;
    v1Column31: TcxGridDBColumn;
    ToolButton9: TToolButton;
    v1Column32: TcxGridDBColumn;
    Label8: TLabel;
    JHChejian: TEdit;
    v3Column4: TcxGridDBColumn;
    ToolButton11: TToolButton;
    Label9: TLabel;
    RTQYJTInfo: TEdit;
    Label10: TLabel;
    khname: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure conPress(Sender: TObject; var Key: Char);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure ToolButton10Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton9Click(Sender: TObject);
    procedure ToolButton11Click(Sender: TObject);
    procedure BatchNoHZKeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitForm();
    procedure UpdateJTInfo(RTMainId: string);
    function SaveJT(MCarNo, MCheJian: string): Boolean;
    procedure InitJT(MCloMainId, MCloSubId: string);
    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_ClothSCInPut, U_Fun, U_ZDYHelp, U_JTSel;

{$R *.dfm}

procedure TfrmClothSCListAnPaiJiTaiAP.InitJT(MCloMainId, MCloSubId: string);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add(' select A.*');
    sql.Add(',APPS=(select Count(APID) from JYCon_Sub_AnPai AP where AP.MainId=A.MainId and AP.CarNo=A.CarNo)');
    sql.Add(' from Cloth_JiTai A');
    sql.Add(' where A.SubId=''' + Trim(MCloMainId) + '''');
    sql.Add(' and A.MainId=''' + Trim(MCloSubId) + '''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_AnPai);
  SInitCDSData20(ADOQueryTemp, CDS_AnPai);
end;

function TfrmClothSCListAnPaiJiTaiAP.SaveJT(MCarNo, MCheJian: string): Boolean;
var
  maxno: string;
begin
  Result := False;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd, maxno, 'JT', 'Cloth_JiTai', 3, 1) = False then
    begin
      CDS_AnPai.EnableControls;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取安排机台流水号失败!', '提示', 0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Cloth_JiTai where 1=2');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('MainId').Value := Trim(Order_Main.fieldbyname('MainId').AsString);
      FieldByName('SubId').Value := Trim(Order_Main.fieldbyname('SubId').AsString);
      FieldByName('JTID').Value := Trim(maxno);
      FieldByName('CarNo').Value := Trim(MCarNo);
      FieldByName('CheJian').Value := Trim(MCheJian);
      FieldByName('StopFlag').Value := 0;
      FieldByName('DOTime').Value := SGetServerDateTime(ADOQueryTemp);
      FieldByName('DOPerson').Value := Trim(DName);
      FieldByName('QYTime').Value := SGetServerDateTime(ADOQueryTemp);
      FieldByName('QYPerson').Value := Trim(DName);
      FieldByName('Filler').Value := Trim(DName);
      Post;
    end;
    UpdateJTInfo(Trim(Order_Main.fieldbyname('MainId').AsString));
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    CDS_AnPai.EnableControls;
    Result := False;
    Application.MessageBox('保存异常！', '提示', 0);
  end;
end;

procedure TfrmClothSCListAnPaiJiTaiAP.FormDestroy(Sender: TObject);
begin
//  frmClothSCListAnPaiJiTaiAP:=nil;
end;

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

procedure TfrmClothSCListAnPaiJiTaiAP.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(trim(self.Caption), Tv1, '指示单管理5');
end;

procedure TfrmClothSCListAnPaiJiTaiAP.InitGrid();
var
  fwhere: string;
begin
  BegDate.SetFocus;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
//      sql.Add(' select A.*,B.*');
//      sql.Add(' ,APPS=(select Count(*) from JYCon_Sub_AnPai AA where AA.MainId=A.MainId)');
//      sql.Add(' ,JYPS=(select Count(*) from WFB_MJJY_PB AA where AA.MainId=A.MainId)');
//      sql.Add(' ,JYQtyKg=(select sum(MJMaoZ) from WFB_MJJY_PB AA where AA.MainId=A.MainId)');
//      sql.Add(' ,RKPS=(select Count(*) from CK_PBCP_CR AA where AA.MainId=A.MainId and CRFlag=''入库'')');
//      sql.Add(' ,RKQtyKg=(select sum(KgQty) from CK_PBCP_CR AA where AA.MainId=A.MainId and CRFlag=''入库'')');
//      sql.Add(' ,CKPS=(select Count(*) from CK_PBCP_CR AA where AA.MainId=A.MainId and CRFlag=''出库'')');
//      sql.Add(' ,CKQtyKg=(select sum(KgQty) from CK_PBCP_CR AA where AA.MainId=A.MainId and CRFlag=''出库'')');
//      //SQL.Add(',CustomerNoName=(select CustomerNoName  from JYOrderCon_Main_PB X where X.MainId=A.ConMainId) ');
//      sql.Add(' from Cloth_Main A');
//      sql.Add(' inner join Cloth_Sub B on A.MainId=B.MainId');
//      sql.Add(' where 1=1 and orderflag=''生产''');
//      if cxTabControl1.TabIndex > 1 then
//      begin
//        sql.Add(' and A.QDTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
//        sql.Add(' and A.QDTime<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
//      end;
//      if cxTabControl1.TabIndex = 0 then
//      begin
//        sql.Add(' and BegFlag=0 and EndFlag=0 ');
//      end
//      else if cxTabControl1.TabIndex = 1 then
//      begin
//        sql.Add(' and BegFlag=1 and EndFlag=0 ');
//      end
//      else if cxTabControl1.TabIndex = 2 then
//      begin
//        sql.Add(' and EndFlag=1 ');
//      end;

      if cxTabControl1.TabIndex > 1 then
      begin
        fwhere := fwhere + (' and A.QDTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
        fwhere := fwhere + (' and A.QDTime<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
      end;
      if cxTabControl1.TabIndex = 0 then
      begin
        fwhere := fwhere + (' and BegFlag=0 and EndFlag=0 ');
      end
      else if cxTabControl1.TabIndex = 1 then
      begin
        fwhere := fwhere + (' and BegFlag=1 and EndFlag=0 ');
      end
      else if cxTabControl1.TabIndex = 2 then
      begin
        fwhere := fwhere + (' and EndFlag=1 ');
      end;
      sql.Add('exec [dbo].[P_View_JTAP] ');

      sql.Add('@criteria= ' + quotedstr(fwhere));

      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmClothSCListAnPaiJiTaiAP.InitForm();
begin

  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 60;
  if trim(self.Caption) = '机台安排查询' then
  begin
    ToolButton6.Visible := false;
    ToolButton1.Visible := false;
    ToolButton2.Visible := false;
    ToolButton3.Visible := false;
    TBDel.Visible := false;
    ToolButton5.Visible := false;
    ToolButton7.Visible := false;
    ToolButton8.Visible := false;
    ToolButton9.Visible := false;
    Panel3.Visible := false;
    Panel2.Visible := false;
  end;
  InitGrid();
end;

procedure TfrmClothSCListAnPaiJiTaiAP.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
end;

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

procedure TfrmClothSCListAnPaiJiTaiAP.ConNoChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
end;

procedure TfrmClothSCListAnPaiJiTaiAP.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmClothSCListAnPaiJiTaiAP.conPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Length(Trim(ConNo.Text)) < 4 then
      Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        sql.Add(' select A.*,B.*');
       // sql.Add(' ,APJT=dbo.F_Get_Order_SubStr_PB(A.MainId,''APJT'')');
       // sql.Add(' ,QYJT=dbo.F_Get_Order_SubStr_PB(A.MainId,''QYJT'')');
        sql.Add(' ,QYJTQty=(Select Count(CarNo) from(select CarNo from  Cloth_JiTai JT where JT.MainId=A.MainId ');
        sql.Add('          and JT.StopFlag=0 and isnull(CarNo,'''')<>'''' Group by JT.CarNo) AA)');
       // sql.Add(' ,TYJT=dbo.F_Get_Order_SubStr_PB(A.MainId,''TYJT'')');
        //sql.Add(' ,APInfo=dbo.F_Get_Order_SubStr_PB(A.MainId,''APInfo'')');
        sql.Add(' ,APPS=(select Count(*) from JYCon_Sub_AnPai AA where AA.MainId=A.MainId)');
        sql.Add(' ,JYPS=(select Count(*) from WFB_MJJY_PB AA where AA.MainId=A.MainId)');
        sql.Add(' ,JYQtyKg=(select sum(MJMaoZ) from WFB_MJJY_PB AA where AA.MainId=A.MainId)');
        sql.Add(' ,RKPS=(select Count(*) from CK_PBCP_CR AA where AA.MainId=A.MainId and CRFlag=''入库'')');
        sql.Add(' ,RKQtyKg=(select sum(KgQty) from CK_PBCP_CR AA where AA.MainId=A.MainId and CRFlag=''入库'')');
        sql.Add(' ,CKPS=(select Count(*) from CK_PBCP_CR AA where AA.MainId=A.MainId and CRFlag=''出库'')');
        sql.Add(' ,CKQtyKg=(select sum(KgQty) from CK_PBCP_CR AA where AA.MainId=A.MainId and CRFlag=''出库'')');
        sql.Add(' from Cloth_Main A');
        sql.Add(' inner join Cloth_Sub B on A.MainId=B.MainId');
        sql.Add(' where A.ConNo like ''' + '%' + Trim(ConNo.Text) + '%' + '''');
        if cxTabControl1.TabIndex = 0 then
        begin
          sql.Add(' and BegFlag=0 and EndFlag=0 ');
        end
        else if cxTabControl1.TabIndex = 1 then
        begin
          sql.Add(' and BegFlag=1 and EndFlag=0 ');
        end
        else if cxTabControl1.TabIndex = 2 then
        begin
          sql.Add(' and EndFlag=1 ');
        end;
        Open;
      end;
      SCreateCDS20(ADOQueryMain, Order_Main);
      SInitCDSData20(ADOQueryMain, Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmClothSCListAnPaiJiTaiAP.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if cxTabControl1.TabIndex <> 0 then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要执行开始操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while Locate('SSel', True, []) = True do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate Cloth_Main Set Begtime=getdate(),BegPerson=''' + Trim(DName) + ''',BegFlag=1');
          sql.Add(' where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

procedure TfrmClothSCListAnPaiJiTaiAP.ToolButton3Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if cxTabControl1.TabIndex = 2 then
    Exit;
  if cxTabControl1.TabIndex = 3 then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要执行结束操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while Locate('SSel', True, []) = True do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate Cloth_Main Set Endtime=getdate(),EndPerson=''' + Trim(DName) + ''',EndFlag=1');
          sql.Add(' where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
          sql.Add('UPdate  Cloth_JiTai SET StopFlag=1,DOTime=getdate(),DOPerson=''' + Trim(DName) + '''');
          sql.Add(',TYTime=getdate(),TYPerson=''' + Trim(DName) + '''');
          sql.Add(' where MainId=' + quotedstr(trim(Order_Main.fieldbyname('MainId').AsString)));
          sql.Add(' and StopFlag=0');
          ExecSQL;
        end;
        UpdateJTInfo(Trim(Order_Main.fieldbyname('MainId').AsString));
        Order_Main.Delete;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

procedure TfrmClothSCListAnPaiJiTaiAP.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if cxTabControl1.TabIndex <> 2 then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要执行结束撤销操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while Locate('SSel', True, []) = True do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate Cloth_Main Set Endtime=getdate(),EndPerson=''' + Trim(DName) + ''',EndFlag=0');
          sql.Add(' where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

procedure TfrmClothSCListAnPaiJiTaiAP.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if cxTabControl1.TabIndex <> 1 then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要执行开始撤销操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while Locate('SSel', True, []) = True do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate Cloth_Main Set Begtime=getdate(),BegPerson=''' + Trim(DName) + ''',BegFlag=0');
          sql.Add(' where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

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

procedure TfrmClothSCListAnPaiJiTaiAP.ToolButton4Click(Sender: TObject);
begin
  try
    frmClothSCInPut := TfrmClothSCInPut.Create(Application);
    with frmClothSCInPut do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      FConNo := Trim(Self.Order_Main.fieldbyname('ConNo').AsString);
      TBSave.Visible := False;
      ToolBar3.Visible := False;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmClothSCInPut.Free;
  end;
end;

procedure TfrmClothSCListAnPaiJiTaiAP.ToolButton5Click(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SCPerson';
      flagname := '挡车工';
      fnote := True;
      V1Note.Caption := '车间';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmZDYHelp.free;
  end;
end;

procedure TfrmClothSCListAnPaiJiTaiAP.ToolButton6Click(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'WFBCPBZZ';
      flagname := '疵点';
      fnote := True;
     // V1Note.Caption:='英文名称';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothSCListAnPaiJiTaiAP.ToolButton7Click(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'JXPerson';
      flagname := '调试机修';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmZDYHelp.free;
  end;
end;

procedure TfrmClothSCListAnPaiJiTaiAP.ToolButton8Click(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'ClothType';
      flagname := '布匹等级';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmZDYHelp.free;
  end;
end;

procedure TfrmClothSCListAnPaiJiTaiAP.Button3Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex <> 1 then
  begin
    Application.MessageBox('只能在已开始状态下安排车号!', '提示', 0);
    Exit;
  end;
  try
    frmJTSel := TfrmJTSel.Create(Application);
    with frmJTSel do
    begin
      if ShowModal = 1 then
      begin
        with frmJTSel.ClientDataSet2 do
        begin
          First;
          while ClientDataSet2.Locate('SSel', True, []) do
          begin
            if Self.CDS_AnPai.Locate('CarNo', Trim(frmJTSel.ClientDataSet2.fieldbyname('MCNO').AsString), []) = False then
            begin
              if SaveJT(Trim(frmJTSel.ClientDataSet2.fieldbyname('MCNO').AsString), Trim(frmJTSel.ClientDataSet2.fieldbyname('MCCJ').AsString)) = False then
              begin
                Application.MessageBox('机台安排失败!', '提示', 0);
                Exit;
              end;
            end;
            ClientDataSet2.Delete;
          end;
        end;
        InitJT(Trim(Order_Main.fieldbyname('SubId').AsString), Trim(Order_Main.fieldbyname('MainId').AsString));
      end;

    end;
  finally
    frmJTSel.Free;
  end;
end;

procedure TfrmClothSCListAnPaiJiTaiAP.UpdateJTInfo(RTMainId: string);
begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update Cloth_Sub Set RTAPJTQty=(select Count(*) from Cloth_JiTai A where A.MainId=Cloth_Sub.MainId )');
    sql.Add(',RTAPJTInfo=dbo.F_Get_Order_SubStr_PB(Cloth_Sub.MainId,''RTAPJTInfo'')');
    sql.Add(',RTQYJTQty=(select Count(*) from Cloth_JiTai A where A.MainId=Cloth_Sub.MainId and StopFlag=0 )');
    sql.Add(',RTQYJTInfo=dbo.F_Get_Order_SubStr_PB(Cloth_Sub.MainId,''RTQYJTInfo'')');
    sql.Add(',RTTYJTQty=(select Count(*) from Cloth_JiTai A where A.MainId=Cloth_Sub.MainId and StopFlag=1 )');
    sql.Add(',RTTYJTInfo=dbo.F_Get_Order_SubStr_PB(Cloth_Sub.MainId,''RTTYJTInfo'')');
    sql.Add(' where MainId=''' + Trim(RTMainId) + '''');
    //Parameters.ParamByName('MainId').Value:=Trim(RTMainId);
    ExecSQL;
  end;
end;

procedure TfrmClothSCListAnPaiJiTaiAP.Button4Click(Sender: TObject);
var
  maxno, FJTID, FFConNo: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if CDS_AnPai.IsEmpty then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_AnPai.DisableControls;
    with CDS_AnPai do
    begin
      First;
      while not Eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Cloth_JiTai where JTID=''' + Trim(CDS_AnPai.fieldbyname('JTID').AsString) + '''');
          Open;
        end;
        FJTID := Trim(ADOQueryTemp.fieldbyname('JTID').AsString);
        ADOQueryTemp.Close;
        if Trim(FJTID) = '' then
        begin
          if GetLSNo(ADOQueryCmd, maxno, 'JT', 'Cloth_JiTai', 3, 1) = False then
          begin
            CDS_AnPai.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取安排机台流水号失败!', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(FJTID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Cloth_JiTai where JTID=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FJTID) = '' then
          begin
            Append;
          end
          else
          begin
            Edit;
          end;
          FieldByName('MainId').Value := Trim(Order_Main.fieldbyname('MainId').AsString);
          FieldByName('SubId').Value := Trim(Order_Main.fieldbyname('SubId').AsString);
          FieldByName('JTID').Value := Trim(maxno);
          FieldByName('CarNo').Value := CDS_AnPai.fieldbyname('CarNo').Value;
          FieldByName('CheJian').Value := CDS_AnPai.fieldbyname('CheJian').Value;
          if CDS_AnPai.FieldByName('StopFlag').AsBoolean = True then
          begin
            FieldByName('StopFlag').Value := 1;
          end
          else
          begin
            FieldByName('StopFlag').Value := 0;
            if Trim(CDS_AnPai.fieldbyname('JTID').AsString) = '' then
            begin
              FieldByName('DOTime').Value := SGetServerDateTime(ADOQueryTemp);
              FieldByName('DOPerson').Value := Trim(DName);
              FieldByName('QYTime').Value := SGetServerDateTime(ADOQueryTemp);
              FieldByName('QYPerson').Value := Trim(DName);
            end;
          end;
          //FieldByName('StopFlag').Value:=0;
          if Trim(FJTID) = '' then
          begin
            FieldByName('Filler').Value := Trim(DName);
          end
          else
          begin
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTemp);
          end;
          Post;
        end;
        {with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select ConNo=(select ConNo from Cloth_Main B where B.Mainid=A.Mainid)  ');
          sql.Add(' from Cloth_JiTai A  where CarNo='''+Trim(CDS_AnPai.fieldbyname('CarNo').AsString)+'''');
          sql.Add(' and StopFlag=0 and JTID<>'''+Trim(maxno)+'''');
          Open;
        end;
        FFConNo:=Trim(ADOQueryTemp.fieldbyname('ConNo').AsString);
        if Trim(FFConNo)<>'' then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update Cloth_JiTai Set StopFlag=1 where JTID='''+trim(maxno)+'''');
            ExecSQL;
          end;
        end; }
        Edit;
        FieldByName('JTID').Value := Trim(maxno);
        {if Trim(FFConNo)<>'' then
        begin
          FieldByName('StopFlag').Value:=True;
        end else
        begin
          FieldByName('StopFlag').Value:=false;
        end;  }
        Post;
        Next;
      end;
    end;
    CDS_AnPai.EnableControls;
    {with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' Update CLoth_Sub Set QYJTHZ=dbo.F_Get_Order_SubStr_PB(:MainId,''QYJT'')');
      sql.Add(' where MainID='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      Parameters.ParamByName('MainId').Value:=Trim(Order_Main.fieldbyname('MainId').AsString);
      ExecSQL;
    end;  }
    UpdateJTInfo(Trim(Order_Main.fieldbyname('MainId').AsString));
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!', '提示', 0);
    Exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    CDS_AnPai.EnableControls;
    Application.MessageBox('保存异常！', '提示', 0);
  end;
end;

procedure TfrmClothSCListAnPaiJiTaiAP.Button6Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if CDS_AnPai.IsEmpty then
    exit;
  if CDS_AnPai.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据！', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要执行启用操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_AnPai.DisableControls;
    with CDS_AnPai do
    begin
      First;
      while CDS_AnPai.Locate('SSel', True, []) do
      begin
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('update  Cloth_JiTai SET StopFlag=0,DOTime=getdate(),DOPerson=''' + Trim(DName) + '''');
          sql.Add(',QYTime=getdate(),QYPerson=''' + Trim(DName) + '''');
          sql.Add('where JTID=' + quotedstr(trim(CDS_AnPai.fieldbyname('JTID').AsString)));
          execsql;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select ConNo=(select ConNo from Cloth_Main B where B.Mainid=A.Mainid)  ');
          sql.Add(' from Cloth_JiTai A  where CarNo=''' + Trim(CDS_AnPai.fieldbyname('CarNo').AsString) + '''');
          sql.Add(' and StopFlag=0 and JTID<>' + quotedstr(trim(CDS_AnPai.fieldbyname('JTID').AsString)));
          Open;
        end;
        if ADOQueryTemp.IsEmpty = False then
        begin
          if Application.MessageBox(Pchar('订单号：' + Trim(ADOQueryTemp.fieldbyname('ConNo').AsString) + '已占用' + Trim(CDS_AnPai.fieldbyname('CarNo').AsString) + '车号,确定要开启此车号吗？'), '提示', 32 + 4) <> IDYES then
          begin
            with ADOQueryCmd do
            begin
              close;
              sql.Clear;
              sql.Add('update  Cloth_JiTai SET StopFlag=1,DOTime=Null,DOPerson=Null');
              sql.Add(',TYTime=getdate(),TYPerson=''' + Trim(DName) + '''');
              sql.Add('where JTID=' + quotedstr(trim(CDS_AnPai.fieldbyname('JTID').AsString)));
              execsql;
            end;
            //ADOQueryCmd.Connection.CommitTrans;
            Edit;
            FieldByName('SSel').Value := False;
            FieldByName('StopFlag').Value := True;
          end
          else
          begin
            with ADOQueryCmd do
            begin
              close;
              sql.Clear;
              sql.Add('update  Cloth_JiTai SET StopFlag=1,DOTime=getdate(),DOPerson=''' + Trim(DName) + '''');
              sql.Add(',TYTime=getdate(),TYPerson=''' + Trim(DName) + '''');
              sql.Add('where JTID<>' + quotedstr(trim(CDS_AnPai.fieldbyname('JTID').AsString)));
              sql.Add(' and CarNo=''' + Trim(CDS_AnPai.fieldbyname('CarNo').AsString) + ''' and StopFlag=0 ');
              execsql;
            end;
            Edit;
            FieldByName('SSel').Value := False;
            FieldByName('StopFlag').Value := False;
          end;
        end
        else
        begin
          Edit;
          FieldByName('SSel').Value := False;
          FieldByName('StopFlag').Value := False;
        end;
      end;
    end;
    CDS_AnPai.EnableControls;
    UpdateJTInfo(Trim(Order_Main.fieldbyname('MainId').AsString));
    ADOQueryCmd.Connection.CommitTrans;
    InitJT(Trim(Order_Main.fieldbyname('SubId').AsString), Trim(Order_Main.fieldbyname('MainId').AsString));
   // Application.MessageBox('启用成功!','提示',0);
    exit;
  except
    application.MessageBox('启用失败！', '提示信息', 0);
  end;
end;

procedure TfrmClothSCListAnPaiJiTaiAP.Button5Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if CDS_AnPai.IsEmpty then
    exit;
  if CDS_AnPai.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据！', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要执行停用操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_AnPai.DisableControls;
    with CDS_AnPai do
    begin
      First;
      while CDS_AnPai.Locate('SSel', True, []) do
      begin
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('update  Cloth_JiTai SET StopFlag=1,DOTime=getdate(),DOPerson=''' + Trim(DName) + '''');
          sql.Add(',TYTime=getdate(),TYPerson=''' + Trim(DName) + '''');
          sql.Add('where JTID=' + quotedstr(trim(CDS_AnPai.fieldbyname('JTID').AsString)));
          execsql;
        end;
        Edit;
        FieldByName('SSel').Value := False;
        FieldByName('StopFlag').Value := True;
      end;
    end;
    CDS_AnPai.EnableControls;
    UpdateJTInfo(Trim(Order_Main.fieldbyname('MainId').AsString));
    ADOQueryCmd.Connection.CommitTrans;
    InitJT(Trim(Order_Main.fieldbyname('SubId').AsString), Trim(Order_Main.fieldbyname('MainId').AsString));
    //Application.MessageBox('停用成功!','提示',0);
    exit;
  except
    application.MessageBox('停用失败！', '提示信息', 0);
  end;
end;

procedure TfrmClothSCListAnPaiJiTaiAP.Button7Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if CDS_AnPai.IsEmpty then
    exit;
  if CDS_AnPai.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据！', '提示', 0);
    Exit;
  end;

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_AnPai.DisableControls;
    with CDS_AnPai do
    begin
      First;
      while CDS_AnPai.Locate('SSel', True, []) do
      begin
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('delete Cloth_JiTai ');
          sql.Add('where JTID=' + quotedstr(trim(CDS_AnPai.fieldbyname('JTID').AsString)));
          sql.Add(' and not exists(select * from JYCon_Sub_AnPai A where A.MainId=Cloth_JiTai.MainId ');
          sql.Add('  and A.CarNo=Cloth_JiTai.CarNo)');
          execsql;
        end;
        CDS_AnPai.Delete;
      end;
    end;
    CDS_AnPai.EnableControls;
    UpdateJTInfo(Trim(Order_Main.fieldbyname('MainId').AsString));
    ADOQueryCmd.Connection.CommitTrans;
    //Application.MessageBox('停用成功!','提示',0);
    exit;
  except
    application.MessageBox('删除失败！', '提示信息', 0);
  end;
end;

procedure TfrmClothSCListAnPaiJiTaiAP.ToolButton10Click(Sender: TObject);
begin
  try
    frmJTSel := TfrmJTSel.Create(Application);
    with frmJTSel do
    begin

      if ShowModal = 1 then
      begin
        with frmJTSel.ClientDataSet1 do
        begin

        end;
      end;
    end;
  finally
    frmJTSel.Free;
  end;
end;

procedure TfrmClothSCListAnPaiJiTaiAP.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  if Order_Main.IsEmpty then
    exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add(' select A.*');
    sql.Add(',APPS=(select Count(APID) from JYCon_Sub_AnPai AP where AP.MainId=A.MainId and AP.CarNo=A.CarNo)');
    sql.Add(' from Cloth_JiTai A');
    sql.Add(' where A.SubId=''' + Trim(Order_Main.fieldbyname('SubId').AsString) + '''');
    sql.Add(' and A.MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_AnPai);
  SInitCDSData20(ADOQueryTemp, CDS_AnPai);
end;

procedure TfrmClothSCListAnPaiJiTaiAP.ToolButton9Click(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'WorkShift';
      flagname := '班次';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmZDYHelp.free;
  end;
end;

procedure TfrmClothSCListAnPaiJiTaiAP.ToolButton11Click(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'houdang';
      flagname := '后档';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmZDYHelp.free;
  end;
end;

procedure TfrmClothSCListAnPaiJiTaiAP.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));
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
end;

procedure TfrmClothSCListAnPaiJiTaiAP.FormCreate(Sender: TObject);
begin
  ReadCxGrid(trim(self.Caption), Tv1, '指示单管理5');
end;

end.

