public QueryResultDisplay SearchByDateRange(QueryDisplay query)
        {
            var invoiceDateStart = query.Parameters.FirstOrDefault(x => x.FieldName == "invoiceDateStart");
            var invoiceDateEnd = query.Parameters.FirstOrDefault(x => x.FieldName == "invoiceDateEnd");

            DateTime startDate;
            DateTime endDate;
            if (invoiceDateStart == null) throw new NullReferenceException("invoiceDateStart is a required parameter");

            var settings = _storeSettingService.GetAll().ToList();
            var dateFormat = settings.FirstOrDefault(s => s.Name == "dateFormat");
            if (dateFormat == null)
            {
                if (!DateTime.TryParse(invoiceDateStart.Value, out startDate)) throw new InvalidCastException("Failed to convert invoiceDateStart to a valid DateTime");
            }
            else if (!DateTime.TryParseExact(invoiceDateStart.Value, dateFormat.Value, CultureInfo.InvariantCulture, DateTimeStyles.None, out startDate))
            {
                throw new InvalidCastException("Failed to convert invoiceDateStart to a valid DateTime");
            }

            endDate = invoiceDateEnd == null || dateFormat == null
                ? DateTime.MaxValue
                : DateTime.TryParseExact(invoiceDateEnd.Value, dateFormat.Value, CultureInfo.InvariantCulture, DateTimeStyles.None, out endDate)
                    ? endDate
                    : DateTime.MaxValue;

            var invoices = _merchello.Query.Invoice.Search(
                startDate,
                endDate.AddDays(1), // through end of day
                1,
                long.MaxValue,
                query.SortBy,
                query.SortDirection);


            var result = new QueryResultDisplay();

            if (!invoices.Items.Any()) return result;

            // Use a visitor to build the collection of report data
            var vistor = new SalesByItemVisitor(_merchello);

            foreach (var invoice in invoices.Items)
            {
                ((InvoiceDisplay)invoice).Accept(vistor);
            }

            result.TotalItems = vistor.Results.Count();
            result.ItemsPerPage = vistor.Results.Count();
            result.CurrentPage = 0;
            result.TotalPages = 1;
            result.Items = vistor.Results;

            return result;
        }
예제 #2
0
        public QueryResultDisplay SearchCustomers(QueryDisplay query)
        {
            var term = query.Parameters.FirstOrDefault(x => x.FieldName == "term");

            return term != null && !string.IsNullOrEmpty(term.Value)
              ?
               _merchello.Query.Customer.Search(
                  term.Value,
                  query.CurrentPage + 1,
                  query.ItemsPerPage,
                  query.SortBy,
                  query.SortDirection)
              :
              _merchello.Query.Customer.Search(
                  query.CurrentPage + 1,
                  query.ItemsPerPage,
                  query.SortBy,
                  query.SortDirection);
        }
예제 #3
0
        public QueryResultDisplay SearchByDateRange(QueryDisplay query)
        {
            var lastActivityDateStart = query.Parameters.FirstOrDefault(x => x.FieldName == "lastActivityDateStart");
            var lastActivityDateEnd = query.Parameters.FirstOrDefault(x => x.FieldName == "lastActivityDateEnd");

            DateTime startDate;
            DateTime endDate;
            Mandate.ParameterNotNull(lastActivityDateStart, "lastActivityDateStart is a required parameter");
            Mandate.ParameterCondition(DateTime.TryParse(lastActivityDateStart.Value, out startDate), "Failed to convert lastActivityDateStart to a valid DateTime");

            endDate = lastActivityDateEnd == null
                ? DateTime.MaxValue
                : DateTime.TryParse(lastActivityDateEnd.Value, out endDate)
                    ? endDate
                    : DateTime.MaxValue;

            return
                _merchello.Query.Customer.Search(
                    startDate,
                    endDate,
                    query.CurrentPage + 1,
                    query.ItemsPerPage,
                    query.SortBy,
                    query.SortDirection);
        }
예제 #4
0
        public QueryResultDisplay SearchInvoices(QueryDisplay query)
        {
            var term = query.Parameters.FirstOrDefault(x => x.FieldName == "term");
            var invoiceDateStart = query.Parameters.FirstOrDefault(x => x.FieldName == "invoiceDateStart");
            var invoiceDateEnd = query.Parameters.FirstOrDefault(x => x.FieldName == "invoiceDateEnd");

            var isTermSearch = term != null && !string.IsNullOrEmpty(term.Value);

            var isDateSearch = invoiceDateStart != null && !string.IsNullOrEmpty(invoiceDateStart.Value);

            var startDate = DateTime.MinValue;
            var endDate = DateTime.MaxValue;
            if (isDateSearch)
            {
                var settings = _storeSettingService.GetAll().ToList();
                var dateFormat = settings.FirstOrDefault(s => s.Name == "dateFormat");

                if (dateFormat == null)
                {
                    if (!DateTime.TryParse(invoiceDateStart.Value, out startDate))
                    {
                        MultiLogHelper.Warn<InvoiceApiController>(string.Format("Was unable to parse startDate: {0}", invoiceDateStart.Value));
                        startDate = DateTime.MinValue;
                    }

                }
                else if (!DateTime.TryParseExact(invoiceDateStart.Value, dateFormat.Value, CultureInfo.InvariantCulture, DateTimeStyles.None, out startDate))
                {
                    MultiLogHelper.Warn<InvoiceApiController>(string.Format("Was unable to parse startDate: {0}", invoiceDateStart.Value));
                    startDate = DateTime.MinValue;
                }

                endDate = invoiceDateEnd == null || dateFormat == null
                ? DateTime.MaxValue
                : DateTime.TryParseExact(invoiceDateEnd.Value, dateFormat.Value, CultureInfo.InvariantCulture, DateTimeStyles.None, out endDate)
                    ? endDate
                    : DateTime.MaxValue;
            }

            if (isTermSearch && isDateSearch)
            {
                return _merchello.Query.Invoice.Search(
                    term.Value,
                    startDate,
                    endDate,
                    query.CurrentPage + 1,
                    query.ItemsPerPage,
                    query.SortBy,
                    query.SortDirection);
            }

            if (isTermSearch)
            {
                return this._merchello.Query.Invoice.Search(
                    term.Value,
                    query.CurrentPage + 1,
                    query.ItemsPerPage,
                    query.SortBy,
                    query.SortDirection);
            }

            if (isDateSearch)
            {
                return this._merchello.Query.Invoice.Search(
                    startDate,
                    endDate,
                    query.CurrentPage + 1,
                    query.ItemsPerPage,
                    query.SortBy,
                    query.SortDirection);
            }

            return this._merchello.Query.Invoice.Search(
                query.CurrentPage + 1,
                query.ItemsPerPage,
                query.SortBy,
                query.SortDirection);
        }
예제 #5
0
        public QueryResultDisplay SearchByDateRange(QueryDisplay query)
        {
            var invoiceDateStart = query.Parameters.FirstOrDefault(x => x.FieldName == "invoiceDateStart");
            var invoiceDateEnd = query.Parameters.FirstOrDefault(x => x.FieldName == "invoiceDateEnd");
            var invoiceStatusKey = query.Parameters.FirstOrDefault(x => x.FieldName == "invoiceStatusKey");

            DateTime startDate;
            DateTime endDate;
            Mandate.ParameterNotNull(invoiceDateStart, "invoiceDateStart is a required parameter");
            Mandate.ParameterCondition(DateTime.TryParse(invoiceDateStart.Value, out startDate), "Failed to convert invoiceDateStart to a valid DateTime");

            endDate = invoiceDateEnd == null
                ? DateTime.MaxValue
                : DateTime.TryParse(invoiceDateEnd.Value, out endDate)
                    ? endDate
                    : DateTime.MaxValue;

            return invoiceStatusKey == null
                ? _merchello.Query.Invoice.Search(
                    startDate,
                    endDate,
                    query.CurrentPage + 1,
                    query.ItemsPerPage,
                    query.SortBy,
                    query.SortDirection) :

                 _merchello.Query.Invoice.Search(
                    startDate,
                    endDate,
                    invoiceStatusKey.Value.EncodeAsGuid(),
                    query.CurrentPage + 1,
                    query.ItemsPerPage,
                    query.SortBy,
                    query.SortDirection);
        }
예제 #6
0
        public QueryResultDisplay SearchByCustomer(QueryDisplay query)
        {
            Guid key;

            var customerKey = query.Parameters.FirstOrDefault(x => x.FieldName == "customerKey");
            Mandate.ParameterNotNull(customerKey, "customerKey was null");
            Mandate.ParameterCondition(Guid.TryParse(customerKey.Value, out key), "customerKey was not a valid GUID");

            return _merchello.Query.Invoice.SearchByCustomer(
                key,
                query.CurrentPage + 1,
                query.ItemsPerPage,
                query.SortBy,
                query.SortDirection);
        }
        public QueryResultDisplay SearchByDateRange(QueryDisplay query)
        {
            var invoiceDateStart = query.Parameters.FirstOrDefault(x => x.FieldName == "invoiceDateStart");
            var invoiceDateEnd = query.Parameters.FirstOrDefault(x => x.FieldName == "invoiceDateEnd");

            DateTime startDate;
            DateTime endDate;
            if (invoiceDateStart == null) throw new NullReferenceException("SalesOverTimeReportApiController::SearchByDateRange: invoiceDateStart is a required parameter");

            var settings = _storeSettingService.GetAll().ToList();
            var dateFormat = settings.FirstOrDefault(s => s.Name == "dateFormat");
            if (dateFormat == null)
            {
                if (!DateTime.TryParse(invoiceDateStart.Value, out startDate)) throw new InvalidCastException("SalesOverTimeReportApiController::SearchByDateRange: Failed to convert invoiceDateStart to a valid DateTime");
            }
            else if (!DateTime.TryParseExact(invoiceDateStart.Value, dateFormat.Value, CultureInfo.InvariantCulture, DateTimeStyles.None, out startDate))
            {
                throw new InvalidCastException("SalesOverTimeReportApiController::SearchByDateRange: Failed to convert invoiceDateStart to a valid DateTime");
            }

            endDate = invoiceDateEnd == null || dateFormat == null
                ? DateTime.MaxValue
                : DateTime.TryParseExact(invoiceDateEnd.Value, dateFormat.Value, CultureInfo.InvariantCulture, DateTimeStyles.None, out endDate)
                    ? endDate
                    : DateTime.MaxValue;

            var invoices = _merchello.Query.Invoice.Search(
                startDate,
                endDate.AddDays(1), // through end of day
                1,
                long.MaxValue,
                query.SortBy,
                query.SortDirection);


            var result = new QueryResultDisplay();

            if (!invoices.Items.Any())
            {
                return result;
            }

            ////build list of items grouped by date. each item has "date", "salestotal", "salescount"
            var source = (from invoiceItem in invoices.Items.ToList().Cast<InvoiceDisplay>()
                         where invoiceItem.InvoiceStatus.Alias == "paid"
                         group invoiceItem by invoiceItem.InvoiceDate.Date
                         into g
                         orderby g.Key descending
                         select
                             new SalesOverTimeResult
                                 {
                                     Date = g.Key.ToString("MMMM dd, yyyy"),
                                     SalesTotal = g.Sum(item => item.Total),
                                     SalesCount = g.Count()
                                 }).ToArray();

            result.Items = source;
            result.TotalItems = source.Count();
            result.ItemsPerPage = 10;
            result.CurrentPage = 0;
            result.TotalPages = result.TotalItems / result.ItemsPerPage;

            return result;
        }
        public override QueryResultDisplay GetDefaultReportData()
        {
            var settings = _storeSettingService.GetAll().ToList();
            var dateFormat = settings.FirstOrDefault(s => s.Name == "dateFormat");

            var query = new QueryDisplay()
            {
                CurrentPage = 0,
                ItemsPerPage = int.MaxValue,
                Parameters = new List<QueryDisplayParameter>()
                {
                    new QueryDisplayParameter()
                        {
                        FieldName = "invoiceDateStart",
                        Value = dateFormat != null ? DateTime.Now.AddMonths(-1).ToString(dateFormat.Value) : DateTime.Now.AddMonths(-1).ToShortDateString()
                        },
                    new QueryDisplayParameter()
                        {
                            FieldName = "invoiceDateEnd",
                            Value = dateFormat != null ? DateTime.Now.ToString(dateFormat.Value) : DateTime.Now.ToShortDateString()
                        }
                },
                SortBy = "invoiceDate"
            };

            try
            {
                var results = SearchByDateRange(query);
                return results;
            }
            catch (Exception ex)
            {
                LogHelper.Error<SalesByItemReportApiController>("The system was unable to determine the default report data for the SalesOverTime report", ex);
            }

            return new QueryResultDisplay();
        }
        public QueryResultDisplay GetCustomerSavedBaskets(QueryDisplay query)
        {
            var page = _itemCacheService.GetCustomerItemCachePage(
                _itemCacheType,
                _startDate,
                _endDate.AddDays(2),
                query.CurrentPage + 1,
                query.ItemsPerPage,
                query.SortBy,
                query.SortDirection);

            return new QueryResultDisplay()
            {
                Items = page.Items.Select(x => x.ToCustomerItemCacheDisplay()),
                CurrentPage = page.CurrentPage - 1,
                ItemsPerPage = page.ItemsPerPage,
                TotalPages = page.TotalPages,
                TotalItems = page.TotalItems
            };
        }
        public QueryResultDisplay PostGetEntitiesNotInCollection(QueryDisplay query)
        {
            var collectionKey = query.Parameters.FirstOrDefault(x => x.FieldName == "collectionKey");
            var entityTypeName = query.Parameters.FirstOrDefault(x => x.FieldName == "entityType");
            if (collectionKey == null || entityTypeName == null) throw new NullReferenceException("collectionKey and entityType must be included as a parameter");

            var key = new Guid(collectionKey.Value);

            var entityType = (EntityType)Enum.Parse(typeof(EntityType), entityTypeName.Value);

            var term = query.Parameters.FirstOrDefault(x => x.FieldName == "term");

            var cachedQuery = this.GetCachedQueryByEntityType(entityType);

            return term != null && !string.IsNullOrEmpty(term.Value)
              ?
               cachedQuery.GetNotInCollection(
                  key,
                  term.Value,
                  query.CurrentPage + 1,
                  query.ItemsPerPage,
                  query.SortBy,
                  query.SortDirection)
              :
              cachedQuery.GetNotInCollection(
                  key,
                  query.CurrentPage + 1,
                  query.ItemsPerPage,
                  query.SortBy,
                  query.SortDirection);
        }
예제 #11
0
        public QueryResultDisplay SearchOptions(QueryDisplay query)
        {
            var termParam = query.Parameters.FirstOrDefault(x => x.FieldName == "term");
            var term = termParam == null ? string.Empty : termParam.Value;

            var sharedOnlyParam = query.Parameters.FirstOrDefault(x => x.FieldName == "sharedOnly");

            var sharedOnly = sharedOnlyParam == null;

            var page = _productOptionService.GetPage(term, query.CurrentPage + 1, query.ItemsPerPage, query.SortBy, query.SortDirection, sharedOnly);

            return page.ToQueryResultDisplay(MapToProductOptionDisplayForEditor);
        }
예제 #12
0
        public QueryResultDisplay SearchOffers(QueryDisplay query)
        {
            var term = query.Parameters.FirstOrDefault(x => x.FieldName == "term");

            var hasSearchTerm = term != null && !string.IsNullOrEmpty(term.Value);

            var page = hasSearchTerm ?
                _offerSettingsService.GetPage(term.Value, query.CurrentPage + 1, query.ItemsPerPage, query.SortBy, query.SortDirection) :
                _offerSettingsService.GetPage(query.CurrentPage + 1, query.ItemsPerPage, query.SortBy, query.SortDirection);

            return this.GetQueryResultDisplay(page);
        }
        public HttpResponseMessage GetOrderReportData(QueryDisplay query)
        {
            HttpResponseMessage result = null;

            var invoiceDateStart = query.Parameters.FirstOrDefault(x => x.FieldName == "invoiceDateStart");
            var invoiceDateEnd = query.Parameters.FirstOrDefault(x => x.FieldName == "invoiceDateEnd");

            DateTime dtStart;
            DateTime dtEnd;
            if (invoiceDateStart == null)
            {
                result = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "invoiceDateStart is a required parameter");
                return result;
            }

            var settings = _storeSettingService.GetAll().ToList();
            var dateFormat = settings.FirstOrDefault(s => s.Name == "dateFormat");
            if (dateFormat == null)
            {
                if (!DateTime.TryParse(invoiceDateStart.Value, out dtStart))
                {
                    result = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Failed to convert invoiceDateStart to a valid DateTime");
                    return result;
                }
            }
            else if (!DateTime.TryParseExact(invoiceDateStart.Value, dateFormat.Value, CultureInfo.InvariantCulture, DateTimeStyles.None, out dtStart))
            {
                result = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Failed to convert invoiceDateStart to a valid DateTime");
                return result;
            }
            
            dtEnd = invoiceDateEnd == null || dateFormat == null
                ? DateTime.MaxValue
                : DateTime.TryParseExact(invoiceDateEnd.Value, dateFormat.Value, CultureInfo.InvariantCulture, DateTimeStyles.None, out dtEnd)
                    ? dtEnd.AddDays(1) // through end of day
                    : DateTime.MaxValue;

            var invoices = _invoiceService.GetInvoicesByDateRange(dtStart,dtEnd).ToArray();

            try
            {
                var csvExport = new CvsExport();
                foreach (var invoice in invoices)
                {
                    csvExport.AddRow();

                    csvExport["Invoice Number"] = invoice.InvoiceNumber;
                    csvExport["PO Number"] = invoice.PoNumber;
                    csvExport["Order Date"] = invoice.InvoiceDate;
                    csvExport["Bill To Name"] = invoice.BillToName;
                    csvExport["Bill To Company"] = invoice.BillToCompany;
                    csvExport["Bill To Address"] = invoice.BillToAddress1;
                    csvExport["Bill To Address2"] = invoice.BillToAddress2;
                    csvExport["Email"] = invoice.BillToEmail;
                    csvExport["Phone"] = invoice.BillToPhone;
                    csvExport["City"] = invoice.BillToLocality;
                    csvExport["State"] = invoice.BillToRegion;
                    csvExport["Postal Code"] = invoice.BillToPostalCode;
                    csvExport["Total"] = invoice.Total;
                    csvExport["Status"] = invoice.InvoiceStatus.Name;

                    foreach (var invoiceItems in invoice.Items)
                    {
                        foreach (var invoiceItem in invoice.Items)
                        {
                            if (invoiceItem.LineItemType == LineItemType.Product)
                            {
                                csvExport["Name"] = invoiceItem.Name;
                                csvExport["Sku"] = invoiceItem.Sku;
                                csvExport["Quantity"] = invoiceItem.Quantity;
                                csvExport["Price"] = invoiceItem.Price;
                            }
                            else if (invoiceItem.LineItemType == LineItemType.Shipping)
                            {
                                csvExport["Ship Method"] = invoiceItem.Name;
                                csvExport["Ship Quantity"] = invoiceItem.Quantity;
                                csvExport["Ship Price"] = invoiceItem.Price;

                                var origin =invoiceItem.ExtendedData.GetAddress(Constants.ExtendedDataKeys.ShippingOriginAddress);
                                var destination =invoiceItem.ExtendedData.GetAddress(Constants.ExtendedDataKeys.ShippingDestinationAddress);

                                csvExport["Ship Origin"] = FormatAddress(origin);
                                csvExport["Ship Destination"] = FormatAddress(destination);
                            }
                            else if (invoiceItem.LineItemType == LineItemType.Tax)
                            {
                                csvExport["Tax"] = invoiceItem.Name;
                                csvExport["Tax Quantity"] = invoiceItem.Quantity;
                                csvExport["Tax Price"] = invoiceItem.Price;
                            }
                            else if (invoiceItem.LineItemType == LineItemType.Discount)
                            {
                                csvExport["Coupon"] = invoiceItem.Name;
                                csvExport["Coupon Quantity"] = invoiceItem.Quantity;
                                csvExport["Coupon Price"] = invoiceItem.Price;
                            }
                        }
                    }
                }

                result = Request.CreateResponse(HttpStatusCode.OK);
                result.Content = new StreamContent(csvExport.ExportToStream());
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/csv");
                result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = "Orders.csv"
                };
            }
            catch (SystemException ex)
            {
                result = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
                return result;
            }

            return result;
        }