125 lines
3.5 KiB
PHP
125 lines
3.5 KiB
PHP
<?php
|
|
/**
|
|
* Labels builder.
|
|
*
|
|
* @package Woocommerce Dpd
|
|
*/
|
|
|
|
/**
|
|
* Can build labels.
|
|
*/
|
|
class WPDesk_Flexible_Shipping_Dpd_Label_Builder extends WPDesk_Flexible_Shipping_Integration_Label_Builder {
|
|
|
|
/**
|
|
* Get labels for shipments.
|
|
*
|
|
* @return array
|
|
*
|
|
* @throws Exception .
|
|
*/
|
|
public function get_labels_for_shipments() {
|
|
$shipping_method = $this->get_shipping_method();
|
|
if ( WPDesk_WooCommerce_DPD_Label_Settings::A4_MULTIPLE === $shipping_method->get_option( WPDesk_WooCommerce_DPD_Shipping_Method::OPTION_LABEL_PAGE_FORMAT, '' ) ) {
|
|
return $this->get_labels_as_single_file_per_session_type();
|
|
} else {
|
|
return $this->get_labels_as_label_per_file();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get labels as label per file.
|
|
*
|
|
* @return array
|
|
*
|
|
* @throws Exception .
|
|
*/
|
|
private function get_labels_as_single_file_per_session_type() {
|
|
$labels = array();
|
|
$packages_domestic = array();
|
|
$packages_international = array();
|
|
/** @var WPDesk_Flexible_Shipping_Shipment_dpd $shipment */ // phpcs:ignore
|
|
foreach ( $this->shipments as $shipment ) {
|
|
if ( $shipment->label_avaliable() ) {
|
|
$package_id = $shipment->get_meta( WPDesk_Flexible_Shipping_Shipment_dpd::DPD_PACKAGE_ID );
|
|
if ( $shipment->get_session_type() === $shipment::DOMESTIC ) {
|
|
$packages_domestic[] = $package_id;
|
|
} else {
|
|
$packages_international[] = $package_id;
|
|
}
|
|
}
|
|
}
|
|
if ( count( $packages_domestic ) ) {
|
|
$labels[] = $this->get_labels_from_api_for_parcels( WPDesk_Flexible_Shipping_Shipment_dpd::DOMESTIC, $packages_domestic );
|
|
}
|
|
if ( count( $packages_international ) ) {
|
|
$labels[] = $this->get_labels_from_api_for_parcels( WPDesk_Flexible_Shipping_Shipment_dpd::INTERNATIONAL, $packages_international );
|
|
}
|
|
return $labels;
|
|
}
|
|
|
|
/**
|
|
* Get labels from api for parcels.
|
|
*
|
|
* @param string $session_type .
|
|
* @param array $packages .
|
|
*
|
|
* @return array
|
|
*
|
|
* @throws Exception .
|
|
*/
|
|
private function get_labels_from_api_for_parcels( $session_type, $packages ) {
|
|
$api = $this->get_shipping_method()->get_api();
|
|
|
|
$label_response = $api->get_labels_in_single_file(
|
|
$packages,
|
|
$session_type,
|
|
WPDesk_WooCommerce_DPD_Label_Settings::PDF,
|
|
WPDesk_WooCommerce_DPD_Label_Settings::A4,
|
|
WPDesk_WooCommerce_DPD_Label_Settings::BIC3
|
|
);
|
|
|
|
$label_format = strtolower( WPDesk_WooCommerce_DPD_Label_Settings::PDF );
|
|
|
|
return array(
|
|
'label_format' => $label_format,
|
|
'content' => $label_response->return->documentData,
|
|
'file_name' => 'dpd_labels_' . strtolower( $session_type ) . '.' . $label_format,
|
|
);
|
|
|
|
}
|
|
|
|
/**
|
|
* Get labels as label per file.
|
|
*
|
|
* @return array
|
|
*
|
|
* @throws Exception .
|
|
*/
|
|
private function get_labels_as_label_per_file() {
|
|
$labels = array();
|
|
/** @var WPDesk_Flexible_Shipping_Shipment_dpd $shipment */ // phpcs:ignore
|
|
foreach ( $this->shipments as $shipment ) {
|
|
if ( $shipment->label_avaliable() ) {
|
|
$labels[] = $shipment->get_label();
|
|
}
|
|
}
|
|
return $labels;
|
|
}
|
|
|
|
/**
|
|
* Get shipping method.
|
|
*
|
|
* @return WPDesk_WooCommerce_DPD_Shipping_Method
|
|
*/
|
|
private function get_shipping_method() {
|
|
$shipping_methods = WC()->shipping()->shipping_methods;
|
|
if ( empty( $shipping_methods ) || ! is_array( $shipping_methods ) || count( $shipping_methods ) === 0 || ! isset( $shipping_methods[ WPDesk_WooCommerce_DPD_Shipping_Method::SHIPPING_METHOD_ID ] ) ) {
|
|
$shipping_methods = WC()->shipping()->load_shipping_methods();
|
|
}
|
|
|
|
return $shipping_methods[ WPDesk_WooCommerce_DPD_Shipping_Method::SHIPPING_METHOD_ID ];
|
|
}
|
|
|
|
}
|
|
|