Esempio n. 1
0
        protected void ExportBySalesButton_Click(Object sender, EventArgs e)
        {
            GenericExportManager <CommerceBuilder.Reporting.ProductSummary> exportManager = GenericExportManager <CommerceBuilder.Reporting.ProductSummary> .Instance;
            GenericExportOptions <CommerceBuilder.Reporting.ProductSummary> options       = new GenericExportOptions <CommerceBuilder.Reporting.ProductSummary>();

            options.CsvFields = new string[] { "ProductId", "Name", "TotalPrice", "TotalQuantity" };

            CacheWrapper cacheWrapper = Cache[_SalesCacheKey] as CacheWrapper;
            Dictionary <string, object> salesData;
            IList <CommerceBuilder.Reporting.ProductSummary> productSales = null;
            DateTime localNow   = LocaleHelper.LocalNow;
            DateTime last60Days = (new DateTime(localNow.Year, localNow.Month, localNow.Day, 0, 0, 0)).AddDays(-60);

            if (cacheWrapper == null)
            {
                productSales = ReportDataSource.GetSalesByProduct(last60Days, DateTime.MaxValue, 8, 0, "TotalPrice DESC");

                //CACHE THE DATA
                salesData = new Dictionary <string, object>();
                salesData["DataSource"] = productSales;
                cacheWrapper            = new CacheWrapper(salesData);
                Cache.Remove(_SalesCacheKey);
                Cache.Add(_SalesCacheKey, cacheWrapper, null, DateTime.UtcNow.AddMinutes(5).AddSeconds(-1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
            }
            else
            {
                //USE CACHED VALUES
                salesData    = (Dictionary <string, object>)cacheWrapper.CacheValue;
                productSales = (List <CommerceBuilder.Reporting.ProductSummary>)salesData["DataSource"];
            }

            options.ExportData = productSales;
            options.FileTag    = string.Format("POPULAR_PRODUCTS_BY_SALES((from_{0}_to_{1})", localNow.ToShortDateString(), last60Days.ToShortDateString());
            exportManager.BeginExport(options);
        }
        private void initSalesChart()
        {
            string       cacheKey     = "0AD3A3DA-15F1-4f43-82A3-C0AC3262399D";
            CacheWrapper cacheWrapper = Cache[cacheKey] as CacheWrapper;
            Dictionary <string, object> salesData;

            if (cacheWrapper == null)
            {
                //GET SALES
                DateTime localNow   = LocaleHelper.LocalNow;
                DateTime last60Days = (new DateTime(localNow.Year, localNow.Month, localNow.Day, 0, 0, 0)).AddDays(-60);
                IList <ProductSummary> productSales = ReportDataSource.GetSalesByProduct(last60Days, DateTime.MaxValue, 8, 0, "TotalPrice DESC");
                if (productSales.Count > 0)
                {
                    SalesChart1.Series["Sales"].Points.Clear();
                    for (int i = 0; i < productSales.Count; i++)
                    {
                        int       roundedTotal = (int)Math.Round((double)productSales[i].TotalPrice, 0);
                        DataPoint point        = new DataPoint(SalesChart1.Series["Sales"]);
                        point.SetValueXY(productSales[i].Name, new object[] { roundedTotal });
                        SalesChart1.Series["Sales"].Points.Add(point);
                    }
                    SalesChart1.DataBind();

                    //BIND THE DATA GRID
                    SalesGrid.DataSource = productSales;
                    SalesGrid.DataBind();

                    //CACHE THE DATA
                    salesData = new Dictionary <string, object>();
                    salesData["DataSource"] = productSales;
                    cacheWrapper            = new CacheWrapper(salesData);
                    Cache.Remove(cacheKey);
                    Cache.Add(cacheKey, cacheWrapper, null, LocaleHelper.LocalNow.AddMinutes(5).AddSeconds(-1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
                }
                else
                {
                    //NO PRODUCTS HAVE BEEN SOLD YET
                    Control container = SalesChart1.Parent;
                    container.Controls.Clear();
                    Panel noViewsPanel = new Panel();
                    noViewsPanel.CssClass = "emptyData";
                    Label noViewsMessage = new Label();
                    noViewsMessage.Text = "No products have been sold yet.";
                    noViewsPanel.Controls.Add(noViewsMessage);
                    container.Controls.Add(noViewsPanel);

                    // REMOVE SALES DATA TAB
                    Tabs.Tabs[1].Visible = false;
                }
            }
            else
            {
                //USE CACHED VALUES
                salesData = (Dictionary <string, object>)cacheWrapper.CacheValue;
                IList <ProductSummary> productSales = (List <ProductSummary>)salesData["DataSource"];
                SalesChart1.Series["Sales"].Points.Clear();
                for (int i = 0; i < productSales.Count; i++)
                {
                    int       roundedTotal = (int)Math.Round((double)productSales[i].TotalPrice, 0);
                    DataPoint point        = new DataPoint(SalesChart1.Series["Sales"]);
                    point.SetValueXY(productSales[i].Name, new object[] { roundedTotal });
                    SalesChart1.Series["Sales"].Points.Add(point);
                }
                SalesChart1.DataBind();
                SalesGrid.DataSource = productSales;
                SalesGrid.DataBind();
            }
        }