示例#1
0
        public async Task <UpsertModel> DeleteMessage()
        {
            var upsert = new UpsertModel();

            try
            {
                string title = "Message Deleted";
                System.Text.StringBuilder builder = new System.Text.StringBuilder()
                                                    .Append("The following Message has been deleted:")
                                                    .AppendLine()
                                                    .AppendLine().AppendFormat("Message: {0}", Message.MessageDescription)
                                                    .AppendLine().AppendFormat("Scheduled: {0}", Message.Scheduled.HasValue ? Message.Scheduled.Value.ToString("dd/MM/yyyy") : "");

                // Record activity
                var activity = CreateActivity(title, builder.ToString());
                activity.UserId = ServiceUserId;

                upsert.ErrorMsg = string.Format("Message: '{0}' deleted successfully", Message.MessageDescription);
                upsert.RecordId = Message.MessageId.ToString();

                // Remove Message
                db.Messages.Remove(Message);

                await db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                upsert.ErrorMsg = ex.Message;
                //RecordException("Delete Message Error", ex);
            }

            return(upsert);
        }
示例#2
0
        public async Task <UpsertModel> DeleteExpense()
        {
            var upsert = new UpsertModel();

            try {
                string title = "Expense Deleted";
                System.Text.StringBuilder builder = new System.Text.StringBuilder()
                                                    .Append("The following Expense has been deleted:")
                                                    .AppendLine()
                                                    .AppendLine().AppendFormat("Amount (Ugx): {0}", Expense.Amount.ToString("n0"))
                                                    .AppendLine().AppendFormat("Expense By: {0}", Expense.By)
                                                    .AppendLine().AppendFormat("Description: {0}", Expense.Description)
                                                    .AppendLine().AppendFormat("Category: {0}", Expense.CategoryValue);

                // Record activity
                var activity = CreateActivity(title, builder.ToString());
                activity.UserId = ServiceUserId;

                upsert.ErrorMsg = string.Format("Expense by '{0}' deleted successfully", Expense.By);
                upsert.RecordId = Expense.ExpenseId.ToString();

                // Remove Expense
                db.Expenses.Remove(Expense);

                await db.SaveChangesAsync();
            }
            catch (Exception ex) {
                upsert.ErrorMsg = ex.Message;
            }

            return(upsert);
        }
示例#3
0
        public async Task <UpsertModel> DeleteEvent()
        {
            var upsert = new UpsertModel();

            try
            {
                string title = "Event Deleted";
                System.Text.StringBuilder builder = new System.Text.StringBuilder()
                                                    .Append("The following Event has been deleted:")
                                                    .AppendLine()
                                                    .AppendLine().AppendFormat("Event: {0}", Event.Name);

                // Record activity
                var activity = CreateActivity(title, builder.ToString());
                activity.UserId = ServiceUserId;

                // Remove Event
                db.Events.Remove(Event);
                db.Entry(Event).State = System.Data.Entity.EntityState.Deleted;


                upsert.ErrorMsg = string.Format("Event: '{0}' deleted successfully", Event.Name);
                upsert.RecordId = Event.EventId.ToString();

                await db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                upsert.ErrorMsg = ex.Message;
                //RecordException("Delete Event Error", ex);
            }

            return(upsert);
        }
        public async Task <UpsertModel> NotifyParents()
        {
            var upsert = new UpsertModel();

            try
            {
                var parents = StudentId <= 0 ? db.Students.ToList().SelectMany(x => x.Parents.Where(y => y.Parent.HasContact)).ToList() : Student.Parents.ToList().Where(p => p.Parent.HasContact);

                var total = parents.Select(x => x.Student).Sum(m => m.Outstanding);

                await Notify(parents);

                string title = "Parents Notified";
                System.Text.StringBuilder builder = new System.Text.StringBuilder()
                                                    .Append("Students' Parents with outstanding arrears have been reminded:")
                                                    .AppendLine()
                                                    .AppendLine().AppendFormat("No of Parents: {0}", parents.Count())
                                                    .AppendLine().AppendFormat("Total Arrears: {0}/=", total.ToString("n0"));

                // Record activity
                var activity = CreateActivity(title, builder.ToString());
                activity.UserId = ServiceUserId;

                await db.SaveChangesAsync();

                upsert.ErrorMsg = string.Format("{0} Parent(s) notified successfully. ", parents.Count());
                upsert.RecordId = Student.StudentId.ToString();
            }
            catch (Exception ex)
            {
                upsert.ErrorMsg = ex.Message;
            }

            return(upsert);
        }
示例#5
0
        public async Task <UpsertModel> EndTerm()
        {
            var upsert = new UpsertModel();

            try
            {
                string title = "Term Ended";
                System.Text.StringBuilder builder = new System.Text.StringBuilder()
                                                    .Append("The following Term has been ended:")
                                                    .AppendLine()
                                                    .AppendLine().AppendFormat("Term: {0}", Term.GetTerm());

                // Record activity
                var activity = CreateActivity(title, builder.ToString());
                activity.UserId = ServiceUserId;

                UpdateStudentsOldDebt(Term);
                Term.IsCurrentTerm   = false;
                db.Entry(Term).State = System.Data.Entity.EntityState.Modified;


                upsert.ErrorMsg = string.Format("Term: '{0}' ended successfully", Term.GetTerm());
                upsert.RecordId = Term.TermId.ToString();

                await db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                upsert.ErrorMsg = ex.Message;
            }

            return(upsert);
        }
        public async Task <UpsertModel> RestoreParent()
        {
            var upsert = new UpsertModel();

            try
            {
                string title = "Parent Restored";
                System.Text.StringBuilder builder = new System.Text.StringBuilder()
                                                    .Append("The following Parent has been restored:")
                                                    .AppendLine()
                                                    .AppendLine().AppendFormat("Parent name: {0}", Parent.FullName)
                                                    .AppendLine().AppendFormat("No of Students: {0}", Parent.Students.Count);

                // Record activity
                var activity = CreateActivity(title, builder.ToString());
                activity.UserId = ServiceUserId;

                // Remove Parent
                Parent.Terminated      = null;
                db.Entry(Parent).State = System.Data.Entity.EntityState.Modified;

                await db.SaveChangesAsync();

                upsert.ErrorMsg = string.Format("Parent: '{0}' restored successfully", Parent.FullName);
                upsert.RecordId = Parent.ParentId.ToString();
            }
            catch (Exception ex)
            {
                upsert.ErrorMsg = ex.Message;
                //RecordException("Delete Parent Error", ex);
            }

            return(upsert);
        }
示例#7
0
        private SyncLog Upsert(SyncType type, object item, UpsertModel upsertDelegate)
        {
            var log = new SyncLog(SyncAction.Upsert, type, item);

            try
            {
                int?newEntityId = upsertDelegate(item);
                log.SetSuccessful(newEntityId);
            }
            catch (Exception ex)
            {
                log.SetFailed(ex.Message);
            }
            return(log);
        }
示例#8
0
        public async Task <UpsertModel> DeleteTerm()
        {
            var upsert = new UpsertModel();

            try
            {
                string title = "Term Deleted";
                System.Text.StringBuilder builder = new System.Text.StringBuilder()
                                                    .Append("The following Term has been deleted:")
                                                    .AppendLine()
                                                    .AppendLine().AppendFormat("Term: {0}", Term.GetTerm());

                // Record activity
                var activity = CreateActivity(title, builder.ToString());
                activity.UserId = ServiceUserId;

                if (Term.Payments.Count() > 0)
                {
                    Term.Deleted         = DateTime.Now;
                    db.Entry(Term).State = System.Data.Entity.EntityState.Modified;
                }
                else
                {
                    //reset students
                    Term.ResetStudents();

                    db.Terms.Remove(Term);
                    db.Entry(Term).State = System.Data.Entity.EntityState.Deleted;
                }


                upsert.ErrorMsg = string.Format("Term: '{0}' deleted successfully", Term.GetTerm());
                upsert.RecordId = Term.TermId.ToString();

                await db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                upsert.ErrorMsg = ex.Message;
                //RecordException("Delete Term Error", ex);
            }

            return(upsert);
        }
        public async Task <UpsertModel> DeleteClassLevel()
        {
            var upsert = new UpsertModel();

            try
            {
                string title = "Class Level Deleted";
                System.Text.StringBuilder builder = new System.Text.StringBuilder()
                                                    .Append("The following ClassLevel has been deleted:")
                                                    .AppendLine()
                                                    .AppendLine().AppendFormat("Class: {0}", ClassLevel.GetClass());

                // Record activity
                var activity = CreateActivity(title, builder.ToString());
                activity.UserId = ServiceUserId;

                // Remove ClassLevel
                if (ClassLevel.Students.Count() <= 0)
                {
                    db.ClassLevels.Remove(ClassLevel);
                    db.Entry(ClassLevel).State = System.Data.Entity.EntityState.Deleted;
                }
                else
                {
                    ClassLevel.Deleted         = DateTime.Now;
                    db.Entry(ClassLevel).State = System.Data.Entity.EntityState.Modified;
                }



                upsert.ErrorMsg = string.Format("Class Level: '{0}' deleted successfully", ClassLevel.GetClass());
                upsert.RecordId = ClassLevel.ClassLevelId.ToString();

                await db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                upsert.ErrorMsg = ex.Message;
                //RecordException("Delete ClassLevel Error", ex);
            }

            return(upsert);
        }
示例#10
0
        public async Task <UpsertModel> DeleteBank()
        {
            var upsert = new UpsertModel();

            try
            {
                string title = "Bank Deleted";
                System.Text.StringBuilder builder = new System.Text.StringBuilder()
                                                    .Append("The following Bank has been deleted:")
                                                    .AppendLine()
                                                    .AppendLine().AppendFormat("Bank: {0}", Bank.Name);

                // Record activity
                var activity = CreateActivity(title, builder.ToString());
                activity.UserId = ServiceUserId;

                if (Bank.Payments.Count() > 0)
                {
                    Bank.DeActivated     = DateTime.Now;;
                    db.Entry(Bank).State = System.Data.Entity.EntityState.Modified;
                }
                else
                {
                    db.Banks.Remove(Bank);
                    db.Entry(Bank).State = System.Data.Entity.EntityState.Deleted;
                }


                upsert.ErrorMsg = string.Format("Bank: '{0}' deleted successfully", Bank.Name);
                upsert.RecordId = Bank.BankId.ToString();

                await db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                upsert.ErrorMsg = ex.Message;
                //RecordException("Delete Bank Error", ex);
            }

            return(upsert);
        }
示例#11
0
        public async Task <UpsertModel> Exclude(int StudentEventId)
        {
            var upsert = new UpsertModel();

            var studentEvent = db.StudentEvents.ToList().FirstOrDefault(m => m.StudentEventId == StudentEventId);

            try
            {
                string title = "Student excluded";
                System.Text.StringBuilder builder = new System.Text.StringBuilder()
                                                    .Append("The following Student has been excluded from Event:")
                                                    .AppendLine()
                                                    .AppendLine().AppendFormat("Student: {0}", studentEvent.Student.FullName)
                                                    .AppendLine().AppendFormat("Event: {0}", studentEvent.Event.Name);

                // Record activity
                var activity = CreateActivity(title, builder.ToString());
                activity.UserId = ServiceUserId;

                // Remove Student Event
                var successMsg = string.Format("Student: '{0}' excluded from {1} successfully", studentEvent.Student.FullName, studentEvent.Event.Name);

                db.StudentEvents.Remove(studentEvent);
                db.Entry(studentEvent).State = System.Data.Entity.EntityState.Deleted;


                upsert.ErrorMsg = successMsg;
                upsert.RecordId = Event.EventId.ToString();

                await db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                upsert.ErrorMsg = ex.Message;
            }

            return(upsert);
        }
示例#12
0
        public UpsertModel CreatePDF(bool inTransaction, IEnumerable <Student> students)
        {
            var upsert = new UpsertModel();

            try
            {
                var pdf    = new PdfHelper(ServiceUserId);
                var status = pdf.CreatePaymentsReport(students);

                if (status.i_RecordId() > 0)
                {
                    string title       = "Payments Report Created";
                    string description = string.Format("A payments report has been generated");

                    var activity = CreateActivity(title, description);
                    activity.UserId = ServiceUserId;

                    if (!inTransaction)
                    {
                        db.SaveChanges();
                    }

                    upsert.ErrorMsg = status.ErrorMsg;
                    upsert.RecordId = students.Count().ToString();
                }
                else
                {
                    upsert.ErrorMsg = status.ErrorMsg;
                }
            }
            catch (Exception ex)
            {
                upsert.ErrorMsg = ex.Message;
            }

            return(upsert);
        }
示例#13
0
        public async Task <UpsertModel> DeletePayment()
        {
            var upsert = new UpsertModel();

            try
            {
                string title = "Payment Deleted";
                System.Text.StringBuilder builder = new System.Text.StringBuilder()
                                                    .Append("The following Payment has been deleted:")
                                                    .AppendLine()
                                                    .AppendLine().AppendFormat("Student name: {0}", Payment.Student.FullName)
                                                    .AppendLine().AppendFormat("Bank: {0}", Payment.Bank.Name)
                                                    .AppendLine().AppendFormat("Term: {0}-{1}", Payment.Term.StartDate.ToString("dd/MM/yyyy"), Payment.Term.EndDate.ToString("dd/MM/yyyy"))
                                                    .AppendLine().AppendFormat("Class: {0}", Payment.ClassLevel.GetClass());

                // Record activity
                var activity = CreateActivity(title, builder.ToString());
                activity.UserId = ServiceUserId;

                upsert.ErrorMsg = string.Format("Payment for '{0}' deleted successfully", Payment.Student.FullName);
                upsert.RecordId = Payment.PaymentId.ToString();

                // Remove Payment
                db.Payments.Remove(Payment);
                db.Entry(Payment).State = System.Data.Entity.EntityState.Deleted;

                await db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                upsert.ErrorMsg = ex.Message;
                //RecordException("Delete Payment Error", ex);
            }

            return(upsert);
        }
示例#14
0
        public async Task <UpsertModel> UpsertMessage(UpsertMode mode, MessageViewModel model)
        {
            var upsert = new UpsertModel();

            try
            {
                // Apply changes
                Message = model.ParseAsEntity(Message);

                if (model.SendNow)
                {
                    var term = db.Terms.FirstOrDefault(x => x.IsCurrentTerm);

                    var parents = db.StudentParents.ToList().Where(x => x.Student.CurrentTermId == term.TermId && x.Parent.HasContact).ToList() /*.Select(m => m.Parent)*/;

                    foreach (var parent in parents)
                    {
                        var    result = sendTextMessage(parent.Parent.Contacts, model.MessageDescription);
                        string msgTitle;
                        System.Text.StringBuilder msgBuilder;

                        if (result.Contains("1701"))
                        {
                            msgTitle   = "Message Sent";
                            msgBuilder = new System.Text.StringBuilder()
                                         .Append("The following Message has been sent:")
                                         .AppendLine();
                        }
                        else
                        {
                            msgTitle   = "Message Sending Failed";
                            msgBuilder = new System.Text.StringBuilder()
                                         .Append("The following Message has failed to be sent:")
                                         .AppendLine();
                        }

                        msgBuilder.AppendLine().AppendFormat("Message: {0}", Message.MessageDescription)
                        .AppendLine().AppendFormat("Student: {0}", parent.Student.FullName)
                        .AppendLine().AppendFormat("Parent: {0}", parent.Parent.FullName);


                        // Record activity
                        var msgActivity = CreateActivity(msgTitle, msgBuilder.ToString());
                        msgActivity.UserId    = ServiceUserId;
                        msgActivity.ParentId  = parent.ParentId;
                        msgActivity.StudentId = parent.StudentId;
                    }

                    Message.Sent = DateTime.Now;
                    // make sure you create activity here after testing
                }

                Activity activity;
                string   title;
                System.Text.StringBuilder builder;

                builder = new System.Text.StringBuilder();

                if (model.MessageId == 0)
                {
                    db.Messages.Add(Message);

                    title = "Message Recorded";
                    builder.Append("A Message record has been made. ").AppendLine();
                }
                else
                {
                    db.Entry(Message).State = System.Data.Entity.EntityState.Modified;

                    title = "Message Updated";
                    builder.Append("The following changes have been made to the Message details");

                    if (mode == UpsertMode.Admin)
                    {
                        builder.Append(" (by the Admin)");
                    }

                    builder.Append(":").AppendLine();
                }

                await db.SaveChangesAsync();

                MessageId = Message.MessageId;


                // Save activity now so we have a MessageId. Not ideal, but hey
                activity        = CreateActivity(title, builder.ToString());
                activity.UserId = ServiceUserId;

                await db.SaveChangesAsync();

                if (model.MessageId == 0)
                {
                    upsert.ErrorMsg = "Message record created successfully";
                }
                else
                {
                    upsert.ErrorMsg = "Message record updated successfully";
                }

                upsert.RecordId = Message.MessageId.ToString();
            }
            catch (Exception ex)
            {
                upsert.ErrorMsg = ex.Message;
                //RecordException("Update Message Error", ex);
            }

            return(upsert);
        }
示例#15
0
        public async Task <UpsertModel> UpsertBank(UpsertMode mode, BankViewModel model)
        {
            var upsert = new UpsertModel();

            try
            {
                Activity activity;
                string   title;
                System.Text.StringBuilder builder;

                // Apply changes
                Bank = model.ParseAsEntity(Bank);

                builder = new System.Text.StringBuilder();

                if (model.BankId == 0)
                {
                    db.Banks.Add(Bank);

                    title = "Bank Recorded";
                    builder.Append("A Bank record has been made").AppendLine();
                }
                else
                {
                    db.Entry(Bank).State = System.Data.Entity.EntityState.Modified;

                    title = "Bank Updated";
                    builder.Append("The following changes have been made to the Bank details");

                    if (mode == UpsertMode.Admin)
                    {
                        builder.Append(" (by the Admin)");
                    }

                    builder.Append(":").AppendLine();
                }

                await db.SaveChangesAsync();

                BankId = Bank.BankId;

                // Save activity now so we have a BankId. Not ideal, but hey
                activity        = CreateActivity(title, builder.ToString());
                activity.UserId = ServiceUserId;

                await db.SaveChangesAsync();

                if (model.BankId == 0)
                {
                    upsert.ErrorMsg = "Bank record created successfully";
                }
                else
                {
                    upsert.ErrorMsg = "Bank record updated successfully";
                }

                upsert.RecordId = Bank.BankId.ToString();
            }
            catch (Exception ex)
            {
                upsert.ErrorMsg = ex.Message;
                //RecordException("Update Bank Error", ex);
            }

            return(upsert);
        }
示例#16
0
        public async Task <UpsertModel> DeleteStudent()
        {
            var upsert = new UpsertModel();

            try
            {
                string title = "Student Terminated";
                System.Text.StringBuilder builder = new System.Text.StringBuilder()
                                                    .Append("The following Student has been terminated:")
                                                    .AppendLine()
                                                    .AppendLine().AppendFormat("Student name: {0}", Student.FullName)
                                                    .AppendLine().AppendFormat("Class: {0}", Student.CurrentLevel.GetClass());

                // Record activity
                var activity = CreateActivity(title, builder.ToString());
                activity.UserId = ServiceUserId;

                // Remove Student
                if (Student.Payments.Count() > 0)
                {
                    Student.Terminated = DateTime.Now;

                    foreach (var parent in Student.Parents.Select(m => m.Parent).ToList())
                    {
                        parent.Terminated      = DateTime.Today;
                        db.Entry(parent).State = System.Data.Entity.EntityState.Modified;
                    }

                    db.Entry(Student).State = System.Data.Entity.EntityState.Modified;
                }
                else
                {
                    // delete parents
                    foreach (var parent in Student.Parents.Select(m => m.Parent).ToList())
                    {
                        db.Parents.Remove(parent);
                        db.Entry(parent).State = System.Data.Entity.EntityState.Deleted;
                    }

                    // delete related events
                    foreach (var Event in Student.StudentEvents)
                    {
                        db.StudentEvents.Remove(Event);
                        db.Entry(Event).State = System.Data.Entity.EntityState.Deleted;
                    }

                    DeleteDp();
                    db.Students.Remove(Student);
                    db.Entry(Student).State = System.Data.Entity.EntityState.Deleted;
                }

                await db.SaveChangesAsync();

                upsert.ErrorMsg = string.Format("Student: '{0}' terminated successfully", Student.FullName);
                upsert.RecordId = Student.StudentId.ToString();
            }
            catch (Exception ex)
            {
                upsert.ErrorMsg = ex.Message;
                //RecordException("Delete Student Error", ex);
            }

            return(upsert);
        }
示例#17
0
        public async Task <UpsertModel> UpsertEvent(UpsertMode mode, EventViewModel model)
        {
            var upsert = new UpsertModel();

            try
            {
                Activity activity;
                string   title;
                System.Text.StringBuilder builder;

                // Apply changes
                Event = model.ParseAsEntity(Event);


                builder = new System.Text.StringBuilder();

                if (model.EventId == 0)
                {
                    db.Events.Add(Event);

                    title = "Event Recorded";
                    builder.Append("An Event record has been made").AppendLine();
                }
                else
                {
                    db.Entry(Event).State = System.Data.Entity.EntityState.Modified;

                    title = "Event Updated";
                    builder.Append("The following changes have been made to the Event details");

                    if (mode == UpsertMode.Admin)
                    {
                        builder.Append(" (by the Admin)");
                    }

                    builder.Append(":").AppendLine();
                }

                await db.SaveChangesAsync();

                EventId = Event.EventId;

                //update students here

                if (model.IsGeneral && Event.EventStudents.ToList().Count() <= 0)
                {
                    var currentTerm = db.Terms.ToList().FirstOrDefault(m => m.IsCurrentTerm);

                    // take all students to be those of only the current term
                    Event.EventStudents   = db.Students.ToList().Where(m => !m.NoParents && m.CurrentTermId == currentTerm.TermId).ToList().Select(p => new StudentEvent(p.StudentId, Event.EventId)).ToList();
                    db.Entry(Event).State = System.Data.Entity.EntityState.Modified;
                }

                //if (model.IsGeneral && Event.EventStudents.Count() <= 0)
                //{
                //    var currentTerm = db.Terms.ToList().FirstOrDefault(m => m.IsCurrentTerm);

                //    // take all students to be those of only the current term
                //    var students = db.Students.ToList().Where(m => !m.NoParents && m.CurrentTermId == currentTerm.TermId).Select(p => new StudentEvent(p.StudentId, Event.EventId)).ToList();
                //    foreach (var student in students)
                //    {
                //        db.StudentEvents.Add(student);
                //    }

                //    //db.Entry(Event).State = System.Data.Entity.EntityState.Modified;
                //}

                // Save activity now so we have a EventId. Not ideal, but hey
                activity        = CreateActivity(title, builder.ToString());
                activity.UserId = ServiceUserId;

                await db.SaveChangesAsync();

                if (model.EventId == 0)
                {
                    upsert.ErrorMsg = "Event record created successfully";
                }
                else
                {
                    upsert.ErrorMsg = "Event record updated successfully";
                }

                upsert.RecordId = Event.EventId.ToString();
            }
            catch (Exception ex)
            {
                upsert.ErrorMsg = ex.Message;
                //RecordException("Update Event Error", ex);
            }

            return(upsert);
        }
示例#18
0
 public static async Task <(int, int)> HandleEntities <TEntity>(this DbContext context, List <TEntity> addedOrUpdated, List <TEntity> deleted, UpsertModel <TEntity> model) where TEntity : class
示例#19
0
        public async Task <UpsertModel> UpsertEvent(UpsertMode mode, EventViewModel model)
        {
            var upsert = new UpsertModel();

            try
            {
                Activity activity;
                string   title;
                System.Text.StringBuilder builder;

                // Apply changes
                Event Event;

                if (model.EventId > 0)
                {
                    Event = db.Events.Find(model.EventId);
                }
                else
                {
                    Event = new Event();
                }

                Event = model.ParseAsEntity(Event);


                builder = new System.Text.StringBuilder();

                if (model.EventId == 0)
                {
                    db.Events.Add(Event);

                    title = "Event Added";
                    builder.AppendFormat("An Event record has been made for: {0} students' Parents", model.Data.Count()).AppendLine();
                }
                else
                {
                    db.Entry(Event).State = System.Data.Entity.EntityState.Modified;

                    title = "Event Updated";
                    builder.Append("The following changes have been made to the Event details");

                    if (mode == UpsertMode.Admin)
                    {
                        builder.Append(" (by the Admin)");
                    }

                    builder.Append(":").AppendLine();
                }

                await db.SaveChangesAsync();


                // update students here
                //Event.Students = model.Data.Select(p => new StudentEvent(p, Event.EventId)).ToList();
                if (model.Students != null && Event.EventStudents.Count() <= 0)
                {
                    if (model.Students.Count() > 0)
                    {
                        Event.EventStudents   = model.Students.Select(p => new StudentEvent(p.StudentId, Event.EventId)).ToList();
                        db.Entry(Event).State = System.Data.Entity.EntityState.Modified;
                    }
                }

                //if (model.Students != null && Event.EventStudents.Count() <= 0)
                //{
                //    if (model.Students.Count() > 0)
                //    {
                //        var students = model.Students.Select(p => new StudentEvent(p.StudentId, Event.EventId)).ToList();
                //        foreach (var student in students)
                //        {
                //            db.StudentEvents.Add(student);
                //        }

                //        //db.Entry(Event).State = System.Data.Entity.EntityState.Modified;
                //    }
                //}

                // Save activity now so we have a StudentId. Not ideal, but hey
                activity        = CreateActivity(title, builder.ToString());
                activity.UserId = ServiceUserId;

                await db.SaveChangesAsync();

                if (model.EventId == 0)
                {
                    upsert.ErrorMsg = "Event record created successfully";
                }
                else
                {
                    upsert.ErrorMsg = "Event record updated successfully";
                }

                upsert.RecordId = Event.EventId.ToString();
            }
            catch (Exception ex)
            {
                upsert.ErrorMsg = ex.Message;
            }

            return(upsert);
        }
示例#20
0
        public UpsertModel CreateClassesReport(IEnumerable <ClassLevel> classes)
        {
            var upsert = new UpsertModel();

            try
            {
                string docName = @"Classes_Report.pdf";

                docName = FileService.RemoveIllegalCharacters(docName, true);

                string destinationFolder = Settings.DOCFOLDER;

                string logo = HttpContext.Current.Server.MapPath("~/Content/Imgs/glo_ss_logo.png");

                // Create a temp folder if not there
                string temp_folder = FileService.CreateFolder(string.Format(@"{0}\Reports\Temp", destinationFolder));

                // Work on the Temp File
                string abs_TempDoc = String.Format(@"{0}\{1}", temp_folder, docName);

                // Save to New File
                string abs_NewDoc = String.Format(@"{0}\Reports\{1}", destinationFolder, docName);

                // Delete the old temp file
                FileService.DeleteFile(abs_TempDoc);

                // Create a document
                var doc = new Document(PageSize.A4);

                // Make landscape
                doc.SetPageSize(PageSize.A4.Rotate());

                // Create the document object
                PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(abs_TempDoc, FileMode.Create));

                // Events
                ITextEvents header = new ITextEvents();
                writer.PageEvent = header;

                // Open for editing/creation
                doc.Open();

                // Lets go!
                // ++++++++++++++++++++++++++++++++++++++++++++
                // +++++++++++++++++ START ++++++++++++++++++++
                PdfPTable    table;
                List <float> cellWidths;
                PdfPCell     cell;
                //Paragraph paragraph;
                //Chunk chunk;

                //SteppingStone Logo
                doc.Add(ImageCell(logo));

                doc.Add(ParseHeading(string.Format("{0} Class Report ", Settings.COMPANY_NAME)));

                cellWidths = new List <float> {
                    3f, 17f, 10f, 10f, 10f, 10f, 10f, 10f, 10f, 10f
                };
                table = SetTable(cellWidths.ToArray(), true, true);

                table.AddCell(SmallBlackLabelCell("#"));
                table.AddCell(SmallBlackLabelCell("Class"));
                table.AddCell(SmallBlackLabelCell("No. Pupils"));
                table.AddCell(SmallBlackLabelCell("Total Payments (Ugx)"));
                table.AddCell(SmallBlackLabelCell("Balance (Ugx)"));
                table.AddCell(SmallBlackLabelCell("Payment Ratio"));

                int    counter       = 0;
                double PaymentsTotal = 0;
                double Outstanding   = 0;
                double Students      = 0;

                foreach (var classLevel in classes)
                {
                    counter++;
                    table.AddCell(DefaultCell(counter.ToString()));

                    table.AddCell(DefaultCell(classLevel.GetClass()));

                    table.AddCell(DefaultCell(classLevel.StudentsThisTerm.Count().ToString()));
                    Students = Students + classLevel.StudentsThisTerm.Count();

                    table.AddCell(DefaultCell(classLevel.GetTermTotal()));
                    PaymentsTotal = PaymentsTotal + classLevel.TotalRevenue;

                    table.AddCell(DefaultCell(classLevel.Outstanding.ToString("n0")));
                    Outstanding = Outstanding + classLevel.Outstanding;

                    table.AddCell(DefaultCell(classLevel.Ratio.ToString()));
                }

                cell         = SmallBlackLabelCell(counter.ToString() + " Classes");
                cell.Colspan = 2;
                table.AddCell(cell);
                table.AddCell(SmallBlackLabelCell(Students.ToString("n0")));
                table.AddCell(SmallBlackLabelCell(PaymentsTotal.ToString("n0")));
                table.AddCell(SmallBlackLabelCell(Outstanding.ToString("n0")));

                doc.Add(table);


                // +++++++++++++++++ FINISH +++++++++++++++++++
                // ++++++++++++++++++++++++++++++++++++++++++++

                // Close and Save the document
                doc.Close();
                doc.Dispose();
                writer.Dispose();

                // Delete the saved file
                FileService.DeleteFile(abs_NewDoc);

                // Save the temp file to the save file
                File.Copy(abs_TempDoc, abs_NewDoc);

                upsert.RecordId = 1.ToString();
                upsert.ErrorMsg = docName;
            }
            catch (Exception ex)
            {
                upsert.ErrorMsg = ex.Message;
            }


            return(upsert);
        }
示例#21
0
        public async Task <UpsertModel> UpsertParent(UpsertMode mode, ParentViewModel model)
        {
            var upsert = new UpsertModel();

            try
            {
                Activity activity;
                string   title;
                System.Text.StringBuilder builder;

                // Get Student
                //var student = db.Students.FirstOrDefault(x => x.StudentId == model.StudentId);

                // Apply changes
                Parent = model.ParseAsEntity(Parent);

                builder = new System.Text.StringBuilder();

                if (model.ParentId == 0)
                {
                    db.Parents.Add(Parent);


                    title = "Parent Recorded";
                    builder.Append("A Parent record has been made : " + model.FirstName + " " + model.LastName).AppendLine();
                }
                else
                {
                    db.Entry(Parent).State = System.Data.Entity.EntityState.Modified;

                    title = "Parent Updated";
                    builder.Append("The following changes have been made to the Parent details");

                    if (mode == UpsertMode.Admin)
                    {
                        builder.Append(" (by the Admin)");
                    }

                    builder.Append(":").AppendLine();
                }

                await db.SaveChangesAsync();

                ParentId = Parent.ParentId;

                // update student parent relationship
                UpdateStudents(model.SelectedStudents);

                // Save activity now so we have a ParentId. Not ideal, but hey
                activity        = CreateActivity(title, builder.ToString());
                activity.UserId = ServiceUserId;

                await db.SaveChangesAsync();

                if (model.ParentId == 0)
                {
                    upsert.ErrorMsg = "Parent record created successfully";
                }
                else
                {
                    upsert.ErrorMsg = "Parent record updated successfully";
                }

                upsert.RecordId = Parent.ParentId.ToString();
            }
            catch (Exception ex)
            {
                upsert.ErrorMsg = ex.Message;
                //RecordException("Update Parent Error", ex);
            }

            return(upsert);
        }
示例#22
0
        public async Task <UpsertModel> UpsertPayment(UpsertMode mode, PaymentViewModel model)
        {
            var upsert = new UpsertModel();

            try
            {
                Activity activity;
                string   title;
                System.Text.StringBuilder builder;

                // Get Student
                var student = db.Students.Single(x => x.StudentId == model.StudentId);

                // get term
                var term = db.Terms.FirstOrDefault(x => x.TermId == model.TermId);

                // Apply changes
                Payment = model.ParseAsEntity(Payment);

                builder = new System.Text.StringBuilder();

                if (model.PaymentId == 0)
                {
                    student.Payments.Add(Payment);

                    if (term.IsCurrentTerm)
                    {
                        // incase student hasn't been promoted, promote. Carry on debt if any
                        if ((student.CurrentLevelId.HasValue && student.CurrentLevelId != Payment.ClassLevelId) || !student.CurrentLevelId.HasValue)
                        {
                            student.CurrentLevelId  = Payment.ClassLevelId;
                            db.Entry(student).State = System.Data.Entity.EntityState.Modified;
                        }

                        if (!student.CurrentTermId.HasValue || (student.CurrentTermId.HasValue && student.CurrentTermId != Payment.TermId))
                        {
                            student.CurrentTermId   = Payment.TermId;
                            db.Entry(student).State = System.Data.Entity.EntityState.Modified;
                        }
                    }


                    title = "Payment Recorded";
                    builder.Append("A Payment record has been made for: " + student.FullName).AppendLine();
                }
                else
                {
                    db.Entry(Payment).State = System.Data.Entity.EntityState.Modified;

                    title = "Payment Updated";
                    builder.Append("The following changes have been made to the Payment details");

                    if (mode == UpsertMode.Admin)
                    {
                        builder.Append(" (by the Admin)");
                    }

                    builder.Append(":").AppendLine();
                }

                await db.SaveChangesAsync();

                PaymentId = Payment.PaymentId;


                // change parent next reminder date
                if (!Payment.Student.NoParents)
                {
                    var parents = Payment.Student.Parents.Select(x => x.Parent);

                    DateTime?nextDate;

                    if (Payment.Student.HasOutstanding)
                    {
                        nextDate = Payment.Date.AddDays(21);
                    }
                    else
                    {
                        nextDate = null;
                    }

                    foreach (var parent in parents)
                    {
                        if (Payment.Term.IsCurrentTerm)
                        {
                            parent.RemindDate      = nextDate;
                            db.Entry(parent).State = System.Data.Entity.EntityState.Modified;
                        }
                    }
                }

                // Save activity now so we have a PaymentId. Not ideal, but hey
                activity        = CreateActivity(title, builder.ToString());
                activity.UserId = ServiceUserId;

                await db.SaveChangesAsync();

                if (model.PaymentId == 0)
                {
                    upsert.ErrorMsg = "Payment record created successfully";
                }
                else
                {
                    upsert.ErrorMsg = "Payment record updated successfully";
                }

                upsert.RecordId = Payment.PaymentId.ToString();
            }
            catch (Exception ex)
            {
                upsert.ErrorMsg = ex.Message;
                //RecordException("Update Payment Error", ex);
            }

            return(upsert);
        }
示例#23
0
        public async Task <UpsertModel> UpsertExpense(UpsertMode mode, ExpenseViewModel model)
        {
            var upsert = new UpsertModel();

            try {
                Activity activity;
                string   title;
                System.Text.StringBuilder builder;

                // Record previous values for comparison
                double amount   = (Expense != null ? Expense.Amount : 0);
                string by       = (Expense != null ? Expense.By : string.Empty);
                string desc     = (Expense != null ? Expense.Description : string.Empty);
                int    category = (Expense != null ? Expense.Category : 0);
                int    changes  = 0;

                // Apply changes
                Expense = model.ParseAsEntity(Expense);

                builder = new System.Text.StringBuilder();

                if (model.ExpenseId == 0)
                {
                    db.Expenses.Add(Expense);

                    title = "Expense Created";
                    builder.Append(string.Format("A Expense record has been created for: {0}", model.By)).AppendLine();
                }
                else
                {
                    db.Entry(Expense).State = System.Data.Entity.EntityState.Modified;

                    title = "Expense Updated";
                    builder.Append("The following changes have been made to the Expense details");

                    if (mode == UpsertMode.Admin)
                    {
                        builder.Append(" (by the Admin)");
                    }

                    builder.Append(":").AppendLine();
                }

                if (amount != Expense.Amount)
                {
                    builder.AppendLine().AppendFormat("Amount: from '{0}' to '{1}'", amount, Expense.Amount);
                    changes += 1;
                }
                if (by != Expense.By)
                {
                    builder.AppendLine().AppendFormat("Expense By: from '{0}' to '{1}'", by, Expense.By);
                    changes += 1;
                }
                if (desc != Expense.Description)
                {
                    builder.AppendLine().AppendFormat("Description: from '{0}' to '{1}'", desc, Expense.Description);
                    changes += 1;
                }
                if (category != Expense.Category)
                {
                    var categoryValue = category == 0 ? "" : Expense.Categories()[category];
                    builder.AppendLine().AppendFormat("Category: from '{0}' to '{1}'", categoryValue, Expense.Categories()[model.Category]);
                    changes += 1;
                }

                await db.SaveChangesAsync();

                ExpenseId = Expense.ExpenseId;

                // Save activity now so we have a ExpenseId. Not ideal, but hey
                activity        = CreateActivity(title, builder.ToString());
                activity.UserId = ServiceUserId;

                await db.SaveChangesAsync();

                if (model.ExpenseId == 0)
                {
                    upsert.ErrorMsg = "Expense record created successfully";
                }
                else
                {
                    upsert.ErrorMsg = "Expense record updated successfully";
                }

                upsert.RecordId = Expense.ExpenseId.ToString();
            }
            catch (Exception ex) {
                upsert.ErrorMsg = ex.Message;
            }

            return(upsert);
        }
示例#24
0
 public InsertWhereNotExistsOp(DbContext context, IEnumerable <TEntity> entityList, UpsertModel <TEntity> model) : base(context, entityList, model)
 {
 }
示例#25
0
        /// <summary>
        /// Do an upsert operation. Note that this executes immediately - savechanges is not necessary
        /// </summary>
        public static async Task <int> Upsert <TEntity>(this DbContext context, TEntity entity, UpsertModel <TEntity> model) where TEntity : class
        {
            var list = new List <TEntity>()
            {
                entity
            };

            return(await new UpsertOp <TEntity>(context, list, model).Execute());
        }
示例#26
0
        public static async Task <int> Delete <TEntity>(this DbContext context, List <TEntity> entities, UpsertModel <TEntity> model) where TEntity : class
        {
            var propCount = model.PropertyNames.Count;

            if ((propCount * entities.Count) > 2000)
            {
                throw new TooManyItemsException();
            }
            else
            {
                return(await new DeleteOp <TEntity>(context, entities, model).Execute());
            }
        }
示例#27
0
        public UpsertModel CreatePaymentsReport(IEnumerable <Student> students)
        {
            var upsert = new UpsertModel();

            try
            {
                string docName = @"Payments_Report.pdf";

                docName = FileService.RemoveIllegalCharacters(docName, true);

                string destinationFolder = "~/App_Data";//Settings.DOCFOLDER;

                string logo = HttpContext.Current.Server.MapPath("~/Content/Imgs/ss_logo.png");

                // Create a temp folder if not there
                string temp_folder = FileService.CreateFolder(string.Format(@"{0}\Reports\Temp", destinationFolder));

                // Work on the Temp File
                string abs_TempDoc = String.Format(@"{0}\{1}", temp_folder, docName);

                destinationFolder = Settings.DOCFOLDER;

                // Save to New File
                string abs_NewDoc = String.Format(@"{0}\Reports\{1}", destinationFolder, docName);

                // Delete the old temp file
                FileService.DeleteFile(abs_TempDoc);

                // Create a document
                var doc = new Document(PageSize.A4);

                // Make landscape
                doc.SetPageSize(PageSize.A4.Rotate());

                // Create the document object
                PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(abs_TempDoc, FileMode.Create));

                // Events
                ITextEvents header = new ITextEvents();
                writer.PageEvent = header;

                // Open for editing/creation
                doc.Open();

                // Lets go!
                // ++++++++++++++++++++++++++++++++++++++++++++
                // +++++++++++++++++ START ++++++++++++++++++++
                PdfPTable    table;
                List <float> cellWidths;
                PdfPCell     cell;
                //Paragraph paragraph;
                //Chunk chunk;

                //SteppingStone Logo
                doc.Add(ImageCell(logo));

                doc.Add(ParseHeading(string.Format("{0} Fees Report ", Settings.COMPANY_NAME)));

                cellWidths = new List <float> {
                    3f, 17f, 10f, 10f, 10f, 10f, 10f, 10f, 10f, 10f
                };
                table = SetTable(cellWidths.ToArray(), true, true);

                table.AddCell(SmallBlackLabelCell("#"));
                table.AddCell(SmallBlackLabelCell("Pupil name"));
                table.AddCell(SmallBlackLabelCell("1st Installment"));
                table.AddCell(SmallBlackLabelCell("2nd Installment"));
                table.AddCell(SmallBlackLabelCell("Transport"));
                table.AddCell(SmallBlackLabelCell("Swimming"));
                table.AddCell(SmallBlackLabelCell("Old Debt"));
                table.AddCell(SmallBlackLabelCell("Total"));
                table.AddCell(SmallBlackLabelCell("Total Pay't"));
                table.AddCell(SmallBlackLabelCell("Total Balance"));

                int    counter       = 0;
                double OldDebt       = 0;
                double AmountToPay   = 0;
                double PaymentsTotal = 0;
                double Outstanding   = 0;
                double firstPayts    = 0;
                double otherPayts    = 0;
                double Transport     = 0;
                double Swimming      = 0;

                foreach (var student in students)
                {
                    counter++;
                    table.AddCell(DefaultCell(counter.ToString()));
                    table.AddCell(DefaultCell(student.FullName));

                    table.AddCell(DefaultCell(student.FirstCurrentTermPayment.ToString("n0")));
                    firstPayts = firstPayts + student.FirstCurrentTermPayment;

                    table.AddCell(DefaultCell(student.OtherCurrentTermPayments.ToString("n0")));
                    otherPayts = otherPayts + student.OtherCurrentTermPayments;

                    table.AddCell(DefaultCell(student.Transport.ToString("n0")));
                    Transport = Transport + student.Transport;

                    table.AddCell(DefaultCell(student.Swimming.ToString("n0")));
                    Swimming = Swimming + student.Swimming;
                    // increment totals
                    table.AddCell(DefaultCell(student.OldDebt.ToString("n0")));
                    OldDebt = OldDebt + student.OldDebt;

                    table.AddCell(DefaultCell(student.AmountToPay.ToString("n0")));
                    AmountToPay = AmountToPay + student.AmountToPay;

                    table.AddCell(DefaultCell(student.StudentTermPaymentsTotal().ToString("n0")));
                    PaymentsTotal = PaymentsTotal + student.StudentTermPaymentsTotal();

                    table.AddCell(DefaultCell(student.Outstanding.ToString("n0")));
                    Outstanding = Outstanding + student.Outstanding;
                }

                cell         = SmallBlackLabelCell(counter.ToString() + " Pupils");
                cell.Colspan = 2;
                table.AddCell(cell);
                table.AddCell(SmallBlackLabelCell(firstPayts.ToString("n0")));
                table.AddCell(SmallBlackLabelCell(otherPayts.ToString("n0")));
                table.AddCell(SmallBlackLabelCell(Transport.ToString("n0")));
                table.AddCell(SmallBlackLabelCell(Swimming.ToString("n0")));
                table.AddCell(SmallBlackLabelCell(OldDebt.ToString("n0")));
                table.AddCell(SmallBlackLabelCell(AmountToPay.ToString("n0")));
                table.AddCell(SmallBlackLabelCell(PaymentsTotal.ToString("n0")));
                table.AddCell(SmallBlackLabelCell(Outstanding.ToString("n0")));

                doc.Add(table);


                // +++++++++++++++++ FINISH +++++++++++++++++++
                // ++++++++++++++++++++++++++++++++++++++++++++

                // Close and Save the document
                doc.Close();
                doc.Dispose();
                writer.Dispose();

                // Delete the saved file
                FileService.DeleteFile(abs_NewDoc);

                // Save the temp file to the save file
                File.Copy(abs_TempDoc, abs_NewDoc);

                upsert.RecordId = 1.ToString();
                upsert.ErrorMsg = docName;
            }
            catch (Exception ex)
            {
                upsert.ErrorMsg = ex.Message;
            }


            return(upsert);
        }
示例#28
0
        public async Task <UpsertModel> UpsertStudent(UpsertMode mode, StudentViewModel model)
        {
            var upsert = new UpsertModel();

            try
            {
                Activity activity;
                string   title;
                System.Text.StringBuilder builder;

                // Apply changes
                Student = model.ParseAsEntity(Student);

                if (model.Enroll)
                {
                    var term = db.Terms.FirstOrDefault(p => p.IsCurrentTerm);
                    if (term != null)
                    {
                        Student.CurrentTermId = term.TermId;
                    }
                }

                builder = new System.Text.StringBuilder();

                if (model.StudentId == 0)
                {
                    db.Students.Add(Student);

                    title = "Student Added";
                    builder.AppendFormat("A Student record has been made for: {0} {1}", model.FirstName, model.LastName).AppendLine();
                }
                else
                {
                    db.Entry(Student).State = System.Data.Entity.EntityState.Modified;

                    title = "Student Updated";
                    builder.Append("The following changes have been made to the Student details");

                    if (mode == UpsertMode.Admin)
                    {
                        builder.Append(" (by the Admin)");
                    }

                    builder.Append(":").AppendLine();
                }

                await db.SaveChangesAsync();

                StudentId = Student.StudentId;

                if (model.Dp != null)
                {
                    UploadDp(model.Dp);
                }

                // Save activity now so we have a StudentId. Not ideal, but hey
                activity        = CreateActivity(title, builder.ToString());
                activity.UserId = ServiceUserId;

                await db.SaveChangesAsync();

                if (model.StudentId == 0)
                {
                    upsert.ErrorMsg = "Student record created successfully";
                }
                else
                {
                    upsert.ErrorMsg = "Student record updated successfully";
                }

                upsert.RecordId = Student.StudentId.ToString();
            }
            catch (Exception ex)
            {
                upsert.ErrorMsg = ex.Message;
                //RecordException("Update Student Error", ex);
            }

            return(upsert);
        }