unit U_Emp_Input;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, cxDropDownEdit, BtnEdit, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges,
  dxBarBuiltInMenu, U_BaseList, JPEG, U_SLT;

  var
  Mach: array of TfrmSlt;

type
  TfrmEmp_Input = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ToolButton3: TToolButton;
    CDS_LXR: TClientDataSet;
    DSLXR: TDataSource;
    ScrollBox1: TScrollBox;
    Label2: TLabel;
    Label7: TLabel;
    GRJL: TMemo;
    EECode: TEdit;
    Panel1: TPanel;
    Label8: TLabel;
    Panel3: TPanel;
    ToolBar3: TToolBar;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    country: TBtnEditC;
    EEID: TEdit;
    adoqueryPicture: TADOQuery;
    Label3: TLabel;
    Label13: TLabel;
    EEName: TEdit;
    Label14: TLabel;
    Birthday: TDateTimePicker;
    Label1: TLabel;
    JG: TEdit;
    Label4: TLabel;
    WH: TEdit;
    Label9: TLabel;
    hy: TEdit;
    Label10: TLabel;
    Phone: TEdit;
    Label11: TLabel;
    hjAdress: TEdit;
    Label12: TLabel;
    xjAdress: TEdit;
    Label15: TLabel;
    IDCard: TEdit;
    Label16: TLabel;
    zw: TEdit;
    Label17: TLabel;
    gzdw: TEdit;
    Label5: TLabel;
    ZZ: TEdit;
    Label6: TLabel;
    ZC: TEdit;
    Label18: TLabel;
    Label20: TLabel;
    DWDate: TDateTimePicker;
    Sex: TComboBox;
    Label19: TLabel;
    jk: TEdit;
    Label21: TLabel;
    Post: TEdit;
    Label22: TLabel;
    Dept: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
//    procedure ToolButton2Click(Sender: TObject);
    procedure countryBtnDnClick(Sender: TObject);
    procedure countryBtnUpClick(Sender: TObject);

    procedure ButtonEdit(Sender: TObject; AButtonIndex: Integer);

  private
    { Private declarations }
    procedure InitGrid();
//    procedure InitImg();
    function SaveData(): Boolean;
  public
    fkhType: string;
    { Public declarations }
//    FEEID: string;
    PState, CopyInt: Integer;
    FMainId, FDPID, FDPName: string;
  end;

var
  frmEmp_Input: TfrmEmp_Input;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_ZDYHelpSelX, U_YGSel, U_CompanySel, U_CustFileUp;

{$R *.dfm}

procedure TfrmEmp_Input.InitGrid();
begin
  try

    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select * from SY_Employee where  EEID=''' + Trim(FMainId) + '''');
      Open;
    end;
    SCSHData(ADOQueryMain, ScrollBox1, 2);
    if Dept.text='' then
    begin
       Dept.text := Trim(FDPName);
    end;
//    ParentCoName.TxtCode := ADOQueryMain.FieldByName('ParentCoCode').AsString;

    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select * from SY_Employee_contact where EEID=' + quotedstr(Trim(FMainId)));
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_LXR);
    SInitCDSData(ADOQueryMain, CDS_LXR);






  finally
    ADOQueryMain.EnableControls;
  end;
end;





procedure TfrmEmp_Input.FormDestroy(Sender: TObject);
begin

  frmEmp_Input := nil;
end;

procedure TfrmEmp_Input.countryBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmEmp_Input.countryBtnUpClick(Sender: TObject);
var
  fsj,zysc: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try

    frmZDYHelpSelX := TfrmZDYHelpSelX.Create(Application);
    with frmZDYHelpSelX do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
//        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
         while frmZDYHelpSelX.ClientDataSet1.Locate('SSel', true, []) do
        begin

          if trim(zysc) = '' then
            zysc := trim(ClientDataSet1.fieldbyname('ZDYName').AsString)
          else
            zysc := zysc + ',' + trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

          TEdit(Sender).Text := zysc;
          frmZDYHelpSelX.ClientDataSet1.delete;
        end;
      end;
    end;
  finally
    frmZDYHelpSelX.Free;
  end;
end;



procedure TfrmEmp_Input.ButtonEdit(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'gx';
      flagname := '关系';
      if ShowModal = 1 then
      begin
        CDS_LXR.Edit;
        CDS_LXR.fieldbyname('gx').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
     //   Order_Sub.fieldbyname('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        CDS_LXR.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
//  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmEmp_Input.FormClose(Sender: TObject; var Action: TCloseAction);
var
  i, j: integer;
begin
  //释放图片缓存
  j := length(Mach);
  if j > 0 then
  begin
    for i := 0 to j - 1 do
    begin
      Mach[i].free;
    end;
  end;
  SetLength(Mach, 0);
  Action := caFree;
end;

procedure TfrmEmp_Input.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'TV2', Tv2, '供应商管理');
//  WriteCxGrid(trim(self.Caption) + 'TV1', Tv1, '供应商管理');
  close;
end;

procedure TfrmEmp_Input.FormShow(Sender: TObject);
var
  i: integer;
  a: string;
begin
  ReadCxGrid(trim(self.Caption) + 'TV2', Tv2, '供应商管理');
//  ReadCxGrid(trim(self.Caption) + 'TV1', Tv1, '供应商管理');
  InitGrid();

end;

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

function TfrmEmp_Input.SaveData(): Boolean;
var
  MaxId, MaxSubId, MaxJRId, FCoCode, FECID, FJRID: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FMainId) = '' then
    begin
      if GetLSNo(ADOQueryCmd, MaxId, 'OA', 'SY_Employee', 4, 1) = False then
      begin
        Result := False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;

      EEID.Text := trim(MaxId);

//      if trim(CoCode.Text) = '' then
//      begin
//        with ADOQueryTemp do
//        begin
//          Close;
//          sql.Clear;
//          sql.Add(' exec P_BS_Com_Get_No @Str=''K'' ');
//          Open;
//        end;
//        CoCode.Text := trim(ADOQueryTemp.FieldByName('NewCoCode').asstring);
//      end;
    end
    else
    begin
      MaxId := Trim(FMainID);
    end;

    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from SY_Employee where EEID=''' + Trim(FMainId) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FMainID) = '' then
      begin
        Append;
        FieldByName('FillId').Value := Trim(DCode);
        FieldByName('Filler').Value := Trim(DName);
//        FieldByName('Saleser').Value := trim(DName);
//        FieldByName('SalesId').Value := trim(DCode);
        FieldByName('EEType').Value := '正式';
        FieldByName('EntryDate').Value := SGetServerDateTime(ADOQueryTemp);
        fieldbyname('DPID').Value := Trim(FDPID);
      end
      else
      begin
        Edit;
        FieldByName('EditId').Value := Trim(DCode);
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('EEID').Value := Trim(MaxId);
      RTSetsavedata(ADOQueryCmd, 'SY_Employee', ScrollBox1, 2);
//      FieldByName('CoType').Value := '客户';
//      FieldByName('ParentCoCode').Value := ParentCoName.TxtCode;
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from SY_Employee where EECode=' + quotedstr(trim(EECode.Text)));
      Open;
    end;
    if ADOQueryCmd.RecordCount > 1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('编号重复!', '提示', 0);
      Exit;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from SY_Employee where EEName=' + quotedstr(trim(EEName.Text)));
//      sql.Add(' and CoType=''客户''    ');
      Open;
    end;
    if ADOQueryCmd.RecordCount > 1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('名称重复!', '提示', 0);
      Exit;
    end;

//    with ADOQueryCmd do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('select * from BS_Company where CoAbbrName=' + quotedstr(trim(CoAbbrName.Text)));
//      sql.Add(' and CoType=''客户''    ');
//      Open;
//    end;
//    if ADOQueryCmd.RecordCount > 1 then
//    begin
//      ADOQueryCmd.Connection.RollbackTrans;
//      Application.MessageBox('简称重复!', '提示', 0);
//      Exit;
//    end;

//////////////            联系人              ///////////////////
    if not CDS_LXR.IsEmpty then
    begin
      with CDS_LXR do
      begin
        CDS_LXR.First;
        while not Eof do
        begin
          FECID := Trim(CDS_LXR.fieldbyname('ECID').AsString);
          if Trim(FECID) = '' then
          begin
            if GetLSNo(ADOQueryCmd, MaxSubId, 'OAC', 'SY_Employee_contact', 4, 1) = False then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('取系人最大ID失败!', '提示', 0);
              Exit;
            end;
          end
          else
          begin
            MaxSubId := trim(FECID);
          end;

          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from SY_Employee_contact where ECID=' + quotedstr(Trim(MaxSubId)));
            Open;
          end;
          with ADOQueryCmd do
          begin
            if Trim(FECID) = '' then
            begin
              Append;
            end
            else
            begin
              Edit;
            end;
            FieldByName('ECID').Value := Trim(MaxSubId);
            FieldByName('EEID').Value := Trim(MaxId);
            RTSetSaveDataCDS(ADOQueryCmd, Tv2, CDS_LXR, 'SY_Employee_contact', 0);
            Post;
          end;
          with CDS_LXR do
          begin
            Edit;
            FieldByName('ECID').Value := Trim(MaxSubId);
            Post;
          end;
          CDS_LXR.Next;
        end;
      end;
    end;
//////////////            联系人              ///////////////////


//////////////            节日提醒              ///////////////////


////////////////         更新名称          ////////////////////////
//    with ADOQueryCmd do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('exec P_Com_Up_Code @COID=' + quotedstr(Trim(MaxId)));
//      ExecSQL;
//    end;
////////////////         更新名称          ////////////////////////
    FMainID := MaxId;
    ADOQueryCmd.Connection.CommitTrans;
    FECID := Trim(MaxSubId);
    Result := True;
  except
    Result := false;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!', '提示', 0);
  end;
end;

procedure TfrmEmp_Input.ToolButton3Click(Sender: TObject);
begin

//  if Trim(CoAbbrName.Text) = '' then
//  begin
//    Application.MessageBox('简称不能为空!', '提示', 0);
//    Exit;
//  end;
//  if Trim(CoBusinessType.Text) = '' then
//  begin
//    Application.MessageBox('客户类型不能为空!', '提示', 0);
//    Exit;
//  end;
//  if Trim(CoName.Text) = '' then
//  begin
//    Application.MessageBox('名称不能为空!', '提示', 0);
//    Exit;
//  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    ModalResult := 1;
  end;
end;



procedure TfrmEmp_Input.ToolButton6Click(Sender: TObject);
var
  maxno: string;
begin
  if GetLSNo(ADOQueryCmd, maxno, 'OAC', 'SY_Employee_contact', 4, 1) = False then
  begin
    Application.MessageBox('取系人最大ID失败!', '提示', 0);
    Exit;
  end;
//  if CDS_LXR.IsEmpty then
//  begin
    with CDS_LXR do
    begin
      Append;
      fieldbyname('ECID').Value := trim(maxno);
//      fieldbyname('IsDefault').Value := true;
      Post;
    end;

//  end;

end;

procedure TfrmEmp_Input.ToolButton7Click(Sender: TObject);
begin
  if CDS_LXR.IsEmpty then
    Exit;
  if Trim(CDS_LXR.fieldbyname('ECID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete from SY_Employee_contact where ECID=''' + Trim(CDS_LXR.fieldbyname('ECID').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_LXR.Delete;
//  if CDS_LXR.IsEmpty = false then
//  begin
//    if CDS_LXR.Locate('IsDefault', true, []) = false then
//    begin
//      CDS_LXR.first;
//      CDS_LXR.edit;
//      CDS_LXR.FieldByName('IsDefault').value := true;
//    end;
//  end;

end;





//procedure TfrmEmp_Input.ToolButton2Click(Sender: TObject);
//var
//  DwFlag: string;
//begin
//  DwFlag := trim(CDS_LXR.FieldByName('CCID').AsString);
//  if CDS_LXR.Locate('IsDefault', true, []) then
//  begin
//    CDS_LXR.edit;
//    CDS_LXR.FieldByName('IsDefault').value := false;
//  end;
//  if CDS_LXR.Locate('CCID', DwFlag, []) then
//  begin
//    CDS_LXR.edit;
//    CDS_LXR.FieldByName('IsDefault').value := true;
//  end;
//
//end;

end.

