Save all data print in POS - Retail D365
Song Nghia - Technical consultant
This class help you save all data print in POS Retail D365.
This class help you save all data print in POS Retail D365.
namespace LAW { namespace Commerce.Runtime.GetReceiptServiceRequest { using System; using System.Collections.Generic; using Microsoft.Dynamics.Commerce.Runtime; using Microsoft.Dynamics.Commerce.Runtime.DataModel; using Microsoft.Dynamics.Commerce.Runtime.DataServices.Messages; using Microsoft.Dynamics.Commerce.Runtime.Messages; using Microsoft.Dynamics.Commerce.Runtime.Services.Messages; using Microsoft.Dynamics.Commerce.Runtime.Workflow; using Microsoft.Dynamics.Commerce.Runtime.RealtimeServices.Messages; using System.Text; using System.Globalization; using System.Linq; using System.Collections.ObjectModel; using System.Text.RegularExpressions; using System.Transactions; using Microsoft.Dynamics.Commerce.Runtime.Data; public class EJFileLawsonService { GetReceiptServiceRequest originalReceiptsRequest; GetReceiptServiceResponse originalReceiptsResponse; public EJFileLawsonService(GetReceiptServiceRequest request, GetReceiptServiceResponse response) { originalReceiptsRequest = request; originalReceiptsResponse = response; } public void Process() { string finalReceipt = ""; string finaleReceiptHTML = "", headerHTML = "", bodyHTML = "", footerHTML = ""; int lineNum = 0; OrgUnit orgUnit = this.GetStoreFromContext(originalReceiptsRequest.RequestContext); foreach (Receipt receipt in originalReceiptsResponse.Receipts) { lineNum++; if (originalReceiptsRequest.IsCopy == false && originalReceiptsRequest.IsPreview == false) { finalReceipt = receipt.Header + receipt.Body + receipt.Footer; finaleReceiptHTML = this.ConvertToHTML(finalReceipt); headerHTML = this.ConvertToHTML(receipt.Header); bodyHTML = this.ConvertToHTML(receipt.Body); footerHTML = this.ConvertToHTML(receipt.Footer); this.InsertDataIntoExTTable(finaleReceiptHTML, headerHTML, bodyHTML, footerHTML, originalReceiptsRequest, receipt, lineNum); } } } private OrgUnit GetStoreFromContext(RequestContext context) { SearchOrgUnitDataRequest orgUnitDataRequest = new SearchOrgUnitDataRequest(context.GetPrincipal().ChannelId); return context.Execute<EntityDataServiceResponse<OrgUnit>>((Request)orgUnitDataRequest).PagedEntityCollection.SingleOrDefault<OrgUnit>(); } private string ConvertToHTML(string receipt) { if (string.IsNullOrEmpty(receipt)) return receipt; string[] strArray = Regex.Split(receipt, "(\\|1C|\\|2C|\\|3C|\\|4C|\\r\\n)", RegexOptions.Multiline); StringBuilder stringBuilder = new StringBuilder(); bool flag = false; foreach (string str in strArray) { if (!string.IsNullOrEmpty(str)) { if (!(str == "|1C") && !(str == "|3C")) { if (!(str == "|2C") && !(str == "|4C")) { if (str == "\r\n") { stringBuilder.Append("\n"); } else { stringBuilder.Append(str); if (flag) stringBuilder.Append(new string(' ', str.Length)); } } else flag = true; } else flag = false; } } receipt = this.RemoveLogo(stringBuilder.ToString()); return receipt; } private string RemoveLogo(string receipt) { foreach (Match match in new Regex("<L(.?|:.+)>").Matches(receipt)) receipt = receipt.Replace(match.Value, string.Empty); return receipt; } private void InsertDataIntoExTTable(string _finalReceiptString, string _header, string _body, string _footer, GetReceiptServiceRequest _originalReceiptsRequest, Receipt _receipt, int _lineNum) { using (TransactionScope transactionScope = new TransactionScope()) { ParameterSet parameters = new ParameterSet(); if (_receipt.ReceiptType == ReceiptType.SalesReceipt || _receipt.ReceiptType == ReceiptType.SalesOrderReceipt || _receipt.ReceiptType == ReceiptType.SuspendedTransaction || _receipt.ReceiptType == ReceiptType.CustomReceipt6 || _receipt.ReceiptType == ReceiptType.CustomReceipt7) { parameters["@TERMINALID"] = _originalReceiptsRequest.SalesOrder.TerminalId; parameters["@SHIFTID"] = _originalReceiptsRequest.SalesOrder.ShiftId; parameters["@RECEIPTTYPES"] = _receipt.ReceiptTypeStrValue; parameters["@CHANNEL"] = _originalReceiptsRequest.SalesOrder.ChannelId; parameters["@TRANSACTIONDATETIME"] = _originalReceiptsRequest.SalesOrder.CreatedDateTime; parameters["@TRANSACTIONDATE"] = _originalReceiptsRequest.SalesOrder.CreatedDateTime; parameters["@TRANSACTIONTYPE"] = _originalReceiptsRequest.SalesOrder.TransactionTypeValue; parameters["@TRANSACTIONID"] = _receipt.TransactionId; parameters["@LINENUM"] = _lineNum; } else if (_receipt.ReceiptType == ReceiptType.SafeDrop || _receipt.ReceiptType == ReceiptType.FloatEntry || _receipt.ReceiptType == ReceiptType.StartingAmount || _receipt.ReceiptType == ReceiptType.RemoveTender) { parameters["@TERMINALID"] = _originalReceiptsRequest.RequestContext.GetPrincipal().ShiftTerminalId; parameters["@SHIFTID"] = _originalReceiptsRequest.RequestContext.GetPrincipal().ShiftId; parameters["@RECEIPTTYPES"] = _receipt.ReceiptTypeStrValue; parameters["@CHANNEL"] = _originalReceiptsRequest.RequestContext.GetPrincipal().ChannelId; parameters["@TRANSACTIONDATETIME"] = DateTime.Now.ToUniversalTime(); parameters["@TRANSACTIONDATE"] = DateTime.Now.ToUniversalTime(); parameters["@TRANSACTIONTYPE"] = _originalReceiptsRequest.NonSalesTenderTransaction.TransactionTypeValue; parameters["@TRANSACTIONID"] = _receipt.TransactionId; parameters["@LINENUM"] = _lineNum; } else if (_receipt.ReceiptType == ReceiptType.TenderDeclaration) { parameters["@TERMINALID"] = _originalReceiptsRequest.DropAndDeclareTransaction.TerminalId; parameters["@RECEIPTTYPES"] = _receipt.ReceiptTypeStrValue; parameters["@SHIFTID"] = _originalReceiptsRequest.DropAndDeclareTransaction.ShiftId; parameters["@CHANNEL"] = _originalReceiptsRequest.RequestContext.GetPrincipal().ChannelId; parameters["@TRANSACTIONDATETIME"] = DateTime.Now.ToUniversalTime(); parameters["@TRANSACTIONDATE"] = DateTime.Now.ToUniversalTime(); parameters["@TRANSACTIONTYPE"] = _originalReceiptsRequest.DropAndDeclareTransaction.TransactionTypeValue; parameters["@TRANSACTIONID"] = _receipt.TransactionId; parameters["@LINENUM"] = _lineNum; } else if (_receipt.ReceiptType == ReceiptType.ZReport) { parameters["@TERMINALID"] = _originalReceiptsRequest.ShiftDetails.TerminalId; parameters["@SHIFTID"] = _originalReceiptsRequest.ShiftDetails.ShiftId; parameters["@RECEIPTTYPES"] = "X-Reading Report"; parameters["@CHANNEL"] = _originalReceiptsRequest.RequestContext.GetPrincipal().ChannelId; parameters["@TRANSACTIONDATETIME"] = DateTime.Now.ToUniversalTime(); parameters["@TRANSACTIONDATE"] = DateTime.Now.ToUniversalTime(); parameters["@TRANSACTIONTYPE"] = 24; parameters["@LINENUM"] = _lineNum; /* Get XDOC Number*/ var queryRetailXReadingTransactionDetail = new SqlPagedQuery(QueryResultSettings.SingleRecord) { DatabaseSchema = "ext", Select = new ColumnSet("DOCNUM"), From = "LAW_RETAILXREADINGTRANSACTIONDETAIL", Where = "STORE = @STORE AND TERMINALID = @TERMINALID AND DATAAREAID = @DATAAREAID AND SHIFT = @SHIFT" }; string storeNum = this.GetStoreNumber(_originalReceiptsRequest); queryRetailXReadingTransactionDetail.Parameters["@TERMINALID"] = _originalReceiptsRequest.ShiftDetails.TerminalId; queryRetailXReadingTransactionDetail.Parameters["@STORE"] = storeNum; queryRetailXReadingTransactionDetail.Parameters["@DATAAREAID"] = _originalReceiptsRequest.RequestContext.GetChannelConfiguration().InventLocationDataAreaId; queryRetailXReadingTransactionDetail.Parameters["@SHIFT"] = _originalReceiptsRequest.ShiftDetails.ShiftId; using (DatabaseContext databaseContextXReadingDetail = new DatabaseContext(_originalReceiptsRequest.RequestContext)) { var xReadingEntityDetail = databaseContextXReadingDetail.ReadEntity<ExtensionsEntity>(queryRetailXReadingTransactionDetail).FirstOrDefault(); if (xReadingEntityDetail != null) { string XDOC = xReadingEntityDetail.GetProperty("DOCNUM").ToString(); parameters["@TRANSACTIONID"] = XDOC; } } } else if (_receipt.ReceiptType == ReceiptType.XReport) { parameters["@TERMINALID"] = _originalReceiptsRequest.ShiftDetails.TerminalId; parameters["@SHIFTID"] = _originalReceiptsRequest.ShiftDetails.ShiftId; parameters["@RECEIPTTYPES"] = "Tender Report"; parameters["@CHANNEL"] = _originalReceiptsRequest.RequestContext.GetPrincipal().ChannelId; parameters["@TRANSACTIONDATETIME"] = DateTime.Now.ToUniversalTime(); parameters["@TRANSACTIONDATE"] = DateTime.Now.ToUniversalTime(); parameters["@TRANSACTIONTYPE"] = 24; parameters["@TRANSACTIONID"] = DateTime.Now.ToString("yyyyMMddHHmmss"); parameters["@LINENUM"] = _lineNum; } parameters["@DATAAREAID"] = _originalReceiptsRequest.RequestContext.GetChannelConfiguration().InventLocationDataAreaId; parameters["@EVENTCREATEDDATETIME"] = DateTime.Now.ToUniversalTime(); parameters["@REPLICATED"] = 0; parameters["@RECEIPTID"] = _receipt.ReceiptId; if(_receipt.ReceiptType == ReceiptType.CustomReceipt7) parameters["@RECEIPTHEADER"] = "DIEP VIEN 007" + _header; else parameters["@RECEIPTHEADER"] = _header; parameters["@RECEIPTBODY"] = _body; parameters["@RECEIPTFOOTER"] = _footer; parameters["@RECEIPTSTRING"] = _finalReceiptString; if (_receipt.ReceiptType == ReceiptType.CustomReceipt6) { parameters["@LINENUM"] = _lineNum + 1; } int errorCode; using (var databaseContext = new DatabaseContext(_originalReceiptsRequest)) { errorCode = databaseContext.ExecuteStoredProcedureNonQuery("[ext].[LAW_RETAILDOCUMENTSPRINTINGHISTORY_Insert]", parameters); } if (errorCode != 0) throw new StorageException(StorageErrors.Microsoft_Dynamics_Commerce_Runtime_CriticalStorageError, errorCode, "Unable to save data"); transactionScope.Complete(); } } public string GetStoreNumber(Request request) { long StoreRecId = request.RequestContext.GetPrincipal().ChannelId; string StoreNumber = string.Empty; var queryRetailStore = new SqlPagedQuery(QueryResultSettings.SingleRecord) { DatabaseSchema = "ax", Select = new ColumnSet(new string[] { "STORENUMBER" }), From = "RETAILSTORETABLE", Where = "RECID = @RECID" }; queryRetailStore.Parameters["@RECID"] = StoreRecId; using (DatabaseContext databaseContext = new DatabaseContext(request.RequestContext)) { var extensions = databaseContext.ReadEntity<ExtensionsEntity>(queryRetailStore).FirstOrDefault(); StoreNumber = extensions.GetProperty("STORENUMBER").ToString(); } return StoreNumber; } } } }