unit U_OrderInPutJZC;

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;

type
  TfrmOrderInPutJZC = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label3: TLabel;
    OrderNo: TEdit;
    OrdDate: TDateTimePicker;
    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;
    Label10: TLabel;
    ConNo: TEdit;
    Label14: TLabel;
    CustomerNoName: TBtnEditC;
    v1XHNo: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1SOrddefstr1: TcxGridDBColumn;
    v1PriceUnit: TcxGridDBColumn;
    v1SOrddefstr2: TcxGridDBColumn;
    Label2: TLabel;
    MPRTCodeName: TBtnEditC;
    Label6: TLabel;
    MPRTSpec: TBtnEditC;
    Label12: TLabel;
    MPRTMF: TBtnEditC;
    Label13: TLabel;
    MPRTKZ: TBtnEditC;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    ToolButton3: TToolButton;
    v1Column4: TcxGridDBColumn;
    Label4: TLabel;
    Label5: TLabel;
    BtnEditC1: TBtnEditC;
    Label7: TLabel;
    BtnEditC2: TBtnEditC;
    Label9: TLabel;
    BtnEditC3: TBtnEditC;
    Label11: TLabel;
    BtnEditC4: TBtnEditC;
    cxGrid2: TcxGrid;
    cxGridLevel1: TcxGridLevel;
    Tv2: TcxGridDBTableView;
    Tv2Column1: TcxGridDBColumn;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    cxGrid4: TcxGrid;
    Tv4: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    cxGrid5: TcxGrid;
    Tv5: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    Label8: 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 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);
  private
    procedure InitData();
    procedure ZDYHelp(FButn:TcxButtonEdit;LType:string);
    function  SaveData():Boolean;
    function  SaveDataSubOne():Boolean;
    function  SaveDataMain():Boolean;
    function SaveDataSubMore():Boolean;
    { Private declarations }
  public
    PState,CopyInt,PriceFlag:Integer;
    FMainId,FFMainId,FOrderNo:String;
    FXS:Integer;
    { Public declarations }
  end;

var
  frmOrderInPutJZC: TfrmOrderInPutJZC;
  newh:hwnd;
implementation
uses
  U_DataLink,U_ZDYHelp,U_Fun,U_ZDYHelpSel;

{$R *.dfm}

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

procedure TfrmOrderInPutJZC.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from JYOrder_Sub ');
    if PState=1 then
    begin
      sql.Add('where MainId='''+Trim(FMainId)+'''');
    end;
    if PState=0 then
    begin
      sql.Add(' where 1<>1');
    end;
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main where MainId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCSHDataNew(ADOQuery1,ScrollBox1,2);
  if PState=0 then
  begin
    OrdDate.DateTime:=SGetServerDateTime(ADOTemp);
    DlyDate.DateTime:=SGetServerDateTime(ADOTemp);
    OrdPerson1.Text:=Trim(DName);
  end else
  begin
  end;
  if CopyInt=99 then
  begin
    PState:=0;
    FMainId:='';
    OrdPerson1.Text:=Trim(DName);
    OrderNo.Text:='';
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value:='';
        FieldByName('SubId').Value:='';
        Post;
        Next;
      end;
    end;
  end;
end;

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

procedure TfrmOrderInPutJZC.FormShow(Sender: TObject);
begin
  readCxGrid('指示单录入',Tv1,'生产指示单管理');
  if PriceFlag=99 then
  begin
    v1PRTPrice.Visible:=False;
    v1PRTPrice.Hidden:=True;
  end;
  InitData();
end;

function TfrmOrderInPutJZC.SaveData():Boolean;
var
  maxno:String;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId)='' then
    begin
      if GetLSNo(ADOCmd,maxno,'JM','JYOrder_Main',3,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(FMainId);
    end;
    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 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;
          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 TfrmOrderInPutJZC.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 TfrmOrderInPutJZC.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 TfrmOrderInPutJZC.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 TfrmOrderInPutJZC.TBSaveClick(Sender: TObject);
begin
  OrdDate.SetFocus;
  if Trim(OrderNo.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('SLbName',null,[])=True 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 SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
  end;
end;

procedure TfrmOrderInPutJZC.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 TfrmOrderInPutJZC.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 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 TfrmOrderInPutJZC.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 TfrmOrderInPutJZC.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 TfrmOrderInPutJZC.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;
end;

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

procedure TfrmOrderInPutJZC.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 TfrmOrderInPutJZC.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 TfrmOrderInPutJZC.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 TfrmOrderInPutJZC.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 TfrmOrderInPutJZC.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 TfrmOrderInPutJZC.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 TfrmOrderInPutJZC.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 TfrmOrderInPutJZC.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 TfrmOrderInPutJZC.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 TfrmOrderInPutJZC.ToolButton3Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  OneKeyPost(Tv1,Order_Sub);
end;

end.
