unit U_ZdyAttInputCP10;

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, cxGraphics, cxLookAndFeels,
  cxStyles, cxCustomData, cxFilter, cxData, cxDataStorage, cxNavigator, cxDBData,
  cxButtonEdit, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxGridCustomView, cxGrid, DBClient, dxSkinsCore,
  dxSkinBlack, dxSkinBlue, dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee,
  dxSkinDarkRoom, dxSkinDarkSide, dxSkinDevExpressDarkStyle,
  dxSkinDevExpressStyle, dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast,
  dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky,
  dxSkinMcSkin, dxSkinMetropolis, dxSkinMetropolisDark, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus, dxSkinSilver,
  dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld,
  dxSkinsDefaultPainters, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue, dxSkinscxPCPainter;

type
  TfrmZdyAttInputCP10 = class(TForm)
    ToolBar1: TToolBar;
    Tsave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    zdyCode: TEdit;
    Label2: TLabel;
    zdyName: TEdit;
    Label3: TLabel;
    Label6: TLabel;
    defstr1: 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;
    Label12: TLabel;
    defstr2: TEdit;
    Label39: TLabel;
    defnote2: TEdit;
    defstr8: TEdit;
    Label4: TLabel;
    defstr7: TEdit;
    Label5: TLabel;
    note: TMemo;
    Label7: TLabel;
    defstr5: TComboBox;
    Label8: TLabel;
    defflt3: TcxCurrencyEdit;
    Label9: TLabel;
    defnote3: TBtnEditA;
    Label10: TLabel;
    defflt4: TcxCurrencyEdit;
    Label11: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    sprice: TEdit;
    tprice: TEdit;
    Label16: TLabel;
    CYCM: TBtnEditA;
    Label18: TLabel;
    cycolor: TBtnEditA;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton7: TToolButton;
    ToolButton9: TToolButton;
    Label19: TLabel;
    CDS_Sub: TClientDataSet;
    DataSource2: TDataSource;
    ADOQuerySub: TADOQuery;
    Tv2Column1: TcxGridDBColumn;
    Picture6: TcxDBImage;
    Label20: TLabel;
    ADOQueryImageYS: TADOQuery;
    DSImage: TDataSource;
    Label21: TLabel;
    Label22: TLabel;
    Label23: TLabel;
    Label24: TLabel;
    DZprice: TEdit;
    Label25: TLabel;
    seprice: TEdit;
    fprice: TEdit;
    Label26: TLabel;
    CPtxm: TEdit;
    Tv2Column2: TcxGridDBColumn;
    CYColorno: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TsaveClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Picture6DblClick(Sender: TObject);
    procedure FileNameClick(Sender: TObject);
    procedure MTypeBtnDnClick(Sender: TObject);
    procedure defnote3BtnClick(Sender: TObject);
    procedure CYCMBtnClick(Sender: TObject);
    procedure cycolorBtnClick(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure Tv2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton9Click(Sender: TObject);
  private
    function savedata(): Boolean;
    procedure InitGrid();
    procedure InitImage();
    procedure InitImageYS(fwbid: string);
    { Private declarations }
  public
    PState, CopyInt: Integer;
    FATID, fFlileFlag: string;
    { Public declarations }
  end;

var
  frmZdyAttInputCP10: TfrmZdyAttInputCP10;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, getpic, getpicYS, U_ZdyAttachGYS_help,
  U_ZdyCMZ_Sel, U_ZdyYS_Sel;
{$R *.dfm}

function TfrmZdyAttInputCP10.savedata(): Boolean;
var
  maxno: string;
begin
  result := false;
  try
    if trim(FATID) = '' then
    begin
      if GetLSNo(ADOQueryCmd, FATID, 'AT', 'KH_Zdy_Attachment', 4, 1) = False then
      begin
        Result := False;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;
    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('defflt3').Value := strtofloatdef(Trim(defflt3.text), 1);
      FieldByName('defflt4').Value := strtofloatdef(Trim(defflt4.text), 0);
      FieldByName('ATID').Value := Trim(FATID);
      FieldByName('Type').Value := 'CP';
      post;
    end;

    result := true;
  except
  end;
  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('Update KH_Zdy_CPCB set zdyCode=''' + Trim(zdyCode.Text) + ''' where  ATID=''' + Trim(FATID) + ''' ');
//    ShowMessage(SQL.text);
    ExecSQL;
  end;

  with CDS_Sub do  //保存颜色
  begin
    First;
    while not Eof do
    begin
      if Trim(CDS_Sub.fieldbyname('YSId').AsString) = '' then
      begin
        if GetLSNo(ADOQueryCmd, maxno, 'YS', 'KH_YS', 4, 1) = False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取子流水号失败！', '提示', 0);
          Exit;
        end;
      end
      else
      begin
        maxno := Trim(CDS_Sub.fieldbyname('YSId').AsString);
      end;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from KH_YS where ATID=''' + Trim(FATID) + '''');
        sql.Add(' and YSId=''' + Trim(maxno) + '''');
        Open;
      end;
      with ADOQueryCmd do
      begin
        if Trim(CDS_Sub.fieldbyname('YSId').AsString) = '' then
        begin
          Append;
        end
        else
          Edit;
        FieldByName('ATID').Value := Trim(FATID);
        FieldByName('YSId').Value := Trim(maxno);
        FieldByName('code').Value := Trim(zdyCode.text);
        RTSetSaveDataCDS(ADOQueryCmd, Tv2, CDS_Sub, 'KH_YS', 0);
//          fieldbyname('SordQty1').Value := CDS_Sub.fieldbyname('SordQty1').AsFloat;
        FieldByName('cyys').Value := CDS_Sub.FieldByName('cyys').AsString;

        Post;
      end;
      CDS_Sub.Edit;
      CDS_Sub.FieldByName('YSId').Value := Trim(maxno);
      CDS_Sub.FieldByName('ATID').Value := Trim(FATID);
      Next;
    end;
  end;

end;

procedure TfrmZdyAttInputCP10.InitGrid();
begin
  with ADOQueryMain do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_Zdy_Attachment A');
    if PState = 1 then
    begin
      sql.Add('where ATID=' + quotedstr(trim(FATID)));
    end;
    if PState = 0 then
    begin
      sql.Add(' where 1<>1');
    end;
//    showmessage(sql.text);
    open;
  end;

  if not ADOQueryMain.IsEmpty then
  begin
//    zdyCode.Enabled := false;
    SCSHDataNew(ADOQueryMain, GroupBox1, 2);
  end;
  with ADOQuerySub do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_YS A');
    if PState = 1 then
    begin
      sql.Add('where ATID=' + quotedstr(trim(FATID)));
    end;
    if PState = 0 then
    begin
      sql.Add(' where 1<>1');
    end;
//    showmessage(sql.text);
    open;
  end;
//    if not ADOQuerySub.IsEmpty then
//  if ADOQuerySub.IsEmpty then
//  begin
  SCreateCDS20(ADOQuerySub, CDS_Sub);
  SInitCDSData20(ADOQuerySub, CDS_Sub);
//  end;
//   if PState = 1 then
//  begin
//    zdyCode.text := '';
//  end;
end;

procedure TfrmZdyAttInputCP10.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 TfrmZdyAttInputCP10.InitImageYS(fwbid: string);
begin
  ADOQueryImageYS.close;
  if fwbid = '' then
    exit;
  with ADOQueryImageYS do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where TFid=''' + CDS_Sub.fieldbyname('YSFile').asstring + '''');
    open;
  end;
end;

procedure TfrmZdyAttInputCP10.FormDestroy(Sender: TObject);
begin
  frmZdyAttInputCP10 := nil;
end;

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

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

procedure TfrmZdyAttInputCP10.TsaveClick(Sender: TObject);
begin
  zdyName.SetFocus;
  if trim(zdyCode.Text) = '' then
  begin
    application.MessageBox('产品编号不能为空！', '提示信息', MB_ICONERROR);
    exit;
  end;

  with adoqueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_Zdy_Attachment X');
    sql.Add('where zdyCode=' + quotedstr(trim(zdyCode.Text)));
    sql.Add('and ATID<>' + quotedstr(trim(FATID)));
    sql.Add('and Type=''CP'' ');
    open;
    if not IsEmpty then
    begin
      application.MessageBox('产品编号重复！', '提示信息', MB_ICONERROR);
      exit;
    end;
  end;

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

  except
    AdoqueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmZdyAttInputCP10.FormShow(Sender: TObject);
var
  fzdyCode: string;
begin
  InitGrid();

  if CopyInt = 99 then
  begin
    FATId := '';
    zdycode.text := '';
    with CDS_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('ATID').Value := '';
        FieldByName('YSId').Value := '';
        FieldByName('YSFile').Value := '';

        Post;
        Next;
      end;
    end;
//    with ADOQueryMain do
//    begin
//      First;
//      while not Eof do
//      begin
//        Edit;
//        FieldByName('ATID').Value := '';
//        Post;
//        Next;
//      end;
//    end;
  end;
  InitImage();
end;

procedure TfrmZdyAttInputCP10.Picture6DblClick(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 TfrmZdyAttInputCP10.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
      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 TfrmZdyAttInputCP10.MTypeBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmZdyAttInputCP10.defnote3BtnClick(Sender: TObject);
begin
  frmZdyAttachGYS_help := TfrmZdyAttachGYS_help.create(self);
  with frmZdyAttachGYS_help do
  begin
    if showmodal = 1 then
    begin
      defnote3.Text := trim(cds_hz.fieldbyname('zdyName').asstring);
    end;
    free;
  end;
end;

procedure TfrmZdyAttInputCP10.CYCMBtnClick(Sender: TObject);
begin
  frmZdyCMZ_sel := TfrmZdyCMZ_sel.create(self);
  with frmZdyCMZ_sel do
  begin
    if showmodal = 1 then
    begin

      CYCM.text := CDS_HZ.fieldbyname('CodeName').AsString;

    end;
    free;
  end;
end;

procedure TfrmZdyAttInputCP10.cycolorBtnClick(Sender: TObject);
begin
  frmZdyYS_sel := TfrmZdyYS_sel.create(self);
  with frmZdyYS_sel do
  begin
    if showmodal = 1 then
    begin

      cycolor.text := CDS_HZ.fieldbyname('CodeName').AsString;
      CYColorno.text := trim(CDS_HZ.fieldbyname('Colorno').AsString);
    end;
    free;
  end;
end;

procedure TfrmZdyAttInputCP10.ToolButton7Click(Sender: TObject);
begin
//  CopyAddRow(Tv2, CDS_Sub);
//  with CDS_Sub do
//  begin
//    Edit;
//    FieldByName('cyys').Value := '';
//    FieldByName('YSFile').Value := '';
//    Post;
//  end;
  frmZdyYS_sel := TfrmZdyYS_sel.create(self);
  with frmZdyYS_sel do
  begin
//    if ShowModal = 1 then
//    begin
//      Self.CDS_Sub.Edit;
//      Self.CDS_Sub.FieldByName('cyys').Value := trim(CDS_HZ.fieldbyname('CodeName').AsString);
////        Self.Order_Sub.FieldByName('SOrddefstr4').Value := Trim(ClientDataSet1.fieldbyname('Note').AsString);
//    end;
    if showmodal = 1 then
    begin
      CDS_HZ.First;
      while not CDS_HZ.Eof do
      begin
        if CDS_HZ.fieldbyname('ssel').AsBoolean then
        begin
//          Self.CDS_Sub.Edit;
//          Self.CDS_Sub.FieldByName('cyys').Value := trim(CDS_HZ.fieldbyname('CodeName').AsString);
          CDS_Sub.Append;
          CDS_Sub.FieldByName('cyys').Value := trim(CDS_HZ.fieldbyname('CodeName').AsString);
          CDS_Sub.post;
        end;
        CDS_HZ.Next;
      end;
      tv2.Controller.EditingController.ShowEdit();
      free;
    end;
  end;
end;

procedure TfrmZdyAttInputCP10.Tv2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  FormGetPicYS := TFormGetPicYS.create(self);
  with FormGetPicYS do
  begin
    fFlileFlag := self.fFlileFlag;
    fkeyNo := CDS_Sub.fieldbyname('YSFile').asstring;
    fkeyNo1 := CDS_Sub.fieldbyname('YSid').asstring;
    pat1 := 'FileName';
    pic1 := 'FilesOther';
    FTFType := 'YS';
    if showmodal = 1 then
    begin
      CDS_Sub.edit;
      CDS_Sub.fieldbyname('YSFile').Value := trim(fkeyNo);
    end;
    free;
  end;
end;

procedure TfrmZdyAttInputCP10.Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  initImageYS(CDS_Sub.fieldbyname('YSFile').AsString);
end;

procedure TfrmZdyAttInputCP10.ToolButton9Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;

  if Trim(CDS_Sub.fieldbyname('YSId').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
//    with ADOCmd do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('delete TP_File where WBID=''' + Trim(CDS_HZ.fieldbyname('SubId').AsString) + ''' and TFType=''EWM'' ');
//      ExecSQL;
//    end;
    with ADOQueryTmp do
    begin
      Close;
      sql.Clear;
      sql.Add('delete KH_YS where YSId=''' + Trim(CDS_Sub.fieldbyname('YSId').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_Sub.Delete;
end;

end.

