1315 lines
46 KiB
PHP
1315 lines
46 KiB
PHP
<?php
|
|
|
|
use WPDesk\DHL\HelperTrait;
|
|
use WPDesk\DHL\SanitizeTrait;
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
} // Exit if accessed directly
|
|
|
|
require_once( 'class-can-manage-weight.php' );
|
|
|
|
if ( class_exists( 'WPDesk_Flexible_Shipping_Shipment' ) ) {
|
|
|
|
class WPDesk_Flexible_Shipping_Shipment_dhl extends WPDesk_Flexible_Shipping_Shipment implements WPDesk_Flexible_Shipping_Shipment_Interface {
|
|
use WPDesk_Can_Manage_DHL_Weight;
|
|
use SanitizeTrait;
|
|
use HelperTrait;
|
|
|
|
private $dhl_one_time_message;
|
|
|
|
private $shipment_date;
|
|
|
|
const DHL_SELECTED_PARCELSHOP_ID_AND_NAME_META_KEY = '_dhl_parcelshop_selected_parcel_name';
|
|
|
|
const DELIVERY_FIELD_ID = 'delivery_point_id';
|
|
const DELIVERY_FIELD_NAME = 'delivery_point_name';
|
|
const DELIVERY_FIELD_ADDRESS = 'delivery_point_address';
|
|
const DELIVERY_FIELD_POSTCODE = 'delivery_point_postcode';
|
|
const DELIVERY_FIELD_CITY = 'delivery_point_city';
|
|
|
|
/**
|
|
* WooCommerce_DHL_Plugin
|
|
*
|
|
* @var null
|
|
*/
|
|
static $_plugin;
|
|
|
|
private $plugin;
|
|
|
|
private function get_shipment_date() {
|
|
|
|
if ( $this->shipment_date === null ) {
|
|
$this->shipment_date = $this->prepare_shipment_date();
|
|
}
|
|
|
|
return $this->shipment_date;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
private function prepare_shipment_date() {
|
|
$settings = $this->get_shipping_method_settings();
|
|
|
|
$api = $this->get_webapi();
|
|
|
|
$time = current_time( 'timestamp' );
|
|
|
|
for ( $i = 0; $i < 6; $i ++ ) {
|
|
$shipment_time = $time + DAY_IN_SECONDS * $i;
|
|
$shipment_date = date( 'Y-m-d', $shipment_time );
|
|
$week_day = date( 'D', $shipment_time );
|
|
if ( $week_day !== 'Sun' ) {
|
|
$current_hour = date( 'G', $shipment_time );
|
|
$skip = false;
|
|
if ( isset( $settings['permanent_collection'] ) && $settings['permanent_collection'] === 'yes' ) {
|
|
if ( isset( $settings['date_change_hour'] ) ) {
|
|
if ( (int) $settings['date_change_hour'] > (int) $current_hour ) {
|
|
$skip = true;
|
|
}
|
|
}
|
|
}
|
|
if ( $i === 0 ) {
|
|
if ( $skip ) {
|
|
break;
|
|
}
|
|
} else {
|
|
try {
|
|
$shipment_date_postal_code = $api->get_postal_code_services( trim( str_replace( '-', '', $settings['sender_postal_code'] ) ), $shipment_date );
|
|
if ( $shipment_date_postal_code->getPostalCodeServicesResult->drPickupFrom !== 'brak' && $shipment_date_postal_code->getPostalCodeServicesResult->drPickupTo !== 'brak' ) {
|
|
break;
|
|
}
|
|
} catch ( Exception $e ) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $shipment_date;
|
|
}
|
|
|
|
public function __construct( $shipment, WC_Order $order = null ) {
|
|
parent::__construct( $shipment, $order );
|
|
|
|
if ( self::$_plugin !== null ) {
|
|
$this->plugin = self::$_plugin;
|
|
}
|
|
}
|
|
|
|
public function checkout( array $fs_method, $package ) {
|
|
$settings = $this->get_shipping_method_settings();
|
|
$order = $this->get_order();
|
|
$this->set_meta( '_dhl_product', $fs_method['dhl_product'] );
|
|
$this->set_meta( '_dhl_package_type', $fs_method['dhl_package_type'] );
|
|
$this->set_meta( '_dhl_insurance', $fs_method['dhl_insurance'] );
|
|
$this->set_meta( '_dhl_insurance_value', $order->get_total() );
|
|
$this->set_meta( '_dhl_cod', $fs_method['dhl_cod'] );
|
|
$this->set_meta( '_dhl_services', $fs_method['dhl_services'] ?? [] );
|
|
$this->set_meta( '_dhl_package_nonstandard', $fs_method['dhl_package_nonstandard'] ?? 0 );
|
|
if ( 'PL' === $order->get_shipping_country() ) {
|
|
$this->set_meta( '_dhl_cod_value', $order->get_total() );
|
|
}
|
|
$this->set_meta( '_dhl_package_width', $settings['package_width'] );
|
|
$this->set_meta( '_dhl_package_length', $settings['package_length'] );
|
|
$this->set_meta( '_dhl_package_height', $settings['package_height'] );
|
|
$weight = $this->convert_wc_weight_to_dhl( fs_calculate_package_weight( $package ) );
|
|
|
|
if ( $weight === 0.0 ) {
|
|
$weight = $settings['package_weight'];
|
|
}
|
|
|
|
$this->set_meta( '_dhl_package_weight', $weight );
|
|
$this->set_meta( '_dhl_content', $settings['package_contents'] );
|
|
$dhl_comment = $settings['package_comment'];
|
|
$dhl_comment = str_replace( '[order_number]', $order->get_order_number(), $dhl_comment );
|
|
$dhl_comment = str_replace( '[shop_name]', get_bloginfo( 'name' ), $dhl_comment );
|
|
$dhl_comment = str_replace( '[shop_url]', home_url(), $dhl_comment );
|
|
$this->set_meta( '_dhl_comment', $dhl_comment );
|
|
if ( wpdesk_get_order_meta( $order, '_payment_method', true ) === 'cod' ) {
|
|
$this->set_meta( '_dhl_cod_value', $order->get_total() );
|
|
$this->set_meta( '_dhl_cod', 1 );
|
|
}
|
|
|
|
if ( isset( $_POST['dhl_parcelshop'] ) ) {
|
|
$this->set_meta( '_dhl_parcelshop', $_POST['dhl_parcelshop'] );
|
|
}
|
|
|
|
$this->save_parcelshop_into_metadata();
|
|
|
|
try {
|
|
$this->save_pickup_details();
|
|
} catch ( Exception $e ) {
|
|
// Do nothing.
|
|
}
|
|
|
|
// Save payment method as debugging data.
|
|
$this->set_meta( '_checkout_payment_method', $order->get_payment_method() );
|
|
}
|
|
|
|
/**
|
|
* Save user selected parcelshop id and name into metadata
|
|
*/
|
|
private function save_parcelshop_into_metadata() {
|
|
$dhl_parcelshop_selected_parcel = WC()->session->get( WPDesk_WooCommerce_DHL::DHL_SELECTED_PARCELSHOP_ID_AND_NAME );
|
|
|
|
if ( isset( $dhl_parcelshop_selected_parcel ) ) {
|
|
$this->set_meta( self::DHL_SELECTED_PARCELSHOP_ID_AND_NAME_META_KEY, $dhl_parcelshop_selected_parcel );
|
|
}
|
|
}
|
|
|
|
public function order_metabox_content() {
|
|
|
|
if ( ! function_exists( 'woocommerce_form_field' ) ) {
|
|
$wc_template_functions = trailingslashit( __DIR__ ) . '../../woocommerce/includes/wc-template-functions.php';
|
|
if ( file_exists( $wc_template_functions ) ) {
|
|
include_once( $wc_template_functions );
|
|
}
|
|
}
|
|
|
|
/** @var WC_Order $order */
|
|
$order = $this->get_order();
|
|
|
|
$settings = $this->get_shipping_method_settings();
|
|
$id = $this->get_id();
|
|
|
|
$dhl_status = $this->get_meta( '_dhl_status' );
|
|
|
|
$disabled = false;
|
|
if ( isset( $dhl_status ) && $dhl_status === 'ok' ) {
|
|
$disabled = true;
|
|
}
|
|
|
|
$dhl_product = $this->get_meta( '_dhl_product' );
|
|
$dhl_parcelshop = $this->get_meta( '_dhl_parcelshop' );
|
|
$dhl_package_type = $this->get_meta( '_dhl_package_type' );
|
|
$dhl_package_weight = $this->get_meta( '_dhl_package_weight' );
|
|
$dhl_package_width = $this->get_meta( '_dhl_package_width' );
|
|
$dhl_package_length = $this->get_meta( '_dhl_package_length' );
|
|
$dhl_package_height = $this->get_meta( '_dhl_package_height' );
|
|
$dhl_package_nonstandard = $this->get_meta( '_dhl_package_nonstandard' );
|
|
$additional_packages = $this->get_meta( '_additional_packages', [] );
|
|
$dhl_shipment_date = $this->get_meta( '_dhl_shipment_date' );
|
|
$dhl_insurance = $this->get_meta( '_dhl_insurance' );
|
|
$dhl_insurance_value = $this->get_meta( '_dhl_insurance_value' );
|
|
$dhl_services = $this->get_meta( '_dhl_services', [] );
|
|
|
|
$dhl_cod = $this->get_meta( '_dhl_cod' );
|
|
$dhl_cod_value = $this->get_meta( '_dhl_cod_value' );
|
|
|
|
$dhl_content = $this->get_meta( '_dhl_content' );
|
|
$dhl_comment = $this->get_meta( '_dhl_comment' );
|
|
|
|
$shipment_date_options = [];
|
|
$shipment_date_options_ex = [];
|
|
|
|
$shipment_date_default = false;
|
|
$shipment_date_default_ex = false;
|
|
|
|
$additional_packages = $this->get_meta( '_additional_packages', [] );
|
|
|
|
$dhl_one_time_message = $this->get_meta( '_dhl_one_time_message', '' );
|
|
|
|
$dhl_message = $this->get_meta( '_dhl_message' );
|
|
|
|
$tracking_url = $this->get_tracking_url();
|
|
|
|
$dhl_package_number = $this->get_meta( '_dhl_package_number' );
|
|
|
|
$label_url = $this->get_label_url();
|
|
|
|
$label_avaliable = $this->label_avaliable();
|
|
|
|
if ( ! $disabled ) {
|
|
$order = $this->get_order();
|
|
$shipment_date_options = $this->dhl_shipment_date_options( $settings );
|
|
} else {
|
|
$shipment_date_options[ $this->get_meta( '_dhl_shipment_date' ) ] = $this->get_meta( '_dhl_shipment_date' );
|
|
}
|
|
|
|
|
|
$shipment_date_default = $this->shipment_date_default( $shipment_date_options );
|
|
|
|
$dhl_one_time_message = $this->dhl_one_time_message;
|
|
|
|
$parcelshop_api = $this->get_api();
|
|
$radius = $this->get_radius();
|
|
|
|
$all_servicepoints = [ '', __( 'Wybierz punkt odbioru', 'woocommerce-dhl' ) ];
|
|
|
|
$user_selected_service_point = [];
|
|
if ( $parcelshop_api ) {
|
|
try {
|
|
$all_items = $parcelshop_api->get_nearest_servicepoints( $this->get_shipping_postcode( $order ), $radius, $this->get_bool_value( $dhl_cod ), $this->get_shipping_country( $order ), $this->get_shipping_city( $order ) );
|
|
foreach ( $all_items as $item ) {
|
|
$all_servicepoints[ $this->prepare_servicepont_id( $item, $this->get_shipping_country( $order ) ) ] = $item->name . ', '
|
|
. $item->address->city . ' '
|
|
. substr( $item->address->postcode, 0, 2 ) . '-' . substr( $item->address->postcode, 2 ) . ', '
|
|
. $item->address->street . ' '
|
|
. $item->address->houseNumber
|
|
. ( ! empty( $item->address->apartmentNumber ) ? '/' . $item->address->apartmentNumber : '' );
|
|
}
|
|
} catch ( Exception $e ) {
|
|
$all_servicepoints[''] = $e->getMessage();
|
|
}
|
|
}
|
|
|
|
$valid_service_point = $this->check_if_service_point_is_available_in_order_data( $dhl_parcelshop, $all_servicepoints );
|
|
|
|
$user_selected_service_point = $this->get_user_selected_service_point_id_and_name();
|
|
|
|
ob_start();
|
|
|
|
$echo = false;
|
|
|
|
include( 'views/order-metabox-content.php' );
|
|
|
|
$content = ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
return $content;
|
|
}
|
|
|
|
/**
|
|
* @param stdClass $item
|
|
* @param string $country
|
|
*
|
|
* @return string
|
|
*/
|
|
private function prepare_servicepont_id( stdClass $item, $country ) {
|
|
if ( 'PL' === $country ) {
|
|
|
|
return $item->sap;
|
|
}
|
|
|
|
return $item->sap . ':' . $item->address->postcode;
|
|
}
|
|
|
|
/**
|
|
* Available shipment date options for order
|
|
*
|
|
* @param array $settings
|
|
*
|
|
* @return array Shipment date options [ '2019-12-09' => '2019-12-09', '2019-12-10' => '2019-12-10' ... ]
|
|
*/
|
|
private function dhl_shipment_date_options( array $settings ) {
|
|
$shipment_date_options = array();
|
|
$time = current_time( 'timestamp' );
|
|
$api = $this->get_webapi();
|
|
|
|
for ( $i = - 1; $i < 5; $i ++ ) {
|
|
$shipment_date = date( 'Y-m-d', $time + DAY_IN_SECONDS * $i );
|
|
$week_day = date( 'D', strtotime( $shipment_date ) );
|
|
if ( $week_day !== 'Sun' ) {
|
|
$current_hour = date( 'G', $time + DAY_IN_SECONDS * $i );
|
|
$skip = false;
|
|
if ( isset( $settings['permanent_collection'] ) && $settings['permanent_collection'] === 'yes' ) {
|
|
if ( isset( $settings['date_change_hour'] ) ) {
|
|
if ( (int) $settings['date_change_hour'] > (int) $current_hour ) {
|
|
$skip = true;
|
|
}
|
|
}
|
|
}
|
|
if ( $i === 0 && $skip ) {
|
|
$shipment_date_options[ $shipment_date ] = $shipment_date;
|
|
} else {
|
|
try {
|
|
if ( isset( $settings['sender_postal_code'] ) ) {
|
|
$sender_postal_code = $settings['sender_postal_code'];
|
|
} else {
|
|
$sender_postal_code = '';
|
|
}
|
|
$shipment_date_postal_code = $api->get_postal_code_services( trim( str_replace( '-', '', $sender_postal_code ) ), $shipment_date );
|
|
if ( $shipment_date_postal_code->getPostalCodeServicesResult->drPickupFrom !== 'brak' && $shipment_date_postal_code->getPostalCodeServicesResult->drPickupTo !== 'brak' ) {
|
|
$shipment_date_options[ $shipment_date ] = $shipment_date;
|
|
}
|
|
} catch ( Exception $e ) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $this->create_shipment_date_options( $time, $shipment_date_options );
|
|
}
|
|
|
|
/**
|
|
* Create shipment date options for order
|
|
*
|
|
* @param int $time
|
|
* @param array $shipment_date_options
|
|
*
|
|
* @return array [ '2019-12-09' => '2019-12-09', '2019-12-10' => '2019-12-10' ... ]
|
|
*/
|
|
private function create_shipment_date_options( $time, array $shipment_date_options ) {
|
|
if ( count( $shipment_date_options ) === 0 ) {
|
|
for ( $i = 0; $i < 5; $i ++ ) {
|
|
$shipment_date = date( 'Y-m-d', $time + DAY_IN_SECONDS * $i );
|
|
$shipment_date_options[ $shipment_date ] = $shipment_date;
|
|
}
|
|
}
|
|
|
|
return $shipment_date_options;
|
|
}
|
|
|
|
/**
|
|
* @param array $shipment_date_options
|
|
*
|
|
* @return string default date for shipment '2019-12-09'
|
|
*/
|
|
private function shipment_date_default( array $shipment_date_options ) {
|
|
$shipment_date_default = '';
|
|
if ( ! empty( $shipment_date_options ) ) {
|
|
foreach ( $shipment_date_options as $shipment_date ) {
|
|
$week_day = date( 'D', strtotime( $shipment_date ) );
|
|
if ( $week_day !== 'Sat' ) {
|
|
$shipment_date_default = $shipment_date;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $shipment_date_default;
|
|
}
|
|
|
|
/**
|
|
* Get User selected service point id and name from meta data
|
|
*
|
|
* @return array id => name of user selected parcelshop [ 400123 => Test shop, Smith Street 12, 12-345 ]
|
|
*/
|
|
private function get_user_selected_service_point_id_and_name() {
|
|
$dhl_parcelshop_selected_parcel = $this->get_meta( self::DHL_SELECTED_PARCELSHOP_ID_AND_NAME_META_KEY );
|
|
$user_selected_service_point = [];
|
|
if ( ! empty( $dhl_parcelshop_selected_parcel ) ) {
|
|
$user_selected_service_point = [
|
|
$dhl_parcelshop_selected_parcel['id'] => $dhl_parcelshop_selected_parcel['name']
|
|
];
|
|
}
|
|
|
|
return $user_selected_service_point;
|
|
}
|
|
|
|
/**
|
|
* @param int $parcel_shop_code
|
|
* @param array $available_services
|
|
*
|
|
* @return bool
|
|
*/
|
|
private function check_if_service_point_is_available_in_order_data( $parcel_shop_code, $available_services ) {
|
|
return array_key_exists( $parcel_shop_code, $available_services );
|
|
}
|
|
|
|
public function order_metabox() {
|
|
echo $this->order_metabox_content();
|
|
}
|
|
|
|
public function get_order_metabox_title() {
|
|
return __( 'DHL', 'woocommerce-dhl' );
|
|
}
|
|
|
|
public function ajax_request( $action, $data ) {
|
|
$ret = array();
|
|
if ( $action === 'save' ) {
|
|
$this->save_ajax_data( $data );
|
|
$this->save();
|
|
$ret['message'] = __( 'Przesyłka została zapisana.', 'woocommerce-dhl' );
|
|
} else if ( $action === 'send' ) {
|
|
$this->save_ajax_data( $data );
|
|
$this->api_create();
|
|
$ret['message'] = __( 'Przesyłka została utworzona.', 'woocommerce-dhl' );
|
|
} else if ( $action === 'cancel' ) {
|
|
$dhl_package_number = $this->get_meta( '_dhl_package_number' );
|
|
$this->save_ajax_data( $data );
|
|
$this->api_cancel();
|
|
$ret['message'] = __( 'Przesyłka została anulowana.', 'woocommerce-dhl' );
|
|
} else if ( $action === 'refresh' ) {
|
|
$this->api_refresh();
|
|
} else {
|
|
throw new Exception( __( 'Nieznana akcja: "' . $action . '"', 'woocommerce-dhl' ) );
|
|
}
|
|
$ret['content'] = $this->order_metabox_content();
|
|
|
|
return $ret;
|
|
}
|
|
|
|
public function api_refresh() {
|
|
}
|
|
|
|
|
|
public function save_ajax_data( $data ) {
|
|
|
|
if ( isset( $data['dhl_product'] ) ) {
|
|
$this->set_meta( '_dhl_product', $data['dhl_product'] );
|
|
} else {
|
|
$this->delete_meta( '_dhl_product' );
|
|
}
|
|
if ( $this->is_parcelshop_shipment() ) {
|
|
if ( isset( $data['dhl_parcelshop'] ) ) {
|
|
$this->set_meta( '_dhl_parcelshop', $data['dhl_parcelshop'] );
|
|
$this->save_pickup_details();
|
|
}
|
|
} else {
|
|
$this->delete_meta( '_dhl_parcelshop' );
|
|
|
|
$this->delete_meta( self::DELIVERY_FIELD_ID );
|
|
$this->delete_meta( self::DELIVERY_FIELD_NAME );
|
|
$this->delete_meta( self::DELIVERY_FIELD_ADDRESS );
|
|
$this->delete_meta( self::DELIVERY_FIELD_POSTCODE );
|
|
$this->delete_meta( self::DELIVERY_FIELD_CITY );
|
|
}
|
|
if ( isset( $data['dhl_package_type'] ) ) {
|
|
$this->set_meta( '_dhl_package_type', $data['dhl_package_type'] );
|
|
} else {
|
|
$this->delete_meta( '_dhl_package_type' );
|
|
}
|
|
if ( isset( $data['dhl_package_weight'] ) ) {
|
|
$this->set_meta( '_dhl_package_weight', $data['dhl_package_weight'] );
|
|
} else {
|
|
$this->delete_meta( '_dhl_package_weight' );
|
|
}
|
|
if ( isset( $data['dhl_package_width'] ) ) {
|
|
$this->set_meta( '_dhl_package_width', $data['dhl_package_width'] );
|
|
} else {
|
|
$this->delete_meta( '_dhl_package_width' );
|
|
}
|
|
if ( isset( $data['dhl_package_length'] ) ) {
|
|
$this->set_meta( '_dhl_package_length', $data['dhl_package_length'] );
|
|
} else {
|
|
$this->delete_meta( '_dhl_package_length' );
|
|
}
|
|
if ( isset( $data['dhl_package_height'] ) ) {
|
|
$this->set_meta( '_dhl_package_height', $data['dhl_package_height'] );
|
|
} else {
|
|
$this->delete_meta( '_dhl_package_height' );
|
|
}
|
|
|
|
if ( isset( $data['dhl_package_nonstandard'] ) ) {
|
|
$this->set_meta( '_dhl_package_nonstandard', $data['dhl_package_nonstandard'] );
|
|
} else {
|
|
$this->delete_meta( '_dhl_package_nonstandard' );
|
|
}
|
|
if ( isset( $data['additional_packages'] ) ) {
|
|
unset( $data['additional_packages']['_additional_key_'] );
|
|
$additional_packages = array();
|
|
foreach ( $data['additional_packages'] as $additional_package ) {
|
|
$additional_packages[] = $additional_package;
|
|
}
|
|
$this->set_meta( '_additional_packages', $additional_packages );
|
|
} else {
|
|
$this->delete_meta( '_additional_packages' );
|
|
}
|
|
if ( isset( $data['dhl_shipment_date'] ) ) {
|
|
$this->set_meta( '_dhl_shipment_date', $data['dhl_shipment_date'] );
|
|
} else {
|
|
$this->delete_meta( '_dhl_shipment_date' );
|
|
}
|
|
if ( isset( $data['dhl_insurance'] ) ) {
|
|
$this->set_meta( '_dhl_insurance', $data['dhl_insurance'] );
|
|
} else {
|
|
$this->delete_meta( '_dhl_insurance' );
|
|
}
|
|
if ( isset( $data['dhl_insurance_value'] ) ) {
|
|
$this->set_meta( '_dhl_insurance_value', $data['dhl_insurance_value'] );
|
|
} else {
|
|
$this->delete_meta( '_dhl_insurance_value' );
|
|
}
|
|
if ( isset( $data['dhl_cod'] ) ) {
|
|
$this->set_meta( '_dhl_cod', $data['dhl_cod'] );
|
|
} else {
|
|
$this->delete_meta( '_dhl_cod' );
|
|
}
|
|
if ( isset( $data['dhl_cod_value'] ) ) {
|
|
$this->set_meta( '_dhl_cod_value', $data['dhl_cod_value'] );
|
|
} else {
|
|
$this->delete_meta( '_dhl_cod_value' );
|
|
}
|
|
if ( isset( $data['dhl_services'] ) ) {
|
|
$this->set_meta( '_dhl_services', $data['dhl_services'] );
|
|
} else {
|
|
$this->delete_meta( '_dhl_services' );
|
|
}
|
|
if ( isset( $data['dhl_content'] ) ) {
|
|
$this->set_meta( '_dhl_content', $data['dhl_content'] );
|
|
} else {
|
|
$this->delete_meta( '_dhl_content' );
|
|
}
|
|
if ( isset( $data['dhl_comment'] ) ) {
|
|
$this->set_meta( '_dhl_comment', $data['dhl_comment'] );
|
|
} else {
|
|
$this->delete_meta( '_dhl_comment' );
|
|
}
|
|
}
|
|
|
|
public function get_error_message() {
|
|
return $this->get_meta( '_dhl_message' );
|
|
}
|
|
|
|
public function get_tracking_number() {
|
|
return $this->get_meta( '_dhl_package_number' );
|
|
}
|
|
|
|
/**
|
|
* Get tracking URL.
|
|
*
|
|
* @return null|string
|
|
*/
|
|
public function get_tracking_url() {
|
|
if ( '' !== $this->get_meta( '_dhl_package_number', '' ) ) {
|
|
return 'https://sprawdz.dhl.com.pl/szukaj.aspx?m=0&sn=' . $this->get_meta( '_dhl_package_number' );
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function api_create() {
|
|
/** @var WC_Order $order */
|
|
$order = $this->get_order();
|
|
|
|
if ( $this->is_parcelshop_shipment() ) {
|
|
if ( $this->is_shipping_country_poland() ) {
|
|
$this->api_create_parcelshop();
|
|
} else {
|
|
$this->api_create_webapi( true );
|
|
}
|
|
} else {
|
|
$this->api_create_webapi();
|
|
}
|
|
$this->save();
|
|
$error_message = $this->get_error_message();
|
|
if ( ! empty( $error_message ) ) {
|
|
throw new Exception( $error_message );
|
|
}
|
|
|
|
$order->add_order_note( sprintf( __( 'Do zamówienia została utworzona przesyłka %s DHL.', 'woocommerce-dhl' ), $this->get_meta( '_dhl_package_number' ) ), true );
|
|
}
|
|
|
|
/**
|
|
* @param bool $parcelshop
|
|
*
|
|
* @throws Exception
|
|
*/
|
|
public function api_create_webapi( $parcelshop = false ) {
|
|
if ( $this->get_status() !== 'fs-new' && $this->get_status() !== 'fs-failed' ) {
|
|
throw new Exception( sprintf( __( 'Invalid status: %s. Package already created?', 'woocommerce-dhl' ), $this->get_status() ) );
|
|
}
|
|
|
|
$settings = $this->get_shipping_method_settings();
|
|
|
|
$shipment = array();
|
|
|
|
$shipment['shipper'] = $this->get_shipper( $settings );
|
|
|
|
$shipment['receiver'] = $this->get_receiver( $this->get_order(), $parcelshop );
|
|
|
|
$shipment['pieceList'] = $this->get_piece_list();
|
|
|
|
$shipment['service'] = $this->get_service();
|
|
|
|
$shipment['payment'] = $this->get_payment( $settings );
|
|
|
|
$shipment['ship']['servicePointAccountNumber'] = $this->get_meta( '_dhl_parcelshop' );
|
|
|
|
if ( $this->get_meta( '_dhl_shipment_date', '' ) !== '' ) {
|
|
$shipment['shipmentDate'] = $this->get_meta( '_dhl_shipment_date' );
|
|
} else {
|
|
$shipment['shipmentDate'] = $this->get_shipment_date();
|
|
$this->set_meta( '_dhl_shipment_date', $shipment['shipmentDate'] );
|
|
}
|
|
|
|
$shipment['content'] = $this->get_meta( '_dhl_content' );
|
|
|
|
if ( $this->get_meta( '_dhl_comment', '' ) !== '' ) {
|
|
$shipment['comment'] = $this->get_meta( '_dhl_comment' );
|
|
}
|
|
|
|
$shipment['skipRestrictionCheck'] = true;
|
|
$api = $this->get_webapi();
|
|
try {
|
|
$dhl_package = $api->create_shipment( array( $shipment ) );
|
|
$this->set_meta( '_dhl_status', 'ok' );
|
|
$this->set_meta( '_dhl_package', $dhl_package );
|
|
$this->set_meta( '_dhl_package_number', $dhl_package->createShipmentsResult->item->shipmentId );
|
|
$this->delete_meta( '_dhl_message' );
|
|
$this->update_status( 'fs-confirmed' );
|
|
$this->save();
|
|
do_action( 'flexible_shipping_shipment_confirmed', $this );
|
|
} catch ( Exception $e ) {
|
|
$this->set_meta( '_dhl_status', 'error' );
|
|
$this->set_meta( '_dhl_message', $e->getMessage() );
|
|
$this->update_status( 'fs-failed' );
|
|
$this->save();
|
|
throw $e;
|
|
}
|
|
|
|
}
|
|
|
|
public function api_create_parcelshop() {
|
|
if ( $this->get_status() !== 'fs-new' && $this->get_status() !== 'fs-failed' ) {
|
|
throw new Exception( sprintf( __( 'Invalid status: %s. Package already created?', 'woocommerce-dhl' ), $this->get_status() ) );
|
|
}
|
|
|
|
$settings = $this->get_shipping_method_settings();
|
|
|
|
$shipment = array();
|
|
|
|
$shipment['pieceList'] = $this->get_piece_list();
|
|
|
|
$shipment['ship'] = array();
|
|
|
|
$shipment['ship']['shipper'] = array();
|
|
|
|
$shipment['ship']['shipper']['address'] = $this->get_shipper_address( $settings );
|
|
$shipment['ship']['shipper']['contact'] = $this->get_shipper_contact( $settings );
|
|
$shipment['ship']['shipper']['preaviso'] = $this->get_shipper_preaviso( $settings );
|
|
|
|
$shipment['ship']['receiver'] = array();
|
|
|
|
$shipment['ship']['receiver']['address'] = $this->get_receiver_address( $this->get_order() );
|
|
$shipment['ship']['receiver']['contact'] = $this->get_receiver_contact( $this->get_order() );
|
|
$shipment['ship']['receiver']['preaviso'] = $this->get_receiver_preaviso( $this->get_order() );
|
|
|
|
$shipment['ship']['servicePointAccountNumber'] = $this->get_meta( '_dhl_parcelshop' );
|
|
|
|
$shipment['shipmentInfo'] = array();
|
|
$shipment['shipmentInfo']['dropOffType'] = 'REGULAR_PICKUP';
|
|
$shipment['shipmentInfo']['serviceType'] = 'LM';
|
|
|
|
$shipment['shipmentInfo']['billing'] = array();
|
|
$shipment['shipmentInfo']['billing']['shippingPaymentType'] = 'SHIPPER';
|
|
$shipment['shipmentInfo']['billing']['billingAccountNumber'] = $settings['account_number'];
|
|
$shipment['shipmentInfo']['billing']['paymentType'] = 'BANK_TRANSFER';
|
|
|
|
$shipment['shipmentInfo']['specialServices'] = array();
|
|
if ( (int) $this->get_meta( '_dhl_insurance', '0' ) === 1 ) {
|
|
$shipment['shipmentInfo']['specialServices'][] = array(
|
|
'serviceType' => 'UBEZP',
|
|
'serviceValue' => $this->get_meta( '_dhl_insurance_value' )
|
|
);
|
|
}
|
|
if ( (int) $this->get_meta( '_dhl_cod', '0' ) === 1 ) {
|
|
$shipment['shipmentInfo']['specialServices'][] = array(
|
|
'serviceType' => 'COD',
|
|
'serviceValue' => $this->get_meta( '_dhl_cod_value' ),
|
|
'collectOnDeliveryForm' => 'BANK_TRANSFER'
|
|
);
|
|
}
|
|
|
|
if ( $this->get_meta( '_dhl_shipment_date', '' ) !== '' ) {
|
|
$shipment['shipmentInfo']['shipmentDate'] = $this->get_meta( '_dhl_shipment_date' );
|
|
} else {
|
|
$shipment['shipmentInfo']['shipmentDate'] = $this->get_shipment_date();
|
|
$this->set_meta( '_dhl_shipment_date', $shipment['shipmentInfo']['shipmentDate'] );
|
|
}
|
|
|
|
$shipment['content'] = $this->get_meta( '_dhl_content' );
|
|
|
|
if ( $this->get_meta( '_dhl_comment', '' ) !== '' ) {
|
|
$shipment['comment'] = $this->get_meta( '_dhl_comment' );
|
|
}
|
|
$shipment['shipmentInfo']['labelType'] = 'BLP';
|
|
|
|
$api = $this->get_api();
|
|
|
|
try {
|
|
$dhl_package = $api->create_shipment( $shipment );
|
|
$this->set_meta( '_dhl_status', 'ok' );
|
|
$this->set_meta( '_dhl_package', $dhl_package );
|
|
$this->set_meta( '_dhl_package_number', $dhl_package->createShipmentResult->shipmentNumber );
|
|
$this->delete_meta( '_dhl_message' );
|
|
$this->update_status( 'fs-confirmed' );
|
|
$this->save();
|
|
do_action( 'flexible_shipping_shipment_confirmed', $this );
|
|
} catch ( Exception $e ) {
|
|
$this->set_meta( '_dhl_status', 'error' );
|
|
$this->set_meta( '_dhl_message', $e->getMessage() );
|
|
$this->update_status( 'fs-failed' );
|
|
$this->save();
|
|
throw $e;
|
|
}
|
|
|
|
}
|
|
|
|
public function api_cancel() {
|
|
|
|
if ( $this->get_status() !== 'fs-confirmed' ) {
|
|
throw new Exception( sprintf( __( 'Niepoprawny status przesyłki: %s. Przesyłka została anulaowana wcześniej lub protokół został już utworzony?', 'woocommerce-dhl' ), $this->get_status() ) );
|
|
}
|
|
|
|
$dhl_package_number = $this->get_meta( '_dhl_package_number' );
|
|
if ( $this->is_parcelshop_shipment() ) {
|
|
$this->api_cancel_parcelshop();
|
|
} else {
|
|
$this->api_cancel_webapi();
|
|
}
|
|
$this->delete_meta( '_dhl_status' );
|
|
$this->delete_meta( '_dhl_package' );
|
|
$this->delete_meta( '_dhl_package_number' );
|
|
$this->update_status( 'fs-new' );
|
|
$this->save();
|
|
$order = $this->get_order();
|
|
$order->add_order_note( sprintf( __( 'Przesyłka DHL %s została usunięta.', 'woocommerce-dhl' ), $dhl_package_number ), true );
|
|
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function api_cancel_parcelshop() {
|
|
$api = $this->get_api();
|
|
|
|
try {
|
|
$delete_shipment = $api->delete_shipment( $this->get_meta( '_dhl_package_number' ) );
|
|
if ( isset( $delete_shipment->deleteShipmentResult->status ) && $delete_shipment->deleteShipmentResult->status !== 'OK' ) {
|
|
throw new Exception( $delete_shipment->deleteShipmentResult->status );
|
|
}
|
|
} catch ( Exception $e ) {
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function api_cancel_webapi() {
|
|
$api = $this->get_webapi();
|
|
|
|
try {
|
|
$delete_shipment = $api->delete_shipment( $this->get_meta( '_dhl_package_number' ) );
|
|
if ( isset( $delete_shipment->deleteShipmentsResult->item->error ) ) {
|
|
throw new Exception( $delete_shipment->deleteShipmentsResult->item->error );
|
|
}
|
|
} catch ( Exception $e ) {
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
public function get_shipper( $settings ) {
|
|
$shipper = array();
|
|
$shipper['name'] = $settings['sender_name'];
|
|
$shipper['postalCode'] = str_replace( '-', '', $settings['sender_postal_code'] );
|
|
$shipper['city'] = $settings['sender_city'];
|
|
$shipper['street'] = $settings['sender_street'];
|
|
$shipper['houseNumber'] = $settings['sender_house_number'];
|
|
$shipper['apartmentNumber'] = $settings['sender_apartment_number'];
|
|
$shipper['contactPerson'] = $settings['sender_contact_person'];
|
|
$shipper['contactPhone'] = $this->format_phone_number( $settings['sender_contact_phone'] );
|
|
$shipper['contactEmail'] = $settings['sender_contact_email'];
|
|
|
|
return $shipper;
|
|
}
|
|
|
|
public function get_shipper_address( $settings ) {
|
|
$shipper_address = array();
|
|
$shipper_address['name'] = $settings['sender_name'];
|
|
$shipper_address['postcode'] = str_replace( '-', '', $settings['sender_postal_code'] );
|
|
$shipper_address['city'] = $settings['sender_city'];
|
|
$shipper_address['street'] = $settings['sender_street'];
|
|
$shipper_address['houseNumber'] = $settings['sender_house_number'];
|
|
$shipper_address['apartmentNumber'] = $settings['sender_apartment_number'];
|
|
|
|
return $shipper_address;
|
|
}
|
|
|
|
public function get_shipper_contact( $settings ) {
|
|
$shipper_contact = array();
|
|
$shipper_contact['personName'] = $settings['sender_contact_person'];
|
|
$shipper_contact['phoneNumber'] = $this->format_phone_number( $settings['sender_contact_phone'] );
|
|
$shipper_contact['emailAddress'] = $settings['sender_contact_email'];
|
|
|
|
return $shipper_contact;
|
|
}
|
|
|
|
public function get_shipper_preaviso( $settings ) {
|
|
$shipper_preaviso = array();
|
|
$shipper_preaviso['personName'] = $settings['sender_contact_person'];
|
|
$shipper_preaviso['phoneNumber'] = $this->format_phone_number( $settings['sender_contact_phone'] );
|
|
$shipper_preaviso['emailAddress'] = $settings['sender_contact_email'];
|
|
|
|
return $shipper_preaviso;
|
|
}
|
|
|
|
/**
|
|
* @param WC_Order $order
|
|
* @param bool $parcelshop
|
|
*
|
|
* @return array
|
|
* @throws Exception
|
|
*/
|
|
public function get_receiver( WC_Order $order, $parcelshop ) {
|
|
$receiver = array();
|
|
|
|
$receiver['country'] = wpdesk_get_order_meta( $order, '_shipping_country', true );
|
|
$receiver['name'] = '';
|
|
if ( wpdesk_get_order_meta( $order, '_shipping_company', true ) !== '' ) {
|
|
$receiver['name'] .= wpdesk_get_order_meta( $order, '_shipping_company', true );
|
|
$receiver['name'] .= ' ';
|
|
}
|
|
$receiver['name'] .= wpdesk_get_order_meta( $order, '_shipping_first_name', true );
|
|
$receiver['name'] .= ' ';
|
|
$receiver['name'] .= wpdesk_get_order_meta( $order, '_shipping_last_name', true );
|
|
$receiver['postalCode'] = str_replace( array(
|
|
'-',
|
|
' '
|
|
), '', wpdesk_get_order_meta( $order, '_shipping_postcode', true ) );
|
|
$receiver['city'] = wpdesk_get_order_meta( $order, '_shipping_city', true );
|
|
$address = wpdesk_get_order_meta( $order, '_shipping_address_1', true );
|
|
if ( wpdesk_get_order_meta( $order, '_shipping_address_2', true ) != '' ) {
|
|
$address .= ' ' . wpdesk_get_order_meta( $order, '_shipping_address_2', true );
|
|
}
|
|
$address_array = explode( ' ', trim( $address ) );
|
|
$receiver['street'] = $address_array[0];
|
|
$address_element = 1;
|
|
while ( $address_element < count( $address_array ) - 1 ) {
|
|
$receiver['street'] .= ' ' . $address_array[ $address_element ];
|
|
$address_element ++;
|
|
}
|
|
$receiver['houseNumber'] = $address_array[ count( $address_array ) - 1 ];
|
|
|
|
$receiver['apartmentNumber'] = '';
|
|
$receiver['contactPerson'] = wpdesk_get_order_meta( $order, '_shipping_first_name', true ) . ' ' . wpdesk_get_order_meta( $order, '_shipping_last_name', true );
|
|
$receiver['contactPhone'] = $this->format_phone_number( wpdesk_get_order_meta( $order, '_billing_phone', true ) );
|
|
$receiver['contactEmail'] = wpdesk_get_order_meta( $order, '_billing_email', true );
|
|
|
|
return $parcelshop ? $this->overwrite_receiver_address_by_parcelshop( $receiver ) : $receiver;
|
|
}
|
|
|
|
/**
|
|
* @param WC_Order $order
|
|
*
|
|
* @return array
|
|
*/
|
|
public function get_receiver_address( WC_Order $order ): array {
|
|
$receiver_address = [];
|
|
|
|
$receiver_address['name'] = '';
|
|
if ( wpdesk_get_order_meta( $order, '_shipping_company', true ) !== '' ) {
|
|
$receiver_address['name'] .= wpdesk_get_order_meta( $order, '_shipping_company', true );
|
|
$receiver_address['name'] .= ' ';
|
|
}
|
|
$receiver_address['name'] .= wpdesk_get_order_meta( $order, '_shipping_first_name', true );
|
|
$receiver_address['name'] .= ' ';
|
|
$receiver_address['name'] .= wpdesk_get_order_meta( $order, '_shipping_last_name', true );
|
|
|
|
$receiver_address['postcode'] = str_replace( '-', '', wpdesk_get_order_meta( $order, '_shipping_postcode', true ) );
|
|
|
|
$receiver_address['city'] = wpdesk_get_order_meta( $order, '_shipping_city', true );
|
|
$address = wpdesk_get_order_meta( $order, '_shipping_address_1', true );
|
|
if ( wpdesk_get_order_meta( $order, '_shipping_address_2', true ) !== '' ) {
|
|
$address .= ' ' . wpdesk_get_order_meta( $order, '_shipping_address_2', true );
|
|
}
|
|
$address_array = explode( ' ', trim( $address ) );
|
|
$receiver_address['street'] = $address_array[0];
|
|
$address_element = 1;
|
|
|
|
while ( $address_element < count( $address_array ) - 1 ) {
|
|
$receiver_address['street'] .= ' ' . $address_array[ $address_element ];
|
|
$address_element ++;
|
|
}
|
|
|
|
if ( count( $address_array ) === 1 ) {
|
|
throw new WPDesk_WooCommerce_DHL_Invalid_Address_Exception( __( 'Adres wysyłki powinien zawierać numer domu.', 'woocommerce-dhl' ) );
|
|
}
|
|
|
|
$receiver_address['houseNumber'] = $address_array[ count( $address_array ) - 1 ];
|
|
$receiver_address['apartmentNumber'] = '';
|
|
|
|
return $receiver_address;
|
|
}
|
|
|
|
/**
|
|
* @param array $receiver_address .
|
|
*
|
|
* @return array
|
|
* @throws Exception
|
|
*/
|
|
private function overwrite_receiver_address_by_parcelshop( array $receiver_address ): array {
|
|
|
|
$point_details = $this->get_point_details();
|
|
|
|
$settings = $this->get_shipping_method_settings();
|
|
|
|
$receiver_address['postalCode'] = $point_details['address']['postcode'];
|
|
$receiver_address['city'] = $point_details['address']['city'];
|
|
$receiver_address['houseNumber'] = $point_details['sap'];
|
|
if ( 'PACKSTATION' === $point_details['type'] ) {
|
|
$receiver_address['isPostfiliale'] = false;
|
|
$receiver_address['isPackstation'] = true;
|
|
$receiver_address['postnummer'] = $settings['account_number'];
|
|
} else {
|
|
$receiver_address['isPostfiliale'] = true;
|
|
$receiver_address['isPackstation'] = false;
|
|
}
|
|
|
|
return $receiver_address;
|
|
}
|
|
|
|
public function get_receiver_contact( WC_Order $order ) {
|
|
$receiver_contact = [];
|
|
|
|
$receiver_contact['personName'] = wpdesk_get_order_meta( $order, '_shipping_first_name', true ) . ' ' . wpdesk_get_order_meta( $order, '_shipping_last_name', true );
|
|
$receiver_contact['phoneNumber'] = $this->format_phone_number( wpdesk_get_order_meta( $order, '_billing_phone', true ) );
|
|
$receiver_contact['emailAddress'] = wpdesk_get_order_meta( $order, '_billing_email', true );
|
|
|
|
return $receiver_contact;
|
|
}
|
|
|
|
public function get_receiver_preaviso( WC_Order $order ) {
|
|
$receiver_preaviso = array();
|
|
$receiver_preaviso['personName'] = wpdesk_get_order_meta( $order, '_shipping_first_name', true ) . ' ' . wpdesk_get_order_meta( $order, '_shipping_last_name', true );
|
|
$receiver_preaviso['phoneNumber'] = $this->format_phone_number( wpdesk_get_order_meta( $order, '_billing_phone', true ) );
|
|
$receiver_preaviso['emailAddress'] = wpdesk_get_order_meta( $order, '_billing_email', true );
|
|
|
|
return $receiver_preaviso;
|
|
}
|
|
|
|
/**
|
|
* @param string $phone_number
|
|
*
|
|
* @return string
|
|
*/
|
|
private function format_phone_number( $phone_number ) {
|
|
return str_replace( [ ' ', '+' ], '', $phone_number );
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function get_piece_list() {
|
|
$piece_list = array();
|
|
|
|
$piece = array();
|
|
$piece['type'] = $this->get_meta( '_dhl_package_type' );
|
|
$piece['width'] = $this->get_meta( '_dhl_package_width' );
|
|
$piece['height'] = $this->get_meta( '_dhl_package_height' );
|
|
$piece['length'] = $this->get_meta( '_dhl_package_length' );
|
|
$piece['lenght'] = $this->get_meta( '_dhl_package_length' );
|
|
$piece['weight'] = $this->get_meta( '_dhl_package_weight' );
|
|
$piece['nonStandard'] = $this->get_meta( '_dhl_package_nonstandard' );
|
|
$piece['quantity'] = 1;
|
|
|
|
$piece_list[] = $piece;
|
|
|
|
$additional_packages = $this->get_meta( '_additional_packages', array() );
|
|
foreach ( $additional_packages as $additional_key => $additional_package ) {
|
|
$piece = array();
|
|
$piece['type'] = $additional_package['dhl_package_type'];
|
|
$piece['width'] = $additional_package['dhl_package_width'];
|
|
$piece['height'] = $additional_package['dhl_package_height'];
|
|
$piece['length'] = $additional_package['dhl_package_length'];
|
|
$piece['weight'] = $additional_package['dhl_package_weight'];
|
|
$piece['nonStandard'] = $additional_package['dhl_package_nonstandard'];
|
|
$piece['quantity'] = 1;
|
|
$piece_list[] = $piece;
|
|
}
|
|
|
|
return $piece_list;
|
|
}
|
|
|
|
public function get_service() {
|
|
$service = array();
|
|
|
|
$service['product'] = $this->get_meta( '_dhl_product' );
|
|
$service['preaviso'] = true;
|
|
|
|
if ( $service['product'] === 'parcelshop' && ! $this->is_shipping_country_poland() ) {
|
|
$service['product'] = 'EK';
|
|
}
|
|
|
|
if ( (int) $this->get_meta( '_dhl_cod', '0' ) === 1 ) {
|
|
$service['collectOnDelivery'] = true;
|
|
$service['collectOnDeliveryValue'] = $this->get_meta( '_dhl_cod_value' );
|
|
$service['collectOnDeliveryForm'] = 'BANK_TRANSFER';
|
|
}
|
|
|
|
if ( (int) $this->get_meta( '_dhl_insurance', '0' ) === 1 ) {
|
|
$service['insurance'] = true;
|
|
$service['insuranceValue'] = $this->get_meta( '_dhl_insurance_value' );
|
|
}
|
|
|
|
$services = $this->get_meta( '_dhl_services', [] );
|
|
|
|
foreach ( WPDesk_WooCommerce_DHL::get_services() as $service_id => $service_name ) {
|
|
if ( in_array( $service_id, $services, true ) ) {
|
|
$service[ $service_id ] = true;
|
|
}
|
|
}
|
|
|
|
return $service;
|
|
}
|
|
|
|
public function get_payment( $settings ) {
|
|
$payment = array();
|
|
|
|
$payment['paymentMethod'] = 'BANK_TRANSFER';
|
|
$payment['payerType'] = 'SHIPPER';
|
|
$payment['accountNumber'] = $settings['account_number'];
|
|
|
|
return $payment;
|
|
}
|
|
|
|
public function get_label() {
|
|
if ( ! $this->label_avaliable() ) {
|
|
throw new Exception( sprintf( __( 'Etykieta niedostępna dla statusu %s.', 'woocommerce-dhl' ), $this->get_status() ) );
|
|
}
|
|
|
|
return $this->api_get_label();
|
|
}
|
|
|
|
public function get_email_after_order_table() {
|
|
$args = array( 'dhl_packages' => array() );
|
|
|
|
if ( $this->label_avaliable() ) {
|
|
$args = array(
|
|
'dhl_packages' => array(
|
|
array(
|
|
'tracking_url' => $this->get_tracking_url(),
|
|
'shipment_id' => $this->get_meta( '_dhl_package_number' ),
|
|
)
|
|
)
|
|
);
|
|
}
|
|
if ( $this->is_parcelshop_shipment() ) {
|
|
$service_point = $this->get_meta( '_dhl_parcelshop' );
|
|
if ( $service_point !== '' ) {
|
|
$parcelshop_api = $this->get_api();
|
|
$service_point_data = $parcelshop_api->get_service_point( $service_point, $this->get_shipping_postcode( $this->get_order() ), $this->get_radius(), false, $this->get_shipping_country( $this->get_order() ), $this->get_shipping_city( $this->get_order() ) );
|
|
if ( $service_point_data !== '' ) {
|
|
$item = $service_point_data;
|
|
$service_point_data->formated_address = $item->address->city . ' '
|
|
. substr( $item->address->postcode, 0, 2 ) . '-' . substr( $item->address->postcode, 2 ) . ', '
|
|
. $item->address->street . ' '
|
|
. $item->address->houseNumber
|
|
. ( ! empty( $item->address->apartmentNumber ) ? '/' . $item->address->apartmentNumber : '' );
|
|
|
|
$args['service_point_data'] = $service_point_data;
|
|
}
|
|
}
|
|
}
|
|
if ( count( $args ) ) {
|
|
echo $this->plugin->load_template( 'email_after_order_table', 'woocommerce', $args );
|
|
}
|
|
}
|
|
|
|
public function get_after_order_table() {
|
|
$args = [ 'dhl_packages' => [] ];
|
|
|
|
if ( $this->label_avaliable() ) {
|
|
$args = [
|
|
'dhl_packages' => [
|
|
[
|
|
'tracking_url' => $this->get_tracking_url(),
|
|
'shipment_id' => $this->get_meta( '_dhl_package_number' ),
|
|
]
|
|
]
|
|
];
|
|
}
|
|
if ( $this->is_parcelshop_shipment() ) {
|
|
$service_point = $this->get_meta( '_dhl_parcelshop', true );
|
|
if ( $service_point !== '' ) {
|
|
$parcelshop_api = $this->get_api();
|
|
$service_point_data = $parcelshop_api->get_service_point( $service_point, $this->get_shipping_postcode( $this->get_order() ), $this->get_radius(), false, $this->get_shipping_country( $this->get_order() ), $this->get_shipping_city( $this->get_order() ) );
|
|
if ( $service_point_data !== '' ) {
|
|
$item = $service_point_data;
|
|
$service_point_data->formated_address = $item->address->city . ' '
|
|
. substr( $item->address->postcode, 0, 2 ) . '-' . substr( $item->address->postcode, 2 ) . ', '
|
|
. $item->address->street . ' '
|
|
. $item->address->houseNumber
|
|
. ( ! empty( $item->address->apartmentNumber ) ? '/' . $item->address->apartmentNumber : '' );
|
|
|
|
$args['service_point_data'] = $service_point_data;
|
|
}
|
|
}
|
|
}
|
|
if ( count( $args ) ) {
|
|
echo $this->plugin->load_template( 'order_details_after_order_table', 'woocommerce', $args );
|
|
}
|
|
}
|
|
|
|
public function api_get_label() {
|
|
$label_data = array(
|
|
'label_format' => 'pdf',
|
|
'content' => null,
|
|
'file_name' => 'dhl_' . $this->get_tracking_number() . '.pdf'
|
|
);
|
|
|
|
$settings = $this->get_shipping_method_settings();
|
|
|
|
$api = $this->get_api();
|
|
|
|
$label = $api->get_label( $this->get_meta( '_dhl_package_number' ), $settings['label_format'] );
|
|
|
|
if ( isset( $label->getLabelResult->labelContent ) ) {
|
|
$label_data['content'] = base64_decode( $label->getLabelResult->labelContent );
|
|
} elseif ( $label->getLabelsResult->item->labelData ) {
|
|
$label_data['content'] = base64_decode( $label->getLabelsResult->item->labelData );
|
|
}
|
|
|
|
$label_format = $settings['label_format'];
|
|
if ( $label_format === 'ZBLP' ) {
|
|
$label_format = 'zpl';
|
|
} else {
|
|
$label_format = 'pdf';
|
|
}
|
|
$label_data['label_format'] = $label_format;
|
|
$label_data['file_name'] = 'dhl_' . $this->get_tracking_number() . '.' . $label_format;
|
|
|
|
return $label_data;
|
|
}
|
|
|
|
/**
|
|
* @return int
|
|
*/
|
|
private function get_radius(): int {
|
|
return ( $this->is_shipping_country_poland() || $this->is_shipping_country_sweden() ) ? 5000 : 24;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
* @throws Exception
|
|
*/
|
|
private function get_point_details(): array {
|
|
$service_point = $this->get_meta( '_dhl_parcelshop', '' );
|
|
$point = $this->get_api()->get_service_point(
|
|
$service_point,
|
|
$this->get_shipping_postcode( $this->get_order() ),
|
|
$this->get_radius(),
|
|
$this->get_bool_value( $this->get_meta( '_dhl_cod' ) ),
|
|
$this->get_shipping_country( $this->get_order() ),
|
|
$this->get_shipping_city( $this->get_order() )
|
|
);
|
|
if ( $point ) {
|
|
return json_decode( json_encode( $point ), true );
|
|
}
|
|
|
|
throw new Exception( sprintf( __( 'Punkt %s nie został znaleziony', 'woocommerce-dhl' ), $service_point ) );
|
|
}
|
|
|
|
/**
|
|
* @return WPDesk_WooCommerce_DHL_API|WPDesk_WooCommerce_DHL_Parcelshop_API
|
|
*/
|
|
private function get_api() {
|
|
if ( $this->is_parcelshop_shipment() ) {
|
|
if ( $this->is_shipping_country_poland() ) {
|
|
return $this->get_parcelshop_api();
|
|
}
|
|
}
|
|
|
|
return $this->get_webapi();
|
|
}
|
|
|
|
/**
|
|
* @return WPDesk_WooCommerce_DHL_API
|
|
*/
|
|
private function get_webapi() {
|
|
return $this->get_shipping_method()->get_api();
|
|
}
|
|
|
|
/**
|
|
* @return WPDesk_WooCommerce_DHL_Parcelshop_API
|
|
*/
|
|
private function get_parcelshop_api() {
|
|
return $this->get_shipping_method()->get_parcelshop_api();
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
private function get_shipping_method_settings() {
|
|
return $this->get_shipping_method()->settings;
|
|
}
|
|
|
|
/**
|
|
* @return WPDesk_WooCommerce_DHL_Shipping_Method
|
|
*/
|
|
public function get_shipping_method() {
|
|
$shipping_methods = WC()->shipping()->shipping_methods;
|
|
if ( empty( $shipping_methods ) || ! is_array( $shipping_methods ) || count( $shipping_methods ) === 0 ) {
|
|
$shipping_methods = WC()->shipping()->load_shipping_methods();
|
|
}
|
|
|
|
return $shipping_methods['dhl'];
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
private function save_pickup_details() {
|
|
$point = $this->get_point_details();
|
|
|
|
$address = $point['address']['street'] ?? '';
|
|
|
|
if ( isset( $point['address']['houseNumber'] ) ) {
|
|
$address .= ' ' . $point['address']['houseNumber'];
|
|
}
|
|
|
|
if ( isset( $point['address']['apartmentNumber'] ) ) {
|
|
$address .= '/' . $point['address']['apartmentNumber'];
|
|
}
|
|
|
|
$this->set_meta( self::DELIVERY_FIELD_ID, $this->get_meta( '_dhl_parcelshop', '' ) );
|
|
$this->set_meta( self::DELIVERY_FIELD_NAME, $point['name'] ?? '' );
|
|
$this->set_meta( self::DELIVERY_FIELD_ADDRESS, $address );
|
|
$this->set_meta( self::DELIVERY_FIELD_POSTCODE, $point['address']['postcode'] ?? '' );
|
|
$this->set_meta( self::DELIVERY_FIELD_CITY, $point['address']['city'] ?? '' );
|
|
}
|
|
|
|
/**
|
|
* @param WC_Order $order .
|
|
*/
|
|
private function get_shipping_country( $order ) {
|
|
$shipping_country = $order->get_shipping_country();
|
|
|
|
return ! empty( $shipping_country ) ? $shipping_country : $order->get_billing_country();
|
|
}
|
|
|
|
/**
|
|
* @param WC_Order $order .
|
|
*/
|
|
private function get_shipping_city( $order ) {
|
|
$shipping_city = $order->get_shipping_city();
|
|
|
|
return ! empty( $shipping_city ) ? $shipping_city : $order->get_billing_city();
|
|
}
|
|
|
|
/**
|
|
* @param WC_Order $order .
|
|
*/
|
|
private function get_shipping_postcode( $order ) {
|
|
$shipping_postcode = $order->get_shipping_postcode();
|
|
|
|
return ! empty( $shipping_postcode ) ? $shipping_postcode : $order->get_billing_postcode();
|
|
}
|
|
|
|
/**
|
|
* @return bool
|
|
*/
|
|
private function is_shipping_country_poland() {
|
|
return 'PL' === $this->get_shipping_country( $this->get_order() );
|
|
}
|
|
|
|
/**
|
|
* @return bool
|
|
*/
|
|
private function is_shipping_country_sweden() {
|
|
return 'SE' === $this->get_shipping_country( $this->get_order() );
|
|
}
|
|
|
|
/**
|
|
* @return bool
|
|
*/
|
|
private function is_shipping_country_germany() {
|
|
return 'DE' === $this->get_shipping_country( $this->get_order() );
|
|
}
|
|
|
|
/**
|
|
* @return bool
|
|
*/
|
|
private function is_parcelshop_shipment() {
|
|
return $this->get_meta( '_dhl_product', '' ) === 'parcelshop';
|
|
}
|
|
|
|
}
|
|
|
|
}
|