unit U_TradePlanList;

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,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu, Math,
  dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges,
  IdExplicitTLSClientServerBase, U_BaseList, Vcl.Clipbrd, Vcl.DBCtrls, dxSkinWXI,
  dxScrollbarAnnotations, dxSkinBasic, dxSkinBlack, dxSkinBlue, dxSkinBlueprint,
  dxSkinCaramel, dxSkinCoffee, dxSkinDarkroom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinOffice2016Colorful, dxSkinOffice2016Dark,
  dxSkinOffice2019Black, dxSkinOffice2019Colorful, dxSkinOffice2019DarkGray,
  dxSkinOffice2019White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringtime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinTheBezier, dxSkinValentine,
  dxSkinVisualStudio2013Blue, dxSkinVisualStudio2013Dark,
  dxSkinVisualStudio2013Light, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue;

type
  TfrmTradePlanList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxgrdpmn1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNo: TEdit;
    v1OrderNo: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    TBCK: TToolButton;
    Label12: TLabel;
    MPRTKZ: TEdit;
    btnCopy: TToolButton;
    cxSplitter1: TcxSplitter;
    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;
    ComboBox1: TComboBox;
    Label1: TLabel;
    CustName: TEdit;
    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: TEdit;
    CDS_WXTS: TClientDataSet;
    Label4: TLabel;
    BuyConNo: TEdit;
    Tv1Column22: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    ToolButton2: TToolButton;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    GPM_3: TcxGridPopupMenu;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    ToolButton1: TToolButton;
    TV2Column1: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Panel3: TPanel;
    Label23: TLabel;
    Label47: TLabel;
    Label48: TLabel;
    Label36: TLabel;
    Label37: TLabel;
    Label40: TLabel;
    Label41: TLabel;
    MainId: TEdit;
    PZNote: TBtnEditC;
    KJNote: TBtnEditC;
    DBNote: TBtnEditC;
    ggft: TMemo;
    tj: TMemo;
    cm: TMemo;
    jm: TMemo;
    Button1: TButton;
    Button2: TButton;
    ADO_BL: TRichEdit;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    CDS_3: TClientDataSet;
    DataSource3: TDataSource;
    ADOCmd: TADOQuery;
    Tv1Column18: TcxGridDBColumn;
    ADOQuery1: TADOQuery;
    ADOQuery2: TADOQuery;
    Label5: TLabel;
    Tv1Column19: TcxGridDBColumn;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    Label6: TLabel;
    DBFS: TBtnEditC;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    Tv1Column20: TcxGridDBColumn;
    Tv1Column21: TcxGridDBColumn;
    Panel4: TPanel;
    ADO_BG: TRichEdit;
    Label7: TLabel;
    Label8: TLabel;
    BGComposition: TEdit;
    Label9: TLabel;
    BGPrice: TEdit;
    Label10: TLabel;
    SJPrice: TEdit;
    Label11: TLabel;
    BGWidth: TEdit;
    Label13: TLabel;
    BGGramWeight: TEdit;
    Label14: TLabel;
    BGMeter: TEdit;
    Label15: TLabel;
    BGLF: TEdit;
    Label16: TLabel;
    BGGrossWeight: TEdit;
    Label17: TLabel;
    BGNetWeight: TEdit;
    Label18: TLabel;
    consignee: TEdit;
    Button3: TButton;
    Button4: TButton;
    BGMainID: TEdit;
    ToolButton9: TToolButton;
    BGName: TBtnEditC;
    Label19: TLabel;
    Label20: TLabel;
    BGCM: TMemo;
    BGJM: TMemo;
    Label21: TLabel;
    MBuyConNo: TBtnEditC;
    Label22: TLabel;
    BGPiece: TEdit;
    Tv1Column23: TcxGridDBColumn;
    Tv1Column24: TcxGridDBColumn;
    Tv1Column25: TcxGridDBColumn;
    BGNote: TMemo;
    Label24: TLabel;
    Label25: TLabel;
    ConNo: TEdit;
    Label26: TLabel;
    DBPrice: TEdit;
    ToolButton10: TToolButton;
    ToolButton11: TToolButton;
    Label27: TLabel;
    BGPLACE: TBtnEditC;
    Tv1Comptime: TcxGridDBColumn;
    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 ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure PZNoteBtnUpClick(Sender: TObject);
    procedure PZNoteBtnDnClick(Sender: TObject);
    procedure ggftDblClick(Sender: TObject);
    procedure cxGridDBColumn8PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure BGNameBtnUpClick(Sender: TObject);
    procedure DBFSBtnUpClick(Sender: TObject);
    procedure ToolButton11Click(Sender: TObject);
  private
    DQdate: TDateTime;
    FOrderNo, FCustName, FConNo, FBuyConNo, FC_Code, FC_Name, FHXYS, FCPMC, FKZPZ, FOrdQty, FOrdUnit: string;
    FWidth, FGramWeight, FConPrice, FJGSY: string;
    procedure InitGrid();
    procedure InitBl();
    procedure InitBG();
    procedure InitHZ();
    procedure SetBlList();
    procedure SetBGList();
    procedure InitForm();
    function DelData(): Boolean;
    procedure InitGridFH();
    procedure InitSub();
    procedure SetStatus();
    procedure InitImage(fwbid: string);
    procedure SendMessageQR(MDateId, KeyWord1, KeyWord2, KeyWord3: string);
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1, canshu2: string;
    fFlileFlag: string;
    FNOTE: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_TradePlanInPut, U_RTFun, U_ZDYHelp, U_LabelPrint, U_TradeInspSet,
  U_AttachmentUpload, U_ZDYHelpSel, U_TradeInspSet_YJ, U_ZDYHelp_P;

{$R *.dfm}

procedure TfrmTradePlanList.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 TfrmTradePlanList.SetStatus();
begin
  btnCopy.Enabled := false;
  tbadd.Enabled := false;
  TBEdit.Enabled := False;
  TBDel.Enabled := False;
  TBTJ.Enabled := False;
  TBTJCX.Enabled := False;
  tchk.Enabled := false;
  tnochk.Enabled := false;
  TWC.Enabled := False;
  TNowc.Enabled := False;
  TBDel.Enabled := False;
  ToolButton5.Enabled := False;
  ToolButton6.Enabled := False;
  ToolButton7.Enabled := False;
  ToolButton8.Enabled := False;

  if (canshu1 = '录入') or (canshu1 = '管理') then
  begin
    case cxTabControl1.TabIndex of
      0:
        begin
          tbadd.Enabled := True;
          TBEdit.Enabled := True;
          TBDel.Enabled := True;
          TBDel.Enabled := True;
          TBTJ.Enabled := True;
          btnCopy.Enabled := True;
        end;
      1:
        begin
          TBTJCX.Enabled := True;
          btnCopy.Enabled := True;
          TBDel.Enabled := True;
          tchk.Enabled := True;

        end;
      2:
        begin

          btnCopy.Enabled := True;
          ToolButton5.Enabled := True;
          ToolButton7.Enabled := True;
          tnochk.Enabled := True;
        end;
      3:
        begin

          ToolButton8.Enabled := True;
          btnCopy.Enabled := True;
        end;
      4:
        begin
          ToolButton6.Enabled := True;
//          TWC.Enabled := True;
//          TNowc.Enabled := True;
          btnCopy.Enabled := True;
        end;
      5:
        begin
//          TNowc.Enabled := True;
          btnCopy.Enabled := True;
        end;
    end;
  end
  else if canshu1 = '完成' then
  begin
    case cxTabControl1.TabIndex of
      4:
        begin
//          ToolButton6.Enabled := True;
          TWC.Enabled := True;
//          TNowc.Enabled := True;
//          btnCopy.Enabled := True;
        end;
      5:
        begin
          TNowc.Enabled := True;
//          btnCopy.Enabled := True;
        end;
    end;

  end;
end;

procedure TfrmTradePlanList.InitSub();
begin
  ADOQuerySub.Close;
  if Order_Main.IsEmpty then
    exit;
  ADOQuerySub.DisableControls;
  with ADOQuerySub do
  begin
    close;
    sql.Clear;
    sql.Add('select *  ');
    sql.Add(' from Trade_Plan_Sub A');
    sql.Add('where mainid =' + quotedstr((Order_Main.fieldbyname('mainID').AsString)));
    open;
  end;
  SCreateCDS(ADOQuerySub, ORDER_SUB);
  SInitCDSData(ADOQuerySub, ORDER_SUB);

  ADOQuerySub.EnableControls;
  TV2.DataController.Filter.Clear;
end;

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

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

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

procedure TfrmTradePlanList.FormCreate(Sender: TObject);
begin
  inherited;
  cxgrid1.Align := alClient;
  DQdate := SGetServerDate(ADOQueryTemp);

end;

procedure TfrmTradePlanList.TBCloseClick(Sender: TObject);
begin

  Close;

end;

procedure TfrmTradePlanList.SetBLList();
begin
  ADO_BL.Text := '';
  ADO_BL.Text := ADO_BL.Text + '指示单号:' + FOrderNo + #13;
  ADO_BL.Text := ADO_BL.Text + '客户:' + FCustName + #13;
  ADO_BL.Text := ADO_BL.Text + '合同号:' + FConNo + #13;
  ADO_BL.Text := ADO_BL.Text + '客户单号:' + FBuyConNo + #13;
  ADO_BL.Text := ADO_BL.Text + '产品编号:' + FC_Code + #13;
  ADO_BL.Text := ADO_BL.Text + '产品名称:' + FC_Name + #13;
  ADO_BL.Text := ADO_BL.Text + '花型颜色:' + FHXYS + #13;
  ADO_BL.Text := ADO_BL.Text + '成品米长:' + FCPMC + #13;
  ADO_BL.Text := ADO_BL.Text + '克重品质:' + FKZPZ + #13;
  ADO_BL.Text := ADO_BL.Text + '订单总数:' + FOrdQty + #13;
end;

procedure TfrmTradePlanList.SetBGList();
begin
  ADO_BG.Text := '';
  ADO_BG.Text := ADO_BG.Text + '指示单号:' + FOrderNo + #13;
  ADO_BG.Text := ADO_BG.Text + '客户:' + FCustName + #13;
  ADO_BG.Text := ADO_BG.Text + '合同号:' + FConNo + #13;
  ADO_BG.Text := ADO_BG.Text + '客户单号:' + FBuyConNo + #13;
  ADO_BG.Text := ADO_BG.Text + '产品编号:' + FC_Code + #13;
  ADO_BG.Text := ADO_BG.Text + '产品名称:' + FC_Name + #13;
  ADO_BG.Text := ADO_BG.Text + '花型颜色:' + FHXYS + #13;
  ADO_BG.Text := ADO_BG.Text + '成品米长:' + FCPMC + #13;
  ADO_BG.Text := ADO_BG.Text + '克重品质:' + FKZPZ + #13;
  ADO_BG.Text := ADO_BG.Text + '订单总数:' + FOrdQty + #13;
  ADO_BG.Text := ADO_BG.Text + '门幅:' + FWidth + #13;
  ADO_BG.Text := ADO_BG.Text + '克重:' + FGramWeight + #13;
  ADO_BG.Text := ADO_BG.Text + '价格术语:' + FJGSY + #13;
  ADO_BG.Text := ADO_BG.Text + '单价:' + FConPrice + #13;
end;

procedure TfrmTradePlanList.InitHZ();
var
  MMeter, MKG: Double;
begin
  if Order_Main.IsEmpty then
  begin
    Label5.Caption := '总米数:0    总重量：0';
    exit;
  end;

  MMeter := tv1.DataController.Summary.FooterSummaryValues[2];
  MKG := tv1.DataController.Summary.FooterSummaryValues[3];
//  with Order_Main do
//  begin
//    First;
//    while not Eof do
//    begin
//      if FieldByName('OrdUnit').ASString = 'Kg' then
//      begin
//        MKG := MKG + StrToFloat(FieldByName('OrdQty').AsString);
//      end
//      else if FieldByName('OrdUnit').ASString = 'M' then
//      begin
//        MMeter := MMeter + StrToFloat(FieldByName('OrdQty').AsString);
//      end
//      else if FieldByName('OrdUnit').ASString = 'Y' then
//      begin
//        MMeter := MMeter + RoundFloat(StrToFloat(FieldByName('OrdQty').AsString) * 0.9144, 2);
//      end;
//      Next;
//    end;
//  end;

  Label5.Caption := '总米数：' + FloattoStr(MMeter) + '    总重量：' + FloattoStr(MKG)
end;

procedure TfrmTradePlanList.InitBl();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.OrderNo,A.CustName,B.ConNo,B.BuyConNo,B.C_Code,B.C_Name,A.HXYS,A.CPMC,A.KZPZ,SUM(B.OrdQty) OrdQty,B.OrdUnit');
    SQL.Add('from Trade_Plan_Main A left join Trade_plan_Sub B on B.MainId=A.MainId');
    sql.Add('where A.mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
    Sql.Add('GROUP BY A.OrderNo,A.CustName,B.ConNo,B.BuyConNo,B.C_Code,B.C_Name,A.HXYS,A.CPMC,A.KZPZ,B.OrdUnit');

    //    ShowMessage(sql.Text);
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    FOrderNo := Trim(ADOQueryTemp.fieldbyname('OrderNo').AsString);
    FCustName := Trim(ADOQueryTemp.fieldbyname('CustName').AsString);
    FConNo := Trim(ADOQueryTemp.fieldbyname('ConNo').AsString);
    FBuyConNo := Trim(ADOQueryTemp.fieldbyname('BuyConNo').AsString);
    FC_Code := Trim(ADOQueryTemp.fieldbyname('C_Code').AsString);
    FC_Name := Trim(ADOQueryTemp.fieldbyname('C_Name').AsString);
    FHXYS := Trim(ADOQueryTemp.fieldbyname('HXYS').AsString);
    FCPMC := Trim(ADOQueryTemp.fieldbyname('CPMC').AsString);
    FKZPZ := Trim(ADOQueryTemp.fieldbyname('KZPZ').AsString);
    FOrdQty := Trim(ADOQueryTemp.fieldbyname('OrdQty').AsString + ADOQueryTemp.fieldbyname('OrdUnit').AsString);
    SetBlList();
  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select MBuyConNo=(select Top 1 BuyConNo from Trade_Plan_Sub X where X.MainID=A.MainID),A.* from Trade_Plan_Main A');
//    SQL.Add('from Trade_Plan_Main A left join Trade_plan_Sub B on B.MainId=A.MainId');
    sql.Add('where A.mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
//    Sql.Add('GROUP BY A.OrderNo,A.CustName,B.ConNo,B.BuyConNo,B.C_Code,B.C_Name,A.HXYS,A.CPMC,A.KZPZ,B.OrdUnit');
    //    ShowMessage(sql.Text);
    Open;
  end;
  SCSHData(ADOQueryTemp, Panel3, 2);

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select *  from Trade_Plan_Sub where 1=1 ');
    sql.Add(' and MainId=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));

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

procedure TfrmTradePlanList.InitBG();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.OrderNo,A.CustName,B.ConNo,B.BuyConNo,B.C_Code,B.C_Name,A.HXYS,A.CPMC,A.KZPZ,SUM(B.OrdQty) OrdQty,B.OrdUnit');
    SQL.Add(',C_Width=(select Top 1 C_Width from Bs_Contract_Sub X where X.ConMid=B.ConMid)');
    SQL.Add(',C_GramWeight=(select Top 1 C_GramWeight from Bs_Contract_Sub X where X.ConMid=B.ConMid)');
    SQL.Add(',ConPrice=(select Top 1 ConPrice from Bs_Contract_Sub X where X.ConMid=B.ConMid)');
    SQL.Add(',JGSY=(select jiagesy from Bs_Contract_Main X where X.ConMid=B.ConMid)');
    SQL.Add('from Trade_Plan_Main A left join Trade_plan_Sub B on B.MainId=A.MainId');
    sql.Add('where A.mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
    Sql.Add('GROUP BY A.OrderNo,A.CustName,B.ConNo,B.BuyConNo,B.C_Code,B.C_Name,A.HXYS,A.CPMC,A.KZPZ,B.OrdUnit,B.ConMid');

    //    ShowMessage(sql.Text);
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    FOrderNo := Trim(ADOQueryTemp.fieldbyname('OrderNo').AsString);
    FCustName := Trim(ADOQueryTemp.fieldbyname('CustName').AsString);
    FConNo := Trim(ADOQueryTemp.fieldbyname('ConNo').AsString);
    FBuyConNo := Trim(ADOQueryTemp.fieldbyname('BuyConNo').AsString);
    FC_Code := Trim(ADOQueryTemp.fieldbyname('C_Code').AsString);
    FC_Name := Trim(ADOQueryTemp.fieldbyname('C_Name').AsString);
    FHXYS := Trim(ADOQueryTemp.fieldbyname('HXYS').AsString);
    FCPMC := Trim(ADOQueryTemp.fieldbyname('CPMC').AsString);
    FKZPZ := Trim(ADOQueryTemp.fieldbyname('KZPZ').AsString);
    FOrdQty := Trim(ADOQueryTemp.fieldbyname('OrdQty').AsString + ADOQueryTemp.fieldbyname('OrdUnit').AsString);
    FWidth := Trim(ADOQueryTemp.fieldbyname('C_Width').AsString);
    FGramWeight := Trim(ADOQueryTemp.fieldbyname('C_GramWeight').AsString);
    FConPrice := Trim(ADOQueryTemp.fieldbyname('ConPrice').AsString);
    FJGSY := Trim(ADOQueryTemp.fieldbyname('JGSY').AsString);
    SetBGList();
  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from Trade_Plan_Main');
//    SQL.Add('from Trade_Plan_Main A left join Trade_plan_Sub B on B.MainId=A.MainId');
    sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
//    Sql.Add('GROUP BY A.OrderNo,A.CustName,B.ConNo,B.BuyConNo,B.C_Code,B.C_Name,A.HXYS,A.CPMC,A.KZPZ,B.OrdUnit');
    //    ShowMessage(sql.Text);
    Open;
  end;
  SCSHData(ADOQueryTemp, Panel4, 2);
  BGName.text := ADOQueryTemp.FieldByName('BGName').AsString;
  BGCM.text := ADOQueryTemp.FieldByName('CM').AsString;
  BGJM.text := ADOQueryTemp.FieldByName('JM').AsString;
  BGPrice.text := ADOQueryTemp.FieldByName('BGPrice').AsString;
  BGPiece.text := ADOQueryTemp.FieldByName('BGPiece').AsString;
end;

procedure TfrmTradePlanList.InitGrid();
var
  STRwhere, Pwhere: string;
begin
  STRwhere := '';
  Pwhere := SGetFilters(Panel1, 1, 2);

  begin
    if trim(Pwhere) <> '' then
      STRwhere := STRwhere + ' and ' + trim(Pwhere);
  end;

  case cxTabControl1.TabIndex of
    0:
      begin
        STRwhere := STRwhere + ' and  isnull(A.status,''0'')=''0'' ';
      end;
    1:
      begin
        STRwhere := STRwhere + ' and  isnull(A.status,''0'')=''1'' ';
      end;
    2:
      begin
        STRwhere := STRwhere + ' and  isnull(A.status,''0'')=''9'' ';
      end;
    3:
      begin
        STRwhere := STRwhere + ' and  isnull(A.status,''0'')=''7'' ';
      end;
    4:
      begin
        STRwhere := STRwhere + ' and  isnull(A.status,''0'')=''11'' ';
      end;
    5:
      begin
        STRwhere := STRwhere + ' and  isnull(A.status,''0'')=''10'' ';
      end;
    6:
      begin
        STRwhere := STRwhere + ' and  isnull(A.status,''0'')=''12'' ';
      end;
  end;

  try
    ADOQueryMain.DisableControls;

    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.add(' select  A.* from V_Trade_Plan_Single_2 A ');

      if (ComboBox1.Text = '制单日期') and (CheckBox1.Checked = True) then
      begin
        sql.add(' where A.OrdDate>=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime))));
        sql.add(' and  A.OrdDate<' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1))));
      end;
      if (ComboBox1.Text = '制单日期') and (CheckBox1.Checked = False) then
      begin
        sql.add(' where A.OrdDate>=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime - 39999))));
        sql.add(' and  A.OrdDate<' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1))));
      end;

      if (ComboBox1.Text = '交货日期') and (CheckBox1.Checked = True) then
      begin
        sql.add(' where A.DlyDate>=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime))));
        sql.add(' and  A.DlyDate<' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1))));
      end;
      if (ComboBox1.Text = '交货日期') and (CheckBox1.Checked = False) then
      begin
        sql.add(' where A.DlyDate>=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime - 39999))));
        sql.add(' and  A.DlyDate<' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1))));
      end;

      if canshu1 = '录入' then
      begin
        if canshu2 = '理单' then
        begin
          sql.add(' and  A.Tallyer= ' + QuotedStr(Trim(DName)));
        end
        else
        begin
          sql.add(' and  (A.Filler= ' + QuotedStr(Trim(DName)) + ' or A.KHSaleser =' + quotedstr(trim(DName)) + ')');
        end;
      end;
      sql.add('     ' + STRwhere);

      sql.add(' order by  filltime desc');
//      ShowMessage(SQL.Text);
      Open;
    end;

    SCreateCDS(ADOQueryMain, Order_Main);
    SInitCDSData(ADOQueryMain, Order_Main);
    InitHZ();
  finally
    ADOQueryMain.EnableControls;
    TV1.DataController.Filter.Clear;

  end;
end;

procedure TfrmTradePlanList.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 TfrmTradePlanList.InitGridFH();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('exec Order_QryList :MainId,:WSql');
      Parameters.ParamByName('WSql').Value := ' and FillTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''' + ' and FillTime<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1)) + '''';
      Open;
    end;
    SCreateCDS(ADOQueryMain, Order_Main);
    SInitCDSData(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmTradePlanList.InitForm();
begin
  ReadCxGrid('指示单列表FF1', Tv1, '生产指示单管理');
  ReadCxGrid('指示单列表FFTv21', Tv2, '生产指示单管理');
  BegDate.DateTime := SGetServerDate(ADOQueryTemp) - 180;
  EndDate.DateTime := SGetServerDate(ADOQueryTemp);

  SetStatus();
  InitGrid();
end;

procedure TfrmTradePlanList.TBEditClick(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;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select Count(B.BCIOID) S from Trade_Plan_Main A');
    SQL.Add('left join Bs_Cloth_IO B on A.MainID=B.FromMainId ');
    sql.Add('where A.mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
    Sql.Add('and B.STKName=''贸易待检布'' and B.IOFlag=''入库''');
    //    ShowMessage(sql.Text);
    Open;
  end;
  if ADOQueryTemp.fieldbyname('S').Value > 0 then
  begin
    Application.MessageBox('已做过待检布入库的计划单，不能修改!', '提示', 0);
    Exit;
  end;

  try
    frmTradePlanInPut := TfrmTradePlanInPut.Create(Application);
    with frmTradePlanInPut do
    begin

      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      FOrderNo := Trim(Self.Order_Main.fieldbyname('OrderNo').AsString);
      canshu1 := self.canshu1;
      canshu2 := self.canshu2;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmTradePlanInPut.Free;
  end;

end;

procedure TfrmTradePlanList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;

  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  if DName <> 'ADMIN' then
  begin
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.fieldbyname('Ssel').AsString = 'True' then
        begin
          if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
          begin
            Application.MessageBox('不能操作他人的数据!', '提示', 0);
            Exit;
          end;
        end;
        Next;
      end;
    end;

  end;

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

  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.fieldbyname('Ssel').AsString = 'True' then
      begin
        with ADOQuery1 do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Trade_Cloth_Inspect where isnull(mainID,'''')<>'''' and mainID=''' + Trim(Order_Main.fieldbyname('mainID').AsString) + '''');
          Open;
        end;
        with ADOQuery2 do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Pur_ClothPlan_Main where Status<>''-1'' and isnull(MainId,'''')<>'''' and mainID=''' + Trim(Order_Main.fieldbyname('mainID').AsString) + '''');
          Open;
        end;
        if ADOQuery1.IsEmpty = False then
        begin
          Application.MessageBox('已检验不能删除数据!', '提示', 0);
          Exit;
        end;
        if ADOQuery2.IsEmpty = False then
        begin
          Application.MessageBox('已生成采购、染色、加工计划单不能删除!', '提示', 0);
          Exit;
        end;
      end;
      Next;
    end;
  end;

//  with ADOQueryTemp do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('select * from Pur_ClothPlan_Main where Status<>''-1'' and isnull(MainId,'''')<>'''' and mainID=''' + Trim(Order_Main.fieldbyname('mainID').AsString) + '''');
//    Open;
//  end;
//  if ADOQueryTemp.IsEmpty = False then
//  begin
//    Application.MessageBox('已生成采购、染色、加工计划单不能删除!', '提示', 0);
//    Exit;
//  end;

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

procedure TfrmTradePlanList.DBFSBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp_P := TfrmZDYHelp_P.Create(Application);
    with frmZDYHelp_P do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        DBPrice.Text := Trim(ClientDataSet1.fieldbyname('ZdyFlt1').AsString);
      end;
    end;
  finally
    frmZDYHelp_P.Free;
  end;
end;

function TfrmTradePlanList.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Trade_Plan_Main where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
      sql.Add('delete Trade_Plan_Sub where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
      sql.Add('delete Trade_Plan_Fty where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');

      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('指示单删除')));
      sql.Add(',' + quotedstr(trim('指示单号：' + trim(Order_Main.FieldByName('OrderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      ExecSQL;
    end;

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

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

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

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

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

procedure TfrmTradePlanList.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 TfrmTradePlanList.PZNoteBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';

end;

procedure TfrmTradePlanList.PZNoteBtnUpClick(Sender: TObject);
var
  fsj, ZYSC: string;
  FWZ: Integer;
begin
  fsj := Trim(Tedit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        while frmZDYHelpSel.ClientDataSet1.Locate('SSel', true, []) do
        begin

          if trim(ZYSC) = '' then
            ZYSC := trim(ClientDataSet1.fieldbyname('ZDYName').AsString)
          else
            ZYSC := ZYSC + ',' + trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

          TEdit(Sender).Text := ZYSC;
          frmZDYHelpSel.ClientDataSet1.delete;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

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

end;

procedure TfrmTradePlanList.ggftDblClick(Sender: TObject);
var
  fsj, ZYSC: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        while frmZDYHelpSel.ClientDataSet1.Locate('SSel', true, []) do
        begin

          if trim(ZYSC) = '' then
            ZYSC := trim(ClientDataSet1.fieldbyname('ZDYName').AsString)
          else
            ZYSC := ZYSC + sLineBreak + trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

          TEdit(Sender).Text := ZYSC;
          frmZDYHelpSel.ClientDataSet1.delete;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

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

procedure TfrmTradePlanList.Button1Click(Sender: TObject);
begin

  if MainId.Text <> '' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate Trade_Plan_Main ');
      sql.Add(' Set PZNote=''' + Trim(PZNote.Text) + '''');
      sql.Add(',KJNote=''' + Trim(KJNote.Text) + '''');
      sql.Add(',DBNote=''' + Trim(DBNote.Text) + '''');
      sql.Add(',DBFS=''' + Trim(DBFS.Text) + '''');
      sql.Add(',DBPrice=''' + Trim(DBPrice.Text) + '''');
      SQL.Add(',ggft=''' + Trim(ggft.Text) + '''');
      sql.Add(',tj=''' + Trim(tj.Text) + '''');
      sql.Add(',cm=''' + Trim(cm.Text) + '''');
      sql.Add(',jm=''' + Trim(jm.Text) + '''');
      SQL.Add(',Editer=''' + Trim(DName) + '''');
      SQL.Add(',EditId=''' + Trim(DCode) + '''');
//      SQL.Add(',Edittime=''' + Trim(DCode) + '''');
      SQL.Add(' where MainId=''' + Trim(MainId.Text) + '''');
      sql.Add('UPdate Trade_Plan_sub ');
      sql.Add(' Set BuyConNo=''' + Trim(MBuyConNO.Text) + '''');
      SQL.Add(' where MainId=''' + Trim(MainId.Text) + '''');
//      ShowMessage(SQL.text);
      execsql;
    end;

  end;

  Application.MessageBox('保存成功!', '提示', 0);
  Button2.Click;
end;

procedure TfrmTradePlanList.Button2Click(Sender: TObject);
begin
  FOrderNo := '';
  FCustName := '';
  FConNo := '';
  FBuyConNo := '';
  FC_Code := '';
  FC_Name := '';
  FHXYS := '';
  FCPMC := '';
  FKZPZ := '';
  FOrdQty := '';
  PZNote.text := '';
  KJNote.text := '';
  DBNote.text := '';
  DBFS.text := '';
  DBPrice.text := '';
  ggft.text := '';
  tj.text := '';
  cm.text := '';
  jm.text := '';
  WriteCxGrid('指示单列表FF1', Tv3, '生产指示单管理');
  Panel3.Visible := False;
end;

procedure TfrmTradePlanList.Button3Click(Sender: TObject);
begin
  if BGMainId.Text <> '' then
  begin
    if Trim(BGPLACE.Text) = '' then
    begin
      Application.MessageBox('货源地不能为空，请输入！', '提示', MB_OK);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate Trade_Plan_Main ');
      sql.Add(' Set BGName=''' + Trim(BGName.Text) + '''');
      sql.Add(',BGComposition=''' + Trim(BGComposition.Text) + '''');
      sql.Add(',BGPrice=''' + Trim(BGPrice.Text) + '''');
      sql.Add(',SJPrice=''' + Trim(SJPrice.Text) + '''');
      SQL.Add(',BGWidth=''' + Trim(BGWidth.Text) + '''');
      sql.Add(',BGGramWeight=''' + Trim(BGGramWeight.Text) + '''');
      sql.Add(',BGPiece=''' + Trim(BGPiece.Text) + '''');
      sql.Add(',BGGrossWeight=''' + FloatToStr(strtofloatdef(Trim(BGGrossWeight.Text), 0)) + '''');
      sql.Add(',BGNetWeight=''' + FloatToStr(strtofloatdef(Trim(BGNetWeight.Text), 0)) + '''');
      sql.Add(',BGLF=''' + Trim(BGLF.Text) + '''');
      sql.Add(',BGMeter=''' + FloatToStr(strtofloatdef(Trim(BGMeter.Text), 0)) + '''');
      sql.Add(',consignee=''' + Trim(consignee.Text) + '''');
      sql.Add(',BGNote=''' + Trim(BGNote.Text) + '''');
      sql.Add(',cm=''' + Trim(BGcm.Text) + '''');
      sql.Add(',bgplace=''' + Trim(bgplace.Text) + '''');
      sql.Add(',jm=''' + Trim(BGjm.Text) + '''');
      SQL.Add(',Editer=''' + Trim(DName) + '''');
      SQL.Add(',EditId=''' + Trim(DCode) + '''');
//      SQL.Add(',Edittime=''' + Trim(DCode) + '''');
      SQL.Add(' where MainId=''' + Trim(BGMainId.Text) + '''');
//      ShowMessage(SQL.text);
      execsql;
    end;

  end;

  Application.MessageBox('保存成功!', '提示', 0);
  Button4.Click;

end;

procedure TfrmTradePlanList.Button4Click(Sender: TObject);
begin
  FOrderNo := '';
  FCustName := '';
  FConNo := '';
  FBuyConNo := '';
  FC_Code := '';
  FC_Name := '';
  FHXYS := '';
  FCPMC := '';
  FKZPZ := '';
  FOrdQty := '';
  FWidth := '';
  FGramWeight := '';
  FConPrice := '';
  FJGSY := '';
  BGName.text := '';
  BGWidth.text := '';
  BGMeter.text := '';
  BGGrossWeight.text := '';
  BGNetWeight.text := '';
  BGComposition.text := '';
  SJPrice.text := '';
  BGGramWeight.text := '';
  consignee.text := '';
  BGLF.text := '';
  BGNote.text := '';

//  WriteCxGrid('指示单列表FF1', Tv3, '生产指示单管理');
  Panel4.Visible := False;

end;

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

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

procedure TfrmTradePlanList.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 TfrmTradePlanList.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 TfrmTradePlanList.TBCKClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;

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

      end;
    end;
  finally
    frmTradePlanInPut.Free;
  end;

end;

procedure TfrmTradePlanList.BGNameBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

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

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

      end;
    end;
  finally
    frmTradePlanInPut.Free;
  end;

end;

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

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

procedure TfrmTradePlanList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  Clipboard.SetTextBuf(PChar(Trim(Order_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

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

end;

procedure TfrmTradePlanList.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 TfrmTradePlanList.ToolButton5Click(Sender: TObject);
var
  HasRed: Integer;
begin
  if Order_Main.IsEmpty then
    exit;
  if Application.MessageBox('是否核实好数据？', '提示', 32 + 4) <> IDYES then
    Exit;

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('EXEC P_Check_PlanLossRed :MainID');
    Parameters.ParamByName('MainID').Value := Order_Main.FieldByName('mainID').AsString;
    Open;
    HasRed := FieldByName('HasRed').AsInteger;
    Close;
  end;

  if HasRed = 1 then
  begin
    Application.MessageBox('该计划单的损耗数据存在异常（超过5%且备注为空），不允许生产完成！' + #13#10 +
      '请完善订单总结备注或核实数据。', '提示', MB_ICONERROR);
    Exit;
  end;

  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Trade_Plan_Main SET status=''11'' ');
      sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));

//      sql.Add('exec P_Ord_Up_SuoLv @DataID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));

      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('计划单生产完成')));
      sql.Add(',' + quotedstr(trim('计划单号：' + trim(Order_Main.FieldByName('orderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;

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

end;

procedure TfrmTradePlanList.ToolButton6Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
//  if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
//  begin
//    Application.MessageBox('不能操作他人的数据!', '提示', 0);
//    Exit;
//  end;
  if cxTabControl1.TabIndex <> 4 then
  begin
    Application.MessageBox('只能在生产完成状态下进行生产完成撤销操作!', '提示', 0);
    Exit;
  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 mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('计划单撤销生产完成')));
      sql.Add(',' + quotedstr(trim('计划单号：' + trim(Order_Main.FieldByName('orderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('计划单撤销生产完成成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('计划单撤销生产完成失败！', '提示信息', 0);
  end;

end;

procedure TfrmTradePlanList.ToolButton7Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if cxTabControl1.TabIndex <> 2 then
  begin
    Application.MessageBox('只能在已审核状态下进行暂存操作!', '提示', 0);
    Exit;
  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=''7'' ');
      sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('计划单暂存')));
      sql.Add(',' + quotedstr(trim('计划单号：' + trim(Order_Main.FieldByName('orderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;

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

end;

procedure TfrmTradePlanList.ToolButton8Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;

  if cxTabControl1.TabIndex <> 3 then
  begin
    Application.MessageBox('只能在暂存状态下进行取消暂存操作!', '提示', 0);
    Exit;
  end;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Trade_Plan_Main SET status=''9'' ');
      sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('计划单取消暂存')));
      sql.Add(',' + quotedstr(trim('计划单号：' + trim(Order_Main.FieldByName('orderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    TBRafresh.Click;
  except
    application.MessageBox('计划单取消暂存失败！', '提示信息', 0);
  end;

end;

procedure TfrmTradePlanList.ToolButton9Click(Sender: TObject);
begin

  BGMainId.text := trim(Order_Main.fieldbyname('mainID').AsString);
  Panel4.Left := (self.width - Panel4.width) div 2;
  Panel4.Top := 37;
  Panel4.Visible := True;
  InitBG();

end;

procedure TfrmTradePlanList.cxGridDBColumn8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;

  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  FFieldName := Trim(Tv3.Controller.FocusedColumn.DataBinding.FilterFieldName);

  with CDS_3 do
  begin
    Edit;

    FieldByName('OrdPiece').Value := StrToFloat(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate Trade_Plan_Sub ');
    sql.Add(' Set ' + FFieldName + '=' + Trim(mvalue));
    SQL.Add(' where SubId=''' + Trim(CDS_3.fieldbyname('SubId').AsString) + '''');

//    SQL.Add('Update YF_Money_KC Set KCBBMoney=KCBBMoney-' + FKCBB + '+' + FBBMoney);
//    sql.Add(' where CRID=''' + Trim(CDS_1.fieldbyname('CRID').AsString) + '''');
    ExecSQL;
  end;
  tv3.Controller.EditingController.ShowEdit();
end;

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

procedure TfrmTradePlanList.TchkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if cxTabControl1.TabIndex <> 1 then
  begin
    Application.MessageBox('只能在待审核状态下进行审核操作!', '提示', 0);
    Exit;
  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'',chker=''' + trim(DName) + ''',Chktime=getdate() ');
      sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('计划单审核')));
      sql.Add(',' + quotedstr(trim('计划单号：' + trim(Order_Main.FieldByName('orderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;

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

procedure TfrmTradePlanList.TnochkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;

  if cxTabControl1.TabIndex <> 2 then
  begin
    Application.MessageBox('只能在已审核状态下进行审核操作!', '提示', 0);
    Exit;
  end;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Trade_Plan_Main SET status=''1'',chker=null,Chktime=null ');
      sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('计划单撤销审核')));
      sql.Add(',' + quotedstr(trim('计划单号：' + trim(Order_Main.FieldByName('orderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    TBRafresh.Click;
  except
    application.MessageBox('计划单撤销审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmTradePlanList.TWCClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
//  if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
//  begin
//    Application.MessageBox('不能操作他人的数据!', '提示', 0);
//    Exit;
//  end;
  if Application.MessageBox('是否核实好数据？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Trade_Plan_Main SET status=''10'' ');
      sql.Add(',CompId=' + QuotedStr(Trim(DCode)));
      sql.Add(',Comper=' + QuotedStr(Trim(DName)));
      sql.Add(',Comptime=getdate() ');
      sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));

//      sql.Add('exec P_Ord_Up_SuoLv @DataID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));

      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('计划单完成')));
      sql.Add(',' + quotedstr(trim('计划单号：' + trim(Order_Main.FieldByName('orderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;

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

procedure TfrmTradePlanList.TNowcClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
//  if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
//  begin
//    Application.MessageBox('不能操作他人的数据!', '提示', 0);
//    Exit;
//  end;
  if cxTabControl1.TabIndex <> 5 then
  begin
    Application.MessageBox('只能在已完成状态下进行撤销完成操作!', '提示', 0);
    Exit;
  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=''11'' ');
      sql.Add(',CompId=null, Comper=null, Comptime=null ');
      sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('计划单撤销完成')));
      sql.Add(',' + quotedstr(trim('计划单号：' + trim(Order_Main.FieldByName('orderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('计划单撤销完成成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('计划单撤销完成失败！', '提示信息', 0);
  end;
end;

procedure TfrmTradePlanList.TBTJClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;

//  if canshu1 <> '高权限' then
//  begin
//    if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
//    begin
//      Application.MessageBox('不能操作他人的数据!', '提示', 0);
//      Exit;
//    end;
//  end;

  if cxTabControl1.TabIndex <> 0 then
  begin
    Application.MessageBox('只能在待提交状态下进行提交操作!', '提示', 0);
    Exit;
  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 mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('计划单提交')));
      sql.Add(',' + quotedstr(trim('计划单号：' + trim(Order_Main.FieldByName('orderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
//    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;
    application.MessageBox('计划单提交成功！', '提示信息');

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

procedure TfrmTradePlanList.TBTJCXClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
//  if canshu1 <> '高权限' then
//  begin
//    if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
//    begin
//      Application.MessageBox('不能操作他人的数据!', '提示', 0);
//      Exit;
//    end;
//  end;

  if cxTabControl1.TabIndex <> 1 then
  begin
    Application.MessageBox('只能在待审核状态下进行提交撤销操作!', '提示', 0);
    Exit;
  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 mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      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(Order_Main.FieldByName('orderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('计划单提交撤销成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('计划单提交撤销失败！', '提示信息', 0);
  end;
end;

procedure TfrmTradePlanList.N1Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(Order_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmTradePlanList.N2Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(ORDER_SUB.fieldbyname(TV2.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmTradePlanList.N3Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, True); //全选
end;

procedure TfrmTradePlanList.N4Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, False); //全弃
end;

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

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

procedure TfrmTradePlanList.ToolButton10Click(Sender: TObject);
begin
  WriteCxGrid('指示单列表FF1', Tv1, '生产指示单管理');
  WriteCxGrid('指示单列表FFTv21', Tv2, '生产指示单管理6');
  application.MessageBox('保存成功！', '提示信息', 0);
end;

procedure TfrmTradePlanList.ToolButton11Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  TcxGridToExcel(Self.Caption, cxGrid1);
end;

procedure TfrmTradePlanList.ToolButton1Click(Sender: TObject);
begin
  try
    frmTradeInspSet_YJ := TfrmTradeInspSet_YJ.Create(Application);
    with frmTradeInspSet_YJ do
    begin

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

      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmTradeInspSet_YJ.Free;
  end;
end;

procedure TfrmTradePlanList.ToolButton2Click(Sender: TObject);
begin
  WriteCxGrid('指示单列表FF1', Tv1, '生产指示单管理');
  WriteCxGrid('指示单列表FFTv21', Tv2, '生产指示单管理');
end;

procedure TfrmTradePlanList.ToolButton3Click(Sender: TObject);
begin
  ReadCxGrid('指示单列表FF1', Tv3, '生产指示单管理');
  MainId.text := trim(Order_Main.fieldbyname('mainID').AsString);
  Panel3.Left := (self.width - Panel3.width) div 2;
  Panel3.Top := 37;
  Panel3.Visible := True;
  InitBl();

end;

end.

