|
|
|
|
@@ -10,7 +10,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
|
class Siti_Stock_Supplier_Exports {
|
|
|
|
|
|
|
|
|
|
const CRON_HOOK = 'siti_stock_plugin_supplier_export';
|
|
|
|
|
const SUPPLIER_META_KEY = '_wpbc_supplier';
|
|
|
|
|
const SUPPLIER_META_KEY = '_wpci_supplier';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var Siti_Stock_Settings
|
|
|
|
|
@@ -178,7 +178,6 @@ class Siti_Stock_Supplier_Exports {
|
|
|
|
|
? $settings['supplier_exports']
|
|
|
|
|
: array();
|
|
|
|
|
|
|
|
|
|
$configured = $this->ensure_defaults( $configured );
|
|
|
|
|
$configured = $this->ensure_distinct_meta_suppliers( $configured );
|
|
|
|
|
|
|
|
|
|
uasort(
|
|
|
|
|
@@ -258,7 +257,7 @@ class Siti_Stock_Supplier_Exports {
|
|
|
|
|
$body_lines[] = $csv;
|
|
|
|
|
|
|
|
|
|
$headers = array( 'Content-Type: text/plain; charset=UTF-8' );
|
|
|
|
|
$sent = wp_mail( $admin_email, $subject, implode( "\n", $body_lines ), $headers );
|
|
|
|
|
$sent = wp_mail( $admin_email, $subject, implode( "\r\n", $body_lines ), $headers );
|
|
|
|
|
|
|
|
|
|
if ( ! $sent ) {
|
|
|
|
|
return new WP_Error( 'siti_stock_mail_failed', __( 'E-mail verzenden is mislukt.', 'siti-stock-plugin' ) );
|
|
|
|
|
@@ -307,16 +306,18 @@ class Siti_Stock_Supplier_Exports {
|
|
|
|
|
private function collect_rows_for_supplier( $supplier ) {
|
|
|
|
|
$key = $this->normalize_supplier_key( $supplier );
|
|
|
|
|
|
|
|
|
|
$after_timestamp = current_time( 'timestamp' ) - DAY_IN_SECONDS;
|
|
|
|
|
$date_filter = array(
|
|
|
|
|
'after' => gmdate( 'Y-m-d H:i:s', $after_timestamp ),
|
|
|
|
|
);
|
|
|
|
|
$site_timezone = function_exists( 'wp_timezone' ) ? wp_timezone() : new DateTimeZone( wp_timezone_string() );
|
|
|
|
|
$after_date = new DateTimeImmutable( 'now', $site_timezone );
|
|
|
|
|
$after_date = $after_date->sub( new DateInterval( 'P1D' ) );
|
|
|
|
|
$after_utc = $after_date->setTimezone( new DateTimeZone( 'UTC' ) );
|
|
|
|
|
|
|
|
|
|
if ( class_exists( 'WC_DateTime' ) ) {
|
|
|
|
|
$after_object = new WC_DateTime( '@' . $after_timestamp );
|
|
|
|
|
$after_object->setTimezone( new DateTimeZone( 'UTC' ) );
|
|
|
|
|
$date_filter = array( 'after' => $after_object );
|
|
|
|
|
}
|
|
|
|
|
$date_query = array(
|
|
|
|
|
array(
|
|
|
|
|
'column' => 'date_created_gmt',
|
|
|
|
|
'after' => $after_utc->format( 'Y-m-d H:i:s' ),
|
|
|
|
|
'inclusive' => true,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$query = new WC_Order_Query(
|
|
|
|
|
array(
|
|
|
|
|
@@ -324,7 +325,7 @@ class Siti_Stock_Supplier_Exports {
|
|
|
|
|
'limit' => -1,
|
|
|
|
|
'type' => 'shop_order',
|
|
|
|
|
'return' => 'objects',
|
|
|
|
|
'date_created' => $date_filter,
|
|
|
|
|
'date_query' => $date_query,
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
@@ -437,7 +438,7 @@ class Siti_Stock_Supplier_Exports {
|
|
|
|
|
$lines[] = sprintf( '%s;%d', $sku, $quantity );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return implode( "\n", $lines );
|
|
|
|
|
return implode( "\r\n", $lines );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@@ -469,37 +470,6 @@ class Siti_Stock_Supplier_Exports {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Ensure default suppliers exist in config.
|
|
|
|
|
*
|
|
|
|
|
* @param array<string,mixed> $configured Configured values.
|
|
|
|
|
* @return array<string,array<string,mixed>>
|
|
|
|
|
*/
|
|
|
|
|
private function ensure_defaults( array $configured ) {
|
|
|
|
|
foreach ( $this->get_default_suppliers() as $key => $data ) {
|
|
|
|
|
if ( isset( $configured[ $key ] ) ) {
|
|
|
|
|
if ( empty( $configured[ $key ]['supplier'] ) ) {
|
|
|
|
|
$configured[ $key ]['supplier'] = $data['label'];
|
|
|
|
|
}
|
|
|
|
|
$configured[ $key ]['time'] = $this->normalize_time(
|
|
|
|
|
isset( $configured[ $key ]['time'] ) ? $configured[ $key ]['time'] : $data['time']
|
|
|
|
|
);
|
|
|
|
|
if ( ! isset( $configured[ $key ]['enabled'] ) ) {
|
|
|
|
|
$configured[ $key ]['enabled'] = $data['enabled'];
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$configured[ $key ] = array(
|
|
|
|
|
'supplier' => $data['label'],
|
|
|
|
|
'time' => $data['time'],
|
|
|
|
|
'enabled' => $data['enabled'],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $configured;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add entries for suppliers found in product meta.
|
|
|
|
|
*
|
|
|
|
|
@@ -507,22 +477,21 @@ class Siti_Stock_Supplier_Exports {
|
|
|
|
|
* @return array<string,mixed>
|
|
|
|
|
*/
|
|
|
|
|
private function ensure_distinct_meta_suppliers( array $configured ) {
|
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
|
|
foreach ( $this->get_distinct_suppliers() as $supplier ) {
|
|
|
|
|
$key = $this->normalize_supplier_key( $supplier );
|
|
|
|
|
|
|
|
|
|
if ( isset( $configured[ $key ] ) ) {
|
|
|
|
|
$configured[ $key ]['supplier'] = $supplier;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$configured[ $key ] = array(
|
|
|
|
|
$existing = isset( $configured[ $key ] ) ? $configured[ $key ] : array();
|
|
|
|
|
$result[ $key ] = array(
|
|
|
|
|
'supplier' => $supplier,
|
|
|
|
|
'time' => $this->get_default_time_for_key( $key ),
|
|
|
|
|
'enabled' => $this->is_default_enabled( $key ),
|
|
|
|
|
'time' => $this->normalize_time(
|
|
|
|
|
isset( $existing['time'] ) ? $existing['time'] : $this->get_default_time_for_key( $key )
|
|
|
|
|
),
|
|
|
|
|
'enabled' => isset( $existing['enabled'] ) ? (bool) $existing['enabled'] : $this->is_default_enabled( $key ),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $configured;
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|