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,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, cxContainer, ShellAPI,
  cxCurrencyEdit, MMSystem, 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
  TfrmJYOrderCDOne = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: 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;
    OrderNo: TEdit;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    PRTMF: TEdit;
    Label7: TLabel;
    Label8: TLabel;
    Label10: TLabel;
    MJID: TEdit;
    Label2: TLabel;
    CDList: TEdit;
    Panel4: TPanel;
    Label11: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    RichEdit1: TRichEdit;
    v1Column1: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1PRTCodeName: TcxGridDBColumn;
    Label5: TLabel;
    PRTCodeName: TEdit;
    v1Column16: TcxGridDBColumn;
    Label6: TLabel;
    MJStr2: TComboBox;
    v1Column17: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    Filler: TComboBox;
    TBManage: TToolButton;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    TBZF: TToolButton;
    TBPrintBq: TToolButton;
    RM2: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    v1Column25: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    RadioGroup1: TRadioGroup;
    MovePanel2: TMovePanel;
    v1Column27: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    TBPrintJYBB: TToolButton;
    v1Column14: TcxGridDBColumn;
    CDS_CD: TClientDataSet;
    RMCD: TRMGridReport;
    RMDBCD: TRMDBDataSet;
    CDS_LS: TClientDataSet;
    v1Column15: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    MJType: TEdit;
    TCPDB: TToolButton;
    v1BaoNo: TcxGridDBColumn;
    v1BaoID: TcxGridDBColumn;
    Label4: TLabel;
    Label9: TLabel;
    BAOID: TEdit;
    BAONO: TEdit;
    TBPrintBao: TToolButton;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    Label12: TLabel;
    SOrddefstr1: TEdit;
    Label13: TLabel;
    Label14: TLabel;
    PRTHX: TEdit;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    MJstr4: TEdit;
    Label15: TLabel;
    v1Column13: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    PRTkuanNo: TEdit;
    Label16: TLabel;
    CDS_HZ: TClientDataSet;
    RMDBHZ: TRMDBDataSet;
    RM3: TRMGridReport;
    Panel5: TPanel;
    Label28: TLabel;
    ComboBox5: TComboBox;
    Button5: TButton;
    Button6: TButton;
    TBDW: TToolButton;
    TBMS: TToolButton;
    Panel2: TPanel;
    Label17: TLabel;
    TZFS: TComboBox;
    Button1: TButton;
    Button2: TButton;
    TZSL: TEdit;
    Label18: TLabel;
    Panel3: TPanel;
    Label19: TLabel;
    Panel6: TPanel;
    Image1: TImage;
    Button7: TButton;
    RadioGroup2: TRadioGroup;
    ComboBox1: TComboBox;
    TBPrint: TToolButton;
    CDS_PRT: TClientDataSet;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    Panel7: TPanel;
    Label24: TLabel;
    Label25: TLabel;
    Label26: TLabel;
    Label27: TLabel;
    Label29: TLabel;
    Label30: TLabel;
    Panel8: TPanel;
    Image3: TImage;
    Button9: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    N3: TMenuItem;
    Label20: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    Label23: TLabel;
    Edit3: TEdit;
    Tv1Column1: TcxGridDBColumn;
    CheckBox2: TCheckBox;
    TBCK: TToolButton;
    Label1: TLabel;
    CKFlag: TComboBox;
    TBNoCK: TToolButton;
    Panel9: TPanel;
    Label32: TLabel;
    Button11: TButton;
    Button12: TButton;
    cxCurrencyEdit2: TcxCurrencyEdit;
    TBPZ: TToolButton;
    ToolButton9: TToolButton;
    TBSMCK: TToolButton;
    Panel_SMCK: TPanel;
    Label31: TLabel;
    Edit_CKMJID: TEdit;
    Button_CKSM: TButton;
    PRTColor: TEdit;
    Tv1Column2: TcxGridDBColumn;
    conNo: TEdit;
    Label33: TLabel;
    ToolButton1: TToolButton;
    Panel11: TPanel;
    Label34: TLabel;
    Label35: TLabel;
    ComboBox2: TComboBox;
    Button3: TButton;
    Button4: TButton;
    Edit4: TEdit;
    Tv1Column3: TcxGridDBColumn;
    Panel12: TPanel;
    Label36: TLabel;
    ComboBox3: TComboBox;
    Button8: TButton;
    Button10: TButton;
    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 TBPrintBqClick(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure PRTColorChange(Sender: TObject);
    procedure TBPrintJYBBClick(Sender: TObject);
    procedure v1Column15PropertiesEditValueChanged(Sender: TObject);
    procedure TCPDBClick(Sender: TObject);
    procedure PRTCodeNameChange(Sender: TObject);
    procedure TBPrintBaoClick(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure TBDWClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure TBMSClick(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure Image3Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure Edit3KeyPress(Sender: TObject; var Key: Char);
    procedure TBCKClick(Sender: TObject);
    procedure TBNoCKClick(Sender: TObject);
    procedure Button11Click(Sender: TObject);
    procedure Button12Click(Sender: TObject);
    procedure TBPZClick(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure v1Column22PropertiesChange(Sender: TObject);
    procedure Image1Click(Sender: TObject);
    procedure Edit_CKMJIDKeyPress(Sender: TObject; var Key: Char);
    procedure Button_CKSMClick(Sender: TObject);
    procedure TBSMCKClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
  private
    DQdate: TDateTime;
    FLeft, FTop: Integer;
    FID: string;
    procedure InitGrid();
    procedure InitSum();
    procedure InitForm();
    procedure GetCDSData();
    procedure GetBGData();
    procedure JSbaoNum();
    { Private declarations }
  public
    FFInt: Integer;

    { Public declarations }
  end;

var
  frmJYOrderCDOne: TfrmJYOrderCDOne;

implementation

uses
  U_DataLink, U_OrderInPut, U_Fun, U_MJEdit, U_CPDBAO, U_MJSJFX;

{$R *.dfm}
procedure TfrmJYOrderCDOne.JSbaoNum();
var
  i: integer;
  baoID: string;
  strlist: Tstringlist;
begin
  i := 0;
  baoID := '';
  if Order_Main.IsEmpty then
  begin
    Label12.Caption := '包数：0';
    exit;
  end;
  strlist := Tstringlist.Create;
  try
    with Order_Main do
    begin
      DisableControls;
      first;
      while not eof do
      begin

        if (trim(fieldbyname('BaoNO').AsString) <> '') then
        begin
          if strlist.IndexOf(trim(fieldbyname('subID').AsString) + trim(fieldbyname('BaoNO').AsString)) < 0 then
          begin
            strlist.Add(trim(fieldbyname('subID').AsString) + trim(fieldbyname('BaoNO').AsString));
          end;
        end;
     {   IF (trim(fieldbyname('BaoID').AsString)<>trim(baoID)) and (trim(fieldbyname('BaoID').AsString)<>'') then
        begin
          i:=i+1;
          baoID:=trim(fieldbyname('BaoID').AsString);
        end;  }
        Next;
      end;
      EnableControls;
    end;
    Label12.Caption := '包数：' + inttostr(strlist.Count);
  finally
    strlist.Free;
  end;
end;

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
      Close;
      Filtered := False;
      sql.Clear;
      sql.Add('select A.*,');
      sql.Add('C.OrderNo,C.conNo,C.MPRTCodeName,C.MPRTMF,C.MPRTKZ,B.*,');
      sql.Add('KouFenQty=(select Sum(X.KouFenQty) from WFB_MJJY_CD X where X.MJID=A.MJID),');
      sql.Add('CDQty=(select count(X.MJID) from WFB_MJJY_CD X where X.MJID=A.MJID),');
      sql.Add('CDName=(select CDName from WFB_MJJY_CD X where X.MJID=A.MJID)');
      SQL.Add('from WFB_MJJY A');
      sql.Add('inner join JYOrder_sub B on B.subID=A.subID ');
      sql.Add('inner join JYOrder_Main C on C.MainID=A.MainID ');
      sql.Add('where A.FillTime>=''' + FormatDateTime('yyyy-MM-dd', BegDate.DateTime) + ''' ');
      sql.Add('and  A.FillTime<''' + FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1) + ''' ');
      if Trim(DParameters2) = '次品' then
      begin
        sql.add('and mjtype=''次品'' ');
      end;
      sql.add('order by A.mainid,B.prtcolor,MJstr4,A.mjxh');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  tbfind.Click;
end;

procedure TfrmJYOrderCDOne.InitSum();
var
  sumP, sumLen, sumMZ, sumJZ: double;
begin
  if Order_Main.IsEmpty then
    exit;
  try
    sumP := 0;
    sumLen := 0;
    sumMZ := 0;
    sumJZ := 0;
//    fid:= Order_Main.fieldbyName('MJID').AsString;
    with Order_Main do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean then
        begin
          sumP := sumP + 1;
          sumLen := RoundFloat(sumLen + Order_Main.fieldbyname('mjlen').AsFloat, 2);
          sumMZ := sumMZ + Order_Main.fieldbyname('mjmaoz').AsFloat;
          sumJZ := sumJZ + Order_Main.fieldbyname('mjqty4').AsFloat;
        end;

        next;
      end;
      EnableControls;
    end;

    label22.Caption := '匹数:' + floattostr(sumP);
    label21.Caption := '长度:' + floattostr(sumLen);
    label20.Caption := '毛重:' + floattostr(sumMZ);
    label23.Caption := '净重:' + floattostr(sumJZ);
    Order_Main.Locate('mjid', fid, []);
  except
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

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

procedure TfrmJYOrderCDOne.TBFindClick(Sender: TObject);
var
  fsj: string;
begin
  if ADOQueryMain.Active = False then
    Exit;
  if RadioGroup1.ItemIndex = 0 then
  begin
    fsj := ' HXFlag=0 ';
  end
  else if RadioGroup1.ItemIndex = 1 then
  begin
    fsj := ' HXFlag=1 ';
  end
  else if RadioGroup1.ItemIndex = 0 then
  begin
    fsj := '';
  end;
  if Trim(SGetFilters(Panel1, 1, 2)) = '' then
  begin
    fsj := fsj;
  end
  else
  begin
    fsj := SGetFilters(Panel1, 1, 2) + ' and ' + fsj;
  end;
  SDofilter(ADOQueryMain, fsj);
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
  JSbaoNum();
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.Visible := True;
  MovePanel2.Refresh;
  InitGrid();
  MovePanel2.Visible := False;
end;

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

procedure TfrmJYOrderCDOne.FormShow(Sender: TObject);
begin
  InitForm();
  if Trim(DParameters2) = '管理' then
  begin
    TBZF.Visible := True;
    TBManage.Visible := True;
    v1Column22.visible := True;
    v1Column15.Options.Focusing := True;
    TCPDB.Visible := True;
    TBPrintBao.Visible := True;

    TBck.Visible := true;
    TBSMck.Visible := True;
    TBnoCK.Visible := True;
    TBDW.Visible := True;
    TBMS.Visible := True;
    TBPZ.Visible := True;

  end;
  if Trim(DParameters2) = '成品管理' then
  begin
    v1Column22.visible := False;
//    TCPDB.Visible:=True;
//    ToolButton3.Visible := True;
  end;
  if Trim(DParameters2) = '成品打包' then
  begin
    TBZF.Visible := False;
    TBManage.Visible := False;
    v1Column22.visible := False;
    TBck.Visible := false;
    TBSMck.Visible := false;
    TBnoCK.Visible := false;
    TBDW.Visible := false;
    TBMS.Visible := false;
    TBPZ.Visible := false;
    TCPDB.Visible := True;
    TBPrintBao.Visible := True;
  end;
  if Trim(DParameters2) = '' then
  begin
    TBZF.Visible := False;
    TBManage.Visible := False;
    v1Column22.visible := False;
    TBck.Visible := false;
    TBSMck.Visible := false;
    TBnoCK.Visible := false;
    TBDW.Visible := false;
    TBMS.Visible := false;
    TBPZ.Visible := false;
//    TCPDB.Visible:=False;
  end;
end;

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

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

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

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

procedure TfrmJYOrderCDOne.TBManageClick(Sender: TObject);
begin
  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;
      MJstr4.Text := Trim(ADOTmp.fieldbyname('MJstr4').AsString);
      MJXH.Text := Trim(ADOTmp.fieldbyname('MJXH').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);
      if Trim(ADOTmp.fieldbyname('MJTypeOther').AsString) = 'M' then
      begin
        MJTypeOther.ItemIndex := 0
      end
      else
        MJTypeOther.ItemIndex := 1;

//     MJTypeOther.text := Trim(ADOTmp.fieldbyname('MJTypeOther').AsString);
      M.Caption := Trim(Trim(ADOTmp.fieldbyname('MJTypeOther').AsString));
      Ma.Caption := Trim(Trim(ADOTmp.fieldbyname('MJStr1').AsString));
//      if Trim(m.Caption) = '' then
//      begin
//        m.Caption := Trim(ADOTmp.fieldbyname('AOrddefstr3').AsString);
//      end;
//      if Trim(Ma.Caption) = '' then
//      begin
//        Ma.Caption := Trim(ADOTmp.fieldbyname('AOrddefstr2').AsString);
//      end;
      if Trim(Trim(ADOTmp.fieldbyname('MJType').AsString)) = '正品' then
      begin
        RadioGroup1.ItemIndex := 0
      end
      else if Trim(Trim(ADOTmp.fieldbyname('MJType').AsString)) = '次品' then
      begin
        RadioGroup1.ItemIndex := 1;
      end
      else if Trim(Trim(ADOTmp.fieldbyname('MJType').AsString)) = '留样' then
      begin
        RadioGroup1.ItemIndex := 2;
      end;
      with ADOTmp do
      begin
        Close;
        sql.Clear;
        SQL.Add('select * from WFB_MJJY_CD A   where A.MJID=''' + Trim(MJID.Text) + '''');
        Open;
      end;
      Label2.Caption := Trim(MJID.Text);
      Label2.Visible := True;
      MJID.Text := '';
      SCreateCDS20(ADOTmp, Order_MJ);
      SInitCDSData20(ADOTmp, Order_MJ);
      if ShowModal = 1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmMJEdit.Free;
  end;
end;

procedure TfrmJYOrderCDOne.N1Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, True);
  InitSum();
end;

procedure TfrmJYOrderCDOne.N2Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, False);
  InitSum();
end;

procedure TfrmJYOrderCDOne.TBZFClick(Sender: TObject);
begin
  if Order_Main.Locate('ssel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要作废数据吗？作废后数据不能恢复。', '提示', 32 + 4) <> IDYES then
    Exit;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean = True then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_BanCP_CR where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty = False then
        begin
          Order_Main.EnableControls;
          Application.MessageBox('已产生入库数据不能删除!', '提示', 0);
          Exit;
        end
        else
        begin
          //begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.add('insert into WFB_MJJY_Del select * from WFB_MJJY where mjid=''' + trim(Order_Main.fieldbyname('MJID').AsString) + ''' ');
            sql.Add('update WFB_MJJY_Del Set DelTime=Getdate(),Deler=''' + trim(DName) + ''' where mjid=''' + trim(Order_Main.fieldbyname('MJID').AsString) + ''' ');
            sql.Add('delete WFB_MJJY where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            sql.Add('delete WFB_MJJY_CD where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            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(Order_Main.FieldByName('subID').AsString) + '缸号:' + trim(Order_Main.FieldByName('MJstr4').AsString) + '卷号:' + trim(Order_Main.FieldByName('MJXH').AsString) + '长度:' + trim(Order_Main.FieldByName('MJLen').AsString) + '净重:' + trim(Order_Main.FieldByName('MJQty4').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            ExecSQL;
          end;
         // end;
        end;
        Order_Main.Delete;
      end
      else
        Next;
    end;
  end;
  Order_Main.EnableControls;

end;

procedure TfrmJYOrderCDOne.TBPrintBqClick(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;
  Panel12.Left := (self.Width - Panel12.Width) div 2;
  Panel12.Visible := true;

//  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;
//        try
//          Moudle := LoadLibrary('MakeQRBarcode.dll');
//          @Makebar := GetProcAddress(Moudle, 'Make');
//          @Mixtext := GetProcAddress(Moudle, 'MixText');
//          Txt := Trim(Order_Main.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);
//          Order_Main.EnableControls;
//          exit;
//        end;
//        with ADOQueryPrint do
//        begin
//          Close;
//          SQL.Clear;
//          sql.Add('select A.MJID,A.mjstr3,QtyUnit=A.MJTypeOther,B.orderNo,B.conNo,B.customerNoName,B.LBName ');
//          sql.Add(',EngColor=(select max(Note) from KH_Zdy X where X.ZDYName=C.PRTColor and X.Type=''OrdColor'' ) ');
//          sql.Add(',B.MPRTCF,C.*,A.* ');
//          sql.Add(' from WFB_MJJY A');
//          sql.Add(' inner join JYOrder_Main B On A.Mainid=B.Mainid');
//          sql.Add(' inner join JYOrder_Sub C on A.SubId=C.SubId');
//          SQL.Add(' where A.MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
//          Open;
//        end;
//        if Trim(ADOQueryPrint.fieldbyname('SLbName').AsString) <> '' then
//        begin
//          ExportFtErpFile(Trim(ADOQueryPrint.fieldbyname('SLbName').AsString), ADOQueryCmd);
//          fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ADOQueryPrint.fieldbyname('SLbName').AsString);
//        end
//        else
//        begin
//          ExportFtErpFile('通用标签.rmf', ADOQueryCmd);
//          fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\通用标签.rmf';
//        end;
//
//        if FileExists(fPrintFile) then
//        begin
//          RMVariables['QRBARCODE'] := fImagePath;
//          RM2.LoadFromFile(fPrintFile);
//          RM2.PrintReport;
//        end
//        else
//        begin
//          Order_Main.EnableControls;
//          Application.MessageBox(PChar('没有找到' + ExtractFilePath(Application.ExeName) + 'Report\卷标签.rmf'), '提示', 0);
//          Exit;
//        end;
//      end;
//      Next;
//    end;
//  end;
//  Order_Main.EnableControls;
end;

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

procedure TfrmJYOrderCDOne.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Length(OrderNo.Text) < 2 then
      Exit;
    MovePanel2.Visible := True;
    MovePanel2.Refresh;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Close;
        Filtered := False;
        sql.Clear;
        sql.Add('select A.*,');
        sql.Add('C.OrderNo,C.MPRTCodeName,C.MPRTMF,C.MPRTKZ,B.*,');
        sql.Add('KouFenQty=(select Sum(X.KouFenQty) from WFB_MJJY_CD X where X.MJID=A.MJID),');
        sql.Add('CDQty=(select count(X.MJID) from WFB_MJJY_CD X where X.MJID=A.MJID)');
        SQL.Add('from WFB_MJJY A');
        sql.Add('inner join JYOrder_sub B on B.subID=A.subID ');
        sql.Add('inner join JYOrder_Main C on C.MainID=A.MainID ');
        sql.Add('where C.orderNo=' + quotedstr(trim(orderNo.Text)));
        if CheckBox2.Checked = True then
        begin
          sql.Add('and A.FillTime>=''' + FormatDateTime('yyyy-MM-dd', BegDate.DateTime) + ''' ');
          sql.Add('and A.FillTime<''' + FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1) + ''' ');
        end;
        Open;
      end;
      SCreateCDS20(ADOQueryMain, Order_Main);
      SInitCDSData20(ADOQueryMain, Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
    MovePanel2.Visible := False;
   // JSbaoNum();
  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.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.TBPrintJYBBClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;

  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;
  panel3.Visible := true;

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.TCPDBClick(Sender: TObject);
begin
  frmCPDBao := TfrmCPDBao.Create(Application);
  with frmCPDBao do
  begin
    show;
  end;
end;

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

procedure TfrmJYOrderCDOne.TBPrintBaoClick(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if Order_Main.IsEmpty then
    Exit;
  if trim(Order_Main.FieldByName('BaoID').AsString) = '' then
  begin
    application.MessageBox('未打包不能打印', '提示');
    exit;
  end;
  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(Order_Main.fieldbyname('BaoID').AsString);
    fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
      CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
    if FileExists(fImagePath) then
      DeleteFile(fImagePath);
    Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
  except
    application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
    Order_Main.EnableControls;
    exit;
  end;
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.Baoid,A.BaoNo,A.MJTypeOther as QtyUnit,D.OrderNo,D.conNo,D.customerNoName,D.MprtCodeName,D.Mprtspec,D.OrdPerson1,D.MPRTKuanNO,D.LBName,D.NLBName,C.KHorderNo,');
    sql.Add('PRTCodeName=DBO.F_Get_Order_SubStr(BaoID,''BNCodeName''),');
    sql.Add('PRTColor=DBO.F_Get_Order_SubStr(BaoID,''BNColor''),SOrddefstr1=DBO.F_Get_Order_SubStr(BaoID,''BNSOrddefstr1''),');
    sql.Add('SOrddefstr4=DBO.F_Get_Order_SubStr(BaoID,''BNSOrddefstr4''),PRtHX=DBO.F_Get_Order_SubStr(BaoID,''BNPRtHX''), ');
    sql.Add('Mjstr4=DBO.F_Get_Order_SubStr(BaoID,''BNGangNo''), ');
    sql.Add('BNMJLENLIST=DBO.F_Get_Order_SubStr(BaoID,''BNMJLENLIST''), ');
    sql.Add('PRTkuanNo=DBO.F_Get_Order_SubStr(BaoID,''BNPrtkuanno''),');
    sql.Add('khConNo=(select top 1 khConNo from JYOrderCon_Main X where X.ConNO=D.conNO), ');
    sql.Add('MprtCodeNameEng=(select top 1 Note from KH_Zdy X where X.zdyName=D.MprtCodeName), ');
    SQL.ADD('count(A.MJID) as JSl,sum(A.MJMaoZ) MJMAOZ,sum(MJQty2) as MJQty2,sum(MJQty3) as MJQty3,sum(MJQty4) as MJQty4,SUM(A.MJLen)as MJLen');
    sql.Add('from WFB_MJJY A');
    sql.Add(' inner join JYOrder_Sub C on C.SubID=A.SubID');
    sql.Add(' inner join JYOrder_Main D on D.MainID=A.MainID');
    SQL.Add(' where A.BaoID=''' + Trim(Order_Main.fieldbyname('BaoID').AsString) + '''');
    SQL.ADD('group by A.Baoid,A.BaoNo,A.MJTypeOther,D.OrderNo,D.conNo,D.customerNoName,D.MprtCodeName,D.Mprtspec,D.OrdPerson1,D.MPRTKuanNO,D.LBName,D.NLBName,C.KHorderNo');
    Open;
  end;
  if Trim(ADOQueryPrint.fieldbyname('NLBName').AsString) <> '' then
  begin
    ExportFtErpFile(Trim(ADOQueryPrint.fieldbyname('NLBName').AsString), ADOQueryCmd);
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ADOQueryPrint.fieldbyname('NLBName').AsString);
  end
  else
  begin
    ExportFtErpFile('通用包标签.rmf', ADOQueryCmd);
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\通用包标签.rmf';
  end;

  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE'] := fImagePath;
    RM2.LoadFromFile(fPrintFile);
    RM2.ShowReport;
    //RM2.printReport;
  end
  else
  begin
    Order_Main.EnableControls;
    Application.MessageBox(PChar('没有找' + trim(fPrintFile)), '提示', 0);
    Exit;
  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 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) + '''');
            ExecSQL;
          end;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存成功！', '提示信息');
    Panel5.Visible := false;
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存失败！', '提示信息', 0);
  end;
end;

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

procedure TfrmJYOrderCDOne.Button1Click(Sender: TObject);
var
  SL: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if trim(TZFS.Text) = '' then
  begin
    Application.MessageBox('调整方式不能为空!', '提示', 0);
    Exit;
  end;
  if trim(TZSL.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
          if Trim(TZFS.Text) = '增加' then
          begin
            SL := FloatToStr(Order_Main.fieldbyname('MJLen').AsFloat + strtofloat(TZSL.Text));
          end;
          if Trim(TZFS.Text) = '减少' then
          begin
            SL := FloatToStr(Order_Main.fieldbyname('MJLen').AsFloat - strtofloat(TZSL.Text));
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update WFB_MJJY Set MJLen=''' + SL + ''' ');
            sql.Add('where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            ExecSQL;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update CK_BanCP_CR Set Qty=''' + SL + ''' ');
            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;
    TZFS.Text := '';
    TZSL.Text := '';
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存失败！', '提示信息', 0);
  end;
end;

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

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

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

  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;

  LBName := RadioGroup2.Items.Strings[RadioGroup2.ItemIndex];
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf';
  ExportFtErpFile(Trim(LBName) + '.rmf', ADOQueryCmd);

  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
//          IF Trim(Order_Main.fieldbyname('APID').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
    RM2.LoadFromFile(fPrintFile);
    RM2.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + trim(fPrintFile)), '提示信息', 0);
    exit;
  end;

end;

procedure TfrmJYOrderCDOne.TBPrintClick(Sender: TObject);
var
  fPrintFile, fPrintFile10, FMainID: string;
  sqlStr: string;
begin

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

  ExportFtErpFile(Trim(ComboBox1.Text) + '.rmf', ADOQueryCmd);

  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;
  sqlStr := '';
  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;
          sqlStr := sqlStr + 'insert into TBSubID(SubId,Dname) ';
          sqlStr := sqlStr + ' values( ';
          sqlStr := sqlStr + ' ''' + Trim(Order_Main.fieldbyname('MJID').AsString) + ''', ';
          sqlStr := sqlStr + ' ''' + Trim(DCode) + ''' ';
          sqlStr := sqlStr + ' ) ';
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;

  Order_Main.EnableControls;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(sqlStr);
//    showmessage(sql.text);
    ExecSQL;
  end;

  if (trim(ComboBox1.Text) = '检验码单(英文)') or (trim(ComboBox1.Text) = '检验码单(中文)') then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMDHAEIL ');
      sql.add('@DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_HZ);
    SInitCDSData20(ADOQueryTemp, CDS_HZ);

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Do_PrintMd_HZ ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      sql.add(',@flag=''2'' ');
      Open;
    end;
  end;

  if (trim(ComboBox1.Text) = '检验码单(打包2)') then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMDHAEIL_copy1_copy1 ');
      sql.add('@DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_HZ);
    SInitCDSData20(ADOQueryTemp, CDS_HZ);

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Do_PrintMd_HZ ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      sql.add(',@flag=''2'' ');
      Open;
    end;
  end;

  if (trim(ComboBox1.Text) = '检验码单(打包)') then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMDHAEIL_copy1 ');
      sql.add('@DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_HZ);
    SInitCDSData20(ADOQueryTemp, CDS_HZ);

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Do_PrintMd_HZ ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      sql.add(',@flag=''2'' ');
      Open;
    end;
  end;

  if trim(ComboBox1.Text) = '检验码单汇总' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMDHZ ');
      SQL.Add('@mainid='''' ');
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      SQL.Add(',@DLen =' + edit3.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_HZ);
    SInitCDSData20(ADOQueryTemp, CDS_HZ);

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Do_PrintMd_HZ ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      sql.add(',@flag=''2'' ');
      Open;
    end;
  end;

  if (trim(ComboBox1.Text) = '检验码单(克重)') or (trim(ComboBox1.Text) = '电子码单') then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD_copy3 ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_HZ);
    SInitCDSData20(ADOQueryTemp, CDS_HZ);

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Do_PrintMd_HZ ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      sql.add(',@flag=''5'' ');
      Open;
    end;
  end;

  if (trim(ComboBox1.Text) = '检验码单(颜色)') or (trim(ComboBox1.Text) = '检验码单(染色米数)') then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD_copy4 ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_HZ);
    SInitCDSData20(ADOQueryTemp, CDS_HZ);

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Do_PrintMd_HZ ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      sql.add(',@flag=''4'' ');
      Open;
    end;
  end;

  if trim(ComboBox1.Text) = '检验码单(码数)' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
//      sql.add('exec P_Print_CKMDYDL_MS ');
//      sql.add('@DName=' + quotedstr(Trim(DCode)));
      sql.add('exec P_Print_CKMD_copy4_copy3 '); //P_Print_CKMD_copy4_copy3
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
//      sql.add(',@Cnum=''4''');

      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_HZ);
    SInitCDSData20(ADOQueryTemp, CDS_HZ);

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Do_PrintMd_HZ_copy2 ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      sql.add(',@flag=''4'' ');
      Open;
    end;
  end;
  if trim(ComboBox1.Text) = '检验码单(一包多卷)' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMDYDL_BS ');
      sql.add('@DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_HZ);
    SInitCDSData20(ADOQueryTemp, CDS_HZ);

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Do_PrintMd_HZ_copy2 ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      sql.add(',@flag=''4'' ');
      Open;
    end;
  end;
  if (trim(ComboBox1.Text) = '检验码单(款号)') then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD_copy4_copy2_copy1 ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_HZ);
    SInitCDSData20(ADOQueryTemp, CDS_HZ);

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Do_PrintMd_HZ_copy1 ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      sql.add(',@flag=''4'' ');
      Open;
    end;
  end;

  if (trim(ComboBox1.Text) = '检验码单(色号)') then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD_copy4_copy2 ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_HZ);
    SInitCDSData20(ADOQueryTemp, CDS_HZ);

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Do_PrintMd_HZ_copy1 ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      sql.add(',@flag=''4'' ');
      Open;
    end;
  end;

  if (trim(ComboBox1.Text) = '检验码单(颜色疵点)') then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD_copy4_copy1 ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_HZ);
    SInitCDSData20(ADOQueryTemp, CDS_HZ);

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Do_PrintMd_HZ ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      sql.add(',@flag=''4'' ');
      Open;
    end;
  end;

  if (trim(ComboBox1.Text) = '检验码单(缸号疵点)') then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD_copy5 ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_HZ);
    SInitCDSData20(ADOQueryTemp, CDS_HZ);

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Do_PrintMd_HZ ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      sql.add(',@flag=''4'' ');
      Open;
    end;
  end;

  if (trim(ComboBox1.Text) = '检验码单(缸号)') or (trim(ComboBox1.Text) = '检验码单(印花米数)') then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_HZ);
    SInitCDSData20(ADOQueryTemp, CDS_HZ);

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Do_PrintMd_HZ ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      sql.add(',@flag=''4'' ');
      Open;
    end;
  end;

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

procedure TfrmJYOrderCDOne.N3Click(Sender: TObject);
begin
  panel7.Visible := true;
  Label27.Caption := trim(Order_Main.Fieldbyname('prtcolor').AsString);
  Label30.Caption := trim(Order_Main.Fieldbyname('orderno').AsString);
  FID := trim(Order_Main.Fieldbyname('MJID').AsString);
end;

procedure TfrmJYOrderCDOne.Image3Click(Sender: TObject);
begin
  panel7.Visible := False;
end;

procedure TfrmJYOrderCDOne.Button9Click(Sender: TObject);
begin
  with Order_Main do
  begin
    DisableControls;
    First;
    while not Eof do
    begin
      if fieldbyname('orderno').Asstring = Label30.Caption then
      begin
        if (fieldbyname('mjxh').asfloat >= strtofloat(edit1.Text)) and (fieldbyname('mjxh').asfloat <= strtofloat(edit2.Text)) then
        begin
          Edit;
          FieldByName('Ssel').Value := True;
          Post;
        end;
      end;
      next;
    end;
    EnableControls;
  end;
  panel7.Visible := false;
  Label30.Caption := '';
  Label27.Caption := '';
  edit1.Text := '';
  edit2.text := '';
  InitSum();
end;

procedure TfrmJYOrderCDOne.ComboBox1Change(Sender: TObject);
begin
  edit3.Visible := False;
  Edit3.Text := '';
  if ComboBox1.ItemIndex = 4 then
  begin
    edit3.Visible := True;
  end;
end;

procedure TfrmJYOrderCDOne.Edit3KeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in ['0'..'9', '.', #8]) then
    Key := #0
end;

procedure TfrmJYOrderCDOne.TBCKClick(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
          //begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('update WFB_MJJY set CKFlag=''已出库'' where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
          ExecSQL;
        end;

        Edit;
        FieldByName('CKFlag').Value := '已出库';
        Post;

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

end;

procedure TfrmJYOrderCDOne.TBNoCKClick(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
          //begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('update WFB_MJJY set CKFlag=''未出库'' where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
          ExecSQL;
        end;

        Edit;
        FieldByName('CKFlag').Value := '未出库';
        FieldByName('SSel').AsBoolean := False;
        Post;
        Next;
      end
      else
        Next;
    end;
  end;
  Order_Main.EnableControls;

end;

procedure TfrmJYOrderCDOne.Button11Click(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;
  J := floattostr(strtofloatdef(trim(cxCurrencyEdit2.Text), 0));
  try
    Order_Main.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean = True then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update WFB_MJJY Set MJQty3=''' + trim(J) + ''' ');
            sql.Add('where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            sql.Add('Update WFB_MJJY Set MJMaoZ=MJQty4+MJQty3  ');
            sql.Add('where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            sql.Add(' update CK_BanCP_CR set KGQty=(select mjmaoz from wfb_mjjy X where X.mjid=CK_BanCP_CR.mjid)');
            sql.Add(' where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');

            ExecSQL;
          end;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存成功！', '提示信息');
    Panel9.Visible := false;
    TBRafresh.Click;
    exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存失败！', '提示信息', 0);
  end;
end;

procedure TfrmJYOrderCDOne.Button12Click(Sender: TObject);
begin
  Panel9.Visible := false;
end;

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

procedure TfrmJYOrderCDOne.ToolButton9Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try

    frmMJSJFX := TfrmMJSJFX.Create(Application);
    with frmMJSJFX do
    begin
      frmMJSJFX.OrderNo.Text := Self.Order_Main.fieldbyname('orderno').AsString;
      frmMJSJFX.OrderNo.Hint := Self.Order_Main.fieldbyname('Mainid').AsString;
      if ShowModal = 1 then
      begin

      end;

    end;
  finally
    frmMJSJFX.Free;
  end;
end;

procedure TfrmJYOrderCDOne.v1Column22PropertiesChange(Sender: TObject);
begin
  FID := trim(Order_Main.Fieldbyname('MJID').AsString);
  Order_Main.Edit;
  if Order_Main.FieldByName('ssel').AsBoolean = false then
  begin
    Order_Main.FieldByName('ssel').AsBoolean := True;
  end
  else
    Order_Main.FieldByName('ssel').AsBoolean := False;
  InitSum();
end;

procedure TfrmJYOrderCDOne.Image1Click(Sender: TObject);
begin
  panel3.Visible := false;
end;

procedure TfrmJYOrderCDOne.Edit_CKMJIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if trim(Edit_CKMJID.Text) = '' then
      exit;

    try
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('update WFB_MJJY set CKFlag=''已出库'' where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
        ExecSQL;
      end;
      PlaySound('wav\正确.wav', 0, SND_FILENAME or SND_ASYNC);
    except
      PlaySound('wav\扫描错误.wav', 0, SND_FILENAME or SND_ASYNC);
    end;
    Edit_CKMJID.Text := '';
  end;
end;

procedure TfrmJYOrderCDOne.Button_CKSMClick(Sender: TObject);
begin
  Panel_SMCK.Visible := false;
end;

procedure TfrmJYOrderCDOne.TBSMCKClick(Sender: TObject);
begin
  Panel_SMCK.Left := (self.Width - Panel_SMCK.Width) div 2;
  Panel_SMCK.Visible := true;
  edit_CKMJID.SetFocus;
end;

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

procedure TfrmJYOrderCDOne.Button3Click(Sender: TObject);
var
  SL: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if trim(ComboBox2.Text) = '' then
  begin
    Application.MessageBox('调整方式不能为空!', '提示', 0);
    Exit;
  end;
  if trim(Edit4.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
          if Trim(ComboBox2.Text) = '乘' then
          begin
            SL := FloatToStr(Order_Main.fieldbyname('MJLen').AsFloat * strtofloat(Edit4.Text));
          end;
          if Trim(ComboBox2.Text) = '除' then
          begin
            SL := FloatToStr(Order_Main.fieldbyname('MJLen').AsFloat / strtofloat(Edit4.Text));
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update WFB_MJJY Set MJLen=''' + SL + ''' ');
            sql.Add('where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            ExecSQL;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update CK_BanCP_CR Set Qty=''' + SL + ''' ');
            sql.Add('where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            ExecSQL;
          end;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存成功！', '提示信息');
    Panel11.Visible := false;
    ComboBox2.Text := '';
    Edit4.Text := '';
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存失败！', '提示信息', 0);
  end;
end;

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

procedure TfrmJYOrderCDOne.Button10Click(Sender: TObject);
begin
  Panel12.Visible := false;
end;

procedure TfrmJYOrderCDOne.Button8Click(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  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;
        try
          Moudle := LoadLibrary('MakeQRBarcode.dll');
          @Makebar := GetProcAddress(Moudle, 'Make');
          @Mixtext := GetProcAddress(Moudle, 'MixText');
          Txt := Trim(Order_Main.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);
          Order_Main.EnableControls;
          exit;
        end;
        with ADOQueryPrint do
        begin
          Close;
          SQL.Clear;
          sql.Add('select A.MJID,A.mjstr3,QtyUnit=A.MJTypeOther,B.orderNo,B.conNo,B.customerNoName,B.LBName ');
          sql.Add(',EngColor=(select max(Note) from KH_Zdy X where X.ZDYName=C.PRTColor and X.Type=''OrdColor'' ) ');
          sql.Add(',B.MPRTCF,C.*,A.* ');
          sql.Add(' from WFB_MJJY A');
          sql.Add(' inner join JYOrder_Main B On A.Mainid=B.Mainid');
          sql.Add(' inner join JYOrder_Sub C on A.SubId=C.SubId');
          SQL.Add(' where A.MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
          Open;
        end;
        if Trim(ADOQueryPrint.fieldbyname('SLbName').AsString) <> '' then
        begin
          ExportFtErpFile(Trim(ADOQueryPrint.fieldbyname('SLbName').AsString), ADOQueryCmd);
          fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ADOQueryPrint.fieldbyname('SLbName').AsString);
        end
        else
        begin
          ExportFtErpFile('通用标签.rmf', ADOQueryCmd);
          fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\通用标签.rmf';
        end;

        if FileExists(fPrintFile) then
        begin
          RMVariables['QRBARCODE'] := fImagePath;
          RM2.LoadFromFile(fPrintFile);
          RM2.DefaultCopies := strtointdef(trim(ComboBox3.Text), 1);
          RM2.PrintReport;
        end
        else
        begin
          Order_Main.EnableControls;
          Application.MessageBox(PChar('没有找到' + ExtractFilePath(Application.ExeName) + 'Report\卷标签.rmf'), '提示', 0);
          Exit;
        end;
      end;
      Next;
    end;
  end;
  Order_Main.EnableControls;
  Panel12.Visible := false;
end;

procedure TfrmJYOrderCDOne.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  Column7Value: string;
  Column7Index: Integer;
  I: Integer;
begin
  // 安全检查
  if (AViewInfo.GridRecord = nil) then
    Exit;

  try
    // 查找 Column7 的索引
    Column7Index := -1;
    for I := 0 to Tv1.ColumnCount - 1 do
    begin
      if Tv1.Columns[I].Name = 'v1Column7' then  // 
      begin
        Column7Index := I;
        Break;
      end;
    end;
    
    if Column7Index <> -1 then
    begin
      Column7Value := VarToStr(AViewInfo.GridRecord.Values[Column7Index]);
      
      // 根据 Column7 的值设置颜色
      if Pos('横档', Column7Value) > 0 then
      begin
        ACanvas.Brush.Color := clRed;
        ACanvas.Font.Color := clBlack;
      end
      else if Pos('氨纶氧化', Column7Value) > 0 then
      begin
        ACanvas.Brush.Color := clYellow;
        ACanvas.Font.Color := clBlack;
      end
      else if Pos('色差', Column7Value) > 0 then
      begin
        ACanvas.Brush.Color := clBlue;
        ACanvas.Font.Color := clWhite;
      end
      else if Pos('脏污', Column7Value) > 0 then
      begin
        ACanvas.Brush.Color := clGreen;
        ACanvas.Font.Color := clBlack;
      end
      else if Pos('其他', Column7Value) > 0 then
      begin
        ACanvas.Brush.Color := clPurple;
        ACanvas.Font.Color := clWhite;
      end
      else if Pos('漏底丝', Column7Value) > 0 then
      begin
        ACanvas.Brush.Color := RGB(255, 192, 203);
        ACanvas.Font.Color := clBlack;
      end;
    end;
  except
    // 忽略异常
  end;
end;

end.

