|
| 1 | +import type {OnyxCollection} from 'react-native-onyx'; |
| 2 | +import type {ValueOf} from 'type-fest'; |
| 3 | +import CONST from '@src/CONST'; |
| 4 | +import type {Policy, Report, Transaction, TransactionViolation} from '@src/types/onyx'; |
| 5 | +import {isApprover as isApprovedMember} from './actions/Policy/Member'; |
| 6 | +import {getCurrentUserAccountID} from './actions/Report'; |
| 7 | +import {arePaymentsEnabled, getCorrectedAutoReportingFrequency, hasAccountingConnections, isAutoSyncEnabled, isPrefferedExporter} from './PolicyUtils'; |
| 8 | +import { |
| 9 | + isClosedReport, |
| 10 | + isCurrentUserSubmitter, |
| 11 | + isExpenseReport, |
| 12 | + isHoldCreator, |
| 13 | + isInvoiceReport, |
| 14 | + isIOUReport, |
| 15 | + isOpenReport, |
| 16 | + isPayer, |
| 17 | + isProcessingReport, |
| 18 | + isReportApproved, |
| 19 | + isSettled, |
| 20 | +} from './ReportUtils'; |
| 21 | +import {getSession} from './SessionUtils'; |
| 22 | +import {allHavePendingRTERViolation, isDuplicate, isOnHold as isOnHoldTransactionUtils, shouldShowBrokenConnectionViolationForMultipleTransactions} from './TransactionUtils'; |
| 23 | + |
| 24 | +function isSubmitAction(report: Report, policy: Policy) { |
| 25 | + const isExpense = isExpenseReport(report); |
| 26 | + const isSubmitter = isCurrentUserSubmitter(report.reportID); |
| 27 | + const isOpen = isOpenReport(report); |
| 28 | + const isManualSubmitEnabled = getCorrectedAutoReportingFrequency(policy) === CONST.POLICY.AUTO_REPORTING_FREQUENCIES.MANUAL; |
| 29 | + |
| 30 | + return isExpense && isSubmitter && isOpen && isManualSubmitEnabled; |
| 31 | +} |
| 32 | + |
| 33 | +function isApproveAction(report: Report, policy: Policy, reportTransactions: Transaction[]) { |
| 34 | + const isExpense = isExpenseReport(report); |
| 35 | + const isApprover = isApprovedMember(policy, getCurrentUserAccountID()); |
| 36 | + const isApprovalEnabled = policy.approvalMode && policy.approvalMode !== CONST.POLICY.APPROVAL_MODE.OPTIONAL; |
| 37 | + |
| 38 | + if (!isExpense || !isApprover || !isApprovalEnabled) { |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + const isOneExpenseReport = isExpense && reportTransactions.length === 1; |
| 43 | + const isOnHold = reportTransactions.some(isOnHoldTransactionUtils); |
| 44 | + const isProcessing = isProcessingReport(report); |
| 45 | + const isOneExpenseReportOnHold = isOneExpenseReport && isOnHold; |
| 46 | + |
| 47 | + if (isProcessing || isOneExpenseReportOnHold) { |
| 48 | + return true; |
| 49 | + } |
| 50 | + |
| 51 | + return false; |
| 52 | +} |
| 53 | + |
| 54 | +function isPayAction(report: Report, policy: Policy) { |
| 55 | + const isExpense = isExpenseReport(report); |
| 56 | + const isReportPayer = isPayer(getSession(), report, false, policy); |
| 57 | + const isPaymentsEnabled = arePaymentsEnabled(policy); |
| 58 | + const isApproved = isReportApproved({report}); |
| 59 | + const isClosed = isClosedReport(report); |
| 60 | + const isFinished = isApproved || isClosed; |
| 61 | + |
| 62 | + if (isReportPayer && isExpense && isPaymentsEnabled && isFinished) { |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + const isProcessing = isProcessingReport(report); |
| 67 | + const isInvoice = isInvoiceReport(report); |
| 68 | + const isIOU = isIOUReport(report); |
| 69 | + |
| 70 | + if ((isInvoice || isIOU) && isProcessing) { |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + return false; |
| 75 | +} |
| 76 | + |
| 77 | +function isExportAction(report: Report, policy: Policy) { |
| 78 | + const hasAccountingConnection = hasAccountingConnections(policy); |
| 79 | + if (!hasAccountingConnection) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + const isExporter = isPrefferedExporter(policy); |
| 84 | + if (!isExporter) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + const syncEnabled = isAutoSyncEnabled(policy); |
| 89 | + if (syncEnabled) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + const isReimbursed = isSettled(report); |
| 94 | + const isApproved = isReportApproved({report}); |
| 95 | + const isClosed = isClosedReport(report); |
| 96 | + |
| 97 | + if (isApproved || isReimbursed || isClosed) { |
| 98 | + return true; |
| 99 | + } |
| 100 | + |
| 101 | + return false; |
| 102 | +} |
| 103 | + |
| 104 | +function isRemoveHoldAction(report: Report, reportTransactions: Transaction[]) { |
| 105 | + const isOnHold = reportTransactions.some(isOnHoldTransactionUtils); |
| 106 | + const isHolder = reportTransactions.some((transaction) => isHoldCreator(transaction, report.reportID)); |
| 107 | + |
| 108 | + return isOnHold && isHolder; |
| 109 | +} |
| 110 | + |
| 111 | +function isReviewDuplicatesAction(report: Report, policy: Policy, reportTransactions: Transaction[]) { |
| 112 | + const hasDuplicates = reportTransactions.some((transaction) => isDuplicate(transaction.transactionID)); |
| 113 | + |
| 114 | + if (!hasDuplicates) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + const isApprover = isApprovedMember(policy, getCurrentUserAccountID()); |
| 119 | + const isSubmitter = isCurrentUserSubmitter(report.reportID); |
| 120 | + const isProcessing = isProcessingReport(report); |
| 121 | + const isOpen = isOpenReport(report); |
| 122 | + |
| 123 | + const isSubmitterOrApprover = isSubmitter || isApprover; |
| 124 | + const isActive = isOpen || isProcessing; |
| 125 | + |
| 126 | + if (isSubmitterOrApprover && isActive) { |
| 127 | + return true; |
| 128 | + } |
| 129 | + |
| 130 | + return false; |
| 131 | +} |
| 132 | + |
| 133 | +function isMarkAsCashAction(report: Report, policy: Policy, reportTransactions: Transaction[], violations: OnyxCollection<TransactionViolation[]>) { |
| 134 | + const transactionIDs = reportTransactions.map((t) => t.transactionID); |
| 135 | + const hasAllPendingRTERViolations = allHavePendingRTERViolation(transactionIDs, violations); |
| 136 | + |
| 137 | + if (hasAllPendingRTERViolations) { |
| 138 | + return true; |
| 139 | + } |
| 140 | + |
| 141 | + const isSubmitter = isCurrentUserSubmitter(report.reportID); |
| 142 | + const isApprover = isApprovedMember(policy, getCurrentUserAccountID()); |
| 143 | + const isAdmin = policy?.role === CONST.POLICY.ROLE.ADMIN; |
| 144 | + |
| 145 | + const shouldShowBrokenConnectionViolation = shouldShowBrokenConnectionViolationForMultipleTransactions(transactionIDs, report, policy, violations); |
| 146 | + |
| 147 | + const userControlsReport = isSubmitter || isApprover || isAdmin; |
| 148 | + return userControlsReport && shouldShowBrokenConnectionViolation; |
| 149 | +} |
| 150 | + |
| 151 | +function getPrimaryAction( |
| 152 | + report: Report, |
| 153 | + policy: Policy, |
| 154 | + reportTransactions: Transaction[], |
| 155 | + violations: OnyxCollection<TransactionViolation[]>, |
| 156 | +): ValueOf<typeof CONST.REPORT.PRIMARY_ACTIONS> | '' { |
| 157 | + if (isSubmitAction(report, policy)) { |
| 158 | + return CONST.REPORT.PRIMARY_ACTIONS.SUBMIT; |
| 159 | + } |
| 160 | + |
| 161 | + if (isApproveAction(report, policy, reportTransactions)) { |
| 162 | + return CONST.REPORT.PRIMARY_ACTIONS.APPROVE; |
| 163 | + } |
| 164 | + |
| 165 | + if (isPayAction(report, policy)) { |
| 166 | + return CONST.REPORT.PRIMARY_ACTIONS.PAY; |
| 167 | + } |
| 168 | + |
| 169 | + if (isExportAction(report, policy)) { |
| 170 | + return CONST.REPORT.PRIMARY_ACTIONS.EXPORT_TO_ACCOUNTING; |
| 171 | + } |
| 172 | + |
| 173 | + if (isRemoveHoldAction(report, reportTransactions)) { |
| 174 | + return CONST.REPORT.PRIMARY_ACTIONS.REMOVE_HOLD; |
| 175 | + } |
| 176 | + |
| 177 | + if (isReviewDuplicatesAction(report, policy, reportTransactions)) { |
| 178 | + return CONST.REPORT.PRIMARY_ACTIONS.REVIEW_DUPLICATES; |
| 179 | + } |
| 180 | + |
| 181 | + if (isMarkAsCashAction(report, policy, reportTransactions, violations)) { |
| 182 | + return CONST.REPORT.PRIMARY_ACTIONS.MARK_AS_CASH; |
| 183 | + } |
| 184 | + |
| 185 | + return ''; |
| 186 | +} |
| 187 | + |
| 188 | +export default getPrimaryAction; |
0 commit comments