unit U_TradeMarketList_XYTH;

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,
  dxDateRanges, IdExplicitTLSClientServerBase, U_BaseList, Vcl.Clipbrd,
  dxScrollbarAnnotations, cxProgressBar, FrameDateSel, dxSkinsCore,
  dxSkinsDefaultPainters, dxSkinWXI;

type
  TfrmTradeMarketList_XYth = 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;
    TMMNo: TcxTextEdit;
    v1OrdDate: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    TBCK: TToolButton;
    btnCopy: TToolButton;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    ToolButton4: TToolButton;
    cxTabControl1: TcxTabControl;
    TWC: TToolButton;
    TNowc: TToolButton;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    IdFTP1: TIdFTP;
    v1Column16: TcxGridDBColumn;
    ORDER_SUB: TClientDataSet;
    PopupMenu2: TPopupMenu;
    N3: TMenuItem;
    N4: TMenuItem;
    Label1: TLabel;
    CustName: TcxTextEdit;
    Tv1Column6: TcxGridDBColumn;
    cxgrdpmn2: TcxGridPopupMenu;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Label2: TLabel;
    C_Name: TcxTextEdit;
    CDS_WXTS: TClientDataSet;
    Label4: TLabel;
    ConNo: TcxTextEdit;
    ToolButton2: TToolButton;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    GPM_3: TcxGridPopupMenu;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    frmFrameDateSel1: TfrmFrameDateSel;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    N5: TMenuItem;
    N6: TMenuItem;
    Tv1Column23: TcxGridDBColumn;
    Tv1Column24: TcxGridDBColumn;
    Tv1Column25: 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 TMMNoKeyPress(Sender: TObject; var Key: Char);
    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 ToolButton1Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure TBZFClick(Sender: TObject);
    procedure TBNOZFClick(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
  private
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    procedure SetStatus();
    procedure InitImage(fwbid: string);
    procedure SendMessageQR(MDateId, KeyWord1, KeyWord2, KeyWord3: string);
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1, canshu2, canshu4: string;
    fFlileFlag: string;
    FNOTE: string;
    FOrdType: string;
    { Public declarations }
  end;

var
  frmTradeMarketList_XYth: TfrmTradeMarketList_XYth;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_LabelPrint, U_AttachmentUpload,
  U_TradeMarketInPut_XYth;

{$R *.dfm}

procedure TfrmTradeMarketList_XYth.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 TfrmTradeMarketList_XYth.SetStatus();
begin
  btnCopy.Enabled := false;
  tbadd.Enabled := false;
  TBEdit.Enabled := False;
  TBDel.Enabled := False;

//  tchk.Enabled := false;
//  tnochk.Enabled := false;
  TWC.Enabled := False;
  TNowc.Enabled := False;
  TBDel.Enabled := False;

  case cxTabControl1.TabIndex of
    0:
      begin
        tbadd.Enabled := True;
        TBEdit.Enabled := True;
        TBDel.Enabled := True;
//        TBTJ.Enabled := True;
        btnCopy.Enabled := True;
        TWC.Enabled := True;
      end;
    1:
      begin
        TNowc.Enabled := True;
        tbadd.Enabled := True;
        btnCopy.Enabled := True;
      end;

    2:
      begin
//        TNowc.Enabled := True;
        tbadd.Enabled := True;
        btnCopy.Enabled := True;
      end;
  end;

end;

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

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

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

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

end;

procedure TfrmTradeMarketList_XYth.TBCloseClick(Sender: TObject);
begin

  Close;

end;

procedure TfrmTradeMarketList_XYth.InitGrid();
var
  SqlStr, FilterStr: string;
begin
  FilterStr := SGetFilters(Panel1, 1, 2);

  SqlStr := ' exec  P_Trade_Market_th_View  ';
  SqlStr := SqlStr + '  @BegDate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.BegDate.Date)));
  SqlStr := SqlStr + ' ,@EndDate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.enddate.Date + 1)));
  SqlStr := SqlStr + ' ,@StkPosition= ' + QuotedStr(Trim(canshu2));
  if canshu1 = '录入' then
    SqlStr := SqlStr + ' ,@Filler= ' + QuotedStr(Trim(DName));

  SqlStr := SqlStr + ' ,@Stores= ' + QuotedStr(Trim(canshu4));
  case cxTabControl1.TabIndex of    // 1 未完成 10已完成
    0:
      begin
        SqlStr := SqlStr + ' , @Status=''1'' '; //未完成
      end;
    1:
      begin
        SqlStr := SqlStr + ' , @Status=''10'' ';   //已完成
      end;

  end;
//  showmessage(SqlStr);
  InitCDSData(ADOQueryMain, Order_Main, Tv1, SqlStr, FilterStr, 'TMMId');
end;

procedure TfrmTradeMarketList_XYth.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 TfrmTradeMarketList_XYth.InitForm();
begin
  ReadCxGrid('指示单列表FF1', Tv1, '生产指示单管理');
  frmFrameDateSel1.BegDate.Date := SGetServerDate(ADOQueryTemp) - 180;
  frmFrameDateSel1.EndDate.Date := SGetServerDate(ADOQueryTemp);

  SetStatus();
  InitGrid();
end;

procedure TfrmTradeMarketList_XYth.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;

  try
    frmTradeMarketInPut_XYTH := TfrmTradeMarketInPut_XYTH.Create(Application);
    with frmTradeMarketInPut_XYTH do
    begin

      PState := 1;
      FTMMId := Trim(Self.Order_Main.fieldbyname('TMMId').AsString);
      FTMMNo := Trim(Self.Order_Main.fieldbyname('TMMNo').AsString);
      canshu1 := self.canshu1;
      canshu4 := self.canshu4;
      frmTradeMarketInPut_XYTH.fordtype := self.fordtype;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmTradeMarketInPut_XYTH.Free;
  end;

end;

procedure TfrmTradeMarketList_XYth.TBNOZFClick(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_Market_Main SET status=''1'' ');
      sql.Add('where TMMId=' + quotedstr(trim(Order_Main.fieldbyname('TMMId').AsString)));

      sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails) ');
      sql.Add('values(''贸易布坯出库''    ');
      sql.Add(',' + quotedstr(trim(Order_Main.fieldbyname('TMMId').AsString)));
      sql.Add(',''作废''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim(Order_Main.fieldbyname('TMMId').AsString)));
      sql.Add(')  ');

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

procedure TfrmTradeMarketList_XYth.TBDelClick(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;

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

function TfrmTradeMarketList_XYth.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Fin_Flow_Judge ');
      Sql.Add(' @FFIDS=' + quotedstr(Order_Main.fieldbyname('TMMId').AsString));
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox(PChar(ADOQueryTemp.fieldbyname('ShowMsg').AsString), '提示', 0);
      exit;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Trade_Market_TH_Main where TMMId=''' + Trim(Order_Main.fieldbyname('TMMId').AsString) + '''');
      sql.Add('delete Trade_Market_TH_Sub where TMMId=''' + Trim(Order_Main.fieldbyname('TMMId').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('TMMNo').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 TfrmTradeMarketList_XYth.TBPrintClick(Sender: TObject);
var
  WSql: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;
  WSql := '';
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.fieldbyname('SSel').AsBoolean then
      begin
        if WSql <> '' then
        begin
          WSql := WSql + ',' + Trim(Order_Main.fieldbyname('TMSId').AsString);
        end
        else
        begin
          WSql := Trim(Order_Main.fieldbyname('TMSId').AsString);
        end;
      end;
      Next;
    end;
  end;
  Order_Main.Locate('SSel', True, []);
  Order_Main.EnableControls;
  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin

      FLMType := 'TradePlanPrintxyth';

      FFiltration1 := Trim(WSql);
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

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

procedure TfrmTradeMarketList_XYth.TBAddClick(Sender: TObject);
begin
  try
    frmTradeMarketInPut_XYTh := TfrmTradeMarketInPut_XYTh.Create(Application);
    with frmTradeMarketInPut_XYTh do
    begin
      PState := 0;
      FTMMId := '';
      canshu1 := self.canshu1;
      canshu4 := self.canshu4;
      frmTradeMarketInPut_XYTh.fordtype := self.fordtype;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmTradeMarketInPut_XYTh.Free;
  end;
end;

procedure TfrmTradeMarketList_XYth.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 TfrmTradeMarketList_XYth.FormShow(Sender: TObject);
begin
  inherited;
  canshu1 := trim(self.fParameters1);
  canshu2 := trim(self.fParameters2);
  canshu4 := trim(self.fParameters4);
  fFlileFlag := UserDataFlag + 'HX';
  cxTabControl1.TabIndex := 0;
  InitForm();
end;

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

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

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

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

  try
    frmTradeMarketInPut_XYTh := TfrmTradeMarketInPut_XYTh.Create(Application);
    with frmTradeMarketInPut_XYTh do
    begin
      PState := 1;
      FTMMId := Trim(Self.Order_Main.fieldbyname('TMMId').AsString);
      ToolBar2.Visible := False;
      TBSave.Visible := False;
      ScrollBox1.Enabled := True;
      N2.Visible := False;
      frmTradeMarketInPut_XYTh.fordtype := self.fordtype;
      TvMX.OnCellDblClick := nil;
      Panel5.Visible := False;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmTradeMarketInPut_XYTh.Free;
  end;

end;

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

  try
    frmTradeMarketInPut_XYTh := TfrmTradeMarketInPut_XYTh.Create(Application);
    with frmTradeMarketInPut_XYTh do
    begin
      PState := 1;
      CopyInt := 99;
      FTMMId := Trim(Self.Order_Main.fieldbyname('TMMId').AsString);
      canshu1 := self.canshu1;
      canshu4 := self.canshu4;
      frmTradeMarketInPut_XYTh.fordtype := self.fordtype;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmTradeMarketInPut_XYTh.Free;
  end;

end;

procedure TfrmTradeMarketList_XYth.TMMNoKeyPress(Sender: TObject; var Key: Char);
begin

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

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

  try
    frmTradeMarketInPut_XYTh := TfrmTradeMarketInPut_XYTh.Create(Application);
    with frmTradeMarketInPut_XYTh do
    begin
      PState := 1;
      FTMMId := Trim(Self.Order_Main.fieldbyname('TMMId').AsString);
      ToolBar2.Enabled := False;
      TBSave.Enabled := False;
      ScrollBox1.Enabled := False;
      N2.Visible := False;
      frmTradeMarketInPut_XYTh.fordtype := self.fordtype;
      TvMX.OnCellDblClick := nil;
      Panel5.Enabled := False;
      canshu4 := self.canshu4;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmTradeMarketInPut_XYTh.Free;
  end;

end;

procedure TfrmTradeMarketList_XYth.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('TMMId').AsString);
      fType := '退货指示单';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmAttachmentUpload.Free;
  end;
end;

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

//procedure TfrmTradeMarketList.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_Market_Main SET status=''9'',chker=''' + trim(DName) + ''',Chktime=getdate() ');
//      sql.Add('where TMMId=' + quotedstr(trim(Order_Main.fieldbyname('TMMId').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('TMMNO').AsString))));
//      sql.Add(',' + quotedstr(trim('成功')));
//      sql.Add(')');
//      execsql;
//    end;
//
//  except
//    application.MessageBox('计划单审核失败！', '提示信息', 0);
//  end;
//  TBRafresh.Click;
//end;

//procedure TfrmTradeMarketList.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_Market_Main SET status=''1'',chker=null,Chktime=null ');
//      sql.Add('where TMMId=' + quotedstr(trim(Order_Main.fieldbyname('TMMId').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('TMMNO').AsString))));
//      sql.Add(',' + quotedstr(trim('成功')));
//      sql.Add(')');
//      execsql;
//    end;
//    TBRafresh.Click;
//  except
//    application.MessageBox('计划单撤销审核失败！', '提示信息', 0);
//  end;
//end;

procedure TfrmTradeMarketList_XYth.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_Market_th_Main SET status=''10'' ');
      sql.Add('where TMMId=' + quotedstr(trim(Order_Main.fieldbyname('TMMId').AsString)));

      execsql;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Trade_Market_th ');
      Sql.Add(' @TMMID=' + quotedstr(Trim(Order_Main.fieldbyname('TMMId').AsString)));
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));

      Open;
    end;
    if ADOQueryTemp.FieldByName('intReturn').AsInteger = -1 then
    begin
      Application.MessageBox(PChar(ADOQueryTemp.fieldbyname('ShowMsg').AsString), '提示', 0);
      exit;
    end;

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails) ');
      sql.Add('values(''贸易布坯出库''    ');
      sql.Add(',' + quotedstr(trim(Order_Main.fieldbyname('TMMId').AsString)));
      sql.Add(',''完成''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim(Order_Main.fieldbyname('TMMId').AsString)));
      sql.Add(')  ');
      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('TMMNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;

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

procedure TfrmTradeMarketList_XYth.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 Application.MessageBox('是否核实好数据？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Trade_Market_th_Main SET status=''1'' ');
      sql.Add('where TMMId=' + quotedstr(trim(Order_Main.fieldbyname('TMMId').AsString)));

      execsql;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Trade_Market_th ');
      Sql.Add(' @TMMID=' + quotedstr(Trim(Order_Main.fieldbyname('TMMId').AsString)));
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
      Open;
    end;
    if ADOQueryTemp.FieldByName('intReturn').AsInteger = -1 then
    begin
      Application.MessageBox(PChar(ADOQueryTemp.fieldbyname('ShowMsg').AsString), '提示', 0);
      exit;
    end;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails) ');
      sql.Add('values(''贸易布坯出库''    ');
      sql.Add(',' + quotedstr(trim(Order_Main.fieldbyname('TMMId').AsString)));
      sql.Add(',''完成''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim(Order_Main.fieldbyname('TMMId').AsString)));
      sql.Add(')  ');

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

procedure TfrmTradeMarketList_XYth.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_Market_th_Main SET status=''2'' ');
      sql.Add(' where TMMId=' + quotedstr(trim(Order_Main.fieldbyname('TMMId').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('TMMNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    SendMessageQR(trim(Order_Main.FieldByName('TMMId').AsString), trim(Order_Main.FieldByName('Filler').AsString), trim(Order_Main.FieldByName('TMMNO').AsString), Trim(FormatDateTime('yyyy-MM-dd', Order_Main.FieldByName('TMMDate').asdatetime)));

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('exec P_MSG_in ');
      sql.Add(' @MSG=' + QuotedStr(trim('计划单号：' + trim(Order_Main.FieldByName('TMMNO').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 TfrmTradeMarketList_XYth.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_Market_Main SET status=''1''  ');
      sql.Add(' where TMMId=' + quotedstr(trim(Order_Main.fieldbyname('TMMId').AsString)));
      sql.Add(' and isnull(status,''0'')=''2'' ');
      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('TMMNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('计划单提交撤销成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('计划单提交撤销失败！', '提示信息', 0);
  end;
end;

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

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

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

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

end;

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

end;

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

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

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

procedure TfrmTradeMarketList_XYth.ToolButton1Click(Sender: TObject);
begin
  TcxGridToExcel(Trim(Self.Caption), cxGrid1);
end;

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

procedure TfrmTradeMarketList_XYth.TBZFClick(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_Market_Main SET status=''11'' ');
      sql.Add('where TMMId=' + quotedstr(trim(Order_Main.fieldbyname('TMMId').AsString)));

      sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails) ');
      sql.Add('values(''贸易布坯出库''    ');
      sql.Add(',' + quotedstr(trim(Order_Main.fieldbyname('TMMId').AsString)));
      sql.Add(',''作废''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim(Order_Main.fieldbyname('TMMId').AsString)));
      sql.Add(')  ');

      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('TMMNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;

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

end.

