unit U_TradePlanColorList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid, cxMemo,
  cxRichEdit, ComCtrls, cxContainer, cxTextEdit, cxMaskEdit, cxButtonEdit,
  StdCtrls, ToolWin, DBClient, ADODB, ExtCtrls, BtnEdit, cxCalendar, StrUtils,
  cxCurrencyEdit, cxImage, cxDBEdit, Menus, RM_Common, RM_Class, RM_e_Xls,
  RM_Dataset, RM_System, RM_GridReport, cxGridCustomPopupMenu, cxGridPopupMenu,
  cxPC, cxSplitter, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdFTP, ShellAPI, cxDropDownEdit, cxCheckBox, RM_e_Graphic, RM_e_Jpeg,
  U_BaseList, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxDateRanges,
  dxScrollbarAnnotations, dxBarBuiltInMenu, IdExplicitTLSClientServerBase,
  FrameDateSel, cxProgressBar, dxSkinsCore, dxSkinsDefaultPainters;

type
  TfrmTradePlanColorList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxgrdpmn1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNo: TcxTextEdit;
    v1OrderNo: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    TBCK: TToolButton;
    btnCopy: TToolButton;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    ToolButton4: TToolButton;
    cxTabControl1: TcxTabControl;
    Tchk: TToolButton;
    Tnochk: TToolButton;
    TWC: TToolButton;
    TNowc: TToolButton;
    Panel2: TPanel;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    IdFTP1: TIdFTP;
    v1Column11: TcxGridDBColumn;
    TBTJ: TToolButton;
    TBTJCX: TToolButton;
    v1Column16: TcxGridDBColumn;
    ORDER_SUB: TClientDataSet;
    PopupMenu2: TPopupMenu;
    N3: TMenuItem;
    N4: TMenuItem;
    Label1: TLabel;
    CustName: TcxTextEdit;
    Tv1Column6: TcxGridDBColumn;
    cxgrdpmn2: TcxGridPopupMenu;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    VC_PRTPs: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Label2: TLabel;
    C_Name: TcxTextEdit;
    CDS_WXTS: TClientDataSet;
    Label4: TLabel;
    ConNo: TcxTextEdit;
    Tv1Column22: TcxGridDBColumn;
    ToolButton2: TToolButton;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    GPM_3: TcxGridPopupMenu;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    TV2Column1: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    N5: TMenuItem;
    N6: TMenuItem;
    ToolButton3: TToolButton;
    TV2Column2: TcxGridDBColumn;
    frmFrameDateSel1: TfrmFrameDateSel;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    Panel3: TPanel;
    CDS_3: TClientDataSet;
    DS_3: TDataSource;
    ADO_3: TADOQuery;
    cxSplitter1: TcxSplitter;
    ToolButton1: TToolButton;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    TV2Column6: 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;
    cxGridLevel2: TcxGridLevel;
    cxSplitter2: TcxSplitter;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    CraftCode: TcxTextEdit;
    GC_Name: TcxTextEdit;
    Saleser: TcxTextEdit;
    TV3Column1: TcxGridDBColumn;
    TV3Column2: TcxGridDBColumn;
    TV3Column3: TcxGridDBColumn;
    TV3Column4: TcxGridDBColumn;
    TV3Column5: TcxGridDBColumn;
    TV3Column6: TcxGridDBColumn;
    TV3Column7: TcxGridDBColumn;
    TV3Column8: TcxGridDBColumn;
    TV2Column7: TcxGridDBColumn;
    Panel4: TPanel;
    Panel5: TPanel;
    tlb2: TToolBar;
    btnConSel: TToolButton;
    ToolButton11: TToolButton;
    ToolButton5: TToolButton;
    Label8: TLabel;
    QS: TcxTextEdit;
    JS: TcxTextEdit;
    ToolButton6: TToolButton;
    Tv1Column3: TcxGridDBColumn;
    TBFind: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure OrderNoMChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBTPClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure v1DeliveryDateCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure TBCKClick(Sender: TObject);
    procedure btnCopyClick(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton4Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure TWCClick(Sender: TObject);
    procedure TNowcClick(Sender: TObject);
    procedure TBTJClick(Sender: TObject);
    procedure TBTJCXClick(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure CustomerNoNameKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton10Click(Sender: TObject);
    procedure JGTypeChange(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure btnConSelClick(Sender: TObject);
    procedure TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxGridDBColumn4PropertiesEditValueChanged(Sender: TObject);
    procedure TV2Column5PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton11Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure TV3DblClick(Sender: TObject);
    procedure frmFrameDateSel1cxButton4Click(Sender: TObject);
    procedure frmFrameDateSel1cxButton1Click(Sender: TObject);
    procedure frmFrameDateSel1cxButton5Click(Sender: TObject);
    procedure frmFrameDateSel1cxButton2Click(Sender: TObject);
    procedure frmFrameDateSel1cxButton3Click(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure ConNoPropertiesChange(Sender: TObject);
  private
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
    procedure InitSub();
    procedure InitTechnical();
    procedure SetStatus();
    procedure InitImage(fwbid: string);
    procedure SendMessageQR(MDateId, KeyWord1, KeyWord2, KeyWord3: string);
    procedure PrtDS(DSCS: string);
    function GetDSCS(MQRK: string): Integer;
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1, canshu2: string;
    fFlileFlag: string;
    FNOTE: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_TradePlanColorInPut, U_RTFun, U_ZDYHelp, U_LabelPrint,
  U_TradeInspSet, U_AttachmentUpload, U_SysLogHelp, U_TradePlanColorResultInput,
  U_TradePlanColorLogInput;

{$R *.dfm}
function TfrmTradePlanColorList.GetDSCS(MQRK: string): Integer;
begin
  Result := 1;

  if TRIM(MQRK) = 'A' then
    Result := 1;
  if TRIM(MQRK) = 'B' then
    Result := 2;
  if TRIM(MQRK) = 'C' then
    Result := 3;
  if TRIM(MQRK) = 'D' then
    Result := 4;
  if TRIM(MQRK) = 'E' then
    Result := 5;
  if TRIM(MQRK) = 'F' then
    Result := 6;
  if TRIM(MQRK) = 'G' then
    Result := 7;
  if TRIM(MQRK) = 'H' then
    Result := 8;
  if TRIM(MQRK) = 'I' then
    Result := 9;
  if TRIM(MQRK) = 'J' then
    Result := 10;
  if TRIM(MQRK) = 'K' then
    Result := 11;
  if TRIM(MQRK) = 'L' then
    Result := 12;
  if TRIM(MQRK) = 'M' then
    Result := 13;
  if TRIM(MQRK) = 'N' then
    Result := 14;
  if TRIM(MQRK) = 'O' then
    Result := 15;
  if TRIM(MQRK) = 'P' then
    Result := 16;
  if TRIM(MQRK) = 'Q' then
    Result := 17;
  if TRIM(MQRK) = 'R' then
    Result := 18;
  if TRIM(MQRK) = 'S' then
    Result := 19;
  if TRIM(MQRK) = 'T' then
    Result := 20;
  if TRIM(MQRK) = 'U' then
    Result := 21;
  if TRIM(MQRK) = 'V' then
    Result := 22;
  if TRIM(MQRK) = 'W' then
    Result := 22;
  if TRIM(MQRK) = 'X' then
    Result := 24;
end;

procedure TfrmTradePlanColorList.InitImage(fwbid: string);
begin
  ADOQueryImage.close;
  if fwbid = '' then
    exit;
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where WBID=' + quotedstr(trim(fwbid)));
    open;
  end;
end;

procedure TfrmTradePlanColorList.SetStatus();
begin
  btnCopy.Visible := false;
  tbadd.Visible := false;
  TBEdit.Visible := False;
  TBDel.Visible := False;
  TBTJ.Visible := False;
  TBTJCX.Visible := False;
  tchk.Visible := false;
  tnochk.Visible := false;
  TWC.Visible := False;
  TNowc.Visible := False;
  TBDel.Visible := False;

  case cxTabControl1.TabIndex of
    0:
      begin
        tbadd.Visible := True;
        TBEdit.Visible := True;
        TBDel.Visible := True;
        TBDel.Visible := True;
        TBTJ.Visible := True;
        btnCopy.Visible := True;
      end;
    1:
      begin
        TBTJCX.Visible := True;
        btnCopy.Visible := True;
        TBDel.Visible := True;
        tchk.Visible := True;

      end;
    2:
      begin
        TWC.Visible := True;
        btnCopy.Visible := True;
        tnochk.Visible := True;
      end;
    3:
      begin
        TNowc.Visible := True;
        btnCopy.Visible := True;
      end;
  end;

end;

procedure TfrmTradePlanColorList.InitSub();
var
  SqlStr: string;
begin
  if Order_Main.IsEmpty then
    exit;
  SqlStr := 'select *   from Trade_Plan_Sub A ';
  SqlStr := SqlStr + ' where mainid =' + quotedstr((Order_Main.fieldbyname('mainID').AsString));

  InitCDSData(ADOQuerySub, ORDER_SUB, Tv2, SqlStr, '', '');

end;

procedure TfrmTradePlanColorList.InitTechnical();
var
  SqlStr: string;
begin
  if ORDER_SUB.IsEmpty then
    exit;
  SqlStr := ' select A.* ';
  SqlStr := SqlStr + ' ,B.Result1,B.Result2,B.Result3,B.Result4,B.Result5,B.Result6,B.Result7,B.Result8,B.Result9,B.Result10 ';
  SqlStr := SqlStr + ' ,B.Result11,B.Result12,B.Result13,B.Result14,B.Result15,B.Result16,B.Result17,B.Result18,B.Result19,B.Result20 ';
  SqlStr := SqlStr + ' ,B.Result21,B.Result22,B.Result23,B.Result24  ';
  SqlStr := SqlStr + ' from Trade_Plan_Technical A left join Trade_Plan_Technical_Results B on A.TPTID=B.TPTID ';
  SqlStr := SqlStr + ' and B.Subid =' + quotedstr(Trim(ORDER_SUB.fieldbyname('Subid').AsString));
  SqlStr := SqlStr + ' where A.mainID =' + quotedstr(Trim(ORDER_SUB.fieldbyname('mainID').AsString));
//  ShowMessage(SqlStr);
  InitCDSData(ADO_3, CDS_3, Tv3, SqlStr, '', '');

end;

procedure TfrmTradePlanColorList.JGTypeChange(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmTradePlanColorList.FormDestroy(Sender: TObject);
begin
  inherited;
//  frmProductOrderList:=nil;
end;

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

procedure TfrmTradePlanColorList.FormCreate(Sender: TObject);
begin
  inherited;
  DQdate := SGetServerDate(ADOQueryTemp);
end;

procedure TfrmTradePlanColorList.TBCloseClick(Sender: TObject);
begin

  Close;

end;

procedure TfrmTradePlanColorList.InitGrid();
var
  SqlStr: string;
begin

  SqlStr := ' select  A.*  from V_Trade_Plan_Color A';
  SqlStr := SqlStr + ' where OrdType=''打色单'' ';
  SqlStr := SqlStr + ' and A.OrdDate>=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.BegDate.Date)));
  SqlStr := SqlStr + ' and  A.OrdDate<' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.enddate.Date + 1)));
  if canshu1 = '录入' then
  begin
    SqlStr := SqlStr + 'and exists(select X.DataCode from BS_OperatorAuthority X where X.DataCode=A.Mainid and X.UserId=' + quotedstr(trim(DCode)) + ' )   ';
  end;

  case cxTabControl1.TabIndex of
    0:
      begin
        SqlStr := SqlStr + ' and  isnull(status,''0'')=''0'' ';
      end;
    1:
      begin
        SqlStr := SqlStr + ' and  isnull(status,''0'')=''1'' ';
      end;
    2:
      begin
        SqlStr := SqlStr + ' and  isnull(status,''0'')=''9'' ';
      end;
    3:
      begin
        SqlStr := SqlStr + ' and  isnull(status,''0'')=''10'' ';
      end;
  end;

  SqlStr := SqlStr + ' order by  filltime desc';

  InitCDSData(ADOQueryMain, Order_Main, Tv1, SqlStr, SGetFilters(Panel1, 1, 2), 'Mainid');
end;

procedure TfrmTradePlanColorList.SendMessageQR(MDateId, KeyWord1, KeyWord2, KeyWord3: string);
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select distinct A.* from SY_User A inner join SY_Purview B on A.UserID=B.UserID  where A.Valid=''Y'' ');
    SQL.Add('and isnull(A.wxid,'''')<>''''');
    SQL.Add(' and exists(select 1 from SY_ModuleSub X where X.ModuleID=B.ModuleID and X.ModuleSubID=B.ModuleSubID and X.FormName=''生产计划单审核'' )');
//    showmessage(sql.text);
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_WXTS);
  SInitCDSData(ADOQueryTemp, CDS_WXTS);

  if CDS_WXTS.IsEmpty then
  begin
    Application.MessageBox('用户未绑定微信,提交失败！', '提示信息', 0);
    Exit;
  end;
  CDS_WXTS.First;
  while not CDS_WXTS.eof do
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from WeixinMessage where 1<>1');
      Open;
      Append;
      FieldByName('MessageType').Value := 'OrderToAudit';
      FieldByName('MessageName').Value := '计划单待审核';
      FieldByName('Title').Value := '您有新的生产计划单待审核';
      FieldByName('URL').Value := 'subpages/plan/view?id=' + Trim(MDateId);
      FieldByName('OpenId').Value := Trim(CDS_WXTS.FieldByName('wxid').AsString);
      FieldByName('Status').Value := '0';
      FieldByName('CreateDate').Value := FormatDateTime('yyyymmdd', SGetServerDateTime(ADOQueryTemp));
      FieldByName('CreateTime').Value := FormatDateTime('HH:mm:ss', SGetServerDateTime(ADOQueryTemp));
      FieldByName('BusinessId').Value := Trim(MDateId);
      FieldByName('KeyWord1').Value := Trim(KeyWord1);
      FieldByName('KeyWord2').Value := Trim(KeyWord2);
      FieldByName('KeyWord3').Value := Trim(KeyWord3);
      FieldByName('Remark').Value := '点击可查询详情！';
      Post;
    end;
    CDS_WXTS.Next;
  end;

  GetHTTP('http://aotang.rightsoft.top/api/message/send/OrderToAudit');
end;

procedure TfrmTradePlanColorList.InitForm();
begin

  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '生产指示单管理');
  ReadCxGrid(trim(self.Caption) + 'Tv2', Tv2, '生产指示单管理');
  ReadCxGrid(trim(self.Caption) + 'Tv3', Tv3, '生产指示单管理');

  frmFrameDateSel1.BegDate.Date := SGetServerDate(ADOQueryTemp) - 180;
  frmFrameDateSel1.EndDate.Date := SGetServerDate(ADOQueryTemp);

  SetStatus();
  InitGrid();
end;

procedure TfrmTradePlanColorList.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('EXEC  P_Authority_Controls   ');
    sql.Add(' @DataType =''贸易计划单'' ');
    sql.Add(',@DataCode=' + QuotedStr(Trim(Self.Order_Main.fieldbyname('MainId').AsString)));
    execsql;
  end;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.MainId  from Trade_Plan_Main A  ');
    sql.Add(' where MainId=' + QuotedStr(Trim(Self.Order_Main.fieldbyname('MainId').AsString)));
    sql.Add(' and exists(select X.DataCode from BS_OperatorAuthority X where X.DataCode=A.MainId and X.UserId=' + quotedstr(trim(DCode)) + ' )   ');
//    ShowMessage(SQL.Text);
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    application.MessageBox('操作员权限不足', '提示信息', 0);
    exit;
  end;

  if not assigned(frmTradePlanColorInPut) then
  begin
    try
      frmTradePlanColorInPut := TfrmTradePlanColorInPut.Create(Application);
      with frmTradePlanColorInPut do
      begin

        PState := 1;
        FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
        FOrderNo := Trim(Self.Order_Main.fieldbyname('OrderNo').AsString);
        canshu1 := self.canshu1;
        Show;
      end;
    finally
//      frmTradePlanColorInPut.Free;
    end;

  end
  else
  begin
    frmTradePlanColorInPut.WindowState := wsMaximized;
    frmTradePlanColorInPut.Show;
  end;

end;

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

procedure TfrmTradePlanColorList.TBDelClick(Sender: TObject);
var
  rtvalues: TArray<string>;
begin
  if Order_Main.IsEmpty then
    exit;

  rtvalues := SelCDSKey(Order_Main, ['MainId', 'OrderNo']);

  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from  Trade_Plan_Main A ');
    sql.Add(' where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where A.MainId=X.RTValue )  ');
    sql.Add(' and Filler<>' + QuotedStr(Trim(Order_Main.fieldbyname('Filler').AsString)));
    open;
  end;
  if not ADOQueryTemp.IsEmpty then
  begin
    Application.MessageBox('不能操作他人的数据!', '提示', 0);
    Exit;
  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Trade_Cloth_Inspect where isnull(mainID,'''')<>''''  ');
    sql.Add(' and EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where Trade_Cloth_Inspect.MainId=X.RTValue )  ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已检验不能删除数据!', '提示', 0);
    Exit;
  end;

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

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update Trade_Plan_Main set DelId=' + QuotedStr(Trim(DCode)) + ',Deler=' + QuotedStr(Trim(dname)) + ',Deltime=GETDATE()  ');
    sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where Trade_Plan_Main.MainId=X.RTValue )  ');

    sql.Add('insert into Trade_Plan_Main_Del select * from Trade_Plan_Main ');
    sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where Trade_Plan_Main.MainId=X.RTValue )  ');

    sql.Add('delete Trade_Plan_Main ');
    sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where Trade_Plan_Main.MainId=X.RTValue )  ');

    sql.Add('update Trade_Plan_Sub set DelId=' + QuotedStr(Trim(DCode)) + ',Deler=' + QuotedStr(Trim(dname)) + ',Deltime=GETDATE()  ');
    sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where Trade_Plan_Sub.MainId=X.RTValue )  ');

    sql.Add('insert into Trade_Plan_Sub_Del select * from Trade_Plan_Sub ');
    sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where Trade_Plan_Sub.MainId=X.RTValue )  ');

    sql.Add('delete Trade_Plan_Sub ');
    sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where Trade_Plan_Sub.MainId=X.RTValue )  ');

    sql.Add('update Trade_Plan_Fty set DelId=' + QuotedStr(Trim(DCode)) + ',Deler=' + QuotedStr(Trim(dname)) + ',Deltime=GETDATE()  ');
    sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where Trade_Plan_Fty.MainId=X.RTValue )  ');

    sql.Add('insert into Trade_Plan_Fty_Del select * from Trade_Plan_Fty ');
    sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where Trade_Plan_Fty.MainId=X.RTValue )  ');

    sql.Add('delete Trade_Plan_Fty ');
    sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where Trade_Plan_Fty.MainId=X.RTValue )  ');
    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(rtvalues[1]))));
    sql.Add(',' + quotedstr(trim('成功')));
    sql.Add(')');
    ExecSQL;
  end;

  InitGrid();
end;

procedure TfrmTradePlanColorList.TBPrintClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'TradePlanPrint12';
      FFiltration1 := Trim(Order_Main.fieldbyname('MainId').AsString);
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

procedure TfrmTradePlanColorList.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  InitGrid();
end;

procedure TfrmTradePlanColorList.TBAddClick(Sender: TObject);
begin

  if not assigned(frmTradePlanColorInPut) then
  begin
    try
      frmTradePlanColorInPut := TfrmTradePlanColorInPut.Create(Application);
      with frmTradePlanColorInPut do
      begin
        PState := 0;
        FMainId := '';
        canshu1 := self.canshu1;
        Show;
       // if ShowModal = 1 then
       // begin
        //  InitGrid();
       // end;
      end;
    finally
      //frmTradePlanColorInPut.Free;
    end;
  end
  else
  begin
    frmTradePlanColorInPut.WindowState := wsMaximized;
    frmTradePlanColorInPut.Show;
  end;
end;

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

procedure TfrmTradePlanColorList.FormShow(Sender: TObject);
begin
  inherited;
  canshu1 := self.fParameters1;
  fFlileFlag := UserDataFlag + 'HX';
  cxTabControl1.TabIndex := 0;
  InitForm();
end;

procedure TfrmTradePlanColorList.frmFrameDateSel1cxButton1Click(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.cxButton1Click(Sender);
  ToolBar1.SetFocus;
  InitGrid();
end;

procedure TfrmTradePlanColorList.frmFrameDateSel1cxButton2Click(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.cxButton2Click(Sender);
  ToolBar1.SetFocus;
  InitGrid();
end;

procedure TfrmTradePlanColorList.frmFrameDateSel1cxButton3Click(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.cxButton3Click(Sender);
  ToolBar1.SetFocus;
  InitGrid();
end;

procedure TfrmTradePlanColorList.frmFrameDateSel1cxButton4Click(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.cxButton4Click(Sender);
  ToolBar1.SetFocus;
  InitGrid();
end;

procedure TfrmTradePlanColorList.frmFrameDateSel1cxButton5Click(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.cxButton5Click(Sender);
  ToolBar1.SetFocus;
  InitGrid();
end;

procedure TfrmTradePlanColorList.TBTPClick(Sender: TObject);
var
  FQty, FQty1, FMxQty, FPQty, FMxQtyS, FPQtyS: string;
begin
end;

procedure TfrmTradePlanColorList.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmTradePlanColorList.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmTradePlanColorList.Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  id, id10: Integer;
begin
  {try
    if Tv1.GroupedItemCount=0 then
    begin
      Id:=Tv1.GetColumnByFieldName('DeliveryDate').Index-tv1.GroupedItemCount;
      Id10:=Tv1.GetColumnByFieldName('SubStatus').Index-tv1.GroupedItemCount;
      if Trim(VarToStr(ARecord.Values[id]))='' then Exit;
      if Id<0 then Exit;
      if ARecord.Values[id10]='完成' then exit;
      if (ARecord.Values[id]-DQdate)>=4 then Exit;
      if ((ARecord.Values[id]-DQdate)>=0) and ((ARecord.Values[id]-DQdate)<4) then
        AStyle:=DataLink_.QHuangSe
      else
      if ARecord.Values[id]-DQdate<0 then
      begin
        AStyle:=DataLink_OrderManage.FenHongS;
      end;
    end else
    begin

    end;
  except
  end; }
end;

procedure TfrmTradePlanColorList.TV2Column5PropertiesEditValueChanged(Sender: TObject);
var
  FC_ColorConfirm: string;
begin
//  FC_ColorConfirm := TcxTextEdit(Sender).EditingText;
//  try
//    with ADOQueryCmd do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add(' update Trade_Plan_Sub   ');
//      sql.Add(' set  C_ColorConfirm=' + quotedstr(FC_ColorConfirm));
//      sql.Add('where SubId =' + quotedstr((order_Sub.fieldbyname('SubId').AsString)));
////      showmessage(sql.text);
//      ExecSQL;
//    end;
//    ModalResult := 1;
//  except
//    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
//
//  end;

end;

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

procedure TfrmTradePlanColorList.TV3DblClick(Sender: TObject);
begin
  if trim(QS.Text) = '' then
  begin
    QS.Text := Trim(Tv3.Controller.FocusedColumn.Caption);
  end
  else
  begin
    if trim(JS.Text) = '' then
    begin
      JS.Text := Trim(Tv3.Controller.FocusedColumn.Caption);
    end
    else
    begin
      QS.Text := JS.Text;
      JS.Text := Trim(Tv3.Controller.FocusedColumn.Caption);
    end;
  end;
end;

procedure TfrmTradePlanColorList.v1DeliveryDateCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
 { Id:=TV1.GetColumnByFieldName('DeliveryDate').Index;//;-TV1.GroupedItemCount;
  Id10:=TV1.GetColumnByFieldName('SubStatus').Index;
  if Id<0 then Exit;
  if AViewInfo.GridRecord.Values[Id10]='完成' then Exit;
  if AViewInfo.GridRecord.Values[Id]-SGetServerDate(ADOQueryTemp)>=4 then Exit;
  if ((AViewInfo.GridRecord.Values[id]-SGetServerDate(ADOQueryTemp))>=0) and ((AViewInfo.GridRecord.Values[id]-SGetServerDate(ADOQueryTemp))<4) then
    ACanvas.Brush.Color:=clYellow
  else
  if (AViewInfo.GridRecord.Values[id])-(SGetServerDate(ADOQueryTemp)<0) then
  begin
    ACanvas.Brush.Color:=clRed;
  end;
  begin
    ACanvas.Brush.Color:=clRed;
  end else
  if AViewInfo.GridRecord.Values[Id]='Purple' then
  begin
    ACanvas.Brush.Color:=clPurple;
  end else
  if AViewInfo.GridRecord.Values[Id]='Olive' then
  begin
    ACanvas.Brush.Color:=clOlive;
  end else
  if AViewInfo.GridRecord.Values[Id]='Teal' then
  begin
    ACanvas.Brush.Color:=clTeal;
  end else
  if AViewInfo.GridRecord.Values[Id]='Background' then
  begin
    ACanvas.Brush.Color:=clBackground;
  end;   }
end;

procedure TfrmTradePlanColorList.TBCKClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if not assigned(frmTradePlanColorInPut) then
  begin
    try
      frmTradePlanColorInPut := TfrmTradePlanColorInPut.Create(Application);
      with frmTradePlanColorInPut do
      begin

        PState := 1;
        FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);

        TBSave.Visible := False;
        ScrollBox1.Enabled := True;
        N2.Visible := False;
        Show;
      end;
    finally
//      frmTradePlanColorInPut.Free;
    end;

  end
  else
  begin
    frmTradePlanColorInPut.WindowState := wsMaximized;
    frmTradePlanColorInPut.Show;
  end;

end;

procedure TfrmTradePlanColorList.btnConSelClick(Sender: TObject);
begin
  try
    frmTradePlanColorResultInput := TfrmTradePlanColorResultInput.Create(Application);
    with frmTradePlanColorResultInput do
    begin
      FmainID := Order_Main.fieldbyname('mainID').AsString;
      FSubId := Order_SUB.fieldbyname('SubId').AsString;
      if ShowModal = 1 then
      begin
        InitTechnical();
      end;
    end;
  finally
    frmTradePlanColorResultInput.Free;
  end;
end;

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

  if not assigned(frmTradePlanColorInPut) then
  begin
    try
      frmTradePlanColorInPut := TfrmTradePlanColorInPut.Create(Application);
      with frmTradePlanColorInPut do
      begin
        PState := 1;
        CopyInt := 99;
        FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
        canshu1 := self.canshu1;
        Show;
       // if ShowModal = 1 then
       // begin
        //  InitGrid();
       // end;
      end;
    finally
      //frmTradePlanColorInPut.Free;
    end;
  end
  else
  begin
    frmTradePlanColorInPut.WindowState := wsMaximized;
    frmTradePlanColorInPut.Show;
  end;

end;

procedure TfrmTradePlanColorList.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin

  if Key = #13 then
  begin
    initgrid();
  end;
end;

procedure TfrmTradePlanColorList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  CopyTVFocusValue(TV1);
end;

procedure TfrmTradePlanColorList.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitSub();

end;

procedure TfrmTradePlanColorList.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 TfrmTradePlanColorList.ToolButton5Click(Sender: TObject);
var
  WSql: string;
begin
  if CDS_3.IsEmpty then
    Exit;

  if not ORDER_SUB.Locate('ssel', True, []) then
  begin
    Application.MessageBox('请选择数据!', '提示', 0);
    Exit;
  end;

  WSql := ' @SubIdS=' + quotedstr(trim(SelCDSKey(ORDER_SUB, ['SubId'])[0]));
  WSql := WSql + ' ,@QS=' + IntToStr(GetDSCS(QS.Text));
  WSql := WSql + ' ,@JS=' + IntToStr(GetDSCS(JS.Text));
  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      IsSql1 := True;
      IsSql2 := True;
      FLMType := 'TradePlanPrint14';
      FFiltration1 := WSql;
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;

end;

procedure TfrmTradePlanColorList.ToolButton6Click(Sender: TObject);
var
  WSql: string;
begin
  if CDS_3.IsEmpty then
    Exit;

  if not ORDER_SUB.Locate('ssel', True, []) then
  begin
    Application.MessageBox('请选择数据!', '提示', 0);
    Exit;
  end;

  WSql := ' @SubIdS=' + quotedstr(trim(SelCDSKey(ORDER_SUB, ['SubId'])[0]));
  WSql := WSql + ' ,@QS=' + IntToStr(GetDSCS(QS.Text));
  WSql := WSql + ' ,@JS=' + IntToStr(GetDSCS(JS.Text));
  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      IsSql1 := True;
      IsSql2 := True;
      FLMType := 'TradePlanPrint15';
      FFiltration1 := WSql;
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;

end;

procedure TfrmTradePlanColorList.PrtDS(DSCS: string);
begin

end;

procedure TfrmTradePlanColorList.cxGridDBColumn4PropertiesEditValueChanged(Sender: TObject);
var
  FColorNo: string;
begin
//  FColorNo := TcxTextEdit(Sender).EditingText;
//  try
//    with ADOQueryCmd do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add(' update trade_Plan_Sub   ');
//      sql.Add(' set  C_ColorNo=' + quotedstr(FColorNo));
//      sql.Add('where SubID =' + quotedstr((Order_Sub.fieldbyname('SubId').AsString)));
//      showmessage(sql.text);
//      ExecSQL;
//    end;
//    ModalResult := 1;
//  except
//    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
//
//  end;

end;

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

procedure TfrmTradePlanColorList.TchkClick(Sender: TObject);
var
  rtvalues: TArray<string>;
begin
  if Order_Main.IsEmpty then
    exit;
  if cxTabControl1.TabIndex <> 1 then
  begin
    Application.MessageBox('只能在待审核状态下进行审核操作!', '提示', 0);
    Exit;
  end;
  rtvalues := SelCDSKey(Order_Main, ['MainId', 'OrderNo']);
  if Application.MessageBox('是否核实好数据？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Trade_Plan_Main SET status=''9'',chker=''' + trim(DName) + ''',Chktime=getdate() ');
      sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where Trade_Plan_Main.MainId=X.RTValue )  ');

      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(rtvalues[1]))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;

  except
    application.MessageBox('计划单审核失败！', '提示信息', 0);
  end;
  TBRafresh.Click;
end;

procedure TfrmTradePlanColorList.TnochkClick(Sender: TObject);
var
  rtvalues: TArray<string>;
begin
  if Order_Main.IsEmpty then
    exit;
  if cxTabControl1.TabIndex <> 2 then
  begin
    Application.MessageBox('只能在已审核状态下进行审核操作!', '提示', 0);
    Exit;
  end;
  rtvalues := SelCDSKey(Order_Main, ['MainId', 'OrderNo']);
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Trade_Plan_Main SET status=''0'',chker=null,Chktime=null ');
      sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where Trade_Plan_Main.MainId=X.RTValue )  ');

      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(rtvalues[1]))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    TBRafresh.Click;
  except
    application.MessageBox('计划单撤销审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmTradePlanColorList.TWCClick(Sender: TObject);
var
  rtvalues: TArray<string>;
begin
  if Order_Main.IsEmpty then
    exit;

  rtvalues := SelCDSKey(Order_Main, ['MainId', 'OrderNo']);

  if canshu1 <> '管理' then
  begin
    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from  Trade_Plan_Main A ');
      sql.Add(' where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where A.MainId=X.RTValue )  ');
      sql.Add(' and Filler<>' + QuotedStr(Trim(Order_Main.fieldbyname('Filler').AsString)));
      open;
    end;
    if not ADOQueryTemp.IsEmpty then
    begin
      Application.MessageBox('不能操作他人的数据!', '提示', 0);
      Exit;
    end;
  end;

  if Application.MessageBox('是否核实好数据？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Trade_Plan_Main SET status=''10'' ');
      sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where Trade_Plan_Main.MainId=X.RTValue )  ');

      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(rtvalues[1]))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;

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

procedure TfrmTradePlanColorList.TNowcClick(Sender: TObject);
var
  rtvalues: TArray<string>;
begin
  if Order_Main.IsEmpty then
    exit;

  rtvalues := SelCDSKey(Order_Main, ['MainId', 'OrderNo']);
  if canshu1 <> '管理' then
  begin
    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from  Trade_Plan_Main A ');
      sql.Add(' where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where A.MainId=X.RTValue )  ');
      sql.Add(' and Filler<>' + QuotedStr(Trim(Order_Main.fieldbyname('Filler').AsString)));
      open;
    end;
    if not ADOQueryTemp.IsEmpty then
    begin
      Application.MessageBox('不能操作他人的数据!', '提示', 0);
      Exit;
    end;
  end;
  if Application.MessageBox('是否核实好数据？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Trade_Plan_Main SET status=''9'' ');
      sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where Trade_Plan_Main.MainId=X.RTValue )  ');

      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(rtvalues[1]))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('计划单撤销完成成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('计划单撤销完成失败！', '提示信息', 0);
  end;
end;

procedure TfrmTradePlanColorList.TBTJClick(Sender: TObject);
var
  rtvalues: TArray<string>;
begin
  if Order_Main.IsEmpty then
    exit;
  if cxTabControl1.TabIndex <> 0 then
  begin
    Application.MessageBox('只能在待提交状态下进行提交操作!', '提示', 0);
    Exit;
  end;

  rtvalues := SelCDSKey(Order_Main, ['MainId', 'OrderNo']);

  if canshu1 <> '管理' then
  begin
    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from  Trade_Plan_Main A ');
      sql.Add(' where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where A.MainId=X.RTValue )  ');
      sql.Add(' and Filler<>' + QuotedStr(Trim(Order_Main.fieldbyname('Filler').AsString)));
      open;
    end;
    if not ADOQueryTemp.IsEmpty then
    begin
      Application.MessageBox('不能操作他人的数据!', '提示', 0);
      Exit;
    end;
  end;

  if Application.MessageBox('确定要提交数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Trade_Plan_Main SET status=''1'' ');
      sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where Trade_Plan_Main.MainId=X.RTValue )  ');

      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(rtvalues[1]))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('计划单提交成功！', '提示信息');

  except
    application.MessageBox('计划单提交失败！', '提示信息', 0);
  end;

  //    SendMessageQR(trim(Order_Main.FieldByName('mainid').AsString), trim(Order_Main.FieldByName('Filler').AsString), trim(Order_Main.FieldByName('orderNO').AsString), Trim(FormatDateTime('yyyy-MM-dd', Order_Main.FieldByName('OrdDate').asdatetime)));
//    with ADOQueryCmd do
//    begin
//      close;
//      sql.Clear;
//      sql.Add('exec P_MSG_in ');
//      sql.Add(' @MSG=' + QuotedStr(trim('计划单号：' + trim(Order_Main.FieldByName('orderNO').AsString) + ' 需要审核')));
//      sql.Add(',@FillUserid=' + QuotedStr(trim(DCode)));
//      sql.Add(',@FillUserName=' + QuotedStr(trim(DName)));
//      sql.Add(',@FillType=' + QuotedStr(trim('需要确认')));
//      sql.Add(',@MegType=' + QuotedStr(trim('计划单提交')));
//      execsql;
//    end;
  TBRafresh.Click;
end;

procedure TfrmTradePlanColorList.TBTJCXClick(Sender: TObject);
var
  rtvalues: TArray<string>;
begin
  if Order_Main.IsEmpty then
    exit;
  if cxTabControl1.TabIndex <> 1 then
  begin
    Application.MessageBox('只能在待审核状态下进行提交撤销操作!', '提示', 0);
    Exit;
  end;

  rtvalues := SelCDSKey(Order_Main, ['MainId', 'OrderNo']);

  if canshu1 <> '管理' then
  begin
    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from  Trade_Plan_Main A ');
      sql.Add(' where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where A.MainId=X.RTValue )  ');
      sql.Add(' and Filler<>' + QuotedStr(Trim(Order_Main.fieldbyname('Filler').AsString)));
      open;
    end;
    if not ADOQueryTemp.IsEmpty then
    begin
      Application.MessageBox('不能操作他人的数据!', '提示', 0);
      Exit;
    end;
  end;

  if Application.MessageBox('确定要撤销提交数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Trade_Plan_Main SET status=''0''  ');
      sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where Trade_Plan_Main.MainId=X.RTValue )  ');
      sql.Add(' and isnull(status,''0'')=''1'' ');

      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(rtvalues[1]))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('计划单提交撤销成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('计划单提交撤销失败！', '提示信息', 0);
  end;
end;

procedure TfrmTradePlanColorList.N1Click(Sender: TObject);
begin
  CopyTVFocusValue(TV1);
end;

procedure TfrmTradePlanColorList.N2Click(Sender: TObject);
begin
  CopyTVFocusValue(TV2);
end;

procedure TfrmTradePlanColorList.N3Click(Sender: TObject);
begin
  SelOKNo(ORDER_SUB, True);
end;

procedure TfrmTradePlanColorList.N4Click(Sender: TObject);
begin
  SelOKNo(ORDER_SUB, False);
end;

procedure TfrmTradePlanColorList.N5Click(Sender: TObject);
begin
  SelOKNo(Order_Main, True);
end;

procedure TfrmTradePlanColorList.N6Click(Sender: TObject);
begin
  SelOKNo(Order_Main, False);
end;

procedure TfrmTradePlanColorList.ComboBox1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmTradePlanColorList.ConNoPropertiesChange(Sender: TObject);
begin
  if Length(Trim(TcxTextEdit(Sender).Text)) < 4 then
    Exit;
  TBFind.Click;
end;

procedure TfrmTradePlanColorList.CustomerNoNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitGrid();
  end;
end;

procedure TfrmTradePlanColorList.ToolButton10Click(Sender: TObject);
begin
  PrtDS('6');
end;

procedure TfrmTradePlanColorList.ToolButton11Click(Sender: TObject);
begin
  try
    frmTradePlanColorLogInput := TfrmTradePlanColorLogInput.Create(Application);
    with frmTradePlanColorLogInput do
    begin
      FmainID := Order_Main.fieldbyname('mainID').AsString;
      FSubId := Order_SUB.fieldbyname('SubId').AsString;
      if ShowModal = 1 then
      begin
        InitTechnical();
      end;
    end;
  finally
    frmTradePlanColorLogInput.Free;
  end;
end;

procedure TfrmTradePlanColorList.ToolButton1Click(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;

  try
    frmTradePlanColorInPut := TfrmTradePlanColorInPut.Create(Application);
    with frmTradePlanColorInPut do
    begin
      FQRS := 1;
      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
    frmTradePlanColorInPut.Free;
  end;
end;

procedure TfrmTradePlanColorList.ToolButton2Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '生产指示单管理');
  WriteCxGrid(trim(self.Caption) + 'Tv2', Tv2, '生产指示单管理');
  WriteCxGrid(trim(self.Caption) + 'Tv3', Tv3, '生产指示单管理');
end;

procedure TfrmTradePlanColorList.ToolButton3Click(Sender: TObject);
begin
  try
    frmSysLogHelp := TfrmSysLogHelp.Create(Application);
    with frmSysLogHelp do
    begin

      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmSysLogHelp.Free;
  end;
end;

end.

