unit U_JYOrderCDOne;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter,
  RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport, RM_e_Xls,
  Menus, MovePanel, cxTextEdit, cxContainer, cxCurrencyEdit,U_SMLB, BtnEdit,
  RM_e_main, RM_e_htm, RM_e_Graphic, RM_e_Jpeg, RM_e_txt, RM_e_csv,
  RM_E_llPDF, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmJYOrderCDOne = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNo: TEdit;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    conNO: TEdit;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    MJID: TEdit;
    Label2: TLabel;
    CDList: TEdit;
    Panel4: TPanel;
    Label11: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    RichEdit1: TRichEdit;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    Label4: TLabel;
    MJType: TComboBox;
    v1Column13: TcxGridDBColumn;
    Label5: TLabel;
    MPRTCodeName: TEdit;
    v1Column16: TcxGridDBColumn;
    Label6: TLabel;
    MJStr2: TComboBox;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    Label12: TLabel;
    Label13: TLabel;
    factoryName: TEdit;
    AOrddefstr6: TEdit;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    Filler: TComboBox;
    TBManage: TToolButton;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    TBZF: TToolButton;
    ToolButton1: TToolButton;
    RM2: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    v1Column25: TcxGridDBColumn;
    TBAftChkHX: TToolButton;
    v1Column24: TcxGridDBColumn;
    MovePanel2: TMovePanel;
    v1Column27: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    ToolButton2: TToolButton;
    v1Column14: TcxGridDBColumn;
    CDS_CD: TClientDataSet;
    RMCD: TRMGridReport;
    RMDBCD: TRMDBDataSet;
    CDS_LS: TClientDataSet;
    v1Column15: TcxGridDBColumn;
    Label14: TLabel;
    CheckBox1: TCheckBox;
    ToolButton3: TToolButton;
    Panel2: TPanel;
    Label15: TLabel;
    cxCurrencyEdit1: TcxCurrencyEdit;
    Button1: TButton;
    Button2: TButton;
    v1MKXS: TcxGridDBColumn;
    CheckBox2: TCheckBox;
    v1Column29: TcxGridDBColumn;
    v1Column30: TcxGridDBColumn;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    BAONO: TEdit;
    BAOID: TEdit;
    Label16: TLabel;
    Label17: TLabel;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    Mjstr4: TEdit;
    RCgangNO: TEdit;
    PrtColor: TEdit;
    Label18: TLabel;
    ComboBox1: TComboBox;
    RM3: TRMGridReport;
    v1Column26: TcxGridDBColumn;
    v1Column31: TcxGridDBColumn;
    ToolButton8: TToolButton;
    ToolButton9: TToolButton;
    v1Column32: TcxGridDBColumn;
    v1Column33: TcxGridDBColumn;
    Panel3: TPanel;
    Check_bq: TCheckBox;
    ComboBox2: TComboBox;
    ComboBox3: TComboBox;
    Fbq: TBtnEditA;
    check_Fbz: TCheckBox;
    Button3: TButton;
    Button4: TButton;
    KHConNO: TEdit;
    Label19: TLabel;
    v1Column34: TcxGridDBColumn;
    APID: TEdit;
    Label20: TLabel;
    v1Column35: TcxGridDBColumn;
    PRTKuanNO: TEdit;
    Label21: TLabel;
    v1Column36: TcxGridDBColumn;
    Label22: TLabel;
    ISHZ: TComboBox;
    ToolButton10: TToolButton;
//    RMllPDFExport1: TRMllPDFExport;
    v1Column37: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    ToolButton11: TToolButton;
    Panel5: TPanel;
    Label23: TLabel;
    Button5: TButton;
    Button6: TButton;
    edit1: TEdit;
    v1Column38: TcxGridDBColumn;
    note: TEdit;
    Label24: TLabel;
    v1Column39: TcxGridDBColumn;
    v1Column40: TcxGridDBColumn;
    Edit_MJQty3: TEdit;
    Label25: TLabel;
    Button7: TButton;
    Label26: TLabel;
    Button8: TButton;
    Edit_MJTypeOther: TComboBox;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Panel10MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Image2Click(Sender: TObject);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TBManageClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBZFClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
    procedure TBAftChkHXClick(Sender: TObject);
    procedure Aorddefstr1Change(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure PRTColorChange(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1Column15PropertiesEditValueChanged(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure FbqBtnClick(Sender: TObject);
    procedure MJIDKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton10Click(Sender: TObject);
    procedure APIDKeyPress(Sender: TObject; var Key: Char);
    procedure Button5Click(Sender: TObject);
    procedure ToolButton11Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
  private
    DQdate:TDateTime;
    FLeft,FTop:Integer;
    procedure InitGrid();
    procedure InitGrid10();
    procedure InitForm();
    function  Savedata():Boolean;
    procedure GetCDSData();
    procedure GetBGData();
    { Private declarations }
  public
    FFInt:Integer;
    { Public declarations }
  end;

var
  frmJYOrderCDOne: TfrmJYOrderCDOne;

implementation
uses
  U_DataLink,U_OrderInPut,U_Fun,U_MJEdit, U_CPDBAO, U_CPGangNo,
  U_JYMJIDPRINT, U_ZDYHelp;

{$R *.dfm}

procedure TfrmJYOrderCDOne.FormDestroy(Sender: TObject);
begin
  frmJYOrderCDOne:=nil;
end;

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

procedure TfrmJYOrderCDOne.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  DQdate:=SGetServerDate(ADOQueryTemp);
end;

procedure TfrmJYOrderCDOne.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('检验报告JF',Tv1,'检验管理');
end;

procedure TfrmJYOrderCDOne.InitGrid();
var
  i:integer;
  j:Integer;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('exec P_JYOrder_HZFX20 :begdate,:enddate,:PState,:FFSql ');
      Parameters.ParamByName('begdate').Value:=FormatDateTime('yyyy-MM-dd',BegDate.DateTime);
      Parameters.ParamByName('enddate').Value:=FormatDateTime('yyyy-MM-dd',enddate.DateTime+1);
      Parameters.ParamByName('PState').Value:=0;
      Parameters.ParamByName('FFSql').Value:=' ';
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
    TBFind.Click;
  end;
end;
procedure TfrmJYOrderCDOne.InitGrid10();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('exec P_JYOrder_HZFX :begdate,:enddate,:PState ');
      Parameters.ParamByName('begdate').Value:='2012-01-01';
      Parameters.ParamByName('enddate').Value:='2011-01-01';
      Parameters.ParamByName('PState').Value:=3;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmJYOrderCDOne.InitForm();
var
  fsj:String;
begin
  ReadCxGrid('检验报告JF',Tv1,'检验管理');
  fsj:='select distinct(Filler) name from WFB_MJJY   ';
  SInitComBoxBySql(ADOQueryCmd,Filler,False,fsj);
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-1;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  //InitGrid();
end;

procedure TfrmJYOrderCDOne.TBFindClick(Sender: TObject);
var
  fsj:String;
begin
  if ADOQueryMain.Active=False then Exit;

  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;


procedure TfrmJYOrderCDOne.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv1,ADOQueryMain,'检验报告');
end;

procedure TfrmJYOrderCDOne.TBRafreshClick(Sender: TObject);
begin
  OrderNo.SetFocus;
  MovePanel2.Left:=(self.Width-MovePanel2.Width) div 2;
  MovePanel2.Visible:=True;
  MovePanel2.Refresh;
  InitGrid();
  MovePanel2.Visible:=False;
end;

procedure TfrmJYOrderCDOne.OrderNoChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmJYOrderCDOne.FormShow(Sender: TObject);
begin
  InitForm();
  if Trim(DParameters2)='管理' then
  begin
    TBManage.Visible:=True;
    TBZF.Visible:=True;
    v1Column15.Options.Focusing:=True;
    ToolButton3.Visible:=true;
    //TBAftChkHX.Visible:=True;
  end;
  if Trim(DParameters2)='低权限' then
  begin
    TBManage.Visible:=True;
 //   TBZF.Visible:=True;
    v1Column15.Options.Focusing:=True;
    ToolButton3.Visible:=true;
    //TBAftChkHX.Visible:=True;
  end;
end;

procedure TfrmJYOrderCDOne.Panel10MouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  ReleaseCapture;
  TWinControl(Panel4).Perform(WM_SYSCOMMAND,$F012,0);
end;

procedure TfrmJYOrderCDOne.Image2Click(Sender: TObject);
begin
  Panel4.Visible:=False;
end;

procedure TfrmJYOrderCDOne.Tv1MouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FLeft:=X;
  FTop:=Y;
end;

procedure TfrmJYOrderCDOne.Tv1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  Panel4.Left:=FLeft;
  Panel4.Top:=FTop+110;
  Panel4.Visible:=True;
  Panel4.Refresh;
  Panel10.Caption:=Trim(TV1.Controller.FocusedColumn.Caption);
  RichEdit1.Text:=Order_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString;
end;

procedure TfrmJYOrderCDOne.TBManageClick(Sender: TObject);
begin
  IF Order_Main.IsEmpty then exit;
  if trim(Order_Main.fieldbyname('MJstr2').AsString)<>'未入库'then
  begin
    application.MessageBox('布匹已出入库，不能修改！','提示信息',MB_ICONERROR);
    exit;
  end;
  try
    frmMJEdit:=TfrmMJEdit.Create(Application);
    with frmMJEdit do
    begin
      MJID.Text:=Trim(Self.Order_Main.fieldbyname('MJID').AsString);
      with ADOTmp do
      begin
        Close;
        sql.Clear;
        SQL.Add('select A.*,AOrddefstr3=(select defstr1 from CK_SXPB_CR B where B.SPID=A.APID),  ');
        sql.Add('AOrddefstr2=(select QtyUnit from CK_SXPB_CR B where B.SPID=A.APID) from WFB_MJJY A');
        sql.Add(' where A.MJID='''+Trim(MJID.Text)+'''');
        Open;
      end;
      if ADOTmp.IsEmpty then
      begin
        MJID.Text:='';
        Label2.Caption:='';
        Label2.Visible:=False;
        Application.MessageBox('条码错误!','提示',0);
        Exit;
      end;
      MJXH.Text:=Trim(ADOTmp.fieldbyname('MJXH').AsString);
      MJstr4.Text:=Trim(ADOTmp.fieldbyname('MJstr4').AsString);
      MJMaoZ.Text:=Trim(ADOTmp.fieldbyname('MJMaoZ').AsString);
      MJLen.Text:=Trim(ADOTmp.fieldbyname('MJLen').AsString);
      MJQty1.Text:=Trim(ADOTmp.fieldbyname('MJQty1').AsString);
      MJQty2.Text:=Trim(ADOTmp.fieldbyname('MJQty2').AsString);
      MJQty4.Text:=Trim(ADOTmp.fieldbyname('MJQty4').AsString);
      MJFK.Text:=Trim(ADOTmp.fieldbyname('MJFK').AsString);
      MJSJKZ.Text:=Trim(ADOTmp.fieldbyname('MJSJKZ').AsString);
      M.Caption:=Trim(Trim(ADOTmp.fieldbyname('MJTypeOther').AsString));
      Ma.Caption:=Trim(Trim(ADOTmp.fieldbyname('MJStr1').AsString));
      if Trim(m.Caption)='' then
      begin
        m.Caption:=Trim(ADOTmp.fieldbyname('AOrddefstr3').AsString);
      end;
      if Trim(Ma.Caption)='' then
      begin
        Ma.Caption:=Trim(ADOTmp.fieldbyname('AOrddefstr2').AsString);
      end;
      if Trim(Trim(ADOTmp.fieldbyname('MJType').AsString))='正品' then
      begin
        RadioGroup1.ItemIndex:=0
      end else
      if Trim(Trim(ADOTmp.fieldbyname('MJType').AsString))='次品' then
      begin
        RadioGroup1.ItemIndex:=1;
      end else
      if Trim(Trim(ADOTmp.fieldbyname('MJType').AsString))='留样' then
      begin
        RadioGroup1.ItemIndex:=2;
      end;
      with ADOTmp do
      begin
        Close;
        sql.Clear;
        SQL.Add('select * from WFB_MJJY_CD A   where A.MJID='''+Trim(MJID.Text)+'''');
        Open;
      end;
      Label2.Caption:=Trim(MJID.Text);
      Label2.Visible:=True;
      MJID.Text:='';
      SCreateCDS20(ADOTmp,Order_MJ);
      SInitCDSData20(ADOTmp,Order_MJ);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmMJEdit.Free;
  end;
end;

procedure TfrmJYOrderCDOne.N1Click(Sender: TObject);
var
  i:integer;
begin
  SelOKNo(Order_Main,true);
{ Screen.Cursor := crHourGlass;
 tv1.DataController.GotoFirst;
 for i := 0 to tv1.DataController.FilteredRecordCount - 1 do
 begin
   tv1.DataController.GetItemByFieldName('ssel').EditValue  := True;
   tv1.DataController.GotoNext;
 end;
 Screen.Cursor := crDefault;   }
end;

procedure TfrmJYOrderCDOne.N2Click(Sender: TObject);
var
  i:integer;
begin
    SelOKNo(Order_Main,false);
 {Screen.Cursor := crHourGlass;
 tv1.DataController.GotoFirst;
 tv1.DataController.Summary.SummaryGroups .
 for i := 0 to tv1.DataController.FilteredRecordCount  tv1.da- 1 do
 begin
   tv1.DataController.GetItemByFieldName('ssel').EditValue  := false;
   tv1.DataController.GotoNext;
 end;
 Screen.Cursor := crDefault; }
end;

procedure TfrmJYOrderCDOne.TBZFClick(Sender: TObject);
begin
  if Order_Main.Locate('ssel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要作废数据吗？作废后数据不能恢复。','提示',32+4)<>IDYES then  Exit;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean=True then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_BanCP_CR where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty=False then
        begin
          Order_Main.EnableControls;
          Application.MessageBox('已产生入库数据不能删除!','提示',0);
          Exit;
        end else
        begin
          //begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('delete WFB_MJJY where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
              sql.Add('delete WFB_MJJY_CD where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
              ExecSQL;
            end;
         // end;
        end;
        Order_Main.Delete;
      end else
        Next;
    end;
  end;
  Order_Main.EnableControls;

end;

procedure TfrmJYOrderCDOne.ToolButton1Click(Sender: TObject);
var
  fPrintFile:string;
   Txt,fImagePath:string;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean=True then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update WFB_MJJY Set PrtAgnFlag=1,PrtAgnDate=getdate(),PrtAgnPerson='''+Trim(DName)+'''');
          sql.Add(' where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryPrint do
        begin
          Close;
          SQL.Clear;
    sql.Add('select QtyUnit=A.MJTypeOther,B.orderNo,B.conNo,D.GangNo,PRTColorEng=E.Note,B.LBName,B.NLBName ');
    sql.Add(',khConNo=(select top 1 khConNo from JYOrderCon_Main X where X.ConNO=B.conNO) ');
    sql.Add(',MPRTCodeNameEng=ISNULL((select Top 1 F.note from KH_Zdy F where F.zdyname=B.MPRTCodeName and F.Type=''PRTCodeName''),B.MPRTCodeName)');
    sql.Add(',B.MPRTCF,B.MPRTMF,B.MPRTKZ,C.SOrddefstr4,B.MPRTCodeName,B.MPRTSpec,B.MPRTKuanNO,D.note as AOrdDefNote1,C.*,A.* ');
    sql.Add(',Case when A.MJSJKZ*A.MJFK<>0 then Cast(A.MJQty4*1000/A.MJSJKZ/(A.MJFK/100) as int) else 0 end as MQty');
    sql.Add(' from WFB_MJJY A') ;
    sql.Add(' inner join JYOrder_Main B On A.Mainid=B.Mainid');
    sql.Add(' inner join JYOrder_Sub C on A.SubId=C.SubId');
    sql.Add(' left join KH_Zdy E on C.PRTColor=E.ZdyName and E.Type=''OrdColor'' ');
          sql.Add(' inner join CK_SXPB_CR D on A.APId=D.SPId ');
          SQL.Add(' where A.MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
          Open;
        end;
        try
          Moudle:=LoadLibrary('MakeQRBarcode.dll');
          @Makebar:=GetProcAddress(Moudle,'Make');
          @Mixtext:=GetProcAddress(Moudle,'MixText');
          Txt:=Trim(ADOQueryPrint.fieldbyname('MJID').AsString);
          fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp.bmp' ;
          if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName)+'image')) then
          CreateDirectory(pchar(ExtractFilePath(Application.ExeName)+'image'),nil);
          if FileExists(fImagePath) then DeleteFile(fImagePath);
          Makebar(pchar(Txt),Length(Txt),3,3,0,PChar(fImagePath),3);
        except
          application.MessageBox('条形码生成失败！','提示信息',MB_ICONERROR);
         exit;
        end;
        if Trim(ADOQueryPrint.fieldbyname('LbName').AsString)<>'' then
          fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\'+Trim(ADOQueryPrint.fieldbyname('LbName').AsString)+'.rmf'
        else
          fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\通用英文标签.rmf' ;
        //  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\15029外唛.rmf' ;
        if FileExists(fPrintFile) then
        begin
            RMVariables['QRBARCODE']:=fImagePath;
            RM2.LoadFromFile(fPrintFile);
            Rm2.DefaultCopies:=strtointdef(trim(ComboBox1.Text),1);
          //  RM2.ShowReport;
            RM2.PrintReport;
        {  IF Trim(ADOQueryPrint.fieldbyname('NLBName').AsString)<>'' then
          begin
             fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\'+Trim(ADOQueryPrint.fieldbyname('NLBName').AsString)+'.rmf';
             if FileExists(fPrintFile) then
             begin
              RMVariables['QRBARCODE']:=fImagePath;
              RM3.LoadFromFile(fPrintFile);
              RM3.DefaultCopies:=1;
            //  RM3.ShowReport;
              RM3.PrintReport;
             end;
           end;  }
        end
        else
        begin
          Order_Main.EnableControls;
          Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\卷标签.rmf'),'提示',0);
          Exit;
        end;
      end;
      Next;
    end;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmJYOrderCDOne.RadioGroup1Click(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmJYOrderCDOne.TBAftChkHXClick(Sender: TObject);
var
  fsj,fsj10:String;
begin
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean=True then
      begin
        if Trim(fsj)='' then
        begin
          fsj:=Trim(Order_Main.fieldbyname('SubId').AsString);
        end
        else
        begin
          if Trim(fsj)<>Trim(Order_Main.fieldbyname('SubId').AsString) then
          begin
            Order_Main.EnableControls;
            Application.MessageBox('不同产品不能一起回修!','提示',0);
            Exit;
          end;
        end;
      end;
      Next;
    end;
  end;
  Order_Main.EnableControls;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean=True then
      begin
        if Trim(fsj10)='' then
        begin
          fsj10:=Trim(Order_Main.fieldbyname('AOrddefstr4').AsString);
        end
        else
        begin
          if Trim(fsj10)<>Trim(Order_Main.fieldbyname('AOrddefstr4').AsString) then
          begin
            Order_Main.EnableControls;
            Application.MessageBox('不同染厂不能一起回修!','提示',0);
            Exit;
          end;
        end;
      end;
      Next;
    end;
  end;
  Order_Main.EnableControls;

end;
function TfrmJYOrderCDOne.Savedata():Boolean;
var
  maxno,LLID:string;
  HXPS:Integer;
  HXQty,HXMQty:Double;
begin
  HXPS:=0;
  HXQty:=0;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Top 1 * from Contract_Cloth_LL where OrdSubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
    sql.Add(' and FirstName='''+Trim(Order_Main.fieldbyname('AOrddefstr4').AsString)+'''');
    Open;
  end;
  LLID:=Trim(ADOQueryTemp.fieldbyname('LLID').AsString);
  if Trim(LLID)='' then
  begin
    Result:=False;
    Application.MessageBox('没有相对应的加工厂!','提示',0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxno,'HX','Contract_Cloth_BefChkHX',2,1)=False then
    begin
      Result:=False;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取回修最大号失败!','提示',0);
      Exit;
    end;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean=True then
        begin
          HXPS:=HXPS+1;
          HXQty:=HXQty+Order_Main.FieldByName('MJLen').Value;
        end;
        Next;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('');
      Open;
    end;
  except
  end;
end;

procedure TfrmJYOrderCDOne.Aorddefstr1Change(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmJYOrderCDOne.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(OrderNo.Text)<4 then Exit;
    MovePanel2.Visible:=True;
    MovePanel2.Refresh;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('exec P_JYOrder_HZFX20 :begdate,:enddate,:PState,:FFSql ');
        Parameters.ParamByName('begdate').Value:='2010-01-01';
        Parameters.ParamByName('enddate').Value:='2050-01-01';
        Parameters.ParamByName('PState').Value:=3;
        Parameters.ParamByName('FFSql').Value:=' where CC.OrderNo like '''+'%'+Trim(OrderNo.Text)+'%'+'''';
        //ShowMessage(sql.Text);
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
      if Order_Main.IsEmpty=False then
      begin
        //InitOrderColor(Trim(Order_Main.fieldbyname('MainId').AsString),PRTColor,ADOQueryTemp);
       // InitBCGangNo(Trim(Order_Main.fieldbyname('SubId').AsString),AOrdDefStr1,ADOQueryTemp);
      //  InitRCGangNo(Trim(Order_Main.fieldbyname('SubId').AsString),GangNo,ADOQueryTemp);
      end;
    finally
      ADOQueryMain.EnableControls;
    end;
    MovePanel2.Visible:=False;
  end;


end;

procedure TfrmJYOrderCDOne.PRTColorChange(Sender: TObject);
begin
  TBFind.Click;
  if Order_Main.IsEmpty=False then
  begin
    //InitOrderColor(Trim(Order_Main.fieldbyname('MainId').AsString),PRTColor,ADOQueryTemp);
   // InitBCGangNo(Trim(Order_Main.fieldbyname('SubId').AsString),AOrdDefStr1,ADOQueryTemp);
  end;
end;

procedure TfrmJYOrderCDOne.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if Order_Main.IsEmpty=False then
  begin
  //  InitOrderColor(Trim(Order_Main.fieldbyname('MainId').AsString),PRTColor,ADOQueryTemp);
  //  InitBCGangNo(Trim(Order_Main.fieldbyname('SubId').AsString),AOrdDefStr1,ADOQueryTemp);
  //  InitRCGangNo(Trim(Order_Main.fieldbyname('SubId').AsString),GangNo,ADOQueryTemp);
  end;
end;
procedure TfrmJYOrderCDOne.GetCDSData();
begin
  CDS_CD.FieldByName('MJXH').Value:=CDS_LS.fieldbyname('MJXH').Value;
  CDS_CD.FieldByName('GangNo').Value:=CDS_LS.fieldbyname('Aorddefstr1').Value;
  CDS_CD.FieldByName('PRTColor').Value:=CDS_LS.fieldbyname('PRTColor').Value;
  CDS_CD.FieldByName('PRTEColor').Value:=CDS_LS.fieldbyname('SOrddefstr4').Value;
  CDS_CD.FieldByName('MJLen').Value:=CDS_LS.fieldbyname('MJLen').Value;
  CDS_CD.FieldByName('MJJZ').Value:=CDS_LS.fieldbyname('MJQty4').Value;
  CDS_CD.FieldByName('MJMZ').Value:=CDS_LS.fieldbyname('MJMaoZ').Value;
  CDS_CD.FieldByName('MJFK').Value:=CDS_LS.fieldbyname('MJFK').Value;
  CDS_CD.FieldByName('KFQtyHZ').Value:=CDS_LS.fieldbyname('KFQtyHZ').Value;
  CDS_CD.FieldByName('DengJi').Value:=CDS_LS.fieldbyname('DengJi').Value;
  CDS_CD.FieldByName('PanDing').Value:=CDS_LS.fieldbyname('PanDing').Value;
  CDS_CD.FieldByName('MJID').Value:=CDS_LS.fieldbyname('MJID').Value;
end;
procedure TfrmJYOrderCDOne.ToolButton2Click(Sender: TObject);
var
  fPrintFile:string;
begin
  GetBGData();
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\检验报告.rmf' ;
  if FileExists(fPrintFile) then
  begin
    RMCD.LoadFromFile(fPrintFile);
    RMVariables['OrderNo']:=Trim(Order_Main.fieldbyname('OrderNo').AsString);
    RMVariables['CustomerNoName']:=Trim(Order_Main.fieldbyname('CustomerNoName').AsString);
    RMVariables['MPRTCodeName']:=Trim(Order_Main.fieldbyname('MPRTCodeName').AsString);
    RMVariables['MPRTMF']:=Trim(Order_Main.fieldbyname('MPRTMF').AsString);
    RMVariables['JGC']:=Trim(Order_Main.fieldbyname('AOrddefstr4').AsString);
    RMVariables['PBC']:=Trim(Order_Main.fieldbyname('AOrddefstr6').AsString);
    RMCD.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\检验报告.rmf'),'提示',0);
  end;
end;
procedure TfrmJYOrderCDOne.GetBGData();
var
  FMJId:string;
  i,j,z:Integer;
begin

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select MJXH=Cast(0 as int),GangNo=Cast('''' as varchar(20)),PRTColor=Cast('''' as varchar(20)),MJID=Cast('''' as varchar(20))');
    sql.Add(',PRTEColor=Cast('''' as varchar(20)),MJLen=Cast(0 as decimal(18,2)),MJJZ=Cast(0 as decimal(18,2)),XH=Cast(0 as int)');
    sql.Add(',MJMZ=Cast(0 as decimal(18,2)),MJFK=Cast(0 as decimal(18,2)),KFQtyHZ=Cast(0 as int),DengJi=Cast('''' as varchar(20)),PanDing=Cast('''' as varchar(20))');
    sql.Add(',CDName0=Cast('''' as varchar(20)),CDBeg0=Cast('''' as varchar(20)),KFQty0=Cast(0 as int)');
    sql.Add(',CDName1=Cast('''' as varchar(20)),CDBeg1=Cast('''' as varchar(20)),KFQty1=Cast(0 as int)');
    sql.Add(',CDName2=Cast('''' as varchar(20)),CDBeg2=Cast('''' as varchar(20)),KFQty2=Cast(0 as int)');
    sql.Add(',CDName3=Cast('''' as varchar(20)),CDBeg3=Cast('''' as varchar(20)),KFQty3=Cast(0 as int)');
    sql.Add(',CDName4=Cast('''' as varchar(20)),CDBeg4=Cast('''' as varchar(20)),KFQty4=Cast(0 as int)');
    sql.Add(',CDName5=Cast('''' as varchar(20)),CDBeg5=Cast('''' as varchar(20)),KFQty5=Cast(0 as int)');
    sql.Add(',CDName6=Cast('''' as varchar(20)),CDBeg6=Cast('''' as varchar(20)),KFQty6=Cast(0 as int)');
    sql.Add(',CDName7=Cast('''' as varchar(20)),CDBeg7=Cast('''' as varchar(20)),KFQty7=Cast(0 as int)');
    sql.Add(',CDName8=Cast('''' as varchar(20)),CDBeg8=Cast('''' as varchar(20)),KFQty8=Cast(0 as int)');
    sql.Add(',CDName9=Cast('''' as varchar(20)),CDBeg9=Cast('''' as varchar(20)),KFQty9=Cast(0 as int)');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_CD);
  SInitCDSData20(ADOQueryTemp,CDS_CD);
  if CDS_CD.IsEmpty=False then CDS_CD.Delete;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.*,Case when isnull(MJType,'''')=''正品'' and KFQtyHZ<26 then ''A''    ');
    sql.Add(' when isnull(MJType,'''')=''正品'' and KFQtyHZ>33 then ''C''');
    sql.Add(' when isnull(MJType,'''')=''正品'' and KFQtyHZ>25 and A.KFQtyHZ<34  then ''B''');
    sql.Add(' else MJType end as DengJi');
    sql.Add('from (select A.CDBeg,A.CDName,A.KouFenQty,B.MJXH,B.MJstr4 as Aorddefstr1,JS.PRTColor,JS.SOrddefstr4,B.MJID ');
    sql.Add(',CDEName=(select Note from KH_Zdy KZ where KZ.ZdyName=A.CDName and KZ.Type=''WFBCD'' )');
    sql.Add(',B.MJLen,B.MJQty4,B.MJMaoZ,B.MJTypeOther,B.MJSJKZ,B.MJFK,B.MJType,B.PanDing');
    sql.Add(',KFQtyHZ=(select Sum(KouFenQty) from WFB_MJJY_CD MCD where MCD.MJID=B.MJID)');
    sql.Add(' from WFB_MJJY B inner join WFB_MJJY_CD A on B.MJID=A.MJID');
//    sql.Add(' inner join JYOrder_Sub_AnPai C on B.APID=C.APID');
    sql.Add(' inner join JYOrder_Sub JS on B.SubId=JS.SubId');
    SQL.Add(' where B.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    if Trim(MJType.Text)<>'' then
    begin
      sql.Add(' and isnull(B.MJType,'''')='''+Trim(MJType.Text)+'''');
    end;

    sql.Add(')A order by PRTColor,Cast(Aorddefstr1 as int),MJID,MJXH');
    Open;
  end;
  //ShowMessage(ADOQueryTemp.SQL.Text);
  //ShowMessage(IntToStr(ADOQueryTemp.RecordCount));
  SCreateCDS20(ADOQueryTemp,CDS_LS);
  SInitCDSData20(ADOQueryTemp,CDS_LS);
  CDS_LS.DisableControls;
  with CDS_LS do
  begin
    First;
    while not Eof do
    begin
      if CDS_CD.IsEmpty then
      begin
        with CDS_CD do
        begin
          Append;
          GetCDSData();
          if CheckBox1.Checked=True then
            FieldByName('CDName0').Value:=CDS_LS.fieldbyname('CDEName').Value
          else
            FieldByName('CDName0').Value:=CDS_LS.fieldbyname('CDName').Value;
          FieldByName('CDBeg0').Value:=CDS_LS.fieldbyname('CDBeg').Value;
          FieldByName('KFQty0').Value:=CDS_LS.fieldbyname('KouFenQty').Value;
          FieldByName('XH').Value:=0;
          Post;
          FMJId:=Trim(CDS_LS.fieldbyname('MJID').AsString);
          i:=0;//列号
          j:=0;
          z:=0;//行号
        end;
      end else
      begin
        if Trim(CDS_LS.fieldbyname('MJID').AsString)=FMJId then
        begin
          i:=i+1;
          if i<10 then
          begin
            with CDS_CD do
            begin
              Edit;
              GetCDSData();
              if CheckBox1.Checked=True then
                FieldByName('CDName'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('CDEName').Value
              else
                FieldByName('CDName'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('CDName').Value;
              FieldByName('CDBeg'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('CDBeg').Value;
              FieldByName('KFQty'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('KouFenQty').Value;
              Post;
            end;
          end else
          begin
            i:=0;
            with CDS_CD do
            begin
              Append;
              GetCDSData();
              if CheckBox1.Checked=True then
                FieldByName('CDName'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('CDEName').Value
              else
                FieldByName('CDName'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('CDName').Value;
              FieldByName('CDBeg'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('CDBeg').Value;
              FieldByName('KFQty'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('KouFenQty').Value;
              Post;
            end;
          end;
        end else
        begin
          i:=0;
          with CDS_CD do
          begin
            Append;
            GetCDSData();
            if CheckBox1.Checked=True then
              FieldByName('CDName'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('CDEName').Value
            else
              FieldByName('CDName'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('CDName').Value;
            FieldByName('CDBeg'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('CDBeg').Value;
            FieldByName('KFQty'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('KouFenQty').Value;
            Post;
          end;
          FMJId:=Trim(CDS_LS.fieldbyname('MJID').AsString);
        end;
      end;
      Next;
    end;
  end;
  CDS_LS.EnableControls;
end;

procedure TfrmJYOrderCDOne.v1Column15PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with Order_Main do
  begin
    Edit;
    FieldByName('PanDing').Value:=Trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate WFB_MJJY Set PanDing='''+Trim(mvalue)+'''');
    SQL.Add(' where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmJYOrderCDOne.Button2Click(Sender: TObject);
begin
  Panel2.Visible:=false;
end;

procedure TfrmJYOrderCDOne.Button1Click(Sender: TObject);
var
  i:string;
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  i:=floattostr(strtofloatdef(trim(cxCurrencyEdit1.Text),0));
  try
    Order_Main.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
      if Order_Main.FieldByName('SSel').AsBoolean=True then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update WFB_MJJY Set MKXs='''+trim(i)+''',MJLen=cast(1.00000* MJQty4 *'''+trim(i)+''' as decimal(18,1))  ');
          sql.Add('where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
          ExecSQL;
        end;
      end;
      Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存成功！','提示信息');
    Panel2.Visible:=false;
    TBRafresh.Click;
    exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存失败！','提示信息',0);
  end;
end;

procedure TfrmJYOrderCDOne.ToolButton3Click(Sender: TObject);
begin
  Panel2.Left:=(self.Width-Panel2.Width) div 2;
  Panel2.Visible:=true;
end;

procedure TfrmJYOrderCDOne.CheckBox2Click(Sender: TObject);
var
  i:integer;
begin
  SelOKNo(Order_Main,checkbox2.Checked);
 {Screen.Cursor := crHourGlass;
 tv1.DataController.GotoFirst;
 for i := 0 to tv1.DataController.FilteredRecordCount - 1 do
 begin
   tv1.DataController.GetItemByFieldName('ssel').EditValue  := checkbox2.Checked;
   tv1.DataController.GotoNext;
 end;
 Screen.Cursor := crDefault;
 }
end;

procedure TfrmJYOrderCDOne.ToolButton4Click(Sender: TObject);
begin
  frmCPDBao:=TfrmCPDBao.Create(self);
  with frmCPDBao do
  begin
    if showmodal=1 then
    begin
     // InitGrid();
    end;
  end;
  frmCPDBao.Release;
end;

procedure TfrmJYOrderCDOne.ToolButton6Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  try
    Order_Main.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean=True then
        begin
          IF trim(fieldbyname('mjstr2').AsString)<>'未入库' then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Order_Main.EnableControls;
            application.MessageBox('此条码已入库不能撤销打包!','提示信息',MB_ICONERROR);
            exit;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update WFB_MJJY Set baoID='''',baoNo=''''  ');
            sql.Add('where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
            ExecSQL;
          end;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据撤销成功！','提示信息');
    TBRafresh.Click;
    exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据撤销失败！','提示信息',0);
  end;
end;

procedure TfrmJYOrderCDOne.ToolButton7Click(Sender: TObject);
begin
  frmCPGangNo:=TfrmCPGangNo.Create(self);
  with frmCPGangNo do
  begin
    show;
  {  if showmodal=1 then
    begin
     // InitGrid();
    end;  }
  end;
  //frmCPGangNo.Free;
end;

procedure TfrmJYOrderCDOne.ToolButton8Click(Sender: TObject);
begin
  IF Order_Main.IsEmpty then exit;
  frmJYMJIDPRINT:=TfrmJYMJIDPRINT.create(self);
  with frmJYMJIDPRINT do
  begin
    fMJID:=trim(Order_Main.fieldbyname('MJID').AsString);
    show;
  end;
end;

procedure TfrmJYOrderCDOne.ToolButton9Click(Sender: TObject);
begin
  frmSMLB:=TfrmSMLB.create(self);
  with frmSMLB do
  begin
    show;
  end;
end;

procedure TfrmJYOrderCDOne.Button4Click(Sender: TObject);
begin
  Panel3.Visible:=false;
end;

procedure TfrmJYOrderCDOne.Button3Click(Sender: TObject);
var fPrintFile: string;
    Txt,fImagePath:string;
    Moudle: THandle;
    Makebar:TMakebar;
    Mixtext:TMixtext;
    FBaoID:string;
begin
  if Order_Main.IsEmpty then Exit;
  if trim(Order_Main.FieldByName('BaoID').AsString)='' then
  begin
    application.MessageBox('未打包不能打印','提示');
    exit;
  end;

  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.Baoid,A.BaoNo,A.MJTypeOther as QtyUnit,D.OrderNo,D.conNo,D.customerNoName,D.MprtCodeName,D.Mprtspec,D.OrdPerson1,D.MPRTKuanNO,');
    sql.Add('PRTColor=DBO.F_Get_Order_SubStr(BaoID,''BNColor''),SOrddefstr1=DBO.F_Get_Order_SubStr(BaoID,''BNSOrddefstr1''),');
    sql.Add('SOrddefstr4=DBO.F_Get_Order_SubStr(BaoID,''BNSOrddefstr4''),PRtHX=DBO.F_Get_Order_SubStr(BaoID,''BNPRtHX''), ');
    sql.Add('Mjstr4=DBO.F_Get_Order_SubStr(BaoID,''BNGangNo''), ');
  //  sql.Add('BNMJLENLIST=DBO.F_Get_Order_SubStr(BaoID,''BNMJLENLIST''), ');
    sql.Add('PRTkuanNo=DBO.F_Get_Order_SubStr(BaoID,''BNPrtkuanno''),');
    sql.Add('khConNo=(select top 1 khConNo from JYOrderCon_Main X where X.ConNO=D.conNO), ');
    sql.Add('MprtCodeNameEng=(select top 1 Note from KH_Zdy X where X.zdyName=D.MprtCodeName), ');
    SQL.ADD('count(A.MJID) as JSl,sum(A.MJMaoZ) MJMAOZ,sum(MJQty3) as MJQty3,sum(MJQty4) as MJQty4,SUM(A.MJLen)as MJLen');
    sql.Add('from WFB_MJJY A');
    sql.Add(' inner join JYOrder_Sub C on C.SubID=A.SubID');
    sql.Add(' inner join JYOrder_Main D on D.MainID=A.MainID');
    SQL.Add(' where A.BaoID='''+Trim(Order_Main.fieldbyname('BaoID').AsString)+'''');
    SQL.ADD('group by A.Baoid,A.BaoNo,A.MJTypeOther,D.OrderNo,D.conNo,D.customerNoName,D.MprtCodeName,D.Mprtspec,D.OrdPerson1,D.MPRTKuanNO');
    Open;
  end;

  if ADOQueryPrint.RecordCount>1 then
  begin
    Application.MessageBox('打包错误，请重新打包!','提示',0);
    Exit;
  end;

  try
    Moudle:=LoadLibrary('MakeQRBarcode.dll');
    @Makebar:=GetProcAddress(Moudle,'Make');
    @Mixtext:=GetProcAddress(Moudle,'MixText');
    Txt:=trim(ADOQueryPrint.fieldbyname('baoID').AsString);
    fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp.bmp';
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName)+'image')) then
    CreateDirectory(pchar(ExtractFilePath(Application.ExeName)+'image'),nil);
    if FileExists(fImagePath) then DeleteFile(fImagePath);
    Makebar(pchar(Txt),Length(Txt),3,3,0,PChar(fImagePath),3);
  except
    application.MessageBox('条形码生成失败！','提示信息',MB_ICONERROR);
    exit;
  end;

  IF Check_bq.Checked then
  begin
    fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\包标签.rmf';

    if FileExists(fPrintFile) then
    begin
      RMVariables['QRBARCODE']:=fImagePath;
      RM2.DefaultCopies:=strtointdef(trim(ComboBox2.Text),1);
      RM2.LoadFromFile(fPrintFile);
     //   RM2.ShowReport;
      RM2.printReport;
    end
    else
    begin
 //   Order_Main.EnableControls;
      Application.MessageBox(PChar('没有找'+fPrintFile),'提示',0);
      Exit;
    end;
  end;

  IF check_Fbz.Checked then
  begin
    fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\'+Trim(Fbq.Text)+'.rmf' ;

    if FileExists(fPrintFile) then
    begin
      RMVariables['QRBARCODE']:=fImagePath;
      RM2.DefaultCopies:=strtointdef(trim(ComboBox3.Text),1);
      RM2.LoadFromFile(fPrintFile);
   //   RM2.ShowReport;
      RM2.printReport;
    end
    else
    begin
 //   Order_Main.EnableControls;
      Application.MessageBox(PChar('没有找'+fPrintFile),'提示',0);
      Exit;
    end;
  end;

end;

procedure TfrmJYOrderCDOne.ToolButton5Click(Sender: TObject);
begin
  Panel3.Left:=(self.Width-Panel3.Width) div 2;
  Panel3.Visible:=true;
end;

procedure TfrmJYOrderCDOne.FbqBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='BAOFFLAG';
      flagname:='包副标签';
      if ShowModal=1 then
      begin
        Fbq.Text:=trim(ClientDataSet1.fieldbyname('zdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmJYOrderCDOne.MJIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(MJID.Text)<4 then Exit;
    MovePanel2.Visible:=True;
    MovePanel2.Refresh;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('exec P_JYOrder_HZFX20 :begdate,:enddate,:PState,:FFSql ');
        Parameters.ParamByName('begdate').Value:='2010-01-01';
        Parameters.ParamByName('enddate').Value:='2050-01-01';
        Parameters.ParamByName('PState').Value:=3;
        Parameters.ParamByName('FFSql').Value:=' where A.MJID like '''+'%'+Trim(MJID.Text)+'%'+'''';
        //ShowMessage(sql.Text);
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
      if Order_Main.IsEmpty=False then
      begin
        //InitOrderColor(Trim(Order_Main.fieldbyname('MainId').AsString),PRTColor,ADOQueryTemp);
       // InitBCGangNo(Trim(Order_Main.fieldbyname('SubId').AsString),AOrdDefStr1,ADOQueryTemp);
      //  InitRCGangNo(Trim(Order_Main.fieldbyname('SubId').AsString),GangNo,ADOQueryTemp);
      end;
    finally
      ADOQueryMain.EnableControls;
    end;
    MovePanel2.Visible:=False;
  end;
end;

procedure TfrmJYOrderCDOne.ToolButton10Click(Sender: TObject);
var
  fPrintFile:string;
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  with ADOqueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName='''+Trim(DCode)+'''');
    ExecSQL;
  end;
    with ADOqueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * FROM TBSubID where 1=2 ');
      open;
    end;
  ADOqueryCmd.Connection.BeginTrans;
  try
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if FieldByName('SSel').AsBoolean then
        begin
          ADOQueryCmd.append;
          ADOQueryCmd.fieldbyname('SubId').Value:=Trim(fieldbyname('MJID').AsString);
          ADOQueryCmd.fieldbyname('Dname').Value:=Trim(DCode);
          ADOQueryCmd.post;
        end;
        Next;
      end;
    end;
    Order_Main.EnableControls;
    ADOqueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
  with ADOqueryPrint do
  begin
    close;
    sql.Clear;
    sql.Add(' exec P_Get_JYMJID_Main ');
    sql.Add(' @MJID='+quotedstr(''));
    sql.Add(',@flag=''0'' ');
    sql.Add(',@userID='+quotedstr(trim(DCode)));
    open;
  end;

  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\检验汇总信息.rmf';;
  if NOT FileExists(fPrintFile) then
  begin
    Application.MessageBox(PChar('没有找'+trim(fPrintFile)),'提示',MB_ICONERROR);
    exit;
  end;
  Rm2.LoadFromFile(fPrintFile);
  Rm2.ShowReport;
end;

procedure TfrmJYOrderCDOne.APIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(APID.Text)<4 then Exit;
    MovePanel2.Visible:=True;
    MovePanel2.Refresh;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('exec P_JYOrder_HZFX20 :begdate,:enddate,:PState,:FFSql ');
        Parameters.ParamByName('begdate').Value:='2010-01-01';
        Parameters.ParamByName('enddate').Value:='2050-01-01';
        Parameters.ParamByName('PState').Value:=3;
        Parameters.ParamByName('FFSql').Value:=' where B.APID like '''+'%'+Trim(APID.Text)+'%'+'''';
        //ShowMessage(sql.Text);
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
      if Order_Main.IsEmpty=False then
      begin
        //InitOrderColor(Trim(Order_Main.fieldbyname('MainId').AsString),PRTColor,ADOQueryTemp);
       // InitBCGangNo(Trim(Order_Main.fieldbyname('SubId').AsString),AOrdDefStr1,ADOQueryTemp);
      //  InitRCGangNo(Trim(Order_Main.fieldbyname('SubId').AsString),GangNo,ADOQueryTemp);
      end;
    finally
      ADOQueryMain.EnableControls;
    end;
    MovePanel2.Visible:=False;
  end;
end;

procedure TfrmJYOrderCDOne.Button5Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  try
    Order_Main.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean=True then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update WFB_MJJY Set Mjstr4='''+trim(edit1.Text)+''' ');
            sql.Add('where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
            ExecSQL;
          end;
        end;
      Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存成功！','提示信息');
    Panel5.Visible:=false;
    TBRafresh.Click;
    exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存失败！','提示信息',0);
  end;
end;

procedure TfrmJYOrderCDOne.ToolButton11Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  Panel5.Left:=(self.Width-Panel2.Width) div 2;
  edit1.Text:='';
  Panel5.Visible:=true;
end;

procedure TfrmJYOrderCDOne.Button7Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  try
    Order_Main.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean=True then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update WFB_MJJY Set MJQty3='''+floattostr(strtofloatdef(trim(edit_MJQty3.Text),0))+''' where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
            sql.Add('Update WFB_MJJY Set MJQty4=MJMaoZ-MJQty3 where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
            ExecSQL;
          end;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存成功！','提示信息');
    Panel5.Visible:=false;
    TBRafresh.Click;
    exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存失败！','提示信息',0);
  end;
end;

procedure TfrmJYOrderCDOne.Button8Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  try
    Order_Main.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean=True then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update WFB_MJJY Set MJTypeOther='''+trim(edit_MJTypeOther.Text)+''' where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
//            sql.Add('Update WFB_MJJY Set MJQty4=MJMaoZ-MJQty3 where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
            ExecSQL;
          end;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存成功！','提示信息');
    Panel5.Visible:=false;
    TBRafresh.Click;
    exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存失败！','提示信息',0);
  end;
end;

end.
