-
Notifications
You must be signed in to change notification settings - Fork 3.9k
feat: Use formula for the optimistic expense report #37160
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
Merged
thienlnam
merged 3 commits into
Expensify:main
from
paultsimura:fix/36267-report-name-formula
Feb 28, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import {format} from 'date-fns'; | ||
| import ExpensiMark from 'expensify-common/lib/ExpensiMark'; | ||
| import Str from 'expensify-common/lib/str'; | ||
| import {isEmpty} from 'lodash'; | ||
|
|
@@ -1977,6 +1978,13 @@ function getFormulaTypeReportField(reportFields: PolicyReportFields) { | |
| return Object.values(reportFields).find((field) => field.type === 'formula'); | ||
| } | ||
|
|
||
| /** | ||
| * Given a set of report fields, return the field that refers to title | ||
| */ | ||
| function getTitleReportField(reportFields: PolicyReportFields) { | ||
| return Object.values(reportFields).find((field) => isReportFieldOfTypeTitle(field)); | ||
| } | ||
|
|
||
| /** | ||
| * Get the report fields attached to the policy given policyID | ||
| */ | ||
|
|
@@ -2884,6 +2892,38 @@ function buildOptimisticIOUReport(payeeAccountID: number, payerAccountID: number | |
| }; | ||
| } | ||
|
|
||
| function getHumanReadableStatus(statusNum: number): string { | ||
| const status = Object.keys(CONST.REPORT.STATUS_NUM).find((key) => CONST.REPORT.STATUS_NUM[key as keyof typeof CONST.REPORT.STATUS_NUM] === statusNum); | ||
| return status ? `${status.charAt(0)}${status.slice(1).toLowerCase()}` : ''; | ||
| } | ||
|
|
||
| /** | ||
| * Populates the report field formula with the values from the report and policy. | ||
| * Currently, this only supports optimistic expense reports. | ||
| * Each formula field is either replaced with a value, or removed. | ||
| * If after all replacements the formula is empty, the original formula is returned. | ||
| * See {@link https://help.expensify.com/articles/expensify-classic/insights-and-custom-reporting/Custom-Templates} | ||
| */ | ||
| function populateOptimisticReportFormula(formula: string, report: OptimisticExpenseReport, policy: Policy | EmptyObject): string { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a couple more we can handle here optimistically
|
||
| const createdDate = report.lastVisibleActionCreated ? new Date(report.lastVisibleActionCreated) : undefined; | ||
| const result = formula | ||
| .replaceAll('{report:id}', report.reportID) | ||
| // We don't translate because the server response is always in English | ||
| .replaceAll('{report:type}', 'Expense Report') | ||
| .replaceAll('{report:startdate}', createdDate ? format(createdDate, CONST.DATE.FNS_FORMAT_STRING) : '') | ||
| .replaceAll('{report:total}', report.total?.toString() ?? '') | ||
| .replaceAll('{report:currency}', report.currency ?? '') | ||
| .replaceAll('{report:policyname}', policy.name ?? '') | ||
| .replaceAll('{report:created}', createdDate ? format(createdDate, CONST.DATE.FNS_DATE_TIME_FORMAT_STRING) : '') | ||
| .replaceAll('{report:created:yyyy-MM-dd}', createdDate ? format(createdDate, CONST.DATE.FNS_FORMAT_STRING) : '') | ||
| .replaceAll('{report:status}', report.statusNum !== undefined ? getHumanReadableStatus(report.statusNum) : '') | ||
| .replaceAll('{user:email}', currentUserEmail ?? '') | ||
| .replaceAll('{user:email|frontPart}', currentUserEmail ? currentUserEmail.split('@')[0] : '') | ||
| .replaceAll(/\{report:(.+)}/g, ''); | ||
|
|
||
| return result.trim().length ? result : formula; | ||
| } | ||
|
|
||
| /** | ||
| * Builds an optimistic Expense report with a randomly generated reportID | ||
| * | ||
|
|
@@ -2893,7 +2933,6 @@ function buildOptimisticIOUReport(payeeAccountID: number, payerAccountID: number | |
| * @param total - Amount in cents | ||
| * @param currency | ||
| */ | ||
|
|
||
| function buildOptimisticExpenseReport(chatReportID: string, policyID: string, payeeAccountID: number, total: number, currency: string): OptimisticExpenseReport { | ||
| // The amount for Expense reports are stored as negative value in the database | ||
| const storedTotal = total * -1; | ||
|
|
@@ -2914,7 +2953,6 @@ function buildOptimisticExpenseReport(chatReportID: string, policyID: string, pa | |
| type: CONST.REPORT.TYPE.EXPENSE, | ||
| ownerAccountID: payeeAccountID, | ||
| currency, | ||
|
|
||
| // We don't translate reportName because the server response is always in English | ||
| reportName: `${policyName} owes ${formattedTotal}`, | ||
| stateNum, | ||
|
|
@@ -2930,6 +2968,11 @@ function buildOptimisticExpenseReport(chatReportID: string, policyID: string, pa | |
| expenseReport.managerID = policy.submitsTo; | ||
| } | ||
|
|
||
| const titleReportField = getTitleReportField(getReportFieldsByPolicyID(policyID) ?? {}); | ||
| if (!!titleReportField && reportFieldsEnabled(expenseReport)) { | ||
| expenseReport.reportName = populateOptimisticReportFormula(titleReportField.defaultValue, expenseReport, policy); | ||
| } | ||
|
|
||
| return expenseReport; | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.