unit U_YGYPInPut_NDGZHZ;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxMemo, cxRichEdit, ComCtrls, cxContainer,
  cxTextEdit, cxMaskEdit, cxButtonEdit, StdCtrls, ToolWin, DBClient, ADODB,
  ExtCtrls, BtnEdit, cxCalendar,StrUtils, cxDropDownEdit,jpeg,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP, cxPC,
  cxGridCustomPopupMenu, cxGridPopupMenu, Menus, ExtDlgs;

type
  TfrmYGYPInPut_NDGZHZ = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    TBSave: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1BGName: TcxGridDBColumn;
    v1BGQty: TcxGridDBColumn;
    v1BGUnit: TcxGridDBColumn;
    v1BGNote: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Panel1: TPanel;
    ToolBar2: TToolBar;
    TAdd: TToolButton;
    TDel: TToolButton;
    Label4: TLabel;
    SQName: TEdit;
    Label7: TLabel;
    GangWei: TEdit;
    Dept: TEdit;
    Label6: TLabel;
    Label5: TLabel;
    SQDate: TDateTimePicker;
    SQType: TComboBox;
    Label1: TLabel;
    Label2: TLabel;
    SQNote: TEdit;
    Label3: TLabel;
    ZDept: TEdit;
    ADOQuery2: TADOQuery;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure v1BGUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1BGNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TaddClick(Sender: TObject);
    procedure TDelClick(Sender: TObject);
  private
    TPInt:Integer;
    procedure InitData();
    function  SaveData():Boolean;
    { Private declarations }
  public
    PState,CopyInt:Integer;
    FBGMainID,FYGID:String;
    { Public declarations }
  end;

var
  frmYGYPInPut_NDGZHZ: TfrmYGYPInPut_NDGZHZ;
implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_ZDYHelpSel,U_YGYPZZChkList_BGYPLYSQ;

{$R *.dfm}

procedure TfrmYGYPInPut_NDGZHZ.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmYGYPInPut_NDGZHZ.InitData();
Var FDEPT:String;
    Fint: Integer;
begin
  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 ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from SY_Dept where DPID='''+trim(FDEPT)+'''');
      open;
    end;
    FDEPT:=Trim(ADOCmd.fieldbyname('DPParent').AsString);
  end;
  if ADOCmd.IsEmpty=False then
  ZDept.Text:=trim(ADOCmd.fieldbyname('DPName').asstring);
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* from OA_YG_DangAN A ');
    sql.add('where isnull(A.YGEName,A.YGName)='''+trim(DName)+''' ');
    Open;
  end;
  SCSHDataNew(ADOCmd,Panel1,2);
  SQName.Text:=trim(DName);
  FYGID:=trim(ADOCmd.fieldbyname('YGID').asstring);
  SQDate.DateTime:=SGetServerDate(ADOTemp);
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* from OA_YG_DangAN_BGYP A ');
    sql.add('where A.BGMainID='''+trim(FBGMainID)+''' ');
    Open;
  end;
  SCSHDataNew(ADOCmd,Panel1,1);
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* from OA_YG_DangAN_BGYP_Sub A ');
    sql.add('where A.BGMainID='''+trim(FBGMainID)+'''');
    Open;
  end;
  SCreateCDS20(ADOCmd,Order_Sub);
  SInitCDSData20(ADOCmd,Order_Sub);
end;

procedure TfrmYGYPInPut_NDGZHZ.FormShow(Sender: TObject);
begin
  InitData();
end;

function TfrmYGYPInPut_NDGZHZ.SaveData():Boolean;
var
  maxno:String;
begin
  Result:=False;
  try
    ADOCmd.Connection.BeginTrans;
    if trim(FBGMainID)='' then
    begin
      if GetLSNo(ADOCmd,FBGMainid,'BY','OA_YG_DangAN_BGYP',4,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
    end;
    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from OA_YG_DangAN_BGYP ');
      sql.add('where BGMainid='''+trim(FBGMainid)+'''');
      open;
      if ADOCmd.IsEmpty then append
      else edit;
      fieldbyname('BGMainid').value:=trim(FBGMainid);
      RTSetsavedata(ADOCmd,'OA_YG_DangAN_BGYP',Panel1,1);
      fieldbyname('YGID').AsString:=trim(FYGID);
      post;
    end;
    with Order_Sub do
    begin
      first;
      while not eof do
      begin
        if fieldbyname('BGSubid').AsString='' then
        begin
          if GetLSNo(ADOCmd,maxno,'BG','OA_YG_DangAN_BGYP_Sub',4,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取最大号失败!','提示',0);
            Exit;
          end;
        end
        else
        begin
          maxno:=trim(Order_Sub.fieldbyname('BGSubid').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from OA_YG_DangAN_BGYP_Sub where BGSubid='''+Trim(maxno)+'''');
          Open;
          if isempty then
          begin
            append;
            fieldbyname('SQStatus').Value:='未提交';
          end
          else
          edit;
          fieldbyname('BGSubid').Value:=trim(Maxno);
          fieldbyname('BGMainid').value:=trim(FBGMainid);
          fieldbyname('BGLYSubid').Value:=trim(Order_Sub.fieldbyname('BGLYSubid').asstring);
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'OA_YG_DangAN_BGYP_Sub',0);
          post;
        end;
        next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmYGYPInPut_NDGZHZ.TBSaveClick(Sender: TObject);
var
  FSFNO:String;
begin
  ToolBar1.SetFocus;
  if order_Sub.isempty then
  begin
    Application.MessageBox('明细不能为空','提示',0);
    Exit;
  end;
  if Dept.Text='' then
  begin
    application.MessageBox('档案不存在,请提交档案','提示');
    exit;
  end;
  if GangWei.Text='' then
  begin
    application.MessageBox('岗位不能为空','提示');
    exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    Modalresult:=1;
  end;
end;

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

procedure TfrmYGYPInPut_NDGZHZ.FormDestroy(Sender: TObject);
begin
  frmYGYPInPut_NDGZHZ:=nil;
end;

procedure TfrmYGYPInPut_NDGZHZ.v1BGUnitPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='BGUnit';
    flagname:='数量单位';
    if showModal=1 then
    begin
      with Order_Sub do
      begin
        edit;
        fieldbyname('BGUnit').Value:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmYGYPInPut_NDGZHZ.v1BGNamePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='BGName';
    flagname:='办公用品';
    //FNote:=True;
    //v1Note.Caption:='数量单位';
    if showModal=1 then
    begin
      with Order_Sub do
      begin
        edit;
        fieldbyname('BGName').Value:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        //fieldbyname('BGUnit').Value:=trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmYGYPInPut_NDGZHZ.TaddClick(Sender: TObject);
begin
  if trim(SQType.Text)='' then
  begin
    application.MessageBox('申请类型不能为空','提示');
    exit;
  end;
  with Order_Sub do
  begin
    append;
      
    post;
  end;
  {else
  begin
    frmYGYPZZChkList_BGYPLYSQ:=TfrmYGYPZZChkList_BGYPLYSQ.Create(self);
    with frmYGYPZZChkList_BGYPLYSQ do
    begin
      FSQType:='领用申请';
      if showmodal=1 then
      begin
        with Order_Main do
        begin
          first;
          while not eof do
          begin
            if fieldbyname('Ssel').AsBoolean=true then
            begin
              with Order_Sub do
              begin
                edit;
                fieldbyname('BGName').Value:=order_Main.fieldbyname('BGName').AsString;
                fieldbyname('BGQty').Value:=Order_Main.fieldbyname('BGQty').asfloat;
                fieldbyname('BGUnit').value:=Order_Main.fieldbyname('BGUnit').AsString;
                fieldbyname('BGNote').value:=Order_Main.fieldbyname('BGNote').asstring;
                fieldbyname('BGLYSubid').value:=Order_Main.fieldbyname('BGSubid').asstring;
                post;
              end;
            end;
            next;
          end;
        end;
      end;
      free;
    end;
  end;}
end;

procedure TfrmYGYPInPut_NDGZHZ.TDelClick(Sender: TObject);
begin
  if Order_Sub.IsEmpty then exit;
  with ADOCmd do
  begin
    close;
    sql.Clear;
    sql.Add('delete from OA_YG_DangAN_BGYP_Sub ');
    sql.add('where BGSubid='''+trim(Order_Sub.fieldbyname('BGSubid').AsString)+'''');
    execsql;
  end;
  Order_Sub.Delete;
end;

end.
