| 1 |
<?php |
| 2 |
/** |
| 3 |
* Overlay the builder's unsaved form values on a document model so the |
| 4 |
* live preview shows what is being typed, not what was last saved. |
| 5 |
* |
| 6 |
* @package EasyInvoice |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace EasyInvoice\Helpers; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
class PreviewOverlay { |
| 16 |
|
| 17 |
/** |
| 18 |
* @param object $model Invoice or Quote model (may be empty/new). |
| 19 |
* @param string $form_data URL-encoded form serialisation from the builder. |
| 20 |
* @param string $type 'invoice' or 'quote'. |
| 21 |
* @return object The same model, with the posted values applied. |
| 22 |
*/ |
| 23 |
public static function apply( $model, string $form_data, string $type = 'invoice' ) { |
| 24 |
if ( '' === $form_data || ! is_object( $model ) ) { |
| 25 |
return $model; |
| 26 |
} |
| 27 |
parse_str( $form_data, $data ); |
| 28 |
$data = wp_unslash( $data ); |
| 29 |
if ( ! is_array( $data ) ) { |
| 30 |
return $model; |
| 31 |
} |
| 32 |
// Both models strip the same prefix in setMetaData(). |
| 33 |
$prefix = '_easy_invoice_'; |
| 34 |
$scalar = [ 'title', 'number', 'issue_date', 'due_date', 'expiry_date', 'notes', 'terms', 'description', 'footer_text', 'status', |
| 35 |
'discount_type', 'discount_value', 'discount_calculation_method', 'tax_enabled', 'tax_rate', 'prices_include_tax', |
| 36 |
'currency_code', 'customer_name', 'customer_email', 'customer_address', 'customer_vat_number', 'customer_country', 'client_id' ]; |
| 37 |
foreach ( $scalar as $key ) { |
| 38 |
if ( ! array_key_exists( $key, $data ) ) { |
| 39 |
continue; |
| 40 |
} |
| 41 |
$value = is_scalar( $data[ $key ] ) ? sanitize_textarea_field( (string) $data[ $key ] ) : ''; |
| 42 |
if ( is_callable( [ $model, 'setMetaData' ] ) ) { |
| 43 |
$model->setMetaData( $prefix . $key, $value ); |
| 44 |
} |
| 45 |
} |
| 46 |
// A checkbox that is absent from the serialisation is unchecked. |
| 47 |
if ( ! array_key_exists( 'tax_enabled', $data ) && is_callable( [ $model, 'setMetaData' ] ) ) { |
| 48 |
$model->setMetaData( $prefix . 'tax_enabled', '0' ); |
| 49 |
} |
| 50 |
if ( ! empty( $data['items'] ) && is_array( $data['items'] ) ) { |
| 51 |
$items = []; |
| 52 |
foreach ( $data['items'] as $row ) { |
| 53 |
if ( ! is_array( $row ) ) { |
| 54 |
continue; |
| 55 |
} |
| 56 |
$name = sanitize_text_field( (string) ( $row['title'] ?? $row['name'] ?? '' ) ); |
| 57 |
$qty = (float) ( $row['quantity'] ?? 0 ); |
| 58 |
$price = (float) str_replace( ',', '', (string) ( $row['price'] ?? 0 ) ); |
| 59 |
if ( '' === $name && 0.0 === $qty && 0.0 === $price ) { |
| 60 |
continue; |
| 61 |
} |
| 62 |
$items[] = [ |
| 63 |
'name' => $name, |
| 64 |
'title' => $name, |
| 65 |
'description' => sanitize_textarea_field( (string) ( $row['description'] ?? '' ) ), |
| 66 |
'quantity' => $qty, |
| 67 |
'price' => $price, |
| 68 |
'amount' => round( $qty * $price, 2 ), |
| 69 |
'total' => round( $qty * $price, 2 ), |
| 70 |
// The form is posted serialised, so an unchecked box is simply absent — |
| 71 |
// absent means not taxable, as it does when the invoice is saved. |
| 72 |
'taxable' => ! empty( $row['taxable'] ) && '0' !== (string) $row['taxable'] ? '1' : '0', |
| 73 |
'adjust_percentage' => (float) ( $row['adjust_percentage'] ?? $row['adjust'] ?? 0 ), |
| 74 |
]; |
| 75 |
} |
| 76 |
if ( is_callable( [ $model, 'setItems' ] ) ) { |
| 77 |
$model->setItems( $items ); |
| 78 |
} |
| 79 |
} |
| 80 |
if ( ! empty( $data['client_id'] ) && is_callable( [ $model, 'populateClientInfo' ] ) ) { |
| 81 |
$model->populateClientInfo(); |
| 82 |
} |
| 83 |
if ( is_callable( [ $model, 'calculateTotals' ] ) ) { |
| 84 |
$model->calculateTotals(); |
| 85 |
} |
| 86 |
return $model; |
| 87 |
} |
| 88 |
} |
| 89 |
|