| 1 |
<?php |
| 2 |
|
| 3 |
class Mbe_Shipping_Helper_Rates |
| 4 |
{ |
| 5 |
protected $_rate_table_name; |
| 6 |
protected $helper; |
| 7 |
|
| 8 |
|
| 9 |
public function __construct() |
| 10 |
{ |
| 11 |
$this->helper = new Mbe_Shipping_Helper_Data(); |
| 12 |
$this->_rate_table_name = $this->helper->getShipmentCsvTable(); |
| 13 |
} |
| 14 |
|
| 15 |
public function uninstallRatesTable() |
| 16 |
{ |
| 17 |
global $wpdb; |
| 18 |
// $sql = "DROP TABLE IF EXISTS `" . $this->_rate_table_name . "`"; |
| 19 |
return $wpdb->query($wpdb->prepare("DROP TABLE IF EXISTS %i", $this->_rate_table_name)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 20 |
} |
| 21 |
|
| 22 |
public function truncate() |
| 23 |
{ |
| 24 |
global $wpdb; |
| 25 |
// $truncateSql = " TRUNCATE `" . $this->_rate_table_name . "` "; |
| 26 |
return $wpdb->query($wpdb->prepare("TRUNCATE %i", $this->_rate_table_name)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 27 |
} |
| 28 |
|
| 29 |
public function insertRate($country, $region, $city, $zip, $zipTo, $weightFrom, $weightTo, $price, $deliveryType) |
| 30 |
{ |
| 31 |
global $wpdb; |
| 32 |
|
| 33 |
$city = esc_sql($city); |
| 34 |
$region = esc_sql($region); |
| 35 |
$country = esc_sql($country); |
| 36 |
$zip = esc_sql($zip); |
| 37 |
$zipTo = esc_sql($zipTo); |
| 38 |
$weightFrom = esc_sql($weightFrom); |
| 39 |
$weightTo = esc_sql($weightTo); |
| 40 |
$price = esc_sql($price); |
| 41 |
$deliveryType = esc_sql($deliveryType); |
| 42 |
|
| 43 |
// $sql = " |
| 44 |
// INSERT INTO `" . $this->_rate_table_name . "` ( |
| 45 |
// `country`,`region`,`city`,`zip`,`zip_to`,`weight_from`,`weight_to`,`price`,`delivery_type` |
| 46 |
// ) |
| 47 |
// VALUES ( |
| 48 |
// '" . $country . "', |
| 49 |
// '" . $region . "', |
| 50 |
// '" . $city . "', |
| 51 |
// '" . $zip . "', |
| 52 |
// '" . $zipTo . "', |
| 53 |
// " . $weightFrom . ", |
| 54 |
// " . $weightTo . ", |
| 55 |
// " . $price . ", |
| 56 |
// '" . $deliveryType . "' |
| 57 |
// ); |
| 58 |
// "; |
| 59 |
|
| 60 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 61 |
return $wpdb->query($wpdb->prepare( |
| 62 |
"INSERT INTO %i ( `country`,`region`,`city`,`zip`,`zip_to`,`weight_from`,`weight_to`,`price`,`delivery_type` ) |
| 63 |
VALUES (%s, %s, %s, %s, %s, %d, %d, %d, %s) ", |
| 64 |
$this->_rate_table_name, |
| 65 |
$country, |
| 66 |
$region, |
| 67 |
$city, |
| 68 |
$zip, |
| 69 |
$zipTo, |
| 70 |
$weightFrom, |
| 71 |
$weightTo, |
| 72 |
$price, |
| 73 |
$deliveryType |
| 74 |
)); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
public function useCustomRates($country) |
| 79 |
{ |
| 80 |
global $wpdb; |
| 81 |
$country = esc_sql($country); |
| 82 |
|
| 83 |
$helper = new Mbe_Shipping_Helper_Data(); |
| 84 |
|
| 85 |
if ($helper->getShipmentsCsvMode() == Mbe_Shipping_Helper_Data::MBE_CSV_MODE_DISABLED) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
if ($helper->getShipmentsCsvMode() == Mbe_Shipping_Helper_Data::MBE_CSV_MODE_TOTAL) { |
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
if ($helper->getShipmentsCsvMode() == Mbe_Shipping_Helper_Data::MBE_CSV_MODE_PARTIAL) { |
| 94 |
// $sql = "SELECT * FROM `" . $this->_rate_table_name . "` WHERE `country` = '" . $country . "'"; |
| 95 |
$rates = $wpdb->get_results($wpdb->prepare("SELECT * FROM %i WHERE country = %s", $this->_rate_table_name, $country),"ARRAY_A"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 96 |
if (is_array($rates) && count($rates) > 0) { |
| 97 |
return true; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
public function applyInsuranceToRate( $rate, $insuranceValue ) { |
| 105 |
$result = $rate; |
| 106 |
|
| 107 |
$helper = new Mbe_Shipping_Helper_Data(); |
| 108 |
|
| 109 |
$percentageValue = $helper->getShipmentsCsvInsurancePercentage() / 100 * (float) $insuranceValue; |
| 110 |
$fixedValue = $helper->getShipmentsCsvInsuranceMin(); |
| 111 |
|
| 112 |
// Apply the insurance cost based on the service type |
| 113 |
switch ( $helper->getSelectedInsuranceCode() ) { |
| 114 |
case Mbe_Shipping_Helper_Data::MBE_SHIPPING_WITH_SAFE_VALUE_CODE_SUFFIX: |
| 115 |
case Mbe_Shipping_Helper_Data::MBE_SHIPPING_WITH_SAFE_VALUE_ART_CODE_SUFFIX: |
| 116 |
case Mbe_Shipping_Helper_Data::MBE_SHIPPING_WITH_SAFE_VALUE_4B_CODE_SUFFIX: |
| 117 |
$result += $percentageValue; |
| 118 |
break; |
| 119 |
default: |
| 120 |
if ( $percentageValue < $fixedValue ) { |
| 121 |
$result += $fixedValue; |
| 122 |
} else { |
| 123 |
$result += $percentageValue; |
| 124 |
} |
| 125 |
break; |
| 126 |
} |
| 127 |
|
| 128 |
return $result; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
public function getCustomRates($country, $region, $city, $postCode, $weight, $insuranceValue) |
| 133 |
{ |
| 134 |
global $wpdb; |
| 135 |
$result = array(); |
| 136 |
$newdata = array(); |
| 137 |
|
| 138 |
$postCode = esc_sql($postCode); |
| 139 |
$city = esc_sql($city); |
| 140 |
$region = esc_sql($region); |
| 141 |
$country = esc_sql($country); |
| 142 |
$zipSql = " '" . $postCode . "' BETWEEN zip AND zip_to"; |
| 143 |
|
| 144 |
$helper = new Mbe_Shipping_Helper_Data(); |
| 145 |
$weight = $helper->convertWeight($weight, 'kg'); |
| 146 |
$services = $helper->getAllowedShipmentServicesArray(); |
| 147 |
|
| 148 |
foreach ($services as $service) { |
| 149 |
for ($j = 0; $j <= 7; $j++) { |
| 150 |
|
| 151 |
$sql = "SELECT * FROM `" . $this->_rate_table_name . "` "; |
| 152 |
|
| 153 |
switch ($j) { |
| 154 |
case 0: |
| 155 |
$sql .= "WHERE "; |
| 156 |
$sql .= "`country` = '" . $country . "'"; |
| 157 |
$sql .= " AND "; |
| 158 |
$sql .= "`region` = '" . $region . "'"; |
| 159 |
$sql .= " AND "; |
| 160 |
$sql .= " STRCMP(LOWER(city),LOWER('" . $city . "')) = 0"; |
| 161 |
$sql .= " AND "; |
| 162 |
$sql .= $zipSql; |
| 163 |
|
| 164 |
break; |
| 165 |
|
| 166 |
case 1: |
| 167 |
$sql .= "WHERE "; |
| 168 |
$sql .= "`country` = '" . $country . "'"; |
| 169 |
$sql .= " AND "; |
| 170 |
$sql .= "`region` = '" . $region . "'"; |
| 171 |
$sql .= " AND "; |
| 172 |
$sql .= "`city` = ''"; |
| 173 |
$sql .= " AND "; |
| 174 |
$sql .= $zipSql; |
| 175 |
|
| 176 |
break; |
| 177 |
case 2: |
| 178 |
$sql .= "WHERE "; |
| 179 |
$sql .= "`country` = '" . $country . "'"; |
| 180 |
$sql .= " AND "; |
| 181 |
$sql .= "`region` = '" . $region . "'"; |
| 182 |
$sql .= " AND "; |
| 183 |
$sql .= "STRCMP(LOWER(city),LOWER('" . $city . "')) = 0"; |
| 184 |
$sql .= " AND "; |
| 185 |
$sql .= " zip = '' "; |
| 186 |
|
| 187 |
break; |
| 188 |
case 3: |
| 189 |
$sql .= "WHERE "; |
| 190 |
$sql .= "`country` = '" . $country . "'"; |
| 191 |
$sql .= " AND "; |
| 192 |
//$sql .= "`region` = '" . $region . "'"; |
| 193 |
$sql .= "`region` = ''"; |
| 194 |
$sql .= " AND "; |
| 195 |
$sql .= "STRCMP(LOWER(city),LOWER('" . $city . "')) = 0"; |
| 196 |
$sql .= " AND "; |
| 197 |
$sql .= $zipSql; |
| 198 |
|
| 199 |
break; |
| 200 |
case 4: |
| 201 |
$sql .= "WHERE "; |
| 202 |
$sql .= "`country` = '" . $country . "'"; |
| 203 |
$sql .= " AND "; |
| 204 |
//$sql .= "`region` = '" . $region . "'"; |
| 205 |
$sql .= "`region` = ''"; |
| 206 |
$sql .= " AND "; |
| 207 |
$sql .= "STRCMP(LOWER(city),LOWER('" . $city . "')) = 0"; |
| 208 |
$sql .= " AND "; |
| 209 |
$sql .= "zip = ''"; |
| 210 |
|
| 211 |
break; |
| 212 |
case 5: |
| 213 |
$sql .= "WHERE "; |
| 214 |
$sql .= "`country` = '" . $country . "'"; |
| 215 |
$sql .= " AND "; |
| 216 |
//$sql .= "`region` = '" . $region . "'"; |
| 217 |
$sql .= "`region` = ''"; |
| 218 |
$sql .= " AND "; |
| 219 |
$sql .= "city = ''"; |
| 220 |
$sql .= " AND "; |
| 221 |
$sql .= $zipSql; |
| 222 |
|
| 223 |
break; |
| 224 |
case 6: |
| 225 |
$sql .= "WHERE "; |
| 226 |
$sql .= "`country` = '" . $country . "'"; |
| 227 |
$sql .= " AND "; |
| 228 |
$sql .= "`region` = '" . $region . "'"; |
| 229 |
$sql .= " AND "; |
| 230 |
$sql .= "city = ''"; |
| 231 |
$sql .= " AND "; |
| 232 |
$sql .= "zip = ''"; |
| 233 |
break; |
| 234 |
case 7: |
| 235 |
$sql .= "WHERE "; |
| 236 |
$sql .= "`country` = '" . $country . "'"; |
| 237 |
$sql .= " AND "; |
| 238 |
$sql .= "`region` = ''"; |
| 239 |
$sql .= " AND "; |
| 240 |
$sql .= "city = ''"; |
| 241 |
$sql .= " AND "; |
| 242 |
$sql .= "zip = ''"; |
| 243 |
break; |
| 244 |
|
| 245 |
} |
| 246 |
$sql .= " AND weight_from <= " . $weight . " AND weight_to >=" . $weight; |
| 247 |
$sql .= " AND delivery_type = '" . $service . "'"; |
| 248 |
|
| 249 |
$sql .= " ORDER BY country DESC, region DESC, zip DESC"; |
| 250 |
|
| 251 |
|
| 252 |
$rows = $wpdb->get_results($sql, 'ARRAY_A'); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 253 |
|
| 254 |
|
| 255 |
if (!empty($rows)) { |
| 256 |
// have found a result or found nothing and at end of list! |
| 257 |
foreach ($rows as $data) { |
| 258 |
$newdata[$data["delivery_type"]] = $data; |
| 259 |
} |
| 260 |
break; |
| 261 |
} |
| 262 |
|
| 263 |
} |
| 264 |
} |
| 265 |
$resultWithoutInsurance = []; |
| 266 |
$resultWithInsurance = []; |
| 267 |
$resultDeliveryPoint = []; |
| 268 |
|
| 269 |
$ws = new Mbe_Shipping_Model_Ws(); |
| 270 |
$selectedInsuranceCode = $this->helper->getSelectedInsuranceCode(); |
| 271 |
$selectedInsuranceLabel = $this->helper->getSelectedInsuranceLabel(); |
| 272 |
foreach ($newdata as $data) { |
| 273 |
$rate = new \stdClass; |
| 274 |
$rate->Service = $data["delivery_type"]; |
| 275 |
$rate->ServiceDesc = $ws->getLabelFromShipmentType($data["delivery_type"]); |
| 276 |
$rate->SubzoneDesc = ''; |
| 277 |
$rate->IdSubzone = ''; |
| 278 |
|
| 279 |
$rate->NetShipmentTotalPrice = $data["price"]; |
| 280 |
|
| 281 |
// Extract all the delivery point CourierService codes |
| 282 |
if(in_array($data["delivery_type"], MBE_ESTIMATE_DELIVERY_POINT_SERVICES)) { |
| 283 |
$customerData = $ws->getCustomer(); |
| 284 |
$enabledServices = explode(',', $customerData->Permissions->enabledServices); |
| 285 |
$enabledCourierServices = explode(',',$customerData->Permissions->enabledCourierServices); |
| 286 |
foreach ($enabledServices as $key=>$value ) { |
| 287 |
if($data["delivery_type"] === $value) { |
| 288 |
$netRate = clone $rate; |
| 289 |
$netRate->CourierService = $enabledCourierServices[$key]; |
| 290 |
$resultDeliveryPoint[] = $netRate; |
| 291 |
} |
| 292 |
} |
| 293 |
} else { |
| 294 |
$resultWithoutInsurance[] = $rate; |
| 295 |
// If the insurance service is the old _insurance the getShipmentsCsvInsuranceMin field is used as minimum insurance cost as before |
| 296 |
// if the service is a new "safe value" one, the getShipmentsCsvInsuranceMin is used as a threshold for the service |
| 297 |
|
| 298 |
if( |
| 299 |
$selectedInsuranceCode === Mbe_Shipping_Helper_Data::MBE_SHIPPING_WITH_INSURANCE_CODE_SUFFIX || |
| 300 |
( |
| 301 |
$helper->getShipmentsCsvInsuranceMin() <= $insuranceValue && ( |
| 302 |
$selectedInsuranceCode === Mbe_Shipping_Helper_Data::MBE_SHIPPING_WITH_SAFE_VALUE_CODE_SUFFIX || |
| 303 |
$selectedInsuranceCode === Mbe_Shipping_Helper_Data::MBE_SHIPPING_WITH_SAFE_VALUE_4B_CODE_SUFFIX || |
| 304 |
$selectedInsuranceCode === Mbe_Shipping_Helper_Data::MBE_SHIPPING_WITH_SAFE_VALUE_ART_CODE_SUFFIX |
| 305 |
) |
| 306 |
) |
| 307 |
) { |
| 308 |
//rate with insurance |
| 309 |
$rateWithInsurance = new \stdClass; |
| 310 |
$rateWithInsurance->Service = $data["delivery_type"] . $selectedInsuranceCode; |
| 311 |
$rateWithInsurance->ServiceDesc = $ws->getLabelFromShipmentType( $data["delivery_type"] ) . $selectedInsuranceLabel;; |
| 312 |
$rateWithInsurance->SubzoneDesc = ''; |
| 313 |
$rateWithInsurance->IdSubzone = ''; |
| 314 |
|
| 315 |
$rateWithInsurance->NetShipmentTotalPrice = $this->applyInsuranceToRate( $data["price"], $insuranceValue ); |
| 316 |
$resultWithInsurance[] = $rateWithInsurance; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
} |
| 322 |
$result = array_merge($resultWithInsurance, $resultWithoutInsurance); |
| 323 |
|
| 324 |
// Add common delivery point service |
| 325 |
if(is_array($resultDeliveryPoint)) { |
| 326 |
$result[] = $this->helper->getDeliveryPointServices($resultDeliveryPoint); |
| 327 |
$result = array_values(array_filter($result)); //Clean empty array items and reindex (remove null deliverypointservices if any) |
| 328 |
} |
| 329 |
|
| 330 |
return $result; |
| 331 |
} |
| 332 |
|
| 333 |
} |