unit U_CKProductBCPInList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView,
  cxGridTableView, cxGridBandedTableView, cxGridDBBandedTableView,
  cxGridLevel, cxClasses, cxControls, cxGridCustomView, cxGridDBTableView,
  cxGrid, StdCtrls, ComCtrls, ExtCtrls, ToolWin, cxGridCustomPopupMenu,
  cxGridPopupMenu, ADODB, DBClient, cxDropDownEdit, cxCheckBox, RM_Common,
  RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, Menus;

type
  TfrmCKProductBCPInList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    Label4: TLabel;
    MPRTCodeName: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Label5: TLabel;
    orderNo: TEdit;
    Label6: TLabel;
    MJID: TEdit;
    Label8: TLabel;
    MPRTKZ: TEdit;
    Label9: TLabel;
    MPRTMF: TEdit;
    Label7: TLabel;
    CPType: TComboBox;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label10: TLabel;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    TBRKCX: TToolButton;
    v1Column4: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1Column11: TcxGridDBColumn;
    PRTColor: TComboBox;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    AOrdDefStr1: TComboBox;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    TBBao: TToolButton;
    TBRKD: TToolButton;
    ComboBox1: TComboBox;
    RM2: TRMGridReport;
    RMDBMain10: TRMDBDataSet;
    ClientDataSet1: TClientDataSet;
    RM3: TRMGridReport;
    RMDBMX: TRMDBDataSet;
    CDS_BaoNo: TClientDataSet;
    CDS_Juan: TClientDataSet;
    CDS_JuanPRT: TClientDataSet;
    CDS_CX: TClientDataSet;
    CDS_BaoPRT: TClientDataSet;
    CDS_WXPTPrint: TClientDataSet;
    CDS_WXPT: TClientDataSet;
    RMDBWXPTPrint: TRMDBDataSet;
    MJXHM: TEdit;
    Label11: TLabel;
    CheckBox1: TCheckBox;
    Label12: TLabel;
    RKOrdID: TEdit;
    Panel4: TPanel;
    Label13: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    RichEdit1: TRichEdit;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    conNo: TEdit;
    Label14: TLabel;
    KHConNo: TEdit;
    Label15: TLabel;
    APID: TEdit;
    Label16: TLabel;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    Label17: TLabel;
    PRTKuanNO: TEdit;
    begtime: TDateTimePicker;
    endTime: TDateTimePicker;
    Label18: TLabel;
    RKPlace: TEdit;
    CheckBox2: TCheckBox;
    TBNote: TToolButton;
    Panel2: TPanel;
    Label19: TLabel;
    Edit_CRnote: TEdit;
    Button1: TButton;
    Button2: TButton;
    v1Column24: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure orderNoChange(Sender: TObject);
    procedure orderNoKeyPress(Sender: TObject; var Key: Char);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBRKCXClick(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure PRTColorChange(Sender: TObject);
    procedure AOrdDefStr1Change(Sender: TObject);
    procedure TBBaoClick(Sender: TObject);
    procedure TBRKDClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure CheckBox2Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure TBNoteClick(Sender: TObject);

  private
    procedure InitGrid();
    procedure GetBaleData();
    procedure GetBaoData();
    procedure GetJuanData();
    procedure GetLotData();
    procedure Print_WXPT();
    { Private declarations }
  public
      FLeft,FTop:Integer;
    { Public declarations }
  end;

var
  frmCKProductBCPInList: TfrmCKProductBCPInList;

implementation
uses
  U_DataLink,U_Fun;

{$R *.dfm}

procedure TfrmCKProductBCPInList.FormDestroy(Sender: TObject);
begin
  frmCKProductBCPInList:=nil;
end;


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

procedure TfrmCKProductBCPInList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
  EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp)
end;

procedure TfrmCKProductBCPInList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      SQL.Add('select A.*,B.OrderNo,B.conNo,B.MPRTCodeName,B.MPRTSpec,B.MprtkuanNo,C.prtkuanNo,C.PRTColor,C.SOrddefstr4,B.MPRTMF,B.MPRTKZ,D.MJXH,C.PRTHX,D.MJQty3,D.MJQty4');
      sql.Add(',isnull(customerNoName,B.OrderNo) KHName');
      sql.Add(',KHConNo=(select Top 1 KHConNo from JYOrderCon_Main JCM where JCM.ConNo=B.OrderNo)');
      sql.Add(',MPRTECodeName=(select Top 1 MPRTCodeName from JYOrderCon_Main JCM where JCM.ConNo=B.OrderNo)');
      sql.Add(',D.mjstr4 as AOrdDefStr1,cast(D.MJXH as varchar) as MJXHM');
      sql.add('from CK_BanCP_CR 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(' inner join WFB_MJJY D on A.MJId=D.MJId');
     // sql.Add(' inner join CK_SXPB_CR E on A.APID=E.SPID');
      sql.add('where A.FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime)+' '+FormatDateTime('HH:mm',BegTime.Time))+'''');
      sql.Add(' and A.FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',enddate.DateTime)+' '+FormatDateTime('HH:mm',endTime.Time))+'''');
      SQL.Add(' and CRType=''检验入库'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCKProductBCPInList.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;

procedure TfrmCKProductBCPInList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  end;
end;

procedure TfrmCKProductBCPInList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('半成品仓库入库',Tv1,'半成品仓库');
  Close;
end;

procedure TfrmCKProductBCPInList.FormShow(Sender: TObject);
begin

  ReadCxGrid('半成品仓库入库',Tv1,'半成品仓库');
  if Trim(DParameters2)='管理' then
  begin
    TBRKCX.Visible:=True;
    TBBao.Visible:=True;
    TBRKD.Visible:=True;
    TBNote.Visible:=True;
  end else
  begin
  //  v1Column4.Visible:=False;
    TBBao.Visible:=False;
    TBRKD.Visible:=False;
    TBNote.Visible:=false;
  end;
  //InitGrid();
end;

procedure TfrmCKProductBCPInList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;
  TcxGridToExcel('入库列表',cxGrid2);
end;

procedure TfrmCKProductBCPInList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCKProductBCPinList.Print_WXPT();
var
  j,z:Integer;
  FGangNo,FHZ:String;
begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_CKMD1 ');
    sql.add('@mainID='+quotedstr(Trim(CDS_Main.FieldByName('RKOrdID').AsString)));
    sql.add(',@DName='+quotedstr(Trim(DCode)));
    Open;
  end;
  SCreateCDS20(ADOQueryCmd,CDS_WXPT);
  SInitCDSData20(ADOQueryCmd,CDS_WXPT);
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('select MainId=Cast('''' as varchar(20)),SubId=Cast('''' as varchar(20)),PONO=Cast('''' as varchar(20)),InvNo=Cast('''' as varchar(20)),');
    sql.Add(' MPrtCodeName=Cast('''' as varchar(200)),PRTColor=Cast('''' as varchar(20)),GangNo=Cast('''' as varchar(20)),BaoNo=Cast('''' as varchar(20)),MZQty=Cast(0 as decimal(18,2)),');
    sql.Add(' RollNo0=Cast('''' as varchar(20)),RollNo1=Cast('''' as varchar(20)),RollNo2=Cast('''' as varchar(20)),ITEM=Cast('''' as varchar(20)),');
    sql.Add(' LenQty0=Cast(0 as decimal(18,2)),LenQty1=Cast(0 as decimal(18,2)),LenQty2=Cast(0 as decimal(18,2)),');
    sql.Add(' KGQty0=Cast(0 as decimal(18,2)),KGQty1=Cast(0 as decimal(18,2)),KGQty2=Cast(0 as decimal(18,2)),');
    sql.Add(' MKGQty0=Cast(0 as decimal(18,2)),MKGQty1=Cast(0 as decimal(18,2)),MKGQty2=Cast(0 as decimal(18,2)),');
    sql.Add(' ZS0=Cast(0 as decimal(18,2)),Zs1=Cast(0 as decimal(18,2)),Zs2=Cast(0 as decimal(18,2)),');
    sql.add(' CRTime=cast(null as datetime),JQty=Cast(0 as decimal(18,2)),XH=Cast(0 as int),SumMZQty=Cast(0 as decimal(18,2)),SumJZQty=Cast(0 as decimal(18,2)),SumlenQty=Cast(0 as decimal(18,2)),SumRoll=Cast(0 as decimal(18,2)),');
    sql.Add(' SumRollGanNo=Cast(0 as decimal(18,2)),SumRollColor=Cast(0 as decimal(18,2)),SumZJ=Cast(0 as decimal(18,2)),');
    sql.add(' QtyUnit=cast('''' as varchar(20))');
    Open;
  end;
  SCreateCDS20(ADOQueryCmd,CDS_WXPTPrint);
  SInitCDSData20(ADOQueryCmd,CDS_WXPTPrint);
  if CDS_WXPTPrint.IsEmpty=False then
  begin
    CDS_WXPTPrint.Delete;
  end;
  j:=0;z:=0;FGangNo:='';
  CDS_WXPT.DisableControls;
  CDS_WXPTPrint.DisableControls;
  with CDS_WXPT do
  begin
    First;
    while not Eof do
    begin
      with CDS_WXPTPrint do
      begin
        if CDS_WXPTPrint.IsEmpty then
        begin
          CDS_WXPTPrint.Append;
          CDS_WXPTPrint.FieldByName('MZQty').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
          CDS_WXPTPrint.FieldByName('INVNO').Value:=CDS_WXPT.fieldbyname('orderno').Value;
          CDS_WXPTPrint.FieldByName('GangNo').Value:=CDS_WXPT.fieldbyname('GangNo').Value;
          CDS_WXPTPrint.FieldByName('BaoNo').Value:=CDS_WXPT.fieldbyname('BaoNo').Value;
          CDS_WXPTPrint.FieldByName('PONO').Value:=CDS_WXPT.fieldbyname('PONO').Value;
          CDS_WXPTPrint.FieldByName('QtyUnit').Value:=trim(CDS_WXPT.fieldbyname('QtyUnit').Asstring);
          CDS_WXPTPrint.FieldByName('CRTime').Value:=CDS_WXPT.fieldbyname('CRTime').asdatetime;
          CDS_WXPTPrint.FieldByName('MprtCodeName').Value:=CDS_WXPT.fieldbyname('MprtCodeName').Value;
          CDS_WXPTPrint.FieldByName('PRTColor').Value:=CDS_WXPT.fieldbyname('PRTColor').Value;
          CDS_WXPTPrint.FieldByName('XH').Value:=0;
          CDS_WXPTPrint.FieldByName('LenQty0').Value:=CDS_WXPT.fieldbyname('qty').AsFloat;
          CDS_WXPTPrint.FieldByName('Zs0').Value:=CDS_WXPT.fieldbyname('MjQty2').AsFloat;
          CDS_WXPTPrint.FieldByName('RollNo0').Value:=CDS_WXPT.fieldbyname('RollNo').Value;
          CDS_WXPTPrint.FieldByName('MKGQty0').Value:=CDS_WXPT.fieldbyname('KGQty').AsFloat;
          CDS_WXPTPrint.FieldByName('KGQty0').Value:=CDS_WXPT.fieldbyname('MJqty4').AsFloat;
          CDS_WXPTPrint.FieldByName('SumJZqty').Value:=CDS_WXPT.fieldbyname('SumJZqty').AsFloat;
          CDS_WXPTPrint.FieldByName('SumMZqty').Value:=CDS_WXPT.fieldbyname('SumMZqty').AsFloat;
          CDS_WXPTPrint.FieldByName('Sumlenqty').Value:=CDS_WXPT.fieldbyname('Sumlenqty').AsFloat;
          CDS_WXPTPrint.FieldByName('sumroll').Value:=CDS_WXPT.fieldbyname('sumroll').AsFloat;
          CDS_WXPTPrint.FieldByName('SumRollGanNo').Value:=CDS_WXPT.fieldbyname('SumRollGanNo').AsFloat;
          CDS_WXPTPrint.FieldByName('SumRollColor').Value:=CDS_WXPT.fieldbyname('SumRollColor').AsFloat;
          CDS_WXPTPrint.FieldByName('SumZJ').Value:=CDS_WXPT.fieldbyname('SumZJ').AsFloat;
          CDS_WXPTPrint.FieldByName('SubId').Value:=CDS_WXPT.fieldbyname('SubId').Value;
          CDS_WXPTPrint.FieldByName('MainId').Value:=CDS_WXPT.fieldbyname('MainId').Value;
          CDS_WXPTPrint.Post;
          FHZ:=Trim(CDS_WXPT.fieldbyname('PRTColor').AsString)+Trim(CDS_WXPT.fieldbyname('Baono').AsString)+Trim(CDS_WXPT.fieldbyname('GangNo').AsString);
          Z:=0;
          j:=0;
        end
        else
        begin
          if (Trim(CDS_WXPT.fieldbyname('PRTColor').AsString)+Trim(CDS_WXPT.fieldbyname('Baono').AsString)+Trim(CDS_WXPT.fieldbyname('GangNo').AsString))<>FHZ then
          begin
            CDS_WXPTPrint.Append;
            CDS_WXPTPrint.FieldByName('MZQty').Value:=trim(CDS_WXPT.fieldbyname('KGQty').Asstring);
            CDS_WXPTPrint.FieldByName('INVNO').Value:=trim(CDS_WXPT.fieldbyname('orderno').Asstring);
            CDS_WXPTPrint.FieldByName('GangNo').Value:=trim(CDS_WXPT.fieldbyname('GangNo').Asstring);
            CDS_WXPTPrint.FieldByName('BaoNo').Value:=Trim(CDS_WXPT.fieldbyname('BaoNo').Asstring);
            CDS_WXPTPrint.FieldByName('PONO').Value:=trim(CDS_WXPT.fieldbyname('PONO').Asstring);
            CDS_WXPTPrint.FieldByName('QtyUnit').Value:=Trim(CDS_WXPT.fieldbyname('QtyUnit').Asstring);
            CDS_WXPTPrint.FieldByName('CRTime').Value:=CDS_WXPT.fieldbyname('CRTime').asdatetime;
            CDS_WXPTPrint.FieldByName('MprtCodeName').Value:=CDS_WXPT.fieldbyname('MprtCodeName').Value;
            CDS_WXPTPrint.FieldByName('PRTColor').Value:=trim(CDS_WXPT.fieldbyname('PRTColor').Asstring);
            CDS_WXPTPrint.FieldByName('XH').Value:=J+1;
            CDS_WXPTPrint.FieldByName('LenQty0').Value:=CDS_WXPT.fieldbyname('qty').AsFloat;
            CDS_WXPTPrint.FieldByName('Zs0').Value:=CDS_WXPT.fieldbyname('MjQty2').AsFloat;
            CDS_WXPTPrint.FieldByName('RollNo0').Value:=trim(CDS_WXPT.fieldbyname('RollNo').Asstring);
            CDS_WXPTPrint.FieldByName('MKGQty0').Value:=CDS_WXPT.fieldbyname('KGQty').AsFloat;
            CDS_WXPTPrint.FieldByName('KGQty0').Value:=CDS_WXPT.fieldbyname('MJqty4').AsFloat;
            CDS_WXPTPrint.FieldByName('SumJZqty').Value:=CDS_WXPT.fieldbyname('SumJZqty').AsFloat;
            CDS_WXPTPrint.FieldByName('SumMZqty').Value:=CDS_WXPT.fieldbyname('SumMZqty').AsFloat;
            CDS_WXPTPrint.FieldByName('Sumlenqty').Value:=CDS_WXPT.fieldbyname('Sumlenqty').AsFloat;
            CDS_WXPTPrint.FieldByName('sumroll').Value:=CDS_WXPT.fieldbyname('sumroll').AsFloat;
            CDS_WXPTPrint.FieldByName('SumRollGanNo').Value:=CDS_WXPT.fieldbyname('SumRollGanNo').AsFloat;
            CDS_WXPTPrint.FieldByName('SumRollColor').Value:=CDS_WXPT.fieldbyname('SumRollColor').AsFloat;
            CDS_WXPTPrint.FieldByName('SumZJ').Value:=CDS_WXPT.fieldbyname('SumZJ').AsFloat;
            CDS_WXPTPrint.FieldByName('SubId').Value:=CDS_WXPT.fieldbyname('SubId').Asstring;
            CDS_WXPTPrint.FieldByName('MainId').Value:=CDS_WXPT.fieldbyname('MainId').Asstring;
            CDS_WXPTPrint.Post;
            Z:=0;
            j:=j+1;
            FHZ:=Trim(CDS_WXPT.fieldbyname('PRTColor').AsString)+Trim(CDS_WXPT.fieldbyname('Baono').AsString)+Trim(CDS_WXPT.fieldbyname('GangNo').AsString);
            end
          else
          begin
            if Z<3 then
            begin
              CDS_WXPTPrint.Locate('XH',j,[]);
              with CDS_WXPTPrint do
              begin
                Edit;
                FieldByName('MZQty').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
                FieldByName('INVNO').Value:=CDS_WXPT.fieldbyname('orderno').Value;
                FieldByName('GangNo').Value:=CDS_WXPT.fieldbyname('GangNo').Value;
                CDS_WXPTPrint.FieldByName('PONO').Value:=CDS_WXPT.fieldbyname('PONO').Value;
                CDS_WXPTPrint.FieldByName('QtyUnit').Value:=trim(CDS_WXPT.fieldbyname('QtyUnit').Asstring);
                CDS_WXPTPrint.FieldByName('CRTime').Value:=CDS_WXPT.fieldbyname('CRTime').asdatetime;
                FieldByName('BaoNO').Value:=CDS_WXPT.fieldbyname('BaoNO').Value;
                CDS_WXPTPrint.FieldByName('MprtCodeName').Value:=CDS_WXPT.fieldbyname('MprtCodeName').Value;
                CDS_WXPTPrint.FieldByName('PRTColor').Value:=CDS_WXPT.fieldbyname('PRTColor').Value;
                FieldByName('LenQty'+Trim(IntToStr(Z))).Value:=CDS_WXPT.fieldbyname('qty').AsFloat;
                FieldByName('Zs'+Trim(IntToStr(Z))).Value:=CDS_WXPT.fieldbyname('MJQty2').AsFloat;
                CDS_WXPTPrint.FieldByName('RollNo'+Trim(IntToStr(Z))).Value:=CDS_WXPT.fieldbyname('RollNo').Value;
                CDS_WXPTPrint.FieldByName('MKGQty'+Trim(IntToStr(Z))).Value:=CDS_WXPT.fieldbyname('KGQty').AsFloat;
                CDS_WXPTPrint.FieldByName('KGQty'+Trim(IntToStr(Z))).Value:=CDS_WXPT.fieldbyname('MJqty4').AsFloat;
                CDS_WXPTPrint.FieldByName('SumJZqty').Value:=CDS_WXPT.fieldbyname('SumJZqty').AsFloat;
                CDS_WXPTPrint.FieldByName('SumMZqty').Value:=CDS_WXPT.fieldbyname('SumMZqty').AsFloat;
                CDS_WXPTPrint.FieldByName('Sumlenqty').Value:=CDS_WXPT.fieldbyname('Sumlenqty').AsFloat;
                CDS_WXPTPrint.FieldByName('sumroll').Value:=CDS_WXPT.fieldbyname('sumroll').AsFloat;
                CDS_WXPTPrint.FieldByName('SumRollGanNo').Value:=CDS_WXPT.fieldbyname('SumRollGanNo').AsFloat;
                CDS_WXPTPrint.FieldByName('SumRollColor').Value:=CDS_WXPT.fieldbyname('SumRollColor').AsFloat;
                CDS_WXPTPrint.FieldByName('SumZJ').Value:=CDS_WXPT.fieldbyname('SumZJ').AsFloat;
                FieldByName('SubId').Value:=CDS_WXPT.fieldbyname('SubId').Value;
                FieldByName('MainId').Value:=CDS_WXPT.fieldbyname('MainId').Value;
                Post;
              end;
            end
            else
            begin
              CDS_WXPTPrint.Append;
              CDS_WXPTPrint.FieldByName('MZQty').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
              CDS_WXPTPrint.FieldByName('INVNO').Value:=CDS_WXPT.fieldbyname('orderno').Value;
              CDS_WXPTPrint.FieldByName('GangNo').Value:=CDS_WXPT.fieldbyname('GangNo').Value;
              CDS_WXPTPrint.FieldByName('BaoNO').Value:=CDS_WXPT.fieldbyname('BaoNO').Value;
              CDS_WXPTPrint.FieldByName('PONO').Value:=CDS_WXPT.fieldbyname('PONO').Value;
              CDS_WXPTPrint.FieldByName('QtyUnit').Value:=trim(CDS_WXPT.fieldbyname('QtyUnit').Asstring);
              CDS_WXPTPrint.FieldByName('CRTime').Value:=CDS_WXPT.fieldbyname('CRTime').asdatetime;
              CDS_WXPTPrint.FieldByName('MprtCodeName').Value:=CDS_WXPT.fieldbyname('MprtCodeName').Value;
              CDS_WXPTPrint.FieldByName('PRTColor').Value:=CDS_WXPT.fieldbyname('PRTColor').Value;
              CDS_WXPTPrint.FieldByName('XH').Value:=J+1;
              CDS_WXPTPrint.FieldByName('LenQty0').Value:=CDS_WXPT.fieldbyname('qty').AsFloat;
              CDS_WXPTPrint.FieldByName('ZS0').Value:=CDS_WXPT.fieldbyname('MJQty2').AsFloat;
              CDS_WXPTPrint.FieldByName('RollNo0').Value:=CDS_WXPT.fieldbyname('RollNo').Value;
              CDS_WXPTPrint.FieldByName('MKGQty0').Value:=CDS_WXPT.fieldbyname('kgQty').AsFloat;
              CDS_WXPTPrint.FieldByName('KGQty0').Value:=CDS_WXPT.fieldbyname('MJqty4').AsFloat;
              CDS_WXPTPrint.FieldByName('SumJZqty').Value:=CDS_WXPT.fieldbyname('SumJZqty').AsFloat;
              CDS_WXPTPrint.FieldByName('SumMZqty').Value:=CDS_WXPT.fieldbyname('SumMZqty').AsFloat;
              CDS_WXPTPrint.FieldByName('Sumlenqty').Value:=CDS_WXPT.fieldbyname('Sumlenqty').AsFloat;
              CDS_WXPTPrint.FieldByName('sumroll').Value:=CDS_WXPT.fieldbyname('sumroll').AsFloat;
              CDS_WXPTPrint.FieldByName('SumRollGanNo').Value:=CDS_WXPT.fieldbyname('SumRollGanNo').AsFloat;
              CDS_WXPTPrint.FieldByName('SumRollColor').Value:=CDS_WXPT.fieldbyname('SumRollColor').AsFloat;
              CDS_WXPTPrint.FieldByName('SumZJ').Value:=CDS_WXPT.fieldbyname('SumZJ').AsFloat;
              CDS_WXPTPrint.FieldByName('SubId').Value:=CDS_WXPT.fieldbyname('SubId').Value;
              CDS_WXPTPrint.FieldByName('MainId').Value:=CDS_WXPT.fieldbyname('MainId').Value;
              CDS_WXPTPrint.Post;
              Z:=0;
              j:=j+1;
            end;
          end;
        end
      end;
      Z:=Z+1;
      Next;
    end;
  end;
  CDS_WXPT.EnableControls;
  CDS_WXPTPrint.EnableControls;
end;

procedure TfrmCKProductBCPInList.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductBCPInList.TBPrintClick(Sender: TObject);
var
  fPrintFile,fPrintFile10,FMainID:String;
begin
  if CDS_Main.IsEmpty then Exit;

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

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName='''+Trim(DCode)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('SELECT * FROM TBSubID where 1=2 ');
    open;
  end;
  FMainID:='';
  CDS_Main.DisableControls;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
    First;
    while not Eof do
    begin
      If Fieldbyname('Ssel').AsBoolean then
      begin
        IF FMainID='' then
        begin
          FMainID:=Trim(CDS_Main.fieldbyname('mainID').AsString);
        end
        else
        begin
          IF Trim(CDS_Main.fieldbyname('mainID').AsString)<>FMainID then
          begin
            application.MessageBox('选择的不是同一个指示单，不能一起打印！','提示信息',0);
            ADOQueryCmd.Connection.RollbackTrans;
            EnableControls;
            exit;
          end;
        end;
          ADOQueryCmd.append;
          ADOQueryCmd.fieldbyname('SubId').Value:=Trim(CDS_Main.fieldbyname('BCID').AsString);
          ADOQueryCmd.fieldbyname('Dname').Value:=Trim(DCode);
          ADOQueryCmd.post;
      end;
      Next;
    end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
  CDS_Main.EnableControls;
  if (Trim(ComboBox1.Text)='外销普通码单') or (Trim(ComboBox1.Text)='内销普通码单') then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD ');
      sql.add('@mainID='+quotedstr(Trim(CDS_Main.FieldByName('RKOrdID').AsString)));
      sql.add(',@DName='+quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryCmd,CDS_WXPTPrint);
    SInitCDSData20(ADOQueryCmd,CDS_WXPTPrint);
  end
  else
  begin
    Print_WXPT();
  end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD1 ');
      sql.add('@mainID='+quotedstr(Trim(CDS_Main.FieldByName('RKOrdID').AsString)));
      sql.add(',@DName='+quotedstr(Trim(DCode)));
      sql.add(',@flag=''2'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryCmd,CDS_HZ);
    SInitCDSData20(ADOQueryCmd,CDS_HZ);
    fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\'+Trim(ComboBox1.Text)+'.rmf' ;
    if FileExists(fPrintFile) then
    begin
    //  RMVariables['OrderNo']:=Trim(CDS_Main.fieldbyname('OrderNo').AsString);
    //  RMVariables['MPRTECodeName']:=Trim(CDS_Main.fieldbyname('MPRTECodeName').AsString);
     // RMVariables['QtyUnit']:=Trim(CDS_Main.fieldbyname('QtyUnit').AsString);
      RM3.LoadFromFile(fPrintFile);
      RM3.ShowReport;
    end
    else
    begin
      Application.MessageBox(PChar('没有找'+fPrintFile),'提示',0);
    end;
end;

procedure TfrmCKProductBCPInList.GetLotData();
var
  i,j,z,h,q:Integer;
  FGangNo,FHZ:String;
begin
  if CDS_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select GangNo=AP.AOrdDefStr1,A.BaoNo,MJ.MJXH,MJ.MJMaoZ,MJ.MJLen,A.SubId,A.MainId,MJ.MJQty4,');
    sql.Add('DD.OrderNo,JS.PRTColor,PRTEColor=JS.SOrddefstr4,PRTColorNo=JS.SOrddefstr1,JS.PRTHX');
    sql.Add(' from CK_BanCP_CR A');
    sql.Add(' inner join WFB_MJJY MJ on A.MJID=MJ.MJID');
    sql.Add(' inner join JYOrder_Main DD on A.MainId=DD.MainId');
    sql.Add(' inner join JYOrder_Sub JS on A.SubId=JS.SubId');
    sql.Add(' inner join JYOrder_Sub_AnPai AP on A.APID=AP.APID');
    sql.Add(' where A.MainId='''+Trim(CDS_Main.fieldbyname('MainId').AsString)+'''');
    //sql.Add(' and A.SubId='''+Trim(CDS_Main.fieldbyname('SubId').AsString)+'''');
    {if Trim(CDS_Main.FieldByName('CKOrdNo').AsString)<>'' then
    begin
      sql.Add(' and A.CKOrdNo='''+Trim(CDS_Main.FieldByName('CKOrdNo').AsString)+'''');
    end; }
    if Trim(CPType.Text)<>'' then
    begin
      sql.Add(' and A.CPType='''+Trim(CPType.Text)+'''');
    end;
    sql.Add(' and A.CRType=''检验入库'' ');
    sql.Add(' order by  A.SubId,Cast(AP.AOrdDefStr1 as int),MJ.MJXH');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Juan);
  SInitCDSData20(ADOQueryTemp,CDS_Juan);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select MainId=Cast('''' as varchar(20)),SubId=Cast('''' as varchar(20)),');
    sql.Add(' PRTEColor=Cast('''' as varchar(20)),PRTHX=Cast('''' as varchar(20)),PRTColorNo=Cast('''' as varchar(20)),');
    sql.Add(' GangNo=Cast('''' as varchar(20)),BaoNo=Cast('''' as varchar(20)),XH=Cast(0 as int),');
    sql.Add(' LenQty0=Cast(1.00 as decimal(18,2)),LenQty1=Cast(1.00 as decimal(18,2)),');
    sql.Add(' LenQty2=Cast(1.00 as decimal(18,2)),LenQty3=Cast(1.00 as decimal(18,2)),');
    sql.Add(' LenQty4=Cast(1.00 as decimal(18,2)),LenQty5=Cast(1.00 as decimal(18,2)),');
    sql.Add(' LenQty6=Cast(1.00 as decimal(18,2)),LenQty7=Cast(1.00 as decimal(18,2)),');
    sql.Add(' LenQty8=Cast(1.00 as decimal(18,2)),LenQty9=Cast(1.00 as decimal(18,2)),');
    sql.Add(' MZ0=Cast(1.00 as decimal(18,2)),MZ1=Cast(1.00 as decimal(18,2)),');
    sql.Add(' MZ2=Cast(1.00 as decimal(18,2)),MZ3=Cast(1.00 as decimal(18,2)),');
    sql.Add(' MZ4=Cast(1.00 as decimal(18,2)),MZ5=Cast(1.00 as decimal(18,2)),');
    sql.Add(' MZ6=Cast(1.00 as decimal(18,2)),MZ7=Cast(1.00 as decimal(18,2)),');
    sql.Add(' MZ8=Cast(1.00 as decimal(18,2)),MZ9=Cast(1.00 as decimal(18,2)),');
    sql.Add(' JZ0=Cast(1.00 as decimal(18,2)),JZ1=Cast(1.00 as decimal(18,2)),');
    sql.Add(' JZ2=Cast(1.00 as decimal(18,2)),JZ3=Cast(1.00 as decimal(18,2)),');
    sql.Add(' JZ4=Cast(1.00 as decimal(18,2)),JZ5=Cast(1.00 as decimal(18,2)),');
    sql.Add(' JZ6=Cast(1.00 as decimal(18,2)),JZ7=Cast(1.00 as decimal(18,2)),');
    sql.Add(' JZ8=Cast(1.00 as decimal(18,2)),JZ9=Cast(1.00 as decimal(18,2)),');
    sql.Add(' MJPS0=Cast(1 as int),MJPS1=Cast(1 as int),');
    sql.Add(' MJPS2=Cast(1 as int),MJPS3=Cast(1 as int),');
    sql.Add(' MJPS4=Cast(1 as int),MJPS5=Cast(1 as int),');
    sql.Add(' MJPS6=Cast(1 as int),MJPS7=Cast(1 as int),');
    sql.Add(' MJPS8=Cast(1 as int),MJPS9=Cast(1 as int),');
    sql.Add(' XH=Cast(0 as int)');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_JuanPRT);
  SInitCDSData20(ADOQueryTemp,CDS_JuanPRT);
  if CDS_JuanPRT.IsEmpty=False then
  begin
    CDS_JuanPRT.Delete;
  end;
  i:=0;j:=0;z:=0;FGangNo:='';h:=0;
  CDS_Juan.DisableControls;
  with CDS_Juan do
  begin
    First;
    while not Eof do
    begin
      //FHZ:=Trim(CDS_Juan.fieldbyname('SubId').AsString)+Trim(CDS_Juan.fieldbyname('BaoNO').AsString)
           //+Trim(CDS_Juan.fieldbyname('GangNo').AsString);
      with CDS_JuanPRT do
      begin
        if CDS_JuanPRT.IsEmpty then
        begin
          CDS_JuanPRT.Append;
          CDS_JuanPRT.FieldByName('GangNo').Value:=CDS_Juan.fieldbyname('GangNo').Value;
         // CDS_JuanPRT.FieldByName('BaoNO').Value:=CDS_Juan.fieldbyname('BaoNO').Value;
          CDS_JuanPRT.FieldByName('PRTEColor').Value:=CDS_Juan.fieldbyname('PRTEColor').Value;
          CDS_JuanPRT.FieldByName('PRTColorNo').Value:=CDS_Juan.fieldbyname('PRTColorNo').Value;
          CDS_JuanPRT.FieldByName('PRTHX').Value:=CDS_Juan.fieldbyname('PRTHX').Value;
          CDS_JuanPRT.FieldByName('MJPS0').Value:=1;
          CDS_JuanPRT.FieldByName('XH').Value:=0;
          CDS_JuanPRT.FieldByName('LenQty0').Value:=CDS_Juan.fieldbyname('MJLen').Value;
          CDS_JuanPRT.FieldByName('MZ0').Value:=CDS_Juan.fieldbyname('MJMaoZ').Value;
          CDS_JuanPRT.FieldByName('JZ0').Value:=CDS_Juan.fieldbyname('MJQty4').Value;
          CDS_JuanPRT.FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
          CDS_JuanPRT.FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').Value;
          CDS_JuanPRT.Post;
          FHZ:=Trim(CDS_Juan.fieldbyname('SubId').AsString)
           +Trim(CDS_Juan.fieldbyname('GangNo').AsString);
          //FGangNo:=Trim(CDS_Juan.fieldbyname('GangNo').AsString);
          Z:=0;//gang 后缀
          j:=0;//行号
          for i:=1 to 9 do
          begin
            CDS_JuanPRT.Edit;
            CDS_JuanPRT.FieldByName('MJPS'+Trim(IntToStr(i))).Value:=0;
            CDS_JuanPRT.Post;
          end;
        end else
        begin
          if (Trim(CDS_Juan.fieldbyname('SubId').AsString)
             +Trim(CDS_Juan.fieldbyname('GangNo').AsString))<>FHZ then
          begin
            CDS_JuanPRT.Append;
            CDS_JuanPRT.FieldByName('GangNo').Value:=CDS_Juan.fieldbyname('GangNo').Value;
            //CDS_JuanPRT.FieldByName('BaoNO').Value:=CDS_Juan.fieldbyname('BaoNO').Value;
            CDS_JuanPRT.FieldByName('PRTEColor').Value:=CDS_Juan.fieldbyname('PRTEColor').Value;
            CDS_JuanPRT.FieldByName('PRTColorNo').Value:=CDS_Juan.fieldbyname('PRTColorNo').Value;
            CDS_JuanPRT.FieldByName('PRTHX').Value:=CDS_Juan.fieldbyname('PRTHX').Value;
            CDS_JuanPRT.FieldByName('MJPS0').Value:=1;
            CDS_JuanPRT.FieldByName('XH').Value:=J+1;
            CDS_JuanPRT.FieldByName('LenQty0').Value:=CDS_Juan.fieldbyname('MJLen').Value;
            CDS_JuanPRT.FieldByName('MZ0').Value:=CDS_Juan.fieldbyname('MJMaoZ').Value;
            CDS_JuanPRT.FieldByName('JZ0').Value:=CDS_Juan.fieldbyname('MJQty4').Value;
            CDS_JuanPRT.FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
            CDS_JuanPRT.FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').Value;
            CDS_JuanPRT.Post;
            FHZ:=Trim(CDS_Juan.fieldbyname('SubId').AsString)
               +Trim(CDS_Juan.fieldbyname('GangNo').AsString);
            Z:=0;//gang 后缀
            j:=j+1;//行号
            for i:=1 to 9 do
            begin
              CDS_JuanPRT.Edit;
              CDS_JuanPRT.FieldByName('MJPS'+Trim(IntToStr(i))).Value:=0;
              CDS_JuanPRT.Post;
            end;
          end else
          begin
            if Z<10 then
            begin
              CDS_JuanPRT.Locate('XH',j,[]);
              with CDS_JuanPRT do
              begin
                Edit;
                FieldByName('GangNo').Value:=CDS_Juan.fieldbyname('GangNo').Value;
                //FieldByName('BaoNO').Value:=CDS_Juan.fieldbyname('BaoNO').Value;
                CDS_JuanPRT.FieldByName('PRTEColor').Value:=CDS_Juan.fieldbyname('PRTEColor').Value;
                CDS_JuanPRT.FieldByName('PRTColorNo').Value:=CDS_Juan.fieldbyname('PRTColorNo').Value;
                CDS_JuanPRT.FieldByName('PRTHX').Value:=CDS_Juan.fieldbyname('PRTHX').Value;
                FieldByName('MJPS'+Trim(IntToStr(Z))).Value:=1;
                FieldByName('LenQty'+Trim(IntToStr(Z))).Value:=CDS_Juan.fieldbyname('MJLen').Value;
                CDS_JuanPRT.FieldByName('MZ'+Trim(IntToStr(Z))).Value:=CDS_Juan.fieldbyname('MJMaoZ').Value;
                CDS_JuanPRT.FieldByName('JZ'+Trim(IntToStr(Z))).Value:=CDS_Juan.fieldbyname('MJQty4').Value;
                FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
                FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').Value;
                Post;
              end;
            end else
            begin
              CDS_JuanPRT.Append;
              CDS_JuanPRT.FieldByName('GangNo').Value:=CDS_Juan.fieldbyname('GangNo').Value;
              CDS_JuanPRT.FieldByName('BaoNO').Value:=CDS_Juan.fieldbyname('BaoNO').Value;
              CDS_JuanPRT.FieldByName('PRTEColor').Value:=CDS_Juan.fieldbyname('PRTEColor').Value;
              CDS_JuanPRT.FieldByName('PRTColorNo').Value:=CDS_Juan.fieldbyname('PRTColorNo').Value;
              CDS_JuanPRT.FieldByName('PRTHX').Value:=CDS_Juan.fieldbyname('PRTHX').Value;
              CDS_JuanPRT.FieldByName('MJPS0').Value:=1;
              CDS_JuanPRT.FieldByName('XH').Value:=J+1;
              CDS_JuanPRT.FieldByName('LenQty0').Value:=CDS_Juan.fieldbyname('MJLen').Value;
              CDS_JuanPRT.FieldByName('MZ0').Value:=CDS_Juan.fieldbyname('MJMaoZ').Value;
              CDS_JuanPRT.FieldByName('JZ0').Value:=CDS_Juan.fieldbyname('MJQty4').Value;
              CDS_JuanPRT.FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
              CDS_JuanPRT.FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').Value;
              CDS_JuanPRT.Post;
              Z:=0;//gang 后缀
              j:=j+1;//行号
              for i:=1 to 9 do
              begin
                CDS_JuanPRT.Edit;
                CDS_JuanPRT.FieldByName('MJPS'+Trim(IntToStr(i))).Value:=0;
                CDS_JuanPRT.Post;
              end;
            end;
          end;
        end
      end;
      Z:=Z+1;
      Next;
    end;
  end;
  CDS_Juan.EnableControls;
end;
procedure TfrmCKProductBCPInList.GetJuanData();
var
  i,j,z,h,q:Integer;
  FGangNo:String;
begin
  if CDS_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select GangNo=AP.AOrdDefStr1,A.BaoNo,MJ.MJXH,MJ.MJMaoZ,MJ.MJLen,A.SubId,A.MainId');
    sql.Add(' from CK_BanCP_CR A');
    sql.Add(' inner join WFB_MJJY MJ on A.MJID=MJ.MJID');
    sql.Add(' inner join JYOrder_Main DD on A.MainId=DD.MainId');
    sql.Add(' inner join JYOrder_Sub JS on A.SubId=JS.SubId');
    sql.Add(' inner join JYOrder_Sub_AnPai AP on A.APID=AP.APID');
    sql.Add(' where A.MainId='''+Trim(CDS_Main.fieldbyname('MainId').AsString)+'''');
    sql.Add(' and A.SubId='''+Trim(CDS_Main.fieldbyname('SubId').AsString)+'''');
    {if Trim(CDS_Main.FieldByName('CKOrdNo').AsString)<>'' then
    begin
      sql.Add(' and A.CKOrdNo='''+Trim(CDS_Main.FieldByName('CKOrdNo').AsString)+'''');
    end; }
    if Trim(CPType.Text)<>'' then
    begin
      sql.Add(' and A.CPType='''+Trim(CPType.Text)+'''');
    end;
    sql.Add(' and A.CRType=''检验入库'' ');
    sql.Add(' order by  Cast(AP.AOrdDefStr1 as int),Cast(A.BaoNo as int),MJ.MJXH');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Juan);
  SInitCDSData20(ADOQueryTemp,CDS_Juan);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select BaoNo0=Cast('''' as varchar(20)),MJXH0=Cast(0 as int),MJPS0=Cast(1 as int),');
    sql.Add('MJPS1=Cast(1 as int),MJPS2=Cast(1 as int),MJPS3=Cast(1 as int),MJPS4=Cast(1 as int),');
    sql.Add(' KGQty0=Cast(1.00 as decimal(18,2)),LenQty0=Cast(1.00 as decimal(18,2)),');
    sql.Add(' BaoNo1=Cast('''' as varchar(20)),MJXH1=Cast(0 as int),');
    sql.Add(' KGQty1=Cast(1.00 as decimal(18,2)),LenQty1=Cast(1.00 as decimal(18,2)),');
    sql.Add(' BaoNo2=Cast('''' as varchar(20)),MJXH2=Cast(0 as int),');
    sql.Add(' KGQty2=Cast(1.00 as decimal(18,2)),LenQty2=Cast(1.00 as decimal(18,2)),');
    sql.Add(' BaoNo3=Cast('''' as varchar(20)),MJXH3=Cast(0 as int),');
    sql.Add(' KGQty3=Cast(1.00 as decimal(18,2)),LenQty3=Cast(1.00 as decimal(18,2)),');
    sql.Add(' BaoNo4=Cast('''' as varchar(20)),MJXH4=Cast(0 as int),');
    sql.Add(' KGQty4=Cast(1.00 as decimal(18,2)),LenQty4=Cast(1.00 as decimal(18,2)),');
    sql.Add(' BaoNo5=Cast('''' as varchar(20)),MJXH5=Cast(0 as int),');
    sql.Add(' KGQty5=Cast(1.00 as decimal(18,2)),LenQty5=Cast(1.00 as decimal(18,2)),');
    sql.Add(' GangNo0=Cast('''' as varchar(20)),XH=Cast('''' as varchar(20)),');
    sql.Add(' GangNo1=Cast('''' as varchar(20)),GangNo2=Cast('''' as varchar(20)),');
    sql.Add(' GangNo3=Cast('''' as varchar(20)),GangNo4=Cast('''' as varchar(20))');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_JuanPRT);
  SInitCDSData20(ADOQueryTemp,CDS_JuanPRT);
  if CDS_JuanPRT.IsEmpty=False then
  begin
    CDS_JuanPRT.Delete;
  end;
  i:=0;j:=0;z:=0;FGangNo:='';h:=0;
  CDS_Juan.DisableControls;
  with CDS_Juan do
  begin
    First;
    while not Eof do
    begin
      with CDS_JuanPRT do
      begin
        if CDS_JuanPRT.IsEmpty then
        begin
          begin
            CDS_JuanPRT.Append;
            CDS_JuanPRT.FieldByName('GangNo0').Value:=CDS_Juan.fieldbyname('GangNo').Value;
            CDS_JuanPRT.FieldByName('BaoNO0').Value:=CDS_Juan.fieldbyname('BaoNO').Value;
            CDS_JuanPRT.FieldByName('MJXH0').Value:=CDS_Juan.fieldbyname('MJXH').Value;
            CDS_JuanPRT.FieldByName('MJPS0').Value:=1;
            CDS_JuanPRT.FieldByName('KGQty0').Value:=CDS_Juan.fieldbyname('MJMaoZ').Value;
            CDS_JuanPRT.FieldByName('LenQty0').Value:=CDS_Juan.fieldbyname('MJLen').Value;
            CDS_JuanPRT.FieldByName('XH').Value:='0';
            CDS_JuanPRT.Post;
            FGangNo:=Trim(CDS_Juan.fieldbyname('GangNo').AsString);
            for h:=1 to 9 do
            begin
              CDS_JuanPRT.Append;
              CDS_JuanPRT.FieldByName('XH').Value:=Trim(IntToStr(h));
              CDS_JuanPRT.Post;
            end;
            J:=0;//XH   行号
            Z:=0;//gang 后缀
            //h:=9;//
            q:=0; //页数
          end;
        end else
        begin
          if i>9 then
          begin
            Z:=Z+1;

            if Z>4 then
            begin
              Z:=0;
              q:=q+1;
              J:=q*10;
              CDS_JuanPRT.Append;
              CDS_JuanPRT.FieldByName('GangNo0').Value:=CDS_Juan.fieldbyname('GangNo').Value;
              CDS_JuanPRT.FieldByName('BaoNO0').Value:=CDS_Juan.fieldbyname('BaoNO').Value;
              CDS_JuanPRT.FieldByName('MJXH0').Value:=CDS_Juan.fieldbyname('MJXH').Value;
              CDS_JuanPRT.FieldByName('KGQty0').Value:=CDS_Juan.fieldbyname('MJMaoZ').Value;
              CDS_JuanPRT.FieldByName('LenQty0').Value:=CDS_Juan.fieldbyname('MJLen').Value;
              CDS_JuanPRT.FieldByName('MJPS0').Value:=1;
              CDS_JuanPRT.FieldByName('XH').Value:=Trim(IntToStr(J));
              CDS_JuanPRT.Post;
              for h:=J+1 to J+9 do
              begin
                CDS_JuanPRT.Append;
                CDS_JuanPRT.FieldByName('XH').Value:=Trim(IntToStr(h));
                CDS_JuanPRT.Post;
              end;
              FGangNo:=Trim(CDS_Juan.fieldbyname('GangNo').AsString);
            end else
            begin
              J:=q*10;
              CDS_JuanPRT.Locate('XH',Trim(IntToStr(10*q)),[]);
              CDS_JuanPRT.Edit;
              CDS_JuanPRT.FieldByName('GangNo'+Trim(IntToStr(Z))).Value:=CDS_Juan.fieldbyname('GangNo').Value;
              CDS_JuanPRT.FieldByName('BaoNO'+Trim(IntToStr(Z))).Value:=CDS_Juan.fieldbyname('BaoNO').Value;
              CDS_JuanPRT.FieldByName('MJXH'+Trim(IntToStr(Z))).Value:=CDS_Juan.fieldbyname('MJXH').Value;
              CDS_JuanPRT.FieldByName('MJPS'+Trim(IntToStr(Z))).Value:=1;
              CDS_JuanPRT.FieldByName('KGQty'+Trim(IntToStr(Z))).Value:=CDS_Juan.fieldbyname('MJMaoZ').Value;
              CDS_JuanPRT.FieldByName('LenQty'+Trim(IntToStr(Z))).Value:=CDS_Juan.fieldbyname('MJLen').Value;
              CDS_JuanPRT.Post;
              FGangNo:=Trim(CDS_Juan.fieldbyname('GangNo').AsString);
            end;
            i:=0;
          end else
          begin
            if FGangNo<>Trim(CDS_Juan.fieldbyname('GangNo').AsString) then
            begin
              Z:=Z+1;

              if Z>4 then
              begin
                Z:=0;
                q:=q+1;
                J:=q*10;
                CDS_JuanPRT.Append;
                CDS_JuanPRT.FieldByName('GangNo0').Value:=CDS_Juan.fieldbyname('GangNo').Value;
                CDS_JuanPRT.FieldByName('BaoNO0').Value:=CDS_Juan.fieldbyname('BaoNO').Value;
                CDS_JuanPRT.FieldByName('MJXH0').Value:=CDS_Juan.fieldbyname('MJXH').Value;
                CDS_JuanPRT.FieldByName('KGQty0').Value:=CDS_Juan.fieldbyname('MJMaoZ').Value;
                CDS_JuanPRT.FieldByName('LenQty0').Value:=CDS_Juan.fieldbyname('MJLen').Value;
                CDS_JuanPRT.FieldByName('MJPS0').Value:=1;
                CDS_JuanPRT.FieldByName('XH').Value:=Trim(IntToStr(J));
                CDS_JuanPRT.Post;
                FGangNo:=Trim(CDS_Juan.fieldbyname('GangNo').AsString);
                for h:=J+1 to J+9 do
                begin
                  CDS_JuanPRT.Append;
                  CDS_JuanPRT.FieldByName('XH').Value:=Trim(IntToStr(h));
                  CDS_JuanPRT.Post;
                end;
              end else
              begin
                J:=q*10;
                CDS_JuanPRT.Locate('XH',Trim(IntToStr(10*q)),[]);
                CDS_JuanPRT.Edit;
                CDS_JuanPRT.FieldByName('GangNo'+Trim(IntToStr(Z))).Value:=CDS_Juan.fieldbyname('GangNo').Value;
                CDS_JuanPRT.FieldByName('BaoNO'+Trim(IntToStr(Z))).Value:=CDS_Juan.fieldbyname('BaoNO').Value;
                CDS_JuanPRT.FieldByName('MJXH'+Trim(IntToStr(Z))).Value:=CDS_Juan.fieldbyname('MJXH').Value;
                CDS_JuanPRT.FieldByName('KGQty'+Trim(IntToStr(Z))).Value:=CDS_Juan.fieldbyname('MJMaoZ').Value;
                CDS_JuanPRT.FieldByName('LenQty'+Trim(IntToStr(Z))).Value:=CDS_Juan.fieldbyname('MJLen').Value;
                CDS_JuanPRT.FieldByName('MJPS'+Trim(IntToStr(Z))).Value:=1;
                CDS_JuanPRT.Post;
                FGangNo:=Trim(CDS_Juan.fieldbyname('GangNo').AsString);
              end;
              i:=0;
            end else
            begin
              j:=j+1;
              CDS_JuanPRT.Locate('XH',Trim(IntToStr(J)),[]);
              CDS_JuanPRT.Edit;
              CDS_JuanPRT.FieldByName('GangNo'+Trim(IntToStr(Z))).Value:=CDS_Juan.fieldbyname('GangNo').Value;
              CDS_JuanPRT.FieldByName('BaoNO'+Trim(IntToStr(Z))).Value:=CDS_Juan.fieldbyname('BaoNO').Value;
              CDS_JuanPRT.FieldByName('MJXH'+Trim(IntToStr(Z))).Value:=CDS_Juan.fieldbyname('MJXH').Value;
              CDS_JuanPRT.FieldByName('KGQty'+Trim(IntToStr(Z))).Value:=CDS_Juan.fieldbyname('MJMaoZ').Value;
              CDS_JuanPRT.FieldByName('LenQty'+Trim(IntToStr(Z))).Value:=CDS_Juan.fieldbyname('MJLen').Value;
              CDS_JuanPRT.FieldByName('MJPS'+Trim(IntToStr(Z))).Value:=1;
              CDS_JuanPRT.Post;
              FGangNo:=Trim(CDS_Juan.fieldbyname('GangNo').AsString);
            end;
          end;
        end;
      end;
      i:=i+1;
      Next;
    end;
  end;
  CDS_Juan.EnableControls;
end;
procedure TfrmCKProductBCPInList.GetBaoData();
var
  i,j,z,h:Integer;
  FGangNo:String;
begin
  if CDS_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select GangNo=AP.AOrdDefStr1,A.BaoNo,MJ.MJXH,MJ.MJMaoZ,MJ.MJLen');
    sql.Add(' from CK_BanCP_CR A');
    sql.Add(' inner join WFB_MJJY MJ on A.MJID=MJ.MJID');
    sql.Add(' inner join JYOrder_Main DD on A.MainId=DD.MainId');
    sql.Add(' inner join JYOrder_Sub_AnPai AP on A.APID=AP.APID');
    sql.Add(' where A.MainId='''+Trim(CDS_Main.fieldbyname('MainId').AsString)+'''');
    sql.Add(' and A.SubId='''+Trim(CDS_Main.fieldbyname('SubId').AsString)+'''');
    {if Trim(CDS_Main.FieldByName('CKOrdNo').AsString)<>'' then
    begin
      sql.Add(' and A.CKOrdNo='''+Trim(CDS_Main.FieldByName('CKOrdNo').AsString)+'''');
    end;  }
    if Trim(CPType.Text)<>'' then
    begin
      sql.Add(' and A.CPType='''+Trim(CPType.Text)+'''');
    end;
    sql.Add(' and A.CRType=''检验入库'' ');
    sql.Add(' order by  Cast(AP.AOrdDefStr1 as int),Cast(A.BaoNo as int),MJ.MJXH');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_BaoNo);
  SInitCDSData20(ADOQueryTemp,CDS_BaoNo);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select GangNo0=Cast('''' as varchar(20)),GangNo1=Cast('''' as varchar(20)),GangNo2=Cast('''' as varchar(20)),');
    sql.Add(' GangNo3=Cast('''' as varchar(20)),GangNo4=Cast('''' as varchar(20)),XH=Cast('''' as varchar(20))');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_BaoPRT);
  SInitCDSData20(ADOQueryTemp,CDS_BaoPRT);
  if CDS_BaoPRT.IsEmpty=False then
  begin
    CDS_BaoPRT.Delete;
  end;
  i:=0;j:=0;z:=0;FGangNo:='';h:=0;
  CDS_BaoNo.DisableControls;
  with CDS_BaoNo do
  begin
    First;
    while not Eof do
    begin
      with CDS_BaoPRT do
      begin
        if CDS_BaoPRT.IsEmpty then
        begin
          if J=0 then
          begin
            CDS_BaoPRT.Append;
            CDS_BaoPRT.FieldByName('GangNo0').Value:=CDS_BaoNo.fieldbyname('GangNo').Value;
            CDS_BaoPRT.FieldByName('XH').Value:='0';
            CDS_BaoPRT.Post;
            FGangNo:=Trim(CDS_BaoNo.fieldbyname('GangNo').AsString);
            J:=0;//XH   行号
            Z:=0;//gang 后缀
            h:=0;//每行的记录数。
          end;
        end else
        begin
          if i>9 then
          begin
            Z:=Z+1;
            if Z>4 then
            begin
              Z:=0;
              J:=J+1;
              CDS_BaoPRT.Append;
              CDS_BaoPRT.FieldByName('GangNo0').Value:=CDS_BaoNo.fieldbyname('GangNo').Value;
              CDS_BaoPRT.FieldByName('XH').Value:=Trim(IntToStr(J));
              CDS_BaoPRT.Post;
              FGangNo:=Trim(CDS_BaoNo.fieldbyname('GangNo').AsString);
            end else
            begin
              CDS_BaoPRT.Locate('XH',Trim(IntToStr(J)),[]);
              CDS_BaoPRT.Edit;
              CDS_BaoPRT.FieldByName('GangNo'+Trim(IntToStr(Z))).Value:=CDS_BaoNo.fieldbyname('GangNo').Value;
              CDS_BaoPRT.Post;
              FGangNo:=Trim(CDS_BaoNo.fieldbyname('GangNo').AsString);
            end;
            i:=0;
          end else
          begin
            if FGangNo<>Trim(CDS_BaoNo.fieldbyname('GangNo').AsString) then
            begin
              Z:=Z+1;
              if Z>4 then
              begin
                Z:=0;
                J:=J+1;
                CDS_BaoPRT.Append;
                CDS_BaoPRT.FieldByName('GangNo0').Value:=CDS_BaoNo.fieldbyname('GangNo').Value;
                CDS_BaoPRT.FieldByName('XH').Value:=Trim(IntToStr(J));
                CDS_BaoPRT.Post;
                FGangNo:=Trim(CDS_BaoNo.fieldbyname('GangNo').AsString);
              end else
              begin
                CDS_BaoPRT.Locate('XH',Trim(IntToStr(J)),[]);
                CDS_BaoPRT.Edit;
                CDS_BaoPRT.FieldByName('GangNo'+Trim(IntToStr(Z))).Value:=CDS_BaoNo.fieldbyname('GangNo').Value;
                CDS_BaoPRT.Post;
                FGangNo:=Trim(CDS_BaoNo.fieldbyname('GangNo').AsString);
              end;
              i:=0;
            end;
          end;
        end;
      end;
      i:=i+1;
      Next;
    end;
  end;
  CDS_BaoNo.EnableControls;

end;
procedure TfrmCKProductBCPInList.GetBaleData();
var
  i,j,z,h,q:Integer;
  FGangNo,FHZ:String;
begin
  if CDS_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select GangNo=AP.AOrdDefStr1,A.BaoNo,MJ.MJXH,MJ.MJMaoZ,MJ.MJLen,A.SubId,A.MainId,');
    sql.Add('DD.OrderNo,JS.PRTColor,PRTEColor=JS.SOrddefstr4,JS.PRTHX');
    sql.Add(' from CK_BanCP_CR A');
    sql.Add(' inner join WFB_MJJY MJ on A.MJID=MJ.MJID');
    sql.Add(' inner join JYOrder_Main DD on A.MainId=DD.MainId');
    sql.Add(' inner join JYOrder_Sub JS on A.SubId=JS.SubId');
    sql.Add(' inner join JYOrder_Sub_AnPai AP on A.APID=AP.APID');
    sql.Add(' where A.MainId='''+Trim(CDS_Main.fieldbyname('MainId').AsString)+'''');
    //sql.Add(' and A.SubId='''+Trim(CDS_Main.fieldbyname('SubId').AsString)+'''');
    if Trim(CDS_Main.FieldByName('CKOrdNo').AsString)<>'' then
    begin
      sql.Add(' and A.CKOrdNo='''+Trim(CDS_Main.FieldByName('CKOrdNo').AsString)+'''');
    end;
    if Trim(CPType.Text)<>'' then
    begin
      sql.Add(' and A.CPType='''+Trim(CPType.Text)+'''');
    end;
    sql.Add(' and A.CRType=''检验入库'' ');
    sql.Add(' order by  Cast(AP.AOrdDefStr1 as int),Cast(A.BaoNo as int),MJ.MJXH');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Juan);
  SInitCDSData20(ADOQueryTemp,CDS_Juan);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select MainId=Cast('''' as varchar(20)),SubId=Cast('''' as varchar(20)),');
    sql.Add(' PRTEColor=Cast('''' as varchar(20)),PRTHX=Cast('''' as varchar(20)),');
    sql.Add(' GangNo=Cast('''' as varchar(20)),BaoNo=Cast('''' as varchar(20)),MJPS=Cast(0 as int),XH=Cast(0 as int),');
    sql.Add(' LenQty0=Cast(1.00 as decimal(18,2)),LenQty1=Cast(1.00 as decimal(18,2)),');
    sql.Add(' LenQty2=Cast(1.00 as decimal(18,2)),LenQty3=Cast(1.00 as decimal(18,2)),');
    sql.Add(' LenQty4=Cast(1.00 as decimal(18,2)),LenQty5=Cast(1.00 as decimal(18,2)),');
    sql.Add(' LenQty6=Cast(1.00 as decimal(18,2)),LenQty7=Cast(1.00 as decimal(18,2)),');
    sql.Add(' LenQty8=Cast(1.00 as decimal(18,2)),LenQty9=Cast(1.00 as decimal(18,2)),');
    sql.Add(' LenQty10=Cast(1.00 as decimal(18,2)),LenQty11=Cast(1.00 as decimal(18,2)),');
    sql.Add(' MJPS0=Cast(0 as int),MJPS1=Cast(0 as int),');
    sql.Add(' MJPS2=Cast(0 as int),MJPS3=Cast(0 as int),');
    sql.Add(' MJPS4=Cast(0 as int),MJPS5=Cast(0 as int),');
    sql.Add(' MJPS6=Cast(0 as int),MJPS7=Cast(0 as int),');
    sql.Add(' MJPS8=Cast(0 as int),MJPS9=Cast(0 as int),');
    sql.Add(' MJPS10=Cast(0 as int),MJPS11=Cast(1 as int),XH=Cast(0 as int)');
    {sql.Add(' KgQty0=Cast(1.00 as decimal(18,2)),KgQty1=Cast(1.00 as decimal(18,2)),');
    sql.Add(' LenQty2=Cast(1.00 as decimal(18,2)),LenQty3=Cast(1.00 as decimal(18,2)),');
    sql.Add(' LenQty4=Cast(1.00 as decimal(18,2)),LenQty5=Cast(1.00 as decimal(18,2)),');
    sql.Add(' LenQty5=Cast(1.00 as decimal(18,2)),LenQty7=Cast(1.00 as decimal(18,2)),');
    sql.Add(' LenQty6=Cast(1.00 as decimal(18,2)),LenQty9=Cast(1.00 as decimal(18,2)),');
    sql.Add(' LenQty10=Cast(1.00 as decimal(18,2)),LenQty11=Cast(1.00 as decimal(18,2)),');}
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_JuanPRT);
  SInitCDSData20(ADOQueryTemp,CDS_JuanPRT);
  if CDS_JuanPRT.IsEmpty=False then
  begin
    CDS_JuanPRT.Delete;
  end;
  i:=0;j:=0;z:=0;FGangNo:='';h:=0;
  CDS_Juan.DisableControls;
  with CDS_Juan do
  begin
    First;
    while not Eof do
    begin
      //FHZ:=Trim(CDS_Juan.fieldbyname('SubId').AsString)+Trim(CDS_Juan.fieldbyname('BaoNO').AsString)
           //+Trim(CDS_Juan.fieldbyname('GangNo').AsString);
      with CDS_JuanPRT do
      begin
        if CDS_JuanPRT.IsEmpty then
        begin
          CDS_JuanPRT.Append;
          CDS_JuanPRT.FieldByName('GangNo').Value:=CDS_Juan.fieldbyname('GangNo').Value;
          CDS_JuanPRT.FieldByName('BaoNO').Value:=CDS_Juan.fieldbyname('BaoNO').Value;
          CDS_JuanPRT.FieldByName('PRTEColor').Value:=CDS_Juan.fieldbyname('PRTEColor').Value;
          CDS_JuanPRT.FieldByName('PRTHX').Value:=CDS_Juan.fieldbyname('PRTHX').Value;
          CDS_JuanPRT.FieldByName('MJPS0').Value:=1;
          CDS_JuanPRT.FieldByName('XH').Value:=0;
          CDS_JuanPRT.FieldByName('LenQty0').Value:=CDS_Juan.fieldbyname('MJLen').Value;
          CDS_JuanPRT.FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
          CDS_JuanPRT.FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').Value;
          CDS_JuanPRT.Post;
          FHZ:=Trim(CDS_Juan.fieldbyname('SubId').AsString)+Trim(CDS_Juan.fieldbyname('BaoNO').AsString)
           +Trim(CDS_Juan.fieldbyname('GangNo').AsString);
          //FGangNo:=Trim(CDS_Juan.fieldbyname('GangNo').AsString);
          Z:=0;//gang 后缀
          j:=0;//行号
        end else
        begin
          if (Trim(CDS_Juan.fieldbyname('SubId').AsString)+Trim(CDS_Juan.fieldbyname('BaoNO').AsString)
             +Trim(CDS_Juan.fieldbyname('GangNo').AsString))<>FHZ then
          begin
            CDS_JuanPRT.Append;
            CDS_JuanPRT.FieldByName('GangNo').Value:=CDS_Juan.fieldbyname('GangNo').Value;
            CDS_JuanPRT.FieldByName('BaoNO').Value:=CDS_Juan.fieldbyname('BaoNO').Value;
            CDS_JuanPRT.FieldByName('PRTEColor').Value:=CDS_Juan.fieldbyname('PRTEColor').Value;
            CDS_JuanPRT.FieldByName('PRTHX').Value:=CDS_Juan.fieldbyname('PRTHX').Value;
            CDS_JuanPRT.FieldByName('MJPS0').Value:=1;
            CDS_JuanPRT.FieldByName('XH').Value:=J+1;
            CDS_JuanPRT.FieldByName('LenQty0').Value:=CDS_Juan.fieldbyname('MJLen').Value;
            CDS_JuanPRT.FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
            CDS_JuanPRT.FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').Value;
            CDS_JuanPRT.Post;
            FHZ:=Trim(CDS_Juan.fieldbyname('SubId').AsString)+Trim(CDS_Juan.fieldbyname('BaoNO').AsString)
               +Trim(CDS_Juan.fieldbyname('GangNo').AsString);
            Z:=0;//gang 后缀
            j:=j+1;//行号
          end else
          begin
            if Z<12 then
            begin
              CDS_JuanPRT.Locate('XH',j,[]);
              with CDS_JuanPRT do
              begin
                Edit;
                FieldByName('GangNo').Value:=CDS_Juan.fieldbyname('GangNo').Value;
                FieldByName('BaoNO').Value:=CDS_Juan.fieldbyname('BaoNO').Value;
                CDS_JuanPRT.FieldByName('PRTEColor').Value:=CDS_Juan.fieldbyname('PRTEColor').Value;
                CDS_JuanPRT.FieldByName('PRTHX').Value:=CDS_Juan.fieldbyname('PRTHX').Value;
                FieldByName('MJPS'+Trim(IntToStr(Z))).Value:=1;
                FieldByName('LenQty'+Trim(IntToStr(Z))).Value:=CDS_Juan.fieldbyname('MJLen').Value;
                FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
                FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').Value;
                Post;
              end;
            end else
            begin
              CDS_JuanPRT.Append;
              CDS_JuanPRT.FieldByName('GangNo').Value:=CDS_Juan.fieldbyname('GangNo').Value;
              CDS_JuanPRT.FieldByName('BaoNO').Value:=CDS_Juan.fieldbyname('BaoNO').Value;
              CDS_JuanPRT.FieldByName('PRTEColor').Value:=CDS_Juan.fieldbyname('PRTEColor').Value;
              CDS_JuanPRT.FieldByName('PRTHX').Value:=CDS_Juan.fieldbyname('PRTHX').Value;
              CDS_JuanPRT.FieldByName('MJPS0').Value:=1;
              CDS_JuanPRT.FieldByName('XH').Value:=J+1;
              CDS_JuanPRT.FieldByName('LenQty0').Value:=CDS_Juan.fieldbyname('MJLen').Value;
              CDS_JuanPRT.FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
              CDS_JuanPRT.FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').Value;
              CDS_JuanPRT.Post;
              Z:=0;//gang 后缀
              j:=j+1;//行号
            end;
          end;
        end
      end;
      Z:=Z+1;
      Next;
    end;
  end;
  CDS_Juan.EnableControls;
end;

procedure TfrmCKProductBCPInList.orderNoChange(Sender: TObject);
begin
  if Length(Trim(orderNo.Text))<4 then Exit;
  TBFind.Click;
end;

procedure TfrmCKProductBCPInList.orderNoKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(Trim(orderNo.Text))<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        SQL.Add('select A.*,B.OrderNo,B.conNo,B.MPRTCodeName,B.MPRTSpec,B.MprtkuanNo,C.prtkuanNo,C.PRTColor,C.SOrddefstr4,B.MPRTMF,B.MPRTKZ,D.MJXH,C.PRTHX,D.MJQty3,D.MJQty4');
        sql.Add(',isnull(customerNoName,B.OrderNo) KHName');
        sql.Add(',KHConNo=(select Top 1 KHConNo from JYOrderCon_Main JCM where JCM.ConNo=B.OrderNo)');
        sql.Add(',MPRTECodeName=(select Top 1 MPRTCodeName from JYOrderCon_Main JCM where JCM.ConNo=B.OrderNo)');
        sql.Add(',D.mjstr4 as AOrdDefStr1,cast(D.MJXH as varchar) as MJXHM');
        sql.add('from CK_BanCP_CR 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(' inner join WFB_MJJY D on A.MJId=D.MJId');
        sql.Add(' inner join JYOrder_Sub_AnPai E on A.APID=E.APID');
        sql.add(' where B.OrderNo like '+quotedstr('%'+trim(orderNo.Text)+'%'));
        SQL.Add(' and CRType=''检验入库'' ');
        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmCKProductBCPInList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,True);
end;

procedure TfrmCKProductBCPInList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,False);
end;

procedure TfrmCKProductBCPInList.TBRKCXClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行操作吗？','提示',32+4)<>IDYES then Exit;
  BegDate.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      while Locate('SSel',True,[]) do
      begin
       { IF trim(Fieldbyname('Filler').AsString)<>trim(DName) then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('不能撤销其他人的数据！','提示',0);
          Exit;
        end;  }
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_BanCP_CR where CRID='+Trim(CDS_Main.fieldbyname('CRID').AsString));
          Open;
        end;
        if ADOQueryTemp.RecordCount>1 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('已出库不能撤销入库!','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete CK_BanCP_CR where BCID='''+Trim(CDS_Main.fieldbyname('BCID').AsString)+'''');
          sql.Add('delete CK_BanCP_KC where CRID='+Trim(CDS_Main.fieldbyname('CRID').AsString));
          sql.Add('Update WFB_MJJY Set MJStr2=''未入库'' where MJID='''+Trim(CDS_Main.fieldbyname('MJID').AsString)+'''');
          ExecSQL;
        end;
        CDS_Main.Delete;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;

  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销失败!','提示',0);
  end;
end;

procedure TfrmCKProductBCPInList.Tv1CellClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_Main.IsEmpty=False then
  begin
    InitOrderColor(Trim(CDS_Main.fieldbyname('MainId').AsString),PRTColor,ADOQueryTemp);
    InitBCGangNo(Trim(CDS_Main.fieldbyname('SubId').AsString),AOrdDefStr1,ADOQueryTemp);
  end;
end;

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

procedure TfrmCKProductBCPInList.AOrdDefStr1Change(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductBCPInList.TBBaoClick(Sender: TObject);
var
  fPrintFile:String;
   Txt,fImagePath:string;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin
  if CDS_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\包标签.rmf';
  with ADOQueryTemp 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.OrdPerson1,D.MPRTKuanNO,');
    sql.Add('PRTColor=DBO.F_Get_Order_SubStr(BaoID,''BNColor''),SOrddefstr1=DBO.F_Get_Order_SubStr(BaoID,''BNSOrddefstr1''),');
    sql.Add('SOrddefstr4=DBO.F_Get_Order_SubStr(BaoID,''BNSOrddefstr4''),PRtHX=DBO.F_Get_Order_SubStr(BaoID,''BNPRtHX''), ');
    sql.Add('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('count(A.MJID) as JSl,sum(A.MJMaoZ) MJMAOZ,sum(MJQty3) as MJQty3,sum(MJQty4) as MJQty4,SUM(A.MJLen)as MJLen');
    sql.Add('from WFB_MJJY A');
    sql.Add(' inner join JYOrder_Sub C on C.SubID=A.SubID');
    sql.Add(' inner join JYOrder_Main D on D.MainID=A.MainID');
    sql.Add(' where BaoId='''+Trim(CDS_Main.fieldbyname('BaoId').AsString)+'''  ');
    SQL.ADD('group by A.Baoid,A.BaoNo,A.MJTypeOther,D.OrderNo,D.conNo,D.customerNoName,D.MprtCodeName,D.OrdPerson1,D.MPRTKuanNO');
    Open;
  end;
  if ADOQueryTemp.RecordCount>1 then
  begin
    Application.MessageBox('打包错误，请撤销入库!','提示',0);
    Exit;
  end;
  try
      Moudle:=LoadLibrary('MakeQRBarcode.dll');
      @Makebar:=GetProcAddress(Moudle,'Make');
      @Mixtext:=GetProcAddress(Moudle,'MixText');
      Txt:=Trim(CDS_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);
      exit;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  SInitCDSData20(ADOQueryTemp,CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['RKOrdID']:=Trim(CDS_MainSel.fieldbyname('RKOrdID').AsString);
    //RMVariables['OrderNo']:=Trim(CDS_MainSel.fieldbyname('OrderNo').AsString);
    //RMVariables['PS']:=Trim(RKOrdPS.Text);
    //RMVariables['QtyUnit']:=Trim(CDS_MainSel.fieldbyname('QtyUnit').AsString);
    RMVariables['QRBARCODE']:=fImagePath;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\包标签.rmf'),'提示',0);
  end;
end;

procedure TfrmCKProductBCPInList.TBRKDClick(Sender: TObject);
var
  fPrintFile:String;
   Txt,fImagePath:string;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin
  if CDS_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\入库单标签.rmf' ;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A1.CRTime,A1.RKOrdID,A.MJTypeOther as QtyUnit,D.OrderNo,D.conNo,D.customerNoName,D.MprtCodeName,D.OrdPerson1,D.MPRTKuanNO,');
    sql.Add('PRTColor=DBO.F_Get_Order_SubStr(RKOrdID,''RKColor''),SOrddefstr1=DBO.F_Get_Order_SubStr(RKOrdID,''RKSOrddefstr1''),');
    sql.Add('SOrddefstr4=DBO.F_Get_Order_SubStr(RKOrdID,''RkSOrddefstr4''),PRtHX=DBO.F_Get_Order_SubStr(RKOrdID,''RKPRtHX''), ');
    sql.Add('PRTkuanNo=DBO.F_Get_Order_SubStr(RKOrdID,''RkPrtkuanno''),');
    sql.Add('MJStr4=DBO.F_Get_Order_SubStr(RKOrdID,''RKGangNO''), ');
    sql.Add('khConNo=(select top 1 khConNo from JYOrderCon_Main X where X.ConNO=D.conNO), ');
    sql.Add('Bsl=(select count(distinct baoNo) from CK_BanCP_CR X where X.RKOrdID=A1.RKOrdID), ');
    sql.Add('kuwei=(select Top 1 RKPlace from CK_BanCP_CR X where X.RKOrdID=A1.RKOrdID), ');
    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 CK_BanCP_CR A1  inner join WFB_MJJY A on A.MJID=A1.MJID ');
    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 A1.RKOrdID='''+Trim(CDS_Main.fieldbyname('RKOrdID').AsString)+'''  ');
    SQL.ADD('group by A1.CRTime,A1.RKOrdID,A.MJTypeOther,D.OrderNo,D.conNo,D.customerNoName,D.MprtCodeName,D.OrdPerson1,D.MPRTKuanNO');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  SInitCDSData20(ADOQueryTemp,CDS_PRT);

  try
      Moudle:=LoadLibrary('MakeQRBarcode.dll');
      @Makebar:=GetProcAddress(Moudle,'Make');
      @Mixtext:=GetProcAddress(Moudle,'MixText');
      Txt:=Trim(CDS_Main.fieldbyname('RKOrdID').AsString);
      fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp.bmp' ;
      if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName)+'image')) then
          CreateDirectory(pchar(ExtractFilePath(Application.ExeName)+'image'),nil);
      if FileExists(fImagePath) then DeleteFile(fImagePath);
      Makebar(pchar(Txt),Length(Txt),3,3,0,PChar(fImagePath),3);
  except
      application.MessageBox('条形码生成失败！','提示信息',MB_ICONERROR);
      exit;
  end;
  if FileExists(fPrintFile) then
  begin
    //CDS_MainSel.Locate('RKOrdID',Trim(RKOrdID.Text),[]);
    //RMVariables['RKOrdID']:=Trim(CDS_MainSel.fieldbyname('RKOrdID').AsString);
    //RMVariables['OrderNo']:=Trim(CDS_MainSel.fieldbyname('OrderNo').AsString);
    //RMVariables['PS']:=Trim(RKOrdPS.Text);
    //RMVariables['QtyUnit']:=Trim(CDS_MainSel.fieldbyname('QtyUnit').AsString);
    RMVariables['QRBARCODE']:=fImagePath;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\入库单标签.rmf'),'提示',0);
  end;
end;

procedure TfrmCKProductBCPInList.CheckBox1Click(Sender: TObject);
begin
    SelOKNo(CDS_Main,CheckBox1.Checked);
end;

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

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

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

procedure TfrmCKProductBCPInList.CheckBox2Click(Sender: TObject);
begin
  IF CheckBox2.Checked then
  orderNo.Tag:=1
  else
  orderNo.Tag:=2;
end;

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

procedure TfrmCKProductBCPInList.Button1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行操作吗？','提示',32+4)<>IDYES then Exit;
  BegDate.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      while Locate('SSel',True,[]) do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('update CK_BanCP_CR SET CRNote='''+trim(edit_CRNote.Text)+''' where BCID='''+Trim(CDS_Main.fieldbyname('BCID').AsString)+''' ');
          sql.Add('update WFB_MJJY SET note='''+trim(edit_CRNote.Text)+''' where MJID='''+Trim(CDS_Main.fieldbyname('MJID').AsString)+''' ');
          execsql;
        end;
        edit;
        fieldbyName('CRnote').Value:=trim(edit_CRNote.text);
        fieldbyName('SSel').Value:=false;
        post;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Panel2.Visible:=false;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!','提示',0);
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmCKProductBCPInList.TBNoteClick(Sender: TObject);
begin
  edit_CRNote.Text:='';
  Panel2.Left:=(self.Width- Panel2.Width) div 2;
  Panel2.Visible:=true;
end;

end.
