Skip to content

Commit

Permalink
Handle quantity in purchasing
Browse files Browse the repository at this point in the history
  • Loading branch information
Deadpikle committed May 11, 2022
1 parent c37943a commit 6d2e84e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 85 deletions.
5 changes: 5 additions & 0 deletions SimpleInventory/Models/ItemSoldInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public int QuantitySold // defaults to 1
set
{
_quantity = value;
if (value > MaxQuantity)
{
value = MaxQuantity >= 0 ? MaxQuantity : 0;
}
NotifyPropertyChanged();
NotifyPropertyChanged(nameof(TotalCost));
NotifyPropertyChanged(nameof(TotalCostWithCurrency));
Expand Down Expand Up @@ -56,6 +60,7 @@ public int QuantitySold // defaults to 1

public string ItemName { get; set; }
public string ItemDescription { get; set; }
public int MaxQuantity { get; set; }

public string FriendlyTime
{
Expand Down
143 changes: 61 additions & 82 deletions SimpleInventory/ViewModels/ScanAndPurchaseViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,18 @@ namespace SimpleInventory.ViewModels
class ScanAndPurchaseViewModel : BaseViewModel, IPurchaseInfoChanged
{
private string _barcodeNumber;
private bool _purchaseInfoIsVisible;

private List<Currency> _currencies;
private Dictionary<int, int> _currencyIDToIndex;
private string _itemPurchaseStatusMessage;
private int _selectedPaidCurrencyIndex;
private string _changeNeeded;
private int _selectedChangeCurrencyIndex;
private Brush _itemPurchaseStatusBrush;


private SoundPlayer _successSoundPlayer;
private SoundPlayer _failureSoundPlayer;

private string _quantityErrorMessage;
private string _purchaseErrorMessage;
private bool _purchaseErrorMessageIsVisible;

private ObservableCollection<ItemSoldInfo> _purchaseInfo;

Expand All @@ -43,10 +40,8 @@ public ScanAndPurchaseViewModel(IChangeViewModel viewModelChanger) : base(viewMo
var currency = _currencies[i];
_currencyIDToIndex.Add(currency.ID, i);
}
PurchaseInfo = new ObservableCollection<ItemSoldInfo>();
PurchaseInfoIsVisible = false;
PurchasedItems = new ObservableCollection<ItemSoldInfo>();
ItemPurchaseStatusMessage = "";
SelectedPaidCurrencyIndex = -1;
ItemPurchaseStatusBrush = new SolidColorBrush(Colors.Black);
_failureSoundPlayer = new SoundPlayer("Sounds/failure-tbone.wav");
_successSoundPlayer = new SoundPlayer("Sounds/success.wav");
Expand All @@ -67,18 +62,12 @@ public string BarcodeNumber
set { _barcodeNumber = value; NotifyPropertyChanged(); }
}

public ObservableCollection<ItemSoldInfo> PurchaseInfo
public ObservableCollection<ItemSoldInfo> PurchasedItems
{
get { return _purchaseInfo; }
set { _purchaseInfo = value; NotifyPropertyChanged(); }
}

public bool PurchaseInfoIsVisible
{
get { return _purchaseInfoIsVisible; }
set { _purchaseInfoIsVisible = value; NotifyPropertyChanged(); }
}

public string ItemPurchaseStatusMessage
{
get { return _itemPurchaseStatusMessage; }
Expand All @@ -91,55 +80,15 @@ public Brush ItemPurchaseStatusBrush
set { _itemPurchaseStatusBrush = value; NotifyPropertyChanged(); }
}

public string ChangeNeeded
{
get { return _changeNeeded; }
set
{
_changeNeeded = value;
NotifyPropertyChanged();
}
}

public int SelectedPaidCurrencyIndex
{
get { return _selectedPaidCurrencyIndex; }
set
{
_selectedPaidCurrencyIndex = value;
NotifyPropertyChanged();
//UpdatePurchaseInfoCurrencies();
// UpdateChange();
}
}

public int SelectedChangeCurrencyIndex
{
get { return _selectedChangeCurrencyIndex; }
set
{
_selectedChangeCurrencyIndex = value;
NotifyPropertyChanged();
//UpdatePurchaseInfoCurrencies();
// UpdateChange();
}
}

public string QuantityErrorMessage
{
get { return _quantityErrorMessage; }
set { _quantityErrorMessage = value; NotifyPropertyChanged(); }
}

public decimal TotalPurchaseCost
{
get
{
var currency = Utilities.CurrencyForOrder(PurchaseInfo.ToList());
var currency = Utilities.CurrencyForOrder(PurchasedItems.ToList());
if (currency != null)
{
decimal cost = 0.0m;
foreach (var item in PurchaseInfo)
foreach (var item in PurchasedItems)
{
if (item.CostCurrency != null)
{
Expand All @@ -153,7 +102,7 @@ public decimal TotalPurchaseCost
// convert to USD
var usdCurrency = Currency.LoadUSDCurrency();
decimal cost = 0.0m;
foreach (var item in PurchaseInfo)
foreach (var item in PurchasedItems)
{
if (item.CostCurrency != null)
{
Expand All @@ -169,15 +118,15 @@ public string TotalPurchaseCostWithCurrency
{
get
{
if (PurchaseInfo.Count > 0)
if (PurchasedItems.Count > 0)
{
var totalPurchaseCost = TotalPurchaseCost;
var usdCurrency = Currency.LoadUSDCurrency();
if (usdCurrency == null)
{
return "Error: could not find USD currency";
}
var currency = Utilities.CurrencyForOrder(PurchaseInfo.ToList());
var currency = Utilities.CurrencyForOrder(PurchasedItems.ToList());
return currency == null
? totalPurchaseCost.ToString("0.00") + " (" + currency.Symbol + ")"
: totalPurchaseCost.ToString("0.00") + " (" + usdCurrency.Symbol + ")";
Expand All @@ -194,7 +143,7 @@ public int TotalItemCount
get
{
var count = 0;
foreach (var item in PurchaseInfo)
foreach (var item in PurchasedItems)
{
count += item.QuantitySold;
}
Expand All @@ -206,9 +155,9 @@ public bool CanFinalize
{
get
{
if (PurchaseInfo.Count > 0 && !TotalPurchaseCostWithCurrency.ToLower().Contains("error"))
if (PurchasedItems.Count > 0 && !PurchaseErrorMessageIsVisible && !TotalPurchaseCostWithCurrency.ToLower().Contains("error"))
{
foreach (var item in PurchaseInfo)
foreach (var item in PurchasedItems)
{
if (item.QuantitySold > 0)
{
Expand All @@ -224,9 +173,9 @@ public bool CanCancel
{
get
{
if (PurchaseInfo.Count > 0)
if (PurchasedItems.Count > 0)
{
foreach (var item in PurchaseInfo)
foreach (var item in PurchasedItems)
{
if (item.QuantitySold > 0)
{
Expand All @@ -238,13 +187,42 @@ public bool CanCancel
}
}

public string PurchaseErrorMessage
{
get { return _purchaseErrorMessage; }
set
{
_purchaseErrorMessage = value;
NotifyPropertyChanged();
NotifyPropertyChanged(nameof(PurchaseErrorMessageIsVisible));
}
}

public bool PurchaseErrorMessageIsVisible
{
get => !string.IsNullOrWhiteSpace(PurchaseErrorMessage);
}

#endregion

public void QuantityChanged(ItemSoldInfo info, int quantity)
{
UpdateTotalsAndFinalizeButtons();
}

private void CheckForQuantityErrors()
{
PurchaseErrorMessage = "";
foreach (var item in PurchasedItems)
{
if (item.QuantitySold > item.MaxQuantity)
{
PurchaseErrorMessage = "Error: Number of items sold for " + item.ItemName + " exceeds maximum of " + item.MaxQuantity;
break;
}
}
}

#region ICommands

public ICommand GoToMainMenu
Expand Down Expand Up @@ -273,19 +251,27 @@ private void ItemWasPurchased()
{
ItemPurchaseStatusBrush = new SolidColorBrush(Colors.Red);
ItemPurchaseStatusMessage = "There are no items left to purchase for this item! Barcode: " + BarcodeNumber;
//PurchaseInfoIsVisible = false;
// play failure sound
_failureSoundPlayer.Play();
}
else
{
ItemPurchaseStatusBrush = new SolidColorBrush(Colors.Green);
ItemPurchaseStatusMessage = "Item successfully found and added! Barcode: " + BarcodeNumber;
// create purchase data object
var existingPurchaseData = PurchaseInfo.Where(x => x.InventoryItemID == item.ID).FirstOrDefault();
var existingPurchaseData = PurchasedItems.Where(x => x.InventoryItemID == item.ID).FirstOrDefault();
if (existingPurchaseData != null)
{
existingPurchaseData.QuantitySold += 1; // TODO: quantity check -- make sure enough items!
if (existingPurchaseData.QuantitySold + 1 <= item.Quantity)
{
existingPurchaseData.QuantitySold += 1;
_successSoundPlayer.Play();
}
else
{
ItemPurchaseStatusBrush = new SolidColorBrush(Colors.Red);
ItemPurchaseStatusMessage = "The maximum number of items left to purchase for this item has already been reached!";
_failureSoundPlayer.Play();
}
}
else
{
Expand All @@ -306,29 +292,21 @@ private void ItemWasPurchased()
purchaseData.ItemName = item.Name;
purchaseData.ItemDescription = item.Description;
purchaseData.PurchaseInfoChanged = this;
PurchaseInfo.Add(purchaseData);
// show info to the user for possible future editing
ChangeNeeded = "0"; // TODO: update if paid updated -- might want to bind to a different property for set {} updates
SelectedChangeCurrencyIndex = _currencyIDToIndex[purchaseData.ChangeCurrency.ID];
SelectedPaidCurrencyIndex = _currencyIDToIndex[purchaseData.PaidCurrency.ID];
//PurchaseInfoIsVisible = true;
purchaseData.MaxQuantity = item.Quantity;
PurchasedItems.Add(purchaseData);
_successSoundPlayer.Play();
}
// play success sound
_successSoundPlayer.Play();
UpdateTotalsAndFinalizeButtons();
}
}
else
{
ItemPurchaseStatusBrush = new SolidColorBrush(Colors.Red);
ItemPurchaseStatusMessage = "Item not found! Barcode: " + BarcodeNumber;
//PurchaseInfoIsVisible = false;
// play failure sound
_failureSoundPlayer.Play();
}
}
BarcodeNumber = ""; // empty the field so that something can be scanned again
QuantityErrorMessage = "";
}

public ICommand CancelPurchase
Expand All @@ -340,12 +318,13 @@ private void PerformPurchaseCancel()
{
BarcodeNumber = "";
ItemPurchaseStatusMessage = "";
PurchaseInfo = new ObservableCollection<ItemSoldInfo>();
PurchasedItems = new ObservableCollection<ItemSoldInfo>();
UpdateTotalsAndFinalizeButtons();
}

private void UpdateTotalsAndFinalizeButtons()
{
CheckForQuantityErrors();
NotifyPropertyChanged(nameof(TotalPurchaseCostWithCurrency));
NotifyPropertyChanged(nameof(TotalItemCount));
NotifyPropertyChanged(nameof(CanFinalize));
Expand All @@ -364,7 +343,7 @@ private void CheckBeforeDeletingItemSoldInfo(ItemSoldInfo item)

public void DeleteItemSoldInfo(ItemSoldInfo info)
{
PurchaseInfo.Remove(info);
PurchasedItems.Remove(info);
ItemPurchaseStatusMessage = "";
UpdateTotalsAndFinalizeButtons();
}
Expand Down
14 changes: 12 additions & 2 deletions SimpleInventory/Views/ScanAndPurchase.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Grid.Row="0"
Margin="0,5,0,0"
Expand All @@ -101,7 +102,7 @@
<DataGrid Grid.Row="1"
Grid.Column="0"
Name="ItemsGrid"
ItemsSource="{Binding PurchaseInfo}"
ItemsSource="{Binding PurchasedItems}"
d:DataContext="{d:DesignInstance Type={x:Type models:ItemSoldInfo}}"
IsReadOnly="True"
CanUserAddRows="False"
Expand Down Expand Up @@ -135,6 +136,7 @@
VerticalContentAlignment="Center"
NumericInputMode="Numbers"
Minimum="0"
Maximum="{Binding MaxQuantity}"
FontSize="14" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
Expand Down Expand Up @@ -169,7 +171,15 @@
VerticalAlignment="Top">
Total Items: <Run Text="{Binding TotalItemCount, Mode=OneWay}" /> | Total Purchase Cost: <Run Text="{Binding TotalPurchaseCostWithCurrency, Mode=OneWay}" />
</TextBlock>
<StackPanel Grid.Row="3"
<TextBlock Grid.Row="3"
FontStyle="Italic"
Text="{Binding PurchaseErrorMessage}"
Visibility="{Binding PurchaseErrorMessageIsVisible, Converter={StaticResource BooleanToVisibilityConverter}}"
HorizontalAlignment="Center"
FontSize="16"
Margin="0,4,0,4"
Foreground="Red" />
<StackPanel Grid.Row="4"
Grid.Column="0"
Orientation="Horizontal"
HorizontalAlignment="Center">
Expand Down
2 changes: 1 addition & 1 deletion SimpleInventory/Views/ScanAndPurchase.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void BarcodeScanTextBox_KeyDown(object sender, KeyEventArgs e)

private void CancelPurchase_Click(object sender, RoutedEventArgs e)
{
if (DataContext is ScanAndPurchaseViewModel sapvm && sapvm.PurchaseInfo.Count > 0)
if (DataContext is ScanAndPurchaseViewModel sapvm && sapvm.PurchasedItems.Count > 0)
{
var result = MessageBox.Show("Are you sure you want to cancel this purchase?", "Cancel Purchase", MessageBoxButton.YesNoCancel);
if (result == MessageBoxResult.Yes)
Expand Down

0 comments on commit 6d2e84e

Please sign in to comment.