unit U_ConInPutNX;

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, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator;

type
  TfrmConInPutNX = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    Label3: TLabel;
    OrdDate: TDateTimePicker;
    Label4: TLabel;
    DlyDate: TDateTimePicker;
    Label7: TLabel;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    v1PRTColor: TcxGridDBColumn;
    v1PRTPrice: TcxGridDBColumn;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    v1PRTOrderQty: TcxGridDBColumn;
    Label9: TLabel;
    ConPerson1: TEdit;
    Label10: TLabel;
    ConNo: TEdit;
    Label11: TLabel;
    Label14: TLabel;
    Label20: TLabel;
    FromPlace: TBtnEditC;
    CustomerNoName: TBtnEditC;
    v1XHNo: TcxGridDBColumn;
    SYRName: TBtnEditC;
    v1OrderUnit: TcxGridDBColumn;
    v1PriceUnit: TcxGridDBColumn;
    Label2: TLabel;
    MPRTCodeName: TBtnEditC;
    Label6: TLabel;
    QtyNote: TBtnEditC;
    Label12: TLabel;
    MPRTMF: TBtnEditC;
    Label13: TLabel;
    MPRTKZ: TBtnEditC;
    Label15: TLabel;
    Payment: TBtnEditC;
    Label1: TLabel;
    ShippMent: TBtnEditC;
    Label17: TLabel;
    Label18: TLabel;
    BankNo: TBtnEditC;
    SYRAddress: TEdit;
    Label19: TLabel;
    BankName: TEdit;
    Label21: TLabel;
    CTMAddress: TEdit;
    ToolButton3: TToolButton;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    Label8: TLabel;
    TelNo: TEdit;
    Label23: TLabel;
    FaxNo: TEdit;
    DlyNote: TBtnEditC;
    Label25: TLabel;
    CPTanLi: TCheckBox;
    CPRanYin: TRadioGroup;
    Label26: TLabel;
    CPRanHouGY: TBtnEditC;
    CPType: TRadioGroup;
    Label27: TLabel;
    CPZHName: TEdit;
    CDS_Type: TClientDataSet;
    CPCFNo: TEdit;
    Label5: TLabel;
    ZhiLiangNote: TBtnEditC;
    Label16: TLabel;
    ChuanYangNote: TBtnEditC;
    Label28: TLabel;
    YSBiaoZhunNote: TBtnEditC;
    Label29: TLabel;
    OtherNote: TBtnEditC;
    Label22: TLabel;
    Label24: TLabel;
    CTMTelNo: TEdit;
    CTMFaxNo: TEdit;
    Label30: TLabel;
    MPRTSpec: TBtnEditC;
    Label31: TLabel;
    Note: TBtnEditC;
    Label32: TLabel;
    SelfBankNo: TBtnEditC;
    Label33: TLabel;
    CPCFBi: TEdit;
    Label34: TLabel;
    KHConNo: TEdit;
    Label35: TLabel;
    CTMBankNo: TBtnEditC;
    Label36: TLabel;
    CTMBankName: TEdit;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    Label37: TLabel;
    condefstr10: TEdit;
    v1Column8: TcxGridDBColumn;
    istax: TComboBox;
    Label38: TLabel;
    v1Column9: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1PRTColorPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure CustomerNoNameBtnUpClick(Sender: TObject);
    procedure CustomerNoNameBtnDnClick(Sender: TObject);
    procedure v1Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure MPRTCodeNameBtnUpClick(Sender: TObject);
    procedure v1OrderUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1PriceUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure SYRNameBtnUpClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure CPCFNoDblClick(Sender: TObject);
    procedure CPCFNoKeyPress(Sender: TObject; var Key: Char);
    procedure CPRanHouGYBtnUpClick(Sender: TObject);
    procedure CPTanLiClick(Sender: TObject);
    procedure CPRanYinClick(Sender: TObject);
    procedure CPTypeClick(Sender: TObject);
    procedure CPRanHouGYBtnDnClick(Sender: TObject);
    procedure CPRanHouGYChange(Sender: TObject);
    procedure CPCFNoChange(Sender: TObject);
    procedure v1Column4PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column6PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column7PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column8PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
        fuserName:string;
    procedure InitData();
    procedure ZDYHelp(FButn:TcxButtonEdit;LType:string);
    function  SaveData():Boolean;
    function  SaveDataSubOne():Boolean;
    function  SaveDataMain():Boolean;
    function SaveDataSubMore():Boolean;
    procedure GetName();
    { Private declarations }
  public
    PState,CopyInt:Integer;
    FMainId,FFMainId:String;
    FXS:Integer;
    { Public declarations }
  end;

var
  frmConInPutNX: TfrmConInPutNX;
  newh:hwnd;
implementation
uses
  U_DataLink,U_ZDYHelp,U_Fun,U_ZDYHelpSel, U_ZdyAttachment, U_ZdyAttachCP;

{$R *.dfm}

procedure TfrmConInPutNX.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('合同录入内销1',Tv1,'生产指示单管理');
end;

procedure TfrmConInPutNX.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KH_Zdy where Type=''MPRTNameType'' ');
    Open;
  end;
  SCreateCDS20(ADOQuery1,CDS_Type);
  SInitCDSData20(ADOQuery1,CDS_Type);
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from JYOrderCon_Sub ');
    if PState=1 then
    begin
      sql.Add('where MainId='''+Trim(FMainId)+'''');
    end;
    if PState=0 then
    begin
      sql.Add(' where 1<>1');
    end;
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrderCon_Main where MainId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCSHDataNew(ADOQuery1,ScrollBox1,2);
  if Trim(ADOQuery1.fieldbyname('CPTanLi').AsString)='弹力' then
  begin
    CPTanLi.Checked:=True;
  end else
  begin
    CPTanLi.Checked:=False;
  end;
  IF not ADOQuery1.IsEmpty then
  begin
    istax.ItemIndex:=0;
    IF  ADOQuery1.fieldbyname('istax').AsBoolean then
    istax.ItemIndex:=1;
  end;

  if Trim(ADOQuery1.fieldbyname('CPRanYin').AsString)='染色' then
  begin
    CPRanYin.ItemIndex:=0;
  end else
  if Trim(ADOQuery1.fieldbyname('CPRanYin').AsString)='印花' then
  begin
    CPRanYin.ItemIndex:=1;
  end else
  begin
    CPRanYin.ItemIndex:=2;
  end;
  
  if Trim(ADOQuery1.fieldbyname('CPType').AsString)='针织布' then
  begin
    CPType.ItemIndex:=0;
  end else
  if Trim(ADOQuery1.fieldbyname('CPType').AsString)='梭织布' then
  begin
    CPType.ItemIndex:=1;
  end;
  CPZHName.Text:=Trim(ADOQuery1.fieldbyname('CPZHName').AsString);
  SYRName.TxtCode:=Trim(ADOQuery1.fieldbyname('ConDefStr2').AsString);
  if PState=0 then
  begin
    OrdDate.DateTime:=SGetServerDateTime(ADOTemp);
    DlyDate.DateTime:=SGetServerDateTime(ADOTemp)+10;
    ConPerson1.Text:=Trim(DName);
    FromPlace.Text:='绍兴';
    CPZHName.Text:='';
    ConNo.Text:='自动生成';
    ShippMent.Text:='自提';
    Payment.Text:='贷款提货';
    QtyNote.Text:='+-3%';
    SYRName.Text:='绍兴县谷甄贸易有限公司';
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select Top 1* from JYOrderCon_Main where SyrName='''+Trim(SYRName.Text)+'''');
      sql.Add(' and MPRTTYpe=''内销'' ');
      sql.Add(' order by FillTime desc');
      Open;
      Self.SYRAddress.Text:=Trim(ADOTemp.fieldbyname('SYRAddress').AsString);
      Self.TelNo.Text:=Trim(ADOTemp.fieldbyname('TelNo').AsString);
      Self.FaxNo.Text:=Trim(ADOTemp.fieldbyname('FaxNo').AsString);
      Self.bankNo.Text:=Trim(ADOTemp.fieldbyname('bankNo').AsString);
      Self.bankName.Text:=Trim(ADOTemp.fieldbyname('bankName').AsString);
    end;
  end
  else
  begin
  end;
  if CopyInt=99 then
  begin
    PState:=0;
    FMainId:='';
    ConPerson1.Text:=Trim(DName);
    ConNo.Text:='自动生成';
    SYRName.Text:='绍兴县谷甄贸易有限公司';
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value:='';
        FieldByName('SubId').Value:='';
        Post;
        Next;
      end;
    end;
  end;
end;

procedure TfrmConInPutNX.ZDYHelp(FButn:TcxButtonEdit;LType:string);
var
  FType,ZDYName,FText:String;
begin
end;

procedure TfrmConInPutNX.FormShow(Sender: TObject);
begin
  fuserName:=DCode;
  if (trim(DCode)='A1') or (trim(DCode)='A2') then
  begin
    fuserName:='A';
  end;
  readCxGrid('合同录入内销1',Tv1,'生产指示单管理');
  InitData();
end;

function TfrmConInPutNX.SaveData():Boolean;
var
  maxno:String;
  fconNO,fmxType:string;
begin
  try
    ///保存主表
    if Trim(FMainId)='' then
    begin
      if GetLSNo(ADOCmd,maxno,'JN','JYOrderCon_Main',3,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
      if GetLSNo(ADOCmd,fconNO,'CF','JYOrderCon_Main',3,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
      ConNo.Text:=uppercase(fconNO);
    end
    else
    begin
      maxno:=Trim(FMainId);
    end;
    
    ADOCmd.Connection.BeginTrans;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from JYOrderCon_Main where MainId='''+Trim(FMainId)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId)='' then
      begin
        Append;
      end
      else begin
        Edit;
      end;
      FieldByName('MainId').Value:=Trim(maxno);
      SSetsaveSqlNew(ADOCmd,'JYOrderCon_Main',ScrollBox1,2);
      if CPTanLi.Checked=True then
      begin
        FieldByName('CPTanLi').Value:='弹力';
      end else
      begin
        FieldByName('CPTanLi').Value:='无弹';
      end;

      if CPRanYin.ItemIndex=0 then
      begin
        FieldByName('CPRanYin').Value:='染色';
      end else
      if CPRanYin.ItemIndex=1 then
      begin
        FieldByName('CPRanYin').Value:='印花'
      end else if CPRanYin.ItemIndex=0 then
      begin
        FieldByName('CPRanYin').Value:='';
      end;
      
      if CPType.ItemIndex=0 then
      begin
        FieldByName('CPType').Value:='针织布';
      end else
      if CPType.ItemIndex=1 then
      begin
        FieldByName('CPType').Value:='梭织布';
      end;
      FieldByName('ConDefStr2').Value:=Trim(SYRName.TxtCode);
      if Trim(FMainId)='' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
      end;
      FieldByName('MPRTType').Value:='内销';
      IF trim(istax.Text)='含税' then
      FieldByName('istax').Value:=1
      else
      FieldByName('istax').Value:=0;
      Post;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrderCon_Main where ConNo='''+Trim(ConNo.Text)+'''');
      Open;
    end;
    if ADOCmd.RecordCount>1 then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('合同号重复!','提示',0);
      Exit;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate JYOrderCon_Main Set Filler='''+Trim(DName)+'''');
      sql.Add(' where MainId='''+Trim(FMainId)+'''');
      ExecSQL;
    end;
    FMainId:=Trim(maxno);
    ///保存子表
    
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('SubId').AsString)='' then
        begin
          if GetLSNo(ADOCmd,maxno,'JNX','JYOrderCon_Sub',4,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(Order_Sub.fieldbyname('SubId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrderCon_Sub where MainId='''+Trim(FMainId)+'''');
          sql.Add(' and SubId='''+Trim(maxno)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('SubId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('MainId').Value:=Trim(FMainId);
          FieldByName('SubId').Value:=Trim(maxno);
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'JYOrderCon_Sub',0);
          FieldByName('prtcode').Value:=Trim(Order_Sub.fieldbyname('PrtCode').AsString);
          if Trim(fieldbyname('PRTOrderQty').AsString)='' then
          begin
            fieldbyname('PRTOrderQty').Value:=0
          end;
          if Trim(fieldbyname('PRTPrice').AsString)='' then
          begin
            fieldbyname('PRTPrice').Value:=0
          end;
          fieldbyname('huilv').Value:=Order_Sub.fieldbyname('huilv').AsFloat;
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('SubId').Value:=Trim(maxno);
        //Order_Sub.Post;
        Next;
      end;
    end;
    with ADOQuery1 do
    begin
      close;
      sql.Clear;
      sql.Add('select PrtCode,COUNT(distinct PrtPrice) as PrtPrice ');
      sql.Add('from JYOrdercon_sub A ');
      sql.Add('where MainID='''+trim(FMainId)+''' ');
      sql.Add('group by PrtCode ');
      sql.Add('having COUNT(distinct PrtPrice)>1 ');
      open;
    end;
    IF not ADOQuery1.IsEmpty then
    begin
      ADOCmd.Connection.RollbackTrans;
      application.MessageBox('有相同产品编号存在不同单价，不能保存！','提示信息',MB_ICONERROR);
      exit;
    end;

    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except

    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
    Result:=False;
  end;
end;
function TfrmConInPutNX.SaveDataMain():Boolean;
var
  maxno:String;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from JYOrder_Main where MainId='''+Trim(FMainId)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId)='' then
      begin
        Append;
        maxno:=Trim(FFMainId);
      end
      else begin
        maxno:=Trim(FMainId);
        Edit;
      end;
      FieldByName('MainId').Value:=Trim(maxno);
      SSetsaveSqlNew(ADOCmd,'JYOrder_Main',ScrollBox1,2);
      if PState=1 then
      begin
        FieldByName('OrdUpDate').Value:=SGetServerDateTime(ADOTemp);
      end;
      if Trim(FMainId)='' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
      end;
      Post;
    end;
    FMainId:=Trim(maxno);
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except

    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
    Result:=False;
  end;
end;
function TfrmConInPutNX.SaveDataSubOne():Boolean;
var
  maxno:String;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存子表
    with Order_Sub do
    begin
      //First;
      //while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('SubId').AsString)='' then
        begin
          if GetLSNo(ADOTemp,maxno,'JS','JYOrder_Sub',4,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(Order_Sub.fieldbyname('SubId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_Sub where MainId='''+Trim(FMainId)+'''');
          sql.Add(' and SubId='''+Trim(maxno)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('SubId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('MainId').Value:=Trim(FMainId);
          FieldByName('SubId').Value:=Trim(maxno);
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'JYOrder_Sub',0);
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('SubId').Value:=Trim(maxno);
        //Order_Sub.Post;
        //Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常！','提示',0);
  end;
end;
function TfrmConInPutNX.SaveDataSubMore():Boolean;
var
  maxno:String;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存子表
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('SubId').AsString)='' then
        begin
          if GetLSNo(ADOTemp,maxno,'JS','JYOrder_Sub',4,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(Order_Sub.fieldbyname('SubId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_Sub where MainId='''+Trim(FMainId)+'''');
          sql.Add(' and SubId='''+Trim(maxno)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('SubId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('MainId').Value:=Trim(FMainId);
          FieldByName('SubId').Value:=Trim(maxno);
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'JYOrder_Sub',0);
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('SubId').Value:=Trim(maxno);
        //Order_Sub.Post;
        Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常！','提示',0);
  end;
end;

procedure TfrmConInPutNX.TBSaveClick(Sender: TObject);
begin
  OrdDate.SetFocus;
  if Trim(ConNo.Text)='' then
  begin
    Application.MessageBox('合同号不能为空！','提示',0);
    Exit;
  end;
  if Trim(condefstr10.Text)='' then
  begin
    Application.MessageBox('客户简称不能为空！','提示',0);
    Exit;
  end;
  IF trim(istax.Text)='' then
  begin
    Application.MessageBox('是否含税不能为空！','提示',0);
    Exit;
  end;
  IF  Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细信息不能为空！','提示',0);
    Exit;
  end;
  with  Order_Sub do
  begin
    first;
    while not eof do
    begin
      IF Order_Sub.FieldByName('PRTOrderQty').AsFloat=0  then
      begin
        application.MessageBox('数量不能为空或者零！','提示信息',MB_ICONERROR);
        exit;
      end;
      IF Order_Sub.FieldByName('PRTPrice').AsFloat=0  then
      begin
        application.MessageBox('单价不能为空或者零！','提示信息',MB_ICONERROR);
        exit;
      end;
      next;
    end;
    first;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
  end;
end;

procedure TfrmConInPutNX.v1PRTColorPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrdColor';
      flagname:='颜色';
      V1Name.Caption:='中文名称';
      V1Note.Caption:='英文名称';
      fnote:=True;
    //  MainType:=Trim(DName);
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PRTColor').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        Self.Order_Sub.FieldByName('SOrdDefStr4').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmConInPutNX.ToolButton1Click(Sender: TObject);
var
  i:Integer;
begin
  if Trim(ConNo.Text)='' then
  begin
    Application.MessageBox('合同号不能为空!','提示',0);
    Exit;
  end;
  i:=Order_Sub.RecordCount;
  i:=i+1;
  CopyAddRow(Tv1,Order_Sub);
  with Order_Sub do
  begin
    Edit;
    FieldByName('XHNO').Value:=IntToStr(i);
    FieldByName('PRTColor').Value:='';
    FieldByName('PRTOrderQty').Value:=null;
    FieldByName('OrderUnit').Value:='M';
    FieldByName('PriceUnit').Value:='RMB';
    FieldByName('huilv').Value:=1;
   // FieldByName('PRTPrice').Value:=null;
    FieldByName('SOrddefstr4').Value:=null;
    //FieldByName('SOrddefstr2').Value:=null;
    Post;
  end;
end;

procedure TfrmConInPutNX.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  if Trim(Order_Sub.fieldbyname('SubId').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrderCon_Sub where SubId='''+Trim(Order_Sub.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
 { if Order_Sub.IsEmpty then
  begin
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Main where MainId='''+Trim(FMainId)+'''');
      ExecSQL;
    end;
  end;  }
end;

procedure TfrmConInPutNX.CustomerNoNameBtnUpClick(Sender: TObject);
begin
 { try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CustomerNoNameNX';
      flagname:='内销客户';
      if Trim(DParameters1)<>'高权限' then
        MainType:=Trim(DName);
      V1Note.Caption:='简称';
      fnote:=true;
      if ShowModal=1 then
      begin
        CustomerNoName.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        CustomerNoName.TxtCode:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        condefstr10.Text:=Trim(ClientDataSet1.fieldbyname('note').AsString);
        with ADOTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select Top 1* from JYOrderCon_Main where CustomerNoName='''+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
          sql.Add(' order by FillTime desc');
          Open;
          Self.CTMAddress.Text:=Trim(ADOTemp.fieldbyname('CTMAddress').AsString);
          Self.CTMTelNo.Text:=Trim(ADOTemp.fieldbyname('CTMTelNo').AsString);
          Self.CTMFaxNo.Text:=Trim(ADOTemp.fieldbyname('CTMFaxNo').AsString);
          Self.CTMbankNo.Text:=Trim(ADOTemp.fieldbyname('CTMbankNo').AsString);
          Self.CTMbankName.Text:=Trim(ADOTemp.fieldbyname('CTMbankName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;}

  try
    frmZdyAttachment:=TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      fkhType:='';
      if ShowModal=1 then
      begin
        CustomerNoName.Text:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
        CustomerNoName.TxtCode:=Trim(CDS_HZ.fieldbyname('ZDYCode').AsString);
        condefstr10.Text:=Trim(CDS_HZ.fieldbyname('ZDYNameZ').AsString);
        Self.CTMAddress.Text:=Trim(CDS_HZ.fieldbyname('DEFNote2').AsString);
        Self.CTMTelNo.Text:=Trim(CDS_HZ.fieldbyname('DEFstr1').AsString);
        Self.CTMFaxNo.Text:=Trim(CDS_HZ.fieldbyname('DEFstr3').AsString);
        with ADOTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select Top 1* from JYOrderCon_Main where CustomerNoName='''+Trim(CDS_HZ.fieldbyname('ZdyName').AsString)+'''');
          sql.Add(' order by FillTime desc');
          Open;
          Self.CTMbankNo.Text:=Trim(ADOTemp.fieldbyname('CTMbankNo').AsString);
          Self.CTMbankName.Text:=Trim(ADOTemp.fieldbyname('CTMbankName').AsString);
        end;
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

procedure TfrmConInPutNX.CustomerNoNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text:='';
  TBtnEditC(Sender).TxtCode:='';
end;

procedure TfrmConInPutNX.v1Column1PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
type
  TMyFunc = function(App:Tapplication; FormH:hwnd; FormID:integer;
                  Language: integer; WinStyle:integer;
                  GCode: Pchar; GName: Pchar; DataBase:Pchar;Title:PChar;
                  Parameters1:PChar;Parameters2:PChar;Parameters3:PChar;Parameters4:PChar;
                  Parameters5:PChar;Parameters6:PChar;Parameters7:PChar;Parameters8:PChar;
                  Parameters9:PChar;Parameters10:PChar;DataBaseStr:PChar):hwnd;stdcall;
var
  Tf: TMyFunc;
  Tp: TFarProc;
  Th:Thandle;
  LabInt,labname:String;
begin
  //if PPInt=2 then Exit;
  Ddatabase:=StringOfChar(' ', 32);
  Th := LoadLibrary('LabelSet.dll');
  if Th > 0 then
  begin
    try
      Tp := GetProcAddress(Th, 'GetDllForm');
      if Tp <> nil then
      begin
        Tf := TMyFunc(Tp);
        newh:=Tf(Application,0,2,0,0,
          PChar(DCode),
          PChar(DName),
          PChar(Ddatabase),
          PChar('标签模版设置'),
          PChar(''),
          PChar(''),
          '','','','','','','','',PChar(DConString)
          );
          if Trim(PChar(Ddatabase))<>'' then
          begin
            Ddatabase:=Trim(PChar(Ddatabase));
            LabInt:=Trim( LeftBStr(Ddatabase,Pos('|',Ddatabase)-1) ) ;
            labname:=Trim(RightBStr(Ddatabase,Length(Ddatabase)-Pos('|',Ddatabase) ) );
            with Order_Sub do
            begin
              Edit;
              FieldByName('SLbName').Value:=labname;
              FieldByName('SLbInt').Value:=LabInt;
            end;
          end;
      end
      else
      begin
        ShowMessage('打印执行错误');
      end;
    finally
    // FreeLibrary();
    end;
  end
  else
  begin
    ShowMessage('找不到'+Trim('LabelSet.dll'));
  end;

end;

procedure TfrmConInPutNX.MPRTCodeNameBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='MPRTCodeNameNX';
      flagname:='产品名称';
      if ShowModal=1 then
      begin
        MPRTCodeName.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        MPRTCodeName.TxtCode:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmConInPutNX.v1OrderUnitPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrderUnit';
      flagname:='数量单位';
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('OrderUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmConInPutNX.v1PriceUnitPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='PriceUnit';
      flagname:='价格单位';
      if Trim(DParameters1)<>'高权限' then
      begin
        TBAdd.Visible:=False;
        TBEdit.Visible:=False;
        TBDel.Visible:=False;
      end;
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PriceUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmConInPutNX.SYRNameBtnUpClick(Sender: TObject);
var
  fsj:string;
  FWZ:Integer;
begin
  fsj:=Trim(TEdit(Sender).Hint);
  FWZ:=Pos('/',fsj);
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Copy(fsj,1,FWZ-1);
      flagname:=Copy(fsj,FWZ+1,Length(fsj)-fwz);
      if Trim(flag)='SYRName' then
      begin
        flag:='OrdDefStr2';
        V1Name.Caption:='中文名称';
        V1Note.Caption:='英文名称';
        fnote:=True;
       { if Trim(DParameters1)<>'高权限' then
        begin
          TBAdd.Visible:=False;
          TBEdit.Visible:=false;
          TBDel.Visible:=false;
          TBSave.Visible:=false;
        end; }
      end;
      if Trim(flag)='BankNo' then
      begin
       { if Trim(DParameters1)<>'高权限' then
        begin
          TBAdd.Visible:=False;
          TBEdit.Visible:=false;
          TBDel.Visible:=false;
          TBSave.Visible:=false;
        end;   }
        fnote:=True;
      end;

      if ShowModal=1 then
      begin
        if Trim(flag)<>'OrdDefStr2' then
        begin
          TEdit(Sender).Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
          if Trim(flag)='BankNo' then
          begin
            with ADOTemp do
            begin
              Close;
              sql.Clear;
              sql.Add('select Top 1* from JYOrderCon_Main where BankNo='''+Trim(ClientDataSet1.fieldbyname('ZDYName').AsString)+'''');
              sql.Add(' and MPRTTYpe=''内销'' ');
              sql.Add(' order by FillTime desc');
              Open;
          //    Self.SYRAddress.Text:=Trim(ADOTemp.fieldbyname('SYRAddress').AsString);
          //    Self.TelNo.Text:=Trim(ADOTemp.fieldbyname('TelNo').AsString);
          //    Self.FaxNo.Text:=Trim(ADOTemp.fieldbyname('FaxNo').AsString);
          //    Self.bankNo.Text:=Trim(ADOTemp.fieldbyname('bankNo').AsString);
              Self.bankName.Text:=Trim(ADOTemp.fieldbyname('bankName').AsString);
              //Self.BankAddress.Text:=Trim(ADOTemp.fieldbyname('BankAddress').AsString);
              //Self.BankFastNo.Text:=Trim(ADOTemp.fieldbyname('BankFastNo').AsString);
            end;
          end;
        end
        else
        begin
          TEdit(Sender).Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
          //SYRName.TxtCode:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
          with ADOTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select Top 1* from JYOrderCon_Main where SYRName='''+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
            sql.Add(' and MPRTTYpe=''内销'' ');
            sql.Add(' order by FillTime desc');
            Open;
            Self.SYRAddress.Text:=Trim(ADOTemp.fieldbyname('SYRAddress').AsString);
            Self.TelNo.Text:=Trim(ADOTemp.fieldbyname('TelNo').AsString);
            Self.FaxNo.Text:=Trim(ADOTemp.fieldbyname('FaxNo').AsString);
            Self.bankNo.Text:=Trim(ADOTemp.fieldbyname('bankNo').AsString);
            Self.bankName.Text:=Trim(ADOTemp.fieldbyname('bankName').AsString);
          end;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmConInPutNX.ToolButton3Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  OneKeyPost(Tv1,Order_Sub);
end;

procedure TfrmConInPutNX.CPCFNoDblClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='MPRTNameType';
      flagname:='产品类别定义';
      V1HelpType.Visible:=True;
      V1HelpType.Caption:='缩写名';
      fnote:=True;
      V1Name.Caption:='中文';
      V1Note.Caption:='英文';
      if ShowModal=1 then
      begin
        with ADOQuery1 do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from KH_Zdy where Type=''MPRTNameType'' ');
          Open;
        end;
        SCreateCDS20(ADOQuery1,CDS_Type);
        SInitCDSData20(ADOQuery1,CDS_Type);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmConInPutNX.CPCFNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    GetName();
  end;
end;
procedure TfrmConInPutNX.GetName();
var
  fsj,FCPCF,FCPHZName,fsjhelp:String;
  i,j,z:Integer;
begin
  if Trim(CPCFNo.Text)='' then
  begin
    CPZHName.Text:='';
    Exit;
  end;
  i:=0;
  j:=0;
  z:=0;
  FCPCF:='';
  FCPHZName:='';
  if Trim(CPCFNo.Text)='' then
  begin
    CPZHName.Text:='';
  end;
  fsj:=Trim(CPCFNo.Text);
  i:=Pos('/',fsj);
  while i>=0 do
  begin
    fsjhelp:=Copy(fsj,i+1,i+1);
    if i>0 then
    begin
      if Trim(fsjhelp)<>'' then
      begin
        if Trim(fsjhelp)<>'/' then
        begin
          FCPCF:=Copy(fsj,1,i-1);
          if CDS_Type.Locate('Note',Trim(FCPCF),[]) then
          begin
            FCPCF:=Trim(CDS_Type.fieldbyname('HelpType').AsString);
            FCPHZName:=FCPHZName+FCPCF;
          end;


          fsj:=Copy(fsj,i+1,Length(fsj));
          i:=Pos('/',fsj);
          j:=1;
          z:=1;
        end else
        begin
          Application.MessageBox('录入错误!','提示',0);
          Exit;
        end;
      end else
      begin
        if z<>1 then
          i:=0
        else begin
          FCPCF:=Copy(fsj,1,i-1);
          if CDS_Type.Locate('Note',Trim(FCPCF),[]) then
          begin
            FCPCF:=Trim(CDS_Type.fieldbyname('HelpType').AsString);
            FCPHZName:=FCPHZName+FCPCF;
          end;


          fsj:=Copy(fsj,i+1,Length(fsj));
          i:=Pos('/',fsj);
          j:=1;
          z:=2;
        end;
      end;

    end else
    begin
      if j=1 then
      begin
        if z<>2 then
        begin
          FCPCF:=Trim(fsj);
          if  CDS_Type.Locate('Note',Trim(FCPCF),[]) then
          begin
            FCPCF:=Trim(CDS_Type.fieldbyname('HelpType').AsString);
            FCPHZName:=FCPHZName+FCPCF;
          end;


        end else
          i:=-1;

      end else
      begin
        FCPCF:=Trim(fsj);
        if CDS_Type.Locate('Note',Trim(FCPCF),[]) then
        begin
          FCPCF:=Trim(CDS_Type.fieldbyname('ZdyName').AsString);
          FCPHZName:=FCPHZName+FCPCF;
        end;


      end;
      i:=-1;
    end;
  end;
  if CPType.ItemIndex=0 then
    FCPHZName:=FCPHZName+'针织';
  if CPTanLi.Checked=True then
    FCPHZName:=FCPHZName+'弹力';
  if CPRanYin.ItemIndex=0 then
    FCPHZName:=FCPHZName+'染色'
  else if CPRanYin.ItemIndex=1 then
    FCPHZName:=FCPHZName+'印花';
  FCPHZName:=FCPHZName+Trim(CPRanHouGY.Text);
  {if CPType.ItemIndex=0 then
    FCPHZName:=FCPHZName+'针织布'
  else if CPType.ItemIndex=1 then
    FCPHZName:=FCPHZName+'布';}
  FCPHZName:=FCPHZName+'布';
  CPZHName.Text:=Trim(FCPHZName);
end;


procedure TfrmConInPutNX.CPRanHouGYBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:='CPRanHouGY';
      flagname:='后工艺';
      JiangeStr:=99;
      if ShowModal=1 then
      begin
        CPRanHouGY.Text:=Trim(ReturnStr);
        GetName();
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmConInPutNX.CPTanLiClick(Sender: TObject);
begin
  GetName();
end;

procedure TfrmConInPutNX.CPRanYinClick(Sender: TObject);
begin
  GetName();
end;

procedure TfrmConInPutNX.CPTypeClick(Sender: TObject);
begin
  GetName();
end;

procedure TfrmConInPutNX.CPRanHouGYBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text:='';
  TBtnEditC(Sender).TxtCode:='';
  GetName();
end;

procedure TfrmConInPutNX.CPRanHouGYChange(Sender: TObject);
begin
  GetName();
end;

procedure TfrmConInPutNX.CPCFNoChange(Sender: TObject);
begin
  GetName();
end;

procedure TfrmConInPutNX.v1Column4PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  {try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='MPRTCodeName';
      flagname:='产品名称';
      if ShowModal=1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('PRTCodeName').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Order_Sub.fieldbyname('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end; }
  try
    frmZdyAttachCP:=TfrmZdyAttachCP.Create(Application);
    with frmZdyAttachCP do
    begin

      if ShowModal=1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('PRTCode').Value:=Trim(CDS_HZ.fieldbyname('ZDYCode').AsString);
        Order_Sub.fieldbyname('PRTCodeName').Value:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
        Order_Sub.fieldbyname('prtspec').Value:=Trim(CDS_HZ.fieldbyname('DEFstr1').AsString);
        Order_Sub.Post;
      end;
    end;
  finally
    frmZdyAttachCP.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmConInPutNX.v1Column6PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='MPRTMF';
      flagname:='门幅信息';
      if ShowModal=1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('PRTMF').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
     //   Order_Sub.fieldbyname('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmConInPutNX.v1Column7PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='MPRTKZ';
      flagname:='克重信息';
      if ShowModal=1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('PRTKZ').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
     //   Order_Sub.fieldbyname('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmConInPutNX.v1Column8PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZdyAttachCP:=TfrmZdyAttachCP.Create(Application);
    with frmZdyAttachCP do
    begin

      if ShowModal=1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('PRTCode').Value:=Trim(CDS_HZ.fieldbyname('ZDYCode').AsString);
        Order_Sub.fieldbyname('PRTCodeName').Value:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
        Order_Sub.fieldbyname('prtspec').Value:=Trim(CDS_HZ.fieldbyname('DEFstr1').AsString);
        Order_Sub.fieldbyname('prtmf').Value:=Trim(CDS_HZ.fieldbyname('DEFstr7').AsString);
        Order_Sub.fieldbyname('prtkz').Value:=Trim(CDS_HZ.fieldbyname('DEFstr8').AsString);
        Order_Sub.Post;
      end;
    end;
  finally
    frmZdyAttachCP.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

end.
