unit U_PBRKSHList;

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, cxContainer, cxGroupBox, ShellAPI, dxSkinsCore,
  dxSkinsDefaultPainters, dxSkinscxPCPainter;

type
  TfrmPBRKSHList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: 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;
    TBDel: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    TBEdit: TToolButton;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    CPName: TEdit;
    FromFactoryName: TEdit;
    DHID: TEdit;
    cxTabControl1: TcxTabControl;
    PopupMenu2: TPopupMenu;
    N3: TMenuItem;
    N4: TMenuItem;
    ToolButton6: TToolButton;
    DS_DH: TDataSource;
    CDS_DH: TClientDataSet;
    cxGrid1: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v1CRType: TcxGridDBColumn;
    VC_FromFactoryName: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    VC_DHPS: TcxGridDBColumn;
    VC_DHQty: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ADOQueryDH: TADOQuery;
    V2Column1: TcxGridDBColumn;
    V2Column2: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    ToolButton1: TToolButton;
    V2Column4: TcxGridDBColumn;
    Label6: TLabel;
    OrderNo: TEdit;
    IdHTTP1: TIdHTTP;
    CDS_WXTS: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    CheckBox1: TCheckBox;
    Label7: TLabel;
    DQFactoryName: TEdit;
    Label8: TLabel;
    CPcode: TEdit;
    V2Column5: TcxGridDBColumn;
    Label9: TLabel;
    DHType: TComboBox;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    TV2Column1: TcxGridDBColumn;
    Label10: TLabel;
    filler: TEdit;
    cxGroupBox1: TcxGroupBox;
    ScrollBox1: TScrollBox;
    DS_IMG: TDataSource;
    Qry_Img: TADOQuery;
    ToolButton2: TToolButton;
    Label11: TLabel;
    DHQty2: TEdit;
    procedure FormDestroy(Sender: TObject);
    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 TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure DHIDChange(Sender: TObject);
    procedure FromFactoryNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure DQFactoryNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure cxGridDBColumn3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxGridDBColumn9PropertiesEditValueChanged(Sender: TObject);
    procedure TV2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure V2Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxGridDBColumn2PropertiesEditValueChanged(Sender: TObject);
    procedure DHTypeChange(Sender: TObject);
    procedure TV2CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure cxGridDBColumn11PropertiesChange(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_ZdyAttachGYS, U_SysLogHelp;

{$R *.dfm}

procedure TfrmPBRKSHList.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 TfrmPBRKSHList.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 TfrmPBRKSHList.FormDestroy(Sender: TObject);
begin
  //frmShaRKList:=nil;
end;

procedure TfrmPBRKSHList.SetStatus();
begin
  cxGridDBColumn11.Options.Editing := False;

  if cxTabControl1.TabIndex = 0 then
  begin
    cxGridDBColumn11.Options.Editing := TRUE;
  end;
end;

procedure TfrmPBRKSHList.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 TfrmPBRKSHList.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 TfrmPBRKSHList.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 TfrmPBRKSHList.InitGrid();
begin
//  try
//    ADOQueryMain.DisableControls;
//    with ADOQueryMain do
//    begin
//      Filtered := False;
//      Close;
//      sql.Clear;
//      sql.Add(' select A.*,A.DQFactoryName  DQFactoryName1');
//      sql.Add(' from SupplierDeliver A');
//      sql.add(' where A.FHDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
//      sql.Add(' and A.FHDate<''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)) + '''');
//      case cxTabControl1.TabIndex of
//        0:
//          begin
//            Sql.Add(' and isnull(ChkStatus,'''')=''跟单已确认'' ');
//          end;
//        1:
//          begin
//            Sql.Add(' and isnull(ChkStatus,'''')=''仓库已确认'' ');
//          end;
//        2:
//          begin
//            Sql.Add(' and isnull(ChkStatus,'''') in (''跟单已确认'',''仓库已确认'') ');
//          end;
//      end;
//
//      Open;
//    end;
//    SCreateCDS20(ADOQueryMain, CDS_Main);
//    SInitCDSData20(ADOQueryMain, CDS_Main);
//  finally
//    ADOQueryMain.EnableControls;
//  end;

  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    sql.Add(' select *,DHQty2=cast(DHQty as varchar) from XH_Cloth_DH  ');
    sql.Add(' where  DHType  not  in (''回修入库'',''成品出库'',''库存对冲'',''调拨出库'',''调拨入库'') ');
    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

          if CheckBox1.Checked then
          begin
            sql.Add(' and  ChkStatus = ''已收货''');
          end
          else
          begin
         
//            sql.Add(' and FromFactoryName=' + quotedstr(Trim(CDS_Main.fieldbyname('SupplierName').AsString)));
            sql.Add(' and  ChkStatus = ''已收货''');
          end;
        end;
      1:
        begin
     
//          sql.Add(' and FromFactoryName=' + quotedstr(Trim(CDS_Main.fieldbyname('SupplierName').AsString)));
          sql.Add(' and  ChkStatus =''仓库已确认'' ');
//          sql.Add(' and FHID=' + quotedstr(Trim(CDS_Main.fieldbyname('FHID').AsString)));
        end;

    end;
//    ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryMain, CDS_DH);
  SInitCDSData20(ADOQueryMain, CDS_DH);
end;

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

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

procedure TfrmPBRKSHList.TBCloseClick(Sender: TObject);
begin
//  WriteCxGrid(Trim(self.Caption) + '1', Tv1, '坯布发货登记');
  WriteCxGrid(Trim(self.Caption) + '2', Tv2, '坯布发货登记');
  Close;
end;

procedure TfrmPBRKSHList.FormShow(Sender: TObject);
var
  FSJ: string;
begin
//  ReadCxGrid(Trim(self.Caption), Tv1, '坯布发货登记');

  FSJ := 'select DISTINCT name=DHTYPE  from XH_Cloth_DH  where  DHType  not  in (''回修入库'',''成品出库'',''库存对冲'',''调拨出库'',''调拨入库'') order by DHTYPE   ';
  SInitComBoxBySql(ADOQueryCmd, DHTYPE, False, FSJ);
  ReadCxGrid(Trim(self.Caption) + '2', Tv2, '坯布发货登记');
  SetStatus();
end;

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

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

end;

procedure TfrmPBRKSHList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_DH, True);
end;

procedure TfrmPBRKSHList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_DH, False);
end;

procedure TfrmPBRKSHList.TBDelClick(Sender: TObject);
var
  str: string;
begin
//  if CDS_Main.IsEmpty then
//    Exit;
  if CheckBox1.Checked then
  begin
    Application.MessageBox('请取消显示全部到货!', '提示', 0);
    Exit;
  end;
//  with ADOQueryCmd do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add(' update  SupplierDeliver  ');
//    sql.Add(' set ChkStatus=''跟单已确认'',CkChker=null');
//    sql.Add(' ,CkChkTime=null');
//    sql.Add(' ,CkChkNote=null');
//    sql.Add(' where FHId=''' + Trim(CDS_Main.fieldbyname('FHId').AsString) + '''');
//    ExecSQL;
//  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_MONEY_CR A inner join YF_MONEY_CR_sub B on A.YFID=B.YFID ');
    sql.Add('where  A.status=''1'' and B.YFPZNO=''' + Trim(CDS_DH.fieldbyname('DHId').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('财务数据已确认不能操作!', '提示', 0);
    Exit;
  end;
  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(' ,FHID=null');
    sql.Add(' where DHID=' + quotedstr(Trim(CDS_DH.fieldbyname('DHID').AsString)));

    sql.Add(' EXEC P_CW_ClothDH @DHId=''' + Trim(CDS_DH.fieldbyname('DHId').AsString) + '''');
    ExecSQL;
  end;
  Application.MessageBox('撤销审核成功!', '提示', 0);
  CDS_DH.Delete;

end;

procedure TfrmPBRKSHList.DHIDChange(Sender: TObject);
begin

  TBFind.Click;
end;

procedure TfrmPBRKSHList.FromFactoryNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

procedure TfrmPBRKSHList.ToolButton6Click(Sender: TObject);
var
  str, SMfsid, SKeyWord2, SUnit: string;
  QRQty: Double;
begin
//  if CDS_Main.IsEmpty then
//    Exit;
  if CheckBox1.Checked then
  begin
    Application.MessageBox('请取消显示全部到货!', '提示', 0);
    Exit;
  end;
  if CDS_DH.Locate('ssel', True, []) = False then
  begin
    Application.MessageBox('请选择到货数据!', '提示', 0);
    Exit;
  end;
  //判断数量是否一致
//  QRQty := CDS_Main.fieldbyname('Qty').AsFloat;
//  SUnit := Trim(CDS_Main.fieldbyname('Unit').AsString);
//
//  with CDS_DH do
//  begin
//    first;
//    while not Eof do
//    begin
//      if CDS_DH.fieldbyname('ssel').AsBoolean then
//      begin
//        QRQty := QRQty - CDS_DH.fieldbyname('DHQty').AsFloat;
//      end;
//      Next;
//    end;
//  end;
//  if SUnit = 'M' then
//  begin
//    if (QRQty < -3) or (QRQty > 3) then
//    begin
//      Application.MessageBox('长度范围超出±3M!', '提示', 0);
//      Exit;
//    end;
//  end
//  else if SUnit = 'KG' then
//  begin
//    if (QRQty < -1) or (QRQty > 1) then
//    begin
//      Application.MessageBox('重量范围超出±1KG!', '提示', 0);
//      Exit;
//    end;
//  end
//  else
//  begin
//    Application.MessageBox('数量单位异常无法确认!', '提示', 0);
//    Exit;
//  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_MONEY_CR A inner join YF_MONEY_CR_sub B on A.YFID=B.YFID ');
    sql.Add('where  A.status=''1'' and B.YFPZNO=''' + Trim(CDS_DH.fieldbyname('DHId').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('财务数据已确认不能操作!', '提示', 0);
    Exit;
  end;
  if InputQuery('确认备注', '请输入：', str) then
  begin
    if trim(str) <> '' then
    begin
//      with ADOQueryCmd do
//      begin
//        Close;
//        sql.Clear;
//        sql.Add(' update  SupplierDeliver  ');
//        sql.Add(' set ChkStatus=''仓库已确认'', CkChker=' + quotedstr(Trim(DName)));
//        sql.Add(' ,CkChkTime=getdate()');
//        sql.Add(' ,CkChkNote=' + quotedstr(Trim(str)));
//        sql.Add(' where FHId=''' + Trim(CDS_Main.fieldbyname('FHId').AsString) + '''');
//        ExecSQL;
//      end;
      while CDS_DH.Locate('ssel', True, []) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' update  XH_Cloth_DH  ');
          sql.Add(' set ChkStatus=''仓库已确认'', Chker=' + quotedstr(Trim(DName)));
          sql.Add(' ,ChkTime=getdate()');
//          sql.Add(' ,FHID=' + quotedstr(Trim(CDS_Main.fieldbyname('FHID').AsString)));
          sql.Add(' where DHId=''' + Trim(CDS_DH.fieldbyname('DHId').AsString) + '''');

          sql.Add(' EXEC P_CW_ClothDH @DHId=''' + Trim(CDS_DH.fieldbyname('DHId').AsString) + '''');
          ExecSQL;
        end;
        CDS_DH.Edit;
        CDS_DH.FieldByName('SSel').Value := False;
      end;
//      SMfsid := Trim(CDS_Main.fieldbyname('FHId').AsString);
//      SKeyWord2 := '存放工厂：' + Trim(CDS_Main.fieldbyname('DQFactoryName').AsString) + ';品名：' + Trim(CDS_Main.fieldbyname('CPName').AsString) + ';匹数：' + Trim(CDS_Main.fieldbyname('PS').AsString) + ';数量：' + Trim(CDS_Main.fieldbyname('Qty').AsString) + '。';
//      SendMessageQR(SMfsid, SKeyWord2);
      Application.MessageBox('确认成功!', '提示', 0);
      CDS_DH.Delete;
    end
    else
    begin
      Application.MessageBox('请输入确认备注!', '提示', 0);
    end;
  end;

end;

procedure TfrmPBRKSHList.TBEditClick(Sender: TObject);
var
  str, strsql, SMfsid, SKeyWord2: string;
begin
  if CDS_DH.IsEmpty then
    Exit;
//  if CheckBox1.Checked then
//  begin
//    Application.MessageBox('请取消显示全部到货!', '提示', 0);
//    Exit;
//  end;
  if InputQuery('驳回原因', '请输入', str) then
  begin

    if trim(str) <> '' then
    begin
      while CDS_DH.Locate('SSel', True, []) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' update  XH_Cloth_DH  ');
          sql.Add(' set ChkStatus=''已驳回'' ');
          sql.Add(' ,RejectReason=' + quotedstr(Trim(str)));
          sql.Add(' where DHId=''' + Trim(CDS_DH.fieldbyname('DHId').AsString) + '''');
          ExecSQL;
        end;
        strsql := '到货ID：' + Trim(CDS_DH.fieldbyname('DHId').AsString) + '驳回原因：' + trim(str);
        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;

        SMfsid := Trim(CDS_DH.fieldbyname('DHId').AsString);
        SKeyWord2 := '染厂：' + Trim(CDS_DH.fieldbyname('DQFactoryName').AsString) + ';品名：' + Trim(CDS_DH.fieldbyname('CPName').AsString) + ';匹数：' + Trim(CDS_DH.fieldbyname('DHPS').AsString) + ';数量：' + Trim(CDS_DH.fieldbyname('DHQty').AsString) + '。';
        SendMessageBH(Trim(CDS_DH.fieldbyname('GdName').AsString), SMfsid, SKeyWord2);
        CDS_DH.Delete;
      end;

//      GetHTTP(IdHTTP1, 'http://www.rightsoft.top/yifu/api/message/Send/OrderProgress');
      Application.MessageBox('驳回成功!', '提示', 0);

    end
    else
    begin
      Application.MessageBox('请输入驳回原因!', '提示', 0);
    end;
  end;

end;

procedure TfrmPBRKSHList.DQFactoryNameChange(Sender: TObject);
begin

  if ADOQueryDH.Active then
  begin
    SDofilter(ADOQueryDH, SGetFilters(Panel1, 3, 4));
    SCreateCDS20(ADOQueryDH, CDS_DH);
    SInitCDSData20(ADOQueryDH, CDS_DH);
  end;
end;

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

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

procedure TfrmPBRKSHList.cxGridDBColumn3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  MFactoryName, MFactoryNO: string;
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        MFactoryNO := Trim(CDS_HZ.fieldbyname('CoCode').AsString);
        MFactoryName := Trim(CDS_HZ.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update XH_Cloth_DH set  FromFactoryNo =' + QuotedStr(Trim(MFactoryNO)));
    sql.Add(' ,FromFactoryName   =' + QuotedStr(Trim(MFactoryName)));
    sql.Add('where DHId=' + quotedstr(Trim(CDS_DH.fieldbyname('DHId').AsString)));
    ExecSQL;
  end;
  with CDS_DH do
  begin
    Edit;
    FieldByName('FromFactoryNo').Value := MFactoryNO;
    FieldByName('FromFactoryName').Value := MFactoryName;
    Post;
  end;
  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmPBRKSHList.cxGridDBColumn9PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_DH do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;

  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update XH_Cloth_DH set  ' + FFieldName + '=' + (Trim(mvalue)));
    sql.Add('where DHId=' + quotedstr(Trim(CDS_DH.fieldbyname('DHId').AsString)));
    ExecSQL;
  end;
  tv2.Controller.EditingController.ShowEdit();

end;

procedure TfrmPBRKSHList.TV2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if cxTabControl1.TabIndex <> 0 then
  begin
    VC_FromFactoryName.Options.Editing := False;
    VC_DHPS.Options.Editing := False;
    VC_DHQty.Options.Editing := False;
  end
  else
  begin
    VC_FromFactoryName.Options.Editing := True;
    VC_DHPS.Options.Editing := True;
    VC_DHQty.Options.Editing := True;
  end;

end;

procedure TfrmPBRKSHList.V2Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  MFactoryName, MFactoryNO: string;
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        MFactoryNO := Trim(CDS_HZ.fieldbyname('CoCode').AsString);
        MFactoryName := Trim(CDS_HZ.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update XH_Cloth_DH set  DQFactoryNO =' + QuotedStr(Trim(MFactoryNO)));
    sql.Add(' ,DQFactoryName   =' + QuotedStr(Trim(MFactoryName)));
    sql.Add('where DHId=' + quotedstr(Trim(CDS_DH.fieldbyname('DHId').AsString)));
    ExecSQL;
  end;
  with CDS_DH do
  begin
    Edit;
    FieldByName('DQFactoryNO').Value := MFactoryNO;
    FieldByName('DQFactoryName').Value := MFactoryName;
    Post;
  end;
  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmPBRKSHList.cxGridDBColumn2PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_DH do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update XH_Cloth_DH set  ' + FFieldName + '=' + QuotedStr((Trim(mvalue))));
    sql.Add(' where DHId=''' + Trim(CDS_DH.fieldbyname('DHId').AsString) + '''');
//    ShowMessage(sql.Text);
    ExecSQL;
  end;
  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmPBRKSHList.DHTypeChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmPBRKSHList.TV2CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
var                         //InitImage(); 但没有打开的方法 打开要点图片按钮
  i, j: integer;
  jpg: TJpegImage;
  myStream: TADOBlobStream;
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);
  if CDS_DH.IsEmpty then
    exit;
  try
    with Qry_Img do
    begin
      close;
      sql.Clear;
      sql.Add(' select A.TFID,A.WBID,A.FilesOther,A.FileName,A.url from TP_File A');
      sql.add(' where A.WBID=' + quotedstr(trim(CDS_DH.fieldbyname('DHID').AsString)));
      open;
    end;

    if Qry_Img.fieldbyname('url').AsString <> '' then
    begin
      j := Qry_Img.RecordCount;
      if j < 1 then
        exit;
      Qry_Img.DisableControls;
      Qry_Img.First;
      SetLength(Mach, j);
      jpg := TJpegImage.Create();
      for i := 0 to j - 1 do
      begin
        if triM(Qry_Img.fieldbyname('FilesOther').AsString) <> '' then
        begin
          myStream := tadoblobstream.Create(tblobfield(Qry_Img.fieldbyname('FilesOther')), bmread);
          jpg.LoadFromStream(myStream);
          Mach[i] := TfrmSlt.Create(Self);
          Mach[i].Name := trim(Qry_Img.fieldbyname('TFID').AsString);
          Mach[i].Parent := ScrollBox1;
          Mach[i].Left := 0 + i * 165;
          Mach[i].Init(Qry_Img.fieldbyname('TFID').AsString, Qry_Img.fieldbyname('FileName').AsString, jpg);
        //打开url网址的图片
        //ShellExecute(Handle, 'open', 'Explorer.exe', PWideChar(Qry_Img.fieldbyname('url').asstring), nil, SW_SHOWNORMAL);
        end;
        Qry_Img.Next;
      end;
    end;
    Qry_Img.EnableControls;
  finally
    jpg.free;
    application.ProcessMessages;
  end;
end;

procedure TfrmPBRKSHList.ToolButton2Click(Sender: TObject);
var
  i, j: integer;
  jpg: TJpegImage;
  myStream: TADOBlobStream;
begin
  if CDS_DH.IsEmpty = True then
    Exit;
  j := length(Mach);
  if j > 0 then
  begin
    for i := 0 to j - 1 do
    begin
      Mach[i].free;
    end;
  end;
  SetLength(Mach, 0);
  if CDS_DH.IsEmpty then
    exit;
  try
    with Qry_Img do
    begin
      close;
      sql.Clear;
      sql.Add(' select A.* from TP_File A');
      sql.add(' where A.WBID=' + quotedstr(trim(CDS_DH.fieldbyname('DHID').AsString)));
      open;
    end;
    j := Qry_Img.RecordCount;
    if j < 1 then
      exit;
    Qry_Img.DisableControls;
    Qry_Img.First;
    SetLength(Mach, j);
    jpg := TJpegImage.Create();
    for i := 0 to j - 1 do
    begin
      if triM(Qry_Img.fieldbyname('FilesOther').AsString) <> '' then
      begin
        myStream := tadoblobstream.Create(tblobfield(Qry_Img.fieldbyname('FilesOther')), bmread);
        jpg.LoadFromStream(myStream);
        Mach[i] := TfrmSlt.Create(Self);
        Mach[i].Name := trim(Qry_Img.fieldbyname('TFID').AsString);
        Mach[i].Parent := ScrollBox1;
        Mach[i].Left := 0 + i * 165;
        Mach[i].Init(Qry_Img.fieldbyname('TFID').AsString, Qry_Img.fieldbyname('FileName').AsString, jpg);
        //打开url网址的图片
        ShellExecute(Handle, 'open', 'Explorer.exe', PAnsiChar(Qry_Img.fieldbyname('url').asstring), nil, SW_SHOWNORMAL);
      end;
      Qry_Img.Next;
    end;
    Qry_Img.EnableControls;
  finally
    jpg.free;
    application.ProcessMessages;
  end;
end;

procedure TfrmPBRKSHList.cxGridDBColumn11PropertiesChange(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_DH do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate XH_Cloth_DH ');
      sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
      sql.Add(' where DHID=' + QuotedStr(CDS_DH.fieldbyname('DHID').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('ID：' + trim(CDS_DH.FieldByName('DHID').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');

//      ShowMessage(SQL.text);
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv2.Controller.EditingController.ShowEdit();
  except
    tv2.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

end.

