unit U_YGYPZZChkList_BGYPCG;

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_BGYPCG = 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;
    Panel1: TPanel;
    TBLiZhi: TToolButton;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    N1: TMenuItem;
    TCXTJ: TToolButton;
    BegDate: TDateTimePicker;
    Label3: TLabel;
    EndDate: TDateTimePicker;
    cxTabControl1: TcxTabControl;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column7: TcxGridDBColumn;
    v1GangWei: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1BGUnit: TcxGridDBColumn;
    v1Dept: TcxGridDBColumn;
    DataSource2: TDataSource;
    order_Sub: TClientDataSet;
    Label2: TLabel;
    v1SQDate: TcxGridDBColumn;
    v1BGQty: TcxGridDBColumn;
    TTiJiao: TToolButton;
    TCXSQ: TToolButton;
    TDel: TToolButton;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    v1BGName: TcxGridDBColumn;
    v1BGNote: TcxGridDBColumn;
    ADOQuery1: TADOQuery;
    v1Ssel: TcxGridDBColumn;
    Label1: TLabel;
    BGName: TEdit;
    Label4: TLabel;
    SQType: TEdit;
    TSsel: TToolButton;
    v1CGQty: TcxGridDBColumn;
    cxStyle2: TcxStyle;
    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 TBLiZhiClick(Sender: TObject);
    procedure TCXTJClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TCXSQClick(Sender: TObject);
    procedure TTiJiaoClick(Sender: TObject);
    procedure TDelClick(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure BGNameChange(Sender: TObject);
    procedure TSselClick(Sender: TObject);
  private
    canshu1:string;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure setstatus();
    { Private declarations }
  public
    FFInt,FCloth:Integer;
    formid:string;
    { Public declarations }
  end;

var
  frmYGYPZZChkList_BGYPCG: TfrmYGYPZZChkList_BGYPCG;

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

{$R *.dfm}

procedure TfrmYGYPZZChkList_BGYPCG.setstatus();
begin
  Tssel.Visible:=False;
  TTijiao.Visible:=False;
  TCXTJ.Visible:=False;
  CxtabControl1.Visible:=False;
  if formid='1' then
  begin
    Tssel.Visible:=true;
  end
  else
  begin
    cxTabControl1.Visible:=true;
    case cxTabControl1.TabIndex of
    0:begin
        TTIJiao.Visible:=true;
      end;
    1:begin
        TCXTJ.Visible:=true;
      end;
    2:begin

      end;
    end;
  end;
end;

procedure TfrmYGYPZZChkList_BGYPCG.FormDestroy(Sender: TObject);
begin
  frmYGYPZZChkList_BGYPCG:=nil;
end;

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

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

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

procedure TfrmYGYPZZChkList_BGYPCG.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.*,B.*,C.* ');
      sql.Add(' from OA_YG_DangAN_BGYP A  ');
      sql.add(' inner join OA_YG_DangAN_BGYP_Sub B on B.BGMainid=A.BGMainid ');
      sql.add(' inner join OA_YG_DangAN C on C.YGID=A.YGID ');
      sql.Add(' where A.SQDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
      sql.Add(' and A.SQDate<='''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      sql.Add(' and A.SQType=''采购申请''');
      if formid='1' then
      begin
        sql.add(' and isnull(B.SQStatus,'''')=''已采购'' ');
      end
      else
      begin
        if cxTabControl1.TabIndex=0 then
        begin
          sql.Add(' and B.SQStatus=''申请通过'' ');
        end else
        if cxTabControl1.TabIndex=1 then
        begin
          sql.add(' and isnull(B.SQStatus,'''')=''已采购'' ');
        end;
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  TBFind.Click;
end;


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

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

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

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

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

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

procedure TfrmYGYPZZChkList_BGYPCG.TBViewClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  frmYGYPInPut_BGYPDJ:=TfrmYGYPInPut_BGYPDJ.create(self);
  with frmYGYPInPut_BGYPDJ do
  begin
    TBSave.Visible:=False;
    FBGMainID:=trim(Order_Main.fieldbyname('BGMainID').AsString);
    if showModal=1 then
    begin

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

procedure TfrmYGYPZZChkList_BGYPCG.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_BGYPCG.TBLiZhiClick(Sender: TObject);
begin
  frmYGYPInPut_BGYPDJ:=TfrmYGYPInPut_BGYPDJ.create(self);
  with frmYGYPInPut_BGYPDJ do
  begin
    FBGMainID:='';
    if showModal=1 then
    begin

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

procedure TfrmYGYPZZChkList_BGYPCG.TCXTJClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if Order_Main.Locate('Ssel',True,[])=False then
  begin
    application.MessageBox('没有选择数据','提示');
    exit;
  end;
  with Order_Main do
  begin
    first;
    while not eof do
    begin
      if fieldbyname('Ssel').AsBoolean=True then
      begin
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('Update OA_YG_DangAN_BGYP_Sub Set SQStatus=''申请通过'',CGQty=NULL ');
          sql.Add(' where BGSubID='''+trim(Order_Main.fieldbyname('BGSubID').AsString)+'''');
          ExecSQL;
        end;
      end;
      next;
    end;
  end;
  application.MessageBox('提交成功','提示');
  InitGrid();
end;

procedure TfrmYGYPZZChkList_BGYPCG.cxTabControl1Change(Sender: TObject);
begin
  setstatus();
  InitGrid();
end;

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

procedure TfrmYGYPZZChkList_BGYPCG.TCXSQClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  frmYGYPInPut_BGYPDJ:=TfrmYGYPInPut_BGYPDJ.create(self);
  with frmYGYPInPut_BGYPDJ do
  begin
    FBGMainID:=trim(Order_Main.fieldbyname('BGMainID').AsString);
    if showModal=1 then
    begin

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

procedure TfrmYGYPZZChkList_BGYPCG.TTiJiaoClick(Sender: TObject);
var maxno: string;
begin
  if Order_Main.IsEmpty then exit;
  if Order_Main.Locate('Ssel',True,[])=False then
  begin
    application.MessageBox('没有选择数据','提示');
    exit;
  end;
  with Order_Main do
  begin
    first;
    while not eof do
    begin
      if fieldbyname('Ssel').AsBoolean=True then
      begin
        if fieldbyname('CGQty').AsFloat=0 then
        begin
          application.MessageBox('采购数量不能为空','提示');
          exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('Update OA_YG_DangAN_BGYP_Sub Set SQStatus=''已采购'',CGQty='''+trim(Order_Main.fieldbyname('CGQty').AsString)+''' ');
          sql.Add(' where BGSubID='''+trim(Order_Main.fieldbyname('BGSubID').AsString)+'''');
          ExecSQL;
        end;
      end;
      next;
    end;
  end;
  application.MessageBox('提交成功','提示');
  InitGrid();
end;

procedure TfrmYGYPZZChkList_BGYPCG.TDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  ToolBar1.SetFocus;
  if application.MessageBox('确定要删除吗','提示',1)=2 then exit;
  with ADOQueryMain do
  begin
    close;
    sql.Clear;
    sql.Add('delete from OA_YG_DangAN_BGYP where BGMainID='''+trim(Order_Main.fieldbyname('BGMainID').AsString)+'''');
    execsql;
  end;
  initGrid();
end;


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

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

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

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

end.
