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, cxCurrencyEdit, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxSkinsCore, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom,
  dxSkinDarkSide, dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle,
  dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary,
  dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin,
  dxSkinMetropolis, dxSkinMetropolisDark, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin,
  dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinsDefaultPainters, dxSkinValentine,
  dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue, dxSkinscxPCPainter;

type
  TfrmOrderInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    OrderNo: TEdit;
    Label7: TLabel;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    v1PRTColor: TcxGridDBColumn;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    v1PRTOrderQty: TcxGridDBColumn;
    Label10: TLabel;
    ConNo: TEdit;
    RanFactory: TBtnEditC;
    v1XHNo: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1SOrddefstr1: TcxGridDBColumn;
    v1SOrddefstr2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Label3: TLabel;
    Label14: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    OrdDate: TDateTimePicker;
    CustomerNoName: TBtnEditC;
    MPRTMF: TBtnEditC;
    MPRTKZ: TBtnEditC;
    Label2: TLabel;
    MPRTCodeName: TBtnEditC;
    Label6: TLabel;
    MPRTSpec: TBtnEditC;
    Label4: TLabel;
    Label5: TLabel;
    MPRTDuiSeGY: TBtnEditC;
    Label11: TLabel;
    MPRTSL: TBtnEditC;
    Label9: TLabel;
    MPRTNiuDu: TBtnEditC;
    Label8: TLabel;
    MPRTPH: TBtnEditC;
    Label15: TLabel;
    HZLFactory: TBtnEditC;
    Label16: TLabel;
    PBFactory: TBtnEditC;
    Label17: TLabel;
    YCLFactory: TBtnEditC;
    Label18: TLabel;
    MPRTZHName: TEdit;
    Label19: TLabel;
    Label20: TLabel;
    MPRTKaiJian: TBtnEditC;
    Label21: TLabel;
    MPRTChuanY: TBtnEditC;
    Label22: TLabel;
    MPRTYaoFeng: TBtnEditC;
    Label23: TLabel;
    MPRTJiBenNote: TBtnEditC;
    Label24: TLabel;
    MPRTTeBieNote: TBtnEditC;
    Label25: TLabel;
    DLYDate: TDateTimePicker;
    Label26: TLabel;
    MPRTCode: TBtnEditC;
    Label27: TLabel;
    MPRTQtyNote: TBtnEditC;
    Label28: TLabel;
    Label29: TLabel;
    Label30: TLabel;
    Label31: TLabel;
    MPRTColorLaoDu1: TBtnEditC;
    MPRTColorLaoDu2: TBtnEditC;
    MPRTColorLaoDu3: TBtnEditC;
    MPRTColorLaoDu4: TBtnEditC;
    Label32: TLabel;
    Label33: TLabel;
    Label34: TLabel;
    MPRTKQiQiu: TBtnEditC;
    MPRTDingPoLv: TBtnEditC;
    MPRTZuRanXing: TBtnEditC;
    Label35: TLabel;
    MPRTKuanNO: TBtnEditC;
    Label36: TLabel;
    Label37: TLabel;
    Label38: TLabel;
    MPRTTangJin: TBtnEditC;
    Label39: TLabel;
    OrdDefDate1: TDateTimePicker;
    Label40: TLabel;
    OrdDefDate2: TDateTimePicker;
    Label41: TLabel;
    OrdPerson1: TBtnEditC;
    Label42: TLabel;
    Label43: TLabel;
    OrdPerson2: TBtnEditC;
    LBNameNote: TMemo;
    MaiTouNote: TMemo;
    Label44: TLabel;
    SYRName: TBtnEditC;
    MPRTBZNote: TMemo;
    MPRTSCTeBieNote: TMemo;
    CPCFBi: TEdit;
    Label45: TLabel;
    v1Column1: TcxGridDBColumn;
    Label46: TLabel;
    orddefnote1: TMemo;
    orddefstr1: TBtnEditC;
    Label47: TLabel;
    orddefstr2: TEdit;
    orddefstr4: TBtnEditC;
    orddefstr3: TEdit;
    Label48: TLabel;
    Label49: TLabel;
    EditCnt: TcxCurrencyEdit;
    v1Column2: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Label50: TLabel;
    Label51: TLabel;
    OrdDefDate3: TDateTimePicker;
    OrdDefDate4: TDateTimePicker;
    cxGrid4: TcxGrid;
    TV4: TcxGridDBTableView;
    V4Column1: TcxGridDBColumn;
    V4Column3: TcxGridDBColumn;
    V4Column2: TcxGridDBColumn;
    V4Column4: TcxGridDBColumn;
    cxGrid4Level1: TcxGridLevel;
    CDS_CON: TClientDataSet;
    DSCON: TDataSource;
    ADOQueryTmp: TADOQuery;
    Label52: TLabel;
    CGCONNO: TBtnEditA;
    Label53: TLabel;
    OrdType: TComboBox;
    CompanyName: TcxButtonEdit;
    Label54: TLabel;
    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 RanFactoryBtnUpClick(Sender: TObject);
    procedure CustomerNoNameBtnUpClick(Sender: TObject);
    procedure CustomerNoNameBtnDnClick(Sender: TObject);
    procedure MPRTCodeNameBtnUpClick(Sender: TObject);
    procedure NoteDblClick(Sender: TObject);
    procedure v1OrderUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1PriceUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure OrdDefStr2BtnUpClick(Sender: TObject);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton3Click(Sender: TObject);
    procedure YCLFactoryBtnUpClick(Sender: TObject);
    procedure MPRTSCTeBieNoteBtnUpClick(Sender: TObject);
    procedure Label36DblClick(Sender: TObject);
    procedure Label37DblClick(Sender: TObject);
    procedure SYRNameBtnUpClick(Sender: TObject);
    procedure MPRTBZNoteDblClick(Sender: TObject);
    procedure MPRTSCTeBieNoteDblClick(Sender: TObject);
    procedure TV4CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ConNoChange(Sender: TObject);
    procedure CGCONNOBtnClick(Sender: TObject);
    procedure CompanyNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    procedure InitData();
    procedure ZDYHelp(FButn:TcxButtonEdit;LType:string);
    function  SaveData():Boolean;
    function  SaveDataSubOne():Boolean;
    function  SaveDataMain():Boolean;
    function SaveDataSubMore():Boolean;
    function getOrderNo(fconNo:string):string;
    { Private declarations }
  public
    PState,CopyInt,PriceFlag:Integer;
    FMainId,FFMainId,FOrderNo:String;
    FXS:Integer;
    canshu1:string;
    { Public declarations }
  end;

var
  frmOrderInPut: TfrmOrderInPut;
  newh:hwnd;
implementation
uses
  U_DataLink,U_ZDYHelp,U_Fun,U_ZDYHelpSel, U_ZdyAttachment,
  U_ClothContractListHelp;

{$R *.dfm}
function TfrmOrderInPut.getOrderNo(fconNo:string):string;
var
  str1:string;
  i:integer;
begin
  str1:=trim(fconNo);
  with ADOTemp do
  begin
    close;                              
    sql.Clear;
    sql.Add('select MainID from JYOrder_Main where orderNo='+quotedstr(trim(str1)));
    open;
  end;
  IF not ADOTemp.IsEmpty then
  begin
    for i:=1 to 99 do
    begin
      str1:=fconNo+'-'+inttostr(i);
      with ADOCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select MainID from JYOrder_Main where orderNo='+quotedstr(trim(str1)));
        open;
      end;
      IF ADOCmd.IsEmpty then break;
    end;
  end;

  result:=str1;
end;

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

procedure TfrmOrderInPut.InitData();
begin
conno.OnChange:=nil;
    OrdDate.DateTime:=SGetServerDateTime(ADOTemp);
    DlyDate.DateTime:=OrdDate.DateTime;
    OrdDefDate1.DateTime:=OrdDate.DateTime;
    OrdDefDate2.DateTime:=OrdDate.DateTime;
    OrdDefDate3.DateTime:=OrdDate.DateTime;
    OrdDefDate4.DateTime:=OrdDate.DateTime;
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from JYOrder_Sub where 1=1 ');
    if PState=1 then
    begin
      sql.Add(' and MainId='''+Trim(FMainId)+'''');
    end;
    if PState=0 then
    begin
      sql.Add(' and 1<>1');
    end;
    //ShowMessage(SQL.Text);
    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;
  SCSHDataNew(ADOQuery1,ScrollBox1,2);
  if PState=0 then
  begin
    OrdDate.DateTime:=SGetServerDateTime(ADOTemp);
    DlyDate.DateTime:=OrdDate.DateTime;
    OrdDefDate1.DateTime:=OrdDate.DateTime;
    OrdDefDate2.DateTime:=OrdDate.DateTime;
    OrdDefDate3.DateTime:=OrdDate.DateTime;
    OrdDefDate4.DateTime:=OrdDate.DateTime;
  end
  else
  begin

  end;
  if CopyInt=99 then
  begin
    PState:=0;
    FMainId:='';
    //OrdPerson1.Text:=Trim(DName);
    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;
   conno.OnChange:=ConNoChange;
end;

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

procedure TfrmOrderInPut.FormShow(Sender: TObject);
begin
  readCxGrid('指示单录入AA',Tv1,'生产指示单管理');
  {if PriceFlag=99 then
  begin
    v1PRTPrice.Visible:=False;
    v1PRTPrice.Hidden:=True;
  end;}
  if PState=1 then
  begin
    Label46.Visible:=true;
    orddefnote1.Visible:=true;
    Label49.Visible:=true;
    EditCnt.Visible:=true;
  end;
  InitData();
end;

function TfrmOrderInPut.SaveData():Boolean;
var
  maxno:String;
begin
  try

    ///保存主表
    if Trim(FMainId)='' then
    begin
      if GetLSNo(ADOTemp,maxno,'JM','JYOrder_Main',3,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
      OrderNo.text:=getOrderNO(trim(conNo.Text));
    end
    else
    begin
      maxno:=Trim(FMainId);
    end;
    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);
      FieldByName('LBNameNote').Value:=Trim(LBNameNote.Text);
      FieldByName('MaiTouNote').Value:=MaiTouNote.Text;
      FieldByName('editcnt').Value:=strtointdef(trim(editcnt.Text),0);

      Post;
    end;

    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;
    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,'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);
          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('PRTPrice').Value:=Order_Sub.FieldByName('PRTPrice').AsString;
          fieldbyname('PriceUnit').Value:=Order_Sub.FieldByName('PriceUnit').AsString;
          FieldByName('Sorddefstr10').Value:=Order_Sub.FieldByName('Sorddefstr10').AsString;
          FieldByName('OrderNo').Value:=Trim(orderNo.Text);
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('SubId').Value:=Trim(maxno);
        //Order_Sub.Post;
        Next;
      end;
    end;
    {if Trim(Order_Sub.FieldByName('PRTPrice').AsString)<>'' then
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR ');
        sql.Add('where MainId='''+Trim(FMainId)+'''');
        SQL.Add(' and CRType=''应收款登记'' ');
        SQL.Add(' and YFType=''自动生成'' ');
        Open;
      end;
      if ADOTemp.RecordCount=1 then
      begin
        
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR set Price='+Order_Sub.FieldByName('PRTPrice').AsString);
          sql.Add(',BZType='''+Trim(Order_Sub.FieldByName('PriceUnit').AsString)+'''');
          SQL.Add(' where YFID='''+Trim(ADOTemp.fieldbyname('YFID').AsString)+'''');
          ExecSQL;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR Set Money=Price*Qty,BBMoney=Price*Qty*HuiLv');
          SQL.Add(' where YFID='''+Trim(ADOTemp.fieldbyname('YFID').AsString)+'''');
          ExecSQL;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_KC Set KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
          sql.Add(' ,KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
          sql.Add(' where CRID='+ADOTemp.fieldbyname('CRID').AsString);
          ExecSQL;
        end;
      end;

    end;  }
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

function TfrmOrderInPut.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
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

function TfrmOrderInPut.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 TfrmOrderInPut.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 TfrmOrderInPut.TBSaveClick(Sender: TObject);
begin
  OrdDate.SetFocus;
  if Trim(OrderNo.Text)='' then
  begin
    Application.MessageBox('订单号不能为空！','提示',0);
    Exit;
  end;
  if Trim(conNo.Text)='' then
  begin
    Application.MessageBox('合同号不能为空！','提示',0);
    Exit;
  end;
  if Trim(ordType.Text)='' then
  begin
    Application.MessageBox('订单类型不能为空！','提示',0);
    Exit;
  end;
  {if Trim(OrdDefStr1.Text)='' 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('SOrddefstr4',null,[])=True then
  begin
    Application.MessageBox('英文颜色不能为空!','提示',0);
    Exit;
  end; }
{  if Order_Sub.Locate('SOrddefstr1',null,[])=True then
  begin
    Application.MessageBox('色号不能为空!','提示',0);
    Exit;
  end;   }


      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from JYOrderCon_main where conNO='''+Trim(connO.Text)+''' and status=''1'' ');
        Open;
      end;
      if ADOTemp.IsEmpty then
      begin
        Application.MessageBox('合同没有审核，不能生成指示单!','提示',0);
        Exit;
      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;
 { if PState=1 then
  begin
    if trim(orddefnote1.Text)='' then
    begin
      Application.MessageBox('修改备注信息不能为空!','提示',0);
      Exit;
    end;
  end;  }

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

procedure TfrmOrderInPut.v1PRTColorPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrdColor';
      flagname:='颜色';
      V1Name.Caption:='中文名称';
      V1Note.Caption:='英文名称';
 //     MainType:=Trim(DName);
      fnote:=True;
      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 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);
   // FieldByName('SOrddefstr1').Value:=IntToStr(i);
  {  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:=0;
    //FieldByName('PRTPrice').Value:=null;
    FieldByName('SOrddefstr4').Value:=null;
    FieldByName('SOrddefstr2').Value:=null;
    FieldByName('SOrddefstr10').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 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;
 { 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 TfrmOrderInPut.RanFactoryBtnUpClick(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
 { try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CustomerNoName';
      flagname:='客户名称';
      if Trim(DParameters1)<>'高权限' then
        MainType:=Trim(DName);
      if ShowModal=1 then
      begin
        CustomerNoName.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        CustomerNoName.TxtCode:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;  }
  try
    frmZdyAttachment:=TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      fkhType:='';
      IF self.canshu1<>'高权限' then canshu1:='业务员';
      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);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

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

procedure TfrmOrderInPut.MPRTCodeNameBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='MPRTCodeName';
      flagname:='产品名称';
      //fnote:=True;
      //V1Note.Caption:='产品编号';
      if ShowModal=1 then
      begin
        MPRTCodeName.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        //MPRTCode.Text:=Trim(ClientDataSet1.fieldbyname('Note').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);
var
  mvalues:string;
begin
  mvalues:='OrderUnit';
  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(mvalues).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 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 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);
        if Trim(flag)='MPRTCode' then
        begin
          with ADOTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select Top 1 * from JYOrder_Main where MPRTCode='''+Trim(ClientDataSet1.fieldbyname('ZDYName').AsString)+'''');
            sql.Add(' order by FillTime desc');
            Open;
          end;
          MPRTCodeName.Text:=ADOTemp.fieldbyname('MPRTCodeName').AsString;
          MPRTMF.Text:=ADOTemp.fieldbyname('MPRTMF').AsString;
          MPRTKZ.Text:=ADOTemp.fieldbyname('MPRTKZ').AsString;
          MPRTSpec.Text:=ADOTemp.fieldbyname('MPRTSpec').AsString;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.ConNoKeyPress(Sender: TObject; var Key: Char);
var
  ConMainId:string;
begin
  if Key=#13 then
  begin
      cxGrid4.Visible:=false;
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from JYOrderCon_main where conNO='''+Trim(connO.Text)+''' and status=''1'' ');
        Open;
      end;
      if ADOTemp.IsEmpty then
      begin
        Application.MessageBox('合同没有审核，不能生成指示单!','提示',0);
        Exit;
      end;
    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);
      MPRTCodeName.Text:=Trim(ADOTemp.fieldbyname('MPRTCodeName').AsString);
      MPRTZHName.Text:=Trim(ADOTemp.fieldbyname('CPZHName').AsString);
      MPRTMF.Text:=Trim(ADOTemp.fieldbyname('MPRTMF').AsString);
      MPRTKZ.Text:=Trim(ADOTemp.fieldbyname('MPRTKZ').AsString);
      MPRTChuanY.Text:=Trim(ADOTemp.fieldbyname('ChuanYangNote').AsString);
      MPRTQtyNote.Text:=Trim(ADOTemp.fieldbyname('QtyNote').AsString);
      CustomerNoName.Text:=Trim(ADOTemp.fieldbyname('CustomerNoName').AsString);
      CustomerNoName.TxtCode:=Trim(ADOTemp.fieldbyname('CustomerNo').AsString);
      DLYDate.DateTime:=ADOTemp.fieldbyname('DLYDate').Value;
      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 top 1 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;
      IF not ADOTemp.IsEmpty then
      begin
        MPRTCode.Text:=Trim(ADOTemp.fieldbyname('PRTCode').AsString);
        MPRTCodeName.Text:=Trim(ADOTemp.fieldbyname('PRTCodeName').AsString);
        MPRTspec.Text:=Trim(ADOTemp.fieldbyname('PRTspec').AsString);
        MPRTMF.Text:=Trim(ADOTemp.fieldbyname('PRTMF').AsString);
        MPRTKZ.Text:=Trim(ADOTemp.fieldbyname('PRTKZ').AsString);
      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);
            FieldByName('Sorddefstr10').Value:=Trim(ADOTemp.fieldbyname('SUBID').AsString);

            Post;
          end;
          Next;
        end;
      end;
    end;

  end;
end;

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

procedure TfrmOrderInPut.YCLFactoryBtnUpClick(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);
      //MainType:=TEdit(Sender).Name;
      if ShowModal=1 then
      begin
        TEdit(Sender).Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.MPRTSCTeBieNoteBtnUpClick(Sender: TObject);
var
  fsj:string;
  FWZ:Integer;
begin
  fsj:=Trim(TEdit(Sender).Hint);
  FWZ:=Pos('/',fsj);
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:=Copy(fsj,1,FWZ-1);
      flagname:=Copy(fsj,FWZ+1,Length(fsj)-fwz);
      if ShowModal=1 then
      begin
        TEdit(Sender).Text:=ReturnStr;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;


procedure TfrmOrderInPut.Label36DblClick(Sender: TObject);
begin
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:='LbNameNote';
      flagname:='标签内容';
      if ShowModal=1 then
      begin
        ClientDataSet1.DisableControls;
        //LBNameNote.Text:='';
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if ClientDataSet1.FieldByName('SSel').AsBoolean=True then
            begin
              if Trim(Self.LBNameNote.Text)='' then
              begin
                Self.LBNameNote.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
              end else
              begin
                Self.LBNameNote.Text:=Self.LBNameNote.Text+#13+#10+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
              end;

            end;
            Next;
          end;
        end;
        ClientDataSet1.EnableControls;
      end;
    end;
  finally
  end;
end;

procedure TfrmOrderInPut.Label37DblClick(Sender: TObject);
begin
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:='MaitouNote';
      flagname:='唛头内容';
      if ShowModal=1 then
      begin
        ClientDataSet1.DisableControls;
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if ClientDataSet1.FieldByName('SSel').AsBoolean=True then
            begin
              if Trim(Self.MaitouNote.Text)='' then
              begin
                Self.MaitouNote.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
              end else
              begin
                Self.MaitouNote.Text:=Self.MaitouNote.Text+#13+#10+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
              end;

            end;
            Next;
          end;
        end;
        ClientDataSet1.EnableControls;
      end;
    end;
  finally
  end;
end;

procedure TfrmOrderInPut.SYRNameBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrdDefStr2';
      flagname:='公司抬头';
      V1Name.Caption:='中文名称';
      V1Note.Caption:='英文名称';
      fnote:=True;
      if Trim(DParameters1)<>'高权限' then
      begin
        TBAdd.Visible:=False;
        TBEdit.Visible:=false;
        TBDel.Visible:=false;
        TBSave.Visible:=false;
      end;
      if ShowModal=1 then
      begin
        SYRName.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.MPRTBZNoteDblClick(Sender: TObject);
var
  fsj:string;
  FWZ:Integer;
  i:integer;
begin
  fsj:=Trim(TMemo(Sender).Hint);
  FWZ:=Pos('/',fsj);
  i:=0;
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:=Copy(fsj,1,FWZ-1);
      flagname:=Copy(fsj,FWZ+1,Length(fsj)-fwz);
      if ShowModal=1 then
      begin
        MPRTBZNote.Lines.Clear;
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if FieldByName('SSel').AsBoolean=True then
            begin
              i:=i+1;
              MPRTBZNote.Lines.Add(inttostr(i)+'.'+FieldByName('ZDYName').AsString)
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderInPut.MPRTSCTeBieNoteDblClick(Sender: TObject);
var
  fsj:string;
  FWZ:Integer;
  i:integer;
begin
  fsj:=Trim(TMemo(Sender).Hint);
  FWZ:=Pos('/',fsj);
  i:=1;
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:=Copy(fsj,1,FWZ-1);
      flagname:=Copy(fsj,FWZ+1,Length(fsj)-fwz);
      if ShowModal=1 then
      begin
        MPRTSCTeBieNote.Lines.Clear;
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if FieldByName('SSel').AsBoolean=True then
            begin
              i:=i+1;
              MPRTSCTeBieNote.Lines.Add(inttostr(i)+'.'+FieldByName('ZDYName').AsString)
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;


procedure TfrmOrderInPut.TV4CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  conno.OnChange:=nil;
  Order_Sub.EmptyDataSet;
  cxGrid4.Visible:=False;
  conNo.Text:=CDS_CON.fieldbyname('conNO').AsString;
  cxGrid4.Visible:=False;
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrderCon_Main where mainID='+quotedstr(trim(CDS_CON.fieldbyname('mainID').AsString)));
      Open;
    end;

    if not ADOQuery1.IsEmpty then
    begin
    //  ConNo.Text:=Trim(ADOQuery1.fieldbyname('ConNo').AsString);
      //OrdDefStr1.Text:=Trim(ADOTemp.fieldbyname('ConDefStr1').AsString);

     ConNo.Text:=Trim(ADOQuery1.fieldbyname('ConNo').AsString);
      //OrdDefStr1.Text:=Trim(ADOTemp.fieldbyname('ConDefStr1').AsString);
      MPRTCodeName.Text:=Trim(ADOQuery1.fieldbyname('MPRTCodeName').AsString);
      MPRTZHName.Text:=Trim(ADOQuery1.fieldbyname('CPZHName').AsString);
      MPRTMF.Text:=Trim(ADOQuery1.fieldbyname('MPRTMF').AsString);
      MPRTKZ.Text:=Trim(ADOQuery1.fieldbyname('MPRTKZ').AsString);
      MPRTChuanY.Text:=Trim(ADOQuery1.fieldbyname('ChuanYangNote').AsString);
      MPRTQtyNote.Text:=Trim(ADOQuery1.fieldbyname('QtyNote').AsString);
      CustomerNoName.Text:=Trim(ADOQuery1.fieldbyname('CustomerNoName').AsString);
      CustomerNoName.TxtCode:=Trim(ADOQuery1.fieldbyname('CustomerNo').AsString);
      DLYDate.DateTime:=ADOQuery1.fieldbyname('DLYDate').Value;
      
      with ADOQuery1 do
      begin
        Close;
        sql.Clear;
        sql.Add('select A.*,ColorName=(select top 1 ZdyName from KH_Zdy B where B.Note=A.PRTColor and B.Type=''OrdColor'' ) ');
        SQL.Add(' from JYOrderCon_Sub A ');
        SQL.Add(' where mainID='+quotedstr(trim(CDS_CON.fieldbyname('mainID').AsString)));
        Open;
      end;
      IF not ADOQuery1.IsEmpty then
      begin
        MPRTCode.Text:=Trim(ADOQuery1.fieldbyname('PRTCode').AsString);
        MPRTCodeName.Text:=Trim(ADOQuery1.fieldbyname('PRTCodeName').AsString);
        MPRTspec.Text:=Trim(ADOQuery1.fieldbyname('PRTspec').AsString);
        MPRTMF.Text:=Trim(ADOQuery1.fieldbyname('PRTMF').AsString);
        MPRTKZ.Text:=Trim(ADOQuery1.fieldbyname('PRTKZ').AsString);
      end;
      with ADOQuery1 do
      begin
        First;
        while not Eof do
        begin
          with Order_Sub do
          begin
            Append;
            FieldByName('PRTColor').Value:=Trim(ADOQuery1.fieldbyname('PRTColor').AsString);
            FieldByName('SOrdDefStr4').Value:=Trim(ADOQuery1.fieldbyname('SOrdDefStr4').AsString);
            FieldByName('SOrdDefStr3').Value:=Trim(ADOQuery1.fieldbyname('SOrdDefStr3').AsString);
            FieldByName('PRTOrderQty').Value:=Trim(ADOQuery1.fieldbyname('PRTOrderQty').AsString);
            FieldByName('OrderUnit').Value:=Trim(ADOQuery1.fieldbyname('OrderUnit').AsString);
            FieldByName('PRTPrice').Value:=Trim(ADOQuery1.fieldbyname('PRTPrice').AsString);
            FieldByName('PriceUnit').Value:=Trim(ADOQuery1.fieldbyname('PriceUnit').AsString);
            FieldByName('PRTHX').Value:=Trim(ADOQuery1.fieldbyname('PRTHX').AsString);
            FieldByName('XHNO').Value:=Trim(ADOQuery1.fieldbyname('XHNO').AsString);
            FieldByName('Sorddefstr10').Value:=Trim(ADOQuery1.fieldbyname('SUBID').AsString);
            Post;
          end;
          Next;
        end;
      end;
    end;
  conno.OnChange:=ConNoChange;
end;

procedure TfrmOrderInPut.ConNoChange(Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=Trim(conNO.Text);
  if Length(Trim(mvalue))>4 then
  begin
  mvalue:='%'+Trim(mvalue)+'%';
  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select distinct A.conNO,B.MainID,B.subID,B.prtCode,B.PrtCodeName,B.Prtspec from JYOrdercon_Main A inner join JYOrdercon_sub B on B.mainID=A.mainID');
    sql.Add('where A.conNO like '+quotedstr('%'+trim(mvalue)+'%'));
    sql.Add('and status=''1''  ');
    Open;
  end;
  SCreateCDS20(ADOQueryTmp,CDS_Con);
  SInitCDSData20(ADOQueryTmp,CDS_CON);
  if CDS_Con.IsEmpty then cxGrid4.Visible:=False else cxGrid4.Visible:=True;
  end;

end;

procedure TfrmOrderInPut.CGCONNOBtnClick(Sender: TObject);
begin
     try
      frmClothContractListHelp:=TfrmClothContractListHelp.Create(Application);
      with frmClothContractListHelp do
      begin
        if ShowModal=1 then
        begin
          CGConNo.Hint:=Order_Main.fieldbyname('mainID').AsString;
          CGConNo.Text:=Order_Main.fieldbyname('conNo').AsString;
        end;
      end;
      finally
        frmClothContractListHelp.Free;
      end;
end;

procedure TfrmOrderInPut.CompanyNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
   try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrdDefStr2';
      flagname:='需方';
      if ShowModal=1 then
      begin
        FXS:=99;
        CompanyName.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
