public void UpdateRoomBillingEntries(DateTime period, int clientId, int roomId, int accountId, decimal entries) { RoomBilling rb = Session.Query <RoomBilling>().FirstOrDefault(x => x.Period == period && x.ClientID == clientId && x.RoomID == roomId && x.AccountID == accountId); if (rb == null) { throw new Exception($"Cannot find RoomBilling record for Period = #{period:yyyy-MM-dd}#, ClientID = {clientId}, RoomID = {roomId}, AccountID = {accountId}"); } rb.Entries = entries; RoomBillingUserApportionData appData = Session.Query <RoomBillingUserApportionData>().FirstOrDefault(x => x.Period == period && x.Client.ClientID == clientId && x.Room.RoomID == roomId && x.Account.AccountID == accountId); if (appData == null) { appData = new RoomBillingUserApportionData() { Client = Require <Client>(rb.ClientID), Account = Require <Account>(rb.AccountID), Room = Require <Room>(rb.RoomID), Period = period, ChargeDays = 0, Entries = 0 }; Session.Save(appData); } else { appData.Entries = entries; Session.Update(appData); } }
public int UpdateChildRoomEntryApportionment(DateTime period, int clientId, int parentRoomId) { int result = 0; Room parentRoom = Session.Get <Room>(parentRoomId); //make sure this really is a parent room if (parentRoom.ParentID == null) { //need to know the child rooms Room[] children = Session.Query <Room>().Where(x => x.ParentID == parentRoomId).ToArray(); //continue only if there are children rooms if (children.Length > 0) { //entries and physical days for each child room var entries = Session.Query <RoomData>() .Where(x => x.Period == period && x.ClientID == clientId && x.ParentID == parentRoomId) .GroupBy(x => x.RoomID) .Select(g => new { RoomID = g.Key, TotalEntries = g.Sum(n => n.Entries), PhysicalDays = (decimal)g.Select(n => n.EvtDate).Distinct().Count() }) .ToArray(); //child room apportionment records, this is what we'll be updating RoomApportionment[] childAppor = Session.Query <RoomApportionment>().Where(x => x.Room.ParentID == parentRoomId && x.Period == period && x.Client.ClientID == clientId).ToArray(); //also update the child user apportionment data RoomBillingUserApportionData[] childUserAppor = Session.Query <RoomBillingUserApportionData>().Where(x => x.Room.ParentID == parentRoomId && x.Period == period && x.Client.ClientID == clientId).ToArray(); //parent room apportionment records RoomApportionment[] parentAppor = Session.Query <RoomApportionment>().Where(x => x.Period == period && x.Client.ClientID == clientId && x.Room == parentRoom).ToArray(); //get the pct for each acct based on parent room day apporiontment foreach (RoomApportionment pa in parentAppor) { decimal pct = Convert.ToDecimal(Math.Round(pa.ChargeDays / pa.PhysicalDays, 3, MidpointRounding.AwayFromZero)); //get the total for each child room foreach (Room child in children) { var e = entries.FirstOrDefault(x => x.RoomID == child.RoomID); //e will be null if they didn't go into a room (e.g. wet chem) if (e != null) { //the pct for this acct decimal chargeDays = e.PhysicalDays * pct; decimal chargeEntries = Convert.ToDecimal(e.TotalEntries) * pct; //get the child RoomApportionment record RoomApportionment ca = childAppor.FirstOrDefault(x => x.Room == child && x.Account == pa.Account); if (ca != null) { //update the record ca.Entries = chargeEntries; ca.ChargeDays = chargeDays; result++; } //update/insert the user apportionment data RoomBillingUserApportionData uad = childUserAppor.FirstOrDefault(x => x.Room == child && x.Account == pa.Account); if (uad != null) { uad.Entries = chargeEntries; uad.ChargeDays = chargeDays; } else { //create the record if it does not exist uad = new RoomBillingUserApportionData() { Client = pa.Client, Account = pa.Account, Room = child, Period = period, ChargeDays = chargeDays, Entries = chargeEntries }; Session.Save(uad); } } } } } } return(result); }