unit U_YGYPZZChkList_BGYPBMRK;

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,
  BtnEdit;

type
  TfrmYGYPZZChkList_BGYPBMRK = 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;
    TBLiZhi: TToolButton;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    N1: TMenuItem;
    BegDate: TDateTimePicker;
    Label3: TLabel;
    EndDate: TDateTimePicker;
    Label2: TLabel;
    TDel: TToolButton;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1KCName: TcxGridDBColumn;
    v1KCPlace: TcxGridDBColumn;
    v1KCQty: TcxGridDBColumn;
    v1KCUnit: TcxGridDBColumn;
    v1RKPeoPle: TcxGridDBColumn;
    v1RKDate: TcxGridDBColumn;
    v1KCNote: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Label1: TLabel;
    KCName: TEdit;
    Label4: TLabel;
    KCPlace: TEdit;
    ADOQuery1: TADOQuery;
    TDWHS: TToolButton;
    Panel2: TPanel;
    Label5: TLabel;
    Label6: TLabel;
    Qty: TEdit;
    Qtyunit: TBtnEditA;
    QD: TButton;
    GB: TButton;
    cxTabControl1: TcxTabControl;
    TCXLY: TToolButton;
    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 CustomerNoNameChange(Sender: TObject);
    procedure TBLiZhiClick(Sender: TObject);
    procedure TDelClick(Sender: TObject);
    procedure KCNameChange(Sender: TObject);
    procedure QtyunitBtnClick(Sender: TObject);
    procedure TDWHSClick(Sender: TObject);
    procedure GBClick(Sender: TObject);
    procedure QDClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TCXLYClick(Sender: TObject);
  private
    canshu1:string;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure setstatus();
    { Private declarations }
  public
    FFInt,FCloth:Integer;

    { Public declarations }
  end;

var
  frmYGYPZZChkList_BGYPBMRK: TfrmYGYPZZChkList_BGYPBMRK;

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

{$R *.dfm}

procedure TfrmYGYPZZChkList_BGYPBMRK.setstatus();
begin
  TDWHS.Visible:=False;
  TCXLY.Visible:=False;
  if cxtabcontrol1.TabIndex=0 then
  begin
    TDWHS.Visible:=true;
  end;
  if cxtabcontrol1.TabIndex=1 then
  begin
    TCXLY.Visible:=true;
  end;
end;

procedure TfrmYGYPZZChkList_BGYPBMRK.FormDestroy(Sender: TObject);
begin
  frmYGYPZZChkList_BGYPBMRK:=nil;
end;

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

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

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

procedure TfrmYGYPZZChkList_BGYPBMRK.InitGrid();
var FDept:string;
    Fint:integer;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQuery1 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:=ADOQuery1.fieldbyname('DPParent').AsString;
    for Fint:=2 to ADOQuery1.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 ADOQueryCmd.IsEmpty then exit;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.* ');
      sql.Add(' from OA_YG_DangAN_BGYPKC A  ');
      sql.Add(' where A.DeptType='''+Trim(self.ADOQueryCmd.fieldbyname('DPName').AsString)+'''');
      if cxtabcontrol1.TabIndex<>0 then
      begin
        sql.add(' and A.RKDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
        sql.Add(' and A.RKDate<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      end;
      if cxtabcontrol1.TabIndex=0 then
      begin
        sql.add(' and isnull(RKStatus,'''')='''' ');
      end;
      if cxtabcontrol1.TabIndex=1 then
      begin
        sql.add(' and isnull(RKStatus,'''')=''1'' ');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


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

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

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

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

procedure TfrmYGYPZZChkList_BGYPBMRK.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_BGYPBMRK.TBLiZhiClick(Sender: TObject);
begin
  frmYGYPInPut_BGYPBMKCDJ:=TfrmYGYPInPut_BGYPBMKCDJ.create(self);
  with frmYGYPInPut_BGYPBMKCDJ do
  begin
    FKCID:='';
    if showModal=1 then
    begin

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

procedure TfrmYGYPZZChkList_BGYPBMRK.TDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if application.MessageBox('确定要撤销吗','提示',1)=2 then exit;
  with ADOQueryMain do
  begin
    close;
    sql.Clear;
    sql.Add('delete from OA_YG_DangAN_BGYPKC where KCID='''+trim(Order_Main.fieldbyname('KCID').AsString)+'''');
    execsql;
  end;
  with ADOQueryMain do
        begin
          Close;
          sql.Clear;
          sql.add('update OA_YG_LZYJQD set RKStatus='''' ');
          sql.add('where YJID='''+Trim(Order_Main.fieldbyname('BGMainid').asstring)+'''');
          execsql;
        end;
  initGrid();
end;


procedure TfrmYGYPZZChkList_BGYPBMRK.KCNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmYGYPZZChkList_BGYPBMRK.QtyunitBtnClick(Sender: TObject);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='BGUnit';
    flagname:='数量单位';
    if showModal=1 then
    begin
      Qtyunit.Text:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
    end;
    free;
  end;
end;

procedure TfrmYGYPZZChkList_BGYPBMRK.TDWHSClick(Sender: TObject);
begin
  if order_Main.IsEmpty then exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.KCUnit ');
    sql.Add(' from OA_YG_DangAN_BGYPKC A  ');
    sql.Add(' where A.DeptType='''+Trim(self.Order_Main.fieldbyname('DeptType').AsString)+'''');
    sql.add(' and KCName='''+Trim(self.Order_Main.fieldbyname('KCName').AsString)+'''');
    sql.add(' and A.RKStatus=''1''');
    open;                                              
  end;
  QtyUnit.Text:=trim(ADOQueryCmd.fieldbyname('KCUnit').AsString);
  panel2.Visible:=true;
end;

procedure TfrmYGYPZZChkList_BGYPBMRK.GBClick(Sender: TObject);
begin
  panel2.Visible:=False;
end;

procedure TfrmYGYPZZChkList_BGYPBMRK.QDClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('update OA_YG_DangAN_BGYPKC set KCQty='''+trim(Qty.Text)+''',');
    sql.add('KCUnit='''+trim(QtyUnit.Text)+''' ');
    sql.add(',RKStatus=''1''');
    sql.add('where KCID='''+trim(Order_Main.fieldbyname('KCID').AsString)+'''');
    execsql;
  end;
  initgrid();
  panel2.Visible:=False;
end;

procedure TfrmYGYPZZChkList_BGYPBMRK.cxTabControl1Change(Sender: TObject);
begin
  setstatus();
  initgrid();
end;

procedure TfrmYGYPZZChkList_BGYPBMRK.TCXLYClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if application.MessageBox('确定要撤销吗','提示',1)=2 then exit;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('update OA_YG_DangAN_BGYPKC set RKStatus=''''');
    sql.add('where KCID='''+trim(Order_Main.fieldbyname('KCID').AsString)+'''');
    execsql;
  end;
  Order_Main.Delete;
end;

end.
