191 lines
5.7 KiB
C#
191 lines
5.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using Lagerprogramm.Models;
|
|
using Lagerprogramm.Services;
|
|
|
|
namespace Lagerprogramm.ViewModels;
|
|
|
|
public partial class PartsViewModel : ObservableObject
|
|
{
|
|
private readonly JsonDataService _dataService;
|
|
private readonly AppData _appData;
|
|
|
|
[ObservableProperty]
|
|
private ObservableCollection<Part> _parts = new();
|
|
|
|
[ObservableProperty]
|
|
[NotifyPropertyChangedFor(nameof(CanSave))]
|
|
private Part? _selectedPart;
|
|
|
|
[ObservableProperty]
|
|
[NotifyPropertyChangedFor(nameof(CanSave))]
|
|
[NotifyCanExecuteChangedFor(nameof(SaveCommand))]
|
|
private string _internalPartNumber = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
[NotifyPropertyChangedFor(nameof(CanSave))]
|
|
[NotifyCanExecuteChangedFor(nameof(SaveCommand))]
|
|
private string _manufacturerPartNumber = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private string _ean = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private string _supplierPartNumber = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
[NotifyPropertyChangedFor(nameof(CanSave))]
|
|
[NotifyCanExecuteChangedFor(nameof(SaveCommand))]
|
|
private string _manufacturer = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private string _supplier = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private string _storageLocation = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private string _storageLocationReserve = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private string _description = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private string _partComment = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
[NotifyPropertyChangedFor(nameof(CanSave))]
|
|
[NotifyCanExecuteChangedFor(nameof(SaveCommand))]
|
|
private decimal _purchasePrice;
|
|
|
|
[ObservableProperty]
|
|
private decimal _msrp;
|
|
|
|
[ObservableProperty]
|
|
private int _stockQuantity;
|
|
|
|
public bool CanSave =>
|
|
!string.IsNullOrWhiteSpace(InternalPartNumber)
|
|
&& !string.IsNullOrWhiteSpace(ManufacturerPartNumber)
|
|
&& !string.IsNullOrWhiteSpace(Manufacturer)
|
|
&& PurchasePrice >= 0;
|
|
|
|
public PartsViewModel(JsonDataService dataService, AppData appData)
|
|
{
|
|
_dataService = dataService;
|
|
_appData = appData;
|
|
foreach (var p in appData.Parts)
|
|
Parts.Add(p);
|
|
}
|
|
|
|
partial void OnSelectedPartChanged(Part? value)
|
|
{
|
|
if (value is null)
|
|
{
|
|
ClearFields();
|
|
return;
|
|
}
|
|
InternalPartNumber = value.InternalPartNumber;
|
|
ManufacturerPartNumber = value.ManufacturerPartNumber;
|
|
Ean = value.Ean;
|
|
SupplierPartNumber = value.SupplierPartNumber;
|
|
Manufacturer = value.Manufacturer;
|
|
Supplier = value.Supplier;
|
|
StorageLocation = value.StorageLocation;
|
|
StorageLocationReserve = value.StorageLocationReserve;
|
|
Description = value.Description;
|
|
PartComment = value.Comment;
|
|
PurchasePrice = value.PurchasePrice;
|
|
Msrp = value.Msrp;
|
|
StockQuantity = value.StockQuantity;
|
|
}
|
|
|
|
[RelayCommand(CanExecute = nameof(CanSave))]
|
|
private void Save()
|
|
{
|
|
var existing = _appData.Parts.FirstOrDefault(p => p.InternalPartNumber == InternalPartNumber);
|
|
if (existing is not null)
|
|
{
|
|
existing.ManufacturerPartNumber = ManufacturerPartNumber;
|
|
existing.Ean = Ean;
|
|
existing.SupplierPartNumber = SupplierPartNumber;
|
|
existing.Manufacturer = Manufacturer;
|
|
existing.Supplier = Supplier;
|
|
existing.StorageLocation = StorageLocation;
|
|
existing.StorageLocationReserve = StorageLocationReserve;
|
|
existing.Description = Description;
|
|
existing.Comment = PartComment;
|
|
existing.PurchasePrice = PurchasePrice;
|
|
existing.Msrp = Msrp;
|
|
// StockQuantity not edited here
|
|
}
|
|
else
|
|
{
|
|
var part = new Part
|
|
{
|
|
InternalPartNumber = InternalPartNumber.Trim(),
|
|
ManufacturerPartNumber = ManufacturerPartNumber.Trim(),
|
|
Ean = Ean.Trim(),
|
|
SupplierPartNumber = SupplierPartNumber.Trim(),
|
|
Manufacturer = Manufacturer.Trim(),
|
|
Supplier = Supplier.Trim(),
|
|
StorageLocation = StorageLocation.Trim(),
|
|
StorageLocationReserve = StorageLocationReserve.Trim(),
|
|
Description = Description.Trim(),
|
|
Comment = PartComment.Trim(),
|
|
PurchasePrice = PurchasePrice,
|
|
Msrp = Msrp,
|
|
StockQuantity = 0
|
|
};
|
|
_appData.Parts.Add(part);
|
|
Parts.Add(part);
|
|
}
|
|
_dataService.Save(_appData);
|
|
SelectedPart = null;
|
|
ClearFields();
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void NewPart()
|
|
{
|
|
SelectedPart = null;
|
|
ClearFields();
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void DeletePart()
|
|
{
|
|
if (SelectedPart is null) return;
|
|
_appData.Parts.Remove(SelectedPart);
|
|
Parts.Remove(SelectedPart);
|
|
_dataService.Save(_appData);
|
|
SelectedPart = null;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void SearchPart()
|
|
{
|
|
}
|
|
|
|
private void ClearFields()
|
|
{
|
|
InternalPartNumber = string.Empty;
|
|
ManufacturerPartNumber = string.Empty;
|
|
Ean = string.Empty;
|
|
SupplierPartNumber = string.Empty;
|
|
Manufacturer = string.Empty;
|
|
Supplier = string.Empty;
|
|
StorageLocation = string.Empty;
|
|
StorageLocationReserve = string.Empty;
|
|
Description = string.Empty;
|
|
PartComment = string.Empty;
|
|
PurchasePrice = 0;
|
|
Msrp = 0;
|
|
StockQuantity = 0;
|
|
}
|
|
}
|