unit U_OrderInPut;

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,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxCurrencyEdit;

type
  TfrmOrderInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label3: TLabel;
    OrderNo: TEdit;
    OrdDate: TDateTimePicker;
    Label4: TLabel;
    DlyDate: TDateTimePicker;
    Label7: TLabel;
    Label8: 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;
    Label10: TLabel;
    ConNo: TEdit;
    Label11: TLabel;
    OrdDefStr1: TEdit;
    Label14: TLabel;
    Label20: TLabel;
    JGFactoryName: TBtnEditC;
    CustomerNoName: TBtnEditC;
    v1XHNo: TcxGridDBColumn;
    OrdDefStr2: TBtnEditC;
    v1OrderUnit: TcxGridDBColumn;
    v1SOrddefstr1: TcxGridDBColumn;
    v1PriceUnit: TcxGridDBColumn;
    Label2: TLabel;
    MPRTCodeName: TBtnEditC;
    Label5: TLabel;
    MPRTCF: TBtnEditC;
    Label6: TLabel;
    MPRTSpec: TBtnEditC;
    Label12: TLabel;
    MPRTMF: TBtnEditC;
    Label13: TLabel;
    MPRTKZ: TBtnEditC;
    Label15: TLabel;
    MPRTSL: TBtnEditC;
    Label16: TLabel;
    OrdDefStr3: TBtnEditC;
    Note: TMemo;
    Label17: TLabel;
    OrdPerson2: TEdit;
    v1Column2: TcxGridDBColumn;
    ToolButton3: TToolButton;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    v1PRTRollNum: TcxGridDBColumn;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label18: TLabel;
    FHAddress: TBtnEditC;
    Label19: TLabel;
    LXFS: TBtnEditC;
    Label21: TLabel;
    orddefnote1: TMemo;
    v1Sorddefnote1: TcxGridDBColumn;
    Panel1: TPanel;
    Label22: TLabel;
    Label23: TLabel;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    OrdPerson1: TBtnEditA;
    Label24: TLabel;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    SYRName: TComboBox;
    Label25: TLabel;
    v1gangFee: TcxGridDBColumn;
    GroupBox1: TGroupBox;
    Label26: TLabel;
    Label27: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Edit3: TEdit;
    Label28: TLabel;
    orderType: TComboBox;
    v1YunFee: TcxGridDBColumn;
    v1OtherFee: TcxGridDBColumn;
    cxGrid4: TcxGrid;
    TV4: TcxGridDBTableView;
    V4Column1: TcxGridDBColumn;
    V4Column5: TcxGridDBColumn;
    V4Column3: TcxGridDBColumn;
    cxGrid4Level1: TcxGridLevel;
    ADO_KH: TADOQuery;
    DS_KH: TDataSource;
    Label29: TLabel;
    v1Column1: TcxGridDBColumn;
    CKName: TComboBox;
    Label30: TLabel;
    v1Column3: TcxGridDBColumn;
    TYB: TBtnEditC;
    Label31: TLabel;
    Label32: TLabel;
    tybtel: TBtnEditC;
    V4Column2: 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 JGFactoryNameBtnUpClick(Sender: TObject);
    procedure CustomerNoNameBtnUpClick(Sender: TObject);
    procedure CustomerNoNameBtnDnClick(Sender: TObject);
    procedure v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure OrdDefStr3BtnUpClick(Sender: TObject);
    procedure MPRTCodeNameBtnUpClick(Sender: TObject);
    procedure NoteDblClick(Sender: TObject);
    procedure v1OrderUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1PriceUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure jKeyPress(Sender: TObject; var Key: Char);
    procedure OrdDefStr2BtnUpClick(Sender: TObject);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton3Click(Sender: TObject);
    procedure v1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure FHAddressBtnUpClick(Sender: TObject);
    procedure LXFSBtnUpClick(Sender: TObject);
    procedure v1Column5PropertiesEditValueChanged(Sender: TObject);
    procedure v1PRTColorPropertiesEditValueChanged(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure OrdPerson1BtnClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure OrdDateChange(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure TV4CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure TV4KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure CustomerNoNameKeyPress(Sender: TObject; var Key: Char);
    procedure v1PRTOrderQtyPropertiesEditValueChanged(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure TYBBtnUpClick(Sender: TObject);
  private
    procedure InitData();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    function SaveData(): Boolean;
    procedure DoKc();
    { Private declarations }
  public
    PState, CopyInt, PriceFlag: Integer;
    FMainId, FFMainId, FOrderNo: string;
    FXS: Integer;
    fCustomerNoName: string;
    forderType: string;
    fsyrName: string;
    { Public declarations }
  end;

var
  frmOrderInPut: TfrmOrderInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_Fun, U_ZDYHelpSel, U_ZdyAttachment, U_ZdyAttachCP,
  U_UserHelp;

{$R *.dfm}
procedure TfrmOrderInPut.DoKc();
begin
  Label22.Caption := '匹数：0';
  Label23.Caption := '数量：0';
  if Order_Sub.IsEmpty then
    exit;
  if trim(Order_Sub.fieldbyname('PRTCodeName').AsString) = '' then
    exit;
//  IF  trim(Order_Sub.fieldbyname('PRTColor').AsString)='' then exit;

  with ADOTemp do
  begin
    close;
    sql.Clear;
    SQL.Add('SELECT sum(A.KCRollnum) as KCRollnum,sum(A.KCQty) as kcQty  FROM CK_BanCP_KC A');
 //   sql.Add('exec P_Get_HCKCList ');
    sql.Add(' where C_CodeName=' + quotedstr(trim(Order_Sub.fieldbyname('PRTCodeName').AsString)));
    sql.Add(' and C_Color=' + quotedstr(trim(Order_Sub.fieldbyname('PRTColor').AsString)));
    sql.Add(' and A.KCQty>0 and A.CPType not in(''一等次品'',''二等次品'') and isnull(A.kcstatus,'''')='''' ');
    open;
  end;
  if not ADOTemp.IsEmpty then
  begin
    Label22.Caption := '匹数：' + trim(ADOTemp.fieldbyname('kcRollNum').AsString);
    Label23.Caption := '数量：' + trim(ADOTemp.fieldbyname('kcQty').AsString);
  end;
  with ADOTemp do
  begin
    close;
    sql.Clear;
    SQL.Add(' SELECT top 1 Price  FROM YF_Money_CR A');
    sql.Add(' where P_CodeName=' + quotedstr(trim(Order_Sub.fieldbyname('PRTCodeName').AsString)));
    //  sql.Add(' and P_Color='+quotedstr(trim(Order_Sub.fieldbyname('PRTColor').AsString)));
    sql.Add(' and factoryName=' + quotedstr(trim(CustomerNoName.Text)));
    sql.Add(' and syrName=''' + fsyrName + ''' ');
    if trim(orderType.Text) = '剪样单' then
      sql.Add(' and YFName=''样品销售'' ')
    else
      sql.Add(' and YFName=''销售金额'' ');
    sql.Add(' and price>0 ');
    sql.Add(' order by FillTime desc ');
    open;
  end;
  if not ADOTemp.IsEmpty then
  begin
    Label29.Caption := '单价：' + trim(ADOTemp.fieldbyname('price').AsString);

  end;
end;

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

procedure TfrmOrderInPut.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*,cast(0.0 as decimal(18,2)) as money from JYOrder_Sub A ');
    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 JYOrder_Main where MainId=''' + Trim(FMainId) + '''');
    Open;
  end;
  CustomerNoName.OnChange := nil;
  SCSHDataNew(ADOQuery1, ScrollBox1, 2);
  if PState = 0 then
  begin
    OrdDate.DateTime := SGetServerDate10(ADOTemp);
    DlyDate.DateTime := SGetServerDate10(ADOTemp);
  end
  else
  begin
  end;
  if CopyInt = 99 then
  begin
    PState := 0;
    FMainId := '';
    OrderNo.Text := '';
    orddefnote1.Text := '';
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value := '';
        FieldByName('SubId').Value := '';
        Post;
        Next;
      end;
    end;
  end;

  if PState = 0 then
  begin
    OrderNo.Text := '自动生成';
    OrderNo.Enabled := false;
  end;
  CustomerNoName.OnChange := CustomerNoNameChange;
end;

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

procedure TfrmOrderInPut.FormShow(Sender: TObject);
begin
  readCxGrid('指示单录入1', Tv1, '生产指示单管理');
  SYRName.Items.Clear;
  with ADOCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''SYRNAME''');
    open;
    if not IsEmpty then
    begin
      while not eof do
      begin
        SYRName.Items.Add(trim(fieldbyname('zdyname').AsString));
        next;
      end;
      SYRName.ItemIndex := 0;
    end;
  end;
  if PriceFlag = 99 then
  begin
    v1PRTPrice.Visible := False;
    v1PRTPrice.Hidden := True;
  end;
  if PState = 1 then
  begin
    Label21.Visible := true;
    orddefnote1.Visible := true;
  end;
  orderType.Items.Clear;
  orderType.Items.Add(fOrderType);
  if fOrderType = '剪样单' then
  begin
    GroupBox1.Visible := true;
    v1gangFee.Visible := false;
    v1yunFee.Visible := true;
    v1otherFee.Visible := false;
  end
  else
  begin
    GroupBox1.Visible := false;
    v1gangFee.Visible := true;
    v1yunFee.Visible := true;
    v1otherFee.Visible := true;
  end;
  orderType.ItemIndex := 0;
  CKName.ItemIndex := 0;
  if trim(fsyrName) <> '' then
  begin
    SYRName.Items.Clear;
    SYRName.Items.Add(fsyrName);
    SYRName.ItemIndex := 0;
    SYRName.Enabled := false;
  //  orderType.Enabled:=false;
  end;
  InitData();
end;

function TfrmOrderInPut.SaveData(): Boolean;
var
  maxno: string;
  i: integer;
begin

  if Trim(FMainId) = '' then
  begin
    if GetLSNo(ADOTemp, maxno, 'JM', 'JYOrder_Main', 3, 1) = False then
    begin
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
  end
  else
  begin
    maxno := Trim(FMainId);
  end;
  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;
      end
      else
      begin
        Edit;
      end;
      FieldByName('MainId').Value := Trim(maxno);
      SSetsaveSqlNew(ADOCmd, 'JYOrder_Main', ScrollBox1, 2);
      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('Note').Value := Trim(Note.Text);
      Post;
    end;
    FMainId := Trim(maxno);
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main where orderno=''' + Trim(OrderNo.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('delete from JYOrder_Sub where MainId=''' + Trim(FMainId) + '''');
      execsql;
    end;
    ///保存子表
    with Order_Sub do
    begin
      DisableControls;
      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;
        if Fieldbyname('PRTRollNum').AsFloat = 0 then
        begin
          i := 0;
          if fieldbyname('SordQty1').AsFloat <> 0 then
            i := i + 1;
          if fieldbyname('SordQty2').AsFloat <> 0 then
            i := i + 1;
          if fieldbyname('SordQty3').AsFloat <> 0 then
            i := i + 1;
          if fieldbyname('SordQty4').AsFloat <> 0 then
            i := i + 1;
          if fieldbyname('SordQty5').AsFloat <> 0 then
            i := i + 1;
          if fieldbyname('SordQty6').AsFloat <> 0 then
            i := i + 1;
          if fieldbyname('SordQty7').AsFloat <> 0 then
            i := i + 1;
          if fieldbyname('SordQty8').AsFloat <> 0 then
            i := i + 1;
          if fieldbyname('SordQty9').AsFloat <> 0 then
            i := i + 1;
          if fieldbyname('SordQty10').AsFloat <> 0 then
            i := i + 1;
          edit;
          fieldbyname('PRTRollNum').Value := i;
          post;
        end;

        with ADOTemp do
        begin
          close;
          sql.Clear;
          sql.Add('select *  from KH_Zdy_Attachment A ');
          sql.Add('where zdyName=' + quotedstr(trim(Order_Sub.fieldbyname('PRTCodeName').AsString)));
          sql.Add('and DEFstr3=' + quotedstr(trim(Order_Sub.fieldbyname('PRTColor').AsString)));
          open;
        end;
        if not ADOTemp.IsEmpty then
        begin
          edit;
          fieldbyname('PRTPrice').Value := fieldbyname('PRTPrice').AsFloat + ADOTemp.fieldbyname('defflt1').AsFloat;
          post;
        end;

        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('SubId').AsString) = '' then
            Append
          else
            Edit;
          FieldByName('orderNo').Value := Trim(orderNo.Text);
          FieldByName('MainId').Value := Trim(FMainId);
          FieldByName('SubId').Value := Trim(maxno);
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'JYOrder_Sub', 0);
          fieldbyname('PRTOrderQty').Value := Order_Sub.fieldbyname('PRTOrderQty').AsFloat;
          fieldbyname('PRTRollNum').Value := Order_Sub.fieldbyname('PRTRollNum').AsFloat;
          fieldbyname('gangNum').Value := Order_Sub.fieldbyname('gangNum').AsFloat;
          fieldbyname('SordQty1').Value := Order_Sub.fieldbyname('SordQty1').AsFloat;
          fieldbyname('SordQty2').Value := Order_Sub.fieldbyname('SordQty2').AsFloat;
          fieldbyname('SordQty3').Value := Order_Sub.fieldbyname('SordQty3').AsFloat;
          fieldbyname('SordQty4').Value := Order_Sub.fieldbyname('SordQty4').AsFloat;
          fieldbyname('SordQty5').Value := Order_Sub.fieldbyname('SordQty5').AsFloat;
          fieldbyname('SordQty6').Value := Order_Sub.fieldbyname('SordQty6').AsFloat;
          fieldbyname('SordQty7').Value := Order_Sub.fieldbyname('SordQty7').AsFloat;
          fieldbyname('SordQty8').Value := Order_Sub.fieldbyname('SordQty8').AsFloat;
          fieldbyname('SordQty9').Value := Order_Sub.fieldbyname('SordQty9').AsFloat;
          fieldbyname('SordQty10').Value := Order_Sub.fieldbyname('SordQty10').AsFloat;

          fieldbyname('GangFee').Value := Order_Sub.fieldbyname('GangFee').AsFloat;
          fieldbyname('YunFee').Value := Order_Sub.fieldbyname('YunFee').AsFloat;
          fieldbyname('OtherFee').Value := Order_Sub.fieldbyname('OtherFee').AsFloat;

          fieldbyname('PRTCode').Value := Order_Sub.fieldbyname('PRTCode').AsString;
          if Trim(fieldbyname('PRTPrice').AsString) = '' then
          begin
            fieldbyname('PRTPrice').Value := 0
          end;
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('SubId').Value := Trim(maxno);
        //Order_Sub.Post;
        Next;
      end;
      Order_Sub.EnableControls;
    end;
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    Order_Sub.EnableControls;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
    if GetLSNo(ADOTemp, maxno, 'JS', 'JYOrder_Sub', 4, 1) = False then
    begin
    end;
  end;
end;

procedure TfrmOrderInPut.TBSaveClick(Sender: TObject);
var
  forderNo: string;
begin
  conNo.SetFocus;
  if Trim(SYRName.Text) = '' then
  begin
    Application.MessageBox('公司抬头不能为空！', '提示', 0);
    Exit;
  end;
  if Trim(CustomerNoName.Text) = '' then
  begin
    Application.MessageBox('客户名称不能为空！', '提示', 0);
    Exit;
  end;
  if Trim(orderType.Text) = '' then
  begin
    Application.MessageBox('订单类型不能为空！', '提示', 0);
    Exit;
  end;
  if Trim(ckName.Text) = '' then
  begin
    Application.MessageBox('发货仓库不能为空！', '提示', 0);
    Exit;
  end;
  if Trim(TYB.Text) = '' then
  begin
    Application.MessageBox('托运部不能为空！', '提示', 0);
    Exit;
  end;
  if Order_Sub.Locate('PRTCodeName', null, []) = True then
  begin
    Application.MessageBox('品名不能为空!', '提示', 0);
    Exit;
  end;
  if Order_Sub.Locate('PRTCodeName', '', []) = True then
  begin
    Application.MessageBox('品名不能为空!', '提示', 0);
    Exit;
  end;
  if Order_Sub.Locate('PRTColor', null, []) = True then
  begin
    Application.MessageBox('颜色不能为空!', '提示', 0);
    Exit;
  end;
  if Order_Sub.Locate('PRTColor', '', []) = True then
  begin
    Application.MessageBox('颜色不能为空!', '提示', 0);
    Exit;
  end;

{  with Order_Sub do
  begin
    DisableControls;
    First;
    while not eof do
    begin
      if (fieldbyname('PRTRollNum').AsFloat=0) and (fieldbyname('PRTOrderQty').AsFloat=0)then
      begin
        EnableControls;
        application.MessageBox('匹数和数量不能同时未零！','提示信息',0);
        exit;
      end;
      next;
    end;
    First;
    EnableControls;
  end;
   }
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KH_Zdy_Attachment where zdyName=''' + Trim(CustomerNoName.Text) + '''');
    Open;
  end;
  if ADOTemp.IsEmpty then
  begin
    Application.MessageBox('客户不存在!', '提示信息', 0);
    Exit;
  end;
  if PState = 1 then
  begin
    if trim(orddefnote1.Text) = '' then
    begin
      Application.MessageBox('修改备注信息不能为空!', '提示', 0);
      Exit;
    end;
  end;
  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空!', '提示', 0);
    Exit;
  end;
  if PState = 1 then
  begin
    if Trim(OrderNo.Text) <> Trim(forderNo) then
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR where MainId=''' + Trim(FMainId) + '''');
        Open;
      end;
      if ADOTemp.IsEmpty = False then
      begin
        Application.MessageBox('已产生数据，不能更改订单号!', '提示', 0);
        Exit;
      end;
    end;
  end
  else
  begin
    if trim(fSYRName) = '龙运纺织' then
    begin
      if GetLSNo(ADOCmd, forderNo, 'L', 'JYOrder_Main', 3, 1) = False then
      begin
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;
    end
    else
    begin
      if trim(orderType.Text) = '剪样单' then
      begin
        if GetLSNo(ADOCmd, forderNo, 'J', 'JYOrder_Main', 3, 1) = False then
        begin
          Application.MessageBox('取最大号失败!', '提示', 0);
          Exit;
        end;
      end
      else
      begin
        if GetLSNo(ADOCmd, forderNo, '', 'JYOrder_Main', 3, 1) = False then
        begin
          Application.MessageBox('取最大号失败!', '提示', 0);
          Exit;
        end;
      end;
    end;
    OrderNo.text := trim(forderNo);
  end;

  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    if PState = 0 then
    begin
      FMainId := '';
      SClearData(ScrollBox1, 2);
      if trim(fsyrName) <> '' then
      begin
        SYRName.Items.Clear;
        SYRName.Items.Add(fsyrName);
        SYRName.ItemIndex := 0;
        SYRName.Enabled := false;
      end;
      orderType.ItemIndex := 0;
      InitData();
      Label29.Caption := '单价：';
      Label22.Caption := '匹数：';
      Label23.Caption := '数量：';
      Note.Text := '';
    end
    else
      ModalResult := 1;
  end;
end;

procedure TfrmOrderInPut.v1PRTColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachCP := TfrmZdyAttachCP.Create(Application);
    with frmZdyAttachCP do
    begin
      if ShowModal = 1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PRTCode').Value := Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
        Self.Order_Sub.FieldByName('PRTCodeName').Value := Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
        Self.Order_Sub.FieldByName('PRTColor').Value := Trim(CDS_HZ.fieldbyname('DEFstr3').AsString);
    //    Self.Order_Sub.FieldByName('SOrddefstr4').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
        Self.Order_Sub.Post;
      end;
    end;
  finally
    frmZdyAttachCP.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmOrderInPut.ToolButton1Click(Sender: TObject);
var
  i: Integer;
begin
  if Trim(OrderNo.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);
    if trim(orderType.Text) = '正常单' then
      FieldByName('OrderUnit').Value := 'M'
    else
      FieldByName('OrderUnit').Value := 'M';
  {  if i<9 then
      FieldByName('SOrddefstr1').Value:='0'+Trim(IntToStr(i))
    else
      FieldByName('SOrddefstr1').Value:=Trim(IntToStr(i));
    FieldByName('PRTColor').Value:='';
    FieldByName('PRTOrderQty').Value:=null;
    //FieldByName('PRTPrice').Value:=null;
   // FieldByName('SOrddefstr1').Value:=null;
    FieldByName('SOrddefstr2').Value:=null;   }
    Post;
  end;
end;

procedure TfrmOrderInPut.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;
 { with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Cloth_LLMX where OrdSubId='''+Trim(Order_Sub.fieldbyname('SubId').AsString)+'''');
    Open;
  end;
  if ADOTemp.IsEmpty=False then
  begin
    Application.MessageBox('已领料不能删除数据!','提示',0);
    Exit;
  end;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Sub_AnPai where SubId='''+Trim(Order_Sub.fieldbyname('SubId').AsString)+'''');
    Open;
  end;
  if ADOTemp.IsEmpty=False then
  begin
    Application.MessageBox('已回仓不能删除数据!','提示',0);
    Exit;
  end;       }
  if Trim(Order_Sub.fieldbyname('SubId').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;

    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_CR where subID=''' + Trim(Order_Sub.fieldbyname('SubId').AsString) + ''' and CRType=''应收款登记''');
      Open;
    end;
    if ADOTemp.IsEmpty = False then
    begin
      Application.MessageBox('已产生数据，不能更改订单号!', '提示', 0);
      Exit;
    end;
  {  with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Sub where SubId='''+Trim(Order_Sub.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end; }
  end;
  Order_Sub.Delete;

end;

procedure TfrmOrderInPut.JGFactoryNameBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'RKPlace';
      flagname := '染厂名称';
      if ShowModal = 1 then
      begin
        JGFactoryName.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        JGFactoryName.TxtCode := Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.CustomerNoNameBtnUpClick(Sender: TObject);
begin

  CustomerNoName.OnChange := nil;
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin

      fkhType := self.fsyrName;
      if ShowModal = 1 then
      begin
        CustomerNoName.Text := Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
        CustomerNoName.TxtCode := Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
        FHAddress.Text := Trim(CDS_HZ.fieldbyname('DEFNote2').AsString);
        LXFS.Text := Trim(CDS_HZ.fieldbyname('DEFstr2').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
  CustomerNoName.OnChange := CustomerNoNameChange;
end;

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

procedure TfrmOrderInPut.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 TfrmOrderInPut.OrdDefStr3BtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag := 'maitou';
      flagname := '唛头';
      if ShowModal = 1 then
      begin
        OrdDefStr3.Text := OrdDefStr3.Text + frmZDYHelpSel.ReturnStr;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderInPut.MPRTCodeNameBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTCodeName';
      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 TfrmOrderInPut.NoteDblClick(Sender: TObject);
begin
  try
    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag := 'OrdNote';
      flagname := '备注及要求';
      if ShowModal = 1 then
      begin
        Note.Text := Note.Text + frmZDYHelpSel.ReturnStr;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderInPut.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 TfrmOrderInPut.v1PriceUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PriceUnit';
      flagname := '价格单位';
      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 TfrmOrderInPut.jKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CP_YDang where CYNo like ''' + '%' + Trim(OrdDefStr1.Text) + '%' + '''');
      Open;
    end;
    if ADOTemp.RecordCount = 1 then
    begin
      MPRTCodeName.Text := Trim(ADOTemp.fieldbyname('CYName').AsString);
      MPRTCF.Text := Trim(ADOTemp.fieldbyname('CYECF').AsString);
      MPRTSpec.Text := Trim(ADOTemp.fieldbyname('CYSpec').AsString);
      MPRTMF.Text := Trim(ADOTemp.fieldbyname('CYMF').AsString);
      MPRTKZ.Text := Trim(ADOTemp.fieldbyname('CYKZ').AsString);
    end
    else if ADOTemp.RecordCount > 1 then
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from CP_YDang where CYNo=''' + Trim(OrdDefStr1.Text) + '''');
        Open;
      end;
      if ADOTemp.RecordCount = 1 then
      begin
        MPRTCodeName.Text := Trim(ADOTemp.fieldbyname('CYName').AsString);
        MPRTCF.Text := Trim(ADOTemp.fieldbyname('CYECF').AsString);
        MPRTSpec.Text := Trim(ADOTemp.fieldbyname('CYSpec').AsString);
        MPRTMF.Text := Trim(ADOTemp.fieldbyname('CYMF').AsString);
        MPRTKZ.Text := Trim(ADOTemp.fieldbyname('CYKZ').AsString);
      end;
    end;

  end;
end;

procedure TfrmOrderInPut.OrdDefStr2BtnUpClick(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) = 'OrdDefStr2' then
      begin
        //flag:='OrdDefStr2';
        V1Name.Caption := '中文名称';
        V1Note.Caption := '英文名称';
        fnote := True;
      end;
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.ConNoKeyPress(Sender: TObject; var Key: Char);
var
  ConMainId: string;
begin
  if Key = #13 then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrderCon_Main where ConNo like ''' + '%' + Trim(ConNo.Text) + '%' + '''');
      Open;
    end;
    if ADOTemp.RecordCount > 1 then
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select Top 1* from JYOrderCon_Main where ConNo=''' + Trim(ConNo.Text) + '''');
        Open;
      end;
      if ADOTemp.IsEmpty then
        Exit;
    end
    else if ADOTemp.RecordCount = 1 then
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from JYOrderCon_Main where ConNo like ''' + '%' + Trim(ConNo.Text) + '%' + '''');
        Open;
      end;
    end;
    begin
      ConNo.Text := Trim(ADOTemp.fieldbyname('ConNo').AsString);
      OrdDefStr1.Text := Trim(ADOTemp.fieldbyname('ConDefStr1').AsString);
      MPRTCF.Text := Trim(ADOTemp.fieldbyname('MPRTCF').AsString);
      MPRTSpec.Text := Trim(ADOTemp.fieldbyname('MPRTSpec').AsString);
      MPRTMF.Text := Trim(ADOTemp.fieldbyname('MPRTMF').AsString);
      MPRTKZ.Text := Trim(ADOTemp.fieldbyname('MPRTKZ').AsString);
      CustomerNoName.Text := Trim(ADOTemp.fieldbyname('CustomerNoName').AsString);
      CustomerNoName.TxtCode := Trim(ADOTemp.fieldbyname('CustomerNo').AsString);
      ConMainId := Trim(ADOTemp.fieldbyname('MainId').AsString);
     // ConGS:=Trim(ADOTemp.fieldbyname('SYRName').AsString);
      OrdDefStr2.Text := Trim(ADOTemp.fieldbyname('ConDefStr2').AsString);
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select A.*,ColorName=(select ZdyName from KH_Zdy B where B.Note=A.PRTColor and B.Type=''OrdColor'' ) ');
        SQL.Add(' from JYOrderCon_Sub A where MainId=''' + Trim(ConMainId) + '''');
        Open;
      end;
      with ADOTemp do
      begin
        First;
        while not Eof do
        begin
          with Order_Sub do
          begin
            Append;
            FieldByName('PRTColor').Value := Trim(ADOTemp.fieldbyname('PRTColor').AsString);
            FieldByName('SOrdDefStr4').Value := Trim(ADOTemp.fieldbyname('SOrdDefStr4').AsString);
            FieldByName('SOrdDefStr3').Value := Trim(ADOTemp.fieldbyname('SOrdDefStr3').AsString);
            FieldByName('PRTOrderQty').Value := Trim(ADOTemp.fieldbyname('PRTOrderQty').AsString);
            FieldByName('OrderUnit').Value := Trim(ADOTemp.fieldbyname('OrderUnit').AsString);
            FieldByName('PRTPrice').Value := Trim(ADOTemp.fieldbyname('PRTPrice').AsString);
            FieldByName('PriceUnit').Value := Trim(ADOTemp.fieldbyname('PriceUnit').AsString);
            FieldByName('PRTHX').Value := Trim(ADOTemp.fieldbyname('PRTHX').AsString);
            FieldByName('XHNO').Value := Trim(ADOTemp.fieldbyname('XHNO').AsString);
            Post;
          end;
          Next;
        end;
      end;
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select Top 1* from CP_YDang where CYNO=''' + Trim(OrdDefStr1.Text) + '''');
        Open;
      end;
      MPRTCodeName.Text := Trim(ADOTemp.fieldbyname('CYName').AsString);
      {with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select Top 1* from KH_Zdy where Note='''+Trim(ConGS)+''' and type=''OrdDefStr2'' ');
        Open;
      end;
      OrdDefStr2.Text:=Trim(ADOTemp.fieldbyname('ZdyName').AsString); }
    end;

  end;
end;

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

procedure TfrmOrderInPut.v1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTMF';
      flagname := '门幅';
      V1Name.Caption := '中文名称';
      V1Note.Caption := '英文名称';
  //    MainType:=Trim(DName);
      fnote := FALSE;
      if ShowModal = 1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PRTMF').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
    //    Self.Order_Sub.FieldByName('SOrddefstr4').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
        Self.Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmOrderInPut.v1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTKZ';
      flagname := '克重';
      V1Name.Caption := '中文名称';
      V1Note.Caption := '英文名称';
   //   MainType:=Trim(DName);
      fnote := FALSE;
      if ShowModal = 1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PRTKZ').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
    //    Self.Order_Sub.FieldByName('SOrddefstr4').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
        Self.Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmOrderInPut.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachCP := TfrmZdyAttachCP.Create(Application);
    with frmZdyAttachCP do
    begin
      if ShowModal = 1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PRTCode').Value := Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
        Self.Order_Sub.FieldByName('PRTCodeName').Value := Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
        Self.Order_Sub.FieldByName('PRTColor').Value := Trim(CDS_HZ.fieldbyname('DEFstr3').AsString);
    //    Self.Order_Sub.FieldByName('SOrddefstr4').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
        Self.Order_Sub.Post;
      end;
    end;
  finally
    frmZdyAttachCP.Free;
  end;
{ try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CP';
      flagname:='产品名称';
      V1Name.Caption:='中文名称';
      V1Note.Caption:='英文名称';
//      MainType:=Trim(DName);
      fnote:=FALSE;
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        Self.Order_Sub.FieldByName('PRTCodeName').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString)+'#';
    //    Self.Order_Sub.FieldByName('SOrddefstr4').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
        Self.Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;       }
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmOrderInPut.v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTSpec';
      flagname := '规格';
      V1Name.Caption := '中文名称';
      V1Note.Caption := '英文名称';
   //   MainType:=Trim(DName);
      fnote := FALSE;
      if ShowModal = 1 then
      begin
        Self.Order_Sub.Edit;
      //  Self.Order_Sub.FieldByName('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYCode').AsString);
        Self.Order_Sub.FieldByName('PRTSpec').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
    //    Self.Order_Sub.FieldByName('SOrddefstr4').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
        Self.Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmOrderInPut.FHAddressBtnUpClick(Sender: TObject);
begin
  if Trim(CustomerNoName.Text) = '' then
  begin
    Application.MessageBox('客户不能为空!', '提示', 0);
    Exit;
  end;
  try
    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag := 'FHAddress';
      flagname := '发货地址';
      MainType := Trim(CustomerNoName.Text);
      if ShowModal = 1 then
      begin
        FHAddress.Text := Trim(ReturnStr);
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderInPut.LXFSBtnUpClick(Sender: TObject);
begin
  if Trim(CustomerNoName.Text) = '' then
  begin
    Application.MessageBox('客户不能为空!', '提示', 0);
    Exit;
  end;
  try
    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag := 'LXFS';
      flagname := '联系方式';
      MainType := Trim(CustomerNoName.Text);
      if ShowModal = 1 then
      begin
        LXFS.Text := Trim(ReturnStr);
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderInPut.v1Column5PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;

  if trim(mvalue) = '' then
    exit;

  if rightBstr(trim(mvalue), 1) <> '#' then
  begin
    mvalue := trim(mvalue) + '#';
  end;

  with Order_Sub do
  begin
    Edit;
    FieldByName('PRTCodeName').Value := Trim(mvalue);
    Post;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
  DoKc();

  with ADOTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select *  from KH_Zdy_Attachment A ');
    sql.Add('where zdyName=' + quotedstr(trim(Order_Sub.fieldbyname('PRTCodeName').AsString)));
    open;
  end;
  if ADOTemp.IsEmpty then
  begin
    application.MessageBox('品名不存在！', '提示信息', 0);
    with Order_Sub do
    begin
      Edit;
      FieldByName('PRTCodeName').Value := '';
      Post;
    end;
  end;

end;

procedure TfrmOrderInPut.v1PRTColorPropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;

  if trim(mvalue) = '' then
    exit;

  if rightBstr(trim(mvalue), 1) <> '#' then
  begin
    mvalue := trim(mvalue) + '#';
  end;

  with Order_Sub do
  begin
    Edit;
    FieldByName('PRTColor').Value := Trim(mvalue);
    Post;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
  DoKc();
 {

  with ADOTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select *  from KH_Zdy_Attachment A ');
    sql.Add('where zdyName='+quotedstr(trim(Order_Sub.fieldbyname('PRTCodeName').AsString)));
    sql.Add('and DEFstr3='+quotedstr(trim(Order_Sub.fieldbyname('PRTColor').AsString)));
    open;
  end;
  IF ADOTemp.IsEmpty then
  begin
    application.MessageBox('颜色不存在！','提示信息',0);
    with Order_Sub do
    begin
      Edit;
      FieldByName('PRTColor').Value:='';
      Post;
    end;
  end;
    with Order_Sub do
    begin
      Edit;
      FieldByName('PRTCode').Value:=trim(ADOTemp.fieldbyname('ZDYCode').AsString);
      Post;
    end;
   self.tv1.Controller.EditingController.ShowEdit();   }
end;

procedure TfrmOrderInPut.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  DoKc();
end;

procedure TfrmOrderInPut.OrdPerson1BtnClick(Sender: TObject);
begin
  frmUserHelp := TfrmUserHelp.create(self);
  with frmUserHelp do
  begin
    if showmodal = 1 then
    begin
      OrdPerson1.Text := trim(ADOQueryHelp.fieldbyname('UserName').AsString);
    end;
    free;
  end;
end;

procedure TfrmOrderInPut.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  if trim(edit1.Text) = '' then
    exit;
  with ADOCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select *  from KH_Zdy_Attachment A ');
    sql.Add('where zdyName=' + quotedstr(trim(edit1.Text)));
    open;
    if IsEmpty then
    begin
      application.MessageBox('品名不存在！', '提示信息', 0);
      exit;
    end;
    Order_Sub.DisableControls;
    while not eof do
    begin
      Self.Order_Sub.Append;
      Self.Order_Sub.FieldByName('XHNO').Value := Self.Order_Sub.RecordCount + 1;
      Self.Order_Sub.FieldByName('OrderUnit').Value := '米';
      Self.Order_Sub.FieldByName('PRTCode').Value := Trim(fieldbyname('ZdyCode').AsString);
      Self.Order_Sub.FieldByName('PRTCodeName').Value := Trim(fieldbyname('ZDYName').AsString);
      Self.Order_Sub.FieldByName('PRTColor').Value := Trim(fieldbyname('DEFstr3').AsString);
      Self.Order_Sub.FieldByName('PRTOrderQty').Value := strtofloatdef(trim(edit2.Text), 0);
      Self.Order_Sub.FieldByName('PRTPrice').Value := strtofloatdef(trim(edit3.Text), 0);
      Self.Order_Sub.post;
      next;
    end;
    Order_Sub.First;
    Order_Sub.EnableControls;
  end;
  self.tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmOrderInPut.OrdDateChange(Sender: TObject);
begin
  if formatdateTime('yyyy-MM-dd', OrdDate.Date) > formatdateTime('yyyy-MM-dd', date()) then
  begin
    application.MessageBox('日期不能大于今天！', '提示信息', 0);
    OrdDate.Date := strtodate(formatdateTime('yyyy-MM-dd', date()));
    exit;
  end;
end;

procedure TfrmOrderInPut.CustomerNoNameChange(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := Trim(CustomerNoName.Text);
  if Length(Trim(mvalue)) > 1 then
  begin
    //mvalue:='%'+Trim(mvalue)+'%';
    with ADO_KH do
    begin
      Close;
      sql.Clear;
      sql.Add(' select * from KH_Zdy_Attachment ');
      sql.Add(' where (ZDYName like ' + quotedstr('%' + trim(mvalue) + '%'));
      sql.Add(' or dbo.getpinYin(zdyName) like ' + quotedstr('%' + trim(mvalue) + '%'));
      sql.Add(' ) and isnull(KHType,'''')=' + quotedstr(trim(SYRName.Text)));
      sql.Add(' and Type=''KHName'' ');
      Open;
    end;
    if ADO_KH.IsEmpty then
      cxGrid4.Visible := False
    else
      cxGrid4.Visible := True;
  end;
end;

procedure TfrmOrderInPut.TV4CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  CustomerNoName.OnChange := nil;
  CustomerNoName.Text := Trim(ADO_KH.fieldbyname('ZDYName').AsString);
  CustomerNoName.TxtCode := Trim(ADO_KH.fieldbyname('ZdyCode').AsString);
  FHAddress.Text := Trim(ADO_KH.fieldbyname('DEFNote2').AsString);
  LXFS.Text := Trim(ADO_KH.fieldbyname('DEFstr2').AsString);
  cxGrid4.Visible := False;
  CustomerNoName.OnChange := CustomerNoNameChange;
end;

procedure TfrmOrderInPut.TV4KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if Key = VK_RETURN then
  begin
    CustomerNoName.OnChange := nil;
    CustomerNoName.Text := Trim(ADO_KH.fieldbyname('ZDYName').AsString);
    CustomerNoName.TxtCode := Trim(ADO_KH.fieldbyname('ZdyCode').AsString);
    FHAddress.Text := Trim(ADO_KH.fieldbyname('DEFNote2').AsString);
    LXFS.Text := Trim(ADO_KH.fieldbyname('DEFstr2').AsString);
    cxGrid4.Visible := False;
    CustomerNoName.OnChange := CustomerNoNameChange;
  end;
end;

procedure TfrmOrderInPut.CustomerNoNameKeyPress(Sender: TObject; var Key: Char);
begin
  CustomerNoName.OnChange := CustomerNoNameChange;
  if Key = #13 then
  begin
    CustomerNoName.OnChange := nil;
    if (cxGrid4.Visible = true) and (not ADO_KH.IsEmpty) then
    begin
      CustomerNoName.OnChange := nil;
      CustomerNoName.Text := Trim(ADO_KH.fieldbyname('ZDYName').AsString);
      CustomerNoName.TxtCode := Trim(ADO_KH.fieldbyname('ZdyCode').AsString);
      FHAddress.Text := Trim(ADO_KH.fieldbyname('DEFNote2').AsString);
      LXFS.Text := Trim(ADO_KH.fieldbyname('DEFstr2').AsString);
      cxGrid4.Visible := False;
    end;
  end;
  //CustomerNoName.OnChange:=CustomerNoNameChange;
 // cxGrid4.Visible:=False;
end;

procedure TfrmOrderInPut.v1PRTOrderQtyPropertiesEditValueChanged(Sender: TObject);
var
  fieldname: string;
  num1: double;
begin

  fieldname := Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  with Order_Sub do
  begin
    Edit;
    FieldByName(Trim(fieldname)).Value := TcxTextEdit(Sender).EditingText;
    num1 := FieldByName('PRTOrderQty').AsFloat * FieldByName('PRTPrice').AsFloat + FieldByName('gangFee').AsFloat + FieldByName('yunFee').AsFloat + FieldByName('otherFee').AsFloat;
    FieldByName('Money').Value := strtofloat(format('%.0f', [num1]));
    Post;
  end;
  Tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmOrderInPut.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  if (not Order_Sub.IsEmpty) and (trim(FMainId) = '') then
  begin
    if application.MessageBox('您有数据未保存？您是否要退出？', '提示信息!', MB_YESNO + MB_ICONQUESTION) = IDYES then
      CanClose := True
    else
      CanClose := False;
  end;
end;

procedure TfrmOrderInPut.TYBBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'TYBType';
      flagname := '托运部';
      fnote := true;
      v1name.Caption := '托运部';
      v1note.Caption := '托运部电话';
      if ShowModal = 1 then
      begin
        TYB.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        TYBTel.Text := Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.

