unit U_JYMJIDPRINT;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, RM_Common, RM_Class, RM_e_Xls, RM_GridReport, RM_System,
  RM_Dataset, DBClient, DB, ADODB, StdCtrls, ExtCtrls, RM_E_llPDF;

type
  TfrmJYMJIDPRINT = class(TForm)
    ADOQueryTmp: TADOQuery;
    CDS_Main: TClientDataSet;
    CDS_T: TClientDataSet;
    CDS_Z: TClientDataSet;
    CDS_E: TClientDataSet;
    RMDB_Main: TRMDBDataSet;
    RMDB_T: TRMDBDataSet;
    RMDB_Z: TRMDBDataSet;
    RMDB_E: TRMDBDataSet;
    RMGridReport1: TRMGridReport;
    Button1: TButton;
    RadioGroup1: TRadioGroup;
    Button2: TButton;
//    RMllPDFExport1: TRMllPDFExport;
    Button3: TButton;
    Button4: TButton;
    CDSHZ: TClientDataSet;
    RMDBHZ: TRMDBDataSet;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    Procedure Print1(flag1:integer);
    Procedure Print2(flag1:integer);
    { Private declarations }
  public
    fMJID:string;
    Fflag:integer;
    { Public declarations }
  end;

var
  frmJYMJIDPRINT: TfrmJYMJIDPRINT;

implementation
 uses
  U_DataLink,U_Fun,U_JYOrderCDOne, U_CKProductBCPOutList;
{$R *.dfm}
Procedure TfrmJYMJIDPRINT.Print2(flag1:integer);
var
  i,j:integer;
   fstr1,fPrintFile:string;
   Txt,fImagePath:string;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin
  with ADOqueryTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName='''+Trim(DCode)+'''');
    ExecSQL;
  end;
  with ADOqueryTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * FROM TBSubID where 1=2 ');
    open;
  end;
  ADOqueryTmp.Connection.BeginTrans;
  try
    IF fflag=1 then
    begin
      frmCKProductBCPOutList.CDS_Main.DisableControls;
      with frmCKProductBCPOutList.CDS_Main do
      begin
        First;
        while not Eof do
        begin
          if FieldByName('SSel').AsBoolean then
          begin
            ADOqueryTmp.append;
            ADOqueryTmp.fieldbyname('SubId').Value:=Trim(fieldbyname('MJID').AsString);
            ADOqueryTmp.fieldbyname('Dname').Value:=Trim(DCode);
            ADOqueryTmp.post;
          end;
        Next;
        end;
      end;
      frmCKProductBCPOutList.CDS_Main.EnableControls;
    end
    else
    begin
      frmJYOrderCDOne.Order_Main.DisableControls;
      with frmJYOrderCDOne.Order_Main do
      begin
        First;
        while not Eof do
        begin
          if FieldByName('SSel').AsBoolean then
          begin
            ADOqueryTmp.append;
            ADOqueryTmp.fieldbyname('SubId').Value:=Trim(fieldbyname('MJID').AsString);
            ADOqueryTmp.fieldbyname('Dname').Value:=Trim(DCode);
            ADOqueryTmp.post;
          end;
        Next;
        end;
      end;
      frmJYOrderCDOne.Order_Main.EnableControls;
    end;
    ADOqueryTmp.Connection.CommitTrans;
  except
    ADOqueryTmp.Connection.RollbackTrans;
  end;



  with ADOqueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add(' exec P_Do_Print_JYBB ');
    sql.Add(' @mainID='+quotedstr(''));
    sql.Add(',@DName='+quotedstr(trim(DCode)));
    IF RadioGroup1.ItemIndex=4  then
    sql.Add(',@Flag='+quotedstr('0'))
    else
    sql.Add(',@Flag='+quotedstr('1'));
    open;
  end;
  SCreateCDS20(ADOqueryTmp,CDSHZ);
  SInitCDSData20(ADOqueryTmp,CDSHZ);


  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\'+trim(RadioGroup1.Items.Strings[RadioGroup1.ItemIndex])+'.rmf';
  if NOT FileExists(fPrintFile) then
  begin
    Application.MessageBox(PChar('没有找'+trim(fPrintFile)),'提示',MB_ICONERROR);
    exit;
  end;
  RMGridReport1.LoadFromFile(fPrintFile);
  if flag1=0 then
  RMGridReport1.ShowReport
  else If flag1=1 then
  begin
    RMGridReport1.PrintReport;
  end;


end;

Procedure TfrmJYMJIDPRINT.Print1(flag1:integer);
var
  i,j:integer;
   fstr1,fPrintFile:string;
   Txt,fImagePath:string;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin

  with ADOqueryTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName='''+Trim(DCode)+'''');
    ExecSQL;
  end;
  with ADOqueryTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * FROM TBSubID where DName='''+Trim(DCode)+'''');
    open;
    append;
    fieldbyname('SubId').Value:=Trim(fMJID);
    fieldbyname('Dname').Value:=Trim(DCode);
    post;
  end;


  with ADOqueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add(' exec P_Get_JYMJID_Main ');
    sql.Add(' @MJID='+quotedstr(fMJID));
    if radiogroup1.ItemIndex>1 then
    sql.Add(',@flag=''1'' ')
    else
    sql.Add(',@flag=''0'' ');
    sql.Add(',@userID='+quotedstr(trim(DCode)));
    open;
  end;
  SCreateCDS20(ADOqueryTmp,CDS_Main);
  SInitCDSData20(ADOqueryTmp,CDS_Main);

  with ADOqueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add(' exec P_Get_JYMJID ');
    sql.Add(' @MJID='+quotedstr(fMJID));
    sql.Add(',@Flag='+quotedstr('0'));
    open;
  end;
  SCreateCDS20(ADOqueryTmp,CDS_T);
  SCreateCDS20(ADOqueryTmp,CDS_Z);
  SCreateCDS20(ADOqueryTmp,CDS_E);
//  SInitCDSData20(ADOQueryMain,Order_Main);


//赋值布头信息
  with ADOqueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add(' exec P_Get_JYMJID ');
    sql.Add(' @MJID='+quotedstr(fMJID));
    if (radiogroup1.ItemIndex=1) or (radiogroup1.ItemIndex=3) then
    sql.Add(',@Flag='+quotedstr('2'))
    else
    sql.Add(',@Flag='+quotedstr('1'));
    open;
  end;
  with CDS_T do
  begin
    append;
    fieldbyname('N').Value:=1;
    post;
    while not ADOqueryTmp.eof do
    begin
      edit;
      fieldbyname('H'+inttostr(ADOqueryTmp.fieldbyname('keyNo').AsInteger)).Value:=ADOqueryTmp.fieldbyname('cdName').asstring;
      post;
      ADOqueryTmp.Next;
      i:=i+1;
    end;
  end;


  //赋值中间次点信息
  with ADOqueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add(' exec P_Get_JYMJID ');
    sql.Add(' @MJID='+quotedstr(fMJID));
    sql.Add(',@Flag='+quotedstr('3'));
    open;
  end;
  i:=1;
  j:=1;
  with CDS_Z do
  begin
    for j:=1 to 150 do
    begin
      append;
      fieldbyname('N').Value:=j;
      post;
    end;
  end;

  with CDS_Z do
  begin
    while not ADOqueryTmp.Eof do
    begin
      IF Locate('N',ADOqueryTmp.fieldbyname('H').AsInteger,[]) then
      begin
        Edit;
        for i:=2 to 15 do
        begin
          Fields[i].Value:='----';
        end;
        fieldbyname('N1').Value:=ADOqueryTmp.fieldbyname('CDBeg').AsString;

        IF ADOqueryTmp.FieldByName('KouFenQty').AsFloat>0 then
        begin
            fieldbyname('H'+inttostr(ADOqueryTmp.fieldbyname('keyNo').AsInteger)).Value:=inttostr(ADOqueryTmp.fieldbyname('KouFenQty').AsInteger)+'p';
        end;
        IF ADOqueryTmp.FieldByName('CDName').AsString='测幅宽' then
        begin
            fieldbyname('E1').Value:='----';
            fieldbyname('E2').Value:='----';
            fieldbyname('E3').Value:=ADOqueryTmp.fieldbyname('CDQty').AsString;
        end;
        post;

        IF (ADOqueryTmp.FieldByName('koufenType').AsString='1米内连续出现瑕疵')
            and  (ADOqueryTmp.FieldByName('H1').AsInteger>ADOqueryTmp.FieldByName('H').AsInteger ) then
        begin
          for i:=ADOqueryTmp.FieldByName('H').AsInteger to ADOqueryTmp.FieldByName('H1').AsInteger do
          begin
            IF Locate('N',i,[]) then
            begin
              Edit;
              fieldbyname('H'+inttostr(ADOqueryTmp.fieldbyname('keyNo').AsInteger)).Value:='=====';
              post;
            end;
          end;
        end;
      end;
      ADOqueryTmp.Next;
    end;
  end;

  //赋值总结信息

  with ADOqueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add(' exec P_Get_JYMJID ');
    sql.Add(' @MJID='+quotedstr(fMJID));
    sql.Add(',@Flag='+quotedstr('4'));
    open;
  end;
  i:=1;
  with CDS_E do
  begin
    append;
    fieldbyname('N').Value:=1;
    post;
    while not ADOqueryTmp.eof do
    begin
      edit;
      fieldbyname('H'+inttostr(ADOqueryTmp.fieldbyname('keyNo').AsInteger)).Value:=ADOqueryTmp.fieldbyname('num').asstring;
      post;
      ADOqueryTmp.Next;
      i:=i+1;
    end;
  end;
  if (radiogroup1.ItemIndex=0) or (radiogroup1.ItemIndex=2) then
  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\检验单卷信息.rmf';
  if (radiogroup1.ItemIndex=1) or (radiogroup1.ItemIndex=3) then
  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\检验单卷信息(英文).rmf';
  if NOT FileExists(fPrintFile) then
  begin
    Application.MessageBox(PChar('没有找'+trim(fPrintFile)),'提示',MB_ICONERROR);
    exit;
  end;
  RMGridReport1.LoadFromFile(fPrintFile);
  if flag1=0 then
  RMGridReport1.ShowReport;
  If flag1=1 then
  begin
    if not DirectoryExists(pchar('D:\pdf文档')) then
    CreateDirectory(pchar('D:\pdf文档'),nil);
    fstr1:='D:\pdf文档\'+trim(CDS_Main.fieldbyname('ISHzEng').AsString)+'_'+trim(CDS_Main.fieldbyname('MJID').AsString)+'_'+trim(CDS_Main.fieldbyname('MJXH').AsString)+'.pdf';
    RMGridReport1.ShowPrintDialog:=false;
    RMGridReport1.PrepareReport;
//    RMGridReport1.ExportTo(RMllPDFExport1,fstr1);
  end
  else If flag1=2 then
  begin
    RMGridReport1.PrintReport;
  end;
 // RMGridReport1.PrepareReport;
 // RMGridReport1.PrintReport10();
//  RMGridReport1.ExportTo(RMXLSExport1,'C:\111.excel');
//  RMGridReport1.ExportTo(RMllPDFExport1,'C:\111.pdf');


end;

procedure TfrmJYMJIDPRINT.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=cafree;
end;

procedure TfrmJYMJIDPRINT.FormDestroy(Sender: TObject);
begin
  frmJYMJIDPRINT:=nil;
end;

procedure TfrmJYMJIDPRINT.Button1Click(Sender: TObject);
begin
  IF RadioGroup1.ItemIndex<4 then
  Print1(0);

  IF RadioGroup1.ItemIndex>=4 then
  Print2(0);
end;

procedure TfrmJYMJIDPRINT.FormCreate(Sender: TObject);
begin
  fMJID:='1504080304';
  Fflag:=0;
end;

procedure TfrmJYMJIDPRINT.Button2Click(Sender: TObject);
begin
  close;
end;

procedure TfrmJYMJIDPRINT.Button3Click(Sender: TObject);
begin
  IF RadioGroup1.ItemIndex<4 then
  begin
  IF Fflag=1 then
  begin
    if frmCKProductBCPOutList.CDS_Main.IsEmpty then Exit;
    if frmCKProductBCPOutList.CDS_Main.Locate('SSel',True,[])=False then
    begin
      Application.MessageBox('没有选择数据!','提示',0);
      Exit;
    end;
    frmCKProductBCPOutList.CDS_Main.DisableControls;
    with frmCKProductBCPOutList.CDS_Main do
    begin
      First;
      while not Eof do
      begin
        if FieldByName('SSel').AsBoolean then
        begin
        fMJID:=frmCKProductBCPOutList.CDS_Main.fieldbyname('MJID').AsString;
        Print1(2);
        end;
        Next;
      end;
    end;
    frmCKProductBCPOutList.CDS_Main.EnableControls;
  end
  else
  begin
    if frmJYOrderCDOne.Order_Main.IsEmpty then Exit;
    if frmJYOrderCDOne.Order_Main.Locate('SSel',True,[])=False then
    begin
      Application.MessageBox('没有选择数据!','提示',0);
      Exit;
    end;
    frmJYOrderCDOne.Order_Main.DisableControls;
    with frmJYOrderCDOne.Order_Main do
    begin
      First;
      while not Eof do
      begin
        if FieldByName('SSel').AsBoolean then
        begin
          fMJID:=frmJYOrderCDOne.Order_Main.fieldbyname('MJID').AsString;
          Print1(2);
        end;
        Next;
      end;
    end;
    frmJYOrderCDOne.Order_Main.EnableControls;
  end;
  end;

  IF RadioGroup1.ItemIndex>=4 then
  Print2(1);
end;

procedure TfrmJYMJIDPRINT.Button4Click(Sender: TObject);
begin
  IF RadioGroup1.ItemIndex<4 then
  begin
  IF Fflag=1 then
  begin
    if frmCKProductBCPOutList.CDS_Main.IsEmpty then Exit;
    if frmCKProductBCPOutList.CDS_Main.Locate('SSel',True,[])=False then
    begin
      Application.MessageBox('没有选择数据!','提示',0);
      Exit;
    end;
    frmCKProductBCPOutList.CDS_Main.DisableControls;
    with frmCKProductBCPOutList.CDS_Main do
    begin
      First;
      while not Eof do
      begin
        if FieldByName('SSel').AsBoolean then
        begin
        fMJID:=frmCKProductBCPOutList.CDS_Main.fieldbyname('MJID').AsString;
        Print1(1);
        end;
        Next;
      end;
    end;
    frmCKProductBCPOutList.CDS_Main.EnableControls;
  end
  else
  begin
    if frmJYOrderCDOne.Order_Main.IsEmpty then Exit;
    if frmJYOrderCDOne.Order_Main.Locate('SSel',True,[])=False then
    begin
      Application.MessageBox('没有选择数据!','提示',0);
      Exit;
    end;
    frmJYOrderCDOne.Order_Main.DisableControls;
    with frmJYOrderCDOne.Order_Main do
    begin
      First;
      while not Eof do
      begin
        if FieldByName('SSel').AsBoolean then
        begin
          fMJID:=frmJYOrderCDOne.Order_Main.fieldbyname('MJID').AsString;
          Print1(1);
        end;
        Next;
      end;
    end;
    frmJYOrderCDOne.Order_Main.EnableControls;
  end;
  end;
end;

end.
