-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatman_invoice.module
executable file
·395 lines (356 loc) · 11.6 KB
/
catman_invoice.module
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
// $Id$
/**
* @file
* Generates the invoice and allows you to display an invoice id in views
*/
// Invoice number generation methods
// One single number, never regenerated
define('CATMAN_INVOICE_METHOD_INFINITE', '[invoice_id]');
// Invoice id is regenerated every year
define('CATMAN_INVOICE_METHOD_YEAR', 'Y-[invoice_id]');
// Invoice id is regenerated every month
define('CATMAN_INVOICE_METHOD_MONTH', 'Y-m-[invoice_id]');
// Invoice number is generated by a callback that is being passed the invoice object
define('CATMAN_INVOICE_METHOD_CALLBACK', 'callback');
/**
* Implements hook_entity_info().
*/
function catman_invoice_entity_info() {
$return = array(
'catman_invoice' => array(
'label' => t('Catman Invoice'),
'controller class' => 'CommerceInvoiceEntityController',
'base table' => 'catman_invoice',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'invoice_id',
'bundle' => 'type',
'label' => 'invoice_number', // TODO: Update to use a custom callback.
),
'bundle keys' => array(
'bundle' => 'type',
),
'bundles' => array(
'catman_invoice' => array(
'label' => t('Invoice', array(), array('context' => 'a drupal commerce invoice')),
),
),
'load hook' => 'catman_invoice_load',
'view modes' => array(
'administrator' => array(
'label' => t('Administrator'),
'custom settings' => FALSE,
),
'customer' => array(
'label' => t('Customer'),
'custom settings' => FALSE,
),
),
'creation callback' => '_catman_invoice_create',
'save callback' => 'catman_invoice_save',
'deletion callback' => 'catman_invoice_delete',
'access callback' => 'commerce_entity_access',
'access arguments' => array(
'user key' => 'uid',
'access tag' => 'catman_invoice_access',
),
'token type' => 'catman-invoice',
'permission labels' => array(
'singular' => t('invoice'),
'plural' => t('invoices'),
),
),
);
return $return;
}
/**
* Loads an invoice by ID.
*/
function catman_invoice_load($invoice_id) {
$invoices = catman_invoice_load_multiple(array($invoice_id), array());
return $invoices ? reset($invoices) : FALSE;
}
/**
* Loads an invoice by order ID
*/
function catman_invoice_load_by_order_id($order_id) {
$query = new EntityFieldQuery();
$return = FALSE;
$result = $query
->entityCondition('entity_type', 'catman_invoice')
->propertyCondition('order_id', $order_id)
->execute();
if ($result) {
$invoice = array_keys($result['catman_invoice']);
$return = catman_invoice_load(reset($invoice));
}
return $return;
}
/**
* Loads multiple invoices by ID or based on a set of matching conditions.
*
* @see entity_load()
*
* @param $invoice_ids
* An array of invoice IDs.
* @param $conditions
* An array of conditions on the {catman_invoice} table in the form
* 'field' => $value.
* @param $reset
* Whether to reset the internal invoice loading cache.
*
* @return
* An array of invoice objects indexed by invoice_id.
*/
function catman_invoice_load_multiple($invoice_ids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('catman_invoice', $invoice_ids, $conditions, $reset);
}
/**
* Returns an initialized invoice object.
*
* @param $uid
* The uid of the owner of the invoice.
* @param $order_id
* The ID of the order the invoice belongs to (if available).
* @param $type
* The type of the invoice; defaults to the standard 'invoice' type.
*
* @return
* An invoice object with all default fields initialized.
*/
function catman_invoice_new($uid = 0, $order_id = 0, $type = 'catman_invoice') {
return entity_get_controller('catman_invoice')->create(array('uid' => $uid, 'order_id' => $order_id, 'type' => $type));
}
/**
* Creation callback for the Entity module.
*/
function _catman_invoice_create($values = array()) {
// Create a new invoice
$invoice = catman_invoice_new();
$wrapper = entity_metadata_wrapper('catman_invoice', $invoice);
return $wrapper->value();
}
/**
* Saves an invoice.
*
* @param $invoice
* The full invoice object to save.
*
* @return
* The saved invoice object.
*/
function catman_invoice_save($invoice) {
return entity_get_controller('catman_invoice')->save($invoice);
}
/**
* Deletes an invoice by ID.
*
* @param $invoice
* The ID of the invoice to delete.
*
* @return
* TRUE on success, FALSE otherwise.
*/
function catman_invoice_delete($invoice_id) {
return catman_invoice_delete_multiple(array($invoice_id));
}
/**
* Deletes multiple invoices by ID.
*
* @param $invoice_ids
* An array of invoice IDs to delete.
*
* @return
* TRUE on success, FALSE otherwise.
*/
function catman_invoice_delete_multiple($invoice_ids) {
return entity_get_controller('catman_invoice')->delete($invoice_ids);
}
/**
* Checks invoice access for various operations.
*
* @param $op
* The operation being performed. One of 'view', 'update', 'create' or
* 'delete'.
* @param $invoice
* Optionally an invoice to check access for.
* @param $account
* The user to check for. Leave it to NULL to check for the current user.
*/
function catman_invoice_access($op, $invoice = NULL, $account = NULL) {
return commerce_entity_access($op, $invoice, $account, 'catman_invoice');
}
/**
* Implementation of hook_query_commerce_order_access_alter().
*/
function catman_invoice_query_catman_invoice_access_alter(QueryAlterableInterface $query) {
commerce_entity_access_query_alter($query, 'catman_invoice');
}
/**
* Implements hook_permission().
*/
function catman_invoice_permission() {
return commerce_entity_access_permissions('catman_invoice') + array(
'configure invoice settings' => array(
'title' => t('Configure invoice settings'),
'description' => t('Allows users to configure invoice settings for the store.'),
'restrict access' => TRUE,
),
);
}
/**
* Returns the name of the specified invoice type or all names keyed by type if no
* type is specified.
*
* @param $type
* The invoice type whose name should be returned; corresponds to the bundle key
* in the invoice entity definition.
*
* @return
* Either the specified name, defaulting to the type itself if the name is not
* found, or an array of all names keyed by type if no type is passed in.
*/
function catman_invoice_type_get_name($type = NULL) {
$names = array();
$entity = entity_get_info('catman_invoice');
foreach ($entity['bundles'] as $key => $value) {
$names[$key] = $value['label'];
}
if (empty($type)) {
return $names;
}
if (empty($names[$type])) {
return check_plain($type);
}
else {
return $names[$type];
}
}
/**
* Implements hook_field_extra_fields().
*/
function catman_invoice_field_extra_fields() {
$extra = array();
$extra['catman_invoice']['catman_invoice'] = array(
'display' => array(
'invoice_number' => array(
'label' => t('Invoice number'),
'description' => t('Display simple invoice number'),
'weight' => -10,
),
'created' => array(
'label' => t('Created'),
'description' => t('Display date of invoice creation'),
'weight' => -5,
),
'balance_due' => array(
'label' => t('Catman payment information'),
'description' => t('Display payment method and balance due'),
'weight' => 10,
),
'invoice_header' => array(
'label' => t('Catman invoice header'),
'description' => t('Display invoice header message'),
'weight' => -6,
),
// 'invoice_footer' => array(
// 'label' => t('Invoice footer'),
// 'description' => t('Display invoice footer message'),
// 'weight' => 11,
// ),
),
);
$order_field_instances = field_info_instances('commerce_order', 'commerce_order');
if (!empty($order_field_instances)) {
foreach($order_field_instances as $field => $properties){
$extra['catman_invoice']['catman_invoice']['display'][$field] = array(
'label' => t('Order: @label', array('@label' => $properties['label'])),
'description' => $properties['description'],
'entity' => 'commerce_order',
'weight' => 0
);
}
}
return $extra;
}
/**
* Implements hook_theme().
*/
function catman_invoice_theme() {
return array(
'catman_invoice_number' => array(
'variables' => array('invoice_number' => NULL, 'label' => NULL, 'invoice' => NULL),
'path' => drupal_get_path('module', 'catman_invoice') . '/theme',
'template' => 'catman-invoice-number',
),
'catman_invoice_created' => array(
'variables' => array('created' => NULL, 'label' => NULL, 'invoice' => NULL),
'path' => drupal_get_path('module', 'catman_invoice') . '/theme',
'template' => 'catman-invoice-created',
),
'catman_invoice_balance_due' => array(
'variables' => array('balance_due' => NULL, 'label' => NULL, 'invoice' => NULL),
'path' => drupal_get_path('module', 'catman_invoice') . '/theme',
'template' => 'catman-invoice-balance-due',
),
'catman_invoice_invoice_header' => array(
'variables' => array('invoice_header' => NULL, 'label' => NULL, 'invoice' => NULL),
'path' => drupal_get_path('module', 'catman_invoice') . '/theme',
'template' => 'catman-invoice-invoice-header',
),
);
}
/**
* Implements hook_views_api().
*/
function catman_invoice_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'catman_invoice') . '/includes/views',
);
}
/**
* @file
* Hooks provided by the Commerce Price Savings formatter.
*/
/**
* Lets modules alter the formatted prices created by the formattters
*
* @param $formatted_prices
* An array of the formatted prices keyed by the price type.
* Each price item 'list', 'price', 'savings' contains:
* - label: The price label SAFE for display
* - amount: The price amount, integer
* - currency_code: The currency code, string
* - formatted: The formatted price provided by default, string
* The 'savings' price item contains the following in addition to the above:
* - percent: An array containing percent information
* -- value: The decimal value of the savings percent, float
* -- formatted: The formatted percent provided by default, string
*
* @param $context
* A context array of information for the display:
* - settings: The field formatter settings
* - entity_type: The entity type that the field is attached to
* - entity: The entity object
* - field: The field structure being rendered.
* - instance: The instance structure being rendered.
* - langcode: The field language.
* - display: The display settings to use, as found in the 'display' entry
* of instance definitions
*/
function catman_invoice_commerce_price_savings_formatter_prices_alter(&$formatted_prices, $context) {
// Exit if custom setting is not there
if (!isset($context['settings']['catman_invoice_custom_setting'])) {
return;
}
foreach ($formatted_prices as $price_type => $formatted_price) {
// Alter the formatted price
$formatted_prices[$price_type]['formatted'] = catman_invoice_commerce_currency_format($formatted_price['amount'], $formatted_price['currency_code'], $context['settings'], $context['entity']);
// Alter the percent rounding
if ($price_type == 'savings' && !empty($formatted_prices[$price_type]['percent']['value'])) {
$formatted_prices[$price_type]['percent']['formatted'] = round($formatted_prices[$price_type]['percent']['value'] * 100, 2) . '%';
}
}
}