| 184 | | v_dispatcher_company_id := (select eh.company_id |
| 185 | | from dispatcher d |
| 186 | | join employmenthistory eh on eh.employee_user_id = d.user_id and |
| 187 | | (eh.end_date is null or eh.end_date > now()) |
| 188 | | where d.user_id = v_dispatcher_user_id |
| 189 | | limit 1); |
| 190 | | |
| 191 | | if v_dispatcher_company_id is null then |
| 192 | | raise exception 'Dispatcher % is not assigned to a company', |
| 193 | | v_dispatcher_user_id; |
| 194 | | end if; |
| 195 | | |
| 196 | | v_start_position := (select start_location from request where id = v_request_id); |
| 197 | | |
| 198 | | v_driver_user_id := (select d.user_id |
| 199 | | from driver d |
| 200 | | join driver_vehicle dc on d.user_id = dc.id_driver |
| 201 | | join employmenthistory eh |
| 202 | | on eh.employee_user_id = d.user_id and (eh.end_date is null or |
| 203 | | eh.end_date > now()) |
| 204 | | where dc.time_to is null |
| 205 | | and eh.company_id = v_dispatcher_company_id |
| 206 | | and d.location is not null |
| 207 | | order by d.location <-> v_start_position |
| 208 | | limit 1); |
| 209 | | |
| 210 | | if v_driver_user_id is null then |
| 211 | | raise exception 'No available drivers found for request %', v_request_id; |
| | 185 | if v_dispatcher_user_id is not null then |
| | 186 | v_dispatcher_company_id := (select eh.company_id |
| | 187 | from dispatcher d |
| | 188 | join employmenthistory eh on eh.employee_user_id = d.user_id and |
| | 189 | (eh.end_date is null or eh.end_date > now()) |
| | 190 | where d.user_id = v_dispatcher_user_id |
| | 191 | limit 1); |
| | 192 | |
| | 193 | if v_dispatcher_company_id is null then |
| | 194 | raise exception 'Dispatcher % is not assigned to a company', v_dispatcher_user_id; |
| | 195 | end if; |
| | 196 | |
| | 197 | v_start_position := (select start_location from request where id = v_request_id); |
| | 198 | |
| | 199 | |
| | 200 | v_computed_driver_id := (select d.user_id |
| | 201 | from driver d |
| | 202 | join driver_vehicle dc on d.user_id = dc.id_driver |
| | 203 | join employmenthistory eh |
| | 204 | on eh.employee_user_id = d.user_id and (eh.end_date is null or |
| | 205 | eh.end_date > now()) |
| | 206 | where dc.time_to is null |
| | 207 | and eh.company_id = v_dispatcher_company_id |
| | 208 | and d.location is not null |
| | 209 | order by d.location <-> v_start_position |
| | 210 | limit 1); |
| | 211 | |
| | 212 | if v_computed_driver_id is null then |
| | 213 | raise exception 'No available drivers found for request %', v_request_id; |
| | 214 | end if; |
| | 215 | |
| | 216 | else |
| | 217 | if v_driver_user_id is null then |
| | 218 | raise exception 'A driver_user_id must be specified if dispatcher is null'; |
| | 219 | end if; |
| | 220 | |
| | 221 | if not exists(select 1 from freelancedriver where driver_user_id = v_driver_user_id) then |
| | 222 | raise exception 'Driver % is not a registered freelance driver', v_driver_user_id; |
| | 223 | end if; |
| | 224 | |
| | 225 | v_computed_driver_id := v_driver_user_id; |