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,
  cxDropDownEdit;

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;
    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;
    Label8: TLabel;
    Priceunit: TComboBox;
    custNo: TBtnEditA;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label10: TLabel;
    huilv: TcxCurrencyEdit;
    V1Column8: TcxGridDBColumn;
    ToPlace: TEdit;
    Label11: TLabel;
    Label13: TLabel;
    V1Column13: TcxGridDBColumn;
    custCountry: TBtnEditA;
    V1Column7: TcxGridDBColumn;
    V1Column9: TcxGridDBColumn;
    V1Column10: TcxGridDBColumn;
    V1Column15: TcxGridDBColumn;
    CYID: TEdit;
    V1Column5: TcxGridDBColumn;
    V1Column6: TcxGridDBColumn;
    ToolButton3: TToolButton;
    V1Column11: TcxGridDBColumn;
    V1Column12: TcxGridDBColumn;
    V1Column14: 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 custNoBtnClick(Sender: TObject);
    procedure PriceunitChange(Sender: TObject);
    procedure V1Column8PropertiesEditValueChanged(Sender: TObject);
    procedure huilvPropertiesEditValueChanged(Sender: TObject);
    procedure custCountryBtnClick(Sender: TObject);
    procedure CYIDKeyPress(Sender: TObject; var Key: Char);
    procedure V1Column6PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure V1Column11PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    function savedata(): Boolean;
    procedure InitGrid();
    procedure InitImage();
    { Private declarations }
  public
    fFlileFlag, fkeyNo: string;
    FType: integer;
    canshu1: string;
    { Public declarations }
  end;

var
  frmZdyAttachmentBJ_Edit: TfrmZdyAttachmentBJ_Edit;

implementation

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

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

  ADOQueryCmd.Connection.BeginTrans;
  try
    if FType = 0 then
    begin
      fmxType := 'BJ' + uppercase(formatdateTime('yyyyMMdd', DServerDate));
      if GetLSNo(ADOQueryTmp, fkeyNo, fmxType, 'KH_CP_BJ', 3, 0) = 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;
        ADOQueryCmd.FieldByName('price1').Value := cds1.fieldbyname('price1').AsFloat;
        ADOQueryCmd.FieldByName('BJQty1').Value := cds1.fieldbyname('BJQty1').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 A.* 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 + '1', Tv1);
  close;
end;

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

procedure TfrmZdyAttachmentBJ_Edit.TsaveClick(Sender: TObject);
begin
//  zdyName.SetFocus;
  cxgrid1.SetFocus;
  if cds1.IsEmpty then
    exit;
  if trim(custName.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 + '1', 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
  frmCPManageSel := TfrmCPManageSel.create(self);
  with frmCPManageSel do
  begin
    Fstatus := 3;
    if showmodal = 1 then
    begin
      cds_main.DisableControls;
      cds_main.first;
      while not cds_main.Eof do
      begin
        if cds_main.FieldByName('ssel').AsBoolean then
        begin
          cds1.append;

          cds1.FieldByName('P_Code').Value := trim(cds_main.fieldbyname('CYNo').AsString);
          cds1.FieldByName('P_CodeName').Value := trim(cds_main.fieldbyname('CYName').AsString);
          cds1.FieldByName('P_CodeNameE').Value := trim(cds_main.fieldbyname('CYEName').AsString);
          cds1.FieldByName('P_spec').Value := trim(cds_main.fieldbyname('CYSpec').AsString);
          cds1.FieldByName('P_cf').Value := trim(cds_main.fieldbyname('CYCF').AsString);
          cds1.FieldByName('P_MF').Value := trim(cds_main.fieldbyname('CYMF').AsString);
          cds1.FieldByName('P_KZ').Value := trim(cds_main.fieldbyname('CYKZ').AsString);
          cds1.FieldByName('BJstr1').Value := trim(cds_main.fieldbyname('defstr18').AsString);
          cds1.FieldByName('Price').Value := cds_main.fieldbyname('price6').AsFloat;
          cds1.FieldByName('Price1').Value := cds_main.fieldbyname('price6').AsFloat;
          cds1.FieldByName('BJQty1').Value := 1;
          cds1.Post;
        end;
        cds_main.next;
      end;
      cds_main.EnableControls;
    end;
    free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmZdyAttachmentBJ_Edit.custNoBtnClick(Sender: TObject);
begin
  frmZdyAttachment := TfrmZdyAttachment.create(self);
  with frmZdyAttachment do
  begin
    if canshu1 = '业务员' then
      fywy := DName;
    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.PriceunitChange(Sender: TObject);
begin
  if trim(huilv.Text) = '' then
  begin
    huilv.Text := '1';
  end;
end;

procedure TfrmZdyAttachmentBJ_Edit.V1Column8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
{  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  FFieldName:=Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS1 do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    FieldByName('Price').Value:=format('%.2f',[fieldbyname('Price1').AsFloat * strtofloatdef(trim(huilv.Text),1)]);
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();}
end;

procedure TfrmZdyAttachmentBJ_Edit.huilvPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
{  mvalue:=TcxCurrencyEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='1';
  end;
  huilv.Text:= mvalue;
  IF not cds1.Active then exit;
  IF cds1.IsEmpty then exit;
  with cds1 do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      Edit;
      FieldByName('Price').Value:=format('%.2f',[fieldbyname('Price1').AsFloat * strtofloatdef(trim(huilv.Text),1)]);
      Post;
      next;
    end;
    EnableControls;
  end;
  tv1.Controller.EditingController.ShowEdit();    }
end;

procedure TfrmZdyAttachmentBJ_Edit.custCountryBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'custCountry';
      flagname := '国家名称';
      if ShowModal = 1 then
      begin
        custCountry.Text := Trim(ClientDataSet1.fieldbyname('zdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmZdyAttachmentBJ_Edit.CYIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from CP_YDang where CYID=''' + trim(CYID.Text) + ''' ');
      open;
    end;
    if not ADOQueryTmp.IsEmpty then
    begin
      cds1.append;
      cds1.FieldByName('P_Code').Value := trim(ADOQueryTmp.fieldbyname('CYNo').AsString);
      cds1.FieldByName('P_CodeName').Value := trim(ADOQueryTmp.fieldbyname('CYName').AsString);
      cds1.FieldByName('P_CodeNameE').Value := trim(ADOQueryTmp.fieldbyname('CYEName').AsString);
      cds1.FieldByName('P_spec').Value := trim(ADOQueryTmp.fieldbyname('CYSpec').AsString);
      cds1.FieldByName('P_cf').Value := trim(ADOQueryTmp.fieldbyname('CYCF').AsString);
      cds1.FieldByName('P_MF').Value := trim(ADOQueryTmp.fieldbyname('CYMF').AsString);
      cds1.FieldByName('P_KZ').Value := trim(ADOQueryTmp.fieldbyname('CYKZ').AsString);
      cds1.FieldByName('Price').Value := 0;
      cds1.Post;
      CYID.Text := '';
    end
    else
    begin
      application.MessageBox('此条码错误！', '提示信息', MB_ICONERROR);
      CYID.Text := '';
    end;
  end;
end;

procedure TfrmZdyAttachmentBJ_Edit.V1Column6PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxCurrencyEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '1';
  end;
  with cds1 do
  begin
    Edit;
    FieldByName('BJQty1').Value := strtofloat(mvalue);
    if FieldByName('BJQty1').AsFloat <> 0 then
      FieldByName('Price').Value := format('%.2f', [fieldbyname('Price1').AsFloat / FieldByName('BJQty1').AsFloat])
    else
      FieldByName('Price').Value := fieldbyname('Price1').AsFloat;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmZdyAttachmentBJ_Edit.ToolButton3Click(Sender: TObject);
begin
  cds1.append;
  cds1.Post;
end;

procedure TfrmZdyAttachmentBJ_Edit.V1Column11PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  FormGetPic := TFormGetPic.create(self);
  with FormGetPic do
  begin
    fFlileFlag := self.fFlileFlag;
    fkeyNo := ADOQuerymain.fieldbyname('BGFile').asstring;
    pat1 := 'FileName';
    pic1 := 'FilesOther';
    FTFType := '报价图片';
    if showmodal = 1 then
    begin
      CDS1.edit;
      CDS1.fieldbyname('BGFile').Value := trim(fkeyNo);
    end;
    free;
  end;
end;

end.

