unit U_YGYPZZChkList_SCHB;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter,
  RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport, RM_e_Xls,
  Menus, cxButtonEdit, cxDropDownEdit, cxPC, ActnMan, ActnColorMaps,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP,IniFiles;

type
  TfrmYGYPZZChkList_SCHB = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    TBView: TToolButton;
    TBADD: TToolButton;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    N1: TMenuItem;
    TUpdate: TToolButton;
    TDel: TToolButton;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    ADOQuery1: TADOQuery;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxStyle2: TcxStyle;
    ADOQuery2: TADOQuery;
    Panel1: TPanel;
    Label4: TLabel;
    orderNo: TEdit;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    SaveDialog1: TSaveDialog;
    Label3: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    v1filltime: TcxGridDBColumn;
    v1ZDNote: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    TFB: TToolButton;
    TCXFB: TToolButton;
    v1OrderNo: TcxGridDBColumn;
    v1filler: TcxGridDBColumn;
    v1GSBiaoTi: TcxGridDBColumn;
    v1BZNote: TcxGridDBColumn;
    Label1: TLabel;
    filler: TEdit;
    Label5: TLabel;
    GSBiaoTi: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure TBViewClick(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure TBADDClick(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TUpdateClick(Sender: TObject);
    procedure TDelClick(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure TFBClick(Sender: TObject);
    procedure TCXFBClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure orderNoChange(Sender: TObject);
  private
    canshu1:string;
    DQdate:TDateTime;
    FDEPT,FDPName: string;
    lstPat: TStringList;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure setstatus();
    procedure SaveData();
    procedure ReadINIFile10();
    { Private declarations }
  public
    FFInt,FCloth:Integer;
    { Public declarations }
  end;

var
  frmYGYPZZChkList_SCHB: TfrmYGYPZZChkList_SCHB;

implementation
uses
  U_DataLink,U_RTFun,U_ZDYHelp, U_YGYPInPut,
  U_ZDYHelpSel,U_YGYPInPut_YGFJDJ,U_YGYPInPut_SCHB;  

{$R *.dfm}

procedure TfrmYGYPZZChkList_SCHB.setstatus();
begin
  TBADD.Visible:=False;
  TUpdate.Visible:=False;
  TDel.Visible:=False;
  TFB.Visible:=False;
  TCXFB.Visible:=False;
  case cxTabControl1.TabIndex of
    0:begin
        TBADD.Visible:=true;
        TUpdate.Visible:=true;
        TDel.Visible:=true;
        TFB.Visible:=true;
      end;
    1:begin
        TCXFB.Visible:=true;
      end;
    2:begin

      end;
    3:begin
      end;
  end;
end;

procedure TfrmYGYPZZChkList_SCHB.SaveData();
var
  maxno:String;
begin
  {if GetLSNo(ADOQueryTemp,maxno,'ZD','OA_YG_GSGZZD',4,1)=False then
  begin
    Application.MessageBox('取最大号失败!','提示',0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from OA_YG_GSGZZD where ZDID='''+Trim(maxno)+'''');
    Open;
    if isempty then
    begin
      append;
      fieldbyname('filler').AsString:=trim(DName);
      fieldbyname('filltime').AsDateTime:=SGetServerDate(ADOQuery2);
      fieldbyname('ZDID').Value:=trim(maxno);
      fieldbyname('DeptType').Value:=trim(FDPName);
      fieldbyname('GSBiaoTi').Value:=trim(Order_Main.fieldbyname('GSBiaoTi').AsString);
      post;
    end;
  end; }
end;

procedure TfrmYGYPZZChkList_SCHB.FormDestroy(Sender: TObject);
begin
  frmYGYPZZChkList_SCHB:=nil;
end;

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

procedure TfrmYGYPZZChkList_SCHB.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
end;

procedure TfrmYGYPZZChkList_SCHB.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('公司内部列表',Tv1,'OA管理');
end;

procedure TfrmYGYPZZChkList_SCHB.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select * ');
      sql.Add(' from OA_YG_SCHB ');
      sql.Add(' where 1=1');
      sql.add(' and filltime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
      sql.Add(' and filltime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      if trim(CanShu1)<>'查询' then
      begin
        if cxTabControl1.TabIndex=0 then
        sql.add(' and (isnull(FBStatus,'''')='''' or isnull(FBStatus,'''')=''不通过'')');
        if cxTabControl1.TabIndex=1 then
        sql.add(' and (isnull(FBStatus,'''')=''已发布'' or isnull(FBStatus,'''')=''申请通过'')');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYGYPZZChkList_SCHB.InitForm();
begin
  ReadCxGrid('公司内部列表',Tv1,'OA管理');
  setstatus();
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  BegDate.Date:=EndDate.Date-7;
  if trim(CanShu1)='查询' then
  begin
    TBADD.Visible:=False;
    TUpdate.Visible:=false;
    TDel.Visible:=False;
    cxTabControl1.Visible:=False;
    TFB.Visible:=False;
    TCXFB.Visible:=False;
  end
  else
  begin
    TBADD.Visible:=true;
    TUpdate.Visible:=true;
    TDel.Visible:=true;
    cxTabControl1.Visible:=true;
    TFB.Visible:=true;
  end;
  InitGrid();
end;

procedure TfrmYGYPZZChkList_SCHB.TBFindClick(Sender: TObject);
begin
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

function TfrmYGYPZZChkList_SCHB.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Delete OA_YG_DangAn where YGId='''+Trim(Order_Main.fieldbyname('YGId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Delete OA_YG_DangAn_Other  where YGId='''+Trim(Order_Main.fieldbyname('YGId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Delete TP_File  where WBId='''+Trim(Order_Main.fieldbyname('YGId').AsString)+'''');
      sql.Add(' and TFType=''YG'' ');
      ExecSQL;
    end;

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

procedure TfrmYGYPZZChkList_SCHB.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel('公司规章制度列表',cxGrid1);
end;

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

procedure TfrmYGYPZZChkList_SCHB.FormShow(Sender: TObject);
begin
  InitForm();
end;

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

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

procedure TfrmYGYPZZChkList_SCHB.TBViewClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  frmYGYPInPut_SCHB:=TfrmYGYPInPut_SCHB.create(self);
  with frmYGYPInPut_SCHB do
  begin
    ToolBar1.Visible:=False;
    FCLID:=trim(Order_Main.fieldbyname('ZDID').AsString);
    if showModal=1 then
    begin
      
    end;
    free;
  end;
end;

procedure TfrmYGYPZZChkList_SCHB.CustomerNoNameChange(Sender: TObject);
begin
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmYGYPZZChkList_SCHB.TBADDClick(Sender: TObject);
begin
  frmYGYPInPut_SCHB:=TfrmYGYPInPut_SCHB.create(self);
  with frmYGYPInPut_SCHB do
  begin
    FCLID:='';
    if showModal=1 then
    begin

    end;
    free;
  end;
  initGrid();
end;

procedure TfrmYGYPZZChkList_SCHB.Tv1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  TBView.Click;
end;

procedure TfrmYGYPZZChkList_SCHB.TUpdateClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if trim(Order_Main.fieldbyname('filler').AsString)<>trim(DName) then
  begin
    application.MessageBox('不能修改他人数据','提示');
    exit;
  end;
  frmYGYPInPut_SCHB:=TfrmYGYPInPut_SCHB.create(self);
  with frmYGYPInPut_SCHB do
  begin
    FCLID:=trim(Order_Main.fieldbyname('ZDID').AsString);
    if showModal=1 then
    begin

    end;
    free;
  end;
  initGrid();
end;

procedure TfrmYGYPZZChkList_SCHB.TDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if trim(Order_Main.fieldbyname('filler').AsString)<>trim(DName) then
  begin
    application.MessageBox('不能删除他人数据','提示');
    exit;
  end;
  if application.MessageBox('确定要删除吗','提示',1)=2 then exit;
  with ADOQueryMain do
  begin
    close;
    sql.Clear;
    sql.Add('delete from OA_YG_SCHB where ZDID='''+trim(Order_Main.fieldbyname('ZDID').AsString)+'''');
    execsql;
  end;
  initGrid();
end;


procedure TfrmYGYPZZChkList_SCHB.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Main,True);
end;

procedure TfrmYGYPZZChkList_SCHB.N1Click(Sender: TObject);
begin
  SelOKNo(Order_Main,False);
end;

procedure TfrmYGYPZZChkList_SCHB.ReadINIFile10();
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 TfrmYGYPZZChkList_SCHB.TFBClick(Sender: TObject);
var maxno: string;
begin
  if Order_Main.IsEmpty then exit;
  frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
  with frmZDYHelpSel do
  begin
    flag:='SPR';
    flagname:='阅读人';
    MainType:=Trim(DName);
    if ShowModal=1 then
    begin
      with ClientDataSet1 do
      begin
        first;
        while not Eof do
        begin
          if fieldbyname('Ssel').AsBoolean=True then
          begin
            with ADOQueryTemp do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from SY_User where UserName='''+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
              Open;
            end;
            if ADOQueryTemp.IsEmpty then
            begin
              Application.MessageBox('阅读人定义错误!','提示',0);
              Exit;
            end;
            with ADOQueryTemp do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from OA_Chk where MainId='''+Trim(Order_Main.fieldbyname('ZDID').AsString)+'''');
              sql.Add(' and isnull(OAType,'''')=''汇报管理'' ');
              sql.Add(' and isnull(Chker,'''')='''+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
              Open;
            end;
            if ADOQueryTemp.IsEmpty then
            begin
              if GetLSNo(ADOQueryCmd,maxno,'FB','OA_Chk',4,1)=False then
              begin
                Application.MessageBox('取最大号失败!','提示',0);
                Exit;
              end;
              with ADOQueryCmd do
              begin
                Close;
                sql.Clear;
                sql.Add('select * from OA_Chk where 1=2 ');
                Open;
                append;
                FieldByName('CKID').Value:=Trim(maxno);
                FieldByName('Mainid').Value:=Trim(Order_Main.fieldbyname('ZDID').AsString);
                FieldByName('OAType').value:='汇报管理';
                FieldByName('Chker').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
                Post;
              end;
            end;
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('update OA_YG_SCHB set FBStatus=''已发布'' ');
              sql.Add('where ZDID='''+trim(Order_Main.fieldbyname('ZDID').asstring)+''' ');
              ExecSQL;
            end;
          end;
          Next;
        end;
      end;
    end;
    Free;
  end;
  InitGrid();
end;

procedure TfrmYGYPZZChkList_SCHB.TCXFBClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Application.MessageBox('确定要执行撤销操作吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete from OA_Chk ');
    sql.Add(' where Mainid='''+Order_Main.fieldbyname('ZDID').AsString+'''');
    sql.Add(' and isnull(OAType,'''')=''汇报管理'' ');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update OA_YG_SCHB Set FBStatus='''' ');
    sql.Add(' where ZDID='''+trim(Order_Main.fieldbyname('ZDID').AsString)+'''');
    ExecSQL;
  end;
  initGrid();
end;

procedure TfrmYGYPZZChkList_SCHB.cxTabControl1Change(Sender: TObject);
begin
  setStatus();
  initGrid();
end;

procedure TfrmYGYPZZChkList_SCHB.orderNoChange(Sender: TObject);
begin
  TBFind.Click;
end;

end.
