| 1 |
<?php |
| 2 |
|
| 3 |
global $wpdb; |
| 4 |
|
| 5 |
$helper = new Mbe_Shipping_Helper_Data(); |
| 6 |
$logger = new Mbe_Shipping_Helper_Logger(); |
| 7 |
|
| 8 |
if ($helper->isEnabled() && $helper->isClosureAutomatically()) { |
| 9 |
|
| 10 |
$ws = new Mbe_Shipping_Model_Ws(); |
| 11 |
if ($ws->mustCloseShipments()) { |
| 12 |
|
| 13 |
$logger->log('Cron Close shipments'); |
| 14 |
|
| 15 |
$time = time(); |
| 16 |
$to = date('Y-m-d H:i:s', $time); |
| 17 |
$lastTime = $time - 60 * 60 * 24 * 30; // 60*60*24*2 |
| 18 |
$from = date('Y-m-d H:i:s', $lastTime); |
| 19 |
|
| 20 |
$post_status = implode("','", array('wc-processing', 'wc-completed')); |
| 21 |
|
| 22 |
$order_ids = $helper->select_mbe_ids(); |
| 23 |
$orders_custom_mapping_ids = $helper->select_custom_mapping_ids(); |
| 24 |
$orders_pickup_batch_ids = $helper->select_pickup_orders_ids(); |
| 25 |
$order_filter = 'AND ((ID IN ('.$order_ids.') OR ID IN ('.$orders_custom_mapping_ids.')) AND ID NOT IN ('.$orders_pickup_batch_ids.'))'; |
| 26 |
|
| 27 |
if ( \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled() ) { |
| 28 |
$table_name = $wpdb->prefix . 'wc_orders'; |
| 29 |
$postmetaTableName = $wpdb->prefix . 'wc_orders_meta'; |
| 30 |
$postmetaTableJoin = ' AS pm ON pm.order_id = p.ID '; |
| 31 |
$postTypeWhere = "type = 'shop_order'"; |
| 32 |
$postStatus = 'status'; |
| 33 |
$orderIdField = 'id'; |
| 34 |
} else { |
| 35 |
$table_name = $wpdb->prefix . 'posts'; |
| 36 |
$postmetaTableName = $wpdb->prefix . 'postmeta'; |
| 37 |
$postmetaTableJoin = ' AS pm ON pm.post_id = p.ID '; |
| 38 |
$postTypeWhere = "post_type = 'shop_order'"; |
| 39 |
$postStatus = 'post_status'; |
| 40 |
$orderIdField = 'ID'; |
| 41 |
} |
| 42 |
$logger->logVar('Selecting the orders with shipments to close'); |
| 43 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 44 |
$results = $wpdb->get_results($wpdb->prepare( |
| 45 |
"SELECT * FROM %i |
| 46 |
WHERE $postTypeWhere $order_filter |
| 47 |
AND %i IN ('{$post_status}')" |
| 48 |
, $table_name, $postStatus)); |
| 49 |
|
| 50 |
$post_ids = array(); |
| 51 |
foreach ($results as $order) { |
| 52 |
$post_ids[] = ($order->$orderIdField); |
| 53 |
} |
| 54 |
$logger->logVar($post_ids,'Orders not closed (all)'); |
| 55 |
$toClosedIds = array(); |
| 56 |
$alreadyClosedIds = array(); |
| 57 |
$withoutTracking = array(); |
| 58 |
$pickupIds = array(); |
| 59 |
|
| 60 |
foreach ($post_ids as $post_id) { |
| 61 |
if (!$helper->hasTracking($post_id)) { |
| 62 |
$withoutTracking[] = $post_id; |
| 63 |
} elseif (!empty($helper->getOrderPickupCustomDataId($post_id))) { |
| 64 |
$pickupIds[] = $post_id; |
| 65 |
} elseif ($helper->isShippingOpen($post_id) && empty($helper->getOrderPickupCustomDataId($post_id))) { // additional check to be sure that pickup shipment won't be closed |
| 66 |
$toClosedIds[] = $post_id; |
| 67 |
} else { |
| 68 |
$alreadyClosedIds[] = $post_id; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
$logger->logVar($toClosedIds,'Orders with shipments to close'); |
| 73 |
|
| 74 |
$ws->closeShipping($toClosedIds); |
| 75 |
|
| 76 |
if (count($withoutTracking) > 0) { |
| 77 |
$message = sprintf(__('%s - Total of %d order(s) without tracking number yet.', 'mail-boxes-etc'), date('Y-m-d H:i:s'), count($withoutTracking)); |
| 78 |
$logger->log($message); |
| 79 |
} |
| 80 |
|
| 81 |
if(count($pickupIds) > 0) { |
| 82 |
$message = sprintf(__('%s - Total of %d order(s) are pickup shipment.', 'mail-boxes-etc'), date('Y-m-d H:i:s'), count($pickupIds)); |
| 83 |
$logger->log($message); |
| 84 |
} |
| 85 |
|
| 86 |
if (count($toClosedIds) > 0) { |
| 87 |
$message = sprintf(__('%s - Total of %d order(s) have been closed.', 'mail-boxes-etc'), date('Y-m-d H:i:s'), count($toClosedIds)); |
| 88 |
$logger->log($message); |
| 89 |
} |
| 90 |
|
| 91 |
if (count($alreadyClosedIds) > 0) { |
| 92 |
$message = sprintf(__('%s - Total of %d order(s) were already closed', 'mail-boxes-etc'), date('Y-m-d H:i:s'), count($alreadyClosedIds)); |
| 93 |
$logger->log($message); |
| 94 |
} |
| 95 |
|
| 96 |
} else { |
| 97 |
$logger->log('User must not close shipments'); |
| 98 |
} |
| 99 |
} |
| 100 |
die(); |