Exemplo n.º 1
0
        public ActionResult Edit(LessonSessionsModel modifiedModel, string FILTER_Keyword, string FILTER_InvoiceNo, int?FILTER_Cancelled,
                                 bool?FILTER_chkDateFrom, DateTime?FILTER_DateFrom, bool?FILTER_chkDateTo, DateTime?FILTER_DateTo)
        {
            if (ModelState.IsValid)
            {
                LessonSessionsModel originalModel = get(modifiedModel.Id);

                //without the specified access, some fields are excluded in edit form resulting in no value. Copy values from original model
                if (!UserAccountsController.getUserAccess(Session).LessonSessions_Edit)
                {
                    modifiedModel.HourlyRates_Rate = originalModel.HourlyRates_Rate;
                    modifiedModel.TravelCost       = originalModel.TravelCost;
                    modifiedModel.TutorTravelCost  = originalModel.TutorTravelCost;
                }

                string log = string.Empty;
                log = Helper.append(log, originalModel.HourlyRates_Rate, modifiedModel.HourlyRates_Rate, LessonSessionsModel.COL_HourlyRates_Rate.LogDisplay);
                log = Helper.append(log, originalModel.TravelCost, modifiedModel.TravelCost, LessonSessionsModel.COL_TravelCost.LogDisplay);
                log = Helper.append(log, originalModel.TutorTravelCost, modifiedModel.TutorTravelCost, LessonSessionsModel.COL_TutorTravelCost.LogDisplay);
                log = Helper.append(log, originalModel.Review, modifiedModel.Review, LessonSessionsModel.COL_Review.LogDisplay);
                log = Helper.append(log, originalModel.InternalNotes, modifiedModel.InternalNotes, LessonSessionsModel.COL_InternalNotes.LogDisplay);

                if (!string.IsNullOrEmpty(log))
                {
                    update(modifiedModel, log);

                    //update payrollitem if rate or travel cost is changed
                    //Tutor Travel Cost is not currently checked against total travel cost amount paid by customer. This edit may cause cost to exceed amount paid by customer.
                    PayrollPaymentItemsModel payrollPaymentItem = PayrollPaymentItemsController.get(Session, originalModel.PayrollPaymentItems_Id);

                    //this is necessary for payrollpaymentitems that has multiple lessonsessions (class)
                    payrollPaymentItem.HourlyRate      += (modifiedModel.HourlyRates_Rate - originalModel.HourlyRates_Rate);
                    payrollPaymentItem.TutorTravelCost += (modifiedModel.TutorTravelCost - originalModel.TutorTravelCost);

                    payrollPaymentItem.Amount = PayrollPaymentItemsController.calculateAmount(originalModel.IsWaiveTutorFee, payrollPaymentItem.Hour, payrollPaymentItem.HourlyRate, modifiedModel.TutorTravelCost);
                    PayrollPaymentItemsController.update(db, Session, payrollPaymentItem);
                }

                return(RedirectToAction(nameof(Index), new
                {
                    FILTER_Keyword = FILTER_Keyword,
                    FILTER_InvoiceNo = FILTER_InvoiceNo,
                    FILTER_Cancelled = FILTER_Cancelled,
                    FILTER_chkDateFrom = FILTER_chkDateFrom,
                    FILTER_DateFrom = FILTER_DateFrom,
                    FILTER_chkDateTo = FILTER_chkDateTo,
                    FILTER_DateTo = FILTER_DateTo
                }));
            }

            setViewBag(FILTER_Keyword, FILTER_InvoiceNo, FILTER_Cancelled, FILTER_chkDateFrom, FILTER_DateFrom, FILTER_chkDateTo, FILTER_DateTo);
            return(View(modifiedModel));
        }
Exemplo n.º 2
0
        public ActionResult Create(string JsonLessonSessions, string FILTER_Keyword, string FILTER_InvoiceNo, int?FILTER_Cancelled,
                                   bool?FILTER_chkDateFrom, DateTime?FILTER_DateFrom, bool?FILTER_chkDateTo, DateTime?FILTER_DateTo)
        {
            List <LessonSessionsModel> LessonSessions = new List <LessonSessionsModel>();

            if (string.IsNullOrEmpty(JsonLessonSessions))
            {
                return(returnView(LessonSessions, "Please add at least one student"));
            }

            LessonSessions = JsonConvert.DeserializeObject <List <LessonSessionsModel> >(JsonLessonSessions);
            LessonSessionsModel model = LessonSessions[0];

            model.Branches_Id = Helper.getActiveBranchId(Session);

            if (ModelState.IsValid)
            {
                Guid PayrollPaymentItems_Id = Guid.NewGuid();

                //verify remaining hours is enough to cover the session hours. show error for the first error only
                string SaleInvoiceItems_IdList = string.Join(",", LessonSessions.Select(x => x.SaleInvoiceItems_Id.ToString()).ToArray());
                List <SaleInvoiceItemsModel> SaleInvoiceItems           = SaleInvoiceItemsController.get_by_IdList(SaleInvoiceItems_IdList);
                List <SaleInvoiceItemsModel> insufficientRemainingHours = SaleInvoiceItems.Where(x => x.SessionHours_Remaining < model.SessionHours).ToList();
                if (insufficientRemainingHours.Count > 0)
                {
                    return(returnView(LessonSessions, string.Format("Insufficient remaining hours for student {0}", insufficientRemainingHours[0].Customer_UserAccounts_Name)));
                }

                //set tutor pay rate
                List <HourlyRatesModel> hourlyRates = HourlyRatesController.get(null, null, model.Tutor_UserAccounts_Id);
                bool isFullTimeTutor = false;
                foreach (HourlyRatesModel hourlyRate in hourlyRates)
                {
                    if (hourlyRate.FullTimeTutorPayrate > 0)
                    {
                        isFullTimeTutor = true;
                        continue;
                    }
                }

                foreach (LessonSessionsModel session in LessonSessions)
                {
                    SaleInvoiceItemsModel saleInvoiceItem = SaleInvoiceItems.Where(x => x.Id == session.SaleInvoiceItems_Id).FirstOrDefault();

                    session.Id                     = Guid.NewGuid();
                    session.Branches_Id            = model.Branches_Id;
                    session.HourlyRates_Rate       = 0;
                    session.SessionHours           = session.IsScheduleChange ? 0 : session.SessionHours;
                    session.TravelCost             = session.IsScheduleChange ? 0 : (int)Math.Ceiling((saleInvoiceItem.TravelCost / saleInvoiceItem.SessionHours) * session.SessionHours);
                    session.TutorTravelCost        = session.IsScheduleChange ? 0 : (int)Math.Ceiling((saleInvoiceItem.TutorTravelCost / saleInvoiceItem.SessionHours) * session.SessionHours);
                    session.PayrollPaymentItems_Id = session.IsScheduleChange ? (Guid?)null : PayrollPaymentItems_Id;

                    //Calculate tutor payrate
                    if (!isFullTimeTutor && hourlyRates.Count > 0)
                    {
                        foreach (HourlyRatesModel hourlyRate in hourlyRates)
                        {
                            session.HourlyRates_Rate = Math.Ceiling(hourlyRate.Rate / LessonSessions.Count);
                            if (hourlyRate.LessonPackages_Id == saleInvoiceItem.LessonPackages_Id) //rate for the exact lesson package
                            {
                                break;
                            }
                        }
                    }
                    model.TutorTravelCost = session.TutorTravelCost;

                    add(session);

                    //adjust remaining session hours
                    saleInvoiceItem.SessionHours_Remaining -= session.SessionHours;
                    SaleInvoiceItemsController.update_SessionHours_Remaining(db, Session, saleInvoiceItem.Id, saleInvoiceItem.SessionHours_Remaining,
                                                                             string.Format("Lesson Session on {0:dd/MM/yy HH:mm} for {1:N2} hours. Remaining hours: {2:N2} hours.", session.Timestamp, session.SessionHours, saleInvoiceItem.SessionHours_Remaining));
                }
                db.SaveChanges();

                //create payrollpaymentitem
                if (!model.IsScheduleChange)
                {
                    //Calculate tutor payrate
                    decimal HourlyRate = 0;
                    if (!isFullTimeTutor && hourlyRates.Count > 0)
                    {
                        foreach (HourlyRatesModel hourlyRate in hourlyRates)
                        {
                            HourlyRate = hourlyRate.Rate;
                            if (hourlyRate.Branches_Id == model.Branches_Id) //rate for the exact lesson package
                            {
                                break;
                            }
                        }
                    }

                    PayrollPaymentItemsController.add(new PayrollPaymentItemsModel()
                    {
                        Id = PayrollPaymentItems_Id,
                        PayrollPayments_Id = null,
                        Timestamp          = model.Timestamp,
                        Description        = null,
                        Hour            = model.IsWaiveTutorFee ? 0 : model.SessionHours,
                        HourlyRate      = HourlyRate,
                        TutorTravelCost = model.TutorTravelCost,
                        Amount          = PayrollPaymentItemsController.calculateAmount(model.IsWaiveTutorFee, model.SessionHours, HourlyRate, model.TutorTravelCost),
                        UserAccounts_Id = model.Tutor_UserAccounts_Id,
                        Branches_Id     = model.Branches_Id,
                        IsFullTime      = false
                    });
                }

                return(RedirectToAction(nameof(Index), new
                {
                    FILTER_Keyword = FILTER_Keyword,
                    FILTER_InvoiceNo = FILTER_InvoiceNo,
                    FILTER_Cancelled = FILTER_Cancelled,
                    FILTER_chkDateFrom = FILTER_chkDateFrom,
                    FILTER_DateFrom = FILTER_DateFrom,
                    FILTER_chkDateTo = FILTER_chkDateTo,
                    FILTER_DateTo = FILTER_DateTo
                }));
            }

            return(returnView(LessonSessions, null));
        }