private void CalculateAmounts(Payment payment, ShareData share) { if (payment.OriginalAmount > share.PaymentLimit) { payment.Status = PaymentStatus.ToReturn; payment.ReasonToReturn = ReasonToReturnType.AmountLimitExceeded; return; } if (!share.Place.IsActive) { payment.Status = PaymentStatus.ToReturn; payment.ReasonToReturn = ReasonToReturnType.PlaceInactive; return; } decimal systemAmount = decimal.Floor(payment.OriginalAmount * share.SystemCommission) / 100M; decimal employeesAmount = payment.OriginalAmount - systemAmount; AmountData amountData = this.CalculateEmployeeAmounts(employeesAmount, share, payment); payment.PayoutAmount = amountData.PayoutAmount; payment.ShareSchemeHistoryId = share.ShareSchemeHistoryId; foreach (PersonalAmountData pad in amountData.PersonalAmounts.Values) { if (pad.Amount > 0M) { payment.PersonalAmounts.Add(new PersonalAmount { EmployeeId = pad.EmployeeId, Amount = pad.Amount }); } } }
private AmountData CalculateEmployeeAmounts(decimal amount, ShareData share, Payment payment) { AmountData amountData = new AmountData(); decimal personalAmount = 0M; if (share.Receiver != null && !share.Receiver.IsFired) { personalAmount = decimal.Floor(amount * share.PersonalShare) / 100M; amountData.AddPersonalAmount(share.Receiver.Id, personalAmount); } decimal groupAmount = amount - personalAmount; if (groupAmount == 0M) { return(amountData); } decimal totalWeight = 0M; foreach (GroupShareData groupShare in share.Groups) { groupShare.CalculateGroupWeight(payment.IsTimeSpecified); totalWeight += groupShare.GroupWeight; } if (totalWeight == 0M) { if (share.Receiver == null) { foreach (GroupShareData groupShare in share.Groups) { groupShare.Weight = 1M; groupShare.CalculateGroupWeight(payment.IsTimeSpecified); totalWeight += groupShare.GroupWeight; } } if (totalWeight == 0M) { if (share.Receiver == null || share.Receiver.IsFired) { payment.Status = PaymentStatus.ToReturn; payment.ReasonToReturn = ReasonToReturnType.NoWeightsToShare; } else { amountData.AddPersonalAmount(share.Receiver.Id, groupAmount); } return(amountData); } } if (payment.IsTimeSpecified) { foreach (GroupShareData groupShare in share.Groups) { decimal planGroupAmount = groupAmount * groupShare.GroupWeight / totalWeight; if (planGroupAmount == 0M) { continue; } decimal count = (decimal)groupShare.Memberships.GetMemberCount(); if (count == 0M) { continue; } foreach (int employeeId in groupShare.Memberships.EnumerateMembers()) { amountData.AddPersonalAmount(employeeId, planGroupAmount / count); } } } else { foreach (GroupShareData groupShare in share.Groups) { decimal planGroupAmount = groupAmount * groupShare.GroupWeight / totalWeight; if (planGroupAmount == 0M) { continue; } foreach (MembershipData md in groupShare.Memberships.EnumeratePartsOfDay()) { if (md.PartOfDay == 0M || groupShare.GroupTotalDays == 0M) { continue; } amountData.AddPersonalAmount(md.EmployeeId, planGroupAmount * md.PartOfDay / groupShare.GroupTotalDays); } } } if (amountData.PersonalAmounts.Count == 1) { amountData.PersonalAmounts.First().Value.Amount = amount; } else { foreach (PersonalAmountData pad in amountData.PersonalAmounts.Values) { pad.NormalizeAmount(); amountData.PayoutAmount += pad.Amount; } } return(amountData); }