1
0

ftl copy2.dart.bak 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  1. import 'dart:developer';
  2. import 'package:collection/collection.dart';
  3. import 'package:jiffy/jiffy.dart';
  4. import 'package:tp5/core/core.dart';
  5. import 'package:tp5/providers/airports.dart';
  6. import 'package:tp5/roster/models/duty.dart';
  7. import 'package:timezone/timezone.dart' as tz;
  8. class Ftl {
  9. Duration minimumrest(String? station) =>
  10. Duration(hours: station == base ? 12 : 12, minutes: 0, seconds: 0);
  11. static Duration postflight = const Duration(minutes: 30);
  12. static Duration preflight = const Duration(minutes: 60);
  13. static Duration maxdutylast7 = const Duration(hours: 60);
  14. static Duration maxdutylast14 = const Duration(hours: 110);
  15. static Duration maxdutylast28 = const Duration(hours: 190);
  16. static Duration maxfltlast28 = const Duration(hours: 100);
  17. static Duration maxfltlastyear = const Duration(hours: 900);
  18. static Duration maxfltlast12 = const Duration(hours: 1000);
  19. Duration travelling(String station) {
  20. if (base == station) return Duration.zero;
  21. switch (station) {
  22. case "DSS":
  23. return const Duration(minutes: 60);
  24. case "ORY":
  25. return const Duration(minutes: 5);
  26. default:
  27. return const Duration(minutes: 30);
  28. }
  29. }
  30. String base;
  31. List<FtlDutyDetails> dutiesDetails = [];
  32. List<Duty> clDuties = [];
  33. List<FtlDuty> duties = [];
  34. // List<FtlDutyTotal> dutiesLength = [];
  35. List<DTInterval> get dutiesAsInterval => duties
  36. .map((e) =>
  37. (e.type != FtlDutyType.other) ? DTInterval(e.start!, e.end!) : null)
  38. .whereNotNull()
  39. .toList();
  40. List<DTInterval> get stdbyAsInterval => dutiesDetails
  41. .map((e) => (e.type == FtlDutyDetailsType.standby)
  42. ? DTInterval(e.start!, e.end!)
  43. : null)
  44. .whereNotNull()
  45. .toList();
  46. List<DTInterval> get fltsAsInterval => dutiesDetails
  47. .map((e) => (e.type == FtlDutyDetailsType.flight)
  48. ? DTInterval(e.start!, e.end!)
  49. : null)
  50. .whereNotNull()
  51. .toList();
  52. bool frms;
  53. Ftl({required this.dutiesDetails, required this.base, this.frms = false});
  54. Ftl.fromCrewlink(
  55. {required this.clDuties, required this.base, this.frms = false}) {
  56. for (Duty one in clDuties) {
  57. switch (one.type) {
  58. case "flight":
  59. dutiesDetails.add(
  60. FtlDutyDetails(
  61. start: one.start,
  62. end: one.end,
  63. placeStart: one.data["dep"],
  64. placeEnd: one.data["des"],
  65. type: FtlDutyDetailsType.flight,
  66. label: one.data["label"],
  67. ),
  68. );
  69. case "dhflight":
  70. dutiesDetails.add(
  71. FtlDutyDetails(
  72. start: one.start,
  73. end: one.end,
  74. placeStart: one.data["dep"],
  75. placeEnd: one.data["des"],
  76. type: FtlDutyDetailsType.dhflight,
  77. label: one.data["label"],
  78. ),
  79. );
  80. case "dhlimo":
  81. dutiesDetails.add(
  82. FtlDutyDetails(
  83. start: one.start,
  84. end: one.end,
  85. placeStart: one.data["dep"],
  86. placeEnd: one.data["des"],
  87. type: FtlDutyDetailsType.dhlimo,
  88. label: one.data["label"],
  89. ),
  90. );
  91. case "ground":
  92. if (["SBY1", "SBY2", "SBY3"].contains(one.data["label"])) {
  93. dutiesDetails.add(
  94. FtlDutyDetails(
  95. start: one.start,
  96. end: one.end,
  97. placeStart: one.data["dep"],
  98. // placeEnd: one.data["des"],
  99. type: FtlDutyDetailsType.standby,
  100. label: one.data["label"],
  101. ),
  102. );
  103. } else if (["R0"].contains(one.data["label"])) {
  104. //reserve is not duty
  105. dutiesDetails.add(
  106. FtlDutyDetails(
  107. start: one.start,
  108. end: one.end,
  109. placeStart: one.data["dep"],
  110. // placeEnd: one.data["des"],
  111. type: FtlDutyDetailsType.reserve,
  112. label: one.data["label"],
  113. ),
  114. );
  115. } else if ((one.data["actype"] ?? "") != "") {
  116. dutiesDetails.add(
  117. FtlDutyDetails(
  118. start: one.start,
  119. end: one.end,
  120. placeStart: one.data["dep"],
  121. // placeEnd: one.data["des"],
  122. type: FtlDutyDetailsType.sim,
  123. label: one.data["label"],
  124. ),
  125. );
  126. } else if (one.start != null &&
  127. one.end != null &&
  128. one.end!.diff(one.start!, unit: Unit.hour).abs() < 12) {
  129. dutiesDetails.add(
  130. FtlDutyDetails(
  131. start: one.start,
  132. end: one.end,
  133. placeStart: one.data["dep"],
  134. // placeEnd: one.data["des"],
  135. type: FtlDutyDetailsType.ground,
  136. label: one.data["label"],
  137. ),
  138. );
  139. } else {
  140. // print("ftl: constr: unknown ${one.date} ${one.type} ${one.data}");
  141. }
  142. case "checkin":
  143. dutiesDetails.add(
  144. FtlDutyDetails(
  145. start: one.start,
  146. end: one.end,
  147. placeStart: one.data["dep"],
  148. // placeEnd: one.data["des"],
  149. type: FtlDutyDetailsType.preflight,
  150. ),
  151. );
  152. case "checkout":
  153. dutiesDetails.add(
  154. FtlDutyDetails(
  155. start: one.start,
  156. end: one.end,
  157. // placeStart: one.data["dep"],
  158. placeEnd: one.data["des"],
  159. type: FtlDutyDetailsType.postflight,
  160. ),
  161. );
  162. case "credit":
  163. case "wholeday":
  164. break;
  165. default:
  166. // print("ftl: unk: ${one.date} ${one.type} ${one.data}");
  167. }
  168. }
  169. }
  170. Jiffy? _calcPostflight(FtlDutyDetails xduty) {
  171. if (xduty.end != null &&
  172. [FtlDutyDetailsType.flight, FtlDutyDetailsType.dhflight]
  173. .contains(xduty.type)) {
  174. return xduty.end!.addDuration(postflight);
  175. } else {
  176. return xduty.end;
  177. }
  178. }
  179. Jiffy? _calcPreflight(FtlDutyDetails xduty) {
  180. if (xduty.start != null &&
  181. [
  182. FtlDutyDetailsType.flight,
  183. FtlDutyDetailsType.dhflight,
  184. FtlDutyDetailsType.dhlimo,
  185. FtlDutyDetailsType.sim,
  186. ].contains(xduty.type)) {
  187. return (base == (xduty.placeStart ?? ""))
  188. ? xduty.start!.subtractDuration(const Duration(minutes: 60))
  189. : (xduty.start!.subtractDuration(preflight));
  190. } else {
  191. return xduty.start;
  192. }
  193. }
  194. final List<FtlDutyDetailsType> _fdpList = [
  195. FtlDutyDetailsType.flight,
  196. FtlDutyDetailsType.dhflight,
  197. FtlDutyDetailsType.dhlimo
  198. ];
  199. //calcul duties
  200. calcduties({Jiffy? date}) {
  201. log("FTL: start calcduties");
  202. duties.clear();
  203. final t1 = Jiffy.now();
  204. _calcDuty();
  205. log("FTL: start _calcfdpmax");
  206. _calcfdpmax();
  207. log("FTL: start _clacdutytotal");
  208. // _calcDutyTotal();
  209. log("FTL: start calclegal");
  210. _calcLegal(date: date);
  211. final t4 = Jiffy.now();
  212. print(
  213. "ftl: calcduties calculation : ${t4.diff(t1, unit: Unit.second, asFloat: true)} ms");
  214. log("FTL: finish calcduties");
  215. // return [duties, dutiesDetails, dutiesLength];
  216. return [duties, dutiesDetails];
  217. }
  218. _calcDuty() {
  219. for (int i = 0; i < dutiesDetails.length; i++) {
  220. var last = i > 0 ? dutiesDetails[i - 1] : null;
  221. final checkin = ((last?.type == FtlDutyDetailsType.preflight)
  222. ? dutiesDetails[i - 1].start
  223. : null);
  224. final newduty =
  225. _addDutyDetail(index: i, duty: FtlDuty(), checkin: checkin);
  226. //print("${dutiesDetails[i].type} $newduty");
  227. if (newduty != null) {
  228. if (duties.isEmpty) {
  229. //first duty
  230. duties.add(newduty);
  231. } else if ((newduty.start!.isAfter(duties.last.end!)) &&
  232. (duties.last.type == FtlDutyType.other ||
  233. newduty.type == FtlDutyType.other)) {
  234. //last and new are not duty nor fdp (other=>stdby or rsrv)
  235. duties.add(newduty);
  236. } else if ((newduty.start!
  237. .subtractDuration(travelling(newduty.placeStart ?? base))
  238. .isAfter(
  239. duties.last.end!
  240. .addDuration(travelling(duties.last.placeEnd ?? base)),
  241. )) &&
  242. // duties.last.type == FtlDutyType.duty &&
  243. newduty.type == FtlDutyType.duty) {
  244. //last is duty
  245. duties.add(newduty);
  246. } else if (newduty.start!.diff(duties.last.end!, unit: Unit.minute) <
  247. minimumrest(duties.last.placeEnd).inMinutes) {
  248. //diff < minimumrest
  249. //ilhim
  250. final mergedduty = _addDutyDetail(
  251. index: i,
  252. duty: duties.isNotEmpty ? duties.last : FtlDuty(),
  253. checkin: checkin);
  254. duties[duties.length - 1] = mergedduty!;
  255. } else {
  256. //jdida
  257. duties.add(newduty);
  258. }
  259. }
  260. }
  261. }
  262. _calcfdpmax() {
  263. for (var i = 0; i < duties.length; i++) {
  264. FtlDuty duty = duties[i];
  265. FtlDuty? lastduty = i == 0 ? null : duties[i - 1];
  266. if (duty.type == FtlDutyType.fdp) {
  267. String acclim = _acclimatized(
  268. tzoffset: tzDiff(duty.placeStart!, lastduty?.placeEnd ?? base,
  269. duty.start!.dateTime),
  270. timeElapsed:
  271. lastduty?.end!.dateTime.difference(duty.start!.dateTime) ??
  272. const Duration());
  273. duties[i].acclim = acclim;
  274. duties[i].reftime = changeTz(duty.start!, duty.placeStart ?? base).Hm;
  275. Jiffy reftime = duty.start!;
  276. if (duty.reportdelay2 != null && duty.reportdelay1 != null) {
  277. if (duty.start!.dateTime
  278. .difference(duty.reportdelay2!.dateTime)
  279. .inMinutes <
  280. (4 * 60)) {
  281. reftime = duty.start!;
  282. duty.start =
  283. duty.notification2!.add(hours: 1).min(duty.reportdelay1!);
  284. } else {
  285. reftime = duty.reportdelay2!;
  286. duty.start = duty.reportdelay2!;
  287. }
  288. } else if (duty.reportdelay1 != null) {
  289. if (duty.start!.dateTime
  290. .difference(duty.reportdelay1!.dateTime)
  291. .inMinutes <
  292. (4 * 60)) {
  293. reftime = duty.start!;
  294. duty.start = duty.reportdelay1!;
  295. } else {
  296. reftime = duty.reportdelay1!;
  297. duty.start = duty.reportdelay1!;
  298. }
  299. }
  300. switch (acclim) {
  301. case "D":
  302. // print("${duty.start?.yMMMd} ${duty.start?.toUtc().Hm}");
  303. duties[i].fdpMax = fdpMaxBasic(
  304. reftime: reftime, iata: duty.placeStart, sectors: duty.sectors);
  305. duties[i].fdpExtMax = fdpMaxExtBasic(
  306. reftime: reftime, iata: duty.placeStart, sectors: duty.sectors);
  307. case "B":
  308. duties[i].fdpMax = fdpMaxBasic(
  309. reftime: reftime,
  310. iata: lastduty?.placeEnd ?? lastduty?.placeStart ?? base,
  311. sectors: duty.sectors);
  312. duties[i].fdpExtMax = fdpMaxExtBasic(
  313. reftime: reftime,
  314. iata: lastduty?.placeEnd ?? lastduty?.placeStart ?? base,
  315. sectors: duty.sectors);
  316. case "X":
  317. if (frms) {
  318. duties[i].fdpMax = fdpMaxUnk(
  319. reftime: reftime,
  320. iata: duty.placeStart,
  321. sectors: duty.sectors);
  322. } else {
  323. duties[i].fdpMax = fdpMaxUnkFrms(
  324. reftime: reftime,
  325. iata: duty.placeStart,
  326. sectors: duty.sectors);
  327. }
  328. break;
  329. default:
  330. }
  331. }
  332. }
  333. }
  334. _calcLegal({Jiffy? date}) {
  335. if (date == null) {
  336. for (var i = 0; i < duties.length; i++) {
  337. _checklegal(index: i);
  338. }
  339. } else {
  340. duties.forEachIndexed((i, e) {
  341. if (duties[i]
  342. .start!
  343. .isSameOrAfter(date.startOf(Unit.day).subtract(days: 1))) {
  344. _checklegal(index: i);
  345. }
  346. });
  347. }
  348. }
  349. Duration _sumDuration(List<DTInterval> x) =>
  350. Duration(milliseconds: x.map((e) => e.duration.inMilliseconds).sum);
  351. List<DTInterval> breaks(FtlDuty duty) {
  352. List<DTInterval> out = [];
  353. for (var e in duty.dutiesDetailsIndex) {
  354. final dutydetail = dutiesDetails[e];
  355. final deptravel = (base == (dutydetail.placeStart ?? ""))
  356. ? Duration.zero
  357. : travelling(dutydetail.placeStart ?? "");
  358. final destravel = (base == (dutydetail.placeEnd ?? ""))
  359. ? Duration.zero
  360. : travelling(dutydetail.placeEnd ?? dutydetail.placeStart ?? "");
  361. final start = _calcPreflight(dutydetail)!.subtractDuration(deptravel);
  362. final finish = _calcPostflight(dutydetail)!.addDuration(destravel);
  363. if (finish.isAfter(start)) {
  364. out.add(DTInterval(start, finish));
  365. }
  366. }
  367. return duty.interval.minusmany(out);
  368. }
  369. _checklegal({required int index}) {
  370. FtlDuty duty = duties[index];
  371. final date = changeTz(duty.start!, base).endOf(Unit.day);
  372. // log("ftl: dutylenght date=${date.format(pattern: "ddMMMyy HHmm")} \n date-7=${date.subtract(days: 7 - 1).startOf(Unit.day).format(pattern: "ddMMMyy HHmm")}");
  373. // print("${date.yMEd}");
  374. final dutyLength = FtlDutyTotal(
  375. date: date, dutyLength: Duration.zero, fltLength: Duration.zero)
  376. ..dutylast7 = _sumDuration(
  377. DTInterval(date.subtract(days: 7 - 1).startOf(Unit.day), date)
  378. .intersectionmany(dutiesAsInterval))
  379. .add(_sumDuration(
  380. DTInterval(date.subtract(days: 7 - 1).startOf(Unit.day), date)
  381. .intersectionmany(stdbyAsInterval))
  382. .multiply(0.25))
  383. ..dutylast14 = _sumDuration(
  384. DTInterval(date.subtract(days: 14 - 1).startOf(Unit.day), date)
  385. .intersectionmany(dutiesAsInterval))
  386. .add(_sumDuration(DTInterval(
  387. date.subtract(days: 14 - 1).startOf(Unit.day), date)
  388. .intersectionmany(stdbyAsInterval))
  389. .multiply(0.25))
  390. ..dutylast28 = _sumDuration(
  391. DTInterval(date.subtract(days: 28 - 1).startOf(Unit.day), date)
  392. .intersectionmany(dutiesAsInterval))
  393. .add(_sumDuration(DTInterval(
  394. date.subtract(days: 28 - 1).startOf(Unit.day), date)
  395. .intersectionmany(stdbyAsInterval))
  396. .multiply(0.25))
  397. ..fltlast28 = _sumDuration(
  398. DTInterval(date.subtract(days: 28 - 1).startOf(Unit.day), date)
  399. .intersectionmany(fltsAsInterval))
  400. ..fltlast12 = _sumDuration(DTInterval(
  401. date.endOf(Unit.month).subtract(months: 12).startOf(Unit.month),
  402. date)
  403. .intersectionmany(fltsAsInterval))
  404. ..fltyear = _sumDuration(DTInterval(date.startOf(Unit.year), date)
  405. .intersectionmany(fltsAsInterval));
  406. duty.dutyTotal = dutyLength;
  407. // print("${date.yMEd} ${dutyLength.fltyear?.tohhmm}");
  408. // dutylast7,
  409. if ((dutyLength.dutylast7?.inMinutes ?? 0) > maxdutylast7.inMinutes) {
  410. duty.legals.add(FtlLegal(
  411. legalCause: FtlLegalCause.dutylast7,
  412. legalCauseMsg:
  413. "Max duty in 7 days ending ${duty.start?.format(pattern: "ddMMMyy")} exceeded: ${dutyLength.dutylast7?.tohhmm} / ${maxdutylast7.inHours}h"));
  414. }
  415. // dutylast14,
  416. if ((dutyLength.dutylast14?.inMinutes ?? 0) > maxdutylast14.inMinutes) {
  417. duty.legals.add(FtlLegal(
  418. legalCause: FtlLegalCause.dutylast14,
  419. legalCauseMsg:
  420. "Max duty in 14 days ending ${duty.start?.format(pattern: "ddMMMyy")} exceeded: ${dutyLength.dutylast14?.tohhmm} / ${maxdutylast14.inHours}h"));
  421. }
  422. // dutylast28,
  423. if ((dutyLength.dutylast28?.inMinutes ?? 0) > maxdutylast28.inMinutes) {
  424. duty.legals.add(FtlLegal(
  425. legalCause: FtlLegalCause.dutylast28,
  426. legalCauseMsg:
  427. "Max duty in 28 days ending ${duty.start?.format(pattern: "ddMMMyy")} exceeded: ${dutyLength.dutylast28?.tohhmm} / ${maxdutylast28.inHours}h"));
  428. }
  429. // fltlast28,
  430. if ((dutyLength.fltlast28?.inMinutes ?? 0) > maxfltlast28.inMinutes) {
  431. duty.legals.add(FtlLegal(
  432. legalCause: FtlLegalCause.fltlast28,
  433. legalCauseMsg:
  434. "Max flight hours in 28 days ending ${duty.start?.format(pattern: "ddMMMyy")} exceeded: ${dutyLength.fltlast28?.tohhmm} / ${maxfltlast28.inHours}h"));
  435. }
  436. // fltyear,
  437. if ((dutyLength.fltyear?.inMinutes ?? 0) > maxfltlastyear.inMinutes) {
  438. duty.legals.add(FtlLegal(
  439. legalCause: FtlLegalCause.fltyear,
  440. legalCauseMsg:
  441. "Max flight hours in calendar year ${duty.start?.format(pattern: "yyyy")} exceeded: ${dutyLength.fltyear?.tohhmm} / ${maxfltlastyear.inHours}h"));
  442. }
  443. // fltlast12,
  444. if ((dutyLength.fltlast12?.inMinutes ?? 0) > maxfltlast12.inMinutes) {
  445. duty.legals.add(FtlLegal(
  446. legalCause: FtlLegalCause.fltlast12,
  447. legalCauseMsg:
  448. "Max flight hours in 12 months ending ${duty.start?.format(pattern: "ddMMMyy")} exceeded: ${dutyLength.fltlast12?.tohhmm} / ${maxfltlast12.inHours}h"));
  449. }
  450. // fdpmax,
  451. if (duty.type == FtlDutyType.fdp && duty.fdpMax != null) {
  452. if (duty.fdpLength.inMinutes <= duty.fdpMax!.inMinutes) {
  453. } else if (duty.fdpExtMax != null &&
  454. duty.fdpLength.inMinutes <= duty.fdpExtMax!.inMinutes) {
  455. int nbFdpExt = 0;
  456. for (var i = index;
  457. i >= 0 &&
  458. DTInterval(
  459. changeTz(duty.start!, base)
  460. .startOf(Unit.day)
  461. .subtract(days: 7),
  462. changeTz(duty.end!, base))
  463. .isOverlap(DTInterval(duties[i].start!, duties[i].end!));
  464. i--) {
  465. if (duties[i].fdpExt) nbFdpExt++;
  466. }
  467. if (nbFdpExt < 2) {
  468. duty.fdpExt = true;
  469. duties[index] = duty;
  470. } else {
  471. //nbext more than2 in 7days
  472. duty.legals.add(FtlLegal(
  473. legalCause: FtlLegalCause.fdpmax,
  474. legalCauseMsg:
  475. "FDP Ext used more than twice in 7 days. ${duty.fdpLength.tohhmm} / ${duty.fdpMax?.tohhmm} / ext${duty.fdpExtMax?.tohhmm}"));
  476. }
  477. } else if (breaks(duty).any((e) => (e.duration.inHours > 3)
  478. //&&
  479. // ((duty.fdpLength.inMinutes) <=
  480. // (e.duration
  481. // .multiply(0.5)
  482. // .add(duty.fdpMax ?? Duration.zero)
  483. // .inMinutes)))) {
  484. )) {
  485. //split duty check
  486. final fdpmaxbreak = breaks(duty)
  487. .map((e) => ((e.duration.inHours > 3)
  488. ? ((e.duration.multiply(0.5).add(duty.fdpMax ?? Duration.zero)))
  489. : null))
  490. .whereNotNull();
  491. if (fdpmaxbreak.isNotEmpty && duty.fdpLength < fdpmaxbreak.first) {
  492. duty.legals.add(FtlLegal(
  493. legalCause: FtlLegalCause.fdpmax,
  494. condition:
  495. "A break on ground must be considered to extend FDP Max to ${fdpmaxbreak.first.tohhmm}, ",
  496. legalCauseMsg: "FDP is: ${duty.fdpLength.tohhmm}"));
  497. } else {
  498. duty.legals.add(FtlLegal(
  499. legalCause: FtlLegalCause.fdpmax,
  500. legalCauseMsg:
  501. "FDP Max is excedeed even with ground break of ${breaks(duty).map((e) => ((e.duration.inHours > 3) ? (e.duration) : null)).whereNotNull().first.tohhmm}. FDP:${duty.fdpLength.tohhmm}/Max:${fdpmaxbreak.first.tohhmm}"));
  502. }
  503. } else {
  504. duty.legals.add(FtlLegal(
  505. legalCause: FtlLegalCause.fdpmax,
  506. legalCauseMsg:
  507. "FDP is ${duty.fdpLength.tohhmm}/Max:${duty.fdpMax?.tohhmm}"));
  508. }
  509. }
  510. // restfdp without ext,
  511. final FtlDuty? lastduty = (index > 0) ? duties[index - 1] : null;
  512. // log("ftl: checking duty <${duty}> lastduty ended <${lastduty}>");
  513. if (lastduty != null &&
  514. duty.type == FtlDutyType.fdp &&
  515. (lastduty.type == FtlDutyType.fdp ||
  516. lastduty.type == FtlDutyType.duty ||
  517. lastduty.dutiesDetailsIndex.any(
  518. (e) => dutiesDetails[e].type == FtlDutyDetailsType.standby))) {
  519. final rest = minimumrest(lastduty.placeEnd)
  520. .max(duty.end!.dateTime.difference(duty.start!.dateTime));
  521. duties[index - 1].restends = lastduty.end!.addDuration(rest);
  522. if (lastduty.restends!.isAfter(duty.start!)) {
  523. duty.legals.add(FtlLegal(
  524. legalCause: FtlLegalCause.restfdp,
  525. legalCauseMsg:
  526. "Need more rest before starting FDP. rest: ${duty.start!.dateTime.difference(lastduty.end!.dateTime).tohhmm} / ${rest.tohhmm}"));
  527. }
  528. }
  529. // restfdp with ext,
  530. duty = duties[index];
  531. FtlDuty? nextduty = (index < duties.length - 1) ? duties[index + 1] : null;
  532. if (duty.fdpExt) {
  533. final restbefore = (lastduty == null)
  534. ? null
  535. : minimumrest(lastduty.placeEnd)
  536. .max(lastduty.end!.dateTime.difference(lastduty.start!.dateTime));
  537. final restafter = (nextduty == null)
  538. ? null
  539. : minimumrest(duty.placeEnd)
  540. .max(duty.end!.dateTime.difference(duty.start!.dateTime));
  541. //+2h rest before && 2h rest after
  542. if ((lastduty == null ||
  543. duty.start!.dateTime
  544. .difference(lastduty.end!.dateTime)
  545. .inMinutes >=
  546. (restbefore?.inMinutes ?? 0) + (2 * 60)) &&
  547. (nextduty == null ||
  548. nextduty.start!.dateTime
  549. .difference(duty.end!.dateTime)
  550. .inMinutes >=
  551. (restafter?.inMinutes ?? 0) + (60 * 2))) {
  552. }
  553. //+4h rest after
  554. else if ((nextduty == null ||
  555. nextduty.start!.dateTime.difference(duty.end!.dateTime).inMinutes >=
  556. (restafter?.inMinutes ?? 0) + (60 * 4))) {
  557. }
  558. //not enough rest
  559. else {
  560. duty.restends = duty.end!.addDuration(restafter!).add(hours: 4);
  561. duties[index] = duty;
  562. nextduty.legals.add(FtlLegal(
  563. legalCause: FtlLegalCause.restfdp,
  564. legalCauseMsg:
  565. "Need more rest after ExtFDP before starting FDP on ${nextduty.start?.format(pattern: "ddMMMyy HH:mm")} rest: ${nextduty.start?.dateTime.difference(duty.end!.dateTime).tohhmm} / ${restafter.add(const Duration(hours: 4)).tohhmm}"));
  566. duties[index + 1] = nextduty;
  567. }
  568. }
  569. // restrecurrent,
  570. // 36h inc 2 local nights or (60h if 4 disruptive)
  571. const Duration maxdutyrecrest = Duration(hours: 168);
  572. Duration minrecrest = const Duration(hours: 36);
  573. DTInterval rest = DTInterval(duty.start!, duty.start!);
  574. var i = index;
  575. int nbdisruptive = 0;
  576. while (i >= 1 &&
  577. !(rest.duration.inMinutes >= const Duration(hours: 36).inMinutes &&
  578. rest.contains(DTInterval.fromHm(
  579. apartir: changeTz(rest.start, duties[i].placeStart!).toUtc(),
  580. h: 23,
  581. m: 0,
  582. duration: const Duration(hours: 7, minutes: 59),
  583. ap: duties[i].placeStart!)) &&
  584. rest.contains(DTInterval.fromHm(
  585. apartir:
  586. changeTz(rest.start.add(hours: 24), duties[i].placeStart!)
  587. .toUtc(),
  588. h: 23,
  589. m: 0,
  590. duration: const Duration(hours: 7, minutes: 59),
  591. ap: duties[i].placeStart!)))) {
  592. final thisduty = duties[i];
  593. if (_isEarlyStart(thisduty) ||
  594. _isLateFinish(thisduty) ||
  595. _isNightDuty(thisduty)) {
  596. //log("${thisduty}");
  597. nbdisruptive++;
  598. if (nbdisruptive == 4) {
  599. minrecrest = const Duration(hours: 60);
  600. }
  601. }
  602. i--;
  603. rest = DTInterval(duties[i].end!, duties[i + 1].start!);
  604. }
  605. if (DTInterval(rest.end, duty.end!).duration.inMinutes >
  606. maxdutyrecrest.inMinutes) {
  607. duty.legals.add(FtlLegal(
  608. legalCause: FtlLegalCause.restrecurrent,
  609. legalCauseMsg:
  610. "Before duty, need ${minrecrest.inHours}h rest including 2 local nights. from: ${rest.end.format(pattern: "ddMMMyy HH:mm")} ${DTInterval(rest.end, duty.end!).duration.tohhmm}/${maxdutyrecrest.tohhmm} "));
  611. }
  612. // 48h inc 2 local days
  613. final startmonth = changeTz(duty.start!.startOf(Unit.month), base);
  614. final endmonth = startmonth.endOf(Unit.month);
  615. if (duty.interval.isOverlap(
  616. DTInterval(endmonth.subtract(days: 4).add(minutes: 1), endmonth))) {
  617. int nb48inmonth = 0;
  618. List<FtlDuty> dutiesmonth = duties
  619. .where((e) => DTInterval(startmonth, endmonth)
  620. .isOverlap(DTInterval(e.start!, e.end!)))
  621. .toList();
  622. List<DTInterval> restmonth = DTInterval(startmonth, endmonth).minusmany(
  623. dutiesmonth
  624. .map((e) => DTInterval(changeTz(e.start!, e.placeStart!),
  625. changeTz(e.end!, e.placeStart!)))
  626. .toList());
  627. // print(restmonth);
  628. while (restmonth.isNotEmpty) {
  629. final e = restmonth.removeAt(0);
  630. final localdays = DTInterval.fromHm(
  631. apartir: e.start.toUtc(),
  632. h: 0,
  633. m: 0,
  634. duration: const Duration(hours: 47, minutes: 59),
  635. ap: base);
  636. if (e.contains(localdays)) {
  637. nb48inmonth++;
  638. // print(
  639. // "2localdays: ${localdays.start.format(pattern: "ddMMMyy HH:mm")} ${localdays.end.format(pattern: "ddMMMyy HH:mm")}");
  640. // print(
  641. // "in rest ${e.start.format(pattern: "ddMMMyy HH:mm")} ${e.end.format(pattern: "ddMMMyy HH:mm")}");
  642. restmonth = [...e.minus(localdays), ...restmonth];
  643. }
  644. }
  645. if (nb48inmonth == 0) {
  646. duty.legals.add(FtlLegal(
  647. legalCause: FtlLegalCause.restrecurrent,
  648. legalCauseMsg:
  649. "Before duty, need 2x2 local days rest from: ${startmonth.format(pattern: "ddMMMyy HH:mm")} to: ${endmonth.format(pattern: "ddMMMyy HH:mm")}"));
  650. } else if (nb48inmonth == 1 &&
  651. DTInterval(endmonth.subtract(days: 2).add(minutes: 1), endmonth)
  652. .isOverlap(duty.interval)) {
  653. duty.legals.add(FtlLegal(
  654. legalCause: FtlLegalCause.restrecurrent,
  655. legalCauseMsg:
  656. "Before duty, need 2x2 local days rest from: ${startmonth.format(pattern: "ddMMMyy HH:mm")} to: ${endmonth.format(pattern: "ddMMMyy HH:mm")}"));
  657. } else if (nb48inmonth == 1 &&
  658. DTInterval(endmonth.subtract(days: 4).add(minutes: 1),
  659. endmonth.subtract(days: 2))
  660. .isOverlap(duty.interval) &&
  661. DTInterval(endmonth.subtract(days: 4).add(minutes: 1),
  662. endmonth.subtract(days: 2))
  663. .intersectionmany(
  664. [...dutiesAsInterval, ...stdbyAsInterval]).isEmpty) {
  665. duty.legals.add(FtlLegal(
  666. legalCause: FtlLegalCause.restrecurrent,
  667. legalCauseMsg:
  668. "Before duty, need 2x2 local days rest from: ${startmonth.format(pattern: "ddMMMyy HH:mm")} to: ${endmonth.format(pattern: "ddMMMyy HH:mm")}"));
  669. }
  670. }
  671. // disruptive
  672. duty.lateFinish = _isLateFinish(duty);
  673. duty.earlyStart = _isEarlyStart(duty);
  674. duty.nightDuty = _isNightDuty(duty);
  675. if (duty.fdpStart != null &&
  676. lastduty != null &&
  677. base == lastduty.placeEnd &&
  678. (_isLateFinish(lastduty) || _isNightDuty(lastduty)) &&
  679. _isEarlyStart(duty)) {
  680. // log("ftl: found late finish or nighty <${duty.start!.format(pattern: "ddMMMyy HH:mm")}");
  681. if (!DTInterval(lastduty.end!, duty.start!).contains(DTInterval.fromHm(
  682. apartir: changeTz(lastduty.end!, lastduty.placeEnd!).toUtc(),
  683. h: 23,
  684. m: 0,
  685. duration: const Duration(hours: 7, minutes: 59),
  686. ap: duty.placeStart!))) {
  687. duty.legals.add(FtlLegal(
  688. legalCause: FtlLegalCause.disruptive,
  689. legalCauseMsg:
  690. "Disruptive duty; one local night of rest is required before early departure."));
  691. }
  692. }
  693. //consecutive night duty
  694. if (_isConsecutiveNight(duty, lastduty)) {
  695. var i = index;
  696. final monthint = DTInterval(
  697. changeTz(duty.start!.startOf(Unit.month), base),
  698. changeTz(duty.start!.endOf(Unit.month), base));
  699. int nbconsnight = 0;
  700. while (i > 0 &&
  701. duties[i]
  702. .interval
  703. .isOverlap(changeTzDt(monthint, duty.placeStart!))) {
  704. if (_isConsecutiveNight(duties[i], duties[i - 1])) {
  705. nbconsnight++;
  706. }
  707. i--;
  708. }
  709. if (nbconsnight > 1) {
  710. duty.legals.add(FtlLegal(
  711. legalCause: FtlLegalCause.disruptive,
  712. legalCauseMsg:
  713. "Consecutive nights is allowed only once per civil month."));
  714. } else {
  715. duty.legals.add(FtlLegal(
  716. legalCause: FtlLegalCause.disruptive,
  717. condition:
  718. "Two consecutive night duties; crew member agreement is required.",
  719. legalCauseMsg: ""));
  720. }
  721. }
  722. //local day rest if night duty
  723. if (lastduty != null && _isNightDuty(lastduty)) {
  724. if (DTInterval.fromHm(
  725. apartir: changeTz(lastduty.end!, lastduty.placeEnd!).toUtc(),
  726. h: 0,
  727. m: 0,
  728. duration: const Duration(hours: 23, minutes: 59),
  729. ap: lastduty.placeEnd!)
  730. .isOverlap(changeTzDt(duty.interval, duty.placeStart!))) {
  731. duty.legals.add(FtlLegal(
  732. legalCause: FtlLegalCause.disruptive,
  733. condition: "You can request a local day off after last night duty.",
  734. legalCauseMsg: ""));
  735. }
  736. }
  737. // if (duty.legals.isNotEmpty) {
  738. // log("${duty.legals.map((e) => e.legalCauseMsg)}");
  739. // }
  740. // duties[index] = duty;
  741. }
  742. bool _isConsecutiveNight(FtlDuty duty, FtlDuty? lastduty) =>
  743. _isNightDuty(duty) &&
  744. lastduty != null &&
  745. DTInterval.fromHm(
  746. apartir: changeTz(lastduty.end!, lastduty.placeEnd!).toUtc(),
  747. h: 2,
  748. m: 0,
  749. duration: const Duration(hours: 2, minutes: 59),
  750. ap: lastduty.placeEnd!)
  751. .isOverlap(lastduty.interval.toUtc());
  752. FtlDuty? _addDutyDetail(
  753. {required int index, required FtlDuty duty, Jiffy? checkin}) {
  754. final one = dutiesDetails[index];
  755. if (_fdpList.contains(one.type)) {
  756. duty.placeStart = duty.placeStart ?? one.placeStart;
  757. duty.placeEnd = one.placeEnd;
  758. //duty.start = duty.start ?? _calcPreflight(one).latest(checkin);
  759. duty.start = duty.start ?? checkin ?? _calcPreflight(one);
  760. duty.end = _calcPostflight(one);
  761. if (one.type == FtlDutyDetailsType.flight) {
  762. duty.type = FtlDutyType.fdp;
  763. duty.sectors++;
  764. duty.fdpStart = duty.start;
  765. duty.fdpEnd = one.end;
  766. } else {
  767. duty.type = duty.type ?? FtlDutyType.duty;
  768. }
  769. duty.dutiesDetailsIndex.add(index);
  770. } else if ([FtlDutyDetailsType.standby, FtlDutyDetailsType.reserve]
  771. .contains(one.type)) {
  772. duty.placeStart = duty.placeStart ?? one.placeStart;
  773. duty.placeEnd = one.placeStart;
  774. duty.start = duty.start ?? _calcPreflight(one);
  775. duty.end = one.end;
  776. duty.type = FtlDutyType.other;
  777. duty.dutiesDetailsIndex.add(index);
  778. } else if ([FtlDutyDetailsType.ground, FtlDutyDetailsType.sim]
  779. .contains(one.type)) {
  780. duty.placeStart = duty.placeStart ?? one.placeStart;
  781. duty.placeEnd = one.placeStart;
  782. duty.start = duty.start ?? _calcPreflight(one);
  783. duty.end = one.end;
  784. duty.type = duty.type ?? FtlDutyType.duty;
  785. duty.dutiesDetailsIndex.add(index);
  786. } else {
  787. return null;
  788. }
  789. return duty;
  790. }
  791. static bool _isEarlyStart(FtlDuty x) {
  792. //500 559
  793. // print(
  794. // "$x is earlystart: ${DTInterval.fromHm(apartir: changeTz(x.start!, x.placeStart!), h: 5, m: 0, duration: const Duration(minutes: 59)).include(x.start!)}");
  795. return (x.fdpStart != null &&
  796. DTInterval.fromHm(
  797. apartir: changeTz(x.start!.subtract(hours: 24), x.placeStart!)
  798. .toUtc(),
  799. h: 5,
  800. m: 0,
  801. duration: const Duration(minutes: 59),
  802. ap: x.placeStart!)
  803. .include(changeTz(x.start!, x.placeStart!)));
  804. }
  805. static bool _isLateFinish(FtlDuty x) {
  806. //2300 159
  807. // print(
  808. // "$x is latefinish: ${DTInterval.fromHm(apartir: changeTz(x.start!, x.placeStart!), h: 23, m: 0, duration: const Duration(hours: 2, minutes: 59)).include(x.end!)}");
  809. return (x.type != FtlDutyType.other) &&
  810. (DTInterval.fromHm(
  811. apartir: changeTz(x.start!, x.placeStart!).toUtc(),
  812. h: 23,
  813. m: 0,
  814. duration: const Duration(hours: 2, minutes: 59),
  815. ap: x.placeStart!)
  816. .include(changeTz(x.end!, x.placeStart!)));
  817. }
  818. static bool _isNightDuty(FtlDuty x) {
  819. //200 459
  820. // print(
  821. // "$x is Night duty: ${DTInterval.fromHm(apartir: changeTz(x.start!, x.placeStart!), h: 2, m: 0, duration: const Duration(hours: 2, minutes: 59)).include(x.end!)}");
  822. return (x.type != FtlDutyType.other) &&
  823. ((DTInterval.fromHm(
  824. apartir: x.start!.toUtc(),
  825. h: 2,
  826. m: 0,
  827. duration: const Duration(hours: 2, minutes: 59),
  828. ap: x.placeStart!))
  829. .isOverlap(x.interval.toUtc()) ||
  830. (DTInterval.fromHm(
  831. apartir: x.start!.subtract(days: 1).toUtc(),
  832. h: 2,
  833. m: 0,
  834. duration: const Duration(hours: 2, minutes: 59),
  835. ap: x.placeStart!))
  836. .isOverlap(x.interval.toUtc()));
  837. }
  838. Duration tzDiff(String iata1, String iata2, DateTime sourceDateTime) {
  839. // print(Airports.instance.find(iata1)!.timezoneid);
  840. // print(Airports.instance.find(iata2)!.timezoneid);
  841. final iata1Location = tz.getLocation(Airports.find(iata1)!.timezoneid);
  842. final iata2Location = tz.getLocation(Airports.find(iata2)!.timezoneid);
  843. tz.TZDateTime iata1TZDateTime =
  844. tz.TZDateTime.from(sourceDateTime, iata1Location);
  845. tz.TZDateTime iata2TZDateTime =
  846. tz.TZDateTime.from(sourceDateTime, iata2Location);
  847. return Duration(
  848. minutes: (iata2TZDateTime.timeZoneOffset.inMinutes -
  849. iata1TZDateTime.timeZoneOffset.inMinutes)
  850. .abs());
  851. }
  852. String _acclimatized(
  853. {required Duration tzoffset, required Duration timeElapsed}) {
  854. if (tzoffset.inHours <= 2) {
  855. return "D";
  856. } else if (timeElapsed.inHours < 48) {
  857. return "B";
  858. } else if (tzoffset.inHours < 4 && timeElapsed.inHours >= 48) {
  859. return "D";
  860. } else if (tzoffset.inHours <= 6 && timeElapsed.inHours >= 72) {
  861. return "D";
  862. } else if (tzoffset.inHours <= 9 && timeElapsed.inHours >= 96) {
  863. return "D";
  864. } else if (tzoffset.inHours <= 12 && timeElapsed.inHours >= 120) {
  865. return "D";
  866. } else {
  867. return "X";
  868. }
  869. }
  870. static Jiffy changeTz(Jiffy jiffy, String iata) {
  871. return Jiffy.parseFromDateTime(tz.TZDateTime.from(jiffy.dateTime,
  872. tz.getLocation(Airports.find(iata)?.timezoneid ?? "UTC")));
  873. }
  874. static DTInterval changeTzDt(DTInterval dt, String iata) {
  875. return DTInterval(changeTz(dt.start, iata), changeTz(dt.end, iata));
  876. }
  877. Duration _fdpMax(
  878. {required reftime,
  879. String? iata,
  880. required int sectors,
  881. required Map<String, List<String>> tab}) {
  882. Jiffy reftjiffy = Jiffy.now();
  883. String reft = "";
  884. if (reftime is Jiffy && iata != null) {
  885. reftjiffy = reftime;
  886. reftjiffy = changeTz(reftjiffy, iata);
  887. reft = reftjiffy.format(pattern: "HHmm");
  888. } else if (reftime is DateTime && iata != null) {
  889. reftjiffy = Jiffy.parseFromDateTime(reftime);
  890. reftjiffy = changeTz(reftjiffy, iata);
  891. reft = reftjiffy.format(pattern: "HHmm");
  892. } else if (reftime is String && reftime.length == 4) {
  893. reft = reftime;
  894. } else if (reftime is String && iata != null) {
  895. reftjiffy = Jiffy.parse(reftime);
  896. reftjiffy = changeTz(reftjiffy, iata);
  897. reft = reftjiffy.format(pattern: "HHmm");
  898. } else {
  899. throw ("_fdpMax type of reftime unrecognized!!!");
  900. }
  901. // print("$iata $reft ${reftjiffy.toUtc().Hm}");
  902. for (String key in tab.keys) {
  903. List<String> intervalle = key.split("-");
  904. int cmph1 = reft.compareTo(intervalle[0]);
  905. int cmph2 = reft.compareTo(intervalle[1]);
  906. bool inint = (intervalle[0].compareTo(intervalle[1]) < 0)
  907. ? (cmph1 >= 0 && cmph2 <= 0)
  908. : ((cmph1 >= 0 || cmph2 <= 0));
  909. if (inint) {
  910. final List<String>? lres = tab[key];
  911. final int nrow = ((sectors == 1) ? 2 : sectors) - 2;
  912. final String? res =
  913. (lres != null && nrow < lres.length) ? lres[nrow] : null;
  914. if (res != null && res != "Not allowed") {
  915. return Duration(
  916. hours: int.parse(res.split(".")[0]),
  917. minutes: int.parse(res.split(".")[1]));
  918. }
  919. }
  920. }
  921. return Duration.zero;
  922. }
  923. Duration fdpMaxBasic(
  924. {required reftime, String? iata, required int sectors}) =>
  925. _fdpMax(reftime: reftime, iata: iata, sectors: sectors, tab: _fdpMaxTab);
  926. Duration fdpMaxExtBasic(
  927. {required reftime, String? iata, required int sectors}) =>
  928. _fdpMax(
  929. reftime: reftime, iata: iata, sectors: sectors, tab: _fdpMaxExtTab);
  930. Duration fdpMaxUnk({required reftime, String? iata, required int sectors}) =>
  931. _fdpMax(reftime: reftime, iata: iata, sectors: sectors, tab: _fdpUnkTab);
  932. Duration fdpMaxUnkFrms(
  933. {required reftime, String? iata, required int sectors}) =>
  934. _fdpMax(
  935. reftime: reftime, iata: iata, sectors: sectors, tab: _fdpUnkFrmsTab);
  936. //acclim
  937. final Map<String, List<String>> _fdpMaxTab = {
  938. "0600-1329": [
  939. "13.00",
  940. "12.30",
  941. "12.00",
  942. "11.30",
  943. "11.00",
  944. "10.30",
  945. "10.00",
  946. "09.30",
  947. "09.00",
  948. ],
  949. "1330-1359": [
  950. "12.45",
  951. "12.15",
  952. "11.45",
  953. "11.15",
  954. "10.45",
  955. "10.15",
  956. "09.45",
  957. "09.15",
  958. "09.00",
  959. ],
  960. "1400-1429": [
  961. "12.30",
  962. "12.00",
  963. "11.30",
  964. "11.00",
  965. "10.30",
  966. "10.00",
  967. "09.30",
  968. "09.00",
  969. "09.00",
  970. ],
  971. "1430-1459": [
  972. "12.15",
  973. "11.45",
  974. "11.15",
  975. "10.45",
  976. "10.15",
  977. "09.45",
  978. "09.15",
  979. "09.00",
  980. "09.00",
  981. ],
  982. "1500-1529": [
  983. "12.00",
  984. "11.30",
  985. "11.00",
  986. "10.30",
  987. "10.00",
  988. "09.30",
  989. "09.00",
  990. "09.00",
  991. "09.00",
  992. ],
  993. "1530-1559": [
  994. "11.45",
  995. "11.15",
  996. "10.45",
  997. "10.15",
  998. "09.45",
  999. "09.15",
  1000. "09.00",
  1001. "09.00",
  1002. "09.00",
  1003. ],
  1004. "1600-1629": [
  1005. "11.30",
  1006. "11.00",
  1007. "10.30",
  1008. "10.00",
  1009. "09.30",
  1010. "09.00",
  1011. "09.00",
  1012. "09.00",
  1013. "09.00",
  1014. ],
  1015. "1630-1659": [
  1016. "11.15",
  1017. "10.45",
  1018. "10.15",
  1019. "09.45",
  1020. "09.15",
  1021. "09.00",
  1022. "09.00",
  1023. "09.00",
  1024. "09.00",
  1025. ],
  1026. "1700-0459": [
  1027. "11.00",
  1028. "10.30",
  1029. "10.00",
  1030. "09.30",
  1031. "09.00",
  1032. "09.00",
  1033. "09.00",
  1034. "09.00",
  1035. "09.00",
  1036. ],
  1037. "0500-0514": [
  1038. "12.00",
  1039. "11.30",
  1040. "11.00",
  1041. "10.30",
  1042. "10.00",
  1043. "09.30",
  1044. "09.00",
  1045. "09.00",
  1046. "09.00",
  1047. ],
  1048. "0515-0529": [
  1049. "12.15",
  1050. "11.45",
  1051. "11.15",
  1052. "10.45",
  1053. "10.15",
  1054. "09.45",
  1055. "09.15",
  1056. "09.00",
  1057. "09.00",
  1058. ],
  1059. "0530-0544": [
  1060. "12.30",
  1061. "12.00",
  1062. "11.30",
  1063. "11.00",
  1064. "10.30",
  1065. "10.00",
  1066. "09.30",
  1067. "09.00",
  1068. "09.00",
  1069. ],
  1070. "0545-0559": [
  1071. "12.45",
  1072. "12.15",
  1073. "11.45",
  1074. "11.15",
  1075. "10.45",
  1076. "10.15",
  1077. "09.45",
  1078. "09.15",
  1079. "09.00",
  1080. ],
  1081. };
  1082. final Map<String, List<String>> _fdpMaxExtTab = {
  1083. "0600-0614": [
  1084. "Not allowed",
  1085. "Not allowed",
  1086. "Not allowed",
  1087. "Not allowed",
  1088. ],
  1089. "0615-0629": [
  1090. "13.15",
  1091. "12.45",
  1092. "12.15",
  1093. "11.45",
  1094. ],
  1095. "0630-0644": [
  1096. "13.30",
  1097. "13.00",
  1098. "12.30",
  1099. "12.00",
  1100. ],
  1101. "0645-0659": [
  1102. "13.45",
  1103. "13.15",
  1104. "12.45",
  1105. "12.15",
  1106. ],
  1107. "0700-1329": [
  1108. "14.00",
  1109. "13.30",
  1110. "13.00",
  1111. "12.30",
  1112. ],
  1113. "1330-1359": [
  1114. "13.45",
  1115. "13.15",
  1116. "12.45",
  1117. "Not allowed",
  1118. ],
  1119. "1400-1429": [
  1120. "13.30",
  1121. "13.00",
  1122. "12.30",
  1123. "Not allowed",
  1124. ],
  1125. "1430-1459": [
  1126. "13.15",
  1127. "12.45",
  1128. "12.15",
  1129. "Not allowed",
  1130. ],
  1131. "1500-1529": [
  1132. "13.00",
  1133. "12.30",
  1134. "12.00",
  1135. "Not allowed",
  1136. ],
  1137. "1530-1559": [
  1138. "12.45",
  1139. "Not allowed",
  1140. "Not allowed",
  1141. "Not allowed",
  1142. ],
  1143. "1600-1629": [
  1144. "12.30",
  1145. "Not allowed",
  1146. "Not allowed",
  1147. "Not allowed",
  1148. ],
  1149. "1630-1659": [
  1150. "12.15",
  1151. "Not allowed",
  1152. "Not allowed",
  1153. "Not allowed",
  1154. ],
  1155. "1700-1729": [
  1156. "12.00",
  1157. "Not allowed",
  1158. "Not allowed",
  1159. "Not allowed",
  1160. ],
  1161. "1730-1759": [
  1162. "11.45",
  1163. "Not allowed",
  1164. "Not allowed",
  1165. "Not allowed",
  1166. ],
  1167. "1800-1829": [
  1168. "11.30",
  1169. "Not allowed",
  1170. "Not allowed",
  1171. "Not allowed",
  1172. ],
  1173. "1830-1859": [
  1174. "11.15",
  1175. "Not allowed",
  1176. "Not allowed",
  1177. "Not allowed",
  1178. ],
  1179. "1900-0359": [
  1180. "Not allowed",
  1181. "Not allowed",
  1182. "Not allowed",
  1183. "Not allowed",
  1184. ],
  1185. "0400-0414": [
  1186. "Not allowed",
  1187. "Not allowed",
  1188. "Not allowed",
  1189. "Not allowed",
  1190. ],
  1191. "0415-0429": [
  1192. "Not allowed",
  1193. "Not allowed",
  1194. "Not allowed",
  1195. "Not allowed",
  1196. ],
  1197. "0430-0444": [
  1198. "Not allowed",
  1199. "Not allowed",
  1200. "Not allowed",
  1201. "Not allowed",
  1202. ],
  1203. "0445-0459": [
  1204. "Not allowed",
  1205. "Not allowed",
  1206. "Not allowed",
  1207. "Not allowed",
  1208. ],
  1209. "0500-0514": [
  1210. "Not allowed",
  1211. "Not allowed",
  1212. "Not allowed",
  1213. "Not allowed",
  1214. ],
  1215. "0515-0529": [
  1216. "Not allowed",
  1217. "Not allowed",
  1218. "Not allowed",
  1219. "Not allowed",
  1220. ],
  1221. "0530-0544": [
  1222. "Not allowed",
  1223. "Not allowed",
  1224. "Not allowed",
  1225. "Not allowed",
  1226. ],
  1227. "0545-0559": [
  1228. "Not allowed",
  1229. "Not allowed",
  1230. "Not allowed",
  1231. "Not allowed",
  1232. ]
  1233. };
  1234. final Map<String, List<String>> _fdpUnkTab = {
  1235. "0000-2359": ["11.00", "10:30", "10.00", "09.30", "09.00", "09.00", "09.00"]
  1236. };
  1237. final Map<String, List<String>> _fdpUnkFrmsTab = {
  1238. "0000-2359": ["12.00", "11.30", "11.00", "10.30", "10.00", "09.30", "09.00"]
  1239. };
  1240. }
  1241. class FtlDuty {
  1242. FtlDuty({this.start, this.end, this.placeStart, this.placeEnd, this.type});
  1243. @override
  1244. String toString() {
  1245. return "${start?.yMEd} ${start?.Hm} >>> ${end?.Hm} : ${fdpLength.inMinutes > 0 ? "FDP" : "DUTY"} ${dutiesDetailsIndex.length}leg(s)";
  1246. }
  1247. Jiffy? start;
  1248. Jiffy? end;
  1249. DTInterval get interval => DTInterval(start!, end!);
  1250. String? placeStart;
  1251. String? placeEnd;
  1252. FtlDutyType? type;
  1253. List<int> dutiesDetailsIndex = [];
  1254. Jiffy? fdpStart;
  1255. Jiffy? fdpEnd;
  1256. int sectors = 0;
  1257. Duration? fdpMax;
  1258. Duration? fdpExtMax;
  1259. //Duration? fdpIfRExtMax;//if inflight rest rest
  1260. bool fdpExt = false;
  1261. List<FtlLegal> legals = [];
  1262. Duration get dutyLength => (start != null && end != null)
  1263. ? end!.dateTime.difference(start!.dateTime)
  1264. : Duration.zero;
  1265. Duration get fdpLength => (fdpStart != null && fdpEnd != null)
  1266. ? fdpEnd!.dateTime.difference(fdpStart!.dateTime)
  1267. : Duration.zero;
  1268. // Duration? get rest => (dutyLength.max(Ftl.minimumrest(placeEnd)));
  1269. Jiffy? restends;
  1270. String? acclim;
  1271. String? reftime;
  1272. FtlDutyTotal? dutyTotal;
  1273. //Jiffy? report;
  1274. Jiffy? reportdelay1;
  1275. Jiffy? notification1;
  1276. Jiffy? reportdelay2;
  1277. Jiffy? notification2;
  1278. bool earlyStart = false;
  1279. bool lateFinish = false;
  1280. bool nightDuty = false;
  1281. }
  1282. class FtlLegal {
  1283. FtlLegal(
  1284. {required this.legalCause,
  1285. required this.legalCauseMsg,
  1286. this.legalIndex,
  1287. this.condition});
  1288. FtlLegalCause legalCause;
  1289. String legalCauseMsg;
  1290. int? legalIndex;
  1291. String? condition;
  1292. }
  1293. enum FtlDutyType { duty, fdp, other }
  1294. class FtlDutyDetails {
  1295. FtlDutyDetails({
  1296. this.start,
  1297. this.end,
  1298. this.placeStart,
  1299. this.placeEnd,
  1300. this.type,
  1301. this.label,
  1302. });
  1303. Jiffy? start;
  1304. Jiffy? end;
  1305. DTInterval get interval => DTInterval(start!, end!);
  1306. Duration get duration => end!.dateTime.difference(start!.dateTime);
  1307. String? placeStart;
  1308. String? placeEnd;
  1309. FtlDutyDetailsType? type;
  1310. String get typeString {
  1311. switch (type) {
  1312. case FtlDutyDetailsType.flight:
  1313. return "FLT";
  1314. case FtlDutyDetailsType.dhflight:
  1315. return "DH-FLT";
  1316. case FtlDutyDetailsType.dhlimo:
  1317. return "DH-LIMO";
  1318. case FtlDutyDetailsType.sim:
  1319. return "SIM";
  1320. case FtlDutyDetailsType.standby:
  1321. return "STBY";
  1322. case FtlDutyDetailsType.reserve:
  1323. return "RSRV";
  1324. case FtlDutyDetailsType.ground:
  1325. return "GRND";
  1326. default:
  1327. return "UNK";
  1328. }
  1329. }
  1330. String? label;
  1331. }
  1332. enum FtlDutyDetailsType {
  1333. preflight,
  1334. flight,
  1335. postflight,
  1336. dhflight,
  1337. dhlimo,
  1338. ground,
  1339. standby,
  1340. reserve,
  1341. sim,
  1342. // training,
  1343. // other
  1344. }
  1345. class FtlDutyTotal {
  1346. FtlDutyTotal(
  1347. {required this.date, required this.dutyLength, required this.fltLength});
  1348. Jiffy date;
  1349. Duration dutyLength;
  1350. Duration fltLength;
  1351. Duration? dutylast7;
  1352. Duration? dutylast14;
  1353. Duration? dutylast28;
  1354. Duration? fltlast28;
  1355. Duration? fltyear;
  1356. Duration? fltlast12;
  1357. }
  1358. enum FtlLegalCause {
  1359. dutylast7,
  1360. dutylast14,
  1361. dutylast28,
  1362. fltlast28,
  1363. fltyear,
  1364. fltlast12,
  1365. fdpmax,
  1366. restfdp,
  1367. restrecurrent,
  1368. disruptive
  1369. //check duty length && flt length
  1370. //check fdp leength
  1371. //check rest before fdp
  1372. //check recurrent rest
  1373. //check disruptive sched
  1374. }