forked from dwkitchen/drupal-commerce_funds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommerce_store_funds.module
executable file
·555 lines (463 loc) · 16.3 KB
/
commerce_store_funds.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
<?php
/**
* @file
* commerce_funds.module
* @todo
* 1. Re-Create default views.
* 2. Implement withdraw request module.
* 3. Implement escrow payments module.
*/
/**
* Implementation of hook_views_api().
*/
function commerce_store_funds_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'commerce_store_funds') . '/includes/views',
);
}
/**
* Implements hook_permission().
*/
function commerce_store_funds_permission() {
return array(
'view own transactions' => array(
'title' => 'View Own Transactions',
'description' => 'Allow users to view their transactions',
),
'view transactions' => array(
'title' => 'View All Transactions',
'description' => 'Allow users to view all transactions',
),
'administer funds' => array(
'title' => 'Administer Funds',
'description' => 'Gives users permission to administer all funds operations',
'restrict access' => TRUE,
),
);
}
/**
* Implements hook_menu().
*/
function commerce_store_funds_menu() {
$items['admin/commerce/marketplace/funds'] = array(
'title' => 'Funds Management',
'description' => 'Administer Marketplace Store Funds',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('access administration pages'),
'file path' => drupal_get_path('module', 'system'),
'file' => 'system.admin.inc',
'weight' => -15,
);
$items['admin/commerce/marketplace/funds/configure/fees'] = array(
'title' => 'Fees',
'page callback' => 'commerce_funds_configure_fees',
'access arguments' => array('administer funds'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements of hook_entity_info().
*/
function commerce_store_funds_entity_info() {
$data = array();
$data['commerce_store_funds_account'] = array(
'label' => t('Commerce Store Funds account'),
'controller class' => 'CommerceStoreFundsAccountEntityController',
'base table' => 'commerce_store_funds_account',
'fieldable' => FALSE,
'entity keys' => array(
'id' => 'account_id',
'label' => 'account_id',
),
'bundles' => array(),
'load hook' => 'commerce_store_funds_account_load',
'view modes' => array(
'administrator' => array(
'label' => t('Administrator'),
'custom settings' => FALSE,
),
),
'uri callback' => 'commerce_store_funds_account_uri',
'access callback' => 'commerce_store_funds_account_access',
'token type' => 'commerce-store-funds-account',
'metadata controller class' => '',
'permission labels' => array(
'singular' => t('funds account'),
'plural' => t('funds accounts'),
),
);
$data['commerce_store_funds_transaction'] = array(
'label' => t('Commerce Store Funds transaction'),
'controller class' => 'CommerceStoreFundsTransactionEntityController',
'base table' => 'commerce_store_funds_transaction',
'fieldable' => FALSE,
'entity keys' => array(
'id' => 'transaction_id',
'bundle' => 'type',
'label' => 'transaction_id',
),
'bundle keys' => array(
'bundle' => 'type',
),
'bundles' => array(
'credit' => 'Credit',
'debit' => 'Debit',
),
'load hook' => 'commerce_store_funds_transaction_load',
'view modes' => array(
'administrator' => array(
'label' => t('Administrator'),
'custom settings' => FALSE,
),
),
'uri callback' => 'commerce_store_funds_transaction_uri',
'access callback' => 'commerce_store_funds_transaction_access',
'token type' => 'commerce-store-funds-transaction',
'metadata controller class' => '',
'permission labels' => array(
'singular' => t('funds transaction'),
'plural' => t('funds transactions'),
),
);
return $data;
}
/**
* Returns an initialized funds account object.
*
* @return
* A transaction object with all default fields initialized.
*/
function commerce_store_funds_account_new($store) {
return entity_get_controller('commerce_store_funds_account')->create(array(
'store_id' => $store->id,
));
}
/**
* Saves a funds account.
*
* @param object $account
* The full transaction object to save.
*
* @return
* SAVED_NEW or SAVED_UPDATED depending on the operation performed.
*/
function commerce_store_funds_account_save($account) {
return entity_get_controller('commerce_store_funds_account')->save($account);
}
/**
* Loads a funds account by ID.
*/
function commerce_store_funds_account_load($account_id) {
$accounts = entity_load('commerce_store_funds_account', $account_id);
return $accounts ? reset($accounts) : FALSE;
}
/**
* Loads multiple funds accounts by ID or based on a set of matching conditions.
*
* @see entity_load()
*/
function commerce_store_funds_account_load_by_store($store, $currency_code, $reset = FALSE) {
$accounts = entity_load('commerce_store_funds_account', array(), array('store_id' => $store->id, 'currency_code' => $currency_code), $reset);
return $accounts ? reset($accounts) : FALSE;
}
/**
* Returns an initialized payment transaction object.
*
* @param $method_id
* The method_id of the payment method for the transaction.
*
* @return
* A transaction object with all default fields initialized.
*/
function commerce_store_funds_transaction_new($type = '') {
return entity_get_controller('commerce_store_funds_transaction')->create(array(
'type' => $type,
));
}
/**
* Saves a funds transaction.
*
* @param $transaction
* The full transaction object to save.
*
* @return
* SAVED_NEW or SAVED_UPDATED depending on the operation performed.
*/
function commerce_store_funds_transaction_save($transaction) {
return entity_get_controller('commerce_store_funds_transaction')->save($transaction);
}
/**
* Loads a funds transaction by ID.
*/
function commerce_store_funds_transaction_load($transaction_id) {
$transactions = commerce_store_funds_transaction_load_multiple(array($transaction_id), array());
return $transactions ? reset($transactions) : FALSE;
}
/**
* Loads multiple funds transaction by ID or based on a set of matching conditions.
*
* @see entity_load()
*
* @param $transaction_ids
* An array of transaction IDs.
* @param $conditions
* An array of conditions on the {commerce_payment_transaction} table in the
* form 'field' => $value.
* @param $reset
* Whether to reset the internal transaction loading cache.
*
* @return
* An array of transaction objects indexed by transaction_id.
*/
function commerce_store_funds_transaction_load_multiple($transaction_ids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('commerce_store_funds_transaction', $transaction_ids, $conditions, $reset);
}
/**
* Deletes a funds transaction by ID.
*
* @param $transaction_id
* The ID of the transaction to delete.
*
* @return
* TRUE on success, FALSE otherwise.
*/
function commerce_store_funds_transaction_delete($transaction_id) {
return commerce_store_funds_transaction_delete_multiple(array($transaction_id));
}
/**
* Deletes multiple funds transactions by ID.
*
* @param $transaction_ids
* An array of transaction IDs to delete.
*
* @return
* TRUE on success, FALSE otherwise.
*/
function commerce_store_funds_transaction_delete_multiple($transaction_ids) {
return entity_get_controller('commerce_store_funds_transaction')->delete($transaction_ids);
}
/**
* Rules action to credit account balance
*
* @param object $account_user
* User
* @param object $amount
* Commerce Price
*/
function commerce_store_funds_credit($store, $amount) {
$account = commerce_store_funds_account_load_by_store($store, $amount['currency_code']);
if (!$account) {
$account = commerce_store_funds_account_new($store);
commerce_store_funds_account_save($account);
}
$account->balance += $amount['amount'];
$transaction = commerce_store_funds_transaction_new('credit');
$transaction->account_id = $account->account_id;
$transaction->amount = $amount['amount'];
$transaction->currency_code = $amount['currency_code'];
if (commerce_store_funds_transaction_save($transaction)) {
commerce_store_funds_account_save($account);
}
}
/**
* Rules action to debit account balance
*
* @param object $account_user
* User
* @param object $amount
* Commerce Price
*/
function commerce_store_funds_debit($store, $amount) {
$account = commerce_store_funds_account_load_by_user($store, $amount['currency_code']);
if (!$account) {
$account = commerce_store_funds_account_new($store);
commerce_store_funds_account_save($account);
}
$account->balance -= $amount['amount'];
$transaction = commerce_store_funds_transaction_new('credit');
$transaction->account_id = $account->account_id;
$transaction->amount =- $amount['amount'];
$transaction->currency_code = $amount['currency_code'];
if (commerce_store_funds_transaction_save($transaction)) {
commerce_store_funds_account_save($account);
}
}
function commerce_funds_view_transactions($form, &$form_state) {
$transactions = db_query("SELECT * FROM {commerce_funds_transactions}");
$types = array('Deposit', 'Transfer', 'Escrow Payment', 'Completed Escrow Payment', 'Cancelled Escrow Payment');
$header = array('Time', 'Type', 'User', 'Amount', 'Details');
$rows = array();
$saved_types = array();
if (array_key_exists('commerce_funds_filter_type', $_SESSION)) {
foreach ($_SESSION['commerce_funds_filter_type'] as $index) {
$saved_types[] = $types[$index];
}
}
foreach ($transactions as $transaction) {
if ((array_key_exists('commerce_funds_filter_type', $_SESSION) && in_array($transaction->type, $saved_types)) || !array_key_exists('commerce_funds_filter_type', $_SESSION)) {
$user = user_load($transaction->uid);
$to_user = $transaction->type == 'Transfer' ? user_load($transaction->reference) : array();
$rows[] = array(date('d/m/Y g:i:s A', $transaction->created), $transaction->type, $user->mail, commerce_currency_format($transaction->amount, commerce_default_currency()), $transaction->type == 'Transfer' ? '<br />To: ' . $to_user->mail : '');
}
}
$form['filter_type'] = array(
'#type' => 'select',
'#title' => t('Type'),
'#options' => $types,
'#attributes' => array('multiple' => TRUE),
'#multiple' => TRUE,
'#size' => 5,
'#default_value' => array_key_exists('commerce_funds_filter_type', $_SESSION) ? $_SESSION['commerce_funds_filter_type'] : '',
);
$form['filter'] = array(
'#type' => 'submit',
'#value' => t('Filter'),
);
$form['reset'] = array(
'#type' => 'submit',
'#value' => t('Reset'),
'#suffix' => theme('table', array('header' => $header, 'rows' => $rows))
);
return $form;
}
function commerce_funds_view_transactions_submit($form, &$form_state) {
if ($form_state['values']['op'] == 'Filter') {
if (!empty($form_state['values']['filter_type']))
$_SESSION['commerce_funds_filter_type'] = $form_state['values']['filter_type']; else {
unset($_SESSION['commerce_funds_filter_type']);
}
}
elseif ($form_state['values']['op'] == 'Reset') {
unset($_SESSION['commerce_funds_filter_type']);
}
$form_state['redirect'] = 'admin/commerce/funds/view-transactions';
}
function commerce_funds_configure_fees_form($form, &$form_state, $enabled_methods) {
$form['#enabledmethods'] = $enabled_methods;
$fees = variable_get('commerce_funds_fees', array());
$form['transfer'] = array(
'#type' => 'textfield',
'#title' => check_plain('Transfer Fee (%)'),
'#description' => check_plain('Fee taken on transfers'),
'#default_value' => array_key_exists('transfer', $fees) ? $fees['transfer'] : '0',
'#size' => 5,
'#maxlength' => 5,
'#required' => TRUE,
);
$form['transfer_fixed'] = array(
'#type' => 'textfield',
'#title' => check_plain('Fixed Transfer Fee (' . commerce_default_currency() . ')'),
'#description' => check_plain('Fixed fee taken on transfers'),
'#default_value' => array_key_exists('transfer_fixed', $fees) ? $fees['transfer_fixed'] / 100 : '0',
'#size' => 5,
'#maxlength' => 5,
'#required' => TRUE,
);
$form['escrow'] = array(
'#type' => 'textfield',
'#title' => check_plain('Escrow Fee (%)'),
'#description' => check_plain('Fee taken on escrows'),
'#default_value' => array_key_exists('escrow', $fees) ? $fees['escrow'] : '0',
'#size' => 5,
'#maxlength' => 5,
'#required' => TRUE,
);
$form['escrow_fixed'] = array(
'#type' => 'textfield',
'#title' => check_plain('Fixed Escrow Fee (' . commerce_default_currency() . ')'),
'#description' => check_plain('Fixed fee taken on escrows'),
'#default_value' => array_key_exists('escrow_fixed', $fees) ? $fees['escrow_fixed'] / 100 : '0',
'#size' => 5,
'#maxlength' => 5,
'#required' => TRUE,
);
$form['deposit'] = array(
'#type' => 'fieldset',
'#title' => check_plain('Deposit Fees'),
'#collapsible' => FALSE,
);
foreach ($enabled_methods as $method) {
$method_id = $method['method_id'];
$form['deposit'][$method_id] = array(
'#type' => 'textfield',
'#title' => check_plain($method['title'] . ' Fee (%)'),
'#description' => check_plain('Fee taken for Deposits made using ' . $method['title']),
'#default_value' => array_key_exists($method_id, $fees) ? $fees[$method_id] : '0',
'#size' => 5,
'#maxlength' => 5,
'#required' => TRUE,
);
$form['deposit'][$method_id . '_fixed'] = array(
'#type' => 'textfield',
'#title' => check_plain('Fixed ' . $method['title'] . ' Fee (' . commerce_default_currency() . ')'),
'#description' => check_plain('Fixed Fee taken for Deposits made using ' . $method['title']),
'#default_value' => array_key_exists($method_id . '_fixed', $fees) ? $fees[$method_id . '_fixed'] / 100 : '0',
'#size' => 5,
'#maxlength' => 5,
'#required' => TRUE,
);
}
$form['withdraw'] = array(
'#type' => 'fieldset',
'#title' => t('Withdrawal Fees'),
'#collapsible' => FALSE,
);
$enabled_methods = commerce_funds_get_enabled_withdrawal_methods();
foreach ($enabled_methods as $key => $method) {
$method_id = $key;
$form['withdraw'][$method_id] = array(
'#type' => 'textfield',
'#title' => check_plain($method . ' Fee (%)'),
'#description' => check_plain('Fee taken for Withdrawals made using ' . $method),
'#default_value' => array_key_exists($method_id, $fees) ? $fees[$method_id] : '0',
'#size' => 5,
'#maxlength' => 5,
'#required' => TRUE,
);
$form['withdraw'][$method_id . '_fixed'] = array(
'#type' => 'textfield',
'#title' => check_plain('Fixed ' . $method . ' Fee (' . commerce_default_currency() . ')'),
'#description' => check_plain('Fixed Fee taken for Withdrawals made using ' . $method),
'#default_value' => array_key_exists($method_id . '_fixed', $fees) ? $fees[$method_id . '_fixed'] / 100 : '0',
'#size' => 5,
'#maxlength' => 5,
'#required' => TRUE,
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function commerce_funds_configure_fees_form_validate($form, &$form_state) {
$enabled_methods = $form['#enabledmethods'];
foreach ($enabled_methods as $method) {
if (!is_numeric($form_state['values'][$method['method_id']])) {
form_set_error('amount', t('Value must be Numeric'));
return FALSE;
}
}
}
function commerce_funds_configure_fees_form_submit($form, &$form_state) {
$fees = array();
$fees['transfer'] = $form_state['values']['transfer'];
$fees['transfer_fixed'] = $form_state['values']['transfer_fixed'] * 100;
$fees['escrow'] = $form_state['values']['escrow'];
$fees['escrow_fixed'] = $form_state['values']['escrow_fixed'] * 100;
$enabled_methods = $form['#enabledmethods'];
foreach ($enabled_methods as $method) {
$method_id = $method['method_id'];
$fees[$method_id] = $form_state['values'][$method_id];
$fees[$method_id . '_fixed'] = $form_state['values'][$method_id . '_fixed'] * 100;
}
$enabled_methods = commerce_funds_get_enabled_withdrawal_methods();
foreach ($enabled_methods as $key => $method) {
$method_id = $key;
$fees[$method_id] = $form_state['values'][$method_id];
$fees[$method_id . '_fixed'] = $form_state['values'][$method_id . '_fixed'] * 100;
}
variable_set('commerce_funds_fees', $fees);
}