public IEnumerable <IRoomBilling> CreateRoomBilling(DateTime period, int clientId = 0) { // Does the same processing as BillingDataProcessStep1.PopulateRoomBilling (transforms // a RoomData record into a RoomBilling record) without saving anything to the database. using (var conn = NewConnection()) { DataSet ds; DataTable dt; var useParentRooms = bool.Parse(Utility.GetRequiredAppSetting("UseParentRooms")); var temp = period == DateTime.Now.FirstOfMonth(); var result = new List <IRoomBilling>(); DateTime now = DateTime.Now; var step1 = new BillingDataProcessStep1(new Step1Config { Connection = conn, Context = "RoomBillingRepository.CreateRoomBilling", Period = period, Now = now, ClientID = clientId, IsTemp = temp }); ds = step1.GetRoomData(); dt = step1.LoadRoomBilling(ds); result.AddRange(CreateRoomBillingItems(dt, temp)); if (useParentRooms) { ds = step1.GetRoomData(BillingDataProcessStep1.FOR_PARENT_ROOMS); dt = step1.LoadRoomBilling(ds); result.AddRange(CreateRoomBillingItems(dt, temp)); } return(result); } }
public IEnumerable <StoreBillingItem> CreateStoreBilling(DateTime period, int clientId = 0) { // Does the same processing as BillingDataProcessStep1.PopulateStoreBilling (transforms // a StoreData record into a StoreBilling record) without saving anything to the database. using (DA.StartUnitOfWork()) { var dt = BillingDataProcessStep1.GetStoreData(period); var result = dt.AsEnumerable().Select(x => new StoreBillingItem { StoreBillingID = x.Field <int>("StoreBillingID"), Period = x.Field <DateTime>("Period"), ClientID = x.Field <int>("ClientID"), AccountID = x.Field <int>("AccountID"), ChargeTypeID = x.Field <int>("ChargeTypeID"), ItemID = x.Field <int>("ItemID"), CategoryID = x.Field <int>("CategoryID"), Quantity = x.Field <decimal>("Quantity"), UnitCost = x.Field <decimal>("UnitCost"), CostMultiplier = x.Field <decimal>("CostMultiplier"), LineCost = x.Field <decimal>("LineCost"), StatusChangeDate = x.Field <DateTime>("StatusChangeDate"), IsTemp = x.Field <bool>("IsTemp") }).ToList(); return(result); } }
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); } }
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 UpdateBilling(int clientId, DateTime period, DateTime now) { using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnSselData"].ConnectionString)) { conn.Open(); bool temp = Utility.IsCurrentPeriod(period); var step1 = new BillingDataProcessStep1(new Step1Config { Connection = conn, Context = "BillingTypeRepository.UpdateBilling", Period = period, Now = now, ClientID = clientId, IsTemp = temp }); step1.PopulateToolBilling(); step1.PopulateRoomBilling(); conn.Close(); } }
public IEnumerable <IToolBilling> CreateToolBilling(DateTime period, int clientId = 0) { using (var conn = NewConnection()) { conn.Open(); var now = DateTime.Now; var temp = period == now.FirstOfMonth(); var step1 = new BillingDataProcessStep1(new Step1Config { Connection = conn, Context = "ToolBillingRepository.CreateToolBilling", Period = period, Now = now, ClientID = clientId, IsTemp = temp }); var source = step1.GetToolData(0); var result = CreateToolBillingItems(source); conn.Close(); return(result); } }
public void Step1_CanPopulateRoomBilling() { using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnSselData"].ConnectionString)) { conn.Open(); var step1 = new BillingDataProcessStep1(new Step1Config { Connection = conn, Period = DateTime.Parse("2015-09-01"), ClientID = 2838, IsTemp = false, Now = DateTime.Now, Context = "Data.Tests.CommonTools.BillingDataProcessTests.Step1_CanPopulateRoomBilling" }); step1.PopulateRoomBilling(); conn.Close(); } }
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 }); }
public static ScriptHost.Result BillingTask(string task, QueryParameters queryParams) { ScriptHost.Result result = new ScriptHost.Result(); DateTime sd; DateTime ed; DateTime period; void getDates(bool startRequired, bool endRequired, bool periodRequired) { if (startRequired && !queryParams.ContainsParameter("StartDate")) { throw new Exception("Missing parameter: StartDate"); } if (endRequired && !queryParams.ContainsParameter("EndDate")) { throw new Exception("Missing parameter: EndDate"); } if (periodRequired && !queryParams.ContainsParameter("Period")) { throw new Exception("Missing parameter: Period"); } sd = queryParams.GetValue("StartDate", DateTime.Now); ed = queryParams.GetValue("EndDate", DateTime.Now); period = queryParams.GetValue("Period", DateTime.Now); } var context = "Data.Models.CommandLine.CommandLineUtility.BillingTask"; var clientId = queryParams.GetValue("ClientID", 0); using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnSselData"].ConnectionString)) { conn.Open(); switch (task) { case "ToolDataClean": getDates(true, true, false); var writeToolDataCleanResult = new WriteToolDataCleanProcess(new WriteToolDataCleanConfig { Connection = conn, StartDate = sd, EndDate = ed, ClientID = clientId, Context = context }).Start(); result.Success = true; result.Message = null; result.Data = writeToolDataCleanResult.LogText; break; case "ToolData": getDates(true, true, false); var writeToolDataResult = new WriteToolDataProcess(new WriteToolDataConfig { Connection = conn, Period = sd, ClientID = clientId, ResourceID = queryParams.GetValue("ResourceID", 0), Context = context }).Start(); result.Success = true; result.Message = null; result.Data = writeToolDataResult.LogText; break; case "RoomDataClean": getDates(true, true, false); var writeRoomDataCleanResult = new WriteRoomDataCleanProcess(new WriteRoomDataCleanConfig { Connection = conn, StartDate = sd, EndDate = ed, ClientID = clientId, Context = context }).Start(); result.Success = true; result.Message = null; result.Data = writeRoomDataCleanResult.LogText; break; case "RoomData": getDates(true, true, false); var writeRoomDataResult = new WriteRoomDataProcess(new WriteRoomDataConfig { Connection = conn, Period = sd, ClientID = clientId, RoomID = queryParams.GetValue("RoomID", 0), Context = context }).Start(); result.Success = true; result.Message = null; result.Data = writeRoomDataResult.LogText; break; case "StoreDataClean": getDates(true, true, false); var writeStoreDataCleanResult = new WriteStoreDataCleanProcess(new WriteStoreDataCleanConfig { Connection = conn, StartDate = sd, EndDate = ed, ClientID = clientId, Context = context }).Start(); result.Success = true; result.Message = null; result.Data = writeStoreDataCleanResult.LogText; break; case "StoreData": getDates(true, true, false); var writeStoreDataResult = new WriteStoreDataProcess(new WriteStoreDataConfig { Connection = conn, Period = sd, ClientID = clientId, ItemID = queryParams.GetValue("ItemID", 0), Context = context }).Start(); result.Success = true; result.Message = null; result.Data = writeStoreDataResult.LogText; break; case "ToolBillingStep1": getDates(false, false, true); var step1 = new BillingDataProcessStep1(new Step1Config { Connection = conn, Period = period, ClientID = clientId, IsTemp = queryParams.GetValue("IsTemp", false), Now = DateTime.Now, Context = context }); var step1Result = step1.PopulateToolBilling(); result.Success = true; result.Message = null; result.Data = step1Result.LogText; break; default: result.Success = false; result.Message = "Unknown task: " + task; break; } conn.Close(); } return(result); }
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); } }
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); } }