Ejemplo n.º 1
0
        private void CalculateSubsidy(DateTime period, int clientId = 0)
        {
            using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnSselData"].ConnectionString))
            {
                conn.Open();

                string context = "Data.Controllers.ApiSubsidyController.CalculateSubsidy";

                var step4 = new BillingDataProcessStep4Subsidy(new Step4Config
                {
                    Connection = conn,
                    Period     = period,
                    ClientID   = clientId,
                    Context    = context
                });

                var step2 = new BillingDataProcessStep2(conn);

                step2.PopulateRoomBillingByAccount(period, clientId);
                step2.PopulateToolBillingByAccount(period, clientId);
                step4.PopulateSubsidyBilling();
                step2.PopulateRoomBillingByAccount(period, clientId);
                step2.PopulateRoomBillingByRoomOrg(period, clientId);
                step2.PopulateToolBillingByAccount(period, clientId);
                step2.PopulateToolBillingByToolOrg(period, clientId);

                conn.Close();
            }
        }
Ejemplo n.º 2
0
        public FinalizeResult Finalize(FinalizeCommand 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();

                var step1 = new BillingDataProcessStep1(new Step1Config {
                    Connection = conn, Context = "ProcessRepository.Finalize", Period = command.Period, Now = startedAt, ClientID = 0, IsTemp = false
                });
                var step4 = new BillingDataProcessStep4Subsidy(new Step4Config {
                    Connection = conn, Context = "ProcessRepository.Finalize", Period = command.Period, ClientID = 0
                });

                var writeToolDataProcessResult = new WriteToolDataProcess(WriteToolDataConfig.Create(conn, "ProcessRepository.Finalize", command.Period, 0, 0, Cost.GetToolCosts(command.Period, 0))).Start();
                var writeRoomDataProcessResult = new WriteRoomDataProcess(new WriteRoomDataConfig {
                    Connection = conn, Context = "ProcessRepository.Finalize", Period = command.Period, ClientID = 0, RoomID = 0
                }).Start();
                var writeStoreDataProcessResult = new WriteStoreDataProcess(new WriteStoreDataConfig {
                    Connection = conn, Context = "ProcessRepository.Finalize", Period = command.Period, ClientID = 0, ItemID = 0
                }).Start();
                var populateToolBillingProcessResult    = step1.PopulateToolBilling();
                var populateRoomBillingProcessResult    = step1.PopulateRoomBilling();
                var populateStoreBillingProcessResult   = step1.PopulateStoreBilling();
                var populateSubsidyBillingProcessResult = step4.PopulateSubsidyBilling();

                conn.Close();

                var result = new FinalizeResult(startedAt)
                {
                    Period = command.Period,
                    WriteToolDataProcessResult          = writeToolDataProcessResult,
                    WriteRoomDataProcessResult          = writeRoomDataProcessResult,
                    WriteStoreDataProcessResult         = writeStoreDataProcessResult,
                    PopulateToolBillingProcessResult    = populateToolBillingProcessResult,
                    PopulateRoomBillingProcessResult    = populateRoomBillingProcessResult,
                    PopulateStoreBillingProcessResult   = populateStoreBillingProcessResult,
                    PopulateSubsidyBillingProcessResult = populateSubsidyBillingProcessResult
                };

                return(result);
            }
        }
Ejemplo n.º 3
0
        public PopulateSubsidyBillingResult Step4(Step4Command command)
        {
            if (command.Period == default)
            {
                throw new Exception("Missing parameter: Period");
            }

            if (command.Period.Day != 1 || command.Period.Hour != 0 || command.Period.Minute != 0 || command.Period.Second != 0)
            {
                throw new Exception("Period must be midnight on the first day of the month.");
            }

            var startedAt = DateTime.Now;

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

                var step4 = new BillingDataProcessStep4Subsidy(new Step4Config {
                    Connection = conn, Context = "ProcessRepository.Step4", Period = command.Period, ClientID = command.ClientID
                });

                PopulateSubsidyBillingResult result;

                switch (command.Command)
                {
                case "subsidy":
                    result = step4.PopulateSubsidyBilling();
                    break;

                case "distribution":
                    step4.DistributeSubsidyMoneyEvenly();
                    result = new PopulateSubsidyBillingResult(startedAt)
                    {
                        Command       = "distribution",
                        Period        = command.Period,
                        ClientID      = command.ClientID,
                        RowsExtracted = 0,
                        RowsDeleted   = 0,
                        RowsLoaded    = 0
                    };
                    break;

                default:
                    throw new Exception($"Unknown command: {command.Command}");
                }

                conn.Close();

                return(result);
            }
        }
Ejemplo n.º 4
0
        public void Step4Subsidy_CanPopulateSubsidyBilling()
        {
            //Dehui Zhang    => 2838
            //James Ricker   => 1419
            //David Pellinen => 189

            using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnSselData"].ConnectionString))
            {
                conn.Open();

                var step4 = new BillingDataProcessStep4Subsidy(new Step4Config
                {
                    Connection = conn,
                    ClientID   = 1419,
                    Period     = DateTime.Parse("2015-09-01"),
                    Context    = "Data.Tests.CommonTools.BillingDataProcessTests.Step4Subsidy_CanPopulateSubsidyBilling"
                });

                step4.PopulateSubsidyBilling();

                conn.Close();
            }
        }
Ejemplo n.º 5
0
        public BillingResult Post([FromBody] BillingModel model)
        {
            HttpContext.Current.Server.ScriptTimeout = 1800;

            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            DateTime start = DateTime.Now;

            int clientId = (model.ClientID == 0) ? -1 : model.ClientID;

            bool success = true;

            int             count;
            string          message = string.Empty;
            string          subject = "Data.Controllers.ApiBillingController.Post";
            LogMessageLevel level   = LogMessageLevel.Info;

            ProcessResult result;

            string context = "Data.Controllers.ApiBillingController.Post";

            if (model.StartPeriod != DateTime.MinValue)
            {
                if (model.EndPeriod == DateTime.MinValue)
                {
                    model.EndPeriod = model.StartPeriod.AddMonths(1);
                }

                using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnSselData"].ConnectionString))
                {
                    conn.Open();

                    BillingDataProcessStep1 step1 = new BillingDataProcessStep1(new Step1Config {
                        Connection = conn, ClientID = clientId, IsTemp = model.IsTemp, Period = model.StartPeriod, Now = DateTime.Now, Context = context
                    });
                    BillingDataProcessStep2        step2;
                    BillingDataProcessStep3        step3;
                    BillingDataProcessStep4Subsidy step4;

                    switch (model.Command)
                    {
                    case "tool-data-clean":
                        result = new WriteToolDataCleanProcess(new WriteToolDataCleanConfig {
                            Connection = conn, StartDate = model.StartPeriod, EndDate = model.EndPeriod, ClientID = clientId, Context = context
                        }).Start();
                        message += result.LogText;
                        break;

                    case "room-data-clean":
                        result = new WriteRoomDataCleanProcess(new WriteRoomDataCleanConfig {
                            Connection = conn, StartDate = model.StartPeriod, EndDate = model.EndPeriod, ClientID = clientId, RoomID = model.RoomID, Context = context
                        }).Start();
                        message += result.LogText;
                        break;

                    case "store-data-clean":
                        result = new WriteStoreDataCleanProcess(new WriteStoreDataCleanConfig {
                            Connection = conn, StartDate = model.StartPeriod, EndDate = model.EndPeriod, ClientID = clientId, Context = context
                        }).Start();
                        message += result.LogText;
                        break;

                    case "tool-data":
                        result = new WriteToolDataProcess(new WriteToolDataConfig {
                            Connection = conn, Period = model.StartPeriod, ClientID = clientId, ResourceID = model.ResourceID, Context = context
                        }).Start();
                        message += result.LogText;
                        break;

                    case "room-data":
                        result = new WriteRoomDataProcess(new WriteRoomDataConfig {
                            Connection = conn, Period = model.StartPeriod, ClientID = clientId, RoomID = model.RoomID, Context = context
                        }).Start();
                        message += result.LogText;
                        break;

                    case "store-data":
                        result = new WriteStoreDataProcess(new WriteStoreDataConfig {
                            Connection = conn, Period = model.StartPeriod, ClientID = clientId, ItemID = model.ItemID, Context = context
                        }).Start();
                        message += result.LogText;
                        break;

                    case "tool-billing-step1":
                        result   = step1.PopulateToolBilling();
                        message += result.LogText;
                        break;

                    case "room-billing-step1":
                        result   = step1.PopulateRoomBilling();
                        message += result.LogText;
                        break;

                    case "store-billing-step1":
                        result   = step1.PopulateStoreBilling();
                        message += result.LogText;
                        break;

                    case "tool-billing-step2":
                        step2    = new BillingDataProcessStep2(conn);
                        count    = step2.PopulateToolBillingByAccount(model.StartPeriod, clientId);
                        message += $"Tool Step2 By Account: count = {count}";
                        count    = step2.PopulateToolBillingByToolOrg(model.StartPeriod, clientId);
                        message += $"{Environment.NewLine}Tool Step2 By Tool Org: count = {count}";
                        break;

                    case "room-billing-step2":
                        step2    = new BillingDataProcessStep2(conn);
                        count    = step2.PopulateRoomBillingByAccount(model.StartPeriod, clientId);
                        message += $"Room Step2 By Account: count = {count}";
                        count    = step2.PopulateRoomBillingByRoomOrg(model.StartPeriod, clientId);
                        message += $"{Environment.NewLine}Room Step2 By Room Org: count = {count}";
                        break;

                    case "store-billing-step2":
                        step2    = new BillingDataProcessStep2(conn);
                        count    = step2.PopulateStoreBillingByAccount(model.StartPeriod, clientId);
                        message += $"Store Step2 By Account: count = {count}";
                        count    = step2.PopulateStoreBillingByItemOrg(model.StartPeriod, clientId);
                        message += $"{Environment.NewLine}Store Step2 By Item Org: count = {count}";
                        break;

                    case "tool-billing-step3":
                        step3    = new BillingDataProcessStep3(conn);
                        count    = step3.PopulateToolBillingByOrg(model.StartPeriod, clientId);
                        message += $"Tool Step3 By Org: count = {count}";
                        break;

                    case "room-billing-step3":
                        step3    = new BillingDataProcessStep3(conn);
                        count    = step3.PopulateRoomBillingByOrg(model.StartPeriod, clientId);
                        message += $"Room Step3 By Org: count = {count}";
                        break;

                    case "store-billing-step3":
                        step3    = new BillingDataProcessStep3(conn);
                        count    = step3.PopulateStoreBillingByOrg(model.StartPeriod);
                        message += $"Store Step3 By Org: count = {count}";
                        break;

                    case "subsidy-billing-step4":
                        step4 = new BillingDataProcessStep4Subsidy(new Step4Config {
                            Connection = conn, Period = model.StartPeriod, ClientID = clientId, Context = context
                        });
                        result   = step4.PopulateSubsidyBilling();
                        message += result.LogText;
                        break;

                    case "subsidy-distribution":
                        step4 = new BillingDataProcessStep4Subsidy(new Step4Config {
                            Connection = conn, Period = model.StartPeriod, ClientID = clientId, Context = context
                        });
                        step4.DistributeSubsidyMoneyEvenly();
                        message += "Subsidy distribution: complete";
                        break;

                    case "finalize-data-tables":
                        result   = DataTableManager.Create(Provider).Finalize(model.StartPeriod);
                        message += result.LogText;
                        break;

                    default:
                        success  = false;
                        message += $"Unknown command: {model.Command}";
                        level    = LogMessageLevel.Error;
                        break;
                    }
                }
            }
            else
            {
                success  = false;
                message += $"Missing parameter: StartPeriod [{model.StartPeriod:yyyy-MM-dd HH:mm:ss}]";
                level    = LogMessageLevel.Error;
            }

            Log.Write(level, subject, message, null);

            DateTime end = DateTime.Now;

            return(new BillingResult()
            {
                Success = success,
                Command = model.Command,
                Description = subject,
                StartDate = start,
                EndDate = end,
                TimeTaken = (end - start).TotalSeconds,
                LogText = message
            });
        }
Ejemplo n.º 6
0
        public UpdateClientBillingResult UpdateClientBilling(UpdateClientBillingCommand 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.");
            }

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

                DateTime now = DateTime.Now;

                DateTime sd = command.Period;
                DateTime ed = command.Period.AddMonths(1);

                var toolDataClean = new WriteToolDataCleanProcess(new WriteToolDataCleanConfig {
                    Connection = conn, Context = "ProcessRepository.UpdateClientBilling", StartDate = sd, EndDate = ed, ClientID = command.ClientID
                });
                var toolData = new WriteToolDataProcess(WriteToolDataConfig.Create(conn, "ProcessRepository.UpdateClientBilling", sd, command.ClientID, 0, Cost.GetToolCosts(sd, 0)));

                var roomDataClean = new WriteRoomDataCleanProcess(new WriteRoomDataCleanConfig {
                    Connection = conn, Context = "ProcessRepository.UpdateClientBilling", StartDate = sd, EndDate = ed, ClientID = command.ClientID
                });
                var roomData = new WriteRoomDataProcess(new WriteRoomDataConfig {
                    Connection = conn, Context = "ProcessRepository.UpdateClientBilling", Period = sd, ClientID = command.ClientID, RoomID = 0
                });

                var pr1 = toolDataClean.Start();
                var pr2 = roomDataClean.Start();

                var pr3 = toolData.Start();
                var pr4 = roomData.Start();

                bool temp = DateTime.Now.FirstOfMonth() == command.Period;

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

                var pr5 = step1.PopulateToolBilling();
                var pr6 = step1.PopulateRoomBilling();

                PopulateSubsidyBillingResult pr7 = null;

                if (!temp)
                {
                    var step4 = new BillingDataProcessStep4Subsidy(new Step4Config {
                        Connection = conn, Context = "ProcessRepository.UpdateClientBilling", Period = command.Period, ClientID = command.ClientID
                    });
                    pr7 = step4.PopulateSubsidyBilling();
                }

                var result = new UpdateClientBillingResult
                {
                    WriteToolDataCleanProcessResult     = pr1,
                    WriteRoomDataCleanProcessResult     = pr2,
                    WriteToolDataProcessResult          = pr3,
                    WriteRoomDataProcessResult          = pr4,
                    PopulateToolBillingProcessResult    = pr5,
                    PopulateRoomBillingProcessResult    = pr6,
                    PopulateSubsidyBillingProcessResult = pr7
                };

                conn.Close();

                return(result);
            }
        }
Ejemplo n.º 7
0
        public IEnumerable <string> UpdateBilling(UpdateBillingArgs args)
        {
            // updates all billing
            using (var conn = NewConnection())
            {
                conn.Open();

                DateTime startTime = DateTime.Now;

                var result = new List <string>
                {
                    $"Started UpdateBilling at {startTime:yyyy-MM-dd HH:mm:ss}",
                    $"Period: {string.Join(", ", args.Periods.Select(x => x.ToString("yyyy-MM-dd")))}",
                    $"ClientID: {args.ClientID}",
                    $"BillingCategory: {args.BillingCategory}"
                };

                Stopwatch sw;
                DateTime  sd, ed;

                foreach (var p in args.Periods)
                {
                    if (p == default)
                    {
                        throw new Exception("Missing parameter: Period");
                    }

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

                    var temp = Utility.IsCurrentPeriod(p);

                    sd = p;
                    ed = p.AddMonths(1);

                    var populateSubsidy = false;

                    sw = new Stopwatch();

                    DateTime now = DateTime.Now;

                    var step1 = new BillingDataProcessStep1(new Step1Config {
                        Connection = conn, Context = "ProcessRepository.UpdateBilling", Period = p, Now = now, ClientID = args.ClientID, IsTemp = temp
                    });

                    if (args.BillingCategory.HasFlag(BillingCategory.Tool))
                    {
                        var toolDataClean = new WriteToolDataCleanProcess(new WriteToolDataCleanConfig {
                            Connection = conn, Context = "ProcessRepository.UpdateBilling", StartDate = sd, EndDate = ed, ClientID = args.ClientID
                        });
                        var toolData = new WriteToolDataProcess(WriteToolDataConfig.Create(conn, "ProcessRepository.UpdateBilling", p, args.ClientID, 0, Cost.GetToolCosts(p, 0)));

                        sw.Restart();
                        toolDataClean.Start();
                        result.Add(string.Format("Completed ToolDataClean in {0}", sw.Elapsed));

                        sw.Restart();
                        toolData.Start();
                        result.Add(string.Format("Completed ToolData in {0}", sw.Elapsed));

                        sw.Restart();
                        step1.PopulateToolBilling();
                        result.Add(string.Format("Completed ToolBilling in {0}", sw.Elapsed));

                        populateSubsidy = true;
                    }

                    if (args.BillingCategory.HasFlag(BillingCategory.Room))
                    {
                        var roomDataClean = new WriteRoomDataCleanProcess(new WriteRoomDataCleanConfig {
                            Connection = conn, Context = "ProcessRepository.UpdateBilling", StartDate = sd, EndDate = ed, ClientID = args.ClientID, RoomID = 0
                        });
                        var roomData = new WriteRoomDataProcess(new WriteRoomDataConfig {
                            Connection = conn, Context = "ProcessRepository.UpdateBilling", Period = p, ClientID = args.ClientID, RoomID = 0
                        });

                        sw.Restart();
                        roomDataClean.Start();
                        result.Add(string.Format("Completed RoomDataClean in {0}", sw.Elapsed));

                        sw.Restart();
                        roomData.Start();
                        result.Add(string.Format("Completed RoomData in {0}", sw.Elapsed));

                        sw.Restart();
                        step1.PopulateRoomBilling();
                        result.Add(string.Format("Completed RoomBilling in {0}", sw.Elapsed));

                        populateSubsidy = true;
                    }

                    if (args.BillingCategory.HasFlag(BillingCategory.Store))
                    {
                        var storeDataClean = new WriteStoreDataCleanProcess(new WriteStoreDataCleanConfig {
                            Connection = conn, Context = "ProcessRepository.UpdateBilling", StartDate = sd, EndDate = ed, ClientID = args.ClientID
                        });
                        var storeData = new WriteStoreDataProcess(new WriteStoreDataConfig {
                            Connection = conn, Context = "ProcessRepository.UpdateBilling", Period = p, ClientID = args.ClientID, ItemID = 0
                        });

                        sw.Restart();
                        storeDataClean.Start();
                        result.Add(string.Format("Completed StoreDataClean in {0}", sw.Elapsed));

                        sw.Restart();
                        storeData.Start();
                        result.Add(string.Format("Completed StoreData in {0}", sw.Elapsed));

                        sw.Restart();
                        step1.PopulateStoreBilling();
                        result.Add(string.Format("Completed StoreBilling in {0}", sw.Elapsed));
                    }

                    if (!temp && populateSubsidy)
                    {
                        sw.Restart();
                        var step4 = new BillingDataProcessStep4Subsidy(new Step4Config {
                            Connection = conn, Context = "ProcessRepository.UpdateBilling", Period = p, ClientID = args.ClientID
                        });

                        step4.PopulateSubsidyBilling();

                        result.Add(string.Format("Completed SubsidyBilling in {0}", sw.Elapsed));
                    }

                    sw.Stop();
                }

                result.Add(string.Format("Completed at {0:yyyy-MM-dd HH:mm:ss}, time taken: {1}", DateTime.Now, DateTime.Now - startTime));

                conn.Close();

                return(result);
            }
        }