コード例 #1
0
            /// <summary>
            /// Gets the transaction for custom receipts.
            /// </summary>
            /// <param name="request">The get receipt request.</param>
            /// <returns>The sales order.</returns>
            /// <remarks>
            /// This method is used to handle custom receipt. Since only sales order related receipt
            /// is using receipt designer while others are hardcoded, so we cannot support customizing receipts for
            /// NonSalesTransaction or DropAndDeclareTransaction.
            /// Here we first try to load the SalesOrder, if we cannot find it, then try to load
            /// a cart and convert it to sales order.
            /// </remarks>
            private SalesOrder GetTransactionForCustomReceipt(GetReceiptRequest request)
            {
                ReceiptRetrievalCriteria criteria = request.ReceiptRetrievalCriteria;
                string transactionId = request.TransactionId;

                SalesOrder salesOrder = null;

                if (!criteria.QueryBySalesId)
                {
                    salesOrder = this.GetTransactionByTransactionId(transactionId, false) ?? this.GetTransactionByTransactionId(transactionId, true);
                }
                else
                {
                    try
                    {
                        salesOrder = this.GetTransactionBySalesId(transactionId);
                    }
                    catch (FeatureNotSupportedException)
                    {
                        // Not able to get sales order in offline mode.
                    }
                }

                // If cannot find a sales order, then try to find a cart (suspended transaction).
                if (salesOrder == null)
                {
                    SalesTransaction salesTransaction = CartWorkflowHelper.LoadSalesTransaction(request.RequestContext, request.TransactionId);
                    if (salesTransaction != null)
                    {
                        salesOrder = new SalesOrder();

                        // Now ReceiptService only accept SalesOrder. So in order to reuse the code in ReceiptService, we need to convert
                        // SalesTransaction to SalesOrder. SalesOrder is extended from SalesTransaction, so in this case we are good. But
                        // in the future we should refactor ReceiptService to make it accept a common interface so that we can extend to
                        // other objects.
                        salesOrder.CopyFrom(salesTransaction);
                    }
                    else
                    {
                        throw new DataValidationException(
                                  DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_ObjectNotFound,
                                  string.Format("Unable to get the transaction created. ID: {0}", transactionId));
                    }
                }

                return(salesOrder);
            }