| 189 | } |
| 190 | |
| 191 | bool ExcludeRulePart(osmoh::RuleSequence const & rulePart, editor::ui::TimeTableSet & tts) |
| 192 | { |
| 193 | auto const ttsInitialSize = tts.Size(); |
| 194 | for (size_t i = 0; i < ttsInitialSize; ++i) |
| 195 | { |
| 196 | auto tt = tts.Get(i); |
| 197 | auto const ttOpeningDays = tt.GetOpeningDays(); |
| 198 | auto const commonDays = GetCommonDays(ttOpeningDays, MakeOpeningDays(rulePart.GetWeekdays())); |
| 199 | |
| 200 | auto const removeCommonDays = [&commonDays](editor::ui::TimeTableSet::Proxy & tt) |
| 201 | { |
| 202 | for (auto const day : commonDays) |
| 203 | VERIFY(tt.RemoveWorkingDay(day), ("Can't remove working day")); |
| 204 | VERIFY(tt.Commit(), ("Can't commit changes")); |
| 205 | }; |
| 206 | |
| 207 | auto const twentyFourHoursGuard = [](editor::ui::TimeTable & tt) |
| 208 | { |
| 209 | if (tt.IsTwentyFourHours()) |
| 210 | { |
| 211 | tt.SetTwentyFourHours(false); |
| 212 | // TODO(mgsergio): Consider TimeTable refactoring: |
| 213 | // get rid of separation of TwentyFourHours and OpeningTime. |
| 214 | tt.SetOpeningTime(kTwentyFourHours); |
| 215 | } |
| 216 | }; |
| 217 | |
| 218 | auto const & excludeTime = rulePart.GetTimes(); |
| 219 | // The whole rule matches to the tt. |
| 220 | if (commonDays.size() == ttOpeningDays.size()) |
| 221 | { |
| 222 | // rulePart applies to commonDays in a whole. |
| 223 | if (excludeTime.empty()) |
| 224 | return tts.Remove(i); |
| 225 | |
| 226 | twentyFourHoursGuard(tt); |
| 227 | |
| 228 | for (auto const & time : excludeTime) |
| 229 | { |
| 230 | // Whatever it is, it's already closed at a time out of opening time. |
| 231 | if (!Includes(tt.GetOpeningTime(), time)) |
| 232 | continue; |
| 233 | |
| 234 | // The whole opening time interval should be switched off |
| 235 | if (!tt.AddExcludeTime(time)) |
| 236 | return tts.Remove(i); |
| 237 | } |
| 238 | VERIFY(tt.Commit(), ("Can't update time table")); |
| 239 | return true; |
| 240 | } |
| 241 | // A rule is applied to a subset of a time table. We should |
| 242 | // subtract common parts from tt and add a new time table if needed. |
| 243 | if (commonDays.size() != 0) |
| 244 | { |
| 245 | // rulePart applies to commonDays in a whole. |
| 246 | if (excludeTime.empty()) |
| 247 | { |
| 248 | removeCommonDays(tt); |
no test coverage detected