Пример #1
0
        public CCWData GetCCW(string regi, string plate, string vin, string userId, string guid, string directory)
        {
            // check we have the right headers.
            if (string.IsNullOrEmpty(userId) || string.IsNullOrEmpty(guid) || string.IsNullOrEmpty(directory))
            {
                return(null);
            }

            var batchUser = _configuration.GetValue <string>("CCW_USER_ID");
            var logPrefix = batchUser == userId ? "[Hangfire]" : "";

            // Check for the following data:
            // 1. registration
            // 2. plate
            // 3. decal

            VehicleDescription vehicle = null;

            if (regi != null)
            {
                // format the regi.
                try
                {
                    int registration = int.Parse(regi);
                    // zero padded, 8 digits
                    regi = registration.ToString("D8");
                }
                catch (Exception)
                {
                    _logger.LogInformation($"{logPrefix} Exception occured parsing registration number {regi}.");
                }

                vehicle = _ccwService.GetBCVehicleForRegistrationNumber(regi, userId, guid, directory);
            }

            if (vehicle == null && plate != null) // check the plate.
            {
                vehicle = _ccwService.GetBCVehicleForLicensePlateNumber(plate, userId, guid, directory);
            }

            if (vehicle == null && vin != null) // check the vin.
            {
                vehicle = _ccwService.GetBCVehicleForSerialNumber(vin, userId, guid, directory);
            }

            if (vehicle == null)
            {
                return(null);
            }

            string icbcRegistrationNumber = vehicle.registrationNumber;

            CCWData ccwdata = null;

            if (_context.CCWDatas.Any(x => x.ICBCRegistrationNumber == icbcRegistrationNumber))
            {
                ccwdata = _context.CCWDatas.First(x => x.ICBCRegistrationNumber == icbcRegistrationNumber);
                _logger.LogInformation($"{logPrefix} Found CCW record for Registration # " + ccwdata.ICBCRegistrationNumber);
            }
            else
            {
                _logger.LogInformation($"{logPrefix} Creating new CCW record");
                ccwdata = new CCWData();
            }

            // update the ccw record.
            ccwdata.ICBCBody               = vehicle.bodyCode;
            ccwdata.ICBCColour             = vehicle.colour;
            ccwdata.ICBCCVIPDecal          = vehicle.inspectionDecalNumber;
            ccwdata.ICBCCVIPExpiry         = vehicle.inspectionExpiryDate;
            ccwdata.ICBCFleetUnitNo        = SanitizeInt(vehicle.fleetUnitNumber);
            ccwdata.ICBCFuel               = vehicle.fuelTypeDescription;
            ccwdata.ICBCGrossVehicleWeight = SanitizeInt(vehicle.grossVehicleWeight);
            ccwdata.ICBCMake               = vehicle.make;
            ccwdata.ICBCModel              = vehicle.model;
            ccwdata.ICBCGrossVehicleWeight = SanitizeInt(vehicle.grossVehicleWeight);
            ccwdata.ICBCModelYear          = SanitizeInt(vehicle.modelYear);
            ccwdata.ICBCNetWt              = SanitizeInt(vehicle.netWeight);
            ccwdata.ICBCNotesAndOrders     = vehicle.cvipDataFromLastInspection;
            ccwdata.ICBCOrderedOn          = vehicle.firstOpenOrderDate;
            ccwdata.ICBCRateClass          = vehicle.rateClass;
            ccwdata.ICBCRebuiltStatus      = vehicle.statusCode;
            ccwdata.ICBCRegistrationNumber = vehicle.registrationNumber;
            ccwdata.ICBCRegOwnerAddr1      = vehicle.owner.mailingAddress1;
            ccwdata.ICBCRegOwnerAddr2      = vehicle.owner.mailingAddress2;
            ccwdata.ICBCRegOwnerCity       = vehicle.owner.mailingAddress3;
            ccwdata.ICBCRegOwnerName       = vehicle.owner.name1;
            ccwdata.ICBCRegOwnerPODL       = vehicle.principalOperatorDlNum;
            ccwdata.ICBCRegOwnerPostalCode = vehicle.owner.postalCode;
            ccwdata.ICBCRegOwnerProv       = vehicle.owner.mailingAddress4;
            ccwdata.ICBCRegOwnerRODL       = vehicle.owner.driverLicenseNumber;
            ccwdata.ICBCSeatingCapacity    = SanitizeInt(vehicle.seatingCapacity);
            ccwdata.ICBCVehicleType        = vehicle.vehicleType + " - " + vehicle.vehicleTypeDescription;

            ccwdata.ICBCVehicleIdentificationNumber = vehicle.serialNumber;

            ccwdata.NSCPlateDecal          = vehicle.decalNumber;
            ccwdata.NSCPolicyEffectiveDate = vehicle.policyStartDate;
            ccwdata.NSCPolicyExpiryDate    = vehicle.policyExpiryDate;
            ccwdata.NSCPolicyStatus        = vehicle.policyStatus + " - " + vehicle.policyStatusDescription;

            // policyAquiredCurrentStatusDate is the preferred field, however it is often null.
            if (vehicle.policyAcquiredCurrentStatusDate != null)
            {
                ccwdata.NSCPolicyStatusDate = vehicle.policyAcquiredCurrentStatusDate;
            }
            else if (vehicle.policyTerminationDate != null)
            {
                ccwdata.NSCPolicyStatusDate = vehicle.policyTerminationDate;
            }
            else if (vehicle.policyReplacedOnDate != null)
            {
                ccwdata.NSCPolicyStatusDate = vehicle.policyReplacedOnDate;
            }
            else if (vehicle.policyStartDate != null)
            {
                ccwdata.NSCPolicyStatusDate = vehicle.policyStartDate;
            }

            if (vehicle.owner != null)
            {
                ccwdata.ICBCRegOwnerRODL = vehicle.owner.driverLicenseNumber;
            }

            ccwdata.ICBCLicencePlateNumber = vehicle.policyNumber;
            // these fields are the same.
            ccwdata.NSCPolicyNumber = vehicle.policyNumber;
            ccwdata.NSCClientNum    = vehicle.nscNumber;

            ccwdata.DateFetched = DateTime.UtcNow;

            // get the nsc client organization data.

            bool foundNSCData = false;

            if (!string.IsNullOrEmpty(ccwdata.NSCPolicyNumber))
            {
                string organizationNameCode = "LE";

                ClientOrganization clientOrganization = _ccwService.GetCurrentClientOrganization(ccwdata.NSCClientNum, organizationNameCode, userId, guid, directory);

                if (clientOrganization != null)
                {
                    foundNSCData = true;
                    ccwdata.NSCCarrierConditions   = clientOrganization.nscInformation.carrierStatus;
                    ccwdata.NSCCarrierName         = clientOrganization.displayName;
                    ccwdata.NSCCarrierSafetyRating = clientOrganization.nscInformation.safetyRating;
                }

                // now try the individual service if there was no match.
                if (foundNSCData == false)
                {
                    ClientIndividual clientIndividual = _ccwService.GetCurrentClientIndividual(ccwdata.NSCClientNum, organizationNameCode, userId, guid, directory);

                    if (clientIndividual != null)
                    {
                        foundNSCData = true;
                        ccwdata.NSCCarrierConditions   = clientIndividual.nscInformation.carrierStatus;
                        ccwdata.NSCCarrierName         = clientIndividual.displayName;
                        ccwdata.NSCCarrierSafetyRating = clientIndividual.nscInformation.safetyRating;
                    }
                }
            }

            if (ccwdata.Id > 0)
            {
                var bus = _context.SchoolBuss.FirstOrDefault(x => x.CCWDataId == ccwdata.Id);

                var changes = _context.GetChanges(ccwdata, "DateFetched");

                if (bus != null && changes.Count > 0)
                {
                    var ccwNotification = (new CCWNotification
                    {
                        HasBeenViewed = false,
                    });

                    bus.CCWNotifications.Add(ccwNotification);

                    foreach (var change in changes)
                    {
                        ccwNotification.CCWNotificationDetails.Add(new CCWNotificationDetail
                        {
                            ColName        = change.ColName,
                            ColDescription = change.ColDescription,
                            ValueFrom      = change.ValueFrom,
                            ValueTo        = change.ValueTo
                        });
                    }
                }
            }
            else
            {
                _context.Add(ccwdata);
            }

            _logger.LogInformation($"{logPrefix} CCW data has been added/updated.");
            _context.SaveChanges();

            return(ccwdata);
        }