Untitled
unknown
plain_text
2 years ago
8.0 kB
11
Indexable
=> This is for adding additional vechile we are not calling any e-vechile update services
def add_vehicle(self, new_vehicles):
from dispatcher.models.dispatch_vehicle_mapping import DispatchVehicleMapping
from dispatcher.models.helper_classes import DispatchVehicleType
if self.is_dispatched():
raise Exception(
'Dispatch#{0} has been dispatched. Vehicle can\'t be added to dispatch.'.format(self.id))
if len(new_vehicles) == 0:
raise Exception('No additional vehicle selected. Please select at least one vehicle to add in the dispatch.')
if not self.dispatch_vehicle:
raise Exception('You can not add additional vehicles to a dispatch not having primary vehicle')
if self.dispatch_vehicle and self.dispatch_vehicle.vehicle_type in [
DispatchVehicleType.FOOTER, DispatchVehicleType.BICYCLE, DispatchVehicleType.BIKE]:
raise Exception('You can not add additional vehicles to a dispatch having primary vehicle as '
'BIKE, BICYCLE or FOOTER')
vehicles = []
current_mapped_vehicles_count = DispatchVehicleMapping.objects.filter(
dispatch_id=self.id, vehicle_function__in=[Function.PRIMARY, Function.ADDITIONAL]).count()
if (current_mapped_vehicles_count + len(new_vehicles)) > 3:
raise Exception('You can not assign more than 2 additional Vehicles to a dispatch')
for vehicle in new_vehicles:
vehicles.append(
DispatchVehicleMapping.create_vehicle_mapping(
self.id,
vehicle
)
)
return vehicles
def create_vehicle_mapping(dispatch_id, data):
vehicle_mapping = None
vehicle_function = data.get('vehicle_function') or Function.PRIMARY
if data.get("registration_no"):
data['registration_no'] = data['registration_no'].upper()
if vehicle_function == Function.PRIMARY or vehicle_function == Function.ADDITIONAL:
not_allowed_vehicle_functions = [Function.PRIMARY, Function.ADDITIONAL]
vehicle_mapping = DispatchVehicleMapping.objects.filter(
registration_no=data['registration_no'],
dispatch__status__in=[DispatchStatus.WIP, DispatchStatus.DISPATCHED],
vehicle_function__in=not_allowed_vehicle_functions
)
vehicle_mapping = list(vehicle_mapping)
max_dispatches_allowd = 1
if data.get('allow_second_dispatch'):
max_dispatches_allowd = 2
if vehicle_mapping:
for x in vehicle_mapping:
if x.dispatch.status == DispatchStatus.WIP:
raise BusinessException("WIP Dispatch {id} already exist for the vehicle {registration_no}".format(
id=x.dispatch.id, registration_no=x.registration_no))
if vehicle_mapping and len(vehicle_mapping) >= max_dispatches_allowd:
logger.debug(
'Vehicle with registration_no {0} is already busy with dispatch {1}'.format(data['registration_no'],
vehicle_mapping[
0].dispatch_id))
raise BusinessException(
'Vehicle with registration_no {0} is already busy with dispatch {1}'.format(data['registration_no'],
vehicle_mapping[
0].dispatch_id))
vehicle_cost = data.get('vehicle_cost')
if not vehicle_cost:
vehicle_cost = 0
vehicle_mapping = DispatchVehicleMapping()
vehicle_mapping.registration_no = data.get("registration_no")
vehicle_mapping.vehicle_id = data.get("vehicle_id")
vehicle_mapping.fleet_id = data.get("fleet_id")
vehicle_mapping.start_km = data.get("start_km", 0)
vehicle_mapping.end_km = data.get("end_km", 0)
vehicle_mapping.contract_id = data.get("contract_id")
vehicle_mapping.dispatch_id = dispatch_id
vehicle_mapping.vehicle_type = data.get("vehicle_type")
vehicle_mapping.vehicle_function = data.get("vehicle_function") or Function.PRIMARY
vehicle_mapping.ownership = data.get("vehicle_ownership", DispatchVehicleOwnership.COMPANY_OWNED) or DispatchVehicleOwnership.COMPANY_OWNED
vehicle_mapping.created_by = Requester.get_current_user_id()
vehicle_mapping.vehicle_cost = vehicle_cost
vehicle_mapping.save()
if vehicle_mapping.registration_no and vehicle_function == Function.PRIMARY: # Validate for Concurrency
vehicle_mapping = DispatchVehicleMapping.objects.filter(registration_no=data['registration_no'],
dispatch__status__in=[DispatchStatus.WIP,
DispatchStatus.DISPATCHED],
vehicle_function=Function.PRIMARY).order_by('-id')[0]
return vehicle_mapping
where we update vechile service
we call this from common pkgs check then update the ewbn
def update_ewbn(self, pkg, async=None, vehicle=None, src=EwaybillUpdateSource.ADD_TO_DISPATCH):
"""
Args:
pkg (Package):
async (None | bool): In case of None, decision will be made here, else based on the value passed
Returns:
(None | dict): In case of async call, return None else response of the API request
"""
if pkg.is_mps() and not pkg.is_mps_master():
logger.debug('update_ewbn(%s): MPS Child Shipment', pkg.waybill)
return False
if not vehicle:
vehicle = self.dispatch_vehicle
if pkg.valid_ewaybill and pkg.e_waybills and vehicle and vehicle.registration_no:
payload = {
"ewaybillNos": pkg.e_waybills,
"modeOfTransport": "ROAD",
"vehicleNo": vehicle.registration_no,
"transhipmentLocation": self.center.city,
"consolidate": False,
"updateReason": "TRANSHIPMENT",
"state": self.center.state_abbr
}
logger.info('EWaybillUpdate: %s %s %s %s', payload.get('ewaybillNos'), payload.get('vehicleNo'), payload.get('state'), payload.get('transhipmentLocation'))
# Earlier, ewaybill was updated using third party systems. It was changed so that ewaybill is updated using
# inhouse ewaybill service.
if not self.condition_for_update_ewbn_das(source=src) and self.status == DispatchStatus.DISPATCHED and not pkg.is_hyperlocal_shipment():
raise AppException(message="Cannot add package {wbn} in Dispatched dispatch {dispatch_id}\
as package requires ewaybill vehicle update. Add package to a new WIP dispatch.".format(
wbn=pkg.waybill, dispatch_id=self.id))
# Key 'sync' is used to tell whether the API request should be sync call. If sync is False, need to
# mention callback URL. Ewaybill service mentioned that keeping sync=True does not gurantee
# ewaybill is updated. So using sync=False.
e_waybill_service_payload = {
'awb': pkg.waybill,
'data': payload,
'sync': False,
'src': 'LM',
'callback_url': settings.LM_BACKEND_URL + '/packages/ewbn_vhu_callback/' + str(pkg.waybill) + '/'
}
ewaybill_update_response = EwaybillService.update_vehicle(e_waybill_service_payload, raise_exception=True, center_code = pkg.center_code)
return ewaybill_update_response
return NoneEditor is loading...