Beispiel #1
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));
        }