Added Taxable to Products

Added the ability to tax products for sale
This commit is contained in:
s459315 2022-07-23 23:56:50 +02:00
parent 57d2ab60a7
commit 0957963cad
10 changed files with 85 additions and 17 deletions

View File

@ -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

View File

@ -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
)

View File

@ -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; }
}
}

View File

@ -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;
}
}
}

View File

@ -0,0 +1,7 @@
namespace RMWPFInterfaceLibrary.Helpers
{
public interface IConfigHelper
{
decimal GetTaxRate();
}
}

View File

@ -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; }
}
}

View File

@ -52,6 +52,8 @@
<Compile Include="Api\IAPIHelper.cs" />
<Compile Include="Api\IProductEndPoint.cs" />
<Compile Include="Api\ProductEndPoint.cs" />
<Compile Include="Helpers\ConfigHelper.cs" />
<Compile Include="Helpers\IConfigHelper.cs" />
<Compile Include="Models\AuthenticatedUser.cs" />
<Compile Include="Models\CartItemModel.cs" />
<Compile Include="Models\ILoggedInUserModel.cs" />

View File

@ -2,6 +2,7 @@
<configuration>
<appSettings>
<add key="api" value="https://localhost:44372/" />
<add key="taxRate" value="8.75" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />

View File

@ -1,5 +1,6 @@
using Caliburn.Micro;
using RMWPFInterfaceLibrary.Api;
using RMWPFInterfaceLibrary.Helpers;
using RMWPFInterfaceLibrary.Models;
using RMWPFUserInterface.Helpers;
using RMWPFUserInterface.ViewModels;
@ -34,6 +35,7 @@ namespace RMWPFUserInterface
.Singleton<IWindowManager, WindowManager>()
.Singleton<IEventAggregator, EventAggregator>()
.Singleton<ILoggedInUserModel, LoggedInUserModel>()
.Singleton<IConfigHelper, ConfigHelper>()
.Singleton<IAPIHelper, APIHelper>();
GetType().Assembly.GetTypes()

View File

@ -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,6 +87,12 @@ namespace RMWPFUserInterface.ViewModels
public string SubTotal
{
get
{
return CalculateSubTotal().ToString("C");
}
}
private decimal CalculateSubTotal()
{
decimal subTotal = 0;
@ -92,25 +101,39 @@ namespace RMWPFUserInterface.ViewModels
subTotal += item.Product.RetailPrice * item.QuantityInCart;
}
return subTotal.ToString("C");
}
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