/** * Get the Export Data * * @access public * @since 1.4 * @return arr $data The data for the CSV file */ public function get_data() { $data = array(); // Export all transactions $offset = 30 * ($this->step - 1); $txn_args = array('post_type' => 'mdjm-transaction', 'posts_per_page' => 30, 'offset' => $offset, 'paged' => $this->step, 'post_status' => array('mdjm-income', 'mdjm-expenditure'), 'order' => 'ASC', 'orderby' => 'date'); if (!empty($this->start) || !empty($this->end)) { $txn_args['date_query'] = array(array('after' => date('Y-n-d 00:00:00', strtotime($this->start)), 'before' => date('Y-n-d 23:59:59', strtotime($this->end)), 'inclusive' => true)); } if (!empty($this->status) && is_array($this->status)) { $meta_query = array(); foreach ($this->status as $txn_status) { $meta_query[] = array('key' => '_mdjm_txn_status', 'value' => $txn_status); } $txn_args['meta_query'] = array('relation' => 'OR', $meta_query); } $all_txns = get_posts($txn_args); if ($all_txns) { $i = 0; $income = 0; $expense = 0; foreach ($all_txns as $txn) { $mdjm_txn = new MDJM_Txn($txn->ID); $data[$i]['id'] = $mdjm_txn->ID; $data[$i]['date'] = date('d-M-Y', strtotime($mdjm_txn->post_date)); $data[$i]['status'] = $mdjm_txn->payment_status; $data[$i]['income'] = 'mdjm-income' == $mdjm_txn->post_status ? mdjm_format_amount($mdjm_txn->price) : ''; $data[$i]['expense'] = 'mdjm-expenditure' == $mdjm_txn->post_status ? mdjm_format_amount($mdjm_txn->price) : ''; $data[$i]['to_from'] = mdjm_get_txn_recipient_name($mdjm_txn->ID); $data[$i]['type'] = $mdjm_txn->get_type(); $data[$i]['source'] = $mdjm_txn->get_method(); $data[$i]['gateway'] = $mdjm_txn->get_gateway(); $data[$i]['event'] = !empty($mdjm_txn->post_parent) ? mdjm_get_event_contract_id($mdjm_txn->post_parent) : ''; if ('mdjm-income' == $mdjm_txn->post_status) { $income += $mdjm_txn->price; } else { $expense += $mdjm_txn->price; } $i++; } $data = apply_filters('mdjm_export_get_data', $data); $data = apply_filters('mdjm_export_get_data_' . $this->export_type, $data); return $data; } return false; }
/** * Save the event transaction * * */ function mdjm_save_event_transaction_ajax() { global $mdjm_event; $result = array(); $mdjm_event = new MDJM_Event($_POST['event_id']); $mdjm_txn = new MDJM_Txn(); $txn_data = array('post_parent' => $_POST['event_id'], 'post_author' => $mdjm_event->client, 'post_status' => $_POST['direction'] == 'Out' ? 'mdjm-expenditure' : 'mdjm-income', 'post_date' => date('Y-m-d H:i:s', strtotime($_POST['date']))); $txn_meta = array('_mdjm_txn_status' => 'Completed', '_mdjm_payment_from' => $mdjm_event->client, '_mdjm_txn_total' => $_POST['amount'], '_mdjm_payer_firstname' => mdjm_get_client_firstname($mdjm_event->client), '_mdjm_payer_lastname' => mdjm_get_client_lastname($mdjm_event->client), '_mdjm_payer_email' => mdjm_get_client_email($mdjm_event->client), '_mdjm_payment_from' => mdjm_get_client_display_name($mdjm_event->client), '_mdjm_txn_source' => $_POST['src']); if ($_POST['direction'] == 'In') { if (!empty($_POST['from'])) { $txn_meta['_mdjm_payment_from'] = sanitize_text_field($_POST['from']); } else { $txn_meta['_mdjm_payment_from'] = mdjm_get_client_display_name($mdjm_event->client); } } if ($_POST['direction'] == 'Out') { if (!empty($_POST['to'])) { $txn_meta['_mdjm_payment_to'] = sanitize_text_field($_POST['to']); } else { $txn_meta['_mdjm_payment_to'] = mdjm_get_client_display_name($mdjm_event->client); } } $mdjm_txn->create($txn_data, $txn_meta); if ($mdjm_txn->ID > 0) { $result['type'] = 'success'; mdjm_set_txn_type($mdjm_txn->ID, $_POST['for']); $args = array('user_id' => get_current_user_id(), 'event_id' => $_POST['event_id'], 'comment_content' => sprintf(__('%1$s payment of %2$s received for %3$s %4$s.', 'mobile-dj-manager'), $_POST['direction'] == 'In' ? __('Incoming', 'mobile-dj-manager') : __('Outgoing', 'mobile-dj-manager'), mdjm_currency_filter(mdjm_format_amount($_POST['amount'])), mdjm_get_label_singular(true), mdjm_get_event_contract_id($_POST['event_id']))); mdjm_add_journal($args); // Email overide if (empty($_POST['send_notice']) && mdjm_get_option('manual_payment_cfm_template')) { $manual_email_template = mdjm_get_option('manual_payment_cfm_template'); mdjm_update_option('manual_payment_cfm_template', 0); } $payment_for = $mdjm_txn->get_type(); $amount = mdjm_currency_filter(mdjm_format_amount($_POST['amount'])); mdjm_add_content_tag('payment_for', __('Reason for payment', 'mobile-dj-manager'), function () use($payment_for) { return $payment_for; }); mdjm_add_content_tag('payment_amount', __('Payment amount', 'mobile-dj-manager'), function () use($amount) { return $amount; }); mdjm_add_content_tag('payment_date', __('Date of payment', 'mobile-dj-manager'), 'mdjm_content_tag_ddmmyyyy'); /** * Allow hooks into this payment. The hook is suffixed with 'in' or 'out' depending * on the payment direction. i.e. mdjm_post_add_manual_txn_in and mdjm_post_add_manual_txn_out * * @since 1.3.7 * @param int $event_id * @param obj $txn_id */ do_action('mdjm_post_add_manual_txn_' . strtolower($_POST['direction']), $_POST['event_id'], $mdjm_txn->ID); // Email overide if (empty($_POST['send_notice']) && isset($manual_email_template)) { mdjm_update_option('manual_payment_cfm_template', $manual_email_template); } $result['deposit_paid'] = 'N'; $result['balance_paid'] = 'N'; if ($mdjm_event->get_remaining_deposit() < 1) { mdjm_update_event_meta($mdjm_event->ID, array('_mdjm_event_deposit_status' => 'Paid')); $result['deposit_paid'] = 'Y'; } if ($mdjm_event->get_balance() < 1) { mdjm_update_event_meta($mdjm_event->ID, array('_mdjm_event_balance_status' => 'Paid')); mdjm_update_event_meta($mdjm_event->ID, array('_mdjm_event_deposit_status' => 'Paid')); $result['balance_paid'] = 'Y'; $result['deposit_paid'] = 'Y'; } } else { $result['type'] = 'error'; $result['msg'] = __('Unable to add transaction', 'mobile-dj-manager'); } ob_start(); mdjm_do_event_txn_table($_POST['event_id']); $result['transactions'] = ob_get_contents(); ob_get_clean(); echo json_encode($result); die; }
/** * Records the merchant fee transaction. * * @since 1.0 * @param arr $gateway_data Transaction data received from the gateway. * @return void */ function mdjm_create_merchant_fee_txn($gateway_data) { if (isset($gateway_data['gateway'])) { $gateway = mdjm_get_gateway_payment_label($gateway_data['gateway']); } else { $gateway = mdjm_get_gateway_payment_label(mdjm_get_default_gateway()); } if (!isset($gateway_data['fee']) || $gateway_data['fee'] < '0.01') { return; } $txn_data = apply_filters('mdjm_merchant_fee_transaction_data', array('post_author' => mdjm_get_event_client_id($gateway_data['event_id']), 'post_type' => 'mdjm-transaction', 'post_title' => sprintf(__('%s Merchant Fee for Transaction %s', 'mobile-dj-manager'), $gateway, $gateway_data['txn_id']), 'post_status' => 'mdjm-expenditure', 'post_parent' => $gateway_data['event_id'])); $txn_meta = apply_filters('mdjm_merchant_fee_transaction_meta', array('_mdjm_txn_status' => 'Completed', '_mdjm_txn_source' => $gateway, '_mdjm_txn_currency' => $gateway_data['currency'], '_mdjm_txn_total' => $gateway_data['fee'], '_mdjm_payment_to' => $gateway)); do_action('mdjm_before_create_merchant_fee', $gateway_data, $txn_data, $txn_meta); $mdjm_txn = new MDJM_Txn(); $mdjm_txn->create($txn_data, $txn_meta); $merchant_fee_id = $mdjm_txn->ID; if (!empty($merchant_fee_id)) { mdjm_set_txn_type($mdjm_txn->ID, mdjm_get_txn_cat_id('slug', 'mdjm-merchant-fees')); // Update the incoming transaction meta to include the merchant txn ID. mdjm_update_txn_meta($gateway_data['txn_id'], array('_mdjm_merchant_fee_txn_id' => $merchant_fee_id)); } do_action('mdjm_after_create_merchant_fee', $merchant_fee_id, $gateway_data); }
/** * Log the primary employees payment settings and update if employee or wage changes. * * @since 1.3 * @param int $event_id Event ID. * @param arr $old_meta Old meta values from before event save. * @param arr $new_meta New meta values after event save. * @return void */ function mdjm_manage_primary_employee_payment_status($event_id, $old_meta, $new_meta) { if (!mdjm_get_option('enable_employee_payments')) { return; } $mdjm_event = new MDJM_Event($event_id); $employee_id = $mdjm_event->get_employee(); if (empty($employee_id)) { return; } $payment_amount = mdjm_get_employees_event_wage($event_id, $employee_id); $payment_status = get_post_meta($event_id, '_mdjm_event_dj_payment_status', true); if (empty($payment_status)) { if (empty($payment_amount) || $payment_amount < 1) { return; } $mdjm_txn = new MDJM_Txn(); $mdjm_txn->create(array('post_title' => sprintf(__('Wage payment to %s for %d', 'mobile-dj-manager'), mdjm_get_employee_display_name($employee_id), $event_id), 'post_status' => 'mdjm-expenditure', 'post_author' => 1, 'post_parent' => $event_id), array('_mdjm_txn_status' => 'Pending', '_mdjm_payment_to' => $employee_id, '_mdjm_txn_total' => $payment_amount)); if (!empty($mdjm_txn)) { $data['txn_id'] = $mdjm_txn->ID; } mdjm_set_txn_type($mdjm_txn->ID, mdjm_get_txn_cat_id('slug', 'mdjm-employee-wages')); $payment_data = array('payment_status' => 'unpaid', 'payment_date' => '', 'txn_id' => $mdjm_txn->ID, 'payment_amount' => ''); update_post_meta($event_id, '_mdjm_event_dj_payment_status', $payment_data); } else { if ($payment_status['payment_status'] == 'paid') { return; } if (in_array($mdjm_event->post_status, array('mdjm-cancelled', 'mdjm-rejected', 'mdjm-failed'))) { update_post_meta($mdjm_txn->ID, '_mdjm_txn_status', 'Cancelled'); } $mdjm_txn = new MDJM_Txn($payment_status['txn_id']); if ($mdjm_txn->recipient_id != $employee_id) { update_post_meta($mdjm_txn->ID, '_mdjm_payment_to', $employee_id); } if ($payment_amount != $mdjm_txn->price) { update_post_meta($mdjm_txn->ID, '_mdjm_txn_total', $payment_amount); } } }
/** * Displays all event transactions within a table. * * @since 1.3.7 * @global obj $mdjm_event MDJM_Event class object * @param int $event_id * @return str */ function mdjm_do_event_txn_table($event_id) { global $mdjm_event; $event_txns = apply_filters('mdjm_event_txns', mdjm_get_event_txns($event_id, array('orderby' => 'post_status'))); $in = 0; $out = 0; ?> <table class="widefat mdjm_event_txn_list"> <thead> <tr> <th style="width: 20%"><?php _e('Date', 'mobile-dj-manager'); ?> </th> <th style="width: 20%"><?php _e('To/From', 'mobile-dj-manager'); ?> </th> <th style="width: 15%"><?php _e('In', 'mobile-dj-manager'); ?> </th> <th style="width: 15%"><?php _e('Out', 'mobile-dj-manager'); ?> </th> <th><?php _e('Details', 'mobile-dj-manager'); ?> </th> <?php do_action('mdjm_event_txn_table_head', $event_id); ?> </tr> </thead> <tbody> <?php if ($event_txns) { ?> <?php foreach ($event_txns as $event_txn) { ?> <?php $txn = new MDJM_Txn($event_txn->ID); ?> <tr class="mdjm_field_wrapper"> <td><a href="<?php echo get_edit_post_link($txn->ID); ?> "><?php echo mdjm_format_short_date($txn->post_date); ?> </a></td> <td><?php echo esc_attr(mdjm_get_txn_recipient_name($txn->ID)); ?> </td> <td> <?php if ($txn->post_status == 'mdjm-income') { ?> <?php $in += mdjm_sanitize_amount($txn->price); ?> <?php echo mdjm_currency_filter(mdjm_format_amount($txn->price)); ?> <?php } else { ?> <?php echo '–'; ?> <?php } ?> </td> <td> <?php if ($txn->post_status == 'mdjm-expenditure') { ?> <?php $out += mdjm_sanitize_amount($txn->price); ?> <?php echo mdjm_currency_filter(mdjm_format_amount($txn->price)); ?> <?php } else { ?> <?php echo '–'; ?> <?php } ?> </td> <td><?php echo $txn->get_type(); ?> </td> </tr> <?php } ?> <?php } else { ?> <tr> <td colspan="5"><?php printf(__('There are currently no transactions for this %s', 'mobile-dj-manager'), mdjm_get_label_singular(true)); ?> </td> </tr> <?php } ?> </tbody> <tfoot> <tr> <th style="width: 20%"> </th> <th style="width: 20%"> </th> <th style="width: 15%"><strong><?php echo mdjm_currency_filter(mdjm_format_amount($in)); ?> </strong></th> <th style="width: 15%"><strong><?php echo mdjm_currency_filter(mdjm_format_amount($out)); ?> </strong></th> <th><strong><?php printf(__('%s Earnings:', 'mobile-dj-manager'), mdjm_get_label_singular()); ?> <?php echo mdjm_currency_filter(mdjm_format_amount($in - $out)); ?> </strong></th> </tr> <?php do_action('mdjm_event_txn_table_foot', $event_id); ?> </tfoot> </table> <?php }
/** * Returns the date for a transaction in short format. * * @since 1.3 * @param int $txn_id The transaction ID. * @return str The date of the transaction. */ function mdjm_get_txn_date($txn_id = '') { if (empty($txn_id)) { return false; } $txn = new MDJM_Txn($txn_id); return mdjm_format_short_date($txn->get_date()); }