unit U_ZHDJInPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxMemo, cxRichEdit, ComCtrls, cxContainer,
  cxTextEdit, cxMaskEdit, cxButtonEdit, StdCtrls, ToolWin, DBClient, ADODB,
  ExtCtrls, BtnEdit, cxCalendar,StrUtils, cxDropDownEdit,jpeg,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP, cxPC,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGroupBox, cxCheckBox,
  RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport,IniFiles,
  cxImage, Menus,ShellAPI;

type
  TfrmZHDJInPut = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    ADOQuery1: TADOQuery;
    TBSave: TToolButton;
    Label26: TLabel;
    CDS_PRT: TClientDataSet;
    RM1: TRMGridReport;
    Panel3: TPanel;
    Panel5: TPanel;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    SaveDialog1: TSaveDialog;
    TBPrtInfo: TToolButton;
    Label4: TLabel;
    adoqueryPicture: TADOQuery;
    RMDBMain: TRMDBDataSet;
    ADOQueryCmd: TADOQuery;
    Panel66: TPanel;
    Image2: TImage;
    Panel2: TPanel;
    Button2: TButton;
    Image22: TImage;
    Panel4: TPanel;
    Label1: TLabel;
    DYID: TEdit;
    KHName: TBtnEditA;
    Label3: TLabel;
    GuoJia: TBtnEditA;
    Label5: TLabel;
    Note: TMemo;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column7: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1ZICost: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Label9: TLabel;
    v1Column3: TcxGridDBColumn;
    v1XS: TcxGridDBColumn;
    v1BaoJia: TcxGridDBColumn;
    YWY: TBtnEditA;
    v1Column9: TcxGridDBColumn;
    TBPrtBJ: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    Edit2: TEdit;
    Panel9: TPanel;
    LabBZ: TLabel;
    BZ: TComboBox;
    LabHuiLv: TLabel;
    HuiLv: TEdit;
    Label8: TLabel;
    XS: TEdit;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    Label2: TLabel;
    LiDan: TBtnEditA;
    KHGY: TMemo;
    Label7: TLabel;
    v1Column5: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1ZIGY: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    Panel1: TPanel;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    ADOQuery2: TADOQuery;
    ADOQuery3: TADOQuery;
    ADOQuery6: TADOQuery;
    Button1: TButton;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure TBPrtInfoClick(Sender: TObject);
    procedure Image2DblClick(Sender: TObject);
    procedure GuoJiaBtnClick(Sender: TObject);
    procedure DYIDKeyPress(Sender: TObject; var Key: Char);
    procedure XSKeyPress(Sender: TObject; var Key: Char);
    procedure v1ZICostPropertiesEditValueChanged(Sender: TObject);
    procedure HuiLvKeyPress(Sender: TObject; var Key: Char);
    procedure BZChange(Sender: TObject);
    procedure YWYBtnClick(Sender: TObject);
    procedure KHNameBtnClick(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure KHGYChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure TBPrtBJClick(Sender: TObject);
    procedure LiDanBtnClick(Sender: TObject);
    procedure v1Column5PropertiesEditValueChanged(Sender: TObject);
    procedure v1BaoJiaCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure v1Column3CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure Tv1DblClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    lstPat,lstPat2:TStringList;
    FangXiang1,FangXiang2:String;
    procedure InitData();
    procedure SaveImage(maxnoWB:String;FIMNO:String);
    procedure ReadINIFile10();
    function  SaveData():Boolean;
    function StrFenHang(FStr:String;var FHStr:String):Boolean;
    procedure initOldYP();
    { Private declarations }
  public
    canshu1,canshu2:String;
    PState,CopyInt:Integer;
    FMainId,FZIID:String;

    { Public declarations }
  end;

var
  frmZHDJInPut: TfrmZHDJInPut;
implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_InfoBtn,MMSystem,U_ZHCPList_BJCX,
  U_ZHCPBigMP, U_KHListSelJJ,U_ZHCPInPut;

{$R *.dfm}

procedure TfrmZHDJInPut.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('展会登记'+canshu1+'YX',Tv1,'展会登记');
end;

procedure TfrmZHDJInPut.InitData();
var
  jpg:TJpegImage;
  myStream: TADOBlobStream;
  FStr:string;
  FZT:Boolean;
begin
  ReadCxGrid('展会登记'+canshu1+'YX',Tv1,'展会登记');
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * ');
    sql.Add(' from ZH_KH_DJ   ');
    sql.Add(' where ZDID='''+Trim(FMainId)+'''');
    Open;
  end;
  SCSHDataNew(ADOQuery1,Panel5,2);
  SCSHDataNew(ADOQuery1,Panel9,2);
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.ZDID,A.ZCID,A.HeSuanStatus,A.ZIID,A.LeiBie,A.XS,A.BaoJia,A.BaoJiaYW,A.ZIIDYS,A.DYID  ');
    sql.Add(',ZICost=isnull(A.ZICost,B.ZICost),A.ZIName,A.ZIMF,A.ZIKZ,A.ZICF,A.ZIGY,ZIGYYS=isnull(C.ZIGY,A.ZIGY)');
    sql.Add(' ,A.ImageFileTP,A.GS');
    sql.Add(' from ZH_KH_DJ_CP A  ');
    sql.Add(' left join ZH_CP_Info B on A.ZIID=B.ZIID ');
    sql.Add(' left join ZH_CP_Info C on B.ZIIDYS=C.ZIID ');
    sql.Add(' where A.ZDID='''+Trim(FMainId)+''' and A.Valid=''Y'' ');
    Open;
  end;
  SCreateCDS20(ADOTemp,Order_Sub);
  SInitCDSData20(ADOTemp,Order_Sub);
  if Trim(HuiLv.Text)='' then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select Top 1 * from ZH_KH_DJ order by FillTime desc');
      Open;
    end;
    HuiLv.Text:=Trim(ADOTemp.fieldbyname('HuiLv').AsString);
  end;

  with adoqueryPicture do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Image_Info A');
    sql.Add(' inner join Image_File B on A.IMID=B.IMID');
    sql.Add(' and A.WBID='''+Trim(FMainId)+'''');
    sql.Add(' and isnull(A.IMNo,'''')=''名片'' and A.Valid=''Y'' ');
    Open;
  end;
  if adoqueryPicture.IsEmpty=False then
  begin
    Fstr:=Trim(adoqueryPicture.fieldbyname('ImageName').AsString);
    jpg:=TJpegImage.Create();
    myStream:=tadoblobstream.Create(tblobfield(adoqueryPicture.fieldbyname('ImageFile')),bmread);
    jpg.LoadFromStream(myStream);
    Image2.Picture.Assign(jpg);
    Image2.Width:=adoqueryPicture.fieldbyname('ImageWidth').Value;
    Image2.Height:=adoqueryPicture.fieldbyname('ImageHeight').Value;
    if Trim(adoqueryPicture.fieldbyname('FangXiang').AsString)='垂直' then
    begin
      Image2.Top:=1;
      Image2.Left:=Round((Panel66.Width-Image2.Width)/2);
    end else
    begin
      Image2.Left:=1;
      Image2.Top:=Round((Panel66.Height-Image2.Height)/2);
    end;
    Image2.Visible:=True;
  end;
  if TBSave.Visible=False then
  begin
    Panel4.Visible:=False;
    Panel2.Visible:=False;
    KHName.Color:=clMenu;
    GuoJia.Color:=clMenu;
  
    YWY.Color:=clMenu;
    BZ.Color:=clMenu;
    HuiLv.Color:=clMenu;
    XS.Color:=clMenu;
    KHGY.Color:=clMenu;
    Note.Color:=clMenu;
  end;
  if Trim(canshu1)='报价' then
  begin
    Panel9.Visible:=True;
    v1ZICost.Visible:=True;
    v1ZICost.Hidden:=False;
    v1XS.Visible:=True;
    v1XS.Hidden:=False;
    //TBPrtBJ.Visible:=True;
    v1BaoJia.Options.Editing:=True;
  end else
  begin
    Panel9.Visible:=False;
    v1ZICost.Visible:=False;
    v1ZICost.Hidden:=True;
    v1XS.Visible:=False;
    v1XS.Hidden:=True;
    v1BaoJia.Options.Editing:=False;
  end;
end;
procedure TfrmZHDJInPut.FormShow(Sender: TObject);
begin
  InitData();
end;

function TfrmZHDJInPut.SaveData():Boolean;
var
  maxno,maxmxno,FMXID,FBaoJia,FZIID,FZIIDAdd,FAdd,FZIIDYS:String;
  FXH:Integer;
  FDate:TDateTime;
begin
  Result:=False;
  FDate:=SGetServerDate(ADOTemp);
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId)='' then
    begin
      if GetLSNoHZ(ADOCmd,maxno,'ZD','ZH_KH_DJ',3,1,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from ZH_KH_DJ where ZDId='''+Trim(maxno)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId)='' then
      begin
        Append;
      end
      else begin
        Edit;
      end;
      FieldByName('ZDId').Value:=Trim(maxno);
      if Trim(FMainId)='' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
        FieldByName('FillerCode').Value:=Trim(DCode);
        FieldByName('Valid').Value:='Y';
        FieldByName('ZDDate').Value:=FDate;
      end else
      begin
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditerCode').Value:=Trim(DCode);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
      end;
      RTSetsavedata(ADOCmd,'ZH_KH_DJ',Panel5,2);
      RTSetsavedata(ADOCmd,'ZH_KH_DJ',Panel9,2);
      Post;
    end;
    FMainId:=Trim(maxno);

    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from ZH_KH_DJ_CP where ZCID='''+Trim(Order_Sub.fieldbyname('ZCID').AsString)+'''');
          Open;
        end;
        FMXID:=Trim(ADOCmd.fieldbyname('ZCID').AsString);
        if Trim(FMXID)='' then
        begin
          if GetLSNo(ADOCmd,maxmxno,'ZC','ZH_KH_DJ_CP',4,1)=False then
          begin
            Order_Sub.EnableControls;
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取明细最大号失败','提示',0);
            Exit;
          end;
        end else
        begin
          maxmxno:=Trim(FMXID)
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from ZH_KH_DJ_CP where ZCID='''+Trim(maxmxno)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(FMXID)='' then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
            FieldByName('FillerCode').Value:=Trim(DCode);
            FieldByName('Valid').Value:='Y';
            FieldByName('ZIID').Value:=Order_Sub.fieldbyname('ZIID').Value;
            FieldByName('ZIIDYS').Value:=Order_Sub.fieldbyname('ZIIDYS').Value;
          end else
          begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditerCode').Value:=Trim(DCode);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
          end;
          FieldByName('ZDID').Value:=Trim(maxno);
          FieldByName('ZCID').Value:=Trim(maxmxno);
          FieldByName('DYID').Value:=Order_Sub.fieldbyname('DYID').Value;
          FieldByName('HeSuanStatus').Value:=Order_Sub.fieldbyname('HeSuanStatus').Value;
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'ZH_KH_DJ_CP',2);
          FieldByName('Valid').Value:='Y';
          Post;
        end;
        if Trim(Order_Sub.fieldbyname('ZIGYYS').AsString)<>Trim(Order_Sub.fieldbyname('ZIGY').AsString) then
        begin
          FZIID:=Trim(Order_Sub.fieldbyname('ZIID').AsString);
          if Pos('-',FZIID)>0 then
          begin
            FZIID:=Copy(FZIID,1,Pos('-',FZIID)-1);
          end;
          with ADOTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from ZH_CP_Info where isnull(ZIIDQuHao,'''')='''+Trim(FZIID)+'''');
            sql.Add(' and ZIGY=:ZIGY');
            Parameters.ParamByName('ZIGY').Value:=Order_Sub.fieldbyname('ZIGY').Value;
            Open;
          end;
          if ADOTemp.IsEmpty then
          begin
            FAdd:='新增';
            with ADOTemp do
            begin
              Close;
              sql.Clear;
              sql.Add('select AA=isnull(max(ZIIDAddXH),0)+1 from ZH_CP_Info where isnull(ZIIDQuHao,'''')='''+Trim(FZIID)+'''');
              Open;
            end;
            FXH:=ADOTemp.fieldbyname('AA').AsInteger;
            if Trim(FMXID)='' then
            begin
              FZIIDYS:=Trim(Order_Sub.fieldbyname('ZIID').AsString);
            end else
            begin
              FZIIDYS:=Trim(Order_Sub.fieldbyname('ZIIDYS').AsString);
            end;
          end else
          begin
            FXH:=ADOTemp.fieldbyname('ZIIDAddXH').AsInteger;
            with ADOCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('Update ZH_KH_DJ_CP Set ZIID='''+Trim(FZIID+'-'+Trim(IntToStr(FXH)))+'''');
              SQL.Add(' where ZCID='''+Trim(maxmxno)+'''');
              ExecSQL;
            end;
            with ADOCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('Update ZH_KH_DJ_CP Set ZIIDYS=(select ZIIDYS from ZH_CP_Info A where A.ZIID=ZH_KH_DJ_CP.ZIID )');
              SQL.Add(' where ZCID='''+Trim(maxmxno)+'''');
              ExecSQL;
            end;
            FAdd:='修改';
          end;
          FZIIDAdd:=FZIID+'-'+Trim(IntToStr(FXH));
          if Trim(FAdd)='新增' then
          begin
            if Pos('-',Trim(Order_Sub.fieldbyname('ZIID').AsString))>0 then
            begin
              with ADOTemp do
              begin
                Close;
                sql.Clear;
                sql.Add('select * from ZH_KH_DJ_CP where ZIID='''+Trim(Order_Sub.fieldbyname('ZIID').AsString)+'''');
                sql.Add(' and ZCID<>'''+Trim(Order_Sub.fieldbyname('ZCID').AsString)+'''');
                Open;
              end;
              if ADOTemp.IsEmpty then
              begin
                with ADOCmd do
                begin
                  Close;
                  sql.Clear;
                  sql.Add('Update ZH_CP_Info Set ZIGY=:ZIGY');
                  sql.Add(' where ZIID='''+Trim(Order_Sub.fieldbyname('ZIID').AsString)+'''');
                  Parameters.ParamByName('ZIGY').Value:=Order_Sub.fieldbyname('ZIGY').Value;
                  ExecSQL;
                end;
                FAdd:='结束';
              end else
              begin
                FAdd:='新增';
              end;

            end;
            if Trim(FAdd)='新增' then
            begin
              with ADOCmd do
              begin
                Close;
                sql.Clear;
                sql.Add('Update ZH_KH_DJ_CP Set ZIID='''+Trim(FZIIDAdd)+'''');
                sql.Add(',ZIIDYS='''+Trim(FZIIDYS)+'''');
                SQL.Add(' where ZCID='''+Trim(maxmxno)+'''');
                ExecSQL;
              end;
              with ADOCmd do
              begin
                Close;
                sql.Clear;
                sql.Add('insert into ZH_CP_Info(ZIID,ZIMF,ZIKZ,ZICF,ZIGY,Filler,FillerCode,Valid,ZIName,ZIIDYS,ZIIDQuHao,ZIIDAddXH,ZIDate)');
                sql.Add('select :ZIID,ZIMF,ZIKZ,ZICF,:ZIGY,:Filler,:FillerCode,''Y'',ZIName,:ZIIDYS,:ZIIDQuHao,:ZIIDAddXH,:ZIDate');
                sql.Add(' from ZH_CP_Info where ZIID='''+Trim(FZIIDYS)+'''');
                Parameters.ParamByName('ZIID').Value:=Trim(FZIIDAdd);
                Parameters.ParamByName('ZIGY').Value:=Trim(Order_Sub.fieldbyname('ZIGY').AsString);
                Parameters.ParamByName('Filler').Value:=Trim(DName);
                Parameters.ParamByName('FillerCode').Value:=Trim(DCode);
                Parameters.ParamByName('ZIIDYS').Value:=Trim(FZIIDYS);
                Parameters.ParamByName('ZIIDQuHao').Value:=Trim(FZIID);
                Parameters.ParamByName('ZIIDAddXH').Value:=FXH;
                Parameters.ParamByName('ZIDate').Value:=FDate;
                ExecSQL;
              end;
              with ADOCmd do
              begin
                Close;
                sql.Clear;
                sql.Add('Update ZH_CP_Info Set ZIGY=:ZIGY');
                sql.Add(' where ZIID='''+Trim(FZIIDAdd)+'''');
                Parameters.ParamByName('ZIGY').Value:=Order_Sub.fieldbyname('ZIGY').Value;
                ExecSQL;
              end;
              with ADOCmd do
              begin
                Close;
                sql.Clear;
                sql.Add('insert into Image_Info(IMID,IMNO,WBID,ImagePath,ImageName,ImageDate,ImageType,Valid)');
                sql.Add('select '''+Trim(FZIIDAdd)+'''+substring(IMID,len(IMID)-1,2),IMNO,:WBID,ImagePath,ImageName,ImageDate,ImageType,''Y'' ');
                sql.Add(' from Image_Info where WBID='''+Trim(FZIIDYS)+''' and Valid=''Y'' ');
                Parameters.ParamByName('WBID').Value:=Trim(FZIIDAdd);
                ExecSQL;
              end;
              with ADOCmd do
              begin
                Close;
                sql.Clear;
                sql.Add('insert into Image_File(IMID,IFID,IFType,ImageFile,Valid,ImageWidth,ImageHeight,FangXiang)');
                sql.Add('select '''+Trim(FZIIDAdd)+'''+substring(B.IMID,len(B.IMID)-1,2) ');
                sql.Add(','''+Trim(FZIIDAdd)+'''+substring(B.IFID,len(B.IFID)-3,4)');
                sql.Add(' ,B.IFType,B.ImageFile,''Y'',B.ImageWidth,B.ImageHeight,B.FangXiang');
                sql.Add(' from Image_Info A ');
                sql.Add(' inner join Image_File B on A.IMID=B.IMID');
                sql.Add(' where A.WBID='''+Trim(FZIIDYS)+''' and A.Valid=''Y'' and B.Valid=''Y'' ');
                ExecSQL;
              end;
            end;
          end;
        end;
        if Trim(Order_Sub.fieldbyname('LeiBie').AsString)='拿走吊卡' then
        begin
          with ADOCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update ZH_CP_Info_KC Set GGQty=0 ');
            sql.Add(' where DYID='''+Trim(Order_Sub.fieldbyname('DYID').AsString)+'''');
            ExecSQL;
          end;
        end else
        begin
          with ADOCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update ZH_CP_Info_KC Set GGQty=1 ');
            sql.Add(' where DYID='''+Trim(Order_Sub.fieldbyname('DYID').AsString)+''' and isnull(KWID,'''')<>'''' ');
            ExecSQL;
          end;
        end;
        Edit;
        FieldByName('ZDID').Value:=Trim(maxno);
        FieldByName('ZCID').Value:=Trim(maxmxno);
        Post;
        Next;
      end;
    end;
    Order_Sub.EnableControls;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update ZH_KH_DJ Set DJYang=(select Count(*) from ZH_KH_DJ_CP A where A.ZDID=ZH_KH_DJ.ZDID and A.LeiBie=''登记样'' and A.Valid=''Y'' )');
      sql.Add(',NaYang=(select Count(*) from ZH_KH_DJ_CP A where A.ZDID=ZH_KH_DJ.ZDID and A.LeiBie=''拿走吊卡'' and A.Valid=''Y'' )');
      sql.Add(' where ZDID='''+Trim(maxno)+'''');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update ZH_KH_DJ Set XunYang=(select Count(*) from ZH_KH_DJ_CP A where A.ZDID=ZH_KH_DJ.ZDID and A.Valid=''Y''  )');
      sql.Add(' where ZDID='''+Trim(maxno)+'''');
      ExecSQL;
    end;
    if Trim(canshu1)='报价' then
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from ZH_KH_DJ_CP  ');
        sql.Add(' where ZDID='''+Trim(maxno)+''' and Valid=''Y'' and isnull(BaoJia,0)>0 ');
        Open;
      end;
      if ADOTemp.IsEmpty=False then
      begin
        FBaoJia:='已报价';
      end else
      begin
        FBaoJia:='';
      end;
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update ZH_KH_DJ Set BJStatus='''+Trim(FBaoJia)+'''');
        sql.Add(' where ZDID='''+Trim(maxno)+'''');
        ExecSQL;
      end;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update ZH_CP_Info Set ZICost=(select Top 1 ZICost from ZH_KH_DJ_CP A where A.ZIID=ZH_CP_Info.ZIID and ZDID='''+Trim(maxno)+'''');
      sql.Add('order by FillTime desc)');
      sql.Add(' where ZIID in (select ZIID from ZH_KH_DJ_CP CP where ZDID='''+Trim(maxno)+''' ');
      sql.Add('  and isnull(ZICost,0)>0 and Valid=''Y'' )');   //and CP.ZIID=ZH_CP_Info.ZIID
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update ZH_CP_Info Set KHNameHZ=dbo.F_Get_HZStr(ZIID,''KHNaYang'')');
      sql.Add(' where ZIID in(select ZIID from ZH_KH_DJ_CP where ZDID='''+Trim(maxno)+'''and Valid=''Y'' )');
      ExecSQL;
    end;
    if Trim(Image2.Hint)<>'' then
    begin
      SaveImage(maxno,'名片');
    end;
    ADOCmd.Connection.CommitTrans;
    FMainId:=Trim(maxno);
    Result:=True;
  except
    Result:=False;
    Panel1.Visible:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmZHDJInPut.TBSaveClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if Trim(KHName.Text)='' then
  begin
    Application.MessageBox('客户不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细数据不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('LeiBie',Null,[]) then
  begin
    Application.MessageBox('类别不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('LeiBie','',[]) then
  begin
    Application.MessageBox('类别不能为空!','提示',0);
    Exit;
  end;
  Panel1.Visible:=True;
  Panel1.Refresh;
  if SaveData() then
  begin
    Panel1.Visible:=False;
    Application.MessageBox('保存成功！','提示',0);
    ModalResult:=1;
  end;
end;
procedure TfrmZHDJInPut.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 TfrmZHDJInPut.SaveImage(maxnoWB:string;FIMNO:String);
var
  ImagePath,ImagName,MaxNo,MaxNoFile:String;
  AJpeg:TJPEGImage;
  myStream:TADOBlobStream;
begin
  try
    if Trim(FIMNO)='名片' then
    begin
      ImagePath:=Image2.Hint;
      ImagName:=Image2.Hint;
    end;
    AJpeg:=TJpegImage.Create();
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('UPdate Image_File Set Valid=''N'' ');
      sql.Add(' where IMID=(select IMID from Image_Info');
      Sql.Add('where WBID='''+Trim(maxnoWB)+''' and Valid=''Y'' and IMNO='''+Trim(FIMNO)+''')');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('UPdate Image_Info Set Editer='''+Trim(DName)+''',EditerCode='''+Trim(DCode)+''',EditTime=getdate()');
      sql.Add(',Valid=''N''  where WBID='''+Trim(maxnoWB)+''' and Valid=''Y'' and IMNO='''+Trim(FIMNO)+''' ');
      ExecSQL;
    end;
    if GetLSNo(ADOCmd,MaxNo,'IM','Image_Info',4,1)=False then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('取图片信息最大号失败！','提示',0);
      Exit;
    end;
    with ADOCmd do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from Image_Info where 1=2');
      Open;
    end;
    with ADOCmd do
    begin
      Append;
      FieldByName('IMID').Value:=Trim(MaxNo);
      FieldByName('IMNo').Value:=FIMNO;
      FieldByName('WBID').Value:=Trim(maxnoWB);
      FieldByName('ImageName').Value:=Trim(ImagName);
      FieldByName('ImagePath').Value:=Trim(MaxNo)+Trim(ImagName);
      FieldByName('ImageDate').Value:=SGetServerDate(ADOTemp);
      Fieldbyname('ImageType').value:=Trim('MP');
      Fieldbyname('Valid').value:=Trim('Y');
      Post;
    end;
    if GetLSNo(ADOCmd,MaxNoFile,'IF','Image_File',4,1)=False then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('取图片文件最大号失败！','提示',0);
      Exit;
    end;
    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add(' select *  from Image_File where 1=2');
      open;
    end;
    with ADOCmd do
    begin
      Append;
      FieldByName('IMID').Value:=Trim(MaxNo);
      FieldByName('IFID').Value:=Trim(MaxNoFile);
      Fieldbyname('Valid').value:=Trim('Y');
      if Trim(FIMNO)='名片' then
      begin
        FieldByName('ImageWidth').Value:=Image2.Width;
        FieldByName('ImageHeight').Value:=Image2.Height;
        FieldByName('FangXiang').Value:=FangXiang2;
      end;
      FieldByName('IFType').Value:='小';
      myStream := TADOBlobStream.Create(TBlobField(ADOCmd.FieldByName('ImageFile')), bmWrite);
      if Trim(FIMNO)='名片' then
      begin
        AJpeg.Assign(Image2.Picture.Graphic);
      end;
      AJpeg.SaveToStream(myStream);
      myStream.Free;
      Post;
    end;
    with ADOCmd do
    begin
      Append;
      FieldByName('IMID').Value:=Trim(MaxNo);
      FieldByName('IFID').Value:=Trim(MaxNoFile)+'D';
      Fieldbyname('Valid').value:=Trim('Y');
      if Trim(FIMNO)='名片' then
      begin
        FieldByName('ImageWidth').Value:=Image22.Width;
        FieldByName('ImageHeight').Value:=Image22.Height;
        FieldByName('FangXiang').Value:=FangXiang2;
      end;
      FieldByName('IFType').Value:='大';
      myStream := TADOBlobStream.Create(TBlobField(ADOCmd.FieldByName('ImageFile')), bmWrite);
      if Trim(FIMNO)='名片' then
      begin
        AJpeg.Assign(Image22.Picture.Graphic);
      end;
      AJpeg.SaveToStream(myStream);
      myStream.Free;
      Post;
    end;
    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;
    if IdFTP1.Connected then
    begin
      if Trim(FIMNO)='名片' then
      begin
        IdFTP1.Put(lstPat2[0], 'MP'+'\'+Trim(MaxNo)+Trim(ImagName));
      end;
    end;
    if IdFTP1.Connected then IdFTP1.Quit;
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('图片保存失败！','提示',0);
  end;
end;

Procedure TfrmZHDJInPut.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  ModalResult:=1;
end;


function TfrmZHDJInPut.StrFenHang(FStr:string;var FHStr:String):Boolean;
var
  i,j:Integer;
  LSStr:String;
begin
  Result:=False;
  FStr:=Trim(FStr);
  FHStr:='';
  j:=1;
  i:=Pos(' ',FStr);
  while i>0 do
  begin
    if j=1 then
    begin
      LSStr:=Copy(FStr,1,i-1);  //'1234  5678 456'
      FHStr:='1:'+LSStr;
    end else
    begin
      LSStr:=Copy(FStr,1,i-1);
      if Trim(LSStr)='' then
      begin
        Fstr:=Copy(Fstr,i+1,Length(Fstr)-i);
      end else
        FHStr:=FHStr+#13+#10+inttostr(j)+':'+LSStr;
    end;
    if Trim(LSStr)<>'' then
    begin
      j:=j+1;
      Fstr:=Copy(Fstr,i+1,Length(Fstr)-i);
    end;
    i:=Pos(' ',FStr);
  end;
  if j=1 then
  begin
    FHStr:=FStr;
  end else
  begin
    if Trim(FStr)<>'' then
    begin
      FHStr:=FHStr+#13+#10+inttostr(j)+':'+FStr;
    end;
  end;
  Result:=True;
end;
procedure TfrmZHDJInPut.FormCreate(Sender: TObject);
begin
  lstPat := TStringList.Create;
  lstPat2 := TStringList.Create;
end;

procedure TfrmZHDJInPut.Button2Click(Sender: TObject);
var
  AJpeg:TJPEGImage;
  Bitmap,Bitmap22: TBitmap;
  ARect,ARect22: TRect;
  TPHeight, AHeightOffset: Integer;
  TPWidth, AWidthOffset: Integer;
  WZShuiPing,WZChuiZhi:Integer;
  WZFlag:String;
begin
  lstPat2.Clear;
  if ODPat.Execute then
  begin
    lstPat2.AddStrings(ODPat.Files);
  end else
  begin
    Exit;
  end;
  if  lstPat2.Count>1 then
  begin
    lstPat2.Clear;
    Application.MessageBox('不能上传多个图片!','提示',0);
    Exit;
  end else
  if lstPat2.Count<1 then
  begin
    Exit;
  end;
  Panel1.Visible:=True;
  Panel1.Refresh;
  AJpeg:=TJpegImage.Create();
  AJpeg.LoadFromFile(ExtractFileName(ODPat.FileName));
  Image2.Hint:=ExtractFileName(ODPat.FileName);
  TPWidth:=AJpeg.Width;
  TPHeight:=AJpeg.Height;
  Bitmap := TBitmap.Create;
  Bitmap22 := TBitmap.Create;
  Bitmap.Width :=Panel66.Width-2;
  Bitmap.Height :=Round(Panel66.Width/TPWidth*TPHeight);
  if Bitmap.Height>Panel66.Height then
  begin
    WZFlag:='垂直';
    Bitmap.Height:=Panel66.Height-2;
    Bitmap.Width :=Round(Panel66.Height/TPHeight*TPWidth);
  end;
  Bitmap22.Width:=Bitmap.Width*2;
  Bitmap22.Height:=Bitmap.Height*2;
  Bitmap.Canvas.FillRect(Rect(0, 0, Bitmap.Width, Bitmap.Height));
  ARect := Rect(0, 0, Bitmap.Width, Bitmap.Height);
  Bitmap.Canvas.StretchDraw(ARect, AJPeg);
  Image2.Height:=Bitmap.Height;
  Image2.Width:=Bitmap.Width;
  Image2.Picture.Assign(BitMap);
  if Trim(WZFlag)='垂直' then
  begin
    Image2.Top:=1;
    Image2.Left:=Round((Panel66.Width-Image2.Width)/2);
  end else
  begin
    Image2.Left:=1;
    Image2.Top:=Round((Panel66.Height-Image2.Height)/2);
  end;
  FangXiang2:=WZFlag;
  Bitmap22.Canvas.FillRect(Rect(0, 0, Bitmap22.Width, Bitmap22.Height));
  ARect22 := Rect(0, 0, Bitmap22.Width, Bitmap22.Height);
  Bitmap22.Canvas.StretchDraw(ARect22, AJPeg);
  Image22.Height:=Bitmap22.Height;
  Image22.Width:=Bitmap22.Width;
  Image22.Picture.Assign(Bitmap22);
  Image2.Visible:=True;
  Edit2.Text:=Copy(Image2.Hint,1,Length(Image2.Hint)-4);
  Panel1.Visible:=False;
end;
procedure TfrmZHDJInPut.TBPrtInfoClick(Sender: TObject);
var
   fPrintFile,Txt,fImagePath,maxno:string;
   i,j:Integer;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin
  if Trim(FMainId)='' then
  begin
    Application.MessageBox('没有保存数据，不能打印!','提示',0);
    Exit;
  end;
  Order_Sub.DisableControls;
  with Order_Sub do
  begin
    First;
    while not Eof do
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from ZH_KH_DJ_CP where ZCID='''+Trim(Order_Sub.fieldbyname('ZCID').AsString)+'''');
        Open;
      end;
      if ADOTemp.IsEmpty then
      begin
        Application.MessageBox('没有保存数据，不能打印!','提示',0);
        Exit;
      end;
      Next;
    end;
  end;
  Order_Sub.EnableControls;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\展会产品标签85.rmf';
  if FileExists(fPrintFile)=False then
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\展会产品标签85.rmf'),'提示',0);
    exit;
  end;
  if Order_Sub.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    exit;
  end;
  Order_Sub.DisableControls;
  with Order_Sub do
  begin
    First;
    while Locate('SSel',True,[]) do
    begin
      with ADOTemp do
      begin
        Close;
        SQL.Clear;
        sql.Add(' select B.* ');
        sql.Add(' from ZH_KH_DJ_CP A');
        sql.Add(' inner join ZH_CP_Info B on A.ZIID=B.ZIID');
        sql.Add(' where A.ZIID='''+Trim(Order_Sub.fieldbyname('ZIID').AsString)+'''');
        sql.Add('  and A.Valid=''Y''  ');
        Open;
      end;
      SCreateCDS20(ADOTemp,CDS_PRT);
      SInitCDSData20(ADOTemp,CDS_PRT);
      try
        ADOQueryCmd.Connection.BeginTrans;
        if GetLSNo(ADOQueryCmd,maxno,'DYKC','ZH_CP_Info_KC',4,1,)=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取样品打印最大号失败!','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from ZH_CP_Info_KC where DYId='''+Trim(maxno)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('ZIId').Value:=Trim(Order_Sub.fieldbyname('ZIID').AsString);
          FieldByName('DYId').Value:=Trim(maxno);
          FieldByName('Filler').Value:=Trim(DName);
          FieldByName('FillerCode').Value:=Trim(DCode);
          FieldByName('Valid').Value:='Y';
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update ZH_CP_Info_KC Set DYXH=isnull((select max(DYXH) from ZH_CP_Info_KC A ');
          sql.Add(' where A.ZIID='''+Trim(Order_Sub.fieldbyname('ZIID').AsString)+'''),0)+1 ');
          sql.Add(' where DYID='''+Trim(maxno)+'''');
          ExecSQL;
        end;
        ADOQueryCmd.Connection.CommitTrans;
      except
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('保存打印流水号异常！','提示',0);
        Exit;
      end;
      try
        Moudle:=LoadLibrary('MakeQRBarcode.dll');
        @Makebar:=GetProcAddress(Moudle,'Make');
        @Mixtext:=GetProcAddress(Moudle,'MixText');
        Txt:=Trim(maxno);
        fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp.bmp';
        if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName)+'image')) then
          CreateDirectory(pchar(ExtractFilePath(Application.ExeName)+'image'),nil);
        if FileExists(fImagePath) then DeleteFile(fImagePath);
        Makebar(pchar(Txt),Length(Txt),3,3,0,PChar(fImagePath),3);
      except
        application.MessageBox('二维码生成失败！','提示信息',MB_ICONERROR);
        exit;
      end;
      RMVariables['QRBARCODE']:=fImagePath;
      RMVariables['DYID']:=Trim(maxno);
      RM1.LoadFromFile(fPrintFile);
      //RM1.ShowReport;
      RM1.PrintReport;
      Edit;
      FieldByName('SSel').Value:=False;
    end;
  end;
  Order_Sub.EnableControls;

end;

procedure TfrmZHDJInPut.Image2DblClick(Sender: TObject);
begin
  if Trim(FMainId)='' then
  begin
    Application.MessageBox('没有保存数据，不能查看大图!','提示',0);
    Exit;
  end;
  try
    frmZHCPBigMP:=TfrmZHCPBigMP.Create(Application);
    with frmZHCPBigMP do
    begin
      frmZHCPBigMP.FMainId:=Self.FMainId;
      frmZHCPBigMP.FIMNO:='名片';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmZHCPBigMP.Free;
  end;  ;
end;

procedure TfrmZHDJInPut.GuoJiaBtnClick(Sender: TObject);
begin
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KH_Main where isnull(KHNameJC,'''')='''+Trim(KHName.Text)+'''');
    sql.add(' and isnull(KHFlag,'''')='''+Trim('KH')+''' and isnull(Valid,'''')=''Y''  ');
    Open;
  end;
  if ADOTemp.IsEmpty=False then
  begin
    if Trim(canshu2)<>'高权限' then
    begin
      Exit;
    end;
  end;
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='GuoJia';
      flagname:='国家';
      if ShowModal=1 then
      begin
        GuoJia.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmZHDJInPut.initOldYP();
var FMXCYID,FColNo,FCYType,CPID:String;
begin
  with ADOQuery6 do
  begin
    close;
    sql.Clear;
    sql.Add('select A.CYID MXCYType,A.ColorNo,CYType=isnull((select Top 1 C.CYType from CP_YDang B inner join CP_YDang C on C.CYID=B.CYType where B.CYID=A.CYID)');
    SQL.Add(',(select Top 1 B.CYType from CP_YDang B where B.CYID=A.CYID))');
    sql.Add(',CYID=isnull((select Top 1 C.CYID from CP_YDang B inner join CP_YDang C on C.CYID=B.CYType where B.CYID=A.CYID),');
    sql.Add('(select Top 1 B.CYID from CP_YDang B where B.CYID=A.CYID))');
    sql.Add(' from CP_YDang_Color A where A.CLRID='''+trim(DYID.Text)+'''');
    //ShowMessage(sql.Text);
    Open;
  end;
  with ADOQuery2 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CP_YDang where CYID='''+Trim(DYID.Text)+'''');
    Open;
  end;
  with ADOQuery3 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CP_YDang where CYID='''+Trim(ADOQuery2.fieldbyname('CYType').AsString)+'''');
    Open;
  end;
  if ADOQuery6.IsEmpty=false then
  begin
    CPID:=Trim(ADOQuery6.fieldbyname('CYType').AsString);
    FMXCYID:=Trim(ADOQuery6.fieldbyname('CYID').AsString);
    FColNo:=Trim(ADOQuery6.fieldbyname('ColorNo').AsString);
    FCYType:=Trim(ADOQuery6.fieldbyname('MXCYType').AsString);
  end;
  if ADOQuery2.IsEmpty=false then
  begin
    CPID:=Trim(ADOQuery2.fieldbyname('CYType').AsString);
    FMXCYID:=Trim(ADOQuery2.fieldbyname('CYID').AsString);
  end;
  if ADOQuery3.IsEmpty=false then
  begin
    CPID:=Trim(ADOQuery3.fieldbyname('CYType').AsString);
    FMXCYID:=Trim(ADOQuery3.fieldbyname('CYID').AsString);
    FCYType:=Trim(DYID.Text);
  end;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' exec P_Select_CP_YDang1 :CYType,:CP,:RON,:WSql,:Stats ');
    Parameters.ParamByName('CYType').Value:=CPID;
    Parameters.ParamByName('CP').Value:=1;
    Parameters.ParamByName('RON').Value:=100;
    Parameters.ParamByName('WSql').Value:=' CYID='''+Trim(FMXCYID)+''' ';
    Parameters.ParamByName('Stats').Value:=2;
    Open;
  end;
end;

procedure TfrmZHDJInPut.DYIDKeyPress(Sender: TObject; var Key: Char);
var
  FZIID,FLeiBie,FZIIDYS,fsj:String;
begin
  if Key=#13 then
  begin
    InitOldYP();
    if ADOTemp.IsEmpty then
    begin
      if Copy(Trim(DYID.Text),1,4)='DYKC' then
      begin
        with ADOTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from ZH_CP_Info_KC where DYID='''+Trim(DYID.Text)+'''');
          Open;
        end;
        FZIID:=Trim(ADOTemp.fieldbyname('ZIID').AsString);
        DYID.Hint:=Trim(DYID.Text);
        if Trim(FZIID)='' then
        begin
          PlaySound('条码错误.wav', 0, SND_FILENAME or SND_ASYNC);
          DYID.Text:='';
          DYID.Hint:='';
          Exit;
        end;
      end;
      if Copy(Trim(DYID.Text),1,3)='Y20' then
      begin
        with ADOTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from ZH_CP_Info where ZIID='''+Trim(DYID.Text)+'''');
          Open;
        end;
        if ADOTemp.IsEmpty then
        begin
          PlaySound('条码错误.wav', 0, SND_FILENAME or SND_ASYNC);
          DYID.Text:='';
          DYID.Hint:='';
          Exit;
        end;
        FZIID:=Trim(DYID.Text);
        DYID.Hint:=Trim(DYID.Text);
      end;

      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from ZH_CP_Info where ZIID='''+Trim(FZIID)+'''');
        Open;
      end;
      FZIIDYS:=Trim(ADOTemp.fieldbyname('ZIIDYS').AsString);
      if Order_Sub.IsEmpty then
      begin
        FLeiBie:='登记样';
      end else
      begin
        FLeiBie:=Trim(Order_Sub.fieldbyname('LeiBie').AsString);
      end;
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select A.*,ZIGYYS=B.ZIGY ');
        sql.Add(' ,ImageFileTP=(select ImageFile from Image_Info B inner join Image_File C on B.IMID=C.IMID ');
        sql.Add('   where B.WBID=A.ZIID and B.IMNO=''图片'' and B.Valid=''Y'' and C.Valid=''Y'' and C.IFType=''小'' )');
        sql.Add(',GS=(select Sum(GGQty) from ZH_CP_Info_KC KC where KC.ZIID=A.ZIID )');
        sql.Add(' from ZH_CP_Info A ');
        sql.Add(' left join ZH_CP_Info B on A.ZIIDYS=B.ZIID ');
        sql.Add('  where A.ZIID='''+Trim(FZIID)+'''');
        Open;
      end;
      if Copy(DYID.Text,1,4)='TGML' then
      begin
        with ADOTemp do
        begin
          Close;
          SQL.Clear;
          SQL.Add('select Top 1 B.*,E.*,MLDLNo SKID,MLPinYin=dbo.getPinYin(A.MLName) ');
          sql.Add(',ColGS=(select count(B.TJML) from Image_Info B where B.TJML=A.TJML)');
          sql.Add(',ImageFile ImageFileTP,GS=cast(1 as decimal(18,2)),ZICost=cast(0 as decimal(18,2))');
          sql.Add('  ,A.MLID,A.MLNo,A.MLName ZIName,A.MLMF ZIMF,A.MLKZ ZIKZ,A.MBPID,A.MLCF ZICF,');
          sql.Add('  A.MLType,A.GYLX,A.TJGYName ZIGY,A.GYType,A.GYTypeJC');
          sql.Add(' from ML_Manage A ');
          sql.Add(' left join Image_Info B on B.TJML=A.TJML ');
          sql.Add(' left join Image_File E on B.IMID=E.IMID ');
          sql.Add(' left join BP_DY C on C.TFID=B.IMID and C.MLID=A.MLID');
          sql.Add(' left join BP_DY D on D.TFID=A.PTID and D.MLID=A.MLID');
          sql.Add(' where isnull(A.MLType,'''')=''烫金面料'' and isnull(C.DYID,D.DYID)='''+trim(DYID.Text)+'''');
          Open;
        end;
        if ADOTemp.IsEmpty then
        begin
          PlaySound('条码错误.wav', 0, SND_FILENAME or SND_ASYNC);
          DYID.Text:='';
          DYID.Hint:='';
          Exit;
        end;
        FZIID:=Trim(DYID.Text);
        DYID.Hint:=Trim(DYID.Text);
      end;
      if Copy(Trim(DYID.Text),1,2)='QM' then
      begin
        with ADOTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select A.MXID,A.MXTime,A.RCGangNo,A.GangNo,A.JH,A.KgQty,A.YQty,A.MQty,A.KuWei,');
          sql.Add(' B.MYType,B.MYID,B.MLID,B.BPID,B.DYID,B.PTID,B.MYCode,B.MYName ZIName,B.MYColor, ');
          sql.Add(' B.MYColorNo,B.MYMF ZIMF,B.MYKZ ZIKZ,B.Note,B.Note2,A.MXNote,');
          sql.Add(' Case when isnull(A.RCGangNo,'''')='''' then A.GangNo else A.RCGangNo end As DYGangNo,');
          sql.Add(' C.GYLX,C.GYLXName ZIGY,C.MLCF ZICF,C.MLJWMD,C.GYType,GS=cast(1 as decimal(18,2)),ZICost=cast(0 as decimal(18,2))');
          sql.Add(' ,ImageFileTP=cast('''' as varchar(50))');
          sql.Add(' Case when ISNULL(A.MQty,0)>0 and ISNULL(A.KgQty,0)>0 ');
          sql.Add('  then Cast(CAST(A.MQty as decimal(18,1)) AS varchar(10))+''M''+''    '' ');
          sql.Add('      +Cast(CAST(A.KgQty as decimal(18,1)) AS varchar(10))+''Kg''+''    '' ');
          sql.Add('  When ISNULL(A.MQty,0)>0 and ISNULL(A.KgQty,0)=0 ');
          sql.Add('  then Cast(CAST(A.MQty as decimal(18,1)) AS varchar(10))+''M'' ');
          sql.Add('  When ISNULL(A.KgQty,0)>0 and ISNULL(A.MQty,0)=0    ');
          sql.Add('  then Cast(CAST(A.KgQty as decimal(18,1)) AS varchar(10))+''Kg''');
          sql.Add('  end as QtyStr  ');
          sql.Add('  from CK_MYSC_CR_MX A  ');
          sql.Add('  inner join CK_MYSC_CR B on A.MYID=B.MYID ');
          sql.Add('  left join ML_Manage C on B.MLID=C.MLID ');
          sql.Add(' Where A.MXID='''+trim(DYID.Text)+'''');
          SQL.Add(' or B.DYID='''+trim(DYID.Text)+'''');
          //ShowMessage(sql.Text);
          Open;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from RT_PTColor where PTID='''+Trim(ADOTemp.fieldbyname('PTID').AsString)+'''');
          Open;
        end;
        fsj:=Trim(ADOCmd.fieldbyname('ColDaiMa').AsString);
        if fsj<>'' then
        Panel66.Color:=StrToInt('$'+Trim(fsj));
        if ADOTemp.IsEmpty then
        begin
          PlaySound('条码错误.wav', 0, SND_FILENAME or SND_ASYNC);
          DYID.Text:='';
          DYID.Hint:='';
          Exit;
        end;
        FZIID:=Trim(DYID.Text);
        DYID.Hint:=Trim(DYID.Text);
      end;
      if Copy(DYID.Text,1,4)='DY20' then
      begin
        with ADOTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select A.DYID,');
          sql.Add('  B.MLID,B.MLNo,B.MLName ZIName,B.MLMF ZIMF,B.MLKZ ZIKZ,B.MBPID,B.MLCF ZICF,');
          sql.Add('  B.MLType,B.GYLX,B.GYLXName ZIGY,B.GYType,B.GYTypeJC,');
          sql.Add('  C.PTID,C.ColNamekh,C.ColNo,GS=cast(1 as decimal(18,2)),ZICost=cast(0 as decimal(18,2))');
          sql.Add('  ,D.ColNameEng,D.ColDaiMa ');
          sql.Add(' ,ImageFileTP=cast('''' as varchar(50))');
          sql.Add('  from BP_DY A  ');
          sql.Add('  inner join ML_Manage B on A.MLID=B.MLID  ');
          sql.Add('  inner join BP_ZDY BP on B.MBPID=BP.BPID ');
          sql.Add('  left join BP_ColZDY C on A.TFID=C.PTID and BP.SKID=C.SKID ');
          sql.Add('  left join RT_PTColor D on C.PTID=D.PTID  ');
          sql.Add(' Where A.DYID='''+trim(DYID.Text)+'''');
          //ShowMessage(sql.Text);
          Open;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from RT_PTColor where PTID='''+Trim(ADOTemp.fieldbyname('PTID').AsString)+'''');
          Open;
        end;
        fsj:=Trim(ADOCmd.fieldbyname('ColDaiMa').AsString);
        if fsj<>'' then
        Panel66.Color:=StrToInt('$'+Trim(fsj));
        if ADOTemp.IsEmpty then
        begin
          PlaySound('条码错误.wav', 0, SND_FILENAME or SND_ASYNC);
          DYID.Text:='';
          DYID.Hint:='';
          Exit;
        end;
        FZIID:=Trim(ADOTemp.fieldbyname('MLNo').Value);
        DYID.Hint:=Trim(DYID.Text);
      end;
    end
    else
    begin
      FZIID:=Trim(DYID.Text);
      DYID.Hint:=Trim(DYID.Text);
    end;
    if FZIIDYS='' then FZIIDYS:=Trim(DYID.Text);

    with Order_Sub do
    begin
      if Trim(DYID.Hint)<>'' then
      begin
        if Locate('DYID',Trim(DYID.Hint),[])=True then
        begin
          DYID.Text:='';
          DYID.Hint:='';
          PlaySound('已登记.wav', 0, SND_FILENAME or SND_ASYNC);
          Exit;
        end;
      end else
      begin
        if Locate('ZIID',Trim(FZIID),[])=True then
        begin
          DYID.Text:='';
          DYID.Hint:='';
          PlaySound('已登记.wav', 0, SND_FILENAME or SND_ASYNC);
          Exit;
        end;
      end;
      Append;
      FieldByName('DYID').Value:=Trim(DYID.Hint);
      FieldByName('ZIID').Value:=Trim(FZIID);
      FieldByName('ZIIDYS').Value:=Trim(FZIIDYS);
      FieldByName('ZIName').Value:=ADOTemp.fieldbyname('ZIName').Value;
      FieldByName('ZIMF').Value:=ADOTemp.fieldbyname('ZIMF').Value;
      FieldByName('ZIKZ').Value:=ADOTemp.fieldbyname('ZIKZ').Value;
      FieldByName('ZICF').Value:=ADOTemp.fieldbyname('ZICF').Value;
      FieldByName('ZIGY').Value:=ADOTemp.fieldbyname('ZIGY').Value;
      FieldByName('GS').Value:=ADOTemp.fieldbyname('GS').Value;
      FieldByName('ZICost').Value:=ADOTemp.fieldbyname('ZICost').Value;
      FieldByName('ImageFileTP').Value:=ADOTemp.fieldbyname('ImageFileTP').Value;
      FieldByName('ZIGYYS').Value:=ADOTemp.fieldbyname('ZIGY').Value;
      FieldByName('LeiBie').Value:=Trim(FLeiBie);
      FieldByName('ZICost').Value:=ADOTemp.fieldbyname('ZICost').Value;
      Post;
    end;
    DYID.Text:='';
    DYID.Hint:='';
    KHGY.Text:=Trim(ADOTemp.fieldbyname('ZIGY').AsString);
    PlaySound('登记成功.wav', 0, SND_FILENAME or SND_ASYNC);
    //ZIGY.Text:=Trim(KHGY.Text);
  end;

end;

procedure TfrmZHDJInPut.XSKeyPress(Sender: TObject; var Key: Char);
var
  FReal:Double;
  FXS:String;
begin
  if Key=#13 then
  begin
    if Order_Sub.IsEmpty then Exit;
    if Trim(BZ.Text)='$' then
    begin
      if Trim(HuiLv.Text)='' then
      begin
        Application.MessageBox('汇率不能为空!','提示',0);
        Exit;
      end;
      if TryStrToFloat(HuiLv.Text,FReal)=False then
      begin
        Application.MessageBox('汇率非法数字!','提示',0);
        Exit;
      end;
    end;
    FXS:=Trim(XS.Text);
    if Trim(FXS)='' then
    begin
      FXS:='1';
    end;
    if TryStrToFloat(FXS,FReal)=False then
    begin
      Application.MessageBox('系数非法数字!','提示',0);
      Exit;
    end;
    if Application.MessageBox('确定要更新所有的系数吗？','提示',32+4)<>IDYES then Exit;
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('XS').Value:=FXS;
        if Trim(Order_Sub.fieldbyname('ZICost').AsString)<>'' then
        begin
          if Trim(BZ.Text)='$' then
          begin
            FieldByName('BaoJia').Value:=SSWR(Order_Sub.fieldbyname('ZICost').Value*StrtoFloat(FXS)/StrtoFloat(HuiLv.Text));
            FieldByName('HeSuanStatus').Value:='已核算';
          end else
          begin
            FieldByName('BaoJia').Value:=SSWR(Order_Sub.fieldbyname('ZICost').Value*StrtoFloat(FXS));
            FieldByName('HeSuanStatus').Value:='已核算';
          end;
        end else
        begin
          FieldByName('HeSuanStatus').Value:=Null;
        end;
        Post;
        Next;
      end;
    end;
    Order_Sub.EnableControls;
  end;
end;

procedure TfrmZHDJInPut.v1ZICostPropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FXS,FHuiLv,FZiCost,FName:String;
  Freal:Double;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  FName:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  with Order_Sub do
  begin
    Edit;
    if Trim(mvalue)='' then
    begin
      if FName='XS' then
      begin
        Edit;
        FieldByName(FName).Value:=1;
        Post;
      end else
      begin
        FieldByName(FName).Value:=Null;
      end;
    end else
    begin
      FieldByName(FName).Value:=mvalue;
    end;
  end;
  FHuiLv:=HuiLv.Text;
  FXS:=Trim(Order_Sub.fieldbyname('XS').AsString);
  if FXS='' then
  begin
    if Trim(XS.Text)<>'' then
    begin
      FXS:=XS.Text;
    end else
    begin
      FXS:='1';
    end;
    with Order_Sub do
    begin
      Edit;
      FieldByName('XS').Value:=FXS;
      Post;
    end;
  end;
  FZiCost:=Trim(Order_Sub.fieldbyname('ZiCost').AsString);
  if Trim(FZiCost)='' then
  begin
    with Order_Sub do
    begin
      Edit;
      FieldByName('BaoJia').Value:=null;
      FieldByName('HeSuanStatus').Value:=Null;
      post;
    end;
    Exit;
  end else
  begin
    if Trim(BZ.Text)='$' then
    begin
      if Trim(HuiLv.Text)='' then
      begin
        Application.MessageBox('汇率不能为空!','提示',0);
        Exit;
      end;
      if TryStrToFloat(HuiLv.Text,FReal)=False then
      begin
        Application.MessageBox('汇率非法数字!','提示',0);
        Exit;
      end;
    end else
    begin
      FHuiLv:='1';
    end;
    with Order_Sub do
    begin
      Edit;
      FieldByName('BaoJia').Value:=SSWR(StrtoFloat(FZiCost)*StrtoFloat(FXS)/StrtoFloat(FHuiLv));
      FieldByName('HeSuanStatus').Value:='已核算';
      Post;
    end;
  end;
end;

procedure TfrmZHDJInPut.HuiLvKeyPress(Sender: TObject; var Key: Char);
var
  FReal:Double;
  FXS:String;
begin
  if Key=#13 then
  begin
    if Order_Sub.IsEmpty then Exit;
    if Trim(BZ.Text)='$' then
    begin
      if Trim(HuiLv.Text)='' then
      begin
        Application.MessageBox('汇率不能为空!','提示',0);
        Exit;
      end;
      if TryStrToFloat(HuiLv.Text,FReal)=False then
      begin
        Application.MessageBox('汇率非法数字!','提示',0);
        Exit;
      end;
    end;
    FXS:=Trim(XS.Text);
    if Trim(FXS)='' then
    begin
      FXS:='1';
    end;
    if TryStrToFloat(FXS,FReal)=False then
    begin
      Application.MessageBox('系数非法数字!','提示',0);
      Exit;
    end;
    if Application.MessageBox('确定要更新汇率吗？','提示',32+4)<>IDYES then Exit;
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        if Trim(Order_Sub.fieldbyname('ZICost').AsString)<>'' then
        begin
          if Trim(BZ.Text)='$' then
          begin
            FieldByName('BaoJia').Value:=SSWR(Order_Sub.fieldbyname('ZICost').Value*StrtoFloat(FXS)/StrtoFloat(HuiLv.Text));
            FieldByName('HeSuanStatus').Value:='已核算';
          end else
          begin
            FieldByName('BaoJia').Value:=SSWR(Order_Sub.fieldbyname('ZICost').Value*StrtoFloat(FXS));
            FieldByName('HeSuanStatus').Value:='已核算';
          end;
        end else
        begin
          FieldByName('HeSuanStatus').Value:=Null;
        end;
        Post;
        Next;
      end;
    end;
    Order_Sub.EnableControls;
  end;
end;

procedure TfrmZHDJInPut.BZChange(Sender: TObject);
var
  FReal:Double;
  FXS:String;
begin
  //if Key=#13 then
  begin
    if Order_Sub.IsEmpty then Exit;
    if Trim(BZ.Text)='$' then
    begin
      if Trim(HuiLv.Text)='' then Exit;
      if TryStrToFloat(HuiLv.Text,FReal)=False then
      begin
        Application.MessageBox('汇率非法数字!','提示',0);
        Exit;
      end;
    end;
    FXS:=Trim(XS.Text);
    if Trim(FXS)='' then
    begin
      FXS:='1';
    end;
    if TryStrToFloat(FXS,FReal)=False then
    begin
      Application.MessageBox('系数非法数字!','提示',0);
      Exit;
    end;
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        if Trim(Order_Sub.fieldbyname('ZICost').AsString)<>'' then
        begin
          if Trim(BZ.Text)='$' then
          begin
            FieldByName('BaoJia').Value:=SSWR(Order_Sub.fieldbyname('ZICost').Value*StrtoFloat(FXS)/StrtoFloat(HuiLv.Text));
            FieldByName('HeSuanStatus').Value:='已核算';
          end else
          begin
            FieldByName('BaoJia').Value:=SSWR(Order_Sub.fieldbyname('ZICost').Value*StrtoFloat(FXS));
            FieldByName('HeSuanStatus').Value:='已核算';
          end;
        end else
        begin
          FieldByName('HeSuanStatus').Value:=Null;
        end;
        Post;
        Next;
      end;
    end;
    Order_Sub.EnableControls;
  end;
end;

procedure TfrmZHDJInPut.YWYBtnClick(Sender: TObject);
begin
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KH_Main where isnull(KHNameJC,'''')='''+Trim(KHName.Text)+'''');
    sql.add(' and isnull(KHFlag,'''')='''+Trim('KH')+''' and isnull(Valid,'''')=''Y''  ');
    Open;
  end;
  if ADOTemp.IsEmpty=False then
  begin
    if Trim(canshu2)<>'高权限' then
    begin
      Exit;
    end;
  end;
  try
    frmInfoBtn:=TfrmInfoBtn.Create(Application);
    with frmInfoBtn do
    begin
      FSql:='select XSInfo=UserName from SY_User where USERID<>''ADMIN'' ';
      if ShowModal=1 then
      begin
        YWY.Text:=Trim(FXSInfo);
      end;
    end;
  finally
    frmInfoBtn.Free;
  end;
end;

procedure TfrmZHDJInPut.KHNameBtnClick(Sender: TObject);
begin
  try
    frmKHListSelJJ:=TfrmKHListSelJJ.Create(Application);
    with frmKHListSelJJ do
    begin
      frmKHListSelJJ.canshu2:=Trim(Self.canshu2);
      if ShowModal=1 then
      begin
        Self.KHName.Text:=Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHNameJC').AsString);
        Self.KHName.TxtCode:=Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHNo').AsString);
        Self.GuoJia.Text:=Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHQuYu').AsString);
        Self.YWY.Text:=Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('YWY').AsString);
        Self.LiDan.Text:=Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('LiDan').AsString);
      end;
    end;
  finally
    frmKHListSelJJ.Free;
  end;
end;

procedure TfrmZHDJInPut.Tv1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Order_Sub.IsEmpty then
  begin
    KHGY.Text:='';
  end else
  begin
    KHGY.Text:=Order_Sub.fieldbyname('ZIGY').AsString;
  end;
end;

procedure TfrmZHDJInPut.KHGYChange(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  with Order_Sub do
  begin
    Edit;
    FieldByName('ZIGY').Value:=KHGY.Text;
    Post;
  end;
end;

procedure TfrmZHDJInPut.N1Click(Sender: TObject);
begin
  SelOKNo(Order_Sub,True);
end;

procedure TfrmZHDJInPut.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Sub,False);
end;

procedure TfrmZHDJInPut.N3Click(Sender: TObject);
begin
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from ZH_KH_DJ_CP where ZCID='''+Trim(Order_Sub.fieldbyname('ZCID').AsString)+'''');
    Open;
  end;
  if ADOTemp.IsEmpty then
  begin
    Order_Sub.Delete;
    Exit;
  end;
  try
    ADOCmd.Connection.BeginTrans;
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      //First;
     // while Order_Main.Locate('SSel',True,[]) do
      begin
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' UPdate ZH_KH_DJ_CP Set DelTime=getdate(),DelerCode='''+Trim(DCode)+''',Deler='''+Trim(DName)+''', Valid=''N'' ');
          sql.Add(' where ZCID='''+Trim(Order_Sub.fieldbyname('ZCID').AsString)+'''');
          ExecSQL;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update ZH_CP_Info_KC Set GGQty=1 ');
          sql.Add(' where DYID='''+Trim(Order_Sub.fieldbyname('DYID').AsString)+''' and isnull(KWID,'''')<>'''' ');
          ExecSQL;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update ZH_KH_DJ Set DJYang=(select Count(*) from ZH_KH_DJ_CP A where A.ZDID=ZH_KH_DJ.ZDID and A.LeiBie=''登记样'' and A.Valid=''Y'' )');
          sql.Add(',NaYang=(select Count(*) from ZH_KH_DJ_CP A where A.ZDID=ZH_KH_DJ.ZDID and A.LeiBie=''拿样'' and A.Valid=''Y'' )');
          sql.Add(' where ZDID='''+Trim(Order_Sub.fieldbyname('ZDID').AsString)+'''');
          ExecSQL;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update ZH_KH_DJ Set XunYang=(select Count(*) from ZH_KH_DJ_CP A where A.ZDID=ZH_KH_DJ.ZDID and A.Valid=''Y''  )');
          sql.Add(' where ZDID='''+Trim(Order_Sub.fieldbyname('ZDID').AsString)+'''');
          ExecSQL;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update ZH_CP_Info Set KHNameHZ=dbo.F_Get_HZStr(ZIID,''KHNaYang'')');
          sql.Add(' where ZIID='''+Trim(Order_Sub.fieldbyname('ZDID').AsString)+'''');
          ExecSQL;
        end;
        //Order_Sub.Delete;
      end;
    end;
    Order_Sub.EnableControls;
    ADOCmd.Connection.CommitTrans;
    Order_Sub.Delete;
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmZHDJInPut.TBPrtBJClick(Sender: TObject);
var
   fPrintFile,Txt,fImagePath,maxno:string;
   i,j:Integer;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin
  if Trim(FMainId)='' then
  begin
    Application.MessageBox('没有保存数据，不能打印!','提示',0);
    Exit;
  end;
  Order_Sub.DisableControls;
  with Order_Sub do
  begin
    First;
    while not Eof do
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from ZH_KH_DJ_CP where ZCID='''+Trim(Order_Sub.fieldbyname('ZCID').AsString)+'''');
        Open;
      end;
      if ADOTemp.IsEmpty then
      begin
        Application.MessageBox('没有保存数据，不能打印!','提示',0);
        Exit;
      end;
      Next;
    end;
  end;
  Order_Sub.EnableControls;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\展会产品标签BJ85.rmf';
  if FileExists(fPrintFile)=False then
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\展会产品标签BJ85.rmf'),'提示',0);
    exit;
  end;
  if Order_Sub.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    exit;
  end;
  Order_Sub.DisableControls;
  with Order_Sub do
  begin
    First;
    while Locate('SSel',True,[]) do
    begin
      with ADOTemp do
      begin
        Close;
        SQL.Clear;
        sql.Add(' select A.* ');
        sql.Add(' from ZH_KH_DJ_CP A');
        sql.Add(' where A.ZCID='''+Trim(Order_Sub.fieldbyname('ZCID').AsString)+'''');
        Open;
      end;
      SCreateCDS20(ADOTemp,CDS_PRT);
      SInitCDSData20(ADOTemp,CDS_PRT);
      try
        Moudle:=LoadLibrary('MakeQRBarcode.dll');
        @Makebar:=GetProcAddress(Moudle,'Make');
        @Mixtext:=GetProcAddress(Moudle,'MixText');
        Txt:=Trim(CDS_PRT.fieldbyname('DYID').AsString);
        fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp.bmp';
        if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName)+'image')) then
          CreateDirectory(pchar(ExtractFilePath(Application.ExeName)+'image'),nil);
        if FileExists(fImagePath) then DeleteFile(fImagePath);
        Makebar(pchar(Txt),Length(Txt),3,3,0,PChar(fImagePath),3);
      except
        application.MessageBox('二维码生成失败！','提示信息',MB_ICONERROR);
        exit;
      end;
      RMVariables['QRBARCODE']:=fImagePath;
      RM1.LoadFromFile(fPrintFile);
      //RM1.ShowReport;
      RM1.PrintReport;
      Edit;
      FieldByName('SSel').Value:=False;
    end;
  end;
  Order_Sub.EnableControls;

end;

procedure TfrmZHDJInPut.LiDanBtnClick(Sender: TObject);
begin
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KH_Main where isnull(KHNameJC,'''')='''+Trim(KHName.Text)+'''');
    sql.add(' and isnull(KHFlag,'''')='''+Trim('KH')+''' and isnull(Valid,'''')=''Y''  ');
    Open;
  end;
  if ADOTemp.IsEmpty=False then
  begin
    if Trim(canshu2)<>'高权限' then
    begin
      Exit;
    end;
  end;
  try
    frmInfoBtn:=TfrmInfoBtn.Create(Application);
    with frmInfoBtn do
    begin
      FSql:='select XSInfo=UserName from SY_User where USERID<>''ADMIN'' ';
      if ShowModal=1 then
      begin
        LiDan.Text:=Trim(FXSInfo);
      end;
    end;
  finally
    frmInfoBtn.Free;
  end;
end;

procedure TfrmZHDJInPut.v1Column5PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with Order_Sub do
  begin
    Edit;
    if Trim(mvalue)='' then
    begin
      FieldByName('BaoJiaYW').Value:=Null;
      if Trim(fieldbyname('HeSuanStatus').AsString)='' then
      begin
        FieldByName('BaoJia').Value:=Null;
      end;
    end else
    begin
      FieldByName('BaoJiaYW').Value:=mvalue;
      if Trim(fieldbyname('BaoJia').AsString)='' then
      begin
        FieldByName('BaoJia').Value:=mvalue;
      end else
      begin
        if Trim(fieldbyname('HeSuanStatus').AsString)='' then
        begin
          FieldByName('BaoJia').Value:=mvalue;
        end;
      end;
    end;
  end;
end;

procedure TfrmZHDJInPut.v1BaoJiaCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  id,id10:Integer;
begin
  Id:=TV1.GetColumnByFieldName('HeSuanStatus').Index;
  Id10:=TV1.GetColumnByFieldName('BaoJia').Index;
  if AViewInfo.GridRecord.Values[id]=null then
  begin
    if AViewInfo.GridRecord.Values[id10]<>null then
    begin
      if AViewInfo.GridRecord.Values[id10]<>0 then
      begin
        ACanvas.Brush.Color:=clRed;
      end;
    end;
  end;
end;

procedure TfrmZHDJInPut.v1Column3CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  id:Integer;
begin
  Id:=TV1.GetColumnByFieldName('LeiBie').Index;//;-TV1.GroupedItemCount;
  if Trim(AViewInfo.GridRecord.Values[id])='拿走吊卡'  then
  begin
    ACanvas.Brush.Color:=clSkyBlue;
  end;
end;

procedure TfrmZHDJInPut.Tv1DblClick(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  try
    frmZHCPInPut:=TfrmZHCPInPut.Create(Application);
    with frmZHCPInPut do
    begin
      PState:=1;
      FMainId:=Trim(Order_Sub.fieldbyname('ZIID').AsString);
      TBSave.Visible:=False;
      if ShowModal=1 then
      begin
        //InitGrid();
      end;
    end;
  finally
    frmZHCPInPut.Free;
  end;
end;

procedure TfrmZHDJInPut.Button1Click(Sender: TObject);
var
  FZIID,FLeiBie,FZIIDYS,fsj:String;
begin
  frmZHCPList_BJCX:=TfrmZHCPList_BJCX.create(self);
  with frmZHCPList_BJCX do
  begin
    if ShowModal=1 then
    begin
      FZIID:=Trim(Order_Main.fieldbyname('ZIID').AsString);
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from ZH_CP_Info where ZIID='''+Trim(FZIID)+'''');
        Open;
      end;
      FZIIDYS:=Trim(ADOTemp.fieldbyname('ZIIDYS').AsString);
      if Order_Sub.IsEmpty then
      begin
        FLeiBie:='登记样';
      end else
      begin
        FLeiBie:=Trim(Order_Sub.fieldbyname('LeiBie').AsString);
      end;
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select A.*,ZIGYYS=B.ZIGY ');
        sql.Add(' ,ImageFileTP=(select ImageFile from Image_Info B inner join Image_File C on B.IMID=C.IMID ');
        sql.Add('             where B.WBID=A.ZIID and B.IMNO=''图片'' and B.Valid=''Y'' and C.Valid=''Y'' and C.IFType=''小'' )');
        sql.Add(',GS=(select Sum(GGQty) from ZH_CP_Info_KC KC where KC.ZIID=A.ZIID )');
        sql.Add(',DYID=(select Top 1 DYID from ZH_CP_Info_KC KC where KC.ZIID=A.ZIID )');
        sql.Add(' from ZH_CP_Info A ');
        sql.Add(' inner join ZH_CP_Info B on A.ZIIDYS=B.ZIID ');
        sql.Add('  where A.ZIID='''+Trim(FZIID)+'''');
        Open;
      end;
      with Order_Sub do
      begin
        if Locate('ZIID',Trim(FZIID),[])=True then
        begin
          DYID.Text:='';
          DYID.Hint:='';
          application.MessageBox('已登记,不能重复登记','提示');
          Exit;
        end;
        Append;
        FieldByName('DYID').Value:=Trim(ADOTemp.fieldbyname('DYID').Value);
        FieldByName('ZIID').Value:=Trim(FZIID);
        FieldByName('ZIIDYS').Value:=Trim(FZIIDYS);
        FieldByName('ZIName').Value:=ADOTemp.fieldbyname('ZIName').Value;
        FieldByName('ZIMF').Value:=ADOTemp.fieldbyname('ZIMF').Value;
        FieldByName('ZIKZ').Value:=ADOTemp.fieldbyname('ZIKZ').Value;
        FieldByName('ZICF').Value:=ADOTemp.fieldbyname('ZICF').Value;
        FieldByName('ZIGY').Value:=ADOTemp.fieldbyname('ZIGY').Value;
        FieldByName('GS').Value:=ADOTemp.fieldbyname('GS').Value;
        FieldByName('ZICost').Value:=ADOTemp.fieldbyname('ZICost').Value;
        FieldByName('ImageFileTP').Value:=ADOTemp.fieldbyname('ImageFileTP').Value;
        FieldByName('ZIGYYS').Value:=ADOTemp.fieldbyname('ZIGY').Value;
        FieldByName('LeiBie').Value:=Trim(FLeiBie);
        FieldByName('ZICost').Value:=ADOTemp.fieldbyname('ZICost').Value;
        Post;
      end;
      KHGY.Text:=Trim(ADOTemp.fieldbyname('ZIGY').AsString);
    end;
    free;
  end;
end;

end.
