Imports BVSoftware.BVC.Core Imports System.Globalization Public Class reports_affiliates Inherits BaseAdminPage Protected WithEvents lstAffiliates As System.Web.UI.WebControls.DropDownList Protected WithEvents btnShow As System.Web.UI.WebControls.ImageButton Protected WithEvents btnLast As System.Web.UI.WebControls.ImageButton Protected WithEvents btnNext As System.Web.UI.WebControls.ImageButton Protected WithEvents MetaDescriptionControl As BVSoftware.WebControls.MetaTag Protected WithEvents MetaKeywordsControl As BVSoftware.WebControls.MetaTag Protected WithEvents lstMonth As BVSoftware.WebControls.SelectDropDownList Protected WithEvents lstYear As BVSoftware.WebControls.SelectDropDownList Protected WithEvents lblFirst As System.Web.UI.WebControls.Label Protected WithEvents msg As BVSoftware.WebControls.WebPageMessage Protected WithEvents dgMain As System.Web.UI.WebControls.DataGrid #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load BVC2004Store.CheckThisPage(Security.RolePermission.LoginToAdmin, Security.RolePermission.AccessReportArea) If Not Page.IsPostBack Then MetaKeywordsControl.Content = WebAppSettings.MetaKeywords MetaDescriptionControl.Content = WebAppSettings.MetaDescription End If If Not Page.IsPostBack() Then FillAffiliates() SetUserControlValues() 'GenerateReport() End If End Sub Sub SetUserControlValues() Dim dDate As Date = Date.Today FillMonths() FillYears(dDate.Year) lstMonth.SelectByValue(dDate.Month) lstYear.SelectByValue(dDate.Year) End Sub Sub FillMonths() lstMonth.Items.Clear() For i As Integer = 1 To 12 Dim li As New ListItem li.Value = i li.Text = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(i) lstMonth.Items.Add(li) li = Nothing Next End Sub Sub FillYears(ByVal iStart As Integer) Dim iCount As Integer For iCount = iStart - 10 To iStart + 10 Dim li As New ListItem li.Value = iCount li.Text = iCount.ToString lstYear.Items.Add(li) li = Nothing Next End Sub Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnShow.Click GenerateReport() End Sub Sub FillAffiliates() lstAffiliates.DataSource = ContactServices.Affiliates.ToDataTable lstAffiliates.DataTextField = "DisplayName" lstAffiliates.DataValueField = "id" lstAffiliates.DataBind() lstAffiliates.Items.Insert(0, New ListItem("-- All Affiliates --", 0)) End Sub Private Sub GenerateReport() Me.lblFirst.Visible = False If lstAffiliates.SelectedValue = 0 Then dgMain.DataSource = ContactServices.Affiliates.ToDataTable Else Dim dt As New DataTable dt.Columns.Add("id", System.Type.GetType("System.Int32")) Dim dr As DataRow dr = dt.NewRow dr.Item("id") = lstAffiliates.SelectedValue dt.Rows.Add(dr) dgMain.DataSource = dt End If dgMain.DataBind() End Sub Private Sub dgList_Edit(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Dim affid As Integer = CType(sender, DataGrid).DataKeys(e.Item.ItemIndex) Response.Redirect("orders_details.aspx?id=" & affid) End Sub Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnLast.Click If lstMonth.SelectedItem.Value = 1 Then lstMonth.SelectByValue(12) Dim iYear As Integer = Convert.ToInt32(lstYear.SelectedItem.Value) iYear -= 1 lstYear.SelectByValue(iYear) Else Dim iMonth = Convert.ToInt32(lstMonth.SelectedItem.Value) iMonth -= 1 lstMonth.SelectByValue(iMonth) End If GenerateReport() End Sub Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnNext.Click If lstMonth.SelectedItem.Value = 12 Then lstMonth.SelectByValue(1) Dim iYear As Integer = Convert.ToInt32(lstYear.SelectedItem.Value) iYear += 1 lstYear.SelectByValue(iYear) Else Dim iMonth = Convert.ToInt32(lstMonth.SelectedItem.Value) iMonth += 1 lstMonth.SelectByValue(iMonth) End If GenerateReport() End Sub Private Sub dgMain_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgMain.ItemDataBound If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then Dim affid As Integer = dgMain.DataKeys(e.Item.ItemIndex) Dim dg As DataGrid = e.Item.FindControl("dgDetails") Dim dStartDate As Date = Date.Parse(lstMonth.SelectedItem.Value & "/01/" & lstYear.SelectedItem.Value) Dim dEndDate As Date = dStartDate.AddMonths(1) dEndDate = dEndDate.AddDays(-1) Dim dtOrders As DataTable = GetAffiliateOrders(affid, dStartDate, dEndDate) Dim _total As Double = 0.0 Dim _referrals As Integer = 0 Dim _sales As Integer = 0 Dim _commission As Double = 0.0 Dim com As String = "" Dim _conversion As Double = 0.0 For i As Integer = 0 To dtOrders.Rows.Count - 1 _total += dtOrders.Rows(i).Item("subtotal") Next _sales = dtOrders.Rows.Count Dim aff As Contacts.Affiliate = ContactServices.Affiliates.GetAffiliate(affid) If Not aff Is Nothing Then If aff.CommissionType = Contacts.AffiliateCommissionType.FlatRateCommission Then _commission = Math.Round(aff.CommissionAmount * CType(_sales, Double), 2) com = String.Format("{0:c}", aff.CommissionAmount) & " per" Else _commission = Math.Round((aff.CommissionAmount / CType(100, Double)) * _total, 2) com = String.Format("{0:p}", (aff.CommissionAmount / CType(100, Double))) End If Dim affiliateName As Label = e.Item.FindControl("affiliateName") If Not affiliateName Is Nothing Then affiliateName.Text = aff.DisplayName End If End If aff = Nothing If Not dg Is Nothing Then dg.DataSource = dtOrders dg.DataBind() End If Dim dtRefs As DataTable = ContactServices.Affiliates.GetReferrals(affid, dStartDate, dEndDate) If Not dtRefs Is Nothing Then _referrals = dtRefs.Rows.Count End If dtRefs = Nothing _conversion = CType(_sales, Double) / CType(_referrals, Double) Dim referrals As Label = e.Item.FindControl("referrals") If Not referrals Is Nothing Then referrals.Text = _referrals End If Dim sales As Label = e.Item.FindControl("sales") If Not sales Is Nothing Then sales.Text = _sales End If Dim conversion As Label = e.Item.FindControl("conversion") If Not conversion Is Nothing Then conversion.Text = String.Format("{0:p}", _conversion) End If Dim commission As Label = e.Item.FindControl("commission") If Not commission Is Nothing Then commission.Text = com & " = " & String.Format("{0:c}", _commission) End If Dim openDiv As Literal = e.Item.FindControl("openDiv") Dim closeDiv As Literal = e.Item.FindControl("closeDiv") If (Not openDiv Is Nothing) And (Not closeDiv Is Nothing) Then openDiv.Text = "Details
" closeDiv.Text = "
" End If End If End Sub Private Function GetAffiliateOrders(ByVal affiliateID As Integer, ByVal startDate As Date, ByVal endDate As Date) As DataTable Return OrderServices.OrderHistory.FindOrdersByAffiliate(affiliateID, 0, startDate.ToShortDateString, endDate.ToShortDateString) End Function End Class