forked from artesis/ding_debt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathding_debt.module
More file actions
237 lines (206 loc) · 5.47 KB
/
ding_debt.module
File metadata and controls
237 lines (206 loc) · 5.47 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
<?php
/**
* @file
* Handles display and creation/deletion of debts for users.
*/
/**
* Implements hook_ctools_plugin_directory().
*
* It simply tells panels where to find the .inc files that define various
* args, contexts, content_types. In this case the subdirectories of
* ctools_plugin_example/panels are used.
*/
function ding_debt_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools' && !empty($plugin)) {
return 'plugins/' . $plugin;
}
}
/**
* Implements hook_ding_provider_user().
*/
function ding_debt_ding_provider_user() {
return array(
'debt' => array(
'required' => TRUE,
),
'payment' => array(
'required' => TRUE,
),
);
}
/**
* Callback for successful payment transaction.
*/
function ding_debt_payment_callback($transaction) {
$result = ding_provider_invoke_page(
'debt',
'payment_received',
$transaction['payment_price'],
$transaction['params'],
$transaction['payment_order_id']
);
module_invoke_all('ding_debt_payment_received');
return $result;
}
/**
* Implements a drupal form, which implements a pay button and debts data.
*/
function ding_debt_debts_form($form, &$form_state, $debts = array()) {
$header = array(
'date' => t('Date'),
'did' => t('Id'),
'title' => t('Title'),
'type' => t('Type'),
'amount' => t('Amount'),
);
$options = array();
$invoices = array();
$default = array();
$total_amount = 0;
foreach ($debts as $id => $debt) {
if (!$debt->invoice_number) {
$note = explode(' ', $debt->note);
// @todo Need default value for item.
$title = !empty($note[1]) ? $note[1] : NULL;
$total_amount += $debt->amount;
$total_amount -= $debt->amount_paid;
$options[$id] = array(
'did' => $id,
'title' => $title,
'type' => ding_debt_get_debt_type_name($debt->type),
'date' => $debt->date,
'amount' => $debt->amount,
);
$default[$id] = $id;
}
else {
$invoices[$id] = array(
'date' => $debt->date,
'did' => $id,
'title' => t('Incasso'),
'type' => ding_debt_get_debt_type_name($debt->type),
'amount' => $debt->amount,
);
}
}
$form['select_table'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#default_value' => $default,
'#empty' => t('No debts found'),
);
if ($invoices) {
$form['invoice_table'] = array(
'#type' => 'item',
'#theme' => 'table',
'#header' => $header,
'#rows' => $invoices,
'#empty' => t('No debts found'),
'#description' => t("* Invoiced fees can't be paid online."),
);
}
$form['debt_data'] = array(
'#type' => 'value',
'#value' => $debts,
);
$form['pay'] = array(
'#type' => 'submit',
'#prefix' => '<div class="pay-button">',
'#value' => t('Pay debts'),
'#suffix' => '</div>',
);
return $form;
}
/**
* Submit handler for the form.
*/
function ding_debt_debts_form_submit($form, &$form_state) {
$amount = 0;
$debts = array_filter($form_state['input']['select_table']);
foreach ($debts as $debt_id => $debt_data) {
if (isset($form_state['input']['select_table'][$debt_id])) {
if (!$form_state['values']['debt_data'][$debt_id]->invoice_number) {
$amount += $form_state['values']['debt_data'][$debt_id]->amount;
$amount -= $form_state['values']['debt_data'][$debt_id]->amount_paid;
}
else {
unset($debts[$debt_id]);
}
}
}
if ($amount > 0) {
$params = array('params' => array('debt_ids' => array_keys($debts), 'debts' => $debts));
$url = ding_provider_invoke('payment', 'get_url', $amount, $params, 'ding_debt_payment_callback');
if ($url) {
$form_state['redirect'] = $url;
}
}
}
/**
* Convert debt type into a human readable text.
*/
function ding_debt_get_debt_type_name($type) {
$result = NULL;
switch ($type) {
case 'deleteReservationFeeDebt':
$result = t('Detele reservation fee');
break;
case 'emailReminderFeeDebt':
$result = t('Email reminder fee');
break;
case 'illFeeDebt':
$result = t('Ill fee');
break;
case 'illReservationFeeDebt':
$result = t('Ill reservation fee');
break;
case 'loanFeeDebt':
$result = t('Loan fee');
break;
case 'messageFeeDebt':
$result = t('Message fee');
break;
case 'overdueFeeDebt':
$result = t('Overdue fee');
break;
case 'overdueFeeInvoiceDebt':
$result = t('Overdue fee invoice');
break;
case 'photocopyFeeDebt':
$result = t('Photocopy fee');
break;
case 'renewFeeDebt':
$result = t('Renew fee');
break;
case 'replacementFeeDebt':
$result = t('Replacement fee');
break;
case 'reservationFeeDebt':
$result = t('Reservation fee');
break;
case 'reservationPickupFeeDebt':
$result = t('Reservation pickup fee');
break;
case 'smsIllFeeDebt':
$result = t('SMS Ill fee');
break;
case 'smsRecall1FeeDebt':
case 'smsRecall2FeeDebt':
case 'smsRecall3FeeDebt':
case 'smsRecall4FeeDebt':
case 'smsRecall5FeeDebt':
$result = t('SMS recall fee');
break;
case 'smsReminderFeeDebt':
$result = t('SMS Reminder fee');
break;
case 'smsReservationFeeDebt':
$result = t('SMS reservation fee');
break;
case 'otherFeeDebt':
default:
$result = t('Other fee');
}
return $result;
}