unit U_JYOrderCDList;

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, BtnEdit, RM_e_main, RM_e_htm, RM_e_Graphic,
  RM_e_Jpeg, RM_e_txt, RM_e_csv, RM_E_llPDF, Math, Clipbrd, cxPC, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu, cxMaskEdit, cxButtonEdit,
  dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter, dxSkinBlack,
  dxSkinBlue, dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom,
  dxSkinDarkSide, dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringTime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinValentine, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinXmas2008Blue;

type
  TfrmJYOrderCDList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    MJID: TEdit;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    Label4: TLabel;
    v1Column13: TcxGridDBColumn;
    Label5: TLabel;
    spName: TEdit;
    v1Column19: TcxGridDBColumn;
    Label12: TLabel;
    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;
    v1Column24: TcxGridDBColumn;
    MovePanel2: TMovePanel;
    v1Column28: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    CDS_CD: TClientDataSet;
    RMCD: TRMGridReport;
    RMDBCD: TRMDBDataSet;
    CDS_LS: TClientDataSet;
    v1Column15: TcxGridDBColumn;
    Panel2: TPanel;
    Label15: TLabel;
    cxCurrencyEdit1: TcxCurrencyEdit;
    Button1: TButton;
    Button2: TButton;
    v1MKXS: TcxGridDBColumn;
    Mjstr4: TEdit;
    spColor: TEdit;
    Label18: TLabel;
    ComboBox1: TComboBox;
    RM3: TRMGridReport;
    ToolButton8: TToolButton;
    v1Column34: TcxGridDBColumn;
    v1Column36: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    CDS_HZ: TClientDataSet;
    RMDBHZ: TRMDBDataSet;
    RMDBPRT: TRMDBDataSet;
    CDS_PRT: TClientDataSet;
    Panel5: TPanel;
    Label28: TLabel;
    ComboBox5: TComboBox;
    Button5: TButton;
    Button6: TButton;
    v1Column39: TcxGridDBColumn;
    Panel6: TPanel;
    Label31: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    IsJYTime: TCheckBox;
    N3: TMenuItem;
    X1: TMenuItem;
    cxTabControl1: TcxTabControl;
    B1: TToolButton;
    lbl1: TLabel;
    APID: TEdit;
    v1Column29: TcxGridDBColumn;
    ToolButton2: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    ComboBox2: TComboBox;
    Label2: TLabel;
    Label3: TLabel;
    ToolButton3: TToolButton;
    Panel3: TPanel;
    Label6: TLabel;
    Button3: TButton;
    Button4: TButton;
    khname: TcxButtonEdit;
    Label11: TLabel;
    Edit1: TEdit;
    Button7: TButton;
    Label1: TLabel;
    CustomerNoName: TEdit;
    spKZ: TEdit;
    ToolButton4: TToolButton;
    Tv1Column3: TcxGridDBColumn;
    Label13: TLabel;
    Button8: TButton;
    edit3: TEdit;
    Tv1Column4: TcxGridDBColumn;
    Label14: TLabel;
    msfw: TEdit;
    Label16: TLabel;
    kcqty: TEdit;
    Label17: TLabel;
    cxCurrencyEdit2: TcxCurrencyEdit;
    Button9: TButton;
    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 FormShow(Sender: TObject);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure TBManageClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBZFClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure v1Column15PropertiesEditValueChanged(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure MJIDKeyPress(Sender: TObject; var Key: Char);
    procedure Button6Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure ToolButton12Click(Sender: TObject);
    procedure BTLPClick(Sender: TObject);
    procedure BTNPClick(Sender: TObject);
    procedure TCBNORChange(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure conNOKeyPress(Sender: TObject; var Key: Char);
    procedure MJStr2Change(Sender: TObject);
    procedure MJTypeChange(Sender: TObject);
    procedure KHConNOKeyPress(Sender: TObject; var Key: Char);
    procedure RKOrdIDKeyPress(Sender: TObject; var Key: Char);
    procedure N3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure X1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure B1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure cxButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
  private
    DQdate: TDateTime;
    FLeft, FTop: Integer;
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    procedure InitForm();
    function Savedata(): Boolean;
    procedure GetCDSData();
    procedure SetStatus();
    procedure PrintCKMDG(MRKOrdID: string);
    { Private declarations }
  public
    FFInt: Integer;
    canshu1: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_OrderInPut, U_Fun, U_MJEdit, U_ZDYHelp, U_ProductOrderListSel,
  U_SysLogHelp, U_KHList;

{$R *.dfm}
procedure TfrmJYOrderCDList.SetStatus();
var
  i: Integer;
begin
  TBManage.Visible := False;
  TBZF.Visible := False;
  v1Column15.Options.Focusing := False;
  ToolButton1.Visible := False;
  ToolButton8.Visible := False;
  ToolButton2.Visible := False;
  ToolButton3.Visible := False;
  ToolButton4.Visible := False;

  if Trim(canshu1) = '管理' then
  begin

    case cxTabControl1.TabIndex of
      0:
        begin

        end;
      1:
        begin
          TBManage.Visible := True;
          TBZF.Visible := True;
          v1Column15.Options.Focusing := True;
          ToolButton1.Visible := true;
          ToolButton8.Visible := true;
          ToolButton2.Visible := true;
          ToolButton3.Visible := true;
          ToolButton4.Visible := true;

        end;
      2:
        begin

        end;
    end;
  end;

  case cxTabControl1.TabIndex of
    0:
      begin
        ClearOrHideControls(Panel1, '0', True, True);
        ClearOrHideControls(Panel1, '1', True, False);

        for i := 0 to tv1.ColumnCount - 1 do
        begin
          tv1.Columns[i].Visible := false;
        end;
        IsVisibleTV(Panel1, TV1, '0', True);
        Tv1Column2.Visible := True;
        v1Column8.Visible := True;
        Tv1Column3.Visible := True;
        v1Column11.Visible := True;
        Tv1Column1.Visible := True;
        v1Column13.Visible := True;
        v1Column4.Visible := True;
        v1Column34.Visible := false;
        v1Column3.Visible := True;
        v1Column5.Visible := True;
        Tv1Column4.Visible := True;
      end;

    1:
      begin
        for i := 0 to tv1.ColumnCount - 1 do
        begin
          tv1.Columns[i].Visible := True;
        end;

        ClearOrHideControls(Panel1, '0', False, True);
        ClearOrHideControls(Panel1, '1', False, True);

      end;
  end;

end;

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

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

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

procedure TfrmJYOrderCDList.PrintCKMDG(MRKOrdID: string);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if Order_Main.IsEmpty then
    Exit;
  ExportFtErpFile('仓库码单.rmf', ADOQueryTemp);
  with Order_Main do
  begin

    with ADOQueryPrint do
    begin
      Close;
      SQL.Clear;
      sql.Add(' exec P_Print_RKMDGangNo ');
      SQL.Add(' @RKOrdID=''' + Trim(MRKOrdID) + '''');
      Open;
    end;
    try
      Moudle := LoadLibrary('MakeQRBarcode.dll');
      @Makebar := GetProcAddress(Moudle, 'Make');
      @Mixtext := GetProcAddress(Moudle, 'MixText');
      Txt := Trim(ADOQueryPrint.fieldbyname('RKOrdID').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;
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\仓库码单.rmf';
    if FileExists(fPrintFile) then
    begin
      RMVariables['QRBARCODE'] := fImagePath;
      RM2.LoadFromFile(fPrintFile);
      Rm2.DefaultCopies := strtointdef(trim(ComboBox1.Text), 1);
//      RM2.PrintReport;
      RM2.ShowReport;
    end
    else
    begin
      Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
      Exit;
    end;

  end;

end;

procedure TfrmJYOrderCDList.InitGrid();
var
  fwhere, Pwhere: string;
begin
  MovePanel2.Left := (self.Width - MovePanel2.Width) div 2;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  if IsJYTime.Checked then
  begin
    fwhere := ' where A.Filltime>' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime));
    fwhere := fwhere + ' and A.Filltime<=' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1));
  end;

  Pwhere := SGetFilters(Panel1, 1, 2);

  if trim(Pwhere) <> '' then
  begin

    Pwhere := Pwhere + ' and MJStr2=''未入库'' ';
  end
  else
  begin
    Pwhere := ' MJStr2=''未入库'' ';
  end;

  if trim(fwhere) <> '' then
  begin
    fwhere := fwhere + ' and ' + trim(Pwhere);
  end
  else
  begin
    fwhere := ' where ' + trim(Pwhere);
  end;

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      case cxTabControl1.TabIndex of
        0:   /////////////////////////汇总//////////////////////////////
          begin                                                                         //
            sql.Add(' SELECT CustomerNoName,FromFactoryName,MJTypeOther,spName,spColor,spMF,spKZ,msfw, sum(kcqty)kcqty ,SUM(kckgqty)kckgqty,count(*)ps FROM V_JYB A');
            SQL.Add(fwhere);
            sql.Add('group by CustomerNoName,MJTypeOther,spName,spColor,spMF,spKZ,msfw,FromFactoryName');

          end;
        1:   /////////////////////////明细//////////////////////////////
          begin
            sql.Add(' SELECT *,ps=1 FROM V_JYB A');

            SQL.Add(fwhere);
          end;
      end;

      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);

    MovePanel2.Visible := false;
  finally
    ADOQueryMain.EnableControls;
    MovePanel2.Visible := false;
  end;
end;

procedure TfrmJYOrderCDList.InitForm();
var
  fsj: string;
begin
  ReadCxGrid('检验报告JF', Tv1, '检验管理');

  fsj := 'select distinct(Filler) name from WFB_MJJY   ';
  SInitComBoxBySql(ADOQueryCmd, Filler, False, fsj);
//  Filler.ItemIndex := Filler.Items.IndexOf(trim(DName));
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  if canshu1 = '次品查询' then
  begin
    BegDate.DateTime := EndDate.DateTime - 90;
  end
  else
  begin
    BegDate.DateTime := EndDate.DateTime - 14;
  end;

  //InitGrid();
end;

procedure TfrmJYOrderCDList.TBFindClick(Sender: TObject);
var
  fsj, fwhere: string;
begin

end;

procedure TfrmJYOrderCDList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  if cxTabControl1.TabIndex = 0 then
  begin
    SelExportData(Tv1, ADOQueryMain, '汇总检验报告');
  end;

  if cxTabControl1.TabIndex = 1 then
  begin
    SelExportData(Tv1, ADOQueryMain, '明细检验报告');
  end;

end;

procedure TfrmJYOrderCDList.TBRafreshClick(Sender: TObject);
begin

  CurrentPage := 1;
  InitGrid();

end;

procedure TfrmJYOrderCDList.FormShow(Sender: TObject);
begin
  CurrentPage := 1;
  RecordsNumber := 500;
  InitForm();
  SetStatus();
end;

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

procedure TfrmJYOrderCDList.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 CRFlag=''入库'' and MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
    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);
      if ShowModal = 1 then
      begin

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

procedure TfrmJYOrderCDList.N1Click(Sender: TObject);
begin
  SelOKNo(Order_Main, True);
end;

procedure TfrmJYOrderCDList.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Main, False);
end;

procedure TfrmJYOrderCDList.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) + ''' and CRFlag=''入库''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty = False then
        begin
          Order_Main.EnableControls;
          Application.MessageBox('已产生入库数据不能删除!', '提示', 0);
          Exit;
        end
        else
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('delete WFB_MJJY where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            sql.Add('delete CK_BanCP_CR where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            sql.Add('delete CK_BanCP_KC where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            sql.Add('delete WFB_MJJY_CD where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            //更新开卡
            sql.Add('exec P_UPdate_LCKJY @APID=''' + trim(Order_Main.fieldbyname('APID').AsString) + '''');

            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(Order_Main.fieldbyname('MJID').AsString) + ' 卷号：' + trim(Order_Main.fieldbyname('MJXH').AsString) + ' 卡号：' + trim(Order_Main.fieldbyname('apid').AsString)));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            ExecSQL;
          end;
        end;
        Order_Main.Delete;
      end
      else
        Next;
    end;
  end;
  Order_Main.EnableControls;

end;

procedure TfrmJYOrderCDList.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(' EXEC P_Print_JYLab ');
          SQL.Add(' @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
        begin
          ExportFtErpFile(Trim(ADOQueryPrint.fieldbyname('LbName').AsString), ADOQueryTemp);
          fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ADOQueryPrint.fieldbyname('LbName').AsString)
        end
        else
        begin
          ExportFtErpFile('通用英文标签.rmf', ADOQueryTemp);
          fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\通用英文标签.rmf';
        end;

        if Trim(Order_Main.fieldbyname('MJType').AsString) = '次品' then
        begin
          ExportFtErpFile('次品标签.rmf', ADOQueryTemp);
          fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\次品标签.rmf';
        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('没有找' + fPrintFile), '提示', 0);
          Exit;
        end;
      end;
      Next;
    end;
  end;
  Order_Main.EnableControls;
end;

function TfrmJYOrderCDList.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 TfrmJYOrderCDList.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin

    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmJYOrderCDList.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 TfrmJYOrderCDList.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 TfrmJYOrderCDList.Button2Click(Sender: TObject);
begin
  Panel2.Visible := false;
end;

procedure TfrmJYOrderCDList.Button1Click(Sender: TObject);
var
  i, J: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if cxCurrencyEdit1.Text = '' then
    Exit;
  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('Update WFB_MJJY Set mjlen=''' + trim(cxCurrencyEdit1.Text) + '''  ');
            sql.Add('where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            sql.Add('Update CK_BanCP_CR Set QTY=''' + trim(cxCurrencyEdit1.Text) + '''  ');
            sql.Add('where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');

            SQL.Add('Update CK_BanCP_KC set KCQTY=''' + trim(cxCurrencyEdit1.Text) + '''  ');

            SQL.Add('WHERE MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');

            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('条码号：' + trim(Order_Main.fieldbyname('MJID').AsString) + ' 改为:' + trim(cxCurrencyEdit1.Text))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            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 TfrmJYOrderCDList.ToolButton3Click(Sender: TObject);
begin
  Panel3.Left := (self.Width - Panel2.Width) div 2;
  Panel3.Visible := true;
end;

procedure TfrmJYOrderCDList.MJIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Length(MJID.Text) < 4 then
      Exit;
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmJYOrderCDList.Button6Click(Sender: TObject);
begin
  Panel5.Visible := false;
end;

procedure TfrmJYOrderCDList.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 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(ComboBox5.Text) + ''' ');
            sql.Add('where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            sql.Add('Update CK_BanCP_CR Set QtyUnit=''' + trim(ComboBox5.Text) + ''' ');
            sql.Add('where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            sql.Add('Update CK_BanCP_KC Set kcQtyUnit=''' + trim(ComboBox5.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('数据保存成功！', '提示信息');
    Panel2.Visible := false;
    TBRafresh.Click;
    exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存失败！', '提示信息', 0);
  end;
end;

procedure TfrmJYOrderCDList.ToolButton12Click(Sender: TObject);
begin
  Panel5.Visible := true;
end;

procedure TfrmJYOrderCDList.BTLPClick(Sender: TObject);
begin
  if CurrentPage > 1 then
    CurrentPage := CurrentPage - 1;
  InitGrid();
end;

procedure TfrmJYOrderCDList.BTNPClick(Sender: TObject);
begin
  if CurrentPage < Order_Main.FieldByName('TotalCount').AsInteger / RecordsNumber then
    CurrentPage := CurrentPage + 1;
  InitGrid();
end;

procedure TfrmJYOrderCDList.TCBNORChange(Sender: TObject);
begin
  RecordsNumber := StrToInt(TCBNOR.Text);
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmJYOrderCDList.ToolButton8Click(Sender: TObject);
var
  SRKOrdID, strsql: string;
begin
  if cxTabControl1.TabIndex <> 1 then
    exit;

  if Order_Main.IsEmpty then
  begin
    ToolButton8.Enabled := True;
    Exit;
  end;

  if GetLSNo(ADOQueryTemp, SRKOrdID, 'RK', 'CK_BanCP_CR', 4, 1) = False then
  begin
    Application.MessageBox('生成流水号异常！', '提示', 0);
    ToolButton8.Enabled := True;
    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 then
        begin
          strsql := '卷ID：' + trim(Order_Main.FieldByName('MJID').AsString) + '原入库单号：' + trim(Order_Main.FieldByName('RKOrdID').AsString) + '新入库单号：' + trim(SRKOrdID);
          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('UPDATE CK_BanCP_CR Set  ');
            sql.add(' CRTime=getdate(),CRType=''检验入库'',CRFlag=''入库''  ');
            sql.add(',RKOrdID=' + QuotedStr(Trim(SRKOrdID)));
            SQL.Add('where mjid=' + quotedstr(trim(Order_Main.FieldByName('MJID').AsString)));
            sql.Add('and CK_BanCP_CR.CRFlag=''待入库''  ');

            sql.Add('UPDATE WFB_MJJY Set MJStr2=''已入库'' ');
            sql.add(',RKOrdID=' + QuotedStr(Trim(SRKOrdID)));
            SQL.Add('where mjid=' + quotedstr(trim(Order_Main.FieldByName('MJID').AsString)));
            SQL.Add('and WFB_MJJY.MJStr2=''未入库'' ');
            ExecSQL;
          end;

        end;
        Next;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
//    if Application.MessageBox('入库完成是否打印码单！！', '提示', 32 + 4) = IDYES then
//    begin
//      PrintCKMDG(Trim(SRKOrdID));
//    end;
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    ToolButton8.Enabled := true;
    application.MessageBox('数据撤销失败！', '提示信息', 0);
  end;
  ToolButton8.Enabled := true;
end;

procedure TfrmJYOrderCDList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  Clipboard.SetTextBuf(PChar(Trim(Order_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmJYOrderCDList.conNOKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmJYOrderCDList.MJStr2Change(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmJYOrderCDList.MJTypeChange(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmJYOrderCDList.KHConNOKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmJYOrderCDList.RKOrdIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmJYOrderCDList.N3Click(Sender: TObject);
var
  SSubId, SGangNo: string;
begin

  with Order_Main do
  begin
    SSubId := trim(FieldByName('SubId').AsString);
    SGangNo := trim(FieldByName('Mjstr4').AsString);
    Order_Main.DisableControls;
    first;
    while not eof do
    begin
      if ((SSubId = trim(FieldByName('SubId').AsString)) and (SGangNo = trim(FieldByName('Mjstr4').AsString))) then
      begin
        edit;
        FieldByName('SSel').value := true;
        Post;
      end
      else
      begin
        edit;
        FieldByName('SSel').value := false;
        Post;
      end;
      Next;
    end;
    Order_Main.Locate('SubId;Mjstr4', VarArrayOf([SSubId, SGangNo]), []);
    Order_Main.EnableControls;
  end;
end;

procedure TfrmJYOrderCDList.ToolButton4Click(Sender: TObject);
begin
  Panel2.Visible := True;
end;

procedure TfrmJYOrderCDList.X1Click(Sender: TObject);
var
  str, SSubId, SGangNo: string;
  ZChiShu, DQChiShu: integer;
  ISSel: boolean;
begin
  ISSel := true;
  SSubId := trim(Order_Main.FieldByName('SubId').AsString);
  SGangNo := trim(Order_Main.FieldByName('Mjstr4').AsString);
  str := '';
  if InputQuery('选择', '请输入数字', str) then
  begin
    ZChiShu := strtointdef(str, 0);
    if ZChiShu <= 0 then
      exit;

    with Order_Main do
    begin

      Order_Main.DisableControls;
      first;
      DQChiShu := 0;
      while not eof do
      begin

        if ((SSubId = trim(FieldByName('SubId').AsString)) and (SGangNo = trim(FieldByName('Mjstr4').AsString))) then
        begin
          edit;
          FieldByName('SSel').value := ISSel;
          DQChiShu := DQChiShu + 1;
          Post;
        end
        else
        begin
          edit;
          FieldByName('SSel').value := false;
          Post;
        end;
        if DQChiShu = ZChiShu then
          ISSel := false;
        Next;
      end;
      Order_Main.Locate('SubId;Mjstr4', VarArrayOf([SSubId, SGangNo]), []);
      Order_Main.EnableControls;
    end;
  end;

end;

procedure TfrmJYOrderCDList.cxTabControl1Change(Sender: TObject);
begin
  CurrentPage := 1;
  SetStatus();
  InitGrid();

end;

procedure TfrmJYOrderCDList.B1Click(Sender: TObject);
begin
  frmSysLogHelp := TfrmSysLogHelp.create(self);
  with frmSysLogHelp do
  begin
    fModel := self.caption;
  //  facction:='生产指示单删除';
    showmodal;
    free;
  end;
end;

procedure TfrmJYOrderCDList.ToolButton2Click(Sender: TObject);
var
  fPrintFile, FMainID, LBName: string;
begin
  if Order_Main.IsEmpty then
    Exit;

  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;
  if ComboBox2.Text = '' then
  begin
    Application.MessageBox('没有选择打印格式', '提示', 0);
    Exit;
  end;
  ExportFtErpFile(trim(ComboBox2.Text) + '.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + trim(ComboBox2.Text) + '.rmf';

  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('APID').AsString);
          end
          else
          begin

          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 ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_RKMD ');
    sql.add(' @DName=' + quotedstr(Trim(DCode)));

    Open;
  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_RKMDHZ ');
    sql.add(' @DName=' + quotedstr(Trim(DCode)));

    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_PRT);
  SInitCDSData20(ADOQueryTemp, CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    RM2.LoadFromFile(fPrintFile);
    RM2.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + trim(fPrintFile)), '提示信息', 0);
    exit;
  end;
end;

procedure TfrmJYOrderCDList.ToolButton5Click(Sender: TObject);
var
  I: Integer;
  Column: TcxGridDBColumn;
  FieldNames: TStringList;
begin
  FieldNames := TStringList.Create;
  try
    for I := 0 to Tv1.ColumnCount - 1 do
    begin
      Column := Tv1.Columns[I];
      FieldNames.Add(Column.Caption + ':' + Column.DataBinding.FieldName);
    end;
    ShowMessage(FieldNames.Text);
  finally
    FieldNames.Free;
  end;
end;

procedure TfrmJYOrderCDList.Tv1DblClick(Sender: TObject);
var
  Tabint: integer;
begin
  Tabint := cxTabControl1.TabIndex;
  case Tabint of
    0:
      begin
        AssignmentControls(Panel1, Order_Main, '0');
        cxTabControl1.TabIndex := 1;
      end;
  end;
end;

procedure TfrmJYOrderCDList.cxButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmKHList := TfrmKHList.Create(Application);
    with frmKHList do
    begin
      if ShowModal = 1 then
      begin

        khname.text := Trim(Order_Main.fieldbyname('CoNAME').AsString);

      end;
    end;
  finally
    frmKHList.Free;
  end;
end;

procedure TfrmJYOrderCDList.Button4Click(Sender: TObject);
begin
  Panel3.Visible := False;
end;

procedure TfrmJYOrderCDList.Button3Click(Sender: TObject);
var
  SRKOrdID, strsql: string;
begin
  if cxTabControl1.TabIndex <> 1 then
    exit;
  if edit1.Text = '' then
  begin
    Application.MessageBox('出库单号不能为空！', '提示', 0);
    Exit;
  end;

  if GetLSNo(ADOQueryTemp, SRKOrdID, 'RK', 'CK_BanCP_CR', 4, 1) = False then
  begin
    Application.MessageBox('生成流水号异常！', '提示', 0);
    ToolButton8.Enabled := True;
    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 then
        begin
          with Self.ADOQueryCmd do
          begin
            Close;
            sql.Clear;

            sql.Add('UPDATE CK_BanCP_CR Set  ');
            sql.add('RKOrdID= ' + quotedstr(trim(SRKOrdID)));
            sql.Add('where mjid=' + quotedstr(trim(Order_Main.fieldbyname('mjid').AsString)));
            sql.Add('and CK_BanCP_CR.CRFlag in (''待入库'',''待出库'')  ');

            sql.Add('UPDATE CK_BanCP_CR Set  ');
            sql.add(' CRTime=getdate(),CRType=''检验入库'',CRFlag=''入库'',CTYPE=''国内''  ');
            sql.Add('where mjid=' + quotedstr(trim(Order_Main.fieldbyname('mjid').AsString)));
            sql.Add('and CK_BanCP_CR.CRFlag=''待入库''  ');

            sql.Add('UPDATE WFB_MJJY Set  ');
            sql.add('RKOrdID= ' + quotedstr(trim(SRKOrdID)));
            sql.add('  ,MJStr2=''已出库'',mjstr3=''国内'' ');
            sql.Add('where mjid=' + quotedstr(trim(Order_Main.fieldbyname('mjid').AsString)));
            SQL.Add('and WFB_MJJY.MJStr2=''未入库'' ');

            sql.Add('Update CK_BanCP_CR Set CRFlag=''出库'',CRType=''国内直接出库'',CRTime=getdate(),filltime=getdate(),CTYPE=''国内'', ');
            SQL.Add('CKOrdNo=' + QuotedStr(Edit1.Text));
            SQL.Add(',FILLER=' + QuotedStr(DNAME));
            SQL.Add(',CUSTNAME=' + QuotedStr(khname.Text));
            sql.Add('where  mjid=' + quotedstr(trim(Order_Main.fieldbyname('mjid').AsString)));
            sql.Add(' and CRFlag=''待出库''');

            sql.Add(' UPdate CK_BanCP_KC Set KCValid=''N'' ');
            sql.Add('where  mjid=' + quotedstr(trim(Order_Main.fieldbyname('mjid').AsString)));
            ExecSQL;
          end;
        end;
        Next;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
//    if Application.MessageBox('入库完成是否打印码单！！', '提示', 32 + 4) = IDYES then
//    begin
//      PrintCKMDG(Trim(SRKOrdID));
//    end;
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    ToolButton8.Enabled := true;
    application.MessageBox('数据撤销失败！', '提示信息', 0);
  end;
  ToolButton8.Enabled := true;
end;

procedure TfrmJYOrderCDList.Button7Click(Sender: TObject);
var
  MAXNO: string;
begin
  if GetLSNo(ADOQueryTemp, MAXNO, 'CK', 'CK_BanCP_CR', 3, 1) = False then
  begin
    Application.MessageBox('生成流水号异常！', '提示', 0);
    ToolButton8.Enabled := True;
    Exit;
  end;
  Edit1.Text := Trim(MAXNO);
end;

procedure TfrmJYOrderCDList.Button8Click(Sender: TObject);
var
  i, J: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if edit3.Text = '' then
    Exit;
  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('Update WFB_MJJY Set msfw=''' + trim(edit3.Text) + '''  ');
            sql.Add('where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            sql.Add('Update CK_BanCP_CR Set msfw=''' + trim(edit3.Text) + '''  ');
            sql.Add('where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');

            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('条码号：' + trim(Order_Main.fieldbyname('MJID').AsString) + ' 改为:' + trim(edit3.Text))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            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 TfrmJYOrderCDList.Button9Click(Sender: TObject);
var
  i, J: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if cxCurrencyEdit2.Text = '' then
    Exit;
  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('Update WFB_MJJY Set mjmaoz=mjmaoz-kongjia+''' + trim(cxCurrencyEdit2.Text) + '''  ');
            sql.Add(',MJQty4=MJQty4-kongjia+''' + trim(cxCurrencyEdit2.Text) + '''  ');
            sql.Add('where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');

            sql.Add('Update WFB_MJJY Set  kongjia=''' + trim(cxCurrencyEdit2.Text) + '''  ');

            sql.Add('where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');

            sql.Add('Update CK_BanCP_CR Set kgqty=(select mjmaoz from WFB_MJJY e where e.mjid=CK_BanCP_CR.mjid)  ');
            sql.Add('where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');

            SQL.Add('Update CK_BanCP_KC set KCkgQTY=(select mjmaoz from WFB_MJJY e where e.mjid=CK_BanCP_KC.mjid) ');

            SQL.Add('WHERE MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');

            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('条码号：' + trim(Order_Main.fieldbyname('MJID').AsString) + ' 改为:' + trim(cxCurrencyEdit2.Text))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            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;

end.

