198 lines
7.1 KiB
PHP
198 lines
7.1 KiB
PHP
<?php
|
|
/**
|
|
* Manifest.
|
|
*
|
|
* @package WooCommerce DPD
|
|
*/
|
|
|
|
/**
|
|
* Implements Manifest methods.
|
|
*/
|
|
class WPDesk_Flexible_Shipping_Manifest_dpd extends WPDesk_Flexible_Shipping_Manifest implements WPDesk_Flexible_Shipping_Manifest_Interface { // phpcs:ignore
|
|
|
|
const META_NUMBER = '_number';
|
|
|
|
const COUNTRY_PL = 'PL';
|
|
|
|
/**
|
|
* Get manifest number.
|
|
*
|
|
* @return string|null
|
|
*/
|
|
public function get_number() {
|
|
return $this->get_meta( self::META_NUMBER );
|
|
}
|
|
|
|
/**
|
|
* Get manifest file.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function get_manifest() {
|
|
return array(
|
|
'file_name' => 'dpd_manifest_' . $this->get_number() . '.pdf',
|
|
'content' => base64_decode( $this->get_meta( '_document_data', '' ) ),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Generate manifest.
|
|
*
|
|
* @throws Exception|WPDesk_WooCommerce_DPD_Manifest_Creation_Exception .
|
|
*/
|
|
public function generate() {
|
|
$api = $this->get_api();
|
|
$shipping_method = $this->get_shipping_method();
|
|
$settings = $shipping_method->settings;
|
|
$fid = $settings['fid'];
|
|
if ( 'yes' === $settings['test_mode'] ) {
|
|
$fid = $settings['fid_testmode'];
|
|
}
|
|
|
|
$packages = array();
|
|
$shipments = $this->get_meta( '_shipments', array() );
|
|
$session_type = '';
|
|
$sender_address_id = false;
|
|
foreach ( $shipments as $shipment_id ) {
|
|
$shipment = fs_get_shipment( $shipment_id );
|
|
$order = $shipment->get_order();
|
|
if ( self::COUNTRY_PL !== $order->get_shipping_country() ) {
|
|
$current_session_type = WPDesk_Flexible_Shipping_Shipment_dpd::INTERNATIONAL;
|
|
} else {
|
|
$current_session_type = WPDesk_Flexible_Shipping_Shipment_dpd::DOMESTIC;
|
|
}
|
|
|
|
if ( '' !== $shipment->get_meta( '_dpd_session_type', '' ) ) {
|
|
$current_session_type = $shipment->get_meta( '_dpd_session_type', '' );
|
|
}
|
|
|
|
if ( '' === $session_type ) {
|
|
$session_type = $current_session_type;
|
|
}
|
|
if ( $session_type !== $current_session_type ) {
|
|
throw new WPDesk_WooCommerce_DPD_Manifest_Creation_Exception( __( 'Protokół może zawierać tylko przesyłki krajowe, lub tylko międzynarodowe.', 'woocommerce-dpd' ) );
|
|
}
|
|
|
|
$sender_address_id = $this->verify_sender_address_id( $sender_address_id, $shipment );
|
|
|
|
$packages[] = $shipment->get_meta( WPDesk_Flexible_Shipping_Shipment_dpd::DPD_PACKAGE_ID, '' );
|
|
}
|
|
$manifest = $api->create_manifest( $packages, $session_type, $this->prepare_pickup_address( $fid, $sender_address_id ) );
|
|
if ( isset( $manifest->return, $manifest->return->session, $manifest->return->session->statusInfo ) ) {
|
|
if ( 'OK' === $manifest->return->session->statusInfo->status && isset( $manifest->return->documentData ) ) {
|
|
$this->set_meta( '_document_data', base64_encode( $manifest->return->documentData ) );
|
|
$this->set_meta( self::META_NUMBER, $manifest->return->documentId );
|
|
} else {
|
|
if ( isset( $manifest->return->session->statusInfo->description ) ) {
|
|
throw new WPDesk_WooCommerce_DPD_Manifest_Creation_Exception( $manifest->return->session->statusInfo->description );
|
|
} elseif ( ! isset( $manifest->return->documentData ) ) {
|
|
throw new WPDesk_WooCommerce_DPD_Manifest_Creation_Exception( __( 'API zwróciło błędną zawartość, brak dokumentu!', 'woocommerce-dpd' ) );
|
|
} else {
|
|
throw new WPDesk_WooCommerce_DPD_Manifest_Creation_Exception( $manifest->return->session->statusInfo->status );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Prepare pickup address.
|
|
*
|
|
* @param string $fid .
|
|
* @param string $sender_address_id .
|
|
* @return array
|
|
*
|
|
* @throws WPDesk_WooCommerce_DPD_Manifest_Creation_Exception .
|
|
*/
|
|
private function prepare_pickup_address( $fid, $sender_address_id ) {
|
|
$pickup_address = false;
|
|
$shipping_method = $this->get_shipping_method();
|
|
if ( '' === $sender_address_id ) {
|
|
$pickup_address = array(
|
|
'name' => $shipping_method->get_option( $shipping_method::OPTION_SENDER_NAME, '' ),
|
|
'company' => $shipping_method->get_option( $shipping_method::OPTION_SENDER_COMPANY, '' ),
|
|
'address' => $shipping_method->get_option( $shipping_method::OPTION_SENDER_ADDRESS, '' ),
|
|
'city' => $shipping_method->get_option( $shipping_method::OPTION_SENDER_CITY, '' ),
|
|
'countryCode' => self::COUNTRY_PL,
|
|
'postalCode' => $shipping_method->get_option( $shipping_method::OPTION_SENDER_POSTAL_CODE, '' ),
|
|
'phone' => $shipping_method->get_option( $shipping_method::OPTION_SENDER_PHONE, '' ),
|
|
'email' => $shipping_method->get_option( $shipping_method::OPTION_SENDER_EMAIL, '' ),
|
|
);
|
|
} else {
|
|
$additional_sender_addresses = $shipping_method->get_additional_sender_addresses();
|
|
foreach ( $additional_sender_addresses as $additional_sender_address ) {
|
|
if ( $sender_address_id === $additional_sender_address->get_address_id() ) {
|
|
$pickup_address = array(
|
|
'name' => $additional_sender_address->get_name(),
|
|
'company' => $additional_sender_address->get_company(),
|
|
'address' => $additional_sender_address->get_address(),
|
|
'city' => $additional_sender_address->get_city(),
|
|
'countryCode' => self::COUNTRY_PL,
|
|
'postalCode' => $additional_sender_address->get_postal_code(),
|
|
'phone' => $additional_sender_address->get_phone(),
|
|
'email' => $additional_sender_address->get_email(),
|
|
);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if ( false === $pickup_address ) {
|
|
throw new WPDesk_WooCommerce_DPD_Manifest_Creation_Exception(
|
|
// Translators: sender address id.
|
|
sprintf( __( 'Błędny identyfikator adresu nadawcy przesyłki: %1$s', 'woocommerce-dpd' ), $sender_address_id )
|
|
);
|
|
}
|
|
return $pickup_address;
|
|
}
|
|
|
|
/**
|
|
* Verify sender address id.
|
|
* All shipments in manifest should have same sender address.
|
|
*
|
|
* @param string|bool $current_sender_address_id .
|
|
* @param WPDesk_Flexible_Shipping_Shipment_dpd $shipment .
|
|
* @return string
|
|
*
|
|
* @throws WPDesk_WooCommerce_DPD_Manifest_Creation_Exception .
|
|
*/
|
|
private function verify_sender_address_id( $current_sender_address_id, $shipment ) {
|
|
$shipment_sender_address_id = $shipment->get_meta( WPDesk_Flexible_Shipping_Shipment_dpd::DPD_SENDER_ADDRESS_ID, '' );
|
|
if ( false !== $current_sender_address_id ) {
|
|
if ( $current_sender_address_id !== $shipment_sender_address_id ) {
|
|
throw new WPDesk_WooCommerce_DPD_Manifest_Creation_Exception( __( 'Protokół może zawierać tylko przesyłki dla jednego adresu nadania.', 'woocommerce-dpd' ) );
|
|
}
|
|
}
|
|
return $shipment_sender_address_id;
|
|
}
|
|
|
|
/**
|
|
* Cancel manifest.
|
|
*/
|
|
public function cancel() {
|
|
$this->delete_meta( self::META_NUMBER );
|
|
}
|
|
|
|
/**
|
|
* Get API.
|
|
*
|
|
* @return bool|WPDesk_WooCommerce_DPD_API
|
|
*/
|
|
public function get_api() {
|
|
$shipping_method = $this->get_shipping_method();
|
|
return $shipping_method->get_api();
|
|
}
|
|
|
|
/**
|
|
* Get shipping method.
|
|
*
|
|
* @return WPDesk_WooCommerce_DPD_Shipping_Method
|
|
*/
|
|
public function get_shipping_method() {
|
|
$shipping_methods = WC()->shipping()->shipping_methods;
|
|
if ( empty( $shipping_methods ) || ! is_array( $shipping_methods ) || 0 === count( $shipping_methods ) ) {
|
|
$shipping_methods = WC()->shipping()->load_shipping_methods();
|
|
}
|
|
return $shipping_methods[ WPDesk_WooCommerce_DPD_Shipping_Method::SHIPPING_METHOD_ID ];
|
|
}
|
|
}
|
|
|