Skip to content

Commit

Permalink
fix: make compatible with php 8.2 (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
joerivanveen authored Mar 20, 2024
1 parent dde4546 commit ce6b67d
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 119 deletions.
2 changes: 1 addition & 1 deletion install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-->
<modification>
<name>MyParcel NL</name>
<version>1.1.8</version>
<version>1.1.17</version>
<code>MyParcelNL</code>
<author>MyParcel NL</author>
<link>https://www.myparcel.nl/</link>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
// Heading
$heading_title = 'Myparcel NL v1.1.10';
$heading_title = 'Myparcel NL v1.1.17';
$_['heading_title'] = $heading_title;
// Text
$_['text_module'] = 'Modules';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
// Heading
$heading_title = 'Myparcel NL v1.1.10';
$heading_title = 'Myparcel NL v1.1.17';
$_['heading_title'] = $heading_title;
// Text
$_['text_module'] = 'Modules';
Expand Down
2 changes: 0 additions & 2 deletions upload/system/library/myparcelnl/assets/js/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,6 @@ var MYPARCEL_SHIPMENT = MYPARCEL_SHIPMENT || {};
}

$("#button-print-batch")[0].click();

$('input[name="selected[]"]').prop('checked', false)
}

} else{
Expand Down
6 changes: 6 additions & 0 deletions upload/system/library/myparcelnl/class_myparcel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@ class MyParcel
public $log;
public $settings;
public $lang;
public $shipment;
public $url;

const PACKAGE_TYPE_STANDARD = 1;
const PACKAGE_TYPE_MAILBOX = 2;
const PACKAGE_TYPE_LETTER = 3;
const PACKAGE_TYPE_DIGITAL_STAMP = 4;

const COUNTRY_CODE_NL = 'NL';
const COUNTRY_CODE_BE = 'BE';

/**
* MyParcel constructor.
* @param null $registry
Expand Down
121 changes: 65 additions & 56 deletions upload/system/library/myparcelnl/includes/class_myparcel_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ public function addShipments ( $shipment_data, $type = 'standard' )

$endpoint = 'shipments';
// define content type
switch ($type) {
case 'standard': default:
$content_type = 'application/vnd.shipment+json';
$data_key = 'shipments';
break;
case 'return':
$content_type = 'application/vnd.return_shipment+json';
$data_key = 'return_shipments';
break;
case 'unrelated_return':
$content_type = 'application/vnd.unrelated_return_shipment+json';
$data_key = 'unrelated_return_shipments';
break;
}
switch ($type) {
case 'standard': default:
$content_type = 'application/vnd.shipment+json;charset=utf-8;version=1.1';
$data_key = 'shipments';
break;
case 'return':
$content_type = 'application/vnd.return_shipment+json; charset=utf-8; version=1.1';
$data_key = 'return_shipments';
break;
case 'unrelated_return':
$content_type = 'application/vnd.unrelated_return_shipment+json; charset=utf-8; version=1.1';
$data_key = 'unrelated_return_shipments';
break;
}

$data = array(
'data' => array (
Expand Down Expand Up @@ -166,55 +166,64 @@ public function getShipmentLabels ( $ids, $params = array(), $return = 'pdf' )
return $response;
}

/**
* Track shipments
* @param array $ids shipment ids
* @param array $params request parameters
* @return array response
*/
public function getTracktraces ( $ids, $params = array() )
{
$endpoint = 'tracktraces';

$headers = array (
'Authorization: basic '. base64_encode("{$this->key}"),
);
/**
* Track shipments
* @param array $ids shipment ids
* @param array $params request parameters
* @return array response
*/
public function getTracktraces ( $ids, $params = array() )
{
$endpoint = 'tracktraces';

$request_url = MyParcel()->helper->add_query_arg( $params, $this->api_domain . $endpoint . '/' . implode(';', $ids) );
$response = $this->get($request_url, $headers, false);
$headers = array (
'Authorization: basic '. base64_encode("{$this->key}"),
);

return $response;
}
$request_url = MyParcel()->helper->add_query_arg( $params, $this->api_domain . $endpoint . '/' . implode(';', $ids) );
$response = $this->sendRequest($request_url, 'GET', null, $headers);
return $response;
}

public function getTracktraceUrl( $order_id, $tracktrace )
{
if (empty($order_id))
return;
$registry = MyParcel::$registry;
public function getTracktraceUrl( $order_id, $tracktrace )
{
if (empty($order_id))
return;
$registry = MyParcel::$registry;

$loader = $registry->get('load');
$loader->model(MyParcel()->getModelPath('shipment'));
$model_shipment = $registry->get('model_extension_myparcelnl_shipment');
$loader->model(MyParcel()->getModelPath('shipment'));
$model_shipment = $registry->get('model_extension_myparcelnl_shipment');

$order_info = MyParcel()->helper->getShippingOrder($order_id);
$country = $order_info['shipping_iso_code_2'];
$postcode = $order_info['shipping_postcode'];
// set url for NL or foreign orders
$shipment_helper = MyParcel()->shipment->shipment_helper;
$is_pickup = $shipment_helper->isPickup( $order_id );
if ($country == 'NL') {
// use billing postcode for pickup/pakjegemak
if ( $is_pickup ) {
$postcode = $order_info['payment_postcode'];;
}
// $tracktrace_url = sprintf('https://mijnpakket.postnl.nl/Inbox/Search?lang=nl&B=%s&P=%s', $tracktrace, $postcode);
$tracktrace_url = sprintf('https://mijnpakket.postnl.nl/Claim?Barcode=%s&Postalcode=%s', $tracktrace, $postcode);
} else {
$tracktrace_url = sprintf('https://www.internationalparceltracking.com/Main.aspx#/track/%s/%s/%s', $tracktrace, $country, $postcode);
}
$shipment_data = $model_shipment->getSavedMyParcelShipments($order_id);
$shipment_data = array_shift($shipment_data);
$response = $this->getTracktraces([$shipment_data['shipment_id']]);

return $tracktrace_url;
}
if ($response['code'] === 200) {
$tracktrace = array_shift($response['body']['data']['tracktraces']);
return $tracktrace['link_consumer_portal'];
}

$order_info = MyParcel()->helper->getShippingOrder($order_id);
$country = $order_info['shipping_iso_code_2'];
$postcode = $order_info['shipping_postcode'];
// set url for NL or foreign orders
$shipment_helper = MyParcel()->shipment->shipment_helper;
$is_pickup = $shipment_helper->isPickup( $order_id );
if ($country == 'NL') {
// use billing postcode for pickup/pakjegemak
if ( $is_pickup ) {
$postcode = $order_info['payment_postcode'];;
}
// $tracktrace_url = sprintf('https://mijnpakket.postnl.nl/Inbox/Search?lang=nl&B=%s&P=%s', $tracktrace, $postcode);
// $tracktrace_url = sprintf('https://mijnpakket.postnl.nl/Claim?Barcode=%s&Postalcode=%s', $tracktrace, $postcode);
$tracktrace_url = sprintf('https://jouw.postnl.nl/track-and-trace/%s-NL-%s?language=nl', $tracktrace, $postcode);
} else {
$tracktrace_url = sprintf('https://www.internationalparceltracking.com/Main.aspx#/track/%s/%s/%s', $tracktrace, $country, $postcode);
}

return $tracktrace_url;
}

public function getTracktraceLinks ( $order_id )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ public function sendRequest($url, $method = 'GET', $post_data = null, $headers =

$this->setOption(CURLOPT_URL, $url);

if ((ini_get('open_basedir') == '') AND (!ini_get('safe_mode'))) {
$this->setOption(CURLOPT_FOLLOWLOCATION, true);
}
// only in use for php 5.4 and lower so no need updated by Vanest ict

// if ((ini_get('open_basedir') == '') && (!ini_get('safe_mode'))) {
// $this->setOption(CURLOPT_FOLLOWLOCATION, true);
// }

switch ($method) {
case "PUT":
Expand Down Expand Up @@ -88,12 +90,12 @@ public function sendRequest($url, $method = 'GET', $post_data = null, $headers =

$this->_curl = curl_init();

if (!is_resource($this->_curl) || !isset($this->_curl)) {
if (!($this->_curl instanceof CurlHandle) && (!is_resource($this->_curl) || !isset($this->_curl))) {
throw new Exception("Unable to create cURL session");
}

$result_setopt = curl_setopt_array($this->_curl, $this->getOptions());
if ($result_setopt !== TRUE) {
if ($result_setopt !== true) {
throw new Exception(curl_error($this->_curl));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ function add($params, $process = false)
$this->errors[] = 'The maximum weight of a shipment to a rest of world country cannot be heavier than 20 kg';
continue;
}
$saved_extra_settings = $model_shipment->getSavedExtraExportSettings($order_id);
$number_of_copies = (!empty($saved_extra_settings['number_of_copies']) ? $saved_extra_settings['number_of_copies'] : 1);
if($number_of_copies > 1 && $shipment_helper->checkMultiCollo($shipment_data_single['recipient']['cc'],$shipment_data_single['options']['package_type'])){
$secondary_shipments = array ();

for ($i = 0; $i < $number_of_copies - 1; $i++){
$secondary_shipments[] = array (
'reference_identifier' => $shipment_data_single['reference_identifier'],
);
}
$shipment_data_single['secondary_shipments'] = $secondary_shipments;
}
$shipment_data[] = $shipment_data_single;
}
}
Expand All @@ -68,19 +80,13 @@ function add($params, $process = false)
// And receive shipment_id from the API
$responses = array();
if (!empty($shipment_data)) {
$saved_extra_settings = $model_shipment->getSavedExtraExportSettings($order_id);
$number_of_copies = (!empty($saved_extra_settings['number_of_copies']) ? $saved_extra_settings['number_of_copies'] : 1);

for ($i = 1; $i <= intval($number_of_copies); $i++) {
try {
MyParcel()->log->add("Prepared data:\n".var_export($shipment_data, true));
$response = $api->addShipments($shipment_data);
$responses[] = $response;
MyParcel()->log->add("API response (order {$order_id}):\n" . var_export($response, true));
} catch (Exception $e) {
$this->errors[] = sprintf(MyParcel()->lang->get('entry_api_error_with_order_id'), $order_id) . $e->getMessage();
}

try {
MyParcel()->log->add("Prepared data:\n".var_export($shipment_data, true));
$response = $api->addShipments($shipment_data);
$responses[] = $response;
MyParcel()->log->add("API response (order {$order_id}):\n" . var_export($response, true));
} catch (Exception $e) {
$this->errors[] = sprintf(MyParcel()->lang->get('entry_api_error_with_order_id'), $order_id) . $e->getMessage();
}
}

Expand Down Expand Up @@ -127,11 +133,15 @@ function add($params, $process = false)
$order_status_data = $model_order_status->getOrderStatus((int)MyParcel()->settings->general->automatic_order_status);
if (!empty($order_status_data)) {
if (version_compare(VERSION, '2.0.0.0', '>=')) {
$order_info = $model_model_sale_order->getOrder($order_id);
$model_helper->addDeliveryDataIntoOrder($order_info);
$shipment_helper->updateOrderStatus($order_id,(int)MyParcel()->settings->general->automatic_order_status, MyParcel()->lang->get('mssg_order_status_changed_by_myparcel'));
$new_order_status_name = !empty($order_status_data['name']) ? $order_status_data['name'] : null;

/** @var MyParcel_Api $api * */
$api = MyParcel()->api;
$response = $api->getLocalRequest('extension/myparcelnl/myparcel_order/updateorderstatus', array('order_id' => $order_id));
if (empty($response['body']['success'])) {
$this->errors[] = MyParcel()->lang->get('entry_update_order_status_error') . ' - Order #' . $order_id;
MyParcel()->log->add('Update status error - Order #' . $order_id);
} else {
$new_order_status_name = !empty($order_status_data['name']) ? $order_status_data['name'] : null;
}
} else {
$data = array(
'order_status_id' => MyParcel()->settings->general->automatic_order_status,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,26 +438,6 @@ function getDeliveryPrices($price_format = true, $color_format = true, $prefix =
);
}

function getTaxFromCart(){
$registry = MyParcel::$registry;
/** @var \Cart\Cart $cart **/
$cart = $registry->get('cart');

/** @var Cart\Tax $tax **/
$tax = $cart->tax;
if (!$tax || !$cart) {
return '';
}

foreach ($cart->getProducts() as $product) {
if ($product['tax_class_id']) {
return $product['tax_class_id'];
}
}
return '';

}

/**
* Converts price string to float value, assuming no thousand-separators used
* @param float $price
Expand All @@ -480,23 +460,13 @@ function convertPriceToFloat( $price )
function getTotalDeliveryTaxAmountFromCart($delivery_fee, $cart)
{
/** @var Cart\Tax $tax **/
$tax = $cart->tax;
$taxes = $cart->getTaxes();
$total = $delivery_fee;

if (!$tax || !$cart) {
if (!$taxes || !$cart) {
return $total;
}

$taxes = array();
foreach ($cart->getProducts() as $product) {
if ($product['tax_class_id']) {
// Check if tax_class_id takes affect to current shipping address
if (!in_array($product['tax_class_id'], $taxes)) {
$taxes[] = $product['tax_class_id'];
}
}
}

if (empty($taxes)) {
return $delivery_fee;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function getOrderShipmentData($order_id)


$shipment = array(
'reference_identifier' => 'Order_' . $order_id,
'recipient' => $this->getRecipient( $order_data ),
'options' => $this->getOptions( $order_data , false, true ),
'carrier' => 1, // default to POSTNL for now
Expand Down Expand Up @@ -78,13 +79,13 @@ public function getOrderShipmentData($order_id)
if(isset($product_info['myparcel_country']) && $product_info['myparcel_country'] != ''){
$country = $product_info['myparcel_country'];
}else{
if(isset($settings['default_country_code'])) $country = $settings['default_country_code'];
if(isset($settings['default_country_origin'])) $country = $settings['default_country_origin'];
}

$weightItem = (isset($product_info['weight']) && is_numeric($product_info['weight'])) ? $product_info['weight'] : $weightDefault;
$weightItem = $class_weight->convert($weightItem, $product_info['weight_class_id'], 2); // always convert to gram

$priceItem = (isset($product_info['price']) && is_numeric($product_info['price'])) ? $product_info['price'] : 1000;
$priceItem = (isset($product_info['price']) && is_numeric($product_info['price'])) ? $product_info['price'] * 100 : 1000;

$shipment['customs_declaration']['items'][] = array(
'description' => $order_product['name'], //product name
Expand All @@ -105,6 +106,9 @@ public function getOrderShipmentData($order_id)
$shipment['physical_properties'] = array(
'weight' => (int) $totalWeight
);
$shipment['options'] = [
"package_type" => 1
];
}
if($shipment['options'] == null){
return null;
Expand Down Expand Up @@ -161,6 +165,7 @@ public function getRecipient( $order )
'number' => $house_number,
'number_suffix' => $number_addition,
'postal_code' => $order['shipping_postcode'],
'street_additional_info' => strlen($order['shipping_address_2']) >= 6 ? $order['shipping_address_2'] : ""
);
}
$address = array_merge($address, $address_intl);
Expand Down Expand Up @@ -1227,4 +1232,11 @@ function updateOrderStatus($order_id, $order_status_id, $comment = '', $notify =
$db->query("INSERT INTO " . DB_PREFIX . "order_history SET order_id = '" . (int)$order_id . "', order_status_id = '" . (int)$order_status_id . "', notify = '" . (int)$notify . "', comment = '" . $db->escape($comment) . "', date_added = NOW()");

}

public function checkMultiCollo ($order_country_code, $package_type) {
return in_array(
$order_country_code,
[MyParcel::COUNTRY_CODE_NL, MyParcel::COUNTRY_CODE_BE]
) && $package_type == MyParcel::PACKAGE_TYPE_STANDARD;
}
}
Loading

0 comments on commit ce6b67d

Please sign in to comment.