-
Notifications
You must be signed in to change notification settings - Fork 0
/
catman_invoice_ui.module
executable file
·174 lines (154 loc) · 4.98 KB
/
catman_invoice_ui.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
<?php
/**
* @file
*/
/**
* Implements hook_menu().
*/
function catman_invoice_ui_menu() {
$items = array();
$items['admin/commerce/config/invoice'] = array(
'title' => 'Invoice settings',
'description' => 'Configure general invoice settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('catman_invoice_settings_form'),
'access arguments' => array('configure invoice settings'),
'file' => 'includes/catman_invoice_ui.invoices.inc',
);
$items['admin/commerce/config/invoice/settings'] = array(
'title' => 'Settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/commerce/orders/%commerce_order/invoice'] = array(
'title' => 'Invoice',
'page callback' => 'catman_invoice_ui_invoice_view_by_order',
'page arguments' => array(3),
'access callback' => 'catman_invoice_ui_access_by_order',
'access arguments' => array('view', 3),
'type' => MENU_LOCAL_TASK,
'weight' => -5,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
$items['user/%user/invoice/%commerce_order'] = array(
'title' => 'Invoice',
'page callback' => 'catman_invoice_ui_invoice_view_by_order',
'page arguments' => array(3,'customer', FALSE),
'access callback' => 'catman_invoice_ui_access_by_order',
'access arguments' => array('view', 3),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
return $items;
}
/**
* Implements hook_views_api().
*/
function catman_invoice_ui_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'catman_invoice_ui') . '/includes/views',
);
}
/**
* Menu item title callback: returns the number of an invoice for its pages.
*
* @param $invoice
* The invoice object as loaded via the URL wildcard.
* @return
* A page title of the format "Invoice ##".
*/
function catman_invoice_ui_invoice_title($invoice) {
return t('Invoice @number', array('@number' => $invoice->invoice_number));
}
/**
* Sets the breadcrumb for invoice pages.
*
* @param $view_mode
* The view mode for the current invoice page, 'administrator' or 'customer'.
*/
function catman_invoice_ui_set_breadcrumb($view_mode = 'administrator') {
$breadcrumb = array();
// Create the breadcrumb array based on the view mode.
if ($view_mode == 'administrator') {
$breadcrumb = array(
l(t('Home'), '<front>'),
l(t('Administration'), 'admin'),
l(t('Store'), 'admin/commerce'),
l(t('Orders', array(), array('context' => 'a drupal commerce order')), 'admin/commerce/orders'),
);
}
drupal_set_breadcrumb($breadcrumb);
}
/**
* Generate an array for rendering the given invoice.
*
* @param $invoice
* A fully loaded invoice object.
* @param $view_mode
* The view mode for displaying the invoice, 'administrator' or 'customer'.
*
* @return
* An array as expected by drupal_render().
*/
function catman_invoice_ui_invoice_view($invoice, $view_mode = 'customer', $breadcrumb = TRUE) {
// Set the breadcrumb for the appropriate view mode if specified.
if ($breadcrumb) {
catman_invoice_ui_set_breadcrumb();
}
drupal_add_css(drupal_get_path('module', 'commerce_order') . '/theme/commerce_order.css');
return entity_view('catman_invoice', array($invoice->invoice_id => $invoice), $view_mode, NULL, TRUE);
}
/**
* Generate an array for rendering the given invoice, based on an order.
*
* @param $order
* A fully loaded order object.
* @param $view_mode
* The view mode for displaying the invoice, 'administrator' or 'customer'.
*
* @return
* An array as expected by drupal_render().
*/
function catman_invoice_ui_invoice_view_by_order($order, $view_mode = 'administrator', $breadcrumb = TRUE) {
$invoice = catman_invoice_load_by_order_id($order->order_id);
if ($invoice) {
return catman_invoice_ui_invoice_view($invoice, $view_mode, $breadcrumb);
}
else {
drupal_set_message(t('The invoice for this order has not been generated yet'), 'warning');
return '';
}
}
/**
* Checks invoice access based on order id.
*
* @param $op
* The operation being performed. One of 'view', 'update', 'create' or
* 'delete'.
* @param $order
* Optionally an order to check access for.
* @param $account
* The user to check for. Leave it to NULL to check for the current user.
*/
function catman_invoice_ui_access_by_order($op, $order = NULL, $account = NULL) {
$invoice = catman_invoice_load_by_order_id($order->order_id);
if ($invoice) {
return catman_invoice_access($op, $invoice, $account, 'catman_invoice');
}
else {
return FALSE;
}
}
/**
* Implements hook_entity_info_alter().
*/
function catman_invoice_ui_entity_info_alter(&$entity_info) {
// Expose the order UI for invoice fields.
$entity_info['catman_invoice']['bundles']['catman_invoice']['admin'] = array(
'path' => 'admin/commerce/config/invoice',
'real path' => 'admin/commerce/config/invoice',
'access arguments' => array('configure invoice settings'),
);
}