-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommerce_library.module
More file actions
905 lines (761 loc) · 26.4 KB
/
commerce_library.module
File metadata and controls
905 lines (761 loc) · 26.4 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
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
<?php
/**
* @file
* Allow commerce products to be lent for a specific time and then re-added back to the system.
*
*/
function commerce_library_entity_info() {
$return = array(
'commerce_library_loan' => array(
'label' => t('Commerce Loan', array(), array('context' => 'a drupal commerce product loan')),
'controller class' => 'CommerceLibraryLoanEntityController',
'locking mode' => 'pessimistic',
'base table' => 'commerce_library_loan',
'load hook' => 'commerce_library_loan_load',
'uri callback' => 'commerce_library_loan_uri',
'label callback' => 'commerce_library_loan_label',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'loan_id',
'bundle' => 'type',
),
'bundle keys' => array(
'bundle' => 'type',
),
'bundles' => array(
'commerce_library_loan' => array(
'label' => t('Loan', array(), array('context' => 'a drupal commerce loan')),
),
),
'view modes' => array(
'administrator' => array(
'label' => t('Administrator'),
'custom settings' => FALSE,
),
'customer' => array(
'label' => t('Customer'),
'custom settings' => FALSE,
),
),
'token type' => 'commerce-library-loan',
'metadata controller class' => '',
'access callback' => 'commerce_entity_access',
'access arguments' => array(
'user key' => 'uid',
'access tag' => 'commerce_library_loan_access',
),
'permission labels' => array(
'singular' => t('loan'),
'plural' => t('loans'),
),
// // Prevent Redirect alteration of the order form.
'redirect' => FALSE,
),
);
return $return;
}
/**
* Implements hook_entity_property_info().
*/
function commerce_library_entity_property_info() {
$info = array();
$properties = &$info['commerce_library_loan']['properties'];
$properties['loan_id'] = array(
'type' => 'integer',
'label' => t('Loan ID', array(), array('context' => 'a drupal commerce loan')),
'description' => t('The internal numeric ID of the loan.'),
'schema field' => 'loan_id',
);
$properties['order_id'] = array(
'type' => 'integer',
'label' => t('Order ID', array(), array('context' => 'a drupal commerce order')),
'description' => t('The internal numeric ID of the order this product loan is from.'),
'required' => TRUE,
'schema field' => 'order_id',
);
$properties['order'] = array(
'label' => t('Order', array(), array('context' => 'a drupal commerce order')),
'type' => 'commerce_order',
'description' => t('The order the loan item belongs to.'),
'getter callback' => 'commerce_line_item_get_properties',
'setter callback' => 'commerce_line_item_set_properties',
'required' => TRUE,
'computed' => TRUE,
'clear' => array('order_id'),
);
$properties['status'] = array(
'type' => 'text',
'label' => t('Status'),
'description' => t('The current status of the loan.'),
'required' => TRUE,
'schema field' => 'status',
);
$properties['amount'] = array(
'label' => t('Amount'),
'description' => t('Amount'),
'type' => 'decimal',
'getter callback' => 'entity_property_verbatim_get',
'setter callback' => 'entity_property_verbatim_set',
'required' => TRUE,
'schema field' => 'amount',
);
$properties['created'] = array(
'type' => 'date',
'label' => t('Date created'),
'description' => t('The date the order was created.'),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer commerce_order entities',
'schema field' => 'created',
);
$properties['updated'] = array(
'type' => 'date',
'label' => t('Date changed'),
'description' => t('The date the loan was most recently updated.'),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer commerce_order entities',
'schema field' => 'updated',
);
$properties['fullfilled'] = array(
'type' => 'date',
'label' => t('Date fullfilled'),
'description' => t('The date the loan was fullfilled.'),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer commerce_order entities',
'schema field' => 'fullfilled',
);
$properties['due'] = array(
'type' => 'date',
'label' => t('Date due'),
'description' => t('The date the loan is due.'),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer commerce_order entities',
'schema field' => 'due',
);
$properties['returned'] = array(
'type' => 'date',
'label' => t('Date returned'),
'description' => t('The date the loan was returned.'),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer commerce_order entities',
'schema field' => 'returned',
);
return $info;
}
function commerce_library_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'commerce_library') . '/includes/views',
);
}
function commerce_library_permission() {
return array(
'administer commerce_library settings' => array('title' => t('Administer commerce library settings')),
'return commerce_library loans' => array('title' => t('Return commerce product loans')),
'renew commerce_library loans' => array('title' => t('Renew commerce product loans'))
);
}
/**
* Implements hook_commerce_checkout_pane_info().
*/
function commerce_library_commerce_checkout_pane_info() {
$checkout_panes = array();
$checkout_panes['cart_contents_loans'] = array(
'title' => t('Product loans'),
'base' => 'commerce_library_contents_pane',
'file' => 'includes/commerce_library.checkout_pane.inc',
'page' => 'checkout',
'weight' => -12,
);
return $checkout_panes;
}
/**
* Implements hook_menu().
*/
function commerce_library_menu() {
$items = array();
$items['admin/commerce/config/library'] = array(
'title' => 'Library management',
'description' => 'Configure library settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('commerce_library_admin_form'),
'access arguments' => array('administer commerce_library settings'),
'file' => 'includes/commerce_library.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/commerce/loans/%commerce_library_loan/return'] = array(
'title' => 'Return',
'description' => 'Return loan.',
'page callback' => 'drupal_get_form',
'page arguments' => array('commerce_library_return_form', 3),
'access arguments' => array('return commerce_library loans'),
'file' => 'includes/commerce_library.forms.inc',
'context' => MENU_CONTEXT_INLINE,
'type' => MENU_LOCAL_TASK,
);
$items['admin/commerce/loans/%commerce_library_loan/renew'] = array(
'title' => 'Renew',
'description' => 'Renew loan.',
'page callback' => 'drupal_get_form',
'page arguments' => array('commerce_library_renew_form', 3),
'access arguments' => array('renew commerce_library loans'),
'file' => 'includes/commerce_library.forms.inc',
'context' => MENU_CONTEXT_INLINE,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function commerce_library_get_default_messages() {
$options=array();
$options['mail']['due-notify-title']=variable_get('commerce_library_due_subject', t("Product [commerce-product:title] loan is due soon"));
$options['mail']['due-notify-body']=variable_get('commerce_library_due_body', t("Dear [user:name]<br>\nYour loan of the product, [commerce-product:title] ([commerce-product:sku]), should be returned soon.") );
$options['mail']['late-notify-title']=variable_get('commerce_library_late_subject', t("Product [commerce-product:sku] loan is due"));
$options['mail']['late-notify-body']=variable_get('commerce_library_late_body', t("Dear [user:name]<br>\nYour loan of the product, [commerce-product:title] ([commerce-product:sku]), is late and should be returned as soon as possible."));
return $options;
}
/**
* Loan states
* - canceled: Order and loans in it are canceled
* - pending: Order and loans are pending handling
* - completed: Loaned items have been delivered to customer
*/
function commerce_library_loan_state_info() {
$s = array();
$s['canceled'] = array(
'name' => 'canceled',
'title' => t('Canceled'),
'description' => t('Loans in this state have been canceled through some user action.'),
'weight' => -10,
'default_status' => 'canceled',
);
$s['pending'] = array(
'name' => 'pending',
'title' => t('Pending'),
'description' => t('Loans in this state have been created and are awaiting further action.'),
'weight' => 0,
'default_status' => 'pending',
);
$s['completed'] = array(
'name' => 'completed',
'title' => t('Completed'),
'description' => t('Loans in this state have been completed as far as the customer is concerned.'),
'weight' => 10,
'default_status' => 'completed',
);
return $s;
}
function commerce_library_loan_label($entity, $entity_type) {
return t('Loan @number', array('@number' => $entity->order_number));
}
function commerce_library_loan_uri($order) {
// Allow modules to specify a path, returning the first one found.
foreach (module_implements('commerce_library_loan_uri') as $module) {
$uri = module_invoke($module, 'commerce_library_loan_uri', $order);
// If the implementation returned data, use that now.
if (!empty($uri)) {
return $uri;
}
}
return NULL;
}
function commerce_library_send_message($type, $account, $product) {
$params['account'] = $account;
$params['product'] = $product;
drupal_mail('commerce_library', $type, $account->mail, user_preferred_language($account), $params);
}
function commerce_library_mail($key, &$message, $params) {
$options['language'] = $message['language'];
$a=$params['account'];
$p=$params['product'];
$td=array(
'user'=>$a,
'commerce-product'=>$p,
);
$variables=array();
switch ($key) {
case 'reminder':
$subject=t("Product [commerce-product:title] loan is due soon");
$body=t("Dear [user:name]<br>\nYour loan of the product, [commerce-product:title] ([commerce-product:sku]), should be returned soon.");
break;
case 'late':
$subject=t("Product [commerce-product:sku] loan is due");
$body=t("Dear [user:name]<br>\nYour loan of the product, [commerce-product:title] ([commerce-product:sku]), is late and should be returned as soon as possible.");
break;
default:
return;
}
$message['subject'] = token_replace($subject, $td);
$message['body'][] = token_replace($body, $td);
}
/**
* Implements hook_cron().
*/
function commerce_library_cron() {
$last_timestamp = variable_get('commerce_library_onceperday_last', 0);
$now=time();
$last_date = date('Y-m-d', $last_timestamp);
$this_date = date('Y-m-d', $now);
$new_day = ($this_date != $last_date);
if (!$new_day)
return;
$this_hour = date('H', $now);
// XXX: every morning for now, could be a setting
$after_hour = $this_hour >= 8;
if (!$after_hour)
return;
variable_set('commerce_library_onceperday_last', $now);
// XXX: Use proper settings for these, but for now:
// - reminder 7 days before (use 7 for invalid, clamp to 2 - 14)
// - late notification 1 day after (use -1 for invalid, clamp to -7 - -1)
$reminder_days=variable_get('commerce_library_reminder_days', 7);
if ($reminder_days<2 || $reminder_days>14) $reminder_days=7;
$late_days=variable_get('commerce_library_late_days', -1);
if ($late_days>-1 || $late_days<-7) $late_days=-1;
commerce_library_check_reminders($reminder_days);
commerce_library_check_reminders($late_days);
}
/**
* Check for loans that are returned (in given days) and send reminder emails to the user
*/
function commerce_library_check_reminders($days) {
$dueby=time()+(86400*$days);
$q=db_select('commerce_library_loan', 'cll');
$q->join('commerce_order', 'co', 'cll.order_id=co.order_id');
$q->fields('cll', array('loan_id', 'product_id', 'due'));
$q->fields('co', array('uid', 'mail'));
$q->condition('due', $dueby, '<')
->condition('returned', 0, '=')
->orderBy('due', 'ASC');
$reminders=$q->execute();
foreach ($reminders as $r) {
$account = user_load($r->uid);
$account->mail=$r->mail;
$product=commerce_product_load($r->product_id);
// Reminder if positive days, late message if negative or 0
commerce_library_send_message($days>0 ? 'reminder' : 'late', $account, $product);
}
}
/**
* Return given loan
* - Adds back the loan amount to the products stock
* - Marks loan as returned
*/
function commerce_library_loan_return($loan) {
if (empty($loan))
return false;
$product=commerce_product_load($loan->product_id);
$transaction = db_transaction();
// Mark the loan as returned
$n = db_update('commerce_library_loan')
->fields(array(
'status' => 'completed',
'returned' => REQUEST_TIME,
))
->condition('loan_id', $loan->loan_id, '=')
->execute();
// Add back to product stock
if (module_exists('commerce_ss')) {
commerce_library_commerce_ss_stock_adjust($product, $loan->amount);
}
return $n>0 ? true : false;
}
/**
* Renew given loan.
*
* Updates the due date (today+loan date)
* Updates renew counter
*/
function commerce_library_loan_renew($loan) {
if (empty($loan))
return false;
// Check for invalid values, this should not happen but who knows...
// XXX: Be more specific with the return value
if ($loan->status!='completed' || $loan->returned>$this->fullfilled)
return false;
// Due for return before fullfilled?
if ($loan->due<$this->fullfilled)
return false;
$product=commerce_product_load($loan->product_id);
$loan_days=commerce_library_get_product_loan_days($product);
// XXX: We should check status of the loan, if not completed, bail.
$loan->updated=REQUEST_TIME;
// If due date is set properly, as it should, add loan days to it.
// but in case the due date is invalid, use current time, just in case.
if ($loan->due>0)
$loan->due=$loan->due+(86400*$loan_days);
else
$loan->due=time()+(86400*$loan_days);
$loan->reloaned++;
return commerce_library_loan_save($loan);
}
function commerce_library_get_product_loan_days($product)
{
$days=(int)$product->commerce_library['und'][0]['value'];
if ($days>0)
return $days;
return false;
}
/**
* commerce_ss stock adjusting, copy&paste here as the function is in a rules.inc in the commerce_stock module :(
*/
function commerce_library_commerce_ss_stock_adjust($product, $qty) {
if (!commerce_ss_product_type_enabled($product->type)) {
return;
}
$wrapper = entity_metadata_wrapper('commerce_product', $product);
$new_stock = $wrapper->commerce_stock->value() + $qty;
$wrapper->commerce_stock->set($new_stock);
$result = $wrapper->save();
if ($result) {
watchdog('commerce_library', 'Modified stock level of product %sku by %amount', array('%sku' => $product->sku, '%amount' => $qty));
} else {
watchdog('commerce_library', 'Failed attempt to modify stock level of product %sku by %amount', array('%sku' => $product->sku, '%amount' => $qty), WATCHDOG_ERROR);
}
return $result;
}
/**
* Implements hook_entity_property_info_alter().
*/
function commerce_library_entity_property_info_alter(&$info) {
if (empty($info['commerce_product']['bundles'])) {
return;
}
$properties = array();
foreach ($info['commerce_product']['bundles'] as $bundle => $bundle_info) {
$bundle_info += array('properties' => array());
$properties += $bundle_info['properties'];
}
if (isset($properties['commerce_library'])) {
$info['commerce_product']['properties']['commerce_stock'] = $properties['commerce_stock'];
}
}
/**
* Implements hook_form_ID_alter().
*
*/
function commerce_library_form_commerce_product_ui_product_type_form_alter(&$form, &$form_state) {
return;
// Get the product bundle type.
$type = $form_state['product_type']['type'];
// Check if loan enabled for the type.
if (commerce_library_product_type_enabled($type)) {
$form['product_type']['loan_days'] = array(
// Provide a configuration option for loan days
'#type' => 'textfield',
'#title' => t('Loan in days'),
'#default_value' => variable_get('commerce_library_default_loan_days_'.$type, FALSE),
'#description' => t('Set the loan days for this product'),
'#element_validate' => array('element_validate_integer_positive'),
);
}
}
/**
* Implements hook_form_ID_alter().
*
*/
function commerce_library_form_commerce_product_ui_product_form_alter(&$form, &$form_state) {
// Get the product bundle type.
$type = $form_state['commerce_product']->type;
// Check if loan is enabled for the type.
if (commerce_library_product_type_enabled($type)) {
$days = variable_get('commerce_library_default_loan_days_'.$type, FALSE);
if (!$days) {
$language = $form['commerce_library']['#language'];
$form['commerce_library'][$language][0]['value']['#default_value'] = intval($form['commerce_library'][$language][0]['value']['#default_value']);
}
}
}
/**
* Implements hook_inline_entity_form_alter().
*
*/
function commerce_library_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
if ($entity_form['#entity_type'] == 'commerce_product') {
$type = $entity_form['#bundle'];
if (commerce_library_product_type_enabled($type)) {
$days = variable_get('commerce_library_default_loan_days_'.$type, FALSE);
if (!$days) {
$language = $entity_form['commerce_stock']['#language'];
$entity_form['commerce_library'][$language][0]['value']['#default_value'] = intval($entity_form['commerce_library'][$language][0]['value']['#default_value']);
}
}
}
}
/**
* Determines whether library management is enabled on a product type.
*
* @param $type
* The product type.
*
* @return bool
* TRUE if library management is enabled.
*/
function commerce_library_product_type_enabled($type) {
$instance = field_info_instance('commerce_product', 'commerce_library', $type);
return (!empty($instance));
}
function commerce_library_product_type_override_enabled($type) {
$instance = field_info_instance('commerce_product', 'commerce_library_days', $type);
return (!empty($instance));
}
/**
* Determines whether library management is enabled for a product.
*
* @param $product
* The product to check.
*
* @return bool
* TRUE if library management is enabled on the product's product type.
*/
function commerce_library_product_enabled($product) {
return commerce_library_product_type_enabled($product->type);
}
/**
* For a given a line item, determine whether library management is enabled.
*
* @param $line_item
* The line item to check.
*
* @return
* Boolean: TRUE if loan management is enabled on the product's product type.
*/
function commerce_library_line_item_product_enabled($line_item) {
return commerce_library_product_type_enabled($line_item->product->type);
}
/**
* Implements hook_inline_entity_form_table_fields_alter().
*/
function commerce_library_inline_entity_form_table_fields_alter(&$fields, $context) {
if ($context['entity_type'] == 'commerce_product') {
$has_loan_field = TRUE;
foreach ($context['allowed_bundles'] as $bundle) {
if (!commerce_library_product_type_enabled($bundle)) {
$has_load_field = FALSE;
}
}
if ($has_loan_field) {
// Loan status
$fields['commerce_library'] = array(
'type' => 'field',
'label' => t('Lendable'),
'formatter' => 'number_integer',
'weight' => 101);
// Loan days
$fields['commerce_library_loan_days'] = array(
'type' => 'field',
'label' => t('Loan days'),
'formatter' => 'number_integer',
'weight' => 101,
);
}
}
}
function commerce_library_loan_new($order_id, $product_id, $amount=0, $status = NULL, $type = 'commerce_library_loan') {
// If no status was specified, use the default Pending status.
if (!isset($status)) {
$status = 'pending';
}
return entity_get_controller('commerce_library_loan')->create(array(
'order_id' => $order_id,
'product_id' => $product_id,
'amount' => $amount,
'status' => $status,
));
}
function commerce_library_loan_save($loan) {
return entity_get_controller('commerce_library_loan')->save($loan);
}
function commerce_library_loan_load_multiple($loan_ids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('commerce_library_loan', $loan_ids, $conditions, $reset);
}
function commerce_library_loan_load($loan_id) {
$loans = commerce_library_loan_load_multiple(array($loan_id), array());
return $loans ? reset($loans) : FALSE;
}
function commerce_library_loan_status_update($loan, $name, $skip_save = FALSE) {
// Do not update the status if the order is already at it.
if ($loan->status != $name) {
$loan->status = $name;
commerce_library_loan_save($loan);
}
return $order;
}
function commerce_library_loan_access($op, $loan = NULL, $account = NULL) {
return commerce_entity_access($op, $loan, $account, 'commerce_library_loan');
}
function commerce_library_loan_rules_access($type, $name) {
if ($type == 'event' || $type == 'condition') {
return commerce_library_loan_access('view');
}
}
/**
* Find loans from given order, returns an array of products that was lended.
*/
function commerce_library_extract_lendable_products($order) {
$ids=array();
foreach ($order->commerce_line_items['und'] as $num => $item) {
$ids[]=$item['line_item_id'];
}
$line_items=commerce_line_item_load_multiple($ids);
$lp=array();
foreach ($line_items as $line_item) {
// Skip non-products
if ($line_item->type!='product')
continue;
$liemw=entity_metadata_wrapper('commerce_line_item', $line_item);
$product_id=$liemw->commerce_product->product_id->value();
$product=commerce_product_load($product_id);
if (!commerce_library_product_type_enabled($product->type))
continue;
// Add the full product object for easy access
$line_item->product_class=$product;
$lp[$product_id]=$line_item;
}
return $lp;
}
/**
* Load all loans by a specific user
* XXX: Do even need this ?
*/
function commerce_library_load_loans_by_user($user)
{
}
/**
* Load loan enties for a specific order
*/
function commerce_library_load_loans_by_order($order) {
$ol = db_select('commerce_library_loan', 'cll')
->fields('cll', array('loan_id'))
->condition('order_id', $order->order_id, '=')
->orderBy('product_id', 'ASC')
->execute();
$r=array();
foreach ($ol as $l) {
$loan=commerce_library_loan_load($l->loan_id);
$r[$l->product_id]=$loan;
}
return $r;
}
/**
* Saves loans from a order and updates details according to status
*/
function commerce_library_save_loans_from_order($order, $status) {
$loans=commerce_library_extract_lendable_products($order);
// Ignore if there are no lendable products in the order
if (empty($loans)) {
// Just in case, delete any loans from this order as the admin can remove items from the order
// XXX: Or should we mark them items as canceled instead?
$num_deleted = db_delete('commerce_library_loan')
->condition('order_id', $order->order_id)
->execute();
return;
}
// If there are, check if this loan is alreday saved or not
$lo=commerce_library_load_loans_by_order($order);
// Save them
foreach ($loans as $product_id => $line_item) {
// Existing loan, update details
// Get the product loan days
$loan_days=commerce_library_get_product_loan_days($line_item->product_class);
// If the days are false (invalid or 0), we assume the product is a "normal" product
if ($loan_days===fales)
continue;
if (isset($lo[$product_id])) {
$loan=$lo[$product_id];
$loan->status=$status;
$loan->updated=REQUEST_TIME;
switch ($status) {
case 'completed':
if ($loan->due==0) {
$loan->fullfilled=REQUEST_TIME;
$loan->due=$loan->fullfilled+(86400*$loan_days);
}
break;
case 'processing':
$loan->fullfilled=REQUEST_TIME;
$loan->due=null;
break;
case 'pending':
case 'canceled':
$loan->fullfilled=null;
$loan->due=null;
break;
default:;
}
} else {
// New loan, save it
$loan=commerce_library_loan_new($order->order_id, $product_id, (int)$line_item->quantity, $status);
// Special case in case order is complete but loans are not yet saved
switch ($order->status) {
case 'complete':
$loan->fullfilled=$order->placed;
$loan->due=$loan->fullfilled+(86400*$loan_days);
break;
case 'processing':
$loan->fullfilled=$order->placed;
$loan->due=null;
break;
case 'pending':
case 'canceled':
default:;
}
}
commerce_library_loan_save($loan);
}
}
/**
* Update loan data from order details
*
* When order is placed and put into pending state, insert the pending loan into
*/
function commerce_library_loan_update_from_order($order) {
switch ($order->status) {
case 'cart':
case 'checkout_checkout':
case 'checkout_shipping':
case 'checkout_review':
case 'checkout_complete':
// Just ignore it for now
//watchdog('commerce_library', 'Ignoring status');
return;
break;
case 'pending':
case 'processing':
case 'completed':
commerce_library_save_loans_from_order($order, $order->status);
break;
default:
watchdog('commerce_library', 'Loan processing @status', array('@status'=>$order->status));
break;
}
}
function commerce_library_commerce_order_update($order) {
commerce_library_loan_update_from_order($order);
}
function commerce_library_commerce_order_presave($order) {
}
function commerce_library_loan_status_info() {
$order_status = array();
$loan_status['pending'] = array(
'name' => 'pending',
'title' => t('Pending'),
'state' => 'pending',
);
$loan_status['canceled'] = array(
'name' => 'canceled',
'title' => t('Canceled'),
'state' => 'canceled',
);
$loan_status['processing'] = array(
'name' => 'processing',
'title' => t('Processing'),
'state' => 'pending',
'weight' => 5,
);
$loan_status['completed'] = array(
'name' => 'completed',
'title' => t('Completed'),
'state' => 'completed',
'weight' => 10,
);
return $loan_status;
}