Retail_manager/RMWPFUserInterface/ViewModels/SalesViewModel.cs

188 lines
4.4 KiB
C#
Raw Normal View History

using Caliburn.Micro;
using RMWPFInterfaceLibrary.Api;
using RMWPFInterfaceLibrary.Models;
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
{
IProductEndPoint _productEndPoint;
public SalesViewModel(IProductEndPoint productEndPoint)
{
_productEndPoint = productEndPoint;
}
protected override async void OnViewLoaded(object view)
{
base.OnViewLoaded(view);
await LoadProducts();
}
private async Task LoadProducts()
{
var prods = await _productEndPoint.GetAll();
Products = new BindingList<ProductModel>(prods);
}
private BindingList<ProductModel> _products;
public BindingList<ProductModel> Products
{
get { return _products; }
set
{
_products = value;
NotifyOfPropertyChange(() => Products);
}
}
private ProductModel _selectedProduct;
public ProductModel SelectedProduct
{
get { return _selectedProduct; }
set
{
_selectedProduct = value;
NotifyOfPropertyChange(() => SelectedProduct);
NotifyOfPropertyChange(() => CanAddToCart);
}
}
private BindingList<CartItemModel> _cart = new BindingList<CartItemModel>();
public BindingList<CartItemModel> Cart
{
get { return _cart; }
set
{
_cart = value;
NotifyOfPropertyChange(() => Cart);
}
}
private int _itemQuantity = 1;
public int ItemQuantity
{
get { return _itemQuantity; }
set
{
_itemQuantity = value;
NotifyOfPropertyChange(() => ItemQuantity);
NotifyOfPropertyChange(() => CanAddToCart);
}
}
public string SubTotal
{
get
{
decimal subTotal = 0;
foreach (var item in Cart)
{
subTotal += item.Product.RetailPrice * item.QuantityInCart;
}
return subTotal.ToString("C");
}
}
public string Tax
{
get
{
// replace with calulation;
return "$0.00";
}
}
public string Total
{
get
{
// replace with calulation;
return "$0.00";
}
}
public bool CanAddToCart
{
get
{
bool output = false;
if (ItemQuantity > 0 && SelectedProduct?.QuantityInStock >= ItemQuantity)
{
output = true;
}
return output;
}
}
public void AddToCart()
{
CartItemModel existingItem = Cart.FirstOrDefault(x => x.Product == SelectedProduct);
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);
Cart.ResetBindings();
}
public bool CanRemoveFromCart
{
get
{
bool output = false;
// Make sure something is selected
return output;
}
}
public void RemoveFromCart()
{
NotifyOfPropertyChange(() => SubTotal);
}
public bool CanCheckOut
{
get
{
bool output = false;
// Make sure there is something in the cart
return output;
}
}
public void CheckOut()
{
}
}
}