Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[$250] Per diem - Time summary on the confirmation page is incorrect #55449

Open
5 of 8 tasks
lanitochka17 opened this issue Jan 18, 2025 · 18 comments
Open
5 of 8 tasks
Assignees
Labels
Bug Something is broken. Auto assigns a BugZero manager. External Added to denote the issue can be worked on by a contributor Reviewing Has a PR in review Weekly KSv2

Comments

@lanitochka17
Copy link

lanitochka17 commented Jan 18, 2025

If you haven’t already, check out our contributing guidelines for onboarding and email [email protected] to request to join our Slack channel!


Version Number: 9.0.87-0
Reproducible in staging?: Y
Reproducible in production?: Y
If this was caught on HybridApp, is this reproducible on New Expensify Standalone?: N/A
If this was caught during regression testing, add the test name, ID and link from TestRail: N/A
Email or phone of affected tester (no customers): [email protected]
Issue reported by: Applause - Internal Team

Action Performed:

Precondition:

  • Workspace has per diem rates
  1. Go to staging.new.expensify.com
  2. Go to workspace chat
  3. Click + > Submit expense > Per diem
  4. Select destination > Next
  5. On time selection page, select 8:00am as Start time and 10:00am as End time, on the same Start and End day
  6. Fill in the rest of the steps and reach the confirmation page
  7. Go to staging.expensify.com
  8. Start per diem expense flow and configure the same date and time on OD as Step 5
  9. Compare the time summary on the confirmation page on ND and OD

Expected Result:

The time summary should be correct on the confirmation page
It should show "First Day: 2.00 hours" because the configured duration is only 2 hours (the same on OD)

Actual Result:

The time summary is wrong on the confirmation page
It shows "First day: 16.00 hours", "Trip: 1 full day", "Last day: 10.00 hours" when the configured duration is only 2 hours
On OD, it shows the correct summary which is "First Day: 2.00 hours"

Workaround:

Unknown

Platforms:

Which of our officially supported platforms is this issue occurring on?

  • Android: Standalone
  • Android: HybridApp
  • Android: mWeb Chrome
  • iOS: Standalone
  • iOS: HybridApp
  • iOS: mWeb Safari
  • MacOS: Chrome / Safari
  • MacOS: Desktop

Screenshots/Videos

Add any screenshot/video evidence
Bug6717376_1737187618758.bandicam_2025-01-18_15-35-42-986.mp4

View all open jobs on GitHub

Upwork Automation - Do Not Edit
  • Upwork Job URL: https://www.upwork.com/jobs/~021881825734850372345
  • Upwork Job ID: 1881825734850372345
  • Last Price Increase: 2025-01-21
Issue OwnerCurrent Issue Owner: @lakchote
@lanitochka17 lanitochka17 added Bug Something is broken. Auto assigns a BugZero manager. Daily KSv2 labels Jan 18, 2025
Copy link

melvin-bot bot commented Jan 18, 2025

Triggered auto assignment to @RachCHopkins (Bug), see https://stackoverflow.com/c/expensify/questions/14418 for more details. Please add this bug to a GH project, as outlined in the SO.

@Shahidullah-Muffakir
Copy link
Contributor

Shahidullah-Muffakir commented Jan 18, 2025

Proposal

Please re-state the problem that we are trying to solve in this issue.

When a user creates a per diem expense for the same day (like 8:00 AM to 10:00 AM), the app shows wrong time calculations. Instead of showing 2 hours, it shows "First day: 16.00 hours", "Trip: 1 full day", and "Last day: 10.00 hours".

What is the root cause of that problem?

The time calculation code doesn't check if the start and end dates are on the same day. It always tries to split the time into first day, trip days, and last day, even when everything happens on one day. This causes wrong calculations for same-day expenses.

What changes do you think we should make in order to solve the problem?

Add a check at the start of the calculation to see if start and end dates are on the same day
For same-day expenses:
Calculate hours directly between start and end times
Set trip days to 0
Don't show last day hours

function getTimeDifferenceIntervals(transaction: OnyxEntry<Transaction>) {

modify it as:

function getTimeDifferenceIntervals(transaction: OnyxEntry<Transaction>) {
    const customUnitRateDate = transaction?.comment?.customUnit?.attributes?.dates ?? {start: '', end: ''};
    const startDate = new Date(customUnitRateDate.start);
    const endDate = new Date(customUnitRateDate.end);

+  // Check if start and end dates are on the same day
+    const isSameDay = startOfDay(startDate).getTime() === startOfDay(endDate).getTime();
+
+   if (isSameDay) {
+        // If same day, just calculate the direct hour difference
+        const hourDiff = (differenceInMinutes(endDate, startDate) / 60).toFixed(2);
+        return {
+           firstDay: hourDiff,
+            tripDays: 0,
+           lastDay: undefined,
+        };
+    }

    // Original logic for multi-day calculations
    const firstDayDiff = differenceInMinutes(startOfDay(addDays(startDate, 1)), startDate);
    const tripDaysDiff = differenceInDays(startOfDay(endDate), startOfDay(addDays(startDate, 1)));
    const lastDayDiff = differenceInMinutes(endDate, startOfDay(endDate));

    return {
        firstDay: firstDayDiff === 1440 ? undefined : (firstDayDiff / 60).toFixed(2),
        tripDays: firstDayDiff === 1440 ? tripDaysDiff + 1 : tripDaysDiff,
        lastDay: lastDayDiff === 0 ? undefined : (lastDayDiff / 60).toFixed(2),
    };
}

What specific scenarios should we cover in automated tests to prevent reintroducing this issue in the future?

NA

What alternative solutions did you explore? (Optional)

NA

Screen.Recording.2025-01-18.at.8.18.54.PM.mov

@huult
Copy link
Contributor

huult commented Jan 20, 2025

Proposal

Please re-state the problem that we are trying to solve in this issue.

After turning on the Make or track payments,the BA flow opens on the page where it was left

What is the root cause of that problem?

The function assumes startDate and endDate always span multiple days.
For same-day transactions, it unnecessarily calculates firstDay, tripDays, and lastDay, leading to incorrect results.
It doesn’t check if startDate and endDate fall on the same day before splitting time intervals.

function getTimeDifferenceIntervals(transaction: OnyxEntry<Transaction>) {
const customUnitRateDate = transaction?.comment?.customUnit?.attributes?.dates ?? {start: '', end: ''};
const startDate = new Date(customUnitRateDate.start);
const endDate = new Date(customUnitRateDate.end);
const firstDayDiff = differenceInMinutes(startOfDay(addDays(startDate, 1)), startDate);
const tripDaysDiff = differenceInDays(startOfDay(endDate), startOfDay(addDays(startDate, 1)));
const lastDayDiff = differenceInMinutes(endDate, startOfDay(endDate));
return {
firstDay: firstDayDiff === 1440 ? undefined : firstDayDiff / 60,
tripDays: firstDayDiff === 1440 ? tripDaysDiff + 1 : tripDaysDiff,
lastDay: lastDayDiff === 0 ? undefined : lastDayDiff / 60,
};
}

Example:

Given startDate = '2025-01-16T08:00:00' and endDate = '2025-01-16T18:00:00'

    const firstDayDiff = differenceInMinutes(startOfDay(addDays(startDate, 1)), startDate); // Incorrectly calculates next day
    const tripDaysDiff = differenceInDays(startOfDay(endDate), startOfDay(addDays(startDate, 1))); // Returns negative or invalid
    const lastDayDiff = differenceInMinutes(endDate, startOfDay(endDate)); // Processes last day even for same-day trips

What changes do you think we should make in order to solve the problem?

To fix this issue, we need to check if startDate and endDate are on the same day at the start of the function. If they are, directly calculate the total time difference as firstDay and set tripDays and lastDay to undefined.

    import {..., isSameDay, ...} from 'date-fns';
function getTimeDifferenceIntervals(transaction: OnyxEntry<Transaction>) {
    const customUnitRateDate = transaction?.comment?.customUnit?.attributes?.dates ?? {start: '', end: ''};
    const startDate = new Date(customUnitRateDate.start);
    const endDate = new Date(customUnitRateDate.end);

    // Check if startDate and endDate are on the same day
    if (isSameDay(startDate, endDate)) { // add this line
        const totalMinutes = differenceInMinutes(endDate, startDate); // add this line
        return { // add this line
            firstDay: totalMinutes / 60, // add this line
            tripDays: undefined, // add this line
            lastDay: undefined, // add this line
        }; // add this line
    } // add this line

    // If not on the same day, calculate intervals
    const firstDayDiff = differenceInMinutes(startOfDay(addDays(startDate, 1)), startDate);
    const tripDaysDiff = differenceInDays(startOfDay(endDate), startOfDay(addDays(startDate, 1)));
    const lastDayDiff = differenceInMinutes(endDate, startOfDay(endDate));

    return {
        firstDay: firstDayDiff === 1440 ? undefined : firstDayDiff / 60,
        tripDays: firstDayDiff === 1440 ? tripDaysDiff + 1 : tripDaysDiff,
        lastDay: lastDayDiff === 0 ? undefined : lastDayDiff / 60,
    };
}
POC

What specific scenarios should we cover in automated tests to prevent reintroducing this issue in the future?

Create unit test for case same day.

What alternative solutions did you explore? (Optional)

Reminder: Please use plain English, be brief and avoid jargon. Feel free to use images, charts or pseudo-code if necessary. Do not post large multi-line diffs or write walls of text. Do not create PRs unless you have been hired for this job.

@melvin-bot melvin-bot bot added the Overdue label Jan 21, 2025
@RachCHopkins
Copy link
Contributor

RachCHopkins commented Jan 21, 2025

I don't appear to have Per Diems. I will have to assume they exist because we have two people above who can repro this.

Update: Per Diem is behind a beta.

@melvin-bot melvin-bot bot removed the Overdue label Jan 21, 2025
@RachCHopkins RachCHopkins added the External Added to denote the issue can be worked on by a contributor label Jan 21, 2025
@melvin-bot melvin-bot bot changed the title Per diem - Time summary on the confirmation page is incorrect [$250] Per diem - Time summary on the confirmation page is incorrect Jan 21, 2025
Copy link

melvin-bot bot commented Jan 21, 2025

Job added to Upwork: https://www.upwork.com/jobs/~021881825734850372345

@melvin-bot melvin-bot bot added the Help Wanted Apply this label when an issue is open to proposals by contributors label Jan 21, 2025
Copy link

melvin-bot bot commented Jan 21, 2025

Triggered auto assignment to Contributor-plus team member for initial proposal review - @c3024 (External)

@Shahidullah-Muffakir
Copy link
Contributor

@c3024, when you get a chance, could you please review the proposals? Thank you

@melvin-bot melvin-bot bot added the Overdue label Jan 24, 2025
@c3024
Copy link
Contributor

c3024 commented Jan 24, 2025

Thanks for the bump. Checking!

@melvin-bot melvin-bot bot removed the Overdue label Jan 24, 2025
@c3024
Copy link
Contributor

c3024 commented Jan 25, 2025

@Shahidullah-Muffakir 's RCA and suggested fix in his proposal here looks good to me!

🎀 👀 🎀 C+ reviewed!

Copy link

melvin-bot bot commented Jan 25, 2025

Triggered auto assignment to @lakchote, see https://stackoverflow.com/c/expensify/questions/7972 for more details.

Copy link

melvin-bot bot commented Jan 28, 2025

@lakchote, @RachCHopkins, @c3024 Whoops! This issue is 2 days overdue. Let's get this updated quick!

@melvin-bot melvin-bot bot added the Overdue label Jan 28, 2025
@Shahidullah-Muffakir
Copy link
Contributor

@lakchote a little bump on this:

@Shahidullah-Muffakir 's RCA and suggested fix in his proposal here looks good to me!

🎀 👀 🎀 C+ reviewed!

Thanks.

Copy link

melvin-bot bot commented Jan 30, 2025

@lakchote, @RachCHopkins, @c3024 Eep! 4 days overdue now. Issues have feelings too...

Copy link

melvin-bot bot commented Feb 1, 2025

@lakchote @RachCHopkins @c3024 this issue was created 2 weeks ago. Are we close to approving a proposal? If not, what's blocking us from getting this issue assigned? Don't hesitate to create a thread in #expensify-open-source to align faster in real time. Thanks!

Copy link

melvin-bot bot commented Feb 3, 2025

@lakchote, @RachCHopkins, @c3024 8 days overdue is a lot. Should this be a Weekly issue? If so, feel free to change it!

@lakchote
Copy link
Contributor

lakchote commented Feb 3, 2025

@Shahidullah-Muffakir's proposal LGTM.

@melvin-bot melvin-bot bot removed the Overdue label Feb 3, 2025
@melvin-bot melvin-bot bot removed the Help Wanted Apply this label when an issue is open to proposals by contributors label Feb 3, 2025
Copy link

melvin-bot bot commented Feb 3, 2025

📣 @Shahidullah-Muffakir You have been assigned to this job!
Please apply to the Upwork job and leave a comment on the Github issue letting us know when we can expect a PR to be ready for review 🧑‍💻
Once you apply to this job, your Upwork ID will be stored and you will be automatically hired for future jobs!
Keep in mind: Code of Conduct | Contributing 📖

@Shahidullah-Muffakir
Copy link
Contributor

PR is ready.
cc: @c3024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something is broken. Auto assigns a BugZero manager. External Added to denote the issue can be worked on by a contributor Reviewing Has a PR in review Weekly KSv2
Projects
None yet
Development

No branches or pull requests

6 participants