/**
 * 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;
}
/**
 * Content tag: client_email.
 * The email address of the client.
 *
 * @param	int		The event ID.
 * @param	int		The client ID.
 *
 * @return	str		The email address of the client.
 */
function mdjm_content_tag_client_email($event_id = '', $client_id = '')
{
    if (!empty($client_id)) {
        $user_id = $client_id;
    } elseif (!empty($event_id)) {
        $user_id = mdjm_get_event_client_id($event_id);
    } else {
        $user_id = '';
    }
    $email = '';
    if (!empty($user_id)) {
        $email = mdjm_get_client_email($user_id);
    }
    return $email;
}
/**
 * Process the sending of the email
 *
 * @since	1.3
 * @param	arr		$data	Super global $_POST array
 * @return	void
 */
function mdjm_send_comm_email($data)
{
    $url = remove_query_arg(array('mdjm-message', 'event_id', 'template', 'recipient', 'mdjm-action'));
    if (!wp_verify_nonce($data['mdjm_nonce'], 'send_comm_email')) {
        $message = 'nonce_fail';
    } elseif (empty($data['mdjm_email_to']) || empty($data['mdjm_email_subject']) || empty($data['mdjm_email_content'])) {
        $message = 'comm_missing_content';
    } else {
        if (isset($_FILES['mdjm_email_upload_file']) && '' !== $_FILES['mdjm_email_upload_file']['name']) {
            $upload_dir = wp_upload_dir();
            $file_name = $_FILES['mdjm_email_upload_file']['name'];
            $file_path = $upload_dir['path'] . '/' . $file_name;
            $tmp_path = $_FILES['mdjm_email_upload_file']['tmp_name'];
            if (move_uploaded_file($tmp_path, $file_path)) {
                $attachments[] = $file_path;
            }
        }
        if (empty($attachments)) {
            $attachments = array();
        }
        $attachments = apply_filters('mdjm_send_comm_email_attachments', $attachments, $data);
        $email_args = array('to_email' => mdjm_get_client_email($data['mdjm_email_to']), 'from_name' => $data['mdjm_email_from_name'], 'from_email' => $data['mdjm_email_from_address'], 'event_id' => $data['mdjm_email_event'], 'client_id' => $data['mdjm_email_to'], 'subject' => stripslashes($data['mdjm_email_subject']), 'attachments' => !empty($attachments) ? $attachments : array(), 'message' => stripslashes($data['mdjm_email_content']), 'track' => true, 'copy_to' => !empty($data['mdjm_email_copy_to']) ? array($data['mdjm_email_copy_to']) : array(), 'source' => __('Communication Feature', 'mobile-dj-manager'));
        if (mdjm_send_email_content($email_args)) {
            $message = 'comm_sent';
            if (!empty($data['mdjm_event_reject'])) {
                $args = array('reject_reason' => !empty($data['mdjm_email_reject_reason']) ? $data['mdjm_email_reject_reason'] : __('No reason specified', 'mobile-dj-manager'));
                mdjm_update_event_status($email_args['event_id'], 'mdjm-rejected', get_post_status($email_args['event_id']), $args);
            }
        } else {
            $message = 'comm_not_sent';
        }
    }
    wp_redirect(add_query_arg('mdjm-message', $message, $url));
    die;
}
/**
 * Mark the event balance as paid.
 *
 * Determines if any balance remains and if so, assumes it has been paid and
 * creates an associted transaction.
 *
 * @since	1.3
 * @param	int		$event_id	The event ID.
 * @return	void
 */
function mdjm_mark_event_balance_paid($event_id)
{
    $mdjm_event = new MDJM_Event($event_id);
    $txn_id = 0;
    if ('Paid' == $mdjm_event->get_balance_status()) {
        return;
    }
    $remaining = $mdjm_event->get_balance();
    do_action('mdjm_pre_mark_event_balance_paid', $event_id, $remaining);
    if (!empty($remaining) && $remaining > 0) {
        $mdjm_txn = new MDJM_Txn();
        $txn_meta = array('_mdjm_txn_source' => mdjm_get_option('default_type', __('Cash', 'mobile-dj-manager')), '_mdjm_txn_currency' => mdjm_get_currency(), '_mdjm_txn_status' => 'Completed', '_mdjm_txn_total' => $remaining, '_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->create(array('post_parent' => $event_id), $txn_meta);
        if ($mdjm_txn->ID > 0) {
            mdjm_set_txn_type($mdjm_txn->ID, mdjm_get_txn_cat_id('slug', 'mdjm-balance-payments'));
            $args = array('user_id' => get_current_user_id(), 'event_id' => $event_id, 'comment_content' => sprintf(__('%1$s payment of %2$s received and %1$s marked as paid.', 'mobile-dj-manager'), mdjm_get_balance_label(), mdjm_currency_filter(mdjm_format_amount($remaining))));
            mdjm_add_journal($args);
            mdjm_add_content_tag('payment_for', __('Reason for payment', 'mobile-dj-manager'), 'mdjm_content_tag_balance_label');
            mdjm_add_content_tag('payment_amount', __('Payment amount', 'mobile-dj-manager'), function () use($remaining) {
                return mdjm_currency_filter(mdjm_format_amount($remaining));
            });
            mdjm_add_content_tag('payment_date', __('Date of payment', 'mobile-dj-manager'), 'mdjm_content_tag_ddmmyyyy');
            do_action('mdjm_post_add_manual_txn_in', $event_id, $mdjm_txn->ID);
        }
    }
    mdjm_update_event_meta($mdjm_event->ID, array('_mdjm_event_deposit_status' => 'Paid', '_mdjm_event_balance_status' => 'Paid'));
    do_action('mdjm_post_mark_event_balance_paid', $event_id);
}