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

type
  TfrmOrderInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    OrderNo: TEdit;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    Label10: TLabel;
    ConNo: TEdit;
    Label3: TLabel;
    Label14: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    OrdDate: TDateTimePicker;
    CustomerNoName: TBtnEditC;
    Label2: TLabel;
    Label6: TLabel;
    Label19: TLabel;
    Label25: TLabel;
    DLYDate: TDateTimePicker;
    Label26: TLabel;
    MPRTCode: TBtnEditC;
    MPRTBZNote: TMemo;
    Note: TMemo;
    Label23: TLabel;
    MPRTMF: TEdit;
    MPRTSpec: TEdit;
    MPRTKZ: TEdit;
    ADOQueryLabel: TADOQuery;
    RMGridReport1: TRMGridReport;
    Label8: TLabel;
    YWY: TBtnEditC;
    Label11: TLabel;
    orderType: TComboBox;
    Label4: TLabel;
    PRTColor: TEdit;
    MPRTCodeName: TBtnEditC;
    Label5: TLabel;
    PRTPs: TEdit;
    Label9: TLabel;
    PRTOrderQty: TEdit;
    Label15: TLabel;
    OrderUnit: TEdit;
    Label16: TLabel;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    N1: TMenuItem;
    CDS_PBCK: TClientDataSet;
    DS_PBKC: TDataSource;
    ADO_PBCK: TADOQuery;
    CDS_Glide: TClientDataSet;
    DS_Glide: TDataSource;
    Label7: TLabel;
    Label17: TLabel;
    PBSPEC: TEdit;
    PBZPS: TEdit;
    Label18: TLabel;
    PRTPiZhong: TEdit;
    ToolButton5: TToolButton;
    Label20: TLabel;
    KHConNo: TEdit;
    Label21: TLabel;
    cxPageControl1: TcxPageControl;
    cxTabSheet3: TcxTabSheet;
    ToolBar3: TToolBar;
    ToolButton1: TToolButton;
    ToolButton6: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxTabSheet4: TcxTabSheet;
    ToolBar2: TToolBar;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    V3Column1: TcxGridDBColumn;
    V3Column2: TcxGridDBColumn;
    V3Column3: TcxGridDBColumn;
    V3Column4: TcxGridDBColumn;
    V3Column5: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    gongyi: TcxButtonEdit;
    Label22: TLabel;
    DBColor: TcxButtonEdit;
    TV2Column4: TcxGridDBColumn;
    Label24: TLabel;
    PBName: TEdit;
    Label27: TLabel;
    BQKH: TEdit;
    Label28: TLabel;
    Jlen1: TEdit;
    Label29: TLabel;
    Jlen2: TEdit;
    Label30: TLabel;
    Jlen3: TEdit;
    Label31: TLabel;
    Jlen4: TEdit;
    Label32: TLabel;
    Jlen5: TEdit;
    Label33: TLabel;
    Jlen6: TEdit;
    Label34: TLabel;
    Jlen7: TEdit;
    Label35: TLabel;
    Jlen8: TEdit;
    Label36: TLabel;
    Label37: TLabel;
    JTname: TBtnEditC;
    JTno: TBtnEditC;
    Label38: TLabel;
    SJSF: TBtnEditC;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure CustomerNoNameBtnUpClick(Sender: TObject);
    procedure CustomerNoNameBtnDnClick(Sender: TObject);
    procedure MPRTCodeNameBtnUpClick(Sender: TObject);
    procedure OrdDefStr2BtnUpClick(Sender: TObject);
    procedure YCLFactoryBtnUpClick(Sender: TObject);
    procedure MPRTSCTeBieNoteBtnUpClick(Sender: TObject);
    procedure MPRTBZNoteDblClick(Sender: TObject);
    procedure MPRTCodeBtnUpClick(Sender: TObject);
    procedure PRTColorChange(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure PRTPsChange(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure PRTPsKeyPress(Sender: TObject; var Key: Char);
    procedure TV2Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TV2Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure gongyiPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Jlen1KeyPress(Sender: TObject; var Key: Char);
  private
    procedure InitData();
    procedure GetGy();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    function SaveData(): Boolean;
    function getOrderNo(fconNo: string): string;
    function CheckData(): Boolean;
    procedure InitPBCK();
    { Private declarations }
  public
    PState, CopyInt, PriceFlag: Integer;
    FMainId, FSubId, FKCID, 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,
  U_CPManageSel, getpic, U_LabelList, U_ProductFileSel, U_PBCKCMSel, U_PBKcList,
  U_GXGL_List_DXCX;

{$R *.dfm}
procedure TfrmOrderInPut.GetGy();
begin
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select top 1 OrderNo,MPRTMF,MPRTKZ,MPRTBZNote from  JYOrder_Main X inner join JYOrder_Sub Y on X.MainId=Y.MainId  ');
    sql.Add('where X.MPRTCodeName=' + quotedstr(trim(MPRTCodeName.Text)));
    sql.Add(' and X.CustomerNoName=' + quotedstr(trim(CustomerNoName.Text)));
    sql.Add(' and Y.PRTColor=' + quotedstr(trim(PRTColor.Text)));
    sql.Add(' and X.MPRTMF=' + quotedstr(trim(MPRTMF.Text)));
    sql.Add(' and X.MPRTKZ=' + quotedstr(trim(MPRTKZ.Text)));
    sql.Add(' and Status=''1'' order by OrdDate desc ');
//    ShowMessage(SQL.Text);
    Open;
  end;
  MPRTBZNote.Text := Trim(ADOTemp.FieldByName('MPRTBZNote').AsString);
//  MPRTMF.Text := Trim(ADOTemp.FieldByName('MPRTMF').AsString);
//  MPRTKZ.Text := Trim(ADOTemp.FieldByName('MPRTKZ').AsString);
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select GlideNo='''',GlideIdx,CarNo='''',MainId='''',SubId='''',GlideName,RollNum,Qty,QtyUnit,Filler,Filltime,Editer,Edittime,Chker,ChkTime,Status,Valid,Note,MXPS,kaipi');
    sql.Add('from JYOrder_Glide A ');
    sql.Add('  where A.CarNo=(select top 1 OrderNo from  JYOrder_Main X inner join JYOrder_Sub Y on X.MainId=Y.MainId  where X.MPRTCodeName=' + quotedstr(trim(MPRTCodeName.Text)));
    sql.Add(' and X.CustomerNoName=' + quotedstr(trim(CustomerNoName.Text)));
    sql.Add(' and X.MPRTMF=' + quotedstr(trim(MPRTMF.Text)));
    sql.Add(' and X.MPRTKZ=' + quotedstr(trim(MPRTKZ.Text)));
    sql.Add(' and Y.PRTColor=' + quotedstr(trim(PRTColor.Text)));
    sql.Add(' and Status=''1'' order by OrdDate desc)');
    sql.Add(' order by Glideidx ');
    Open;
  end;
  SCreateCDS20(ADOTemp, CDS_Glide);
  SInitCDSData20(ADOTemp, CDS_Glide);
end;

procedure TfrmOrderInPut.InitPBCK();
begin

  try
    with ADO_PBCK do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*');
      sql.Add(',KCPS=(select X.KCPS from CK_PB_CR X  where X.Valid=''Y'' and X.CRFlag=''入库'' and X.CRID=A.KCID)');
      sql.Add('from JYOrder_PBKC A ');
      sql.Add(' where A.MainID=' + QUOTEDSTR(Trim(FMainID)));
      sql.Add(' AND A.SUBID=' + QUOTEDSTR(Trim(FSUBID)));
//      ShowMessage(SQL.Text);
      open;
    end;
    SCreateCDS20(ADO_PBCK, CDS_PBCK);
    SInitCDSData20(ADO_PBCK, CDS_PBCK);
  finally

  end;
end;

function TfrmOrderInPut.CheckData(): Boolean;
var
  mvalue, FReal: double;
begin
  OrdDate.SetFocus;
  try
//    if Trim(conNo.Text) = '' then
//      raise Exception.Create('订单号不能为空！');

    if Trim(MPRTCodeName.Text) = '' then
      raise Exception.Create('产品名称不能为空！');

//    if Order_Sub.IsEmpty then
//      raise Exception.Create('明细不能为空！');
//
//    if Order_Sub.Locate('PRTOrderQty', null, []) then
//      raise Exception.Create('数量不能为空！');

    if Trim(PRTPs.Text) = '' then
      raise Exception.Create('匹数不能为空！');
    if Tv1.DataController.Summary.FooterSummaryValues[1] < StrToInt(PRTPs.Text) then
      raise Exception.Create('分配匹数不足！');

    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

function TfrmOrderInPut.getOrderNo(fconNo: string): string;
var
  str1: string;
  i: integer;
begin
  str1 := trim(fconNo) + '01';
  with ADOTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select MainID from JYOrder_Main where orderNo=' + quotedstr(trim(str1)));
    open;
  end;
  if not ADOTemp.IsEmpty then
  begin
    for i := 1 to 99 do
    begin
      str1 := trim(fconNo) + rightstr(inttostr(100 + i), 2);
      with ADOCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select MainID from JYOrder_Main where orderNo=' + quotedstr(trim(str1)));
        open;
      end;
      if ADOCmd.IsEmpty then
        break;
    end;
  end;
  result := str1;
end;

procedure TfrmOrderInPut.TBCloseClick(Sender: TObject);
begin

  WriteCxGrid(Self.Caption, Tv1, '生产指示单管理');
  Close;
end;

procedure TfrmOrderInPut.InitData();
var
  a:String;
  b:TStrings;
begin
  if PState = 0 then
  begin
    OrdDate.DateTime := SGetServerDateTime(ADOTemp);
    DlyDate.DateTime := OrdDate.DateTime;
  end;
  
  //**********************刷新计划单信息*****************************
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    if PState = 0 then
    begin
      sql.Add('select X.* ');
      sql.Add('from CK_PB_CR X  where X.Valid=''Y'' and X.CRFlag=''入库'' and X.KCID= ' + QUOTEDSTR(Trim(FKCID)));
    end
    else
    begin
      sql.Add('select A.*,B.* from JYOrder_Main A inner join JYOrder_Sub B on A.Mainid=B.Mainid ');
      sql.Add(' where B.SubId=' + Quotedstr(Trim(FSubId)));
    end;
    Open;
  end;
  if PState = 0 then
  begin
    CustomerNoName.Text := Trim(ADOTemp.FieldByName('fromFactoryName').AsString);
    MPRTCodeName.Text := Trim(ADOTemp.FieldByName('Code').AsString);
    MPRTCodeName.Text := Trim(ADOTemp.FieldByName('CodeName').AsString);
    MPRTMF.Text := Trim(ADOTemp.FieldByName('MF').AsString);
    MPRTKZ.Text := Trim(ADOTemp.FieldByName('KZ').AsString);
    MPRTSpec.Text := Trim(ADOTemp.FieldByName('BatchNo').AsString);
    PRTColor.Text := Trim(ADOTemp.FieldByName('CPColor').AsString);
    PRTPs.Text := Trim(ADOTemp.FieldByName('PS').AsString);
    PRTPiZhong.Text := Trim(ADOTemp.FieldByName('PiZhong').AsString);
    PRTOrderQty.Text := Trim(ADOTemp.FieldByName('Qty').AsString);
    OrderUnit.Text := Trim(ADOTemp.FieldByName('QtyUnit').AsString);
    PBZPS.Text := Trim(ADOTemp.FieldByName('PS').AsString);
    PBSPEC.Text := Trim(ADOTemp.FieldByName('MF').AsString) + '/' + Trim(ADOTemp.FieldByName('KZ').AsString);
//    a:='切边、米、公斤、打包';
//    b.Text:=a;
//    ExtractStrings(['#'],[' '],PChar(a),b);
//    MPRTBZNote.Text :='切边、米、公斤、打包';
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add(' select top 1 A.* from JYOrder_Main A  ');
      sql.Add(' where A.MPRTCodeName=' + Quotedstr(Trim(MPRTCodeName.Text)));
      sql.Add(' and A.Status=''1'' order by A.OrdDate desc');
      Open;
    end;

    if Trim(MPRTKZ.Text) = '' then
      MPRTMF.Text := Trim(ADOTemp.FieldByName('MPRTMF').AsString);
    if Trim(MPRTKZ.Text) = '' then
      MPRTKZ.Text := Trim(ADOTemp.FieldByName('MPRTKZ').AsString);
  end
  else
  begin
    SCSHDataNew(ADOTemp, ScrollBox1, 1);
    SCSHDataNew(ADOTemp, ScrollBox1, 2);
  end;

  //**********************刷新计划单信息*****************************
  //**********************刷新安排坯布库存*****************************
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    if PState = 0 then
    begin
      sql.Add('select CodeName,X.KCPS,X.KCId,PBKCID='''',MainId='''',SubId='''',	FPPS=KCPS,YYPS=0 ');
      sql.Add('from CK_PB_CR X  where X.Valid=''Y'' and X.CRFlag=''入库'' and X.KCID= ' + QUOTEDSTR(Trim(FKCID)));
    end
    else
    begin
      sql.Add('select A.*');
      sql.Add(',CodeName=(select X.CodeName from CK_PB_CR X  where X.Valid=''Y'' and X.CRFlag=''入库'' and X.CRID=A.KCID)');
      sql.Add(',KCPS=(select X.KCPS from CK_PB_CR X  where X.Valid=''Y'' and X.CRFlag=''入库'' and X.CRID=A.KCID)');
      sql.Add('from JYOrder_PBKC A ');
      sql.Add('where A.MainID=' + QUOTEDSTR(Trim(FMainID)));
      sql.Add('AND A.SUBID=' + QUOTEDSTR(Trim(FSUBID)));
    end;
    Open;
  end;
  SCreateCDS20(ADOTemp, CDS_PBCK);
  SInitCDSData20(ADOTemp, CDS_PBCK);
  //**********************刷新安排坯布库存*****************************
  //**********************刷新工序安排*****************************
  if PState = 0 then
  begin

    //2、新增计划单时客户、品名、色号和之前一样时调取最近一次的门幅、克重、颜色、包装要求、工序
    GetGY()

  end
  else
  begin
    with ADOTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add('select A.*');
      sql.Add('from JYOrder_Glide A ');
      sql.Add('where A.CarNo=' + quotedstr(trim(FOrderNo)));
      sql.Add(' order by Glideidx ');
//      showmessage(SQL.text);
      Open;
    end;
  end;

  SCreateCDS20(ADOTemp, CDS_Glide);
  SInitCDSData20(ADOTemp, CDS_Glide);
  //**********************刷新工序安排*****************************
  //**********************复制生产单*****************************
  if CopyInt = 99 then
  begin
    PState := 0;
    FMainId := '';
    FSubId := '';
    OrderNo.Text := '';
  end;
  //**********************复制生产单*****************************

end;

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

procedure TfrmOrderInPut.FormShow(Sender: TObject);
begin
  readCxGrid(Self.Caption, Tv1, '生产指示单管理');
  OrderNo.Text := '';
  fFlileFlag := UserDataFlag + 'HX';
  InitData();
  if PState = 0 then
  begin
  MPRTBZNote.Text :='切边、米、公斤、打包';
  end;

end;

function TfrmOrderInPut.SaveData(): Boolean;
var
  maxno, Smaxno, maxorderno, MaxPBKCID, MaxGlideNo: 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;
      if GetLSNo(ADOCmd, maxorderno, 'FH', 'JYOrder_Main', 3, 1) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;
      FOrderNo := TRIM(maxorderno);
      OrderNo.Text := TRIM(maxorderno);
    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;
        FieldByName('status').Value := '1';
        FieldByName('XSWS').Value:=1;
        FieldByName('CDWS').Value:=0;
      end
      else
      begin
        Edit;
      end;
      FieldByName('MainId').Value := Trim(maxno);
      SSetsaveSqlNew(ADOCmd, 'JYOrder_Main', ScrollBox1, 1);
      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;
    //*************************保存主表*************************
    //*************************保存产品表*************************
    if Trim(FSubId) = '' then
    begin
      if GetLSNo(ADOCmd, Smaxno, 'JS', 'JYOrder_Sub', 4, 1) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;
    end
    else
    begin
      Smaxno := Trim(FSubId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from JYOrder_Sub  where SubId=' + quotedstr(Trim(Trim(FSubId))));
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FSubId) = '' then
      begin
        Append;
      end
      else
      begin
        Edit;
      end;
      FieldByName('MainId').Value := Trim(maxno);
      FieldByName('SubId').Value := Trim(Smaxno);
      SSetsaveSqlNew(ADOCmd, 'JYOrder_Sub', ScrollBox1, 2);
      Post;
    end;
    //*************************保存产品表*************************
    //*************************保存坯布库存*************************
    with CDS_PBCK do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_PBCK.fieldbyname('PBKCID').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, MaxPBKCID, 'YK', 'JYOrder_PBKC', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          MaxPBKCID := Trim(CDS_PBCK.fieldbyname('PBKCID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_PBKC');
          sql.Add('where PBKCID=' + QuotedStr(trim(MaxPBKCID)));
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(CDS_PBCK.fieldbyname('PBKCID').AsString) = '' then
            Append
          else
            Edit;
          FieldByName('MainId').Value := Trim(maxno);
          FieldByName('SubId').Value := Trim(Smaxno);
          FieldByName('KCId').Value := FieldByName('KCId').Value;
          RTSetSaveDataCDS(ADOCmd, Tv1, CDS_PBCK, 'JYOrder_PBKC', 0);
          FieldByName('PBKCID').Value := Trim(MaxPBKCID);
          Post;
        end;
        CDS_PBCK.Edit;
        CDS_PBCK.FieldByName('PBKCID').Value := Trim(MaxPBKCID);
        Next;
      end;
    end;
    //*************************保存坯布库存*************************
    //*************************保存工序*************************
    with CDS_Glide do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_Glide.fieldbyname('GlideNo').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, MaxGlideNo, 'CG', 'JYOrder_Glide', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          MaxGlideNo := Trim(CDS_Glide.fieldbyname('GlideNo').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_Glide');
          sql.Add('where GlideNo=' + QuotedStr(trim(MaxGlideNo)));
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(CDS_Glide.fieldbyname('GlideNo').AsString) = '' then
            Append
          else
            Edit;
          FieldByName('MainId').Value := Trim(maxno);
          FieldByName('SubId').Value := Trim(Smaxno);
          FieldByName('GlideNo').Value := Trim(MaxGlideNo);
          FieldByName('CarNo').Value := Trim(FOrderNo);
          RTSetSaveDataCDS(ADOCmd, Tv2, CDS_Glide, 'JYOrder_Glide', 0);
          Post;
        end;
        CDS_Glide.Edit;
        CDS_Glide.FieldByName('GlideNo').Value := Trim(MaxGlideNo);
        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

  if CheckData() = false then
    exit;

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

procedure TfrmOrderInPut.CustomerNoNameBtnUpClick(Sender: TObject);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
 
//      IF self.canshu1<>'高权限' then canshu1:='业务员';
      if ShowModal = 1 then
      begin
        CustomerNoName.Text := Trim(CDS_HZ.fieldbyname('CoHRName').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
  frmPBCKCMSel := TfrmPBCKCMSel.Create(self);
  with frmPBCKCMSel do
  begin
    if showmodal = 1 then
    begin
//      MPRTCode.Text := CDS_HZ.fieldbyname('CYNo').asstring;
      MPRTCodeName.Text := CDS_HZ.fieldbyname('CodeName').asstring;
      MPRTSpec.Text := CDS_HZ.fieldbyname('Spec').asstring;
      MPRTMF.Text := CDS_HZ.fieldbyname('MF').asstring;
      MPRTKZ.Text := CDS_HZ.fieldbyname('kz').asstring;
    end;
    free;
  end;
  tv1.Controller.EditingController.ShowEdit();
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 ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.YCLFactoryBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

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

procedure TfrmOrderInPut.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.MPRTCodeBtnUpClick(Sender: TObject);
begin
  frmProductFileSel := TfrmProductFileSel.Create(self);
  with frmProductFileSel do
  begin
    if showmodal = 1 then
    begin
      MPRTCode.Text := CDS_HZ.fieldbyname('SPCode').asstring;
      MPRTCodeName.Text := CDS_HZ.fieldbyname('SPName').asstring;
      MPRTSpec.Text := CDS_HZ.fieldbyname('SPSpec').asstring;
      MPRTMF.Text := CDS_HZ.fieldbyname('SPMF').asstring;
      MPRTKZ.Text := CDS_HZ.fieldbyname('SPKZ').asstring;

    end;
    free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmOrderInPut.PRTColorChange(Sender: TObject);
begin
// if pos('#', mvalue) = 0 then
//  begin
//    with Order_Sub do
//    begin
//      Edit;
//      FieldByName(FFieldName).Value := Trim(mvalue) + '#';
//      Post;
//    end;
//  end;
end;

procedure TfrmOrderInPut.ToolButton7Click(Sender: TObject);
var
  maxno: string;
begin
  frmPBKcList := TfrmPBKcList.create(self);
  with frmPBKcList do
  begin
    FType := 10;
    if showmodal = 1 then
    begin
      with CDS_Main do
      begin
        while CDS_Main.Locate('SSel', True, []) do
        begin
          with ADOQueryCmd do
          begin
            sql.Add('select * from JYOrder_PBKC');
            sql.Add('where MainID=' + QuotedStr(trim(Self.fMainID)));
            sql.Add('and subID=' + QuotedStr(trim(Self.fsubID)));
            sql.Add('and KCID=' + QuotedStr(Trim(CDS_Main.FieldByName('KCID').AsString)));
          end;
          if ADOQueryCmd.IsEmpty then
          begin
            if GetLSNo(ADOQueryCmd, maxno, 'YK', 'JYOrder_PBKC', 4, 1) = False then
            begin
              Application.MessageBox('生成流水号异常！', '提示', 0);
              exit;
            end;
            with ADOQueryCmd do
            begin
              Close;
              SQL.Clear;
              sql.Add('insert into JYOrder_PBKC(PBKCID,MainID,subID,KCID,FPPS ) ');
              sql.Add('values(' + QuotedStr(maxno));
              sql.Add(',' + QuotedStr(trim(Self.fMainID)));
              sql.Add(',' + QuotedStr(trim(Self.fsubID)));
              sql.Add(',' + QuotedStr(Trim(CDS_Main.FieldByName('KCID').AsString)));
              sql.Add(',' + QuotedStr(Trim(CDS_Main.FieldByName('KCPS').AsString)));
              sql.Add(' )');
              ExecSQL;
            end;
          end;
          CDS_Main.Delete;
        end;
      end;
    end;
    free;
  end;
  InitPBCK();
end;

procedure TfrmOrderInPut.ToolButton8Click(Sender: TObject);
begin
  if CDS_PBCK.IsEmpty then
    exit;

  if CDS_PBCK.fieldbyname('YYPS').AsInteger > 0 then
  begin
    Application.MessageBox('已开卡不能删除！', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' delete JYOrder_PBKC where PBKCID=''' + Trim(CDS_PBCK.fieldbyname('PBKCID').AsString) + '''');
      sql.Add(' and  YYPS=0');
      execsql;
    end;
    CDS_PBCK.Delete;
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

procedure TfrmOrderInPut.ToolButton6Click(Sender: TObject);
begin

  frmGXGL_List_DXCX := TfrmGXGL_List_DXCX.create(self);
  with frmGXGL_List_DXCX do
  begin
    if showmodal = 1 then
    begin
      Order_FZ.First;
      while not Order_FZ.eof do
      begin
        CDS_Glide.Append;
        CDS_Glide.FieldByName('Glideidx').Value := CDS_Glide.RecordCount + 1;
        CDS_Glide.FieldByName('kaipi').Value := '否';
        CDS_Glide.FieldByName('GlideName').Value := Order_FZ.fieldbyname('PSName').AsString;
        CDS_Glide.FieldByName('Note').Value := trim(Order_FZ.fieldbyname('Note').AsString);
        CDS_Glide.Post;
        Order_FZ.next;
      end;
    end;
    free;
  end;
end;

procedure TfrmOrderInPut.ToolButton2Click(Sender: TObject);
begin
  with CDS_Glide do
  begin
    if IsEmpty then
      Exit;
    if RecNo >= 1 then
    begin
      Edit;
      fieldByName('GlideIdx').Value := fieldByName('GlideIdx').Value - 1;
      Post;
      Prior;
      Edit;
      fieldByName('GlideIdx').Value := fieldByName('GlideIdx').Value + 1;
      Post;
    end;
    Prior;
  end;
end;

procedure TfrmOrderInPut.ToolButton3Click(Sender: TObject);
begin
  CDS_Glide.IndexFieldNames := 'GlideIdx';
  with CDS_Glide do
  begin
    if IsEmpty then
      Exit;
    Next;
    if not Eof then
      Prior
    else
    begin
      exit;
    end;
    if not Eof then
    begin
      Edit;
      fieldByName('GlideIdx').Value := fieldByName('GlideIdx').Value + 1;
      Post;
      Prior;
      Edit;
      fieldByName('GlideIdx').Value := fieldByName('GlideIdx').Value - 1;
      Post;
    end;
    Next;
  end;
end;

procedure TfrmOrderInPut.ToolButton4Click(Sender: TObject);
var
  k: integer;
begin
  if cds_Glide.IsEmpty then
    exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' delete JYOrder_Glide where GlideNo=''' + Trim(cds_Glide.fieldbyname('GlideNo').AsString) + '''');

      execsql;
    end;
    cds_Glide.Delete;
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
//  cds_Glide.Delete;
  cds_Glide.IndexFieldNames := 'GlideIdx';
  cds_Glide.DisableControls;
  cds_Glide.First;
  k := 1;
  while not cds_Glide.Eof do
  begin
    cds_Glide.Edit;
    cds_Glide.FieldByName('GlideIdx').Value := k;
    cds_Glide.Post;
    inc(k);
    cds_Glide.Next;
  end;
  cds_Glide.First;
  cds_Glide.EnableControls;
end;

procedure TfrmOrderInPut.ToolButton1Click(Sender: TObject);
var
  maxId: string;
begin
//  if Trim(FOrderNo) = '' then
//  begin
//    Application.MessageBox('请先保存计划单！', '提示信息', MB_ICONERROR);
//    exit;
//  end;
  try
//    frmGXGL_List_DXCX := TfrmGXGL_List_DXCX.create(self);
//    with frmGXGL_List_DXCX do
//    begin
//      if showmodal = 1 then
//      begin
//        Order_FZ.First;
//        while not Order_FZ.eof do
//        begin
//
//          CDS_Glide.Append;
//          CDS_Glide.FieldByName('Glideidx').Value := Order_FZ.RecNo;
//          CDS_Glide.FieldByName('GlideName').Value := trim(Order_FZ.fieldbyname('PSName').AsString);
//          CDS_Glide.FieldByName('Note').Value := trim(Order_FZ.fieldbyname('Note').AsString);
//          CDS_Glide.Post;
//
//          Order_FZ.Next;
//        end;
//      end;
//    end;
    CDS_Glide.Append;
    CDS_Glide.FieldByName('Glideidx').Value := CDS_Glide.RecordCount + 1;
    CDS_Glide.FieldByName('kaipi').Value := '否';
    CDS_Glide.Post;
  finally
    frmGXGL_List_DXCX.Free;
  end;
end;

procedure TfrmOrderInPut.PRTPsChange(Sender: TObject);
begin

  PRTOrderQty.Text := FloatToStr(RoundFloat(StrToFloatDef(PRTPs.Text, 0) * StrToFloatDef(PRTPiZhong.Text, 0), 2));
end;

procedure TfrmOrderInPut.ToolButton5Click(Sender: TObject);
begin
  GetGy();
end;

procedure TfrmOrderInPut.PRTPsKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if not CDS_PBCK.IsEmpty then
    begin

      with CDS_PBCK do
      begin
        First;
        if FieldByName('FPPS').AsInteger < StrToIntDef(PRTPs.Text, 0) then
        begin
          PRTPs.Text := CDS_PBCK.FieldByName('FPPS').AsString;
        end;
        Edit;
        FieldByName('FPPS').Value := StrToIntDef(PRTPs.Text, 0);
        Post;
      end;
    end;
  end;
end;

procedure TfrmOrderInPut.TV2Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'GXMF';
      flagname := '工序门幅';
      if ShowModal = 1 then
      begin
        with CDS_Glide do
        begin
          Edit;
          FieldByName('PSNote').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.TV2Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'GXKZ';
      flagname := '工序克重';
      if ShowModal = 1 then
      begin
        with CDS_Glide do
        begin
          Edit;
          FieldByName('PSNote').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.gongyiPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TcxButtonEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.Jlen1KeyPress(Sender: TObject; var Key: Char);
var
  p: integer;
begin
  if not (Key in [#8, #13, #127, '.', '-', '0'..'9']) then Key := #0;
//  if Key = '.' then
//  begin
//     p := Pos('.', Self.Text);
//     if p > 0 then Key := #0;
//  end;
//     //限制只能在第一位输入且只能输入一个'-'号
//  if Key = '-' then
//  begin
//     if TEdit(Controls[26]).SelStart > 0 then Key := #0;
//     p := Pos('-', Self.Text);
//     if p > 0 then Key := #0;
//  end;
end;

end.

