Exemple #1
0
            private GetReceiptServiceRequest CreateXZReportReceiptServiceRequest(GetXAndZReportReceiptRequest request)
            {
                ThrowIf.Null(request, "request");

                Shift shift;

                var shiftId     = request.ShiftId;
                var receiptType = request.ReceiptType;

                // Validates if the request is XReport or ZReport type
                if ((receiptType != ReceiptType.XReport) && (receiptType != ReceiptType.ZReport))
                {
                    throw new DataValidationException(DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_ReceiptTypeNotSupported, "Only receipt types for X or Z reports are expected.");
                }

                if (receiptType == ReceiptType.XReport)
                {
                    var terminalId = request.ShiftTerminalId;
                    GetShiftDataRequest getShiftDataRequest = new GetShiftDataRequest(terminalId, shiftId);
                    shift = this.Context.Execute <SingleEntityDataServiceResponse <Shift> >(getShiftDataRequest).Entity;

                    // Validates if an open or blind-closed shift of the ShiftId can be found when requesting XReport
                    if (shift == null ||
                        (shift.Status != ShiftStatus.Open && shift.Status != ShiftStatus.BlindClosed))
                    {
                        throw new DataValidationException(
                                  DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_ShiftNotFound,
                                  string.Format("No open shift information can be found using the shift Id {0} on terminal {1} for X report.", shiftId, terminalId));
                    }

                    // Calculates the shift information inorder to generate the X report
                    ShiftCalculator.Calculate(this.Context, shift, shift.TerminalId, shift.ShiftId);
                }
                else
                {
                    var terminalId = this.Context.GetTerminal().TerminalId;
                    GetLastClosedShiftDataRequest getLastClosedShiftDataRequest = new GetLastClosedShiftDataRequest(terminalId);
                    shift = this.Context.Execute <SingleEntityDataServiceResponse <Shift> >(getLastClosedShiftDataRequest).Entity;

                    // Validates if a closed shift of the ShiftId can be found when requesting XReport
                    if (shift == null || shift.Status != ShiftStatus.Closed)
                    {
                        throw new DataValidationException(
                                  DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_ShiftNotFound,
                                  string.Format("No closed shift information can be found using the shift Id {0} on terminal {1} for Z report.", shiftId, terminalId));
                    }
                }

                var getReceiptServiceRequest = new GetReceiptServiceRequest(
                    shift,
                    new List <ReceiptType>()
                {
                    receiptType
                }.AsReadOnly(),
                    request.HardwareProfileId);

                return(getReceiptServiceRequest);
            }
            /// <summary>
            /// Gets the last closed shift.
            /// </summary>
            /// <param name="request">The request.</param>
            /// <returns>A single entity data service response.</returns>
            private SingleEntityDataServiceResponse <Shift> GetLastClosedShift(GetLastClosedShiftDataRequest request)
            {
                ThrowIf.Null(request, "request");
                ThrowIf.NullOrWhiteSpace(request.TerminalId, "request.TerminalId");

                long channelId = request.RequestContext.GetPrincipal().ChannelId;

                var query = new SqlPagedQuery(QueryResultSettings.FirstRecord)
                {
                    From    = ShiftsView,
                    Where   = "CHANNEL = @ChannelId AND CLOSEDATTERMINAL = @TerminalId AND STATUS = @Status",
                    OrderBy = "CLOSEDATETIMEUTC DESC"
                };

                query.Parameters["@ChannelId"]  = channelId;
                query.Parameters["@TerminalId"] = request.TerminalId;
                query.Parameters["@Status"]     = (int)ShiftStatus.Closed;

                Shift lastShift = null;

                using (var sqlServerDatabaseContext = new SqlServerDatabaseContext(request))
                {
                    lastShift = sqlServerDatabaseContext.ReadEntity <Shift>(query).Results.FirstOrDefault();
                }

                if (lastShift != null)
                {
                    GetShiftDataRequest getShiftDataRequest = new GetShiftDataRequest(lastShift.TerminalId, lastShift.ShiftId);
                    lastShift = request.RequestContext.Execute <SingleEntityDataServiceResponse <Shift> >(getShiftDataRequest).Entity;

                    // Convert UTC time to channel time.
                    if (lastShift.StartDateTime != null)
                    {
                        lastShift.StartDateTime = new DateTimeOffset(lastShift.StartDateTime.Value.DateTime, new TimeSpan(0));
                    }

                    if (lastShift.StatusDateTime != null)
                    {
                        lastShift.StatusDateTime = new DateTimeOffset(lastShift.StatusDateTime.Value.DateTime, new TimeSpan(0));
                    }

                    if (lastShift.CloseDateTime != null)
                    {
                        lastShift.CloseDateTime = new DateTimeOffset(lastShift.CloseDateTime.Value.DateTime, new TimeSpan(0));
                    }
                }

                return(new SingleEntityDataServiceResponse <Shift>(lastShift));
            }