unit U_BPZdy_LRXH_M;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, ToolWin, ComCtrls,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGridLevel,
  cxClasses, cxControls, cxGridCustomView, cxGrid, DBClient, ADODB, ImgList,
  StdCtrls, ExtCtrls, cxTextEdit, cxGridCustomPopupMenu, cxGridPopupMenu,
  cxButtonEdit, BtnEdit, cxTL, cxMaskEdit, cxInplaceContainer, cxDBTL,Math, Menus,
  cxTLData, cxContainer, cxCurrencyEdit, cxCheckBox, cxSplitter,jpeg,ShellAPI,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP,IniFiles;

type
  TfrmBPZDY_XHPB_M = class(TForm)
    ToolBar1: TToolBar;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    TBClose: TToolButton;
    ADOConnection1: TADOConnection;
    ThreeImgList: TImageList;
    DataSource2: TDataSource;
    ML_GYS: TClientDataSet;
    DataSource3: TDataSource;
    ML_GX: TClientDataSet;
    ADOQuery6: TADOQuery;
    ADOQuery2: TADOQuery;
    ADOQuery3: TADOQuery;
    Panel4: TPanel;
    Panel5: TPanel;
    Label6: TLabel;
    Label7: TLabel;
    MXName: TEdit;
    Label10: TLabel;
    MBPGYSName: TEdit;
    Button1: TButton;
    DataSource4: TDataSource;
    ADOQueryTree: TADOQuery;
    Order_Tree: TClientDataSet;
    Panel6: TPanel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    V1BPCode: TcxGridDBColumn;
    V1BPName: TcxGridDBColumn;
    v1MBPGYSName: TcxGridDBColumn;
    V1BPID: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolBar2: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    ToolButton1: TToolButton;
    Panel7: TPanel;
    cxDBTreeList1: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    ToolBar3: TToolBar;
    ToolPBLE: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle2: TcxStyle;
    MXCode: TEdit;
    Label13: TLabel;
    MNo: TEdit;
    V1MNo: TcxGridDBColumn;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    BPNameM: TEdit;
    BPCodeM: TEdit;
    V1BPNote: TcxGridDBColumn;
    Label14: TLabel;
    BPNote: TEdit;
    V1BPPrice: TcxGridDBColumn;
    Label15: TLabel;
    BPPrice: TEdit;
    Label16: TLabel;
    V1BPNameQC: TcxGridDBColumn;
    Label3: TLabel;
    BPNameQC: TEdit;
    V1BPHXST: TcxGridDBColumn;
    ToolButton2: TToolButton;
    Label4: TLabel;
    BPHXST: TEdit;
    VIFSC: TcxGridDBColumn;
    V1YHType: TcxGridDBColumn;
    Label5: TLabel;
    YHType: TEdit;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    SaveDialog1: TSaveDialog;
    Image2: TImage;
    TXZTP: TToolButton;
    Panel16: TPanel;
    ADOQueryFile: TADOQuery;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBAddClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TV1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure BPNamehange(Sender: TObject);
    procedure V1NotePropertiesEditValueChanged(Sender: TObject);
    procedure V1HelpTypePropertiesEditValueChanged(Sender: TObject);
    procedure v1note1PropertiesEditValueChanged(Sender: TObject);
    procedure V1BPCodePropertiesEditValueChanged(Sender: TObject);
    procedure V1BPNamePropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure BPCodeMChange(Sender: TObject);
    procedure Tv3CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Button1Click(Sender: TObject);
    procedure TPBLBClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxDBTreeList1Click(Sender: TObject);
    procedure MXCodeKeyPress(Sender: TObject; var Key: Char);
    procedure CYNoKeyPress(Sender: TObject; var Key: Char);
    procedure MNoKeyPress(Sender: TObject; var Key: Char);
    procedure FormDestroy(Sender: TObject);
    procedure TV1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure BPPriceKeyPress(Sender: TObject; var Key: Char);
    procedure BPNoteKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton2Click(Sender: TObject);
    procedure TXZTPClick(Sender: TObject);
  private
    FBPID,FCPID,FCPNo,FCPName:string;
    lstPat: TStringList;
		AJpeg: TJPEGImage;
    procedure InitGrid();
    procedure SaveData();
    procedure initTree();
    procedure ReadINIFile10();
    procedure CreThumb(AJPeg:TJPEGImage;Image1:TImage;Width, Height: Integer);
    { Private declarations }
  public
    flag,flagname,snote,MainType,FGYName,Fint,canshu1:string;
    fnote,fnote1,forderno,fZdyFlag,ViewFlag,fHelpType:Boolean;
    PPSTE:integer;
    { Public declarations }
  end;
var
  frmBPZDY_XHPB_M: TfrmBPZDY_XHPB_M;

implementation
uses
  U_DataLink,U_Fun10,U_BPZdy_SH, U_BPZdy_HXK,U_CPTypePB,U_CPTypePB_M,U_FileUp_TPSH,U_CPTypeXH_M;

{$R *.dfm}
procedure TfrmBPZDY_XHPB_M.initTree();
begin
  with ADOQueryTree do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select *');
    sql.Add(', Case when isnull(CPNo,'''')<>'''' and CPlevel=2 then RTrim(CPNo)+CPName else CPName end as CP ');
    SQL.Add('from CP_TypeXH_M order by CPlevel,CPOrder,CPName');
    Open;
  end;
  SCreateCDS20(ADOQueryTree,Order_Tree);
  SInitCDSData20(ADOQueryTree,Order_Tree);
  cxDBTreeList1.Items[0].Expand(true);
end;

procedure TfrmBPZDY_XHPB_M.SaveData();
var maxno:string;
begin
  if FGYName<>'' then exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      First;
      while not eof do
      begin
        if Trim(ClientDataSet1.FieldByName('BPID').AsString)='' then
        begin
          if GetLSNo(ADOQueryTemp,maxno,'BP','BP_ZDY_XH',3,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            ClientDataSet1.EnableControls;
            Application.MessageBox('取最大编号失败！','提示',0);
            Exit;
          end;
        end
        else
        begin
          maxno:=Trim(ClientDataSet1.fieldbyname('BPID').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from BP_ZDY_XH where BPID='''+Trim(maxno)+'''');
          Open;
        end;
        if ADOQueryCmd.IsEmpty then
        begin
          ADOQueryCmd.Append;
        end
        else
        begin
          ADOQueryCmd.Edit;
        end;
        ADOQueryCmd.FieldByName('BPID').Value:=Trim(maxno);
        SSetSaveDataCDSNew(ADOQueryCmd,Tv1,ClientDataSet1,'BP_ZDY_XH',3);
        ADOQueryCmd.FieldByName('BPType').Value:=flagName;
        ADOQueryCmd.FieldByName('BPFlag').Value:=flag;
        ADOQueryCmd.FieldByName('valid').Value:='Y';
        ADOQueryCmd.Post;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.add('select * from BP_ZDY_XH where BPCode='''+Trim(ClientDataSet1.fieldbyname('BPCode').AsString)+'''');
          sql.Add(' and BPFlag='''+trim(flag)+'''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty=False then
        begin
          if ADOQueryTemp.RecordCount>1 then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('delete BP_ZDY_XH where BPID='''+Trim(ClientDataSet1.fieldbyname('BPID').AsString)+'''');
              ExecSQL;
            end;
            ADOQueryCmd.Connection.RollbackTrans;
            ClientDataSet1.EnableControls;
            Application.MessageBox('编号重复！','提示',0);
            Exit;
          end;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.add('select * from BP_ZDY_XH where BPName='''+Trim(ClientDataSet1.fieldbyname('BPName').AsString)+'''');
          sql.Add(' and BPFlag='''+trim(flag)+'''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty=False then
        begin
          if ADOQueryTemp.RecordCount>1 then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('delete BP_ZDY_XH where BPID='''+Trim(ClientDataSet1.fieldbyname('BPID').AsString)+'''');
              ExecSQL;
            end;
            ADOQueryCmd.Connection.RollbackTrans;
            ClientDataSet1.EnableControls;
            Application.MessageBox('简称重复！','提示',0);
            Exit;
          end;
        end;
        ClientDataSet1.Edit;
        ClientDataSet1.FieldByName('BPID').Value:=Trim(maxno);
        //ClientDataSet1.Post;
        Next;
      end;
      ClientDataSet1.EnableControls;
    end;
    TV1.OptionsData.Editing:=false;
    TV1.OptionsSelection.CellSelect:=false;
    ADOQueryCmd.Connection.CommitTrans;
  except;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmBPZDY_XHPB_M.FormCreate(Sender: TObject);
begin
  try
    //cxGrid1.Align:=alClient;
    with ADOConnection1 do
    begin
      Connected:=false;
      ConnectionString:=DConString;
      //ConnectionString:='';
      Connected:=true;
    end;
  except;
    frmBPZDY_XHPB_M.Free;
  end;
  lstPat := TStringList.Create;
  canshu1:=Trim(DParameters1);
end;

procedure TfrmBPZDY_XHPB_M.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  ToolBar1.SetFocus;
  Action:=caFree;
end;

procedure TfrmBPZDY_XHPB_M.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Select_XH_M :Code,:PState,:CYType,:Stats ');
      Parameters.ParamByName('Code').Value:='';
      Parameters.ParamByName('PState').Value:=0;
      Parameters.ParamByName('CYType').Value:=Trim(Order_Tree.fieldbyname('CPID').AsString);
      Parameters.ParamByName('Stats').Value:='';
      Open;
    end;
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmBPZDY_XHPB_M.TBAddClick(Sender: TObject);
var
  i:Integer;
begin
  if Order_Tree.FieldByName('CPLevel').AsInteger<3 then
  begin
    Panel5.Visible:=False;
    Application.MessageBox('没有选择类型，不能新增!','提示',0);
    Exit;
  end else
  begin
    Panel5.Visible:=True;
    SClearData(Panel5,2);
    FBPID:='';
    MXCode.SetFocus;
    Button1.Caption:='新增保存';
  end;
end;

procedure TfrmBPZDY_XHPB_M.TBSaveClick(Sender: TObject);
var
  maxno:string;
begin
  if ClientDataSet1.IsEmpty then Exit;
  Toolbar1.SetFocus;
  if ClientDataSet1.Locate('BPCode',null,[]) then
  begin
    Application.MessageBox('编号不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet1.Locate('BPCode','',[]) then
  begin
    Application.MessageBox('编号不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet1.Locate('BPName',null,[]) then
  begin
    Application.MessageBox('简称不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet1.Locate('BPName','',[]) then
  begin
    Application.MessageBox('简称不能为空！','提示',0);
    Exit;
  end;
  SaveData();
  Application.MessageBox('保存成功！','提示',0);
end;

procedure TfrmBPZDY_XHPB_M.TBDelClick(Sender: TObject);
begin
  MXCode.SetFocus;
  if ClientDataSet1.IsEmpty then Exit;
  if Trim(ClientDataSet1.FieldByName('BPID').AsString)<>'' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from ML_Manage_PB where isnull(BPID,'''')='''+Trim(FBPID)+'''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty=False then
    begin
      Application.MessageBox('已生成面料信息，不能删除数据!','提示',0);
      Exit;
    end;
    if application.MessageBox('确定要删除吗?','提示信息',1)=2 then exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete BP_ZDY_XH where BPID='''+Trim(ClientDataSet1.fieldbyname('BPID').AsString)+'''');
      ExecSQL;
    end;
  end;
  ClientDataSet1.Delete;
  SClearData(Panel5,2);
  FBPID:='';
end;

procedure TfrmBPZDY_XHPB_M.TBCloseClick(Sender: TObject);
begin
  panel1.SetFocus;
  WriteCxGrid('自定义'+Trim(flag)+'1YX1',TV1,'自定义数据');
  Close;
end;

procedure TfrmBPZDY_XHPB_M.FormShow(Sender: TObject);
begin
  initTree();
  InitGrid();
  ReadCxGrid('自定义'+Trim(flag)+'1YX1',TV1,'自定义数据');
  if ViewFlag=True then
  begin
    TBAdd.Visible:=False;
    TBDel.Visible:=False;
    ToolButton1.Visible:=true;
  end;
  if canshu1='查询' then
  begin
    ToolBar3.Visible:=False;
    ToolBar2.Visible:=false;
    Button1.Visible:=false;
    Panel4.Align:=alClient;
    V1BPNote.Visible:=False;
    V1BPPrice.Visible:=false;
    Label15.Visible:=false;
    BPPrice.Visible:=false;
    Label14.Visible:=false;
    BPNote.Visible:=false;
  end
  else
  begin
    ToolBar3.Visible:=true;
    ToolBar2.Visible:=true;
    Button1.Visible:=true;
    Panel4.Align:=alLeft;
    V1BPNote.Visible:=true;
    V1BPPrice.Visible:=true;
    Label15.Visible:=true;
    BPPrice.Visible:=true;
    Label14.Visible:=true;
    BPNote.Visible:=true;
  end;
end;

procedure TfrmBPZDY_XHPB_M.TV1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var i:integer;
begin
  if ToolButton1.Visible=true then
  begin
    Fint:='1';
    ModalResult:=1;
  end else
  begin
    ToolBar1.SetFocus;
    if ClientDataSet1.IsEmpty then exit;
    MBPGYSName.Text:=Trim(ClientDataSet1.fieldbyname('MBPGYSName').AsString);
    MNo.Text:=Trim(ClientDataSet1.fieldbyname('MNo').AsString);
    YHType.Text:=Trim(ClientDataSet1.fieldbyname('YHType').AsString);
    BPHXST.Text:=Trim(ClientDataSet1.fieldbyname('BPHXST').AsString);
    MXCode.Text:=Trim(ClientDataSet1.FieldByName('MXCode').AsString);
    MXName.Text:=Trim(ClientDataSet1.FieldByName('MXName').AsString);
    BPNameQC.Text:=Trim(ClientDataSet1.FieldByName('BPNameQC').AsString);
    BPPrice.Text:=Trim(ClientDataSet1.FieldByName('BPPrice').AsString);
    BPNote.Text:=Trim(ClientDataSet1.FieldByName('BPNote').AsString);
    FBPID:=Trim(ClientDataSet1.fieldbyname('BPID').AsString);
    FCPID:=Trim(ClientDataSet1.fieldbyname('CPID').AsString);
    FCPNo:=Trim(ClientDataSet1.fieldbyname('CPNo').AsString);
    FCPName:=Trim(ClientDataSet1.fieldbyname('CPName').AsString);
    Panel5.Visible:=True;
    Button1.Caption:='修改保存';
    TBDel.Visible:=True;
  end;
end;

procedure TfrmBPZDY_XHPB_M.BPNamehange(Sender: TObject);
var
  fsj:String;
begin
  if Trim(BPNameM.Text)<>'' then
  begin
    fsj:=' BPName like '''+'%'+Trim(BPNameM.Text)+'%'+'''';
  end;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,fsj);
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
  end;
end;

procedure TfrmBPZDY_XHPB_M.V1NotePropertiesEditValueChanged(Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('BPKZ').Value:=mvalue;
    //Post;
  end;
end;

procedure TfrmBPZDY_XHPB_M.V1HelpTypePropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('BPWS').Value:=mvalue;
    Post;
  end;
  {with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update KH_Zdy Set HelpType='''+Trim(mvalue)+'''');
    sql.Add(' where ZdyNo='''+Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString)+'''');
    ExecSQL;
  end;}
end;

procedure TfrmBPZDY_XHPB_M.v1note1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('BPMiDu').Value:=mvalue;
    Post;
  end;
end;

procedure TfrmBPZDY_XHPB_M.V1BPCodePropertiesEditValueChanged(Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('BPCode').Value:=mvalue;
  end;
end;

procedure TfrmBPZDY_XHPB_M.V1BPNamePropertiesEditValueChanged(Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('BPName').Value:=mvalue;
  end;
end;

procedure TfrmBPZDY_XHPB_M.ToolButton4Click(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if ClientDataSet1.IsEmpty then exit;
  if ClientDataSet1.FieldByName('BPID').AsString='' then
  begin
    application.MessageBox('请保存主信息','提示');
    exit;
  end;
  with ML_GYS do
  begin
    Append;
    post;
  end;
end;

procedure TfrmBPZDY_XHPB_M.BPCodeMChange(Sender: TObject);
var
  fsj:String;
begin
  if Trim(BPCodeM.Text)<>'' then
  begin
    fsj:=' BPCode like '''+'%'+Trim(BPCodeM.Text)+'%'+'''';
  end;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,fsj);
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
  end;
end;

procedure TfrmBPZDY_XHPB_M.Tv3CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var i:Integer;
begin
  if ML_GX.IsEmpty then exit;
  ML_GX.Delete;
  i:=1;
  ML_GX.DisableControls;
  with ML_GX do
  begin
    first;
    while not eof do
    begin
      Edit;
      FieldByName('BPMiDu').Value:=i;
      i:=i+1;
      next;
    end;
  end;
  ML_GX.EnableControls;
end;

procedure TfrmBPZDY_XHPB_M.Button1Click(Sender: TObject);
var
  maxno,FCPMiDu:string;
  FInt,GS:Integer;
  FReal:Double;
begin
  if Button1.Caption='修改保存' then
  begin
    if Application.MessageBox('确定要修改保存吗','提示',1)=2 then exit;
  end;
  if Button1.Caption='新增保存' then
  begin
    if Label16.Caption='' then
    begin
      application.MessageBox('没有选择类型','提示');
      exit;
    end;
  end;
  if MXCode.Text='' then
  begin
    application.MessageBox('代码不能为空','提示');
    exit;
  end;
  if Length(MXCode.Text)>4 then
  begin
    application.MessageBox('代码不能超过4位数','提示');
    exit;
  end;
  if TryStrToInt(MXCode.Text,FInt)=False then
  begin
    Application.MessageBox('代码非法数字!','提示',0);
    Exit;
  end;
  if Trim(BPPrice.Text)<>'' then
  begin
    if TryStrToFloat(BPPrice.Text,FReal)=False then
    begin
      Application.MessageBox('单价非法数字!','提示',0);
      Exit;
    end;
  end;
  {if Trim(MNo.Text)<>'' then
  begin
    Application.MessageBox('膜编号不能为空!','提示',0);
    Exit;
  end;}
  if Trim(Button1.Caption)<>'修改保存' then
  begin
    if Order_Tree.FieldByName('CPLevel').AsInteger=1 then
    begin
      application.MessageBox('不能在主类里新增','提示');
      exit;
    end;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if FBPID='' then
    begin
      if GetLSNo(ADOQueryTemp,FBPID,'BP','BP_ZDY_XH',3,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大编号失败！','提示',0);
        Exit;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from BP_ZDY_XH where BPID='''+Trim(FBPID)+'''');
      Open;
    end;
    if ADOQueryCmd.IsEmpty then
    begin
      ADOQueryCmd.Append;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.add('select count(*) GS from BP_ZDY_XH where MNo='''+Trim(MNo.Text)+'''');
        sql.Add(' and BPFlag='''+trim(flag)+'''');
        Open;
      end;
      GS:=ADOQueryTemp.fieldbyname('GS').AsInteger;
    end
    else
    begin
      ADOQueryCmd.Edit;
    end;
    ADOQueryCmd.FieldByName('BPID').Value:=Trim(FBPID);
    RTSetsavedata(ADOQueryCmd,'BP_ZDY_XH',panel5,2);
    ADOQueryCmd.FieldByName('BPType').Value:=flagName;
    ADOQueryCmd.FieldByName('BPFlag').Value:=flag;
    ADOQueryCmd.FieldByName('valid').Value:='Y';
    ADOQueryCmd.FieldByName('CPID').Value:=trim(FCPID);
    ADOQueryCmd.FieldByName('CPNo').Value:=trim(FCPNo);
    ADOQueryCmd.FieldByName('CPName').Value:=trim(FCPName);
    if Trim(BPPrice.Text)<>'' then
      ADOQueryCmd.FieldByName('BPPrice').Value:=BPPrice.Text
    else
      ADOQueryCmd.FieldByName('BPPrice').Value:=Null;
    ADOQueryCmd.FieldByName('BPNote').Value:=Trim(BPNote.Text);
    if GS<>0 then
    ADOQueryCmd.FieldByName('BPCode').Value:=trim(FCPNo)+trim(MXCode.Text)+'-'+IntToStr(GS)
    else
    ADOQueryCmd.FieldByName('BPCode').Value:=trim(FCPNo)+trim(MXCode.Text);
    ADOQueryCmd.FieldByName('BPName').Value:=trim(MXName.Text);
    ADOQueryCmd.Post;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('select * from BP_ZDY_XH where BPCode='''+Trim(trim(FCPNo)+trim(MXCode.Text))+'''');
      sql.Add(' and BPFlag='''+trim(flag)+'''');
      Open;
    end;
    if ADOQueryTemp.RecordCount>1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('编号重复！','提示',0);
      Exit;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('select * from BP_ZDY_XH where MNo+BPHXST+YHType='''+Trim(trim(MNo.Text)+trim(BPHXST.Text)+trim(YHType.Text))+'''');
      sql.Add(' and BPFlag='''+trim(flag)+'''');
      Open;
    end;
    if ADOQueryTemp.RecordCount>1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('绣花厂花型编号+头距+框架重复！','提示',0);
      Exit;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('select * from BP_ZDY_XH where BPName='''+Trim(trim(FCPName)+trim(MXName.Text))+'''');
      sql.Add(' and BPFlag='''+trim(flag)+'''');
      Open;
    end;
    if ADOQueryTemp.RecordCount>1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('信息重复！','提示',0);
      Exit;
    end;
    with ClientDataSet1 do
    begin
      if Button1.Caption<>'修改保存' then
        Append
      else
        edit;
      FieldByName('BPID').Value:=Trim(FBPID);
      FieldByName('BPCode').Value:=trim(FCPNo)+Trim(MXCode.Text);
      FieldByName('BPName').Value:=Trim(MXName.Text);
      FieldByName('MBPGYSName').Value:=Trim(MBPGYSName.Text);
      FieldByName('BPNameQC').Value:=Trim(BPNameQC.Text);
      FieldByName('MNo').Value:=Trim(MNo.Text);
      FieldByName('YHType').Value:=Trim(YHType.Text);
      FieldByName('BPHXST').Value:=Trim(BPHXST.Text);
      FieldByName('MXCode').Value:=Trim(MXCode.Text);
      FieldByName('MXName').Value:=Trim(MXName.Text);
      if Trim(BPPrice.Text)<>'' then
        FieldByName('BPPrice').Value:=BPPrice.Text
      else
        FieldByName('BPPrice').Value:=Null;
      FieldByName('BPNote').Value:=Trim(BPNote.Text);
      post;
    end;
    SClearData(Panel5,2);
    FBPID:='';
    Button1.Caption:='新增保存';
    Panel5.Visible:=false;
    TBDel.Visible:=False;
    ADOQueryCmd.Connection.CommitTrans;
  except;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
  Application.MessageBox('保存成功','提示');
end;

procedure TfrmBPZDY_XHPB_M.TPBLBClick(Sender: TObject);
begin
  frmCPTypeXH_M:=TfrmCPTypeXH_M.Create(self);
  with frmCPTypeXH_M do
  begin
    if ShowModal=1 then
    begin

    end;
    free;
  end;
  initTree();
end;

procedure TfrmBPZDY_XHPB_M.ToolButton1Click(Sender: TObject);
begin
  ToolBar1.SetFocus;
  Fint:='1';
  ModalResult:=1;
end;

procedure TfrmBPZDY_XHPB_M.cxDBTreeList1Click(Sender: TObject);
begin
  InitGrid();
  TBDel.Visible:=False;
  Panel5.Visible:=False;
  if Order_Tree.FieldByName('CPLevel').AsInteger<3 then
  begin
    TBAdd.Visible:=False;
  end else
  begin
    FBPID:='';
    FCPID:=Trim(Order_Tree.fieldbyname('CPID').AsString);
    FCPNo:=Trim(Order_Tree.fieldbyname('CPNo').AsString);
    FCPName:=Trim(Order_Tree.fieldbyname('CPName').AsString);
    TBAdd.Visible:=True;
  end;
  Label16.Caption:=Trim(Order_Tree.fieldbyname('CPNo').AsString);
end;

procedure TfrmBPZDY_XHPB_M.MXCodeKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    MXName.SetFocus;
  end;
end;

procedure TfrmBPZDY_XHPB_M.CYNoKeyPress(Sender: TObject; var Key: Char);
var CPID:string;
begin
  
end;

procedure TfrmBPZDY_XHPB_M.MNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    BPPrice.SetFocus;
  end;
end;

procedure TfrmBPZDY_XHPB_M.FormDestroy(Sender: TObject);
begin
  frmBPZDY_XHPB_M:=nil;
end;

procedure TfrmBPZDY_XHPB_M.TV1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  Panel5.Visible:=False;
  TBAdd.Visible:=False;
  TBDel.Visible:=False;
end;

procedure TfrmBPZDY_XHPB_M.BPPriceKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    BPNote.SetFocus;
  end;
end;

procedure TfrmBPZDY_XHPB_M.BPNoteKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    Button1.SetFocus;
  end;
end;

procedure TfrmBPZDY_XHPB_M.CreThumb(AJPeg:TJPEGImage;Image1:TImage;Width, Height: Integer);
var
  Bitmap: TBitmap;
  Ratio: Double;
  ARect: TRect;
  AHeight, AHeightOffset: Integer;
  AWidth, AWidthOffset: Integer;
begin
  Bitmap := TBitmap.Create;
  try
    Ratio := AJPeg.Width /AJPeg.Height;
    if Ratio > 1.333 then
    begin
      AHeight := Round(Width / Ratio);
      AHeightOffset := (Height - AHeight) div 2;
      AWidth := Width;
      AWidthOffset := 0;
    end
    else
    begin
      AWidth := Round(Height * Ratio);
      AWidthOffset := (Width - AWidth) div 2;
      AHeight := Height;
      AHeightOffset := 0;
    end;
    Bitmap.Width := Width;
    Bitmap.Height := Height;
    Bitmap.Canvas.Brush.Color := clBtnFace;
    Bitmap.Canvas.FillRect(Rect(0, 0, Width, Height));
    ARect := Rect(AWidthOffset, AHeightOffset, AWidth + AWidthOffset, AHeight + AHeightOffset);
    Bitmap.Canvas.StretchDraw(ARect, AJPeg);
    Image1.Picture.Assign(BitMap);
  finally
    Bitmap.Free;
  end;
end;

procedure TfrmBPZDY_XHPB_M.ReadINIFile10();
var
  programIni:Tinifile;  //配置文件名
  FileName:string;
begin
  FileName:=ExtractFilePath(Paramstr(0))+'SYSTEMSET.INI';
  programIni:=Tinifile.create(FileName);
  server:=programIni.ReadString('SERVER','服务器地址','127.0.0.1');
  programIni.Free;
end;

procedure TfrmBPZDY_XHPB_M.ToolButton2Click(Sender: TObject);
var
  i,j: Integer;
  PatFile,CYID: String;
  FTPPath,FConNo,MaxNo:string;
  AJpeg: TJPEGImage;
  myStream: TADOBlobStream;
begin
  lstPat.Clear;
  if ODPat.Execute then
  begin
    lstPat.AddStrings(ODPat.Files);
  end;
  if lstPat.Count > 0 then
  begin
    try
      ReadINIFile10();
      server:=ReadINIFileStr('SYSTEMSET.INI','SERVER','服务器地址','127.0.0.1');
      IdFTP1.Host :=server;//PicSvr;
      IdFTP1.Username := 'three';
      IdFTP1.Password := '641010';
      IdFTP1.Connect();
    except
      IdFTP1.Quit;
      Application.MessageBox('无法连接到文件服务器，请检查！', '提示', MB_ICONWARNING);
      Exit;
    end;
  end;
  Panel16.Visible:=True;
  Panel16.Refresh;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select Count(*) MM from XD_File where CYNO='''+Trim(self.ClientDataSet1.fieldbyname('BPID').AsString)+'''');
    SQL.Add('and filetype=''YP''');
    Open;
    j:=fieldbyname('MM').AsInteger;
  end;
  try
    AJpeg:=TJpegImage.Create();
    ADOQueryCmd.Connection.BeginTrans;
    for i := 0 to lstPat.Count - 1 do
    begin
      PatFile := ExtractFileName(lstPat[i]);
      PatFile:=Copy(PatFile,(Pos('.',PatFile)+1),(Length(PatFile)-Pos('.',PatFile))  );
      if GetLSNo(ADOQueryCmd,FConNo,'S','XD_File',3,0)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取图片最大号失败！','提示',0);
        Exit;
      end;
      PatFile:=Trim(self.ClientDataSet1.fieldbyname('BPID').AsString)+Trim(FConNo)+'.'+PatFile;
      AJpeg.LoadFromFile(ExtractFileName(lstPat[i]));
      CreThumb(AJpeg,Image2,160, 120);
      if IdFTP1.Connected then
      begin
        IdFTP1.Put(lstPat[i], 'YP'+'\'+Trim(PatFile));
        if GetLSNo(ADOQueryCmd,CYID,'H','XD_File',4,1)=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取图片最大号失败！','提示',0);
          Exit;
        end;
        with ADOQueryTemp do
        begin
          Close;
          SQL.Clear;
          SQL.Add('select * from XD_File where CYID='''+Trim(CYID)+'''');
          SQL.Add(' and filename='''+Trim(PatFile)+'''');
          SQL.Add(' and filetype=''YP''');
          Open;
          if not IsEmpty then
          begin
            Panel16.Visible:=False;
            Application.MessageBox(PChar('文件<'+Trim(PatFile)+'>重复,'+inttostr(i)+'个文件上传成功！'),'提示',0);
            Exit;
          end;
        end;
        if GetLSNo(ADOQueryCmd,MaxNo,'SS','XD_File',4,1)=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取图片最大号失败！','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          SQL.Add('select * from XD_File where 1=2');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('XFID').Value:=Trim(MaxNo);
          FieldByName('CYID').Value:=Trim(CYID);
          FieldByName('CYNO').Value:=Trim(self.ClientDataSet1.fieldbyname('BPID').AsString);
          FieldByName('SKID').Value:=Trim(self.ClientDataSet1.fieldbyname('BPID').AsString);
          FieldByName('filename').Value:=Trim(PatFile);
          FieldByName('FileDate').Value:=SGetServerDate(ADOQueryTemp);
          fieldbyname('FileType').value:=Trim('YP');
          Post;
        end;
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add(' select *  from TP_File where TFID='''+Trim(MaxNo)+'''');
          //ShowMessage(sql.Text);
          open;
        end;
        with ADOQueryCmd do
        begin
          if ADOQueryCmd.IsEmpty then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('TFID').Value:=Trim(MaxNo);
          FieldByName('WBID').Value:=Trim(CYID);
          FieldByName('TFType').Value:='样品';
          myStream := TADOBlobStream.Create(TBlobField(ADOQueryCmd.FieldByName('FilesOther')), bmWrite);
          AJpeg.Assign(Image2.Picture.Graphic);
          AJpeg.SaveToStream(myStream);
          myStream.Free;
          Post;
        end;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('图片上传失败！','提示',0);
  end;
  if IdFTP1.Connected then IdFTP1.Quit;
  with ADOQueryFile do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from XD_File where CYNo='''+Trim(self.ClientDataSet1.fieldbyname('BPID').AsString)+'''');
    open;
  end;
  Panel16.Visible:=False;
  if i>0 then
    Application.MessageBox(PChar(inttostr(i)+'个文件上传成功！'),'提示',0);
  InitGrid();
end;

procedure TfrmBPZDY_XHPB_M.TXZTPClick(Sender: TObject);
var
  IdFTP1: TIdFTP;
  FPath,FFName:string;
  FInt:integer;
  maxno:string;
  j,i:Integer;
begin
  try
    IdFTP1:=TIdFTP.Create(self);
    IdFTP1.Host :=PicSvr;
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    IdFTP1.Quit;
    IdFTP1.Free;
    Application.MessageBox('无法连接到文件服务器，请检查！', '提示', MB_ICONWARNING);
    Exit;
  end;
  FPath:='D:\Right1209\';
  if not DirectoryExists(ExtractFileDir(FPath)) then
    CreateDir(ExtractFileDir(FPath));
  FFName:=Trim(ClientDataSet1.fieldbyname('FileName').AsString);
  FFName:=FPath+FFName;
  if  DirectoryExists(ExtractFileDir(FFName)) then
    DeleteFile(FFName);
  if FileExists(FFName) then
  begin
    FInt:=1;
  end;
  if FInt<>1 then
    IdFTP1.Get('YP\'+ClientDataSet1.fieldbyname('FileName').AsString,FFName);
  if IdFTP1.Connected then
  begin
    IdFTP1.Quit;
    IdFTP1.Free;
  end;
  ShellExecute(Handle, 'open',PChar(FFName),'', '', SW_SHOWNORMAL);
end;

end.
