/// <summary>
            /// Validates the counting difference for tender declaration operation.
            /// </summary>
            /// <param name="request">Drop and declare service request.</param>
            internal static void ValidateTenderDeclarationCountingDifference(SaveDropAndDeclareServiceRequest request)
            {
                ThrowIf.Null(request, "request");
                ThrowIf.NullOrWhiteSpace(request.ShiftId, "request.ShiftId");

                if (request.TransactionType == TransactionType.TenderDeclaration)
                {
                    // Retrieves the tender types info
                    Dictionary <string, TenderType> tenderTypeDict = StoreOperationServiceHelper.GetChannelTenderTypes(request.RequestContext);

                    // Check if tender declaration recount is needed
                    bool isRecountNeeded = request.TenderDetails.All(t => tenderTypeDict.ContainsKey(t.TenderTypeId)) && request.TenderDetails.Any(t => t.TenderRecount < tenderTypeDict[t.TenderTypeId].MaxRecount);

                    if (isRecountNeeded)
                    {
                        // Retrieves the expected shift tender amounts per tender type
                        Dictionary <string, ShiftTenderLine> expectedShiftTenderAmounts = StoreOperationServiceHelper.GetShiftRequiredAmountsPerTender(request.RequestContext, request.ShiftTerminalId, request.ShiftId);

                        // Validates the counting amount in tender declaration lines
                        foreach (TenderDetail declartionLine in request.TenderDetails)
                        {
                            string  tenderTypeId = declartionLine.TenderTypeId;
                            decimal countingDifferenceAllowed = tenderTypeDict[tenderTypeId].MaxCountingDifference;
                            decimal expectedShiftTenderAmount = expectedShiftTenderAmounts.ContainsKey(tenderTypeId) ? expectedShiftTenderAmounts[tenderTypeId].TenderedAmountOfStoreCurrency : 0M;
                            int     maxRecountAllowed         = tenderTypeDict[tenderTypeId].MaxRecount;
                            string  tenderName = tenderTypeDict[tenderTypeId].Name;

                            // Checks if current number of recount exceeds the permissible recount
                            if (declartionLine.TenderRecount < maxRecountAllowed)
                            {
                                // Checks if the difference between current tender delcartion amount with expected tender amounts
                                // exceeds the permissible counting difference for that tender type
                                if (Math.Abs(declartionLine.Amount - expectedShiftTenderAmount) > countingDifferenceAllowed)
                                {
                                    throw new TenderValidationException(
                                              tenderTypeId,
                                              DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_MaxCountingDifferenceExceeded,
                                              string.Format("The counted amount differs from the total sales amount for the {0} tender type.", tenderName))
                                          {
                                              LocalizedMessageParameters = new object[] { tenderName }
                                          };
                                }
                            }
                        }
                    }
                }
            }
            private static void OnSaveDropAndDeclareExecuting(SaveDropAndDeclareServiceRequest request)
            {
                switch (request.TransactionType)
                {
                case TransactionType.BankDrop:
                    request.RequestContext.Execute <NullResponse>(new CheckAccessServiceRequest(RetailOperation.BankDrop));
                    break;

                case TransactionType.SafeDrop:
                    request.RequestContext.Execute <NullResponse>(new CheckAccessServiceRequest(RetailOperation.SafeDrop));
                    break;

                case TransactionType.TenderDeclaration:
                    bool isNonDrawerOperationCheckRequired = IsNonDrawerOperationCheckRequired(request.RequestContext, request.ShiftId);
                    request.RequestContext.Execute <NullResponse>(new CheckAccessServiceRequest(RetailOperation.TenderDeclaration, isNonDrawerOperationCheckRequired));
                    break;
                }
            }
            /// <summary>
            /// Gets the object of tender drop and declare operation.
            /// </summary>
            /// <param name="request">The SaveDropAndDeclareServiceRequest object.</param>
            /// <returns>The tender drop and declare operation object.</returns>
            internal static DropAndDeclareTransaction ConvertTenderDropAndDeclareTransaction(SaveDropAndDeclareServiceRequest request)
            {
                RequestContext context     = request.RequestContext;
                var            transaction = new DropAndDeclareTransaction
                {
                    Id                          = request.TransactionId,
                    ShiftId                     = request.ShiftId,
                    ShiftTerminalId             = string.IsNullOrWhiteSpace(request.ShiftTerminalId) ? context.GetPrincipal().ShiftTerminalId : request.ShiftTerminalId,
                    TransactionType             = request.TransactionType,
                    ChannelCurrencyExchangeRate = StoreOperationServiceHelper.GetExchangeRate(context),
                    StoreId                     = context.GetOrgUnit().OrgUnitNumber,
                    StaffId                     = context.GetPrincipal().UserId,
                    TerminalId                  = context.GetTerminal().TerminalId,
                    ChannelCurrency             = context.GetChannelConfiguration().Currency, // channel currency code
                    Description                 = request.Description
                };

                transaction.TenderDetails = new Collection <TenderDetail>();
                foreach (var tenderDetail in request.TenderDetails)
                {
                    TenderDetail tender = ConvertToTenderDetail(context, tenderDetail);
                    transaction.TenderDetails.Add(tender);
                }

                if (request.ReasonCodeLines != null && request.ReasonCodeLines.Any())
                {
                    // Read reason code details from request for [Tender Declaration] store operation
                    transaction.ReasonCodeLines = new Collection <ReasonCodeLine>();
                    foreach (var reasonCodeLine in request.ReasonCodeLines)
                    {
                        transaction.ReasonCodeLines.Add(reasonCodeLine);
                    }
                }

                return(transaction);
            }
示例#4
0
            /// <summary>
            /// Invoke the method to save drop and declare transactions.
            /// </summary>
            /// <param name="request">Request context.</param>
            /// <returns>Returns response for save drop and declare.</returns>
            private static SaveDropAndDeclareServiceResponse SaveDropAndDeclareTransactions(SaveDropAndDeclareServiceRequest request)
            {
                StoreOperationServiceHelper.ValidateTenderDeclarationCountingDifference(request);

                DropAndDeclareTransaction tenderDropAndDeclare = StoreOperationServiceHelper.ConvertTenderDropAndDeclareTransaction(request);

                // If the previously created drop transaction response did not get received due to network connection issue.
                // On client retry, check if it was already saved. If true, returns saved object.
                var getDropAndDeclareTransactionDataRequest = new GetDropAndDeclareTransactionDataRequest(tenderDropAndDeclare.Id, QueryResultSettings.SingleRecord);
                DropAndDeclareTransaction transaction       = request.RequestContext.Runtime.Execute <EntityDataServiceResponse <DropAndDeclareTransaction> >(getDropAndDeclareTransactionDataRequest, request.RequestContext).PagedEntityCollection.FirstOrDefault();

                if (transaction != null)
                {
                    var getDropAndDeclareTransactionTenderDetailsDataRequest = new GetDropAndDeclareTransactionTenderDetailsDataRequest(tenderDropAndDeclare.Id, QueryResultSettings.AllRecords);
                    transaction.TenderDetails = request.RequestContext.Runtime.Execute <EntityDataServiceResponse <TenderDetail> >(getDropAndDeclareTransactionTenderDetailsDataRequest, request.RequestContext).PagedEntityCollection.Results;
                }
                else
                {
                    var saveDropAndDeclareTransactionDataRequest = new SaveDropAndDeclareTransactionDataRequest(tenderDropAndDeclare);
                    transaction = request.RequestContext.Runtime.Execute <SingleEntityDataServiceResponse <DropAndDeclareTransaction> >(saveDropAndDeclareTransactionDataRequest, request.RequestContext).Entity;
                }

                return(new SaveDropAndDeclareServiceResponse(transaction));
            }