-
Notifications
You must be signed in to change notification settings - Fork 5
/
commerce_funds.module
executable file
·476 lines (433 loc) · 13.4 KB
/
commerce_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
<?php
/**
* @file
* commerce_funds.module
*/
/**
* Implementation of hook_views_api().
*/
function commerce_funds_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'commerce_funds') . '/includes/views',
);
}
/**
* Implements hook_permission().
*/
function commerce_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_funds_menu() {
$items['admin/commerce/funds'] = array(
'title' => 'Funds Management',
'description' => 'Administer 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['user/%user/funds'] = array(
'title' => 'Funds Account',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
return $items;
}
/**
* Implements of hook_entity_info().
*/
function commerce_funds_entity_info() {
$data = array();
$data['commerce_funds_account'] = array(
'label' => t('Commerce Funds account'),
'plural label' => t('Commerce Funds accounts'),
'entity class' => 'Entity',
'controller class' => 'EntityAPIController',
'views controller class' => 'EntityDefaultViewsController',
'base table' => 'commerce_funds_account',
'fieldable' => FALSE,
'entity keys' => array(
'id' => 'account_id',
'label' => 'account_id',
),
'bundles' => array(),
'load hook' => 'commerce_funds_account_load',
'view modes' => array(
'full' => array(
'label' => t('Administrator'),
'custom settings' => FALSE,
),
),
'uri callback' => 'commerce_funds_account_uri',
'access callback' => 'commerce_funds_account_access',
'token type' => 'commerce-funds-account',
'metadata controller class' => '',
'permission labels' => array(
'singular' => t('funds account'),
'plural' => t('funds accounts'),
),
);
$data['commerce_funds_transaction'] = array(
'label' => t('Commerce Funds transaction'),
'entity class' => 'Entity',
'controller class' => 'EntityAPIController',
'views controller class' => 'EntityDefaultViewsController',
'base table' => 'commerce_funds_transaction',
'fieldable' => FALSE,
'entity keys' => array(
'id' => 'transaction_id',
'bundle' => 'type',
'label' => 'transaction_id',
),
'bundle keys' => array(
'bundle' => 'type',
),
'bundles' => array(
'credit' => array(
'label' => 'Credit',
),
'debit' => array(
'label' => 'Debit',
),
),
'load hook' => 'commerce_funds_transaction_load',
'view modes' => array(
'full' => array(
'label' => t('Administrator'),
'custom settings' => FALSE,
),
),
'uri callback' => 'commerce_funds_transaction_uri',
'access callback' => 'commerce_funds_transaction_access',
'token type' => 'commerce-funds-transaction',
'metadata controller class' => '',
'permission labels' => array(
'singular' => t('funds transaction'),
'plural' => t('funds transactions'),
),
);
$data['commerce_funds_withdraw_requests'] = array(
'label' => t('Commerce Funds Withdraws'),
'plural label' => t('Commerce Funds withdraws'),
'entity class' => 'Entity',
'controller class' => 'EntityAPIController',
'views controller class' => 'EntityDefaultViewsController',
'base table' => 'commerce_funds_withdraw_requests',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'request_id',
'label' => 'request_id',
),
'bundles' => array(),
'load hook' => 'commerce_funds_withdraw_load',
'view modes' => array(
'full' => array(
'label' => t('Administrator'),
'custom settings' => FALSE,
),
),
'uri callback' => 'commerce_funds_withdraw_uri',
'access callback' => 'commerce_funds_withdraw_access',
'token type' => 'commerce-funds-withdraw',
'metadata controller class' => '',
'permission labels' => array(
'singular' => t('funds withdraws'),
'plural' => t('funds withdraws'),
),
);
return $data;
}
/**
* Access callback for the entity API.
*
* @param $op
* The operation being performed. One of 'view', 'update', 'create', 'delete'
* or just 'edit' (being the same as 'create' or 'update').
* @param $profile
* (optional) A profile to check access for. If nothing is given, access for
* all profiles is determined.
* @param $account
* (optional) The user to check for. Leave it to NULL to check for the global user.
*
* @return boolean
* Whether access is allowed or not.
*/
function commerce_funds_account($op, $profile = NULL, $account = NULL) {
return user_access('administer commerce_funds entities', $account);
}
/**
* Returns an initialized funds account object.
*
* @return
* A transaction object with all default fields initialized.
*/
function commerce_funds_account_new($account_user, $currency_code) {
return entity_get_controller('commerce_funds_account')->create(array(
'uid' => $account_user->uid,
'currency_code' => $currency_code,
));
}
/**
* 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_funds_account_save($account) {
return entity_get_controller('commerce_funds_account')->save($account);
}
/**
* Loads a funds account by ID.
*/
function commerce_funds_account_load($account_id) {
$accounts = entity_load('commerce_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_funds_account_load_by_user($account_user, $currency_code, $reset = FALSE) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'commerce_funds_account')
->propertyCondition('uid', $account_user->uid)
->propertyCondition('currency_code', $currency_code);
$return = array();
$result = $query->execute();
if (!empty($result['commerce_funds_account'])) {
foreach($result['commerce_funds_account'] as $account) {
$return[] = entity_load_single('commerce_funds_account', $account->account_id);
}
}
return $return ? reset($return) : 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_funds_transaction_new($type = '') {
return entity_get_controller('commerce_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_funds_transaction_save($transaction) {
return entity_get_controller('commerce_funds_transaction')->save($transaction);
}
/**
* Loads a funds transaction by ID.
*/
function commerce_funds_transaction_load($transaction_id) {
$transactions = commerce_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_funds_transaction_load_multiple($transaction_ids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('commerce_funds_transaction', $transaction_ids, $conditions, $reset);
}
/**
* View callback
*/
function commerce_funds_view($commerce_funds, $view_mode) {
return entity_view('commerce_funds', array($commerce_funds->id => $commerce_funds), $view_mode, NULL, TRUE);
}
/**
* Deletes a funds transaction by ID.
*
* @param $transaction_id
* The ID of the transaction to delete.
*
* @return
* TRUE on success, FALSE otherwise.
*/
function commerce_funds_transaction_delete($transaction_id) {
return commerce_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_funds_transaction_delete_multiple($transaction_ids) {
return entity_get_controller('commerce_funds_transaction')->delete($transaction_ids);
}
/**
* Rules action to credit account balance
*
* @param object $account_user
* User
* @param object $amount
* Commerce Price
*/
function commerce_funds_credit($account_user, $amount) {
$account = commerce_funds_account_load_by_user($account_user, $amount['currency_code']);
$currency_code = $amount['currency_code'];
if (empty($account)) {
$account = commerce_funds_account_new($account_user, $currency_code);
commerce_funds_account_save($account);
}
$account->balance += $amount['amount'];
$account->currency_code = $amount['currency_code'];
$transaction = commerce_funds_transaction_new('credit');
$transaction->account_id = $account->account_id;
$transaction->uid = $account_user->uid;
if (empty($amount['reference'])) {
$transaction->reference = $account_user->uid;
}
else {
$transaction->reference = $amount['reference'];
}
$transaction->amount = $amount['amount'];
$transaction->currency_code = $amount['currency_code'];
if (commerce_funds_transaction_save($transaction)) {
commerce_funds_account_save($account);
}
}
/**
* Rules action to debit account balance
*
* @param object $account_user
* User
* @param object $amount
* Commerce Price
*/
function commerce_funds_debit($account_user, $amount) {
$account = commerce_funds_account_load_by_user($account_user, $amount['currency_code']);
if (!$account) {
$account = commerce_funds_account_new($account_user);
commerce_funds_account_save($account);
}
$account->balance -= $amount['amount'];
$transaction = commerce_funds_transaction_new('debit');
$transaction->account_id = $account->account_id;
$transaction->amount = - $amount['amount'];
$transaction->type = 'debit';
$transaction->currency_code = $amount['currency_code'];
if (empty($amount['reference'])) {
$transaction->reference = $account_user->uid;
}
else {
$transaction->reference = $amount['reference'];
}
if (commerce_funds_transaction_save($transaction)) {
commerce_funds_account_save($account);
}
}
/**
* Loads requests by request_id.
*/
function commerce_funds_withdraw_load($request_id) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'commerce_funds_withdraw_requests')
->propertyCondition('request_id', $request_id);
$return = array();
$result = $query->execute();
if (!empty($result['commerce_funds_withdraw_requests'])) {
$return = entity_load_single('commerce_funds_withdraw_requests', $request_id);
}
return $return;
}
/**
* Loads multiple withdraw requests by user ID or based on a set of matching conditions.
*
* @see entity_load()
*/
function commerce_funds_withdraw_load_multiple_by_reuests($request_id, $reset = FALSE) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'commerce_funds_withdraw_requests')
->propertyCondition('request_id', $request_id, $operator = 'IN');
$return = array();
$result = $query->execute();
if (!empty($result['commerce_funds_withdraw_requests'])) {
foreach($result['commerce_funds_withdraw_requests'] as $withdraw) {
$return[] = entity_load_single('commerce_funds_withdraw_requests', $withdraw->request_id);
}
}
return $return;
}
/**
* Get line item by id.
*/
function commerce_line_item($line_item_id) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'commerce_line_item')
->propertyCondition('line_item_id', $line_item_id)
->propertyOrderBy('line_item_id', 'DESC')
->range(0,1);
$return = array();
$result = $query->execute();
if (!empty($result['commerce_line_item'])) {
foreach($result['commerce_line_item'] as $line_item) {
$return[] = entity_load_single('commerce_line_item', $line_item->line_item_id);
}
}
return $return;
}
/**
* Checks whether or not the product type in the order is deposit or not.
*/
function commerce_line_item_comparison($deposit_order){
$item_id = $deposit_order->commerce_line_items['und'][0]['line_item_id'];
$line_item = commerce_line_item($item_id);
if ($line_item[0]->type == 'deposit' ) {
$return = TRUE;
}
elseif($line_item[0]->type != 'deposit' ) {
$return = FALSE;
}
return $return;
}