/**
  * 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;
}
/**
 * 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 '&ndash;';
                ?>
                        <?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 '&ndash;';
                ?>
                        <?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%">&nbsp;</th>
            <th style="width: 20%">&nbsp;</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 
}
/**
 * Return the type of transaction.
 *
 * @since	1.3
 * @param	int		$txn_id		ID of the current transaction.
 * @return	str		Transaction type.
 */
function mdjm_get_txn_type($txn_id)
{
    $txn = new MDJM_Txn($txn_id);
    // Return the label for the status
    return $txn->get_type();
}