unit U_MJManage;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxCalendar, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, RM_Common, RM_Class, RM_GridReport,
  RM_System, RM_Dataset, ADODB, DBClient, cxGridCustomPopupMenu,
  cxGridPopupMenu, ExtCtrls, ComCtrls, ToolWin, cxTextEdit, Buttons;
{function CommOpen(fhandle:hwnd;sCommName:PAnsiChar;
                    IntTime:integer):integer;stdcall;external 'JCYData.DLL';
function CommClose(sCommName:PAnsiChar):integer;stdcall;external 'JCYData.DLL';}


type
  TfrmMJManage = class(TForm)
    Panel1: TPanel;
    MainId: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Edit1: TEdit;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxGridPopupMenu1: TcxGridPopupMenu;
    DataSource1: TDataSource;
    Order_MJ: TClientDataSet;
    ADOQueryMain: TADOQuery;
    ADOCmd: TADOQuery;
    ADOTmp: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    RM2: TRMGridReport;
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    Panel2: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    v1OrderDate: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    tv2CDType: TcxGridDBColumn;
    tv2CDWZ: TcxGridDBColumn;
    Tv2CDQty: TcxGridDBColumn;
    Tv2CDReason: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    TBCD: TToolButton;
    Panel3: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    Button10: TButton;
    Button11: TButton;
    Button12: TButton;
    Panel4: TPanel;
    Button13: TButton;
    Button14: TButton;
    Button15: TButton;
    Button16: TButton;
    Button17: TButton;
    Button18: TButton;
    Button19: TButton;
    Button20: TButton;
    Button21: TButton;
    Button22: TButton;
    Button23: TButton;
    Button24: TButton;
    Button25: TButton;
    Button26: TButton;
    Button27: TButton;
    Button28: TButton;
    Button29: TButton;
    Button30: TButton;
    Button31: TButton;
    Button32: TButton;
    Button33: TButton;
    Button34: TButton;
    Button35: TButton;
    Button36: TButton;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    DataSource2: TDataSource;
    CDS_MJCD: TClientDataSet;
    DataSource3: TDataSource;
    CDS_MJID: TClientDataSet;
    v2Column1: TcxGridDBColumn;
    Button39: TButton;
    Button40: TButton;
    Button41: TButton;
    Button42: TButton;
    Button43: TButton;
    Button44: TButton;
    Button45: TButton;
    Button46: TButton;
    Button47: TButton;
    Button48: TButton;
    Button49: TButton;
    Button50: TButton;
    Panel5: TPanel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    MJMaoZ: TEdit;
    MJLen: TEdit;
    MJFK: TEdit;
    MJBanZu: TEdit;
    BTPrint: TButton;
    Button38: TButton;
    MJSJKZ: TEdit;
    CDQty: TEdit;
    Timer1: TTimer;
    Label14: TLabel;
    BTAdd: TButton;
    BTEdit: TButton;
    ADOQueryPrint: TADOQuery;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure MainIdKeyPress(Sender: TObject; var Key: Char);
    procedure TBCloseClick(Sender: TObject);
    procedure Tv2CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TBCDClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button13Click(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Button14Click(Sender: TObject);
    procedure Button15Click(Sender: TObject);
    procedure Button16Click(Sender: TObject);
    procedure Button17Click(Sender: TObject);
    procedure Button18Click(Sender: TObject);
    procedure Button19Click(Sender: TObject);
    procedure Button20Click(Sender: TObject);
    procedure Button21Click(Sender: TObject);
    procedure Button22Click(Sender: TObject);
    procedure Button23Click(Sender: TObject);
    procedure Button24Click(Sender: TObject);
    procedure Button25Click(Sender: TObject);
    procedure Button26Click(Sender: TObject);
    procedure Button27Click(Sender: TObject);
    procedure Button28Click(Sender: TObject);
    procedure Button29Click(Sender: TObject);
    procedure Button30Click(Sender: TObject);
    procedure Button31Click(Sender: TObject);
    procedure Button32Click(Sender: TObject);
    procedure Button33Click(Sender: TObject);
    procedure Button34Click(Sender: TObject);
    procedure Button35Click(Sender: TObject);
    procedure Button36Click(Sender: TObject);
    procedure Button38Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure MJMaoZClick(Sender: TObject);
    procedure MJLenClick(Sender: TObject);
    procedure MJFKClick(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure Button11Click(Sender: TObject);
    procedure MJBanZuClick(Sender: TObject);
    procedure MJSJKZClick(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Tv3CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure FormClick(Sender: TObject);
    procedure Button12Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button39Click(Sender: TObject);
    procedure Button40Click(Sender: TObject);
    procedure Button41Click(Sender: TObject);
    procedure Button42Click(Sender: TObject);
    procedure Button43Click(Sender: TObject);
    procedure Button44Click(Sender: TObject);
    procedure Button45Click(Sender: TObject);
    procedure Button46Click(Sender: TObject);
    procedure Button47Click(Sender: TObject);
    procedure Button48Click(Sender: TObject);
    procedure Button49Click(Sender: TObject);
    procedure Button50Click(Sender: TObject);
    procedure MJMaoZChange(Sender: TObject);
    procedure BTAddClick(Sender: TObject);
    procedure BTEditClick(Sender: TObject);
    procedure BTPrintClick(Sender: TObject);
    procedure Tv2CDQtyPropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
    FInt,PState:Integer;
    FColumn:String;
    MValue:String;
    procedure InitJP();
    procedure InitGrid();
    procedure InitCDGrid();
    procedure InitCDGridID();
    procedure AddCD(Fbtn:TButton);
    procedure AddSL(Fbtn:TButton);
    function  SaveData():Boolean;
    procedure BtnStatus(BSInt:Boolean);
  public
    { Public declarations }
  end;

var
  frmMJManage: TfrmMJManage;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp;

{$R *.dfm}

procedure TfrmMJManage.FormDestroy(Sender: TObject);
begin
  frmMJManage:=nil;
end;

procedure TfrmMJManage.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;




procedure TfrmMJManage.MainIdKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    InitGrid();
    InitCDGridID();
    ADOQueryMain.First;
    SSetWinData(ADOQueryMain,Panel5);
    if CDS_MJID.IsEmpty then
    begin
      SClearData(Panel5,0);
    end;
    InitCDGrid();

  end;
end;
procedure TfrmMJManage.InitGrid();
begin
  with ADOQueryMain do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from WFBOrder_Main where MainID='''+Trim(MainId.Text)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryMain,Order_MJ);
  SInitCDSData20(ADOQueryMain,Order_MJ);
  MainId.Text:='';
end;
procedure TfrmMJManage.InitCDGrid();
begin
  with ADOQueryMain do
  begin
    Close;
    SQL.Clear;
    if PState=1 then
      sql.Add('select * from WFB_MJJY_CD where MJID='''' ')
    else
      sql.Add('select * from WFB_MJJY_CD where MJID='''+Trim(CDS_MJID.fieldbyname('MJID').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryMain,CDS_MJCD);
  SInitCDSData20(ADOQueryMain,CDS_MJCD);
  //MainId.Text:='';
end;
procedure TfrmMJManage.InitCDGridID();
begin
  with ADOQueryMain do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from WFB_MJJY where MainID='''+Trim(Order_MJ.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryMain,CDS_MJID);
  SInitCDSData20(ADOQueryMain,CDS_MJID);
  //MainId.Text:='';
end;

procedure TfrmMJManage.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmMJManage.Tv2CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var
  fsj:string;
begin
  FInt:=0;
  //Tv1.DataController.FocusedRecordIndex;
  //fsj:=Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName;
end;

procedure TfrmMJManage.TBCDClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='WFBCD';
      flagname:='无纺布疵点';
      if ShowModal=1 then
      begin
        Self.InitJP();
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmMJManage.FormShow(Sender: TObject);
begin
  InitJP();
end;
procedure TfrmMJManage.InitJP();
var
  AA:array[0..100] of string;
  i,j:Integer;
begin
  with ADOTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select ZDYName from KH_Zdy where Type=''WFBCD'' order by ZDYNO ');
    Open;
  end;
  if ADOTmp.IsEmpty then
  begin
    Application.MessageBox('没有定义疵点！','提示',0);
    Exit;
  end;
  with ADOTmp do
  begin
    First;
    i:=0;
    while not Eof do
    begin
      AA[i]:=Trim(fieldbyname('ZDYName').AsString);
      i:=i+1;
      Next;
    end;
  end;
  i:=i-1;
  for j:=0 to 32 do
  begin
    with Panel4 do
    begin
      TButton(Controls[j]).Visible:=True;
      if Length(AA[j])>3 then
      begin

      end;
      TButton(Controls[j]).Caption:=AA[j];
    end;
  end;
end;

procedure TfrmMJManage.Button13Click(Sender: TObject);
begin
  AddCD(Button13);
end;
procedure TfrmMJManage.AddCD(Fbtn:TButton);
begin
  if PState<1 then Exit;
  if Order_MJ.IsEmpty then Exit;
  with CDS_MJCD do
  begin
    Append;
    FieldByName('cdname').Value:=Trim(TButton(Fbtn).Caption);
    Post;
  end;
end;
procedure TfrmMJManage.AddSL(Fbtn:TButton);
begin
  if PState<1 then Exit;
  if Order_MJ.IsEmpty then Exit;

  //if MJMaoZ.Focused then
  if FInt=4 then Exit;
  if FInt=1 then
  begin
    MJMaoZ.Text:=Trim(MJMaoZ.Text)+Trim(TButton(Fbtn).Caption);
  end else
  if FInt=2 then
  begin
    MJLen.Text:=Trim(MJLen.Text)+Trim(TButton(Fbtn).Caption);
  end else
  if FInt=3 then
  begin
    MJFK.Text:=Trim(MJFK.Text)+Trim(TButton(Fbtn).Caption);
  end else
  if CDS_MJCD.IsEmpty=False then
  begin
    FColumn:=Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName;
    if Trim(FColumn)<>'' then
    begin
      with CDS_MJCD do
      begin
        Edit;
        FieldByName(FColumn).Value:=Trim(FieldByName(FColumn).AsString)+Trim(TButton(Fbtn).Caption);
        if Trim(fieldbyname('CDQtyS').AsString)<>'' then
          Fieldbyname('CDQty').Value:=Trim(fieldbyname('CDQtyS').AsString);
        Post;
      end;
    end;
  end;
end;

procedure TfrmMJManage.Tv2CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if Tv2.OptionsSelection.CellSelect=True then
  begin
    if CDS_MJCD.IsEmpty then Exit;
      CDS_MJCD.Delete;
  end;

end;

procedure TfrmMJManage.Button14Click(Sender: TObject);
begin
  AddCD(Button14);
end;

procedure TfrmMJManage.Button15Click(Sender: TObject);
begin
  AddCD(Button15);
end;

procedure TfrmMJManage.Button16Click(Sender: TObject);
begin
  AddCD(Button16);
end;

procedure TfrmMJManage.Button17Click(Sender: TObject);
begin
  AddCD(Button17);
end;

procedure TfrmMJManage.Button18Click(Sender: TObject);
begin
  AddCD(Button18);
end;

procedure TfrmMJManage.Button19Click(Sender: TObject);
begin
  AddCD(Button19);
end;

procedure TfrmMJManage.Button20Click(Sender: TObject);
begin
  AddCD(Button20);
end;

procedure TfrmMJManage.Button21Click(Sender: TObject);
begin
  AddCD(Button21);
end;

procedure TfrmMJManage.Button22Click(Sender: TObject);
begin
  AddCD(Button22);
end;

procedure TfrmMJManage.Button23Click(Sender: TObject);
begin
  AddCD(Button23);
end;

procedure TfrmMJManage.Button24Click(Sender: TObject);
begin
  AddCD(Button24);
end;

procedure TfrmMJManage.Button25Click(Sender: TObject);
begin
  AddCD(Button25);
end;

procedure TfrmMJManage.Button26Click(Sender: TObject);
begin
  AddCD(Button26);
end;

procedure TfrmMJManage.Button27Click(Sender: TObject);
begin
  AddCD(Button27);
end;

procedure TfrmMJManage.Button28Click(Sender: TObject);
begin
  AddCD(Button28);
end;

procedure TfrmMJManage.Button29Click(Sender: TObject);
begin
  AddCD(Button29);
end;

procedure TfrmMJManage.Button30Click(Sender: TObject);
begin
  AddCD(Button30);
end;

procedure TfrmMJManage.Button31Click(Sender: TObject);
begin
  AddCD(Button31);
end;

procedure TfrmMJManage.Button32Click(Sender: TObject);
begin
  AddCD(Button32);
end;

procedure TfrmMJManage.Button33Click(Sender: TObject);
begin
  AddCD(Button33);
end;

procedure TfrmMJManage.Button34Click(Sender: TObject);
begin
  AddCD(Button34);
end;

procedure TfrmMJManage.Button35Click(Sender: TObject);
begin
  AddCD(Button35);
end;

procedure TfrmMJManage.Button36Click(Sender: TObject);
begin
  AddCD(Button36);
end;

procedure TfrmMJManage.Button38Click(Sender: TObject);
begin
  FColumn:='';
  FInt:=0;
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='WFBBZ';
      flagname:='班组';
      if ShowModal=1 then
      begin
        Self.MJBanZu.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmMJManage.Button1Click(Sender: TObject);
begin
  AddSL(Button1);
end;

procedure TfrmMJManage.MJMaoZClick(Sender: TObject);
begin
  FInt:=1;
  FColumn:='';
end;

procedure TfrmMJManage.MJLenClick(Sender: TObject);
begin
  FInt:=2;
  FColumn:='';
end;

procedure TfrmMJManage.MJFKClick(Sender: TObject);
begin
  FInt:=3;
  FColumn:='';
end;

procedure TfrmMJManage.Button2Click(Sender: TObject);
begin
  AddSL(Button2);
end;

procedure TfrmMJManage.Button3Click(Sender: TObject);
begin
  AddSL(Button3);
end;

procedure TfrmMJManage.Button4Click(Sender: TObject);
begin
  AddSL(Button4);
end;

procedure TfrmMJManage.Button5Click(Sender: TObject);
begin
  AddSL(Button5);
end;

procedure TfrmMJManage.Button6Click(Sender: TObject);
begin
  AddSL(Button6);
end;

procedure TfrmMJManage.Button7Click(Sender: TObject);
begin
  AddSL(Button7);
end;

procedure TfrmMJManage.Button8Click(Sender: TObject);
begin
  AddSL(Button8);
end;

procedure TfrmMJManage.Button9Click(Sender: TObject);
begin
  AddSL(Button9);
end;

procedure TfrmMJManage.Button10Click(Sender: TObject);
begin
  AddSL(Button10);
end;

procedure TfrmMJManage.Button11Click(Sender: TObject);
begin
  AddSL(Button11);
end;

procedure TfrmMJManage.MJBanZuClick(Sender: TObject);
begin
  FInt:=4;
end;

procedure TfrmMJManage.MJSJKZClick(Sender: TObject);
begin
  FInt:=4;
end;

procedure TfrmMJManage.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  FInt:=4;
end;

procedure TfrmMJManage.Tv3CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  FInt:=4;
  PState:=0;
  InitCDGrid();
  with ADOTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from WFB_MJJY where MJID='''+Trim(CDS_MJID.fieldbyname('MJID').AsString)+'''');
    Open;
  end;
  SSetWinData(ADOTmp,Panel5);
  BtnStatus(False);
end;

procedure TfrmMJManage.FormClick(Sender: TObject);
begin
  FInt:=4;
end;

procedure TfrmMJManage.Button12Click(Sender: TObject);
var
  fsj:string;
begin
  if PState<1 then Exit;
  if Order_MJ.IsEmpty then Exit;
  //if MJMaoZ.Focused then
  if FInt=4 then Exit;
  if FInt=1 then
  begin
    if Trim(MJMaoZ.Text)<>'' then
      MJMaoZ.Text:=Copy(Trim(MJMaoZ.Text),1,Length(Trim(MJMaoZ.Text))-1);
  end else
  if FInt=2 then
  begin
    if Trim(MJLen.Text)<>'' then
      MJLen.Text:=Copy(Trim(MJLen.Text),1,Length(Trim(MJLen.Text))-1);
  end else
  if FInt=3 then
  begin
    if Trim(MJFK.Text)<>'' then
      MJFK.Text:=Copy(Trim(MJFK.Text),1,Length(Trim(MJFK.Text))-1);
  end else
  begin
    FColumn:=Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName;
    if Trim(FColumn)<>'' then
    begin
      if Trim(CDS_MJCD.FieldByName(FColumn).AsString)<>'' then
      begin
        with CDS_MJCD do
        begin
          Edit;
          if Length(CDS_MJCD.FieldByName(FColumn).AsString)=1 then
          begin
            FieldByName(FColumn).Value:=null ;
            FieldByName('CDQty').Value:=0;
          end
          else
          begin
            FieldByName(FColumn).Value:=Copy(Trim(FieldByName(FColumn).AsString),1,Length(Trim(FieldByName(FColumn).AsString))-1);
            FieldByName('CDQty').Value:=FieldByName(FColumn).Value;
          end;
          Post;
        end;
        //MValue:=TcxTextEdit(Tv2CDQty).EditingText;
        //BTPrint.SetFocus;
      end;

      
    end;
  end;
end;

procedure TfrmMJManage.Timer1Timer(Sender: TObject);
begin
  CDQty.Text:=Tv2.DataController.Summary.FooterSummaryTexts[0];
end;

procedure TfrmMJManage.Button39Click(Sender: TObject);
begin
  AddCD(Button39);
end;

procedure TfrmMJManage.Button40Click(Sender: TObject);
begin
  AddCD(Button40);
end;

procedure TfrmMJManage.Button41Click(Sender: TObject);
begin
  AddCD(Button41);
end;

procedure TfrmMJManage.Button42Click(Sender: TObject);
begin
  AddCD(Button42);
end;

procedure TfrmMJManage.Button43Click(Sender: TObject);
begin
  AddCD(Button43);
end;

procedure TfrmMJManage.Button44Click(Sender: TObject);
begin
    AddCD(Button44);
end;

procedure TfrmMJManage.Button45Click(Sender: TObject);
begin
    AddCD(Button45);
end;

procedure TfrmMJManage.Button46Click(Sender: TObject);
begin
    AddCD(Button46);
end;

procedure TfrmMJManage.Button47Click(Sender: TObject);
begin
    AddCD(Button47);
end;

procedure TfrmMJManage.Button48Click(Sender: TObject);
begin
   AddCD(Button48);
end;

procedure TfrmMJManage.Button49Click(Sender: TObject);
begin
  AddCD(Button49);
end;

procedure TfrmMJManage.Button50Click(Sender: TObject);
begin
  AddCD(Button50);
end;

procedure TfrmMJManage.MJMaoZChange(Sender: TObject);
var
  KZSX,KZXX:string;
begin
  if (Trim(MJMaoZ.Text)<>'') and (Trim(MJLen.Text)<>'') and (Trim(MJFK.Text)<>'') then
  begin
    MJSJKZ.Text:=FloatToStr(StrToFloat(MJMaoZ.Text)/(StrToFloat(MJLen.Text)*StrToFloat(MJFK.Text)/100)) ;
    MJSJKZ.Text:=FloatToStr(SSWR(StrToFloat(MJSJKZ.Text)*1000));
    KZSX:=Trim(Order_MJ.fieldbyname('MJKZD').AsString);
    KZXX:=Trim(Order_MJ.fieldbyname('MJKZX').AsString);
    if (StrToFloat(MJSJKZ.Text)>=StrToFloat(KZXX)) and (StrToFloat(MJSJKZ.Text)<=StrToFloat(KZSX)) then
    begin
      Label14.Visible:=True;
      Label14.Caption:='母卷克重正常。';
    end else
    if StrToFloat(MJSJKZ.Text)<StrToFloat(KZXX) then
    begin
      Label14.Visible:=True;
      Label14.Caption:='母卷克重不足<'+floattostr(StrToFloat(KZXX)-StrToFloat(MJSJKZ.Text))+'g/㎡';
    end else
    if StrToFloat(MJSJKZ.Text)>StrToFloat(KZSX) then
    begin
      Label14.Visible:=True;
      Label14.Caption:='母卷克重超限>'+floattostr(StrToFloat(MJSJKZ.Text)-StrToFloat(KZSX))+'g/㎡';
    end;
  end else
  begin
    MJSJKZ.Text:='';
    Label14.Caption:='';
    Label14.Visible:=False;
  end;

end;

function  TfrmMJManage.SaveData():Boolean;
var
  maxno,FMJID:String;
begin
  if PState=1 then
      FMJID:=''
    else if PState=2 then
     FMJID:=Trim(CDS_MJID.fieldbyname('MJID').AsString)
  else if PState<1 then Exit;
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from WFB_MJJY where MJId='''+Trim(FMJID)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMJID)='' then
      begin
        Append;
        if GetLSNo(ADOTmp,maxno,'','WFB_MJJY',2,1)=False then
        begin
          ADOCmd.Connection.RollbackTrans;
          Application.MessageBox('生成流水号异常！','提示',0);
          exit;
        end;
      end
      else begin
        maxno:=Trim(FMJID);
        Edit;
      end;
      FieldByName('MainId').value:=Trim(Order_MJ.fieldbyname('MainId').AsString);
      FieldByName('MJID').Value:=Trim(maxno);
      FieldByName('MJMaoZ').Value:=Trim(MJMaoZ.Text);
      FieldByName('MJLen').Value:=Trim(MJLen.Text);
      FieldByName('MJFK').Value:=Trim(MJFK.Text);
      FieldByName('MJSJKZ').Value:=Trim(MJSJKZ.Text);
      FieldByName('MJBanZu').Value:=Trim(MJBanZu.Text);
      if Trim(FMJID)='' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOTmp);
      end;
      Post;
    end;
    FMJID:=Trim(maxno);
    ///保存母卷疵点表
    with CDS_MJCD do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_MJCD.fieldbyname('MCID').AsString)='' then
        begin
          if GetLSNo(ADOTmp,maxno,'MC','WFB_MJJY_CD',3,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(CDS_MJCD.fieldbyname('MCID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from WFB_MJJY_CD  ');
          sql.Add(' where MCID='''+Trim(maxno)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(CDS_MJCD.fieldbyname('MCID').AsString)='' then
            Append
          else
            Edit;
          FieldByName('MJId').Value:=Trim(FMJID);
          FieldByName('MCID').Value:=Trim(maxno);
          SSetSaveDataCDSNew(ADOCmd,Tv2,CDS_MJCD,'WFB_MJJY_CD',0);
          Post;
        end;
        CDS_MJCD.Edit;
        CDS_MJCD.FieldByName('MCID').Value:=Trim(maxno);
        Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
    InitCDGridID();
    CDS_MJID.Locate('MJID',FMJID,[]);
    PState:=0;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmMJManage.BTAddClick(Sender: TObject);
begin
  if PState=1 then
  begin
    if( (CDS_MJCD.IsEmpty=False) or (Trim(MJMaoZ.Text)<>'') or (Trim(MJLen.Text)<>'') or (Trim(MJFK.Text)<>'') ) then
      if Application.MessageBox(PChar('    条码未打印，是否要新增?'+#13+#13+'单击"是(Y)"后当前界面的数据将会丢失！'),'提示',32+4)<>IDYES then Exit;
  end;
  if Order_MJ.IsEmpty then Exit;
  PState:=1;
  InitCDGrid();
  SClearData(Panel5,0);
  {Tv2.OptionsSelection.CellSelect:=True;
  MJMaoZ.ReadOnly:=False;
  MJLen.ReadOnly:=False;
  MJFK.ReadOnly:=False;
  Button38.Enabled:=True; }
  BtnStatus(True);
end;

procedure TfrmMJManage.BTEditClick(Sender: TObject);
begin
  if CDS_MJID.IsEmpty then Exit;
  PState:=2;
end;

procedure TfrmMJManage.BTPrintClick(Sender: TObject);
var
  fPrintFile:string;
begin
  if Order_MJ.IsEmpty then Exit;
  if Trim(MJMaoZ.Text)='' then
  begin
    Application.MessageBox('毛重不能为空！','提示',0);
    Exit;
  end;
  if Trim(MJLen.Text)='' then
  begin
    Application.MessageBox('长度不能为空！','提示',0);
    Exit;
  end;
  if Trim(MJFK.Text)='' then
  begin
    Application.MessageBox('幅宽不能为空！','提示',0);
    Exit;
  end;
  if Trim(MJBanZu.Text)='' then
  begin
    Application.MessageBox('班组不能为空！','提示',0);
    Exit;
  end;
  if PState=1 then
  begin
    if Application.MessageBox('确定要打印条码吗？打印后数据将不能修改！','提示',32+4)<>IDYES then Exit;
  end;
  if SaveData() then
  begin
    //Application.MessageBox('打印成功！','提示',0);
    BtnStatus(False);
    {Tv2.OptionsSelection.CellSelect:=False;
    MJMaoZ.ReadOnly:=True;
    MJLen.ReadOnly:=True;
    MJFK.ReadOnly:=True;
    Button38.Enabled:=False;}
  end;
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select RTrim(A.MJID) MJID,RTrim(A.MJSJKZ) MJSJKZ,RTrim(A.MJFK) MJFK,RTrim(B.OrderNo) OrderNo,RTrim(B.WFBCodeName) WFBCodeName from WFB_MJJY A inner join WFBOrder_Main B on A.MainId=B.MainId');
    sql.Add(' where A.MJID='''+Trim(CDS_MJID.fieldbyname('MJID').AsString)+'''');
    Open;
  end;
  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\母卷标签.rmf' ;
  if FileExists(fPrintFile) then
  begin
    RM2.LoadFromFile(fPrintFile);
    RM2.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\母卷标签.rmf'),'提示',0);
  end;
end;
procedure TfrmMJManage.BtnStatus(BSInt:Boolean);
begin
  Tv2.OptionsSelection.CellSelect:=BSInt;
  MJMaoZ.ReadOnly:=not BSInt;
  MJLen.ReadOnly:=not BSInt;
  MJFK.ReadOnly:=not BSInt;
  Button38.Enabled:=BSInt;
end;
procedure TfrmMJManage.Tv2CDQtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue:string;
begin
  try
    mvalue:=TcxTextEdit(Sender).EditingText;
    if Trim(mvalue)<>'' then
    begin
      CDS_MJCD.Edit;
      CDS_MJCD.FieldByName('CDQty').Value:=mvalue;
      CDS_MJCD.Post;
    end else
    begin
      CDS_MJCD.Edit;
      CDS_MJCD.FieldByName('CDQty').Value:=0;
      CDS_MJCD.Post;
    end;
    CDS_MJCD.Edit;
    CDS_MJCD.FieldByName('CDQtyS').Value:=mvalue;
  except
    Application.MessageBox('非法数字！','提示',0);
  end;
end;

end.
