diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index ed43569c360a..4a6e191e5fc9 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -19,6 +19,7 @@ import * as ReportActionsUtils from '@libs/ReportActionsUtils'; import * as ReportUtils from '@libs/ReportUtils'; import * as TransactionUtils from '@libs/TransactionUtils'; import * as UserUtils from '@libs/UserUtils'; +import ViolationsUtils from '@libs/ViolationsUtils'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; @@ -126,6 +127,9 @@ function buildOnyxDataForMoneyRequest( optimisticPolicyRecentlyUsedTags, isNewChatReport, isNewIOUReport, + policy, + policyTags, + policyCategories, ) { const optimisticData = [ { @@ -352,6 +356,25 @@ function buildOnyxDataForMoneyRequest( }, ]; + if (!policy || !policy.id) { + return [optimisticData, successData, failureData]; + } + + const violationsOnyxData = ViolationsUtils.getViolationsOnyxData(transaction, [], policy.requiresTags, policyTags, policy.requiresCategory, policyCategories); + + if (violationsOnyxData) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction.transactionID}`, + value: violationsOnyxData, + }); + failureData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction.transactionID}`, + value: [], + }); + } + return [optimisticData, successData, failureData]; } @@ -373,6 +396,9 @@ function buildOnyxDataForMoneyRequest( * @param {String} [category] * @param {String} [tag] * @param {Boolean} [billable] + * @param {Object} [policy] + * @param {Object} [policyTags] + * @param {Object} [policyCategories] * @returns {Object} data * @returns {String} data.payerEmail * @returns {Object} data.iouReport @@ -402,6 +428,9 @@ function getMoneyRequestInformation( category = undefined, tag = undefined, billable = undefined, + policy = undefined, + policyTags = undefined, + policyCategories = undefined, ) { const payerEmail = OptionsListUtils.addSMSDomainIfPhoneNumber(participant.login); const payerAccountID = Number(participant.accountID); @@ -561,6 +590,9 @@ function getMoneyRequestInformation( optimisticPolicyRecentlyUsedTags, isNewChatReport, isNewIOUReport, + policy, + policyTags, + policyCategories, ); return { @@ -809,6 +841,9 @@ function editDistanceMoneyRequest(transactionID, transactionThreadReportID, tran * @param {String} [category] * @param {String} [tag] * @param {Boolean} [billable] + * @param {Object} [policy] + * @param {Object} [policyTags] + * @param {Object} [policyCategories] */ function requestMoney( report, @@ -824,12 +859,33 @@ function requestMoney( category = undefined, tag = undefined, billable = undefined, + policy = undefined, + policyTags = undefined, + policyCategories = undefined, ) { // If the report is iou or expense report, we should get the linked chat report to be passed to the getMoneyRequestInformation function const isMoneyRequestReport = ReportUtils.isMoneyRequestReport(report); const currentChatReport = isMoneyRequestReport ? ReportUtils.getReport(report.chatReportID) : report; const {payerAccountID, payerEmail, iouReport, chatReport, transaction, iouAction, createdChatReportActionID, createdIOUReportActionID, reportPreviewAction, onyxData} = - getMoneyRequestInformation(currentChatReport, participant, comment, amount, currency, created, merchant, payeeAccountID, payeeEmail, receipt, undefined, category, tag, billable); + getMoneyRequestInformation( + currentChatReport, + participant, + comment, + amount, + currency, + created, + merchant, + payeeAccountID, + payeeEmail, + receipt, + undefined, + category, + tag, + billable, + policy, + policyTags, + policyCategories, + ); const activeReportID = isMoneyRequestReport ? report.reportID : chatReport.reportID; API.write( diff --git a/src/pages/iou/steps/MoneyRequestConfirmPage.js b/src/pages/iou/steps/MoneyRequestConfirmPage.js index d6000e4e48cc..7bc865b6e667 100644 --- a/src/pages/iou/steps/MoneyRequestConfirmPage.js +++ b/src/pages/iou/steps/MoneyRequestConfirmPage.js @@ -22,6 +22,7 @@ import * as OptionsListUtils from '@libs/OptionsListUtils'; import * as ReportUtils from '@libs/ReportUtils'; import {iouDefaultProps, iouPropTypes} from '@pages/iou/propTypes'; import reportPropTypes from '@pages/reportPropTypes'; +import {policyDefaultProps, policyPropTypes} from '@pages/workspace/withPolicy'; import useThemeStyles from '@styles/useThemeStyles'; import * as IOU from '@userActions/IOU'; import * as Policy from '@userActions/Policy'; @@ -47,12 +48,28 @@ const propTypes = { /** Holds data related to Money Request view state, rather than the underlying Money Request data. */ iou: iouPropTypes, + /** The policy of the current report */ + policy: policyPropTypes, + + policyTags: PropTypes.shape({ + /** List of tags */ + tags: PropTypes.arrayOf(PropTypes.string), + }), + + policyCategories: PropTypes.shape({ + /** List of categories */ + categories: PropTypes.arrayOf(PropTypes.string), + }), + ...withCurrentUserPersonalDetailsPropTypes, }; const defaultProps = { report: {}, + policyCategories: {}, + policyTags: {}, iou: iouDefaultProps, + policy: policyDefaultProps, ...withCurrentUserPersonalDetailsDefaultProps, }; @@ -163,6 +180,9 @@ function MoneyRequestConfirmPage(props) { props.iou.category, props.iou.tag, props.iou.billable, + props.policy, + props.policyTags, + props.policyCategories, ); }, [ @@ -176,6 +196,9 @@ function MoneyRequestConfirmPage(props) { props.iou.category, props.iou.tag, props.iou.billable, + props.policy, + props.policyTags, + props.policyCategories, ], ); @@ -423,11 +446,14 @@ export default compose( selectedTab: { key: `${ONYXKEYS.COLLECTION.SELECTED_TAB}${CONST.TAB.RECEIPT_TAB_ID}`, }, - }), - // eslint-disable-next-line rulesdir/no-multiple-onyx-in-file - withOnyx({ policy: { key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY}${report ? report.policyID : '0'}`, }, + policyCategories: { + key: ({policy}) => `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policy ? policy.id : '0'}`, + }, + policyTags: { + key: ({policy}) => `${ONYXKEYS.COLLECTION.POLICY_TAGS}${policy ? policy.id : '0'}`, + }, }), )(MoneyRequestConfirmPage); diff --git a/src/pages/workspace/withPolicy.tsx b/src/pages/workspace/withPolicy.tsx index 8e6a67ce5ee7..8ed6be67ebf8 100644 --- a/src/pages/workspace/withPolicy.tsx +++ b/src/pages/workspace/withPolicy.tsx @@ -51,6 +51,20 @@ const policyPropTypes = { * } */ errorFields: PropTypes.objectOf(PropTypes.objectOf(PropTypes.string)), + + /** Whether or not the policy requires tags */ + requiresTags: PropTypes.bool, + + /** Whether or not the policy requires categories */ + requiresCategories: PropTypes.bool, + + /** Whether or not the policy has multiple tag lists */ + hasMultipleTagLists: PropTypes.bool, + + /** Whether or not the policy has tax tracking enabled */ + isTrackingTaxEnabled: PropTypes.bool, + + /** */ }), /** The employee list of this policy */