Printing SalesOrderAcknowledgement via ButtonClick

The code below gets the printer settings from a BAQ I wrote that links user names to printers in the system and then submits the BAQ report to the System Agent.

private void PrintBillsSSRS(){
		if (numBOLNum.Value == null || Convert.ToInt32(numBOLNum.Value) <= 0) { return; }
		// Make sure the boxes and weight are up-to-date before printing
		UpdateBOLBoxesAndWeight(Convert.ToInt32(numBOLNum.Value));
	    // ** Place Event Handling Code Here **
		string[] printerInfo = GetDefaultPrinter(((Session)HHConfirmShipForm.Session).UserID);
	    string printer = printerInfo[0];
		string pageSettings = printerInfo[1];
		string printerSettings = printerInfo[2];
		//MessageBox.Show("Printer: " + printer);
	    string agentID = "";
	
	    try
	    {
	        Session otSession = (Session)oTrans.Session;
	
	        //Set the workstationID
	        string workStation = Ice.Lib.Report.EpiReportFunctions.GetWorkStationID(otSession);
	
	        //Get or Set the AgentID
	        using (var aSA = new SysAgentAdapter(oTrans))
	        {
	            //oTrans.PushStatusText("Get System Agent Info", true);
	            aSA.BOConnect();
	            aSA.GetDefaultTaskAgentID(out agentID);
	            if (!string.IsNullOrEmpty(agentID)) { agentID = "SystemTaskAgent"; }
	        }
	
	        //MessageBox.Show(agentID + ", " + workStation);
			for (int i =0; i < 3; i++)
	        {
	            var baqR = WCFServiceSupport.CreateImpl<Ice.Proxy.Rpt.BAQReportImpl>((Session)oTrans.Session, Epicor.ServiceModel.Channels.ImplBase<Ice.Contracts.BAQReportSvcContract>.UriPath);
	            var dynamicReport = WCFServiceSupport.CreateImpl<Ice.Proxy.BO.DynamicReportImpl>((Session)oTrans.Session, Epicor.ServiceModel.Channels.ImplBase<Ice.Contracts.DynamicReportSvcContract>.UriPath);
	            var rptMonitor = WCFServiceSupport.CreateImpl<Ice.Proxy.BO.ReportMonitorImpl>((Session)oTrans.Session, Epicor.ServiceModel.Channels.ImplBase<Ice.Contracts.ReportMonitorSvcContract>.UriPath);
	
	            // GET DEFAULT REPORT PARAMETERS
	            var rptDs = dynamicReport.GetByID("PSG-tmsCustBOL");
	            var baqRptDS = baqR.GetNewBAQReportParam("PSG-tmsCustBOL");
	            baqRptDS.BAQReportParam[0].Option01 = numBOLNum.Value.ToString();
	            baqRptDS.BAQReportParam[0].AutoAction = "SSRSPRINT";
	            baqRptDS.BAQReportParam[0].WorkstationID = workStation;
				baqRptDS.BAQReportParam[0].PrinterName = printer;
				baqRptDS.BAQReportParam[0].RptPageSettings = pageSettings;
				baqRptDS.BAQReportParam[0].RptPrinterSettings = printerSettings;
	            baqRptDS.BAQReportParam[0].SSRSRenderFormat = "PDF";
	            //baqRptDS.BAQReportParam[0].Character01 = numBOLNum.Value.ToString();
	            //baqRptDS.BAQReportParam[0].Character02=flag;
	            baqRptDS.BAQReportParam[0].BAQRptID = "PSG-tmsCustBOL";
	            baqRptDS.BAQReportParam[0].ReportID = "PSG-tmsCustBOL";
	            baqRptDS.BAQReportParam[0].Summary = false;
	            baqRptDS.BAQReportParam[0].ReportStyleNum = 1;
	            baqRptDS.BAQReportParam[0].BAQID = "PSG-tmsCustomerBOL";
	            baqRptDS.BAQReportParam[0].ReportTitle = "Bill of Lading";
	            //baqRptDS.BAQReportParam[0].TaskNote = guid.ToString();
	            rptDs.BAQRptOptionFld[0].FieldValue = numBOLNum.Value.ToString();
	
	            rptDs.AcceptChanges();
	            StringWriter writer = new StringWriter();
	            rptDs.WriteXml(writer);
	            baqRptDS.BAQReportParam[0].Filter1 = writer.ToString();
	
	            //baqR.RunDirect(baqRptDS);
	            baqR.SubmitToAgent(baqRptDS, agentID, 0, 0, "Erp.UIRpt.BAQReport");
	
	            //MessageBox.Show("Report Submitted to System Agent for processing.");
	        }
	    }
	    catch (Exception ex)
	    {
	        MessageBox.Show("An error occured trying to print report." + ex.Message);
	    }
	}
	
	// Gets the printer for the current user.
	private string[] GetDefaultPrinter(string currentUser)
	{
		string[] output = new string[3];
		string printer = string.Empty;
		string pageSet = "Color=True,Landscape=False,PaperSize=[Kind=\"Letter\" PaperName=\"Letter\" Height=1100 Width=850],PaperSource=[SourceName=\"FormSource\" Kind=\"FormSource\"],PrinterResolution=[Kind=\"Custom\" X=600 Y=600]";
		string printSet = "PrinterName=\"" + printer + "\",Copies=1,Collate=False,Duplex=Default,FromPage=1,ToPage=0";
		// Special cases for PLT02 and PLT03, because we can only add 2 printers to each workstationID
		if (currentUser.ToUpper().Equals("HH10") || currentUser.ToUpper().Equals("HH04"))
		{
			// PLT02. Default to PLT2-PR-02
			printer = "\\\\PSG-SRV-02\\PLT2-PR-02";
			printSet = "PrinterName=\"" + printer + "\",Copies=1,Collate=False,Duplex=Default,FromPage=1,ToPage=0";
			output[0] = printer;
			output[1] = pageSet;
			output[2] = printSet;
			return output;
		} else if (currentUser.ToUpper().Equals("HH08") || currentUser.ToUpper().Equals("HH05") || currentUser.ToUpper().Equals("HH06"))
		{
			// PLT03. Default to PLT3-PR-06
			printer = "\\\\PSG-SRV-02\\PLT3-PR-06";
			printSet = "PrinterName=\"" + printer + "\",Copies=1,Collate=False,Duplex=Default,FromPage=1,ToPage=0";
			output[0] = printer;
			output[1] = pageSet;
			output[2] = printSet;
			return output;
		}
		DynamicQueryAdapter dqa = new DynamicQueryAdapter(oTrans);
		dqa.BOConnect();
		QueryExecutionDataSet qeds = dqa.GetQueryExecutionParametersByID("PSG_tmsPrinterForUser");
		qeds.ExecutionParameter.Clear();
		qeds.ExecutionParameter.AddExecutionParameterRow("UserID", currentUser, "nvarchar",false,Guid.NewGuid(), "A");
		dqa.ExecuteByID("PSG_tmsPrinterForUser", qeds);
		foreach (DataRow row in dqa.QueryResults.Tables["Results"].Rows)
		{
			// Only use printers that aren't label printers (do not contain "LBL")
			if (!row["SysPrinter_PrinterID"].ToString().ToUpper().Contains("LBL"))
			{
				printer = row["SysPrinter_NetworkPath"].ToString();
				pageSet = row["Calculated_PageSettings"].ToString();
				printSet = row["Calculated_PrinterSettings"].ToString();
			}
		}
		output[0] = printer;
		output[1] = pageSet;
		output[2] = printSet;
		return output;
	}
4 Likes