unit U_KDInPut;

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, cxDropDownEdit, cxCalendar, cxGridCustomPopupMenu,
  cxGridPopupMenu, cxSplitter;

type
  TfrmKDInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column10: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1KHName: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1FeeShouQuType: TcxGridDBColumn;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton5: TToolButton;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    DataSource2: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ClientDataSet2: TClientDataSet;
    v2Column2: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1IFDS: TcxGridDBColumn;
    v1HKMoney: TcxGridDBColumn;
    TYPADD: TToolButton;
    ToADD: TToolButton;
    v1FKName: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure v1Column6PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column9PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column16PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure cxGridDBColumn3PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure v1Column6PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column18PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1KHNamePropertiesEditValueChanged(Sender: TObject);
    procedure v1ConNoPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TYPADDClick(Sender: TObject);
    procedure ToADDClick(Sender: TObject);
  private
    FXS:Integer;
    procedure InitData();
    procedure ZDYHelp(FButn:TcxButtonEdit;LType:string);
    function  SaveData():Boolean;
    function  SaveDataMX():Boolean;
    procedure GetMaxConNO();
    { Private declarations }
  public
    PState,PCopyInt:Integer;
    FMainId,FConNo,FConType,FYLType,canshu1:String;

    { Public declarations }
  end;

var
  frmKDInPut: TfrmKDInPut;

implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_ZDYHelpSel , U_ZHKHListNewCX,U_CPManage_Sel,U_ZdyAttachCP_HX,U_ContractList_CX;

{$R *.dfm}

procedure TfrmKDInPut.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('寄件信息录入11',Tv1,'寄件管理');
end;

procedure TfrmKDInPut.InitData();
begin
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*');
    SQL.Add(' from KuaiDi_Money A');
    sql.Add(' where A.KDID='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOTemp,Order_Sub);
  SInitCDSData20(ADOTemp,Order_Sub);
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KuaiDi_Money_Sub ');
    sql.Add('where KDID='''+Trim(Order_Sub.fieldbyname('KDID').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOTemp,ClientDataSet2);
  SInitCDSData20(ADOTemp,ClientDataSet2);
end;

procedure TfrmKDInPut.ZDYHelp(FButn:TcxButtonEdit;LType:string);
begin
end;

procedure TfrmKDInPut.FormShow(Sender: TObject);
begin
  ReadCxGrid('寄件信息录入11',Tv1,'寄件管理');
  v1Column20.Visible:=false;
  InitData();
end;
procedure TfrmKDInPut.GetMaxConNO();
begin

end;
function TfrmKDInPut.SaveData():Boolean;
var
  maxno:String;
begin
  try
    Result:=False;
    ADOCmd.Connection.BeginTrans;
    //保存主表
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('KDID').AsString)='' then
        begin
          if GetLSNo(ADOCmd,maxno,'KD','KuaiDi_Money',4,1)=False then
          begin
            Order_Sub.EnableControls;
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取规则流水号失败!','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(Order_Sub.fieldbyname('KDID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from KuaiDi_Money where KDID='''+Trim(Order_Sub.fieldbyname('KDID').AsString)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('KDID').AsString)='' then
          begin
            Append;
          end
          else begin
            Edit;
          end;
          FieldByName('KDID').Value:=Trim(maxno);
          fieldbyname('fillCode').Value:=trim(DCode);
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'KuaiDi_Money',0) ;
          FieldByName('KHID').Value:=Trim(Order_Sub.fieldbyname('KHID').AsString);
          FieldByName('ConMainid').Value:=Trim(Order_Sub.fieldbyname('ConMainid').AsString);
          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;
        Edit;
        FieldByName('KDID').Value:=Trim(maxno);
        Next;
      end;
    end;
    Order_Sub.EnableControls;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except;
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;


procedure TfrmKDInPut.TBSaveClick(Sender: TObject);
begin
  ToolBar1.SetFocus;

  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空！','提示',0);
    exit;
  end;
  if Order_Sub.Locate('KDDate',null,[]) then
  begin
    Application.MessageBox('寄件日期不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('KDComName',null,[]) then
  begin
    Application.MessageBox('快递公司不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('KDNO',null,[]) then
  begin
    Application.MessageBox('快递单号不能为空!','提示',0);
    Exit;
  end; 
  if Order_Sub.Locate('KHName',Null,[]) then
  begin
    Application.MessageBox('收件人不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('JYPerson',Null,[]) then
  begin
    Application.MessageBox('寄件人不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('ToPlace',Null,[]) then
  begin
    Application.MessageBox('收件地址不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('Country',null,[]) then
  begin
    Application.MessageBox('收件地区不能为空!','提示',0);
    Exit;
  end;
  {if Order_Sub.Locate('KDType',null,[]) then
  begin
    Application.MessageBox('寄件类型不能为空!','提示',0);
    Exit;
  end;}
  if Order_Sub.Locate('FKType',null,[]) then
  begin
    Application.MessageBox('付款方式不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('FeeShouQuType',null,[]) then
  begin
    Application.MessageBox('费用收取方式不能为空!','提示',0);
    Exit;
  end;

  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    //ModalResult:=1;
  end;

end;

procedure TfrmKDInPut.ToolButton3Click(Sender: TObject);
var
  FSQDate:string;
begin
  FSQDate:=Trim(FormatDateTime('yyyy-MM-dd',SGetServerDate(ADOTemp)));
  if Order_Sub.IsEmpty then
  begin
    with Order_Sub do
    begin
      Append;
      FieldByName('KDDate').Value:=FSQDate;
      FieldByName('YWZhuLi').Value:=Trim(DName);
      fieldbyname('KDType').Value:='包裹';
      Fieldbyname('Qty').AsFloat:=1;
      Post;
    end;
  end
  else
  begin
    CopyAddRow(Tv1,Order_Sub);
  end;
end;

procedure TfrmKDInPut.ToolButton4Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  if Trim(Order_Sub.fieldbyname('KDId').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete KuaiDi_Money where KDId='''+Trim(Order_Sub.fieldbyname('KDId').AsString)+'''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmKDInPut.v1Column6PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with Order_Sub do
  begin
    Edit;
    FieldByName('MDID').Value:=Trim(mvalue);
    Post;
  end;
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select B.OrderNo,A.PS,A.MDDate from JYOrder_Main_MD A');
    sql.Add(' inner join JYorder_Main B on A.ORDMainId=B.MainId');
    sql.Add(' and A.MDID='''+Trim(mvalue)+'''');
    Open;
  end;
  if ADOTemp.IsEmpty then
  begin
    Application.MessageBox('条码录入错误!','提示',0);
    Exit;
  end;
  with Order_Sub do
  begin
    Edit;
    FieldByName('OrderNo').Value:=ADOTemp.fieldbyname('OrderNo').Value;
    FieldByName('PS').Value:=ADOTemp.fieldbyname('PS').Value;
    Post;
  end;
end;

procedure TfrmKDInPut.v1Column1PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='KDComName';
      flagname:='快递公司';
      if Trim(canshu1)<>'高权限' then
      begin
        frmZDYHelp.TBAdd.Visible:=False;
        frmZDYHelp.TBDel.Visible:=False;
        frmZDYHelp.TBEdit.Visible:=False;
      end;
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('KDComName').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmKDInPut.v1Column9PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='Country';
      flagname:='收件地区';
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('Country').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmKDInPut.v1Column16PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='KDHeZuo';
      flagname:='快递合作方';
      if Trim(canshu1)<>'高权限' then
      begin
        frmZDYHelp.TBAdd.Visible:=False;
        frmZDYHelp.TBDel.Visible:=False;
        frmZDYHelp.TBEdit.Visible:=False;
      end;
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('KDHeZuo').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmKDInPut.cxGridDBColumn3PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from CP_YDang where CYNO='''+Trim(mvalue)+'''');
    Open;
  end;
  with ClientDataSet2 do
  begin
    Edit;
    FieldByName('CYNO').Value:=Trim(mvalue);
    FieldByName('CYName').Value:=ADOTemp.fieldbyname('CYName').Value;
    FieldByName('CYCF').Value:=ADOTemp.fieldbyname('CYECF').Value;
    FieldByName('CYMF').Value:=ADOTemp.fieldbyname('CYMF').Value;
    FieldByName('CYKZ').Value:=ADOTemp.fieldbyname('CYKZ').Value;
  end;
end;

procedure TfrmKDInPut.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if Order_Sub.IsEmpty then Exit;
  if ClientDataSet2.IsEmpty then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from KuaiDi_Money_Sub ');
      sql.Add('where KDID='''+Trim(Order_Sub.fieldbyname('KDID').AsString)+'''');
      Open;
    end;
    SCreateCDS20(ADOTemp,ClientDataSet2);
    SInitCDSData20(ADOTemp,ClientDataSet2);
  end else
  begin
    if Trim(Order_Sub.fieldbyname('KDID').AsString)<>Trim(ClientDataSet2.fieldbyname('KDID').AsString) then
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from KuaiDi_Money_Sub ');
        sql.Add('where KDID='''+Trim(Order_Sub.fieldbyname('KDID').AsString)+'''');
        Open;
      end;
      SCreateCDS20(ADOTemp,ClientDataSet2);
      SInitCDSData20(ADOTemp,ClientDataSet2);
    end;
  end;

end;

procedure TfrmKDInPut.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KuaiDi_Money where KDID='''+Trim(Order_Sub.fieldbyname('KDID').AsString)+'''');
    Open;
  end;
  if ADOTemp.IsEmpty then
  begin
    Application.MessageBox('上面数据未保存，不能增行!','提示',0);
    Exit;
  end;
  frmZdyAttachCP_HX:=TfrmZdyAttachCP_HX.Create(self);
  with frmZdyAttachCP_HX do
  begin
    if ShowModal=1 then
    begin
      with CDS_Sub do
      begin
        First;
        while not eof do
        begin
          with ClientDataSet2 do
          begin
            Append;
            FieldByName('CYNO').Value:=Trim(CDS_Sub.fieldbyname('ZdyCode').AsString);
            FieldByName('CYName').Value:=Trim(CDS_Sub.fieldbyname('ZdyName').AsString);
            FieldByName('KDID').Value:=Trim(Order_Sub.fieldbyname('KDID').AsString);
            Post;
          end;
          next;
        end;
      end;
    end;
    free;
  end;
end;

procedure TfrmKDInPut.ToolButton5Click(Sender: TObject);
begin
  if ClientDataSet2.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete KuaiDi_Money_Sub where KSID='''+Trim(ClientDataSet2.fieldbyname('KSID').AsString)+'''');
    ExecSQL;
  end;
  ClientDataSet2.Delete;
end;

procedure TfrmKDInPut.ToolButton1Click(Sender: TObject);
begin
  ToolBar2.SetFocus;
  if ClientDataSet2.IsEmpty then
  begin
    Application.MessageBox('寄件明细不能为空！','提示',0);
    exit;
  end;
  if ClientDataSet2.Locate('CYNO',null,[]) then
  begin
    Application.MessageBox('产品编号不能为空!','提示',0);
    Exit;
  end;
  if SaveDataMX() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    //ModalResult:=1;
  end;
end;
function TfrmKDInPut.SaveDataMX():Boolean;
var
  maxno:String;
begin
  try
    Result:=False;
    ADOCmd.Connection.BeginTrans;
    //保存主表
    ClientDataSet2.DisableControls;
    with ClientDataSet2 do
    begin
      First;
      while not Eof do
      begin
        if Trim(ClientDataSet2.fieldbyname('KSID').AsString)='' then
        begin
          if GetLSNo(ADOCmd,maxno,'KS','KuaiDi_Money_Sub',4,1)=False then
          begin
            ClientDataSet2.EnableControls;
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取规则流水号失败!','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(ClientDataSet2.fieldbyname('KSID').AsString);
        end;
        with ADOCmd do
        begin                                                            
          Close;
          sql.Clear;
          SQL.Add('select * from KuaiDi_Money_Sub where KSID='''+Trim(ClientDataSet2.fieldbyname('KSID').AsString)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(ClientDataSet2.fieldbyname('KSID').AsString)='' then
          begin
            Append;
          end
          else begin
            Edit;
          end;
          FieldByName('KDID').Value:=Trim(Order_Sub.fieldbyname('KDID').AsString);
          FieldByName('KSID').Value:=Trim(maxno);
          RTSetSaveDataCDS(ADOCmd,Tv2,ClientDataSet2,'KuaiDi_Money_Sub',0) ;
          FieldByName('CYID').Value:=Trim(ClientDataSet2.fieldbyname('CYID').AsString);
          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;
        Edit;
        FieldByName('KSID').Value:=Trim(maxno);
        Next;
      end;
    end;
    ClientDataSet2.EnableControls;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmKDInPut.v1Column6PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='ToPlace';
      flagname:='收件地址';
      {if Trim(canshu1)<>'高权限' then
      begin
        frmZDYHelp.TBAdd.Visible:=False;
        frmZDYHelp.TBDel.Visible:=False;
        frmZDYHelp.TBEdit.Visible:=False;
      end; }
      Maintype:=trim(DName);
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('ToPlace').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmKDInPut.v1Column18PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZHKHListNewCX:=TfrmZHKHListNewCX.Create(Application);
    with frmZHKHListNewCX do
    begin
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('KHName').Value:=Trim(frmZHKHListNewCX.Order_Main.fieldbyname('ZDYName').AsString);
          fieldbyname('ToPlace').Value:=trim(frmZHKHListNewCX.Order_Main.fieldbyname('DEFNote1').AsString);
          fieldbyname('KHLXFS').Value:=trim(frmZHKHListNewCX.Order_Main.fieldbyname('Defstr2').AsString);
          fieldbyname('Country').Value:=trim(frmZHKHListNewCX.Order_Main.fieldbyname('Defstr7').AsString);
        end;
      end;
    end;
  finally;
    frmZHKHListNewCX.Free;
  end;
end;

procedure TfrmKDInPut.v1KHNamePropertiesEditValueChanged(Sender: TObject);
var mavlue:string;
begin
  mavlue:=TcxTextEdit(Sender).EditingText;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Top 1 * from KuaiDi_Money ');
    sql.Add('where KHName='''+trim(Mavlue)+'''');
    SQL.Add(' Order by KDDate desc');
    open;
  end;
  with Order_Sub do
  begin
    Edit;
    FieldByName('KHName').Value:=Trim(mavlue);
    FieldByName('ToPlace').Value:=ADOTemp.fieldbyname('ToPlace').AsString;
    FieldByName('KHLXFS').Value:=ADOTemp.fieldbyname('KHLXFS').AsString;
    FieldByName('Country').Value:=ADOTemp.fieldbyname('Country').AsString;
  end;
end;

procedure TfrmKDInPut.v1ConNoPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmContractList_CX:=TfrmContractList_CX.create(self);
  with frmContractList_CX do
  begin
    if ShowModal=1 then
    begin
      with Order_Sub do
      begin
        edit;
        FieldByName('ConNo').Value:=Trim(Order_Main.fieldbyname('ConNO').AsString);
        FieldByName('ConMainid').Value:=Trim(Order_Main.fieldbyname('Mainid').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmKDInPut.TYPADDClick(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KuaiDi_Money where KDID='''+Trim(Order_Sub.fieldbyname('KDID').AsString)+'''');
    Open;
  end;
  if ADOTemp.IsEmpty then
  begin
    Application.MessageBox('上面数据未保存，不能增行!','提示',0);
    Exit;
  end;
  frmCPManageSel:=TfrmCPManageSel.Create(self);
  with frmCPManageSel do
  begin
    if ShowModal=1 then
    begin
      with ClientDataSet1 do
      begin
        First;
        while not eof do
        begin
          with ClientDataSet2 do
          begin
            Append;
            FieldByName('CYNO').Value:=Trim(ClientDataSet1.fieldbyname('CYNo').AsString);
            FieldByName('CYName').Value:=Trim(ClientDataSet1.fieldbyname('CYName').AsString);
            FieldByName('CYCF').Value:=Trim(ClientDataSet1.fieldbyname('CYCF').AsString);
            FieldByName('CYMF').Value:=Trim(ClientDataSet1.fieldbyname('CYMF').AsString);
            FieldByName('CYKZ').Value:=Trim(ClientDataSet1.fieldbyname('CYKZ').AsString);
            FieldByName('KDID').Value:=Trim(Order_Sub.fieldbyname('KDID').AsString);
            Post;
          end;
          next;
        end;
      end;
    end;
    free;
  end;
end;

procedure TfrmKDInPut.ToADDClick(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KuaiDi_Money where KDID='''+Trim(Order_Sub.fieldbyname('KDID').AsString)+'''');
    Open;
  end;
  if ADOTemp.IsEmpty then
  begin
    Application.MessageBox('上面数据未保存，不能增行!','提示',0);
    Exit;
  end;
  with ClientDataSet2 do
  begin
    Append;
    FieldByName('KDID').Value:=Trim(Order_Sub.fieldbyname('KDID').AsString);
    Post;
  end;
end;

end.
