| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\PDF; |
| 4 |
|
| 5 |
class ReceiptPdfTemplateService |
| 6 |
{ |
| 7 |
protected $metaKey = 'receipt_pdf_templates'; |
| 8 |
|
| 9 |
/** |
| 10 |
* Get all built-in default templates. |
| 11 |
*/ |
| 12 |
public function getDefaultTemplates(): array |
| 13 |
{ |
| 14 |
return [ |
| 15 |
'order_receipt' => [ |
| 16 |
'name' => 'order_receipt', |
| 17 |
'title' => __('Order Receipt', 'fluent-cart'), |
| 18 |
'description' => __('Default receipt template for paid orders', 'fluent-cart'), |
| 19 |
'is_default' => true, |
| 20 |
'pdf_settings' => [ |
| 21 |
[ |
| 22 |
'active' => 'yes', |
| 23 |
'title' => __('Order Receipt', 'fluent-cart'), |
| 24 |
'preview_image' => '', |
| 25 |
'pdf_structure' => DefaultPdfStructures::getDefaultReceiptStructure(), |
| 26 |
] |
| 27 |
] |
| 28 |
], |
| 29 |
'renewal_receipt' => [ |
| 30 |
'name' => 'renewal_receipt', |
| 31 |
'title' => __('Renewal Receipt', 'fluent-cart'), |
| 32 |
'description' => __('Receipt template for subscription renewal payments', 'fluent-cart'), |
| 33 |
'is_default' => true, |
| 34 |
'pdf_settings' => [ |
| 35 |
[ |
| 36 |
'active' => 'yes', |
| 37 |
'title' => __('Renewal Receipt', 'fluent-cart'), |
| 38 |
'preview_image' => '', |
| 39 |
'pdf_structure' => DefaultPdfStructures::getDefaultRenewalReceiptStructure(), |
| 40 |
] |
| 41 |
] |
| 42 |
], |
| 43 |
'refund_notice' => [ |
| 44 |
'name' => 'refund_notice', |
| 45 |
'title' => __('Refund Notice', 'fluent-cart'), |
| 46 |
'description' => __('Notice template for refund confirmations', 'fluent-cart'), |
| 47 |
'is_default' => true, |
| 48 |
'pdf_settings' => [ |
| 49 |
[ |
| 50 |
'active' => 'yes', |
| 51 |
'title' => __('Refund Notice', 'fluent-cart'), |
| 52 |
'preview_image' => '', |
| 53 |
'pdf_structure' => DefaultPdfStructures::getDefaultRefundNoticeStructure(), |
| 54 |
] |
| 55 |
] |
| 56 |
], |
| 57 |
'proforma_invoice' => [ |
| 58 |
'name' => 'proforma_invoice', |
| 59 |
'title' => __('Invoice', 'fluent-cart'), |
| 60 |
'description' => __('Invoice template for offline/pending orders', 'fluent-cart'), |
| 61 |
'is_default' => true, |
| 62 |
'pdf_settings' => [ |
| 63 |
[ |
| 64 |
'active' => 'yes', |
| 65 |
'title' => __('Invoice', 'fluent-cart'), |
| 66 |
'preview_image' => '', |
| 67 |
'pdf_structure' => DefaultPdfStructures::getDefaultProformaInvoiceStructure(), |
| 68 |
] |
| 69 |
] |
| 70 |
], |
| 71 |
]; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Get saved templates, migrating from old format if needed. |
| 76 |
*/ |
| 77 |
public function getTemplates(): array |
| 78 |
{ |
| 79 |
$saved = fluent_cart_get_option($this->metaKey); |
| 80 |
|
| 81 |
if (!$saved || !is_array($saved)) { |
| 82 |
$defaults = $this->getDefaultTemplates(); |
| 83 |
fluent_cart_update_option($this->metaKey, $defaults); |
| 84 |
return $defaults; |
| 85 |
} |
| 86 |
|
| 87 |
// Migrate old single-template format ('default' key without 'is_default') |
| 88 |
if (isset($saved['default']) && !isset($saved['default']['is_default'])) { |
| 89 |
$migrated = $this->migrateOldFormat($saved); |
| 90 |
fluent_cart_update_option($this->metaKey, $migrated); |
| 91 |
return $migrated; |
| 92 |
} |
| 93 |
|
| 94 |
// Ensure all built-in templates exist (in case new ones were added) |
| 95 |
$defaults = $this->getDefaultTemplates(); |
| 96 |
$needsUpdate = false; |
| 97 |
foreach ($defaults as $key => $defaultTemplate) { |
| 98 |
if (!isset($saved[$key])) { |
| 99 |
$saved[$key] = $defaultTemplate; |
| 100 |
$needsUpdate = true; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
if ($needsUpdate) { |
| 105 |
fluent_cart_update_option($this->metaKey, $saved); |
| 106 |
} |
| 107 |
|
| 108 |
return $saved; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Migrate from old single 'default' template to new multi-template format. |
| 113 |
*/ |
| 114 |
private function migrateOldFormat(array $saved): array |
| 115 |
{ |
| 116 |
$defaults = $this->getDefaultTemplates(); |
| 117 |
|
| 118 |
// Rename 'default' to 'order_receipt', preserving user's customizations |
| 119 |
if (isset($saved['default'])) { |
| 120 |
$oldTemplate = $saved['default']; |
| 121 |
$defaults['order_receipt']['pdf_settings'] = $oldTemplate['pdf_settings'] ?? $defaults['order_receipt']['pdf_settings']; |
| 122 |
} |
| 123 |
|
| 124 |
// Preserve any custom templates that might exist |
| 125 |
foreach ($saved as $key => $template) { |
| 126 |
if ($key !== 'default' && !isset($defaults[$key])) { |
| 127 |
$defaults[$key] = $template; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
return $defaults; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Update a template by key. |
| 136 |
*/ |
| 137 |
public function updateTemplate(string $key, array $template): bool |
| 138 |
{ |
| 139 |
$templates = $this->getTemplates(); |
| 140 |
if (!isset($templates[$key])) { |
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
$templates[$key] = array_merge($templates[$key], $template); |
| 145 |
|
| 146 |
return (bool) fluent_cart_update_option($this->metaKey, $templates); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Save the PDF structure for a specific template. |
| 151 |
*/ |
| 152 |
public function setTemplateProperties(array $data, string $templateId = 'order_receipt'): bool |
| 153 |
{ |
| 154 |
$templates = $this->getTemplates(); |
| 155 |
|
| 156 |
if (!isset($templates[$templateId])) { |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
$templates[$templateId]['pdf_settings'][0]['pdf_structure'] = $data; |
| 161 |
|
| 162 |
return (bool) fluent_cart_update_option($this->metaKey, $templates); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Create a new custom template. |
| 167 |
* |
| 168 |
* @return string The generated template slug |
| 169 |
*/ |
| 170 |
public function createTemplate(string $title): string |
| 171 |
{ |
| 172 |
$templates = $this->getTemplates(); |
| 173 |
|
| 174 |
$slug = 'custom_' . time() . '_' . wp_rand(1000, 9999); |
| 175 |
$templates[$slug] = [ |
| 176 |
'name' => $slug, |
| 177 |
'title' => $title, |
| 178 |
'description' => '', |
| 179 |
'is_default' => false, |
| 180 |
'pdf_settings' => [ |
| 181 |
[ |
| 182 |
'active' => 'yes', |
| 183 |
'title' => $title, |
| 184 |
'preview_image' => '', |
| 185 |
'pdf_structure' => DefaultPdfStructures::getDefaultReceiptStructure(), |
| 186 |
] |
| 187 |
] |
| 188 |
]; |
| 189 |
|
| 190 |
fluent_cart_update_option($this->metaKey, $templates); |
| 191 |
|
| 192 |
return $slug; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Delete a custom template. Built-in templates cannot be deleted. |
| 197 |
*/ |
| 198 |
public function deleteTemplate(string $key): bool |
| 199 |
{ |
| 200 |
$templates = $this->getTemplates(); |
| 201 |
|
| 202 |
if (!isset($templates[$key])) { |
| 203 |
return false; |
| 204 |
} |
| 205 |
|
| 206 |
if (!empty($templates[$key]['is_default'])) { |
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
unset($templates[$key]); |
| 211 |
|
| 212 |
return (bool) fluent_cart_update_option($this->metaKey, $templates); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Get the factory default template structures (ignoring saved data). |
| 217 |
*/ |
| 218 |
public function getFactoryDefault(): array |
| 219 |
{ |
| 220 |
return $this->getDefaultTemplates(); |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Get the current PDF structure for a specific template. |
| 225 |
*/ |
| 226 |
public function getPdfStructure(string $templateId = 'order_receipt'): array |
| 227 |
{ |
| 228 |
$templates = $this->getTemplates(); |
| 229 |
|
| 230 |
if (isset($templates[$templateId])) { |
| 231 |
return $templates[$templateId]['pdf_settings'][0]['pdf_structure'] ?? DefaultPdfStructures::getDefaultReceiptStructure(); |
| 232 |
} |
| 233 |
|
| 234 |
// Fallback to order_receipt if requested template doesn't exist |
| 235 |
if (isset($templates['order_receipt'])) { |
| 236 |
return $templates['order_receipt']['pdf_settings'][0]['pdf_structure'] ?? DefaultPdfStructures::getDefaultReceiptStructure(); |
| 237 |
} |
| 238 |
|
| 239 |
return DefaultPdfStructures::getDefaultReceiptStructure(); |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Get a simplified list of templates for dropdown selection. |
| 244 |
*/ |
| 245 |
public function getTemplateList(): array |
| 246 |
{ |
| 247 |
$templates = $this->getTemplates(); |
| 248 |
$list = []; |
| 249 |
|
| 250 |
foreach ($templates as $key => $template) { |
| 251 |
$list[] = [ |
| 252 |
'id' => $key, |
| 253 |
'title' => $template['title'] ?? $key, |
| 254 |
]; |
| 255 |
} |
| 256 |
|
| 257 |
return $list; |
| 258 |
} |
| 259 |
} |
| 260 |
|