unit U_CPCKQEList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridBandedTableView, cxGridDBBandedTableView, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridDBTableView, cxGrid, StdCtrls, ComCtrls,
  ExtCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB, DBClient,
  cxDropDownEdit, cxCheckBox, RM_Common, RM_Class, RM_e_Xls, RM_Dataset,
  RM_System, RM_GridReport, Menus, cxCalendar, cxButtonEdit, cxTextEdit, cxPC,
  IniFiles, jpeg, U_SLT, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdHTTP, cxLookAndFeels, cxLookAndFeelPainters,
  dxBarBuiltInMenu, cxNavigator;

type
  TfrmCPCKQEList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    TBCXQR: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    CPName: TEdit;
    DQFactoryName: TEdit;
    DHID: TEdit;
    cxTabControl1: TcxTabControl;
    PopupMenu2: TPopupMenu;
    N3: TMenuItem;
    N4: TMenuItem;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v1CRType: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    V2Column1: TcxGridDBColumn;
    V2Column2: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    ToolButton1: TToolButton;
    V2Column4: TcxGridDBColumn;
    Label6: TLabel;
    OrderNo: TEdit;
    IdHTTP1: TIdHTTP;
    CDS_WXTS: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ToolButton2: TToolButton;
    CheckBox1: TCheckBox;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBCXQRClick(Sender: TObject);
    procedure DHIDChange(Sender: TObject);
    procedure DQFactoryNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
  private
    canshu1, canshu2, canshu3: string;
    procedure InitGrid();
    procedure SetStatus();
    procedure ReadINIFile();
    procedure SendMessageQR(Mfsid, KeyWord2: string);
    procedure SendMessageBH(GdName, Mfsid, KeyWord2: string);
    { Private declarations }
  public
    { Public declarations }
  end;

//var
 // frmShaRKList: TfrmShaRKList;
var
  Mach: array of TfrmSlt;

implementation

uses
  U_DataLink, U_RTFun, U_Fun, U_PBRKInPut, U_FileUp, U_WechatAuthority,
  U_SysLogHelp;

{$R *.dfm}

procedure TfrmCPCKQEList.SendMessageQR(Mfsid, KeyWord2: 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;
  SCreateCDS20(ADOQueryTemp, CDS_WXTS);
  SInitCDSData20(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 := 'OrderProgress';
      FieldByName('MessageName').Value := '订单进度提醒';
      FieldByName('Title').Value := '您有新的坯布到货仓库已确认，请及时处理';
      FieldByName('URL').Value := 'pages/grayfabric/ywConfirm?fhid=' + Trim(Mfsid);
      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(Mfsid);
      FieldByName('KeyWord1').Value := Trim(Mfsid);
      FieldByName('KeyWord2').Value := Trim(KeyWord2);
      Post;
    end;
    CDS_WXTS.Next;
  end;

  GetHTTP(IdHTTP1, 'http://www.rightsoft.top/yifu/api/message/Send/OrderProgress');
end;

procedure TfrmCPCKQEList.SendMessageBH(GdName, Mfsid, KeyWord2: string);
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.* from SY_User A  where A.Valid=''Y'' ');
    SQL.Add('and isnull(A.wxid,'''')<>''''');
    SQL.Add(' and UserName= ' + quotedstr(Trim(GdName)));
    Open;
  end;

  if ADOQueryTemp.IsEmpty then
  begin
    Application.MessageBox('用户未绑定微信,提交失败！', '提示信息', 0);
    Exit;
  end;
  ADOQueryTemp.First;

  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from WeixinMessage where 1<>1');
    Open;
    Append;
    FieldByName('MessageType').Value := 'OrderProgress';
    FieldByName('MessageName').Value := '订单进度提醒';
    FieldByName('Title').Value := '您有新的坯布到货被驳回，请及时处理';
    FieldByName('URL').Value := 'pages/grayfabric/gdConfirm?fhid=' + Trim(Mfsid);
    FieldByName('OpenId').Value := Trim(ADOQueryTemp.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(Mfsid);
    FieldByName('KeyWord1').Value := Trim(Mfsid);
    FieldByName('KeyWord2').Value := Trim(KeyWord2);
    Post;
  end;

end;

procedure TfrmCPCKQEList.SetStatus();
begin
  TBCXQR.Visible := False;
  case cxTabControl1.TabIndex of
    0:
      begin

      end;
    1:
      begin
        TBCXQR.Visible := True;
      end;
    2:
      begin

      end;
  end;
end;

procedure TfrmCPCKQEList.FormClose(Sender: TObject; var Action: TCloseAction);
var
  i, j: integer;
begin
  j := length(Mach);
  if j > 0 then
  begin
    for i := 0 to j - 1 do
    begin
      Mach[i].free;
    end;
  end;
  SetLength(Mach, 0);
  Action := caFree;
end;

procedure TfrmCPCKQEList.ReadINIFile();
var
  programIni: Tinifile;  //配置文件名
  FileName: string;
begin
  FileName := ExtractFilePath(Paramstr(0)) + 'SYSTEMSET.INI';
  programIni := Tinifile.create(FileName);
  server := programIni.ReadString('SERVER', '服务器地址', '127.0.0.1');
  programIni.Free;
end;

procedure TfrmCPCKQEList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime;
  canshu1 := Trim(DParameters1);
  canshu2 := Trim(DParameters2);
  canshu3 := Trim(DParameters3);
end;

procedure TfrmCPCKQEList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select * from XH_Cloth_DH  ');
      sql.Add(' where  DHType=''成品出库'' ');
      sql.add(' and DHDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
      sql.Add(' and DHDate<''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)) + '''');
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add(' and  ChkStatus =''已发货'' ');
          end;
        1:
          begin
            sql.Add(' and  ChkStatus =''仓库已确认'' ');
          end;
        2:
          begin
            sql.Add(' and  ChkStatus =''已驳回'' ');
          end;
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCPCKQEList.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;

procedure TfrmCPCKQEList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  end;
end;

procedure TfrmCPCKQEList.TBCloseClick(Sender: TObject);
begin
  if Trim(DName)='ADMIN' then
  begin
    WriteCxGridAll(trim(self.Caption), Tv1,ADOQueryCmd,ADOQueryTemp);
  end;


  Close;
end;

procedure TfrmCPCKQEList.FormShow(Sender: TObject);
begin
  ReadCxGridAll(trim(self.Caption), Tv1);
  SetStatus();
end;

procedure TfrmCPCKQEList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;

end;

procedure TfrmCPCKQEList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmCPCKQEList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

procedure TfrmCPCKQEList.TBCXQRClick(Sender: TObject);
var
  strsql: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('ssel', True, []) = False then
  begin
    Application.MessageBox('请选择到货数据!', '提示', 0);
    Exit;
  end;
  while CDS_Main.Locate('ssel', True, []) do
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' update  XH_Cloth_DH  ');
      sql.Add(' set ChkStatus=''已发货'', Chker=null');
      sql.Add(' ,ChkTime=null');
      sql.Add(' where DHID=' + quotedstr(Trim(CDS_Main.fieldbyname('DHID').AsString)));
      sql.Add(' and ChkStatus=''仓库已确认''');
      ExecSQL;
    end;
    strsql := 'ID：' + Trim(CDS_Main.fieldbyname('DHId').AsString) + '确认人：' + Trim(CDS_Main.fieldbyname('Chker').AsString) + '确认时间：' + Trim(CDS_Main.fieldbyname('ChkTime').AsString);
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('成品撤销确认')));
      sql.Add(',' + quotedstr(trim(strsql)));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      ExecSQL;
    end;
    CDS_Main.Delete;
  end;
  Application.MessageBox('撤销审核成功!', '提示', 0);
end;

procedure TfrmCPCKQEList.DHIDChange(Sender: TObject);
begin

  TBFind.Click;
end;

procedure TfrmCPCKQEList.DQFactoryNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

procedure TfrmCPCKQEList.ToolButton1Click(Sender: TObject);
begin
  frmSysLogHelp := TfrmSysLogHelp.create(self);
  with frmSysLogHelp do
  begin
    fModel := self.caption;
  //  facction:='生产指示单删除';
    showmodal;
    free;
  end;
end;

procedure TfrmCPCKQEList.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel(self.Caption, cxGrid1);
end;

procedure TfrmCPCKQEList.CheckBox1Click(Sender: TObject);
begin
  if CheckBox1.Checked=True then
  begin
    DHID.Tag:=1;
    CPName.Tag:=1;
    DQFactoryName.Tag:=1;
    OrderNo.Tag:=1;
  end else
  begin
    DHID.Tag:=2;
    CPName.Tag:=2;
    DQFactoryName.Tag:=2;
    OrderNo.Tag:=2;
  end;
  TBFind.Click;
end;

end.

