0 ) { $ei_builder_post = get_post( $invoice_id ); if ( ! $ei_builder_post || \EasyInvoice\Constants\PostTypes::EASY_INVOICE_POST_TYPE !== $ei_builder_post->post_type ) { wp_die( esc_html__( 'That invoice does not exist or has been deleted.', 'easy-invoice' ), esc_html__( 'Invoice not found', 'easy-invoice' ), array( 'back_link' => true, 'response' => 404 ) ); } } $invoice = null; // Check if this is a new invoice (no ID) $is_new_invoice = ($invoice_id === 0); // Default values for new invoice $invoice_number_service = easy_invoice_get_invoice_number_service(); $invoice_data = array( 'number' => $invoice_number_service->getNextNumber(), 'date' => current_time('Y-m-d'), 'due_date' => wp_date('Y-m-d', strtotime('+30 days')), 'client_id' => '', 'client_name' => '', 'client_email' => '', 'client_phone' => '', 'client_address' => '', 'items' => array(), 'notes' => '', 'internal_notes' => '', 'discount' => 0, 'discount_type' => 'percentage', 'calculation_method' => 'before_tax', 'tax_rate' => 10, 'prices_include_tax' => 'no', 'payment_method' => 'bank_transfer', 'payment_status' => 'unpaid', 'currency' => 'USD', 'currency_symbol' => '$', 'title' => '', 'description' => '', 'terms' => '', 'is_recurring' => 'no', 'recurring_frequency' => '', ); // Load clients for the dropdown $client_repository = ClientServiceProvider::getClientRepository(); // The hidden #select-client mirror only needs the document's own client; the picker // searches over AJAX. Loading every client here built a model per user on each open. $clients = []; // If editing an existing invoice, load its data if ($invoice_id > 0) { // Get the invoice from repository $invoice_repository = InvoiceServiceProvider::getInvoiceRepository(); $invoice = $invoice_repository->find($invoice_id); } else { // Create a temporary WP_Post object for new invoice $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' => $invoice_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_POST_TYPE, 'post_mime_type' => '', 'comment_count' => 0, 'filter' => 'raw', )); $invoice = new \EasyInvoice\Models\Invoice($empty_post); // Set default values on the invoice object foreach ($invoice_data as $key => $value) { $setter = 'set' . easy_invoice_str_replace('_', '', ucwords($key, '_')); if (method_exists($invoice, $setter)) { // Handle client_id specially to avoid type errors if ($key === 'client_id' && empty($value)) { $invoice->setClientId(0); } else { $invoice->$setter($value); } } } // Initialize empty items array $invoice->setItems([]); } // Only load custom meta that's not handled by Invoice object if ($invoice) { // These are still accessed from meta as they don't have model methods yet $invoice_data['terms'] = $invoice_id > 0 ? get_post_meta($invoice_id, '_easy_invoice_terms', true) : $invoice_data['terms']; $invoice_data['internal_notes'] = $invoice_id > 0 ? get_post_meta($invoice_id, '_easy_invoice_internal_notes', true) : $invoice_data['internal_notes']; $invoice_data['payment_method'] = $invoice_id > 0 ? get_post_meta($invoice_id, '_easy_invoice_payment_method', true) : $invoice_data['payment_method']; $invoice_data['payment_status'] = $invoice_id > 0 ? get_post_meta($invoice_id, '_easy_invoice_payment_status', true) : $invoice_data['payment_status']; $invoice_data['currency'] = $invoice_id > 0 ? get_post_meta($invoice_id, '_easy_invoice_currency_code', true) : $invoice_data['currency']; $invoice_data['currency_symbol'] = $invoice_id > 0 ? get_post_meta($invoice_id, '_easy_invoice_currency_position', true) : $invoice_data['currency_symbol']; $invoice_data['calculation_method'] = $invoice_id > 0 ? get_post_meta($invoice_id, '_easy_invoice_calculation_method', true) : $invoice_data['calculation_method']; } // Prepare invoice items JSON for JavaScript $invoice_items_json = json_encode($invoice ? $invoice->getItems() : []); // Create nonce for AJAX calls $admin_nonce = wp_create_nonce('easy_invoice_admin_nonce'); // Prepare client data for JavaScript $client_data_json = json_encode($client_data ?? null); // Start output buffering to capture content if ( $invoice_id && $invoice ) { $ei_header_display_title = $invoice->title ?: $invoice->number ?: __( 'Untitled invoice', 'easy-invoice' ); } else { $ei_header_display_title = __( 'Create new invoice', 'easy-invoice' ); } ?>