unit U_JYJT_New;

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

type
  TfrmJYJT_New = 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;
    Label3: TLabel;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    MLConNo: TEdit;
    Label7: TLabel;
    Label8: TLabel;
    Label10: TLabel;
    JYNo: TEdit;
    Label2: TLabel;
    MLCodeName: TEdit;
    Panel4: TPanel;
    Label11: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    RichEdit1: TRichEdit;
    v1MLOrderNo: TcxGridDBColumn;
    v1GangNo: TcxGridDBColumn;
    v1KHName: TcxGridDBColumn;
    v1JYType: TcxGridDBColumn;
    v1JYKG: TcxGridDBColumn;
    v1JYLen: TcxGridDBColumn;
    v1JYUnit: TcxGridDBColumn;
    Label4: TLabel;
    v1MLCodeName: TcxGridDBColumn;
    v1RKType: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1FillTime: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1JYNo: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    TBZF: TToolButton;
    RM2: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    MovePanel2: TMovePanel;
    v1JYPZ: TcxGridDBColumn;
    v1JYJZ: TcxGridDBColumn;
    CDS_CD: TClientDataSet;
    RMCD: TRMGridReport;
    RMDBCD: TRMDBDataSet;
    CDS_LS: TClientDataSet;
    Label14: TLabel;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    gangNO: TEdit;
    MLColor: TEdit;
    RM3: TRMGridReport;
    v1Column31: TcxGridDBColumn;
    v1MLConNo: TcxGridDBColumn;
    Label22: TLabel;
    ISHZ: TComboBox;
   // RMllPDFExport1: TRMllPDFExport;
    RMXLSExport1: TRMXLSExport;
    CDS_HZ: TClientDataSet;
    RMDBHZ: TRMDBDataSet;
    RMDBPRT: TRMDBDataSet;
    CDS_PRT: TClientDataSet;
    v1MLColorNo: TcxGridDBColumn;
    v1MLHX: TcxGridDBColumn;
    Filler: TEdit;
    IsJYTime: TCheckBox;
    Panel6: TPanel;
    LBCPAP: TLabel;
    Label1: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1Column4: TcxGridDBColumn;
    Label6: TLabel;
    JYID: TEdit;
    cxTabControl1: TcxTabControl;
    Label9: TLabel;
    HZPS: TEdit;
    Label12: TLabel;
    HZQty: TEdit;
    Panel2: TPanel;
    Label13: TLabel;
    NewMLID: TEdit;
    ToolButton2: TToolButton;
    Panel3: TPanel;
    Label5: TLabel;
    FS: TComboBox;
    Label15: TLabel;
    RKType: TComboBox;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Label16: TLabel;
    BaoId: TEdit;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    MLOrderNo: TEdit;
    ADOQuery1: TADOQuery;
    KHNameJC: TComboBox;
    Label17: TLabel;
    KHNameJCC: TEdit;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Panel5: TPanel;
    Label18: TLabel;
    Edit1: TEdit;
    Button1: TButton;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    ADOQueryPrint2: TADOQuery;
    Label19: TLabel;
    LBQty: TEdit;
    Tv1Column13: TcxGridDBColumn;
    Label20: TLabel;
    MLHX: TEdit;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(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 N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBZFClick(Sender: TObject);
    procedure v1Column15PropertiesEditValueChanged(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure BTLPClick(Sender: TObject);
    procedure BTNPClick(Sender: TObject);
    procedure TCBNORChange(Sender: TObject);
    procedure MLColorKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure NewMLIDKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton2Click(Sender: TObject);
    procedure RKTypeChange(Sender: TObject);
    procedure BaoIdKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure Tv1Column4CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
    procedure Order_MainFilterRecord(DataSet: TDataSet; var Accept: Boolean);
    procedure Tv1DataControllerDataChanged(Sender: TObject);
    procedure KHNameJCChange(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
  private
    DQdate: TDateTime;
    FLeft, FTop: Integer;
    CurrentPage, RecordsNumber: Integer;
    canshu1, FNewMLID: string;
    procedure InitGrid();
    procedure GetHZData(fwhere: string);
    procedure InitForm();
    function Savedata(): Boolean;
    procedure GetCDSData();
    procedure GetBGData();
    procedure PrtDataMD(FMJID: string);
    { Private declarations }
  public
    FFInt: Integer;
    { Public declarations }
  end;

var
  frmJYJT_New: TfrmJYJT_New;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_MJEdit, U_Fun;

{$R *.dfm}
procedure TfrmJYJT_New.PrtDataMD(FMJID: string);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
  i: Integer;
begin

  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.*,B.*,C.* ');
    sql.Add(',CASE WHEN(isnull(CPBQName,'''')<>'''' ) THEN CPBQName ELSE ''通用标签'' END AS CPBQName1 ');
    sql.Add(',KHNameJC=(select KHNameJC  from KH_Main KM where KM.KHNO=B.KHNO)');
    SQL.Add('from JTJY_New A ');
    SQL.Add('inner join ML_OrderMainNew B on B.NewMLID=A.NewMLID ');
    SQL.Add('inner join ML_OrderSubidNew C on C.NewSubid=A.NewSubid ');
    sql.Add('where A.JYID=''' + trim(FMJID) + '''');
    Open;
  end;
  if cxTabControl1.TabIndex = 0 then
  begin
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ADOQueryPrint.fieldbyname('CPBQName1').AsString) + '.rmf';
  end
  else if cxTabControl1.TabIndex = 1 then
  begin
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\面布标签.rmf';
  end
  else if cxTabControl1.TabIndex = 2 then
  begin
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\底布标签.rmf';
  end
  else if cxTabControl1.TabIndex = 3 then
  begin
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ADOQueryPrint.fieldbyname('CPBQName1').AsString) + '.rmf';
  end;

  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(ADOQueryPrint.fieldbyname('JYID').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 FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE'] := fImagePath;
    RM2.LoadFromFile(fPrintFile);
    for i := 1 to StrToInt(FS.Text) do
    begin
      RM2.PrintReport;
    end;
  end
  else
  begin
    Application.MessageBox(pchar('没有找' + fPrintFile), '提示', 0);

    exit;
  end;
end;

procedure TfrmJYJT_New.FormDestroy(Sender: TObject);
begin
  frmJYJT_New := nil;
end;

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

procedure TfrmJYJT_New.FormCreate(Sender: TObject);
begin
  cxgrid1.Align := alClient;
  DQdate := SGetServerDate(ADOQueryTemp);
  canshu1 := Trim(DParameters1);
  FNewMLID := '';
end;

procedure TfrmJYJT_New.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('检验信息管理', Tv1, '检验管理');
end;

procedure TfrmJYJT_New.InitGrid();
var
  fwhere, Pwhere, FKHNameJC: string;
begin

  HZPS.Text := '';
  HZQty.Text := '';
  MovePanel2.Left := (self.Width - MovePanel2.Width) div 2;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  Pwhere := SGetFilters(Panel1, 1, 2);
  Tv1Column11.Visible := false;
  Tv1Column12.Visible := false;
  ToolButton4.Visible := false;
  if trim(Pwhere) <> '' then
    fwhere := fwhere + ' where ' + trim(Pwhere);

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select top ' + inttostr(RecordsNumber) + ' *  from  ');
      sql.Add('(select rownumber=Cast(row_number() over(order by AA.filltime desc) as int) ');
      sql.Add(',TotalCount=Cast(COUNT(1) OVER() AS int) ,* ');
      if (cxTabControl1.TabIndex = 1) or (cxTabControl1.TabIndex = 2) then
      begin
        sql.Add(',AVGLBLen=LBLen/CPPS2 ');
        sql.Add(',CASE WHEN(CPPS2 > 1) THEN CPPS2 END AS CPPS');  //不显示拆匹匹数为1的数据
      end;
      sql.Add('from (select isnull(LBID,'''') AS LBID2,');
      sql.Add('  A.*  ');
      sql.Add(' ,B.MLOrderNo,B.MLConNo,B.MLCodeName,B.KHName,KM.KHNameJC,KHNameJCC=KM.KHNameJC,isnull(B.ZCZ,99) AS ZCZ ');
      sql.Add(' ,C.MLColor,C.MLColorNo,C.MLHX,C.MLColorEng,C.MLColorNoKH ');
      sql.Add(',KHConNo=(select TOP 1 X.KHConNo from ML_ConMain X where X.MLConID=B.ConMainId)');
      sql.Add(',YWY=(select TOP 1 X.SYRYWY from ML_ConMain X where X.MLConID=B.ConMainId)');
      sql.Add(',LDY=(select TOP 1 X.SYRYWFZR from ML_ConMain X where X.MLConID=B.ConMainId)');
      if (cxTabControl1.TabIndex = 1) or (cxTabControl1.TabIndex = 2) then
      begin
//        sql.Add(',CASE WHEN((select count(*) from JTJY_New X where A.LBID=X.LBID and isnull(LBLen,0) > 0 ) > 1) THEN (select count(*) from JTJY_New X where A.LBID=X.LBID) END AS CPPS');
        sql.Add(',CASE WHEN((select count(*) from JTJY_New X where A.LBID=X.LBID and isnull(LBLen,0) > 0 ) > 1) THEN (select count(*) from JTJY_New X where A.LBID=X.LBID) ELSE 1 END AS CPPS2');
        sql.Add(',CASE WHEN((select sum(X.JYLen) from JTJY_New X where A.LBID=X.LBID and isnull(LBLen,0) > 0 ) > 0) THEN ((select sum(X.JYLen) from JTJY_New X where A.LBID=X.LBID)/LBLen * 100) END AS MC');
      end;
      sql.Add('  from JTJY_New A ');
      sql.Add('inner join ML_OrderMainNew B on B.NewMLID=A.NewMLID ');
      sql.Add('inner join ML_OrderSubidNew C on C.NewSubid=A.NewSubid ');
      sql.Add('inner join KH_Main KM on B.KHNO=KM.KHNO');
      sql.Add(' where 1=1');
      if Trim(FNewMLID) <> '' then
      begin
        sql.Add(' and A.NewMLID=''' + Trim(FNewMLID) + '''');
      end;
      if IsJYTime.Checked then
      begin
        sql.Add('and A.FillTime>=''' + trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
        sql.add(' and A.Filltime<''' + trim(FormatDateTime('yyyy-MM-dd', enddate.Date + 1)) + '''');
      end;
      if cxTabControl1.TabIndex = 0 then
      begin
        sql.Add(' and JYTYpe=''成品'' ');
      end
      else if cxTabControl1.TabIndex = 1 then
      begin
        sql.Add(' and JYTYpe=''面布'' ');
      end
      else if cxTabControl1.TabIndex = 2 then
      begin
        sql.Add(' and JYTYpe=''底布'' ');
      end
      else if cxTabControl1.TabIndex = 3 then
      begin
        sql.Add(' and JYTYpe=''退货'' ');

        Tv1Column11.Visible := true;
        Tv1Column12.Visible := true;
        ToolButton4.Visible := true;
      end;
      sql.Add('   ) AA');
      sql.Add(fwhere + ') temp_row');
      sql.Add(' where rownumber> ' + inttostr((CurrentPage - 1) * RecordsNumber));
      sql.Add('   order by LBID,Filltime desc');
      ShowMessage(SQL.Text);
      Open;
    end;

    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
    LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(Order_Main.FieldByName('TotalCount').AsInteger / RecordsNumber));
    MovePanel2.Visible := false;
    GetHZData(fwhere);
    FNewMLID := '';
  finally
    ADOQueryMain.EnableControls;
    MovePanel2.Visible := false;
  end;


  ////////////////////客户查询框///////////////////////


  with ADOQuery1 do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    sql.Add('select DISTINCT KM.KHNameJC AS Name from JTJY_New A ');
    sql.Add('inner join ML_OrderMainNew B on B.NewMLID=A.NewMLID ');
    sql.Add('inner join ML_OrderSubidNew C on C.NewSubid=A.NewSubid ');
    sql.Add('inner join KH_Main KM on B.KHNO=KM.KHNO');
    sql.Add(' where 1=1');
    if IsJYTime.Checked then
    begin
      sql.Add('and A.FillTime>=''' + trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
      sql.add(' and A.Filltime<''' + trim(FormatDateTime('yyyy-MM-dd', enddate.Date + 1)) + '''');
    end;
    if cxTabControl1.TabIndex = 0 then
    begin
      sql.Add(' and JYTYpe=''成品'' ');
    end
    else if cxTabControl1.TabIndex = 1 then
    begin
      sql.Add(' and JYTYpe=''面布'' ');
    end
    else if cxTabControl1.TabIndex = 2 then
    begin
      sql.Add(' and JYTYpe=''底布'' ');
    end
    else if cxTabControl1.TabIndex = 3 then
    begin
      sql.Add(' and JYTYpe=''退货'' ');
    end;
//      ShowMessage(SQL.Text);
    Open;
  end;
  if Trim(KHNameJC.Text) <> '' then
  begin
    KHNameJC.onchange := nil;
    FKHNameJC := Trim(KHNameJC.Text);
  end;
  KHNameJC.Items.Clear; //清空
  KHNameJC.Items.Add(Trim(''));

  while not ADOQuery1.eof do
  begin
    KHNameJC.Items.Add(ADOQuery1.fieldbyname('name').AsString);
    ADOQuery1.next;
  end;

  KHNameJC.ItemIndex := KHNameJC.Items.IndexOf(FKHNameJC);
  KHNameJC.onchange := KHNameJCChange;
    ////////////////////客户查询框///////////////////////
end;

procedure TfrmJYJT_New.GetHZData(fwhere: string);
begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' select HZPS=count(*),HZQty=sum(JYLen)   ');
    sql.Add('from (select  ');
    sql.Add(' * from (select ');
    sql.Add('  A.*  ');
    sql.Add(' ,B.MLOrderNo,B.MLConNo,B.MLCodeName,B.KHName,KM.KHNameJC,KHNameJCC=KM.KHNameJC ');
    sql.Add(' ,C.MLColor,C.MLColorNo,C.MLHX,C.MLColorEng,C.MLColorNoKH ');
    sql.Add(',KHConNo=(select TOP 1 X.KHConNo from ML_ConMain X where X.MLConID=B.ConMainId)');
    sql.Add('  from JTJY_New A ');
    sql.Add('inner join ML_OrderMainNew B on B.NewMLID=A.NewMLID ');
    sql.Add('inner join ML_OrderSubidNew C on C.NewSubid=A.NewSubid ');
    sql.Add('inner join KH_Main KM on B.KHNO=KM.KHNO');
    sql.Add(' where 1=1');
    if Trim(FNewMLID) <> '' then
    begin
      sql.Add(' and A.NewMLID=''' + Trim(FNewMLID) + '''');
    end;
    if IsJYTime.Checked then
    begin
      sql.Add('and A.FillTime>=''' + trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
      sql.add(' and A.Filltime<''' + trim(FormatDateTime('yyyy-MM-dd', enddate.Date + 1)) + '''');
    end;
    if cxTabControl1.TabIndex = 0 then
    begin
      sql.Add(' and JYTYpe=''成品'' ');
    end
    else if cxTabControl1.TabIndex = 1 then
    begin
      sql.Add(' and JYTYpe=''面布'' ');
    end
    else if cxTabControl1.TabIndex = 2 then
    begin
      sql.Add(' and JYTYpe=''底布'' ');
    end
    else if cxTabControl1.TabIndex = 3 then
    begin
      sql.Add(' and JYTYpe=''退货'' ');
    end;
    sql.Add('   ) AA');
    sql.Add(fwhere + ') temp_row');
//    ShowMessage(sql.text);
    Open;
  end;
  HZPS.Text := Trim(ADOQueryCmd.fieldbyname('HZPS').AsString);
  HZQty.Text := Trim(ADOQueryCmd.fieldbyname('HZQty').AsString);
end;

procedure TfrmJYJT_New.InitForm();
var
  fsj: string;
begin
  ReadCxGrid('检验信息管理', Tv1, '检验管理');
  BegDate.DateTime := SGetServerDate10(ADOQueryTemp) - 1;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
//  TBZF.Visible := false;
  if canshu1 = '高权限' then
  begin
    TBZF.Visible := true;
  end;
  //InitGrid();
end;

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

procedure TfrmJYJT_New.TBRafreshClick(Sender: TObject);
begin
  toolbar1.SetFocus;
  CurrentPage := 1;
  InitGrid();
end;

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

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

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

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

procedure TfrmJYJT_New.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 TfrmJYJT_New.N1Click(Sender: TObject);
begin
  SelOKNoFilter(Tv1, True);
end;

procedure TfrmJYJT_New.N2Click(Sender: TObject);
begin
  SelOKNoFilter(Tv1, False);
end;

procedure TfrmJYJT_New.TBZFClick(Sender: TObject);
var
  FYFID: string;
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_MLCP_CR where MJID=' + quotedstr(Trim(Order_Main.fieldbyname('JYID').AsString)));
          sql.Add('and CRFlag=''出库'' ');
          Open;
        end;
        if ADOQueryTemp.RecordCount >= 1 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('已出库不能删除!', '提示', 0);
          Exit;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JTJY_New where JYID=''' + Trim(Order_Main.fieldbyname('JYID').AsString) + '''');
          Open;
        end;
        FYFID := Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete JTJY_New where JYID=''' + Trim(Order_Main.fieldbyname('JYID').AsString) + '''');
          sql.Add('delete CK_MLCP_CR where MJID=''' + Trim(Order_Main.fieldbyname('JYID').AsString) + '''');
          sql.Add('delete CK_MLCP_KC where MJID=''' + Trim(Order_Main.fieldbyname('JYID').AsString) + '''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' exec P_Update_OrderSub :SubId');
          Parameters.ParamByName('SubId').Value := Trim(Order_Main.fieldbyname('NewSubid').AsString);
          ExecSQL;
        end;
        if cxTabControl1.TabIndex = 0 then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' exec ZDCK_Mian :MainID,:SubId,:DName');
            Parameters.ParamByName('MainID').Value := Trim(Order_Main.fieldbyname('NewMLid').AsString);
            Parameters.ParamByName('SubID').Value := Trim(Order_Main.fieldbyname('NewSubid').AsString);
            Parameters.ParamByName('DName').Value := Trim(DName);
            ExecSQL;
          end;
        end;

        if Trim(FYFID) <> '' then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' exec P_Update_THData :YFID');
            Parameters.ParamByName('YFID').Value := Trim(FYFID);
            ExecSQL;
          end;
        end;
        //更新棉&无纺布耗用
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          //实际
          sql.Add('Update ML_OrderZuHe Set MLJYQty=(select sum(JYLen) from JTJY_New A where A.NewMLID=ML_OrderZuHe.NewMLID and A.JYType=''成品'') ');
          sql.Add(' where NewMLID=''' + Trim(Order_Main.fieldbyname('NewMLid').AsString) + '''');
          sql.Add(' and isnull(MLName,'''')<>'''' and SSGS=''自己'' ');

          ExecSQL;
        end;
        //更新棉&无纺布耗用

        Order_Main.Delete;
      end
      else
        Next;
    end;
  end;
  Order_Main.EnableControls;
end;

function TfrmJYJT_New.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 TfrmJYJT_New.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 TfrmJYJT_New.GetBGData();
begin

end;

procedure TfrmJYJT_New.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 TfrmJYJT_New.CheckBox2Click(Sender: TObject);
begin
  SelOKNo(Order_Main, CheckBox2.Checked);
end;

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

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

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

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

procedure TfrmJYJT_New.ToolButton1Click(Sender: TObject);
var
  i: Integer;
begin
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while Locate('SSel', True, []) do
    begin
      PrtDataMD(Trim(Order_Main.fieldbyname('JYID').AsString));
      Edit;
      FieldByName('SSel').Value := False;
    end;
  end;
  Order_Main.EnableControls;

end;

procedure TfrmJYJT_New.cxTabControl1Change(Sender: TObject);
begin
  KHNameJC.ItemIndex := -1;
  InitGrid();
end;

procedure TfrmJYJT_New.NewMLIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    FNewMLID := Trim(NewMLID.Text);
    NewMLID.Text := '';
    InitGrid();
  end;
end;

procedure TfrmJYJT_New.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_MLCP_CR where MJID=''' + Trim(Order_Main.fieldbyname('JYID').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
      FJYID := Trim(Self.Order_Main.fieldbyname('JYID').AsString);
      if ShowModal = 1 then
      begin

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

procedure TfrmJYJT_New.RKTypeChange(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmJYJT_New.BaoIdKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
    BaoId.Text := '';
  end;
end;

procedure TfrmJYJT_New.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if (cxTabControl1.TabIndex = 1) or (cxTabControl1.TabIndex = 2) then
  begin
    if (AViewInfo.GridRecord.Values[Tv1Column6.Index] > AViewInfo.GridRecord.Values[Tv1Column5.Index]) and (AViewInfo.GridRecord.Values[Tv1Column5.Index] > 0) then
      ACanvas.Brush.Color := $7436FF;

    if AViewInfo.GridRecord.Values[Tv1Column5.Index] > 100 then
      ACanvas.Brush.Color := $66ff66;
  end;
end;

procedure TfrmJYJT_New.Tv1Column4CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
begin
  if (ARow1.Values[Tv1Column7.Index] = ARow2.Values[Tv1Column7.Index]) and (Trim(ARow2.Values[Tv1Column7.Index]) <> '') then
    AAreEqual := True
  else
    AAreEqual := False;
end;

procedure TfrmJYJT_New.Order_MainFilterRecord(DataSet: TDataSet; var Accept: Boolean);
begin

//  HZPS.Text := Tv1.DataController.Summary.FooterSummaryValues[9];
//  HZQty.Text := Tv1.DataController.Summary.FooterSummaryValues[3];

end;

procedure TfrmJYJT_New.Tv1DataControllerDataChanged(Sender: TObject);
begin

  if (Tv1.DataController.Summary.FooterSummaryValues[9] <> null) and (Tv1.DataController.Summary.FooterSummaryValues[3] <> null) then
  begin
    HZPS.Text := Tv1.DataController.Summary.FooterSummaryValues[9];
    HZQty.Text := Tv1.DataController.Summary.FooterSummaryValues[3];
    LBQty.Text := Tv1.DataController.Summary.FooterSummaryValues[11];
  end;

end;

procedure TfrmJYJT_New.KHNameJCChange(Sender: TObject);
begin

  CurrentPage := 1;
  InitGrid();

end;

procedure TfrmJYJT_New.ToolButton3Click(Sender: TObject);
begin
  cxGrid1.Enabled := False;
  Panel5.Visible := true;
  Edit1.Text := '';
end;

procedure TfrmJYJT_New.Button1Click(Sender: TObject);
begin
  cxGrid1.Enabled := true;
  Panel5.Visible := False;
  Edit1.Text := '';
  InitGrid();
end;

procedure TfrmJYJT_New.Edit1KeyPress(Sender: TObject; var Key: Char);
var
  FJYID: string;
begin
  if Key = #13 then
  begin
    FJYID := Trim(Edit1.Text);
    Edit1.Text := '';
    if (Copy(Trim(FJYID), 1, 2) = 'MB') or (Copy(Trim(FJYID), 1, 2) = 'DB') then
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from JTJY_New where JYID=''' + Trim(FJYID) + '''');
        Open;
      end;
      if ADOQueryTemp.IsEmpty then
      begin
        Application.MessageBox('条码不存在!', '提示', 0);
        Exit;
      end
      else
      begin
        if Trim(ADOQueryTemp.fieldbyname('JYTYpe').AsString) = '退货' then
        begin
          Application.MessageBox('类型已经是退货，不能扫描!', '提示', 0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('update JTJY_New set JYTYpe =''退货'' ');
          sql.Add(',THer =''' + trim(DName) + '''');
          sql.Add(',THTime = getdate() ');
          sql.Add('where JYID=''' + Trim(FJYID) + '''');

          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(FJYID))));
          sql.Add(',' + quotedstr(trim('成功')));
          sql.Add(')');
//          ShowMessage(sql.text);
          ExecSQL;
        end;
      end;

    end
    else
    begin
      Application.MessageBox('只有面布和底布能扫描退货!', '提示', 0);
      Exit;
    end;
    FJYID := '';
  end;
end;

procedure TfrmJYJT_New.ToolButton4Click(Sender: TObject);
var
  FYFID: string;
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 JTJY_New where JYTYpe=''退货'' and JYID=''' + Trim(Order_Main.fieldbyname('JYID').AsString) + '''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('条码删除!', '提示', 0);
          Exit;
        end;

        if (Copy(Trim(Order_Main.fieldbyname('JYID').AsString), 1, 2) = 'MB') or (Copy(Trim(Order_Main.fieldbyname('JYID').AsString), 1, 2) = 'DB') then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            if (Copy(Trim(Order_Main.fieldbyname('JYID').AsString), 1, 2) = 'MB') then
            begin
              sql.Add('update JTJY_New set JYTYpe =''面布'' ');
            end
            else if (Copy(Trim(Order_Main.fieldbyname('JYID').AsString), 1, 2) = 'DB') then
            begin
              sql.Add('update JTJY_New set JYTYpe =''底布'' ');
            end;

            sql.Add(',THer = null');
            sql.Add(',THTime = null ');
            sql.Add('where JYID=''' + Trim(Order_Main.fieldbyname('JYID').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('JYID').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
//            ShowMessage(sql.text);
            ExecSQL;
          end;
        end
        else
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('无法撤销!', '提示', 0);
          Exit;
        end;
        Order_Main.Delete;
      end
      else
        Next;
    end;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmJYJT_New.ToolButton5Click(Sender: TObject);
var
  fPrintFile: string;
begin
  if Order_Main.IsEmpty then
    EXIT;

  if Order_Main.Locate('SSel', True, []) = false then
  begin
    Application.MessageBox('请选择数据', '提示', 0);
    EXIT;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TbForBatch where DName=' + quotedstr(Trim(DName)));
    SQL.Add('and MainType=' + quotedstr('退货码单打印'));
    ExecSQL;
  end;

  while Order_Main.Locate('SSel', True, []) do
  begin
    with Self.ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' insert into TbForBatch(DName,MainType,Value1) ');
      sql.Add(' values( ' + quotedstr(Trim(DName)) + ',' + quotedstr('退货码单打印') + ',' + quotedstr(Trim(Self.Order_Main.FieldByName('JYID').AsString)) + ')');
//      ShowMessage(sql.text);
      ExecSQL;
    end;
    with Order_Main do
    begin
      Edit;
      FieldByName('SSel').Value := False;
      Post;
    end;
    Next;
  end;
  ExportFtErpFile(trim('退货码单') + '.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim('退货码单') + '.rmf';

  with ADOQueryPrint2 do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_CKMD8 @DName=' + quotedstr(Trim(DName)));
//      ShowMessage(SQL.text);
    Open;
  end;

  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

end.

