2022-07-27 14:57:33 +02:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
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-27 14:57:33 +02:00
|
|
|
|
using RMWPFUserInterface.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;
|
2022-07-25 17:02:36 +02:00
|
|
|
|
ISaleEndPoint _saleEndPoint;
|
2022-07-27 14:57:33 +02:00
|
|
|
|
IMapper _mapper;
|
2022-07-25 17:02:36 +02:00
|
|
|
|
|
2022-07-27 14:57:33 +02:00
|
|
|
|
public SalesViewModel(IProductEndPoint productEndPoint, IConfigHelper configHelper,
|
|
|
|
|
ISaleEndPoint saleEndPoint, IMapper mapper)
|
2022-07-23 17:09:13 +02:00
|
|
|
|
{
|
|
|
|
|
_productEndPoint = productEndPoint;
|
2022-07-23 23:56:50 +02:00
|
|
|
|
_configHelper = configHelper;
|
2022-07-25 17:02:36 +02:00
|
|
|
|
_saleEndPoint = saleEndPoint;
|
2022-07-27 14:57:33 +02:00
|
|
|
|
_mapper = mapper;
|
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()
|
|
|
|
|
{
|
2022-07-27 14:57:33 +02:00
|
|
|
|
var products_lsit = await _productEndPoint.GetAll();
|
|
|
|
|
var products = _mapper.Map<List<ProductDisplayModel>>(products_lsit);
|
|
|
|
|
Products = new BindingList<ProductDisplayModel>(products);
|
2022-07-23 17:09:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-27 14:57:33 +02:00
|
|
|
|
private BindingList<ProductDisplayModel> _products;
|
2022-07-23 17:09:13 +02:00
|
|
|
|
|
2022-07-27 14:57:33 +02:00
|
|
|
|
public BindingList<ProductDisplayModel> Products
|
2022-07-22 21:20:13 +02:00
|
|
|
|
{
|
|
|
|
|
get { return _products; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_products = value;
|
|
|
|
|
NotifyOfPropertyChange(() => Products);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-27 14:57:33 +02:00
|
|
|
|
private ProductDisplayModel _selectedProduct;
|
2022-07-22 21:20:13 +02:00
|
|
|
|
|
2022-07-27 14:57:33 +02:00
|
|
|
|
public ProductDisplayModel 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-27 15:43:26 +02:00
|
|
|
|
private CartItemDisplayModel _selectedCartItem;
|
|
|
|
|
public CartItemDisplayModel SelectedCartItem
|
|
|
|
|
{
|
|
|
|
|
get { return _selectedCartItem; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_selectedCartItem = value;
|
|
|
|
|
NotifyOfPropertyChange(() => SelectedCartItem);
|
|
|
|
|
NotifyOfPropertyChange(() => CanRemoveFromCart);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-23 17:09:13 +02:00
|
|
|
|
|
2022-07-27 14:57:33 +02:00
|
|
|
|
private BindingList<CartItemDisplayModel> _cart = new BindingList<CartItemDisplayModel>();
|
2022-07-23 17:09:13 +02:00
|
|
|
|
|
2022-07-27 14:57:33 +02:00
|
|
|
|
public BindingList<CartItemDisplayModel> Cart
|
2022-07-23 17:09:13 +02:00
|
|
|
|
{
|
|
|
|
|
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-27 14:57:33 +02:00
|
|
|
|
CartItemDisplayModel 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
|
|
|
|
|
{
|
2022-07-27 14:57:33 +02:00
|
|
|
|
CartItemDisplayModel item = new CartItemDisplayModel
|
2022-07-23 17:09:13 +02:00
|
|
|
|
{
|
|
|
|
|
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-25 17:02:36 +02:00
|
|
|
|
NotifyOfPropertyChange(() => CanCheckOut);
|
2022-07-22 21:20:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CanRemoveFromCart
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
bool output = false;
|
|
|
|
|
|
|
|
|
|
// Make sure something is selected
|
2022-07-27 15:43:26 +02:00
|
|
|
|
if (SelectedCartItem != null && SelectedCartItem?.Product.QuantityInStock > 0)
|
|
|
|
|
{
|
|
|
|
|
output = true;
|
|
|
|
|
}
|
2022-07-22 21:20:13 +02:00
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public void RemoveFromCart()
|
|
|
|
|
{
|
2022-07-27 15:43:26 +02:00
|
|
|
|
SelectedCartItem.Product.QuantityInStock += 1;
|
|
|
|
|
|
|
|
|
|
if (SelectedCartItem.QuantityInCart > 1)
|
|
|
|
|
{
|
|
|
|
|
SelectedCartItem.QuantityInCart -= 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Cart.Remove(SelectedCartItem);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-23 17:09:13 +02:00
|
|
|
|
NotifyOfPropertyChange(() => SubTotal);
|
2022-07-23 23:56:50 +02:00
|
|
|
|
NotifyOfPropertyChange(() => Tax);
|
|
|
|
|
NotifyOfPropertyChange(() => Total);
|
2022-07-25 17:02:36 +02:00
|
|
|
|
NotifyOfPropertyChange(() => CanCheckOut);
|
2022-07-22 21:20:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CanCheckOut
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
bool output = false;
|
|
|
|
|
|
2022-07-25 17:02:36 +02:00
|
|
|
|
if ( Cart.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
output = true;
|
|
|
|
|
}
|
2022-07-22 21:20:13 +02:00
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-25 17:02:36 +02:00
|
|
|
|
public async Task CheckOut()
|
2022-07-22 21:20:13 +02:00
|
|
|
|
{
|
2022-07-25 17:02:36 +02:00
|
|
|
|
SaleModel sale = new SaleModel();
|
|
|
|
|
|
|
|
|
|
foreach (var item in Cart)
|
|
|
|
|
{
|
|
|
|
|
sale.SaleDetails.Add(new SaleDetailModel
|
|
|
|
|
{
|
|
|
|
|
ProductId = item.Product.Id,
|
|
|
|
|
Quantity = item.QuantityInCart
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
2022-07-22 21:20:13 +02:00
|
|
|
|
|
2022-07-25 17:02:36 +02:00
|
|
|
|
await _saleEndPoint.PostSale(sale);
|
2022-07-22 21:20:13 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|