result( Severity::UNKNOWN, null, array(), 'MB_COST_MISSING' ); } if ( null === $revenue ) { return $this->result( Severity::UNKNOWN, null, array(), 'MB_TOTALS_INVALID' ); } if ( ! $evaluation_complete ) { return $this->result( Severity::UNKNOWN, null, array(), 'MB_PRICING_UNSUPPORTED' ); } $profit = MoneyMath::sub( $revenue, $cogs ); // Zero-revenue edge cases (PRD 9.9 / MB014 fully discounted line). if ( $revenue->is_zero() ) { if ( $cogs->is_positive() ) { return $this->result( Severity::CRITICAL, $profit, array( 'case' => 'zero_revenue_positive_cogs', ), 'MB014' ); } return $this->result( Severity::UNKNOWN, $profit, array( 'case' => 'zero_revenue_zero_cogs' ), null ); } if ( $revenue->is_negative() ) { return $this->result( Severity::UNKNOWN, $profit, array(), 'MB_TOTALS_INVALID' ); } $critical = (int) $thresholds['critical_below']; $high = (int) $thresholds['high_below']; $warning = (int) $thresholds['warning_below']; $vs_critical = MoneyMath::compare_margin_to_threshold( $profit, $revenue, $critical, Constants::THRESHOLD_SCALE ); if ( $vs_critical['cmp'] < 0 ) { return $this->result( Severity::CRITICAL, $profit, $vs_critical, 'MB001' ); } $vs_high = MoneyMath::compare_margin_to_threshold( $profit, $revenue, $high, Constants::THRESHOLD_SCALE ); if ( $vs_high['cmp'] < 0 ) { return $this->result( Severity::HIGH, $profit, $vs_high, 'MB001' ); } $vs_warning = MoneyMath::compare_margin_to_threshold( $profit, $revenue, $warning, Constants::THRESHOLD_SCALE ); if ( $vs_warning['cmp'] < 0 ) { return $this->result( Severity::WARNING, $profit, $vs_warning, 'MB001' ); } return $this->result( Severity::SAFE, $profit, $vs_warning, null ); } /** * @param string $severity Severity. * @param MoneyAmount|null $profit Gross profit. * @param array $basis Comparison basis. * @param string|null $reason Reason code. * @return array */ private function result( $severity, $profit, array $basis, $reason ) { return array( 'severity' => $severity, 'gross_profit' => $profit, 'severity_basis' => $basis, 'reason_code' => $reason, ); } }