unit U_PRTJYListGL;

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, BtnEdit;

type
  TfrmPRTJYListGL = 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;
    PRTColor: 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;
    MJType: TComboBox;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label10: TLabel;
    GangNo: TEdit;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    TBRKCX: TToolButton;
    v1Column4: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    CDS_Juan: TClientDataSet;
    CDS_JuanPRT: TClientDataSet;
    RMDBMX: TRMDBDataSet;
    RM3: TRMGridReport;
    BtnEditA1: TBtnEditA;
    ADOQueryPrint: TADOQuery;
    ToolButton2: TToolButton;
    v1Column7: TcxGridDBColumn;
    Label11: TLabel;
    SelfGangNo: TEdit;
    Panel4: TPanel;
    Label12: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    RichEdit1: TRichEdit;
    v1Column14: TcxGridDBColumn;
    ToolButton3: TToolButton;
    v1Column15: TcxGridDBColumn;
    ToolButton4: TToolButton;
    v1Column20: TcxGridDBColumn;
    Label36: TLabel;
    Label37: TLabel;
    LabNote1: TEdit;
    LabNote2: TEdit;
    v1Column21: TcxGridDBColumn;
    Panel2: TPanel;
    v1Column22: TcxGridDBColumn;
    Label13: TLabel;
    JTType: TEdit;
    ToolButton5: TToolButton;
    v1Column23: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    v1Column26: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    ToolButton6: TToolButton;
    CDS_CD: TClientDataSet;
    CDS_LS: TClientDataSet;
    RMCD: TRMGridReport;
    RMDBCD: TRMDBDataSet;
    Lot: TComboBox;
    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 ToolButton1Click(Sender: TObject);
    procedure BtnEditA1BtnClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure Panel10MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    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 ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
  private
    FLeft,FTop:Integer;
    procedure InitGrid();
    procedure GetLotData();
    procedure GetLotDataLie();
    procedure GetLotDataLie38Ye();
    function SaveConData(FConNO:string):Boolean;
    procedure GetBGData();
    procedure GetCDSData();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmPRTJYListGL: TfrmPRTJYListGL;

implementation
uses
  U_DataLink,U_RTFun,U_ZDYHelp,U_MJEdit;

{$R *.dfm}

procedure TfrmPRTJYListGL.FormDestroy(Sender: TObject);
begin
  frmPRTJYListGL:=nil;
end;

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

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

procedure TfrmPRTJYListGL.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      SQL.Add('select D.*,C.PiTiaoQty,C.OrderUnit,');
      sql.Add(' Case when D.MJLen>0 then D.KouFenHZ*1.00*100/(MJLen*MJFK) else Null end as KouFenAvg,');
      SQL.Add(' B.OrderNo,MPRTCode=isnull(B.MPRTCode,'''')+isnull(B.MPRTGY,''''),B.MPRTCodeName,B.MPRTMF,B.MPRTKZ,B.OrderNo,B.CustomerNoName,B.KgZMiXS,');
      SQL.Add(' PRTColor=Rtrim(isnull(C.PRTColorNo,''''))+'' ''+Rtrim(isnull(C.PRTColor,''''))+'' ''+RTrim(isnull(C.PRTColorEng,'''')) ,C.PRTHX,');
      sql.Add(' E.GangNo,E.SelfGangNo,B.CustomerNoName,'); 
      sql.Add(' Case when isnull(D.MJLenMa,-1)<=0 then Cast(D.MJLen*1.0936 as decimal(18,1)) else Cast(D.MJLenMa*1 as decimal(18,1)) end as YQty, ');
      SQL.Add(' CDQK=dbo.F_Get_Order_SubStr(D.MJID,''MJCDHZSL'')');
      sql.Add(',JYDate=CONVERT(varchar(10) , D.FillTime,120 ) ');
      sql.add('from WFB_MJJY D ');
      Sql.add(' inner join JYOrder_Main B on D.MainId=B.MainId');
      Sql.add(' inner join JYOrder_Sub C on D.SubId=C.SubId');
      sql.Add(' inner join JYOrder_Main_MD E on D.APID=E.MDID');
      sql.add('where D.FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
      sql.Add('  and D.FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',enddate.DateTime+1))+'''');
      Open;
      //ShowMessage(SQL.Text);
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

procedure TfrmPRTJYListGL.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('成品检验管理',Tv1,'成品检验');
  Close;
end;

procedure TfrmPRTJYListGL.FormShow(Sender: TObject);
begin

  ReadCxGrid('成品检验管理',Tv1,'成品检验');

end;

procedure TfrmPRTJYListGL.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;

  TcxGridToExcel('入库列表',cxGrid2);
end;

procedure TfrmPRTJYListGL.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 TfrmPRTJYListGL.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmPRTJYListGL.TBPrintClick(Sender: TObject);
var
   fPrintFile:String;
begin
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据！','提示',0);
    exit;
  end;
  with CDS_Main do
  begin
    while Locate('SSel',True,[])=True do
    begin
      with ADOQueryPrint do
      begin
        Close;
        SQL.Clear;
        sql.Add('exec P_Print_Lable :MJID');
        Parameters.ParamByName('MJID').Value:=Trim(CDS_Main.fieldbyname('MJID').AsString);
        Open;
      end;
      if Trim(ADOQueryPrint.fieldbyname('SLBName').AsString)<>'' then
        fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\'+Trim(ADOQueryPrint.fieldbyname('SLBName').AsString)
      else
        fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\通用英文标签.rmf' ;
      if FileExists(fPrintFile) then
      begin
        RMVariables['LabNote1']:=Trim(LabNote1.Text);
        RMVariables['LabNote2']:=Trim(LabNote2.Text);
        RM1.LoadFromFile(fPrintFile);
        //RM1.ShowReport;
        RM1.PrintReport;
      end else
      begin
        Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\'+Trim(ADOQueryPrint.fieldbyname('SLBName').AsString)),'提示',0);
      end;
      CDS_Main.Edit;
      CDS_Main.FieldByName('SSel').Value:=False;
      CDS_Main.Post;
    end;
  end;

end;

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

procedure TfrmPRTJYListGL.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 D.*,C.PiTiaoQty,C.OrderUnit,');
        sql.Add(' Case when D.MJLen>0 then D.KouFenHZ*1.00*100/(MJLen*MJFK) else Null end as KouFenAvg,');
        SQL.Add(' B.OrderNo,MPRTCode=isnull(B.MPRTCode,'''')+isnull(B.MPRTGY,''''),B.MPRTCodeName,B.MPRTMF,B.MPRTKZ,B.OrderNo,B.CustomerNoName,B.KgZMiXS,');
        SQL.Add(' PRTColor=Rtrim(isnull(C.PRTColorNo,''''))+'' ''+Rtrim(isnull(C.PRTColor,''''))+'' ''+RTrim(isnull(C.PRTColorEng,'''')) ,C.PRTHX,');
        sql.Add(' E.GangNo,E.SelfGangNo,B.CustomerNoName,');
        sql.Add(' Case when isnull(D.MJLenMa,-1)<=0 then Cast(D.MJLen*1.0936 as decimal(18,1)) else Cast(D.MJLenMa*1 as decimal(18,1)) end as YQty, ');
        sql.Add(' JYDate=CONVERT(varchar(10) , D.FillTime,120 ) ');
        sql.add('from WFB_MJJY D ');
        Sql.add(' inner join JYOrder_Main B on D.MainId=B.MainId');
        Sql.add(' inner join JYOrder_Sub C on D.SubId=C.SubId');
        sql.Add(' inner join JYOrder_Main_MD E on D.APID=E.MDID');
        sql.add('where B.OrderNo like :OrderNo');
        Parameters.ParamByName('orderNo').Value:='%'+Trim(orderNo.Text)+'%';

        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

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

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

procedure TfrmPRTJYListGL.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 CDS_Main.Locate('SSEl',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete WFB_MJJY 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 TfrmPRTJYListGL.GetLotDataLie();
var
  i,j,z,H,q,L,Y:Integer;
  FGangNo,FHZ,Lot0,Lot1,Lot2,Lot3,FSubId:String;
begin


  if CDS_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select GangNo=MD.SelfGangNo,A.MJXH,A.MJMaoZ,A.SubId,A.MainId,A.MJQty4,');
    if Pos('Y',Trim(BtnEditA1.Text))>0 then
    begin
      //sql.Add('MJLen=Cast(A.MJLen*1.0936 as decimal(18,1)),');
      sql.Add(' Case when isnull(A.MJLenMa,-1)=-1 then Cast(A.MJLen*1.0936 as decimal(18,1)) else Cast(A.MJLenMa*1 as decimal(18,1)) end as MJLen ');
    end else
    begin
      sql.Add('A.MJLen,');
    end;
    sql.Add('DD.OrderNo,JS.PRTColor,PRTEColor=JS.PRTColorEng,PRTColorNo=JS.PRTColorNo,JS.PRTHX ');
    //SQL.Add(' JQty=(select Count(*) from WFB_MJJY WM where WM.SubId=JS.SubId),');
    //sql.Add(' GQty=(select Count(*) from WFB_MJJY WM inner join JYOrder_Main_MD JMD on WM.APID=JMD.MDID ');
   // sql.add('       where WM.SubId=JS.SubId and JMD.SelfGangNo=MD.SelfGangNo)') ;
    sql.Add(' from WFB_MJJY A');
    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_Main_MD MD on A.APID=MD.MDID');
    sql.Add(' where A.MainId='''+Trim(CDS_Main.fieldbyname('MainId').AsString)+'''');
    sql.Add(' and A.ZhuangXiangSYFlag=0');
    //sql.Add(' and A.SubId=''JS1402100003'' ');
    //sql.Add(' and A.Valie=''Y'' ');
    if Trim(MJType.Text)<>'' then
    begin
      sql.Add(' and A.MJType='''+Trim(MJType.Text)+'''');
    end;
    sql.Add(' order by  JS.SubId,Cast(MD.SelfGangNo as int),A.MJXH');
    Open;
  end;
  //ShowMessage(IntToStr(ADOQueryTemp.RecordCount));
  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(' Lot0=Cast('''' as varchar(20)),Lot1=Cast('''' as varchar(20)),');
    sql.Add(' Lot2=Cast('''' as varchar(20)),Lot3=Cast('''' as varchar(20)),');
    sql.Add(' BaoNo=Cast('''' as varchar(20)),XH=Cast(0 as int),JJQty=Cast(0 as int),GGQty=Cast(0 as int),');
    sql.Add(' LenQty0=Cast(1.00 as decimal(18,1)),LenQty1=Cast(1.00 as decimal(18,1)),');
    sql.Add(' LenQty2=Cast(1.00 as decimal(18,1)),LenQty3=Cast(1.00 as decimal(18,1)),');
    sql.Add(' LenQty4=Cast(1.00 as decimal(18,1)),LenQty5=Cast(1.00 as decimal(18,1)),');
    sql.Add(' LenQty6=Cast(1.00 as decimal(18,1)),LenQty7=Cast(1.00 as decimal(18,1)),');
    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(' 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(' 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(' JH0=Cast(1 as int),JH1=Cast(1 as int),');
    sql.Add(' JH2=Cast(1 as int),JH3=Cast(1 as int),');
    sql.Add(' JH4=Cast(1 as int),JH5=Cast(1 as int),');
    sql.Add(' JH6=Cast(1 as int),JH7=Cast(1 as int),');
    sql.Add(' Hang=Cast(0 as int),');
    sql.Add(' Ye=Cast(0 as int),');
    sql.Add(' Lie=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;L:=0;Y:=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('Lot0').Value:=CDS_Juan.fieldbyname('GangNo').Value;
          //Lot.Items[0]:=CDS_Juan.fieldbyname('GangNo').Value;
          //Lot.Items[1]:='';
          //Lot.Items[2]:='';
          //Lot.Items[3]:='';
          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('Hang').Value:=0;
          CDS_JuanPRT.FieldByName('Ye').Value:=0;
          CDS_JuanPRT.FieldByName('JH0').Value:=1;
          CDS_JuanPRT.FieldByName('JH1').Value:=26;
          CDS_JuanPRT.FieldByName('JH2').Value:=1;
          CDS_JuanPRT.FieldByName('JH3').Value:=26;
          CDS_JuanPRT.FieldByName('JH4').Value:=1;
          CDS_JuanPRT.FieldByName('JH5').Value:=26;
          CDS_JuanPRT.FieldByName('JH6').Value:=1;
          CDS_JuanPRT.FieldByName('JH7').Value:=26;
          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));
          FSubId:=Trim(CDS_Juan.fieldbyname('SubId').AsString);
          //FGangNo:=Trim(CDS_Juan.fieldbyname('GangNo').AsString);
          H:=0;//行号
          L:=0;//列号
          Y:=0;//页号
          for i:=1 to 24 do
          begin
            CDS_JuanPRT.Append;
            CDS_JuanPRT.FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
            CDS_JuanPRT.FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').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('JH0').Value:=1+i;
            CDS_JuanPRT.FieldByName('JH1').Value:=26+i;
            CDS_JuanPRT.FieldByName('JH2').Value:=1+i;
            CDS_JuanPRT.FieldByName('JH3').Value:=26+i;
            CDS_JuanPRT.FieldByName('JH4').Value:=1+i;
            CDS_JuanPRT.FieldByName('JH5').Value:=26+i;
            CDS_JuanPRT.FieldByName('JH6').Value:=1+i;
            CDS_JuanPRT.FieldByName('JH7').Value:=26+i;
            CDS_JuanPRT.FieldByName('Lot0').Value:=CDS_Juan.fieldbyname('GangNo').Value;
            //CDS_JuanPRT.FieldByName('Lot0').Value:=Lot.Items[0];
           // CDS_JuanPRT.FieldByName('Lot1').Value:=Lot.Items[0];
            //CDS_JuanPRT.FieldByName('Lot2').Value:=Lot.Items[0];
            //CDS_JuanPRT.FieldByName('Lot3').Value:=Lot.Items[0];
            CDS_JuanPRT.FieldByName('Ye').Value:=Y;
            CDS_JuanPRT.FieldByName('Hang').Value:=i;
            CDS_JuanPRT.Post;
          end;
        end else //如果数据集不为空，色号缸号变为新的。
        begin
          if Trim(CDS_Juan.fieldbyname('SubId').AsString)<>Trim(FSubId) then
          begin
            CDS_JuanPRT.Append;
            CDS_JuanPRT.FieldByName('Lot0').Value:=CDS_Juan.fieldbyname('GangNo').Value;
            //Lot.Items[0]:=CDS_Juan.fieldbyname('GangNo').Value;
            //Lot.Items[1]:='';
            //Lot.Items[2]:='';
            //Lot.Items[3]:='';
            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('Hang').Value:=0;
            CDS_JuanPRT.FieldByName('Ye').Value:=Y+1;
            CDS_JuanPRT.FieldByName('JH0').Value:=1;
            CDS_JuanPRT.FieldByName('JH1').Value:=26;
            CDS_JuanPRT.FieldByName('JH2').Value:=1;
            CDS_JuanPRT.FieldByName('JH3').Value:=26;
            CDS_JuanPRT.FieldByName('JH4').Value:=1;
            CDS_JuanPRT.FieldByName('JH5').Value:=26;
            CDS_JuanPRT.FieldByName('JH6').Value:=1;
            CDS_JuanPRT.FieldByName('JH7').Value:=26;
            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.FieldByName('JQty').Value:=CDS_Juan.fieldbyname('JQty').Value;
            //CDS_JuanPRT.FieldByName('GQty').Value:=CDS_Juan.fieldbyname('GQty').Value;
            CDS_JuanPRT.Post;
            FHZ:=Trim((CDS_Juan.fieldbyname('SubId').AsString)
                 +Trim(CDS_Juan.fieldbyname('GangNo').AsString));
            FSubId:=Trim(CDS_Juan.fieldbyname('SubId').AsString);
            //FGangNo:=Trim(CDS_Juan.fieldbyname('GangNo').AsString);
            H:=0;//行号
            L:=0;//列号
            Y:=Y+1;//页号
            for i:=1 to 24 do
            begin
              CDS_JuanPRT.Append;
              CDS_JuanPRT.FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
              CDS_JuanPRT.FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').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('JH0').Value:=1+i;
              CDS_JuanPRT.FieldByName('JH1').Value:=26+i;
              CDS_JuanPRT.FieldByName('JH2').Value:=1+i;
              CDS_JuanPRT.FieldByName('JH3').Value:=26+i;
              CDS_JuanPRT.FieldByName('JH4').Value:=1+i;
              CDS_JuanPRT.FieldByName('JH5').Value:=26+i;
              CDS_JuanPRT.FieldByName('JH6').Value:=1+i;
              CDS_JuanPRT.FieldByName('JH7').Value:=26+i;
              CDS_JuanPRT.FieldByName('Lot0').Value:=CDS_Juan.fieldbyname('GangNo').Value;
              //CDS_JuanPRT.FieldByName('Lot0').Value:=Lot.Items[0];
             // CDS_JuanPRT.FieldByName('Lot1').Value:=Lot.Items[0];
              //CDS_JuanPRT.FieldByName('Lot2').Value:=Lot.Items[0];
              //CDS_JuanPRT.FieldByName('Lot3').Value:=Lot.Items[0];
              CDS_JuanPRT.FieldByName('Ye').Value:=Y;
              CDS_JuanPRT.FieldByName('Hang').Value:=i;
              CDS_JuanPRT.Post;
            end;
          end else  //subid 相同，缸号不同
          begin
            if (Trim(CDS_Juan.fieldbyname('SubId').AsString)
             +Trim(CDS_Juan.fieldbyname('GangNo').AsString))<>FHZ then
            begin
              if (L mod 2)=1 then
                L:=L+1
              else
                L:=L+2;
              if L<8 then
              begin
                CDS_JuanPRT.Locate('Ye;Hang', VarArrayOf([Y, 0]), [loPartialKey]);//如果列号小于8 定位到当前页第一行
                CDS_JuanPRT.Edit;
                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('Lot'+Trim(IntToStr(L div 2))).Value:=CDS_Juan.fieldbyname('GangNo').Value;
                //Lot.Items[L div 2]:=CDS_Juan.fieldbyname('GangNo').Value;
                CDS_JuanPRT.FieldByName('LenQty'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('MJLen').Value;
                CDS_JuanPRT.FieldByName('MZ'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('MJMaoZ').Value;
                CDS_JuanPRT.FieldByName('JZ'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('MJQty4').Value;
                CDS_JuanPRT.FieldByName('MJPS'+Trim(IntToStr(L))).Value:=1;
                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));
                FSubId:=Trim(CDS_Juan.fieldbyname('SubId').AsString);
                H:=0;
              end
              else  //如果列号大于7，则增加新的一页
              begin
                H:=0;//行号
                L:=0;//列号
                Y:=Y+1;//页号
                CDS_JuanPRT.Append;
                CDS_JuanPRT.FieldByName('Lot0').Value:=CDS_Juan.fieldbyname('GangNo').Value;
                //Lot.Items[0]:=CDS_Juan.fieldbyname('GangNo').Value;
                //Lot.Items[1]:='';
                //Lot.Items[2]:='';
                //Lot.Items[3]:='';
                //Lot0:=Trim(CDS_Juan.fieldbyname('GangNo').AsString);
                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('Hang').Value:=0;
                CDS_JuanPRT.FieldByName('Ye').Value:=Y;
               // CDS_JuanPRT.FieldByName('Lie').Value:=0;
                CDS_JuanPRT.FieldByName('JH0').Value:=1;
                CDS_JuanPRT.FieldByName('JH1').Value:=26;
                CDS_JuanPRT.FieldByName('JH2').Value:=1;
                CDS_JuanPRT.FieldByName('JH3').Value:=26;
                CDS_JuanPRT.FieldByName('JH4').Value:=1;
                CDS_JuanPRT.FieldByName('JH5').Value:=26;
                CDS_JuanPRT.FieldByName('JH6').Value:=1;
                CDS_JuanPRT.FieldByName('JH7').Value:=26;
                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.FieldByName('JQty').Value:=CDS_Juan.fieldbyname('JQty').Value;
                //CDS_JuanPRT.FieldByName('GQty').Value:=CDS_Juan.fieldbyname('GQty').Value;
                CDS_JuanPRT.Post;
                FHZ:=Trim(CDS_Juan.fieldbyname('SubId').AsString)
                 +Trim(CDS_Juan.fieldbyname('GangNo').AsString);
                FSubId:=Trim(CDS_Juan.fieldbyname('SubId').AsString);
                //FGangNo:=Trim(CDS_Juan.fieldbyname('GangNo').AsString);

                for i:=1 to 24 do
                begin
                  CDS_JuanPRT.Append;
                  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('JH0').Value:=1+i;
                  CDS_JuanPRT.FieldByName('JH1').Value:=26+i;
                  CDS_JuanPRT.FieldByName('JH2').Value:=1+i;
                  CDS_JuanPRT.FieldByName('JH3').Value:=26+i;
                  CDS_JuanPRT.FieldByName('JH4').Value:=1+i;
                  CDS_JuanPRT.FieldByName('JH5').Value:=26+i;
                  CDS_JuanPRT.FieldByName('JH6').Value:=1+i;
                  CDS_JuanPRT.FieldByName('JH7').Value:=26+i;
                  //CDS_JuanPRT.FieldByName('Lot0').Value:=CDS_Juan.fieldbyname('gangno').Value;
                  //CDS_JuanPRT.FieldByName('Lot1').Value:=Lot.Items[0];
                  //CDS_JuanPRT.FieldByName('Lot2').Value:=Lot.Items[0];
                 // CDS_JuanPRT.FieldByName('Lot3').Value:=Lot.Items[0];
                  CDS_JuanPRT.FieldByName('Ye').Value:=Y;
                  CDS_JuanPRT.FieldByName('Hang').Value:=i;
                  CDS_JuanPRT.FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
                  CDS_JuanPRT.FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').Value;
                  CDS_JuanPRT.Post;
                end;
              end;
            end else //数据集不为空，缸号色号相同
            begin

                H:=H+1;
                if H<25 then  //如果行号小于25   数据集不为空，缸号色号相同
                begin
                  CDS_JuanPRT.Locate('Ye;Hang', VarArrayOf([Y, H]), [loPartialKey]);//定位到当前页下一行
                  CDS_JuanPRT.Edit;
                  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('LenQty'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('MJLen').Value;
                  CDS_JuanPRT.FieldByName('MZ'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('MJMaoZ').Value;
                  CDS_JuanPRT.FieldByName('JZ'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('MJQty4').Value;
                  CDS_JuanPRT.FieldByName('Lot'+Trim(IntToStr(L div 2))).Value:=CDS_Juan.fieldbyname('GangNo').Value;
                  CDS_JuanPRT.FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
                  CDS_JuanPRT.FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').Value;
                  CDS_JuanPRT.FieldByName('MJPS'+Trim(IntToStr(L))).Value:=1;
                 // Lot.Items[L div 2]:=CDS_Juan.fieldbyname('GangNo').Value;
                  CDS_JuanPRT.Post;
                  FHZ:=Trim((CDS_Juan.fieldbyname('SubId').AsString)
                   +Trim(CDS_Juan.fieldbyname('GangNo').AsString));
                  FSubId:=Trim(CDS_Juan.fieldbyname('SubId').AsString);
                end else
                begin    //如果行号大于25，进行换列     数据集不为空，缸号色号相同
                  H:=0;
                  L:=L+1;
                  {if (L mod 2)=1 then
                    L:=L+1
                  else
                    L:=L+2; }
                  if L<8 then  //如果还在当前页   数据集不为空，缸号色号相同
                  begin
                    CDS_JuanPRT.Locate('Ye;Hang', VarArrayOf([Y, 0]), [loPartialKey]);//定位到当前页下一列第一行
                    CDS_JuanPRT.Edit;
                    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('LenQty'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('MJLen').Value;
                    CDS_JuanPRT.FieldByName('MZ'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('MJMaoZ').Value;
                    CDS_JuanPRT.FieldByName('JZ'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('MJQty4').Value;
                    CDS_JuanPRT.FieldByName('MJPS'+Trim(IntToStr(L))).Value:=1;
                    CDS_JuanPRT.FieldByName('Lot'+Trim(IntToStr(L div 2))).Value:=CDS_Juan.fieldbyname('GangNo').Value;
                    CDS_JuanPRT.FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
                    CDS_JuanPRT.FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').Value;
                    //Lot.Items[L div 2]:=CDS_Juan.fieldbyname('GangNo').Value;
                    CDS_JuanPRT.Post;
                    FHZ:=Trim((CDS_Juan.fieldbyname('SubId').AsString)
                         +Trim(CDS_Juan.fieldbyname('GangNo').AsString));
                    FSubId:=Trim(CDS_Juan.fieldbyname('SubId').AsString);
                  end else //如果不在当前页，则增加一页 数据集不为空，缸号色号相同
                  begin
                    CDS_JuanPRT.Append;
                    CDS_JuanPRT.FieldByName('Lot0').Value:=CDS_Juan.fieldbyname('GangNo').Value;
                   // Lot.Items[0]:=CDS_Juan.fieldbyname('GangNo').Value;
                    //Lot.Items[1]:='';
                    //Lot.Items[2]:='';
                    //Lot.Items[3]:='';
                    //Lot0:=Trim(CDS_Juan.fieldbyname('GangNo').AsString);
                    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('Hang').Value:=0;
                    CDS_JuanPRT.FieldByName('Ye').Value:=Y+1;
                   // CDS_JuanPRT.FieldByName('Lie').Value:=0;
                    CDS_JuanPRT.FieldByName('JH0').Value:=1;
                    CDS_JuanPRT.FieldByName('JH1').Value:=26;
                    CDS_JuanPRT.FieldByName('JH2').Value:=1;
                    CDS_JuanPRT.FieldByName('JH3').Value:=26;
                    CDS_JuanPRT.FieldByName('JH4').Value:=1;
                    CDS_JuanPRT.FieldByName('JH5').Value:=26;
                    CDS_JuanPRT.FieldByName('JH6').Value:=1;
                    CDS_JuanPRT.FieldByName('JH7').Value:=26;
                    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.FieldByName('JQty').Value:=CDS_Juan.fieldbyname('JQty').Value;
                    //CDS_JuanPRT.FieldByName('GQty').Value:=CDS_Juan.fieldbyname('GQty').Value;
                    CDS_JuanPRT.Post;
                    FHZ:=Trim(CDS_Juan.fieldbyname('SubId').AsString)
                     +Trim(CDS_Juan.fieldbyname('GangNo').AsString);
                    FSubId:=Trim(CDS_Juan.fieldbyname('SubId').AsString);
                    //FGangNo:=Trim(CDS_Juan.fieldbyname('GangNo').AsString);
                    H:=0;//行号
                    L:=0;//列号
                    Y:=Y+1;//页号
                    for i:=1 to 24 do
                    begin
                      CDS_JuanPRT.Append;
                      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('JH0').Value:=1+i;
                      CDS_JuanPRT.FieldByName('JH1').Value:=26+i;
                      CDS_JuanPRT.FieldByName('JH2').Value:=1+i;
                      CDS_JuanPRT.FieldByName('JH3').Value:=26+i;
                      CDS_JuanPRT.FieldByName('JH4').Value:=1+i;
                      CDS_JuanPRT.FieldByName('JH5').Value:=26+i;
                      CDS_JuanPRT.FieldByName('JH6').Value:=1+i;
                      CDS_JuanPRT.FieldByName('JH7').Value:=26+i;
                      CDS_JuanPRT.FieldByName('Lot0').Value:=Lot.Items[0];
                      CDS_JuanPRT.FieldByName('Lot1').Value:=Lot.Items[0];
                      CDS_JuanPRT.FieldByName('Lot2').Value:=Lot.Items[0];
                      CDS_JuanPRT.FieldByName('Lot3').Value:=Lot.Items[0];
                      CDS_JuanPRT.FieldByName('Ye').Value:=Y;
                      CDS_JuanPRT.FieldByName('Hang').Value:=i;
                      CDS_JuanPRT.FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
                      CDS_JuanPRT.FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').Value;
                      CDS_JuanPRT.Post;
                    end;
                  end;
                end;
          


            end;
          end;

        end
      end;
      Next;
    end;
  end;
  CDS_Juan.EnableControls;
end;
procedure TfrmPRTJYListGL.GetLotDataLie38Ye();//按页分
var
  i,j,z,H,q,L,Y:Integer;
  FGangNo,FHZ,Lot0,Lot1,Lot2,Lot3,FSubId:String;
begin
  if CDS_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select GangNo=MD.SelfGangNo,A.MJXH,A.MJMaoZ,A.SubId,A.MainId,A.MJQty4,');
    if Pos('Y',Trim(BtnEditA1.Text))>0 then
    begin
      //sql.Add('MJLen=Cast(A.MJLen*1.0936 as decimal(18,1)),');
      sql.Add(' Case when isnull(A.MJLenMa,-1)=-1 then Cast(A.MJLen*1.0936 as decimal(18,1)) else Cast(A.MJLenMa*1 as decimal(18,1)) end as MJLen ');
    end else
    begin
      sql.Add('A.MJLen,');
    end;
    sql.Add('DD.OrderNo,JS.PRTColor,PRTEColor=JS.PRTColorEng,PRTColorNo=JS.PRTColorNo,JS.PRTHX');
   // sql.Add(' JQty=(select Count(*) from WFB_MJJY WM where WM.SubId=JS.SubId),');
    //sql.Add(' GQty=(select Count(*) from WFB_MJJY WM inner join JYOrder_Main_MD JMD on WM.APID=JMD.MDID ');
    //sql.add('       where WM.SubId=JS.SubId and JMD.SelfGangNo=MD.SelfGangNo)') ;
    sql.Add(' from WFB_MJJY A');
    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_Main_MD MD on A.APID=MD.MDID');
    sql.Add(' where A.MainId='''+Trim(CDS_Main.fieldbyname('MainId').AsString)+'''');
    sql.Add(' and A.ZhuangXiangSYFlag=0');
    //sql.Add(' and A.SubId=''JS1402100003'' ');
    //sql.Add(' and A.Valie=''Y'' ');
    if Trim(MJType.Text)<>'' then
    begin
      sql.Add(' and A.MJType='''+Trim(MJType.Text)+'''');
    end;
    sql.Add(' order by  JS.SubId,Cast(MD.SelfGangNo as int),A.MJXH');
    Open;
  end;
  //ShowMessage(IntToStr(ADOQueryTemp.RecordCount));
  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(' Lot0=Cast('''' as varchar(20)),Lot1=Cast('''' as varchar(20)),');
    sql.Add(' Lot2=Cast('''' as varchar(20)),Lot3=Cast('''' as varchar(20)),');
    sql.Add(' Lot4=Cast('''' as varchar(20)),Lot5=Cast('''' as varchar(20)),');
    sql.Add(' Lot6=Cast('''' as varchar(20)),Lot7=Cast('''' as varchar(20)),');
    sql.Add(' CoL0=Cast('''' as varchar(80)),CoL1=Cast('''' as varchar(80)),');
    sql.Add(' CoL2=Cast('''' as varchar(80)),CoL3=Cast('''' as varchar(80)),');
    sql.Add(' CoL4=Cast('''' as varchar(80)),CoL5=Cast('''' as varchar(80)),');
    sql.Add(' CoL6=Cast('''' as varchar(80)),CoL7=Cast('''' as varchar(80)),');
    sql.Add(' BaoNo=Cast('''' as varchar(20)),XH=Cast(0 as int),JJQty=Cast(0 as int),GGQty=Cast(0 as int),');
    sql.Add(' LenQty0=Cast(1.00 as decimal(18,1)),LenQty1=Cast(1.00 as decimal(18,1)),');
    sql.Add(' LenQty2=Cast(1.00 as decimal(18,1)),LenQty3=Cast(1.00 as decimal(18,1)),');
    sql.Add(' LenQty4=Cast(1.00 as decimal(18,1)),LenQty5=Cast(1.00 as decimal(18,1)),');
    sql.Add(' LenQty6=Cast(1.00 as decimal(18,1)),LenQty7=Cast(1.00 as decimal(18,1)),');
    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(' 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(' 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(' JH0=Cast(1 as int),JH1=Cast(1 as int),');
    sql.Add(' JH2=Cast(1 as int),JH3=Cast(1 as int),');
    sql.Add(' JH4=Cast(1 as int),JH5=Cast(1 as int),');
    sql.Add(' JH6=Cast(1 as int),JH7=Cast(1 as int),');
    sql.Add(' Hang=Cast(0 as int),');
    sql.Add(' Ye=Cast(0 as int),');
    sql.Add(' Lie=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;L:=0;Y:=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('Lot0').Value:=CDS_Juan.fieldbyname('GangNo').Value;
          //Lot.Items[0]:=CDS_Juan.fieldbyname('GangNo').Value;
          //Lot.Items[1]:='';
          //Lot.Items[2]:='';
          //Lot.Items[3]:='';
          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('CoL0').Value:=Trim(CDS_Juan.fieldbyname('PRTColorNo').AsString)
                                                 +' '+Trim(CDS_Juan.fieldbyname('PRTEColor').AsString)
                                                 +' '+Trim(CDS_Juan.fieldbyname('PRTHX').AsString);
          CDS_JuanPRT.FieldByName('MJPS0').Value:=1;
          CDS_JuanPRT.FieldByName('Hang').Value:=0;
          CDS_JuanPRT.FieldByName('Ye').Value:=0;
          CDS_JuanPRT.FieldByName('JH0').Value:=1;
          CDS_JuanPRT.FieldByName('JH1').Value:=1;
          CDS_JuanPRT.FieldByName('JH2').Value:=1;
          CDS_JuanPRT.FieldByName('JH3').Value:=1;
          CDS_JuanPRT.FieldByName('JH4').Value:=1;
          CDS_JuanPRT.FieldByName('JH5').Value:=1;
          CDS_JuanPRT.FieldByName('JH6').Value:=1;
          CDS_JuanPRT.FieldByName('JH7').Value:=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));
          FSubId:=Trim(CDS_Juan.fieldbyname('SubId').AsString);
          //FGangNo:=Trim(CDS_Juan.fieldbyname('GangNo').AsString);
          H:=0;//行号
          L:=0;//列号
          Y:=0;//页号
          for i:=1 to 37 do
          begin
            CDS_JuanPRT.Append;
            {CDS_JuanPRT.FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
            CDS_JuanPRT.FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').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('JH0').Value:=1+i;
            CDS_JuanPRT.FieldByName('JH1').Value:=1+i;
            CDS_JuanPRT.FieldByName('JH2').Value:=1+i;
            CDS_JuanPRT.FieldByName('JH3').Value:=1+i;
            CDS_JuanPRT.FieldByName('JH4').Value:=1+i;
            CDS_JuanPRT.FieldByName('JH5').Value:=1+i;
            CDS_JuanPRT.FieldByName('JH6').Value:=1+i;
            CDS_JuanPRT.FieldByName('JH7').Value:=1+i;
            CDS_JuanPRT.FieldByName('Lot0').Value:=CDS_Juan.fieldbyname('GangNo').Value;
            CDS_JuanPRT.FieldByName('CoL0').Value:=Trim(CDS_Juan.fieldbyname('PRTColorNo').AsString)
                                                 +' '+Trim(CDS_Juan.fieldbyname('PRTEColor').AsString)
                                                 +' '+Trim(CDS_Juan.fieldbyname('PRTHX').AsString);
            //CDS_JuanPRT.FieldByName('Lot0').Value:=Lot.Items[0];
           // CDS_JuanPRT.FieldByName('Lot1').Value:=Lot.Items[0];
            //CDS_JuanPRT.FieldByName('Lot2').Value:=Lot.Items[0];
            //CDS_JuanPRT.FieldByName('Lot3').Value:=Lot.Items[0];
            CDS_JuanPRT.FieldByName('Ye').Value:=Y;
            CDS_JuanPRT.FieldByName('Hang').Value:=i;
            CDS_JuanPRT.Post;
          end;
        end else //如果数据集不为空，色号缸号变为新的。
        begin
          if Trim(CDS_Juan.fieldbyname('SubId').AsString)<>Trim(FSubId) then
          begin
            L:=L+1;
            if L<8 then
            begin
              CDS_JuanPRT.Locate('Ye;Hang', VarArrayOf([Y, 0]), [loPartialKey]);//如果列号小于8 定位到当前页第一行
              CDS_JuanPRT.Edit;
              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('Lot'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('GangNo').Value;
              CDS_JuanPRT.FieldByName('CoL'+Trim(IntToStr(L))).Value:=Trim(CDS_Juan.fieldbyname('PRTColorNo').AsString)
                                                 +' '+Trim(CDS_Juan.fieldbyname('PRTEColor').AsString)
                                                 +' '+Trim(CDS_Juan.fieldbyname('PRTHX').AsString);
              //Lot.Items[L div 2]:=CDS_Juan.fieldbyname('GangNo').Value;
              CDS_JuanPRT.FieldByName('LenQty'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('MJLen').Value;
              CDS_JuanPRT.FieldByName('MZ'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('MJMaoZ').Value;
              CDS_JuanPRT.FieldByName('JZ'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('MJQty4').Value;
              CDS_JuanPRT.FieldByName('MJPS'+Trim(IntToStr(L))).Value:=1;
              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));
              FSubId:=Trim(CDS_Juan.fieldbyname('SubId').AsString);
              H:=0;
            end else   //另起一页
            begin
              CDS_JuanPRT.Append;
              CDS_JuanPRT.FieldByName('Lot0').Value:=CDS_Juan.fieldbyname('GangNo').Value;
              CDS_JuanPRT.FieldByName('CoL0').Value:=Trim(CDS_Juan.fieldbyname('PRTColorNo').AsString)
                                                 +' '+Trim(CDS_Juan.fieldbyname('PRTEColor').AsString)
                                                 +' '+Trim(CDS_Juan.fieldbyname('PRTHX').AsString);
              //Lot.Items[0]:=CDS_Juan.fieldbyname('GangNo').Value;
              //Lot.Items[1]:='';
              //Lot.Items[2]:='';
              //Lot.Items[3]:='';
              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('Hang').Value:=0;
              CDS_JuanPRT.FieldByName('Ye').Value:=Y+1;
              CDS_JuanPRT.FieldByName('JH0').Value:=1;
              CDS_JuanPRT.FieldByName('JH1').Value:=1;
              CDS_JuanPRT.FieldByName('JH2').Value:=1;
              CDS_JuanPRT.FieldByName('JH3').Value:=1;
              CDS_JuanPRT.FieldByName('JH4').Value:=1;
              CDS_JuanPRT.FieldByName('JH5').Value:=1;
              CDS_JuanPRT.FieldByName('JH6').Value:=1;
              CDS_JuanPRT.FieldByName('JH7').Value:=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.FieldByName('JQty').Value:=CDS_Juan.fieldbyname('JQty').Value;
              //CDS_JuanPRT.FieldByName('GQty').Value:=CDS_Juan.fieldbyname('GQty').Value;
              CDS_JuanPRT.Post;
              FHZ:=Trim((CDS_Juan.fieldbyname('SubId').AsString)
                   +Trim(CDS_Juan.fieldbyname('GangNo').AsString));
              FSubId:=Trim(CDS_Juan.fieldbyname('SubId').AsString);
              //FGangNo:=Trim(CDS_Juan.fieldbyname('GangNo').AsString);
              H:=0;//行号
              L:=0;//列号
              Y:=Y+1;//页号
              for i:=1 to 37 do
              begin
                CDS_JuanPRT.Append;
               { CDS_JuanPRT.FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
                CDS_JuanPRT.FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').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('JH0').Value:=1+i;
                CDS_JuanPRT.FieldByName('JH1').Value:=1+i;
                CDS_JuanPRT.FieldByName('JH2').Value:=1+i;
                CDS_JuanPRT.FieldByName('JH3').Value:=1+i;
                CDS_JuanPRT.FieldByName('JH4').Value:=1+i;
                CDS_JuanPRT.FieldByName('JH5').Value:=1+i;
                CDS_JuanPRT.FieldByName('JH6').Value:=1+i;
                CDS_JuanPRT.FieldByName('JH7').Value:=1+i;
                CDS_JuanPRT.FieldByName('Lot0').Value:=CDS_Juan.fieldbyname('GangNo').Value;
                CDS_JuanPRT.FieldByName('CoL0').Value:=Trim(CDS_Juan.fieldbyname('PRTColorNo').AsString)
                                                 +' '+Trim(CDS_Juan.fieldbyname('PRTEColor').AsString)
                                                 +' '+Trim(CDS_Juan.fieldbyname('PRTHX').AsString);
                //CDS_JuanPRT.FieldByName('Lot0').Value:=Lot.Items[0];
               // CDS_JuanPRT.FieldByName('Lot1').Value:=Lot.Items[0];
                //CDS_JuanPRT.FieldByName('Lot2').Value:=Lot.Items[0];
                //CDS_JuanPRT.FieldByName('Lot3').Value:=Lot.Items[0];
                CDS_JuanPRT.FieldByName('Ye').Value:=Y;
                CDS_JuanPRT.FieldByName('Hang').Value:=i;
                CDS_JuanPRT.Post;
              end;
            end;

          end else  //subid 相同，缸号不同
          begin
            if (Trim(CDS_Juan.fieldbyname('SubId').AsString)
             +Trim(CDS_Juan.fieldbyname('GangNo').AsString))<>FHZ then
            begin
              L:=L+1;
              if L<8 then
              begin
                CDS_JuanPRT.Locate('Ye;Hang', VarArrayOf([Y, 0]), [loPartialKey]);//如果列号小于8 定位到当前页第一行
                CDS_JuanPRT.Edit;
                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('Lot'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('GangNo').Value;
                CDS_JuanPRT.FieldByName('CoL'+Trim(IntToStr(L))).Value:=Trim(CDS_Juan.fieldbyname('PRTColorNo').AsString)
                                                 +' '+Trim(CDS_Juan.fieldbyname('PRTEColor').AsString)
                                                 +' '+Trim(CDS_Juan.fieldbyname('PRTHX').AsString);
                //Lot.Items[L div 2]:=CDS_Juan.fieldbyname('GangNo').Value;
                CDS_JuanPRT.FieldByName('LenQty'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('MJLen').Value;
                CDS_JuanPRT.FieldByName('MZ'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('MJMaoZ').Value;
                CDS_JuanPRT.FieldByName('JZ'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('MJQty4').Value;
                CDS_JuanPRT.FieldByName('MJPS'+Trim(IntToStr(L))).Value:=1;
                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));
                FSubId:=Trim(CDS_Juan.fieldbyname('SubId').AsString);
                H:=0;
              end
              else  //如果列号大于7，则增加新的一页
              begin
                H:=0;//行号
                L:=0;//列号
                Y:=Y+1;//页号
                CDS_JuanPRT.Append;
                CDS_JuanPRT.FieldByName('Lot0').Value:=CDS_Juan.fieldbyname('GangNo').Value;
                CDS_JuanPRT.FieldByName('CoL0').Value:=Trim(CDS_Juan.fieldbyname('PRTColorNo').AsString)
                                                 +' '+Trim(CDS_Juan.fieldbyname('PRTEColor').AsString)
                                                 +' '+Trim(CDS_Juan.fieldbyname('PRTHX').AsString);
                //Lot.Items[0]:=CDS_Juan.fieldbyname('GangNo').Value;
                //Lot.Items[1]:='';
                //Lot.Items[2]:='';
                //Lot.Items[3]:='';
                //Lot0:=Trim(CDS_Juan.fieldbyname('GangNo').AsString);
                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('Hang').Value:=0;
                CDS_JuanPRT.FieldByName('Ye').Value:=Y;
               // CDS_JuanPRT.FieldByName('Lie').Value:=0;
                CDS_JuanPRT.FieldByName('JH0').Value:=1;
                CDS_JuanPRT.FieldByName('JH1').Value:=1;
                CDS_JuanPRT.FieldByName('JH2').Value:=1;
                CDS_JuanPRT.FieldByName('JH3').Value:=1;
                CDS_JuanPRT.FieldByName('JH4').Value:=1;
                CDS_JuanPRT.FieldByName('JH5').Value:=1;
                CDS_JuanPRT.FieldByName('JH6').Value:=1;
                CDS_JuanPRT.FieldByName('JH7').Value:=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.FieldByName('JQty').Value:=CDS_Juan.fieldbyname('JQty').Value;
                //CDS_JuanPRT.FieldByName('GQty').Value:=CDS_Juan.fieldbyname('GQty').Value;
                CDS_JuanPRT.Post;
                FHZ:=Trim(CDS_Juan.fieldbyname('SubId').AsString)
                 +Trim(CDS_Juan.fieldbyname('GangNo').AsString);
                FSubId:=Trim(CDS_Juan.fieldbyname('SubId').AsString);
                //FGangNo:=Trim(CDS_Juan.fieldbyname('GangNo').AsString);

                for i:=1 to 37 do
                begin
                  CDS_JuanPRT.Append;
                  {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('JH0').Value:=1+i;
                  CDS_JuanPRT.FieldByName('JH1').Value:=1+i;
                  CDS_JuanPRT.FieldByName('JH2').Value:=1+i;
                  CDS_JuanPRT.FieldByName('JH3').Value:=1+i;
                  CDS_JuanPRT.FieldByName('JH4').Value:=1+i;
                  CDS_JuanPRT.FieldByName('JH5').Value:=1+i;
                  CDS_JuanPRT.FieldByName('JH6').Value:=1+i;
                  CDS_JuanPRT.FieldByName('JH7').Value:=1+i;
                  //CDS_JuanPRT.FieldByName('Lot0').Value:=CDS_Juan.fieldbyname('gangno').Value;
                  //CDS_JuanPRT.FieldByName('Lot1').Value:=Lot.Items[0];
                  //CDS_JuanPRT.FieldByName('Lot2').Value:=Lot.Items[0];
                 // CDS_JuanPRT.FieldByName('Lot3').Value:=Lot.Items[0];
                  CDS_JuanPRT.FieldByName('Ye').Value:=Y;
                  CDS_JuanPRT.FieldByName('Hang').Value:=i;
                 { CDS_JuanPRT.FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
                  CDS_JuanPRT.FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').Value;}
                  CDS_JuanPRT.Post;
                end;
              end;
            end else //数据集不为空，缸号色号相同
            begin

                H:=H+1;
                if H<38 then  //如果行号小于38   数据集不为空，缸号色号相同
                begin
                  CDS_JuanPRT.Locate('Ye;Hang', VarArrayOf([Y, H]), [loPartialKey]);//定位到当前页下一行
                  CDS_JuanPRT.Edit;
                  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('LenQty'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('MJLen').Value;
                  CDS_JuanPRT.FieldByName('MZ'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('MJMaoZ').Value;
                  CDS_JuanPRT.FieldByName('JZ'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('MJQty4').Value;
                  CDS_JuanPRT.FieldByName('Lot'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('GangNo').Value;
                  CDS_JuanPRT.FieldByName('CoL'+Trim(IntToStr(L))).Value:=Trim(CDS_Juan.fieldbyname('PRTColorNo').AsString)
                                                 +' '+Trim(CDS_Juan.fieldbyname('PRTEColor').AsString)
                                                 +' '+Trim(CDS_Juan.fieldbyname('PRTHX').AsString);
                  CDS_JuanPRT.FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
                  CDS_JuanPRT.FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').Value;
                  CDS_JuanPRT.FieldByName('MJPS'+Trim(IntToStr(L))).Value:=1;
                 // Lot.Items[L div 2]:=CDS_Juan.fieldbyname('GangNo').Value;
                  CDS_JuanPRT.Post;
                  FHZ:=Trim((CDS_Juan.fieldbyname('SubId').AsString)
                   +Trim(CDS_Juan.fieldbyname('GangNo').AsString));
                  FSubId:=Trim(CDS_Juan.fieldbyname('SubId').AsString);
                end else
                begin    //如果行号大于38，进行换列     数据集不为空，缸号色号相同
                  H:=0;
                  L:=L+1;
                  {if (L mod 2)=1 then
                    L:=L+1
                  else
                    L:=L+2; }
                  if L<8 then  //如果还在当前页   数据集不为空，缸号色号相同
                  begin
                    CDS_JuanPRT.Locate('Ye;Hang', VarArrayOf([Y, 0]), [loPartialKey]);//定位到当前页下一列第一行
                    CDS_JuanPRT.Edit;
                    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('LenQty'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('MJLen').Value;
                    CDS_JuanPRT.FieldByName('MZ'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('MJMaoZ').Value;
                    CDS_JuanPRT.FieldByName('JZ'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('MJQty4').Value;
                    CDS_JuanPRT.FieldByName('MJPS'+Trim(IntToStr(L))).Value:=1;
                    CDS_JuanPRT.FieldByName('Lot'+Trim(IntToStr(L))).Value:=CDS_Juan.fieldbyname('GangNo').Value;
                    CDS_JuanPRT.FieldByName('CoL'+Trim(IntToStr(L))).Value:=Trim(CDS_Juan.fieldbyname('PRTColorNo').AsString)
                                                 +' '+Trim(CDS_Juan.fieldbyname('PRTEColor').AsString)
                                                 +' '+Trim(CDS_Juan.fieldbyname('PRTHX').AsString);
                    CDS_JuanPRT.FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
                    CDS_JuanPRT.FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').Value;
                    //Lot.Items[L div 2]:=CDS_Juan.fieldbyname('GangNo').Value;
                    CDS_JuanPRT.Post;
                    FHZ:=Trim((CDS_Juan.fieldbyname('SubId').AsString)
                         +Trim(CDS_Juan.fieldbyname('GangNo').AsString));
                    FSubId:=Trim(CDS_Juan.fieldbyname('SubId').AsString);
                  end else //如果不在当前页，则增加一页 数据集不为空，缸号色号相同
                  begin
                    CDS_JuanPRT.Append;
                    CDS_JuanPRT.FieldByName('Lot0').Value:=CDS_Juan.fieldbyname('GangNo').Value;
                    CDS_JuanPRT.FieldByName('CoL0').Value:=Trim(CDS_Juan.fieldbyname('PRTColorNo').AsString)
                                                 +' '+Trim(CDS_Juan.fieldbyname('PRTEColor').AsString)
                                                 +' '+Trim(CDS_Juan.fieldbyname('PRTHX').AsString);
                   // Lot.Items[0]:=CDS_Juan.fieldbyname('GangNo').Value;
                    //Lot.Items[1]:='';
                    //Lot.Items[2]:='';
                    //Lot.Items[3]:='';
                    //Lot0:=Trim(CDS_Juan.fieldbyname('GangNo').AsString);
                    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('Hang').Value:=0;
                    CDS_JuanPRT.FieldByName('Ye').Value:=Y+1;
                   // CDS_JuanPRT.FieldByName('Lie').Value:=0;
                    CDS_JuanPRT.FieldByName('JH0').Value:=1;
                    CDS_JuanPRT.FieldByName('JH1').Value:=1;
                    CDS_JuanPRT.FieldByName('JH2').Value:=1;
                    CDS_JuanPRT.FieldByName('JH3').Value:=1;
                    CDS_JuanPRT.FieldByName('JH4').Value:=1;
                    CDS_JuanPRT.FieldByName('JH5').Value:=1;
                    CDS_JuanPRT.FieldByName('JH6').Value:=1;
                    CDS_JuanPRT.FieldByName('JH7').Value:=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.FieldByName('JQty').Value:=CDS_Juan.fieldbyname('JQty').Value;
                    //CDS_JuanPRT.FieldByName('GQty').Value:=CDS_Juan.fieldbyname('GQty').Value;
                    CDS_JuanPRT.Post;
                    FHZ:=Trim(CDS_Juan.fieldbyname('SubId').AsString)
                     +Trim(CDS_Juan.fieldbyname('GangNo').AsString);
                    FSubId:=Trim(CDS_Juan.fieldbyname('SubId').AsString);
                    //FGangNo:=Trim(CDS_Juan.fieldbyname('GangNo').AsString);
                    H:=0;//行号
                    L:=0;//列号
                    Y:=Y+1;//页号
                    for i:=1 to 37 do
                    begin
                      CDS_JuanPRT.Append;
                      {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('JH0').Value:=1+i;
                      CDS_JuanPRT.FieldByName('JH1').Value:=1+i;
                      CDS_JuanPRT.FieldByName('JH2').Value:=1+i;
                      CDS_JuanPRT.FieldByName('JH3').Value:=1+i;
                      CDS_JuanPRT.FieldByName('JH4').Value:=1+i;
                      CDS_JuanPRT.FieldByName('JH5').Value:=1+i;
                      CDS_JuanPRT.FieldByName('JH6').Value:=1+i;
                      CDS_JuanPRT.FieldByName('JH7').Value:=1+i;
                      CDS_JuanPRT.FieldByName('Ye').Value:=Y;
                      CDS_JuanPRT.FieldByName('Hang').Value:=i;
                      {CDS_JuanPRT.FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
                      CDS_JuanPRT.FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').Value; }
                      CDS_JuanPRT.Post;
                    end;
                  end;
                end;
            end;
          end;
        end
      end;
      Next;
    end;
  end;
  CDS_Juan.EnableControls;
end;
procedure TfrmPRTJYListGL.GetLotData();
var
  i,j,z,h,q,Hang,GHang,JuanQty,GangQty:Integer;
  FGangNo,FHZ,FSubId:String;
begin
  if CDS_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select GangNo=MD.SelfGangNo,A.MJXH,A.MJMaoZ,A.SubId,A.MainId,A.MJQty4,');
    sql.Add(' Case when isnull(A.DuoPinNote,'''')<>'''' then ''(''+RTrim(A.DuoPinNote)+'')'' else A.DuoPinNote end as  DPNote,');
    if Pos('Y',Trim(BtnEditA1.Text))>0 then
    begin
      //sql.Add('MJLen=Cast(A.MJLen*1.0936 as decimal(18,1)),');
      sql.Add(' Case when isnull(A.MJLenMa,-1)=-1 then Cast(A.MJLen*1.0936 as decimal(18,1)) else Cast(A.MJLenMa*1 as decimal(18,1)) end as MJLen, ');
    end else
    begin
      sql.Add('A.MJLen,');
    end;
    sql.Add('DD.OrderNo,JS.PRTColor,PRTEColor=JS.PRTColorEng,PRTColorNo=JS.PRTColorNo,JS.PRTHX ');
    //sql.Add(' JJQty=(select Count(*) from WFB_MJJY WM where WM.SubId=JS.SubId),');
    //sql.Add(' GGQty=(select Count(*) from WFB_MJJY WM inner join JYOrder_Main_MD JMD on WM.APID=JMD.MDID ');
    //sql.add('       where WM.SubId=JS.SubId and JMD.SelfGangNo=MD.SelfGangNo)') ;
    sql.Add(' from WFB_MJJY A');
    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_Main_MD MD on A.APID=MD.MDID');
    sql.Add(' where A.MainId='''+Trim(CDS_Main.fieldbyname('MainId').AsString)+'''');
    sql.Add(' and A.ZhuangXiangSYFlag=0');
    if Trim(MJType.Text)<>'' then
    begin
      sql.Add(' and A.MJType='''+Trim(MJType.Text)+'''');
    end;
    sql.Add(' order by  A.SubId,Cast(MD.SelfGangNo as int),A.MJXH');
    //ShowMessage(sql.Text);
    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,1)),LenQty1=Cast(1.00 as decimal(18,1)),');
    sql.Add(' LenQty2=Cast(1.00 as decimal(18,1)),LenQty3=Cast(1.00 as decimal(18,1)),');
    sql.Add(' LenQty4=Cast(1.00 as decimal(18,1)),LenQty5=Cast(1.00 as decimal(18,1)),');
    sql.Add(' LenQty6=Cast(1.00 as decimal(18,1)),LenQty7=Cast(1.00 as decimal(18,1)),');
    sql.Add(' LenQty8=Cast(1.00 as decimal(18,1)),LenQty9=Cast(1.00 as decimal(18,1)),');
    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(' DP0=Cast('''' as varchar(20)),DP1=Cast('''' as varchar(20)),');
    sql.Add(' DP2=Cast('''' as varchar(20)),DP3=Cast('''' as varchar(20)),');
    sql.Add(' DP4=Cast('''' as varchar(20)),DP5=Cast('''' as varchar(20)),');
    sql.Add(' DP6=Cast('''' as varchar(20)),DP7=Cast('''' as varchar(20)),');
    sql.Add(' DP8=Cast('''' as varchar(20)),DP9=Cast('''' as varchar(20)),');
    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(' JuanQty=Cast(0 as int),GangQty=Cast(0 as int),');
    sql.Add(' XH=Cast(0 as int),Hang=Cast(0 as int),GHang=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;hang:=0; GHang:=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
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            SQL.Add('select Count(*) JuanQty from WFB_MJJY where SubId='''+Trim(CDS_Juan.fieldbyname('SubId').AsString)+'''');
            Open;
          end;
          JuanQty:=ADOQueryTemp.fieldbyname('JuanQty').AsInteger;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            SQL.Add('select Count(*) GangQty from WFB_MJJY A');
            sql.Add(' inner join JYOrder_Main_MD B on A.APID=B.MDID ');
            sql.Add(' where A.SubId='''+Trim(CDS_Juan.fieldbyname('SubId').AsString)+'''');
            sql.Add(' and B.SelfGangNo='''+Trim(CDS_Juan.fieldbyname('GangNo').AsString)+'''');
            Open;
          end;
          GangQty:=ADOQueryTemp.fieldbyname('GangQty').AsInteger;
          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('Hang').Value:=0;
          CDS_JuanPRT.FieldByName('GHang').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('DP0').Value:=CDS_Juan.fieldbyname('DPNote').Value;
          CDS_JuanPRT.FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
          CDS_JuanPRT.FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').Value;
          CDS_JuanPRT.FieldByName('JuanQty').Value:=JuanQty;
          CDS_JuanPRT.FieldByName('GangQty').Value:=GangQty;
          CDS_JuanPRT.Post;
          FHZ:=Trim(CDS_Juan.fieldbyname('SubId').AsString)
           +Trim(CDS_Juan.fieldbyname('GangNo').AsString);
          FSubId:=Trim(CDS_Juan.fieldbyname('SubId').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
            GHang:=0;
            if Trim(CDS_Juan.fieldbyname('SubId').AsString)<>FSubId then
            begin
              Hang:=0;
              with ADOQueryTemp do
              begin
                Close;
                sql.Clear;
                SQL.Add('select Count(*) JuanQty from WFB_MJJY where SubId='''+Trim(CDS_Juan.fieldbyname('SubId').AsString)+'''');
                Open;
              end;
              JuanQty:=ADOQueryTemp.fieldbyname('JuanQty').AsInteger;
              with ADOQueryTemp do
              begin
                Close;
                sql.Clear;
                SQL.Add('select Count(*) GangQty from WFB_MJJY A');
                sql.Add(' inner join JYOrder_Main_MD B on A.APID=B.MDID ');
                sql.Add(' where A.SubId='''+Trim(CDS_Juan.fieldbyname('SubId').AsString)+'''');
                sql.Add(' and B.SelfGangNo='''+Trim(CDS_Juan.fieldbyname('GangNo').AsString)+'''');
                Open;
              end;
              GangQty:=ADOQueryTemp.fieldbyname('GangQty').AsInteger;
            end else
            begin
              Hang:=Hang+1;
              with ADOQueryTemp do
              begin
                Close;
                sql.Clear;
                SQL.Add('select Count(*) GangQty from WFB_MJJY A');
                sql.Add(' inner join JYOrder_Main_MD B on A.APID=B.MDID ');
                sql.Add(' where A.SubId='''+Trim(CDS_Juan.fieldbyname('SubId').AsString)+'''');
                sql.Add(' and B.SelfGangNo='''+Trim(CDS_Juan.fieldbyname('GangNo').AsString)+'''');
                Open;
              end;
              GangQty:=ADOQueryTemp.fieldbyname('GangQty').AsInteger;
            end;
            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('Hang').Value:=Hang;
            CDS_JuanPRT.FieldByName('GHang').Value:=GHang;
            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('DP0').Value:=CDS_Juan.fieldbyname('DPNote').Value;
            CDS_JuanPRT.FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
            CDS_JuanPRT.FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').Value;
            CDS_JuanPRT.FieldByName('JuanQty').Value:=JuanQty;
            CDS_JuanPRT.FieldByName('GangQty').Value:=GangQty;
            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;
                CDS_JuanPRT.FieldByName('DP'+Trim(IntToStr(Z))).Value:=CDS_Juan.fieldbyname('DPNote').Value;
                FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
                FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').Value;
                Post;
              end;
            end else
            begin
              GHang:=GHang+1;
              Hang:=Hang+1;
              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('Hang').Value:=Hang;
              CDS_JuanPRT.FieldByName('GHang').Value:=GHang;
              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('DP0').Value:=CDS_Juan.fieldbyname('DPNote').Value;
              CDS_JuanPRT.FieldByName('SubId').Value:=CDS_Juan.fieldbyname('SubId').Value;
              CDS_JuanPRT.FieldByName('MainId').Value:=CDS_Juan.fieldbyname('MainId').Value;
              CDS_JuanPRT.FieldByName('JuanQty').Value:=JuanQty;
              CDS_JuanPRT.FieldByName('GangQty').Value:=GangQty;
              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 TfrmPRTJYListGL.ToolButton1Click(Sender: TObject);
var
  fPrintFile:String;
begin
  if Trim(BtnEditA1.Text)='' then
  begin
    Application.MessageBox('码单名称不能为空!','提示',0);
    Exit;
  end;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\'+Trim(BtnEditA1.Text)+'.rmf' ;
  Panel2.Visible:=True;
  Panel2.Refresh;
  if Trim(BtnEditA1.Text)='通用外销码单(缸号列)' then
  begin
    GetLotDataLie();
  end else
  if Trim(BtnEditA1.Text)='通用外销码单(缸号38列)' then
  begin
    //GetLotDataLie38();
    GetLotDataLie38Ye();
  end else
  if Trim(BtnEditA1.Text)='通用外销码单(缸号38列M)' then
  begin
    //GetLotDataLie38();
    GetLotDataLie38Ye();
  end else
  begin
    GetLotData();
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.add('select AAA.*,JSM.OrderNo,JSB.PRTColorEng,JSB.PRTHX,JSB.PRTColorNo from(');
    sql.Add('select Sum(MJLen) MQty,Sum(YQty) YQty,Count(*) PS,Sum(MJQty4) MJJingZ,Sum(MJMaoZ) MJMaoZ ,SubId');
    SQL.Add('from (select A.MJLen,A.MJQty4,A.MJMaoZ,A.SubId,');
    sql.Add(' Case when isnull(A.MJLenMa,-1)<=0 then Cast(A.MJLen*1.0936 as decimal(18,1)) else Cast(A.MJLenMa*1 as decimal(18,1)) end as YQty ');
    //sql.Add('GangNo=(select GangNo from JYOrder_Main_MD B where B.MDID=A.APID),A.Subid');
    sql.Add(' from WFB_MJJY A');
    sql.Add(' inner join JYOrder_Main DD on A.MainId=DD.MainId');
    sql.Add(' where A.MainId='''+Trim(CDS_Main.fieldbyname('MainId').AsString)+'''');
    sql.Add(' and A.ZhuangXiangSYFlag=0');
    if Trim(MJType.Text)<>'' then
    begin
      SQL.Add(' and A.MJType='''+Trim(MJType.Text)+'''  )AA ');
    end else
    begin
      SQL.Add(' and 1=1 )AA');
    end;
    sql.Add('Group by SubId)AAA');
    sql.add(' inner join JYOrder_Sub JSB on AAA.SubId=JSB.SubID');
    sql.Add(' inner join JYOrder_Main JSM on JSB.MainId=JSM.MainId');
    sql.add(' order by SubId');
    //ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryCmd,CDS_HZ);
  SInitCDSData20(ADOQueryCmd,CDS_HZ);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select sum(MJLen) MQty,Cast(sum(MJLen)*1.0936 as decimal(18,1)) YQty, ');
    sql.Add(' sum(MJMaoZ) MaoZ,sum(MJQty4) JingZ,Count(*) PS');
    sql.Add(' from WFB_MJJY where MainId='''+Trim(CDS_Main.fieldbyname('MainId').AsString)+'''');
    sql.Add(' and ZhuangXiangSYFlag=0');
    sql.Add(' group by MainId');
    Open;
  end;
  Panel2.Visible:=False;
  if FileExists(fPrintFile) then
  begin
    RMVariables['OrderNo']:=Trim(CDS_Main.fieldbyname('OrderNo').AsString);
    RMVariables['MPRTCode']:=Trim(CDS_Main.fieldbyname('MPRTCode').AsString);
    RMVariables['MQty']:=ADOQueryTemp.fieldbyname('MQty').Value;
    RMVariables['MaoZ']:=ADOQueryTemp.fieldbyname('MaoZ').Value;
    RMVariables['JingZ']:=ADOQueryTemp.fieldbyname('JingZ').Value;
    RMVariables['PS']:=ADOQueryTemp.fieldbyname('PS').Value;
   // RMVariables['QtyUnit']:=Trim(CDS_Main.fieldbyname('QtyUnit').AsString);
    RM3.LoadFromFile(fPrintFile);
    RM3.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\'+Trim(BtnEditA1.Text)+'.rmf'),'提示',0);
  end;
end;

procedure TfrmPRTJYListGL.BtnEditA1BtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CKMD';
      flagname:='出库码单';
      if ShowModal=1 then
      begin
        BtnEditA1.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmPRTJYListGL.ToolButton2Click(Sender: TObject);
begin
  try
    frmMJEdit:=TfrmMJEdit.Create(Application);
    with frmMJEdit do
    begin
      MJID.Text:=Trim(Self.CDS_Main.fieldbyname('MJID').AsString);
      KgZMiXS:=Trim(Self.CDS_Main.fieldbyname('KgZMiXS').AsString);
      with ADOTmp do
      begin
        Close;
        sql.Clear;
        SQL.Add('select A.* 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;
      MJMaoZ.Text:=Trim(ADOTmp.fieldbyname('MJMaoZ').AsString);
      MJLen.Text:=Trim(ADOTmp.fieldbyname('MJLen').AsString);
      MJQty3.Text:=Trim(ADOTmp.fieldbyname('MJQty3').AsString);
      MJQty2.Text:=Trim(ADOTmp.fieldbyname('MJQty2').AsString);
      MJQty4.Text:=Trim(ADOTmp.fieldbyname('MJQty4').AsString);
      MJFK.Text:=Trim(ADOTmp.fieldbyname('MJFK').AsString);
      MJSJKZ.Text:=Trim(ADOTmp.fieldbyname('MJSJKZ').AsString);
      //M.Caption:=Trim(Trim(ADOTmp.fieldbyname('MJTypeOther').AsString));
     // Ma.Caption:=Trim(Trim(ADOTmp.fieldbyname('MJStr1').AsString));

      if Trim(Trim(ADOTmp.fieldbyname('MJType').AsString))='正品' then
      begin
        RadioGroup1.ItemIndex:=0
      end else
      if Trim(Trim(ADOTmp.fieldbyname('MJType').AsString))='次品' then
      begin
        RadioGroup1.ItemIndex:=1;
      end else
      if Trim(Trim(ADOTmp.fieldbyname('MJType').AsString))='多拼' then
      begin
        RadioGroup1.ItemIndex:=2;
      end;
      with ADOTmp do
      begin
        Close;
        sql.Clear;
        SQL.Add('select * from WFB_MJJY_CD A   where A.MJID='''+Trim(MJID.Text)+'''');
        Open;
      end;
      Label2.Caption:=Trim(MJID.Text);
      Label2.Visible:=True;
      MJID.Text:='';
      SCreateCDS20(ADOTmp,Order_MJ);
      SInitCDSData20(ADOTmp,Order_MJ);
      if ShowModal=1 then
      begin

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

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

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

procedure TfrmPRTJYListGL.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:=CDS_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString;
end;

procedure TfrmPRTJYListGL.Tv1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FLeft:=X;
  FTop:=Y;
end;
function TfrmPRTJYListGL.SaveConData(FConNO:string):Boolean;
var
  maxno,maxsubno,MainID,SubId,PSName:string;
begin
  Result:=False;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Main where ConNo='''+Trim(FConNO)+'''');
    sql.Add(' and ConType=''检验包装'' and ConTypeOther=''装箱剩余'' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    MainID:=Trim(ADOQueryTemp.fieldbyname('MainID').AsString);
  end else
  begin
    MainID:='';
  end;
  if Trim(MainID)='' then
  begin
    if GetLSNo(ADOQueryCmd,maxno,'ZM','Contract_Main',3,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Result:=False;
      Application.MessageBox('取加工采购合同流水号失败!!','提示',0);
      Exit;
    end;
  end else
  begin
    maxno:=Trim(MainID);
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Main where MainId='''+Trim(MainID)+'''');
    Open;
  end;
  with ADOQueryCmd do
  begin
    if Trim(MainID)='' then
    begin
      Append;
      FieldByName('Filler').Value:=Trim(DName);
    end else
    begin
      Edit;
      FieldByName('Editer').Value:=Trim(DName);
      FieldByName('EditTIme').Value:=SGetServerDateTime(ADOQueryTemp);
    end;
    FieldByName('MainID').Value:=Trim(maxno);
    FieldByName('ConNo').Value:=Trim(FConNO);
    FieldByName('FactoryNoName').Value:='长阳仓库';
    FieldByName('FactoryNo').Value:='GS131230001';
    FieldByName('ConTypeOther').Value:='装箱剩余';
    FieldByName('ConType').Value:='检验包装';
    Post;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Sub where MainId='''+Trim(maxno)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    SubId:='';
  end else
  begin
    SubId:=Trim(ADOQueryTemp.fieldbyname('SubId').AsString);
  end;
  if Trim(SubId)='' then
  begin
    if GetLSNo(ADOQueryCmd,maxsubno,'ZS','Contract_Sub',3,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Result:=False;
      Application.MessageBox('取加工采购合同子流水号失败!!','提示',0);
      Exit;
    end;
  end else
  begin
    maxsubno:=Trim(SubId);
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Sub where SubId='''+Trim(SubId)+'''');
    Open;
  end;
  with ADOQueryCmd do
  begin
    if Trim(SubId)='' then
    begin
      Append;
    end else
    begin
      Edit;
    end;
    FieldByName('MainId').Value:=Trim(maxno);
    FieldByName('SubId').Value:=Trim(maxsubno);
    FieldByName('C_Code').Value:=CDS_Main.fieldbyname('MPRTCode').Value;
    FieldByName('C_CodeName').Value:=CDS_Main.fieldbyname('MPRTCodeName').Value;
    Post;
  end;
  Result:=True;

end;
procedure TfrmPRTJYListGL.ToolButton3Click(Sender: TObject);
var
  FPSID,FFMainId,FFOrdeNo:String;
begin
  if CDS_Main.IsEmpty then Exit;
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  CDS_Main.Locate('SSel',True,[]);
  FFMainId:=Trim(CDS_Main.fieldbyname('MainId').AsString);
  FFOrdeNo:=Trim(CDS_Main.fieldbyname('OrderNo').AsString);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Process where MainId='''+Trim(FFMainId)+'''');
    sql.Add(' and PSName=''检验包装'' ');
    Open;
  end;
  FPSID:=Trim(ADOQueryTemp.fieldbyname('PSID').AsString);
  if Trim(FPSID)='' then
  begin
    Application.MessageBox('生产流程没有定义<检验包装>','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;

  BegDate.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while Locate('SSel',True,[])=True do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate WFB_MJJY Set ZhuangXiangSYFlag=1 where MJID='''+Trim(CDS_Main.fieldbyname('MJID').AsString)+'''');
          ExecSQL;
        end;
        Edit;
        FieldByName('SSel').Value:=False;
        FieldByName('ZhuangXiangSYFlag').Value:=1;
        Post;
      end;
    end;
    CDS_Main.EnableControls;
    if SaveConData(Trim(FFOrdeNo))=False then
    begin
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' insert into JYOrder_PCS_Sub(MainId,SubId,PSID,PFID,FactoryNo,FactoryName,Filler,FillTime)');
      sql.Add('select MainId,SubId,'''+FPSID+'''');
      sql.Add(',''FP''+RTrim(SubId),''GS131230001'',''长阳仓库'','''+Trim(DName)+''',getdate()');
      SQL.Add(' from WFB_MJJY where Mainid='''+Trim(FFMainId)+'''');
      sql.Add(' and ZhuangXiangSYFlag=1 ');
      sql.Add(' and not exists(select * from JYOrder_PCS_Sub A where A.SubId=WFB_MJJY.SubId and A.PSID='''+Trim(FPSID)+''')');
      sql.Add(' group by MainId,SubId');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_PCS_Sub Set');
      sql.Add('PFTPPS=(select isnull(Count(*),0) from WFB_MJJY A where A.SubId=JYOrder_PCS_Sub.SubId and A.ZhuangXiangSYFlag=1)');
      sql.Add(',PFTPQty=(select isnull(sum(MJQty4),0) from WFB_MJJY A where A.SubId=JYOrder_PCS_Sub.SubId and A.ZhuangXiangSYFlag=1)');
      sql.Add(',PFTPUnit=''KG'' ');
      sql.Add(' where PSID='''+Trim(FPSID)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('操作成功!','提示',0);
    Exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!','提示',0);
  end;
end;

procedure TfrmPRTJYListGL.ToolButton4Click(Sender: TObject);
var
  FPSID,FFMainId,FFOrdeNo:String;
begin
  if CDS_Main.IsEmpty then Exit;
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  CDS_Main.Locate('SSel',True,[]);
  FFMainId:=Trim(CDS_Main.fieldbyname('MainId').AsString);
  FFOrdeNo:=Trim(CDS_Main.fieldbyname('OrderNo').AsString);
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;

  BegDate.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while Locate('SSel',True,[])=True do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate WFB_MJJY Set ZhuangXiangSYFlag=0 where MJID='''+Trim(CDS_Main.fieldbyname('MJID').AsString)+'''');
          ExecSQL;
        end;
        Edit;
        FieldByName('SSel').Value:=False;
        FieldByName('ZhuangXiangSYFlag').Value:=0;
        Post;
      end;
    end;
    CDS_Main.EnableControls;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_PCS_Sub Set');
      sql.Add('PFTPPS=(select Count(*) from WFB_MJJY A where A.SubId=JYOrder_PCS_Sub.SubId and A.ZhuangXiangSYFlag=1)');
      sql.Add(',PFTPQty=(select sum(MJQty4) from WFB_MJJY A where A.SubId=JYOrder_PCS_Sub.SubId and A.ZhuangXiangSYFlag=1)');
      sql.Add(',PFTPUnit=''KG'' ');
      sql.Add(' where PSID='''+Trim(FPSID)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('操作成功!','提示',0);
    Exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!','提示',0);
  end;
end;

procedure TfrmPRTJYListGL.ToolButton5Click(Sender: TObject);
var
   fPrintFile:String;
begin
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据！','提示',0);
    exit;
  end;
  with CDS_Main do
  begin
    while Locate('SSel',True,[])=True do
    begin
      with ADOQueryPrint do
      begin
        Close;
        SQL.Clear;
        sql.Add('exec P_Print_Lable :MJID');
        Parameters.ParamByName('MJID').Value:=Trim(CDS_Main.fieldbyname('MJID').AsString);
        Open;
      end;
      fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\匹条标签.rmf' ;
      if FileExists(fPrintFile) then
      begin
        RM1.LoadFromFile(fPrintFile);
        //RM1.ShowReport;
        RM1.PrintReport;
      end else
      begin
        Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\匹条标签'),'提示',0);
      end;
      CDS_Main.Edit;
      CDS_Main.FieldByName('SSel').Value:=False;
      CDS_Main.Post;
    end;
  end;
end;

procedure TfrmPRTJYListGL.ToolButton6Click(Sender: TObject);
var
  fPrintFile:string;
begin
  GetBGData();
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\检验报告.rmf' ;
  if FileExists(fPrintFile) then
  begin
    RMCD.LoadFromFile(fPrintFile);
    RMVariables['OrderNo']:=Trim(CDS_Main.fieldbyname('OrderNo').AsString);
    RMVariables['CustomerNoName']:=Trim(CDS_Main.fieldbyname('CustomerNoName').AsString);
    RMVariables['MPRTMF']:=Trim(CDS_Main.fieldbyname('MPRTMF').AsString);
    RMVariables['MPRTKZ']:=Trim(CDS_Main.fieldbyname('MPRTKZ').AsString);
    RMVariables['MPRTCode']:=Trim(CDS_Main.fieldbyname('MPRTCode').AsString);
   // RMVariables['FillTIme']:=Trim(CDS_Main.fieldbyname('FillTIme').AsString);
    RMCD.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\检验报告.rmf'),'提示',0);
  end;
end;
procedure TfrmPRTJYListGL.GetCDSData();
begin
  CDS_CD.FieldByName('MJXH').Value:=CDS_LS.fieldbyname('MJXH').Value;
  CDS_CD.FieldByName('SelfGangNo').Value:=CDS_LS.fieldbyname('SelfGangNo').Value;
  CDS_CD.FieldByName('PRTColor').Value:=CDS_LS.fieldbyname('PRTColor').Value;
  CDS_CD.FieldByName('PRTColorNo').Value:=CDS_LS.fieldbyname('PRTColorNo').Value;
  CDS_CD.FieldByName('MJLen').Value:=CDS_LS.fieldbyname('MJLen').Value;
  CDS_CD.FieldByName('MJLenMa').Value:=CDS_LS.fieldbyname('MJLenMa').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('KouFenHZ').Value:=CDS_LS.fieldbyname('KouFenHZ').Value;
  CDS_CD.FieldByName('KouFenAvg').Value:=CDS_LS.fieldbyname('KouFenAvg').Value;
  CDS_CD.FieldByName('MJID').Value:=CDS_LS.fieldbyname('MJID').Value;
  CDS_CD.FieldByName('FillTime').Value:=CDS_LS.fieldbyname('FillTime').Value;
end;
procedure TfrmPRTJYListGL.GetBGData();
var
  FMJId:string;
  i,j,z:Integer;
begin

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select MJXH=Cast(0 as int),SelfGangNo=Cast('''' as varchar(20)),PRTColorNo=Cast('''' as varchar(20))');
    sql.Add(',MJID=Cast('''' as varchar(20)),PRTColor=Cast('''' as varchar(20)),FillTime=Cast(Null as datetime)');
    sql.Add(',MJLen=Cast(0 as decimal(18,1)),MJLenMa=Cast(0 as decimal(18,1)),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)),KouFenHZ=Cast(0 as int),KouFenAvg=Cast(0 as decimal(18,2))');
    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)');
    sql.Add(',CDName10=Cast('''' as varchar(20)),CDBeg10=Cast('''' as varchar(20)),KFQty10=Cast(0 as int)');
    sql.Add(',CDName11=Cast('''' as varchar(20)),CDBeg11=Cast('''' as varchar(20)),KFQty11=Cast(0 as int)');
    sql.Add(',CDName12=Cast('''' as varchar(20)),CDBeg12=Cast('''' as varchar(20)),KFQty12=Cast(0 as int)');
    sql.Add(',CDName13=Cast('''' as varchar(20)),CDBeg13=Cast('''' as varchar(20)),KFQty13=Cast(0 as int)');
    sql.Add(',CDName14=Cast('''' as varchar(20)),CDBeg14=Cast('''' as varchar(20)),KFQty14=Cast(0 as int)');
    sql.Add(',CDName15=Cast('''' as varchar(20)),CDBeg15=Cast('''' as varchar(20)),KFQty15=Cast(0 as int)');
    sql.Add(',CDName16=Cast('''' as varchar(20)),CDBeg16=Cast('''' as varchar(20)),KFQty16=Cast(0 as int)');
    sql.Add(',CDName17=Cast('''' as varchar(20)),CDBeg17=Cast('''' as varchar(20)),KFQty17=Cast(0 as int)');
    sql.Add(',CDName18=Cast('''' as varchar(20)),CDBeg18=Cast('''' as varchar(20)),KFQty18=Cast(0 as int)');
    sql.Add(',CDName19=Cast('''' as varchar(20)),CDBeg19=Cast('''' as varchar(20)),KFQty19=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 AA.*, ');
    SQL.Add(' case when CDBeg=CDEnd then Cast(CDBeg as varchar(20)) else Cast(CDBeg as varchar(20))+''-''+Cast(CDEnd as varchar(20)) end as CDQK');
    sql.Add('from (select A.CDBeg,A.CDEnd,A.CDName,A.KouFen,B.MJXH,C.SelfGangNo,JS.PRTColorNo,JS.PRTColor,B.MJID, ');
    sql.Add(' Case when B.MJLen>0 then B.KouFenHZ*1.00*100/(B.MJLen*B.MJFK) else Null end as KouFenAvg');
    sql.Add(',CDEName=(select Note from KH_Zdy KZ where KZ.ZdyName=A.CDName and KZ.Type=''WFBCD'' )');
    sql.Add(',B.MJLen,B.MJLenMa,B.MJQty4,B.MJMaoZ,B.MJSJKZ,B.MJFK,B.MJType,B.KouFenHZ,B.FillTime');
    sql.Add(' from WFB_MJJY B ');
    sql.Add(' left join WFB_MJJY_CD A on B.MJID=A.MJID');
    sql.Add(' inner join JYOrder_Main_MD C on B.APID=C.MDID');
    sql.Add(' inner join JYOrder_Sub JS on C.ORDSubId=JS.SubId');
    SQL.Add(' where B.MainId='''+Trim(CDS_Main.fieldbyname('MainId').AsString)+'''');
    if Trim(MJType.Text)<>'' then
    begin
      sql.Add(' and isnull(B.MJType,'''')='''+Trim(MJType.Text)+'''');
    end;

    sql.Add(')AA order by PRTColorNo,Cast(SelfGangNo as int),MJXH');
    Open;
  end;
  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('CDQK').Value;
          FieldByName('KFQty0').Value:=CDS_LS.fieldbyname('KouFen').Value;
          FieldByName('XH').Value:=CDS_LS.fieldbyname('MJXH').Value;
          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<20 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('CDQK').Value;
              FieldByName('KFQty'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('KouFen').Value;
              FieldByName('XH').Value:=CDS_LS.fieldbyname('MJXH').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('CDQK').Value;
              FieldByName('KFQty'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('KouFen').Value;
              FieldByName('XH').Value:=CDS_LS.fieldbyname('MJXH').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('CDQK').Value;
            FieldByName('KFQty'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('KouFen').Value;
            FieldByName('XH').Value:=CDS_LS.fieldbyname('MJXH').Value;
            Post;
          end;
          FMJId:=Trim(CDS_LS.fieldbyname('MJID').AsString);
        end;
      end;
      Next;
    end;
  end;
  CDS_LS.EnableControls;
end;

end.
