unit U_YGYPZZChkList_BGYPSH;

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;

type
  TfrmYGYPZZChkList_BGYPSH = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Order_Main: TClientDataSet;
    TBView: TToolButton;
    Panel1: TPanel;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    N1: TMenuItem;
    TCXTJ: TToolButton;
    BegDate: TDateTimePicker;
    Label3: TLabel;
    EndDate: TDateTimePicker;
    cxTabControl1: TcxTabControl;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    V2Chker: TcxGridDBColumn;
    V2Chktime: TcxGridDBColumn;
    V2Chkstatus: TcxGridDBColumn;
    V2chkNote: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    DataSource2: TDataSource;
    order_Sub: TClientDataSet;
    Label2: TLabel;
    TTiJiao: TToolButton;
    TNOTG: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1SQDate: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1GangWei: TcxGridDBColumn;
    v1Dept: TcxGridDBColumn;
    v1BGName: TcxGridDBColumn;
    v1BGQty: TcxGridDBColumn;
    v1BGUnit: TcxGridDBColumn;
    v1BGNote: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    v1SQType: TcxGridDBColumn;
    v1Ssel: TcxGridDBColumn;
    Label1: TLabel;
    BGName: 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 TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBViewClick(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure TCXTJClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TTiJiaoClick(Sender: TObject);
    procedure TNOTGClick(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure BGNameChange(Sender: TObject);
  private
    canshu1:string;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    procedure setstatus();
    procedure GengXinKC();
    procedure GengXinBMKC();
    { Private declarations }
  public
    FFInt,FCloth:Integer;
    FSQType:string;
    { Public declarations }
  end;

var
  frmYGYPZZChkList_BGYPSH: TfrmYGYPZZChkList_BGYPSH;

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

{$R *.dfm}
procedure TfrmYGYPZZChkList_BGYPSH.GengXinBMKC();
var maxno:string;
begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from OA_YG_DangAN_BGYPKC where BGMainID='''+Trim(Order_Main.fieldbyname('BGSubid').AsString)+'''');
    open;
  end;
  if ADOQueryCmd.isempty then
  begin
    if GetLSNo(ADOQueryCmd,maxno,'CK','OA_YG_DangAN_BGYPKC',4,1)=False then
    begin
      Application.MessageBox('取最大号失败!','提示',0);
      Exit;
    end;
  end
  else
  begin
    maxno:=ADOQueryCmd.fieldbyname('KCID').asstring;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from OA_YG_DangAN_BGYPKC where KCID='''+Trim(maxno)+'''');
    Open;
    if isempty then
    begin
      append;
    end
    else
    edit;
    fieldbyname('KCID').Value:=trim(Maxno);
    fieldbyname('BGMainid').Value:=trim(Order_Main.fieldbyname('BGSubid').asstring);
    fieldbyname('DeptType').Value:=Order_Main.fieldbyname('ZDept').asstring;
    fieldbyname('RKPeoPle').Value:=trim(Order_Main.fieldbyname('YGName').asstring);
    fieldbyname('KCName').value:=Order_Main.fieldbyname('BGName').asstring;
    fieldbyname('KCUnit').value:=Order_Main.fieldbyname('BGUnit').asstring;
    fieldbyname('RKDate').asdatetime:=SGetServerDate(ADOQueryTemp);
    fieldbyname('KCQty').value:=Order_Main.fieldbyname('BGQty').asfloat;
    post;
  end;
end;

procedure TfrmYGYPZZChkList_BGYPSH.GengXinKC();
var maxno: string;
begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from OA_YG_DangAN_BGYPCK where BGSubID='''+Trim(Order_Main.fieldbyname('BGSubid').AsString)+'''');
    open;
  end;
  if ADOQueryCmd.isempty then
  begin
    if GetLSNo(ADOQueryCmd,maxno,'CK','OA_YG_DangAN_BGYPCK',4,1)=False then
    begin
      Application.MessageBox('取最大号失败!','提示',0);
      Exit;
    end;
  end
  else
  begin
    maxno:=ADOQueryCmd.fieldbyname('CKID').asstring;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from OA_YG_DangAN_BGYPCK where CKID='''+Trim(maxno)+'''');
    Open;
    if isempty then
    begin
      append;
    end
    else
    edit;
    fieldbyname('CKID').Value:=trim(Maxno);
    fieldbyname('CKDate').AsDateTime:=SGetServerDate(ADOQueryTemp);
    fieldbyname('CKQty').Value:=Order_Main.fieldbyname('BGQty').asfloat;
    fieldbyname('CKUnit').value:=Order_Main.fieldbyname('BGUnit').AsString;
    fieldbyname('BGSubID').value:=Order_Main.fieldbyname('BGSubID').AsString;
    fieldbyname('LYName').AsString:=Order_Main.fieldbyname('YGName').AsString;
    fieldbyname('DeptType').AsString:='总经办';
    fieldbyname('CKName').AsString:=Order_Main.fieldbyname('BGName').AsString;
    post;
  end;
end;

procedure TfrmYGYPZZChkList_BGYPSH.setstatus();
begin
  TTiJiao.Visible:=False;
  TCXTJ.Visible:=False;
  TNOTG.Visible:=False;
  case cxTabControl1.TabIndex of
    0:begin
        TTiJiao.Visible:=True;
        TNOTG.Visible:=True;
      end;
    1:begin
        TCXTJ.Visible:=true;
      end;
    2:begin

      end;
  end;
end;

procedure TfrmYGYPZZChkList_BGYPSH.FormDestroy(Sender: TObject);
begin
  frmYGYPZZChkList_BGYPSH:=nil;
end;

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

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

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

procedure TfrmYGYPZZChkList_BGYPSH.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.*,B.*,C.*,D.* ');
      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_Chk C on C.Mainid=B.BGSubID and C.OAType=''办公申请'' ');
      sql.add(' inner join OA_YG_DangAn D on D.YGID=A.YGID ');
      sql.add(' where A.SQType='''+trim(FSQType)+''' ');
      if cxTabControl1.TabIndex<>0 then
      begin
        sql.Add(' and A.SQDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
        sql.Add(' and A.SQDate<='''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      end;
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and isnull(C.Chkstatus,'''')='''' and isnull(B.SQStatus,'''')=''已送审''');
      end;
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add(' and isnull(C.Chkstatus,'''')=''申请通过'' ');
      end;
      sql.add(' and C.Chker='''+trim(DName)+''' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
    with ADOQueryCmd do
    begin
      close;
      sql.clear;
      sql.add('select A.* from OA_Chk A ');
      sql.add('where A.Mainid='''+trim(Order_Main.fieldbyname('BGSubID').AsString)+''' ');
      sql.Add('and isnull(OAType,'''')=''办公申请''');
      open;
    end;
    SCreateCDS20(ADOQueryCmd,Order_Sub);
    SInitCDSData20(ADOQueryCmd,Order_Sub);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


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

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

procedure TfrmYGYPZZChkList_BGYPSH.TBRafreshClick(Sender: TObject);
begin
  Toolbar1.SetFocus;
  InitGrid();
end;

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

procedure TfrmYGYPZZChkList_BGYPSH.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_BGYPSH.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_BGYPSH.TCXTJClick(Sender: TObject);
var FNXMoney: double;
begin
  toolbar1.SetFocus;
  if Order_Main.IsEmpty then Exit;
  if Application.MessageBox('确定要执行撤销操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('Update OA_YG_DangAN_BGYP_Sub Set SQStatus=''已送审'' ');
      sql.Add(' where BGSubid='''+Order_Main.fieldbyname('BGSubid').AsString+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from OA_YG_DangAN_BGYPCK ');
      sql.add('where BGSubID='''+trim(Order_Main.fieldbyname('BGSubid').AsString)+'''');
      execsql;
    end;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from OA_YG_DangAN_BGYPKC ');
      sql.add('where BGMainid='''+trim(Order_Main.fieldbyname('BGSubid').AsString)+'''');
      execsql;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('Update OA_Chk set ChkStatus='''' ');
      sql.Add(' where CKID='''+Order_Main.fieldbyname('CKID').AsString+'''');
      ExecSQL;
    end;
    initGrid();
    ADOQueryCmd.Connection.CommitTrans;
    Exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

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

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

procedure TfrmYGYPZZChkList_BGYPSH.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  with ADOQueryCmd do
  begin
      close;
      sql.clear;
      sql.add('select A.* from OA_Chk A ');
      sql.add('where A.Mainid='''+trim(Order_Main.fieldbyname('BGSubid').AsString)+''' ');
      sql.Add('and isnull(OAType,'''')=''办公申请''');
      open;
    end;
    SCreateCDS20(ADOQueryCmd,Order_Sub);
    SInitCDSData20(ADOQueryCmd,Order_Sub);
end;

procedure TfrmYGYPZZChkList_BGYPSH.TTiJiaoClick(Sender: TObject);
var FNXMoney: double;
    Fbgname:string;
begin
  if Order_Main.IsEmpty then exit;
  toolbar1.SetFocus;
  if order_Main.Locate('Ssel',true,[])=False then
  begin
    application.MessageBox('没有选择数据','提示');
    exit;
  end;
  try
    with Order_Main do
    begin
      first;
      while not eof do
      begin
        if fieldbyname('Ssel').AsBoolean=True then
        begin
          if Order_Main.FieldByName('SQType').AsString='领用申请' then
          begin
            with ADOQueryMain do
            begin
            Close;
            sql.Clear;
            sql.Add('select AA.*,SumKCQty=(isnull(SumRKQty,0)-isnull(SumCKQty,0)) ');
            sql.add('from (select A.KCName,A.DeptType,A.KCUnit,Sum(A.KCQty) SumRKQty,');
            sql.add(' SumCKQty=(select Sum(B.CKQty) from OA_YG_DangAN_BGYPCK B where B.CKName=A.KCName And B.CKUnit=A.KCUnit and B.DeptType=A.DeptType)');
            sql.Add(' from OA_YG_DangAN_BGYPKC A ');
            sql.Add(' where A.DeptType=''总经办'' ');
            sql.add(' and A.KCName='''+trim(Order_Main.fieldbyname('BGName').AsString)+''' ');
            sql.add(' and A.KCUnit='''+trim(Order_Main.fieldbyname('BGUnit').AsString)+''' ');
            sql.Add(' group by A.KCName,A.DeptType,A.KCUnit) AA');
            open;
            end;
            Fbgname:=trim(Order_Main.fieldbyname('BGName').AsString);
            if ADOQueryMain.IsEmpty then
            begin
            showMessage('库存中没有'+FBGName+'或单位不统一');
            exit;
            end
            else
            begin
            if ADOQueryMain.FieldByName('SumKCQty').AsFloat<Order_Main.fieldbyname('BGQty').AsFloat then
            begin
              application.MessageBox('库存数量不足','提示');
              exit;
            end;
            end;
          end;
          
          if trim(DParameters1)='高权限' then
          begin
            with ADOQueryCmd do
            begin
              Close;
              SQL.Clear;
              sql.Add('Update OA_Chk Set ');
              sql.Add('ChkTime=getdate(),Chkstatus=''申请通过'' ');
              sql.Add('where Mainid='''+trim(Order_Main.fieldbyname('BGSubID').AsString)+''' ');
              sql.add('and OAType=''办公申请''');
              ExecSQL;
            end;
          end
          else
          begin
            with ADOQueryCmd do
            begin
              Close;
              SQL.Clear;
              sql.Add('Update OA_Chk Set ');
              sql.Add(' ChkTime=getdate(),Chkstatus=''申请通过'' ');
              sql.Add(' where CKID='''+trim(Order_Main.fieldbyname('CKID').AsString)+''' ');
              ExecSQL;
            end;
          end;
          with ADOQueryCmd do
          begin
            close;
            sql.clear;
            sql.add('select A.* from OA_Chk A ');
            sql.add('where A.Mainid='''+trim(Order_Main.fieldbyname('Mainid').AsString)+''' ');
            sql.add('and isnull(A.Chkstatus,'''')<>''申请通过'' ');
            sql.add('and isnull(A.OAType,'''')=''办公申请''');
            open;
          end;
          if ADOQueryCmd.IsEmpty then
          begin
            with ADOQueryMain do
            begin
              close;
              sql.Clear;
              sql.add('update OA_YG_DangAN_BGYP_Sub set ');
              sql.add('SQStatus=''申请通过'' ');
              sql.Add(' where BGSubID='''+trim(Order_Main.fieldbyname('BGSubID').AsString)+'''');
              execsql;
            end;
            if Order_Main.FieldByName('SQType').AsString='领用申请' then
            begin
              GengXinKC();
              GengXinBMKC();
            end;
          end;
          Order_Main.Delete;
        end;
        next;
      end;
    end;
    application.MessageBox('审核成功','提示');
    initGrid();
  except;
    application.MessageBox('审核失败','提示');
    exit;
  end;
end;

procedure TfrmYGYPZZChkList_BGYPSH.TNOTGClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  toolbar1.SetFocus;
  if Application.MessageBox('确定要执行撤销操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('Update OA_YG_DangAN_BGYP_Sub Set SQStatus=''不通过''');
      sql.Add(' where BGSubID='''+trim(Order_Main.fieldbyname('BGSubID').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete from OA_Chk ');
      sql.Add(' where Mainid='''+Order_Main.fieldbyname('BGMainID').AsString+'''');
      sql.Add(' and isnull(OAType,'''')=''办公申请'' ');
      ExecSQL;
    end;
    initGrid();
    ADOQueryCmd.Connection.CommitTrans;
    Exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

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

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

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

end.
