/**
 * Given a user id, provide a detailed list of commissions
 *
 * @param  integer $user_id Given a user id, get their commissions details
 * @return string           HTML markup for the commissions details
 */
function eddc_user_commissions($user_id = 0)
{
    $user_id = empty($user_id) ? get_current_user_id() : $user_id;
    // If still empty, exit
    if (empty($user_id)) {
        return;
    }
    $per_page = 20;
    $unpaid_paged = isset($_GET['eddcup']) ? absint($_GET['eddcup']) : 1;
    $paid_paged = isset($_GET['eddcp']) ? absint($_GET['eddcp']) : 1;
    $revoked_paged = isset($_GET['eddcrp']) ? absint($_GET['eddcrp']) : 1;
    $unpaid_commissions = eddc_get_unpaid_commissions(array('user_id' => $user_id, 'number' => $per_page, 'paged' => $unpaid_paged));
    $paid_commissions = eddc_get_paid_commissions(array('user_id' => $user_id, 'number' => $per_page, 'paged' => $paid_paged));
    $revoked_commissions = eddc_get_revoked_commissions(array('user_id' => $user_id, 'number' => $per_page, 'paged' => $paid_paged));
    $total_unpaid = eddc_count_user_commissions($user_id, 'unpaid');
    $total_paid = eddc_count_user_commissions($user_id, 'paid');
    $total_revoked = eddc_count_user_commissions($user_id, 'revoked');
    $unpaid_offset = $per_page * ($unpaid_paged - 1);
    $unpaid_total_pages = ceil($total_unpaid / $per_page);
    $paid_offset = $per_page * ($paid_paged - 1);
    $paid_total_pages = ceil($total_paid / $per_page);
    $revoked_offset = $per_page * ($revoked_paged - 1);
    $revoked_total_pages = ceil($total_revoked / $per_page);
    $page_prefix = false !== strpos(edd_get_current_page_url(), '?') ? '&' : '?';
    $stats = '';
    if (eddc_user_has_commissions($user_id)) {
        // only show tables if user has commission data
        ob_start();
        ?>
			<div id="edd_user_commissions">

				<!-- unpaid -->
				<div id="edd_user_commissions_unpaid">
					<h3 class="edd_user_commissions_header"><?php 
        _e('Unpaid Commissions', 'eddc');
        ?>
</h3>
					<table id="edd_user_unpaid_commissions_table" class="edd_user_commissions">
						<thead>
							<tr class="edd_user_commission_row">
								<?php 
        do_action('eddc_user_commissions_unpaid_head_row_begin');
        ?>
								<th class="edd_commission_item"><?php 
        _e('Item', 'eddc');
        ?>
</th>
								<th class="edd_commission_amount"><?php 
        _e('Amount', 'eddc');
        ?>
</th>
								<th class="edd_commission_rate"><?php 
        _e('Rate', 'eddc');
        ?>
</th>
								<th class="edd_commission_date"><?php 
        _e('Date', 'eddc');
        ?>
</th>
								<?php 
        do_action('eddc_user_commissions_unpaid_head_row_end');
        ?>
							</tr>
						</thead>
						<tbody>
						<?php 
        $total = (double) 0;
        ?>
						<?php 
        if (!empty($unpaid_commissions)) {
            ?>
							<?php 
            foreach ($unpaid_commissions as $commission) {
                ?>
								<tr class="edd_user_commission_row">
									<?php 
                do_action('eddc_user_commissions_unpaid_row_begin', $commission);
                $item_name = get_the_title(get_post_meta($commission->ID, '_download_id', true));
                $commission_info = get_post_meta($commission->ID, '_edd_commission_info', true);
                $amount = $commission_info['amount'];
                $rate = $commission_info['rate'];
                ?>
									<td class="edd_commission_item"><?php 
                echo esc_html($item_name);
                ?>
</td>
									<td class="edd_commission_amount">
										<?php 
                echo edd_currency_filter(edd_format_amount(edd_sanitize_amount($amount)));
                ?>
										<?php 
                if (eddc_commission_is_renewal($commission->ID)) {
                    ?>
											&nbsp;&olarr;
										<?php 
                }
                ?>
									</td>
									<td class="edd_commission_rate"><?php 
                echo $rate . '%';
                ?>
</td>
									<td class="edd_commission_date"><?php 
                echo date_i18n(get_option('date_format'), strtotime($commission->post_date));
                ?>
</td>
									<?php 
                do_action('eddc_user_commissions_unpaid_row_end', $commission);
                ?>
								</tr>
							<?php 
            }
            ?>
						<?php 
        } else {
            ?>
							<tr class="edd_user_commission_row edd_row_empty">
								<td colspan="4"><?php 
            _e('No unpaid commissions', 'eddc');
            ?>
</td>
							</tr>
						<?php 
        }
        ?>
						</tbody>
					</table>
					<div id="edd_user_commissions_unpaid_total"><?php 
        _e('Total unpaid:', 'eddc');
        ?>
&nbsp;<?php 
        echo edd_currency_filter(edd_format_amount(eddc_get_unpaid_totals($user_id)));
        ?>
</div>

					<div id="edd_commissions_unpaid_pagination" class="navigation">
					<?php 
        $big = 999999;
        $search_for = array($big, '#038;');
        $replace_with = array('%#%', '&');
        echo paginate_links(array('base' => remove_query_arg('eddcup', str_replace($search_for, $replace_with, edd_get_current_page_url())) . '%_%', 'format' => $page_prefix . 'eddcup=%#%', 'current' => max(1, $unpaid_paged), 'total' => $unpaid_total_pages));
        ?>
					</div>

				</div><!--end #edd_user_commissions_unpaid-->

				<!-- paid -->
				<div id="edd_user_commissions_paid">
					<h3 class="edd_user_commissions_header"><?php 
        _e('Paid Commissions', 'eddc');
        ?>
</h3>
					<table id="edd_user_paid_commissions_table" class="edd_user_commissions">
						<thead>
							<tr class="edd_user_commission_row">
								<?php 
        do_action('eddc_user_commissions_paid_head_row_begin');
        ?>
								<th class="edd_commission_item"><?php 
        _e('Item', 'eddc');
        ?>
</th>
								<th class="edd_commission_amount"><?php 
        _e('Amount', 'eddc');
        ?>
</th>
								<th class="edd_commission_rate"><?php 
        _e('Rate', 'eddc');
        ?>
</th>
								<th class="edd_commission_date"><?php 
        _e('Date', 'eddc');
        ?>
</th>
								<?php 
        do_action('eddc_user_commissions_paid_head_row_end');
        ?>
							</tr>
						</thead>
						<tbody>
						<?php 
        $total = (double) 0;
        ?>
						<?php 
        if (!empty($paid_commissions)) {
            ?>
							<?php 
            foreach ($paid_commissions as $commission) {
                ?>
								<tr class="edd_user_commission_row">
									<?php 
                do_action('eddc_user_commissions_paid_row_begin', $commission);
                $item_name = get_the_title(get_post_meta($commission->ID, '_download_id', true));
                $commission_info = get_post_meta($commission->ID, '_edd_commission_info', true);
                $amount = $commission_info['amount'];
                $rate = $commission_info['rate'];
                ?>
									<td class="edd_commission_item"><?php 
                echo esc_html($item_name);
                ?>
</td>
									<td class="edd_commission_amount">
										<?php 
                echo edd_currency_filter(edd_format_amount(edd_sanitize_amount($amount)));
                ?>
										<?php 
                if (eddc_commission_is_renewal($commission->ID)) {
                    ?>
											&nbsp;&olarr;
										<?php 
                }
                ?>
									</td>
									<td class="edd_commission_rate"><?php 
                echo $rate . '%';
                ?>
</td>
									<td class="edd_commission_date"><?php 
                echo date_i18n(get_option('date_format'), strtotime($commission->post_date));
                ?>
</td>
									<?php 
                do_action('eddc_user_commissions_paid_row_end', $commission);
                ?>
								</tr>
							<?php 
            }
            ?>
						<?php 
        } else {
            ?>
							<tr class="edd_user_commission_row edd_row_empty">
								<td colspan="4"><?php 
            _e('No paid commissions', 'eddc');
            ?>
</td>
							</tr>
						<?php 
        }
        ?>
						</tbody>
					</table>
					<div id="edd_user_commissions_paid_total"><?php 
        _e('Total paid:', 'eddc');
        ?>
&nbsp;<?php 
        echo edd_currency_filter(edd_format_amount(eddc_get_paid_totals($user_id)));
        ?>
</div>

					<div id="edd_commissions_paid_pagination" class="navigation">
					<?php 
        $big = 999999;
        $search_for = array($big, '#038;');
        $replace_with = array('%#%', '&');
        echo paginate_links(array('base' => remove_query_arg('eddcp', str_replace($search_for, $replace_with, edd_get_current_page_url())) . '%_%', 'format' => $page_prefix . 'eddcp=%#%', 'current' => max(1, $paid_paged), 'total' => $paid_total_pages));
        ?>
					</div>

				</div><!--end #edd_user_commissions_paid-->

				<!-- revoked -->
				<div id="edd_user_commissions_revoked">
					<h3 class="edd_user_commissions_header"><?php 
        _e('Revoked Commissions', 'eddc');
        ?>
</h3>
					<table id="edd_user_revoked_commissions_table" class="edd_user_commissions">
						<thead>
							<tr class="edd_user_commission_row">
								<?php 
        do_action('eddc_user_commissions_revoked_head_row_begin');
        ?>
								<th class="edd_commission_item"><?php 
        _e('Item', 'eddc');
        ?>
</th>
								<th class="edd_commission_amount"><?php 
        _e('Amount', 'eddc');
        ?>
</th>
								<th class="edd_commission_rate"><?php 
        _e('Rate', 'eddc');
        ?>
</th>
								<th class="edd_commission_date"><?php 
        _e('Date', 'eddc');
        ?>
</th>
								<?php 
        do_action('eddc_user_commissions_revoked_head_row_end');
        ?>
							</tr>
						</thead>
						<tbody>
						<?php 
        $total = (double) 0;
        ?>
						<?php 
        if (!empty($revoked_commissions)) {
            ?>
							<?php 
            foreach ($revoked_commissions as $commission) {
                ?>
								<tr class="edd_user_commission_row">
									<?php 
                do_action('eddc_user_commissions_revoked_row_begin', $commission);
                $item_name = get_the_title(get_post_meta($commission->ID, '_download_id', true));
                $commission_info = get_post_meta($commission->ID, '_edd_commission_info', true);
                $amount = $commission_info['amount'];
                $rate = $commission_info['rate'];
                ?>
									<td class="edd_commission_item"><?php 
                echo esc_html($item_name);
                ?>
</td>
									<td class="edd_commission_amount">
										<?php 
                echo edd_currency_filter(edd_format_amount(edd_sanitize_amount($amount)));
                ?>
										<?php 
                if (eddc_commission_is_renewal($commission->ID)) {
                    ?>
											&nbsp;&olarr;
										<?php 
                }
                ?>
									</td>
									<td class="edd_commission_rate"><?php 
                echo $rate . '%';
                ?>
</td>
									<td class="edd_commission_date"><?php 
                echo date_i18n(get_option('date_format'), strtotime($commission->post_date));
                ?>
</td>
									<?php 
                do_action('eddc_user_commissions_revoked_row_end', $commission);
                ?>
								</tr>
							<?php 
            }
            ?>
						<?php 
        } else {
            ?>
							<tr class="edd_user_commission_row edd_row_empty">
								<td colspan="4"><?php 
            _e('No revoked commissions', 'eddc');
            ?>
</td>
							</tr>
						<?php 
        }
        ?>
						</tbody>
					</table>
					<div id="edd_user_commissions_revoked_total"><?php 
        _e('Total revoked:', 'eddc');
        ?>
&nbsp;<?php 
        echo edd_currency_filter(edd_format_amount(eddc_get_revoked_totals($user_id)));
        ?>
</div>

					<div id="edd_commissions_revoked_pagination" class="navigation">
					<?php 
        $big = 999999;
        $search_for = array($big, '#038;');
        $replace_with = array('%#%', '&');
        echo paginate_links(array('base' => remove_query_arg('eddcrp', str_replace($search_for, $replace_with, edd_get_current_page_url())) . '%_%', 'format' => $page_prefix . 'eddcrp=%#%', 'current' => max(1, $revoked_paged), 'total' => $revoked_total_pages));
        ?>
					</div>

				</div><!--end #edd_user_commissions_revoked-->

				<?php 
        if (!empty($total_paid)) {
            ?>
				<div id="edd_commissions_export">
					<?php 
            $args = array('user_id' => $user_id, 'number' => 1, 'orderby' => 'date', 'order' => 'ASC');
            $first_commission = eddc_get_paid_commissions($args);
            $first_year = date('Y', strtotime($first_commission[0]->post_date));
            $years_back = date('Y', current_time('timestamp')) - $first_year;
            $url = is_admin() ? admin_url('index.php') : home_url();
            ?>
					<h3><?php 
            _e('Export Paid Commissions', 'eddc');
            ?>
</h3>
					<form method="post" action="<?php 
            echo $url;
            ?>
">
						<?php 
            echo EDD()->html->month_dropdown();
            ?>
						<?php 
            echo EDD()->html->year_dropdown('year', 0, $years_back, 0);
            ?>
						<input type="hidden" name="user_id" value="<?php 
            echo $user_id;
            ?>
"/>
						<input type="hidden" name="edd_action" value="generate_commission_export"/>
						<input type="submit" class="edd-submit button" value="<?php 
            _e('Download CSV', 'eddc');
            ?>
"/>
					</form>
				</div>
				<?php 
        }
        ?>


			</div><!--end #edd_user_commissions-->
		<?php 
        $stats = apply_filters('edd_user_commissions_display', ob_get_clean());
    }
    return $stats;
}
Beispiel #2
0
/**
 * Commissions Summary Dashboard Widget
 *
 * @access      private
 * @since       1.6
*/
function eddc_dashboard_commissions_widget()
{
    global $user_ID;
    $per_page = 20;
    $unpaid_paged = isset($_GET['eddcup']) ? absint($_GET['eddcup']) : 1;
    $paid_paged = isset($_GET['eddcp']) ? absint($_GET['eddcp']) : 1;
    $unpaid_commissions = eddc_get_unpaid_commissions(array('user_id' => $user_ID, 'number' => $per_page, 'paged' => $unpaid_paged));
    $paid_commissions = eddc_get_paid_commissions(array('user_id' => $user_ID, 'number' => $per_page, 'paged' => $paid_paged));
    $total_unpaid = eddc_count_user_commissions($user_ID, 'unpaid');
    $total_paid = eddc_count_user_commissions($user_ID, 'paid');
    $unpaid_offset = $per_page * ($unpaid_paged - 1);
    $unpaid_total_pages = ceil($total_unpaid / $per_page);
    $paid_offset = $per_page * ($paid_paged - 1);
    $paid_total_pages = ceil($total_paid / $per_page);
    $stats = '';
    if (!empty($unpaid_commissions) || !empty($paid_commissions)) {
        // only show tables if user has commission data
        ob_start();
        ?>
			<div id="edd_user_commissions" class="edd_dashboard_widget">
				<style>#edd_user_commissions_unpaid { margin-top: 30px; }#edd_user_commissions_unpaid_total,#edd_user_commissions_paid_total { padding-bottom: 20px; } .edd_user_commissions { width: 100%; margin: 0 0 20px; }.edd_user_commissions th, .edd_user_commissions td { text-align:left; padding: 4px 4px 4px 0; }</style>
				<!-- unpaid -->
				<div id="edd_user_commissions_unpaid" class="table">
					<p class="edd_user_commissions_header sub"><?php 
        _e('Unpaid Commissions', 'eddc');
        ?>
</p>
					<table id="edd_user_unpaid_commissions_table" class="edd_user_commissions">
						<thead>
							<tr class="edd_user_commission_row">
								<th class="edd_commission_item"><?php 
        _e('Item', 'eddc');
        ?>
</th>
								<th class="edd_commission_amount"><?php 
        _e('Amount', 'eddc');
        ?>
</th>
								<th class="edd_commission_rate"><?php 
        _e('Rate', 'eddc');
        ?>
</th>
								<th class="edd_commission_date"><?php 
        _e('Date', 'eddc');
        ?>
</th>
							</tr>
						</thead>
						<tbody>
						<?php 
        $total = (double) 0;
        ?>
						<?php 
        if (!empty($unpaid_commissions)) {
            ?>
							<?php 
            foreach ($unpaid_commissions as $commission) {
                ?>
								<tr class="edd_user_commission_row">
									<?php 
                $item_name = get_the_title(get_post_meta($commission->ID, '_download_id', true));
                $commission_info = get_post_meta($commission->ID, '_edd_commission_info', true);
                $amount = $commission_info['amount'];
                $rate = $commission_info['rate'];
                $total += $amount;
                ?>
									<td class="edd_commission_item"><?php 
                echo esc_html($item_name);
                ?>
</td>
									<td class="edd_commission_amount">
										<?php 
                echo edd_currency_filter(edd_format_amount(edd_sanitize_amount($amount)));
                ?>
										<?php 
                if (eddc_commission_is_renewal($commission->ID)) {
                    ?>
											&nbsp;&olarr;
										<?php 
                }
                ?>
									</td>
									<td class="edd_commission_rate"><?php 
                echo $rate . '%';
                ?>
</td>
									<td class="edd_commission_date"><?php 
                echo date_i18n(get_option('date_format'), strtotime($commission->post_date));
                ?>
</td>
								</tr>
							<?php 
            }
            ?>
						<?php 
        } else {
            ?>
							<tr class="edd_user_commission_row edd_row_empty">
								<td colspan="4"><?php 
            _e('No unpaid commissions', 'eddc');
            ?>
</td>
							</tr>
						<?php 
        }
        ?>
						</tbody>
					</table>

					<div id="edd_user_commissions_unpaid_total"><?php 
        _e('Total unpaid:', 'eddc');
        ?>
&nbsp;<?php 
        echo edd_currency_filter(eddc_get_unpaid_totals($user_ID));
        ?>
</div>

					<div id="edd_commissions_unpaid_pagination" class="navigation" style="padding: 0 0 15px;">
					<?php 
        $big = 999999;
        echo paginate_links(array('base' => admin_url() . '%_%#edd_user_commissions_unpaid', 'format' => '?eddcup=%#%', 'current' => max(1, $unpaid_paged), 'total' => $unpaid_total_pages));
        ?>
					</div>

				</div><!--end #edd_user_commissions_unpaid-->

				<!-- paid -->
				<div id="edd_user_commissions_paid" class="table">
					<p class="edd_user_commissions_header sub"><?php 
        _e('Paid Commissions', 'eddc');
        ?>
</p>
					<table id="edd_user_paid_commissions_table" class="edd_user_commissions">
						<thead>
							<tr class="edd_user_commission_row">
								<th class="edd_commission_item"><?php 
        _e('Item', 'eddc');
        ?>
</th>
								<th class="edd_commission_amount"><?php 
        _e('Amount', 'eddc');
        ?>
</th>
								<th class="edd_commission_rate"><?php 
        _e('Rate', 'eddc');
        ?>
</th>
								<th class="edd_commission_date"><?php 
        _e('Date', 'eddc');
        ?>
</th>
							</tr>
						</thead>
						<tbody>
						<?php 
        $total = (double) 0;
        ?>
						<?php 
        if (!empty($paid_commissions)) {
            ?>
							<?php 
            foreach ($paid_commissions as $commission) {
                ?>
								<tr class="edd_user_commission_row">
									<?php 
                $item_name = get_the_title(get_post_meta($commission->ID, '_download_id', true));
                $commission_info = get_post_meta($commission->ID, '_edd_commission_info', true);
                $amount = $commission_info['amount'];
                $rate = $commission_info['rate'];
                $total += $amount;
                ?>
									<td class="edd_commission_item"><?php 
                echo esc_html($item_name);
                ?>
</td>
									<td class="edd_commission_amount">
										<?php 
                echo edd_currency_filter(edd_format_amount(edd_sanitize_amount($amount)));
                ?>
										<?php 
                if (eddc_commission_is_renewal($commission->ID)) {
                    ?>
											&nbsp;&olarr;
										<?php 
                }
                ?>
									</td>
									<td class="edd_commission_rate"><?php 
                echo $rate . '%';
                ?>
</td>
									<td class="edd_commission_date"><?php 
                echo date_i18n(get_option('date_format'), strtotime($commission->post_date));
                ?>
</td>
								</tr>
							<?php 
            }
            ?>
						<?php 
        } else {
            ?>
							<tr class="edd_user_commission_row edd_row_empty">
								<td colspan="4"><?php 
            _e('No paid commissions', 'eddc');
            ?>
</td>
							</tr>
						<?php 
        }
        ?>
						</tbody>
					</table>

					<div id="edd_user_commissions_paid_total"><?php 
        _e('Total paid:', 'eddc');
        ?>
&nbsp;<?php 
        echo edd_currency_filter(eddc_get_paid_totals($user_ID));
        ?>
</div>

					<div id="edd_commissions_paid_pagination" class="navigation" style="padding: 0 0 15px;">
					<?php 
        $big = 999999;
        echo paginate_links(array('base' => admin_url() . '%_%#edd_user_commissions_paid', 'format' => '?eddcp=%#%', 'current' => max(1, $paid_paged), 'total' => $paid_total_pages));
        ?>
					</div>
				</div><!--end #edd_user_commissions_paid-->

				<div id"edd_commissions_export">
					<p><strong><?php 
        _e('Export Paid Commissions', 'eddc');
        ?>
</strong></p>
					<form method="post" action="<?php 
        echo admin_url('index.php');
        ?>
">
						<?php 
        echo EDD()->html->month_dropdown();
        ?>
						<?php 
        echo EDD()->html->year_dropdown();
        ?>
						<input type="hidden" name="edd_action" value="generate_commission_export"/>
						<input type="submit" class="button-secondary" value="<?php 
        _e('Download CSV', 'eddc');
        ?>
"/>
					</form>
				</div>
			</div><!--end #edd_user_commissions-->
		<?php 
        $stats = ob_get_clean();
    }
    echo $stats;
}