diff --git a/PointOfSale/Pos.Models/ReceiptDetailModel.cs b/PointOfSale/Pos.Models/ReceiptDetailModel.cs new file mode 100644 index 0000000..f33b242 --- /dev/null +++ b/PointOfSale/Pos.Models/ReceiptDetailModel.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace Pos.Models +{ + public class ReceiptDetailModel + { + public int SaleId { get; set; } + public List Lines { get; set; } = new(); + public decimal Total { get; set; } + } +} diff --git a/PointOfSale/Pos.Models/ReceiptLineModel.cs b/PointOfSale/Pos.Models/ReceiptLineModel.cs new file mode 100644 index 0000000..16374be --- /dev/null +++ b/PointOfSale/Pos.Models/ReceiptLineModel.cs @@ -0,0 +1,10 @@ +namespace Pos.Models +{ + public class ReceiptLineModel + { + public string Product { get; set; } = string.Empty; + public int Pieces { get; set; } + public decimal Price { get; set; } + public decimal Total { get; set; } + } +} diff --git a/PointOfSale/Pos.Models/ReceiptSummaryModel.cs b/PointOfSale/Pos.Models/ReceiptSummaryModel.cs new file mode 100644 index 0000000..f40b6c4 --- /dev/null +++ b/PointOfSale/Pos.Models/ReceiptSummaryModel.cs @@ -0,0 +1,11 @@ +using System; + +namespace Pos.Models +{ + public class ReceiptSummaryModel + { + public int SaleId { get; set; } + public DateTime SaleTime { get; set; } + public decimal Total { get; set; } + } +} diff --git a/PointOfSale/Pos.Service/EmployeeService.cs b/PointOfSale/Pos.Service/EmployeeService.cs new file mode 100644 index 0000000..3afbc31 --- /dev/null +++ b/PointOfSale/Pos.Service/EmployeeService.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using Database.Models; +using Database.Repository; + +namespace Pos.Service +{ + public class EmployeeService + { + private readonly EmployeeRepository _employeeRepository = + new EmployeeRepository(); + + public List GetEmployees() + { + return _employeeRepository.GetAll(); + } + + public void AddEmployee(string name) + { + _employeeRepository.Add(ValidateName(name)); + CacheService.Invalidate(); + } + + public void RenameEmployee(int employeeId, string name) + { + ValidateId(employeeId); + _employeeRepository.Edit(employeeId, ValidateName(name)); + CacheService.Invalidate(); + } + + public void ArchiveEmployee(int employeeId) + { + ValidateId(employeeId); + _employeeRepository.Delete(employeeId); + CacheService.Invalidate(); + } + + private static string ValidateName(string name) + { + if (string.IsNullOrWhiteSpace(name)) + throw new ArgumentException( + "Medarbejderens navn skal være udfyldt.", + nameof(name)); + + return name.Trim(); + } + + private static void ValidateId(int employeeId) + { + if (employeeId <= 0) + throw new ArgumentOutOfRangeException(nameof(employeeId)); + } + } +} diff --git a/PointOfSale/Pos.Service/ProductGroupService.cs b/PointOfSale/Pos.Service/ProductGroupService.cs new file mode 100644 index 0000000..751540d --- /dev/null +++ b/PointOfSale/Pos.Service/ProductGroupService.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Database.Models; +using Database.Repository; + +namespace Pos.Service +{ + public class ProductGroupService + { + private readonly ProductGroupRepository _productGroupRepository = + new ProductGroupRepository(); + + public List GetProductGroups() + { + return _productGroupRepository.GetAll(); + } + + public bool HasProductGroups() + { + return _productGroupRepository.Any(); + } + + public void AddProductGroup(string name) + { + _productGroupRepository.Add(ValidateName(name)); + CacheService.Invalidate(); + } + + public void RenameProductGroup(int productGroupId, string name) + { + ValidateId(productGroupId); + _productGroupRepository.Edit( + ValidateName(name), + productGroupId); + CacheService.Invalidate(); + } + + public void ArchiveProductGroup(int productGroupId) + { + ValidateId(productGroupId); + _productGroupRepository.Archive(productGroupId); + CacheService.Invalidate(); + } + + public void UpdateOrder(IReadOnlyList orderedProductGroupIds) + { + ValidateOrder(orderedProductGroupIds); + _productGroupRepository.SetOrder(orderedProductGroupIds); + CacheService.Invalidate(); + } + + private static string ValidateName(string name) + { + if (string.IsNullOrWhiteSpace(name)) + throw new ArgumentException( + "Produktgruppen skal have et navn.", + nameof(name)); + + return name.Trim(); + } + + private static void ValidateId(int productGroupId) + { + if (productGroupId <= 0) + throw new ArgumentOutOfRangeException(nameof(productGroupId)); + } + + private static void ValidateOrder(IReadOnlyList orderedIds) + { + if (orderedIds == null) + throw new ArgumentNullException(nameof(orderedIds)); + + if (orderedIds.Any(id => id <= 0)) + throw new ArgumentException( + "Rækkefølgen indeholder et ugyldigt id.", + nameof(orderedIds)); + + if (orderedIds.Distinct().Count() != orderedIds.Count) + throw new ArgumentException( + "Rækkefølgen indeholder dubletter.", + nameof(orderedIds)); + } + } +} diff --git a/PointOfSale/Pos.Service/ProductService.cs b/PointOfSale/Pos.Service/ProductService.cs new file mode 100644 index 0000000..2f8d737 --- /dev/null +++ b/PointOfSale/Pos.Service/ProductService.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Database.Models; +using Database.Repository; + +namespace Pos.Service +{ + public class ProductService + { + private readonly ProductRepository _productRepository = + new ProductRepository(); + + public List GetProducts(int productGroupId) + { + ValidateId(productGroupId, nameof(productGroupId)); + return _productRepository.GetByProductGroup(productGroupId); + } + + public ProductEntity GetProduct(int productId) + { + ValidateId(productId, nameof(productId)); + return _productRepository.GetById(productId); + } + + public void AddProduct(string name, int productGroupId) + { + ValidateId(productGroupId, nameof(productGroupId)); + _productRepository.Add(ValidateName(name), productGroupId); + CacheService.Invalidate(); + } + + public void UpdateProduct( + int productId, + int productGroupId, + string name) + { + ValidateId(productId, nameof(productId)); + ValidateId(productGroupId, nameof(productGroupId)); + _productRepository.Update( + productId, + productGroupId, + ValidateName(name)); + CacheService.Invalidate(); + } + + public void ArchiveProduct(int productId) + { + ValidateId(productId, nameof(productId)); + _productRepository.Archive(productId); + CacheService.Invalidate(); + } + + public void UpdateOrder( + int productGroupId, + IReadOnlyList orderedProductIds) + { + ValidateId(productGroupId, nameof(productGroupId)); + + if (orderedProductIds == null) + throw new ArgumentNullException(nameof(orderedProductIds)); + + if (orderedProductIds.Any(id => id <= 0)) + throw new ArgumentException( + "Rækkefølgen indeholder et ugyldigt id.", + nameof(orderedProductIds)); + + if (orderedProductIds.Distinct().Count() != orderedProductIds.Count) + throw new ArgumentException( + "Rækkefølgen indeholder dubletter.", + nameof(orderedProductIds)); + + _productRepository.SetOrder( + productGroupId, + orderedProductIds); + CacheService.Invalidate(); + } + + private static string ValidateName(string name) + { + if (string.IsNullOrWhiteSpace(name)) + throw new ArgumentException( + "Produktet skal have et navn.", + nameof(name)); + + return name.Trim(); + } + + private static void ValidateId(int id, string parameterName) + { + if (id <= 0) + throw new ArgumentOutOfRangeException(parameterName); + } + } +} diff --git a/PointOfSale/Pos.Service/SaleHistoryService.cs b/PointOfSale/Pos.Service/SaleHistoryService.cs new file mode 100644 index 0000000..32e9b8c --- /dev/null +++ b/PointOfSale/Pos.Service/SaleHistoryService.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Database.Models; +using Database.Repository; +using Pos.Models; + +namespace Pos.Service +{ + public class SaleHistoryService + { + private readonly SaleRepository _saleRepository = + new SaleRepository(); + + public List Search( + DateTime startDate, + DateTime endDate) + { + if (endDate.Date < startDate.Date) + throw new ArgumentException( + "Slutdatoen må ikke ligge før startdatoen."); + + List sales = + _saleRepository.GetByDateRange(startDate, endDate); + + List receipts = + new List(); + + foreach (SaleEntity sale in sales) + { + decimal total = _saleRepository + .GetSaleLineBySaleId(sale.Id) + .Sum(line => line.Total); + + receipts.Add(new ReceiptSummaryModel + { + SaleId = sale.Id, + SaleTime = sale.Time, + Total = total + }); + } + + return receipts; + } + + public ReceiptDetailModel GetReceipt(int saleId) + { + if (saleId <= 0) + throw new ArgumentOutOfRangeException(nameof(saleId)); + + List saleLines = + _saleRepository.GetSaleLineBySaleId(saleId); + + ReceiptDetailModel receipt = new ReceiptDetailModel + { + SaleId = saleId + }; + + foreach (SaleLineEntity saleLine in saleLines) + { + receipt.Lines.Add(new ReceiptLineModel + { + Product = saleLine.Product, + Pieces = saleLine.Pieces, + Price = saleLine.Price, + Total = saleLine.Total + }); + } + + receipt.Total = receipt.Lines.Sum(line => line.Total); + return receipt; + } + } +} diff --git a/PointOfSale/Pos.Ui/Database/Repository/ProductGroupRepository.cs b/PointOfSale/Pos.Ui/Database/Repository/ProductGroupRepository.cs index ba8c303..051ef31 100644 --- a/PointOfSale/Pos.Ui/Database/Repository/ProductGroupRepository.cs +++ b/PointOfSale/Pos.Ui/Database/Repository/ProductGroupRepository.cs @@ -69,10 +69,30 @@ namespace Database.Repository context.SaveChanges(); } + public void SetOrder(IReadOnlyList orderedIds) + { + using PosDbContext context = new PosDbContext(); + List productGroups = context.ProductGroups + .Where(productGroup => orderedIds.Contains(productGroup.Id)) + .ToList(); + + if (productGroups.Count != orderedIds.Count) + throw new InvalidOperationException( + "En eller flere produktgrupper findes ikke."); + + Dictionary productGroupsById = + productGroups.ToDictionary(productGroup => productGroup.Id); + + for (int index = 0; index < orderedIds.Count; index++) + productGroupsById[orderedIds[index]].Index = index; + + context.SaveChanges(); + } + public bool Any() { using PosDbContext context = new PosDbContext(); - bool any = context.ProductGroups.Any(); + bool any = context.ProductGroups.Any(c => !c.IsArchived); return any; } } diff --git a/PointOfSale/Pos.Ui/Database/Repository/ProductRepository.cs b/PointOfSale/Pos.Ui/Database/Repository/ProductRepository.cs index 95c2fd4..e593fc6 100644 --- a/PointOfSale/Pos.Ui/Database/Repository/ProductRepository.cs +++ b/PointOfSale/Pos.Ui/Database/Repository/ProductRepository.cs @@ -1,6 +1,7 @@  using System.Collections.Generic; +using System; using System.Linq; using Database.Models; @@ -38,6 +39,33 @@ namespace Database.Repository context.SaveChanges(); } + public void SetOrder( + int productGroupId, + IReadOnlyList orderedIds) + { + using PosDbContext context = new PosDbContext(); + List products = context.Products + .Where(product => product.ProductGroupId == productGroupId) + .Where(product => orderedIds.Contains(product.Id)) + .ToList(); + + if (products.Count != orderedIds.Count) + throw new InvalidOperationException( + "Et eller flere produkter findes ikke i produktgruppen."); + + Dictionary productsById = + products.ToDictionary(product => product.Id); + + for (int index = 0; index < orderedIds.Count; index++) + { + ProductEntity product = productsById[orderedIds[index]]; + product.Index = index; + product.IsModified = true; + } + + context.SaveChanges(); + } + public ProductEntity GetById(int id) { using PosDbContext context = new PosDbContext(); @@ -72,7 +100,11 @@ namespace Database.Repository product.ProductGroupId = productGroupId; using PosDbContext context = new PosDbContext(); //Get the highest index - ProductEntity highest = context.Products.OrderByDescending(c => c.Index).Take(1).FirstOrDefault(); + ProductEntity highest = context.Products + .Where(c => c.ProductGroupId == productGroupId) + .OrderByDescending(c => c.Index) + .Take(1) + .FirstOrDefault(); if (highest == null) { highest = new ProductEntity(); diff --git a/PointOfSale/Pos.Ui/Pos/PrintReceiptWindow.xaml.cs b/PointOfSale/Pos.Ui/Pos/PrintReceiptWindow.xaml.cs index 4f7acd5..cd9bc71 100644 --- a/PointOfSale/Pos.Ui/Pos/PrintReceiptWindow.xaml.cs +++ b/PointOfSale/Pos.Ui/Pos/PrintReceiptWindow.xaml.cs @@ -11,8 +11,7 @@ using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; -using Database.Models; -using Database.Repository; +using Pos.Models; using Pos.Service; namespace Pos @@ -25,6 +24,9 @@ namespace Pos private List _receiptList = new List(); private bool _init = false; private int _saleId = 0; + private readonly SaleHistoryService _saleHistoryService = + new SaleHistoryService(); + private readonly SaleService _saleService = new SaleService(); public PrintReceiptWindow() { @@ -38,28 +40,39 @@ namespace Pos private void Search_OnClick(object sender, RoutedEventArgs e) { - DateTime startDate = (DateTime)StartDatePicker.SelectedDate; - DateTime endDate = (DateTime)EndDatePicker.SelectedDate; - SaleRepository saleRepository = new SaleRepository(); - List saleEntities = saleRepository.GetByDateRange(startDate, endDate); - foreach (SaleEntity entity in saleEntities) - { - List saleLineBySaleId = saleRepository.GetSaleLineBySaleId(entity.Id); - decimal total = 0; - foreach (SaleLineEntity lineEntity in saleLineBySaleId) - { - total += lineEntity.Total; - } + if (StartDatePicker.SelectedDate is not DateTime startDate || + EndDatePicker.SelectedDate is not DateTime endDate) + return; + List receipts; + try + { + receipts = _saleHistoryService.Search(startDate, endDate); + } + catch (ArgumentException exception) + { + MessageBox.Show( + exception.Message, + "Ugyldigt datointerval", + MessageBoxButton.OK, + MessageBoxImage.Warning); + return; + } + + _receiptList.Clear(); + foreach (ReceiptSummaryModel receipt in receipts) + { ListReceiptModel receiptModel = new ListReceiptModel { - Id = entity.Id, - Content = $"{entity.Time:dd-MM-yyyy HH:mm} - Pris: {total:0.00}" + Id = receipt.SaleId, + Content = + $"{receipt.SaleTime:dd-MM-yyyy HH:mm} - Pris: {receipt.Total:0.00}" }; _receiptList.Add(receiptModel); } _init = true; + ListReciept.ItemsSource = null; ListReciept.ItemsSource = _receiptList; _init = false; } @@ -78,23 +91,21 @@ namespace Pos private void ListReciept_OnSelectionChanged(object sender, SelectionChangedEventArgs e) { - if (_init) + if (_init || e.AddedItems.Count == 0) return; ListReceiptModel data = (ListReceiptModel) e.AddedItems[0]; _saleId = data.Id; - SaleRepository saleRepository = new SaleRepository(); - List saleLineBySaleId = saleRepository.GetSaleLineBySaleId(data.Id); + ReceiptDetailModel receipt = + _saleHistoryService.GetReceipt(data.Id); StringBuilder sb = new StringBuilder(); - decimal total = 0; - foreach (SaleLineEntity saleLine in saleLineBySaleId) + foreach (ReceiptLineModel saleLine in receipt.Lines) { - total += saleLine.Total; sb.AppendLine( - $"{saleLine.Product} - {saleLine.Pieces} stk. a' {saleLine.Price.ToString("0.00")} = {saleLine.Total.ToString("0.00")}"); + $"{saleLine.Product} - {saleLine.Pieces} stk. a' {saleLine.Price:0.00} = {saleLine.Total:0.00}"); } - sb.AppendLine($"Total: {total.ToString("0.00")} kr."); + sb.AppendLine($"Total: {receipt.Total:0.00} kr."); PrintReceipt.Visibility = Visibility.Visible; SaleLines.Clear(); SaleLines.Text = sb.ToString(); @@ -102,8 +113,7 @@ namespace Pos private void PrintReceipt_OnClick(object sender, RoutedEventArgs e) { - SaleService saleService = new SaleService(); - saleService.PrintReceipt(_saleId); + _saleService.PrintReceipt(_saleId); } } } diff --git a/PointOfSale/Pos.Ui/Pos/SaleOfDay.xaml.cs b/PointOfSale/Pos.Ui/Pos/SaleOfDay.xaml.cs index d571226..3c6bd09 100644 --- a/PointOfSale/Pos.Ui/Pos/SaleOfDay.xaml.cs +++ b/PointOfSale/Pos.Ui/Pos/SaleOfDay.xaml.cs @@ -13,11 +13,11 @@ namespace Pos { private bool _initialLoad = true; private TotalSaleDetail totalSaleDetail; + private readonly SaleService _saleService = new SaleService(); public SaleOfDay() { InitializeComponent(); - SaleService saleService = new SaleService(); - totalSaleDetail = saleService.TotalSale(DateTime.Now); + totalSaleDetail = _saleService.TotalSale(DateTime.Now); TotalSale.Content = $"Dagens salg: kr. {totalSaleDetail.TotalSale:0.00}"; StartDatePicker.SelectedDate = DateTime.Now; GridSaleDetail.ItemsSource = totalSaleDetail.TotalSaleCategories; @@ -29,18 +29,20 @@ namespace Pos if (_initialLoad) return; - _initialLoad = false; - DateTime selectedDate = DateTime.Parse(sender.ToString()); - SaleService saleService = new SaleService(); - totalSaleDetail = saleService.TotalSale(selectedDate); + if (StartDatePicker.SelectedDate is not DateTime selectedDate) + return; + + totalSaleDetail = _saleService.TotalSale(selectedDate); TotalSale.Content = $"Dagens salg: kr. {totalSaleDetail.TotalSale:0.00}"; GridSaleDetail.ItemsSource = totalSaleDetail.TotalSaleCategories; } private void PrintSale_OnClick(object sender, RoutedEventArgs e) { - SaleService saleService = new SaleService(); - saleService.PrintSaleOfDay(StartDatePicker.SelectedDate.Value,totalSaleDetail); + if (StartDatePicker.SelectedDate is not DateTime selectedDate) + return; + + _saleService.PrintSaleOfDay(selectedDate, totalSaleDetail); } } } diff --git a/PointOfSale/Pos.Ui/Pos/Setting/Employee/AddEmployeeWindow.xaml.cs b/PointOfSale/Pos.Ui/Pos/Setting/Employee/AddEmployeeWindow.xaml.cs index 43844f4..581ae6c 100644 --- a/PointOfSale/Pos.Ui/Pos/Setting/Employee/AddEmployeeWindow.xaml.cs +++ b/PointOfSale/Pos.Ui/Pos/Setting/Employee/AddEmployeeWindow.xaml.cs @@ -11,7 +11,7 @@ using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; -using Database.Repository; +using Pos.Service; namespace Pos.Setting { @@ -20,6 +20,9 @@ namespace Pos.Setting /// public partial class AddEmployeeWindow : Window { + private readonly EmployeeService _employeeService = + new EmployeeService(); + public AddEmployeeWindow() { InitializeComponent(); @@ -27,9 +30,19 @@ namespace Pos.Setting private void btnAdd_Click(object sender, RoutedEventArgs e) { - EmployeeRepository employeeRepository = new EmployeeRepository(); - employeeRepository.Add(txtStaff.Text); - this.Close(); + try + { + _employeeService.AddEmployee(txtStaff.Text); + Close(); + } + catch (ArgumentException exception) + { + MessageBox.Show( + exception.Message, + "Ugyldig medarbejder", + MessageBoxButton.OK, + MessageBoxImage.Warning); + } } } } diff --git a/PointOfSale/Pos.Ui/Pos/Setting/Employee/DeleteEmployeeWindow.xaml.cs b/PointOfSale/Pos.Ui/Pos/Setting/Employee/DeleteEmployeeWindow.xaml.cs index 2609278..8cbce7c 100644 --- a/PointOfSale/Pos.Ui/Pos/Setting/Employee/DeleteEmployeeWindow.xaml.cs +++ b/PointOfSale/Pos.Ui/Pos/Setting/Employee/DeleteEmployeeWindow.xaml.cs @@ -11,7 +11,7 @@ using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; -using Database.Repository; +using Pos.Service; namespace Pos.Setting { @@ -21,6 +21,8 @@ namespace Pos.Setting public partial class DeleteEmployeeWindow : Window { private int _employeeId; + private readonly EmployeeService _employeeService = + new EmployeeService(); public DeleteEmployeeWindow(int employeeId, string staffName) { @@ -31,9 +33,8 @@ namespace Pos.Setting private void btnYes_Click(object sender, RoutedEventArgs e) { - EmployeeRepository employeeRepository = new EmployeeRepository(); - employeeRepository.Delete(_employeeId); - this.Close(); + _employeeService.ArchiveEmployee(_employeeId); + Close(); } private void btnNo_Click(object sender, RoutedEventArgs e) diff --git a/PointOfSale/Pos.Ui/Pos/Setting/Employee/EditEmployeeWindow.xaml.cs b/PointOfSale/Pos.Ui/Pos/Setting/Employee/EditEmployeeWindow.xaml.cs index ecfbfa0..2754677 100644 --- a/PointOfSale/Pos.Ui/Pos/Setting/Employee/EditEmployeeWindow.xaml.cs +++ b/PointOfSale/Pos.Ui/Pos/Setting/Employee/EditEmployeeWindow.xaml.cs @@ -11,7 +11,7 @@ using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; -using Database.Repository; +using Pos.Service; namespace Pos.Setting { @@ -21,6 +21,8 @@ namespace Pos.Setting public partial class EditEmployeeWindow : Window { private int _employeeId; + private readonly EmployeeService _employeeService = + new EmployeeService(); public EditEmployeeWindow(int employeeId, string staffName) { @@ -31,9 +33,21 @@ namespace Pos.Setting private void btnEdit_Click(object sender, RoutedEventArgs e) { - EmployeeRepository employeeRepository = new EmployeeRepository(); - employeeRepository.Edit(_employeeId,txtEmployee.Text); - this.Close(); + try + { + _employeeService.RenameEmployee( + _employeeId, + txtEmployee.Text); + Close(); + } + catch (ArgumentException exception) + { + MessageBox.Show( + exception.Message, + "Ugyldig medarbejder", + MessageBoxButton.OK, + MessageBoxImage.Warning); + } } } } diff --git a/PointOfSale/Pos.Ui/Pos/Setting/Employee/EmployeeWindow.xaml.cs b/PointOfSale/Pos.Ui/Pos/Setting/Employee/EmployeeWindow.xaml.cs index 151051f..ab0c220 100644 --- a/PointOfSale/Pos.Ui/Pos/Setting/Employee/EmployeeWindow.xaml.cs +++ b/PointOfSale/Pos.Ui/Pos/Setting/Employee/EmployeeWindow.xaml.cs @@ -13,7 +13,7 @@ using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using Database.Models; -using Database.Repository; +using Pos.Service; namespace Pos.Setting { @@ -24,6 +24,8 @@ namespace Pos.Setting { private int _selectedEmployee = -1; private string _employeeName = string.Empty; + private readonly EmployeeService _employeeService = + new EmployeeService(); public EmployeeWindow() { InitializeComponent(); @@ -62,15 +64,14 @@ namespace Pos.Setting deleteEmployeeWindow.Show(); } - private void WindowClosed(object? sender, EventArgs e) + private void WindowClosed(object sender, EventArgs e) { LoadData(); } private void LoadData() { - using EmployeeRepository employeeRepository = new EmployeeRepository(); - List allStaff = employeeRepository.GetAll(); + List allStaff = _employeeService.GetEmployees(); LstStaff.Items.Clear(); foreach (EmployeeEntity staff in allStaff) { diff --git a/PointOfSale/Pos.Ui/Pos/Setting/Product/AddProductWindow.xaml.cs b/PointOfSale/Pos.Ui/Pos/Setting/Product/AddProductWindow.xaml.cs index b75fbe9..03228a5 100644 --- a/PointOfSale/Pos.Ui/Pos/Setting/Product/AddProductWindow.xaml.cs +++ b/PointOfSale/Pos.Ui/Pos/Setting/Product/AddProductWindow.xaml.cs @@ -1,8 +1,8 @@ -using Database.Repository; using System; using System.Collections.Generic; using System.Windows; using System.Windows.Controls; +using Pos.Service; namespace Pos.Setting.ProductGroup { @@ -11,6 +11,11 @@ namespace Pos.Setting.ProductGroup /// public partial class AddProductWindow : Window { + private readonly ProductGroupService _productGroupService = + new ProductGroupService(); + private readonly ProductService _productService = + new ProductService(); + public AddProductWindow() { InitializeComponent(); @@ -19,13 +24,16 @@ namespace Pos.Setting.ProductGroup private void LoadData(int selectedIndex) { - ProductGroupRepository productGroupRepository = new ProductGroupRepository(); - List products = productGroupRepository.GetAll(); - foreach (Database.Models.ProductGroupEntity productGroup in products) + List productGroups = + _productGroupService.GetProductGroups(); + + foreach (Database.Models.ProductGroupEntity productGroup in productGroups) { - ComboBoxItem comboBoxItem = new ComboBoxItem(); - comboBoxItem.Tag = productGroup.Id; - comboBoxItem.Content = productGroup.Name; + ComboBoxItem comboBoxItem = new ComboBoxItem + { + Tag = productGroup.Id, + Content = productGroup.Name + }; cmbProductGroup.Items.Add(comboBoxItem); } @@ -34,12 +42,23 @@ namespace Pos.Setting.ProductGroup private void btnAdd_Click(object sender, RoutedEventArgs e) { - ComboBoxItem item = (ComboBoxItem) cmbProductGroup.SelectedItem; - string name = txtProduct.Text; - int id = Convert.ToInt32(item.Tag); - ProductRepository productRepository = new ProductRepository(); - productRepository.Add(name,id); - this.Close(); + ComboBoxItem item = (ComboBoxItem)cmbProductGroup.SelectedItem; + + try + { + _productService.AddProduct( + txtProduct.Text, + Convert.ToInt32(item.Tag)); + Close(); + } + catch (ArgumentException exception) + { + MessageBox.Show( + exception.Message, + "Ugyldigt produkt", + MessageBoxButton.OK, + MessageBoxImage.Warning); + } } } } diff --git a/PointOfSale/Pos.Ui/Pos/Setting/Product/ArchiveProductWindow.xaml.cs b/PointOfSale/Pos.Ui/Pos/Setting/Product/ArchiveProductWindow.xaml.cs index b6126de..3819bd2 100644 --- a/PointOfSale/Pos.Ui/Pos/Setting/Product/ArchiveProductWindow.xaml.cs +++ b/PointOfSale/Pos.Ui/Pos/Setting/Product/ArchiveProductWindow.xaml.cs @@ -11,7 +11,7 @@ using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; -using Database.Repository; +using Pos.Service; namespace Pos.Setting.Product { @@ -21,20 +21,21 @@ namespace Pos.Setting.Product public partial class ArchiveProductWindow : Window { private int _productId; + private readonly ProductService _productService = + new ProductService(); public ArchiveProductWindow(int productId) { InitializeComponent(); _productId = productId; - ProductRepository productRepository = new ProductRepository(); - Database.Models.ProductEntity product = productRepository.GetById(_productId); + Database.Models.ProductEntity product = + _productService.GetProduct(_productId); txtDelProduct.Text = product.Name; } private void btnYes_Click(object sender, RoutedEventArgs e) { - ProductRepository productRepository = new ProductRepository(); - productRepository.Archive(_productId); - this.Close(); + _productService.ArchiveProduct(_productId); + Close(); } private void btnNo_Click(object sender, RoutedEventArgs e) diff --git a/PointOfSale/Pos.Ui/Pos/Setting/Product/ChangeProductOrderWindow.xaml.cs b/PointOfSale/Pos.Ui/Pos/Setting/Product/ChangeProductOrderWindow.xaml.cs index 4b378f7..b1f8013 100644 --- a/PointOfSale/Pos.Ui/Pos/Setting/Product/ChangeProductOrderWindow.xaml.cs +++ b/PointOfSale/Pos.Ui/Pos/Setting/Product/ChangeProductOrderWindow.xaml.cs @@ -12,7 +12,7 @@ using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; -using Database.Repository; +using Pos.Service; namespace Pos.Setting.Product { @@ -23,6 +23,8 @@ namespace Pos.Setting.Product { ObservableCollection _productList = new ObservableCollection(); private int _productGroupId; + private readonly ProductService _productService = + new ProductService(); public ChangeProductOrderWindow(int productGroupId) { InitializeComponent(); @@ -32,8 +34,8 @@ namespace Pos.Setting.Product private void LoadData() { - ProductRepository productRepository = new ProductRepository(); - List products = productRepository.GetByProductGroup(_productGroupId); + List products = + _productService.GetProducts(_productGroupId); foreach (Database.Models.ProductEntity product in products) { _productList.Add(product); @@ -86,13 +88,10 @@ namespace Pos.Setting.Product private void btnSave_Click(object sender, RoutedEventArgs e) { - ProductRepository productRepository = new ProductRepository(); - for (int i = 0; i < _productList.Count; i++) - { - Database.Models.ProductEntity product = _productList[i]; - productRepository.SetIndex(product.Id, i); - } - this.Close(); + _productService.UpdateOrder( + _productGroupId, + _productList.Select(product => product.Id).ToList()); + Close(); } } diff --git a/PointOfSale/Pos.Ui/Pos/Setting/Product/EditProductWindow.xaml.cs b/PointOfSale/Pos.Ui/Pos/Setting/Product/EditProductWindow.xaml.cs index d375fe4..d0d702c 100644 --- a/PointOfSale/Pos.Ui/Pos/Setting/Product/EditProductWindow.xaml.cs +++ b/PointOfSale/Pos.Ui/Pos/Setting/Product/EditProductWindow.xaml.cs @@ -11,7 +11,7 @@ using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; -using Database.Repository; +using Pos.Service; namespace Pos.Setting.Product { @@ -23,6 +23,10 @@ namespace Pos.Setting.Product private int _productId; private int _selectedProductGroupId; private Database.Models.ProductEntity _product; + private readonly ProductGroupService _productGroupService = + new ProductGroupService(); + private readonly ProductService _productService = + new ProductService(); public EditProductWindow(int productId, int selectedProductGroupId) { @@ -34,8 +38,8 @@ namespace Pos.Setting.Product private void Init() { - ProductGroupRepository categoryRepository = new ProductGroupRepository(); - List productGroups = categoryRepository.GetAll(); + List productGroups = + _productGroupService.GetProductGroups(); foreach (Database.Models.ProductGroupEntity productGroup in productGroups) { @@ -46,17 +50,29 @@ namespace Pos.Setting.Product } cmbProductGroup.SelectedIndex = _selectedProductGroupId; - ProductRepository productRepository = new ProductRepository(); - _product = productRepository.GetById(_productId); + _product = _productService.GetProduct(_productId); txtProduct.Text = _product.Name; } private void btnEdit_Click(object sender, RoutedEventArgs e) { ComboBoxItem item = (ComboBoxItem)cmbProductGroup.SelectedItem; - ProductRepository productRepository = new ProductRepository(); - productRepository.Update(_product.Id,(int)item.Tag,txtProduct.Text); - this.Close(); + try + { + _productService.UpdateProduct( + _product.Id, + (int)item.Tag, + txtProduct.Text); + Close(); + } + catch (ArgumentException exception) + { + MessageBox.Show( + exception.Message, + "Ugyldigt produkt", + MessageBoxButton.OK, + MessageBoxImage.Warning); + } } } } diff --git a/PointOfSale/Pos.Ui/Pos/Setting/Product/ProductWindow.xaml.cs b/PointOfSale/Pos.Ui/Pos/Setting/Product/ProductWindow.xaml.cs index 60e2332..789c924 100644 --- a/PointOfSale/Pos.Ui/Pos/Setting/Product/ProductWindow.xaml.cs +++ b/PointOfSale/Pos.Ui/Pos/Setting/Product/ProductWindow.xaml.cs @@ -3,7 +3,7 @@ using System; using System.Collections.Generic; using System.Windows; using System.Windows.Controls; -using Database.Repository; +using Pos.Service; using Pos.Setting.Product; namespace Pos.Setting.ProductGroup @@ -14,7 +14,10 @@ namespace Pos.Setting.ProductGroup public partial class ProductWindow : Window { private List _productGroups; - private int _productId; + private readonly ProductGroupService _productGroupService = + new ProductGroupService(); + private readonly ProductService _productService = + new ProductService(); public ProductWindow() { @@ -26,8 +29,7 @@ namespace Pos.Setting.ProductGroup private void Init() { - ProductGroupRepository categoryRepository = new ProductGroupRepository(); - _productGroups = categoryRepository.GetAll(); + _productGroups = _productGroupService.GetProductGroups(); cmbCat.Items.Clear(); foreach (Database.Models.ProductGroupEntity productGroup in _productGroups) { @@ -38,14 +40,19 @@ namespace Pos.Setting.ProductGroup cmbCat.Items.Add(comboBoxItem); } - cmbCat.SelectedIndex = 0; + if (_productGroups.Count > 0) + cmbCat.SelectedIndex = 0; } private void LoadData() { - ProductRepository productRepository = new ProductRepository(); - List products = productRepository.GetByProductGroup(_productGroups[cmbCat.SelectedIndex].Id); + if (cmbCat.SelectedIndex < 0) + return; + + List products = + _productService.GetProducts( + _productGroups[cmbCat.SelectedIndex].Id); lstProductGroup.Items.Clear(); foreach (Database.Models.ProductEntity product in products) { diff --git a/PointOfSale/Pos.Ui/Pos/Setting/ProductGroup/AddProductGroupWindow.xaml.cs b/PointOfSale/Pos.Ui/Pos/Setting/ProductGroup/AddProductGroupWindow.xaml.cs index 4ddb1ab..3767f79 100644 --- a/PointOfSale/Pos.Ui/Pos/Setting/ProductGroup/AddProductGroupWindow.xaml.cs +++ b/PointOfSale/Pos.Ui/Pos/Setting/ProductGroup/AddProductGroupWindow.xaml.cs @@ -11,7 +11,7 @@ using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; -using Database.Repository; +using Pos.Service; namespace Pos.Setting.Category { @@ -20,6 +20,9 @@ namespace Pos.Setting.Category /// public partial class AddProductGroupWindow : Window { + private readonly ProductGroupService _productGroupService = + new ProductGroupService(); + public AddProductGroupWindow() { InitializeComponent(); @@ -27,9 +30,19 @@ namespace Pos.Setting.Category private void btnAdd_Click(object sender, RoutedEventArgs e) { - ProductGroupRepository categoryRepository = new ProductGroupRepository(); - categoryRepository.Add(txtProductGroup.Text); - this.Close(); + try + { + _productGroupService.AddProductGroup(txtProductGroup.Text); + Close(); + } + catch (ArgumentException exception) + { + MessageBox.Show( + exception.Message, + "Ugyldig produktgruppe", + MessageBoxButton.OK, + MessageBoxImage.Warning); + } } } } diff --git a/PointOfSale/Pos.Ui/Pos/Setting/ProductGroup/ArchiveProductGroupWindow.xaml.cs b/PointOfSale/Pos.Ui/Pos/Setting/ProductGroup/ArchiveProductGroupWindow.xaml.cs index 3b79849..a4ecafe 100644 --- a/PointOfSale/Pos.Ui/Pos/Setting/ProductGroup/ArchiveProductGroupWindow.xaml.cs +++ b/PointOfSale/Pos.Ui/Pos/Setting/ProductGroup/ArchiveProductGroupWindow.xaml.cs @@ -11,7 +11,7 @@ using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; -using Database.Repository; +using Pos.Service; namespace Pos.Setting.Category { @@ -21,6 +21,8 @@ namespace Pos.Setting.Category public partial class ArchiveProductGroupWindow : Window { private readonly int _id; + private readonly ProductGroupService _productGroupService = + new ProductGroupService(); public ArchiveProductGroupWindow(string name, int id) { InitializeComponent(); @@ -36,9 +38,8 @@ namespace Pos.Setting.Category private void btnYes_Click(object sender, RoutedEventArgs e) { - ProductGroupRepository categoryRepository = new ProductGroupRepository(); - categoryRepository.Archive(_id); - this.Close(); + _productGroupService.ArchiveProductGroup(_id); + Close(); } } } diff --git a/PointOfSale/Pos.Ui/Pos/Setting/ProductGroup/ChangeProductGroupOrderWindow.xaml.cs b/PointOfSale/Pos.Ui/Pos/Setting/ProductGroup/ChangeProductGroupOrderWindow.xaml.cs index 8c1d84c..8951eb7 100644 --- a/PointOfSale/Pos.Ui/Pos/Setting/ProductGroup/ChangeProductGroupOrderWindow.xaml.cs +++ b/PointOfSale/Pos.Ui/Pos/Setting/ProductGroup/ChangeProductGroupOrderWindow.xaml.cs @@ -12,7 +12,7 @@ using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; -using Database.Repository; +using Pos.Service; namespace Pos.Setting.Category { @@ -22,6 +22,8 @@ namespace Pos.Setting.Category public partial class ChangeProductGroupOrderWindow : Window { ObservableCollection _prodGroupList = new ObservableCollection(); + private readonly ProductGroupService _productGroupService = + new ProductGroupService(); public ChangeProductGroupOrderWindow() { InitializeComponent(); @@ -30,8 +32,8 @@ namespace Pos.Setting.Category private void LoadData() { - ProductGroupRepository categoryRepository = new ProductGroupRepository(); - List categories = categoryRepository.GetAll(); + List categories = + _productGroupService.GetProductGroups(); foreach (Database.Models.ProductGroupEntity category in categories) { _prodGroupList.Add(category); @@ -84,13 +86,9 @@ namespace Pos.Setting.Category private void btnSave_Click(object sender, RoutedEventArgs e) { - ProductGroupRepository categoryRepository = new ProductGroupRepository(); - for (int i = 0; i < _prodGroupList.Count; i++) - { - Database.Models.ProductGroupEntity productGroup = _prodGroupList[i]; - categoryRepository.SetIndex(productGroup.Id,i); - } - this.Close(); + _productGroupService.UpdateOrder( + _prodGroupList.Select(productGroup => productGroup.Id).ToList()); + Close(); } } } diff --git a/PointOfSale/Pos.Ui/Pos/Setting/ProductGroup/EditProductGroupWindow.xaml.cs b/PointOfSale/Pos.Ui/Pos/Setting/ProductGroup/EditProductGroupWindow.xaml.cs index 4b02f8c..09e8af7 100644 --- a/PointOfSale/Pos.Ui/Pos/Setting/ProductGroup/EditProductGroupWindow.xaml.cs +++ b/PointOfSale/Pos.Ui/Pos/Setting/ProductGroup/EditProductGroupWindow.xaml.cs @@ -11,7 +11,7 @@ using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; -using Database.Repository; +using Pos.Service; namespace Pos.Setting.Category { @@ -21,6 +21,8 @@ namespace Pos.Setting.Category public partial class EditProductGroupWindow : Window { private readonly int _id; + private readonly ProductGroupService _productGroupService = + new ProductGroupService(); public EditProductGroupWindow(string name, int id) { @@ -31,9 +33,21 @@ namespace Pos.Setting.Category private void btnEdit_Click(object sender, RoutedEventArgs e) { - ProductGroupRepository categoryRepository = new ProductGroupRepository(); - categoryRepository.Edit(txtProductGroup.Text,_id); - this.Close(); + try + { + _productGroupService.RenameProductGroup( + _id, + txtProductGroup.Text); + Close(); + } + catch (ArgumentException exception) + { + MessageBox.Show( + exception.Message, + "Ugyldig produktgruppe", + MessageBoxButton.OK, + MessageBoxImage.Warning); + } } } } diff --git a/PointOfSale/Pos.Ui/Pos/Setting/ProductGroup/ProductGroupWindow.xaml.cs b/PointOfSale/Pos.Ui/Pos/Setting/ProductGroup/ProductGroupWindow.xaml.cs index 10cde55..7b55aaf 100644 --- a/PointOfSale/Pos.Ui/Pos/Setting/ProductGroup/ProductGroupWindow.xaml.cs +++ b/PointOfSale/Pos.Ui/Pos/Setting/ProductGroup/ProductGroupWindow.xaml.cs @@ -11,7 +11,7 @@ using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; -using Database.Repository; +using Pos.Service; using Pos.Setting.Category; namespace Pos.Category @@ -23,6 +23,8 @@ namespace Pos.Category { private int _id = -1; private string _name = string.Empty; + private readonly ProductGroupService _productGroupService = + new ProductGroupService(); public ProductGroupWindow() { @@ -53,8 +55,8 @@ namespace Pos.Category private void LoadData() { - ProductGroupRepository categoryRepository = new ProductGroupRepository(); - List categories = categoryRepository.GetAll(); + List categories = + _productGroupService.GetProductGroups(); LstCategory.Items.Clear(); foreach (Database.Models.ProductGroupEntity category in categories) { diff --git a/PointOfSale/Pos.Ui/Pos/Setting/SettingWindow.xaml.cs b/PointOfSale/Pos.Ui/Pos/Setting/SettingWindow.xaml.cs index bfaab83..63b79ac 100644 --- a/PointOfSale/Pos.Ui/Pos/Setting/SettingWindow.xaml.cs +++ b/PointOfSale/Pos.Ui/Pos/Setting/SettingWindow.xaml.cs @@ -11,8 +11,8 @@ using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; -using Database.Repository; using Pos.Category; +using Pos.Service; using Pos.Setting.ProductGroup; namespace Pos.Setting @@ -22,6 +22,9 @@ namespace Pos.Setting /// public partial class SettingWindow : Window { + private readonly ProductGroupService _productGroupService = + new ProductGroupService(); + public SettingWindow() { InitializeComponent(); @@ -42,10 +45,8 @@ namespace Pos.Setting private void btnProductGroups_Click(object sender, RoutedEventArgs e) { - ProductWindow productWindow = new ProductWindow(); //Check if there is any categories, if not close this window with a message. - ProductGroupRepository categoryRepository = new ProductGroupRepository(); - bool anyExist = categoryRepository.Any(); + bool anyExist = _productGroupService.HasProductGroups(); if (!anyExist) { MessageBox.Show("Der er ingen kategorier, opret dem først", "Kategorier", MessageBoxButton.OK, @@ -53,6 +54,7 @@ namespace Pos.Setting } else { + ProductWindow productWindow = new ProductWindow(); productWindow.Show(); } }