/**
  * Get the Export Data
  *
  * @access	public
  * @since	1.4
  * @return	arr		$data	The data for the CSV file
  */
 public function get_data()
 {
     $start_year = isset($_POST['start_year']) ? absint($_POST['start_year']) : date('Y');
     $end_year = isset($_POST['end_year']) ? absint($_POST['end_year']) : date('Y');
     $start_month = isset($_POST['start_month']) ? absint($_POST['start_month']) : date('n');
     $end_month = isset($_POST['end_month']) ? absint($_POST['end_month']) : date('n');
     $data = array();
     $year = $start_year;
     $stats = new MDJM_Stats();
     while ($year <= $end_year) {
         if ($year == $start_year && $year == $end_year) {
             $m1 = $start_month;
             $m2 = $end_month;
         } elseif ($year == $start_year) {
             $m1 = $start_month;
             $m2 = 12;
         } elseif ($year == $end_year) {
             $m1 = 1;
             $m2 = $end_month;
         } else {
             $m1 = 1;
             $m2 = 12;
         }
         while ($m1 <= $m2) {
             $date1 = mktime(0, 0, 0, $m1, 1, $year);
             $date2 = mktime(0, 0, 0, $m1, cal_days_in_month(CAL_GREGORIAN, $m1, $year), $year);
             $event_status = array_keys(mdjm_all_event_status());
             $data[] = array('date' => date_i18n('F Y', $date1), 'earnings' => mdjm_format_amount($stats->get_earnings($m1, $year)));
             $m1++;
         }
         $year++;
     }
     $data = apply_filters('mdjm_export_get_data', $data);
     $data = apply_filters('mdjm_export_get_data_' . $this->export_type, $data);
     return $data;
 }
 /**
  * Build all the reports data
  *
  * @access	public
  * @since	1.4
  * @return	arr		$reports_data	All the data for customer reports
  */
 public function reports_data()
 {
     $stats = new MDJM_Stats();
     $dates = mdjm_get_report_dates();
     $stats->setup_dates($dates['range']);
     $cached_reports = false;
     if (false !== $cached_reports) {
         $reports_data = $cached_reports;
     } else {
         $reports_data = array();
         $term_args = array('parent' => 0, 'hierarchical' => 0);
         $employees = mdjm_get_employees();
         foreach ($employees as $employee) {
             $event_count = 0;
             $employee_id = $employee->ID;
             $event_args = array('post_status' => apply_filters('mdjm_events_by_type_statuses', array('mdjm-contract', 'mdjm-approved', 'mdjm-completed')), 'fields' => 'ids', 'meta_query' => array('relation' => 'AND', array('key' => '_mdjm_event_date', 'value' => array(date('Y-m-d', $stats->start_date), date('Y-m-d', $stats->end_date)), 'type' => 'date', 'compare' => 'BETWEEN'), array('relation' => 'OR', array('key' => '_mdjm_event_dj', 'value' => $employee_id, 'compare' => '=', 'type' => 'numeric'), array('key' => '_mdjm_event_employees', 'value' => sprintf(':"%s";', $employee_id), 'compare' => 'LIKE'))));
             $wages = 0.0;
             $paid = 0.0;
             $owed = 0.0;
             $events = mdjm_get_events($event_args);
             if ($events) {
                 foreach ($events as $event) {
                     $event_count++;
                     $payment_status = mdjm_event_employees_paid($event, $employee_id);
                     $current_wages = mdjm_get_employees_event_wage($event, $employee_id);
                     $current_paid_wages = 0;
                     $current_owed_wages = 0;
                     $wages += $current_wages;
                     $paid += $payment_status ? $current_wages : 0.0;
                     $owed += $payment_status ? 0.0 : $current_wages;
                 }
             } else {
                 continue;
             }
             $reports_data[] = array('ID' => $employee->ID, 'employee' => $employee->display_name, 'total_events' => $event_count, 'total_wages' => mdjm_currency_filter(mdjm_format_amount($wages)), 'total_wages_raw' => $wages, 'wages_paid' => mdjm_currency_filter(mdjm_format_amount($paid)), 'wages_owed' => mdjm_currency_filter(mdjm_format_amount($owed)), 'is_child' => false);
         }
     }
     return $reports_data;
 }
 /**
  * Build all the reports data
  *
  * @access	public
  * @since	1.4
  * @return	arr		$reports_data	All the data for customer reports
  */
 public function reports_data()
 {
     $stats = new MDJM_Stats();
     $dates = mdjm_get_report_dates();
     $stats->setup_dates($dates['range']);
     $cached_reports = false;
     if (false !== $cached_reports) {
         $reports_data = $cached_reports;
     } else {
         $reports_data = array();
         $term_args = array('parent' => 0, 'hierarchical' => 0);
         $packages = mdjm_get_packages();
         if ($packages) {
             foreach ($packages as $package) {
                 $event_count = 0;
                 $total_value = 0;
                 $event_args = array('fields' => 'ids', 'meta_query' => array('relation' => 'AND', array('key' => '_mdjm_event_package', 'value' => $package->ID, 'type' => 'NUMERIC'), array('key' => '_mdjm_event_date', 'value' => array(date('Y-m-d', $stats->start_date), date('Y-m-d', $stats->end_date)), 'type' => 'date', 'compare' => 'BETWEEN')));
                 $events = mdjm_get_events($event_args);
                 if ($events) {
                     foreach ($events as $event) {
                         $event_count++;
                         $event_date = get_post_meta($event, '_mdjm_event_date', true);
                         $total_value += mdjm_get_package_price($package->ID, $event_date);
                     }
                 } else {
                     continue;
                 }
                 $reports_data[] = array('ID' => $package->ID, 'package' => mdjm_get_package_name($package->ID), 'events' => $event_count, 'value' => mdjm_currency_filter(mdjm_format_amount($total_value)), 'value_raw' => $total_value);
             }
         }
     }
     return $reports_data;
 }
/**
 * Generate and display the content for the Events Overview dashboard widget.
 *
 * @since	1.3
 * @param
 * @return
 */
function mdjm_widget_events_overview()
{
    global $current_user;
    if (mdjm_employee_can('manage_mdjm')) {
        $stats = new MDJM_Stats();
        $enquiry_counts = array('month' => 0, 'this_year' => 0, 'last_year' => 0);
        $conversion_counts = array('month' => 0, 'this_year' => 0, 'last_year' => 0);
        $enquiry_periods = array('month' => date('Y-m-01'), 'this_year' => date('Y-01-01'), 'last_year' => date('Y-01-01', strtotime('-1 year')));
        foreach ($enquiry_periods as $period => $date) {
            $current_count = mdjm_count_events(array('start-date' => $date, 'end-date' => $period != 'last_year' ? date('Y-m-d') : date('Y-12-31', strtotime('-1 year'))));
            foreach ($current_count as $status => $count) {
                $enquiry_counts[$period] += $count;
                if (in_array($status, array('mdjm-approved', 'mdjm-contract', 'mdjm-completed', 'mdjm-cancelled'))) {
                    $conversion_counts[$period] += $count;
                }
            }
        }
        $completed_counts = array('month' => 0, 'this_year' => 0, 'last_year' => 0);
        $event_periods = array('month' => array(date('Y-m-01'), date('Y-m-d')), 'this_year' => array(date('Y-01-01'), date('Y-m-d')), 'last_year' => array(date('Y-m-01', strtotime('-1 year')), date('Y-12-31', strtotime('-1 year'))));
        foreach ($event_periods as $period => $date) {
            $current_count = mdjm_count_events(array('date' => $date, 'status' => 'mdjm-completed'));
            foreach ($current_count as $status => $count) {
                $completed_counts[$period] += $count;
            }
        }
        $income_month = $stats->get_income_by_date(null, date('n'), date('Y'));
        $income_year = $stats->get_income_by_date(null, '', date('Y'));
        $income_last = $stats->get_income_by_date(null, '', date('Y') - 1);
        $expense_month = $stats->get_expenses_by_date(null, date('n'), date('Y'));
        $expense_year = $stats->get_expenses_by_date(null, '', date('Y'));
        $expense_last = $stats->get_expenses_by_date(null, '', date('Y') - 1);
        $earnings_month = $income_month - $expense_month;
        $earnings_year = $income_year - $expense_year;
        $earnings_last = $income_last - $expense_last;
        ?>
		<div class="mdjm_stat_grid">
        	<?php 
        do_action('mdjm_before_events_overview');
        ?>
			<table>
				<thead>
					<tr>
						<th>&nbsp;</th>
						<th><?php 
        _e('MTD', 'mobile-dj-manager');
        ?>
</th>
						<th><?php 
        _e('YTD', 'mobile-dj-manager');
        ?>
</th>
						<th><?php 
        echo date('Y', strtotime('-1 year'));
        ?>
</th>
					</tr>
				</thead>
				<tbody>
					<tr>
						<th><?php 
        printf(__('%s Received', 'mobile-dj-manager'), get_post_status_object('mdjm-enquiry')->plural);
        ?>
</th>
						<td><?php 
        echo $enquiry_counts['month'];
        ?>
</td>
						<td><?php 
        echo $enquiry_counts['this_year'];
        ?>
</td>
						<td><?php 
        echo $enquiry_counts['last_year'];
        ?>
</td>
					</tr>
					<tr>
						<th><?php 
        printf(__('%s Converted', 'mobile-dj-manager'), get_post_status_object('mdjm-enquiry')->plural);
        ?>
</th>
						<td><?php 
        echo $conversion_counts['month'];
        ?>
</td>
						<td><?php 
        echo $conversion_counts['this_year'];
        ?>
</td>
						<td><?php 
        echo $conversion_counts['last_year'];
        ?>
</td>
					</tr>
					<tr>
						<th><?php 
        printf(__('%s Completed', 'mobile-dj-manager'), mdjm_get_label_plural());
        ?>
</th>
						<td><?php 
        echo $completed_counts['month'];
        ?>
</td>
						<td><?php 
        echo $completed_counts['this_year'];
        ?>
</td>
						<td><?php 
        echo $completed_counts['last_year'];
        ?>
</td>
					</tr>
					<tr>
						<th><?php 
        _e('Income', 'mobile-dj-manager');
        ?>
</th>
						<td><?php 
        echo mdjm_currency_filter(mdjm_format_amount($income_month));
        ?>
</td>
						<td><?php 
        echo mdjm_currency_filter(mdjm_format_amount($income_year));
        ?>
</td>
						<td><?php 
        echo mdjm_currency_filter(mdjm_format_amount($income_last));
        ?>
</td>
					</tr>
					<tr>
						<th><?php 
        _e('Outgoings', 'mobile-dj-manager');
        ?>
</th>
						<td><?php 
        echo mdjm_currency_filter(mdjm_format_amount($expense_month));
        ?>
</td>
						<td><?php 
        echo mdjm_currency_filter(mdjm_format_amount($expense_year));
        ?>
</td>
						<td><?php 
        echo mdjm_currency_filter(mdjm_format_amount($expense_last));
        ?>
</td>
					</tr>
					<tr>
						<th><?php 
        _e('Earnings', 'mobile-dj-manager');
        ?>
</th>
						<td><?php 
        echo mdjm_currency_filter(mdjm_format_amount($earnings_month));
        ?>
</td>
						<td><?php 
        echo mdjm_currency_filter(mdjm_format_amount($earnings_year));
        ?>
</td>
						<td><?php 
        echo mdjm_currency_filter(mdjm_format_amount($earnings_last));
        ?>
</td>
					</tr>
				</tbody>
			</table>
			
			<p>
				<?php 
        printf(__('<a href="%s">Create %s</a>', 'mobile-dj-manager'), admin_url('post-new.php?post_type=mdjm-event'), mdjm_get_label_singular());
        ?>
				&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
				<?php 
        printf(__('<a href="%s">Manage %s</a>', 'mobile-dj-manager'), admin_url('edit.php?post_type=mdjm-event'), mdjm_get_label_plural());
        ?>
				&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
				<?php 
        printf(__('<a href="%s">Transactions</a>', 'mobile-dj-manager'), admin_url('edit.php?post_type=mdjm-transaction'));
        ?>
				&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
				<?php 
        printf(__('<a href="%s">Settings</a>', 'mobile-dj-manager'), admin_url('admin.php?page=mdjm-settings'));
        ?>
            </p>

			<?php 
        $sources = $stats->get_enquiry_sources_by_date('this_month');
        ?>

			<?php 
        if (!empty($sources)) {
            ?>
				
				<?php 
            foreach ($sources as $count => $source) {
                ?>
					<p>
					 <?php 
                printf(__('<p>Most enquiries have been received via <strong>%s (%d)</strong> so far this month.', 'mobile-dj-manager'), $source, (int) $count);
                ?>
					</p>
				<?php 
            }
            ?>
				
			<?php 
        } else {
            ?>
				<p><?php 
            _e('No enquiries yet this month.', 'mobile-dj-manager');
            ?>
</p>
			<?php 
        }
        ?>

			<?php 
        do_action('mdjm_after_events_overview');
        ?>
			
		</div>
    
		<?php 
    }
}
/**
 * Show report graphs for earnings.
 *
 * @since	1.4
 * @return	void
*/
function mdjm_transactions_reports_graph()
{
    // Retrieve the queried dates
    $dates = mdjm_get_report_dates();
    $stats = new MDJM_Stats();
    // Determine graph options
    switch ($dates['range']) {
        case 'today':
        case 'yesterday':
            $day_by_day = true;
            break;
        case 'last_year':
        case 'this_year':
            $day_by_day = false;
            break;
        case 'last_quarter':
        case 'this_quarter':
            $day_by_day = true;
            break;
        case 'other':
            if ($dates['m_end'] - $dates['m_start'] >= 3 || $dates['year_end'] > $dates['year'] && $dates['m_start'] - $dates['m_end'] != 10) {
                $day_by_day = false;
            } else {
                $day_by_day = true;
            }
            break;
        default:
            $day_by_day = true;
            break;
    }
    $income_totals = 0.0;
    // Total income for time period shown
    $expense_totals = 0.0;
    // Total expense for time period shown
    $events_totals = 0;
    // Total events for the time period shown
    if ($dates['range'] == 'today' || $dates['range'] == 'yesterday') {
        // Hour by hour
        $hour = 1;
        $month = $dates['m_start'];
        while ($hour <= 23) {
            $income = $stats->get_income_by_date($dates['day'], $month, $dates['year'], $hour);
            $expense = $stats->get_expenses_by_date($dates['day'], $month, $dates['year'], $hour);
            $events = $stats->get_events_by_date($dates['day'], $month, $dates['year'], $hour);
            $income_totals += $income;
            $expense_totals += $expense;
            $events_totals += $events;
            $date = mktime($hour, 0, 0, $month, $dates['day'], $dates['year']) * 1000;
            $income_data[] = array($date, $income);
            $expense_data[] = array($date, $expense);
            $events_data[] = array($date, $events);
            $hour++;
        }
    } elseif ($dates['range'] == 'this_week' || $dates['range'] == 'last_week') {
        $num_of_days = cal_days_in_month(CAL_GREGORIAN, $dates['m_start'], $dates['year']);
        $report_dates = array();
        $i = 0;
        while ($i <= 6) {
            if ($dates['day'] + $i <= $num_of_days) {
                $report_dates[$i] = array('day' => (string) $dates['day'] + $i, 'month' => $dates['m_start'], 'year' => $dates['year']);
            } else {
                $report_dates[$i] = array('day' => (string) $i, 'month' => $dates['m_end'], 'year' => $dates['year_end']);
            }
            $i++;
        }
        foreach ($report_dates as $report_date) {
            $income = $stats->get_income_by_date($report_date['day'], $report_date['month'], $report_date['year']);
            $income_totals += $income;
            $expense = $stats->get_expenses_by_date($report_date['day'], $report_date['month'], $report_date['year']);
            $expense_totals += $expense;
            $events = $stats->get_events_by_date($report_date['day'], $report_date['month'], $report_date['year']);
            $events_totals += $events;
            $date = mktime(0, 0, 0, $report_date['month'], $report_date['day'], $report_date['year']) * 1000;
            $income_data[] = array($date, $income);
            $expense_data[] = array($date, $expense);
            $events_data[] = array($date, $events);
        }
    } else {
        $y = $dates['year'];
        $temp_data = array('income' => array(), 'expense' => array());
        while ($y <= $dates['year_end']) {
            $last_year = false;
            if ($dates['year'] == $dates['year_end']) {
                $month_start = $dates['m_start'];
                $month_end = $dates['m_end'];
                $last_year = true;
            } elseif ($y == $dates['year']) {
                $month_start = $dates['m_start'];
                $month_end = 12;
            } elseif ($y == $dates['year_end']) {
                $month_start = 1;
                $month_end = $dates['m_end'];
            } else {
                $month_start = 1;
                $month_end = 12;
            }
            $i = $month_start;
            while ($i <= $month_end) {
                $d = $dates['day'];
                if ($i == $month_end) {
                    $num_of_days = $dates['day_end'];
                    if ($month_start < $month_end) {
                        $d = 1;
                    }
                } else {
                    $num_of_days = cal_days_in_month(CAL_GREGORIAN, $i, $y);
                }
                while ($d <= $num_of_days) {
                    $income = $stats->get_income_by_date($d, $i, $y);
                    $income_totals += $income;
                    $expense = $stats->get_expenses_by_date($d, $i, $y);
                    $expense_totals += $expense;
                    $events = $stats->get_events_by_date($d, $i, $y);
                    $events_totals += $events;
                    $temp_data['income'][$y][$i][$d] = $income;
                    $temp_data['expense'][$y][$i][$d] = $expense;
                    $temp_data['events'][$y][$i][$d] = $events;
                    $d++;
                }
                $i++;
            }
            $y++;
        }
        $income_data = array();
        $expense_data = array();
        // When using 3 months or smaller as the custom range, show each day individually on the graph
        if ($day_by_day) {
            foreach ($temp_data['income'] as $year => $months) {
                foreach ($months as $month => $days) {
                    foreach ($days as $day => $income) {
                        $date = mktime(0, 0, 0, $month, $day, $year) * 1000;
                        $income_data[] = array($date, $income);
                    }
                }
            }
            foreach ($temp_data['expense'] as $year => $months) {
                foreach ($months as $month => $days) {
                    foreach ($days as $day => $expense) {
                        $date = mktime(0, 0, 0, $month, $day, $year) * 1000;
                        $expense_data[] = array($date, $expense);
                    }
                }
            }
            foreach ($temp_data['events'] as $year => $months) {
                foreach ($months as $month => $days) {
                    foreach ($days as $day => $events) {
                        $date = mktime(0, 0, 0, $month, $day, $year) * 1000;
                        $events_data[] = array($date, $events);
                    }
                }
            }
            // When showing more than 3 months of results, group them by month, by the first (except for the last month, group on the last day of the month selected)
        } else {
            foreach ($temp_data['income'] as $year => $months) {
                $month_keys = array_keys($months);
                $last_month = end($month_keys);
                foreach ($months as $month => $days) {
                    $day_keys = array_keys($days);
                    $last_day = end($day_keys);
                    $consolidated_date = $month === $last_month ? $last_day : 1;
                    $income = array_sum($days);
                    $date = mktime(0, 0, 0, $month, $consolidated_date, $year) * 1000;
                    $income_data[] = array($date, $income);
                }
            }
            foreach ($temp_data['expense'] as $year => $months) {
                $month_keys = array_keys($months);
                $last_month = end($month_keys);
                foreach ($months as $month => $days) {
                    $day_keys = array_keys($days);
                    $last_day = end($day_keys);
                    $consolidated_date = $month === $last_month ? $last_day : 1;
                    $expense = array_sum($days);
                    $date = mktime(0, 0, 0, $month, $consolidated_date, $year) * 1000;
                    $expense_data[] = array($date, $expense);
                }
            }
            foreach ($temp_data['events'] as $year => $months) {
                $month_keys = array_keys($months);
                $last_month = end($month_keys);
                foreach ($months as $month => $days) {
                    $day_keys = array_keys($days);
                    $last_day = end($day_keys);
                    $consolidated_date = $month === $last_month ? $last_day : 1;
                    $events = array_sum($days);
                    $date = mktime(0, 0, 0, $month, $consolidated_date, $year) * 1000;
                    $events_data[] = array($date, $events);
                }
            }
        }
    }
    $data = array(__('Income', 'mobile-dj-manager') => $income_data, __('Expense', 'mobile-dj-manager') => $expense_data, mdjm_get_label_plural() => $events_data);
    // start our own output buffer
    ob_start();
    ?>
	<div id="mdjm-dashboard-widgets-wrap">
		<div class="metabox-holder" style="padding-top: 0;">
			<div class="postbox">
				<h3><span><?php 
    _e('Transactions Over Time', 'mobile-dj-manager');
    ?>
</span></h3>

				<div class="inside">
					<?php 
    mdjm_reports_graph_controls();
    $graph = new MDJM_Graph($data);
    $graph->set('x_mode', 'time');
    $graph->set('multiple_y_axes', false);
    $graph->display();
    ?>

					<p class="mdjm_graph_totals">
						<strong>
							<?php 
    _e('Total income for period shown: ', 'mobile-dj-manager');
    echo mdjm_currency_filter(mdjm_format_amount($income_totals));
    ?>
						</strong>
					</p>
					<p class="mdjm_graph_totals">
						<strong>
							<?php 
    _e('Total expense for period shown: ', 'mobile-dj-manager');
    echo mdjm_currency_filter(mdjm_format_amount($expense_totals));
    ?>
						</strong>
					</p>
                    <p class="mdjm_graph_totals">
						<strong>
							<?php 
    _e('Total earnings for period shown: ', 'mobile-dj-manager');
    echo mdjm_currency_filter(mdjm_format_amount($income_totals - $expense_totals));
    ?>
						</strong>
					</p>
					<p class="mdjm_graph_totals">
						<strong>
							<?php 
    printf(__('Total %s for period shown: ', 'mobile-dj-manager'), mdjm_get_label_plural());
    echo $events_totals;
    ?>
						</strong>
					</p>

					<?php 
    do_action('mdjm_reports_transactions_graph_additional_stats');
    ?>

					<p class="mdjm-graph-notes">
                        <span>
                            <em><sup>&dagger;</sup> <?php 
    printf(__('Stats include all %s taking place within the date period selected.', 'mobile-dj-manager'), mdjm_get_label_plural(true));
    ?>
</em>
                        </span>
                    </p>

				</div>
			</div>
		</div>
	</div>
	<?php 
    // get output buffer contents and end our own buffer
    $output = ob_get_contents();
    ob_end_clean();
    echo $output;
}
 /**
  * Build all the reports data
  *
  * @access	public
  * @since	1.4
  * @return	arr		$reports_data	All the data for customer reports
  */
 public function reports_data()
 {
     $stats = new MDJM_Stats();
     $dates = mdjm_get_report_dates();
     $stats->setup_dates($dates['range']);
     $cached_reports = false;
     if (false !== $cached_reports) {
         $reports_data = $cached_reports;
     } else {
         $reports_data = array();
         $term_args = array('parent' => 0, 'hierarchical' => 0);
         $categories = get_terms('transaction-types', $term_args);
         foreach ($categories as $category_id => $category) {
             $category_slugs = array($category->slug);
             $txn_args = array('post_status' => array('mdjm-income', 'mdjm-expenditure'), 'fields' => 'ids', 'meta_key' => '_mdjm_txn_status', 'meta_value' => 'Completed', 'tax_query' => array(array('taxonomy' => 'transaction-types', 'field' => 'slug', 'terms' => $category_slugs)), 'date_query' => array(array('after' => date('Y-m-d', $stats->start_date), 'before' => date('Y-m-d', $stats->end_date), 'inclusive' => true)));
             $txn_count = 0;
             $total_value = 0;
             $total_income = 0;
             $total_expense = 0;
             $txns = mdjm_get_txns($txn_args);
             if ($txns) {
                 foreach ($txns as $txn) {
                     $txn_count++;
                     $mdjm_txn = new MDJM_Txn($txn);
                     if ('mdjm-income' == $mdjm_txn->post_status) {
                         $total_income += $mdjm_txn->price;
                     } else {
                         $total_expense += $mdjm_txn->price;
                     }
                     $total_value += $mdjm_txn->price;
                 }
             } else {
                 continue;
             }
             $reports_data[] = array('ID' => $category->term_id, 'type' => $category->name, 'total_transactions' => $txn_count, 'total_income' => mdjm_currency_filter(mdjm_format_amount($total_income)), 'total_income_raw' => $total_income, 'total_expense' => mdjm_currency_filter(mdjm_format_amount($total_expense)), 'total_expense_raw' => $total_expense, 'total_value' => mdjm_currency_filter(mdjm_format_amount($total_value)), 'total_value_raw' => $total_value, 'is_child' => false);
             $this->total_txn_count += $txn_count;
             $this->total_txn_income += $total_income;
             $this->total_txn_expense += $total_expense;
         }
     }
     return $reports_data;
 }
 /**
  * Build all the reports data
  *
  * @access	public
  * @since	1.4
  * @return	arr		$reports_data	All the data for customer reports
  */
 public function reports_data()
 {
     $stats = new MDJM_Stats();
     $dates = mdjm_get_report_dates();
     $stats->setup_dates($dates['range']);
     $cached_reports = false;
     if (false !== $cached_reports) {
         $reports_data = $cached_reports;
     } else {
         $reports_data = array();
         $term_args = array('parent' => 0, 'hierarchical' => 0);
         $categories = get_terms('enquiry-source', $term_args);
         foreach ($categories as $category_id => $category) {
             $conversions = 0;
             $ratio = 0;
             $event_count = 0;
             $category_slugs = array($category->slug);
             $all_event_args = array('post_status' => 'any', 'fields' => 'ids', 'tax_query' => array(array('taxonomy' => 'enquiry-source', 'field' => 'slug', 'terms' => $category_slugs)), 'date_query' => array(array('after' => date('Y-m-d', $stats->start_date), 'before' => date('Y-m-d', $stats->end_date), 'inclusive' => true)));
             $value = 0.0;
             $events = mdjm_get_events($all_event_args);
             $statuses = mdjm_active_event_statuses();
             $statuses[] = 'mdjm-completed';
             $statuses[] = 'mdjm-cancelled';
             if ($events) {
                 foreach ($events as $event) {
                     $event_count++;
                     $mdjm_event = new MDJM_Event($event);
                     if (in_array($mdjm_event->post_status, $statuses)) {
                         $current_value = $mdjm_event->get_total_profit();
                         $value += $current_value;
                     }
                 }
             } else {
                 continue;
             }
             $conversions += $stats->get_conversions($events, 'enquiry-source', $category_slugs, $stats->start_date, $stats->end_date);
             $ratio = round((double) ($conversions / $event_count) * 100);
             $reports_data[] = array('ID' => $category->term_id, 'source' => $category->name, 'total_events' => $event_count, 'total_conversions' => $conversions, 'conversion_ratio' => $ratio . '%', 'total_value' => mdjm_currency_filter(mdjm_format_amount($value)), 'total_value_raw' => $value, 'is_child' => false);
             $this->total_enquiries += $event_count;
             $this->total_value += $value;
             $this->total_conversions += $conversions;
         }
     }
     return $reports_data;
 }
 /**
  * Build all the reports data
  *
  * @access	public
  * @since	1.4
  * @return	arr		$reports_data	All the data for customer reports
  */
 public function reports_data()
 {
     $stats = new MDJM_Stats();
     $dates = mdjm_get_report_dates();
     $stats->setup_dates($dates['range']);
     $cached_reports = false;
     if (false !== $cached_reports) {
         $reports_data = $cached_reports;
     } else {
         $reports_data = array();
         $term_args = array('parent' => 0, 'hierarchical' => 0);
         $categories = get_terms('event-types', $term_args);
         foreach ($categories as $category_id => $category) {
             $event_count = 0;
             $category_slugs = array($category->slug);
             $all_event_args = array('post_status' => apply_filters('mdjm_events_by_type_statuses', array('mdjm-contract', 'mdjm-approved', 'mdjm-completed')), 'fields' => 'ids', 'tax_query' => array(array('taxonomy' => 'event-types', 'field' => 'slug', 'terms' => $category_slugs)), 'meta_query' => array(array('key' => '_mdjm_event_date', 'value' => array(date('Y-m-d', $stats->start_date), date('Y-m-d', $stats->end_date)), 'type' => 'date', 'compare' => 'BETWEEN')));
             $earnings = 0.0;
             $avg_earnings = 0.0;
             $events = mdjm_get_events($all_event_args);
             if ($events) {
                 foreach ($events as $event) {
                     $event_count++;
                     $mdjm_event = new MDJM_Event($event);
                     $current_average_earnings = $current_earnings = $mdjm_event->get_total_profit();
                     $received_date = get_post_field('post_date', $event);
                     $diff = abs(current_time('timestamp') - strtotime($received_date));
                     $months = floor($diff / (30 * 60 * 60 * 24));
                     // Number of months since publication
                     if ($months > 0) {
                         $current_average_earnings = $current_earnings / $months;
                         //$current_average_events      = ( $events / $months );
                     }
                     $earnings += $current_earnings;
                     $avg_earnings += $current_average_earnings;
                 }
             } else {
                 continue;
             }
             $avg_earnings = round($avg_earnings / $event_count, mdjm_currency_decimal_filter());
             $reports_data[] = array('ID' => $category->term_id, 'type' => $category->name, 'total_events' => $event_count, 'total_earnings' => mdjm_currency_filter(mdjm_format_amount($earnings)), 'total_earnings_raw' => $earnings, 'avg_earnings' => mdjm_currency_filter(mdjm_format_amount($avg_earnings)), 'is_child' => false);
         }
     }
     return $reports_data;
 }