unit U_FormPas;

interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, StdCtrls, BtnEdit, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit,DBGrids, DB, cxDBData,
  cxGridLevel, cxClasses, cxControls, cxGridCustomView, ADODB,StrUtils,
  Midas,cxGridCustomTableView, cxGridTableView, cxGridDBTableView,cxTimeEdit,
    cxGrid,cxDBLookupComboBox,cxCalendar, cxCurrencyEdit,cxExportGrid4Link,
  ExtCtrls, Buttons,DBClient,FTComboBox,cxDropDownEdit;
type
    TA = class(TComponent)
    public
      S:string;
end;
procedure ClearWinData(mParent:TWinControl);
procedure InitComBoxBySql(ADOQueryTmp:TADOQuery;cb: TComboBox;
                          sqlstr:string;Boxtype:integer;showMsg:string;emptyFlag:Boolean);
procedure SplitCtrName(ms:string;mvg:string;var mtable:string;var mfield:string);
procedure InitComBoxByCode(ADOQueryTmp:TADOQuery;
                           cb: TComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean
                           );
procedure InitFtComBoxByCode(ADOQueryTmp:TADOQuery;
                           cb: TFtComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean
                           );
procedure InitFtComBoxByCode10(ADOQueryTmp:TADOQuery;
                           cb: TFtComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean;
                           isClearOld:boolean
                           );
procedure SetWinData(ADOQueryTmp:TADOQuery;mParent:TWinControl);
procedure SetWinData_CDS(mCDS:TclientDataset;mParent:TWinControl);
procedure CreateCDS(SADOqry:TADOQuery; mClientDataset:TclientDataSet);
procedure CreateCDS20(SADOQry:TADOQuery; mClientDataset:TclientDataSet );
procedure InitCDSData(fromADO:TADOQuery;toCDS:TclientDataSet);
procedure InitCDSData20(fromADO:TADOQuery;toCDS:TclientDataSet);
procedure CreateCxGridCol(ADOQueryTmp:TADOQuery;tv1:TcxGridDBTableView;HasFooter:Boolean);
procedure CreateFooterCol(tv1:TcxGridTableView;tcol:TcxGriddbColumn;mcolName:string;FooterType:string);
function getCombIdx(cobBox:TComboBox;k:integer;vv:string):integer;
procedure SaveTocxGridColSet(ADOQueryCmd:TADOQuery;winCode:string;gridName:string;cxGrid:TcxGridTableView);
procedure SaveToQrycxGridColSet(ADOQueryCmd:TADOQuery;winCode:string;cxGrid:TcxGridTableView);
procedure SaveTodbGridColSet(ADOQueryCmd:TADOQuery;winCode:string;gridName:string;dbGrid:TDbgrid);
procedure SaveToQrydbGridColSet(ADOQueryCmd:TADOQuery;winCode:string;dbGrid:TDbgrid);
procedure CreateCxGridCol10(ADOQueryTmp:TADOQuery;
                          tv1:TcxGridDBTableView;
                          HasFooter:Boolean);
procedure  TcxGridToExcel(mfileName:string;gridName:TcxGrid);

function GetMaxNo(ADOStoredProc1:TADOStoredProc;
                    var mMaxNo:string;
                    mFlag:string):Boolean;
function GetMaxNo20(ADOQueryTmp:TADOQuery;
                    var mMaxNo:string;
                    mTable:string;
                    mFlag:string;
                    hasTp:integer;lsLen:integer=3):Boolean;
function GetMaxGangNo(ADOQueryTmp:TADOQuery;
                    var mMaxNo:string;
                    mTable:string;
                    mFlag:string;
                    hasTp:integer;
                    lsLen:integer=3;
                    BhType:integer=0):Boolean;
procedure InitComBoxByUnit(ADOQueryTmp:TADOQuery;
                           cb: TComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean
                           );
procedure InitComBoxByUnit10(ADOQueryTmp:TADOQuery;
                           cb: TComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean
                           );
procedure InitFtComBoxByUnit(ADOQueryTmp:TADOQuery;
                           cb: TFtComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean
                           );
procedure InitFtComBoxByUnit10(ADOQueryTmp:TADOQuery;
                           cb: TFtComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean
                           );
procedure InitFtComBoxBySql(ADOQueryTmp:TADOQuery;
                           cb: TFtComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean;
                           mSql:string
                           );                                                   
procedure GetServerTime(ADOQueryTmp:TADOQuery);
function GetNameByBh(ADOQueryTmp:TADOQuery;mBhType:string;
                      mValue:string;var mCode:string;var mName:string):Boolean;
function GetNameByBh10(ADOQueryTmp:TADOQuery;mBhType:string;mValue:string):Boolean;                      
function GetServerTime10(ADOQueryTmp:TADOQuery):TdateTime;
procedure InitCxGridComboBoxByCode(ADOQueryTmp:TADOQuery;c3:TcxGriddbColumn;FlagType:string);
procedure GetDll(DllName:string;StrFormid:integer;Str:string);
procedure GetDll10(DllName:string;sFormid:integer;sDatabase:string;sTitle:string;sDef1:string;sDef2:string);

function KillNoFuncWinForm(formName:string;formTitle:string;formId:integer=0):boolean;
function GetMaxPlanNo(ADOQueryTmp:TADOQuery;
                    var mMaxNo:string;
                    mTable:string;
                    mFlag:string;
                    hasTp:integer;
                    lsLen:integer=3;
                    BhType:integer=0):Boolean;
procedure Dofilter(ADOQry:TADOQuery;FilterStr:string);
procedure DisableCtrl(mParent:TWinControl);
procedure DofilterNew(ADOQry:TADOQuery;FilterStr:string);
function GetNoDateMaxNo(ADOQueryTmp:TADOQuery;
                    var mMaxNo:string;
                    mTable:string;
                    mFlag:string;
                    lsLen:integer=3):Boolean;
procedure DofilterCDS(Cds_Sub:TClientDataSet;FilterStr:string);
var
  newh:hwnd;
implementation
uses U_global;
//////////////////////////////////////////////////////
  //函数：清空窗口数据
//////////////////////////////////////////////////////
procedure ClearWinData(mParent:TWinControl);
var
  i:integer;
begin
  with mParent do
  begin
    for i:=0 to ControlCount-1 do
    begin
      if Controls[i] is TLabel then continue;
      //EDIT
      if Controls[i] is TEdit then
      begin
        TEdit(Controls[i]).Text:='';
      end
      //combobox
      else if Controls[i] is TComboBox then
      begin
        TComboBox(Controls[i]).Text:='';
      end
      //checkBox
      else if Controls[i] is TCheckBox then
      begin
        TCheckBox(Controls[i]).Checked :=false;
      end
      //TBtnEdit
      else if (Controls[i] is TBtnEditA) or (Controls[i] is TBtnEditB)
          or (Controls[i] is TBtnEditC) then
      begin
        TBtnEditA(Controls[i]).text:='';
      end
      else if  Controls[i] is TdateTimePicker then
        TdateTimePicker(Controls[i]).DateTime :=Date()
      else if Controls[i] is Tmemo then
        Tmemo(Controls[i]).Text :=''
      else if Controls[i] is TcxDateEdit then
      begin
        TcxDateEdit(Controls[i]).Clear ;
      end
      else if Controls[i] is TcxTimeEdit then
      begin
        TcxTimeEdit(Controls[i]).Clear ;
      end
      else if Controls[i] is TcxCurrencyEdit then
      begin
          TcxCurrencyEdit(Controls[i]).Clear;
      end;
    end;
  end;

end;
//////////////////////////////////////////////////////
  //函数：使容器内的内容不能修改
//////////////////////////////////////////////////////
procedure DisableCtrl(mParent:TWinControl);
var
  i:integer;
begin
  with mParent do
  begin
    for i:=0 to ControlCount-1 do
    begin
      if Controls[i] is TLabel then continue;
      if Controls[i] is Tmemo then
        Tmemo(Controls[i]).ReadOnly :=true
      else
        Controls[i].Enabled :=false;
    end;
  end;

end;
/////////////////////////////////////////////////////
  //函数功能:初始化窗口数据
/////////////////////////////////////////////////////
procedure SetWinData(ADOQueryTmp:TADOQuery;mParent:TWinControl);
var
  i,idx:integer;
  mfield:string;
  ma:TA;
begin
  with ADOQueryTmp do
  begin
    if isEmpty then exit;
    with mParent  do
    begin
      for i:=0 to ControlCount-1 do
      begin
        if Controls[i] is TLabel then continue;
        if Controls[i].Tag >=999 then  continue;
        mfield:=Controls[i].Name;

        ///////////////////////////
        //EDIT
        if Controls[i] is TEdit then
        begin
          if Controls[i].Tag =999 then
            continue
          else
            TEdit(Controls[i]).Text:=trim(fieldByName(mfield).AsString);
        end
        //ftcombobox
        else if Controls[i] is TFTComboBox then
        begin
          if Controls[i].Tag =99 then
          begin
           idx:=TftComboBox(Controls[i]).Items.IndexOf(trim(fieldByName(mfield).AsString));
           TComboBox(Controls[i]).ItemIndex:=idx;
          end
          else
          begin
           idx:=TftComboBox(Controls[i]).IndexOfItem2(trim(fieldByName(mfield).AsString));
           TComboBox(Controls[i]).ItemIndex:=idx;
          end;
        end
        //combobox
        else if Controls[i] is TComboBox then
        begin
          if TComboBox(Controls[i]).Items.Count>0 then
          begin
            idx:=getCombIdx(TComboBox(Controls[i]),i,trim(fieldByName(mfield).AsString));
          end
          else
            idx:=-1;
          TComboBox(Controls[i]).ItemIndex:=idx;
        end
        else if Controls[i] is TBtnEditA then
        begin
          if TBtnEditA(Controls[i]).Tag=1 then
          begin
            TBtnEditA(Controls[i]).TxtCode := trim(fieldByName(mfield).AsString);
            TBtnEditA(Controls[i]).text:= trim(fieldByName(mfield+'Name').AsString);
          end
          else
          begin
            TBtnEditA(Controls[i]).TxtCode := trim(fieldByName(mfield).AsString);
            TBtnEditA(Controls[i]).text:= trim(fieldByName(mfield).AsString);
          end;
        end
        else if Controls[i] is TBtnEditC then
        begin
          if TBtnEditC(Controls[i]).Tag=1 then
          begin
            TBtnEditC(Controls[i]).TxtCode := trim(fieldByName(mfield).AsString);
            TBtnEditC(Controls[i]).text:= trim(fieldByName(mfield+'Name').AsString);
          end
          else
          begin
            TBtnEditC(Controls[i]).TxtCode := trim(fieldByName(mfield).AsString);
            TBtnEditC(Controls[i]).text:= trim(fieldByName(mfield).AsString);
          end
        end
        else if Controls[i] is TDateTimePicker then
        begin
          if IsEmpty  or fieldByName(mfield).IsNull then
          begin
            if TDateTimePicker(Controls[i]).Checked then
              TDateTimePicker(Controls[i]).Date:=strToDate('1990-01-01');
          end
          else
            TDateTimePicker(Controls[i]).Date:=fieldByName(mfield).AsDateTime;
        end
        else if Controls[i] is Tmemo then
        begin
            Tmemo(Controls[i]).Text :=trim(fieldByName(mfield).AsString);
        end
        else if Controls[i] is TcxDateEdit then
        begin
           if not IsEmpty  and not fieldByName(mfield).IsNull then
             TcxDateEdit(Controls[i]).text :=fieldByName(mfield).AsString;
        end
        else if Controls[i] is TcxTimeEdit then
        begin
           if not IsEmpty  and ( fieldByName(mfield).AsString<>'') then
             TcxTimeEdit(Controls[i]).text :=fieldByName(mfield).AsString;
        end
        else if Controls[i] is TcxCurrencyEdit then
        begin
          TcxCurrencyEdit(Controls[i]).Text:=fieldByName(mfield).AsString;
        end
        else if  Controls[i] is TcheckBox then
        begin
          TcheckBox(Controls[i]).Checked:=fieldByName(mfield).asBoolean;
        end;
      end; // end for
    end; // end with
  end;  //end for with

end;
/////////////////////////////////////////////////////
  //函数功能:初始化窗口数据
  //数据源为TclientDataset
/////////////////////////////////////////////////////
procedure SetWinData_CDS(mCDS:TclientDataset;mParent:TWinControl);
var
  i,idx:integer;
  mfield:string;
  ma:TA;
begin
  with mCDS do
  begin
    with mParent  do
    begin
      for i:=0 to ControlCount-1 do
      begin
        if Controls[i] is TLabel then continue;
        if Controls[i].Tag =999 then
            continue ;

        mfield:=Controls[i].Name;

        ///////////////////////////
        //EDIT
        if Controls[i] is TEdit then
        begin
          TEdit(Controls[i]).Text:=trim(fieldByName(mfield).AsString);
        end
        //ftcombobox
        else if Controls[i] is TFTComboBox then
        begin
          if Controls[i].Tag =99 then
          begin
           idx:=TftComboBox(Controls[i]).Items.IndexOf(trim(fieldByName(mfield).AsString));
           TComboBox(Controls[i]).ItemIndex:=idx;
          end
          else
          begin
           idx:=TftComboBox(Controls[i]).IndexOfItem2(trim(fieldByName(mfield).AsString));
           TComboBox(Controls[i]).ItemIndex:=idx;
          end;
        end
        //combobox
        else if Controls[i] is TComboBox then
        begin
          if TComboBox(Controls[i]).Items.Count>0 then
          begin
            idx:=getCombIdx(TComboBox(Controls[i]),i,trim(fieldByName(mfield).AsString));
          end
          else
            idx:=-1;
          TComboBox(Controls[i]).ItemIndex:=idx;
        end
        else if Controls[i] is TBtnEditA then
        begin
          if TBtnEditA(Controls[i]).Tag=1 then
          begin
            TBtnEditA(Controls[i]).TxtCode := trim(fieldByName(mfield).AsString);
            TBtnEditA(Controls[i]).text:= trim(fieldByName(mfield+'Name').AsString);
          end
          else
          begin
            TBtnEditA(Controls[i]).TxtCode := trim(fieldByName(mfield).AsString);
            TBtnEditA(Controls[i]).text:= trim(fieldByName(mfield).AsString);
          end;        end
        else if Controls[i] is TBtnEditC then
        begin
          if TBtnEditC(Controls[i]).Tag=1 then
          begin
            TBtnEditC(Controls[i]).TxtCode := trim(fieldByName(mfield).AsString);
            TBtnEditC(Controls[i]).text:= trim(fieldByName(mfield+'Name').AsString);
          end
          else
          begin
            TBtnEditC(Controls[i]).TxtCode := trim(fieldByName(mfield).AsString);
            TBtnEditC(Controls[i]).text:= trim(fieldByName(mfield).AsString);
          end
        end
        else if Controls[i] is Tmemo then
        begin
            Tmemo(Controls[i]).Text :=trim(fieldByName(mfield).AsString);
        end
        else if Controls[i] is TDateTimePicker then
        begin
          if IsEmpty  or fieldByName(mfield).IsNull then
            TDateTimePicker(Controls[i]).Date:=strToDate('1990-01-01')
          else
            TDateTimePicker(Controls[i]).Date:=fieldByName(mfield).AsDateTime;
        end
        else if Controls[i] is TcxDateEdit then
        begin
           if not IsEmpty  and not fieldByName(mfield).IsNull then
             TcxDateEdit(Controls[i]).text :=fieldByName(mfield).AsString;
        end
        else if Controls[i] is TcxTimeEdit then
        begin
           if not IsEmpty  and ( fieldByName(mfield).AsString<>'') then
             TcxTimeEdit(Controls[i]).text :=fieldByName(mfield).AsString;
        end
        else if Controls[i] is TcxCurrencyEdit then
        begin
          TcxCurrencyEdit(Controls[i]).Text:=fieldByName(mfield).AsString;
        end;
      end; // end for
    end; // end with
  end;  //end for with

end;
//////////////////////////////////////////
  //函数功能:返回combobox objects 的  idx
//////////////////////////////////////////
function getCombIdx(cobBox:TComboBox;k:integer;vv:string):integer;
var
   i:integer;
   mm:string;
   rv:integer;
begin
  rv:=-1;
  for i:=0 to cobBox.Items.Count-1 do
  begin
      if TA(cobBox.Items.Objects[i])=nil then
      begin
        rv:=cobBox.Items.IndexOf(vv); 
        break;
      end;
      mm:=trim(TA(cobBox.Items.Objects[i]).s);
      if mm=vv then
      begin
        rv:=i;
        break;
      end;
  end;
  result:=rv;
end;
//////////////////////////////////////////////
  //函数：初试化combox中的数据
  //Boxtype:0: 不带编号;1:带;
//////////////////////////////////////////////
procedure InitComBoxBySql(ADOQueryTmp:TADOQuery;
                           cb: TComboBox;sqlstr:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean
                           );
var
  A:TA;
begin
  cb.Items.Clear ;
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.Add(sqlstr);
    Open;
    if isEmpty then
    begin
      application.MessageBox(pChar('未找到：'+showMsg),'',0);
      exit;
    end;

    while not EOF do
    begin
      if Boxtype=0 then
      begin
        cb.Items.Add(trim(fieldByName('Name').AsString));
      end
      else
      begin
        A   :=   TA.Create(Nil);
        A.s:= trim(fieldByName('code').AsString);
        cb.Items.AddObject(trim(fieldByName('Name').AsString),TObject(a));
      end;
      next;
    end;
    if not emptyFlag then
      cb.Items.Add('');

    if emptyFlag and (cb.Items.Count >0) then
       cb.ItemIndex :=0;
  end;
end;
//////////////////////////////////////////////
  //函数：初试化combox中的数据
  //从XC_Code表中取定义数据
  //Boxtype:0; 带编号：1;
//////////////////////////////////////////////
procedure InitComBoxByCode(ADOQueryTmp:TADOQuery;
                           cb: TComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean
                           );
var
  A:TA;
begin
  cb.Items.Clear ;
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.Add('exec P_Get_XC_code');
    sql.Add(quotedStr(trim(flagType)));
    Open;
    if isEmpty then
    begin
      application.MessageBox(pChar('未找到：'+showMsg),'',0);
      exit;
    end;

    while not EOF do
    begin
      if Boxtype=0 then
      begin
        cb.Items.Add(trim(fieldByName('名称').AsString));
      end
      else
      begin
        A   :=   TA.Create(Nil);
        A.s:= trim(fieldByName('编号').AsString);
        cb.Items.AddObject(trim(fieldByName('名称').AsString),TObject(a));
      end;
      next;
    end;
    
    if not emptyFlag then
      cb.Items.Add('');

    if emptyFlag and (cb.Items.Count >0) then
       cb.ItemIndex :=0;
  end;
end;
//////////////////////////////////////////////
  //函数：初试化combox中的数据
  //从XC_Code表中取定义数据
  //Boxtype:0; 带编号：1;
//////////////////////////////////////////////
procedure InitFtComBoxByCode(ADOQueryTmp:TADOQuery;
                           cb: TFtComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean
                           );
var
  A:TA;
begin
  cb.Items.Clear ;
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.Add('exec P_Get_XC_code');
    sql.Add(quotedStr(trim(flagType)));
    Open;
    if isEmpty then
    begin
      application.MessageBox(pChar('基础设置中未找到：'+showMsg),'',0);
      exit;
    end;

    while not EOF do
    begin
      if Boxtype=0 then
      begin
        cb.Items.Add(trim(fieldByName('名称').AsString));
      end
      else
      begin
        //A   :=   TA.Create(Nil);
        //A.s:= trim(fieldByName('编号').AsString);
        //cb.Items.AddObject(trim(fieldByName('名称').AsString),TObject(a));
        cb.AddItem2(trim(fieldByName('名称').AsString),nil,trim(fieldByName('编号').AsString));
      end;
      next;
    end;
    
    if not emptyFlag then
      cb.Items.Add('');

    if emptyFlag and (cb.Items.Count >0) then
       cb.ItemIndex :=0;
  end;
end;
//////////////////////////////////////////////
  //函数：初试化combox中的数据
  //从XC_Code表中取定义数据
  //Boxtype:0; 带编号：1;
  //emptyFlag：是否默认为空
  //isClearOld:清除原有的项目
//////////////////////////////////////////////
procedure InitFtComBoxByCode10(ADOQueryTmp:TADOQuery;
                           cb: TFtComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean;
                           isClearOld:boolean
                           );
var
  A:TA;
begin
  if isClearOld then
    cb.Items.Clear ;
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.Add('exec P_Get_XC_code');
    sql.Add(quotedStr(trim(flagType)));
    Open;
    if isEmpty then
    begin
      application.MessageBox(pChar('基础设置中未找到：'+showMsg),'',0);
      exit;
    end;

    while not EOF do
    begin
      if Boxtype=0 then
      begin
        cb.Items.Add(trim(fieldByName('名称').AsString));
      end
      else
      begin
        //A   :=   TA.Create(Nil);
        //A.s:= trim(fieldByName('编号').AsString);
        //cb.Items.AddObject(trim(fieldByName('名称').AsString),TObject(a));
        cb.AddItem2(trim(fieldByName('名称').AsString),nil,trim(fieldByName('编号').AsString));
      end;
      next;
    end;
    
    if not emptyFlag then
      cb.Items.Add('');

    if emptyFlag and (cb.Items.Count >0) then
       cb.ItemIndex :=0;
  end;
end;
//////////////////////////////////////////////
  //函数：初试化combox中的数据
  //从XC_Code表中取定义数据
  //Boxtype:0; 带编号：1;
  //emptyFlag：是否默认为空
  //mSql:sql语句；
//////////////////////////////////////////////
procedure InitFtComBoxBySql(ADOQueryTmp:TADOQuery;
                           cb: TFtComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean;
                           mSql:string
                           );
var
  A:TA;
begin
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.Add(mSql);
    Open;
    if isEmpty then
    begin
      application.MessageBox(pChar('基础设置中未找到：'+showMsg),'',0);
      exit;
    end;

    while not EOF do
    begin
      if Boxtype=0 then
      begin
        cb.Items.Add(trim(fieldByName('Name').AsString));
      end
      else
      begin
        cb.AddItem2(trim(fieldByName('Name').AsString),nil,trim(fieldByName('code').AsString));
      end;
      next;
    end;
    
    if not emptyFlag then
      cb.Items.Add('');

    if emptyFlag and (cb.Items.Count >0) then
       cb.ItemIndex :=0;
  end;
end;

//////////////////////////////////////////////
  //函数：初试化combox中的数据
  //从XC_unit表中取定义数据
  //Boxtype:1; 带编号：0：不带;
//////////////////////////////////////////////
procedure InitComBoxByUnit(ADOQueryTmp:TADOQuery;
                           cb: TComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean
                           );
var
  A:TA;
begin
  cb.Items.Clear ;
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.Add('exec P_Get_XC_Unit');
    sql.Add(quotedStr(trim(flagType)));
    Open;
    if isEmpty then
    begin
      application.MessageBox(pChar('未找到：'+showMsg),'',0);
      exit;
    end;
    while not EOF do
    begin
      if Boxtype=0 then
      begin
        cb.Items.Add(trim(fieldByName('名称').AsString));
      end
      else
      begin
        A   :=   TA.Create(Nil);
        A.s:= trim(fieldByName('编号').AsString);
        cb.Items.AddObject(trim(fieldByName('名称').AsString),TObject(a));
      end;
      next;
    end;
    if emptyFlag and (cb.Items.Count >0) then
       cb.ItemIndex :=0;
  end;
end;
//////////////////////////////////////////////
  //函数：初试化combox中的数据,从单位分类表中读取
  //从XC_unit表中取定义数据
  //Boxtype:1; 带编号：0：不带;
//////////////////////////////////////////////
procedure InitComBoxByUnit10(ADOQueryTmp:TADOQuery;
                           cb: TComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean
                           );
var
  A:TA;
begin
  cb.Items.Clear ;
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.Add('exec P_Get_XC_Unit');
    sql.Add(quotedStr(trim(flagType)));
    sql.Add(',9');
    Open;
    if isEmpty then
    begin
      application.MessageBox(pChar('未找到：'+showMsg),'',0);
      exit;
    end;
    while not EOF do
    begin
      if Boxtype=0 then
      begin
        cb.Items.Add(trim(fieldByName('名称').AsString));
      end
      else
      begin
        A   :=   TA.Create(Nil);
        A.s:= trim(fieldByName('编号').AsString);
        cb.Items.AddObject(trim(fieldByName('名称').AsString),TObject(a));
      end;
      next;
    end;
    if emptyFlag and (cb.Items.Count >0) then
       cb.ItemIndex :=0;
  end;
end;

//////////////////////////////////////////////
  //函数：初试化combox中的数据
  //从XC_unit表中取定义数据
  //Boxtype:1; 带编号：0：不带;
//////////////////////////////////////////////
procedure InitFtComBoxByUnit(ADOQueryTmp:TADOQuery;
                           cb: TFtComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean
                           );
var
  A:TA;
begin
  cb.Items.Clear ;
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.Add('exec P_Get_XC_Unit');
    sql.Add(quotedStr(trim(flagType)));
    Open;
    if isEmpty then
    begin
      application.MessageBox(pChar('未找到：'+showMsg),'',0);
      exit;
    end;
    while not EOF do
    begin
      if Boxtype=0 then
      begin
        cb.Items.Add(trim(fieldByName('名称').AsString));
      end
      else
      begin
        cb.AddItem2(trim(fieldByName('名称').AsString),nil,trim(fieldByName('编号').AsString));
      end;
      next;
    end;
    if emptyFlag and (cb.Items.Count >0) then
       cb.ItemIndex :=0;
  end;
end;
//////////////////////////////////////////////
  //函数：初试化combox中的数据
  //从XC_unit表中取定义数据  从单位分类表中读取
  //Boxtype:1; 带编号：0：不带;
//////////////////////////////////////////////
procedure InitFtComBoxByUnit10(ADOQueryTmp:TADOQuery;
                           cb: TFtComboBox;FlagType:string;
                           Boxtype:integer;
                           showMsg:string;
                           emptyFlag:Boolean
                           );
var
  A:TA;
begin
  cb.Items.Clear ;
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.Add('exec P_Get_XC_Unit');
    sql.Add(quotedStr(trim(flagType)));
    sql.Add(',9');
    Open;
    if isEmpty then
    begin
      application.MessageBox(pChar('未找到：'+showMsg),'',0);
      exit;
    end;
    while not EOF do
    begin
      if Boxtype=0 then
      begin
        cb.Items.Add(trim(fieldByName('名称').AsString));
      end
      else
      begin
        cb.AddItem2(trim(fieldByName('名称').AsString),nil,trim(fieldByName('编号').AsString));
      end;
      next;
    end;
    if emptyFlag and (cb.Items.Count >0) then
       cb.ItemIndex :=0;
  end;
end;
////////////////////////////////////////////
  //函数功能：创建ClientDataSet
///////////////////////////////////////////
procedure CreateCDS(SADOQry:TADOQuery; mClientDataset:TclientDataSet );
var
  i:integer;
  mfieldName:string;
  mSize:integer;
begin
  mfieldName:='';
  mClientDataset.FieldDefs.Clear;
  with SADOQry do
  begin
    for i:=0 to fieldCount-1 do     //
    begin
      if (Fields[i].DataType=ftString) and (Fields[i].Size=0) then
      begin
        msize:=1;
      end
      else
        msize:=Fields[i].Size;
      mfieldName:=trim(fields[i].FieldName);
      mClientDataset.FieldDefs.Add(mfieldName,
                                    Fields[i].DataType,msize);

    end;
  end;
  mClientDataset.FieldDefs.Add('flag',ftString,1);
  mClientDataset.FieldDefs.Add('index',ftInteger,0);
  mClientDataset.Close;
  mClientDataset.CreateDataSet;
end;
////////////////////////////////////////////
  //函数功能：创建ClientDataSet,增加选择列
///////////////////////////////////////////
procedure CreateCDS20(SADOQry:TADOQuery; mClientDataset:TclientDataSet );
var
  i:integer;
  mfieldName:string;
  mSize:integer;
begin
  mfieldName:='';
  mClientDataset.FieldDefs.Clear;
  SADOQry.DisableControls;
  with SADOQry do
  begin
    for i:=0 to fieldCount-1 do     //
    begin
      if (Fields[i].DataType=ftString) and (Fields[i].Size=0) then
      begin
        msize:=1;
      end
      else
        msize:=Fields[i].Size;
      mfieldName:=trim(fields[i].FieldName);
      mClientDataset.FieldDefs.Add(mfieldName,
                                    Fields[i].DataType,msize);

    end;
  end;
  SADOQry.EnableControls;
  mClientDataset.FieldDefs.Add('flag',ftString,1);
  mClientDataset.FieldDefs.Add('index',ftInteger,0);
  mClientDataset.FieldDefs.Add('sel',ftBoolean,0);
  mClientDataset.FieldDefs.Add('DefNote',ftString,50);
  mClientDataset.Close;
  mClientDataset.CreateDataSet;
end;
/////////////////////////////////////////////////////
  //函数：创建汇总列
  //FooterType:
/////////////////////////////////////////////////////
procedure CreateFooterCol(tv1:TcxGridTableView;
                          tcol:TcxGridDBColumn;
                          mcolName:string;
                          FooterType:string);
begin
    ///创建汇总列
    with tv1.DataController.Summary do
    begin
      BeginUpdate;
      try
        with FooterSummaryItems.Add as TcxGridDBTableSummaryItem do
        begin
          Column := tcol;
          //Column.Styles.Footer:=cxstyle1;
          fieldName:=trim(mcolName);
          if lowerCase(trim(FooterType))='sum' then
            Kind := skSum
          else if lowerCase(trim(FooterType))='max' then
            kind:=skMax
          else if lowerCase(trim(FooterType))='min' then
            kind:=skMin
          else if lowerCase(trim(FooterType))='avg' then
            kind:=skAverage
          else if lowerCase(trim(FooterType))='count' then
            kind:=skCount
          else
            kind:=skNone;
          Position:=spFooter;
        end;

      finally
        EndUpdate;
    end;
  end;
end;
///////////////////////////////////////////////////////////
  //函数功能：动态创建CxGrid列
///////////////////////////////////////////////////////////
procedure CreateCxGridCol(ADOQueryTmp:TADOQuery;
                          tv1:TcxGridDBTableView;
                          HasFooter:Boolean);
var
  ADBColumn: TcxGridDBColumn;
  strField,strCaption:string;
begin
  with ADOQueryTmp do
  begin
    if recordCount>0 then
      tv1.ClearItems;

    while not EOF do
    begin
      if trim(fieldByName('fieldName').AsString)='' then
      begin
        next;
        continue;
      end;
      ADBColumn := tv1.CreateColumn;
      ADBColumn.DataBinding.FieldName :=trim(fieldByName('fieldName').AsString);
      ADBColumn.Caption:=trim(fieldByName('Caption').AsString);
      //if fieldByName('width').AsInteger>5 then
      //   ADBColumn.Width :=fieldByName('width').AsInteger;
      ADBColumn.Visible:=fieldByName('visible').AsBoolean;
      ADBColumn.HeaderAlignmentHorz:=taCenter;
      ADBColumn.Name :='tv1'+trim(fieldByName('fieldName').AsString);

      ADBColumn.Options.Editing :=not fieldByName('ReadOnly').AsBoolean;
      ADBColumn.Options.Focusing :=not fieldByName('ReadOnly').AsBoolean;
      ////////////////
      //创建footer
      if HasFooter then
      begin
        if fieldByName('FooterFlag').AsBoolean then
        begin
          CreateFooterCol(tv1,ADBColumn,ADBColumn.DataBinding.FieldName
                        ,trim(fieldByName('FooterType').AsString));
        end;
      end;

      Next;

    end;
    tv1.OptionsView.Footer:=HasFooter;


    close;
  end;
end;
///////////////////////////////////////////////////////////
  //函数功能：动态创建CxGrid列 (查询模块的cxgrid创建)
///////////////////////////////////////////////////////////
procedure CreateCxGridCol10(ADOQueryTmp:TADOQuery;
                          tv1:TcxGridDBTableView;
                          HasFooter:Boolean);
var
  ADBColumn: TcxGridDBColumn;
  strField,strCaption:string;
begin
  with ADOQueryTmp do
  begin
    if recordCount>0 then
      tv1.ClearItems;

    while not EOF do
    begin
      if trim(fieldByName('fieldName').AsString)='' then
      begin
        next;
        continue;
      end;
      ADBColumn := tv1.CreateColumn;
      ADBColumn.DataBinding.FieldName :=trim(fieldByName('fieldName').AsString);
      ADBColumn.Caption:=trim(fieldByName('Caption').AsString);
      if fieldByName('width').AsInteger>5 then
         ADBColumn.Width :=fieldByName('width').AsInteger;
      ADBColumn.Visible:=fieldByName('visible').AsBoolean;
      ADBColumn.HeaderAlignmentHorz:=taCenter;
      ADBColumn.Name :=tv1.Name+trim(fieldByName('fieldName').AsString);

      ADBColumn.Options.Editing :=not fieldByName('ReadOnly').AsBoolean;
      ADBColumn.Options.Focusing :=not fieldByName('ReadOnly').AsBoolean;
      ////////////////
      //创建footer
      if HasFooter then
      begin
        if fieldByName('FooterFlag').AsBoolean then
        begin
          CreateFooterCol(tv1,ADBColumn,ADBColumn.DataBinding.FieldName
                        ,trim(fieldByName('FooterType').AsString));
        end;
      end;

      Next;

    end;
    tv1.OptionsView.Footer:=HasFooter;


    close;
  end;
end;

////////////////////////////////////////////////////
  //函数：
///////////////////////////////////////////////////
procedure SplitCtrName(ms:string;mvg:string;
                                     var mtable:string;
                                     var mfield:string);
var
  ps:integer;
begin
  if (trim(ms)='') or (trim(mvg)='') then
  begin
    mtable:='';
    mfield:='';
    exit;
  end;
  ps:=Pos(mvg,ms);
  if ps<=0 then
  begin
    mtable:='';
    mfield:=ms;
    exit;
  end;
  mtable:=leftStr(ms,ps-1);
  mfield:=MidStr(ms,ps+1,length(ms));

end;
///////////////////////////////////////////////////////////
  //
//////////////////////////////////////////////////////////
procedure InitCDSData(fromADO:TADOQuery;toCDS:TclientDataSet);
var
  i:integer;
  k:integer;
begin
  fromADO.first;
  K:=1;
  while not fromADO.Eof do
  begin
    with toCDS do
    begin
      Append;
      for i:=0 to fromADO.FieldCount-1 do
      begin
        fields[i].value:=fromADO.Fields[i].Value ;
      end;
      fieldByName('flag').AsString :='1';
      fieldByName('index').value :=k;
      inc(k);
      Post;
    end;
    fromADO.Next;
  end;
  if not toCDS.IsEmpty then
  begin
    toCDS.First ;
  end;
end;
///////////////////////////////////////////////////////////
  //有选择列
//////////////////////////////////////////////////////////
procedure InitCDSData20(fromADO:TADOQuery;toCDS:TclientDataSet);
var
  i:integer;
  k:integer;
begin
  if fromADO.IsEmpty then exit;
  fromADO.DisableControls;
  fromADO.first;
  K:=1;
  try
    toCDS.DisableControls;
    toCDS.Filtered:=false;

    while not fromADO.Eof do
    begin
      with toCDS do
      begin
        Append;
        for i:=0 to fromADO.FieldCount-1 do
        begin
          fields[i].value:=fromADO.Fields[i].Value ;
        end;
        fieldByName('flag').AsString :='1';
        fieldByName('index').value :=k;
        fieldByName('sel').value :=false;
        inc(k);
        Post;
      end;
      fromADO.Next;
    end;
    if not toCDS.IsEmpty then
    begin
      toCDS.First ;
    end;
  finally
    toCDS.EnableControls;
  end;
  fromADO.EnableControls;
end;
///////////////////////////////////////////////////
  //函数功能：从编码器中获取最大的编号
///////////////////////////////////////////////////
function GetMaxNo(ADOStoredProc1:TADOStoredProc;
                    var mMaxNo:string;
                    mFlag:string):Boolean;
begin
  try
    with ADOStoredProc1 do
    begin
      Close;
      Parameters.ParamByName('@sysclass').Value :=mFlag;
      Prepared;
      ExecProc;
      if (trim(Parameters.ParamByName('@theCoder').Value) = 'ERROR')
        or (Trim(Parameters.ParamByName('@theCoder').Value) = '') then
      begin
        application.MessageBox(Pchar('无法生成自动编号('+mflag+')'),'提示信息',MB_ICONINFORMATION);
        Close;
        result:=false;
        Exit;
      end;
      mMaxNo := trim(Parameters.ParamByName('@theCoder').Value);
      Close;
    end;
    result:=true;
  Except
    result:=false;
  end;
end;
///////////////////////////////////////////////////
  //函数功能：从新的编码器中获取最大的编号
  //hasTp:是否带前缀；mTable:表名
  //lsLen:流水号长度
///////////////////////////////////////////////////
function GetMaxNo20(ADOQueryTmp:TADOQuery;
                    var mMaxNo:string;
                    mTable:string;
                    mFlag:string;
                    hasTp:integer;
                    lsLen:integer=3):Boolean;
begin
  try
    with ADOQueryTmp do
    begin
      Close;
      sql.Clear ;
      sql.Add('exec XC_TableDH10 ');
      sql.Add(intTostr(hasTp));
      sql.Add(','+quotedStr(mTable));
      sql.Add(','+quotedStr(mFlag));
      sql.Add(','+intTostr(LsLen));
      //ShowMessage(SQL.Text);
      Open;

      if RecordCount>0 then
      begin
        mMaxNo:=trim(fieldByName('DepotShow').AsString) ;
        if mMaxNo<>'' then
          result:=true
        else
          Result:=false;
      end
      else
      begin
        result:=false;
      end;

    end;
    if not Result then
      application.MessageBox(Pchar('无法生成自动编号('+mflag+')'),'提示信息',MB_ICONINFORMATION);

  Except
    result:=false;
    application.MessageBox(Pchar('无法生成自动编号('+mflag+')'),'提示信息',MB_ICONINFORMATION);

  end;
end;
///////////////////////////////////////////////////
  //函数功能：从新的编码器中获取最大的编号
  //hasTp:是否带前缀；mTable:表名
  //lsLen:流水号长度
  //增加了编号格式  --0:yyyy-MM-dd,1:yy-mm
///////////////////////////////////////////////////
function GetMaxGangNo(ADOQueryTmp:TADOQuery;
                    var mMaxNo:string;
                    mTable:string;
                    mFlag:string;
                    hasTp:integer;
                    lsLen:integer=3;
                    BhType:integer=0):Boolean;
begin
  try
    with ADOQueryTmp do
    begin
      Close;
      sql.Clear ;
      sql.Add('exec P_GET_GangNo');
      sql.Add(intTostr(hasTp));
      sql.Add(','+quotedStr(mTable));
      sql.Add(','+quotedStr(mFlag));
      sql.Add(','+intTostr(LsLen));
      sql.Add(','+intTostr(BhType));
      Open;

      if RecordCount>0 then
      begin
        mMaxNo:=trim(fieldByName('DepotShow').AsString) ;
        if mMaxNo<>'' then
          result:=true
        else
          Result:=false;
      end
      else
      begin
        result:=false;
      end;

    end;
    if not Result then
      application.MessageBox(Pchar('无法生成自动编号('+mflag+')'),'提示信息',MB_ICONINFORMATION);

  Except
    result:=false;
    application.MessageBox(Pchar('无法生成自动编号('+mflag+')'),'提示信息',MB_ICONINFORMATION);

  end;
end;

///////////////////////////////////////////////////
  //获取最大的计划单号
  //函数功能：从新的编码器中获取最大的编号
  //hasTp:是否带前缀；mTable:表名
  //lsLen:流水号长度
  //增加了编号格式  --0:yyyy-MM-dd,1:yy-mm
///////////////////////////////////////////////////
function GetMaxPlanNo(ADOQueryTmp:TADOQuery;
                    var mMaxNo:string;
                    mTable:string;
                    mFlag:string;
                    hasTp:integer;
                    lsLen:integer=3;
                    BhType:integer=0):Boolean;
begin
  try
    with ADOQueryTmp do
    begin
      Close;
      sql.Clear ;
      sql.Add('exec P_GET_MaxPlanNo');
      sql.Add(intTostr(hasTp));
      sql.Add(','+quotedStr(mTable));
      sql.Add(','+quotedStr(mFlag));
      sql.Add(','+intTostr(LsLen));
      sql.Add(','+intTostr(BhType));
      Open;

      if RecordCount>0 then
      begin
        mMaxNo:=trim(fieldByName('DepotShow').AsString) ;
        if mMaxNo<>'' then
          result:=true
        else
          Result:=false;
      end
      else
      begin
        result:=false;
      end;

    end;
    if not Result then
      application.MessageBox(Pchar('无法生成自动编号('+mflag+')'),'提示信息',MB_ICONINFORMATION);

  Except
    result:=false;
    application.MessageBox(Pchar('无法生成自动编号('+mflag+')'),'提示信息',MB_ICONINFORMATION);

  end;
end;

//////////////////////////////////////////////
  //函数功能：保存cxgrid列到数据库中
//////////////////////////////////////////////
procedure SaveTocxGridColSet(ADOQueryCmd:TADOQuery;winCode:string;gridName:string;cxGrid:TcxGridTableView);
var
  mgridName:string;
  mCaption:string;
  mfieldName:string;
  i:integer;
begin
  mgridName:=trim(cxGrid.Name) ;
  
  with ADOQueryCmd do
  begin
    close;
    sql.Clear ;
    sql.Add('select * from XD_GridCustomSet');
    sql.Add('where winCode='''+winCode+'''');
    sql.Add('and GridName='''+mgridName+'''');
    Open;
    for i:=0 to cxGrid.ColumnCount-1 do
    begin
       //mCaption:=trim(cxGrid.Columns[i].Caption);
       mfieldName:=TcxGridDBColumn(cxGrid.Columns[i]).DataBinding.FieldName;

       filtered:=false;
       filter:='fieldName='''+mfieldName+'''';
       filtered:=true;

       if RecordCount>0 then
          Edit
       else
       begin
          Append;
          fieldByName('winCode').value:=winCode;
          fieldByName('GridName').value:=mgridName;
       end;
       fieldByName('GridIndex').value:=1;
       fieldByName('FieldName').value:=trim(TcxGridDBColumn(cxGrid.Columns[i]).DataBinding.FieldName);
       fieldByName('Caption').value:=mCaption;
       fieldByName('ColType').value:=trim(TcxGridDBColumn(cxGrid.Columns[i]).DataBinding.ValueType);
       fieldByName('Visible').value:=1;
       fieldByName('Width').value:=cxGrid.Columns[i].Width;
       fieldByName('IsOrder').value:=1;
       fieldByName('FieldOrder').value:=intTostr(i);
       fieldByName('Valid').value:=1;
       Post;
    end;
  end;
end;

//////////////////////////////////////////////
  //函数功能：保存查询模块cxgrid列到数据库中
//////////////////////////////////////////////
procedure SaveToQrycxGridColSet(ADOQueryCmd:TADOQuery;winCode:string;cxGrid:TcxGridTableView);
var
  mgridName:string;
  mCaption:string;
  mfieldName:string;
  i:integer;
begin
  mgridName:=trim(cxGrid.Name) ;

  try
    ADOQueryCmd.Connection.BeginTrans ;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear ;
      sql.Add('select * from XD_QryGridCustomSet');
      sql.Add('where winCode='''+winCode+'''');
      sql.Add('and GridName='''+mgridName+'''');
      Open;
      for i:=0 to cxGrid.ColumnCount-1 do
      begin
        mfieldName:=TcxGridDBColumn(cxGrid.Columns[i]).DataBinding.FieldName;
        //mCaption:=trim(cxGrid.Columns[i].DataBinding);
        filtered:=false;
        filter:='fieldName='''+mfieldName+'''';
        filtered:=true;

        if RecordCount>0 then
          Edit
        else
        begin
          Append;
          fieldByName('winCode').value:=winCode;
          fieldByName('GridName').value:=mgridName;
        end;
        fieldByName('GridIndex').value:=1;
        fieldByName('FieldName').value:=trim(TcxGridDBColumn(cxGrid.Columns[i]).DataBinding.FieldName);
        fieldByName('Caption').value:=mCaption;
        fieldByName('ColType').value:=trim(TcxGridDBColumn(cxGrid.Columns[i]).DataBinding.ValueType);
        fieldByName('Visible').value:=1;
        fieldByName('Width').value:=cxGrid.Columns[i].Width;
        fieldByName('IsOrder').value:=1;
        fieldByName('FieldOrder').value:=intTostr(i);
        fieldByName('Valid').value:=1;
        post;
      end;
    end;
      ADOQueryCmd.Connection.CommitTrans ;
  except
    ADOQueryCmd.Connection.RollbackTrans ;
  end;

end;

//////////////////////////////////////////////
  //函数功能：保存dbgrid列到数据库中
//////////////////////////////////////////////
procedure SaveTodbGridColSet(ADOQueryCmd:TADOQuery;winCode:string;gridName:string;dbGrid:TDbgrid);
var
  mgridName:string;
  mFieldName:string;
  i:integer;
begin
  mgridName:=trim(dbGrid.Name) ;

  with ADOQueryCmd do
  begin
    close;
    sql.Clear ;
    sql.Add('select * from XD_GridCustomSet');
    sql.Add('where winCode='''+winCode+'''');
    sql.Add('and GridName='''+mgridName+'''');
    Open;
    for i:=0 to dbGrid.Columns.Count -1 do
    begin
       mFieldName:=trim(dbGrid.Columns[i].FieldName);
       filtered:=false;
       filter:='fieldName='''+mFieldName+'''';
       filtered:=true;

       if RecordCount>0 then
          Edit
       else
       begin
          Append;
          fieldByName('winCode').value:=winCode;
          fieldByName('GridName').value:=mgridName;
       end;
       fieldByName('GridIndex').value:=1;
       fieldByName('FieldName').value:=mFieldName;
       fieldByName('Caption').value:=trim(dbGrid.Columns[i].Title.Caption) ;
       fieldByName('ColType').value:=dbGrid.Columns[i].Field.DataType;
       fieldByName('Visible').value:=dbGrid.Columns[i].Visible ;
       fieldByName('Width').value:=dbGrid.Columns[i].Width;
       fieldByName('IsOrder').value:=1;
       fieldByName('FieldOrder').value:=intTostr(i);
       fieldByName('Valid').value:=1;
       Post;
    end;
  end;
end;
//////////////////////////////////////////////
  //函数功能：保存dbgrid列到数据库中
//////////////////////////////////////////////
procedure SaveToQrydbGridColSet(ADOQueryCmd:TADOQuery;winCode:string;dbGrid:TDbgrid);
var
  mgridName:string;
  mFieldName:string;
  i:integer;
begin
  mgridName:=trim(dbGrid.Name) ;

  with ADOQueryCmd do
  begin
    close;
    sql.Clear ;
    sql.Add('select * from XD_QryGridCustomSet');
    sql.Add('where winCode='''+winCode+'''');
    sql.Add('and GridName='''+mgridName+'''');
    Open;
    for i:=0 to dbGrid.Columns.Count -1 do
    begin
       mFieldName:=trim(dbGrid.Columns[i].FieldName);
       filtered:=false;
       filter:='fieldName='''+mFieldName+'''';
       filtered:=true;

       if RecordCount>0 then
          Edit
       else
       begin
          Append;
          fieldByName('winCode').value:=winCode;
          fieldByName('GridName').value:=mgridName;
       end;
       fieldByName('GridIndex').value:=1;
       fieldByName('FieldName').value:=mFieldName;
       fieldByName('Caption').value:=trim(dbGrid.Columns[i].Title.Caption) ;
       fieldByName('ColType').value:=dbGrid.Columns[i].Field.DataType;
       fieldByName('Visible').value:=dbGrid.Columns[i].Visible ;
       fieldByName('Width').value:=dbGrid.Columns[i].Width;
       fieldByName('IsOrder').value:=1;
       fieldByName('FieldOrder').value:=intTostr(i);
       fieldByName('Valid').value:=1;
       Post;
    end;
  end;
end;

//////////////////////////////////////////////////
  //
///////////////////////////////////////////////////
procedure GetServerTime(ADOQueryTmp:TADOQuery);
begin
  //服务器日期
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select getDate()as dt');
    open;
  end;
  gServerDate:=ADOQueryTmp.fieldByName('dt').AsDatetime;
end;
//////////////////////////////////////////////////
  //功能：返回服务器日期
///////////////////////////////////////////////////
function GetServerTime10(ADOQueryTmp:TADOQuery):TdateTime;
begin
  //服务器日期
  try
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select getDate()as dt');
      open;
      result:=fieldByName('dt').AsDatetime;
      close;
    end;
  except
    application.MessageBox('获取当前日期时间发生错误!','提示信息',0); 
  end;

end;
///////////////////////////////////////////////////////////////////
  //函数功能：利用cxGrid自带的功能导出到excel中
//////////////////////////////////////////////////////////////////
procedure  TcxGridToExcel(mfileName:string;gridName:TcxGrid);
var
    saveDialog:TSaveDialog;
begin
    try
        saveDialog:=TSaveDialog.Create(nil);
        saveDialog.Filter:='xls(*.xls)|*.xls|全部(*.*)|*.*';
        saveDialog.Options:=[ofOverwritePrompt];
        saveDialog.FileName:=mfileName;
        if saveDialog.Execute then
            if Assigned(gridName) then
            begin
                try

                    ExportGrid4ToExcel(saveDialog.FileName,gridName);
                except
                    application.MessageBox('创建失败,源文件可能处于编辑状态!','提示信息',0);
                    exit;
                end;
                application.MessageBox('成功导出!','提示信息',0);
            end
            else
               application.MessageBox('导出失败!','提示信息',0);
    finally
        saveDialog.Free;
    end;
end;
//////////////////////////////////////////////////////////////////
  //函数功能：根据编号获取名称
//////////////////////////////////////////////////////////////////
function GetNameByBh(ADOQueryTmp:TADOQuery;mBhType:string;mValue:string;var mCode:string;var mName:string):Boolean;
begin
  try
    with ADOQuerytmp do
    begin
      close;
      sql.Clear ;
      sql.Add('exec P_get_NameByBh');
      sql.Add(quotedStr(mBhType));
      sql.Add(','+quotedStr(mValue));
      Open;

      if RecordCount>0 then
      begin
        mName:=trim(fieldByName('Name').AsString);
        mCode:=trim(fieldByName('code').AsString);
      end;
      close;
    end;
    Result:=true;
  except
    Result:=false;
  end;
end;
//////////////////////////////////////////////////////////////////
  //函数功能：根据编号获取名称
//////////////////////////////////////////////////////////////////
function GetNameByBh10(ADOQueryTmp:TADOQuery;mBhType:string;mValue:string):Boolean;
begin
  try
    with ADOQuerytmp do
    begin
      close;
      sql.Clear ;
      sql.Add('exec P_get_NameByBh');
      sql.Add(quotedStr(mBhType));
      sql.Add(','+quotedStr(mValue));
      Open;
    end;
    Result:=true;
  except
    Result:=false;
  end;
end;
//////////////////////////////////////////////////////////////
  //
//////////////////////////////////////////////////////////////
procedure InitCxGridComboBoxByCode(ADOQueryTmp:TADOQuery;c3:TcxGriddbColumn;FlagType:string);
var
  A:TA;
begin
 {
  (c3.Properties as TcxComboBoxProperties).Items.Clear ;
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.Add(' select * from XC_Code  '+
            ' where Flag='''+trim(FlagType)+''' '+
            ' order by orderno ');
    Open;
    if isEmpty then
    begin
      exit;
    end;
    while not EOF do
    begin
      A   :=   TA.Create(Nil);
      A.s:= trim(fieldByName('Code').AsString);
      (c3.Properties as TcxComboBoxProperties).Items.AddObject(trim(fieldByName('name').AsString),TObject(a));
      //fieldByName(mField).value:=TA(c3.Properties as TcxComboBoxProperties).Items.Objects[idx]).s;
      next;
    end;
  end;
  }
end;
/////////////////////////////////////////////////////////////
  //获取窗口DLL
/////////////////////////////////////////////////////////////
procedure GetDll(DllName:string;StrFormid:integer;Str:string);
type
  TMyFunc = function(App: Tapplication; FormH: hwnd; FormID: Integer; Language: integer; WinStyle: integer;
                     UserID: String; UserName: String; dataBase:string): HWND; stdcall;
var
  Tf: TMyFunc;
  Tp: TFarProc;
  Th:Thandle;
begin
  Th := LoadLibrary(Pchar(trim(DllName)));
  if Th > 0 then
  begin
    try
      Tp := GetProcAddress(Th, 'GetForm');
      if Tp <> nil then
      begin
        Tf := TMyFunc(Tp);
        newh:=Tf(Application,0,StrFormid,0,0,
          gUserID,gUserName,trim(Str));
      end
      else
      begin

      end;
    finally
    //释放窗体FreeLibrary(Th);
    end;
  end
  else
  begin
    application.MessageBox(Pchar('找不到 '+trim(DllName)+' 文件！'),'提示');
  end;
end;
////////////////////////////////////////////////
  //功能：删除同名不同功能title的窗口
  //formId:备用
////////////////////////////////////////////////
function KillNoFuncWinForm(formName:string;formTitle:string;formId:integer=0):boolean;
var
  bFound:boolean;
  i:integer;
begin
  bFound := FALSE;
  try
    for i := (application.MainForm.MDIChildCount - 1) downto 0 do
    begin
      //已存在
      if (application.MainForm.MDIChildren[i].Name =formName) and
         (application.MainForm.MDIChildren[i].Caption = formTitle) then
      begin
        bFound:=true;
        application.MainForm.MDIChildren[i].BringToFront;
        break;
      end;
      //同名不同title
      if (application.MainForm.MDIChildren[i].Name =formName) and
         (application.MainForm.MDIChildren[i].Caption <> formTitle) then
      begin
        application.MainForm.MDIChildren[i].Free;
        //application.MainForm.MDIChildren[i]:=nil;
        break;
      end;
    end;
  finally
    result:=bFound;
  end;
end;
////////////////////////////////////////////////////////////
  //公用过滤函数
////////////////////////////////////////////////////////////
procedure Dofilter(ADOQry:TADOQuery;FilterStr:string);
begin
  try
    ADOQry.DisableControls;
    with ADOQry do
    begin
      if Trim(FilterStr)='' then
      begin
        Filtered:=False;
      end else
      begin
        Filtered:=False;
        Filter:=FilterStr;
        Filtered:=True;
      end;
    end;
  finally
    ADOQry.EnableControls;
  end;
end;
////////////////////////////////////////////////////////////
  //公用过滤函数
////////////////////////////////////////////////////////////
procedure DofilterNew(ADOQry:TADOQuery;FilterStr:string);
begin
  try
    ADOQry.DisableControls;
    with ADOQry do
    begin
      if Trim(FilterStr)='' then
      begin
        Filtered:=False;
      end else
      begin
        Filtered:=False;
        Filter:=FilterStr;
        Filtered:=True;
      end;
    end;
  finally
    ADOQry.EnableControls;
  end;
end;
procedure DofilterCDS(Cds_Sub:TClientDataSet;FilterStr:string);
begin
  try
    Cds_Sub.DisableControls;
    with Cds_Sub do
    begin
      if Trim(FilterStr)='' then
      begin
        Filtered:=False;
      end else
      begin
        Filtered:=False;
        Filter:=FilterStr;
        Filtered:=True;
      end;
    end;
  finally
    Cds_Sub.EnableControls;
  end;
end;
///////////////////////////////////////////////////////
  // //获取窗口DLL 新接口 getForm10
///////////////////////////////////////////////////////
procedure GetDll10(DllName:string;sFormid:integer;sDatabase:string;sTitle:string;sDef1:string;sDef2:string);
type
  TMyFunc = function(App:Tapplication; FormH:hwnd; FormID:integer;
                 Language: integer; WinStyle:integer;
                 UID: String; UName: String; dataBase:String;
                 Title:String;Def1: String;Def2: String):HWND; stdcall;
var
  Tf: TMyFunc;
  Tp: TFarProc;
  Th:Thandle;
begin
  Th := LoadLibrary(Pchar(trim(DllName)));
  if Th > 0 then
  begin
    try
      Tp := GetProcAddress(Th, 'GetForm10');
      if Tp <> nil then
      begin
        Tf := TMyFunc(Tp);
        newh:=Tf(Application,0,SFormid,0,0,
          gUserID,gUserName,trim(sdatabase),trim(sTitle),trim(sDef1),trim(sDef2));
      end
      else
      begin

      end;
    finally
    //释放窗体FreeLibrary(Th);
    end;
  end
  else
  begin
    application.MessageBox(Pchar('找不到 '+trim(DllName)+' 文件！'),'提示');
  end;
end;
function GetNoDateMaxNo(ADOQueryTmp:TADOQuery;
                    var mMaxNo:string;
                    mTable:string;
                    mFlag:string;
                    lsLen:integer=3):Boolean;
begin
  try
    with ADOQueryTmp do
    begin
      Close;
      sql.Clear ;
      sql.Add('exec XC_TableNoDateDH');
      sql.Add(quotedStr(mTable));
      sql.Add(','+quotedStr(mFlag));
      sql.Add(','+intTostr(LsLen));
      Open;

      if RecordCount>0 then
      begin
        mMaxNo:=trim(fieldByName('DepotShow').AsString) ;
        if mMaxNo<>'' then
          result:=true
        else
          Result:=false;
      end
      else
      begin
        result:=false;
      end;

    end;
    if not Result then
      application.MessageBox(Pchar('无法生成自动编号('+mflag+')'),'提示信息',MB_ICONINFORMATION);

  Except
    result:=false;
    application.MessageBox(Pchar('无法生成自动编号('+mflag+')'),'提示信息',MB_ICONINFORMATION);

  end;
end;

end.

