public async Task PurchaseItems(List <ItemModel> shoppingCart) { StoreModel store = await _storeData.LoadStore(_config.Configuration.GetSection("Store:Name").Value); foreach (ItemModel item in shoppingCart) { var PayementDueFromDbList = await _config.Connection.QueryRawSQL <decimal, dynamic>($"select PaymentDue from Vendors where Id = {item.Owner.Id};", new { }); decimal paymentDueFromDb = PayementDueFromDbList.First(); item.Owner.PaymentDue += paymentDueFromDb; item.Sold = true; item.Owner.PaymentDue += (decimal)item.Owner.CommissionRate * item.Price; store.StoreProfit += (1 - (decimal)item.Owner.CommissionRate) * item.Price; store.StoreBank += item.Price; await _itemData.UpdateItem(item); await _vendorData.UpdateVendor(item.Owner); await _storeData.UpdateStore(store); } }
public async Task PayVendor(VendorModel vendor) { if (vendor == null) { throw new ArgumentNullException(nameof(vendor), "Vendor cannot be null."); } string storeName = _config.Configuration.GetSection("Store:Name").Value; StoreModel store = await _storeData.LoadStore(storeName); var itemsOwnedByVendor = await _itemData.LoadSoldItemsByVendor(vendor); foreach (ItemModel item in itemsOwnedByVendor) { if (!item.PaymentDistributed) { //var AmountOwedFromDbList = await GlobalConfig.Connection.QueryRawSQL<decimal>($"select PaymentDue from Vendors where Id = {item.Owner.Id};"); //decimal paymentDueFromDb = AmountOwedFromDbList.First(); //item.Owner.PaymentDue = paymentDueFromDb; decimal amountOwed = (decimal)item.Owner.CommissionRate * item.Price; if (store.StoreBank >= amountOwed) { store.StoreBank -= amountOwed; vendor.PaymentDue -= amountOwed; item.PaymentDistributed = true; } else { throw new InvalidOperationException("The store bank does not contain enough money to pay the vendor!"); } } await _itemData.UpdateItem(item); await _vendorData.UpdateVendor(vendor); } _storeData.UpdateStore(store); }
private async void btnAddVendor_Click(object sender, System.EventArgs e) { VendorModel output = null; if (!ValidateData()) { return; } if (_editing) { _editingVendor.FirstName = textBoxFirstName.Text; _editingVendor.LastName = textBoxLastName.Text; _editingVendor.CommissionRate = double.Parse(textBoxCommison.Text) / 100; btnAddVendor.Text = "Add Vendor"; btnEdit.Enabled = true; _editing = false; output = _editingVendor; textBoxCommison.Enabled = true; _vendorData.UpdateVendor(output); } else { output = new VendorModel() { FirstName = textBoxFirstName.Text, LastName = textBoxLastName.Text, CommissionRate = double.Parse(textBoxCommison.Text) / 100 }; await _vendorData.CreateVendor(output); } UpdateVendors(); ClearVendorTextBoxes(); }