# easy-invoice/2.1.2/templates/quotes/builder.php

Easy Invoice – Invoice Generator, PDF Quotes &amp; Payments, version 2.1.2. 439 lines.

- Page: https://pluginprobe.com/plugins/easy-invoice/2.1.2/code/templates/quotes/builder.php
- Raw: https://pluginprobe.com/plugins/easy-invoice/2.1.2/raw/templates/quotes/builder.php
- Modified: 2025-09-17T11:27:10+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/easy-invoice/2.1.2/code/templates/quotes/builder.php#L10-L20`.

```php
<?php
// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}
use EasyInvoice\Providers\ClientServiceProvider;
use EasyInvoice\Providers\QuoteServiceProvider;

// Enqueue CSS and JS files for the builder page
wp_enqueue_style('easy-invoice-form', plugin_dir_url(__FILE__) . '../../assets/css/invoice-form.css', array(), EASY_INVOICE_VERSION);
wp_enqueue_script('easy-invoice-form', plugin_dir_url(__FILE__) . '../../assets/js/invoice-form.js', array('jquery'), EASY_INVOICE_VERSION, true);
wp_enqueue_script('easy-quote-save', plugin_dir_url(__FILE__) . '../../assets/js/quote-save.js', array('jquery'), EASY_INVOICE_VERSION, true);
wp_enqueue_script('easy-client-manager', plugin_dir_url(__FILE__) . '../../assets/js/client-manager.js', array('jquery'), EASY_INVOICE_VERSION, true);

// Create nonce for AJAX calls
$ajax_nonce = wp_create_nonce('easy_invoice_nonce');

// Localize script for AJAX URL and nonce
wp_localize_script('easy-quote-save', 'easyInvoice', array(
    'ajaxUrl' => admin_url('admin-ajax.php'),
    'nonce' => $ajax_nonce
));

// Localize client manager script
wp_localize_script('easy-client-manager', 'easyInvoice', array(
    'ajaxUrl' => admin_url('admin-ajax.php'),
    'nonce' => $ajax_nonce
));

// Get quote ID if present in URL
$quote_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
if ($quote_id) {
    $post = get_post($quote_id);
    if (!$post || $post->post_type !== \EasyInvoice\Constants\PostTypes::EASY_INVOICE_QUOTE_POST_TYPE) {
        wp_die(__('Invalid post type. This builder can only be used for quotes.', 'easy-invoice'), __('Invalid Post Type', 'easy-invoice'), array('back_link' => true));
    }
}
$quote = null;

// Check if this is a new quote (no ID)
$is_new_quote = ($quote_id === 0);

// Default values for new quote
$quote_number_service = function_exists('easy_invoice_get_quote_number_service') ? easy_invoice_get_quote_number_service() : null;

// Get global quote settings
$settings_controller = new \EasyInvoice\Controllers\SettingsController();
$quote_prefix = $settings_controller::getQuotePrefix();
$quote_terms = $settings_controller::getQuoteTermsConditions();
$quote_footer = $settings_controller::getQuoteFooterText();
$quote_accept_button = get_option('easy_invoice_quote_accept_button', 'yes');
$quote_accept_action = get_option('easy_invoice_quote_accept_action', 'email');
$quote_accept_text = get_option('easy_invoice_quote_accept_text', __('Accept Quote', 'easy-invoice'));
$quote_accepted_message = get_option('easy_invoice_quote_accepted_message', __('Thank you for accepting our quote!', 'easy-invoice'));
$quote_declined_message = get_option('easy_invoice_quote_declined_message', __('Thank you for your consideration.', 'easy-invoice'));

$quote_data = array(
    'number' => $quote_number_service ? $quote_number_service->getNextNumber() : 'QT-1',
    'date' => date('Y-m-d'),
    'expiry_date' => date('Y-m-d', strtotime('+30 days')),
    'client_id' => 0,
    'client_name' => '',
    'client_email' => '',
    'client_phone' => '',
    'client_address' => '',
    'items' => array(),
    'notes' => '',
    'internal_notes' => '',
    'discount' => 0,
    'discount_type' => 'percentage',
    'calculation_method' => 'before_tax',
    'tax_rate' => easy_invoice_get_tax_rate(),
    'prices_include_tax' => 'no',
    'status' => 'draft',
    'currency' => 'USD',
    'currency_symbol' => '$',
    'title' => '',
    'description' => '',
    'terms' => $quote_terms, // Use global terms setting
    'footer_text' => $quote_footer, // Use global footer setting
    'accept_button' => $quote_accept_button, // Use global accept button setting
    'accept_action' => $quote_accept_action, // Use global accept action setting
    'accept_text' => $quote_accept_text, // Use global accept text setting
    'accepted_message' => $quote_accepted_message, // Use global accepted message setting
    'declined_message' => $quote_declined_message, // Use global declined message setting
);

// Get clients from repository
$client_repository = ClientServiceProvider::getClientRepository();
$clients = $client_repository->all();

// Load client data directly in PHP if quote has a client
$client_data = null;
if ($quote && $quote->getClientId()) {
    $client = $client_repository->find($quote->getClientId());
    if ($client) {
        $client_data = array(
            'id' => $client->getId(),
            'name' => $client->getBusinessClientName() ?: ($client->getFirstName() . ' ' . $client->getLastName()),
            'email' => $client->getEmail() ?: '',
            'phone' => $client->getExtraInfo() ?: '',
            'company' => $client->getBusinessClientName() ?: '',
            'address' => $client->getAddress() ?: '',
            'website' => $client->getWebsite() ?: '',
        );
    }
}

// If editing an existing quote, load its data
if ($quote_id > 0) {
    // Get the quote from repository
    $quote_repository = QuoteServiceProvider::getQuoteRepository();
    $quote = $quote_repository->find($quote_id);

    if ($quote && $quote->getId()) {
        // Quote loaded successfully
    } else {
        // Failed to load quote
    }
} else {
    // Create a temporary WP_Post object for new quote
    $empty_post = new WP_Post((object) array(
        'ID' => 0,
        'post_author' => get_current_user_id(),
        'post_date' => current_time('mysql'),
        'post_date_gmt' => current_time('mysql', 1),
        'post_title' => $quote_data['number'],
        'post_status' => 'auto-draft',
        'comment_status' => 'closed',
        'ping_status' => 'closed',
        'post_name' => '',
        'post_modified' => current_time('mysql'),
        'post_modified_gmt' => current_time('mysql', 1),
        'post_parent' => 0,
        'guid' => '',
        'menu_order' => 0,
        'post_type' => \EasyInvoice\Constants\PostTypes::EASY_INVOICE_QUOTE_POST_TYPE,
        'post_mime_type' => '',
        'comment_count' => 0,
        'filter' => 'raw',
    ));

    $quote = new \EasyInvoice\Models\Quote($empty_post);

    // Set default values on the quote object
    foreach ($quote_data as $key => $value) {
        $setter = 'set' . easy_invoice_str_replace('_', '', ucwords($key, '_'));
        if (method_exists($quote, $setter)) {
            // Handle type-specific setters
            switch ($setter) {
                case 'setClientId':
                    $quote->setClientId((int) $value);
                    break;
                case 'setItems':
                    $quote->setItems((array) $value);
                    break;
                case 'setSubtotal':
                case 'setTaxAmount':
                case 'setDiscountAmount':
                case 'setTotal':
                case 'setDiscountValue':
                case 'setTaxRate':
                    $quote->$setter((float) $value);
                    break;
                case 'setPricesIncludeTax':
                    $quote->$setter((bool) $value);
                    break;
                default:
                    $quote->$setter((string) $value);
                    break;
            }
        }
    }

    // Initialize empty items array
    $quote->setItems([]);
}

// Only load custom meta that's not handled by Quote object
if ($quote) {
    // These are still accessed from meta as they don't have model methods yet
    $quote_data['terms'] = $quote_id > 0 ? get_post_meta($quote_id, '_easy_invoice_terms', true) : $quote_data['terms'];
    $quote_data['internal_notes'] = $quote_id > 0 ? get_post_meta($quote_id, '_easy_invoice_internal_notes', true) : $quote_data['internal_notes'];
    $quote_data['currency'] = $quote_id > 0 ? get_post_meta($quote_id, '_easy_invoice_currency_code', true) : $quote_data['currency'];
    $quote_data['currency_symbol'] = $quote_id > 0 ? get_post_meta($quote_id, '_easy_invoice_currency_position', true) : $quote_data['currency_symbol'];
    $quote_data['calculation_method'] = $quote_id > 0 ? get_post_meta($quote_id, '_easy_invoice_calculation_method', true) : $quote_data['calculation_method'];
}

// Prepare quote items JSON for JavaScript
$quote_items_json = json_encode($quote ? $quote->getItems() : []);

// Create nonce for AJAX calls
$admin_nonce = wp_create_nonce('easy_invoice_admin_nonce');

// Initialize quote form manager for field configuration
$quote_form_manager = new \EasyInvoice\Forms\Quote\QuoteFormManager();
$quote_field_config = $quote_form_manager->getFieldConfigForJavaScript();

// Remove the following code that preloads all templates into JS
// $templates = $quote_form_manager->getTemplates();
// $template_htmls = [];
// $formatter = new \EasyInvoice\Helpers\QuoteFormatter($quote);
// foreach ($templates as $template_id => $template_config) {
//     $template_file = EASY_INVOICE_PLUGIN_DIR . 'templates/quote-templates/' . $template_id . '.php';
//     if (file_exists($template_file)) {
//         ob_start();
//         include $template_file;
//         $template_htmls[$template_id] = ob_get_clean();
//     } else {
//         $template_htmls[$template_id] = '<div class="template-not-found"><h3>Template Not Found</h3><p>The template "' . esc_html($template_id) . '" does not exist.</p></div>';
//     }
// }

?>

<script>
// Flag to indicate this is a quote form
window.isQuoteForm = true;

// Handle Send Quote button
jQuery(document).ready(function($) {
    $('#send-quote-btn').on('click', function(e) {
        e.preventDefault();

        // First save the quote if it's not saved yet
        if ($('#quote-id').val() == '0') {
            // Quote not saved yet, save it first
            $('#save-quote-btn').click();

            // Wait for save to complete, then send
            setTimeout(function() {
                if ($('#quote-id').val() != '0') {
                    sendQuoteEmail();
                } else {
                    if (typeof EasyInvoiceToast !== 'undefined') {
                        EasyInvoiceToast.warning('Please save the quote first before sending.');
                    } else {
                        console.log('Please save the quote first before sending.');
                    }
                }
            }, 2000);
        } else {
            // Quote already saved, send directly
            sendQuoteEmail();
        }
    });

    function sendQuoteEmail() {
        const quoteId = $('#quote-id').val();

        if (quoteId == '0') {
            if (typeof EasyInvoiceToast !== 'undefined') {
                EasyInvoiceToast.warning('Please save the quote first before sending.');
            } else {
                console.log('Please save the quote first before sending.');
            }
            return;
        }

        // Use the confirmation system instead of alert
        if (typeof EasyInvoiceConfirmation !== 'undefined') {
            EasyInvoiceConfirmation.confirmAction('send email', 'this quote', function() {
                // Show loading state
                const $btn = $('#send-quote-btn');
                const originalText = $btn.html();
                $btn.prop('disabled', true).html('<i class="fas fa-spinner fa-spin mr-2"></i>Sending...');

                // Send AJAX request
                $.ajax({
                    url: window.easyInvoice.ajaxUrl,
                    type: 'POST',
                    data: {
                        action: 'easy_invoice_send_quote_email',
                        quote_id: quoteId,
                        nonce: '<?php echo wp_create_nonce('easy_invoice_send_quote_email'); ?>'
                    },
                    success: function(response) {
                        if (response.success) {
                            if (typeof EasyInvoiceToast !== 'undefined') {
                                EasyInvoiceToast.success('Quote email sent successfully!');
                            } else {
                                console.log('Quote email sent successfully!');
                            }
                        } else {
                            if (typeof EasyInvoiceToast !== 'undefined') {
                                // Handle both response.data.message and response.data (direct string)
                                const errorMessage = (response.data && response.data.message) ? response.data.message : (response.data || 'Error sending email');
                                EasyInvoiceToast.error(errorMessage);
                            } else {
                                const errorMessage = (response.data && response.data.message) ? response.data.message : (response.data || 'Unknown error');
                                console.log('Error sending email: ' + errorMessage);
                            }
                        }
                    },
                    error: function() {
                        if (typeof EasyInvoiceToast !== 'undefined') {
                            EasyInvoiceToast.error('Error connecting to server');
                        } else {
                            console.log('Error connecting to server');
                        }
                    },
                    complete: function() {
                        // Reset button
                        $btn.prop('disabled', false).html(originalText);
                    }
                });
            });
        } else {
            // Fallback to browser confirm if confirmation system not available
            if (confirm('Send this quote via email?')) {
                // Show loading state
                const $btn = $('#send-quote-btn');
                const originalText = $btn.html();
                $btn.prop('disabled', true).html('<i class="fas fa-spinner fa-spin mr-2"></i>Sending...');

                // Send AJAX request
                $.ajax({
                    url: window.easyInvoice.ajaxUrl,
                    type: 'POST',
                    data: {
                        action: 'easy_invoice_send_quote_email',
                        quote_id: quoteId,
                        nonce: '<?php echo wp_create_nonce('easy_invoice_send_quote_email'); ?>'
                    },
                    success: function(response) {
                        if (response.success) {
                            alert('Quote email sent successfully!');
                        } else {
                            const errorMessage = (response.data && response.data.message) ? response.data.message : (response.data || 'Error sending email');
                            alert(errorMessage);
                        }
                    },
                    error: function() {
                        alert('Error connecting to server');
                    },
                    complete: function() {
                        // Reset button
                        $btn.prop('disabled', false).html(originalText);
                    }
                });
            }
        }
    }
});
</script>

<script>
// Quote field configuration and pre-loaded data
window.easyInvoice = window.easyInvoice || {};
window.easyInvoice.fieldConfig = <?php echo json_encode($quote_field_config); ?>;
window.easyInvoice.clientData = <?php echo json_encode($client_data); ?>;
window.easyInvoice.ajaxUrl = '<?php echo admin_url('admin-ajax.php'); ?>';
window.easyInvoice.nonce = '<?php echo wp_create_nonce('easy_invoice_nonce'); ?>';
window.easyInvoice.isPro = <?php echo easy_invoice_has_pro() ? 'true' : 'false'; ?>;
</script>

<form id="quote-form" method="post">
    <input type="hidden" id="quote-id" name="quote_id" value="<?php echo $quote_id; ?>">
    <input type="hidden" id="quote_nonce" name="quote_nonce" value="<?php echo wp_create_nonce('easy_invoice_nonce'); ?>">

    <div id="easy-invoice-content" class="h-screen flex flex-col">
        <!-- Header -->
        <div class="bg-white shadow-sm w-full z-10">
            <div class="max-w-full mx-auto px-5">
                <div class="py-3 flex items-center justify-between">
                    <div class="flex items-center">
                        <a href="<?php echo admin_url('admin.php?page=easy-quote-all'); ?>"
                           class="inline-flex items-center text-gray-600 hover:text-gray-900">
                            <i class="fas fa-arrow-left mr-2"></i>
                            <span>Back to Quotes</span>
                        </a>
                    </div>
                    <h1 class="text-lg font-semibold text-gray-800">
                        <?php
                        if ($quote_id && $quote) {
                            $title = $quote->title ?: $quote->number ?: 'Untitled Quote';
                            echo esc_html($title);
                        } else {
                            echo 'Create New Quote';
                        }
                        ?>
                    </h1>
                    <div class="flex items-center space-x-4">
                        <button type="button" id="save-quote-btn" class="inline-flex items-center px-4 py-2 border border-gray-300 shadow-sm text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
                            <i class="fas fa-save mr-2"></i>
                            <span><?php echo (isset($_GET['id']) ? __('Update Quote', 'easy-invoice') : __('Save Quote', 'easy-invoice')); ?></span>
                        </button>
                        <button type="button" id="send-quote-btn" class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
                            <i class="fas fa-paper-plane mr-2"></i>
                            <span>Send Quote</span>
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <!-- Main Content -->
        <div class="flex-grow overflow-y-auto">
            <div class="max-w-full mx-auto px-5 py-5">
                <div id="ei-quote-builder-grid" class="grid grid-cols-1 lg:grid-cols-2 gap-8">
                    <!-- Left Panel - Editable Fields -->

                    <?php
                        include_once EASY_INVOICE_PLUGIN_DIR . 'templates/quotes/form.php';

                        include_once EASY_INVOICE_PLUGIN_DIR . 'templates/quotes/live-preview.php';
                    ?>
                </div>

                <!-- Add Client Modal -->
                <div id="add_client_modal" class="hidden fixed inset-0 bg-gray-600 bg-opacity-75 overflow-y-auto h-full w-full z-50">
                    <div class="relative top-20 mx-auto p-5 border w-11/12 md:w-2/3 lg:w-1/2 shadow-lg rounded-md bg-white">
                        <div class="flex justify-between items-center mb-4">
                            <h3 class="text-lg font-medium text-gray-900">Add New Client</h3>
                            <button type="button" class="close-modal text-gray-400 hover:text-gray-500">
                                <span class="sr-only">Close</span>
                                <i class="fas fa-times"></i>
                            </button>
                        </div>

                        <?php
                        // Unset the $client variable from the foreach loop to ensure clean add form
                        unset($client);
                        // Also unset any client variable that might have been set in form.php
                        if (isset($client)) {
                            unset($client);
                        }
                        include_once EASY_INVOICE_PLUGIN_DIR . 'templates/client-form.php';
                        ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>




```
