Пример #1
0
 public ActionResult Create(Payment payment)
 {
     if (ModelState.IsValid)
     {
         payment.TransactionDateTime = DateTime.Now;
         db.Payments.Add(payment);
         db.SaveChanges();
         return Content(Boolean.TrueString);
     }
     return Content("Please review your form");
 }
        public ActionResult Save(Dictionary<string,string> groups, int GroupInstanceId, string Date)
        {
            var InstanceDate = Convert.ToDateTime(Date);
            //Jquery only sends string,string.
            Dictionary<int, AttendanceType> groupsParsed = groups.ToDictionary(x => Convert.ToInt32(x.Key), x => (AttendanceType)Convert.ToInt32(x.Value));
            var existingAttendance = db.UserGroupInstances.Include(x=>x.User)
                                     .Where(x => x.GroupInstanceId == GroupInstanceId).ToList()
                                     .Where(x => x.InstanceDateTime.Date == InstanceDate.Date);

            var GroupInstance = db.GroupInstances.Include(x=>x.Group).Where(x => x.GroupInstanceId == GroupInstanceId).First();
            var group = GetLatestGroup(GroupInstance.GroupId);
            var PaymentProfileId = group.PaymentProfileId;
            var paymentRule = db.PaymentRules.Where(x => x.PaymentProfileId == PaymentProfileId && x.EffectiveDate <= DateTime.Today).FirstOrDefault();
            var amount = 0.0m;
            if (paymentRule != null)
            {
                amount = paymentRule.Amount;
            }
            foreach (KeyValuePair<int, AttendanceType> kvp in groupsParsed)
            {
                var currentAttendance = existingAttendance.Where(x => x.UserId == kvp.Key).FirstOrDefault();
                if (currentAttendance!=null)
                {
                    if (currentAttendance.Present == kvp.Value)
                    {
                        continue;
                    }
                    else
                    {
                        if (currentAttendance.Present == AttendanceType.Absent || currentAttendance.Present == AttendanceType.Present)
                        {
                            if (kvp.Value == AttendanceType.AbsentWithExcuse || kvp.Value == AttendanceType.NA)
                            {
                                var Payment = new Payment()
                                {
                                    comments = "Посещение изменено с \"" +
                                                    currentAttendance.Present.GetDescription() +
                                                    "\" на \"" + kvp.Value.GetDescription() +
                                                    "\"  " + InstanceDate +
                                                    "- Возвращено через страницу посещений (" + Membership.GetUser().UserName + ")",
                                    TransactionDateTime = DateTime.Now,
                                    UserId = kvp.Key,
                                    Amount = amount
                                };
                                db.Payments.Add(Payment);
                                db.SaveChanges();
                            }
                        }
                        if (currentAttendance.Present == AttendanceType.AbsentWithExcuse)
                        {
                            if (kvp.Value == AttendanceType.Absent || kvp.Value == AttendanceType.Present)
                            {
                                var Payment = new Payment()
                                {
                                    comments = "Посещение изменено с \"" +
                                                    currentAttendance.Present.GetDescription() +
                                                    "\" на \"" + kvp.Value.GetDescription() +
                                                    "\"  " + InstanceDate +
                                                    "- Списано через страницу посещений (" + Membership.GetUser().UserName + ")",
                                    TransactionDateTime = DateTime.Now,
                                    UserId = kvp.Key,
                                    Amount = amount * -1
                                };
                                db.Payments.Add(Payment);
                                db.SaveChanges();
                            }
                        }
                        if (currentAttendance.Present == AttendanceType.NA)
                        {
                            if (kvp.Value == AttendanceType.Absent || kvp.Value == AttendanceType.Present)
                            {
                                var Payment = new Payment()
                                {
                                    comments = kvp.Value.GetDescription() + "  " + InstanceDate + "- Списано через страницу посещений (" + Membership.GetUser().UserName + ")",
                                    TransactionDateTime = DateTime.Now,
                                    UserId = kvp.Key,
                                    Amount = amount * -1
                                };
                                db.Payments.Add(Payment);
                                db.SaveChanges();
                            }
                        }
                        currentAttendance.Present = kvp.Value;
                        db.Entry(currentAttendance).State = EntityState.Modified;
                        db.SaveChanges();

                    }
                }
                else
                {
                    var newInstance = new UserGroupInstance() { UserId = kvp.Key,
                                                                InstanceDateTime = InstanceDate,
                                                                Present = kvp.Value,
                                                                GroupInstanceId = GroupInstanceId,
                                                                AttendanceTaken= DateTime.Now};
                    db.UserGroupInstances.Add(newInstance);
                    db.SaveChanges();
                    if (kvp.Value == AttendanceType.Absent || kvp.Value == AttendanceType.Present)
                    {
                        var Payment = new Payment()
                        {
                            comments =  kvp.Value.GetDescription() + "  " + InstanceDate + "- Списано через страницу посещений (" + Membership.GetUser().UserName + ")",
                            TransactionDateTime = DateTime.Now,
                            UserId = newInstance.UserId,
                            Amount = amount * -1
                        };
                        db.Payments.Add(Payment);
                        db.SaveChanges();
                    }
                }
                //var groupInstance = groupInstances.Where(x => x.UserGroupInstanceID == kvp.Key).First();
                //var PaymentProfileId = db.GroupInstances.Include(x=>x.Group).Where(x => x.GroupInstanceId == groupInstance.GroupInstanceId).First().Group.PaymentProfileId;
                //var amount = db.PaymentRules.Where(x => x.PaymentProfileId == PaymentProfileId && x.EffectiveDate <= DateTime.Today).First().Amount;
                //groupInstance.Present = kvp.Value;
                //if (kvp.Value == AttendanceType.Absent || kvp.Value == AttendanceType.Present)
                //{
                //    Payment payment = new Payment();
                //    payment.Amount = amount*-1;
                //    payment.comments = "Списано через страницу посещений (" + Membership.GetUser().UserName + ")";
                //    payment.UserId = groupInstance.UserId;
                //    payment.TransactionDateTime = DateTime.Now;
                //    db.Payments.Add(payment);
                //}
                //db.Entry(groupInstance).State = EntityState.Modified;
            }
            db.SaveChanges();
            return Content(Boolean.TrueString);
        }
Пример #3
0
 public ActionResult Edit(Payment payment)
 {
     if (ModelState.IsValid)
     {
         db.Entry(payment).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     ViewBag.UserId = new SelectList(db.UserProfiles, "UserId", "UserName", payment.UserId);
     return View(payment);
 }