diff --git a/src/CONST.ts b/src/CONST.ts index 864d078ab04b..4cb4509e96a3 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -465,6 +465,7 @@ const CONST = { ACTIONS: { LIMIT: 50, TYPE: { + APPROVED: 'APPROVED', ADDCOMMENT: 'ADDCOMMENT', CLOSED: 'CLOSED', CREATED: 'CREATED', diff --git a/src/components/AvatarWithDisplayName.js b/src/components/AvatarWithDisplayName.js index 2fe1b3b7c57f..cbc34d6f9d47 100644 --- a/src/components/AvatarWithDisplayName.js +++ b/src/components/AvatarWithDisplayName.js @@ -26,8 +26,8 @@ const propTypes = { /** The report currently being looked at */ report: reportPropTypes, - /** The policies which the user has access to and which the report could be tied to */ - policies: PropTypes.shape({ + /** The policy which the user has access to and which the report is tied to */ + policy: PropTypes.shape({ /** Name of the policy */ name: PropTypes.string, }), @@ -47,7 +47,7 @@ const propTypes = { const defaultProps = { personalDetails: {}, - policies: {}, + policy: {}, report: {}, isAnonymous: false, size: CONST.AVATAR_SIZE.DEFAULT, @@ -72,7 +72,7 @@ function AvatarWithDisplayName(props) { const subtitle = ReportUtils.getChatRoomSubtitle(props.report); const parentNavigationSubtitleData = ReportUtils.getParentNavigationSubtitle(props.report); const isMoneyRequestOrReport = ReportUtils.isMoneyRequestReport(props.report) || ReportUtils.isMoneyRequest(props.report); - const icons = ReportUtils.getIcons(props.report, props.personalDetails, props.policies, true); + const icons = ReportUtils.getIcons(props.report, props.personalDetails, props.policy, true); const ownerPersonalDetails = OptionsListUtils.getPersonalDetailsForAccountIDs([props.report.ownerAccountID], props.personalDetails); const displayNamesWithTooltips = ReportUtils.getDisplayNamesWithTooltips(_.values(ownerPersonalDetails), false); const shouldShowSubscriptAvatar = ReportUtils.shouldReportShowSubscript(props.report); diff --git a/src/components/HeaderWithBackButton/headerWithBackButtonPropTypes.js b/src/components/HeaderWithBackButton/headerWithBackButtonPropTypes.js index a5f1c81823b0..d2cdc5b29898 100644 --- a/src/components/HeaderWithBackButton/headerWithBackButtonPropTypes.js +++ b/src/components/HeaderWithBackButton/headerWithBackButtonPropTypes.js @@ -73,8 +73,8 @@ const propTypes = { /** Report, if we're showing the details for one and using AvatarWithDisplay */ report: iouReportPropTypes, - /** Policies, if we're showing the details for a report and need info about it for AvatarWithDisplay */ - policies: PropTypes.shape({ + /** The report's policy, if we're showing the details for a report and need info about it for AvatarWithDisplay */ + policy: PropTypes.shape({ /** Name of the policy */ name: PropTypes.string, }), diff --git a/src/components/HeaderWithBackButton/index.js b/src/components/HeaderWithBackButton/index.js index 6e6164f4c4fc..bbf905cc1ac2 100755 --- a/src/components/HeaderWithBackButton/index.js +++ b/src/components/HeaderWithBackButton/index.js @@ -27,7 +27,7 @@ function HeaderWithBackButton({ onDownloadButtonPress = () => {}, onThreeDotsButtonPress = () => {}, report = null, - policies = {}, + policy = {}, personalDetails = {}, shouldShowAvatarWithDisplay = false, shouldShowBackButton = true, @@ -77,7 +77,7 @@ function HeaderWithBackButton({ {shouldShowAvatarWithDisplay && ( )} diff --git a/src/components/MoneyReportHeader.js b/src/components/MoneyReportHeader.js index c4ce8cd173f1..7e6cc78ebd13 100644 --- a/src/components/MoneyReportHeader.js +++ b/src/components/MoneyReportHeader.js @@ -1,8 +1,9 @@ -import React from 'react'; +import React, {useMemo} from 'react'; import {withOnyx} from 'react-native-onyx'; import {View} from 'react-native'; import PropTypes from 'prop-types'; import lodashGet from 'lodash/get'; +import useLocalize from '../hooks/useLocalize'; import HeaderWithBackButton from './HeaderWithBackButton'; import iouReportPropTypes from '../pages/iouReportPropTypes'; import * as ReportUtils from '../libs/ReportUtils'; @@ -12,9 +13,10 @@ import withWindowDimensions, {windowDimensionsPropTypes} from './withWindowDimen import compose from '../libs/compose'; import Navigation from '../libs/Navigation/Navigation'; import ROUTES from '../ROUTES'; -import SettlementButton from './SettlementButton'; -import * as Policy from '../libs/actions/Policy'; import ONYXKEYS from '../ONYXKEYS'; +import CONST from '../CONST'; +import SettlementButton from './SettlementButton'; +import Button from './Button'; import * as IOU from '../libs/actions/IOU'; import * as CurrencyUtils from '../libs/CurrencyUtils'; import reportPropTypes from '../pages/reportPropTypes'; @@ -23,10 +25,16 @@ const propTypes = { /** The report currently being looked at */ report: iouReportPropTypes.isRequired, - /** The policies which the user has access to and which the report could be tied to */ - policies: PropTypes.shape({ + /** The policy tied to the money request report */ + policy: PropTypes.shape({ /** Name of the policy */ name: PropTypes.string, + + /** Type of the policy */ + type: PropTypes.string, + + /** The role of the current user in the policy */ + role: PropTypes.string, }).isRequired, /** The chat report this report is linked to */ @@ -51,39 +59,50 @@ const defaultProps = { }, }; -function MoneyReportHeader(props) { - const moneyRequestReport = props.report; +function MoneyReportHeader({session, personalDetails, policy, chatReport, report: moneyRequestReport, isSmallScreenWidth}) { + const {translate} = useLocalize(); + const reportTotal = ReportUtils.getMoneyRequestTotal(moneyRequestReport); + const isApproved = ReportUtils.isReportApproved(moneyRequestReport); const isSettled = ReportUtils.isSettled(moneyRequestReport.reportID); - const policy = props.policies[`${ONYXKEYS.COLLECTION.POLICY}${props.report.policyID}`]; - const isPayer = - Policy.isAdminOfFreePolicy([policy]) || (ReportUtils.isMoneyRequestReport(moneyRequestReport) && lodashGet(props.session, 'accountID', null) === moneyRequestReport.managerID); - const reportTotal = ReportUtils.getMoneyRequestTotal(props.report); - const shouldShowSettlementButton = !isSettled && isPayer && !moneyRequestReport.isWaitingOnBankAccount && reportTotal !== 0; - const bankAccountRoute = ReportUtils.getBankAccountRoute(props.chatReport); - const shouldShowPaypal = Boolean(lodashGet(props.personalDetails, [moneyRequestReport.managerID, 'payPalMeAddress'])); - const formattedAmount = CurrencyUtils.convertToDisplayString(reportTotal, props.report.currency); + const policyType = lodashGet(policy, 'type'); + const isPolicyAdmin = policyType !== CONST.POLICY.TYPE.PERSONAL && lodashGet(policy, 'role') === CONST.POLICY.ROLE.ADMIN; + const isManager = ReportUtils.isMoneyRequestReport(moneyRequestReport) && lodashGet(session, 'accountID', null) === moneyRequestReport.managerID; + const isPayer = policyType === CONST.POLICY.TYPE.CORPORATE ? isPolicyAdmin && isApproved : isPolicyAdmin || (ReportUtils.isMoneyRequestReport(moneyRequestReport) && isManager); + const shouldShowSettlementButton = useMemo( + () => isPayer && !isSettled && !moneyRequestReport.isWaitingOnBankAccount && reportTotal !== 0, + [isPayer, isSettled, moneyRequestReport, reportTotal], + ); + const shouldShowApproveButton = useMemo(() => { + if (policyType !== CONST.POLICY.TYPE.CORPORATE) { + return false; + } + return isManager && !isApproved && !isSettled; + }, [policyType, isManager, isApproved, isSettled]); + const bankAccountRoute = ReportUtils.getBankAccountRoute(chatReport); + const shouldShowPaypal = Boolean(lodashGet(personalDetails, [moneyRequestReport.managerID, 'payPalMeAddress'])); + const formattedAmount = CurrencyUtils.convertToDisplayString(reportTotal, moneyRequestReport.currency); return ( Navigation.goBack(ROUTES.HOME, false, true)} - shouldShowBorderBottom={!shouldShowSettlementButton || !props.isSmallScreenWidth} + shouldShowBorderBottom={!shouldShowSettlementButton || !isSmallScreenWidth} > - {shouldShowSettlementButton && !props.isSmallScreenWidth && ( + {shouldShowSettlementButton && !isSmallScreenWidth && ( IOU.payMoneyRequest(paymentType, props.chatReport, props.report)} + chatReportID={chatReport.reportID} + iouReport={moneyRequestReport} + onPress={(paymentType) => IOU.payMoneyRequest(paymentType, chatReport, moneyRequestReport)} enablePaymentsRoute={ROUTES.BANK_ACCOUNT_NEW} addBankAccountRoute={bankAccountRoute} shouldShowPaymentOptions @@ -92,16 +111,27 @@ function MoneyReportHeader(props) { /> )} + {shouldShowApproveButton && !isSmallScreenWidth && ( + +