Exemple #1
0
            /// <summary>
            /// Gets the reason code settings dictionary.
            /// </summary>
            /// <param name="context">The context.</param>
            /// <returns>The reason code settings dictionary.</returns>
            private static ReasonCodeSettings GetReasonCodeSettings(RequestContext context)
            {
                ReasonCodeSettings settings = null;

                GetReasonCodeSettingsDataRequest getReasonCodeSettingsDataRequest = new GetReasonCodeSettingsDataRequest(QueryResultSettings.SingleRecord);

                settings = context.Runtime.Execute <SingleEntityDataServiceResponse <ReasonCodeSettings> >(getReasonCodeSettingsDataRequest, context).Entity;

                // Reason code settings should be available for retail store.
                if (settings == null)
                {
                    var     getChannelByIdDataRequest = new GetChannelByIdDataRequest(context.GetPrincipal().ChannelId);
                    Channel currentChannel            = context.Runtime.Execute <SingleEntityDataServiceResponse <Channel> >(getChannelByIdDataRequest, context).Entity;

                    if (currentChannel.OrgUnitType == RetailChannelType.RetailStore)
                    {
                        throw new DataValidationException(
                                  DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_ObjectNotFound,
                                  "The required reason code settings not found.");
                    }

                    // Use default settings for non-retail-store channels.
                    settings = ReasonCodeSettings.DefaultSettings;
                }

                return(settings);
            }
Exemple #2
0
            /// <summary>
            /// Sets default Sales Tax Group (STG) for the cart based on a channel tax configuration for non-return transaction.
            /// </summary>
            /// <param name="context">The request context.</param>
            /// <param name="salesTransaction">Current transaction.</param>
            private static void SetSalesTaxGroupOnNonReturn(RequestContext context, SalesTransaction salesTransaction)
            {
                ThrowIf.Null(context, "context");
                ThrowIf.Null(salesTransaction, "salesTransaction");

                Channel channel;
                long    currentChannelId = context.GetPrincipal().ChannelId;
                string  channelTaxGroup;

                if (context.GetChannelConfiguration().ChannelType == RetailChannelType.RetailStore)
                {
                    OrgUnit store = context.GetOrgUnit();
                    channel         = store;
                    channelTaxGroup = store.TaxGroup;
                }
                else
                {
                    var getChannelByIdDataRequest = new GetChannelByIdDataRequest(currentChannelId);
                    channel = context.Runtime.Execute <SingleEntityDataServiceResponse <Channel> >(getChannelByIdDataRequest, context).Entity;

                    channelTaxGroup = string.Empty;
                }

                Address headerAddress = Address.IsNullOrEmpty(salesTransaction.ShippingAddress)
                    ? null
                    : salesTransaction.ShippingAddress;

                // Header charges follows header when taxed
                SalesTaxGroupPicker headerPicker = SalesTaxGroupPicker.Create(
                    channel,
                    context,
                    headerAddress,
                    salesTransaction.DeliveryMode,
                    salesTransaction.StoreId ?? string.Empty,
                    salesTransaction.InventoryLocationId,
                    salesTransaction.CustomerId);

                FillChargeLinesSalesTaxGroup(context, salesTransaction.ChargeLines, headerPicker.SalesTaxGroup, channelTaxGroup);

                // items needed to retrieve ITG information from Product repo for each sales line
                IEnumerable <Item>        items         = GetItemsForSalesLines(context, salesTransaction.ActiveSalesLines);
                Dictionary <string, Item> itemsByItemId = items.ToDictionary(i => i.ItemId, i => i, StringComparer.OrdinalIgnoreCase);

                // Consider active lines for taxation purpose only.
                foreach (SalesLine salesLine in salesTransaction.ActiveSalesLines)
                {
                    // On Return By Receipt carts, we don't change tax groups as they come populated from the headquarters
                    // Tax groups have been set during sales transaction creation and could be from a different store than the one the return is being made.
                    if (!salesLine.IsReturnByReceipt)
                    {
                        Address shippingAddress = Address.IsNullOrEmpty(salesLine.ShippingAddress)
                            ? headerAddress
                            : salesLine.ShippingAddress;

                        SalesTaxGroupPicker linePicker = SalesTaxGroupPicker.Create(
                            channel,
                            context,
                            shippingAddress,
                            salesLine.DeliveryMode,
                            salesLine.FulfillmentStoreId ?? string.Empty,
                            salesTransaction.InventoryLocationId,
                            salesTransaction.CustomerId);
                        salesLine.SalesTaxGroupId         = linePicker.SalesTaxGroup;
                        salesLine.OriginalSalesTaxGroupId = salesLine.SalesTaxGroupId;

                        Item cartItem;

                        // case 1: item without item tax group (set to null), regular cash and carry or sales order
                        //          -> set ITG to Product ITG
                        // case 2: customer order recall with tax exempt, sales line ITG
                        //         is set to "" (empty string) or carries any other value
                        //          -> keep ITG already set on sales line
                        if (salesLine.ItemId != null &&
                            salesLine.ItemTaxGroupId == null &&
                            itemsByItemId.TryGetValue(salesLine.ItemId, out cartItem))
                        {
                            salesLine.ItemTaxGroupId         = cartItem.ItemTaxGroupId;
                            salesLine.OriginalItemTaxGroupId = salesLine.ItemTaxGroupId;
                        }

                        FillChargeLinesSalesTaxGroup(context, salesLine.ChargeLines, linePicker.SalesTaxGroup, channelTaxGroup);
                    }
                }
            }