| 1 |
<?php |
| 2 |
|
| 3 |
namespace MakeCommerce\Shipping; |
| 4 |
|
| 5 |
/** |
| 6 |
* All functionality that has to do with shipping labels and printing |
| 7 |
* |
| 8 |
* @since 3.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
class Label extends \MakeCommerce\Shipping { |
| 12 |
|
| 13 |
private $loader; |
| 14 |
|
| 15 |
//page format used for creating labels |
| 16 |
public $page_fromat = "A4"; |
| 17 |
|
| 18 |
/** |
| 19 |
* Constructs Label class, defines loader and hooks |
| 20 |
* |
| 21 |
* @since 3.0.0 |
| 22 |
*/ |
| 23 |
public function __construct( \MakeCommerce\Loader $loader ) { |
| 24 |
|
| 25 |
$this->loader = $loader; |
| 26 |
|
| 27 |
$this->define_hooks(); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Define all wordpress hooks needed for printing stuff and creating labels |
| 32 |
* |
| 33 |
* @since 3.0.0 |
| 34 |
*/ |
| 35 |
public function define_hooks() { |
| 36 |
|
| 37 |
$this->loader->add_action( 'woocommerce_order_actions_end', $this, 'print_button' ); |
| 38 |
$this->loader->add_action( 'wp_ajax_print_pml', $this, 'print' ); |
| 39 |
$this->loader->add_filter( 'admin_action_parcel_machine_print_labels', $this, 'bulk_print' ); |
| 40 |
$this->loader->add_filter( 'admin_footer', $this, 'bulk_print_and_register' ); |
| 41 |
$this->loader->add_filter( 'admin_action_parcel_machine_labels', $this, 'bulk_register' ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Creates shipping label using MakeCommerce API and redirect to the page where it is available or return data |
| 46 |
* |
| 47 |
* @since 3.0.0 |
| 48 |
*/ |
| 49 |
public function create_labels( $post_ids, $ajax = false ) { |
| 50 |
|
| 51 |
if ( !is_array( $post_ids ) ) { |
| 52 |
$post_ids = array( $post_ids ); |
| 53 |
} |
| 54 |
|
| 55 |
$this->initalize(); |
| 56 |
|
| 57 |
$shipping_request = array( 'credentials' => array(), 'orders' => array(), 'printFormat' => $this->page_fromat ); |
| 58 |
|
| 59 |
foreach ( $this->shipping_methods as $shipping_method ) { |
| 60 |
$shipping_classes_map[$shipping_method["method"]] = $shipping_method["class"]; |
| 61 |
} |
| 62 |
|
| 63 |
$missing_ids = array(); |
| 64 |
|
| 65 |
foreach ( $post_ids as $post_id ) { |
| 66 |
|
| 67 |
$shipment_id = get_post_meta( $post_id, '_parcel_machine_shipment_id', true ); |
| 68 |
|
| 69 |
if ( !$shipment_id ) { |
| 70 |
$missing_ids[] = $post_id; |
| 71 |
continue; |
| 72 |
} |
| 73 |
|
| 74 |
$order = new \WC_Order( $post_id ); |
| 75 |
$shipping_methods = $order->get_shipping_methods(); |
| 76 |
|
| 77 |
if ( empty( $shipping_methods ) ) { |
| 78 |
continue; |
| 79 |
} |
| 80 |
|
| 81 |
foreach ( $shipping_methods as $shipping_method ) { |
| 82 |
|
| 83 |
$shipping_id = explode( ':', $shipping_method['method_id'] ); |
| 84 |
$shipping_class = $shipping_id[0]; |
| 85 |
$shipping_instance = !empty( $shipping_id[1] ) ? $shipping_id[1] : null; |
| 86 |
|
| 87 |
if ( empty( $shipping_classes_map[$shipping_class] ) ) { |
| 88 |
continue; |
| 89 |
} |
| 90 |
|
| 91 |
$transport_class = new $shipping_classes_map[$shipping_class]($shipping_instance); |
| 92 |
$carrier_uc = mb_strtoupper( $transport_class->carrier ); |
| 93 |
|
| 94 |
//apt == parcelmachine |
| 95 |
if ( $transport_class->type === 'apt' ) { |
| 96 |
|
| 97 |
$parcel_machine = get_post_meta( $post_id, '_parcel_machine', true ); |
| 98 |
|
| 99 |
if ( !$parcel_machine ) { |
| 100 |
continue; |
| 101 |
} |
| 102 |
|
| 103 |
list( $carrier, $machine_id ) = explode( '||', $parcel_machine ); |
| 104 |
|
| 105 |
if ( !$carrier || !$machine_id ) { |
| 106 |
continue; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if ( empty( $shipping_request['credentials'][$carrier_uc] ) ) { |
| 111 |
|
| 112 |
$api_user = $transport_class->settings['service_user']; |
| 113 |
$api_password = $transport_class->settings['service_password']; |
| 114 |
|
| 115 |
$use_mk_contract = false; |
| 116 |
|
| 117 |
if ( isset( $transport_class->settings['use_mk_contract'] ) ) { |
| 118 |
$use_mk_contract = $transport_class->settings['use_mk_contract']; |
| 119 |
} |
| 120 |
|
| 121 |
//change carrier_uc if it has been set in the interface. |
| 122 |
if ( isset( $transport_class->settings['service_carrier'] ) ) { |
| 123 |
$carrier_uc = $transport_class->settings['service_carrier']; |
| 124 |
} |
| 125 |
|
| 126 |
if ( ( !$api_user || !$api_password ) and ( !$use_mk_contract ) ) { |
| 127 |
|
| 128 |
$this->loader->add_action( 'admin_notices', $this, 'mk_admin_error' ); |
| 129 |
continue; |
| 130 |
} |
| 131 |
|
| 132 |
if ( !$use_mk_contract ) { |
| 133 |
$shipping_request['credentials'][$carrier_uc] = array( 'carrier' => $carrier_uc, 'username' => $api_user, 'password' => $api_password ); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
$sender = array( |
| 138 |
'name' => $transport_class->settings['shop_name'], |
| 139 |
'phone' => $transport_class->settings['shop_phone'], |
| 140 |
'email' => $transport_class->settings['shop_email'], |
| 141 |
'postalCode' => !empty( $transport_class->settings['shop_postal_code'] ) ? $transport_class->settings['shop_postal_code'] : '', |
| 142 |
'country' => !empty( $transport_class->settings['shop_address_country'] ) ? $transport_class->settings['shop_address_country'] : '', |
| 143 |
'city' => !empty( $transport_class->settings['shop_address_city'] ) ? $transport_class->settings['shop_address_city'] : '', |
| 144 |
'street' => !empty( $transport_class->settings['shop_address_street'] ) ? $transport_class->settings['shop_address_street'] : '' |
| 145 |
); |
| 146 |
|
| 147 |
$s_first_name = method_exists( $order, 'get_shipping_first_name' ) ? $order->get_shipping_first_name() : $order->shipping_first_name; |
| 148 |
$s_last_name = method_exists( $order, 'get_shipping_last_name' ) ? $order->get_shipping_last_name() : $order->shipping_last_name; |
| 149 |
$recipient = trim( $s_first_name . ' ' . $s_last_name ); |
| 150 |
|
| 151 |
if ( empty( $recipient ) ) { |
| 152 |
|
| 153 |
$b_first_name = method_exists( $order, 'get_billing_first_name' ) ? $order->get_billing_first_name() : $order->billing_first_name; |
| 154 |
$b_last_name = method_exists( $order, 'get_billing_last_name' ) ? $order->get_billing_last_name() : $order->billing_last_name; |
| 155 |
$recipient = trim( $b_first_name . ' ' . $b_last_name ); |
| 156 |
} |
| 157 |
|
| 158 |
$b_phone = method_exists( $order, 'get_billing_phone' ) ? $order->get_billing_phone() : $order->billing_phone; |
| 159 |
$b_email = method_exists( $order, 'get_billing_email' ) ? $order->get_billing_email() : $order->billing_email; |
| 160 |
|
| 161 |
$sr_order = array( |
| 162 |
'carrier' => $carrier_uc, |
| 163 |
'orderId' => $order->get_id(), |
| 164 |
'recipient' => array( 'name' => $recipient, 'phone' => $b_phone, 'email' => $b_email ), |
| 165 |
'sender' => $sender, |
| 166 |
'shipmentId' => $shipment_id |
| 167 |
); |
| 168 |
|
| 169 |
$m_service_type = null; |
| 170 |
|
| 171 |
if ( $transport_class->carrier === 'omniva' && $transport_class->type === 'atp' ) { |
| 172 |
$m_service_type = 'PA'; |
| 173 |
} else if ( !empty( $transport_class->settings['registerOnPaymentCode'] ) ) { |
| 174 |
$m_service_type = $transport_class->settings['registerOnPaymentCode']; |
| 175 |
} |
| 176 |
|
| 177 |
if ( !empty( $machine_id ) ) { |
| 178 |
$sr_order['destination']['destinationId'] = $machine_id; |
| 179 |
} |
| 180 |
|
| 181 |
if ( $transport_class->type === 'cou' ) { |
| 182 |
|
| 183 |
$sr_order['destination'] = array( |
| 184 |
'postalCode' => $order->shipping_postcode, |
| 185 |
'country' => $order->shipping_country, |
| 186 |
'county' => $order->shipping_state, |
| 187 |
'city' => $order->shipping_city, |
| 188 |
'street' => $order->shipping_address_1 . ' ' . $order->shipping_address_2 |
| 189 |
); |
| 190 |
|
| 191 |
if ( $shipping_class == "courier_smartpost" ) { |
| 192 |
|
| 193 |
$delivery_time = get_post_meta( $post_id, '_delivery_time', true ); |
| 194 |
|
| 195 |
if ( $delivery_time ) { |
| 196 |
$sr_order['destination']['timeWindow'] = $delivery_time; |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
if ( $m_service_type ) { |
| 202 |
$sr_order['services'] = array( 'serviceType' => $m_service_type ); |
| 203 |
} |
| 204 |
|
| 205 |
$shipping_request['orders'][] = $sr_order; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
$shipping_request['credentials'] = array_values( $shipping_request['credentials'] ); |
| 210 |
|
| 211 |
if ( empty( $shipping_request['orders'] ) ) { |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
$MK = \MakeCommerce::get_api(); |
| 216 |
|
| 217 |
if ( !$MK ) { |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
try { |
| 222 |
$response = $MK->createLabels( $shipping_request ); |
| 223 |
} catch ( \Exception $e ) { |
| 224 |
|
| 225 |
echo $e->getMessage(); |
| 226 |
exit(); |
| 227 |
} |
| 228 |
|
| 229 |
if ( empty( $response->labelUrl ) ) { |
| 230 |
return; |
| 231 |
} |
| 232 |
|
| 233 |
if ( $ajax ) { |
| 234 |
return $response->labelUrl; |
| 235 |
} |
| 236 |
|
| 237 |
$mk_err = false; |
| 238 |
if ( !empty( $missing_ids ) ) { |
| 239 |
$mk_err = sprintf( __( 'Orders %s did not have shipment ID attached so no labels was printed! Please register those packages if you think this is an error!', 'wc_makecommerce_domain' ), join( ', ', $missing_ids ) ); |
| 240 |
} |
| 241 |
|
| 242 |
$sendback = add_query_arg( array( 'post_type' => 'shop_order', 'mk_pdf' => urlencode( $response->labelUrl ), 'mk_err' => urlencode( $mk_err ) ), '' ); |
| 243 |
|
| 244 |
wp_redirect( esc_url_raw( $sendback ) ); |
| 245 |
|
| 246 |
exit(); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Prints labels using ajax function in admin |
| 251 |
* |
| 252 |
* @since 3.0.0 |
| 253 |
*/ |
| 254 |
public function print() { |
| 255 |
|
| 256 |
if ( !current_user_can( 'manage_woocommerce' ) ) { |
| 257 |
die(); |
| 258 |
} |
| 259 |
|
| 260 |
echo $this->create_labels( array( intval( $_POST['id'] ) ), true ); |
| 261 |
|
| 262 |
die(); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Prints labels for all selected orders |
| 267 |
* |
| 268 |
* @since 3.0.0 |
| 269 |
*/ |
| 270 |
public function bulk_print() { |
| 271 |
|
| 272 |
$wp_list_table = _get_list_table('WP_Posts_List_Table'); |
| 273 |
$post_ids = array_map('absint', (array)$_REQUEST['post']); |
| 274 |
|
| 275 |
$this->create_labels( $post_ids ); |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Adds bulk shipment register and printing actions to order view |
| 280 |
* |
| 281 |
* @since 3.0.0 |
| 282 |
*/ |
| 283 |
public function bulk_print_and_register() { |
| 284 |
|
| 285 |
wp_enqueue_script( 'jquery-ui-core' ); |
| 286 |
wp_enqueue_script( 'jquery-ui-datepicker' ); |
| 287 |
wp_enqueue_script( 'wc_mk_timepicker', plugins_url( '/js/jquery-ui.timepicker.js', __FILE__ ) ); |
| 288 |
|
| 289 |
wp_enqueue_style( 'wc_mk_timepicker-css', plugins_url( '/css/jquery-ui.timepicker.css', __FILE__ ) ); |
| 290 |
|
| 291 |
global $post_type; |
| 292 |
|
| 293 |
if ( 'shop_order' === $post_type ) { ?> |
| 294 |
<script type="text/javascript"> |
| 295 |
jQuery(function() { |
| 296 |
jQuery('<option>').val('parcel_machine_labels').text('<?php _e( 'Register parcel machine shipments', 'wc_makecommerce_domain' )?>').appendTo('select[name="action"]'); |
| 297 |
jQuery('<option>').val('parcel_machine_labels').text('<?php _e( 'Register parcel machine shipments', 'wc_makecommerce_domain' )?>').appendTo('select[name="action2"]'); |
| 298 |
jQuery('<option>').val('parcel_machine_print_labels').text('<?php _e( 'Print parcel machine labels', 'wc_makecommerce_domain' )?>').appendTo('select[name="action"]'); |
| 299 |
jQuery('<option>').val('parcel_machine_print_labels').text('<?php _e( 'Print parcel machine labels', 'wc_makecommerce_domain' )?>').appendTo('select[name="action2"]'); |
| 300 |
jQuery('select[name="action"]').after('<input type="text" name="transport_time" class="hidden timepicker"/>'); |
| 301 |
jQuery('select[name="action"], select[name="action2"]').on('change', function(){ |
| 302 |
var val = jQuery(this).val(); |
| 303 |
if (val === 'parcel_machine_order_courier') { |
| 304 |
jQuery('.timepicker').removeClass('hidden'); |
| 305 |
} else { |
| 306 |
jQuery('.timepicker').addClass('hidden'); |
| 307 |
} |
| 308 |
}); |
| 309 |
jQuery('.tablenav.bottom .actions.bulkactions').hide(); |
| 310 |
var now = new Date(); |
| 311 |
var mins = now.getMinutes(); |
| 312 |
var quarterHours = Math.ceil(mins/15); |
| 313 |
if (quarterHours == 4) { now.setHours(now.getHours()+1); } |
| 314 |
var rounded = (quarterHours*15)%60; |
| 315 |
now.setMinutes(rounded); |
| 316 |
jQuery('.timepicker').datetimepicker({dateFormat: 'dd/mm/yy', timeFormat: 'HH:mm', stepMinute: 15, minDateTime: now, devaultValue: jQuery.datepicker.formatTime('dd/mm/yyyy HH:mm', now)}); |
| 317 |
}); |
| 318 |
</script> |
| 319 |
<?php if (!empty($_REQUEST['mk_err'])) { ?> |
| 320 |
<script type="text/javascript"> |
| 321 |
jQuery(function(){ |
| 322 |
alert('<?php echo htmlspecialchars($_REQUEST['mk_err']) ?>'); |
| 323 |
}); |
| 324 |
</script> |
| 325 |
<?php } ?> |
| 326 |
<?php if (!empty($_REQUEST['mk_pdf'])) { ?> |
| 327 |
<script type="text/javascript"> |
| 328 |
jQuery(function(){ |
| 329 |
window.open('<?php echo $_REQUEST['mk_pdf'] ?>', 'pdf'); |
| 330 |
}); |
| 331 |
</script> |
| 332 |
<?php } |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Registers shipments in bulk (admin action) |
| 338 |
* |
| 339 |
* @since 3.0.0 |
| 340 |
*/ |
| 341 |
public function bulk_register() { |
| 342 |
|
| 343 |
$wp_list_table = _get_list_table( 'WP_Posts_List_Table' ); |
| 344 |
$post_ids = array_map( 'absint', ( array )$_REQUEST['post'] ); |
| 345 |
|
| 346 |
$this->register_shipment( $post_ids ); |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Supporting function for print_labels. Creates needed javasript for printing button |
| 351 |
* |
| 352 |
* @since 3.0.0 |
| 353 |
*/ |
| 354 |
public function print_button( $post_id ) { |
| 355 |
|
| 356 |
$shipment_id = get_post_meta( $post_id, '_parcel_machine_shipment_id', true ); |
| 357 |
|
| 358 |
if ( !$shipment_id ) { |
| 359 |
return; |
| 360 |
} |
| 361 |
|
| 362 |
?> |
| 363 |
<li class="wide"> |
| 364 |
<a id="print_parcel_machine_label" href="#" class="button"><?php _e( 'Print parcel label', 'wc_makecommerce_domain' );?></a> |
| 365 |
<img class="mc_loading" src="<?php echo site_url('/wp-admin/images/loading.gif');?>"> |
| 366 |
<script type="text/javascript"> |
| 367 |
jQuery(function($){ |
| 368 |
var data = { |
| 369 |
'action': 'print_pml', |
| 370 |
'id': '<?php echo $post_id; ?>' |
| 371 |
}; |
| 372 |
var loading = $('.mc_loading'); |
| 373 |
$('#print_parcel_machine_label').click(function(e){ |
| 374 |
e.preventDefault(); |
| 375 |
var button = $(this); |
| 376 |
button.addClass('disabled'); |
| 377 |
loading.show(); |
| 378 |
$.post(ajaxurl, data, function(response) { |
| 379 |
button.removeClass('disabled'); |
| 380 |
loading.hide(); |
| 381 |
window.open(response, 'pdf'); |
| 382 |
}); |
| 383 |
}); |
| 384 |
}); |
| 385 |
</script> |
| 386 |
</li> |
| 387 |
<?php |
| 388 |
} |
| 389 |
} |