-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchannelmanagement_rentalsunited_import_prices.class.php
194 lines (152 loc) · 7 KB
/
channelmanagement_rentalsunited_import_prices.class.php
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
<?php
/**
* Jomres CMS Agnostic Plugin
* @author Woollyinwales IT <sales@jomres.net>
* @version Jomres 9
* @package Jomres
* @copyright 2019 Woollyinwales IT
* Jomres (tm) PHP files are released under both MIT and GPL2 licenses. This means that you can choose the license that best suits your project.
**/
// ################################################################
defined( '_JOMRES_INITCHECK' ) or die( '' );
// ################################################################
class channelmanagement_rentalsunited_import_prices
{
public static function import_prices( $manager_id , $channel , $remote_property_id = 0 , $property_uid = 0 , $sleeps = 0 , $room_type_id = 0 )
{
if ( (int)$remote_property_id == 0 ) {
throw new Exception( jr_gettext('CHANNELMANAGEMENT_RENTALSUNITED_IMPORT_PROPERTYID_NOTSET','CHANNELMANAGEMENT_RENTALSUNITED_IMPORT_PROPERTYID_NOTSET',false) );
}
if ( (int)$property_uid == 0 ) {
throw new Exception( "Property uid is not set " );
}
if ( (int)$sleeps == 0 ) {
throw new Exception( "Number of persons property sleeps is not set " );
}
if ( (int)$room_type_id == 0 ) {
throw new Exception( "Room type id is not set " );
}
jr_import('channelmanagement_rentalsunited_communication');
$channelmanagement_rentalsunited_communication = new channelmanagement_rentalsunited_communication();
set_showtime("property_managers_id" , $manager_id );
$auth = get_auth();
$output = array(
"AUTHENTICATION" => $auth,
"PROPERTY_ID" => $remote_property_id,
"DATE_FROM" => date ( "Y-m-d" , strtotime("now") ),
"DATE_TO" => date ( "Y-m-d" , strtotime(" +1 year ") )
);
$tmpl = new patTemplate();
$tmpl->addRows('pageoutput', array($output));
$tmpl->setRoot(RENTALS_UNITED_PLUGIN_ROOT . 'templates' . JRDS . "xml");
$tmpl->readTemplatesFromInput('Pull_ListPropertyPrices_RQ.xml');
$xml_str = $tmpl->getParsedTemplate();
$remote_prices = $channelmanagement_rentalsunited_communication->communicate( 'Pull_ListPropertyPrices_RQ' , $xml_str );
$tmpl = new patTemplate();
$tmpl->addRows('pageoutput', array($output));
$tmpl->setRoot(RENTALS_UNITED_PLUGIN_ROOT . 'templates' . JRDS . "xml");
$tmpl->readTemplatesFromInput('Pull_ListPropertyAvailabilityCalendar_RQ.xml');
$xml_str = $tmpl->getParsedTemplate();
$remote_availability = $channelmanagement_rentalsunited_communication->communicate( 'Pull_ListPropertyAvailabilityCalendar_RQ' , $xml_str );
$atts = '@attributes';
$CalDays = array();
if ( isset($remote_availability['PropertyCalendar']['CalDay']) ) {
foreach ($remote_availability['PropertyCalendar']['CalDay'] as $CalDay ) {
$date = strtotime(str_replace ( "-" , "/" , $CalDay[$atts]['Date']));
$CalDays[$date] = array ( "MinStay" => $CalDay['MinStay']);
}
}
$primary_price_set = array();
$extra_price_set = array();
if ( !empty($remote_prices['Prices']['Season'])) {
foreach ($remote_prices['Prices']['Season'] as $season ) {
if ( !isset($season[$atts]) ) {
if (isset($season['DateFrom'])){
$season[$atts]['DateFrom'] = $season['DateFrom'];
$season['Price'] = $remote_prices['Prices']['Season']['Price'];
$season['Extra'] = $remote_prices['Prices']['Season']['Extra'];
} else {
throw new Exception( "DateFrom in season not set" );
}
if (isset($season['DateTo'])){
$season[$atts]['DateTo'] = $season['DateTo'];
} else {
throw new Exception( "DateTo in season not set" );
}
}
$from_date = str_replace ( "-" , "/" , $season[$atts]['DateFrom']) ;
$to_date = str_replace ( "-" , "/" , $season[$atts]['DateTo']) ;
if (!DateTime::createFromFormat('Y/m/d', $from_date )) {
throw new Exception( "DateFrom not valid" );
}
if (!DateTime::createFromFormat('Y/m/d', $to_date )) {
throw new Exception( "DateTo not valid" );
}
$date_range_array = findDateRangeForDates( $from_date , $to_date);
if (isset($season['Price'])) {
$dates_and_prices = array();
$price_per_person = $season['Price']/2;
$price_per_person_extra = ($season['Price']/2)+$season['Extra'];
foreach ($date_range_array as $date ) {
$stt_date = strtotime($date);
if (isset($CalDays[$stt_date])) {
$minDays = $CalDays[$stt_date]['MinStay'];
} else {
$minDays = 1;
}
$primary_price_set[$stt_date] = array ( "price" => $price_per_person , "mindays" =>$minDays , "minpeople" => 1 , "maxpeople" => 2 ) ;
if (isset($season['Extra'])) { // We'll create an extra tariff for min-max 3 people too
$extra_price_set[$stt_date] = array ( "price" => $price_per_person_extra , "mindays" =>$minDays , "minpeople" => 3 , "maxpeople" => $sleeps ) ;
}
}
}
$basic_post_data = array (
"property_uid" => $property_uid ,
"tarifftypeid" => 0 , // Create a new micromanage tariff
"rate_title" => "Tariff" ,
"rate_description" => "Tariff description" ,
"maxdays" => 364 ,
"roomclass_uid" => $room_type_id ,
"dayofweek" => 7 , // Every day
"ignore_pppn" => 0 , // Ignore per person per night flag in property config set to No.
"allow_we" => 1 , // Allow bookings to span weekends
"weekendonly" => 0 , // Bookings for this tariff only allowed if all days in the booking are on the weekend = No
"minrooms_alreadyselected" => 0 , // Specialised setting, do not change unless you understand the consequences
"maxrooms_alreadyselected" => 1000 , // Specialised setting, do not change unless you understand the consequences
);
$channelmanagement_framework_singleton = jomres_singleton_abstract::getInstance('channelmanagement_framework_singleton');
if (!empty($primary_price_set)) {
$post_data = $basic_post_data;
$counter = 0;
foreach ($primary_price_set as $key => $vals ) {
$post_data["tariffinput"][$key] = $vals['price'];
$post_data["mindaysinput"][$key] = $vals['mindays'];
$post_data['minpeople'] = $vals['minpeople'];
$post_data['maxpeople'] = $vals['maxpeople'];
$counter++;
if ($counter == 365 ) {
break;
}
}
$primary_tariff_response = $channelmanagement_framework_singleton->rest_api_communicate( $channel , 'PUT' , 'cmf/property/tariff/' , $post_data );
}
if (!empty($extra_price_set)) {
$post_data = $basic_post_data;
$counter = 0;
foreach ($extra_price_set as $key => $vals ) {
$post_data["tariffinput"][$key] = $vals['price'];
$post_data["mindaysinput"][$key] = $vals['mindays'];
$post_data['minpeople'] = $vals['minpeople'];
$post_data['maxpeople'] = $vals['maxpeople'];
$counter++;
if ($counter == 10 ) {
break;
}
}
$secondary_tariff_response = $channelmanagement_framework_singleton->rest_api_communicate( $channel , 'PUT' , 'cmf/property/tariff/' , $post_data );
}
}
}
return true;
}
}