unit U_OrderInPut_CY;

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

type
  TfrmOrderInPut_CY = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    ToolButton3: TToolButton;
    cxTabControl2: TcxTabControl;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1XHNo: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxTabControl1: TcxTabControl;
    v1Column1: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N3: TMenuItem;
    CDS_CG: TClientDataSet;
    DataSource2: TDataSource;
    PopupMenu2: TPopupMenu;
    N4: TMenuItem;
    N5: TMenuItem;
    N6: TMenuItem;
    N7: TMenuItem;
    N8: TMenuItem;
    N9: TMenuItem;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Panel1: TPanel;
    Label1: TLabel;
    Label3: TLabel;
    Label39: TLabel;
    Label25: TLabel;
    Label10: TLabel;
    Label43: TLabel;
    Label14: TLabel;
    Label19: TLabel;
    Label42: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    CYNO: TEdit;
    OrdDate: TDateTimePicker;
    OrdDefDate1: TDateTimePicker;
    DLYDate: TDateTimePicker;
    OrdPerson2: TBtnEditC;
    CustomerNoName: TBtnEditC;
    MPRTBZNote: TMemo;
    MPRTSCTeBieNote: TMemo;
    conNO: TEdit;
    KHConNO: TEdit;
    orderNo: TBtnEditA;
    Label5: TLabel;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1SordQty4: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    OrdPerson1: TBtnEditC;
    LBNameNote: TMemo;
    Label36: TLabel;
    MaiTouNote: TMemo;
    Label37: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    orddefstr1: TEdit;
    orddefstr2: TEdit;
    orddefstr3: TEdit;
    Label8: TLabel;
    conDefstr1: TEdit;
    Label13: TLabel;
    Label9: TLabel;
    Label35: TLabel;
    Label11: TLabel;
    conDefstr2: TEdit;
    ConPerson1: TEdit;
    ConPerson2: TEdit;
    v1Column2: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label12: TLabel;
    Label15: TLabel;
    orddefstr5: TEdit;
    orddefstr4: TEdit;
    orddefstr6: TEdit;
    Label16: TLabel;
    orddefstr7: TEdit;
    Label17: TLabel;
    OrdDefDate2: TDateTimePicker;
    Label18: TLabel;
    orddefstr10: TEdit;
    Label20: 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 v1OrderUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1PriceUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure YCLFactoryBtnUpClick(Sender: TObject);
    procedure MPRTSCTeBieNoteBtnUpClick(Sender: TObject);
    procedure MPRTBZNoteDblClick(Sender: TObject);
    procedure MPRTSCTeBieNoteDblClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure conNoChange(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure orderNoBtnClick(Sender: TObject);
    procedure OrdPerson1BtnUpClick(Sender: TObject);
  private
    procedure InitData();
    procedure ZDYHelp(FButn:TcxButtonEdit;LType:string);
    function  SaveData():Boolean;
    function YFData():Boolean;
    { Private declarations }
  public
    PState,CopyInt,PriceFlag:Integer;
    FMainId,FFMainId,FOrderNo:String;
    FXS:Integer;
    { Public declarations }
  end;

var
  frmOrderInPut_CY: TfrmOrderInPut_CY;
  newh:hwnd;
implementation
uses
  U_DataLink,U_ZDYHelp,U_Fun,U_ZDYHelpSel, U_ProductOrderListSel,
  U_ZdyAttachment;

{$R *.dfm}

function TfrmOrderInPut_CY.YFData():Boolean;
var
  CRID,OrdMainId,YFID,FComTaiTou,FCRID,FFactoryName:String;
begin
  Result:=False;
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(CDS_CG.fieldbyname('custName').AsString)+'''');
    Open;
  end;
  if ADOTemp.IsEmpty=False then
  begin
    CRID:=ADOTemp.fieldbyname('CRID').AsString;
  end else
  begin
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CRID set CRID=CRID+1');
      sql.Add('select * from YF_Money_CRID ');
      Open;
    end;
    CRID:=ADOCmd.fieldbyname('CRID').AsString;
    with ADOCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_KC where 1<>1');
      Open;
    end;
    with ADOCmd do
    begin
      Append;
      FieldByName('CRID').Value:=StrToInt(CRID);
      FieldByName('FactoryName').Value:=Trim(CDS_CG.fieldbyname('custName').AsString);
      FieldByName('ZdyStr1').Value:='应付付';
      Post;
    end;
  end;


      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR where FactoryName='''+Trim(CDS_CG.fieldbyname('custName').AsString)+'''');
        sql.Add(' and mainID='''+Trim(CDS_CG.fieldbyname('CRNO').AsString)+'''');
      //  sql.Add(' and subID='''+Trim(ADOQuerySub.fieldbyname('subID').AsString)+'''');
        Open;
      end;
      if ADOTemp.IsEmpty then
      begin
      if GetLSNo(ADOCmd,YFID,'PF','YF_Money_CR',3,1)=False then
      begin
        Application.MessageBox('取坯布应付最大号失败!','提示',0);
        Exit;
      end;
      end
      else
      begin
        YFID:=Trim(ADOTemp.fieldbyname('YFID').AsString);
      end;
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR where YFID='''+Trim(YFID)+'''');
        Open;
      end;
      with ADOCmd do
      begin
        if ADOTemp.IsEmpty then
          Append
        else
        Edit;
        FieldByName('YFID').Value:=Trim(YFID);
        FieldByName('YFTypeId').Value:=Trim(CDS_CG.fieldbyname('mainID').AsString);
        FieldByName('CRID').Value:=StrToInt(CRID);
        FieldByName('Filler').Value:=Trim(DName);
        FieldByName('CRType').Value:='应付款登记';
        FieldByName('CRFlag').Value:='应付付';
        FieldByName('QtyFlag').Value:=1;
        FieldByName('FactoryName').Value:=Trim(CDS_CG.fieldbyname('custName').AsString);
        FieldByName('CRTime').Value:=CDS_CG.fieldbyname('CRTime').AsDateTime;
        FieldByName('Qty').Value:=CDS_CG.fieldbyname('Qty').AsFloat;
        FieldByName('PS').Value:=0;
        FieldByName('YFType').Value:='自动生成';
        FieldByName('Price').Value:=CDS_CG.fieldbyname('Price').AsFloat;
        FieldByName('money').Value:=CDS_CG.fieldbyname('money').AsFloat;
        FieldByName('BBmoney').Value:=CDS_CG.fieldbyname('money').AsFloat;
        FieldByName('HuiLv').Value:=1;
        FieldByName('BZType').Value:='RMB';
        FieldByName('ComTaiTou').Value:=Trim(CDS_CG.fieldbyname('custName').AsString);
        FieldByName('QtyUnit').Value:=Trim(CDS_CG.fieldbyname('QtyUnit').AsString);
        FieldByName('YFName').Value:='采购金额';
        FieldByName('MainId').Value:=Trim(CDS_CG.fieldbyname('CRNO').AsString);
        FieldByName('subId').Value:=Trim(CDS_CG.fieldbyname('CRID').AsString);
        Post;
      end;


    with ADOCmd do
    begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(' where CRID='+CRID);
    ExecSQL;
    end;
  Result:=True;
end;

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

procedure TfrmOrderInPut_CY.InitData();
begin


  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from JYOrderCY_Sub where 1=1 ');
    //sql.Add('exec  P_View_OrderSub :begdate,:enddate,:WSQl');
    //Parameters.ParamByName('begdate').Value:='2010-10-10';
   // Parameters.ParamByName('enddate').Value:='2050-12-24';
    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 JYOrderCY_Main where MainId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCSHDataNew(ADOQuery1,Panel1,2);
  if PState=0 then
  begin

  end else
  begin
  end;
  if CopyInt=99 then
  begin
    PState:=0;
    FMainId:='';
    //OrdPerson1.Text:=Trim(DName);
    CYNO.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 TfrmOrderInPut_CY.ZDYHelp(FButn:TcxButtonEdit;LType:string);
var
  FType,ZDYName,FText:String;
begin
end;

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

  InitData();
end;

function TfrmOrderInPut_CY.SaveData():Boolean;
var
  maxno:String;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId)='' then
    begin
      if GetLSNo(ADOCmd,maxno,'CY','JYOrderCY_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 JYOrderCY_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,'JYOrderCY_Main',Panel1,2);
      if Trim(FMainId)='' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
        FieldByName('status').Value:='0';
      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 JYOrderCY_Main where CYNO='''+Trim(CYNO.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,'CS','JYOrderCY_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 JYOrderCY_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,'JYOrderCY_Sub',0);
          FieldByName('orderNO').Value:=Trim(orderNO.Text);

          fieldbyname('PRTOrderQty').Value:=Order_Sub.fieldbyname('PRTOrderQty').AsFloat;
          fieldbyname('PRTPrice').Value:=Order_Sub.fieldbyname('PRTPrice').AsFloat;
          fieldbyname('PRTmoney').Value:=Order_Sub.fieldbyname('PRTmoney').AsFloat;
          fieldbyname('SordQty1').Value:=Order_Sub.fieldbyname('SordQty1').AsFloat;
          fieldbyname('SordQty2').Value:=Order_Sub.fieldbyname('SordQty2').AsFloat;
          fieldbyname('SordQty3').Value:=Order_Sub.fieldbyname('SordQty3').AsFloat;
          fieldbyname('SordQty4').Value:=Order_Sub.fieldbyname('SordQty4').AsFloat;
          fieldbyname('SordQty5').Value:=Order_Sub.fieldbyname('SordQty5').AsFloat;
          fieldbyname('SordQty6').Value:=Order_Sub.fieldbyname('SordQty6').AsFloat;
          FieldByName('PriceUnit').Value:=Trim(Order_Sub.fieldbyname('PriceUnit').AsString);
          FieldByName('Sorddefstr10').Value:=Trim(Order_Sub.fieldbyname('Sorddefstr10').AsString);
        //  FieldByName('PriceUnitRate').Value:=Order_Sub.fieldbyname('PriceUnitRate').AsFloat;
          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_CY.TBSaveClick(Sender: TObject);
begin
  OrdDate.SetFocus;
  if Trim(OrderNo.Text)='' then
  begin
    Application.MessageBox('订单号不能为空！','提示',0);
    Exit;
  end;
  if Trim(orddefstr10.Text)='' then
  begin
    Application.MessageBox('客户简称不能为空！','提示',0);
    Exit;
  end;
  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('PrtPrice',null,[])=True then
  begin
    Application.MessageBox('单价不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('PrtPrice',0,[])=True then
  begin
    Application.MessageBox('单价不能为空!','提示',0);
    Exit;
  end;
  if Trim(orddefstr5.Text)='' then
  begin
    Application.MessageBox('价格术语不能为空','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    ModalResult:=1;
  end;
end;

procedure TfrmOrderInPut_CY.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_CY.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:=null;
    FieldByName('SOrddefstr4').Value:=null;
    FieldByName('SOrddefstr2').Value:=null;
    Post;
  end;
end;

procedure TfrmOrderInPut_CY.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;


  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 JYOrderCY_Sub where SubId='''+Trim(Order_Sub.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;

end;

procedure TfrmOrderInPut_CY.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_CY.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 ShowModal=1 then
      begin
        CustomerNoName.Text:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
        CustomerNoName.TxtCode:=Trim(CDS_HZ.fieldbyname('ZDYCode').AsString);
        orddefstr10.Text:=Trim(CDS_HZ.fieldbyname('ZDYNameZ').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

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

procedure TfrmOrderInPut_CY.v1OrderUnitPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrderUnit';
      flagname:='数量单位';
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('OrderUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut_CY.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_CY.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_CY.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_CY.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_CY.MPRTSCTeBieNoteDblClick(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
        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_CY.FormCreate(Sender: TObject);
begin
  cxGrid1.Align:=alClient;
  OrdDate.DateTime:=SGetServerDateTime(ADOTemp);
  DlyDate.DateTime:=OrdDate.Date;
  OrdDefDate1.DateTime:=OrdDate.Date;
  OrdDefDate2.DateTime:=OrdDate.Date;
end;

procedure TfrmOrderInPut_CY.N1Click(Sender: TObject);
begin
  ToolButton1.Click;
end;

procedure TfrmOrderInPut_CY.N3Click(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmOrderInPut_CY.conNoChange(Sender: TObject);

var
  ConMainId:string;
begin

 
end;

procedure TfrmOrderInPut_CY.ToolButton4Click(Sender: TObject);
begin
  CDS_CG.Append;
  CDS_CG.FieldByName('CRTime').Value:=formatdateTime('yyyy-MM-dd',date());
  CDS_CG.Post;
end;

procedure TfrmOrderInPut_CY.ToolButton5Click(Sender: TObject);
begin
  IF CDS_CG.IsEmpty then exit;
  IF CDS_CG.FieldByName('defBit1').AsBoolean then
  begin
    if Application.MessageBox('此单已生成采购单，是否删除采购单和拆分信息','提示',32+4)<>IDYES then Exit;
  end;

  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete BP_InOut_CF where CRNO='''+Trim(CDS_CG.fieldbyname('CRNO').AsString)+'''');
    sql.Add('delete YF_Money_CR where mainID='''+Trim(CDS_CG.fieldbyname('CRNO').AsString)+'''');
    ExecSQL;
  end;
  CDS_CG.Delete;
end;

procedure TfrmOrderInPut_CY.ToolButton6Click(Sender: TObject);
var
  strlist:Tstringlist;
  i:integer;
  maxno:string;
begin
 // strlist:=Tstringlist.Create;
  IF (CDS_CG.Locate('ssel',true,[]) and CDS_CG.Locate('defbit1',true,[]))  then
  begin
    if Application.MessageBox('所选信息已有采购单生成，是否重新生成？','提示',32+4)<>IDYES   then exit;
  end;

  with CDS_CG do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      if fieldbyname('ssel').AsBoolean then
      begin
        if trim(fieldbyname('custName').AsString)='' then
        begin
          Application.MessageBox('生成采购单的拆分信息供应商不能为空！','提示信息',MB_ICONERROR);
          EnableControls;
          Exit;
        end;
      //  IF strlist.IndexOf(trim(fieldbyname('custName').AsString))<0 then
      //  strlist.Add(trim(fieldbyname('custName').AsString));
      end;
      next;
    end;
    first;
    EnableControls;
  end;
  ADOCmd.Connection.BeginTrans;
  try

      with CDS_CG do
      begin
        DisableControls;
        first;
        while not eof do
        begin
          if fieldbyname('ssel').AsBoolean then
          begin
         //   maxNo:=trim(orderNo.Text)+'-'+CDS_CG.fieldbyname('ID').AsString;
         //   if trim(fieldbyname('custName').AsString)=trim(strlist.Strings[i]) then
        //    begin
              with ADOCmd do
              begin
                close;
                sql.Clear;
                sql.Add('update BP_InOut_CF SET defBit1=1');
              //  sql.Add('inoutNO='+quotedstr(trim(orderNo.Text)+'-'+trim(maxno)));
                sql.Add('where CRNO='+quotedstr(CDS_CG.fieldbyname('CRNO').AsString));
                execsql;
              end;

              IF pos('华恒',trim(CDS_CG.fieldbyname('custName').AsString))<1 then
              begin
                IF not YFData() then
                begin
                  ADOCmd.Connection.RollbackTrans;
                  application.MessageBox('应付款生成失败！','提示信息',MB_ICONERROR);
                  exit;
                end;
              end;
          //  end;
          end;
          next;;
        end;
        first;
        EnableControls;
      end;

      with ADOCmd do
      begin
        close;
        sql.Clear;
        sql.Add('update BP_InOut_CF SET inoutNO=rtrim(B.orderNo)+''-''+cast(A.CRID as varchar)  ');
        sql.Add('from BP_InOut_CF  A');
        sql.Add('inner join JYOrder_main B on B.mainID=A.mainID');
        sql.Add('where A.mainID='+quotedstr(CDS_CG.fieldbyname('mainID').AsString));
        execsql;
      end;
      with ADOCmd do
      begin
        close;
        sql.Clear;
        sql.Add('update BP_InOut_CF  SET inoutNO=(select min(inoutNO) from BP_InOut_CF X where X.mainID=BP_InOut_CF.mainID and X.custName=BP_InOut_CF.custName)');
        sql.Add('where mainID='+quotedstr(CDS_CG.fieldbyname('mainID').AsString));
        execsql;
      end;
  {  for i:=0 to strlist.Count-1 do
    begin
      if GetLSNo(ADOCmd,maxno,'',trim(orderNO.Text),2,0)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;

     // i:=i+1;
    end;   }
    ADOCmd.Connection.CommitTrans;
    application.MessageBox('成功生成采购单！','提示信息');
    InitData();
  except
    ADOCmd.Connection.RollbackTrans;
    CDS_CG.EnableControls;
    application.MessageBox('生成采购单失败！','提示信息',MB_ICONERROR);
  end;
end;

procedure TfrmOrderInPut_CY.orderNoBtnClick(Sender: TObject);
var
  ConMainId:string;
begin
   ConMainId:='';
   frmProductOrderListSel:=TfrmProductOrderListSel.create(self);
   with frmProductOrderListSel do
   begin
     FFInt:=1;
     if showmodal=1 then
     begin
       ConMainId:=trim(Order_Main.fieldbyname('mainID').asstring);
     end;
     free;
   end;
   iF ConMainId='' then exit;
   with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select B.*,A.*,c.KhconNo,C.ConPerson2,C.ConPerson3,C.conDefstr2,C.conDefstr6,C.condefstr10,C.priceNote,C.Payment,C.ShippMent from JYOrder_sub A ');
      sql.Add('inner join  JYOrder_Main B on B.mainID=A.mainID          ');
      sql.Add('left  join JYOrderCon_Main C on C.conNO=B.conNO           ');
      sql.Add('where  B.mainID like '''+'%'+Trim(ConMainId)+'%'+'''');
      Open;
    end;
    IF not ADOTemp.IsEmpty then
    begin
      ConNo.Text:=Trim(ADOTemp.fieldbyname('ConNo').AsString);
      orderNo.Text:=Trim(ADOTemp.fieldbyname('orderNo').AsString);
      CYNO.Text:='CY_'+Trim(ADOTemp.fieldbyname('ConNo').AsString);
      CustomerNoName.Text:=Trim(ADOTemp.fieldbyname('CustomerNoName').AsString);
      CustomerNoName.TxtCode:=Trim(ADOTemp.fieldbyname('CustomerNo').AsString);
      DLYDate.DateTime:=ADOTemp.fieldbyname('DLYDate').AsDateTime;
   //   ConMainId:=Trim(ADOTemp.fieldbyname('MainId').AsString);
      OrdPerson2.Text:=Trim(ADOTemp.fieldbyname('OrdPerson2').AsString);
      KHCONNO.Text:=Trim(ADOTemp.fieldbyname('KHCONNO').AsString);
      LBNameNote.Text:=Trim(ADOTemp.fieldbyname('LBNameNote').AsString);
      MaiTouNote.Text:=Trim(ADOTemp.fieldbyname('MaiTouNote').AsString);

      ConPerson1.Text:=Trim(ADOTemp.fieldbyname('ConPerson2').AsString);
      ConPerson2.Text:=Trim(ADOTemp.fieldbyname('ConPerson3').AsString);

      conDefstr1.Text:=Trim(ADOTemp.fieldbyname('conDefstr2').AsString);
      conDefstr2.Text:=Trim(ADOTemp.fieldbyname('conDefstr6').AsString);

      orddefstr4.Text:=Trim(ADOTemp.fieldbyname('Payment').AsString);
      orddefstr5.Text:=Trim(ADOTemp.fieldbyname('priceNote').AsString);
      orddefstr6.Text:=Trim(ADOTemp.fieldbyname('ShippMent').AsString);

      orddefstr10.Text:=Trim(ADOTemp.fieldbyname('condefstr10').AsString);
    end;
    Order_Sub.EmptyDataSet;
    with ADOTemp do
    begin
        First;
        while not Eof do
        begin
          with Order_Sub do
          begin
            Append;
            FieldByName('XHNO').Value:=Trim(ADOTemp.fieldbyname('XHNO').AsString);
            FieldByName('PRTCode').Value:=Trim(ADOTemp.fieldbyname('MPRTCode').AsString);
            FieldByName('PRTCodeName').Value:=Trim(ADOTemp.fieldbyname('MPRTCodeName').AsString);
            FieldByName('PRTspec').Value:=Trim(ADOTemp.fieldbyname('MPRTspec').AsString);
            FieldByName('PRTOrderQty').Value:=Trim(ADOTemp.fieldbyname('PRTOrderQty').AsString);
            FieldByName('PRTMF').Value:=Trim(ADOTemp.fieldbyname('MPRTMF').AsString);
            FieldByName('PRTKZ').Value:=Trim(ADOTemp.fieldbyname('MPRTKZ').AsString);
            FieldByName('Sorddefstr1').Value:=Trim(ADOTemp.fieldbyname('Sorddefstr1').AsString);
            FieldByName('Sorddefstr2').Value:=Trim(ADOTemp.fieldbyname('Sorddefstr2').AsString);
             FieldByName('Sorddefstr4').Value:=Trim(ADOTemp.fieldbyname('Sorddefstr4').AsString);
            FieldByName('PRTColor').Value:=Trim(ADOTemp.fieldbyname('PRTColor').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('PriceUnitRate').Value:=Trim(ADOTemp.fieldbyname('PriceUnitRate').AsString);
       //     FieldByName('PRTmoney').Value:=Trim(ADOTemp.fieldbyname('PRTmoney').AsString);
            FieldByName('SordQty1').Value:=Trim(ADOTemp.fieldbyname('PRTOrderQty').AsString);

            FieldByName('Sorddefstr10').Value:=Trim(ADOTemp.fieldbyname('subID').AsString);
            Post;
          end;
          Next;
        end;
    end;



end;

procedure TfrmOrderInPut_CY.OrdPerson1BtnUpClick(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;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

end.
