private void Increase(int n) { if (IsTook) { _progress.Current += n; _progress.CheckCurrent(); OnPropertyChanged(nameof(Progress)); SharedWith?.SharedIncrease(n); } }
public async Task <bool> UpdateBillAsync(BillResponse billResponse) { Bill bill = new Bill(); if (billResponse.groupId != 0) { bill.groupId = billResponse.groupId; } bill.billid = billResponse.billid; bill.bill_created_at = billResponse.bill_created_at; bill.bill_created_byId = billResponse.bill_created_byId; bill.bill_date = billResponse.bill_date; bill.bill_updated_at = billResponse.bill_updated_at; bill.description = billResponse.description; bill.total_amount = billResponse.total_amount; bill.bill_updated_byId = billResponse.bill_updated_byId; _Context.bills.Attach(bill); _Context.Entry(bill).State = EntityState.Modified; int payer_count = (from n in _Context.payers where n.billId == billResponse.billid select n).Count(); for (int i = 0; i < payer_count; i++) { var pay = await _Context.payers .FirstOrDefaultAsync(c => c.billId == billResponse.billid); _Context.payers.Remove(pay); await _Context.SaveChangesAsync(); } foreach (var x in billResponse.payers) { Payer payer = new Payer(); payer.billId = bill.billid; payer.paid_byId = x.paid_byId; payer.amount_paid = x.amount_paid; _Context.payers.Add(payer); } int shared_count = (from n in _Context.sharedWiths where n.billId == billResponse.billid select n).Count(); for (int i = 0; i < shared_count; i++) { var pay = await _Context.sharedWiths .FirstOrDefaultAsync(c => c.billId == billResponse.billid); _Context.sharedWiths.Remove(pay); await _Context.SaveChangesAsync(); } foreach (var x in billResponse.sharedwiths) { SharedWith sharedWith = new SharedWith(); sharedWith.billId = bill.billid; sharedWith.shared_withId = x.shared_withId; sharedWith.owes_amount = x.owes_amount; _Context.sharedWiths.Add(sharedWith); } try { return(await _Context.SaveChangesAsync() > 0 ? true : false); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(UpdateBillAsync)}: " + exp.Message); } return(false); }
public async Task <Bill> AddNewBillAsync(BillResponse billResponse) { Bill bill = new Bill(); if (billResponse.groupId != 0) { bill.groupId = billResponse.groupId; } bill.bill_created_at = billResponse.bill_created_at; bill.bill_created_byId = billResponse.bill_created_byId; bill.bill_date = billResponse.bill_date; bill.bill_updated_at = billResponse.bill_updated_at; bill.description = billResponse.description; bill.total_amount = billResponse.total_amount; bill.bill_updated_byId = billResponse.bill_updated_byId; _Context.bills.Add(bill); try { await _Context.SaveChangesAsync(); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(AddNewBillAsync)}: " + exp.Message); } //int id = bill.billid; foreach (var x in billResponse.payers) { Payer payer = new Payer(); payer.billId = bill.billid; payer.paid_byId = x.paid_byId; payer.amount_paid = x.amount_paid; _Context.payers.Add(payer); try { await _Context.SaveChangesAsync(); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(AddNewBillAsync)}: " + exp.Message); } } foreach (var x in billResponse.sharedwiths) { SharedWith sharedWith = new SharedWith(); sharedWith.billId = bill.billid; sharedWith.shared_withId = x.shared_withId; sharedWith.owes_amount = x.owes_amount; _Context.sharedWiths.Add(sharedWith); try { await _Context.SaveChangesAsync(); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(AddNewBillAsync)}: " + exp.Message); } } foreach (var x in billResponse.settlements) { if (billResponse.groupId != 0) { Settlements settlements = new Settlements(); settlements = await _Context.settlements .SingleOrDefaultAsync(c => c.groupId == billResponse.groupId && c.payeeId == x.payeeId && c.payerId == x.payerId); if (settlements != null) { settlements.payerId = x.payerId; settlements.payeeId = x.payeeId; settlements.amount = settlements.amount + x.paid_amount; settlements.groupId = billResponse.groupId; _Context.settlements.Attach(settlements); _Context.Entry(settlements).State = EntityState.Modified; } else { settlements = await _Context.settlements .SingleOrDefaultAsync(c => c.groupId == billResponse.groupId && c.payeeId == x.payerId && c.payerId == x.payeeId); if (settlements != null) { if (settlements.amount > x.paid_amount) { settlements.payerId = x.payeeId; settlements.payeeId = x.payerId; settlements.groupId = billResponse.groupId; settlements.amount = settlements.amount - x.paid_amount; _Context.settlements.Attach(settlements); _Context.Entry(settlements).State = EntityState.Modified; } else { settlements.payerId = x.payerId; settlements.payeeId = x.payeeId; settlements.groupId = billResponse.groupId; settlements.amount = x.paid_amount - settlements.amount; _Context.settlements.Attach(settlements); _Context.Entry(settlements).State = EntityState.Modified; } } else { Settlements settlements1 = new Settlements(); settlements1.payerId = x.payerId; settlements1.payeeId = x.payeeId; settlements1.amount = x.paid_amount; settlements1.groupId = billResponse.groupId; _Context.settlements.Add(settlements1); } } } else { Settlements settlements = new Settlements(); settlements = await _Context.settlements .FirstOrDefaultAsync(c => c.groupId == bill.groupId && c.payeeId == x.payeeId && c.payerId == x.payerId); if (settlements != null) { settlements.payerId = x.payerId; settlements.payeeId = x.payeeId; settlements.amount = settlements.amount + x.paid_amount; _Context.settlements.Attach(settlements); _Context.Entry(settlements).State = EntityState.Modified; } else { settlements = await _Context.settlements .FirstOrDefaultAsync(c => c.groupId == bill.groupId && c.payeeId == x.payerId && c.payerId == x.payeeId); if (settlements != null) { if (settlements.amount > x.paid_amount) { settlements.payerId = x.payeeId; settlements.payeeId = x.payerId; settlements.amount = settlements.amount - x.paid_amount; _Context.settlements.Attach(settlements); _Context.Entry(settlements).State = EntityState.Modified; } else { settlements.payerId = x.payerId; settlements.payeeId = x.payeeId; settlements.amount = x.paid_amount - settlements.amount; _Context.settlements.Attach(settlements); _Context.Entry(settlements).State = EntityState.Modified; } } else { Settlements settlements1 = new Settlements(); settlements1.payerId = x.payerId; settlements1.payeeId = x.payeeId; settlements1.amount = x.paid_amount; // settlements1.groupId = billResponse.groupId; _Context.settlements.Add(settlements1); } } } try { await _Context.SaveChangesAsync(); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(AddNewBillAsync)}: " + exp.Message); } } for (int i = 0; i < billResponse.sharedwiths.Count; i++) { for (int j = i + 1; j < billResponse.sharedwiths.Count; j++) { Friend friend = new Friend(); var frdexist = _Context.friends.SingleOrDefault( c => (c.userId == billResponse.sharedwiths[i].shared_withId && c.friendId == billResponse.sharedwiths[j].shared_withId) || (c.userId == billResponse.sharedwiths[j].shared_withId && c.friendId == billResponse.sharedwiths[i].shared_withId)); if (frdexist == null) { friend.friendId = billResponse.sharedwiths[j].shared_withId; friend.userId = billResponse.sharedwiths[i].shared_withId; _Context.friends.Add(friend); try { await _Context.SaveChangesAsync(); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(AddNewBillAsync)}: " + exp.Message); } } } } return(bill); }