1
0

ftl copy.dart.bak 44 KB

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