109 lines
3.7 KiB
PHP
109 lines
3.7 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Can add plugin data to tracker.
|
|
*/
|
|
class WPDesk_Paczkomaty_Tracker implements \VendorInPost\WPDesk\PluginBuilder\Plugin\Hookable {
|
|
|
|
public function hooks() {
|
|
add_filter( 'wpdesk_tracker_data', [ $this, 'wpdesk_tracker_data_paczkomaty_shipping_method' ], 11 );
|
|
}
|
|
|
|
public function wpdesk_tracker_data_paczkomaty_shipping_method( $data ) {
|
|
$shipping_methods = WC()->shipping()->get_shipping_methods();
|
|
if ( isset( $shipping_methods['paczkomaty_shipping_method'] ) ) {
|
|
$paczkomaty_shipping_method = $shipping_methods['paczkomaty_shipping_method'];
|
|
$settings = $paczkomaty_shipping_method->settings;
|
|
|
|
$plugin_data = [];
|
|
if ( isset( $settings['kurier_self_send'] ) ) {
|
|
$plugin_data['kurier_self_send'] = $settings['kurier_self_send'];
|
|
}
|
|
if ( isset( $settings['auto_create'] ) ) {
|
|
$plugin_data['auto_create'] = $settings['auto_create'];
|
|
}
|
|
if ( isset( $settings['order_status'] ) ) {
|
|
$plugin_data['order_status'] = $settings['order_status'];
|
|
}
|
|
if ( isset( $settings['complete_order'] ) ) {
|
|
$plugin_data['complete_order'] = $settings['complete_order'];
|
|
}
|
|
if ( isset( $settings['label_format'] ) ) {
|
|
$plugin_data['label_format'] = $settings['label_format'];
|
|
}
|
|
if ( isset( $settings['label_format_type'] ) ) {
|
|
$plugin_data['label_format_type'] = $settings['label_format_type'];
|
|
}
|
|
if ( isset( $settings['auto_print'] ) ) {
|
|
$plugin_data['auto_print'] = $settings['auto_print'];
|
|
}
|
|
$plugin_data['api_type'] = isset( $settings['paczkomaty'] ) && $settings['paczkomaty'] === 'yes'
|
|
? WC_Paczkomaty_Shipping_Method::API_TYPE_XML
|
|
: WC_Paczkomaty_Shipping_Method::API_TYPE_SHIPX;
|
|
|
|
$plugin_data['parcels'] = [];
|
|
$all_parcels = 0;
|
|
global $wpdb;
|
|
$sql = "
|
|
SELECT count(p.ID) AS count, p.post_status AS post_status, min(p.post_date) AS min, max(p.post_date) AS max
|
|
FROM {$wpdb->posts} p, {$wpdb->postmeta} m
|
|
WHERE p.post_type = 'shipment'
|
|
AND p.ID = m.post_id
|
|
AND m.meta_key = '_integration'
|
|
AND m.meta_value = 'paczkomaty'
|
|
GROUP BY p.post_status
|
|
";
|
|
$query = $wpdb->get_results( $sql ); // phpcs:ignore.
|
|
if ( $query ) {
|
|
foreach ( $query as $row ) {
|
|
$plugin_data['parcels'][ $row->post_status ] = $row->count;
|
|
$all_parcels = $all_parcels + $row->count;
|
|
}
|
|
}
|
|
$plugin_data['all_parcels'] = $all_parcels;
|
|
|
|
$plugin_data['parcels_by_service'] = [];
|
|
$sql = "
|
|
SELECT count(p.ID) AS count, m2.meta_value AS paczkomaty_usluga
|
|
FROM {$wpdb->posts} p, {$wpdb->postmeta} m1, {$wpdb->postmeta} m2
|
|
WHERE p.post_type = 'shipment'
|
|
AND p.ID = m2.post_id
|
|
AND m1.meta_key = '_integration'
|
|
AND m1.meta_value = 'paczkomaty'
|
|
AND p.ID = m1.post_id
|
|
AND m2.meta_key = '_paczkomaty_usluga'
|
|
GROUP BY m2.meta_value
|
|
";
|
|
$query = $wpdb->get_results( $sql ); // phpcs:ignore.
|
|
if ( $query ) {
|
|
foreach ( $query as $row ) {
|
|
$plugin_data['parcels_by_service'][ $row->paczkomaty_usluga ] = $row->count;
|
|
}
|
|
}
|
|
|
|
$sql = "
|
|
SELECT TIMESTAMPDIFF(MONTH, min(p.post_date), max(p.post_date) )+1 AS months
|
|
FROM {$wpdb->posts} p, {$wpdb->postmeta} m
|
|
WHERE p.post_type = 'shipment'
|
|
AND p.ID = m.post_id
|
|
AND m.meta_key = '_integration'
|
|
AND m.meta_value = 'paczkomaty'
|
|
GROUP BY p.post_status
|
|
";
|
|
$query = $wpdb->get_results( $sql ); // phpcs:ignore.
|
|
if ( $query ) {
|
|
foreach ( $query as $row ) {
|
|
if ( $row->months != 0 ) {
|
|
$plugin_data['avg_parcels_per_month'] = floatval( $all_parcels ) / floatval( $row->months );
|
|
}
|
|
}
|
|
}
|
|
$data['paczkomaty_shipping_method'] = $plugin_data;
|
|
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
}
|