Mini Kabibi Habibi
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.SessionState
Imports System.Data.Linq
Imports System.Globalization
Imports System.Collections
Imports System.Configuration
Public Class DataProvider
Private Const DataProviderSessionKey As String = "DataProvider"
Private Shared ReadOnly Property Context() As IDictionary
Get
Return HttpContext.Current.Items
End Get
End Property
Public Shared ReadOnly Property Instance() As DataProvider
Get
If Context(DataProviderSessionKey) Is Nothing Then
Context(DataProviderSessionKey) = New DataProvider()
End If
Return CType(Context(DataProviderSessionKey), DataProvider)
End Get
End Property
Private Shared _isReadOnly As Nullable(Of Boolean)
Public Shared ReadOnly Property IsReadOnly() As Boolean
Get
If (Not _isReadOnly.HasValue) Then
_isReadOnly = ConfigurationManager.AppSettings("SiteMode").Equals("true", StringComparison.InvariantCultureIgnoreCase)
End If
Return _isReadOnly.Value
End Get
End Property
Private Sub New()
DataContext = New DataClassesDataContext()
End Sub
Public ReadOnly Property CurrentDate() As DateTime
Get
Return DateTime.Now
End Get
End Property
Private privateDataContext As DataClassesDataContext
Public Property DataContext() As DataClassesDataContext
Get
Return privateDataContext
End Get
Protected Set(ByVal value As DataClassesDataContext)
privateDataContext = value
End Set
End Property
Public Function SelectBudgets(Optional ByVal overViewOnly As Boolean = False) As IEnumerable(Of BudgetCategory)
Return _
From category In DataContext.BudgetCategories _
Where category.UsedInBudget AndAlso ((Not overViewOnly) OrElse category.ShowInOverview) _
Order By category.Title _
Select category
End Function
Public Function GetSpreadingExpenses(Optional ByVal take As Integer = 0, Optional ByVal orderByTotal As Boolean = True) As IEnumerable(Of BudgetCategory)
Return GetSpreading(BudgetCategoryType.Expenses, take, orderByTotal)
End Function
Public Function GetSpreadingIncome(Optional ByVal take As Integer = 0, Optional ByVal orderByTotal As Boolean = True) As IEnumerable(Of BudgetCategory)
Return GetSpreading(BudgetCategoryType.Income, take, orderByTotal)
End Function
Public Function GetSpreadingBusiness(Optional ByVal take As Integer = 0, Optional ByVal orderByTotal As Boolean = True) As IEnumerable(Of BudgetCategory)
Return GetSpreading(BudgetCategoryType.Business, take, orderByTotal)
End Function
Private Function GetSpreading(ByVal categoryType As BudgetCategoryType, ByVal take As Integer, ByVal orderByTotal As Boolean) As IEnumerable(Of BudgetCategory)
Dim query = ( _
From category In DataContext.BudgetCategories _
Where category.Type = categoryType _
Select category).ToList()
If take > 0 Then
Return query.OrderBy(Function(c) c.TotalAmount).Take(take)
Else
Return query
End If
End Function
Public Function GetCurrentMonthlyIncome() As Decimal
Dim transactions = ( _
From transaction In DataContext.Transactions _
Where transaction.Amount > 0 AndAlso transaction.Date.Month = CurrentDate.Month AndAlso transaction.Date.Year = CurrentDate.Year _
Select transaction.Amount)
If transactions.Any() Then
Return transactions.Sum()
Else
Return 0
End If
End Function
Public Function GetCurrentMonthlySpent() As Decimal
Return GetMonthlySpent(DateTime.Now)
End Function
Public Function GetCurrentMonthToPreviousMonthSpent() As Decimal
Dim prevMonthSpent As Decimal = GetMonthlySpent(DateTime.Now.AddMonths(-1))
Dim currentMonthSpent As Decimal = GetMonthlySpent(DateTime.Now)
Return (currentMonthSpent / prevMonthSpent)
End Function
Public Function GetAccounts() As IEnumerable(Of Account)
Return ( _
From accounts In DataContext.Accounts _
Select accounts).ToList()
End Function
Public Function GetAvailableAmount() As Decimal
Return DataContext.Transactions.Sum(Function(t) t.Amount)
End Function
Public Function SelectMonthlyUpcomingBills() As IEnumerable(Of UpcomingBill)
Dim monthlyBills As New List(Of UpcomingBill)()
Dim last = New DateTime(CurrentDate.Year, CurrentDate.Month, 1).AddMonths(1).AddDays(-1)
Dim bills = _
From bill In DataContext.UpcomingBills _
Where (bill.Date >= CurrentDate AndAlso bill.Date < last) OrElse (bill.RepeatType <> UpcomingBillRepeatType.NoRepeat AndAlso bill.Date < last) _
Select bill
For Each bill In bills
If bill.Date.Year = CurrentDate.Year AndAlso bill.Date.DayOfYear = CurrentDate.DayOfYear Then
monthlyBills.Add(bill)
Continue For
End If
Select Case bill.RepeatType
Case UpcomingBillRepeatType.NoRepeat
If bill.Date.Year = CurrentDate.Year AndAlso bill.Date.Month = CurrentDate.Month AndAlso bill.Date >= CurrentDate Then
monthlyBills.Add(bill)
End If
Case UpcomingBillRepeatType.Month
If bill.Date.Day >= CurrentDate.Day Then
monthlyBills.Add(bill)
End If
Case UpcomingBillRepeatType.Year
If bill.Date.Month = CurrentDate.Month AndAlso bill.Date.Day >= CurrentDate.Day Then
monthlyBills.Add(bill)
End If
End Select
Next bill
Return monthlyBills
End Function
Public Function GetSpentToBudget() As Decimal
Dim planned As Decimal = 0
Dim spent As Decimal = 0
For Each bc In GetBudgets()
If bc.Amount.HasValue AndAlso bc.Amount > 0 Then
planned += bc.Amount.Value
spent += bc.CurrentMonthAmount
End If
Next bc
If planned = 0 Then
Return 0
Else
Return spent / planned
End If
End Function
Private Function GetMonthlySpent(ByVal [date] As DateTime) As Decimal
Dim transactions = ( _
From transaction In DataContext.Transactions _
Where transaction.Amount < 0 AndAlso transaction.Date.Month = [date].Month AndAlso transaction.Date.Year = [date].Year _
Select transaction.Amount)
If transactions.Any() Then
Return transactions.Sum() * -1
Else
Return 0
End If
End Function
Public Function GetBudgets() As IEnumerable(Of BudgetCategory)
Return _
From budget In DataContext.BudgetCategories _
Where budget.UsedInBudget _
Order By budget.Title _
Select budget
End Function
Public Function GetNearestDateInThisMonth(ByVal repeateType As UpcomingBillRepeatType, ByVal [date] As DateTime) As DateTime
Select Case repeateType
Case UpcomingBillRepeatType.NoRepeat
Return [date]
Case UpcomingBillRepeatType.Month, UpcomingBillRepeatType.Year
Return New DateTime(CurrentDate.Year, CurrentDate.Month, [date].Day)
End Select
Return DateTime.MinValue
End Function
Public Function SelectNonBudgetCategories() As IEnumerable(Of BudgetCategory)
Return _
From category In DataContext.BudgetCategories _
Where (Not category.UsedInBudget) AndAlso category.Type = BudgetCategoryType.Expenses _
Select category
End Function
Public Sub AddCategoryToBudget(ByVal categoryID As Integer)
Dim category = DataContext.BudgetCategories.FirstOrDefault(Function(bc) bc.ID = categoryID)
If category IsNot Nothing Then
category.UsedInBudget = True
End If
DataContext.SubmitChanges()
End Sub
End Class
Public Module CurrencyHelper
Public ReadOnly CurrencyFormat As NumberFormatInfo = CreateCurrencyFormat()
Private Function CreateCurrencyFormat() As NumberFormatInfo
Dim usCulture = CultureInfo.CreateSpecificCulture("en-US")
Dim clonedNumbers = CType(usCulture.NumberFormat.Clone(), NumberFormatInfo)
clonedNumbers.CurrencyNegativePattern = 1
Return clonedNumbers
End Function
<System.Runtime.CompilerServices.Extension> _
Public Function AsCurrency(ByVal amount As Decimal) As String
Return amount.ToString("c", CurrencyFormat)
End Function
End Module
Partial Public Class BudgetCategory
Public ReadOnly Property TotalAmount() As Decimal
Get
Dim transactions = ( _
From transaction In Me.Transactions _
Select transaction)
If transactions.Any() Then
Return Math.Abs((transactions.Sum(Function(t) t.Amount)))
Else
Return Math.Abs((0))
End If
End Get
End Property
Public ReadOnly Property CurrentMonthAmount() As Decimal
Get
Dim transactions = ( _
From transaction In Me.Transactions _
Where transaction.Date.Month = DateTime.Now.Month AndAlso transaction.Date.Year = DateTime.Now.Year _
Select transaction)
If transactions.Any() Then
Return Math.Abs((transactions.Sum(Function(t) t.Amount)))
Else
Return Math.Abs((0))
End If
End Get
End Property
Public ReadOnly Property SpentAmountPercent() As Integer
Get
If (Not IsActiveBudget) Then
Return 0
End If
Return Decimal.ToInt32((CurrentMonthAmount / Math.Abs(Amount.Value)) * 100)
End Get
End Property
Public ReadOnly Property IsActiveBudget() As Boolean
Get
Return UsedInBudget AndAlso Amount.HasValue AndAlso Amount.Value > 0
End Get
End Property
End Class
Partial Public Class UpcomingBill
Public ReadOnly Property NearestBillDate() As DateTime
Get
Return DataProvider.Instance.GetNearestDateInThisMonth(RepeatType, [Date])
End Get
End Property
End Class