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, cxDropDownEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxSkinsCore,
  dxSkinsDefaultPainters, dxSkinscxPCPainter, 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, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue;

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;
    JGFactory: TBtnEditC;
    v1OrderUnit: TcxGridDBColumn;
    v1SOrddefstr1: TcxGridDBColumn;
    Label3: TLabel;
    Label14: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    OrdDate: TDateTimePicker;
    CustomerNoName: TBtnEditC;
    MPRTMF: TBtnEditC;
    MPRTKZ: TBtnEditC;
    Label2: TLabel;
    MPRTCodeName: TBtnEditC;
    Label6: TLabel;
    MPRTSpec: TBtnEditC;
    Label5: TLabel;
    MPRTDuiSeGY: TBtnEditC;
    Label11: TLabel;
    SXSL: TBtnEditC;
    Label9: TLabel;
    WeiXie: TBtnEditC;
    Label8: TLabel;
    MPRTPH: TBtnEditC;
    Label19: TLabel;
    Label25: TLabel;
    DLYDate: TDateTimePicker;
    Label26: TLabel;
    MPRTCode: TBtnEditC;
    Label27: TLabel;
    MPRTQtyNote: TBtnEditC;
    Label28: TLabel;
    Label29: TLabel;
    Label31: TLabel;
    SLDZaoXi: TBtnEditC;
    SLDShiMo: TBtnEditC;
    SLDGanMo: TBtnEditC;
    Label32: TLabel;
    Label33: TLabel;
    MPRTKQiQiu: TBtnEditC;
    shougan: TBtnEditC;
    Label41: TLabel;
    JGGDY: TBtnEditC;
    Label43: TLabel;
    MPRTBZNote: TMemo;
    v1Column1: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Note: TMemo;
    Label23: TLabel;
    CDS_CON: TClientDataSet;
    DSCON: TDataSource;
    v1Column8: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Label51: TLabel;
    Label53: TLabel;
    Label59: TLabel;
    Label60: TLabel;
    JGType: TBtnEditC;
    YTSL: TBtnEditC;
    DSSC: TBtnEditC;
    JGJQ: TDateTimePicker;
    Label65: TLabel;
    Label66: TLabel;
    Label67: TLabel;
    ZJGFactory: TBtnEditC;
    ZJGType: TBtnEditC;
    ZJGJQ: TDateTimePicker;
    Label69: TLabel;
    ZJGGDY: TBtnEditC;
    YWY: TEdit;
    ConNo: TBtnEditC;
    v1Column2: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    V2Column8: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolBar3: TToolBar;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    CDS_Fact: TClientDataSet;
    DSFact: TDataSource;
    v1Column3: TcxGridDBColumn;
    Label4: TLabel;
    LIDAN: TBtnEditC;
    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 JGFactoryBtnUpClick(Sender: TObject);
    procedure CustomerNoNameBtnUpClick(Sender: TObject);
    procedure CustomerNoNameBtnDnClick(Sender: TObject);
    procedure MPRTCodeNameBtnUpClick(Sender: TObject);
    procedure NoteDblClick(Sender: TObject);
    procedure v1PriceUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure OrdDefStr2BtnUpClick(Sender: TObject);
    procedure MPRTSCTeBieNoteBtnUpClick(Sender: TObject);
    procedure MPRTBZNoteDblClick(Sender: TObject);
    procedure v1Column8PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure FormCreate(Sender: TObject);
    procedure v1Column7PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column14PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure SLDZaoXiKeyPress(Sender: TObject; var Key: Char);
    procedure ZJGFactoryBtnUpClick(Sender: TObject);
    procedure ConNoBtnUpClick(Sender: TObject);
    procedure ConNoBtnDnClick(Sender: TObject);
    procedure cxGridDBColumn3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure cxGridDBColumn5PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure cxGridDBColumn2PropertiesButtonClick(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;
    { Private declarations }
  public
    PState,CopyInt,PriceFlag:Integer;
    FMainId,FFMainId,FOrderNo:String;
    FXS:Integer;
    canshu1:string;
    fFlileFlag:string;
    { Public declarations }
  end;

var
  frmOrderInPut: TfrmOrderInPut;
  newh:hwnd;
implementation
uses
  U_DataLink,U_ZDYHelp,U_Fun,U_ZDYHelpSel, U_ZdyAttachment, U_ZdyAttachGYS,
  getpic,U_CPManageSel,U_ContractListSel;                    

{$R *.dfm}

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

procedure TfrmOrderInPut.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main where MainId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCSHDataNew(ADOQuery1,ScrollBox1,2);
  
   with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from JYOrder_Fact 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;
    Open;
  end;
  SCreateCDS20(ADOQuery1,CDS_Fact);
  SInitCDSData20(ADOQuery1,CDS_Fact);
  
  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;
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);

  
  if PState=0 then
  begin
    OrdDate.DateTime:=SGetServerDateTime(ADOTemp);
    DlyDate.DateTime:=OrdDate.DateTime;
    JGJQ.DateTime:=OrdDate.DateTime;
    ZJGJQ.DateTime:=OrdDate.DateTime;
    ZJGJQ.Checked:=False;
    JGJQ.Checked:=False;
    DLYDate.Checked:=False;
  end;
  if PState=1 then
  begin
    ConNo.Enabled:=false;
    OrderNo.Enabled:=false;
    MPRTCode.Enabled:=false;
  end;
  if CopyInt=99 then
  begin
    PState:=0;
    FMainId:='';
    OrderNo.Text:='';
    OrderNo.Enabled:=true;
    OrdDate.DateTime:=SGetServerDateTime(ADOTemp);
    DlyDate.DateTime:=OrdDate.DateTime;
    JGJQ.DateTime:=OrdDate.DateTime;
    ZJGJQ.DateTime:=OrdDate.DateTime;
    JGJQ.Checked:=False;
    ZJGJQ.Checked:=False;
    DLYDate.Checked:=False;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value:='';
        FieldByName('SubId').Value:='';
        FieldByName('hxFile').Value:='';
        Post;
        Next;
      end;
    end;
  end;
end;

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

procedure TfrmOrderInPut.FormShow(Sender: TObject);
begin
  readCxGrid(trim(self.Caption),Tv1,'生产指示单管理');

  fFlileFlag:=UserDataFlag+'HX';
  InitData();

end;

function TfrmOrderInPut.SaveData():Boolean;
var
  maxno,fmxType,forderno:String;
  LiuShui:Integer;
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;
//      if Trim(OrderNo.Text)='' then
//      begin
//        with ADOTemp do
//        begin
//          Close;
//          SQL.Clear;
//          SQL.Add('select isnull(MAX(OrderNo),''第一个'')  OrderNo from JYOrder_Main where ConNo='+quotedstr(trim(ConNo.Text)));
//          Open;
//        end;
//        if Trim(ADOTemp.FieldByName('OrderNo').AsString)='第一个' then
//        begin
//          forderno:= trim(ConNo.Text)+'-01';
//        end
//        else
//        begin
//          LiuShui:= StrToIntDef(RightStr(trim(ADOTemp.FieldByName('OrderNo').AsString),2),1)+1;
//          if   LiuShui<10 then
//          begin
//           forderno:= trim(ConNo.Text)+'-0'+inttostr(LiuShui);
//          end
//          else
//          begin
//           forderno:= trim(ConNo.Text)+'-'+inttostr(LiuShui);
//          end;
//        end;
//        OrderNo.Text:=forderno;
//      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;
      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);
           
          Post;
        end;

        Order_Sub.Edit;
        Order_Sub.FieldByName('SubId').Value:=Trim(maxno);
       
        Next;
      end;
    end;
     ///保存加工厂
    with CDS_Fact do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_Fact.fieldbyname('FactId').AsString)='' then
        begin
          if GetLSNo(ADOCmd,maxno,'JG','JYOrder_Fact',4,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end
        else
        begin
          maxno:=Trim(CDS_Fact.fieldbyname('FactId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_Fact where MainId='''+Trim(FMainId)+'''');
          sql.Add(' and FactId='''+Trim(maxno)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(CDS_Fact.fieldbyname('FactId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('MainId').Value:=Trim(FMainId);
          FieldByName('FactId').Value:=Trim(maxno);
          RTSetSaveDataCDS(ADOCmd,Tv2,CDS_Fact,'JYOrder_Fact',0);
          Post;
        end;

        CDS_Fact.Edit;
        CDS_Fact.FieldByName('FactId').Value:=Trim(maxno);
       
        Next;
      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(ConNo.Text)='' then
  begin
    Application.MessageBox('合同号不能为空！','提示',0);
    Exit;
  end;

  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空!','提示',0);
    Exit;
  end;


  if DLYDate.Checked=false then
  begin
    Application.MessageBox('交货日期不能为空!','提示',0);
    Exit;
  end;
//  if Trim(OrdPerson1.Text)='' then
//  begin
//    Application.MessageBox('跟单员不能为空！','提示',0);
//    Exit;
//  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;
  FConMainId:string;
begin
  if Trim(conno.Text)='' then
  begin
    Application.MessageBox('合同号不能为空!','提示',0);
    Exit;
  end;
  try
    frmContractListSel:=TfrmContractListSel.Create(Application);
    with frmContractListSel do
    begin
      FConNo:=trim(self.ConNo.Text);
      if ShowModal=1 then
      begin
        Order_Main.locate('SSel',true,[]);
        while Order_Main.locate('SSel',true,[]) do
        begin
          if  self.Order_Sub.locate('ConSubId',trim(Order_Main.Fieldbyname('SubId').AsString),[]) then
          begin
            Order_Main.Edit;
            Order_Main.fieldbyname('SSel').value:=false;
            continue;
          end;
          with self.Order_Sub do
          begin
            append;
            Fieldbyname('PrtKuanNO').value:=trim(frmContractListSel.Order_Main.Fieldbyname('PrtKuanNO').AsString);
//            Fieldbyname('PrtColorNo').value:=trim(frmContractListSel.Order_Main.Fieldbyname('PrtColorNo').AsString);
            Fieldbyname('PrtColor').value:=trim(frmContractListSel.Order_Main.Fieldbyname('PrtColor').AsString);
            Fieldbyname('PRTHX').value:=trim(frmContractListSel.Order_Main.Fieldbyname('PRTHX').AsString);
            Fieldbyname('OrderQty').value:=frmContractListSel.Order_Main.Fieldbyname('PRTOrderQty').AsFloat;
            Fieldbyname('OrderUnit').value:=trim(frmContractListSel.Order_Main.Fieldbyname('OrderUnit').AsString);
            Fieldbyname('ConSubId').value:=trim(frmContractListSel.Order_Main.Fieldbyname('SubId').AsString);
          end;
          Order_Main.Edit;
          Order_Main.fieldbyname('SSel').value:=false;
        end;
      end;
    end;
  finally
    frmContractListSel.Free;
  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 WFB_MJJY 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;
end;

procedure TfrmOrderInPut.JGFactoryBtnUpClick(Sender: TObject);
begin
  try
    frmZdyAttachGYS:=TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
//      DEFstr5.ItemIndex:=DEFstr5.Items.IndexOf('加工厂');
//      DEFstr5.Enabled:=false;
      if ShowModal=1 then
      begin
        JGFactory.Text:=Trim(CDS_HZ.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmOrderInPut.CustomerNoNameBtnUpClick(Sender: TObject);
begin
  try
    frmZdyAttachment:=TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if ShowModal=1 then
      begin
        CustomerNoName.Text:=Trim(CDS_HZ.fieldbyname('CoName').AsString);
        CustomerNoName.TxtCode:=Trim(CDS_HZ.fieldbyname('CoCode').AsString);
        YWY.Text:=Trim(CDS_HZ.fieldbyname('Salesman').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.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.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.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
        TMemo(Sender).Lines.Clear;
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if FieldByName('SSel').AsBoolean=True then
            begin
              i:=i+1;
              TMemo(Sender).Lines.Add(inttostr(i)+'.'+FieldByName('ZDYName').AsString)
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderInPut.v1Column8PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  FormGetPic:=TFormGetPic.create(self);
  with FormGetPic do
  begin
    fFlileFlag:=self.fFlileFlag;
    fkeyNo:=Order_Sub.fieldbyname('HXFile').asstring;
    pat1:='FileName';
    pic1:='FilesOther';
    FTFType:='HX';
    if showmodal=1 then
    begin
      Order_Sub.edit;
      Order_Sub.fieldbyname('HXFile').Value:=trim(fkeyNo);
    end;
    free;
  end;
end;

procedure TfrmOrderInPut.FormCreate(Sender: TObject);
begin
  canshu1:=trim(DParameters1);
end;

procedure TfrmOrderInPut.v1Column7PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZdyAttachGYS:=TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      DEFstr5.ItemIndex:=DEFstr5.Items.IndexOf('坯布厂');
//      DEFstr5.Enabled:=false;
      if ShowModal=1 then
      begin
        with Order_Sub do
        begin
          Edit;
          FieldByName('PBGYS').Value:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
//          Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmOrderInPut.v1Column14PropertiesButtonClick(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:='PBUnit';
      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.SLDZaoXiKeyPress(Sender: TObject;
  var Key: Char);
begin
	if Key = #13 then
	begin
		Key := #0;
		SelectNext(ActiveControl as TWinControl, True, True);
	end;
end;

procedure TfrmOrderInPut.ZJGFactoryBtnUpClick(Sender: TObject);
begin
  try
    frmZdyAttachGYS:=TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
//      DEFstr5.ItemIndex:=DEFstr5.Items.IndexOf('加工厂');
//      DEFstr5.Enabled:=false;
      if ShowModal=1 then
      begin
        ZJGFactory.Text:=Trim(CDS_HZ.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmOrderInPut.ConNoBtnUpClick(Sender: TObject);
var
  FConMainId,FMPRTCode:string;
begin
  try
    frmContractListSel:=TfrmContractListSel.Create(Application);
    with frmContractListSel do
    begin
      FConNo:=trim(self.ConNo.Text);
      if ShowModal=1 then
      begin
        Order_Main.locate('SSel',true,[]);
        FConMainId:=Trim(frmContractListSel.Order_Main.fieldbyname('MainId').AsString);
        self.YWY.Text:=Trim(frmContractListSel.Order_Main.fieldbyname('ywy').AsString);
        FMPRTCode:=Trim(frmContractListSel.Order_Main.fieldbyname('PRTCode').AsString);
        self.ConNo.Text:=Trim(frmContractListSel.Order_Main.fieldbyname('ConNo').AsString);
         self.OrderNo.Text:=Trim(frmContractListSel.Order_Main.fieldbyname('ConNo').AsString);
        self.CustomerNoName.Text:=Trim(frmContractListSel.Order_Main.fieldbyname('CustomerNoName').AsString);
        self.CustomerNoName.TxtCode:=Trim(frmContractListSel.Order_Main.fieldbyname('CustomerNo').AsString);
        self.MPRTCode.Text:=Trim(frmContractListSel.Order_Main.fieldbyname('PRTCode').AsString);
        self.MPRTCodeName.Text:=Trim(frmContractListSel.Order_Main.fieldbyname('PRTCodeName').AsString);
        self.MPRTSpec.Text:=Trim(frmContractListSel.Order_Main.fieldbyname('PRTSpec').AsString);
        self.MPRTMF.Text:=Trim(frmContractListSel.Order_Main.fieldbyname('PRTMF').AsString);
        self.MPRTKZ.Text:=Trim(frmContractListSel.Order_Main.fieldbyname('PRTKZ').AsString);
        while Order_Main.locate('SSel',true,[]) do
        begin
          if FConMainId<>trim(frmContractListSel.Order_Main.Fieldbyname('MainId').AsString) then
          begin
            Application.MessageBox('不能选择多个合同！','提示',0);
            break;
          end;
          if FMPRTCode<>trim(frmContractListSel.Order_Main.Fieldbyname('PRTCode').AsString) then
          begin
            Application.MessageBox('不能选择多个产品！','提示',0);
            break;
          end;
          if  self.Order_Sub.locate('ConSubId',trim(Order_Main.Fieldbyname('SubId').AsString),[]) then
          begin
            Order_Main.Edit;
            Order_Main.fieldbyname('SSel').value:=false;
            continue;
          end;
          with self.Order_Sub do
          begin
            append;
            Fieldbyname('PrtKuanNO').value:=trim(frmContractListSel.Order_Main.Fieldbyname('PrtKuanNO').AsString);
//            Fieldbyname('PrtColorNo').value:=trim(frmContractListSel.Order_Main.Fieldbyname('PrtColorNo').AsString);
            Fieldbyname('PrtColor').value:=trim(frmContractListSel.Order_Main.Fieldbyname('PrtColor').AsString);
            Fieldbyname('PRTHX').value:=trim(frmContractListSel.Order_Main.Fieldbyname('PRTHX').AsString);
            Fieldbyname('OrderQty').value:=frmContractListSel.Order_Main.Fieldbyname('PRTOrderQty').AsFloat;
            Fieldbyname('OrderUnit').value:=trim(frmContractListSel.Order_Main.Fieldbyname('OrderUnit').AsString);
            Fieldbyname('ConSubId').value:=trim(frmContractListSel.Order_Main.Fieldbyname('SubId').AsString);
             Fieldbyname('MXNote').value:=trim(frmContractListSel.Order_Main.Fieldbyname('MXNote').AsString);
          end;
          Order_Main.Edit;
          Order_Main.fieldbyname('SSel').value:=false;
        end;
//        YWY.Text:=Trim(CDS_HZ.fieldbyname('Salesman').AsString);
      end;
    end;
  finally
    frmContractListSel.Free;
  end;
end;

procedure TfrmOrderInPut.ConNoBtnDnClick(Sender: TObject);
begin
  if Order_Sub.IsEmpty=false then
  begin
    Application.MessageBox('请先删除明细数据','提示',0);
    Exit;
  end;
  TBtnEditC(Sender).Text:='';
  TBtnEditC(Sender).TxtCode:='';
end;

procedure TfrmOrderInPut.cxGridDBColumn3PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachGYS:=TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal=1 then
      begin
        CDS_Fact.Edit;
        CDS_Fact.FieldByName('SJGFact').Value:= Trim(CDS_HZ.fieldbyname('CoHRName').AsString);
        CDS_Fact.FieldByName('SJGFactory').Value:= Trim(CDS_HZ.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmOrderInPut.ToolButton3Click(Sender: TObject);
var
  i:Integer;
begin
//  if Trim(conno.Text)='' then
//  begin
//    Application.MessageBox('合同号不能为空!','提示',0);
//    Exit;
//  end;
  i:=CDS_Fact.RecordCount;
  i:=i+1;
//  CopyAddRow(Tv1,CDS_Fact);

  with CDS_Fact do
  begin
    append;
    FieldByName('SJGXH').Value:=i;
    Post;
  end;
end;

procedure TfrmOrderInPut.ToolButton4Click(Sender: TObject);
begin
if CDS_Fact.IsEmpty then Exit;

//  with ADOTemp do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('select * from WFB_MJJY where SubId='''+Trim(Order_Sub.fieldbyname('SubId').AsString)+'''');
//    Open;
//  end;
//  if ADOTemp.IsEmpty=False then
//  begin
//    Application.MessageBox('已检验不能删除数据!','提示',0);
//    Exit;
//  end;
  if Trim(CDS_Fact.fieldbyname('FactId').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Fact where FactId='''+Trim(CDS_Fact.fieldbyname('FactId').AsString)+'''');
      ExecSQL;
    end;
  end;
  CDS_Fact.Delete;
end;

procedure TfrmOrderInPut.cxGridDBColumn5PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
 try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='GDY';
      flagname:='跟单员';
      if ShowModal=1 then
      begin
        with CDS_Fact do
        begin
          edit;
          fieldbyname('SGDY').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.cxGridDBColumn2PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='SJGType';
      flagname:='加工类型';
      if ShowModal=1 then
      begin
        Self.CDS_Fact.Edit;
        Self.CDS_Fact.FieldByName('SJGType').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
