function column_default($item, $column_name)
 {
     switch ($column_name) {
         case 'rate':
             $download = get_post_meta($item['ID'], '_download_id', true);
             $type = eddc_get_commission_type($download);
             if ('percentage' == $type) {
                 return $item[$column_name] . '%';
             } else {
                 return edd_currency_filter(edd_sanitize_amount($item[$column_name]));
             }
         case 'status':
             return $item[$column_name];
         case 'amount':
             return edd_currency_filter(edd_format_amount($item[$column_name]));
         case 'date':
             return date_i18n(get_option('date_format'), strtotime(get_post_field('post_date', $item['ID'])));
         case 'download':
             $download = !empty($item['download']) ? $item['download'] : false;
             return $download ? '<a href="' . esc_url(add_query_arg('download', $download)) . '" title="' . __('View all commissions for this item', 'eddc') . '">' . get_the_title($download) . '</a>' . (!empty($item['variation']) ? ' - ' . $item['variation'] : '') : '';
         case 'payment':
             $payment = get_post_meta($item['ID'], '_edd_commission_payment_id', true);
             return $payment ? '<a href="' . esc_url(admin_url('edit.php?post_type=download&page=edd-payment-history&view=view-order-details&id=' . $payment)) . '" title="' . __('View payment details', 'eddc') . '">#' . $payment . '</a> - ' . edd_get_payment_status(get_post($payment), true) : '';
         default:
             return print_r($item, true);
             //Show the whole array for troubleshooting purposes
     }
 }
/**
 * Setup PayPal receivers when a purchase is made
 *
 * @since 2.7
 * @param $receivers string The default receivers and their percentages as defined in the Payment Gateway settings
 * @param $payment_id int The payment ID of the purchase
 * @return receivers $string The modified receivers string
 */
function eddc_paypal_adaptive_autopay($receivers, $payment_id)
{
    if (!edd_get_option('edd_commissions_autopay_pa')) {
        return $receivers;
    }
    $cart = edd_get_payment_meta_cart_details($payment_id);
    if ('subtotal' == edd_get_option('edd_commissions_calc_base', 'subtotal')) {
        $total = edd_get_payment_subtotal($payment_id);
    } else {
        $total = edd_get_payment_amount($payment_id);
    }
    $final = array();
    foreach ($cart as $item) {
        $recipients = eddc_get_recipients($item['id']);
        if ('subtotal' == edd_get_option('edd_commissions_calc_base', 'subtotal')) {
            $price = $item['subtotal'];
        } else {
            $price = $item['price'];
        }
        foreach ($recipients as $recipient) {
            $type = eddc_get_commission_type($item['id']);
            $rate = eddc_get_recipient_rate($item['id'], $recipient);
            $args = array('price' => $price, 'rate' => $rate, 'type' => $type, 'download_id' => $item['id'], 'recipient' => $recipient, 'payment_id' => $payment_id);
            $amount = eddc_calc_commission_amount($args);
            $percentage = round(100 / $total * $amount, 2);
            $user = get_userdata($recipient);
            $custom_paypal = get_user_meta($recipient, 'eddc_user_paypal', true);
            $email = is_email($custom_paypal) ? $custom_paypal : $user->user_email;
            if ($percentage !== 0) {
                if (isset($final[$email])) {
                    $final[$email] = $percentage + $final[$email];
                } else {
                    $final[$email] = $percentage;
                }
            }
        }
    }
    $return = '';
    $counter = 0;
    $taken = 0;
    // Add up the total commissions
    foreach ($final as $person => $val) {
        $taken = $taken + $val;
    }
    // Calculate the final percentage the store owners should receive
    $remaining = 100 - $taken;
    $owners = $receivers;
    $owners = explode("\n", $owners);
    foreach ($owners as $key => $val) {
        $val = explode('|', $val);
        $email = $val[0];
        $percentage = $val[1];
        $remainder = $percentage / 100 * $remaining;
        if (isset($final[$email])) {
            $final[$email] = $final[$email] + $remainder;
        } else {
            $final[$email] = $remainder;
        }
    }
    // Rebuild the final PayPal receivers string
    foreach ($final as $person => $val) {
        if ($counter === 0) {
            $return = $person . "|" . $val;
        } else {
            $return = $return . "\n" . $person . "|" . $val;
        }
        $counter++;
    }
    //echo '<pre>'; print_r( $return ); echo '</pre>'; exit;
    return $return;
}
/**
 * Record Commissions
 *
 * @access      private
 * @since       1.0
 * @return      void
 */
function eddc_record_commission($payment_id, $new_status, $old_status)
{
    // Check if the payment was already set to complete
    if ($old_status == 'publish' || $old_status == 'complete') {
        return;
    }
    // Make sure that payments are only completed once
    // Make sure the commission is only recorded when new status is complete
    if ($new_status != 'publish' && $new_status != 'complete') {
        return;
    }
    if (edd_get_payment_gateway($payment_id) == 'manual_purchases' && !isset($_POST['commission'])) {
        return;
    }
    // do not record commission on manual payments unless specified
    $payment_data = edd_get_payment_meta($payment_id);
    $user_info = maybe_unserialize($payment_data['user_info']);
    $cart_details = maybe_unserialize($payment_data['cart_details']);
    // loop through each purchased download and award commissions, if needed
    foreach ($cart_details as $download) {
        $download_id = absint($download['id']);
        $commissions_enabled = get_post_meta($download_id, '_edd_commisions_enabled', true);
        if ('subtotal' == edd_get_option('edd_commissions_calc_base', 'subtotal')) {
            $price = $download['subtotal'];
        } else {
            $price = $download['price'];
        }
        // if we need to award a commission, and the price is greater than zero
        if ($commissions_enabled && floatval($price) > '0') {
            // set a flag so downloads with commissions awarded are easy to query
            update_post_meta($download_id, '_edd_has_commission', true);
            $commission_settings = get_post_meta($download_id, '_edd_commission_settings', true);
            if ($commission_settings) {
                $type = eddc_get_commission_type($download_id);
                // but if we have price variations, then we need to get the name of the variation
                if (edd_has_variable_prices($download_id)) {
                    $price_id = edd_get_cart_item_price_id($download);
                    $variation = edd_get_price_option_name($download_id, $price_id);
                }
                $recipients = eddc_get_recipients($download_id);
                // Record a commission for each user
                foreach ($recipients as $recipient) {
                    $rate = eddc_get_recipient_rate($download_id, $recipient);
                    // percentage amount of download price
                    $commission_amount = eddc_calc_commission_amount($price, $rate, $type);
                    // calculate the commission amount to award
                    $currency = $payment_data['currency'];
                    $commission = array('post_type' => 'edd_commission', 'post_title' => $user_info['email'] . ' - ' . get_the_title($download_id), 'post_status' => 'publish');
                    $commission_id = wp_insert_post(apply_filters('edd_commission_post_data', $commission));
                    $commission_info = apply_filters('edd_commission_info', array('user_id' => $recipient, 'rate' => $rate, 'amount' => $commission_amount, 'currency' => $currency), $commission_id);
                    update_post_meta($commission_id, '_edd_commission_info', $commission_info);
                    update_post_meta($commission_id, '_commission_status', 'unpaid');
                    update_post_meta($commission_id, '_download_id', $download_id);
                    update_post_meta($commission_id, '_user_id', $recipient);
                    update_post_meta($commission_id, '_edd_commission_payment_id', $payment_id);
                    //if we are dealing with a variation, then save variation info
                    if (isset($variation)) {
                        update_post_meta($commission_id, '_edd_commission_download_variation', $variation);
                    }
                    do_action('eddc_insert_commission', $recipient, $commission_amount, $rate, $download_id, $commission_id, $payment_id);
                }
            }
        }
    }
}
/**
 * Record Commissions
 *
 * @access      private
 * @since       1.0
 * @return      void
 */
function eddc_record_commission($payment_id, $new_status, $old_status)
{
    // Check if the payment was already set to complete
    if ($old_status == 'publish' || $old_status == 'complete') {
        return;
    }
    // Make sure that payments are only completed once
    // Make sure the commission is only recorded when new status is complete
    if ($new_status != 'publish' && $new_status != 'complete') {
        return;
    }
    if (edd_get_payment_gateway($payment_id) == 'manual_purchases' && !isset($_POST['commission'])) {
        return;
    }
    // do not record commission on manual payments unless specified
    if (edd_get_payment_meta($payment_id, '_edd_completed_date')) {
        return;
    }
    $payment_data = edd_get_payment_meta($payment_id);
    $user_info = maybe_unserialize($payment_data['user_info']);
    $cart_details = edd_get_payment_meta_cart_details($payment_id);
    $calc_base = edd_get_option('edd_commissions_calc_base', 'subtotal');
    $shipping = edd_get_option('edd_commissions_shipping', 'ignored');
    // loop through each purchased download and award commissions, if needed
    foreach ($cart_details as $download) {
        $download_id = absint($download['id']);
        $commissions_enabled = get_post_meta($download_id, '_edd_commisions_enabled', true);
        if ('subtotal' == $calc_base) {
            $price = $download['subtotal'];
        } else {
            if ('total_pre_tax' == $calc_base) {
                $price = $download['price'] - $download['tax'];
            } else {
                $price = $download['price'];
            }
        }
        if (!empty($download['fees'])) {
            foreach ($download['fees'] as $fee_id => $fee) {
                if (false !== strpos($fee_id, 'shipping')) {
                    // If we're adjusting the commission for shipping, we need to remove it from the calculation and then add it after the commission amount has been determined
                    if ('ignored' !== $shipping) {
                        continue;
                    }
                }
                $price += $fee['amount'];
            }
        }
        // if we need to award a commission, and the price is greater than zero
        if ($commissions_enabled && floatval($price) > '0') {
            // set a flag so downloads with commissions awarded are easy to query
            update_post_meta($download_id, '_edd_has_commission', true);
            $commission_settings = get_post_meta($download_id, '_edd_commission_settings', true);
            if ($commission_settings) {
                $type = eddc_get_commission_type($download_id);
                // but if we have price variations, then we need to get the name of the variation
                $has_variable_prices = edd_has_variable_prices($download_id);
                if ($has_variable_prices) {
                    $price_id = edd_get_cart_item_price_id($download);
                    $variation = edd_get_price_option_name($download_id, $price_id);
                }
                $recipients = eddc_get_recipients($download_id);
                // Record a commission for each user
                foreach ($recipients as $recipient) {
                    $rate = eddc_get_recipient_rate($download_id, $recipient);
                    // percentage amount of download price
                    $args = array('price' => $price, 'rate' => $rate, 'type' => $type, 'download_id' => $download_id, 'recipient' => $recipient, 'payment_id' => $payment_id);
                    $commission_amount = eddc_calc_commission_amount($args);
                    // calculate the commission amount to award
                    $currency = $payment_data['currency'];
                    // If shipping is included or not included, we need to adjust the amount
                    if (!empty($download['fees']) && 'ignored' !== $shipping) {
                        foreach ($download['fees'] as $fee_id => $fee) {
                            if (false !== strpos($fee_id, 'shipping')) {
                                // If we're adjusting the commission for shipping, we need to remove it from the calculation and then add it after the commission amount has been determined
                                if ('include_shipping' == $shipping) {
                                    $commission_amount += $fee['amount'];
                                }
                            }
                        }
                    }
                    $commission = array('post_type' => 'edd_commission', 'post_title' => $user_info['email'] . ' - ' . get_the_title($download_id), 'post_status' => 'publish');
                    $commission_id = wp_insert_post(apply_filters('edd_commission_post_data', $commission));
                    $commission_info = apply_filters('edd_commission_info', array('user_id' => $recipient, 'rate' => $rate, 'amount' => $commission_amount, 'currency' => $currency), $commission_id, $payment_id, $download_id);
                    eddc_set_commission_status($commission_id, 'unpaid');
                    update_post_meta($commission_id, '_edd_commission_info', $commission_info);
                    update_post_meta($commission_id, '_download_id', $download_id);
                    update_post_meta($commission_id, '_user_id', $recipient);
                    update_post_meta($commission_id, '_edd_commission_payment_id', $payment_id);
                    // If we are dealing with a variation, then save variation info
                    if ($has_variable_prices && isset($variation)) {
                        update_post_meta($commission_id, '_edd_commission_download_variation', $variation);
                    }
                    // If it's a renewal, save that detail
                    if (!empty($download['item_number']['options']['is_renewal'])) {
                        update_post_meta($commission_id, '_edd_commission_is_renewal', true);
                    }
                    do_action('eddc_insert_commission', $recipient, $commission_amount, $rate, $download_id, $commission_id, $payment_id);
                }
            }
        }
    }
}
/**
 * Display the commissions area for the customer view
 *
 * @since  3.2
 * @param  object $customer The Customer being displayed
 * @return void
 */
function eddc_customer_commissions_view($customer)
{
    ?>
	<div class="edd-item-notes-header">
		<?php 
    echo get_avatar($customer->email, 30);
    ?>
 <span><?php 
    echo $customer->name;
    ?>
</span>
	</div>

	<div id="edd-item-stats-wrapper" class="customer-section">
		<ul>
			<li>
				<span class="dashicons dashicons-chart-area"></span>
				<?php 
    echo edd_currency_filter(edd_format_amount(eddc_get_paid_totals($customer->user_id)));
    ?>
 <?php 
    _e('Paid Commissions', 'eddc');
    ?>
				<?php 
    $paid_sales = eddc_count_user_commissions($customer->user_id, 'paid');
    ?>
				<?php 
    if (!empty($paid_sales)) {
        ?>
				<br />
				<a title="<?php 
        _e('View All Paid Commissions', 'edd');
        ?>
" href="<?php 
        echo admin_url('edit.php?post_type=download&page=edd-commissions&view=paid&user='******'via %d sale', 'via %d sales', $paid_sales, 'eddc'), $paid_sales);
        ?>
				</a>
				<?php 
    }
    ?>
			</li>
			<li>
				<span class="dashicons dashicons-chart-area"></span>
				<?php 
    echo edd_currency_filter(edd_format_amount(eddc_get_unpaid_totals($customer->user_id)));
    ?>
 <?php 
    _e('Unpaid Commissions', 'eddc');
    ?>
				<?php 
    $unpaid_sales = eddc_count_user_commissions($customer->user_id, 'unpaid');
    ?>
				<?php 
    if (!empty($unpaid_sales)) {
        ?>
				<br />
				<a title="<?php 
        _e('View All Unpaid Commissions', 'edd');
        ?>
" href="<?php 
        echo admin_url('edit.php?post_type=download&page=edd-commissions&view=unpaid&user='******'via %d sale', 'via %d sales', $unpaid_sales, 'eddc'), $unpaid_sales);
        ?>
				</a>
				<?php 
    }
    ?>
			</li>
		</ul>
	</div>

	<?php 
    $downloads = eddc_get_download_ids_of_user($customer->user_id);
    ?>
	<?php 
    if (false !== $downloads) {
        ?>
	<div id="edd-item-tables-wrapper" class="customer-section">
		<h3><?php 
        printf(__('Commissioned %s', 'eddc'), edd_get_label_plural());
        ?>
</h3>

		<table class="wp-list-table widefat striped downloads">
			<thead>
				<tr>
					<th><?php 
        echo edd_get_label_singular();
        ?>
</th>
					<th><?php 
        _e('Rate', 'eddc');
        ?>
</th>
					<th width="120px"><?php 
        _e('Actions', 'eddc');
        ?>
</th>
				</tr>
			</thead>
			<tbody>
				<?php 
        if (!empty($downloads)) {
            ?>
					<?php 
            foreach ($downloads as $download) {
                ?>
						<?php 
                $download = new EDD_Download($download);
                ?>
						<?php 
                $commission_type = eddc_get_commission_type($download->ID);
                ?>
						<?php 
                $commission_rate = eddc_get_recipient_rate($download->ID, $customer->user_id);
                ?>
						<tr>
							<td><?php 
                echo $download->post_title;
                ?>
</td>
							<td>
								<?php 
                if ($commission_type === 'percentage') {
                    ?>
									<?php 
                    echo $commission_rate;
                    ?>
%
								<?php 
                } else {
                    ?>
									<?php 
                    echo edd_currency_filter(edd_format_amount($commission_rate));
                    ?>
								<?php 
                }
                ?>

							</td>
							<td>
								<a title="<?php 
                echo esc_attr(sprintf(__('View %s', 'edd'), $download->post_title));
                ?>
" href="<?php 
                echo esc_url(admin_url('post.php?action=edit&post=' . $download->ID));
                ?>
">
									<?php 
                printf(__('View %s', 'eddc'), edd_get_label_singular());
                ?>
								</a>
							</td>
						</tr>
					<?php 
            }
            ?>
				<?php 
        } else {
            ?>
					<tr><td colspan="2"><?php 
            printf(__('No %s Found', 'eddc'), edd_get_label_plural());
            ?>
</td></tr>
				<?php 
        }
        ?>
			</tbody>
		</table>

	</div>
	<?php 
    }
    ?>

	<div id="edd-item-tables-wrapper" class="customer-section">

		<h3><?php 
    _e('Recent Unpaid Commissions', 'edd');
    ?>
</h3>
		<?php 
    $args = array('user_id' => $customer->user_id, 'number' => 10);
    $commissions = eddc_get_unpaid_commissions($args);
    ?>
		<table class="wp-list-table widefat striped payments">
			<thead>
				<tr>
					<th><?php 
    _e('ID', 'edd');
    ?>
</th>
					<th><?php 
    _e('Item', 'edd');
    ?>
</th>
					<th><?php 
    _e('Amount', 'edd');
    ?>
</th>
				</tr>
			</thead>
			<tbody>
				<?php 
    if (!empty($commissions)) {
        ?>
					<?php 
        foreach ($commissions as $commission) {
            ?>
						<?php 
            $commission_meta = get_post_meta($commission->ID, '_edd_commission_info', true);
            ?>
						<tr>
							<td><?php 
            echo $commission->ID;
            ?>
</td>
							<td><?php 
            echo get_the_title(get_post_meta($commission->ID, '_download_id', true));
            ?>
</td>
							<td><?php 
            echo edd_currency_filter(edd_sanitize_amount($commission_meta['amount']));
            ?>
</td>
						</tr>
					<?php 
        }
        ?>
				<?php 
    } else {
        ?>
					<tr><td colspan="5"><?php 
        _e('No unpaid commissions', 'edd');
        ?>
</td></tr>
				<?php 
    }
    ?>
			</tbody>
		</table>

	</div>

	<?php 
}