Example #1
0
    public void Process()
    {
        // Execute Step 1
        Step1Result st1result = Step1();

        try
        {
            _lock.EnterWriteLock();
            _result.Step1Result = st1result;
        }
        finally
        {
            _lock.ExitWriteLock();
        }

        Step2Result st2result = Step2();

        try
        {
            _lock.EnterWriteLock();
            _result.Step2Result = st2result;
        }
        finally
        {
            _lock.ExitWriteLock();
        }
        // Other Steps ( long - running )
    }
Example #2
0
        public Step1Result Step1(Step1Command command)
        {
            if (command.Period == default)
            {
                throw new Exception("Missing parameter: Period");
            }

            if (command.Period.Day != 1)
            {
                throw new Exception("Period must be the first day of the month.");
            }

            var startedAt = DateTime.Now;

            using (var conn = NewConnection())
            {
                conn.Open();

                PopulateToolBillingResult  populateToolBillingProcessResult  = null;
                PopulateRoomBillingResult  populateRoomBillingProcessResult  = null;
                PopulateStoreBillingResult populateStoreBillingProcessResult = null;

                var step1 = new BillingDataProcessStep1(new Step1Config {
                    Connection = conn, Context = "ProcessRepository.Step1", Period = command.Period, Now = startedAt, ClientID = command.ClientID, IsTemp = command.IsTemp
                });

                if ((command.BillingCategory & BillingCategory.Tool) > 0)
                {
                    populateToolBillingProcessResult = step1.PopulateToolBilling();
                }

                if ((command.BillingCategory & BillingCategory.Room) > 0)
                {
                    populateRoomBillingProcessResult = step1.PopulateRoomBilling();
                }

                if ((command.BillingCategory & BillingCategory.Store) > 0)
                {
                    populateStoreBillingProcessResult = step1.PopulateStoreBilling();
                }

                conn.Close();

                Step1Result result = new Step1Result(startedAt)
                {
                    PopulateToolBillingProcessResult  = populateToolBillingProcessResult,
                    PopulateRoomBillingProcessResult  = populateRoomBillingProcessResult,
                    PopulateStoreBillingProcessResult = populateStoreBillingProcessResult
                };

                return(result);
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            HttpContextBase ctx = new HttpContextWrapper(context);

            ctx.Response.ContentType = "application/json";

            string command = ctx.Request["Command"];

            int clientId = Utility.ConvertTo(ctx.Request["ClientID"], 0);

            DateTime period;

            object result;

            if (command == "update-billing")
            {
                period = Convert.ToDateTime(ctx.Request["Period"]);

                Provider.Billing.Process.UpdateBilling(new UpdateBillingArgs
                {
                    BillingCategory = BillingCategory.Tool | BillingCategory.Room,
                    ClientID        = clientId,
                    Periods         = new[] { period }
                });

                result = new { Error = false, Message = "ok" };
            }
            else if (command == "reservation-history-billing-update")
            {
                bool            error                    = false;
                string          msg                      = "OK!";
                DataCleanResult dataCleanResult          = null;
                DataResult      dataResult               = null;
                Step1Result     step1Result              = null;
                PopulateSubsidyBillingResult step4Result = null;

                bool isTemp;

                try
                {
                    period = Convert.ToDateTime(ctx.Request["Period"]);

                    isTemp = Utility.IsCurrentPeriod(period);

                    dataCleanResult = Provider.Billing.Process.DataClean(new DataCleanCommand
                    {
                        BillingCategory = BillingCategory.Tool | BillingCategory.Room,
                        ClientID        = clientId,
                        StartDate       = period,
                        EndDate         = period.AddMonths(1)
                    });

                    dataResult = Provider.Billing.Process.Data(new DataCommand
                    {
                        BillingCategory = BillingCategory.Tool | BillingCategory.Room,
                        ClientID        = clientId,
                        Period          = period,
                        Record          = 0
                    });

                    step1Result = Provider.Billing.Process.Step1(new Step1Command
                    {
                        BillingCategory = BillingCategory.Tool | BillingCategory.Room,
                        ClientID        = clientId,
                        Period          = period,
                        Record          = 0,
                        Delete          = true,
                        IsTemp          = isTemp
                    });

                    // [2022-01-06 jg] Adding subsidy calculation, not sure why this wasn't already being done. Definitely need to recalculate in case account is changed from
                    //      internal to external or vice versa.

                    step4Result = Provider.Billing.Process.Step4(new Step4Command
                    {
                        ClientID = clientId,
                        Period   = period,
                        Command  = "subsidy"
                    });

                    // [2022-01-06 jg] This error check does not work for staff because repair reservations exist in ToolDataClean
                    //      but are not added to ToolData (and subsequently ToolBilling), so ToolDataClean rows loaded
                    //      can be greater than ToolData rows loaded. So now it will be skipped if the client is staff.

                    var c = Provider.Data.Client.GetClient(clientId);
                    if (!c.HasPriv(ClientPrivilege.Staff))
                    {
                        var toolDataRowsLoaded      = dataResult.WriteToolDataProcessResult.RowsLoaded;
                        var toolDataCleanRowsLoaded = dataCleanResult.WriteToolDataCleanProcessResult.RowsLoaded;
                        var step1RowsLoaded         = step1Result.PopulateToolBillingProcessResult.RowsLoaded;

                        string errmsg = string.Empty;

                        if (toolDataRowsLoaded < toolDataCleanRowsLoaded)
                        {
                            errmsg += $" Tool data row count ({toolDataRowsLoaded}) is less than tool data clean row count ({toolDataCleanRowsLoaded}).";
                        }

                        if (step1RowsLoaded < toolDataRowsLoaded)
                        {
                            errmsg += $" Step1 row count ({step1RowsLoaded}) is less than tool data row count ({toolDataRowsLoaded}).";
                        }

                        var errorCheck =
                            toolDataRowsLoaded >= toolDataCleanRowsLoaded &&
                            step1RowsLoaded >= toolDataRowsLoaded;

                        if (!errorCheck)
                        {
                            throw new Exception($"A problem occurred when trying to update billing data. {errmsg} Period: {period:yyyy-MM-dd:HH:mm:ss}, ClientID: {clientId}, Rows Loaded: DataClean = {dataCleanResult.WriteToolDataCleanProcessResult.RowsLoaded}, Data = {dataResult.WriteToolDataProcessResult.RowsLoaded}, Step1 = {step1Result.PopulateToolBillingProcessResult.RowsLoaded}");
                        }
                    }
                }
                catch (Exception ex)
                {
                    error = true;
                    msg   = ex.Message;
                    SendEmail.SendDebugEmail(clientId, "LNF.Web.Scheduler.Handlers.ReservationHandler.ProcessRequest", $"[DEBUG:{DateTime.Now:yyyy-MM-dd HH:mm:ss}] An error occurred while updating billing from the Reservation History page.", msg);
                }

                result = new { Error = error, Message = msg, DataCleanResult = dataCleanResult, DataResult = dataResult, Step1Result = step1Result, Step4Result = step4Result };
            }
            else if (command == "get-clients-for-reservation-history")
            {
                bool     isok = clientId > 0;
                DateTime sd, ed;

                if (string.IsNullOrEmpty(ctx.Request.Form["StartDate"]))
                {
                    sd = Reservations.MinReservationBeginDate;
                }
                else
                {
                    isok &= DateTime.TryParse(ctx.Request.Form["StartDate"], out sd);
                }

                if (string.IsNullOrEmpty(ctx.Request.Form["EndDate"]))
                {
                    ed = Reservations.MaxReservationEndDate;
                }
                else
                {
                    isok &= DateTime.TryParse(ctx.Request.Form["EndDate"], out ed);
                }

                IEnumerable <ReservationHistoryClient> clients = null;

                if (isok)
                {
                    clients = ReservationHistoryUtility.Create(Provider).SelectReservationHistoryClients(sd, ed, clientId);
                    result  = new { Error = false, Message = "OK", Clients = clients };
                }
                else
                {
                    result = new { Error = true, Message = "One or more parameters are invalid.", Clients = clients };
                }
            }
            else if (int.TryParse(ctx.Request["ReservationID"], out int reservationId))
            {
                switch (command)
                {
                case "start-reservation":
                    clientId = ctx.CurrentUser(Provider).ClientID;
                    result   = ReservationHandlerUtility.Start(ctx, Provider, reservationId, clientId);
                    break;

                case "get-reservation":
                    result = ReservationHandlerUtility.GetReservation(ctx, Provider, reservationId);
                    break;

                case "save-reservation-history":
                    string notes       = ctx.Request["Notes"];
                    double forgivenPct = Utility.ConvertTo(ctx.Request["ForgivenPct"], 0D);
                    int    accountId   = Utility.ConvertTo(ctx.Request["AccountId"], 0);
                    bool   emailClient = Utility.ConvertTo(ctx.Request["EmailClient"], false);

                    double chargeMultiplier = 1.00 - (forgivenPct / 100.0);

                    clientId = ctx.CurrentUser(Provider).ClientID;

                    var model = new ReservationHistoryUpdate()
                    {
                        ReservationID    = reservationId,
                        AccountID        = accountId,
                        ChargeMultiplier = chargeMultiplier,
                        Notes            = notes,
                        EmailClient      = emailClient,
                        ClientID         = clientId
                    };

                    bool   updateResult = Provider.Scheduler.Reservation.UpdateReservationHistory(model);
                    string msg          = updateResult ? "OK" : "An error occurred.";
                    result = new
                    {
                        Error   = !updateResult,
                        Message = msg
                    };

                    break;

                case "test":
                    result = new { Error = false, Message = "ok" };
                    break;

                case "":
                    throw new Exception("Missing parameter: command");

                default:
                    throw new Exception("Invalid command: " + command);
                }
            }
            else
            {
                throw new Exception("Missing parameter: command");
            }

            ctx.Response.Write(JsonConvert.SerializeObject(result));
        }
 public Step2BResult(Step1Result result)
 {
 }