-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbusiness_ledger.module
More file actions
276 lines (249 loc) · 8.85 KB
/
Copy pathbusiness_ledger.module
File metadata and controls
276 lines (249 loc) · 8.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* @file
* Primary module hooks for Business Ledger module.
*/
use Drupal\business_ledger\Entity\Client;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_theme().
*/
function business_ledger_theme($existing, $type, $theme, $path) {
return [
'business_ledger_dashboard' => [
'variables' => [
'stats' => [],
'compliance' => [],
'period' => 'current_month',
'period_options' => [],
'date_range' => [],
],
],
'bl_invoice' => [
'render element' => 'elements',
],
'bl_client' => [
'render element' => 'elements',
],
'business_ledger_navigation' => [
'variables' => [
'current_route' => NULL,
],
],
'business_ledger_kpo_book' => [
'variables' => [
'rows' => [],
'year' => NULL,
'year_options' => [],
'total_services' => 0,
'total_products' => 0,
'grand_total' => 0,
],
],
'business_ledger_invoice_pdf' => [
'variables' => [
'invoice' => NULL,
'company' => [],
'footer_text' => NULL,
'line_items' => [],
],
],
'business_ledger_invoice_email' => [
'variables' => [
'invoice' => NULL,
'client' => NULL,
'company' => [],
'invoice_url' => NULL,
'site_name' => NULL,
],
],
];
}
/**
* Prepares variables for invoice templates.
*
* Default template: bl-invoice.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the invoice information.
*/
function template_preprocess_bl_invoice(&$variables) {
$variables['invoice'] = $variables['elements']['#bl_invoice'];
foreach (\Drupal\Core\Render\Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
// Add company configuration to template variables.
$config = \Drupal::config('business_ledger.settings');
$variables['company'] = [
'name' => $config->get('company_name'),
'address' => $config->get('company_address'),
'city' => $config->get('company_city'),
'postal_code' => $config->get('company_postal_code'),
'country' => $config->get('company_country'),
'tax_id' => $config->get('company_tax_id'),
'registration_number' => $config->get('company_registration_number'),
'bank_account' => $config->get('company_bank_account'),
'eur_intermediary_bank' => $config->get('company_eur_intermediary_bank'),
'eur_intermediary_swift' => $config->get('company_eur_intermediary_swift'),
'eur_bank' => $config->get('company_eur_bank'),
'eur_swift' => $config->get('company_eur_swift'),
'eur_iban' => $config->get('company_eur_iban'),
'email' => $config->get('company_email'),
'phone' => $config->get('company_phone'),
'website' => $config->get('company_website'),
];
$variables['footer_text'] = $config->get('invoice_footer_text');
// Load line items for print view.
$invoice = $variables['invoice'];
$variables['line_items'] = [];
if (!$invoice->isNew()) {
$line_item_storage = \Drupal::entityTypeManager()->getStorage('bl_line_item');
$line_item_ids = $line_item_storage->getQuery()
->accessCheck(TRUE)
->condition('invoice_id', $invoice->id())
->execute();
$variables['line_items'] = $line_item_storage->loadMultiple($line_item_ids);
}
}
/**
* Prepares variables for client templates.
*
* Default template: bl-client.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the client information.
*/
function template_preprocess_bl_client(&$variables) {
$variables['client'] = $variables['elements']['#bl_client'];
foreach (\Drupal\Core\Render\Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}
/**
* Allowed values callback for the country field.
*
* @return array
* An array of allowed values for the country field.
*/
function business_ledger_country_allowed_values() {
return Client::getCountryList();
}
/**
* Implements hook_form_alter().
*/
function business_ledger_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Attach styling and JavaScript to entity forms.
$entity_forms = [
'bl_client_add_form',
'bl_client_edit_form',
'bl_client_default_form',
'bl_invoice_add_form',
'bl_invoice_edit_form',
'bl_invoice_default_form',
'bl_line_item_add_form',
'bl_line_item_edit_form',
'bl_line_item_default_form',
];
if (in_array($form_id, $entity_forms)) {
// Attach entity forms styling.
$form['#attached']['library'][] = 'business_ledger/entity-forms';
// Add wrapper class.
$form['#attributes']['class'][] = 'bl-entity-form';
}
// Attach country-phone prefix JavaScript to client forms.
if (in_array($form_id, ['bl_client_add_form', 'bl_client_edit_form', 'bl_client_default_form'])) {
$form['#attached']['library'][] = 'business_ledger/country-phone-prefix';
}
// Attach invoice line items JavaScript to invoice forms.
if (in_array($form_id, ['bl_invoice_add_form', 'bl_invoice_edit_form', 'bl_invoice_default_form'])) {
$form['#attached']['library'][] = 'business_ledger/invoice-line-items';
}
}
/**
* Implements hook_mail().
*/
function business_ledger_mail($key, &$message, $params) {
switch ($key) {
case 'invoice_notification':
/** @var \Drupal\business_ledger\Entity\Invoice $invoice */
$invoice = $params['invoice'];
/** @var \Drupal\business_ledger\Entity\Client $client */
$client = $params['client'];
$config = \Drupal::config('business_ledger.settings');
// Set subject.
$message['subject'] = t('Invoice @number from @site', [
'@number' => $invoice->label(),
'@site' => \Drupal::config('system.site')->get('name'),
]);
// Get invoice URL.
$invoice_url = \Drupal\Core\Url::fromRoute('entity.bl_invoice.canonical', [
'bl_invoice' => $invoice->id(),
], ['absolute' => TRUE])->toString();
// Prepare company information.
$company = [
'name' => $config->get('company_name'),
'address' => $config->get('company_address'),
'city' => $config->get('company_city'),
'postal_code' => $config->get('company_postal_code'),
'country' => $config->get('company_country'),
'tax_id' => $config->get('company_tax_id'),
'registration_number' => $config->get('company_registration_number'),
'bank_account' => $config->get('company_bank_account'),
'email' => $config->get('company_email'),
'phone' => $config->get('company_phone'),
'website' => $config->get('company_website'),
];
// Render HTML email body.
$build = [
'#theme' => 'business_ledger_invoice_email',
'#invoice' => $invoice,
'#client' => $client,
'#company' => $company,
'#invoice_url' => $invoice_url,
'#site_name' => \Drupal::config('system.site')->get('name'),
];
$renderer = \Drupal::service('renderer');
$html_body = $renderer->renderPlain($build);
// Explicitly set content type for Symfony Mailer Lite.
$message['params']['content_type'] = 'text/html';
// Set headers for HTML email.
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8';
$message['headers']['MIME-Version'] = '1.0';
// Set HTML body.
$message['body'][] = $html_body;
// Add PDF attachment if provided in params.
if (isset($params['pdf_filepath']) && file_exists($params['pdf_filepath'])) {
$pdf_content = file_get_contents($params['pdf_filepath']);
// Sanitize invoice number for filename.
$invoice_number = $invoice->get('invoice_number')->value;
$safe_invoice_number = preg_replace('/[^a-zA-Z0-9_-]/', '_', $invoice_number);
$pdf_filename = 'invoice-' . $safe_invoice_number . '.pdf';
// Add attachment for Symfony Mailer Lite.
$message['params']['attachments'][] = [
'filecontent' => $pdf_content,
'filename' => $pdf_filename,
'filemime' => 'application/pdf',
];
}
break;
}
}
/**
* Implements hook_mail_alter().
*/
function business_ledger_mail_alter(&$message) {
// Handle invoice notification emails.
if ($message['id'] === 'business_ledger_invoice_notification') {
// Strip Twig debug comments from email body.
// This removes <!-- THEME DEBUG --> and similar comments that shouldn't appear in emails.
if (!empty($message['body'])) {
foreach ($message['body'] as $key => $body_part) {
$message['body'][$key] = preg_replace('/<!--.*?-->/s', '', $body_part);
}
}
// Note: Attachments are handled automatically by Symfony Mailer Lite
// through $message['params']['attachments']. No manual MIME construction needed.
}
}