/** * 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; }
/** * Get Expense By Date. * * @since 1.4 * @param int $day Day number * @param int $month_num Month number * @param int $year Year * @param int $hour Hour * @return int $expense Expenses */ public function get_expenses_by_date($day = null, $month_num, $year = null, $hour = null) { global $wpdb; $args = array('post_type' => 'mdjm-transaction', 'nopaging' => true, 'year' => $year, 'monthnum' => $month_num, 'post_status' => 'mdjm-expenditure', 'meta_key' => '_mdjm_txn_status', 'meta_value' => 'Completed', 'fields' => 'ids', 'update_post_term_cache' => false); if (!empty($day)) { $args['day'] = $day; } if (!empty($hour)) { $args['hour'] = $hour; } $args = apply_filters('mdjm_get_expenses_by_date_args', $args); $txns = mdjm_get_txns($args); $expense = 0; if ($txns) { $txns = implode(',', $txns); $expense = $wpdb->get_var("SELECT SUM(meta_value) FROM {$wpdb->postmeta} WHERE meta_key = '_mdjm_txn_total' AND post_id IN ({$txns})"); } return round($expense, 2); }
/** * Retrieve event transactions. * * @since 1.3.8 * @param int $event_id Event ID. * @param arr $args @see get_posts * @return obj Array of event transactions. */ function mdjm_get_event_txns($event_id, $args = array()) { $defaults = array('post_parent' => $event_id, 'post_status' => 'any', 'meta_key' => '_mdjm_txn_status', 'meta_query' => array('key' => '_mdjm_txn_status', 'value' => 'Completed', 'compare' => '=')); $args = wp_parse_args($args, $defaults); return mdjm_get_txns($args); }