let updatedOrderDataObj = Object.assign({}, orderDataObj); const distanceBuffer = Number(((orderDataObj.distance * 10) / 100).toFixed(2)); const estimatedDistance = Number(orderDataObj.distance.toFixed(2)); const estimatedDistanceWithBuffer = Number((orderDataObj.distance + distanceBuffer).toFixed(2)); const distanceNum = Number(distance); console.log("orderDataObj.distance", orderDataObj.distance); console.log("distanceBuffer", distanceBuffer); console.log("estimatedDistanceWithBuffer", estimatedDistanceWithBuffer); console.log("distanceNum", distanceNum); if (estimatedDistanceWithBuffer < distanceNum) { const distanceNum: number = +distance; const categoryDataObj: any = await findCategory({ _id: updatedOrderDataObj.vehicleType, status: "active", type: "vehicle" }); const pricingListArr: any = await findPrices({ category: updatedOrderDataObj.vehicleType, status: "active" }); const sum = pricingListArr.reduce((result: any, item: any, currentIndex: number, pricingListArr: any) => { console.log("result", result); if (!result[item.category]) { result[item.category] = 0; } if (item.type == "range") { result[item.category] += item.price; } else { if (item.value < distanceNum) { let difference = distanceNum - item.value; if (pricingListArr[currentIndex + 1] && pricingListArr[currentIndex + 1].type == "single" && pricingListArr[currentIndex + 1].value) { let kmsDiff = pricingListArr[currentIndex + 1].value - item.value; console.log("kmsDiff", kmsDiff); difference = kmsDiff; } else if ( pricingListArr[currentIndex + 1] && pricingListArr[currentIndex + 1].type == "range" && pricingListArr[currentIndex + 1].startingFrom && pricingListArr[currentIndex + 1].endingTo ) { let kmsDiff = pricingListArr[currentIndex + 1].startingFrom - item.value; console.log("kmsDiffRange", kmsDiff); difference = kmsDiff; } console.log(difference, Math.ceil(difference)); let localKMPrice = Math.ceil(difference) * item.price; console.log("localKMPrice", localKMPrice); result[item.category] += localKMPrice; } else { console.log("NOT Required"); } //return result + item.price; } return result; }, []); console.log("sum", sum); // 10 const date = new Date(); const currentHour = date.getHours(); console.log("currentHour", currentHour); const timingDataObj = await findTiming({ status: "active", startingFrom: { $lte: currentHour }, endingTo: { $gt: currentHour }, }); let amountUpdatePercentage = 0; let amountUpdateMethod = "increase"; if (!timingDataObj) { } else { amountUpdatePercentage = timingDataObj.percentage; amountUpdateMethod = timingDataObj.method; } console.log("timingDataObj", timingDataObj); //console.log("categoriesListArr", categoriesListArr); // 10 var netAmount = 0; let objectKey = categoryDataObj._id.toString(); if (amountUpdateMethod == "decrease") { netAmount = sum[objectKey] - (sum[objectKey] * parseFloat(amountUpdatePercentage.toString())) / 100; } else { netAmount = sum[objectKey] + (sum[objectKey] * parseFloat(amountUpdatePercentage.toString())) / 100; } const billingDetails = { ...updatedOrderDataObj.billingDetails }; const estimatedBillingDetails = { ...updatedOrderDataObj.billingDetails }; const tripAmount = Number(netAmount.toFixed(2)); // const payableAmount = Number((tripAmount - estimatedBillingDetails.discountAmount - estimatedBillingDetails.creditsAmount).toFixed(2)); let grossAmount = Number((tripAmount - estimatedBillingDetails.discountAmount).toFixed(2)); const payableAmount = Number((tripAmount - estimatedBillingDetails.discountAmount).toFixed(2)); billingDetails.tripAmount = tripAmount; billingDetails.payableAmount = payableAmount; billingDetails.withoutTaxTotalAmount = payableAmount; billingDetails.withTaxTotalAmount = payableAmount; billingDetails.gstAmount = 0; billingDetails.cgstAmount = 0; billingDetails.sgstAmount = 0; billingDetails.igstAmount = 0; let gstOptionsQuery = { key: { $in: ["GST", "CGST", "SGST", "IGST"], }, }; const gstListArr = await findOptions(gstOptionsQuery); let gstPercentage = Number(gstListArr.find((o) => o.key === "GST")?.value); let sgstPercentage = Number(gstListArr.find((o) => o.key === "SGST")?.value); let cgstPercentage = Number(gstListArr.find((o) => o.key === "CGST")?.value); let igstPercentage = Number(gstListArr.find((o) => o.key === "IGST")?.value); if (gstPercentage != 0) { let gstAmount = Number((billingDetails.payableAmount * (gstPercentage / 100)).toFixed(3)); let sgstAmount = Number((billingDetails.payableAmount * (sgstPercentage / 100)).toFixed(3)); let cgstAmount = Number((billingDetails.payableAmount * (cgstPercentage / 100)).toFixed(3)); let igstAmount = Number((billingDetails.payableAmount * (igstPercentage / 100)).toFixed(3)); billingDetails.withTaxTotalAmount += gstAmount; billingDetails.gstAmount = gstAmount; billingDetails.sgstAmount = sgstAmount; billingDetails.cgstAmount = cgstAmount; billingDetails.igstAmount = igstAmount; // billingDetails.payableAmount = billingDetails.withTaxTotalAmount; } billingDetails.withTaxTotalAmount = Number(billingDetails.withTaxTotalAmount.toFixed(2)); billingDetails.payableAmount = Number((billingDetails.withTaxTotalAmount - estimatedBillingDetails.creditsAmount).toFixed(2)); try { let updateOrder: any = { billingDetails, estimatedBillingDetails, distance: distanceNum, estimatedDistance: updatedOrderDataObj.distance, pendingPayment: updatedOrderDataObj.pendingPayment + (billingDetails.payableAmount - estimatedBillingDetails.payableAmount), }; console.log(updateOrder, "updateOrder"); const updatedOrder = await findAndUpdateOrder({ _id: updatedOrderDataObj._id }, updateOrder, { new: true, }); updatedOrderDataObj = Object.assign(updatedOrderDataObj, updatedOrder); try { let singleNotificationInput = { clickAction: "trackOrder", type: "dropOrder", //iconType: "", relationType: "order", relationId: updatedOrderDataObj._id, user: userDataObj._id, fromDriver: driverId, unread: true, title: "Your fare may be incereased due to distance exceeding the required distance.", body: "Your fare may be incereased due to distance exceeding the required distance.", push: true, sms: false, email: false, }; const notificationDataObj = await createNotification(singleNotificationInput); const notifyObj = { title: singleNotificationInput.title, body: singleNotificationInput.body, clickAction: singleNotificationInput.clickAction, type: singleNotificationInput.type, relationType: singleNotificationInput.relationType, relationID: singleNotificationInput.relationId.toString(), notifyIds: [notificationDataObj._id], }; let firebaseToken = userDataObj.firebaseToken; common.sendPushNotification([firebaseToken], notifyObj); } catch (e: any) { logger.error(e, "PaymentOrderDistanceUserNotification: " + ReasonPhrases.INTERNAL_SERVER_ERROR); let responseBody = { status: false, message: "PaymentOrderDistanceUserNotification: " + ReasonPhrases.INTERNAL_SERVER_ERROR, data: {}, errors: e.errors, }; return common.response(res, responseBody, StatusCodes.INTERNAL_SERVER_ERROR); } } catch (error: any) { logger.error(error, "UpdatedFarePaymentInfoDistanceUpdateOrder: " + ReasonPhrases.INTERNAL_SERVER_ERROR); let responseBody = { status: false, message: "UpdatedFarePaymentInfoDistanceUpdateOrder: " + ReasonPhrases.INTERNAL_SERVER_ERROR, data: {}, errors: error.errors, }; return common.response(res, responseBody, StatusCodes.INTERNAL_SERVER_ERROR); } } else { if (estimatedDistance < distanceNum) { try { let updateOrder: any = { distance: distanceNum, estimatedDistance: updatedOrderDataObj.distance, }; console.log(updateOrder, "updateOrder"); const updatedOrder = await findAndUpdateOrder({ _id: updatedOrderDataObj._id }, updateOrder, { new: true, }); updatedOrderDataObj = Object.assign(updatedOrderDataObj, updatedOrder); } catch (error: any) { logger.error(error, "UpdatedPaymentInfoDistanceOnlyUpdateOrder: " + ReasonPhrases.INTERNAL_SERVER_ERROR); let responseBody = { status: false, message: "UpdatedPaymentInfoDistanceOnlyUpdateOrder: " + ReasonPhrases.INTERNAL_SERVER_ERROR, data: {}, errors: error.errors, }; return common.response(res, responseBody, StatusCodes.INTERNAL_SERVER_ERROR); } } } const locations = updatedOrderDataObj.locations; const locationsDataListArr: any = await findLocations({ _id: { $in: locations } }, 0, 0, [], { position: 1 }); // console.log("locationsDataListArr", locationsDataListArr); let totalDuration = 0; let totalDurationInTraffic = 0; if (locationsDataListArr.length > 0) { const totalDuration = locationsDataListArr.reduce((result: any, item: any, currentIndex: number, currentArr: any) => { // console.log("result", result); // console.log("item", item); if (item.position == 1) return result; result += item.duration.value; return result; }, 0); const totalDurationInTraffic = locationsDataListArr.reduce((result: any, item: any, currentIndex: number, currentArr: any) => { // console.log("result", result); // console.log("item", item); if (item.position == 1) return result; result += item.durationInTraffic.value; return result; }, 0); const firstLocationDataObj = locationsDataListArr.find((o: any) => o.type === "pickup"); console.log("totalDuration", totalDuration); console.log("totalDurationInTraffic", totalDurationInTraffic); const durationBuffer = Number(((totalDurationInTraffic * 10) / 100).toFixed(2)); const estimatedDurationWithBufferInSecs = Math.floor(Number((totalDurationInTraffic + durationBuffer).toFixed(2))); let startedAtMom = moment(firstLocationDataObj.reachedAt); let currentAtMom = moment(); let actualDurationInSecs = currentAtMom.diff(startedAtMom, "seconds"); console.log("startedAtMom", startedAtMom); console.log("currentAtMom", currentAtMom); console.log("actualDurationInSecs", actualDurationInSecs); console.log("totalDurationInTraffic", totalDurationInTraffic); console.log("durationBuffer", durationBuffer); console.log("estimatedDurationWithBufferInSecs", estimatedDurationWithBufferInSecs); const estimatedDuration = moment.duration(estimatedDurationWithBufferInSecs, "seconds"); const estimatedDurationInMins = Math.floor(estimatedDuration.asMinutes()); // 25 console.log("estimatedDurationInMins", estimatedDurationInMins); const actualDuration = moment.duration(actualDurationInSecs, "seconds"); const actualDurationInMins = Math.floor(actualDuration.asMinutes()); // 25 console.log("actualDurationInMins", actualDurationInMins); const diffInMins = actualDurationInMins - estimatedDurationInMins; console.log("diffInMins", diffInMins); // const h = actualDuration.hours(); // 1 // const m = actualDuration.minutes(); // 20 // const s = actualDuration.seconds(); // 25 // console.log("duration", duration); diffInMins; if (diffInMins > 0) { const durationListArr: any = await findDurations({ category: updatedOrderDataObj.vehicleType, status: "active" }); const additionalTripAmount: number = durationListArr.reduce((result: any, item: any, currentIndex: number, pricingListArr: any) => { // console.log("result", result); if (item.type == "range" && item.endingTo && item.endingTo >= diffInMins && item.startingFrom <= diffInMins) { // console.log("IF IN"); result = item.price * diffInMins; } else if (item.type == "single" && item.value && item.value < diffInMins) { // console.log("ELSE IF IN"); result = item.price * diffInMins; } else { // console.log("ELSE IN"); result = result; // result = item.price * diffInMins; } return result; }, 0); console.log("additionalTripAmount", additionalTripAmount); // 10 let netAmount = additionalTripAmount; netAmount += updatedOrderDataObj.billingDetails.tripAmount; const billingDetails = { ...updatedOrderDataObj.billingDetails }; // const estimatedBillingDetails = { ...updatedOrderDataObj.billingDetails }; const estimatedBillingDetails = !updatedOrderDataObj.hasOwnProperty("estimatedBillingDetails") ? { ...updatedOrderDataObj.billingDetails } : { ...updatedOrderDataObj.estimatedBillingDetails }; const estimatedDistanceBillingDetails = { ...updatedOrderDataObj.billingDetails }; const tripAmount = Number(netAmount.toFixed(2)); console.log(tripAmount); // const payableAmount = Number((tripAmount - estimatedBillingDetails.discountAmount - estimatedBillingDetails.creditsAmount).toFixed(2)); let grossAmount = Number((tripAmount - estimatedBillingDetails.discountAmount).toFixed(2)); const payableAmount = Number((tripAmount - estimatedBillingDetails.discountAmount).toFixed(2)); billingDetails.tripAmount = tripAmount; billingDetails.payableAmount = payableAmount; billingDetails.withoutTaxTotalAmount = payableAmount; billingDetails.withTaxTotalAmount = payableAmount; billingDetails.gstAmount = 0; billingDetails.cgstAmount = 0; billingDetails.sgstAmount = 0; billingDetails.igstAmount = 0; let gstOptionsQuery = { key: { $in: ["GST", "CGST", "SGST", "IGST"], }, }; const gstListArr = await findOptions(gstOptionsQuery); let gstPercentage = Number(gstListArr.find((o) => o.key === "GST")?.value); let sgstPercentage = Number(gstListArr.find((o) => o.key === "SGST")?.value); let cgstPercentage = Number(gstListArr.find((o) => o.key === "CGST")?.value); let igstPercentage = Number(gstListArr.find((o) => o.key === "IGST")?.value); if (gstPercentage != 0) { let gstAmount = Number((billingDetails.payableAmount * (gstPercentage / 100)).toFixed(3)); let sgstAmount = Number((billingDetails.payableAmount * (sgstPercentage / 100)).toFixed(3)); let cgstAmount = Number((billingDetails.payableAmount * (cgstPercentage / 100)).toFixed(3)); let igstAmount = Number((billingDetails.payableAmount * (igstPercentage / 100)).toFixed(3)); billingDetails.withTaxTotalAmount += gstAmount; billingDetails.gstAmount = gstAmount; billingDetails.sgstAmount = sgstAmount; billingDetails.cgstAmount = cgstAmount; billingDetails.igstAmount = igstAmount; // billingDetails.payableAmount = billingDetails.withTaxTotalAmount; } billingDetails.withTaxTotalAmount = Number(billingDetails.withTaxTotalAmount.toFixed(2)); billingDetails.payableAmount = Number((billingDetails.withTaxTotalAmount - estimatedBillingDetails.creditsAmount).toFixed(2)); console.warn("billingDetails", billingDetails); try { let updateOrder: any = { billingDetails, estimatedBillingDetails, duration: actualDurationInSecs, estimatedDuration: updatedOrderDataObj.duration, pendingPayment: updatedOrderDataObj.pendingPayment + (billingDetails.payableAmount - estimatedBillingDetails.payableAmount), }; if (estimatedDistanceWithBuffer < distanceNum) { updateOrder.estimatedDistanceBillingDetails = estimatedDistanceBillingDetails; } console.log(updateOrder, "updateOrder"); const updatedOrder = await findAndUpdateOrder({ _id: updatedOrderDataObj._id }, updateOrder, { new: true, }); updatedOrderDataObj = Object.assign(updatedOrderDataObj, updatedOrder); try { let singleNotificationInput = { clickAction: "trackOrder", type: "dropOrder", //iconType: "", relationType: "order", relationId: updatedOrderDataObj._id, user: userDataObj._id, fromDriver: driverId, unread: true, title: "Your fare may be incereased due to time exceeding the required time.", body: "Your fare may be incereased due to time exceeding the required time.", push: true, sms: false, email: false, }; const notificationDataObj = await createNotification(singleNotificationInput); const notifyObj = { title: singleNotificationInput.title, body: singleNotificationInput.body, clickAction: singleNotificationInput.clickAction, type: singleNotificationInput.type, relationType: singleNotificationInput.relationType, relationID: singleNotificationInput.relationId.toString(), notifyIds: [notificationDataObj._id], }; let firebaseToken = userDataObj.firebaseToken; common.sendPushNotification([firebaseToken], notifyObj); } catch (e: any) { logger.error(e, "PaymentOrderTimeDiffUserNotification: " + ReasonPhrases.INTERNAL_SERVER_ERROR); let responseBody = { status: false, message: "PaymentOrderTimeDiffUserNotification: " + ReasonPhrases.INTERNAL_SERVER_ERROR, data: {}, errors: e.errors, }; return common.response(res, responseBody, StatusCodes.INTERNAL_SERVER_ERROR); } } catch (error: any) { logger.error(error, "UpdatedFarePaymentInfoTimeUpdateOrder: " + ReasonPhrases.INTERNAL_SERVER_ERROR); let responseBody = { status: false, message: "UpdatedFarePaymentInfoUpdateOrder: " + ReasonPhrases.INTERNAL_SERVER_ERROR, data: {}, errors: error.errors, }; return common.response(res, responseBody, StatusCodes.INTERNAL_SERVER_ERROR); } } else { if (totalDurationInTraffic < actualDurationInSecs) { try { let updateOrder: any = { duration: actualDurationInSecs, estimatedDuration: updatedOrderDataObj.duration, }; console.log(updateOrder, "updateOrder"); const updatedOrder = await findAndUpdateOrder({ _id: updatedOrderDataObj._id }, updateOrder, { new: true, }); updatedOrderDataObj = Object.assign(updatedOrderDataObj, updatedOrder); } catch (error: any) { logger.error(error, "UpdatedPaymentInfoDurationOnlyUpdateOrder: " + ReasonPhrases.INTERNAL_SERVER_ERROR); let responseBody = { status: false, message: "UpdatedPaymentInfoDurationOnlyUpdateOrder: " + ReasonPhrases.INTERNAL_SERVER_ERROR, data: {}, errors: error.errors, }; return common.response(res, responseBody, StatusCodes.INTERNAL_SERVER_ERROR); } } } }