Пример #1
0
        /// <summary>
        /// Returns Product by id (key)
        /// GET /umbraco/Merchello/SettingsApi/GetAllSettings
        /// </summary>
        public SettingDisplay GetAllSettings()
        {
            var settings       = _storeSettingService.GetAll();
            var settingDisplay = new SettingDisplay();

            if (settings == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return(settingDisplay.ToStoreSettingDisplay(settings));
        }
        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 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);
        }
Пример #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));
        }
        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);
        }