2022-07-22 21:20:13 +02:00
|
|
|
|
using Caliburn.Micro;
|
2022-07-23 17:09:13 +02:00
|
|
|
|
using RMWPFInterfaceLibrary.Api;
|
2022-07-23 23:56:50 +02:00
|
|
|
|
using RMWPFInterfaceLibrary.Helpers;
|
2022-07-23 17:09:13 +02:00
|
|
|
|
using RMWPFInterfaceLibrary.Models;
|
2022-07-22 21:20:13 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace RMWPFUserInterface.ViewModels
|
|
|
|
|
{
|
|
|
|
|
public class SalesViewModel : Screen
|
|
|
|
|
{
|
2022-07-23 17:09:13 +02:00
|
|
|
|
IProductEndPoint _productEndPoint;
|
2022-07-23 23:56:50 +02:00
|
|
|
|
IConfigHelper _configHelper;
|
|
|
|
|
public SalesViewModel(IProductEndPoint productEndPoint, IConfigHelper configHelper)
|
2022-07-23 17:09:13 +02:00
|
|
|
|
{
|
|
|
|
|
_productEndPoint = productEndPoint;
|
2022-07-23 23:56:50 +02:00
|
|
|
|
_configHelper = configHelper;
|
2022-07-23 17:09:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async void OnViewLoaded(object view)
|
|
|
|
|
{
|
|
|
|
|
base.OnViewLoaded(view);
|
|
|
|
|
await LoadProducts();
|
|
|
|
|
}
|
2022-07-22 21:20:13 +02:00
|
|
|
|
|
2022-07-23 17:09:13 +02:00
|
|
|
|
private async Task LoadProducts()
|
|
|
|
|
{
|
|
|
|
|
var prods = await _productEndPoint.GetAll();
|
|
|
|
|
Products = new BindingList<ProductModel>(prods);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private BindingList<ProductModel> _products;
|
|
|
|
|
|
|
|
|
|
public BindingList<ProductModel> Products
|
2022-07-22 21:20:13 +02:00
|
|
|
|
{
|
|
|
|
|
get { return _products; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_products = value;
|
|
|
|
|
NotifyOfPropertyChange(() => Products);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-23 17:09:13 +02:00
|
|
|
|
private ProductModel _selectedProduct;
|
2022-07-22 21:20:13 +02:00
|
|
|
|
|
2022-07-23 17:09:13 +02:00
|
|
|
|
public ProductModel SelectedProduct
|
2022-07-22 21:20:13 +02:00
|
|
|
|
{
|
2022-07-23 17:09:13 +02:00
|
|
|
|
get { return _selectedProduct; }
|
2022-07-22 21:20:13 +02:00
|
|
|
|
set
|
|
|
|
|
{
|
2022-07-23 17:09:13 +02:00
|
|
|
|
_selectedProduct = value;
|
|
|
|
|
NotifyOfPropertyChange(() => SelectedProduct);
|
|
|
|
|
NotifyOfPropertyChange(() => CanAddToCart);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private BindingList<CartItemModel> _cart = new BindingList<CartItemModel>();
|
|
|
|
|
|
|
|
|
|
public BindingList<CartItemModel> Cart
|
|
|
|
|
{
|
|
|
|
|
get { return _cart; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_cart = value;
|
2022-07-22 21:20:13 +02:00
|
|
|
|
NotifyOfPropertyChange(() => Cart);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-23 17:09:13 +02:00
|
|
|
|
private int _itemQuantity = 1;
|
2022-07-22 21:20:13 +02:00
|
|
|
|
|
2022-07-23 17:09:13 +02:00
|
|
|
|
public int ItemQuantity
|
2022-07-22 21:20:13 +02:00
|
|
|
|
{
|
|
|
|
|
get { return _itemQuantity; }
|
|
|
|
|
set
|
|
|
|
|
{
|
2022-07-23 17:09:13 +02:00
|
|
|
|
_itemQuantity = value;
|
2022-07-22 21:20:13 +02:00
|
|
|
|
NotifyOfPropertyChange(() => ItemQuantity);
|
2022-07-23 17:09:13 +02:00
|
|
|
|
NotifyOfPropertyChange(() => CanAddToCart);
|
2022-07-22 21:20:13 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string SubTotal
|
|
|
|
|
{
|
|
|
|
|
get
|
2022-07-23 23:56:50 +02:00
|
|
|
|
{
|
|
|
|
|
return CalculateSubTotal().ToString("C");
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-23 17:09:13 +02:00
|
|
|
|
|
2022-07-23 23:56:50 +02:00
|
|
|
|
private decimal CalculateSubTotal()
|
|
|
|
|
{
|
|
|
|
|
decimal subTotal = 0;
|
2022-07-23 17:09:13 +02:00
|
|
|
|
|
2022-07-23 23:56:50 +02:00
|
|
|
|
foreach (var item in Cart)
|
|
|
|
|
{
|
|
|
|
|
subTotal += item.Product.RetailPrice * item.QuantityInCart;
|
2022-07-22 21:20:13 +02:00
|
|
|
|
}
|
2022-07-23 23:56:50 +02:00
|
|
|
|
|
|
|
|
|
return subTotal;
|
2022-07-22 21:20:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Tax
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2022-07-23 23:56:50 +02:00
|
|
|
|
return CalculateTax().ToString("C");
|
2022-07-22 21:20:13 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-23 23:56:50 +02:00
|
|
|
|
private decimal CalculateTax()
|
|
|
|
|
{
|
|
|
|
|
decimal taxAmount = 0;
|
|
|
|
|
decimal TaxRate = _configHelper.GetTaxRate()/100;
|
|
|
|
|
|
2022-07-24 00:10:49 +02:00
|
|
|
|
taxAmount = Cart
|
|
|
|
|
.Where(x => x.Product.IsTaxable)
|
|
|
|
|
.Sum(x => x.Product.RetailPrice * x.QuantityInCart * TaxRate);
|
2022-07-23 23:56:50 +02:00
|
|
|
|
|
|
|
|
|
return taxAmount;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 21:20:13 +02:00
|
|
|
|
public string Total
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2022-07-23 23:56:50 +02:00
|
|
|
|
decimal total = CalculateSubTotal() + CalculateTax();
|
|
|
|
|
return total.ToString("C");
|
2022-07-22 21:20:13 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CanAddToCart
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
bool output = false;
|
|
|
|
|
|
2022-07-23 17:09:13 +02:00
|
|
|
|
if (ItemQuantity > 0 && SelectedProduct?.QuantityInStock >= ItemQuantity)
|
|
|
|
|
{
|
|
|
|
|
output = true;
|
|
|
|
|
}
|
2022-07-22 21:20:13 +02:00
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public void AddToCart()
|
|
|
|
|
{
|
2022-07-23 17:09:13 +02:00
|
|
|
|
CartItemModel existingItem = Cart.FirstOrDefault(x => x.Product == SelectedProduct);
|
2022-07-22 21:20:13 +02:00
|
|
|
|
|
2022-07-23 17:09:13 +02:00
|
|
|
|
if (existingItem != null)
|
|
|
|
|
{
|
|
|
|
|
existingItem.QuantityInCart += ItemQuantity;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CartItemModel item = new CartItemModel
|
|
|
|
|
{
|
|
|
|
|
Product = SelectedProduct,
|
|
|
|
|
QuantityInCart = ItemQuantity
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Cart.Add(item);
|
|
|
|
|
}
|
|
|
|
|
SelectedProduct.QuantityInStock -= ItemQuantity;
|
|
|
|
|
ItemQuantity = 1;
|
|
|
|
|
NotifyOfPropertyChange(() => SubTotal);
|
2022-07-23 23:56:50 +02:00
|
|
|
|
NotifyOfPropertyChange(() => Tax);
|
|
|
|
|
NotifyOfPropertyChange(() => Total);
|
2022-07-23 17:09:13 +02:00
|
|
|
|
Cart.ResetBindings();
|
2022-07-22 21:20:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CanRemoveFromCart
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
bool output = false;
|
|
|
|
|
|
|
|
|
|
// Make sure something is selected
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public void RemoveFromCart()
|
|
|
|
|
{
|
2022-07-23 17:09:13 +02:00
|
|
|
|
NotifyOfPropertyChange(() => SubTotal);
|
2022-07-23 23:56:50 +02:00
|
|
|
|
NotifyOfPropertyChange(() => Tax);
|
|
|
|
|
NotifyOfPropertyChange(() => Total);
|
2022-07-22 21:20:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CanCheckOut
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
bool output = false;
|
|
|
|
|
|
|
|
|
|
// Make sure there is something in the cart
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public void CheckOut()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|