unit U_YGYPZZChkList_BGYPBMKC;

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;

type
  TfrmYGYPZZChkList_BGYPBMKC = 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;
    Panel1: TPanel;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    N1: TMenuItem;
    DataSource2: TDataSource;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1KCName: TcxGridDBColumn;
    v1KCPlace: TcxGridDBColumn;
    v1SumKCQty: TcxGridDBColumn;
    v1KCUnit: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    TSsel: TToolButton;
    v1Ssel: TcxGridDBColumn;
    Label1: TLabel;
    KCName: TEdit;
    Label2: TLabel;
    KCPlace: TEdit;
    cxStyle2: TcxStyle;
    cxStyle3: TcxStyle;
    ADOQuery2: TADOQuery;
    v1DeptType: TcxGridDBColumn;
    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 CustomerNoNameChange(Sender: TObject);
    procedure TSselClick(Sender: TObject);
    procedure KCNameChange(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
  private
    canshu1:string;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure setstatus();
    { Private declarations }
  public
    FFInt,FCloth:Integer;
    Fformid: string;
    { Public declarations }
  end;

var
  frmYGYPZZChkList_BGYPBMKC: TfrmYGYPZZChkList_BGYPBMKC;

implementation
uses
  U_DataLink,U_RTFun,U_ZDYHelp, U_YGYPInPut,
  U_ZDYHelpSel,U_YGYPInput_BGYPKCDJ;

{$R *.dfm}

procedure TfrmYGYPZZChkList_BGYPBMKC.setstatus();
begin
  Tssel.Visible:=False;
  v1Ssel.Visible:=False;
  v1KCPlace.Visible:=true;
  v1SumKCQty.Visible:=true;
  if trim(Fformid)='1' then
  begin
    Tssel.Visible:=True;
    v1Ssel.Visible:=true;
  end;
  if trim(Fformid)='2' then
  begin
    v1KCPlace.Visible:=false;
    v1SumKCQty.Visible:=false;
    TSsel.Visible:=True;
  end;
end;

procedure TfrmYGYPZZChkList_BGYPBMKC.FormDestroy(Sender: TObject);
begin
  frmYGYPZZChkList_BGYPBMKC:=nil;
end;

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

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

procedure TfrmYGYPZZChkList_BGYPBMKC.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('员工办公入库列表',Tv1,'OA管理');
end;

procedure TfrmYGYPZZChkList_BGYPBMKC.InitGrid();
var FDEPT,FDPName: string;
    Fint:integer;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQuery2 do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.* from OA_YG_DangAn A ');
      sql.Add('inner join SY_Dept B on A.DPID=B.DPID');
      SQL.Add('where isnull(A.YGEName,YGName)='''+trim(DName)+'''');
      Open;
    end;
    FDEPT:=ADOQuery2.fieldbyname('DPParent').AsString;
    for Fint:=2 to ADOQuery2.fieldbyname('DPlevel').AsInteger-1 do
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from SY_Dept where DPID='''+trim(FDEPT)+'''');
        open;
      end;
      FDEPT:=Trim(ADOQueryCmd.fieldbyname('DPParent').AsString);
    end;
    if FDEPT<>'' then
    begin
      FDPName:=ADOQueryCmd.fieldbyname('DPName').AsString;
    end
    else
    begin
      FDPName:='';
    end;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select AA.*,SumKCQty=AA.SumRKQty-AA.SumCKQty from  ');
      sql.add('(select KCPlace=(select Top 1 KCPlace from OA_YG_DangAN_BGYPKC B where ');
      sql.add(' B.KCName=A.KCName and B.DeptType=A.DeptType and B.RKStatus=''1'' order by B.KCID desc), ');
      sql.add('A.KCName,A.DeptTYpe,A.KCUnit,Sum(A.KCQty) SumRKQty, ');
      sql.add('SumCKQty=(select isnull(Sum(B.CKQty),0) from OA_YG_DangAN_BGYPCK B where ');
      sql.add('B.CKName=A.KCName and B.DeptType=A.DeptType and B.CKUnit=A.KCUnit) ');
      sql.add('from OA_YG_DangAN_BGYPKC A  ');
      sql.add(' where A.DeptType='''+Trim(FDPName)+''' and A.RKStatus=''1''');
      sql.add('group by A.KCName,A.DeptTYpe,A.KCUnit) AA');
      sql.add(' Order by AA.KCName');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmYGYPZZChkList_BGYPBMKC.InitForm();
begin
  //EndDate.Date:=SGetServerDate(ADOQueryCmd);
  //BegDate.Date:=EndDate.Date-7;
  ReadCxGrid('员工办公入库列表',Tv1,'OA管理');
  setstatus();
  InitGrid();
end;

procedure TfrmYGYPZZChkList_BGYPBMKC.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

function TfrmYGYPZZChkList_BGYPBMKC.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_BGYPBMKC.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel('员工入库列表',cxGrid1);
end;

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

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

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

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

procedure TfrmYGYPZZChkList_BGYPBMKC.CustomerNoNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmYGYPZZChkList_BGYPBMKC.TSselClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  Modalresult:=1;
end;

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

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

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

procedure TfrmYGYPZZChkList_BGYPBMKC.Tv1DblClick(Sender: TObject);
begin
  Tssel.Click;
end;

end.
