unit U_MLOrderInPut_JHLR;

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

type
  TfrmMLOrderInPut_JHLR = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    MLOrderNo: TEdit;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    v1MLColor: TcxGridDBColumn;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    v1Qty: TcxGridDBColumn;
    Label10: TLabel;
    v1XHNo: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1MLColorNo: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Label3: TLabel;
    Label14: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    OrdDate: TDateTimePicker;
    KHName: TBtnEditC;
    JGLXR: TBtnEditC;
    JGLXFS: TBtnEditC;
    Label2: TLabel;
    JGDanWei: TBtnEditC;
    Label6: TLabel;
    JGAdd: TBtnEditC;
    Label41: TLabel;
    GenDan: TBtnEditC;
    Label43: TLabel;
    YWY: TBtnEditC;
    v1Ssel: TcxGridDBColumn;
    CDS_JGMX: TClientDataSet;
    DSCON: TDataSource;
    JGtype: TBtnEditC;
    Label53: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    MLConNo: TBtnEditC;
    ADOQueryCmd: TADOQuery;
    CYID: TEdit;
    ADOQueryTmp: TADOQuery;
    ADOQuery2: TADOQuery;
    v1TrueSe: TcxGridDBColumn;
    Label61: TLabel;
    SHName: TEdit;
    Label25: TLabel;
    SHAdd: TBtnEditC;
    Label59: TLabel;
    SHLXFS: TEdit;
    Label60: TLabel;
    GenDanLXR: TBtnEditC;
    Label19: TLabel;
    GYNote: TBtnEditC;
    Label42: TLabel;
    BZNote: TBtnEditC;
    Label58: TLabel;
    ZLNote: TBtnEditC;
    Label62: TLabel;
    QtyNote: TBtnEditC;
    Label63: TLabel;
    QTnote: TBtnEditC;
    Panel1: TPanel;
    SGYang: TMemo;
    Label23: TLabel;
    Label4: TLabel;
    MLCodeNameM: TBtnEditC;
    MLCodeM: TEdit;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    V2Column1: TcxGridDBColumn;
    V2Column2: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    V2Column4: TcxGridDBColumn;
    ToolButton4: TToolButton;
    V2Column5: TcxGridDBColumn;
    V2Column6: 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 KHNameBtnUpClick(Sender: TObject);
    procedure KHNameBtnDnClick(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 v1Column3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure MLConNoBtnUpClick(Sender: TObject);
    procedure CYIDKeyPress(Sender: TObject; var Key: Char);
    procedure v1PRTCodePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure MLCodeNameMBtnUpClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    procedure InitData();
    function  SaveData():Boolean;
    function  getOrderNo(fconNo:string):string;
    procedure CFSaveData();
    { Private declarations }
  public
    PState,CopyInt,PriceFlag:Integer;
    FMainId,FFMainId,FOrderNo:String;
    FXS:Integer;
    canshu1,canshu2:string;
    fFlileFlag:string;
    { Public declarations }
  end;

var
  frmMLOrderInPut_JHLR: TfrmMLOrderInPut_JHLR;
  newh:hwnd;
implementation
uses
  U_DataLink,U_ZDYHelp,U_Fun,U_ZDYHelpSel, U_ZdyAttachment, U_ZdyAttachGYS,
  U_CPManageSel, getpic, U_ZdyAttachCP_HX_sel,U_ContractList_CX,U_KHList_CX,U_GYSList;

{$R *.dfm}
procedure TfrmMLOrderInPut_JHLR.CFSaveData();
var
  fCFID:string;
begin
  with ADOQuery2 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from JYOrder_Sub_CF where subID='''+Trim(Order_Sub.fieldbyname('subID').asstring)+'''');
    open;
  end;
  if ADOQuery2.IsEmpty then
  begin
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      SQL.Add('select A.CengJI,A.MLName,A.MLSeHao,A.MLID ');
      sql.Add(',HXNo=(select Top 1 B.HXNo from CP_YDang_JG B where B.CengJi=A.CengJi and B.CYID=A.CYID and B.IFZY=1)');
      sql.Add(',GYDM=(select Top 1 B.GYDM from CP_YDang_JG B where B.CengJi=A.CengJi and B.CYID=A.CYID and B.IFZY=1)');
      sql.Add(' from CP_YDang_ML A ');
      SQL.Add('where A.CYID='''+trim(Order_Sub.fieldbyname('PRTCode').AsString)+'''');
      sql.Add(' and A.IFSel=1 ');
      sql.Add(' order by A.Cengji,A.MLID');
      open;
    end;
    while not ADOQuery1.Eof do
    begin
      IF GetLSNo(ADOQueryCmd,fCFID,'JC','JYOrder_Sub_CF',4,1)=False then
      begin
        Application.MessageBox('取子流水号失败！','提示',0);
        Exit;
      end;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from JYOrder_Sub_CF where CFID='''+Trim(fCFID)+'''');
        Open;
        IF IsEmpty then Append
        else edit;
        fieldbyname('CFID').Value:=trim(fCFID);
        fieldbyname('PRTCode').Value:=trim(Order_Sub.fieldbyname('PRTCode').AsString);
        fieldbyname('PrtCodeName').Value:=trim(ADOQuery1.fieldbyname('MLName').AsString);
        fieldbyname('PrtSpec').Value:=trim(Order_Sub.fieldbyname('PrtSpec').AsString);
        fieldbyname('SOrddefstr1').Value:=trim(ADOQuery1.fieldbyname('MLSeHao').AsString);
        fieldbyname('PRTColor').Value:=trim(ADOQuery1.fieldbyname('MLColor').AsString);
        fieldbyname('PRTHX').Value:=trim(ADOQuery1.fieldbyname('HXName').AsString);
        fieldbyname('PRTOrderRollNum').Value:=Order_Sub.fieldbyname('PRTOrderRollNum').AsFloat;
        fieldbyname('PrtOrderQty').Value:=Order_Sub.fieldbyname('PRTOrderQty').AsFloat;
        fieldbyname('OrderUnit').Value:=trim(Order_Sub.fieldbyname('OrderUnit').AsString);
        FieldByName('PRTType').Value:=trim(ADOQuery1.fieldbyname('CengJi').AsString)+'层';
        fieldbyname('MainID').Value:=trim(Order_Sub.fieldbyname('MainID').AsString);
        fieldbyname('SubID').Value:=trim(Order_Sub.fieldbyname('SubID').AsString);
        post;
      end;
      ADOQuery1.Next;
    end;
  end
  else
  begin
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('update JYOrder_Sub_CF set ');
      sql.Add('PRTCode='''+trim(Order_Sub.fieldbyname('PRTCode').AsString)+'''');
      sql.Add(',PrtSpec='''+trim(Order_Sub.fieldbyname('PrtSpec').AsString)+'''');
      {if Order_Sub.fieldbyname('PRTOrderRollNum').AsFloat=0 then
      sql.Add(',PRTOrderRollNum=0')
      else
      sql.Add(',PRTOrderRollNum='''+trim(Order_Sub.fieldbyname('PRTOrderRollNum').AsString)+'''');
      if Order_Sub.fieldbyname('PrtOrderQty').AsFloat=0 then
      sql.Add(',PrtOrderQty=0')
      else
      sql.Add(',PrtOrderQty='''+trim(Order_Sub.fieldbyname('PrtOrderQty').AsString)+'''');}
      sql.Add(',OrderUnit='''+trim(Order_Sub.fieldbyname('OrderUnit').AsString)+'''');
      sql.Add(' where Subid='''+trim(Order_Sub.fieldbyname('Subid').AsString)+'''');
      ExecSQL;
    end;
  end;
end;

function TfrmMLOrderInPut_JHLR.getOrderNo(fconNo:string):string;
var
  str1:string;
  i:integer;
begin
  str1:=trim(fconNo)+'01';
  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:=trim(fconNo)+rightstr(inttostr(100+i),2);
      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 TfrmMLOrderInPut_JHLR.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('面料染色计划单',Tv1,'生产指示单管理');
end;

procedure TfrmMLOrderInPut_JHLR.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from ML_OrderSubid where 1=1 ');
    if PState=1 then
    begin
      sql.Add(' and MLID='''+Trim(FMainId)+'''');
    end;
    if PState=0 then
    begin
      sql.Add(' and 1<>1');
    end;
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from ML_OrderMain where MLID='''+Trim(FMainId)+'''');
    Open;
  end;
  SCSHDataNew(ADOQuery1,ScrollBox1,2);
  if PState=0 then
  begin
    OrdDate.DateTime:=SGetServerDateTime(ADOTemp);
    YWY.Text:=Trim(DName);
  end
  else
  begin
  end;
  if CopyInt=99 then
  begin
    PState:=0;
    FMainId:='';
    OrdDate.DateTime:=SGetServerDateTime(ADOTemp);
    YWY.Text:=Trim(DName);
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MLID').Value:='';
        FieldByName('MLSubid').Value:='';
        Post;
        Next;
      end;
    end;
  end;
end;

procedure TfrmMLOrderInPut_JHLR.FormShow(Sender: TObject);
begin
  readCxGrid('面料染色计划单',Tv1,'生产指示单管理');
  MLOrderNo.Text:='自动生成';
  InitData();
end;

function TfrmMLOrderInPut_JHLR.SaveData():Boolean;
var
  maxno,forderNo:String;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId)='' then
    begin
      if GetLSNo(ADOCmd,maxno,'ML','ML_OrderMain',3,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
      if GetLSNo(ADOCmd,forderNo,'','ML_OrderMain',3,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
     MLorderNo.Text:=forderNo;
    end
    else
    begin
      maxno:=Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from ML_OrderMain where MLID='''+Trim(FMainId)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId)='' then
      begin
        Append;
      end
      else begin
        Edit;
      end;
      FieldByName('MLID').Value:=Trim(maxno);
      SSetsaveSqlNew(ADOCmd,'ML_OrderMain',ScrollBox1,2);
      FieldByName('Status').Value:='未审核';
      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);
    ///保存子表
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('MLSubid').AsString)='' then
        begin
          if GetLSNo(ADOCmd,maxno,'MS','ML_OrderSubid',4,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(Order_Sub.fieldbyname('MLSubid').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from ML_OrderSubid where MLID='''+Trim(FMainId)+'''');
          sql.Add(' and MLSubid='''+Trim(maxno)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('MLSubid').AsString)='' then
            Append
          else
            Edit;
          FieldByName('MLID').Value:=Trim(FMainId);
          FieldByName('MLSubid').Value:=Trim(maxno);
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'ML_OrderSubid',0);
          fieldbyname('Qty').Value:=Order_Sub.FieldByName('Qty').AsFloat;
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('MLSubid').Value:=Trim(maxno);
        Order_Sub.FieldByName('MLID').Value:=Trim(FMainId);
        Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmMLOrderInPut_JHLR.TBSaveClick(Sender: TObject);
begin
  OrdDate.SetFocus;
  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空!','提示',0);
    Exit;
  end;
  {if Order_Sub.Locate('SOrddefstr7',null,[]) then
  begin
    Application.MessageBox('面料正反面不能为空!','提示',0);
    Exit;
  end; }
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    ModalResult:=1;
  end;
end;

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

procedure TfrmMLOrderInPut_JHLR.ToolButton1Click(Sender: TObject);
var
  i:Integer;
begin
  i:=Order_Sub.RecordCount;
  CopyAddRow(Tv1,Order_Sub);
  with Order_Sub do
  begin
    Edit;
    FieldByName('XHNO').Value:=IntToStr(i+1);
    Post;
  end;
end;

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

procedure TfrmMLOrderInPut_JHLR.KHNameBtnUpClick(Sender: TObject);
begin
  try
    frmKHList_CX:=TfrmKHList_CX.Create(Application);
    with frmKHList_CX do
    begin
      IF self.canshu1<>'高权限' then canshu1:='业务员';
      if ShowModal=1 then
      begin
        KHName.Text:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmKHList_CX.Free;
  end;
end;

procedure TfrmMLOrderInPut_JHLR.KHNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text:='';
  TBtnEditC(Sender).TxtCode:='';
end;

procedure TfrmMLOrderInPut_JHLR.v1OrderUnitPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
var
  mvalues:string;
begin
  mvalues:=tv1.Columns[tv1.Controller.FocusedColumn.index].DataBinding.FieldName;
  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 TfrmMLOrderInPut_JHLR.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 TfrmMLOrderInPut_JHLR.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 ShowModal=1 then
      begin
        TEdit(Sender).Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmMLOrderInPut_JHLR.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(MLConNo.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(MLConNo.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(MLConNo.Text)+'%'+'''');
        Open;
      end;
    end;
    begin
      MLConNo.Text:=Trim(ADOTemp.fieldbyname('ConNo').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);
            FieldByName('Sorddefstr10').Value:=Trim(ADOTemp.fieldbyname('SUBID').AsString);
            Post;
          end;
          Next;
        end;
      end;
    end;

  end;
end;

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

procedure TfrmMLOrderInPut_JHLR.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 TfrmMLOrderInPut_JHLR.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 TfrmMLOrderInPut_JHLR.v1Column3PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmZdyAttachCP_HX_Sel:=TfrmZdyAttachCP_HX_Sel.create(self);
  with frmZdyAttachCP_HX_Sel do
  begin
    IF showmodal=1 then
    begin
      Order_Sub.Edit;
      Order_Sub.FieldByName('PRTHX').Value:=trim(cds_Hz.FieldByName('ZdyName').asstring);
      Order_Sub.Post;
    end;
    frmZdyAttachCP_HX_Sel.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmMLOrderInPut_JHLR.MLConNoBtnUpClick(Sender: TObject);
begin
  frmContractList_CX:=TfrmContractList_CX.create(self);
  with frmContractList_CX do
  begin
    if showmodal=1 then
    begin
      self.mlconNo.Text:=Order_Main.fieldbyname('conNO').AsString;
      with Order_SubCX do
      begin
        First;
        while not Eof do
        begin
          if FieldByName('Ssel').AsBoolean=true then
          begin
            with Order_Sub do
            begin
              Append;
              FieldByName('MLCode').Value:=Trim(Order_SubCX.fieldbyname('prtCode').AsString);
              FieldByName('MLCodeName').Value:=Trim(Order_SubCX.fieldbyname('prtCodeName').AsString);
              FieldByName('MLColor').Value:=Trim(Order_SubCX.fieldbyname('PRTColor').AsString);
              FieldByName('MLColorno').Value:=Trim(Order_SubCX.fieldbyname('SOrdDefStr1').AsString);
              FieldByName('Qty').Value:=Order_SubCX.fieldbyname('PRTOrderQty').AsFloat;
              FieldByName('QtyUnit').Value:=Trim(Order_SubCX.fieldbyname('OrderUnit').AsString);
              Post;
            end;
          end;
          Next;
        end;
      end;
    end;
    Free;
  end;
end;

procedure TfrmMLOrderInPut_JHLR.CYIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select B.* ');
      sql.Add('from CP_YDang B where B.CYID='''+trim(CYID.Text)+''' ');
      open;
    end;
    IF not ADOQueryTmp.IsEmpty then
    begin
       Order_Sub.append;
       Order_Sub.FieldByName('MLCode').Value:=trim(ADOQueryTmp.fieldbyname('CYNo').AsString);
       Order_Sub.FieldByName('XTNo').Value:=trim(ADOQueryTmp.fieldbyname('XTNo').AsString);
       Order_Sub.FieldByName('MLCodeName').Value:=trim(ADOQueryTmp.fieldbyname('CYEName').AsString);
       Order_Sub.FieldByName('CYID').Value:=trim(ADOQueryTmp.fieldbyname('CYID').AsString);
       Order_Sub.FieldByName('MLColor').Value:=trim(ADOQueryTmp.fieldbyname('ColorName').AsString);
       Order_Sub.FieldByName('MLColorNO').Value:=trim(ADOQueryTmp.fieldbyname('ColorNO').AsString);
       Order_Sub.Post;
       CYID.Text:='';
    end
    else
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select A.*,B.ColorNo,B.ColorName,B.ColorEName');
        sql.Add(',PRTHX=(select Top 1 C.HXName from CP_YDang_JG C where C.CYID=B.CYID and C.IFZY=1 and isnull(C.HXName,'''')<>''''');
        SQL.Add(' order by (case when C.cengji=6 then 1 else 2 end))');
        SQL.Add(' from CP_YDang_Color B inner join CP_YDang A on A.CYID=B.CYID');
        sql.Add(' where B.CLRID='''+trim(CYID.Text)+''' ');
        open;
      end;
      IF not ADOQueryCmd.IsEmpty then
      begin
        Order_Sub.append;
        Order_Sub.FieldByName('MLCode').Value:=trim(ADOQueryTmp.fieldbyname('CYNo').AsString);
        Order_Sub.FieldByName('XTNo').Value:=trim(ADOQueryTmp.fieldbyname('XTNo').AsString);
        Order_Sub.FieldByName('MLCodeName').Value:=trim(ADOQueryTmp.fieldbyname('CYEName').AsString);
        Order_Sub.FieldByName('CYID').Value:=trim(ADOQueryTmp.fieldbyname('CYID').AsString);
        Order_Sub.FieldByName('MLColor').Value:=trim(ADOQueryTmp.fieldbyname('ColorName').AsString);
        Order_Sub.FieldByName('MLColorNO').Value:=trim(ADOQueryTmp.fieldbyname('ColorNO').AsString);
        Order_Sub.Post;
        CYID.Text:='';
      end
      else
      application.MessageBox('此条码错误！','提示信息',MB_ICONERROR);
      CYID.Text:='';
    end;
  end;
end;

procedure TfrmMLOrderInPut_JHLR.v1PRTCodePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmCPManageSel:=TfrmCPManageSel.Create(self);
  with frmCPManageSel do
  begin
    if showmodal=1 then
    begin
      Order_Sub.Edit;
      Order_Sub.fieldbyname('MLCode').Value:=CDS_Main.fieldbyname('CYNo').asstring;
      Order_Sub.fieldbyname('MLCodeName').Value:=CDS_Main.fieldbyname('CYEName').asstring;
      Order_Sub.fieldbyname('MLColor').Value:=CDS_Main.fieldbyname('CYColor').asstring;
      Order_Sub.fieldbyname('MLColorNo').Value:=CDS_Main.fieldbyname('CYColorNo').asstring;
      Order_Sub.fieldbyname('CYID').Value:=CDS_Main.fieldbyname('CYID').asstring;
      Order_Sub.Post;
    end;
    free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmMLOrderInPut_JHLR.MLCodeNameMBtnUpClick(Sender: TObject);
begin
  frmCPManageSel:=TfrmCPManageSel.Create(self);
  with frmCPManageSel do
  begin
    if showmodal=1 then
    begin
      MLCodeM.Text:=CDS_Main.fieldbyname('CYNo').asstring;
      MLCodeNameM.Text:=CDS_Main.fieldbyname('CYEName').asstring;
    end;
    free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmMLOrderInPut_JHLR.ToolButton4Click(Sender: TObject);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      KHType.Text:='染厂';
      KHType.TxtCode:='染厂';
      if ShowModal=1 then
      begin
        TBFind.Click;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

end.
