hmfayyaz commited on
Commit
75bbbd1
·
unverified ·
1 Parent(s): 6ae5445

Update formats.py

Browse files
Files changed (1) hide show
  1. src/formats.py +51 -2
src/formats.py CHANGED
@@ -1,9 +1,52 @@
1
  import pandas as pd
2
  import os
3
  import logging
 
4
 
5
  logger = logging.getLogger(__name__)
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  def format_iraqi_airways(data_list):
8
  """
9
  Formats data for Iraqi Airways template.
@@ -18,8 +61,11 @@ def format_iraqi_airways(data_list):
18
  gender_full = "Male" if sex == 'M' else "Female"
19
  title = "MR" if sex == 'M' else "MRS"
20
 
 
 
 
21
  row = {
22
- "TYPE": "Adult", # Default
23
  "TITLE": title,
24
  "FIRST NAME": item.get('name', ''),
25
  "LAST NAME": item.get('surname', ''),
@@ -65,11 +111,14 @@ def format_flydubai(data_list):
65
  dob_formatted = _to_ddmmmyy(item.get('date_of_birth', ''))
66
  expiry_formatted = _to_ddmmmyy(item.get('expiration_date', ''))
67
 
 
 
 
68
  row = {
69
  "Last Name": item.get('surname', ''),
70
  "First Name and Middle Name": first_middle,
71
  "Title": title,
72
- "PTC": "ADT", # Default to Adult
73
  "Gender": sex,
74
  "Date of Birth": dob_formatted,
75
  "Passport Last Name": item.get('surname', ''),
 
1
  import pandas as pd
2
  import os
3
  import logging
4
+ from datetime import datetime, date
5
 
6
  logger = logging.getLogger(__name__)
7
 
8
+ def calculate_passenger_type(dob_str):
9
+ """
10
+ Calculate passenger type based on age from date of birth.
11
+
12
+ Age groups:
13
+ - Infants: 0-1 year (0-11 months)
14
+ - Children: 1-12 years (12 months to 12 years)
15
+ - Adults: 12+ years
16
+
17
+ Returns:
18
+ For Iraqi Airways: "Infant", "Child", "Adult"
19
+ For Flydubai: "INF", "CHD", "ADT"
20
+ """
21
+ if not dob_str:
22
+ return "Adult", "ADT" # Default to adult if no DOB
23
+
24
+ try:
25
+ # Parse the date (assuming DD/MM/YYYY format)
26
+ dob = datetime.strptime(dob_str, '%d/%m/%Y').date()
27
+ today = date.today()
28
+
29
+ # Calculate age in years
30
+ age_years = today.year - dob.year
31
+ if today.month < dob.month or (today.month == dob.month and today.day < dob.day):
32
+ age_years -= 1
33
+
34
+ # Calculate age in months for more accurate infant check
35
+ age_months = (today.year - dob.year) * 12 + (today.month - dob.month)
36
+ if today.day < dob.day:
37
+ age_months -= 1
38
+
39
+ # Determine passenger type based on age
40
+ if age_months < 12: # Less than 12 months - Infant
41
+ return "Infant", "INF"
42
+ elif age_years < 12: # 1-11 years - Child
43
+ return "Child", "CHD"
44
+ else: # 12+ years - Adult
45
+ return "Adult", "ADT"
46
+
47
+ except (ValueError, TypeError):
48
+ return "Adult", "ADT" # Default to adult if date parsing fails
49
+
50
  def format_iraqi_airways(data_list):
51
  """
52
  Formats data for Iraqi Airways template.
 
61
  gender_full = "Male" if sex == 'M' else "Female"
62
  title = "MR" if sex == 'M' else "MRS"
63
 
64
+ # Calculate passenger type based on age
65
+ passenger_type, _ = calculate_passenger_type(item.get('date_of_birth', ''))
66
+
67
  row = {
68
+ "TYPE": passenger_type,
69
  "TITLE": title,
70
  "FIRST NAME": item.get('name', ''),
71
  "LAST NAME": item.get('surname', ''),
 
111
  dob_formatted = _to_ddmmmyy(item.get('date_of_birth', ''))
112
  expiry_formatted = _to_ddmmmyy(item.get('expiration_date', ''))
113
 
114
+ # Calculate passenger type based on age
115
+ _, ptc_code = calculate_passenger_type(item.get('date_of_birth', ''))
116
+
117
  row = {
118
  "Last Name": item.get('surname', ''),
119
  "First Name and Middle Name": first_middle,
120
  "Title": title,
121
+ "PTC": ptc_code, # Passenger Type Code based on age
122
  "Gender": sex,
123
  "Date of Birth": dob_formatted,
124
  "Passport Last Name": item.get('surname', ''),