public override void ExecuteOption(OptionReport report) { string urlLoginName = _databaseMaterialeBehovAssignment.urlLoginName; int updateProgressFrequency = _databaseMaterialeBehovAssignment.updateProgressFrequency; DatabaseUrlLogin login = DatabaseUrlLogin.GetUrlLogin(Connection, urlLoginName); DynamicsCrmConnection dynamicsCrmConnection = DynamicsCrmConnection.GetConnection(login.Url, login.Username, login.Password); Materiale materiale = Materiale.ReadCalculationNeed(dynamicsCrmConnection, Config.GetResourcePath); if (materiale == null) { report.Success = true; return; } materiale.FindBehovDefinitioner(Config.GetResourcePath); int total = CountTotalAccountsToUpdate(dynamicsCrmConnection, materiale); int staleMaterialeBehovCount = materiale.CountStaleMaterialeBehov(Config.GetResourcePath); total += staleMaterialeBehovCount; materiale.new_beregningsstatus = 0; materiale.Update(); materiale.RemoveStaleMaterialeBehov(Config.GetResourcePath, currentProgress => updateProgress(materiale, currentProgress, total), updateProgressFrequency); int progress = staleMaterialeBehovCount; int materialeAddedCurrent = -1; MaterialeProcessState state = new MaterialeProcessState(); state.pagingInformation = new PagingInformation(); state.owningbusinessunit = materiale.owningbusinessunitGuid.Value; while (materialeAddedCurrent != 0) { materiale.AddMissingMateriale(state, Config.GetResourcePath); materialeAddedCurrent = state.AccountsProcessed; progress += materialeAddedCurrent; updateProgress(materiale, progress, total); } materialeAddedCurrent = -1; materiale.behovsberegning = Materiale.behovsberegningEnum.Afsluttet; materiale.new_beregningsstatus = null; materiale.Update(); report.Success = true; return; }
private void updateProgress(Materiale materiale, int current, int total) { int progress = 0; if (total != 0) { progress = ((current * 100) / total); } if (progress < 0) { progress = 0; } if (progress > 99) { progress = 99; } materiale.new_beregningsstatus = progress; materiale.Update(); }