unit U_ProcessDesign;

interface

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

type
  TfrmProcessDesign = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_GX: TDataSource;
    CDS_GX: TClientDataSet;
    YGCode: TEdit;
    EndDate: TDateTimePicker;
    Label4: TLabel;
    BegDate: TDateTimePicker;
    Label1: TLabel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridLevel2: TcxGridLevel;
    cxDBTreeList1: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    ADOConnection1: TADOConnection;
    ADOQueryTree: TADOQuery;
    DataSource1: TDataSource;
    V1Column1: TcxGridDBColumn;
    V1Column2: TcxGridDBColumn;
    V1Column3: TcxGridDBColumn;
    V1Column4: TcxGridDBColumn;
    V1Column5: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N3: TMenuItem;
    X1: TMenuItem;
    N1: TMenuItem;
    Label2: TLabel;
    YGName: TEdit;
    Label5: TLabel;
    BZName: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure C_CodeNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cxDBTreeList1Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure X1Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
  private

    { Private declarations }
    canshu1:string;
    procedure InitGrid();
    procedure InitTree();
  public
    fkhType:string;
    Fmanage:string;
  end;

var
  frmProcessDesign: TfrmProcessDesign;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp,U_GXAdd ,U_BZAdd,U_YGAdd
  ,U_GXGL_List,U_YGList,U_YGCL_BZ;

{$R *.dfm}

procedure TfrmProcessDesign.InitTree();
begin
  with ADOQueryTree do
  begin
    Close;
    SQL.Clear;
    SQL.Add('exec P_View_ProcessRelationshipTree ');
    Open;
  end;
  cxDBTreeList1.Items[0].Expand(false);
end;
procedure TfrmProcessDesign.InitGrid();
begin
   try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add(' EXEC P_View_ProcessRelationship ');
      sql.Add(' @CPID='+quotedstr(Trim(ADOQueryTree.fieldbyname('CPID').AsString)));
      sql.Add(' ,@CPLEVEL='+quotedstr(Trim(ADOQueryTree.fieldbyname('CPLevel').AsString)));
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_GX);
    SInitCDSData20(ADOQueryMain,CDS_GX);
  finally
    ADOQueryMain.EnableControls;
  end; 
end;


procedure TfrmProcessDesign.FormDestroy(Sender: TObject);
begin
  frmProcessDesign:=nil;
end;

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

procedure TfrmProcessDesign.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption,Tv1,'员工工序');
  Close;
end;
procedure TfrmProcessDesign.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption,Tv1,'员工工序');
   InitTree();
  InitGrid();
end;

procedure TfrmProcessDesign.TBRafreshClick(Sender: TObject);
begin
 InitTree();
  Initgrid();
end;

procedure TfrmProcessDesign.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_GX);
    SInitCDSData20(ADOQueryMain,CDS_GX);
  end;
end;

procedure TfrmProcessDesign.C_CodeNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;


procedure TfrmProcessDesign.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel(self.Caption,cxgrid1);
end;

procedure TfrmProcessDesign.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime:=EndDate.DateTime;
end;

procedure TfrmProcessDesign.cxDBTreeList1Click(Sender: TObject);
begin
InitGrid();
end;

procedure TfrmProcessDesign.N3Click(Sender: TObject);
var
  MCPLevel:integer;
  MCPID,MMaxId,MCPParent:string;
begin
  MCPID:=Trim(ADOQueryTree.fieldbyname('CPID').AsString);
  MCPLevel:=ADOQueryTree.fieldbyname('CPLevel').AsInteger;
  MCPParent:=Trim(ADOQueryTree.fieldbyname('CPParent').AsString);

  //添加工序

  if GetLSNo(ADOQueryCmd,MMaxId,'JP','JYOrder_Process',4,1)=False then
  begin
    Application.MessageBox('取最大号失败！','提示',0);
    Exit;
  end;
  try
    frmGXAdd:=TfrmGXAdd.Create(Application);
    with frmGXAdd do
    begin
    PSID.Text:=trim(MMaxId);
     if ShowModal=1 then
     begin
      self.TBRafresh.Click;
      self.ADOQueryTree.Locate('CPID',trim(MMaxId),[]);
      end;
    end;
  finally
   frmGXAdd.Free;
  end;


end;

procedure TfrmProcessDesign.X1Click(Sender: TObject);
var
  MCPLevel:integer;
  MCPID,MMaxId,MCPParent,MCPName:string;
begin
  MCPID:=Trim(ADOQueryTree.fieldbyname('CPID').AsString);
  MCPLevel:=ADOQueryTree.fieldbyname('CPLevel').AsInteger;
  MCPParent:=Trim(ADOQueryTree.fieldbyname('CPParent').AsString);
  MCPName:=Trim(ADOQueryTree.fieldbyname('CPName').AsString);


  
  //添加班组
  if MCPLevel=1 then
  begin
    if GetLSNo(ADOQueryCmd,MMaxId,'BZ','OA_YG_BZ',4,1)=False then
    begin
      Application.MessageBox('取最大号失败！','提示',0);
      Exit;
    end;
    try
      frmBZAdd:=TfrmBZAdd.Create(Application);
      with frmBZAdd do
      begin
      BZID.Text:=trim(MMaxId);
      PSID.Text:=trim(MCPID);
      GXName.Text:=trim(MCPName);
      
       if ShowModal=1 then
       begin
        self.TBRafresh.Click;
        self.ADOQueryTree.Locate('CPID',trim(MMaxId),[]);
       end;
      end;
    finally
     frmBZAdd.Free;
    end;
  end;

   //添加员工
  if MCPLevel=2 then
  begin
     if GetLSNo(ADOQueryCmd,MMaxId,'BY','OA_YG_BZYG',4,1)=False then
    begin
      Application.MessageBox('取最大号失败！','提示',0);
      Exit;
    end;
    try
      frmYGAdd:=TfrmYGAdd.Create(Application);
      with frmYGAdd do
      begin
       BZYGID.Text:=trim(MMaxId);
       BZID.Text:=trim(MCPID);
       BZName.Text:=trim(MCPName);
       
       if ShowModal=1 then
       begin
        TBRafresh.Click;
        self.ADOQueryTree.Locate('CPID',trim(MMaxId),[]);
       end;
      end;
    finally
     frmYGAdd.Free;
    end;
  end
end;
procedure TfrmProcessDesign.N1Click(Sender: TObject);
var
  MCPLevel:integer;
  MCPID,MMaxId,MCPParent,MCPName:string;
begin
  MCPID:=Trim(ADOQueryTree.fieldbyname('CPID').AsString);
  MCPLevel:=ADOQueryTree.fieldbyname('CPLevel').AsInteger;
  MCPParent:=Trim(ADOQueryTree.fieldbyname('CPParent').AsString);
  MCPName:=Trim(ADOQueryTree.fieldbyname('CPName').AsString);


    //删除工序
  if MCPLevel=1 then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select count(*) as AA from OA_YG_BZ where PSID='''+Trim(MCPID)+'''');
      Open;
      if FieldByName('AA').AsInteger>0 then
      begin
        Application.MessageBox('已有子项数据不能删除！','提示',0);
        Exit;
      end;
    end;
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
   with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete JYOrder_Process where PSID='''+Trim(MCPID)+'''');
      ExecSQL;
    end;
  end;
  //删除班组
  if MCPLevel=2 then
  begin
  with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select count(*) as AA from OA_YG_BZYG where BZID='''+Trim(MCPID)+'''');
      Open;
      if FieldByName('AA').AsInteger>0 then
      begin
        Application.MessageBox('已有子项数据不能删除！','提示',0);
        Exit;
      end;
    end;
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
   with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete OA_YG_BZ where BZID='''+Trim(MCPID)+'''');
      ExecSQL;
    end;
  end;

   //删除员工
  if MCPLevel=3 then
  begin
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
   with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete OA_YG_BZYG where BZYGID='''+Trim(MCPID)+'''');
      ExecSQL;
    end;
  end;

   TBRafresh.Click;
   ADOQueryTree.Locate('CPID',trim(MCPParent),[]);
end;

end.
