Example #1
0
        private async Task <ShareData> GetShareData(Payment payment, SqlConnection conn, SqlTransaction tx)
        {
            ShareData share = new ShareData();
            Dictionary <int, GroupShareData> groups = new Dictionary <int, GroupShareData>();

            using (SqlCommand cmd = _sqlServer.GetSpCommand("payment.LoadShareData", conn, tx))
            {
                cmd.AddIntParam("@PlaceId", payment.PlaceId);
                cmd.AddIntParam("@EmployeeId", payment.EmployeeId);
                cmd.AddDateTime2Param("@PaymentDateTime", payment.PaymentDateTime);
                cmd.AddBitParam("@IsTimeSpecified", payment.IsTimeSpecified);

                SqlParameter PaymentLimitParam         = cmd.AddDecimalParam("@PaymentLimit", 18, 2).Output();
                SqlParameter SystemCommissionParam     = cmd.AddDecimalParam("@SystemCommission", 4, 2).Output();
                SqlParameter IsPlaceActiveParam        = cmd.AddIntParam("@IsPlaceActive").Output();
                SqlParameter PlaceDisplayNameParam     = cmd.AddNVarCharParam("@PlaceDisplayName", 100).Output();
                SqlParameter ShareSchemeHistoryIdParam = cmd.AddIntParam("@ShareSchemeHistoryId").Output();
                SqlParameter PersonalShareParam        = cmd.AddTinyIntParam("@PersonalShare").Output();
                SqlParameter EmployeeFirstNameParam    = cmd.AddNVarCharParam("@EmployeeFirstName", 50).Output();
                SqlParameter EmployeeLastNameParam     = cmd.AddNVarCharParam("@EmployeeLastName", 50).Output();
                SqlParameter EmployeeIsFiredParam      = cmd.AddBitParam("@EmployeeIsFired").Output();

                using (SqlDataReader dr = await cmd.ExecuteReaderAsync())
                {
                    while (dr.Read())
                    {
                        GroupShareData gdata = new GroupShareData();
                        gdata.Name   = dr.GetString("Name");
                        gdata.Id     = dr.GetInt32("GroupId");
                        gdata.Weight = dr.GetByte("GroupWeight");
                        groups.Add(gdata.Id, gdata);
                        share.Groups.Add(gdata);
                    }

                    dr.NextResult();

                    while (dr.Read())
                    {
                        MembershipData md = new MembershipData();
                        md.EmployeeId    = dr.GetInt32("EmployeeId");
                        md.GroupId       = dr.GetInt32("GroupId");
                        md.BeginDateTime = dr.GetDateTime("BeginDateTime");
                        md.EndDateTime   = dr.GetDateTime("EndDateTime");
                        md.IsManager     = dr.GetBoolean("IsManager");
                        md.IsOwner       = dr.GetBoolean("IsOwner");
                        share.Memberships.Add(md);

                        groups[md.GroupId].AddMembership(md);
                    }
                }

                share.PaymentLimit         = PaymentLimitParam.GetDecimal();
                share.SystemCommission     = SystemCommissionParam.GetDecimal();
                share.Place.Id             = payment.PlaceId;
                share.Place.Name           = PlaceDisplayNameParam.Value.ToString();
                share.Place.IsActive       = (IsPlaceActiveParam.GetInt32() != 0);
                share.ShareSchemeHistoryId = ShareSchemeHistoryIdParam.GetInt32();
                share.PersonalShare        = PersonalShareParam.GetByte();

                if (payment.EmployeeId.HasValue)
                {
                    share.Receiver           = new ReceiverData();
                    share.Receiver.Id        = payment.EmployeeId.Value;
                    share.Receiver.FirstName = EmployeeFirstNameParam.Value.ToString();
                    share.Receiver.LastName  = EmployeeLastNameParam.Value.ToString();
                    share.Receiver.IsFired   = EmployeeIsFiredParam.GetBooleanOrDefault();
                }
            }

            return(share);
        }
Example #2
0
        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);
        }