-
Notifications
You must be signed in to change notification settings - Fork 0
/
roomify_channel_connector.module
2314 lines (2003 loc) · 86.4 KB
/
roomify_channel_connector.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
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
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Code for the roomify_channel_connector feature.
*/
use Roomify\Bat\Event\Event;
use Roomify\Bat\Calendar\Calendar;
use Roomify\Bat\Store\DrupalDBStore;
use Roomify\Bat\Unit\Unit;
use Roomify\Bat\Constraint\MinMaxDaysConstraint;
use Roomify\Bat\Constraint\CheckInDayConstraint;
use Roomify\Bat\Constraint\ConstraintManager;
include_once 'roomify_channel_connector.features.inc';
/**
* Implements hook_menu().
*/
function roomify_channel_connector_menu() {
$items = array();
// Channel management tab for units.
$items['admin/config/services/channel-management'] = array(
'title' => 'Roomify Channel Management',
'page callback' => 'drupal_get_form',
'page arguments' => array('roomify_channel_connector_configuration_form'),
'access arguments' => array('configure channel management'),
'type' => MENU_NORMAL_ITEM,
'description' => 'Configuration for the Roomify Channel Management service',
);
// Channel management tab for units.
$items['admin/bat/config/property/manage/%roomify_property/channel-management'] = array(
'title' => 'Channel Management',
'page callback' => 'roomify_channel_connector_management_tab',
'page arguments' => array(5),
'access callback' => 'roomify_channel_connector_management_tab_access',
'access arguments' => array(5),
'type' => MENU_LOCAL_TASK,
'weight' => '20',
);
$items['admin/bat/config/property/manage/%roomify_property/channel-management/resolve-conflict/%'] = array(
'page callback' => 'roomify_channel_connector_management_resolve_conflict',
'page arguments' => array(5, 8),
'access callback' => 'roomify_channel_connector_management_tab_access',
'access arguments' => array(5),
);
$items['admin/bat/config/property/manage/%roomify_property/channel-management/resync/%'] = array(
'page callback' => 'roomify_channel_connector_management_resync_reservation',
'page arguments' => array(5, 8),
'access callback' => 'roomify_channel_connector_management_tab_access',
'access arguments' => array(5),
);
return $items;
}
/**
* Implements hook_permission().
*/
function roomify_channel_connector_permission() {
return array(
'configure channel management' => array(
'title' => t('Configure Channel Management'),
'description' => t('Configure Roomify Channel Management.'),
),
'manage property channels' => array(
'title' => t('Manage Property Channels'),
'description' => t('Access the Channel Management tab on properties.'),
),
'create channel reservations' => array(
'title' => t('Create Channel Reservations'),
'description' => t('Create Channel Reservations.'),
),
'retrieve channel availability information' => array(
'title' => t('Retrieve channel availability info'),
'description' => t('Retrieve channel availability info'),
),
'retrieve type availability information' => array(
'title' => t('Retrieve type availability info'),
'description' => t('Retrieve type availability info'),
),
'retrieve rate information' => array(
'title' => t('Retrieve channel rate info'),
'description' => t('Retrieve channel rate info'),
),
'retrieve restriction information' => array(
'title' => t('Retrieve channel restriction info'),
'description' => t('Retrieve channel restriction info'),
),
);
}
/**
* Access callback for the 'Channel Management' tab.
*/
function roomify_channel_connector_management_tab_access($property) {
if (variable_get('roomify_channel_connector_site_id') != '') {
if ($property->type == 'casa_property' || $property->type == 'locanda_property') {
return user_access('manage property channels');
}
}
return FALSE;
}
/**
* Module configuration form.
*/
function roomify_channel_connector_configuration_form($form, &$form_state) {
if (variable_get('roomify_channel_connector_site_id', '') == '') {
$form['intro'] = array(
'#markup' => '<h1 style="margin-bottom: 20px;">' . t('To setup channel management you just need to do the following:') . '</h1><h3 style="font-weight: 200;">' . t('Request a Site ID / Token, Connector URL for channel management access from Roomify (email [email protected])') . '</h3>',
);
}
$form['roomify_channel_connector_site_id'] = array(
'#type' => 'textfield',
'#title' => t('Roomify Site ID'),
'#description' => t('Enter the Site ID you were given when subscribing to the Roomify channel management service.'),
'#element_validate' => array('element_validate_integer_positive'),
'#default_value' => variable_get('roomify_channel_connector_site_id', ''),
'#required' => TRUE,
);
$form['roomify_channel_connector_site_token'] = array(
'#type' => 'textfield',
'#title' => t('Roomify Site Token'),
'#description' => t('Enter the Site Token you were given when subscribing to the Roomify channel management service.'),
'#default_value' => variable_get('roomify_channel_connector_site_token', ''),
'#required' => TRUE,
);
$form['roomify_channel_connector_app_url'] = array(
'#type' => 'textfield',
'#title' => t('Connect App URL'),
'#description' => t('Enter the Connect App URL you were given when subscribing to the Roomify channel management service.'),
'#default_value' => variable_get('roomify_channel_connector_app_url', ''),
'#required' => TRUE,
);
if (variable_get('roomify_channel_connector_site_id', '') == '') {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Activate'),
);
}
else {
$form['roomify_channel_connector_site_id']['#disabled'] = TRUE;
$form['roomify_channel_connector_site_token']['#disabled'] = TRUE;
$form['roomify_channel_connector_app_url']['#disabled'] = TRUE;
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Disconnect'),
);
if (variable_get('roomify_accommodation_booking_future_limit', 31104000) < 31104000) {
drupal_set_message(t('Your current limit for bookings in the future is less than a year. It is recommended you set it to at least one year as most OTAs require that. Bookings in the future that are over the limit will not be created otherwise. We suggest to set it at least one year. You can change it <a href="@link">here</a>.', array('@link' => url('admin/bat/config/booking/settings'))), 'warning');
}
}
$role = user_role_load_by_name(variable_get('service_api_key_role', 'roomify manager'));
$query = 'SELECT DISTINCT(ur.uid)
FROM {users_roles} AS ur
WHERE ur.rid = :rids LIMIT 1';
$result = db_query($query, array(':rids' => $role->rid));
if ($result->fetchColumn() === FALSE) {
$form['roomify_channel_connector_site_id']['#disabled'] = TRUE;
$form['roomify_channel_connector_site_token']['#disabled'] = TRUE;
$form['roomify_channel_connector_app_url']['#disabled'] = TRUE;
$form['submit']['#disabled'] = TRUE;
drupal_set_message(t('No user with the @role role found, please contact support.', array('@role' => $role->name)), 'error');
}
return $form;
}
/**
* Submit callback for the roomify_channel_connector_configuration_form form.
*/
function roomify_channel_connector_configuration_form_submit($form, &$form_state) {
if (variable_get('roomify_channel_connector_site_id', '') == '') {
$site_id = $form_state['values']['roomify_channel_connector_site_id'];
$site_token = $form_state['values']['roomify_channel_connector_site_token'];
$app_url = $form_state['values']['roomify_channel_connector_app_url'];
// Normalize connector URL.
if (substr($app_url, -1) != '/') {
$app_url .= '/';
}
if (roomify_channel_connector_validate_site_id($site_id, $site_token, $app_url)) {
variable_set('roomify_channel_connector_site_id', $site_id);
variable_set('roomify_channel_connector_site_token', $site_token);
variable_set('roomify_channel_connector_app_url', $app_url);
// Rebuild permissions to take into account the channel connection.
roomify_rights_rebuild_permissions();
drupal_set_message(t('You have successfully connected to the Roomify Channel Manager'));
}
else {
drupal_set_message(t('The Connector App URL, Roomify Site ID, or Roomify Site Token is not correct or the App is currently not responding. Please try again and if it continues to fail send the URL you are trying to use to [email protected]'), 'error');
}
}
else {
variable_del('roomify_channel_connector_site_id');
variable_del('roomify_channel_connector_site_token');
variable_del('roomify_channel_connector_app_url');
roomify_rights_rebuild_permissions();
}
}
/**
* @param $site_id
* @param $site_token
* @param $app_url
*
* @return bool
*/
function roomify_channel_connector_validate_site_id($site_id, $site_token, $app_url) {
$request = drupal_http_request($app_url . 'admin/authcheck/' . $site_id . '/' . $site_token);
if (isset($request->status_message) && $request->status_message == 'OK') {
return TRUE;
}
return FALSE;
}
/**
* Implements hook_services_api_key_valid_alter().
*
* This ignores the settings at the service level and instead takes into consideration
* the API key set for the channel manager.
*
* @param $valid
* @param $args
*/
function roomify_channel_connector_services_api_key_valid_alter(&$valid, &$args) {
// Ensure we are dealing with the CM endpoint.
if (arg(0) == 'rcc') {
$api_key = variable_get('roomify_channel_connector_site_token');
// Compare the api token to what is provided via the api call.
$args[0]['api_key'] = (!empty($_SERVER['HTTP_API_KEY'])) ? $_SERVER['HTTP_API_KEY'] : '';
$valid = services_api_key_auth_compare_key($api_key, $args[0]['api_key']);
// Get list of users with given role.
$role = user_role_load_by_name(variable_get('service_api_key_role', 'roomify manager'));
$query = 'SELECT DISTINCT(ur.uid)
FROM {users_roles} AS ur
WHERE ur.rid = :rids LIMIT 1';
$result = db_query($query, array(':rids' => $role->rid));
// Set the user to a roomify manager.
$user = '';
while ($uid = $result->fetchColumn()) {
$user = user_load($uid);
}
$args[0]['user'] = $user->name;
}
}
/**
* Implements hook_roomify_rights().
*
* @return mixed
*/
function roomify_channel_connector_roomify_rights() {
$rights['roomify_channel_connector'] = array();
if (variable_get('roomify_channel_connector_site_id') != NULL) {
$rights['roomify_channel_connector'] = array(
'roomify manager' => array(
'manage property channels',
'create channel reservations',
'retrieve type availability information',
'retrieve channel availability information',
'retrieve rate information',
'retrieve restriction information',
),
);
}
return $rights;
}
/**
*
*/
function roomify_channel_connector_services_resources() {
$resources = array();
$resources['channel-reservation'] = array(
'operations' => array(
'create' => array(
'callback' => 'roomify_channel_connector_create',
'help' => t('Creates a new reservation through the channel manager'),
'access arguments' => array('create channel reservations'),
'args' => array(
array(
'name' => 'reservation',
'type' => 'array',
'description' => 'The reservation object',
'source' => 'data',
'optional' => FALSE,
),
),
),
),
);
$resources['channel-availability'] = array(
'operations' => array(
'index' => array(
'callback' => 'roomify_channel_connector_availability',
'help' => t('Returns availability information in Json.'),
'access arguments' => array('retrieve channel availability information'),
'args' => array(
array(
'name' => 'property_id',
'type' => 'string',
'description' => t('The property id for which to retrieve info - should be mapped correctly'),
'source' => array('path' => 0),
'optional' => TRUE,
),
array(
'name' => 'room_id',
'type' => 'string',
'description' => t('The roomid for which to retrieve availability info'),
'source' => array('path' => 1),
'optional' => TRUE,
),
array(
'name' => 'rate_id',
'type' => 'string',
'description' => t('The rateid for which to retrieve availability info'),
'source' => array('path' => 2),
'optional' => TRUE,
),
array(
'name' => 'start_date',
'type' => 'string',
'description' => t('The start date for which to retrieve availability info - e.g. 2016-03-01'),
'source' => array('path' => 3),
'optional' => TRUE,
),
array(
'name' => 'end_date',
'type' => 'string',
'description' => t('The end date for which to retrieve availability info - e.g. 2016-03-01'),
'source' => array('path' => 4),
'optional' => TRUE,
),
),
),
),
);
$resources['type-availability'] = array(
'operations' => array(
'index' => array(
'callback' => 'roomify_channel_type_availability',
'help' => t('Returns availability information for a bat type in Json.'),
'access arguments' => array('retrieve type availability information'),
'args' => array(
array(
'name' => 'property_id',
'type' => 'string',
'description' => t('The property id for which to retrieve info - should be mapped correctly'),
'source' => array('path' => 0),
'optional' => TRUE,
),
array(
'name' => 'type_id',
'type' => 'string',
'description' => t('The type id for which to retrieve availability info'),
'source' => array('path' => 1),
'optional' => TRUE,
),
array(
'name' => 'start_date',
'type' => 'string',
'description' => t('The start date for which to retrieve availability info - e.g. 2016-03-01'),
'source' => array('path' => 2),
'optional' => TRUE,
),
array(
'name' => 'end_date',
'type' => 'string',
'description' => t('The end date for which to retrieve availability info - e.g. 2016-03-01'),
'source' => array('path' => 3),
'optional' => TRUE,
),
),
),
),
);
$resources['channel-rates'] = array(
'operations' => array(
'index' => array(
'callback' => 'roomify_channel_connector_rates',
'help' => t('Returns rate information in Json.'),
'access arguments' => array('retrieve rate information'),
'args' => array(
array(
'name' => 'property_id',
'type' => 'string',
'description' => t('The property id for which to retrieve info - should be mapped correctly'),
'source' => array('path' => 0),
'optional' => TRUE,
),
array(
'name' => 'room_id',
'type' => 'string',
'description' => t('The room_id for which to retrieve availability info'),
'source' => array('path' => 1),
'optional' => TRUE,
),
array(
'name' => 'rate_id',
'type' => 'string',
'description' => t('The rate_id for which to retrieve availability info'),
'source' => array('path' => 2),
'optional' => TRUE,
),
array(
'name' => 'start_date',
'type' => 'string',
'description' => t('The start date for which to retrieve availability info - e.g. 2016-03-01'),
'source' => array('path' => 3),
'optional' => TRUE,
),
array(
'name' => 'end_date',
'type' => 'string',
'description' => t('The end date for which to retrieve availability info - e.g. 2016-03-01'),
'source' => array('path' => 4),
'optional' => TRUE,
),
),
),
),
);
$resources['channel-restrictions'] = array(
'operations' => array(
'index' => array(
'callback' => 'roomify_channel_connector_restrictions',
'help' => t('Returns restriction information in Json.'),
'access arguments' => array('retrieve restriction information'),
'args' => array(
array(
'name' => 'property_id',
'type' => 'string',
'description' => t('The property id for which to retrieve info - should be mapped correctly'),
'source' => array('path' => 0),
'optional' => TRUE,
),
array(
'name' => 'room_id',
'type' => 'string',
'description' => t('The room_id for which to retrieve availability info'),
'source' => array('path' => 1),
'optional' => TRUE,
),
array(
'name' => 'rate_id',
'type' => 'string',
'description' => t('The rate_id for which to retrieve availability info'),
'source' => array('path' => 2),
'optional' => TRUE,
),
array(
'name' => 'start_date',
'type' => 'string',
'description' => t('The start date for which to retrieve availability info - e.g. 2016-03-01'),
'source' => array('path' => 3),
'optional' => TRUE,
),
array(
'name' => 'end_date',
'type' => 'string',
'description' => t('The end date for which to retrieve availability info - e.g. 2016-03-01'),
'source' => array('path' => 4),
'optional' => TRUE,
),
),
),
),
);
return $resources;
}
/**
* Creates a booking of type booking.com
*
* @param $reservations
*
* @return string
*/
function roomify_channel_connector_create($reservations) {
watchdog('Channel Manager', 'Bookingcom Create Reservation Called', array(), WATCHDOG_INFO);
$reply = array();
foreach ($reservations as $reservation) {
// Let us check if cancelled and deal with that.
if ($reservation['status'] == 'cancelled') {
// We should have all the bookings associated with this reservation so let's load them and set status to cancelled.
$efq = new EntityFieldQuery();
$efq->entityCondition('entity_type', 'bat_booking')
->entityCondition('bundle', 'booking_com', '=')
->fieldCondition('field_bcom_reservation_id', 'value', $reservation['id'], '=');
$bookings = $efq->execute();
if (isset($bookings['bat_booking'])) {
// Cycle through each booking and set status to cancelled.
foreach ($bookings['bat_booking'] as $key => $booking) {
$cm_room = bat_booking_load($key);
$cm_room->field_bcom_status[LANGUAGE_NONE][0]['value'] = "cancelled";
$cm_room->field_bcom_cancellation_fee[LANGUAGE_NONE][0]['value'] = $reservation['total_cancellation_fee'];
$event = bat_event_load($cm_room->booking_event_reference[LANGUAGE_NONE][0]['target_id']);
$available_state = bat_event_load_state_by_machine_name(AVAILABLE);
$event->event_state_reference[LANGUAGE_NONE][0]['state_id'] = $available_state['id'];
$event->save();
$cm_room->save();
watchdog('Channel Manager', 'Booking.com reservation @reservation_id cancelled', array('@reservation_id' => $reservation['id']), WATCHDOG_INFO);
$reply['message'] = 'Booking.com reservation ' . $reservation['id'] . ' cancelled';
$reply['status'] = 'cancelled';
}
}
if (!isset($bookings['bat_booking']) || count($bookings['bat_booking']) == 0) {
$reply['message'] = 'Booking.com reservation ' . $reservation['id'] . ' not found';
$reply['status'] = 'booking_not_found';
}
return $reply;
}
// With modified reservations two things can happen. 1.
// A room is completely cancelled or room data has been altered or changed.
// We should remove all existing bookings and create new ones with status modified.
if ($reservation['status'] == 'modified') {
// We start by getting all the bookings on our system and check
// if they still match bookings on the other side, bookings
// that are lost are set to cancelled, the ones that remain are set to modified.
$efq = new EntityFieldQuery();
$efq->entityCondition('entity_type', 'bat_booking')
->entityCondition('bundle', 'booking_com', '=')
->fieldCondition('field_bcom_reservation_id', 'value', $reservation['id'], '=');
$bookings = $efq->execute();
if (isset($bookings['bat_booking'])) {
// Cycle through the rooms coming from Booking.com.
$bookingrooms = array();
foreach ($reservation['rooms'] as $room) {
$bookingrooms[] = $room['roomreservation_id'];
}
// Do a comparison between room info we got and room info
// that we have and set any rooms that got cancelled.
foreach ($bookings['bat_booking'] as $key => $booking) {
$cm_room = bat_booking_load($key);
// Check if the booking still exists.
$room_res_id = $cm_room->field_bcom_room_reservation_id[LANGUAGE_NONE][0]['value'];
if (in_array($room_res_id, $bookingrooms)) {
// We are good room is still there.
}
else {
$cm_room->field_bcom_status[LANGUAGE_NONE][0]['value'] = "cancelled";
// Load the event and set it to available.
$event = bat_event_load($cm_room->booking_event_reference[LANGUAGE_NONE][0]['target_id']);
$available_state = bat_event_load_state_by_machine_name(AVAILABLE);
$event->event_state_reference[LANGUAGE_NONE][0]['state_id'] = $available_state['id'];
$event->save();
$cm_room->save();
watchdog('Channel Manager', 'Booking.com room reservation @reservation_id cancelled', array('@reservation_id' => $cm_room->field_bcom_room_reservation_id[LANGUAGE_NONE][0]['value']), WATCHDOG_INFO);
$reply['message'] = 'Booking.com room reservation ' . $cm_room->field_bcom_room_reservation_id[LANGUAGE_NONE][0]['value'] . ' cancelled';
$reply['status'] = 'cancelled';
}
}
}
}
// Now we move on with dealing with reservation data knowing
// that we may be creating or modifying existing rooms or dealing with
// a new reservation.
foreach ($reservation['rooms'] as $room) {
// Check if we are dealing with an existing reservation.
$efq = new EntityFieldQuery();
$efq->entityCondition('entity_type', 'bat_booking')
->entityCondition('bundle', 'booking_com', '=')
->fieldCondition('field_bcom_reservation_id', 'value', $reservation['id'], '=')
->fieldCondition('field_bcom_room_reservation_id', 'value', $room['roomreservation_id'], '=');
$result = $efq->execute();
if (isset($result['bat_booking'])) {
$return = key($result['bat_booking']);
$booking = bat_booking_load($return);
$updated_time = $reservation['date'] . ' ' . $reservation['time'];
$booking->changed = strtotime($updated_time);
// Dates may have changed so reset the event.
$booking->field_bcom_status[LANGUAGE_NONE][0]['value'] = "modified";
// Dates may have changed so reset the existing event.
$event = bat_event_load($booking->booking_event_reference[LANGUAGE_NONE][0]['target_id']);
$available_state = bat_event_load_state_by_machine_name(AVAILABLE);
$event->event_state_reference[LANGUAGE_NONE][0]['state_id'] = $available_state['id'];
$event->save();
// Create a new event (i.e. find an available unit and create event).
$type = roomify_channel_connector_get_type_from_map($reservation['hotel_id'], $booking->field_bcom_room_id[LANGUAGE_NONE][0]['value'], 'booking_com');
if ($type == FALSE) {
watchdog('Channel Manager', 'Booking.com reservation @reservation_id (@roomreservation_id) cannot be saved as no matching room type exists.', array('@reservation_id' => $reservation['id'], '@roomreservation_id' => $room['roomreservation_id']), WATCHDOG_INFO);
$reply['message'] = 'Booking.com reservation ' . $reservation['id'] . ' (' . $room['roomreservation_id'] . ') cannot be saved as no matching room type exists.';
$reply['status'] = 'no_matching_room_type';
return $reply;
}
$new_event = roomify_channel_connector_create_reservation_event($room['arrival_date'], $room['departure_date'], $type);
$booking->booking_event_reference[LANGUAGE_NONE][0]['target_id'] = $new_event->event_id;
watchdog('Channel Manager', 'Booking.com reservation @reservation_id (@roomreservation_id) updated', array('@reservation_id' => $reservation['id'], '@roomreservation_id' => $room['roomreservation_id']), WATCHDOG_INFO);
$reply['message'] = 'Booking.com reservation ' . $reservation['id'] . ' (' . $room['roomreservation_id'] . ') updated';
$reply['status'] = 'updated';
}
else {
// Need to gear up to create a new booking.
// First thing is to locate the unit to place the booking in.
// Get the room type.
$type = roomify_channel_connector_get_type_from_map($reservation['hotel_id'], $room['id'], 'booking_com');
if ($type == FALSE) {
watchdog('Channel Manager', 'Booking.com reservation @reservation_id (@roomreservation_id) cannot be saved as no matching room type exists.', array('@reservation_id' => $reservation['id'], '@roomreservation_id' => $room['roomreservation_id']), WATCHDOG_INFO);
$reply['message'] = 'Booking.com reservation ' . $reservation['id'] . ' (' . $room['roomreservation_id'] . ') cannot be saved as no matching room type exists for ' . $room['id'];
$reply['status'] = 'no_matching_room_type';
return $reply;
}
// Attempt to create an event (i.e. find an available unit and create event).
$event = roomify_channel_connector_create_reservation_event($room['arrival_date'], $room['departure_date'], $type);
// If no valid units ids were found stop here.
if ($event == FALSE) {
watchdog('Channel Manager', 'Booking.com reservation @reservation_id (@roomreservation_id) cannot be saved as no available units found.', array('@reservation_id' => $reservation['id'], '@roomreservation_id' => $room['roomreservation_id']), WATCHDOG_INFO);
$reply['message'] = 'Booking.com reservation ' . $reservation['id'] . ' (' . $room['roomreservation_id'] . ') cannot be saved as no available units found.';
$reply['status'] = 'no_available_units';
$data = array(
'start_date' => $room['arrival_date'],
'end_date' => $room['departure_date'],
'type_id' => $type->type_id,
'room' => $room,
'reservation' => $reservation,
);
$booking_id = roomify_channel_connector_get_conflict_booking_id($room['arrival_date'], $room['departure_date'], $type);
roomify_channel_connector_conflict_log('booking_com', $type->field_st_property_reference[LANGUAGE_NONE][0]['target_id'], $reservation['id'], 'Booking.com reservation ' . $reservation['id'] . ' (' . $room['roomreservation_id'] . ')', $booking_id, $data);
return $reply;
}
$created_time = $reservation['date'] . ' ' . $reservation['time'];
// Owner of the booking will be the same as the owner of the type.
$booking = bat_booking_create(array(
'type' => 'booking_com',
'uid' => $event->uid,
'created' => strtotime($created_time),
));
$booking->booking_event_reference[LANGUAGE_NONE][0]['target_id'] = $event->event_id;
watchdog('Channel Manager', 'Booking.com reservation @reservation_id (@roomreservation_id) created', array('@reservation_id' => $reservation['id'], '@roomreservation_id' => $room['roomreservation_id']), WATCHDOG_INFO);
$reply['message'] = 'Booking.com reservation ' . $reservation['id'] . ' (' . $room['roomreservation_id'] . ') created';
$reply['status'] = 'created';
}
$booking->field_bcom_room_reservation_id[LANGUAGE_NONE][0]['value'] = $room['roomreservation_id'];
// Start Date for the booking.
$booking->field_bcom_arrival_date[LANGUAGE_NONE][0]['value'] = $room['arrival_date'];
$booking->booking_start_date[LANGUAGE_NONE][0]['value'] = $room['arrival_date'];
// End Date for the booking.
$booking->field_bcom_departure_date[LANGUAGE_NONE][0]['value'] = $room['departure_date'];
$booking->booking_end_date[LANGUAGE_NONE][0]['value'] = $room['departure_date'];
$price_index = 0;
foreach ($room['price'] as $price) {
$booking->field_bcom_price_data_per_date[LANGUAGE_NONE][$price_index]['value'] = drupal_json_encode($price);
$price_index++;
}
$booking->field_bcom_room_commission[LANGUAGE_NONE][0]['value'] = $room['commissionamount'];
$booking->field_bcom_room_total_price[LANGUAGE_NONE][0]['value'] = $room['totalprice'];
$booking->field_bcom_room_currency_code[LANGUAGE_NONE][0]['value'] = $room['currencycode'];
$booking->field_bcom_extra_info[LANGUAGE_NONE][0]['value'] = $room['extra_info'];
$booking->field_bcom_facilities[LANGUAGE_NONE][0]['value'] = $room['facilities'];
$booking->field_bcom_guest_name[LANGUAGE_NONE][0]['value'] = $room['guest_name'];
$booking->field_bcom_room_id[LANGUAGE_NONE][0]['value'] = $room['id'];
$booking->field_bcom_room_info[LANGUAGE_NONE][0]['value'] = $room['extra_info'];
$booking->field_bcom_max_children[LANGUAGE_NONE][0]['value'] = $room['max_children'];
$booking->field_bcom_meal_plan[LANGUAGE_NONE][0]['value'] = $room['meal_plan'];
$booking->field_bcom_room_type_name[LANGUAGE_NONE][0]['value'] = $room['name'];
$booking->field_bcom_number_of_guests[LANGUAGE_NONE][0]['value'] = $room['numberofguests'];
$booking->field_bcom_room_remarks[LANGUAGE_NONE][0]['value'] = $room['remarks'];
$booking->field_bcom_smoking[LANGUAGE_NONE][0]['value'] = $room['smoking'];
// Add the generic reservation data.
$booking->field_bcom_reservation_id[LANGUAGE_NONE][0]['value'] = $reservation['id'];
$booking->field_bcom_status[LANGUAGE_NONE][0]['value'] = $reservation['status'];
$reservation_time = $reservation['date'] . ' ' . $reservation['time'];
$booking->field_bcom_reservation_time[LANGUAGE_NONE][0]['value'] = $reservation_time;
$booking->changed = strtotime($reservation_time);
$booking->field_bcom_commission_amount[LANGUAGE_NONE][0]['value'] = $reservation['commissionamount'];
$booking->field_bcom_currency_code[LANGUAGE_NONE][0]['value'] = $reservation['currencycode'];
$booking->field_bcom_reservation_date[LANGUAGE_NONE][0]['value'] = $reservation['date'];
$booking->field_bcom_hotel_id[LANGUAGE_NONE][0]['value'] = $reservation['hotel_id'];
$booking->field_bcom_hotel_name[LANGUAGE_NONE][0]['value'] = $reservation['hotel_name'];
$booking->field_bcom_total_price[LANGUAGE_NONE][0]['value'] = $reservation['totalprice'];
// Add the customer data.
$booking->field_bcom_first_name[LANGUAGE_NONE][0]['value'] = $reservation['customer']['first_name'];
$booking->field_bcom_last_name[LANGUAGE_NONE][0]['value'] = $reservation['customer']['last_name'];
$booking->label = 'Booking.com: ' . $reservation['customer']['first_name'] . ' ' . $reservation['customer']['last_name'];
$booking->field_bcom_email[LANGUAGE_NONE][0]['value'] = $reservation['customer']['email'];
$booking->field_bcom_telephone[LANGUAGE_NONE][0]['value'] = $reservation['customer']['telephone'];
$booking->field_bcom_company[LANGUAGE_NONE][0]['value'] = $reservation['customer']['company'];
$booking->field_bcom_address[LANGUAGE_NONE][0]['value'] = $reservation['customer']['address'];
$booking->field_bcom_city[LANGUAGE_NONE][0]['value'] = $reservation['customer']['city'];
$booking->field_bcom_zip[LANGUAGE_NONE][0]['value'] = $reservation['customer']['zip'];
$booking->field_bcom_countrycode[LANGUAGE_NONE][0]['value'] = $reservation['customer']['countrycode'];
$booking->field_bcom_cc_cvc[LANGUAGE_NONE][0]['value'] = $reservation['customer']['cc_cvc'];
$booking->field_bcom_cc_expiration_date[LANGUAGE_NONE][0]['value'] = $reservation['customer']['cc_expiration_date'];
$booking->field_bcom_cc_name[LANGUAGE_NONE][0]['value'] = $reservation['customer']['cc_name'];
$booking->field_bcom_cc_number[LANGUAGE_NONE][0]['value'] = $reservation['customer']['cc_number'];
$booking->field_bcom_cc_type[LANGUAGE_NONE][0]['value'] = $reservation['customer']['cc_type'];
$booking->field_bcom_dc_issue_numebr[LANGUAGE_NONE][0]['value'] = $reservation['customer']['dc_issue_number'];
$booking->field_bcom_dc_start_date[LANGUAGE_NONE][0]['value'] = $reservation['customer']['dc_start_date'];
$booking->field_bcom_remarks[LANGUAGE_NONE][0]['value'] = $reservation['customer']['remarks'];
// Add reservation extra info data.
$booking->field_bcom_reserv_extra_info[LANGUAGE_NONE] = array();
foreach ($reservation['reservation_extra_info'] as $name => $value) {
$booking->field_bcom_reserv_extra_info[LANGUAGE_NONE][]['value'] = drupal_json_encode(array($name => $value));
}
try {
$booking->save();
$booking_wrapper = entity_metadata_wrapper('bat_booking', $booking);
$type = bat_type_load($booking_wrapper->booking_event_reference->event_bat_unit_reference->type_id->value());
$property = roomify_property_load($type->field_st_property_reference[LANGUAGE_NONE][0]['target_id']);
$reply['booking_id'] = $booking->booking_id;
$reply['property_id'] = $property->property_id;
$reply['property_name'] = $property->name;
}
catch (Exception $e) {
$reply['message'] = 'Booking.com reservation ' . $reservation['id'] . ' error: "' . $e->getMessage() . '"';
$reply['status'] = 'error';
return $reply;
}
watchdog('Channel Manager', 'Booking.com reservation @reservation_id (@roomreservation_id) saved', array('@reservation_id' => $reservation['id'], '@roomreservation_id' => $room['roomreservation_id']), WATCHDOG_INFO);
}
}
return $reply;
}
/**
* Retrieve availability information for a given date range and format appropriately.
*
* @param $property_id
* @param $room_id
* @param $rate_id
* @param $start_date
* @param $end_date
*
* @return array
*/
function roomify_channel_connector_availability($property_id, $room_id, $rate_id, $start_date, $end_date) {
$map = roomify_channel_connector_get_map_external($property_id, 'booking_com');
$roomify_room_id = 0;
if ($map != FALSE) {
// Load the map.
$map = roomify_channel_map_load($map->channel_map_id);
foreach ($map->data as $property) {
foreach ($property as $internal_rate_id => $rate_details) {
if (($rate_details['standardrate'] == $rate_id) && $rate_details['roomid'] == $room_id) {
$roomify_room_id = $internal_rate_id;
}
}
}
}
else {
return '';
}
if ($roomify_room_id == 0) {
exit;
}
// Assuming we have that then we load all the units associated with that type.
$controller = entity_get_controller('bat_unit');
// Get the units that belong to the relevant room id.
$entities = $controller->getReferencedIds($roomify_room_id);
// Create an array of unit objects - that we will pass to the calendar.
$units = array();
foreach ($entities as $entity) {
$eo = bat_unit_load($entity['id']);
$units[] = new Unit($entity['id'], $eo->getEventDefaultValue('availability'));
}
// Create an event store for availability type events.
$event_store = new DrupalDBStore('availability', DrupalDBStore::BAT_STATE);
// Provided that we have units let's check availability.
if (!empty($units)) {
$calendar = new Calendar($units, $event_store);
// Clean up end date.
$end_date = substr($end_date, 0, 10);
$interval = new \DateInterval('P1D');
$daterange = new \DatePeriod(new \DateTime($start_date), $interval, new \DateTime($end_date));
$rooms_available = array();
foreach ($daterange as $date) {
$response = $calendar->getMatchingUnits($date, $date, array(1));
$rooms_available[$date->format('Y-m-d')] = count($response->getIncluded());
}
// With the units in place we can create the properly formatted message to sent to booking.com.
$message = array();
$i = -1;
$old_count = 0;
$start = TRUE;
foreach ($rooms_available as $day => $rooms) {
if ($old_count != $rooms) {
$start_date = $day;
$i++;
}
// Need to add one day to the end date because Booking.com expects the guest departure date.
$to_date = new \DateTime($day);
$to_date = $to_date->add(new \DateInterval('P1D'))->format('Y-m-d');
$message[$i] = array(
'room_id' => $room_id,
'rate_id' => $rate_id,
'date_range' => array(
'from' => $start_date,
'to' => $to_date,
),
'roomstosell' => $rooms,
);
$old_count = $rooms;
}
}
// Formatting the message based on what the channel connector expects.
$final_message = array(
'hotel_id' => $property_id,
'rooms' => $message,
);
return $final_message;
}
/**
* Retrieve availability information for a given date range and format appropriately.
*
* @param $property_id
* @param $type_id
* @param $start_date
* @param $end_date
*
* @return array
*/
function roomify_channel_type_availability($property_id, $type_id, $start_date, $end_date) {
// Load all the units associated with that type.
$controller = entity_get_controller('bat_unit');
// Get the units that belong to the relevant room id.
$entities = $controller->getReferencedIds($type_id);
// Create an array of unit objects - that we will pass to the calendar.
$units = array();
foreach ($entities as $entity) {
$eo = bat_unit_load($entity['id']);
$units[] = new Unit($entity['id'], $eo->getEventDefaultValue('availability'));
}
// Create an event store for availability type events.
$event_store = new DrupalDBStore('availability', DrupalDBStore::BAT_STATE);
$message = array();
// Provided that we have units let's check availability.
if (!empty($units)) {
$calendar = new Calendar($units, $event_store);
// Clean up end date.
$end_date = substr($end_date, 0, 10);
$interval = new \DateInterval('P1D');
$daterange = new \DatePeriod(new \DateTime($start_date), $interval, new \DateTime($end_date));
$rooms_available = array();
foreach ($daterange as $date) {
$response = $calendar->getMatchingUnits($date, $date, array(1));
$rooms_available[$date->format('Y-m-d')] = count($response->getIncluded());
}
// With the units in place we can create the properly formatted message.
$i = -1;
$old_count = 0;
$start = TRUE;
foreach ($rooms_available as $day => $rooms) {
if ($old_count != $rooms) {
$start_date = $day;
$i++;
}
// Add one day to the end date.
$to_date = new \DateTime($day);
$to_date = $to_date->add(new \DateInterval('P1D'))->format('Y-m-d');
$message[$i] = array(
'room_id' => NULL,
'rate_id' => NULL,
'date_range' => array(
'from' => $start_date,
'to' => $to_date,
),
'roomstosell' => $rooms,
);
$old_count = $rooms;
}
}
// Formatting the message based on what the channel connector expects.
$final_message = array(
'hotel_id' => $property_id,
'rooms' => $message,
);
return $final_message;
}
/**
* Retrieve pricing information for a given date range and format appropriately.
*
* @param $hotel_id
* @param $room_id
* @param $rate_id
* @param $start_date
* @param $end_date
*
* @return array
*/
function roomify_channel_connector_rates($property_id, $room_id, $rate_id, $start_date, $end_date) {
$roomify_rate_id = NULL;
$map = roomify_channel_connector_get_map_external($property_id, 'booking_com');
if ($map != FALSE) {
// Load the map.
$map = roomify_channel_map_load($map->channel_map_id);
foreach ($map->data as $property) {
foreach ($property as $internal_rate_id => $rate_details) {