unit U_Printckmd;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxMemo, cxRichEdit, ComCtrls, cxContainer,
  cxTextEdit, cxMaskEdit, cxButtonEdit, StdCtrls, ToolWin, DBClient, ADODB,
  ExtCtrls, BtnEdit, cxCalendar,StrUtils, RM_Common, RM_Class, RM_e_Xls,
  RM_GridReport, RM_System, RM_Dataset;

type
  TfrmPrintckmd = class(TForm)
    Button1: TButton;
    Button2: TButton;
    RadioGroup1: TRadioGroup;
    RMDBMain: TRMDBDataSet;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    ADOQueryTemp: TADOQuery;
    CDS_Main: TClientDataSet;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    procedure doPrint(Print1:integer);

    Procedure createcds(fADO:TADOQuery;fCDS:TclientDataSet);
    { Private declarations }
  public
    fckNo:string;
    flag:integer;
    { Public declarations }
  end;

var
  frmPrintckmd: TfrmPrintckmd;

implementation
 uses
  U_DataLink,U_Fun;
{$R *.dfm}

Procedure TfrmPrintckmd.createcds(fADO:TADOQuery;fCDS:TclientDataSet);
var
  i,j:integer;
  fcolor:string;
begin
  fCDS.EmptyDataSet;


  fADO.First;
  i:=1;
  j:=0;
  fcolor:=trim(fADO.fieldbyname('SOrddefstr4').AsString);
  fCDS.Append;
  fCDS.fieldbyname('MJXHlist').Value:=trim(fADO.fieldbyname('MJXH').AsString);
  fCDS.fieldbyname('QtyList').Value:=trim(fADO.fieldbyname('Qty').AsString);
  fCDS.fieldbyname('SOrddefstr4').Value:=trim(fADO.fieldbyname('SOrddefstr4').AsString);

  fCDS.fieldbyname('kgQty').Value:=fADO.fieldbyname('kgQty').AsFloat;
  fCDS.fieldbyname('Qty').Value:=fADO.fieldbyname('Qty').AsFloat;
  fCDS.Post;
  fADO.Next;
  while not fADO.Eof do
  begin
    if (trim(fADO.fieldbyname('SOrddefstr4').AsString)= fcolor ) and (i<10) then
    begin
      fCDS.Edit;
      fCDS.fieldbyname('kgQty').Value:= fCDS.fieldbyname('kgQty').AsFloat+fADO.fieldbyname('kgQty').AsFloat;
      fCDS.fieldbyname('Qty').Value:= fCDS.fieldbyname('Qty').AsFloat+fADO.fieldbyname('Qty').AsFloat;
      if pos('-',trim(fCDS.fieldbyname('MJXHlist').AsString))>0 then
      fCDS.fieldbyname('MJXHlist').Value:=trim(LeftBStr(trim(fCDS.fieldbyname('MJXHlist').AsString),pos('-',trim(fCDS.fieldbyname('MJXHlist').AsString))-1))+
                                          '-'+trim(fADO.fieldbyname('MJXH').AsString)
      else
      fCDS.fieldbyname('MJXHlist').Value:=trim(fCDS.fieldbyname('MJXHlist').AsString)+'-'+trim(fADO.fieldbyname('MJXH').AsString);
    //  if pos('-',trim(fCDS.fieldbyname('QtyList').AsString))>0 then
      fCDS.fieldbyname('Qtylist').Value:=trim(fCDS.fieldbyname('Qtylist').AsString)+'-'+trim(fADO.fieldbyname('Qty').AsString);

      i:=i+1;
      fCDS.Post;
    end
    else
    begin
      fCDS.Append;
      fCDS.fieldbyname('MJXHlist').Value:=trim(fADO.fieldbyname('MJXH').AsString);
      fCDS.fieldbyname('QtyList').Value:=trim(fADO.fieldbyname('Qty').AsString);
      fCDS.fieldbyname('SOrddefstr4').Value:=trim(fADO.fieldbyname('SOrddefstr4').AsString);

      fCDS.fieldbyname('kgQty').Value:=fADO.fieldbyname('kgQty').AsFloat;
      fCDS.fieldbyname('Qty').Value:=fADO.fieldbyname('Qty').AsFloat;
      fCDS.Post;
      fcolor:=trim(fADO.fieldbyname('SOrddefstr4').AsString);
      i:=1;
    end;
    fADO.Next;
  end;
end;

procedure TfrmPrintckmd.doPrint(Print1:integer);
var
  fPrintFile:String;
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select AA.*,REPLICATE('' '', 20)as MJXHList,REPLICATE('' '', 200)as QtyList from (select A.*,');
    sql.Add('GangNo=(select AOrdDefStr1 from JYOrder_Sub_AnPai B where B.APID=A.APID),');
    sql.Add('OrderNo=(select OrderNo from JYOrder_Main B where B.MainID=A.MainID),');
    sql.Add('SOrddefstr4=(select SOrddefstr4 from JYOrder_Sub B where B.SubID=A.SubID),');
    sql.Add('PRTHX=(select PRTHX from JYOrder_Sub B where B.SubID=A.subID),');
    SQL.Add('TaiTou=(select Top 1 Note from KH_Zdy B where B.ZdyName=DD.OrdDefStr2 and Type=''OrdDefStr2'' ),');
    SQL.Add('MpRTCodeNameEng=(select Top 1 Note from KH_Zdy B where B.ZdyName=DD.MpRTCodeName  ),');
    sql.Add('MJXH=(select MJXH from WFB_MJJY B where B.MJID=A.MJID)');
    sql.Add(' from CK_BanCP_CR A');
    sql.Add(' inner join JYOrder_Main DD on A.MainId=DD.MainId');
    if flag=1 then
    begin
      sql.Add(' where A.MainId='''+Trim(fckNo)+'''');
      sql.Add(' and CRflag=''入库'' ');
    end
    else
    begin
      sql.Add(' where A.ckordNo='''+Trim(fckNo)+'''');
      sql.Add(' and CRflag=''出库'' ');
    end;
 {   if Trim(CDS_Main.FieldByName('CKOrdNo').AsString)<>'' then
    begin
      sql.Add(' and A.CKOrdNo='''+Trim(CDS_Main.FieldByName('CKOrdNo').AsString)+'''');
    end;      }
//    sql.Add(' and CRType=''正常出库'' ');
    sql.Add(' )AA order by SOrddefstr4,GangNo,MJXH ');
    Open;
  end;

  SCreateCDS20(ADOQueryTemp,CDS_Main);
  SInitCDSData20(ADOQueryTemp,CDS_Main);

  if Print1=1 then
  begin
    createcds(ADOQueryTemp,CDS_Main);
  end;
  
  if  Print1=0 then
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\出库码单.rmf'
  else
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\出库码单2.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\出库码单.rmf'),'提示',0);
  end;
end;
procedure TfrmPrintckmd.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=cahide;
end;

procedure TfrmPrintckmd.FormDestroy(Sender: TObject);
begin
  close;
end;

procedure TfrmPrintckmd.Button1Click(Sender: TObject);
begin
  doPrint(RadioGroup1.ItemIndex);
end;

procedure TfrmPrintckmd.Button2Click(Sender: TObject);
begin
  ModalResult:=-1;
end;

end.
