Пример #1
0
        // POST api/<controller>
        public async Task <HttpResponseMessage> Post([FromBody] CampaignMiniDonation viewModel)
        {
            try
            {
                MiniDonationModel model = await serve.AddCampaignDonation(viewModel);

                ResponseObject response = new ResponseObject();
                response.campaignId  = viewModel.CampaignId.ToString();
                response.ResponseMsg = "Campaign Donation with order id created successfully";

                return(Request.CreateResponse(HttpStatusCode.OK, model));
            }
            catch (Exception ex)
            {
                ResponseObject response = new ResponseObject();
                response.ExceptionMsg = ex.ToString();
                response.ResponseMsg  = "Campaign Donation with order id creation - is not successful";
                response.ErrorCode    = HttpStatusCode.InternalServerError.ToString();
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, response));
            }
        }
        public async Task <MiniDonationModel> AddCampaignDonation(CampaignMiniDonation viewModel)
        {
            try
            {
                if (viewModel.id == 0)
                {
                    Tbl_CampaignDonation donation = new Tbl_CampaignDonation();
                    donation.StoryId      = viewModel.CampaignId;
                    donation.DonatedBy    = viewModel.DonorName;
                    donation.DonatedOn    = DateTime.Now;
                    donation.MoneyType    = viewModel.DonationMoneyType;
                    donation.DonationAmnt = viewModel.DonationAmnt;
                    donation.PhoneNumber  = viewModel.PhoneNumber;
                    donation.Email        = viewModel.EMail;
                    donation.isAnanymous  = viewModel.isAnanymous;
                    string RazorPayProcessingFeePerc = ConfigurationManager.AppSettings["RazorPayProcessingFeePerc"];

                    string RazorPayGSTPerc = ConfigurationManager.AppSettings["RazorPayGSTPerc"];
                    donation.PayGSTPerc           = Convert.ToInt32(Convert.ToDecimal(RazorPayGSTPerc) * 100);
                    donation.PayProcessingFeePerc = Convert.ToInt32(Convert.ToDecimal(RazorPayProcessingFeePerc) * 100);
                    donation.PayProcessingFeeAmnt = viewModel.DonationAmnt * Convert.ToDecimal(RazorPayProcessingFeePerc);

                    donation.PayGSTAmnt      = donation.PayProcessingFeeAmnt * Convert.ToDecimal(RazorPayGSTPerc);
                    donation.PaidDOnationAmt = viewModel.DonationAmnt - (donation.PayProcessingFeeAmnt + donation.PayGSTAmnt);
                    donation.Status          = true;
                    Entity.Tbl_CampaignDonation.Add(donation);
                    Entity.SaveChanges();
                    var donateId = donation.Id;

                    MiniDonationModel model = new MiniDonationModel();
                    model.Amount     = viewModel.DonationAmnt;
                    model.MType      = viewModel.DonationMoneyType;
                    model.DonateId   = donateId;
                    model.PhNo       = viewModel.PhoneNumber;
                    model.Email      = viewModel.EMail;
                    model.campaignId = viewModel.CampaignId;

                    model = CreateOrderRazorPay(model);


                    var CurrentDetail = await(from cm in Entity.Tbl_Campaign.AsNoTracking()
                                              join Dn in Entity.Tbl_CampaignDonation.AsNoTracking() on cm.Id equals Dn.StoryId into outerJoinDon
                                              from Dn in outerJoinDon.DefaultIfEmpty()
                                              where cm.Id == viewModel.CampaignId
                                              select new
                    {
                        CampaignId = cm.Id,
                        cm.TargetAmount,
                        DonationId   = Dn != null ? Dn.Id : 0,
                        DonatedBy    = Dn != null ? Dn.DonatedBy : "",
                        DonatedOn    = Dn != null ? Dn.DonatedOn : DateTime.Now,
                        isAnanymous  = Dn != null ? Dn.isAnanymous : true,
                        DonationAmnt = Dn != null ? Dn.DonationAmnt : 0,
                        isPaid       = Dn != null ? Dn.isPaid : false
                    }).ToListAsync();

                    var DonList = CurrentDetail.Where(x => x.isPaid == true).GroupBy(x => x.CampaignId)
                                  .Select(g => new
                    {
                        DonatedBy = g.Key,
                        Total     = g.Sum(x => x.DonationAmnt)
                    });

                    var DonorList = CurrentDetail.Where(x => x.isPaid == true)
                                    .GroupBy(x => x.DonatedBy)
                                    .Select(g => new
                    {
                        DonatedBy = g.Key,
                        Total     = g.Sum(x => x.DonationAmnt)
                    });

                    var targetAmount = CurrentDetail.FirstOrDefault() != null?CurrentDetail.FirstOrDefault().TargetAmount.Value : 0;

                    var     RaisedAmt  = DonList.Any() ? DonList.ToList().FirstOrDefault().Total : 0;
                    decimal difference = targetAmount - RaisedAmt;
                    var     RaisedBy   = DonorList.Any() ? DonorList.Distinct().ToList().Count() : 0;
                    model.TotalDonorsCount = RaisedBy;
                    model.CanDonate        = difference > 0 ? true : false;
                    model.TotalRaisedAmnt  = RaisedAmt;
                    model.EligibleTarget   = difference > 0 ? difference : 0;

                    return(model);
                }
                else
                {
                    var donation = (from S in Entity.Tbl_CampaignDonation where S.Id == viewModel.id select S).FirstOrDefault();

                    if (donation != null)
                    {
                        donation.DonatedBy    = viewModel.DonorName;
                        donation.DonatedOn    = DateTime.Now;
                        donation.MoneyType    = viewModel.DonationMoneyType;
                        donation.DonationAmnt = viewModel.DonationAmnt;
                        donation.PhoneNumber  = viewModel.PhoneNumber;
                        donation.Email        = viewModel.EMail;
                        donation.isAnanymous  = viewModel.isAnanymous;
                        string RazorPayProcessingFeePerc = ConfigurationManager.AppSettings["RazorPayProcessingFeePerc"];

                        string RazorPayGSTPerc = ConfigurationManager.AppSettings["RazorPayGSTPerc"];
                        donation.PayGSTPerc           = Convert.ToInt32(Convert.ToDecimal(RazorPayGSTPerc) * 100);
                        donation.PayProcessingFeePerc = Convert.ToInt32(Convert.ToDecimal(RazorPayProcessingFeePerc) * 100);
                        donation.PayProcessingFeeAmnt = viewModel.DonationAmnt * Convert.ToDecimal(RazorPayProcessingFeePerc);

                        donation.PayGSTAmnt      = donation.PayProcessingFeeAmnt * Convert.ToDecimal(RazorPayGSTPerc);
                        donation.PaidDOnationAmt = viewModel.DonationAmnt - (donation.PayProcessingFeeAmnt + donation.PayGSTAmnt);
                        donation.Status          = true;
                        donation.UpdatedOn       = DateTime.UtcNow;
                        Entity.SaveChanges();
                        MiniDonationModel model = new MiniDonationModel();
                        model.Amount   = viewModel.DonationAmnt;
                        model.MType    = viewModel.DonationMoneyType;
                        model.DonateId = viewModel.id;
                        model.PhNo     = viewModel.PhoneNumber;
                        model.Email    = viewModel.EMail;
                        model          = CreateOrderRazorPay(model);


                        var CurrentDetail = await(from cm in Entity.Tbl_Campaign.AsNoTracking()
                                                  join Dn in Entity.Tbl_CampaignDonation.AsNoTracking() on cm.Id equals Dn.StoryId into outerJoinDon
                                                  from Dn in outerJoinDon.DefaultIfEmpty()
                                                  where cm.Id == viewModel.CampaignId
                                                  select new
                        {
                            CampaignId = cm.Id,
                            cm.TargetAmount,
                            DonationId   = Dn != null ? Dn.Id : 0,
                            DonatedBy    = Dn != null ? Dn.DonatedBy : "",
                            DonatedOn    = Dn != null ? Dn.DonatedOn : DateTime.Now,
                            isAnanymous  = Dn != null ? Dn.isAnanymous : true,
                            DonationAmnt = Dn != null ? Dn.DonationAmnt : 0,
                            isPaid       = Dn != null ? Dn.isPaid : false
                        }).ToListAsync();

                        var DonList = CurrentDetail.Where(x => x.isPaid == true).GroupBy(x => x.CampaignId)
                                      .Select(g => new
                        {
                            DonatedBy = g.Key,
                            Total     = g.Sum(x => x.DonationAmnt)
                        });

                        var DonorList = CurrentDetail.Where(x => x.isPaid == true)
                                        .GroupBy(x => x.DonatedBy)
                                        .Select(g => new
                        {
                            DonatedBy = g.Key,
                            Total     = g.Sum(x => x.DonationAmnt)
                        });

                        var targetAmount = CurrentDetail.FirstOrDefault() != null?CurrentDetail.FirstOrDefault().TargetAmount.Value : 0;

                        var     RaisedAmt  = DonList.Any() ? DonList.ToList().FirstOrDefault().Total : 0;
                        decimal difference = targetAmount - RaisedAmt;
                        var     RaisedBy   = DonorList.Any() ? DonorList.Distinct().ToList().Count() : 0;
                        model.TotalDonorsCount = RaisedBy;
                        model.CanDonate        = difference > 0 ? true : false;
                        model.TotalRaisedAmnt  = RaisedAmt;
                        model.EligibleTarget   = difference > 0 ? difference : 0;
                        return(model);
                    }
                    else
                    {
                        return(new MiniDonationModel());
                    }
                }
            }
            catch (Exception ex) { throw ex; }
        }