unit U_ZdyAttachmentBJ_Edit;

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, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxDBData, cxGridLevel, cxClasses,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, DBClient, cxButtonEdit, cxGridCustomPopupMenu, cxGridPopupMenu,
  cxLookAndFeels, cxNavigator;

type
  TfrmZdyAttachmentBJ_Edit = class(TForm)
    ToolBar1: TToolBar;
    Tsave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label6: TLabel;
    custName: TEdit;
    Label17: TLabel;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    ADOQuerymain: TADOQuery;
    Label12: TLabel;
    custCountry: TEdit;
    Label39: TLabel;
    FromPlace: TEdit;
    custphone: TEdit;
    Label4: TLabel;
    Label5: TLabel;
    note: TMemo;
    Label7: TLabel;
    priceNote: TComboBox;
    Panel2: TPanel;
    Panel3: TPanel;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    TV1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    CDS1: TClientDataSet;
    Label9: TLabel;
    DS1: TDataSource;
    CRTime: TDateTimePicker;
    DlyDate: TDateTimePicker;
    V1Column1: TcxGridDBColumn;
    V1Column2: TcxGridDBColumn;
    V1Column3: TcxGridDBColumn;
    V1Column4: TcxGridDBColumn;
    V1Column5: TcxGridDBColumn;
    Label8: TLabel;
    Priceunit: TComboBox;
    V1Column6: TcxGridDBColumn;
    custNo: TBtnEditA;
    V1Column7: TcxGridDBColumn;
    cxGridPopupMenu1: TcxGridPopupMenu;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    TV1Column4: TcxGridDBColumn;
    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 MTypeBtnDnClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure V1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure custNoBtnClick(Sender: TObject);
    procedure TV1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TV1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TV1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    function savedata(): Boolean;
    procedure InitGrid();
    procedure InitImage();
    { Private declarations }
  public
    fkeyNo: string;                 
    FType: integer;
    { Public declarations }
  end;

var
  frmZdyAttachmentBJ_Edit: TfrmZdyAttachmentBJ_Edit;

implementation

uses
  U_DataLink, U_Fun10, getpic, U_ZdyAttachCP10_help, U_ZdyAttachment_Help,
  U_ZdyCMZ_Sel, U_ZdyYS_Sel;
{$R *.dfm}

function TfrmZdyAttachmentBJ_Edit.savedata(): Boolean;
var
  Filler: string;
  FillTime: TdateTime;
begin
  result := false;

  ADOQueryCmd.Connection.BeginTrans;
  try
    if FType = 0 then
    begin
      if GetLSNo(ADOQueryTmp, fkeyNo, '', 'KH_CP_BJ', 4, 1) = False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;
      Filler := DName;
      FillTime := SGetServerDateTime(adoqueryTmp);
    end
    else
    begin
      with ADOQueryTmp do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from KH_CP_BJ where BJNO=''' + Trim(fkeyNo) + '''');
        Open;
      end;
      Filler := adoqueryTmp.fieldbyname('Filler').AsString;
      FillTime := adoqueryTmp.fieldbyname('FillTime').AsDateTime;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete  from KH_CP_BJ where BJNO=''' + Trim(fkeyNo) + '''');
      execsql;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from KH_CP_BJ where BJNO=''' + Trim(fkeyNo) + '''');
      Open;
    end;
    with cds1 do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        ADOQueryCmd.Append;
        SSetsaveSqlNew(ADOQueryCmd, 'KH_CP_BJ', GroupBox1, 2);
        RTSetSaveDataCDS(ADOQueryCmd, Tv1, cds1, 'KH_CP_BJ', 0);
        ADOQueryCmd.FieldByName('BJNO').Value := trim(fkeyNO);
        ADOQueryCmd.FieldByName('BJID').Value := recNo;
        ADOQueryCmd.FieldByName('Filler').Value := Trim(Filler);
        ADOQueryCmd.FieldByName('FillTime').Value := FillTime;
        ADOQueryCmd.FieldByName('CRTime').Value := formatdateTime('yyyy-MM-dd', CRTime.DateTime);
        ADOQueryCmd.FieldByName('DlyDate').Value := formatdateTime('yyyy-MM-dd', DlyDate.DateTime);
        ADOQueryCmd.FieldByName('FillTime').Value := FillTime;
        ADOQueryCmd.FieldByName('price').Value := cds1.fieldbyname('price').AsFloat;
        if FType = 1 then
        begin
          ADOQueryCmd.FieldByName('editer').Value := trim(dName);
          ADOQueryCmd.FieldByName('editTime').Value := SGetServerDateTime(adoqueryTmp);
        end;
        ADOQueryCmd.Post;

        next;
      end;
      EnableControls;
    end;

    result := true;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmZdyAttachmentBJ_Edit.InitGrid();
begin
  with ADOQueryMain do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_CP_BJ A');
    sql.Add('where BJNO=' + quotedstr(trim(fkeyNo)));
    open;
  end;
  if not ADOQueryMain.IsEmpty then
  begin
    SCSHDataNew(ADOQueryMain, GroupBox1, 2);

  end;
  SCreateCDS20(ADOQueryMain, CDS1);
  SInitCDSData20(ADOQueryMain, CDS1);
end;

procedure TfrmZdyAttachmentBJ_Edit.InitImage();
begin
 { with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where TFid='+quotedstr(trim(zdyCode.Text)));
    open;
  end; }
end;

procedure TfrmZdyAttachmentBJ_Edit.FormDestroy(Sender: TObject);
begin
  frmZdyAttachmentBJ_Edit := nil;
end;

procedure TfrmZdyAttachmentBJ_Edit.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption, Tv1);
  close;
end;

procedure TfrmZdyAttachmentBJ_Edit.FormCreate(Sender: TObject);
begin
  Panel1.Align := alclient;
  clearControl(GroupBox1, 2);
end;

procedure TfrmZdyAttachmentBJ_Edit.TsaveClick(Sender: TObject);
begin
//  zdyName.SetFocus;
  cxgrid1.SetFocus;
  if trim(custNo.Text) = '' then
  begin
    application.MessageBox('客户编号不能为空！', '提示信息', MB_ICONERROR);
    exit;
  end;

  try
    if not savedata() then
    begin
      application.MessageBox('数据保存失败！', '提示信息', MB_ICONERROR);
    end
    else
    begin
      application.MessageBox('数据保存成功！', '提示信息');
      self.ModalResult := 1;
    end;

  except

  end;
end;

procedure TfrmZdyAttachmentBJ_Edit.FormShow(Sender: TObject);
begin
  readCxGrid(self.Caption, Tv1);
  InitGrid();
end;

procedure TfrmZdyAttachmentBJ_Edit.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 TfrmZdyAttachmentBJ_Edit.FileNameClick(Sender: TObject);
begin
  {IF trim(custNo.Text)='' then
  begin
    application.MessageBox('产品编号不能为空！','提示信息',MB_ICONERROR);
    exit;
  end;

  try
	  FormGetPic := TFormGetPic.Create(Application);
    with FormGetPic do
    begin
      fkeyNO:=trim(zdyCode.Text);
      pat1:=TCXbutton(Sender).Name;
      pic1:=TCXbutton(Sender).Hint;
      FTFType:='产品';
 //   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 TfrmZdyAttachmentBJ_Edit.MTypeBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmZdyAttachmentBJ_Edit.ToolButton2Click(Sender: TObject);
begin
  if CDS1.IsEmpty then
    exit;
  CDS1.Delete;
end;

procedure TfrmZdyAttachmentBJ_Edit.ToolButton1Click(Sender: TObject);
begin
  with cds1 do
  begin
    edit;
    post;
  end;
//  cds1.Append;
//  cds1.Post;
end;

procedure TfrmZdyAttachmentBJ_Edit.V1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmZdyAttachCP10_help := TfrmZdyAttachCP10_help.create(self);
  with frmZdyAttachCP10_help do
  begin
    if showmodal = 1 then
    begin
      cds1.Edit;
      cds1.FieldByName('P_Code').Value := trim(cds_hz.fieldbyname('zdyCode').AsString);
      cds1.FieldByName('P_CodeName').Value := trim(cds_hz.fieldbyname('zdyName').AsString);
      cds1.FieldByName('P_Spec').Value := trim(cds_hz.fieldbyname('defstr1').AsString);
      cds1.Post;
    end;
    free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmZdyAttachmentBJ_Edit.custNoBtnClick(Sender: TObject);
begin
  frmZdyAttachment_Help := TfrmZdyAttachment_Help.create(self);
  with frmZdyAttachment_Help do
  begin
    if showmodal = 1 then
    begin
      custNo.Text := trim(cds_hz.fieldbyname('zdyCode').AsString);
      custName.Text := trim(cds_hz.fieldbyname('zdyName').AsString);
      custCountry.Text := trim(cds_hz.fieldbyname('DEFStr7').AsString);
      custphone.Text := trim(cds_hz.fieldbyname('DEFstr1').AsString);
    end;
    free;
  end;
end;

procedure TfrmZdyAttachmentBJ_Edit.TV1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmZdyCMZ_sel := TfrmZdyCMZ_sel.create(self);
  with frmZdyCMZ_sel do
  begin
    if showmodal = 1 then
    begin
      with CDS1 do
      begin
        Edit;
        FieldByName('P_chima').value := CDS_HZ.fieldbyname('CodeName').AsString;
        Post;
      end;
    end;
    free;
  end;
end;

procedure TfrmZdyAttachmentBJ_Edit.TV1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmZdyYS_sel := TfrmZdyYS_sel.create(self);
  with frmZdyYS_sel do
  begin
    if showmodal = 1 then
    begin
      with CDS1 do
      begin
        Edit;
        FieldByName('P_yanse').value := CDS_HZ.fieldbyname('CodeName').AsString;
        Post;
      end;
    end;
    free;
  end;
end;

procedure TfrmZdyAttachmentBJ_Edit.TV1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  FormGetPic := TFormGetPic.create(self);
  with FormGetPic do
  begin
//    fFlileFlag:=self.fFlileFlag;
//    fFlileFlag := UserDataFlag + '产品';
    fkeyNo := CDS1.fieldbyname('HXPic').asstring;
    pat1 := 'FileName';
    pic1 := 'FilesOther';
    FTFType := '报价';
    if showmodal = 1 then
    begin
      Self.cds1.edit;
      Self.cds1.fieldbyname('HXPic').Value := trim(fkeyNo);
    end;
    free;
  end;
end;

end.

