Product Performance Mod

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
User avatar
William_firefold
Commander (CMDR)
Commander (CMDR)
Posts: 186
Joined: Fri Aug 01, 2008 8:38 am

Product Performance Mod

Post by William_firefold » Mon Apr 13, 2009 2:07 pm

I wrote a control that uses the product-breakdown-summary to generate a month to month report of the sales of a particular product. The code works perfectly for products that are active, and anyone here is free to use it.

There is one small problem I have found with it though, and it seems to be a problem with the way ReportDataSource.GetProductBreakdownSummary gets the results. If a product has no sales during the selected time-period, a report is not even generated for it. My code covers up this error most of the time, but if it has no sales in the LAST 30 days, it causes my months to be off by 1.
If anyone can use my code to help me with this problem it could be useful to others.


Image


How to use the code:


On Admin/products/EditProduct.aspx:
At the top:

Code: Select all

<%@ Register Src="~/ConLib/productPerformance.ascx" TagName="Perform" TagPrefix="uc" %>
At the bottom:

Code: Select all

<uc:Perform id="performanceControl" runat="server" />
Save this code as a new file in conlib called productPerformance.ascx:

Code: Select all

<%@ Control Language="C#" ClassName="productPerformance" %>
<script runat="server">
private int _ProductId = 0;
int monthSelect=1;
int quantityRoller=0;
LSDecimal amountRoller=0;
List<ProductBreakdownSummary> cleanreport=new List<ProductBreakdownSummary>();

    protected void Page_Init(object sender, EventArgs e)
    {
		_ProductId = PageHelper.GetProductId();
	}	
    protected void ProcessButton_Click(object sender, EventArgs e)
    {
		BindGrid();
    }
    protected void BindGrid()
    {
        DateTime toDate =DateTime.Today;
		for(int i=0;i<=3;i++){
		DateTime theDay =new DateTime(DateTime.Today.Year, (DateTime.Today.Month)-i, 1);
        List<ProductBreakdownSummary> breakdownReport = ReportDataSource.GetProductBreakdownSummary(theDay.Date, toDate.Date, 0, "Quantity");
        foreach (ProductBreakdownSummary pbs in breakdownReport)
        {
			if(pbs.ProductId==_ProductId){
			pbs.Quantity=pbs.Quantity-quantityRoller;
			quantityRoller+=pbs.Quantity;			
			pbs.Amount=pbs.Amount-amountRoller;
			amountRoller+=pbs.Amount;			
			cleanreport.Add(pbs);
			}
		}
		}
        ProductBreakdownGrid.DataSource = cleanreport;
        ProductBreakdownGrid.DataBind();
    }
</script>
    <ajax:UpdatePanel ID="ReportAjax" runat="server">
        <ContentTemplate>
            <div class="pageHeader">
                <div class="caption">
                    <h1>
                        <asp:Localize ID="Caption" runat="server" Text="Product Performance"></asp:Localize>
                   </h1>
                </div>
            </div>
	<asp:Label ID="msg" runat="server" Text="Sales figures are represented as a total for each month. Month 0 is the current month."></asp:Label>
            <table align="left" class="form" cellpadding="0" cellspacing="0" border="0" width="100%">
                <tr>
                    <td>
                        <div style="text-align: left; vertical-align: middle">
                            <asp:Button ID="ProcessButton" runat="server" Text="Generate Report" OnClick="ProcessButton_Click" />
                        </div>
                    </td>
                </tr>
                <tr>
                    <td class="dataSheet" >
                        <asp:GridView ID="ProductBreakdownGrid" runat="server" AutoGenerateColumns="False" PageSize="20" Width="100%" SkinID="PagedList">
                            <Columns>
								<asp:TemplateField HeaderText="Months">
									<ItemTemplate>
										<%# -Container.DataItemIndex %>
									</ItemTemplate>
								</asp:TemplateField>
                                <asp:TemplateField HeaderText="Name">
                                    <HeaderStyle HorizontalAlign="Left" />
                                    <ItemTemplate>
                                        <asp:Label ID="CustomerLabel" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Sku">
                                    <HeaderStyle HorizontalAlign="Left" />
                                    <ItemTemplate>
                                        <asp:Label ID="Label0" runat="server" Text='<%# Eval("Sku", "{0}") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Vendor">
                                    <HeaderStyle HorizontalAlign="Left" />
                                    <ItemTemplate>
                                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("Vendor", "{0:lc}") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Quantity">
                                    <HeaderStyle HorizontalAlign="Left" />
                                    <ItemTemplate>
                                        <asp:Label ID="QuantityLabel" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Amount">
                                    <HeaderStyle HorizontalAlign="Left" />
                                    <ItemTemplate>
                                        <asp:Label ID="AmountLabel" runat="server" Text='<%# string.Format("{0:lc}", Eval("Amount")) %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                            <EmptyDataTemplate>
                                <div class="emptyResult">
                                    <asp:Label ID="EmptyResultsMessage" runat="server" Text="There are no results"></asp:Label>
                                </div>
                            </EmptyDataTemplate>
                        </asp:GridView>
                    </td>
                </tr>
            </table>
        </ContentTemplate>
    </ajax:UpdatePanel>

User avatar
compunerdy
Admiral (ADM)
Admiral (ADM)
Posts: 1283
Joined: Sun Nov 18, 2007 3:55 pm

Re: Product Performance Mod

Post by compunerdy » Tue May 05, 2009 8:51 pm

You need to put the

Code: Select all

<uc:Perform id="performanceControl" runat="server" />
before the

Code: Select all

</asp:Content>
tag..other than that it worked great...thanks!!

User avatar
William_firefold
Commander (CMDR)
Commander (CMDR)
Posts: 186
Joined: Fri Aug 01, 2008 8:38 am

Re: Product Performance Mod

Post by William_firefold » Wed Jan 20, 2010 8:07 am

My code broke after the new year because it is trying to look at month -1,-2,-3, but it didnt circle back around to 12. I have that problem fixed now, but I havent done extensive testing with this code. Let me know if the figures are off.

Code: Select all

<%@ Control Language="C#" ClassName="productPerformance" %>

<script runat="server">
private int _ProductId = 0;
int monthSelect=1;
int quantityRoller=0;
LSDecimal amountRoller=0;

List<ProductBreakdownSummary> cleanreport=new List<ProductBreakdownSummary>();

    protected void Page_Init(object sender, EventArgs e)
    {
		_ProductId = PageHelper.GetProductId();
	}
	
    protected void ProcessButton_Click(object sender, EventArgs e)
    {
		BindGrid();
    }
    protected void BindGrid()
    {
        DateTime toDate =DateTime.Today;
		
		for(int i=0;i<4;i++){
		bool firstreport=false;
		
		int monthcount=DateTime.Today.Month;
		int yearcount=0;
		if(DateTime.Today.Month-i<=0){
		yearcount=1;
		monthcount=13-i;
		}
		DateTime theDay =new DateTime((DateTime.Today.Year)-yearcount, monthcount, 1);

        List<ProductBreakdownSummary> breakdownReport = ReportDataSource.GetProductBreakdownSummary(theDay.Date, toDate.Date, 0, "Quantity");

        foreach (ProductBreakdownSummary pbs in breakdownReport)
        {
			if(pbs.ProductId==_ProductId){
			pbs.Quantity=pbs.Quantity-quantityRoller;
			quantityRoller+=pbs.Quantity;
			
			pbs.Amount=pbs.Amount-amountRoller;
			amountRoller+=pbs.Amount;
			
			cleanreport.Add(pbs);
			firstreport=true;
			}
		}
		if(i==0&& !firstreport){
			ProductBreakdownSummary pbster=new ProductBreakdownSummary();
			pbster.Quantity=0;
			pbster.Amount=0;
			pbster.ProductId=_ProductId;
			cleanreport.Add(pbster);
			}
		}
        ProductBreakdownGrid.DataSource = cleanreport;
        ProductBreakdownGrid.DataBind();
    }
</script>
    <ajax:UpdatePanel ID="ReportAjax" runat="server">
        <ContentTemplate>
            <div class="pageHeader">
                <div class="caption">
                    <h1>
                        <asp:Localize ID="Caption" runat="server" Text="Product Performance"></asp:Localize>
                   </h1>
                </div>
            </div>
	<asp:Label ID="msg" runat="server" Text="Sales figures are represented as a total for each month. Month 0 is the current month."></asp:Label>
            <table align="left" class="form" cellpadding="0" cellspacing="0" border="0" width="100%">
                <tr>
                    <td>
                        <div style="text-align: left; vertical-align: middle">
                            <asp:Button ID="ProcessButton" runat="server" Text="Generate Report" OnClick="ProcessButton_Click" />
                        </div>
                    </td>
                </tr>
                <tr>
                    <td class="dataSheet" >
                        <asp:GridView ID="ProductBreakdownGrid" runat="server" AutoGenerateColumns="False" PageSize="20" Width="100%" SkinID="PagedList">
                            <Columns>
								<asp:TemplateField HeaderText="Months">
									<ItemTemplate>
										<%# -Container.DataItemIndex %>
									</ItemTemplate>
								</asp:TemplateField>
                                <asp:TemplateField HeaderText="Name">
                                    <HeaderStyle HorizontalAlign="Left" />
                                    <ItemTemplate>
                                        <asp:Label ID="CustomerLabel" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Sku">
                                    <HeaderStyle HorizontalAlign="Left" />
                                    <ItemTemplate>
                                        <asp:Label ID="Label0" runat="server" Text='<%# Eval("Sku", "{0}") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Vendor">
                                    <HeaderStyle HorizontalAlign="Left" />
                                    <ItemTemplate>
                                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("Vendor", "{0:lc}") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Quantity">
                                    <HeaderStyle HorizontalAlign="Left" />
                                    <ItemTemplate>
                                        <asp:Label ID="QuantityLabel" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Amount">
                                    <HeaderStyle HorizontalAlign="Left" />
                                    <ItemTemplate>
                                        <asp:Label ID="AmountLabel" runat="server" Text='<%# string.Format("{0:lc}", Eval("Amount")) %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                            <EmptyDataTemplate>
                                <div class="emptyResult">
                                    <asp:Label ID="EmptyResultsMessage" runat="server" Text="There are no results"></asp:Label>
                                </div>
                            </EmptyDataTemplate>
                        </asp:GridView>
                    </td>
                </tr>
            </table>
        </ContentTemplate>
    </ajax:UpdatePanel>

Post Reply