Reverse Pre-Qual Analysis
Cain & Company Real Estate LLC | NMLS: 1980282
/** * Cain CDA — Server-Side Commission Fix * ─────────────────────────────────────── * WPCode → PHP Snippet → "CDA Server Commission Fix" * Run on: Everywhere * * Intercepts the CDA submit REST request BEFORE it hits the original * plugin handler, captures commission fields from the request body, * stores them in a global, then saves them to post meta and regenerates * the invoice after the post is created. No JavaScript required. */ // ── Step 1: Capture commission data from REST request body ──────────────── add_filter( 'rest_pre_dispatch', 'cain_capture_commission_request', 1, 3 ); function cain_capture_commission_request( $result, $server, $request ) { $route = $request->get_route(); // Only intercept the CDA submit endpoint if ( strpos($route, 'cda/submit') === false ) { return $result; } // Pull every possible commission field from the request body $body = $request->get_body_params(); if ( empty($body) ) { // Try JSON body $json = $request->get_json_params(); if ( ! empty($json) ) $body = $json; } if ( empty($body) ) { // Try raw body $raw = $request->get_body(); if ( $raw ) { $decoded = json_decode($raw, true); if ( is_array($decoded) ) $body = $decoded; } } // Store in global for use after post is created $GLOBALS['cain_cda_commission'] = [ 'gross_commission' => floatval( $body['grossCommission'] ?? $body['gross_commission'] ?? $body['commission_amt'] ?? 0 ), 'agent_split' => floatval( $body['agentSplit'] ?? $body['agent_split'] ?? $body['split_pct'] ?? 0 ), 'agent_commission' => floatval( $body['agentGrossCommission'] ?? $body['agent_commission'] ?? 0 ), 'broker_fee' => floatval( $body['brokerFee'] ?? $body['broker_fee'] ?? $body['broker_commission'] ?? 0 ), 'transaction_fee' => floatval( $body['transactionFee'] ?? $body['transaction_fee'] ?? 0 ), 'eo_fee' => floatval( $body['eoFee'] ?? $body['eo_fee'] ?? 0 ), 'other1' => floatval( $body['other1'] ?? $body['other_deduction_1'] ?? 0 ), 'other2' => floatval( $body['other2'] ?? $body['other_deduction_2'] ?? 0 ), 'net_disbursement' => floatval( $body['netDisbursement'] ?? $body['net_disbursement'] ?? 0 ), ]; // Calculate anything missing $c = &$GLOBALS['cain_cda_commission']; if ( ! $c['agent_commission'] && $c['gross_commission'] && $c['agent_split'] ) { $c['agent_commission'] = $c['gross_commission'] * ( $c['agent_split'] / 100 ); } if ( ! $c['net_disbursement'] && $c['agent_commission'] ) { $c['net_disbursement'] = $c['agent_commission'] - $c['broker_fee'] - $c['transaction_fee'] - $c['eo_fee'] - $c['other1'] - $c['other2']; } return $result; // Don't block the original handler } // ── Step 2: After post is saved, write commission meta + new invoice ─────── add_action( 'save_post_cain_cda', 'cain_save_commission_and_invoice', 99, 3 ); function cain_save_commission_and_invoice( $post_id, $post, $update ) { if ( $post->post_status !== 'publish' ) return; if ( wp_is_post_revision($post_id) ) return; if ( wp_is_post_autosave($post_id) ) return; // Get captured commission data $c = $GLOBALS['cain_cda_commission'] ?? []; // Also check if commission is already in post meta (from a previous save) $stored_gross = floatval( get_post_meta($post_id, '_commission_amt', true) ?: get_post_meta($post_id, '_gross_commission', true) ?: 0 ); // Merge: prefer captured data, fall back to stored $gross = $c['gross_commission'] ?: $stored_gross; $split = $c['agent_split'] ?: floatval(get_post_meta($post_id, '_split_pct', true) ?: get_post_meta($post_id, '_agent_split', true) ?: 0); $acomm = $c['agent_commission'] ?: floatval(get_post_meta($post_id, '_agent_commission', true) ?: 0); $broker = $c['broker_fee'] ?: floatval(get_post_meta($post_id, '_broker_commission', true) ?: get_post_meta($post_id, '_broker_fee', true) ?: 0); $tx = $c['transaction_fee'] ?: floatval(get_post_meta($post_id, '_transaction_fee', true) ?: 0); $eo = $c['eo_fee'] ?: floatval(get_post_meta($post_id, '_eo_fee', true) ?: 0); $other1 = $c['other1'] ?: floatval(get_post_meta($post_id, '_other_deduction_1', true) ?: 0); $other2 = $c['other2'] ?: floatval(get_post_meta($post_id, '_other_deduction_2', true) ?: 0); $net = $c['net_disbursement'] ?: floatval(get_post_meta($post_id, '_net_disbursement', true) ?: 0); // Skip if no commission data at all if ( ! $gross && ! $acomm ) return; // Calculate missing values if ( ! $acomm && $gross && $split ) $acomm = $gross * ($split / 100); if ( ! $net && $acomm ) $net = $acomm - $broker - $tx - $eo - $other1 - $other2; // Save to post meta (correct keys) update_post_meta( $post_id, '_commission_amt', $gross ); update_post_meta( $post_id, '_gross_commission', $gross ); update_post_meta( $post_id, '_split_pct', $split ); update_post_meta( $post_id, '_agent_split', $split ); update_post_meta( $post_id, '_agent_commission', $acomm ); update_post_meta( $post_id, '_broker_commission', $broker ); update_post_meta( $post_id, '_broker_fee', $broker ); update_post_meta( $post_id, '_transaction_fee', $tx ); update_post_meta( $post_id, '_eo_fee', $eo ); update_post_meta( $post_id, '_other_deduction_1', $other1 ); update_post_meta( $post_id, '_other_deduction_2', $other2 ); update_post_meta( $post_id, '_net_disbursement', $net ); // ── Pull all other meta for invoice ─────────────────────────────────── $m = fn($k) => get_post_meta($post_id, '_' . $k, true) ?: ''; $agent_name = $m('agent_name') ?: get_the_title($post_id); $agent_email = $m('agent_email'); $agent_phone = $m('agent_phone'); $trec = $m('trec_license'); $tx_type = $m('transaction_type') ?: 'Sale'; $address = $m('property_address'); $city = $m('city'); $state = $m('state'); $zip = $m('zip'); $csz = trim("$city, $state $zip", ', '); $close_date = $m('closing_date'); $title_co = $m('title_company'); $title_ct = $m('title_contact'); $inv_co = $m('invoice_company') ?: $m('title_company'); $inv_email = $m('invoice_email'); $notes = $m('notes'); // Invoice number $inv_num = get_post_meta($post_id, '_invoice_number', true); if ( ! $inv_num ) { $inv_num = 'CDA-' . date('Ymd', strtotime($post->post_date)) . '-' . $post_id; update_post_meta($post_id, '_invoice_number', $inv_num); } $$ = fn($n) => $n > 0 ? '$' . number_format($n, 2) : '—'; $p = fn($n) => $n > 0 ? number_format($n, 1) . '%' : '—'; $t = fn($v) => $v ? esc_html($v) : '—'; // ── Build invoice HTML ───────────────────────────────────────────────── $html = '
Cain & Company Real Estate LLC | NMLS: 1980282