public decimal currentBalance(string Type, decimal balance, VMMixData data) { if (Type == "purchase") { return(balance - (data.unitPrice * data.quantity)); } if (Type == "sales") { return(balance + (data.unitPrice * data.quantity));; } if (Type == "payment") { return(balance + data.amount); } if (Type == "collection") { return(balance - data.amount); } return(0); }
public bool updateBalanceHistory(int orderID, int vendorID, string Type, bool edit = false, decimal amount = 0) { BalanceHistory balanceHistory = new BalanceHistory(); VMMixData mixData = new VMMixData(); decimal vendorCurrentInitialBalance = 0; var isDateExist = db.BalanceHistory.Where(x => x.Date.Day == DateTime.Now.Day && x.Date.Month == DateTime.Now.Month && x.Date.Year == DateTime.Now.Year && x.UserMasId == vendorID).ToList(); var userHistory = db.BalanceHistory.Where(x => x.UserMasId == vendorID).ToList(); BalanceHistory lastHistory = new BalanceHistory(); PurchaseOrder PurchaseData = new PurchaseOrder(); SalesOrder SalesData = new SalesOrder(); Payment PaymentOrCollectionData = new Payment(); if (Type == "purchase") { PurchaseData = db.PurchaseOrder.SingleOrDefault(x => x.Id == orderID); mixData.quantity = PurchaseData.OrderQuantity; mixData.unitPrice = PurchaseData.UnitPrice; } if (Type == "sales") { SalesData = db.SalesOrder.SingleOrDefault(x => x.Id == orderID); mixData.quantity = SalesData.OrderQuantity; mixData.unitPrice = SalesData.UnitPrice; } if ((Type == "payment" || Type == "collection") && edit == false) { PaymentOrCollectionData = db.Payment.SingleOrDefault(x => x.Id == orderID); mixData.amount = PaymentOrCollectionData.Amount; } else if ((Type == "payment" || Type == "collection") && edit == true) { PaymentOrCollectionData = db.Payment.SingleOrDefault(x => x.Id == orderID); mixData.amount = amount; } var update = false; if (userHistory.Count > 0) { if (isDateExist.Count > 0) { update = true; lastHistory = isDateExist[0]; balanceHistory = lastHistory; vendorCurrentInitialBalance = currentBalance(Type, lastHistory.initialBalance, mixData); } else { lastHistory = userHistory[userHistory.Count - 1]; vendorCurrentInitialBalance = currentBalance(Type, lastHistory.initialBalance, mixData); } } else { vendorCurrentInitialBalance = db.UserMas.SingleOrDefault(userMas => userMas.Id == vendorID).InitialBalance; vendorCurrentInitialBalance = currentBalance(Type, lastHistory.initialBalance, mixData); } balanceHistory.UserMasId = vendorID; balanceHistory.initialBalance = vendorCurrentInitialBalance; balanceHistory.Date = DateTime.Now; if (update) { db.Entry(balanceHistory).State = EntityState.Modified; db.SaveChanges(); } else { db.BalanceHistory.Add(balanceHistory); db.SaveChanges(); } return(true); }