unit U_ZdyAttInputXH;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, cxPC, cxControls, ComCtrls, ToolWin, StdCtrls, Menus,
  cxTextEdit, cxCurrencyEdit, BtnEdit, cxContainer, cxEdit, cxImage, DB, ADODB,
  cxDBEdit, cxLookAndFeelPainters, cxButtons, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdFTP, ShellAPI, strutils, cxGraphics,
  cxLookAndFeels;

type
  TfrmZdyAttInputXH = class(TForm)
    ToolBar1: TToolBar;
    Tsave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    zdyCode: TEdit;
    Label2: TLabel;
    zdyName: TEdit;
    Label6: TLabel;
    defnote1: TEdit;
    Label13: TLabel;
    Label17: TLabel;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    ADOQueryImage: TADOQuery;
    ADOQuerymain: TADOQuery;
    Picture4: TcxDBImage;
    FileName: TcxButton;
    IdFTP1: TIdFTP;
    DataSource1: TDataSource;
    note: TMemo;
    Label7: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    defstr1: TEdit;
    defstr2: TEdit;
    Label5: TLabel;
    Label8: TLabel;
    defnote2: TBtnEditA;
    Label9: TLabel;
    defnote5: TBtnEditA;
    defstr3: TBtnEditA;
    Label10: TLabel;
    defnote6: TBtnEditA;
    Label11: TLabel;
    DEFStr4: TComboBox;
    Label12: TLabel;
    DJ: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TsaveClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxDBImage1DblClick(Sender: TObject);
    procedure FileNameClick(Sender: TObject);
    procedure defnote2BtnClick(Sender: TObject);
    procedure defnote5BtnClick(Sender: TObject);
    procedure defstr3BtnClick(Sender: TObject);
    procedure defnote6BtnClick(Sender: TObject);
  private
    function savedata(): Boolean;
    procedure InitGrid();
    procedure InitImage();
    { Private declarations }
  public
    canshu1: string;
    FATID, FHXType: string;
    Fflag: integer;
    fFlileFlag, UserDataFlag: string;

    { Public declarations }
  end;

var
  frmZdyAttInputXH: TfrmZdyAttInputXH;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, getpic, U_KHListSelJJ;
{$R *.dfm}

function TfrmZdyAttInputXH.savedata(): Boolean;
var
  fzdyCode, fType: string;
begin
  result := false;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if fflag = 0 then
    begin
      if Trim(DEFStr4.Text) = '印花型' then
      begin
        if GetLSNo(ADOQueryCmd, fzdyCode, 'P', 'KH_Zdy_Attachment', 4, 0) = False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取最大号失败！', '提示', 0);
          Exit;
        end;
      end
      else if Trim(DEFStr4.Text) = '超声波' then
      begin
        if GetLSNo(ADOQueryCmd, fzdyCode, 'CSB', 'KH_Zdy_Attachment', 3, 0) = False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取最大号失败！', '提示', 0);
          Exit;
        end;
      end
      else
      begin
        if GetLSNo(ADOQueryCmd, fzdyCode, 'T', 'KH_Zdy_Attachment', 4, 0) = False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取最大号失败！', '提示', 0);
          Exit;
        end;
      end;
      zdyCode.Text := fzdyCode;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from KH_Zdy_Attachment where ATID=''' + Trim(FATID) + '''');
      Open;
      if IsEmpty then
      begin
        append;
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        Edit;
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTmp);
      end;
      SSetsaveSqlNew(ADOQueryCmd, 'KH_Zdy_Attachment', GroupBox1, 2);
      FieldByName('ATID').Value := Trim(FATID);
      FieldByName('Type').Value := 'CPHX';
      post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from KH_Zdy_Attachment where ZdyName=''' + Trim(zdyName.Text) + '''');
      sql.Add('and Type=''CPHX'' ');
      Open;
    end;
    if ADOQueryCmd.RecordCount > 1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('花型名称重复!', '提示', 0);
      Exit;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    result := true;
  except
  end;
end;

procedure TfrmZdyAttInputXH.InitGrid();
begin
  with ADOQueryMain do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_Zdy_Attachment A');
    sql.Add('where ATID=' + quotedstr(trim(FATID)));
    open;
  end;
  if not ADOQueryMain.IsEmpty then
  begin
    zdyCode.Enabled := false;
    FHXType := Trim(ADOQuerymain.fieldbyname('DEFstr4').AsString);
    SCSHDataNew(ADOQueryMain, GroupBox1, 2);
  end;
end;

procedure TfrmZdyAttInputXH.InitImage();
begin
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where WBID=' + quotedstr(trim(FATID)));
    sql.Add('and TFType=' + quotedstr(trim('HX')));
    open;
  end;
end;

procedure TfrmZdyAttInputXH.FormDestroy(Sender: TObject);
begin
  frmZdyAttInputXH := nil;
end;

procedure TfrmZdyAttInputXH.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmZdyAttInputXH.FormCreate(Sender: TObject);
begin
  Panel1.Align := alclient;
  clearControl(GroupBox1, 2);
//  canshu1 := trim(DParameters1);
end;

procedure TfrmZdyAttInputXH.TsaveClick(Sender: TObject);
begin
  if trim(DEFStr4.Text) = '' then
  begin
    application.MessageBox('花型类型不能为空！', '提示信息', MB_ICONERROR);
    exit;
  end;
  if trim(canshu1) = '高权限' then
  begin
    if trim(DJ.Text) = '' then
    begin
      application.MessageBox('单价不能为空！', '提示信息', MB_ICONERROR);
      exit;
    end;
  end;
  if trim(DEFStr4.Text) <> '印花型' then
  begin
    if trim(defnote5.Text) = '' then
    begin
      if trim(zdyName.Text) = '' then
      begin
        application.MessageBox('花型名称不能为空！', '提示信息', MB_ICONERROR);
        exit;
      end;
      if trim(defstr1.Text) = '' then
      begin
        application.MessageBox('产量不能为空！', '提示信息', MB_ICONERROR);
        exit;
      end;
      if trim(defstr2.Text) = '' then
      begin
        application.MessageBox('头距不能为空！', '提示信息', MB_ICONERROR);
        exit;
      end;
    end
    else
    begin
      if trim(defstr3.Text) = '' then
      begin
        application.MessageBox('业务员不能为空！', '提示信息', MB_ICONERROR);
        exit;
      end;
    end;
    if trim(defnote2.Text) = '' then
    begin
      application.MessageBox('客户不能为空！', '提示信息', MB_ICONERROR);
      exit;
    end;
  end
  else
  begin
    with ADOQueryTmp do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from TP_File where WBID=' + quotedstr(trim(FATID)));
      SQL.Add('and TFType=''HX''');
      Open;
    end;
    if ADOQueryTmp.IsEmpty then
    begin
      application.MessageBox('图片不能为空！', '提示信息', MB_ICONERROR);
      exit;
    end;
  end;
  if not savedata() then
  begin
    application.MessageBox('数据保存失败！', '提示信息', MB_ICONERROR);
  end
  else
  begin
    application.MessageBox('数据保存成功！', '提示信息');
    self.ModalResult := 1;
  end;
end;

procedure TfrmZdyAttInputXH.FormShow(Sender: TObject);
begin
  fFlileFlag := UserDataFlag + 'HX';
  FHXType := '';
  InitGrid();
  InitImage();
  if trim(canshu1) = '高权限' then
  begin
    Label12.Visible := true;
    DJ.Visible := true;
  end;
  if fflag = 0 then
  begin
    zdyCode.Text := '自动生成';
    if GetLSNo(ADOQueryCmd, FATID, 'HX', 'KH_Zdy_Attachment', 4, 1) = False then
    begin
      Application.MessageBox('取最大号失败！', '提示', 0);
    end;
  end
  else
  begin
    DEFStr4.Enabled := False;
    defnote2.ReadOnly := True;

 {   IF ((trim(ADOQueryMain.FieldByName('Filler').AsString)<>trim(DName)) and (Trim(DName)<>'ADMIN')) then
    begin
      zdyName.Enabled:=false;
      defnote1.Enabled:=false;
      defstr1.Enabled:=false;
      defstr2.Enabled:=false;
    end;  }
  end;
end;

procedure TfrmZdyAttInputXH.cxDBImage1DblClick(Sender: TObject);
var
  sFieldName: string;
  fileName: string;
begin
{
  IF TcxDBImage(Sender).Picture.Height=0 then exit;

  sFieldName:='D:\图片查看';

  if not DirectoryExists(pchar(sFieldName)) then
  CreateDirectory(pchar(sFieldName),nil);

  fileName:=adoqueryImage.fieldbyname(TcxDBImage(Sender).Hint).asstring;

  sFieldName:=sFieldName+'\'+trim(fileName);
  try
    IdFTP1.Host := PicSvr;
    IdFTP1.Username := 'three';
		IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    ;
  end;

  if IdFTP1.Connected then
  begin
    try
      IdFTP1.Get('TP\'+ Trim(fileName), sFieldName,false, true);
    except
      Application.MessageBox('客户图样文件不存在', '提示', MB_ICONWARNING);
      IdFTP1.Quit;
      Exit;
    end;
  end
  else
  begin
    Application.MessageBox('无法连接文件服务器', '提示', MB_ICONWARNING);
    IdFTP1.Quit;
    Exit;
  end;
   if IdFTP1.Connected then IdFTP1.Quit;
  ShellExecute(Handle, 'open',PChar(sFieldName),'', '', SW_SHOWNORMAL);    }
end;

procedure TfrmZdyAttInputXH.FileNameClick(Sender: TObject);
begin
  if trim(zdyCode.Text) = '' then
  begin
    application.MessageBox('内部花型编号不能为空！', '提示信息', MB_ICONERROR);
    exit;
  end;
  try
    FormGetPic := TFormGetPic.Create(Application);
    with FormGetPic do
    begin
      fFlileFlag := self.fFlileFlag;
      fkeyNO := trim(FATID);
      pat1 := TCXbutton(Sender).Name;
      pic1 := TCXbutton(Sender).Hint;
      FTFType := 'HX';
 //   pat1:='PatFile1';
//   pic1:='Picture1';
      if ShowModal = 1 then
      begin
     // self.Timage(Tbutton(Sender).Hint).Picture.Assign(FormGetPic.Image2.Picture.Bitmap);
        zdycode.Enabled := false;
      end;

      Release;
    end;
    Initimage();
  except
    if FormGetPic <> nil then
      FormGetPic.Release;
  end;
end;

procedure TfrmZdyAttInputXH.defnote2BtnClick(Sender: TObject);
begin
  {frmZdyAttachment_Sel:=TfrmZdyAttachment_Sel.create(self);
  with frmZdyAttachment_Sel do
  begin
    if showmodal=1 then
    begin
      IF trim(defnote2.Text)='' then
        defnote2.Text:=trim(CDS_HZ.fieldbyname('ZdyNameZ').asstring)
      else
       defnote2.Text:=trim(defnote2.Text)+';'+trim(CDS_HZ.fieldbyname('ZdyNameZ').asstring);
    end;
    free;
  end;}
  try
    frmKHListSelJJ := TfrmKHListSelJJ.Create(Application);
    with frmKHListSelJJ do
    begin
      frmKHListSelJJ.canshu2 := '高权限';
      if ShowModal = 1 then
      begin
        if trim(defnote2.Text) = '' then
          defnote2.Text := trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHNameJC').asstring)
        else
          defnote2.Text := trim(defnote2.Text) + ';' + trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHNameJC').asstring);
      end;
    end;
  finally
    frmKHListSelJJ.Free;
  end;
end;

procedure TfrmZdyAttInputXH.defnote5BtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'ZhikaDanwei';
      flagname := '制卡单位';
      if ShowModal = 1 then
      begin
        defnote5.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmZdyAttInputXH.defstr3BtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'YWY';
      flagname := '业务员';
      if ShowModal = 1 then
      begin
        defstr3.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmZdyAttInputXH.defnote6BtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'ZhikaType';
      flagname := '制卡类型';
      if ShowModal = 1 then
      begin
        defnote6.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.

