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;

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;
    fromfactoryName: TEdit;
    factoryName: 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;
    v1Column24: TcxGridDBColumn;
    RadioGroup1: TRadioGroup;
    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;
    v1MKXS: TcxGridDBColumn;
    CheckBox2: TCheckBox;
    v1Column29: TcxGridDBColumn;
    v1Column30: TcxGridDBColumn;
    ToolButton5: TToolButton;
    BAONO: TEdit;
    BAOID: TEdit;
    Label16: TLabel;
    Label17: TLabel;
    ToolButton6: TToolButton;
    Mjstr4: TEdit;
    RCgangNO: TEdit;
    PrtColor: TEdit;
    Label18: TLabel;
    ComboBox1: TComboBox;
    RM3: TRMGridReport;
    v1Column26: TcxGridDBColumn;
    v1Column31: TcxGridDBColumn;
    v1Column32: TcxGridDBColumn;
    v1Column33: TcxGridDBColumn;
    KHConNO: TEdit;
    Label19: TLabel;
    v1Column34: TcxGridDBColumn;
    APID: TEdit;
    Label20: TLabel;
    v1Column35: TcxGridDBColumn;
    PRTKuanNO: TEdit;
    Label21: TLabel;
    v1Column36: TcxGridDBColumn;
    Label22: TLabel;
    ISHZ: TComboBox;
    RMXLSExport1: TRMXLSExport;
    ISbao: TComboBox;
    Label23: TLabel;
    CDS_HZ: TClientDataSet;
    RMDBHZ: TRMDBDataSet;
    TBBN: TToolButton;
    RMDBPRT: TRMDBDataSet;
    CDS_PRT: TClientDataSet;
    v1Column37: TcxGridDBColumn;
    v1Column38: TcxGridDBColumn;
    SOrddefstr1: TEdit;
    Label26: TLabel;
    prthx: TEdit;
    Label27: TLabel;
    ToolButton12: TToolButton;
    Panel5: TPanel;
    Label28: TLabel;
    ComboBox5: TComboBox;
    Button5: TButton;
    Button6: TButton;
    v1Column39: TcxGridDBColumn;
    v1Column40: TcxGridDBColumn;
    v1Column41: TcxGridDBColumn;
    v1Column42: TcxGridDBColumn;
    v1Column43: TcxGridDBColumn;
    Label29: TLabel;
    JYPerson: TEdit;
    v1Column44: TcxGridDBColumn;
    v1Column45: TcxGridDBColumn;
    v1Column46: TcxGridDBColumn;
    v1Column47: TcxGridDBColumn;
    v1Column48: TcxGridDBColumn;
    v1Column49: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Panel2: TPanel;
    Label15: TLabel;
    Button1: TButton;
    Button2: TButton;
    RichEdit2: TRichEdit;
    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 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 CheckBox2Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure MJIDKeyPress(Sender: TObject; var Key: Char);
    procedure APIDKeyPress(Sender: TObject; var Key: Char);
    procedure ISbaoChange(Sender: TObject);
    procedure TBBNClick(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure ToolButton12Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
  private
    DQdate:TDateTime;
    FLeft,FTop:Integer;
    procedure InitGrid();
    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_JYMJIDPRINT, U_ZDYHelp, U_ProductOrderListSel;

{$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;
      if Trim(DParameters2)='' then
      Parameters.ParamByName('FFSql').Value:=' and A.Fillter= '+quotedstr(Trim(DName))
      else
      Parameters.ParamByName('FFSql').Value:=' ';
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
    TBFind.Click;
  end;
end;

procedure TfrmJYOrderCDOne.InitForm();
var
  fsj:String;
begin
  ReadCxGrid('检验报告JF',Tv1,'检验管理');
  if Trim(DParameters2)='' then
  begin
    Filler.ItemIndex:=Filler.Items.Add(Trim(DName));
    Filler.Enabled:=false;
    TBManage.Visible:=True;
    TBZF.Visible:=True;
  end
  else
  begin
    fsj:='select distinct(Filler) name from WFB_MJJY   ';
    SInitComBoxBySql(ADOQueryCmd,Filler,False,fsj);
  end;

  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-1;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  
  if Trim(DParameters2)='管理' then
  begin
    TBManage.Visible:=True;
    TBZF.Visible:=True;
    v1Column15.Options.Focusing:=True;
    TBBN.Visible:=true;
    ToolButton12.Visible:=true;
    ToolButton1.Visible:=true;
    ToolButton6.Visible:=true;
    ToolButton5.Visible:=true;
  end;
  if Trim(DParameters2)='次品' then
  begin
    MJType.ItemIndex:=MJType.Items.IndexOf('次品');
    MJType.Enabled:=false;
  end;

  //InitGrid();
end;

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

  fwhere:=SGetFilters(Panel1,1,2);
  fsj:='';
  if trim(ISbao.Text)='已打包' then
  begin
    fsj:=fsj+' baoNo>''0'' ';
  end else
  if trim(ISbao.Text)='未打包' then
  begin
    fsj:=fsj+' baoNo='''' ';
  end;

  IF trim(fsj)<>'' then
  begin
    IF fwhere='' then fwhere:=fsj
    else fwhere:=fwhere+' and '+fsj;
  end;

  SDofilter(ADOQueryMain,fwhere);
  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();

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;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_BanCP_CR where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+''' and CRFlag=''入库''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Order_Main.EnableControls;
    Application.MessageBox('已产生入库数据不能编辑!','提示',0);
    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 AOrddefstr3 from JYOrder_Sub_AnPai B where B.APID=A.APID),  ');
        sql.Add('AOrddefstr2=(select AOrddefstr2 from JYOrder_Sub_AnPai B where B.APID=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));
      JYNote.Text:=Trim(ADOTmp.fieldbyname('JYNote').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);
begin
  SelOKNo(Order_Main,True);
end;

procedure TfrmJYOrderCDOne.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Main,False);
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;
  ADOQueryCmd.Connection.BeginTrans;
  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete TBValueWhere where DName='+quotedstr(Trim(DName)));
      SQL.Add('and MainType='+quotedstr('检验作废') );
      ExecSQL;
    end;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean=True then
        begin
          with Self.ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' insert into TBValueWhere(DName,MainType,Value1,Value2,wsql1) ');
            sql.Add(' values( '+quotedstr(Trim(DName))
            +','+quotedstr('检验作废')
            +','+Quotedstr(Trim(Self.Order_Main.FieldByName('MainId').AsString))
            +','+quotedstr(Trim(Self.Order_Main.FieldByName('SubId').AsString))
            +','+quotedstr(Trim(Self.Order_Main.FieldByName('MJID').AsString))
            +')');
            ExecSQL;
          end;
          with Self.ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
            sql.Add(' '+quotedstr(trim(DName)));
            sql.Add(',getdate() ');
            sql.Add(','+quotedstr(trim(self.Caption)));
            sql.Add(','+quotedstr(trim('检验作废')));
            sql.Add(','+quotedstr('卷：'+Trim(Self.Order_Main.FieldByName('MJID').AsString)+' 订单:'+trim(Self.Order_Main.FieldByName('orderNO').AsString)));
            sql.Add(','+quotedstr(trim('成功')));
            sql.Add(')');
            ExecSQL;
          end;
        end;
          Next;
      end;
    end;
    //已打包数据不能删除
    with Self.ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select count(*) JL from WFB_MJJY A where ISNULL(BaoId,'''')<>'''' ');
      SQL.Add(' and exists (select MainType from TBValueWhere X where  A.MJID=X.wsql1 and X.MainType=''检验作废'' and X.DName='+quotedstr(Trim(DName))+' )');
      open;
    end;
    if Self.ADOQueryCmd.FieldByName('JL').AsInteger>0 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Order_Main.EnableControls;
      Application.MessageBox('已打包数据不能删除!','提示',0);
      Exit;
    end;
    //已入库数据不能删除
    with Self.ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select count(*) JL from CK_BanCP_CR  A where CRFlag=''入库'' ');
      SQL.Add(' and exists (select * from TBValueWhere X where  A.MJID=X.wsql1 and X.MainType=''检验作废'' and X.DName='+quotedstr(Trim(DName))+' )');
//      showmessage(sql.Text) ;
      open;
    end;
    if Self.ADOQueryCmd.FieldByName('JL').AsInteger>0 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Order_Main.EnableControls;
      Application.MessageBox('已入库数据不能删除!','提示',0);
      Exit;
    end;
    //删除检验信息
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete WFB_MJJY  where exists (select * from TBValueWhere X where WFB_MJJY.MJID=X.wsql1 and MainType=''检验作废'' and DName='+quotedstr(Trim(DName))+' )');
      sql.Add('delete CK_BanCP_CR  where exists (select * from TBValueWhere X where CK_BanCP_CR.MJID=X.wsql1 and MainType=''检验作废'' and DName='+quotedstr(Trim(DName))+' )');
      sql.Add('delete CK_BanCP_KC  where exists (select * from TBValueWhere X where CK_BanCP_KC.MJID=X.wsql1 and MainType=''检验作废'' and DName='+quotedstr(Trim(DName))+' )');
      sql.Add('delete WFB_MJJY_CD  where exists (select * from TBValueWhere X where WFB_MJJY_CD.MJID=X.wsql1 and MainType=''检验作废'' and DName='+quotedstr(Trim(DName))+' )');
      ExecSQL;
    end;
    //更新检验进度
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Update_Order_JD_ByOther @MainType=''检验作废'', @DName ='+quotedstr(Trim(DName)));
      execsql;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    //删除显示记录
    while Order_Main.Locate('SSel',True,[]) do
    begin
      Order_Main.Delete;
    end;
    Order_Main.EnableControls;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据删除失败！','提示信息',0);
  end
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(' P_Do_PrintJyBq'+quotedstr(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
        begin
           fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\'+Trim(ADOQueryPrint.fieldbyname('LbName').AsString)+'.rmf';
        end
        else
        begin
          if ADOQueryPrint.FieldByName('MJType').AsString='多拼' then
          begin
            fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\通用英文标签1.rmf';
          end
          else
          begin
            fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\通用英文标签.rmf';
          end;
        end;
        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;

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);
    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,fPrintFile10,FMainID: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;
  FMainID:='';
  Order_Main.DisableControls;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
    First;
    while not Eof do
    begin
      If Fieldbyname('Ssel').AsBoolean then
      begin
        IF FMainID='' then
        begin
          FMainID:=Trim(Order_Main.fieldbyname('mainID').AsString);
        end
        else
        begin
          IF Trim(Order_Main.fieldbyname('mainID').AsString)<>FMainID then
          begin
            application.MessageBox('选择的不是同一个指示单，不能一起打印！','提示信息',0);
            ADOQueryCmd.Connection.RollbackTrans;
            EnableControls;
            exit;
          end;
        end;
          ADOQueryCmd.append;
          ADOQueryCmd.fieldbyname('SubId').Value:=Trim(Order_Main.fieldbyname('MJID').AsString);
          ADOQueryCmd.fieldbyname('Dname').Value:=Trim(DCode);
          ADOQueryCmd.post;
      end;
      Next;
    end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
  Order_Main.EnableControls;


    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Do_Print_JYBB ');
      sql.add('@mainID='+quotedstr(Trim('')));
      sql.add(',@DName='+quotedstr(Trim(DCode)));
      sql.add(',@flag=''0'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryCmd,CDS_HZ);
    SInitCDSData20(ADOQueryCmd,CDS_HZ);
    fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\检验报告普通版.rmf';
    if FileExists(fPrintFile) then
    begin
      RM3.LoadFromFile(fPrintFile);
      RM3.ShowReport;
    end
    else
    begin
      Application.MessageBox(PChar('没有找'+fPrintFile),'提示',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,C.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 C.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.CheckBox2Click(Sender: TObject);
begin
  SelOKNo(Order_Main,CheckBox2.Checked);
end;

procedure TfrmJYOrderCDOne.ToolButton6Click(Sender: TObject);
var
  strsql:string;
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要撤销打包吗？撤销打包后数据不能恢复。','提示',32+4)<>IDYES then  Exit;
  try
    Order_Main.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete TBValueWhere where DName='+quotedstr(Trim(DName)));
      SQL.Add('and MainType='+quotedstr('撤销打包') );
      ExecSQL;
    end;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if ((Order_Main.FieldByName('SSel').AsBoolean=True) and (trim(Order_Main.FieldByName('baoNo').AsString)<>'')) then
        begin
          IF trim(fieldbyname('mjstr2').AsString)<>'未入库' then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Order_Main.EnableControls;
            application.MessageBox('此条码已入库不能撤销打包!','提示信息',MB_ICONERROR);
            exit;
          end;
          strsql:='卷号：'+trim(Order_Main.FieldByName('MJID').AsString)+';包ID:'+trim(Order_Main.FieldByName('BAOID').AsString)+
                  '包号：'+trim(Order_Main.FieldByName('baoNo').AsString);
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
            sql.Add(' '+quotedstr(trim(DName)));
            sql.Add(',getdate() ');
            sql.Add(','+quotedstr(trim(self.Caption)));
            sql.Add(','+quotedstr(trim('撤销打包')));
            sql.Add(','+quotedstr(trim(strsql)));
            sql.Add(','+quotedstr(trim('成功')));
            sql.Add(')');
            ExecSQL;
          end;

          with Self.ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' insert into TBValueWhere(DName,MainType,wsql1,wsql2) ');
            sql.Add(' values( '+quotedstr(Trim(DName))
            +','+quotedstr('撤销打包')
            +','+quotedstr(Trim(Self.Order_Main.FieldByName('BaoId').AsString))
            +','+quotedstr(Trim(Self.Order_Main.FieldByName('MainId').AsString))
            +')');
            ExecSQL;
          end;
        end;
        Next;
      end;
    end;
    with Self.ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPDATE CK_BanCP_CR Set  ');
      sql.add('baoID='''',baoNo='''' ');
      sql.Add('FROM TBValueWhere A where CK_BanCP_CR.BaoId=A.wsql1  and isnull(CK_BanCP_CR.BaoId,'''')<>'''' ');
      SQL.Add('and A.MainType=''撤销打包'' and A.DName='+quotedstr(trim(DName)));

      sql.Add('UPDATE WFB_MJJY Set  ');
      sql.add('baoID='''',baoNo='''' ');
      sql.Add('FROM TBValueWhere A where WFB_MJJY.BaoId=A.wsql1 and isnull(WFB_MJJY.BaoId,'''')<>'''' ');
      SQL.Add('and A.MainType=''撤销打包'' and A.DName='+quotedstr(trim(DName)));
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Update_Order_JD_ByOther @MainType=''撤销打包''');
      sql.Add(',@DName='+quotedstr(Trim(DName)));
      ExecSQL;
    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.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.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.ISbaoChange(Sender: TObject);
begin
  Tbfind.Click;
end;

procedure TfrmJYOrderCDOne.TBBNClick(Sender: TObject);
var
  forderno,fmainid,fsubid:string;
begin
  IF  Order_Main.IsEmpty then exit;
  if Order_Main.Locate('ssel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Order_Main.Locate('ssel;MJStr2',VarArrayOf([true,'已出库']),[]) then
  begin
    Application.MessageBox('已出库数据不能转单!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要转单吗？','提示',32+4)<>IDYES then  Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBValueWhere where DName='+quotedstr(Trim(DName)));
    SQL.Add('and MainType='+quotedstr('成品转单') );
    ExecSQL;
  end;
  fmainID:='';
  frmProductOrderListSel:=TfrmProductOrderListSel.create(self);
  with frmProductOrderListSel do
  begin
    if showmodal=1 then
    begin
      forderno:= Order_Main.fieldbyname('orderno').asstring;
      fmainID:= Order_Main.fieldbyname('MainID').asstring;
      fsubID:= Order_Main.fieldbyname('subID').asstring;
    end;
    free;
  end;
  IF trim(fmainID)<>'' then
  begin
    ADOQueryCmd.Connection.BeginTrans;
    Try
      with Order_Main do
      begin
        DisableControls;
        First;
        while not eof do
        begin
          IF fieldbyname('ssel').AsBoolean then
          begin
            with Self.ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add(' insert into TBValueWhere(DName,MainType,Value1,Value2,wsql1) ');
              sql.Add(' values( '+quotedstr(Trim(DName))
              +','+quotedstr('成品转单')
              +','+Quotedstr(Trim(fmainid))
              +','+quotedstr(Trim(fsubid))
              +','+quotedstr(Trim(Self.Order_Main.FieldByName('MJID').AsString))
              +')');
              ExecSQL;
            end;
            with Self.ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
              sql.Add(' '+quotedstr(trim(DName)));
              sql.Add(',getdate() ');
              sql.Add(','+quotedstr(trim(self.Caption)));
              sql.Add(','+quotedstr(trim('成品转单')));
              sql.Add(','+quotedstr('卷：'+Trim(Self.Order_Main.FieldByName('MJID').AsString)+'包：'+Trim(Self.Order_Main.FieldByName('baoid').AsString)+'-'+Trim(Self.Order_Main.FieldByName('baono').AsString)+' 订单:'+trim(Self.Order_Main.FieldByName('orderNO').AsString)+'-->'+trim(forderno)  ));
              sql.Add(','+quotedstr(trim('成功')));
              sql.Add(')');
              ExecSQL;
            end;
          end;
          next;
        end;
        first;
        EnableControls;
      end;
      
      with Self.ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPDATE CK_BanCP_CR Set  ');
        sql.add('MainId=A.Value1,SubId=A.Value2,ZDPerson=A.DName,ZDTime=getdate()');
        sql.Add('FROM TBValueWhere A where CK_BanCP_CR.MJID=A.wsql1 ');
        SQL.Add('and A.MainType=''成品转单'' and A.DName='+quotedstr(trim(DName)));

        sql.Add('UPDATE WFB_MJJY Set  ');
        sql.add('MainId=A.Value1,SubId=A.Value2');
        sql.Add('FROM TBValueWhere A where WFB_MJJY.MJID=A.wsql1 ');
        SQL.Add('and A.MainType=''成品转单'' and A.DName='+quotedstr(trim(DName)));
        ExecSQL;
      end;
      //更新检验进度
      with Self.ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('exec P_Update_Order_JD_ByOther @MainType=''成品转单'', @DName ='+quotedstr(Trim(DName)));
        execsql;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      TBRafresh.Click;
    except
      Order_Main.EnableControls;
      ADOQueryCmd.Connection.RollbackTrans;
    end;
  end;
end;

procedure TfrmJYOrderCDOne.Button6Click(Sender: TObject);
begin
  Panel5.Visible:=false;
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;
  if trim(ComboBox5.Text)='' then
  begin
    Application.MessageBox('数量单位不能为空!','提示',0);
    Exit;
  end;
  try
    Order_Main.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete TBValueWhere where DName='+quotedstr(Trim(DName)));
      SQL.Add('and MainType='+quotedstr('单位修改') );
      ExecSQL;
    end;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean=True then
        begin
          with Self.ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' insert into TBValueWhere(DName,MainType,Value1,Value2,Value3,wsql1) ');
            sql.Add(' values( '+quotedstr(Trim(DName))
            +','+quotedstr('单位修改')
            +','+Quotedstr(Trim(Self.Order_Main.FieldByName('MainId').AsString))
            +','+quotedstr(Trim(Self.Order_Main.FieldByName('SubId').AsString))
            +','+quotedstr(trim(ComboBox5.Text))
            +','+quotedstr(Trim(Self.Order_Main.FieldByName('MJID').AsString))
            +')');
            ExecSQL;
          end;
          with Self.ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
            sql.Add(' '+quotedstr(trim(DName)));
            sql.Add(',getdate() ');
            sql.Add(','+quotedstr(trim(self.Caption)));
            sql.Add(','+quotedstr(trim('单位修改')));
            sql.Add(','+quotedstr('卷：'+Trim(Self.Order_Main.FieldByName('MJID').AsString)+' 订单:'+trim(Self.Order_Main.FieldByName('orderNO').AsString)));
            sql.Add(','+quotedstr(trim('成功')));
            sql.Add(')');
            ExecSQL;
          end;
        end;
        Next;
      end;
    end;
    //已打包数据不能修改单位
    with Self.ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select count(*) JL from WFB_MJJY A where ISNULL(BaoId,'''')<>'''' ');
      SQL.Add(' and exists (select MainType from TBValueWhere X where  A.MJID=X.wsql1 and X.MainType=''单位修改'' and X.DName='+quotedstr(Trim(DName))+' )');
//      ShowMessage(SQL.Text);
      open;
    end;
    if Self.ADOQueryCmd.FieldByName('JL').AsInteger>0 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Order_Main.EnableControls;
      Application.MessageBox('已打包数据不能修改!','提示',0);
      Exit;
    end;
    //已入库数据不能修改单位
    with Self.ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select count(*) JL from CK_BanCP_CR  A where A.CRFlag=''入库'' ');
      SQL.Add(' and exists (select * from TBValueWhere X where  A.MJID=X.wsql1 and X.MainType=''单位修改'' and X.DName='+quotedstr(Trim(DName))+' )');
//      ShowMessage(SQL.Text);
      Open;
    end;
    if Self.ADOQueryCmd.FieldByName('JL').AsInteger>0 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Order_Main.EnableControls;
      Application.MessageBox('已入库数据不能修改!','提示',0);
      Exit;
    end;
    
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update WFB_MJJY Set MJTypeOther=A.Value3 from TBValueWhere A where  WFB_MJJY.MJID=A.wsql1 and A.MainType=''单位修改'' and A.DName='+quotedstr(Trim(DName)));
      sql.Add('Update CK_BanCP_CR Set QtyUnit=A.Value3 from TBValueWhere A where  CK_BanCP_CR.MJID=A.wsql1 and A.MainType=''单位修改'' and A.DName='+quotedstr(Trim(DName)));
      sql.Add('Update CK_BanCP_KC Set KCQtyUnit=A.Value3 from TBValueWhere A where  CK_BanCP_KC.MJID=A.wsql1 and A.MainType=''单位修改'' and A.DName='+quotedstr(Trim(DName)));
      ExecSQL;
    end;
    
    //更新检验进度
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec [P_Update_Order_JD_ByOther] @MainType=''单位修改'', @DName ='+quotedstr(Trim(DName)));
      execsql;
    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.ToolButton12Click(Sender: TObject);
begin
  Panel5.Visible:=true;
end;

procedure TfrmJYOrderCDOne.ToolButton3Click(Sender: TObject);
begin
  Panel2.Visible:=true;
end;

procedure TfrmJYOrderCDOne.Button1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if trim(RichEdit2.Text)='' then
  begin
    Application.MessageBox('备注不能为空!','提示',0);
    Exit;
  end;
  try
    Order_Main.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete TBValueWhere where DName='+quotedstr(Trim(DName)));
      SQL.Add('and MainType='+quotedstr('备注修改') );
      ExecSQL;
    end;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean=True then
        begin
          with Self.ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' insert into TBValueWhere(DName,MainType,wsql1) ');
            sql.Add(' values( '+quotedstr(Trim(DName))
            +','+quotedstr('备注修改')
            +','+quotedstr(Trim(Self.Order_Main.FieldByName('MJID').AsString))
            +')');
            ExecSQL;
          end;
        end;
        Next;
      end;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update WFB_MJJY Set JYNote='+quotedstr(trim(RichEdit2.Text))+' from TBValueWhere A where  WFB_MJJY.MJID=A.wsql1 and A.MainType=''备注修改'' and A.DName='+quotedstr(Trim(DName)));
      ExecSQL;
    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.Button2Click(Sender: TObject);
begin
 Panel2.Visible:=false;
end;

procedure TfrmJYOrderCDOne.ToolButton5Click(Sender: TObject);
  var
   fPrintFile,FBaoID: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
//    while locate('SSel',true,[]) do
//    begin
//
//
//    end;
//  end;
//  Order_Main.EnableControls;
  FBaoID:=Order_Main.fieldbyname('BaoID').AsString;
  if trim(FBaoID)='' then
  begin
    Application.MessageBox('BaoId不能为空!','提示',0);
    Exit;
  end;
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    SQL.Add(' P_Do_PrintDaBao'+quotedstr(Trim(FBaoID)));
    Open;
  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
    Order_Main.EnableControls;
    application.MessageBox('条形码生成失败！','提示信息',MB_ICONERROR);
    exit;
  end;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\'+Trim(ADOQueryPrint.fieldbyname('NLBName').AsString)+'.rmf';
  IF Not FileExists(fPrintFile) then
  begin
    Application.MessageBox(PChar('没有找'+fPrintFile),'提示',0);
    Exit;
  end;
  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE']:=fImagePath;
    RM2.LoadFromFile(fPrintFile);
    Rm2.DefaultCopies:=strtointdef(trim(ComboBox1.Text),1);
    RM2.PrintReport;
  end
  else
  begin
    Order_Main.EnableControls;
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+fPrintFile),'提示',0);
    Exit;
  end;

end;

end.
