1616 decode_dates_filters ,
1717 get_parcel_info ,
1818 get_farm_operation_data ,
19- FarmInfo ,
19+ FarmInfo , display_pdf_parcel_details ,
2020)
2121from utils .json_handler import make_get_request
2222from geopy .geocoders import Nominatim
@@ -55,7 +55,7 @@ def __init__(
5555
5656
5757def create_farm_calendar_pdf (
58- calendar_data : FarmCalendarData , token : dict [str , str ]
58+ calendar_data : FarmCalendarData , token : dict [str , str ], parcel_id : str | None = None
5959) -> EX :
6060 """Create PDF report from farm calendar data"""
6161 pdf = EX ()
@@ -85,6 +85,11 @@ def create_farm_calendar_pdf(
8585 pdf .line (pdf .l_margin , y_position , line_end_x , y_position )
8686 pdf .ln (5 )
8787
88+ parcel_defined = False
89+ if parcel_id and len (calendar_data .operations ) > 1 :
90+ display_pdf_parcel_details (pdf , parcel_id , geolocator , token )
91+ parcel_defined = True
92+
8893 if len (calendar_data .operations ) == 1 :
8994 operation = calendar_data .operations [0 ]
9095
@@ -175,8 +180,9 @@ def create_farm_calendar_pdf(
175180 ag = operation .responsibleAgent if operation .responsibleAgent else ""
176181 pdf .multi_cell (0 , 8 , ag , ln = True , fill = True , align = "L" )
177182
178- pdf .set_font ("FreeSerif" , "B" , 10 )
179- pdf .cell (40 , 8 , "Initial Materials:" )
183+ pdf .set_font ("FreeSerif" , "B" , 12 )
184+ pdf .set_x (15 )
185+ pdf .cell (40 , 8 , "Initial Materials" )
180186 pdf .ln (15 )
181187 if calendar_data .materials :
182188 with pdf .table (text_align = "CENTER" , padding = 0.5 , v_align = VAlign .M ) as table :
@@ -204,10 +210,11 @@ def create_farm_calendar_pdf(
204210
205211 if len (calendar_data .operations ) > 1 :
206212 calendar_data .operations .sort (key = lambda x : x .hasStartDatetime )
213+ pdf .set_font ("FreeSerif" , "B" , 12 )
214+ pdf .set_x (15 )
215+ pdf .cell (0 , 10 , "Operations" , ln = True , align = 'L' )
216+ pdf .ln (5 )
207217 with pdf .table (text_align = "CENTER" , padding = 0.5 ) as table :
208- pdf .set_font ("FreeSerif" , "B" , 12 )
209- pdf .cell (0 , 10 , "Operations" , ln = True )
210- pdf .ln (5 )
211218
212219 row = table .row ()
213220 pdf .set_font ("FreeSerif" , "B" , 10 )
@@ -217,8 +224,9 @@ def create_farm_calendar_pdf(
217224 row .cell ("End" )
218225 row .cell ("Agent" )
219226 row .cell ("Machinery IDs" )
220- row .cell ("Parcel" )
221- row .cell ("Farm" )
227+ if not parcel_defined :
228+ row .cell ("Parcel" )
229+ row .cell ("Farm" )
222230 row .cell ("Compost Pile" )
223231 row .cell ("Responsible Agent" )
224232 pdf .set_font ("FreeSerif" , "" , 9 )
@@ -275,9 +283,10 @@ def create_farm_calendar_pdf(
275283 address = parcel_data .address
276284
277285 row .cell (f"{ machinery_ids } " )
278- row .cell (address )
279- farm_local = f"Name: { farm .name } | Municipality: { farm .municipality } "
280- row .cell (farm_local )
286+ if not parcel_defined :
287+ row .cell (address )
288+ farm_local = f"Name: { farm .name } | Municipality: { farm .municipality } "
289+ row .cell (farm_local )
281290 operation = calendar_data .operations [0 ]
282291 cp = (
283292 operation .isOperatedOn .get ("@id" ).split (":" )[3 ]
@@ -300,6 +309,7 @@ def create_farm_calendar_pdf(
300309 pdf .set_fill_color (0 , 255 , 255 )
301310
302311 pdf .set_font ("FreeSerif" , "B" , 12 )
312+ pdf .set_x (15 )
303313 pdf .cell (0 , 10 , "Data Table" , ln = True )
304314 pdf .ln (5 )
305315 types = {
@@ -440,44 +450,44 @@ def process_farm_calendar_data(
440450 if not operation_id :
441451 # Check for generic response
442452 if calendar_activity_type :
443- params ["name" ] = calendar_activity_type
444- farm_activity_type_info = make_get_request (
445- url = f'{ settings .REPORTING_FARMCALENDAR_BASE_URL } { settings .REPORTING_FARMCALENDAR_URLS ["activity_types" ]} ' ,
446- token = token ,
447- params = params ,
448- )
449-
450- del params ["name" ]
451-
452- if farm_activity_type_info :
453- params ["activity_type" ] = farm_activity_type_info [0 ][
454- "@id"
455- ].split (":" )[3 ]
456- decode_dates_filters (params , from_date , to_date )
457- observations = make_get_request (
458- url = obs_url ,
453+ if calendar_activity_type .strip () != "Compost Operation" :
454+ params ["name" ] = calendar_activity_type
455+ farm_activity_type_info = make_get_request (
456+ url = f'{ settings .REPORTING_FARMCALENDAR_BASE_URL } { settings .REPORTING_FARMCALENDAR_URLS ["activity_types" ]} ' ,
459457 token = token ,
460458 params = params ,
461459 )
462460
461+ del params ["name" ]
462+ if farm_activity_type_info :
463+
464+ params ["activity_type" ] = farm_activity_type_info [0 ][
465+ "@id"
466+ ].split (":" )[3 ]
467+ decode_dates_filters (params , from_date , to_date )
468+ observations = make_get_request (
469+ url = obs_url ,
470+ token = token ,
471+ params = params ,
472+ )
473+ else :
463474 if parcel_id :
464475 params ["parcel" ] = parcel_id
465476 operations = make_get_request (
466477 url = operation_url ,
467478 token = token ,
468479 params = params ,
469480 )
470-
471- del params ["activity_type" ]
472- for o in operations :
473- id = o ["@id" ].split (":" )[3 ]
474- get_farm_operation_data (
475- id = id ,
476- materials = materials ,
477- params = params ,
478- observations = observations ,
479- token = token ,
480- )
481+ if operations :
482+ for o in operations :
483+ id = o ["@id" ].split (":" )[3 ]
484+ get_farm_operation_data (
485+ id = id ,
486+ materials = materials ,
487+ params = params ,
488+ observations = observations ,
489+ token = token ,
490+ )
481491
482492 else :
483493 operation_url = f"{ operation_url } { operation_id } /"
@@ -555,7 +565,7 @@ def process_farm_calendar_data(
555565 materials = [],
556566 )
557567
558- pdf = create_farm_calendar_pdf (calendar_data , token )
568+ pdf = create_farm_calendar_pdf (calendar_data , token , parcel_id )
559569 pdf_dir = f"{ settings .PDF_DIRECTORY } { pdf_file_name } "
560570 os .makedirs (os .path .dirname (f"{ pdf_dir } .pdf" ), exist_ok = True )
561571 pdf .output (f"{ pdf_dir } .pdf" )
0 commit comments