Page 1 of 1

Adding fields to the main inventory report

Posted: Mon Dec 01, 2008 11:26 am
by tntmjr
I found a post that added the sku to the Low inventory report :
viewtopic.php?f=44&t=8713&hilit=inventory+report..
I was wondering would this work on the Inventory report under Manage and what other fields are available. I would like to see Prices, Manufacturer, categories, featured, vendor, visibility and whatever else can be seen. If they are available how do I access them.

Re: Adding fields to the main inventory report

Posted: Mon Dec 01, 2008 12:22 pm
by mazhar
Yes it will work. Use it in very same as described in that post.
I would like to see Prices, Manufacturer, categories, featured, vendor, visibility and whatever else can be seen. If they are available how do I access them.
You can create other function similarly like the one defiend for Sku. For example for vendor you can define the GetVendor and in function load the product and return its vendor.

Code: Select all

public string GetVendor(Object dataItem)
    {
        ProductInventoryDetail detail = (ProductInventoryDetail)dataItem;
        Product product = ProductDataSource.Load(detail.ProductId);
        return product.Vendor.Name;
    }
For manufacturer you can use

Code: Select all

return product.Manufacturer.Name;
For price you can use

Code: Select all

return product.Price.ToString();
For featured product

Code: Select all

if(product.IsFeatured)
            return "Featured";
else
    return string.Empty;
For visibility you can use

Code: Select all

product.Visibility.ToString();

Re: Adding fields to the main inventory report

Posted: Tue Dec 02, 2008 11:45 am
by tntmjr
Thanks Mazhar,

I did have to modify the <asp:HyperLink ID="ProductLink" to have unique id's so the sorting would work but otherwise this was pretty simple. I also added a couple more columns. Now I have to figure out how to get the Products category in there and I'm all set. We'll also be working on a way to send this report to excel (if anyone has any ideas hit me up).

Here is the modified code for anyone else that might have a need for it.

Code: Select all

<%@ Page Language="C#" MasterPageFile="~/Admin/Admin.master" Title="Manage Inventory" Inherits="CommerceBuilder.Web.UI.AbleCommerceAdminPage" %>
<%@ Register TagPrefix="ComponentArt" Namespace="ComponentArt.Web.UI" Assembly="ComponentArt.Web.UI" %>
<%@ Register Assembly="CommerceBuilder.Web" Namespace="CommerceBuilder.Web.UI.WebControls" TagPrefix="cb" %>
<script runat="server">

    protected string GetName(object dataItem)
    {
        ProductInventoryDetail detail = (ProductInventoryDetail)dataItem;
        if (string.IsNullOrEmpty(detail.VariantName)) return detail.Name;
        return string.Format("{0} ({1})", detail.Name, detail.VariantName);
    }
	
	
	public string GetSku(Object dataItem) 
    {
        ProductInventoryDetail detail = (ProductInventoryDetail)dataItem;
        Product product = ProductDataSource.Load(detail.ProductId);
        return product.Sku;
    }
	
	public string GetPrice(Object dataItem) 
    {
        ProductInventoryDetail detail = (ProductInventoryDetail)dataItem;
        Product product = ProductDataSource.Load(detail.ProductId);
        return product.Price.ToString();
    }
	
	
	public string GetMsrp(Object dataItem) 
    {
        ProductInventoryDetail detail = (ProductInventoryDetail)dataItem;
        Product product = ProductDataSource.Load(detail.ProductId);
        return product.MSRP.ToString();
    }
	
	public string GetManu(Object dataItem) 
    {
        ProductInventoryDetail detail = (ProductInventoryDetail)dataItem;
        Product product = ProductDataSource.Load(detail.ProductId);
        return product.Manufacturer.Name;
    }
	
	public string GetFeat(Object dataItem) 
    {
        ProductInventoryDetail detail = (ProductInventoryDetail)dataItem;
        Product product = ProductDataSource.Load(detail.ProductId);
        if(product.IsFeatured)
            return "Featured";
else
    return string.Empty;
    }
	
	public string GetVis(Object dataItem) 
    {
        ProductInventoryDetail detail = (ProductInventoryDetail)dataItem;
        Product product = ProductDataSource.Load(detail.ProductId);
        return product.Visibility.ToString();
    }	
	
	public string GetCost(Object dataItem) 
    {
        ProductInventoryDetail detail = (ProductInventoryDetail)dataItem;
        Product product = ProductDataSource.Load(detail.ProductId);
        return product.CostOfGoods.ToString();
    }	
	

    protected void InventoryGrid_DataBound(object sender, EventArgs e)
    {
        SaveButton.Visible = (InventoryGrid.Rows.Count > 0);
        InventoryGrid.GridLines = (SaveButton.Visible) ? GridLines.Both : GridLines.None;
    }

    private int GetControlValue(GridViewRow row, string controlId)
    {
        TextBox tb = row.FindControl(controlId) as TextBox;
        if (tb != null)
        {
            return AlwaysConvert.ToInt(tb.Text);
        }
        return 0;
    }    

    protected void SaveButton_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in InventoryGrid.Rows)
        {
			int dataItemIndex = row.DataItemIndex;
			dataItemIndex = (dataItemIndex - (InventoryGrid.PageSize * InventoryGrid.PageIndex));
            int productId = (int)InventoryGrid.DataKeys[dataItemIndex].Values[0];
            int productVariantId = AlwaysConvert.ToInt(InventoryGrid.DataKeys[dataItemIndex].Values[1].ToString());
            int inStock = GetControlValue(row, "InStock");
            int lowStock = GetControlValue(row, "LowStock");
            if (productVariantId.Equals(0))
            {
                Product product = ProductDataSource.Load(productId);
                product.InStock = inStock;
                product.InStockWarningLevel = lowStock;
                product.Save();
            }
            else
            {
                ProductVariant variant = new ProductVariant();
                variant.Load(productVariantId);
                variant.InStock = inStock;
                variant.InStockWarningLevel = lowStock;
                variant.Save();
            }
        }
        SavedMessage.Text = string.Format(SavedMessage.Text, DateTime.Now);
        SavedMessage.Visible = true;
        InventoryGrid.DataBind();
    }

    protected void GoButton_Click(object sender, EventArgs e)
    {
        InventoryGrid.DataBind();
    }
</script>

<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" Runat="Server">
   
<ajax:UpdatePanel ID="ReportAjax" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
	    <div class="pageHeader">
	    	<div class="caption">
	    		<h1><asp:Localize ID="Localize1" runat="server" Text="Manage Product Inventory"></asp:Localize></h1>
	    	</div>
	    </div>
		<table cellpadding="2" cellspacing="0" class="innerLayout">
            <tr class="noPrint">
                <td style="padding:10px 20px 0px 20px;">
                    <asp:Label ID="MaxInStockLabel" runat="server" Text="Show stock levels at or below:" SkinID="FieldHeader"></asp:Label>
                    <asp:TextBox ID="MaxInStock" runat="server" Text="0" Columns="4"></asp:TextBox>
                    <asp:Button ID="GoButton" runat="server" Text="Go" OnClick="GoButton_Click" /><br /><br />
                    <asp:Label ID="SavedMessage" runat="server" Text="Data saved at {0:g}" EnableViewState="false" Visible="false" SkinID="GoodCondition"></asp:Label>
                 </td>
			</tr>            
            <tr>
                 <td class="itemList">
                    <cb:SortedGridView ID="InventoryGrid" runat="server" AutoGenerateColumns="False" DataSourceID="InventoryDs" DataKeyNames="ProductId,ProductVariantId"
                    DefaultSortExpression="Name" AllowPaging="True" AllowSorting="true" PageSize="20" 
                    CellPadding="4" RowStyle-CssClass="odd" AlternatingRowStyle-CssClass="even" 
                    OnDataBound="InventoryGrid_DataBound" SkinID="Summary" Width="100%">
                    <Columns>
                       		<asp:TemplateField HeaderText="Name" SortExpression="Name">
                            <ItemTemplate>
                                <asp:HyperLink ID="ProductLink" runat="server" Text='<%# GetName(Container.DataItem) %>' NavigateUrl='<%#Eval("ProductId", "../Products/EditProduct.aspx?ProductId={0}")%>'></asp:HyperLink>
                            </ItemTemplate>                    
                                       <HeaderStyle HorizontalAlign="Left" />
                        	</asp:TemplateField>
                        
                        
                           <asp:TemplateField HeaderText="Sku" >
                                <ItemTemplate>
                                    <asp:HyperLink ID="ProductLink1" runat="server" Text='<%# GetSku(Container.DataItem) %>' NavigateUrl='<%#Eval("ProductId", "../Products/EditProduct.aspx?ProductId={0}")%>'></asp:HyperLink>
                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:TemplateField>

                  			<asp:TemplateField HeaderText="Cost" >
                                <ItemTemplate>
                                    <asp:HyperLink ID="ProductLink7" runat="server" Text='<%# GetCost(Container.DataItem) %>' NavigateUrl='<%#Eval("ProductId", "../Products/EditProduct.aspx?ProductId={0}")%>'></asp:HyperLink>
                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:TemplateField>
                            
 							<asp:TemplateField HeaderText="Price" >
                                <ItemTemplate>
                                    <asp:HyperLink ID="ProductLink2" runat="server" Text='<%# GetPrice(Container.DataItem) %>' NavigateUrl='<%#Eval("ProductId", "../Products/EditProduct.aspx?ProductId={0}")%>'></asp:HyperLink>
                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:TemplateField>
                            
                            
                            <asp:TemplateField HeaderText="MSRP" >
                                <ItemTemplate>
                                    <asp:HyperLink ID="ProductLink6" runat="server" Text='<%# GetMsrp(Container.DataItem) %>' NavigateUrl='<%#Eval("ProductId", "../Products/EditProduct.aspx?ProductId={0}")%>'></asp:HyperLink>
                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:TemplateField>
                            
                            
                             <asp:TemplateField HeaderText="Manufacturer" >
                                <ItemTemplate>
                                    <asp:HyperLink ID="ProductLink3" runat="server" Text='<%# GetManu(Container.DataItem) %>' NavigateUrl='<%#Eval("ProductId", "../Products/EditProduct.aspx?ProductId={0}")%>'></asp:HyperLink>
                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:TemplateField>
                            
                            <asp:TemplateField HeaderText="Featured" >
                                <ItemTemplate>
                                    <asp:HyperLink ID="ProductLink4" runat="server" Text='<%# GetFeat(Container.DataItem) %>' NavigateUrl='<%#Eval("ProductId", "../Products/EditProduct.aspx?ProductId={0}")%>'></asp:HyperLink>
                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:TemplateField>
                            
                            <asp:TemplateField HeaderText="Visibility" >
                                <ItemTemplate>
                                    <asp:HyperLink ID="ProductLink5" runat="server" Text='<%# GetVis(Container.DataItem) %>' NavigateUrl='<%#Eval("ProductId", "../Products/EditProduct.aspx?ProductId={0}")%>'></asp:HyperLink>
                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:TemplateField>
                            
                                      

                        
                        
                        <asp:TemplateField HeaderText="In Stock" SortExpression="InStock">
                            <ItemStyle HorizontalAlign="Center" />
                            <ItemTemplate>
                                <asp:TextBox ID="InStock" runat="server" Text='<%# Eval("InStock") %>' Columns="4"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Low Stock">
                            <ItemStyle HorizontalAlign="Center" />
                            <ItemTemplate>
                                <asp:TextBox ID="LowStock" runat="server" Text='<%# Eval("InStockWarningLevel") %>' Columns="4"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <PagerStyle CssClass="paging" HorizontalAlign="right" />
                    <PagerSettings NextPageText="»" PreviousPageText="«" />
                    <EmptyDataTemplate>
                        <div class="emptyResult_FromCustomFile">
                            <asp:Label ID="EmptyResultsMessage" runat="server" Text="There are no items currently at or below the indicated stock level."></asp:Label>
                        </div>
                    </EmptyDataTemplate>
                    </cb:SortedGridView>
                    <br /><asp:Button ID="SaveButton" runat="server" Text="Save" OnClick="SaveButton_Click" />
                </td>
            </tr>
        </table>
    </ContentTemplate>
</ajax:UpdatePanel>
        
    <asp:ObjectDataSource ID="InventoryDs" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetProductInventory" 
        TypeName="CommerceBuilder.Reporting.ProductInventoryDataSource" SortParameterName="sortExpression" EnablePaging="true" 
        SelectCountMethod="GetProductInventoryCount">
        <SelectParameters>
            <asp:ControlParameter ControlID="MaxInStock" Name="maxInStock" PropertyName="Text" Type="Int32" />
        </SelectParameters>
    </asp:ObjectDataSource>
</asp:Content>

Thanks again Mazhar. You have all the answers!!!

Re: Adding fields to the main inventory report

Posted: Tue Dec 02, 2008 11:51 am
by mazhar
that's great. Thanks for sharing the code

Re: Adding fields to the main inventory report

Posted: Wed Apr 08, 2009 8:44 am
by niall08
Is there any way to show the Variant SKU instead of the product Sku (where applicable) in this inventory report?

Re: Adding fields to the main inventory report

Posted: Wed Apr 08, 2009 9:02 am
by mazhar
Its possible. You can try following code.

Code: Select all

 public string GetSku(Object dataItem)
    {
        ProductInventoryDetail detail = (ProductInventoryDetail)dataItem;
        if (detail.ProductVariantId > 0)
        {
            ProductVariant productVariant = ProductVariantDataSource.Load(detail.ProductVariantId);
            if (!string.IsNullOrEmpty(productVariant.Sku))
                return productVariant.Sku;
        }
        
        Product product = ProductDataSource.Load(detail.ProductId);
        return product.Sku;
    }