/**
 * Content tag: client_lastname.
 * The last name of the client.
 *
 * @param	int		The event ID.
 * @param	int		The client ID.
 *
 * @return	str		The last name of the client.
 */
function mdjm_content_tag_client_lastname($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 = '';
    }
    $last_name = '';
    if (!empty($user_id)) {
        $last_name = mdjm_get_client_lastname($user_id);
    }
    return $last_name;
}
/**
 * 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;
}
/**
 * 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);
}