ftl.dart 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  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. bool foundrecrest = false;
  574. var i = index;
  575. int nbdisruptive = 0;
  576. DTInterval rest = DTInterval(duty.start!.toUtc(), duty.start!.toUtc());
  577. String restPlace = duty.placeStart!;
  578. DTInterval dutyl = DTInterval(duty.start!.toUtc(), duty.end!.toUtc());
  579. Jiffy lastdutystart = duty.start!.toUtc();
  580. DTInterval? recrest;
  581. //find last recrest
  582. while (i >= 1 && !(foundrecrest)) {
  583. i--;
  584. lastdutystart = duties[i + 1].start!.toUtc();
  585. dutyl = DTInterval(lastdutystart, duty.end!.toUtc());
  586. bool disruptive = (_isEarlyStart(duties[i + 1]) ||
  587. _isLateFinish(duties[i + 1]) ||
  588. _isNightDuty(duties[i + 1]));
  589. while (i >= 1 &&
  590. duties[i]
  591. .legals
  592. .any((e) => e.legalCause == FtlLegalCause.restrecurrent)) {
  593. i--;
  594. }
  595. rest = DTInterval(duties[i].end!.toUtc(), lastdutystart);
  596. restPlace = duties[i].placeStart!;
  597. foundrecrest = (rest.duration.inMinutes >= minrecrest.inMinutes) &&
  598. rest.contains(DTInterval.fromHm(
  599. apartir: rest.start.toUtc(),
  600. h: 23,
  601. m: 0,
  602. duration: const Duration(hours: 7, minutes: 59),
  603. ap: restPlace)) &&
  604. rest.contains(DTInterval.fromHm(
  605. apartir: rest.start.toUtc().add(days: 1),
  606. h: 23,
  607. m: 0,
  608. duration: const Duration(hours: 7, minutes: 59),
  609. ap: restPlace));
  610. if (foundrecrest) {
  611. //print(("ftl: recrest 36h: $rest");
  612. recrest = rest;
  613. }
  614. if (!foundrecrest && disruptive) {
  615. nbdisruptive++;
  616. }
  617. }
  618. //check if 60h rest needed
  619. //find recrest before last recrest
  620. int j = i;
  621. final duty2 = duties[j];
  622. bool foundrecrest2 = false;
  623. int nbdisruptive2 = 0;
  624. DTInterval rest2 = DTInterval(duty2.start!.toUtc(), duty2.start!.toUtc());
  625. String restPlace2 = duty2.placeStart!;
  626. Jiffy lastdutystart2 = duty2.start!.toUtc();
  627. if (recrest != null && duty.start!.isBefore(recrest.start.add(hours: 60))) {
  628. //print(("ftl 60h check: $duty");
  629. while (j >= 1 && !(foundrecrest2)) {
  630. j--;
  631. lastdutystart2 = duties[j + 1].start!.toUtc();
  632. while (j >= 1 &&
  633. duties[j]
  634. .legals
  635. .any((e) => e.legalCause == FtlLegalCause.restrecurrent)) {
  636. j--;
  637. }
  638. rest2 = DTInterval(duties[j].end!.toUtc(), lastdutystart2);
  639. restPlace2 = duties[j].placeStart!;
  640. foundrecrest2 = (rest2.duration.inMinutes >= minrecrest.inMinutes) &&
  641. rest2.contains(DTInterval.fromHm(
  642. apartir: rest2.start.toUtc(),
  643. h: 23,
  644. m: 0,
  645. duration: const Duration(hours: 7, minutes: 59),
  646. ap: restPlace2)) &&
  647. rest2.contains(DTInterval.fromHm(
  648. apartir: rest2.start.toUtc().add(days: 1),
  649. h: 23,
  650. m: 0,
  651. duration: const Duration(hours: 7, minutes: 59),
  652. ap: restPlace2));
  653. // if (foundrecrest) print((rest);
  654. bool disruptive2 = (_isEarlyStart(duties[j]) ||
  655. _isLateFinish(duties[j]) ||
  656. _isNightDuty(duties[j]));
  657. if (!foundrecrest2 && disruptive2) {
  658. nbdisruptive2++;
  659. }
  660. }
  661. //print(("ftl: duty 60h: nbdisruptive: $nbdisruptive2");
  662. }
  663. if (nbdisruptive2 >= 4) {
  664. const restlength = Duration(hours: 60);
  665. final lastduty = ((index >= 2 &&
  666. duties[index - 1]
  667. .legals
  668. .any((e) => e.legalCause == FtlLegalCause.restrecurrent))
  669. ? duties[index - 2]
  670. : duties[index - 1]);
  671. duty.legals.add(FtlLegal(
  672. legalCause: FtlLegalCause.restrecurrent,
  673. legalCauseMsg:
  674. "More than 4 disruptive duties found before recurrent rest. Need ${restlength.inHours}h rest. Next duty can be started after <${lastduty.end!.addDuration(restlength).max(DTInterval.fromHm(apartir: lastduty.end!.toUtc(), h: 23, m: 0, duration: const Duration(hours: 7, minutes: 59), ap: lastduty.placeEnd!).end.add(days: 1)).format(pattern: "ddMMMyy HH:mm")}>"));
  675. } else {
  676. // if (DTInterval(duties[i].start!, duty.end!).duration.inMinutes >
  677. // print("ftl nb disruptive $nbdisruptive");
  678. if (dutyl.duration.inMinutes > maxdutyrecrest.inMinutes) {
  679. final restlength =
  680. ((nbdisruptive <= 4) ? minrecrest : const Duration(hours: 60));
  681. final lastduty = ((index >= 2 &&
  682. duties[index - 1]
  683. .legals
  684. .any((e) => e.legalCause == FtlLegalCause.restrecurrent))
  685. ? duties[index - 2]
  686. : duties[index - 1]);
  687. duty.legals.add(FtlLegal(
  688. legalCause: FtlLegalCause.restrecurrent,
  689. legalCauseMsg:
  690. "More than ${maxdutyrecrest.inHours}h without recurrent rest. from <${rest.end.format(pattern: "ddMMMyy HH:mm")}> to <${duty.end!.format(pattern: "ddMMMyy HH:mm")}> ${DTInterval(rest.end, duty.end!).duration.tohhmm}. ${maxdutyrecrest.inHours}h ends at <${rest.end.addDuration(maxdutyrecrest).format(pattern: "ddMMMyy HH:mm")}>. Need ${restlength.inHours}h rest. Next duty can be started after <${lastduty.end!.addDuration(restlength).max(DTInterval.fromHm(apartir: lastduty.end!.toUtc(), h: 23, m: 0, duration: const Duration(hours: 7, minutes: 59), ap: lastduty.placeEnd!).end.add(days: 1)).format(pattern: "ddMMMyy HH:mm")}>"));
  691. }
  692. }
  693. // 48h inc 2 local days
  694. final startmonth = changeTz(duty.start!.startOf(Unit.month), base);
  695. final endmonth = startmonth.endOf(Unit.month);
  696. if (duty.interval.isOverlap(
  697. DTInterval(endmonth.subtract(days: 4).add(minutes: 1), endmonth))) {
  698. int nb48inmonth = 0;
  699. List<FtlDuty> dutiesmonth = duties
  700. .where((e) => DTInterval(startmonth, endmonth)
  701. .isOverlap(DTInterval(e.start!, e.end!)))
  702. .toList();
  703. List<DTInterval> restmonth = DTInterval(startmonth, endmonth).minusmany(
  704. dutiesmonth
  705. .map((e) => DTInterval(changeTz(e.start!, e.placeStart!),
  706. changeTz(e.end!, e.placeStart!)))
  707. .toList());
  708. // print(restmonth);
  709. while (restmonth.isNotEmpty) {
  710. final e = restmonth.removeAt(0);
  711. final localdays = DTInterval.fromHm(
  712. apartir: e.start.toUtc(),
  713. h: 0,
  714. m: 0,
  715. duration: const Duration(hours: 47, minutes: 59),
  716. ap: base);
  717. if (e.contains(localdays)) {
  718. nb48inmonth++;
  719. // print(
  720. // "2localdays: ${localdays.start.format(pattern: "ddMMMyy HH:mm")} ${localdays.end.format(pattern: "ddMMMyy HH:mm")}");
  721. // print(
  722. // "in rest ${e.start.format(pattern: "ddMMMyy HH:mm")} ${e.end.format(pattern: "ddMMMyy HH:mm")}");
  723. restmonth = [...e.minus(localdays), ...restmonth];
  724. }
  725. }
  726. if (nb48inmonth == 0) {
  727. duty.legals.add(FtlLegal(
  728. legalCause: FtlLegalCause.restrecurrent,
  729. legalCauseMsg:
  730. "Before duty, need 2x2 local days rest from: ${startmonth.format(pattern: "ddMMMyy HH:mm")} to: ${endmonth.format(pattern: "ddMMMyy HH:mm")}"));
  731. } else if (nb48inmonth == 1 &&
  732. DTInterval(endmonth.subtract(days: 2).add(minutes: 1), endmonth)
  733. .isOverlap(duty.interval)) {
  734. duty.legals.add(FtlLegal(
  735. legalCause: FtlLegalCause.restrecurrent,
  736. legalCauseMsg:
  737. "Before duty, need 2x2 local days rest from: ${startmonth.format(pattern: "ddMMMyy HH:mm")} to: ${endmonth.format(pattern: "ddMMMyy HH:mm")}"));
  738. } else if (nb48inmonth == 1 &&
  739. DTInterval(endmonth.subtract(days: 4).add(minutes: 1),
  740. endmonth.subtract(days: 2))
  741. .isOverlap(duty.interval) &&
  742. DTInterval(endmonth.subtract(days: 4).add(minutes: 1),
  743. endmonth.subtract(days: 2))
  744. .intersectionmany(
  745. [...dutiesAsInterval, ...stdbyAsInterval]).isEmpty) {
  746. duty.legals.add(FtlLegal(
  747. legalCause: FtlLegalCause.restrecurrent,
  748. legalCauseMsg:
  749. "Before duty, need 2x2 local days rest from: ${startmonth.format(pattern: "ddMMMyy HH:mm")} to: ${endmonth.format(pattern: "ddMMMyy HH:mm")}"));
  750. }
  751. }
  752. // disruptive
  753. duty.lateFinish = _isLateFinish(duty);
  754. duty.earlyStart = _isEarlyStart(duty);
  755. duty.nightDuty = _isNightDuty(duty);
  756. if (duty.fdpStart != null &&
  757. lastduty != null &&
  758. base == lastduty.placeEnd &&
  759. (_isLateFinish(lastduty) || _isNightDuty(lastduty)) &&
  760. _isEarlyStart(duty)) {
  761. // log("ftl: found late finish or nighty <${duty.start!.format(pattern: "ddMMMyy HH:mm")}");
  762. if (!DTInterval(lastduty.end!, duty.start!).contains(DTInterval.fromHm(
  763. apartir: changeTz(lastduty.end!, lastduty.placeEnd!).toUtc(),
  764. h: 23,
  765. m: 0,
  766. duration: const Duration(hours: 7, minutes: 59),
  767. ap: duty.placeStart!))) {
  768. duty.legals.add(FtlLegal(
  769. legalCause: FtlLegalCause.disruptive,
  770. legalCauseMsg:
  771. "Disruptive duty; one local night of rest is required before early departure."));
  772. }
  773. }
  774. //consecutive night duty
  775. if (_isConsecutiveNight(duty, lastduty)) {
  776. var i = index;
  777. final monthint = DTInterval(
  778. changeTz(duty.start!.startOf(Unit.month), base),
  779. changeTz(duty.start!.endOf(Unit.month), base));
  780. int nbconsnight = 0;
  781. while (i > 0 &&
  782. duties[i]
  783. .interval
  784. .isOverlap(changeTzDt(monthint, duty.placeStart!))) {
  785. if (_isConsecutiveNight(duties[i], duties[i - 1])) {
  786. nbconsnight++;
  787. }
  788. i--;
  789. }
  790. if (nbconsnight > 1) {
  791. duty.legals.add(FtlLegal(
  792. legalCause: FtlLegalCause.disruptive,
  793. legalCauseMsg:
  794. "Consecutive nights is allowed only once per civil month."));
  795. } else {
  796. duty.legals.add(FtlLegal(
  797. legalCause: FtlLegalCause.disruptive,
  798. condition:
  799. "Two consecutive night duties; crew member agreement is required.",
  800. legalCauseMsg: ""));
  801. }
  802. }
  803. //local day rest if night duty
  804. if (lastduty != null && _isNightDuty(lastduty)) {
  805. if (DTInterval.fromHm(
  806. apartir: changeTz(lastduty.end!, lastduty.placeEnd!).toUtc(),
  807. h: 0,
  808. m: 0,
  809. duration: const Duration(hours: 23, minutes: 59),
  810. ap: lastduty.placeEnd!)
  811. .isOverlap(changeTzDt(duty.interval, duty.placeStart!))) {
  812. duty.legals.add(FtlLegal(
  813. legalCause: FtlLegalCause.disruptive,
  814. condition: "You can request a local day off after last night duty.",
  815. legalCauseMsg: ""));
  816. }
  817. }
  818. // if (duty.legals.isNotEmpty) {
  819. // log("${duty.legals.map((e) => e.legalCauseMsg)}");
  820. // }
  821. // duties[index] = duty;
  822. }
  823. bool _isConsecutiveNight(FtlDuty duty, FtlDuty? lastduty) =>
  824. _isNightDuty(duty) &&
  825. lastduty != null &&
  826. DTInterval.fromHm(
  827. apartir: changeTz(lastduty.end!, lastduty.placeEnd!).toUtc(),
  828. h: 2,
  829. m: 0,
  830. duration: const Duration(hours: 2, minutes: 59),
  831. ap: lastduty.placeEnd!)
  832. .isOverlap(lastduty.interval.toUtc());
  833. FtlDuty? _addDutyDetail(
  834. {required int index, required FtlDuty duty, Jiffy? checkin}) {
  835. final one = dutiesDetails[index];
  836. if (_fdpList.contains(one.type)) {
  837. duty.placeStart = duty.placeStart ?? one.placeStart;
  838. duty.placeEnd = one.placeEnd;
  839. //duty.start = duty.start ?? _calcPreflight(one).latest(checkin);
  840. duty.start = duty.start ?? checkin ?? _calcPreflight(one);
  841. duty.end = _calcPostflight(one);
  842. if (one.type == FtlDutyDetailsType.flight) {
  843. duty.type = FtlDutyType.fdp;
  844. duty.sectors++;
  845. duty.fdpStart = duty.start;
  846. duty.fdpEnd = one.end;
  847. } else {
  848. duty.type = duty.type ?? FtlDutyType.duty;
  849. }
  850. duty.dutiesDetailsIndex.add(index);
  851. } else if ([FtlDutyDetailsType.standby, FtlDutyDetailsType.reserve]
  852. .contains(one.type)) {
  853. duty.placeStart = duty.placeStart ?? one.placeStart;
  854. duty.placeEnd = one.placeStart;
  855. duty.start = duty.start ?? _calcPreflight(one);
  856. duty.end = one.end;
  857. duty.type = FtlDutyType.other;
  858. duty.dutiesDetailsIndex.add(index);
  859. } else if ([FtlDutyDetailsType.ground, FtlDutyDetailsType.sim]
  860. .contains(one.type)) {
  861. duty.placeStart = duty.placeStart ?? one.placeStart;
  862. duty.placeEnd = one.placeStart;
  863. duty.start = duty.start ?? _calcPreflight(one);
  864. duty.end = one.end;
  865. duty.type = duty.type ?? FtlDutyType.duty;
  866. duty.dutiesDetailsIndex.add(index);
  867. } else {
  868. return null;
  869. }
  870. return duty;
  871. }
  872. static bool _isEarlyStart(FtlDuty x) {
  873. //500 559
  874. // print(
  875. // "$x is earlystart: ${DTInterval.fromHm(apartir: changeTz(x.start!, x.placeStart!), h: 5, m: 0, duration: const Duration(minutes: 59)).include(x.start!)}");
  876. return (x.fdpStart != null &&
  877. DTInterval.fromHm(
  878. apartir: changeTz(x.start!.subtract(hours: 24), x.placeStart!)
  879. .toUtc(),
  880. h: 5,
  881. m: 0,
  882. duration: const Duration(minutes: 59),
  883. ap: x.placeStart!)
  884. .include(changeTz(x.start!, x.placeStart!)));
  885. }
  886. static bool _isLateFinish(FtlDuty x) {
  887. //2300 159
  888. // print(
  889. // "$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!)}");
  890. return (x.type != FtlDutyType.other) &&
  891. (DTInterval.fromHm(
  892. apartir: changeTz(x.start!, x.placeStart!).toUtc(),
  893. h: 23,
  894. m: 0,
  895. duration: const Duration(hours: 2, minutes: 59),
  896. ap: x.placeStart!)
  897. .include(changeTz(x.end!, x.placeStart!)));
  898. }
  899. static bool _isNightDuty(FtlDuty x) {
  900. //200 459
  901. // print(
  902. // "$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!)}");
  903. return (x.type != FtlDutyType.other) &&
  904. ((DTInterval.fromHm(
  905. apartir: x.start!.toUtc(),
  906. h: 2,
  907. m: 0,
  908. duration: const Duration(hours: 2, minutes: 59),
  909. ap: x.placeStart!))
  910. .isOverlap(x.interval.toUtc()) ||
  911. (DTInterval.fromHm(
  912. apartir: x.start!.subtract(days: 1).toUtc(),
  913. h: 2,
  914. m: 0,
  915. duration: const Duration(hours: 2, minutes: 59),
  916. ap: x.placeStart!))
  917. .isOverlap(x.interval.toUtc()));
  918. }
  919. Duration tzDiff(String iata1, String iata2, DateTime sourceDateTime) {
  920. // print(Airports.instance.find(iata1)!.timezoneid);
  921. // print(Airports.instance.find(iata2)!.timezoneid);
  922. final iata1Location = tz.getLocation(Airports.find(iata1)!.timezoneid);
  923. final iata2Location = tz.getLocation(Airports.find(iata2)!.timezoneid);
  924. tz.TZDateTime iata1TZDateTime =
  925. tz.TZDateTime.from(sourceDateTime, iata1Location);
  926. tz.TZDateTime iata2TZDateTime =
  927. tz.TZDateTime.from(sourceDateTime, iata2Location);
  928. return Duration(
  929. minutes: (iata2TZDateTime.timeZoneOffset.inMinutes -
  930. iata1TZDateTime.timeZoneOffset.inMinutes)
  931. .abs());
  932. }
  933. String _acclimatized(
  934. {required Duration tzoffset, required Duration timeElapsed}) {
  935. if (tzoffset.inHours <= 2) {
  936. return "D";
  937. } else if (timeElapsed.inHours < 48) {
  938. return "B";
  939. } else if (tzoffset.inHours < 4 && timeElapsed.inHours >= 48) {
  940. return "D";
  941. } else if (tzoffset.inHours <= 6 && timeElapsed.inHours >= 72) {
  942. return "D";
  943. } else if (tzoffset.inHours <= 9 && timeElapsed.inHours >= 96) {
  944. return "D";
  945. } else if (tzoffset.inHours <= 12 && timeElapsed.inHours >= 120) {
  946. return "D";
  947. } else {
  948. return "X";
  949. }
  950. }
  951. static Jiffy changeTz(Jiffy jiffy, String iata) {
  952. return Jiffy.parseFromDateTime(tz.TZDateTime.from(jiffy.dateTime,
  953. tz.getLocation(Airports.find(iata)?.timezoneid ?? "UTC")));
  954. }
  955. static DTInterval changeTzDt(DTInterval dt, String iata) {
  956. return DTInterval(changeTz(dt.start, iata), changeTz(dt.end, iata));
  957. }
  958. Duration _fdpMax(
  959. {required reftime,
  960. String? iata,
  961. required int sectors,
  962. required Map<String, List<String>> tab}) {
  963. Jiffy reftjiffy = Jiffy.now();
  964. String reft = "";
  965. if (reftime is Jiffy && iata != null) {
  966. reftjiffy = reftime;
  967. reftjiffy = changeTz(reftjiffy, iata);
  968. reft = reftjiffy.format(pattern: "HHmm");
  969. } else if (reftime is DateTime && iata != null) {
  970. reftjiffy = Jiffy.parseFromDateTime(reftime);
  971. reftjiffy = changeTz(reftjiffy, iata);
  972. reft = reftjiffy.format(pattern: "HHmm");
  973. } else if (reftime is String && reftime.length == 4) {
  974. reft = reftime;
  975. } else if (reftime is String && iata != null) {
  976. reftjiffy = Jiffy.parse(reftime);
  977. reftjiffy = changeTz(reftjiffy, iata);
  978. reft = reftjiffy.format(pattern: "HHmm");
  979. } else {
  980. throw ("_fdpMax type of reftime unrecognized!!!");
  981. }
  982. // print("$iata $reft ${reftjiffy.toUtc().Hm}");
  983. for (String key in tab.keys) {
  984. List<String> intervalle = key.split("-");
  985. int cmph1 = reft.compareTo(intervalle[0]);
  986. int cmph2 = reft.compareTo(intervalle[1]);
  987. bool inint = (intervalle[0].compareTo(intervalle[1]) < 0)
  988. ? (cmph1 >= 0 && cmph2 <= 0)
  989. : ((cmph1 >= 0 || cmph2 <= 0));
  990. if (inint) {
  991. final List<String>? lres = tab[key];
  992. final int nrow = ((sectors == 1) ? 2 : sectors) - 2;
  993. final String? res =
  994. (lres != null && nrow < lres.length) ? lres[nrow] : null;
  995. if (res != null && res != "Not allowed") {
  996. return Duration(
  997. hours: int.parse(res.split(".")[0]),
  998. minutes: int.parse(res.split(".")[1]));
  999. }
  1000. }
  1001. }
  1002. return Duration.zero;
  1003. }
  1004. Duration fdpMaxBasic(
  1005. {required reftime, String? iata, required int sectors}) =>
  1006. _fdpMax(reftime: reftime, iata: iata, sectors: sectors, tab: _fdpMaxTab);
  1007. Duration fdpMaxExtBasic(
  1008. {required reftime, String? iata, required int sectors}) =>
  1009. _fdpMax(
  1010. reftime: reftime, iata: iata, sectors: sectors, tab: _fdpMaxExtTab);
  1011. Duration fdpMaxUnk({required reftime, String? iata, required int sectors}) =>
  1012. _fdpMax(reftime: reftime, iata: iata, sectors: sectors, tab: _fdpUnkTab);
  1013. Duration fdpMaxUnkFrms(
  1014. {required reftime, String? iata, required int sectors}) =>
  1015. _fdpMax(
  1016. reftime: reftime, iata: iata, sectors: sectors, tab: _fdpUnkFrmsTab);
  1017. //acclim
  1018. final Map<String, List<String>> _fdpMaxTab = {
  1019. "0600-1329": [
  1020. "13.00",
  1021. "12.30",
  1022. "12.00",
  1023. "11.30",
  1024. "11.00",
  1025. "10.30",
  1026. "10.00",
  1027. "09.30",
  1028. "09.00",
  1029. ],
  1030. "1330-1359": [
  1031. "12.45",
  1032. "12.15",
  1033. "11.45",
  1034. "11.15",
  1035. "10.45",
  1036. "10.15",
  1037. "09.45",
  1038. "09.15",
  1039. "09.00",
  1040. ],
  1041. "1400-1429": [
  1042. "12.30",
  1043. "12.00",
  1044. "11.30",
  1045. "11.00",
  1046. "10.30",
  1047. "10.00",
  1048. "09.30",
  1049. "09.00",
  1050. "09.00",
  1051. ],
  1052. "1430-1459": [
  1053. "12.15",
  1054. "11.45",
  1055. "11.15",
  1056. "10.45",
  1057. "10.15",
  1058. "09.45",
  1059. "09.15",
  1060. "09.00",
  1061. "09.00",
  1062. ],
  1063. "1500-1529": [
  1064. "12.00",
  1065. "11.30",
  1066. "11.00",
  1067. "10.30",
  1068. "10.00",
  1069. "09.30",
  1070. "09.00",
  1071. "09.00",
  1072. "09.00",
  1073. ],
  1074. "1530-1559": [
  1075. "11.45",
  1076. "11.15",
  1077. "10.45",
  1078. "10.15",
  1079. "09.45",
  1080. "09.15",
  1081. "09.00",
  1082. "09.00",
  1083. "09.00",
  1084. ],
  1085. "1600-1629": [
  1086. "11.30",
  1087. "11.00",
  1088. "10.30",
  1089. "10.00",
  1090. "09.30",
  1091. "09.00",
  1092. "09.00",
  1093. "09.00",
  1094. "09.00",
  1095. ],
  1096. "1630-1659": [
  1097. "11.15",
  1098. "10.45",
  1099. "10.15",
  1100. "09.45",
  1101. "09.15",
  1102. "09.00",
  1103. "09.00",
  1104. "09.00",
  1105. "09.00",
  1106. ],
  1107. "1700-0459": [
  1108. "11.00",
  1109. "10.30",
  1110. "10.00",
  1111. "09.30",
  1112. "09.00",
  1113. "09.00",
  1114. "09.00",
  1115. "09.00",
  1116. "09.00",
  1117. ],
  1118. "0500-0514": [
  1119. "12.00",
  1120. "11.30",
  1121. "11.00",
  1122. "10.30",
  1123. "10.00",
  1124. "09.30",
  1125. "09.00",
  1126. "09.00",
  1127. "09.00",
  1128. ],
  1129. "0515-0529": [
  1130. "12.15",
  1131. "11.45",
  1132. "11.15",
  1133. "10.45",
  1134. "10.15",
  1135. "09.45",
  1136. "09.15",
  1137. "09.00",
  1138. "09.00",
  1139. ],
  1140. "0530-0544": [
  1141. "12.30",
  1142. "12.00",
  1143. "11.30",
  1144. "11.00",
  1145. "10.30",
  1146. "10.00",
  1147. "09.30",
  1148. "09.00",
  1149. "09.00",
  1150. ],
  1151. "0545-0559": [
  1152. "12.45",
  1153. "12.15",
  1154. "11.45",
  1155. "11.15",
  1156. "10.45",
  1157. "10.15",
  1158. "09.45",
  1159. "09.15",
  1160. "09.00",
  1161. ],
  1162. };
  1163. final Map<String, List<String>> _fdpMaxExtTab = {
  1164. "0600-0614": [
  1165. "Not allowed",
  1166. "Not allowed",
  1167. "Not allowed",
  1168. "Not allowed",
  1169. ],
  1170. "0615-0629": [
  1171. "13.15",
  1172. "12.45",
  1173. "12.15",
  1174. "11.45",
  1175. ],
  1176. "0630-0644": [
  1177. "13.30",
  1178. "13.00",
  1179. "12.30",
  1180. "12.00",
  1181. ],
  1182. "0645-0659": [
  1183. "13.45",
  1184. "13.15",
  1185. "12.45",
  1186. "12.15",
  1187. ],
  1188. "0700-1329": [
  1189. "14.00",
  1190. "13.30",
  1191. "13.00",
  1192. "12.30",
  1193. ],
  1194. "1330-1359": [
  1195. "13.45",
  1196. "13.15",
  1197. "12.45",
  1198. "Not allowed",
  1199. ],
  1200. "1400-1429": [
  1201. "13.30",
  1202. "13.00",
  1203. "12.30",
  1204. "Not allowed",
  1205. ],
  1206. "1430-1459": [
  1207. "13.15",
  1208. "12.45",
  1209. "12.15",
  1210. "Not allowed",
  1211. ],
  1212. "1500-1529": [
  1213. "13.00",
  1214. "12.30",
  1215. "12.00",
  1216. "Not allowed",
  1217. ],
  1218. "1530-1559": [
  1219. "12.45",
  1220. "Not allowed",
  1221. "Not allowed",
  1222. "Not allowed",
  1223. ],
  1224. "1600-1629": [
  1225. "12.30",
  1226. "Not allowed",
  1227. "Not allowed",
  1228. "Not allowed",
  1229. ],
  1230. "1630-1659": [
  1231. "12.15",
  1232. "Not allowed",
  1233. "Not allowed",
  1234. "Not allowed",
  1235. ],
  1236. "1700-1729": [
  1237. "12.00",
  1238. "Not allowed",
  1239. "Not allowed",
  1240. "Not allowed",
  1241. ],
  1242. "1730-1759": [
  1243. "11.45",
  1244. "Not allowed",
  1245. "Not allowed",
  1246. "Not allowed",
  1247. ],
  1248. "1800-1829": [
  1249. "11.30",
  1250. "Not allowed",
  1251. "Not allowed",
  1252. "Not allowed",
  1253. ],
  1254. "1830-1859": [
  1255. "11.15",
  1256. "Not allowed",
  1257. "Not allowed",
  1258. "Not allowed",
  1259. ],
  1260. "1900-0359": [
  1261. "Not allowed",
  1262. "Not allowed",
  1263. "Not allowed",
  1264. "Not allowed",
  1265. ],
  1266. "0400-0414": [
  1267. "Not allowed",
  1268. "Not allowed",
  1269. "Not allowed",
  1270. "Not allowed",
  1271. ],
  1272. "0415-0429": [
  1273. "Not allowed",
  1274. "Not allowed",
  1275. "Not allowed",
  1276. "Not allowed",
  1277. ],
  1278. "0430-0444": [
  1279. "Not allowed",
  1280. "Not allowed",
  1281. "Not allowed",
  1282. "Not allowed",
  1283. ],
  1284. "0445-0459": [
  1285. "Not allowed",
  1286. "Not allowed",
  1287. "Not allowed",
  1288. "Not allowed",
  1289. ],
  1290. "0500-0514": [
  1291. "Not allowed",
  1292. "Not allowed",
  1293. "Not allowed",
  1294. "Not allowed",
  1295. ],
  1296. "0515-0529": [
  1297. "Not allowed",
  1298. "Not allowed",
  1299. "Not allowed",
  1300. "Not allowed",
  1301. ],
  1302. "0530-0544": [
  1303. "Not allowed",
  1304. "Not allowed",
  1305. "Not allowed",
  1306. "Not allowed",
  1307. ],
  1308. "0545-0559": [
  1309. "Not allowed",
  1310. "Not allowed",
  1311. "Not allowed",
  1312. "Not allowed",
  1313. ]
  1314. };
  1315. final Map<String, List<String>> _fdpUnkTab = {
  1316. "0000-2359": ["11.00", "10:30", "10.00", "09.30", "09.00", "09.00", "09.00"]
  1317. };
  1318. final Map<String, List<String>> _fdpUnkFrmsTab = {
  1319. "0000-2359": ["12.00", "11.30", "11.00", "10.30", "10.00", "09.30", "09.00"]
  1320. };
  1321. }
  1322. class FtlDuty {
  1323. FtlDuty({this.start, this.end, this.placeStart, this.placeEnd, this.type});
  1324. @override
  1325. String toString() {
  1326. return "${start?.yMEd} ${start?.Hm} >>> ${end?.Hm} : ${fdpLength.inMinutes > 0 ? "FDP" : "DUTY"} ${dutiesDetailsIndex.length}leg(s)";
  1327. }
  1328. Jiffy? start;
  1329. Jiffy? end;
  1330. DTInterval get interval => DTInterval(start!, end!);
  1331. String? placeStart;
  1332. String? placeEnd;
  1333. FtlDutyType? type;
  1334. List<int> dutiesDetailsIndex = [];
  1335. Jiffy? fdpStart;
  1336. Jiffy? fdpEnd;
  1337. int sectors = 0;
  1338. Duration? fdpMax;
  1339. Duration? fdpExtMax;
  1340. //Duration? fdpIfRExtMax;//if inflight rest rest
  1341. bool fdpExt = false;
  1342. List<FtlLegal> legals = [];
  1343. Duration get dutyLength => (start != null && end != null)
  1344. ? end!.dateTime.difference(start!.dateTime)
  1345. : Duration.zero;
  1346. Duration get fdpLength => (fdpStart != null && fdpEnd != null)
  1347. ? fdpEnd!.dateTime.difference(fdpStart!.dateTime)
  1348. : Duration.zero;
  1349. // Duration? get rest => (dutyLength.max(Ftl.minimumrest(placeEnd)));
  1350. Jiffy? restends;
  1351. String? acclim;
  1352. String? reftime;
  1353. FtlDutyTotal? dutyTotal;
  1354. //Jiffy? report;
  1355. Jiffy? reportdelay1;
  1356. Jiffy? notification1;
  1357. Jiffy? reportdelay2;
  1358. Jiffy? notification2;
  1359. bool earlyStart = false;
  1360. bool lateFinish = false;
  1361. bool nightDuty = false;
  1362. }
  1363. class FtlLegal {
  1364. FtlLegal(
  1365. {required this.legalCause,
  1366. required this.legalCauseMsg,
  1367. this.legalIndex,
  1368. this.condition});
  1369. FtlLegalCause legalCause;
  1370. String legalCauseMsg;
  1371. int? legalIndex;
  1372. String? condition;
  1373. }
  1374. enum FtlDutyType { duty, fdp, other }
  1375. class FtlDutyDetails {
  1376. FtlDutyDetails({
  1377. this.start,
  1378. this.end,
  1379. this.placeStart,
  1380. this.placeEnd,
  1381. this.type,
  1382. this.label,
  1383. });
  1384. Jiffy? start;
  1385. Jiffy? end;
  1386. DTInterval get interval => DTInterval(start!, end!);
  1387. Duration get duration => end!.dateTime.difference(start!.dateTime);
  1388. String? placeStart;
  1389. String? placeEnd;
  1390. FtlDutyDetailsType? type;
  1391. String get typeString {
  1392. switch (type) {
  1393. case FtlDutyDetailsType.flight:
  1394. return "FLT";
  1395. case FtlDutyDetailsType.dhflight:
  1396. return "DH-FLT";
  1397. case FtlDutyDetailsType.dhlimo:
  1398. return "DH-LIMO";
  1399. case FtlDutyDetailsType.sim:
  1400. return "SIM";
  1401. case FtlDutyDetailsType.standby:
  1402. return "STBY";
  1403. case FtlDutyDetailsType.reserve:
  1404. return "RSRV";
  1405. case FtlDutyDetailsType.ground:
  1406. return "GRND";
  1407. default:
  1408. return "UNK";
  1409. }
  1410. }
  1411. String? label;
  1412. }
  1413. enum FtlDutyDetailsType {
  1414. preflight,
  1415. flight,
  1416. postflight,
  1417. dhflight,
  1418. dhlimo,
  1419. ground,
  1420. standby,
  1421. reserve,
  1422. sim,
  1423. // training,
  1424. // other
  1425. }
  1426. class FtlDutyTotal {
  1427. FtlDutyTotal(
  1428. {required this.date, required this.dutyLength, required this.fltLength});
  1429. Jiffy date;
  1430. Duration dutyLength;
  1431. Duration fltLength;
  1432. Duration? dutylast7;
  1433. Duration? dutylast14;
  1434. Duration? dutylast28;
  1435. Duration? fltlast28;
  1436. Duration? fltyear;
  1437. Duration? fltlast12;
  1438. }
  1439. enum FtlLegalCause {
  1440. dutylast7,
  1441. dutylast14,
  1442. dutylast28,
  1443. fltlast28,
  1444. fltyear,
  1445. fltlast12,
  1446. fdpmax,
  1447. restfdp,
  1448. restrecurrent,
  1449. disruptive
  1450. //check duty length && flt length
  1451. //check fdp leength
  1452. //check rest before fdp
  1453. //check recurrent rest
  1454. //check disruptive sched
  1455. }