| 1 |
<?php |
| 2 |
/** |
| 3 |
* Invoice Field Registration Class |
| 4 |
* |
| 5 |
* @package EasyInvoice |
| 6 |
* @author Your Name |
| 7 |
* @copyright Copyright (c) 2023, Your Company |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace EasyInvoice\Forms\Invoice; |
| 13 |
|
| 14 |
use EasyInvoice\Forms\BaseFieldRegistration; |
| 15 |
|
| 16 |
/** |
| 17 |
* Invoice Field Registration |
| 18 |
* |
| 19 |
* Handles registration of invoice-specific form fields, tabs, and templates. |
| 20 |
* |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
class InvoiceFieldRegistration extends BaseFieldRegistration { |
| 24 |
|
| 25 |
/** |
| 26 |
* Register default tabs |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
public function registerDefaultTabs(): void { |
| 32 |
// Define all tabs in a single array for easy modification by pro plugins |
| 33 |
$all_tabs = $this->getDefaultTabsArray(); |
| 34 |
|
| 35 |
// Apply filter to allow pro plugins to modify tabs |
| 36 |
$all_tabs = apply_filters('easy_invoice_invoice_form_tabs', $all_tabs); |
| 37 |
|
| 38 |
// Register all tabs from the array |
| 39 |
foreach ($all_tabs as $tab_id => $tab_config) { |
| 40 |
$this->registerTab($tab_id, $tab_config); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get default tabs array |
| 46 |
* |
| 47 |
* @since 1.0.0 |
| 48 |
* @return array |
| 49 |
*/ |
| 50 |
private function getDefaultTabsArray(): array { |
| 51 |
return [ |
| 52 |
'invoice-tab' => [ |
| 53 |
'label' => __('Invoice Details', 'easy-invoice'), |
| 54 |
'icon' => 'fas fa-file-invoice', |
| 55 |
'active' => true, |
| 56 |
'order' => 1 |
| 57 |
], |
| 58 |
'items-tab' => [ |
| 59 |
'label' => __('Items', 'easy-invoice'), |
| 60 |
'icon' => 'fas fa-list', |
| 61 |
'active' => false, |
| 62 |
'order' => 2 |
| 63 |
], |
| 64 |
'client-tab' => [ |
| 65 |
'label' => __('Client', 'easy-invoice'), |
| 66 |
'icon' => 'fas fa-user', |
| 67 |
'active' => false, |
| 68 |
'order' => 3 |
| 69 |
], |
| 70 |
'discounts-taxes-tab' => [ |
| 71 |
'label' => __('Discounts & Taxes', 'easy-invoice'), |
| 72 |
'icon' => 'fas fa-percentage', |
| 73 |
'active' => false, |
| 74 |
'order' => 4 |
| 75 |
], |
| 76 |
'payments-tab' => [ |
| 77 |
'label' => __('Payment', 'easy-invoice'), |
| 78 |
'icon' => 'fas fa-credit-card', |
| 79 |
'active' => false, |
| 80 |
'order' => 5 |
| 81 |
] |
| 82 |
]; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Register default fields |
| 87 |
* |
| 88 |
* @since 1.0.0 |
| 89 |
* @return void |
| 90 |
*/ |
| 91 |
public function registerDefaultFields(): void { |
| 92 |
// Define all fields in a single array for easy modification by pro plugins |
| 93 |
$all_fields = $this->getDefaultFieldsArray(); |
| 94 |
|
| 95 |
// Gate the basic tax fields on the global Settings → Tax → |
| 96 |
// Enable Tax setting. When tax is globally off, the entire |
| 97 |
// tax block (override checkbox, tax rate, prices include tax) |
| 98 |
// is hidden from the invoice builder — the merchant has to |
| 99 |
// turn tax on globally before any tax controls surface here. |
| 100 |
// discount_calculation_method stays (it lives in the discount |
| 101 |
// section, controls discount timing, not tax). |
| 102 |
if (get_option('easy_invoice_tax_enabled', 'no') !== 'yes') { |
| 103 |
$tax_field_names = ['tax_enabled', 'tax_rate', 'prices_include_tax']; |
| 104 |
$all_fields = array_values(array_filter( |
| 105 |
$all_fields, |
| 106 |
static fn($f) => !in_array($f['name'] ?? '', $tax_field_names, true) |
| 107 |
)); |
| 108 |
} |
| 109 |
|
| 110 |
// Apply filter to allow pro plugins to modify fields |
| 111 |
$all_fields = apply_filters('easy_invoice_invoice_fields', $all_fields); |
| 112 |
|
| 113 |
// Register all fields from the array |
| 114 |
foreach ($all_fields as $field_config) { |
| 115 |
$this->registerField($field_config['tab_id'], $field_config); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Get default fields array |
| 121 |
* |
| 122 |
* @since 1.0.0 |
| 123 |
* @return array |
| 124 |
*/ |
| 125 |
private function getDefaultFieldsArray(): array { |
| 126 |
return [ |
| 127 |
// Invoice Tab Fields |
| 128 |
[ |
| 129 |
'tab_id' => 'invoice-tab', |
| 130 |
'name' => 'invoice_template', |
| 131 |
'type' => 'hidden', |
| 132 |
'label' => __('Invoice Template', 'easy-invoice'), |
| 133 |
'required' => false, |
| 134 |
'grid_cols' => 'sm:col-span-6', |
| 135 |
'order' => 0, |
| 136 |
'default_value' => 'standard', |
| 137 |
'sanitize_callback' => 'sanitize_text_field', |
| 138 |
'validate_callback' => function($value) { |
| 139 |
return !empty(trim($value)) ? true : __('Invoice template is required.', 'easy-invoice'); |
| 140 |
} |
| 141 |
], |
| 142 |
[ |
| 143 |
'tab_id' => 'invoice-tab', |
| 144 |
'name' => 'title', |
| 145 |
'type' => 'text', |
| 146 |
'label' => __('Invoice Title', 'easy-invoice'), |
| 147 |
'placeholder' => __('Enter invoice title', 'easy-invoice'), |
| 148 |
'required' => true, |
| 149 |
'grid_cols' => 'sm:col-span-6', |
| 150 |
'order' => 1, |
| 151 |
'sanitize_callback' => 'sanitize_text_field', |
| 152 |
'validate_callback' => function($value) { |
| 153 |
return !empty(trim($value)) ? true : __('Invoice title is required.', 'easy-invoice'); |
| 154 |
} |
| 155 |
], |
| 156 |
[ |
| 157 |
'tab_id' => 'invoice-tab', |
| 158 |
'name' => 'description', |
| 159 |
'type' => 'textarea', |
| 160 |
'label' => __('Invoice Description', 'easy-invoice'), |
| 161 |
'placeholder' => __('Enter invoice description or additional details', 'easy-invoice'), |
| 162 |
'required' => false, |
| 163 |
'grid_cols' => 'sm:col-span-6', |
| 164 |
'rows' => 3, |
| 165 |
'order' => 2, |
| 166 |
'description' => __('Optional description or additional details about this invoice.', 'easy-invoice'), |
| 167 |
'sanitize_callback' => 'sanitize_textarea_field', |
| 168 |
], |
| 169 |
[ |
| 170 |
'tab_id' => 'invoice-tab', |
| 171 |
'name' => 'number', |
| 172 |
'type' => 'text', |
| 173 |
'label' => __('Invoice Number', 'easy-invoice'), |
| 174 |
'placeholder' => __('INV-001', 'easy-invoice'), |
| 175 |
'required' => true, |
| 176 |
'readonly' => true, |
| 177 |
'description' => __('This is the unique number for your invoice. It cannot be changed.', 'easy-invoice'), |
| 178 |
'grid_cols' => 'sm:col-span-3', |
| 179 |
'order' => 3, |
| 180 |
'default_value' => function() { |
| 181 |
// For new invoices, show the next number without incrementing |
| 182 |
// For existing invoices, this will be overridden by the invoice object's getNumber() method |
| 183 |
if (class_exists('\\EasyInvoice\\Services\\InvoiceNumberService')) { |
| 184 |
$service = new \EasyInvoice\Services\InvoiceNumberService(); |
| 185 |
return $service->getNextNumber(); |
| 186 |
} |
| 187 |
return 'INV-' . str_pad(time(), 6, '0', STR_PAD_LEFT); |
| 188 |
}, |
| 189 |
'sanitize_callback' => 'sanitize_text_field', |
| 190 |
'validate_callback' => function($value) { |
| 191 |
return !empty(trim($value)) ? true : __('Invoice number is required.', 'easy-invoice'); |
| 192 |
} |
| 193 |
], |
| 194 |
[ |
| 195 |
'tab_id' => 'invoice-tab', |
| 196 |
'name' => 'issue_date', |
| 197 |
'type' => 'date', |
| 198 |
'label' => __('Issue Date', 'easy-invoice'), |
| 199 |
'required' => true, |
| 200 |
'grid_cols' => 'sm:col-span-3', |
| 201 |
'order' => 4, |
| 202 |
'sanitize_callback' => 'sanitize_text_field', |
| 203 |
'validate_callback' => function($value) { |
| 204 |
return !empty($value) && strtotime($value) ? true : __('Please enter a valid issue date.', 'easy-invoice'); |
| 205 |
} |
| 206 |
], |
| 207 |
[ |
| 208 |
'tab_id' => 'invoice-tab', |
| 209 |
'name' => 'due_date', |
| 210 |
'type' => 'date', |
| 211 |
'label' => __('Due Date', 'easy-invoice'), |
| 212 |
'required' => true, |
| 213 |
'grid_cols' => 'sm:col-span-3', |
| 214 |
'order' => 5, |
| 215 |
'sanitize_callback' => 'sanitize_text_field', |
| 216 |
'validate_callback' => function($value) { |
| 217 |
return !empty($value) && strtotime($value) ? true : __('Please enter a valid due date.', 'easy-invoice'); |
| 218 |
} |
| 219 |
], |
| 220 |
[ |
| 221 |
'tab_id' => 'invoice-tab', |
| 222 |
'name' => 'status', |
| 223 |
'type' => 'select', |
| 224 |
'label' => __('Status', 'easy-invoice'), |
| 225 |
'required' => true, |
| 226 |
'grid_cols' => 'sm:col-span-3', |
| 227 |
'options' => [ |
| 228 |
'available' => __('Available', 'easy-invoice'), |
| 229 |
'draft' => __('Draft', 'easy-invoice'), |
| 230 |
'overdue' => __('Overdue', 'easy-invoice'), |
| 231 |
'paid' => __('Paid', 'easy-invoice'), |
| 232 |
'unpaid' => __('Unpaid', 'easy-invoice'), |
| 233 |
'cancelled' => __('Cancelled', 'easy-invoice') |
| 234 |
], |
| 235 |
'order' => 6 |
| 236 |
], |
| 237 |
[ |
| 238 |
'tab_id' => 'invoice-tab', |
| 239 |
'name' => 'notes', |
| 240 |
'type' => 'textarea', |
| 241 |
'label' => __('Notes', 'easy-invoice'), |
| 242 |
'placeholder' => __('Additional notes for the client', 'easy-invoice'), |
| 243 |
'grid_cols' => 'sm:col-span-6', |
| 244 |
'rows' => 3, |
| 245 |
'section' => 'notes', |
| 246 |
'order' => 6, |
| 247 |
'description' => __('These notes will be visible to your client on the invoice.', 'easy-invoice'), |
| 248 |
], |
| 249 |
[ |
| 250 |
'tab_id' => 'invoice-tab', |
| 251 |
'name' => 'internal_notes', |
| 252 |
'type' => 'textarea', |
| 253 |
'label' => __('Internal Notes', 'easy-invoice'), |
| 254 |
'placeholder' => __('Internal notes (not visible to client)', 'easy-invoice'), |
| 255 |
'grid_cols' => 'sm:col-span-6', |
| 256 |
'rows' => 3, |
| 257 |
'section' => 'notes', |
| 258 |
'order' => 7, |
| 259 |
'description' => __('Only you and your team can see these notes. Clients will not see them.', 'easy-invoice'), |
| 260 |
], |
| 261 |
[ |
| 262 |
'tab_id' => 'invoice-tab', |
| 263 |
'name' => 'terms', |
| 264 |
'type' => 'textarea', |
| 265 |
'label' => __('Payment Terms', 'easy-invoice'), |
| 266 |
'placeholder' => __('Payment terms and conditions', 'easy-invoice'), |
| 267 |
'grid_cols' => 'sm:col-span-6', |
| 268 |
'rows' => 3, |
| 269 |
'section' => 'notes', |
| 270 |
'order' => 8, |
| 271 |
'default_value' => function() { |
| 272 |
return \EasyInvoice\Controllers\SettingsController::getInvoiceTermsConditions(); |
| 273 |
}, |
| 274 |
'description' => __('Specify the payment terms and conditions for this invoice.', 'easy-invoice'), |
| 275 |
], |
| 276 |
|
| 277 |
|
| 278 |
// Client Tab Fields |
| 279 |
[ |
| 280 |
'tab_id' => 'client-tab', |
| 281 |
'name' => 'client_id', |
| 282 |
'type' => 'select', |
| 283 |
'label' => __('Select Client', 'easy-invoice'), |
| 284 |
'required' => false, |
| 285 |
'grid_cols' => 'sm:col-span-6', |
| 286 |
'options' => $this->getClientOptions(), |
| 287 |
'order' => 1, |
| 288 |
'description' => __('Choose an existing client or add a new one.', 'easy-invoice'), |
| 289 |
], |
| 290 |
|
| 291 |
// Discounts & Taxes Tab Fields |
| 292 |
[ |
| 293 |
'tab_id' => 'discounts-taxes-tab', |
| 294 |
'name' => 'discount_type', |
| 295 |
'type' => 'select', |
| 296 |
'label' => __('Discount Type', 'easy-invoice'), |
| 297 |
'grid_cols' => 'sm:col-span-2', |
| 298 |
'section' => 'discount', |
| 299 |
'options' => [ |
| 300 |
'none' => __('No Discount', 'easy-invoice'), |
| 301 |
'percentage' => __('Percentage', 'easy-invoice'), |
| 302 |
'fixed' => __('Fixed Amount', 'easy-invoice') |
| 303 |
], |
| 304 |
'order' => 1, |
| 305 |
'description' => __('Choose how the discount should be applied to this invoice.', 'easy-invoice'), |
| 306 |
], |
| 307 |
[ |
| 308 |
'tab_id' => 'discounts-taxes-tab', |
| 309 |
'name' => 'discount_value', |
| 310 |
'type' => 'number', |
| 311 |
'label' => __('Discount Value', 'easy-invoice'), |
| 312 |
'placeholder' => '0.00', |
| 313 |
'grid_cols' => 'sm:col-span-2', |
| 314 |
'section' => 'discount', |
| 315 |
'min' => '0', |
| 316 |
'step' => '0.01', |
| 317 |
'order' => 2, |
| 318 |
'sanitize_callback' => 'floatval', |
| 319 |
'validate_callback' => function($value) { |
| 320 |
return is_numeric($value) && $value >= 0 ? true : __('Discount value must be a non-negative number.', 'easy-invoice'); |
| 321 |
}, |
| 322 |
'description' => __('Enter the discount amount or percentage for this invoice.', 'easy-invoice'), |
| 323 |
], |
| 324 |
[ |
| 325 |
// Per-invoice "Enable Tax" override. |
| 326 |
// |
| 327 |
// Defaults to the global Settings → Tax → Enable Tax |
| 328 |
// checkbox value. The user can flip it for a specific |
| 329 |
// invoice — handy for tax-exempt customers (override |
| 330 |
// "on" globally → "off" here) or one-off taxable |
| 331 |
// invoices when tax is globally off. |
| 332 |
// |
| 333 |
// calculateTotals() in the Invoice model treats the |
| 334 |
// tax_rate as 0 when this is unchecked, regardless of |
| 335 |
// what value sits in the tax_rate field — so the rest |
| 336 |
// of this section can stay visible without applying |
| 337 |
// tax until the user opts in. |
| 338 |
'tab_id' => 'discounts-taxes-tab', |
| 339 |
'name' => 'tax_enabled', |
| 340 |
'type' => 'checkbox', |
| 341 |
'label' => __('Apply Tax to This Invoice', 'easy-invoice'), |
| 342 |
'grid_cols' => 'sm:col-span-6', |
| 343 |
'section' => 'tax', |
| 344 |
'order' => 2.5, |
| 345 |
'default_value' => function() { |
| 346 |
// Default to global setting. Saved values override. |
| 347 |
return get_option('easy_invoice_tax_enabled', 'no') === 'yes' ? 'yes' : 'no'; |
| 348 |
}, |
| 349 |
'sanitize_callback' => function($value) { |
| 350 |
// Coerce any truthy input ('yes', '1', 1, true, 'on') |
| 351 |
// to 'yes'; anything else to 'no'. Stored as a |
| 352 |
// string so the field-save loop persists '' as 'no' |
| 353 |
// (PHP checkbox unchecked = field absent from POST). |
| 354 |
if ($value === 'yes' || $value === '1' || $value === 1 || $value === true || $value === 'on') return 'yes'; |
| 355 |
return 'no'; |
| 356 |
}, |
| 357 |
'description' => __('Override the global tax setting for this invoice. Defaults to whatever is configured in Settings → Tax.', 'easy-invoice'), |
| 358 |
], |
| 359 |
// Discount calculation timing — belongs in the discount |
| 360 |
// section (it controls when the discount applies relative |
| 361 |
// to tax, not how tax is calculated). Pinned next to the |
| 362 |
// Discount Type / Value fields above so the grouping is |
| 363 |
// intuitive. |
| 364 |
[ |
| 365 |
'tab_id' => 'discounts-taxes-tab', |
| 366 |
'name' => 'discount_calculation_method', |
| 367 |
'type' => 'select', |
| 368 |
'label' => __('Discount Timing', 'easy-invoice'), |
| 369 |
'grid_cols' => 'sm:col-span-2', |
| 370 |
'section' => 'discount', |
| 371 |
'options' => [ |
| 372 |
'before_tax' => __('Before Tax', 'easy-invoice'), |
| 373 |
'after_tax' => __('After Tax', 'easy-invoice') |
| 374 |
], |
| 375 |
'order' => 3, |
| 376 |
'description' => __('Apply discount before tax (reduces taxable amount) or after tax (tax on full amount)', 'easy-invoice'), |
| 377 |
], |
| 378 |
|
| 379 |
// Tax sub-fields below sit on one row (2 + 2 + 2 = 6 cols) |
| 380 |
// so the user sees Tax Rate and Price Includes Tax |
| 381 |
// side-by-side under the Apply Tax checkbox. |
| 382 |
[ |
| 383 |
'tab_id' => 'discounts-taxes-tab', |
| 384 |
'name' => 'tax_rate', |
| 385 |
'type' => 'number', |
| 386 |
'label' => __('Tax Rate (%)', 'easy-invoice'), |
| 387 |
'placeholder' => '0.00', |
| 388 |
'grid_cols' => 'sm:col-span-3', |
| 389 |
'section' => 'tax', |
| 390 |
'min' => '0', |
| 391 |
'max' => '100', |
| 392 |
'step' => '0.01', |
| 393 |
'order' => 4, |
| 394 |
'default_value' => function() { |
| 395 |
return easy_invoice_get_tax_rate(); |
| 396 |
}, |
| 397 |
'sanitize_callback' => 'floatval', |
| 398 |
'validate_callback' => function($value) { |
| 399 |
return is_numeric($value) && $value >= 0 && $value <= 100 ? true : __('Tax rate must be between 0 and 100.', 'easy-invoice'); |
| 400 |
}, |
| 401 |
'description' => __('The tax rate that will be applied to taxable items in this invoice.', 'easy-invoice'), |
| 402 |
], |
| 403 |
|
| 404 |
[ |
| 405 |
'tab_id' => 'discounts-taxes-tab', |
| 406 |
'name' => 'prices_include_tax', |
| 407 |
'type' => 'select', |
| 408 |
'label' => __('Price Includes Tax', 'easy-invoice'), |
| 409 |
'grid_cols' => 'sm:col-span-3', |
| 410 |
'section' => 'tax', |
| 411 |
'options' => [ |
| 412 |
'no' => __('No', 'easy-invoice'), |
| 413 |
'yes' => __('Yes', 'easy-invoice') |
| 414 |
], |
| 415 |
'order' => 5, |
| 416 |
'description' => __('Choose whether the prices in this invoice include tax or not.', 'easy-invoice'), |
| 417 |
], |
| 418 |
|
| 419 |
// Payments Tab Fields |
| 420 |
[ |
| 421 |
'tab_id' => 'payments-tab', |
| 422 |
'name' => 'payment_gateways', |
| 423 |
'type' => 'payment_gateways', |
| 424 |
'label' => __('Payment Gateways', 'easy-invoice'), |
| 425 |
'grid_cols' => 'sm:col-span-12', |
| 426 |
'section' => 'payment', |
| 427 |
'order' => 1 |
| 428 |
], |
| 429 |
[ |
| 430 |
'tab_id' => 'payments-tab', |
| 431 |
'name' => 'payment_gateways_toggle_state', |
| 432 |
'type' => 'hidden', |
| 433 |
'label' => __('Payment Gateways Toggle State', 'easy-invoice'), |
| 434 |
'default_value' => '0', |
| 435 |
'section' => 'payment', |
| 436 |
'order' => 2 |
| 437 |
], |
| 438 |
|
| 439 |
// Currency Section in Payment Tab |
| 440 |
[ |
| 441 |
'tab_id' => 'payments-tab', |
| 442 |
'name' => 'currency_code', |
| 443 |
'type' => 'select', |
| 444 |
'label' => __('Currency', 'easy-invoice'), |
| 445 |
'grid_cols' => 'sm:col-span-3', |
| 446 |
'section' => 'currency', |
| 447 |
'options' => array_merge( |
| 448 |
['global' => __('Use from Global Settings', 'easy-invoice')], |
| 449 |
\EasyInvoice\Helpers\CurrencyHelper::getCurrencyOptions() |
| 450 |
), |
| 451 |
'default_value' => 'global', |
| 452 |
'order' => 2, |
| 453 |
'description' => __('Select the currency for this invoice or use global settings.', 'easy-invoice'), |
| 454 |
], |
| 455 |
[ |
| 456 |
'tab_id' => 'payments-tab', |
| 457 |
'name' => 'currency_position', |
| 458 |
'type' => 'select', |
| 459 |
'label' => __('Currency Symbol Position', 'easy-invoice'), |
| 460 |
'grid_cols' => 'sm:col-span-3', |
| 461 |
'section' => 'currency', |
| 462 |
'options' => [ |
| 463 |
'global' => __('Use from Global Settings', 'easy-invoice'), |
| 464 |
'before' => __('Before Amount', 'easy-invoice'), |
| 465 |
'after' => __('After Amount', 'easy-invoice'), |
| 466 |
], |
| 467 |
'default_value' => 'global', |
| 468 |
'order' => 3, |
| 469 |
'description' => __('Choose where to display the currency symbol or use global setting.', 'easy-invoice'), |
| 470 |
], |
| 471 |
]; |
| 472 |
} |
| 473 |
|
| 474 |
/** |
| 475 |
* Register default templates |
| 476 |
* |
| 477 |
* @since 1.0.0 |
| 478 |
* @return void |
| 479 |
*/ |
| 480 |
public function registerDefaultTemplates(): void { |
| 481 |
// Define all templates in a single array for easy modification by pro plugins |
| 482 |
$all_templates = $this->getDefaultTemplatesArray(); |
| 483 |
|
| 484 |
// Apply filter to allow pro plugins to modify templates |
| 485 |
$all_templates = apply_filters('easy_invoice_invoice_templates', $all_templates); |
| 486 |
|
| 487 |
// Register all templates from the array |
| 488 |
foreach ($all_templates as $template_id => $template_config) { |
| 489 |
$this->registerTemplate($template_id, $template_config); |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
/** |
| 494 |
* Get default templates array |
| 495 |
* |
| 496 |
* @since 1.0.0 |
| 497 |
* @return array |
| 498 |
*/ |
| 499 |
private function getDefaultTemplatesArray(): array { |
| 500 |
// Check if this is the free version (no pro plugin active) |
| 501 |
$is_free_version = !easy_invoice_has_pro(); |
| 502 |
|
| 503 |
return [ |
| 504 |
'standard' => [ |
| 505 |
'name' => __('Standard', 'easy-invoice'), |
| 506 |
'icon' => 'fas fa-file-invoice', |
| 507 |
'description' => __('Clean and professional standard template', 'easy-invoice'), |
| 508 |
'order' => 1, |
| 509 |
'is_free' => true |
| 510 |
], |
| 511 |
'legacy' => [ |
| 512 |
'name' => __('Legacy', 'easy-invoice'), |
| 513 |
'icon' => 'fas fa-file-alt', |
| 514 |
'description' => __('Clean, minimalist design with traditional business layout', 'easy-invoice'), |
| 515 |
'order' => 2, |
| 516 |
'is_free' => true |
| 517 |
], |
| 518 |
'professional' => [ |
| 519 |
'name' => __('Professional', 'easy-invoice'), |
| 520 |
'icon' => 'fas fa-briefcase', |
| 521 |
'description' => __('Professional business template with modern design', 'easy-invoice'), |
| 522 |
'order' => 3, |
| 523 |
'is_free' => !$is_free_version |
| 524 |
], |
| 525 |
'modern' => [ |
| 526 |
'name' => __('Modern', 'easy-invoice'), |
| 527 |
'icon' => 'fas fa-rocket', |
| 528 |
'description' => __('Modern and sleek design template', 'easy-invoice'), |
| 529 |
'order' => 4, |
| 530 |
'is_free' => !$is_free_version |
| 531 |
] |
| 532 |
]; |
| 533 |
} |
| 534 |
|
| 535 |
/** |
| 536 |
* Register default item fields |
| 537 |
* |
| 538 |
* @since 1.0.0 |
| 539 |
* @return void |
| 540 |
*/ |
| 541 |
public function registerDefaultItemFields(): void { |
| 542 |
// Define all item fields in a single array for easy modification by pro plugins |
| 543 |
$all_item_fields = $this->getDefaultItemFieldsArray(); |
| 544 |
|
| 545 |
// Apply filter to allow pro plugins to modify item fields |
| 546 |
$all_item_fields = apply_filters('easy_invoice_invoice_item_fields', $all_item_fields); |
| 547 |
|
| 548 |
// Register all item fields from the array |
| 549 |
foreach ($all_item_fields as $field_config) { |
| 550 |
$this->registerItemField($field_config); |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
/** |
| 555 |
* Get default item fields array |
| 556 |
* |
| 557 |
* @since 1.0.0 |
| 558 |
* @return array |
| 559 |
*/ |
| 560 |
private function getDefaultItemFieldsArray(): array { |
| 561 |
$fields = [ |
| 562 |
[ |
| 563 |
'name' => 'title', |
| 564 |
'type' => 'text', |
| 565 |
'label' => __('Item Title', 'easy-invoice'), |
| 566 |
'placeholder' => __('Enter item title', 'easy-invoice'), |
| 567 |
'required' => true, |
| 568 |
'container_class' => 'sm:col-span-6', |
| 569 |
'order' => 1, |
| 570 |
'description' => __('A short name for this item (e.g. Service Fee, Product Name).', 'easy-invoice'), |
| 571 |
], |
| 572 |
[ |
| 573 |
'name' => 'description', |
| 574 |
'type' => 'textarea', |
| 575 |
'label' => __('Description', 'easy-invoice'), |
| 576 |
'placeholder' => __('Enter item description', 'easy-invoice'), |
| 577 |
'container_class' => 'sm:col-span-6', |
| 578 |
'rows' => 3, |
| 579 |
'order' => 2, |
| 580 |
'description' => __('Details about this item for your client.', 'easy-invoice'), |
| 581 |
], |
| 582 |
[ |
| 583 |
'name' => 'quantity', |
| 584 |
'type' => 'number', |
| 585 |
'label' => __('Quantity', 'easy-invoice'), |
| 586 |
'placeholder' => '0', |
| 587 |
'required' => true, |
| 588 |
'container_class' => 'sm:col-span-1', |
| 589 |
'min' => '0', |
| 590 |
'step' => '0.01', |
| 591 |
'default_value' => '0', |
| 592 |
'order' => 3, |
| 593 |
'description' => __('How many units of this item are being billed?', 'easy-invoice'), |
| 594 |
], |
| 595 |
[ |
| 596 |
'name' => 'price', |
| 597 |
'type' => 'number', |
| 598 |
'label' => __('Price', 'easy-invoice'), |
| 599 |
'placeholder' => '0.00', |
| 600 |
'required' => true, |
| 601 |
'container_class' => 'sm:col-span-1', |
| 602 |
'min' => '0', |
| 603 |
'step' => '0.01', |
| 604 |
'default_value' => '0.00', |
| 605 |
'order' => 4, |
| 606 |
'description' => __('Unit price for this item.', 'easy-invoice'), |
| 607 |
] |
| 608 |
]; |
| 609 |
|
| 610 |
// Add adjust field only if setting allows it |
| 611 |
if (\EasyInvoice\Controllers\SettingsController::shouldShowInvoiceAdjustField()) { |
| 612 |
$fields[] = [ |
| 613 |
'name' => 'adjust_percentage', |
| 614 |
'type' => 'number', |
| 615 |
'label' => __('Adjust (%)', 'easy-invoice'), |
| 616 |
'placeholder' => '0.00', |
| 617 |
'container_class' => 'sm:col-span-1', |
| 618 |
'min' => '-100', |
| 619 |
'max' => '1000', |
| 620 |
'step' => '0.01', |
| 621 |
'default_value' => '0.00', |
| 622 |
'order' => 5, |
| 623 |
'description' => __('Percentage adjustment to apply to this item (positive for markup, negative for discount).', 'easy-invoice'), |
| 624 |
]; |
| 625 |
} |
| 626 |
|
| 627 |
// Add total field |
| 628 |
$fields[] = [ |
| 629 |
'name' => 'total', |
| 630 |
'type' => 'number', |
| 631 |
'label' => __('Total', 'easy-invoice'), |
| 632 |
'placeholder' => '0.00', |
| 633 |
'container_class' => 'sm:col-span-1', |
| 634 |
'min' => '0', |
| 635 |
'step' => '0.01', |
| 636 |
'default_value' => '0.00', |
| 637 |
'readonly' => true, |
| 638 |
'order' => \EasyInvoice\Controllers\SettingsController::shouldShowInvoiceAdjustField() ? 6 : 5, |
| 639 |
'description' => __('Total amount for this item (Quantity x Price) with adjustment applied.', 'easy-invoice'), |
| 640 |
]; |
| 641 |
|
| 642 |
// Add taxable field |
| 643 |
$fields[] = [ |
| 644 |
'name' => 'taxable', |
| 645 |
'type' => 'checkbox', |
| 646 |
'label' => __('Taxable', 'easy-invoice'), |
| 647 |
'container_class' => 'sm:col-span-6', |
| 648 |
'default_value' => '1', |
| 649 |
'order' => \EasyInvoice\Controllers\SettingsController::shouldShowInvoiceAdjustField() ? 7 : 6, |
| 650 |
'description' => __('Check if this item is subject to tax.', 'easy-invoice'), |
| 651 |
]; |
| 652 |
|
| 653 |
return $fields; |
| 654 |
} |
| 655 |
|
| 656 |
/** |
| 657 |
* Get client options for select field |
| 658 |
* |
| 659 |
* @since 1.0.0 |
| 660 |
* @return array |
| 661 |
*/ |
| 662 |
private function getClientOptions(): array { |
| 663 |
$options = ['' => __('Select a client', 'easy-invoice')]; |
| 664 |
|
| 665 |
// Get clients from repository |
| 666 |
$client_repository = new \EasyInvoice\Repositories\ClientRepository(); |
| 667 |
$clients = $client_repository->all(); |
| 668 |
|
| 669 |
foreach ($clients as $client) { |
| 670 |
$client_name = $client->getBusinessClientName() ?: ($client->getFirstName() . ' ' . $client->getLastName()); |
| 671 |
$options[$client->getId()] = $client_name; |
| 672 |
} |
| 673 |
|
| 674 |
return $options; |
| 675 |
} |
| 676 |
|
| 677 |
/** |
| 678 |
* Get the filter name for tabs |
| 679 |
* |
| 680 |
* @since 1.0.0 |
| 681 |
* @return string |
| 682 |
*/ |
| 683 |
protected function getTabFilterName(): string { |
| 684 |
return 'easy_invoice_invoice_register_tab'; |
| 685 |
} |
| 686 |
|
| 687 |
/** |
| 688 |
* Get the filter name for fields |
| 689 |
* |
| 690 |
* @since 1.0.0 |
| 691 |
* @return string |
| 692 |
*/ |
| 693 |
protected function getFieldFilterName(): string { |
| 694 |
return 'easy_invoice_invoice_register_field'; |
| 695 |
} |
| 696 |
|
| 697 |
/** |
| 698 |
* Get the filter name for templates |
| 699 |
* |
| 700 |
* @since 1.0.0 |
| 701 |
* @return string |
| 702 |
*/ |
| 703 |
protected function getTemplateFilterName(): string { |
| 704 |
return 'easy_invoice_invoice_register_template'; |
| 705 |
} |
| 706 |
|
| 707 |
/** |
| 708 |
* Get the filter name for item fields |
| 709 |
* |
| 710 |
* @since 1.0.0 |
| 711 |
* @return string |
| 712 |
*/ |
| 713 |
protected function getItemFieldFilterName(): string { |
| 714 |
return 'easy_invoice_invoice_register_item_field'; |
| 715 |
} |
| 716 |
} |