diff --git a/RMData/dbo/Stored Procedures/spProductGetAll.sql b/RMData/dbo/Stored Procedures/spProductGetAll.sql index d0fe2cc..d4331a0 100644 --- a/RMData/dbo/Stored Procedures/spProductGetAll.sql +++ b/RMData/dbo/Stored Procedures/spProductGetAll.sql @@ -3,7 +3,7 @@ AS BEGIN SET NOCOUNT ON; - select Id, ProductName, [Description], RetailPrice, QuantityInStock + select Id, ProductName, [Description], RetailPrice, QuantityInStock, IsTaxable from [dbo].[Product] order by ProductName; END \ No newline at end of file diff --git a/RMData/dbo/Tables/Product.sql b/RMData/dbo/Tables/Product.sql index 61dada0..c77925f 100644 --- a/RMData/dbo/Tables/Product.sql +++ b/RMData/dbo/Tables/Product.sql @@ -6,5 +6,6 @@ [RetailPrice] MONEY NOT NULL, [QuantityInStock] INT NOT NULL DEFAULT 1, [CreatedDate] DATETIME2 NOT NULL DEFAULT getutcdate(), - [LastModified] DATETIME2 NOT NULL DEFAULT getutcdate() + [LastModified] DATETIME2 NOT NULL DEFAULT getutcdate(), + [IsTaxable] BIT NOT NULL DEFAULT 1 ) diff --git a/RMDataManagerLibrary/Models/ProductModel.cs b/RMDataManagerLibrary/Models/ProductModel.cs index eba19f7..671d8f8 100644 --- a/RMDataManagerLibrary/Models/ProductModel.cs +++ b/RMDataManagerLibrary/Models/ProductModel.cs @@ -12,5 +12,6 @@ namespace RMDataManagerLibrary.Models public string Description { get; set; } public decimal RetailPrice { get; set; } public int QuantityInStock { get; set; } + public bool IsTaxable { get; set; } } } \ No newline at end of file diff --git a/RMWPFInterfaceLibrary/Helpers/ConfigHelper.cs b/RMWPFInterfaceLibrary/Helpers/ConfigHelper.cs new file mode 100644 index 0000000..9449ba0 --- /dev/null +++ b/RMWPFInterfaceLibrary/Helpers/ConfigHelper.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RMWPFInterfaceLibrary.Helpers +{ + public class ConfigHelper : IConfigHelper + { + public decimal GetTaxRate() + { + string rateText = ConfigurationManager.AppSettings["taxRate"]; + + bool isValidTaxRate = Decimal.TryParse(rateText, out decimal output); + + if (isValidTaxRate == false) + { + throw new ConfigurationErrorsException("The taxRate is not setup properly"); + } + + return output; + } + } +} diff --git a/RMWPFInterfaceLibrary/Helpers/IConfigHelper.cs b/RMWPFInterfaceLibrary/Helpers/IConfigHelper.cs new file mode 100644 index 0000000..e0c120d --- /dev/null +++ b/RMWPFInterfaceLibrary/Helpers/IConfigHelper.cs @@ -0,0 +1,7 @@ +namespace RMWPFInterfaceLibrary.Helpers +{ + public interface IConfigHelper + { + decimal GetTaxRate(); + } +} \ No newline at end of file diff --git a/RMWPFInterfaceLibrary/Models/ProductModel.cs b/RMWPFInterfaceLibrary/Models/ProductModel.cs index 2c26c94..1c1137c 100644 --- a/RMWPFInterfaceLibrary/Models/ProductModel.cs +++ b/RMWPFInterfaceLibrary/Models/ProductModel.cs @@ -13,5 +13,6 @@ namespace RMWPFInterfaceLibrary.Models public string Description { get; set; } public decimal RetailPrice { get; set; } public int QuantityInStock { get; set; } + public bool IsTaxable { get; set; } } } diff --git a/RMWPFInterfaceLibrary/RMWPFInterfaceLibrary.csproj b/RMWPFInterfaceLibrary/RMWPFInterfaceLibrary.csproj index b64de7e..3be943e 100644 --- a/RMWPFInterfaceLibrary/RMWPFInterfaceLibrary.csproj +++ b/RMWPFInterfaceLibrary/RMWPFInterfaceLibrary.csproj @@ -52,6 +52,8 @@ + + diff --git a/RMWPFUserInterface/App.config b/RMWPFUserInterface/App.config index e3bc1cf..1e8a75b 100644 --- a/RMWPFUserInterface/App.config +++ b/RMWPFUserInterface/App.config @@ -2,6 +2,7 @@ + diff --git a/RMWPFUserInterface/BootStrapper.cs b/RMWPFUserInterface/BootStrapper.cs index 094cecf..c568307 100644 --- a/RMWPFUserInterface/BootStrapper.cs +++ b/RMWPFUserInterface/BootStrapper.cs @@ -1,5 +1,6 @@ using Caliburn.Micro; using RMWPFInterfaceLibrary.Api; +using RMWPFInterfaceLibrary.Helpers; using RMWPFInterfaceLibrary.Models; using RMWPFUserInterface.Helpers; using RMWPFUserInterface.ViewModels; @@ -29,11 +30,12 @@ namespace RMWPFUserInterface { _container.Instance(_container) .PerRequest(); - + _container .Singleton() .Singleton() .Singleton() + .Singleton() .Singleton(); GetType().Assembly.GetTypes() diff --git a/RMWPFUserInterface/ViewModels/SalesViewModel.cs b/RMWPFUserInterface/ViewModels/SalesViewModel.cs index ec14020..adcc55d 100644 --- a/RMWPFUserInterface/ViewModels/SalesViewModel.cs +++ b/RMWPFUserInterface/ViewModels/SalesViewModel.cs @@ -1,5 +1,6 @@ using Caliburn.Micro; using RMWPFInterfaceLibrary.Api; +using RMWPFInterfaceLibrary.Helpers; using RMWPFInterfaceLibrary.Models; using System; using System.Collections.Generic; @@ -13,9 +14,11 @@ namespace RMWPFUserInterface.ViewModels public class SalesViewModel : Screen { IProductEndPoint _productEndPoint; - public SalesViewModel(IProductEndPoint productEndPoint) + IConfigHelper _configHelper; + public SalesViewModel(IProductEndPoint productEndPoint, IConfigHelper configHelper) { _productEndPoint = productEndPoint; + _configHelper = configHelper; } protected override async void OnViewLoaded(object view) @@ -84,33 +87,53 @@ namespace RMWPFUserInterface.ViewModels public string SubTotal { get - { - decimal subTotal = 0; - - foreach (var item in Cart) - { - subTotal += item.Product.RetailPrice * item.QuantityInCart; - } - - return subTotal.ToString("C"); + { + return CalculateSubTotal().ToString("C"); } } + private decimal CalculateSubTotal() + { + decimal subTotal = 0; + + foreach (var item in Cart) + { + subTotal += item.Product.RetailPrice * item.QuantityInCart; + } + + return subTotal; + } + public string Tax { get { - // replace with calulation; - return "$0.00"; + return CalculateTax().ToString("C"); } } + private decimal CalculateTax() + { + decimal taxAmount = 0; + decimal TaxRate = _configHelper.GetTaxRate()/100; + + foreach (var item in Cart) + { + if (item.Product.IsTaxable) + { + taxAmount += (item.Product.RetailPrice * item.QuantityInCart * TaxRate); + } + } + + return taxAmount; + } + public string Total { get { - // replace with calulation; - return "$0.00"; + decimal total = CalculateSubTotal() + CalculateTax(); + return total.ToString("C"); } } @@ -149,6 +172,8 @@ namespace RMWPFUserInterface.ViewModels SelectedProduct.QuantityInStock -= ItemQuantity; ItemQuantity = 1; NotifyOfPropertyChange(() => SubTotal); + NotifyOfPropertyChange(() => Tax); + NotifyOfPropertyChange(() => Total); Cart.ResetBindings(); } @@ -166,6 +191,8 @@ namespace RMWPFUserInterface.ViewModels public void RemoveFromCart() { NotifyOfPropertyChange(() => SubTotal); + NotifyOfPropertyChange(() => Tax); + NotifyOfPropertyChange(() => Total); } public bool CanCheckOut