| 1 |
<?php |
| 2 |
|
| 3 |
class Mbe_Shipping_Model_Pickup_Custom_Data implements Mbe_Shipping_Entity_Model_Interface { |
| 4 |
|
| 5 |
use \Traits\Mbe_Entity_Model_Trait { |
| 6 |
insertRow as protected traitInserRow; |
| 7 |
truncate as protected traitTruncate; |
| 8 |
tableExists as protected traitTableExists; |
| 9 |
insertRow as protected traitInsertRow; |
| 10 |
updateRow as protected traitUpdateRow; |
| 11 |
deleteRow as protected traitDeleteRow; |
| 12 |
} |
| 13 |
|
| 14 |
public function tableExists() { |
| 15 |
return $this->traitTableExists($this->getTableName()); |
| 16 |
} |
| 17 |
|
| 18 |
public function truncate() { |
| 19 |
return $this->traitTruncate($this->getTableName()); |
| 20 |
} |
| 21 |
|
| 22 |
public function updateRow($id, $row ) { |
| 23 |
return $this->traitUpdateRow($this->getTableName(), $id, $row); |
| 24 |
} |
| 25 |
|
| 26 |
public function deleteRow( $id ) { |
| 27 |
return $this->traitDeleteRow($this->getTableName(), $id); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Validate a row based on specific criteria |
| 32 |
* |
| 33 |
* @param array $item The row data to be validated |
| 34 |
* |
| 35 |
* @return true|string Returns true if the row is valid, or a string with error messages if the row is invalid |
| 36 |
*/ |
| 37 |
public function validate_row($item) { |
| 38 |
$helper = new Mbe_Shipping_Helper_Data(); |
| 39 |
$ws = new Mbe_Shipping_Model_Ws(); |
| 40 |
$messages = array(); |
| 41 |
|
| 42 |
// address exists |
| 43 |
$pickupAddress = $ws->getPickupAddressById($item['pickup_address_id']); |
| 44 |
if(empty($pickupAddress)) $messages[] = __("Please check that the address selected exists", 'mail-boxes-etc' ); |
| 45 |
|
| 46 |
// start time >= end time |
| 47 |
if(!$helper->checkStartEndTime($item['preferred_from'], $item['preferred_to'])) $messages[] = __("Maximum pickup preferred time should be greater than minimum", 'mail-boxes-etc' ); |
| 48 |
if(!$helper->checkStartEndTime($item['alternative_from'], $item['alternative_to'])) $messages[] = __("Maximum pickup alternative time should be greater than minimum", 'mail-boxes-etc' ); |
| 49 |
|
| 50 |
// Pickup date not in the past |
| 51 |
$pickupDate = new DateTime($item['date']); |
| 52 |
$today = new DateTime('today'); |
| 53 |
if($pickupDate < $today) $messages[] = __("Pickup date can't be set to the past", 'mail-boxes-etc' ); |
| 54 |
|
| 55 |
if (empty($messages)) return true; |
| 56 |
return implode('<br />', $messages); |
| 57 |
} |
| 58 |
|
| 59 |
// Override the trait to return the new row id |
| 60 |
public function insertRow( $row ) { |
| 61 |
global $wpdb; |
| 62 |
try { |
| 63 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 64 |
if (!$wpdb->insert($this->getTableName(), $row)) { |
| 65 |
$this->helper->setWpAdminMessages( [ |
| 66 |
'message' => urlencode( $wpdb->last_error ), |
| 67 |
'status' => urlencode( 'error' ) |
| 68 |
] ); |
| 69 |
\WC_Admin_Settings::add_error(__('Error while adding data to the table ', 'mail-boxes-etc') . $this->getTableName() . ' :' . $wpdb->last_error); |
| 70 |
return false; |
| 71 |
} |
| 72 |
} catch (\Exception $e) { |
| 73 |
$this->helper->setWpAdminMessages( [ |
| 74 |
'message' => urlencode( $e->getMessage() ), |
| 75 |
'status' => urlencode( 'error' ) |
| 76 |
] ); |
| 77 |
\WC_Admin_Settings::add_error(__('Unexpected error', 'mail-boxes-etc') . ': ' . $e->getMessage()); |
| 78 |
} |
| 79 |
return $wpdb->insert_id; |
| 80 |
} |
| 81 |
|
| 82 |
public function getTableName() { |
| 83 |
global $wpdb; |
| 84 |
return $wpdb->prefix . Mbe_Shipping_Helper_Data::MBE_PICKUP_CUSTOM_DATA_TABLE_NAME; |
| 85 |
} |
| 86 |
|
| 87 |
public function getSqlCreate() { |
| 88 |
global $wpdb; |
| 89 |
$charset = $wpdb->get_charset_collate(); |
| 90 |
|
| 91 |
return "CREATE TABLE " . $this->getTableName() . " ( |
| 92 |
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY , |
| 93 |
pickup_batch_id varchar(255) NULL, |
| 94 |
pickup_address_id varchar(256) NULL, |
| 95 |
cutoff varchar(20) NULL, |
| 96 |
preferred_from varchar(5) NULL, |
| 97 |
preferred_to varchar(5) NULL, |
| 98 |
alternative_from varchar(5) NULL, |
| 99 |
alternative_to varchar(5) NULL, |
| 100 |
notes text NULL, |
| 101 |
date date NULL, |
| 102 |
status varchar(20) DEFAULT 'ready' NOT NULL, |
| 103 |
INDEX " . $this->getTableName() . "_pickup_batch_id_index (pickup_batch_id) |
| 104 |
) $charset;"; |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
public function getRow( $rowId ) { |
| 109 |
global $wpdb; |
| 110 |
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $this->getTableName() . " where id = %d", $rowId), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Get the row for a specific pickupBatchId |
| 115 |
* @param $pickupBatchId |
| 116 |
*/ |
| 117 |
public function getBatchIdRow( $pickupBatchId ) { |
| 118 |
global $wpdb; |
| 119 |
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM %i where pickup_batch_id = %s", $this->getTableName(), $pickupBatchId )); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 120 |
} |
| 121 |
|
| 122 |
public function getBatchIdOrders( $pickupBatchId, $orderClause = '' ) { |
| 123 |
global $wpdb; |
| 124 |
$orderItemTable = $wpdb->prefix . 'woocommerce_order_items'; |
| 125 |
$orderItemMetaTable = $wpdb->prefix . 'woocommerce_order_itemmeta'; |
| 126 |
|
| 127 |
if ( \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled() ) { |
| 128 |
$postTable = $wpdb->prefix . 'wc_orders'; |
| 129 |
} else { |
| 130 |
$postTable = $wpdb->prefix . 'posts'; |
| 131 |
} |
| 132 |
|
| 133 |
$metakey = Mbe_Shipping_Helper_Data::META_FIELD_PICKUP_BATCH_ID; |
| 134 |
$orderSql = $orderClause?"ORDER BY $orderClause":""; |
| 135 |
|
| 136 |
$query = $wpdb->prepare("SELECT DISTINCT p.*,p.ID orderid FROM $postTable AS p |
| 137 |
INNER JOIN $orderItemTable AS oi ON p.id = oi.order_id |
| 138 |
LEFT JOIN $orderItemMetaTable AS oim ON oi.order_item_id = oim.order_item_id |
| 139 |
WHERE oi.order_item_type='shipping' |
| 140 |
AND oim.meta_key = %s |
| 141 |
AND oim.meta_value = %s |
| 142 |
$orderSql", |
| 143 |
array($metakey, $pickupBatchId) |
| 144 |
); |
| 145 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 146 |
return $wpdb->get_results($query, ARRAY_A); |
| 147 |
} |
| 148 |
|
| 149 |
public function getPickupIdOrders( $pickupCustomDataId, $orderClause = '' ) { |
| 150 |
global $wpdb; |
| 151 |
$orderItemTable = $wpdb->prefix . 'woocommerce_order_items'; |
| 152 |
$orderItemMetaTable = $wpdb->prefix . 'woocommerce_order_itemmeta'; |
| 153 |
|
| 154 |
if ( \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled() ) { |
| 155 |
$postTable = $wpdb->prefix . 'wc_orders'; |
| 156 |
} else { |
| 157 |
$postTable = $wpdb->prefix . 'posts'; |
| 158 |
} |
| 159 |
$metakey = Mbe_Shipping_Helper_Data::META_FIELD_PICKUP_CUSTOM_DATA_ID; |
| 160 |
$orderSql = $orderClause?"ORDER BY $orderClause":""; |
| 161 |
|
| 162 |
$query = $wpdb->prepare("SELECT DISTINCT p.*,p.ID orderid FROM $postTable AS p |
| 163 |
INNER JOIN $orderItemTable AS oi ON p.id = oi.order_id |
| 164 |
LEFT JOIN $orderItemMetaTable AS oim ON oi.order_item_id = oim.order_item_id |
| 165 |
WHERE oi.order_item_type='shipping' |
| 166 |
AND oim.meta_key = %s |
| 167 |
AND %s = oim.meta_value |
| 168 |
$orderSql", |
| 169 |
array($metakey, $pickupCustomDataId) |
| 170 |
); |
| 171 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 172 |
return $wpdb->get_results($query, ARRAY_A); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
public function getStatus( $pickupBatchId ) { |
| 177 |
$row = $this->getBatchIdRow( $pickupBatchId ); |
| 178 |
if($row) { |
| 179 |
return $row->status; |
| 180 |
} |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
public function setStatus( $rowId, $value = 'ready' ) { |
| 185 |
global $wpdb; |
| 186 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 187 |
$wpdb->update( |
| 188 |
$this->getTableName(), |
| 189 |
['status' => $value], |
| 190 |
['id' => $rowId] |
| 191 |
); |
| 192 |
} |
| 193 |
|
| 194 |
public function form_meta_box($item) { |
| 195 |
$helper = new Mbe_Shipping_Helper_Data(); |
| 196 |
require_once( MBE_ESHIP_PLUGIN_DIR . 'includes/class-mbe-csv-editor-pickup-addresses.php' ); |
| 197 |
$csvPickupAddresses = new Mbe_Shipping_Csv_Editor_Pickup_Addresses(); |
| 198 |
$tomorrow = new DateTime('tomorrow'); |
| 199 |
$today = new DateTime('today'); |
| 200 |
// $pickupBatchIdInput = isset($item['pickup_batch_id'])?'<input type="hidden" name="pickup_batch_id" id="pickup_batch_id" value="' . esc_attr($item['pickup_batch_id']) . '"/>':'' |
| 201 |
?> |
| 202 |
|
| 203 |
<table style="width: 100%;" class="form-table"> |
| 204 |
<tbody> |
| 205 |
<?php echo (isset($item['pickup_batch_id'])?'<input type="hidden" name="pickup_batch_id" id="pickup_batch_id" value="' . esc_attr($item['pickup_batch_id']) . '"/>':'') ?> |
| 206 |
<tr class="form-field"> |
| 207 |
<th scope="row"> |
| 208 |
<label for="address"><?php esc_html_e( 'Order list', 'mail-boxes-etc' ) ?></label> |
| 209 |
</th> |
| 210 |
<td> |
| 211 |
<div style="width: 95%; height: auto"> |
| 212 |
<?php |
| 213 |
foreach ( $item['order_ids'] as $order ) { |
| 214 |
?> |
| 215 |
<span style="font-size: smaller; float: left; color:#0a4b78; border: solid 1px #0a4b78; padding:4px ;text-align: center;border-radius: 5px; margin: 3px;"> |
| 216 |
<?php esc_attr_e($order)?> |
| 217 |
</span> |
| 218 |
<?php |
| 219 |
} |
| 220 |
?> |
| 221 |
</div> |
| 222 |
</td> |
| 223 |
</tr> |
| 224 |
<tr class="form-field"> |
| 225 |
<th scope="row"> |
| 226 |
<label for="address"><?php esc_html_e( 'Pickup address', 'mail-boxes-etc' ) ?></label> |
| 227 |
<span style="color: #c03939">*</span> |
| 228 |
</th> |
| 229 |
<td> |
| 230 |
<select id="pickup_address_id" name="pickup_address_id"> |
| 231 |
<?php |
| 232 |
foreach ( $csvPickupAddresses->get_remoteRows() as $address ) { |
| 233 |
?> |
| 234 |
<option value="<?php esc_attr_e($address['pickup_address_id']) ?>" <?php echo $address['pickup_address_id']==($item['pickup_address_id']??'')?"selected":"" ?>> |
| 235 |
<?php esc_html_e($address['trade_name'] .', ' . $address['address_1'] .', ' . $address['zip_code'] .', ' . $address['city']) ?> |
| 236 |
</option> |
| 237 |
<?php |
| 238 |
} |
| 239 |
?> |
| 240 |
</select> |
| 241 |
<p style="margin-top:5px; margin-bottom: 5px;"> |
| 242 |
<?php |
| 243 |
$returnLink = urlencode(MBE_ESHIP_ID . "_pickup_data_tabs&orderids=".json_encode($item['order_ids'])."&nonce=".wp_create_nonce('mbe_pickup_return_from_address')); |
| 244 |
$hrefUrl = get_admin_url(null, "admin.php?backpage=".$returnLink."&page=" . MBE_ESHIP_ID . "_csv_edit_form&action=new&csv=pickup-addresses&id=&nonce=".wp_create_nonce('csv_form_page_handler')); |
| 245 |
echo wp_kses_post(sprintf( '<a class="button" href="%s">%s</a>',$hrefUrl, __('Add New Pickup Address', 'mail-boxes-etc'))); |
| 246 |
?> |
| 247 |
</p> |
| 248 |
</td> |
| 249 |
</tr> |
| 250 |
<!-- Pickup Date--> |
| 251 |
<tr class="form-field"> |
| 252 |
<th scope="row"> |
| 253 |
<label for="date"><?php esc_html_e( 'Pickup Date', 'mail-boxes-etc' ) ?></label> |
| 254 |
<span style="color: #c03939">*</span> |
| 255 |
</th> |
| 256 |
<td> |
| 257 |
<input id="date" name="date" type="date" style="width: 95%" |
| 258 |
value="<?php esc_html_e($item['date']??$tomorrow->format('Y-m-d')) ?>" |
| 259 |
min="<?php esc_html_e($today->format('Y-m-d')) ?>" |
| 260 |
required> |
| 261 |
<p class="description"> |
| 262 |
<?php esc_html_e('Pickup for PUDO shipments will be booked automatically after shipments closure for the next working day', 'mail-boxes-etc') ?> |
| 263 |
</p> |
| 264 |
</td> |
| 265 |
</tr> |
| 266 |
<!-- Preferred time--> |
| 267 |
<tr class="form-field"> |
| 268 |
<th scope="row"> |
| 269 |
<label for="preferred"> |
| 270 |
<span><?php esc_html_e('Pickup Time - Preferred', 'mail-boxes-etc') ?></span> |
| 271 |
</label> |
| 272 |
<span style="color: #c03939">*</span> |
| 273 |
</th> |
| 274 |
<td class="forminp forminp-time"> |
| 275 |
<fieldset> |
| 276 |
<legend class="screen-reader-text"><span><?php __('Pickup Time - Preferred from', 'mail-boxes-etc') ?></span></legend> |
| 277 |
<input class="input-text regular-input" |
| 278 |
type="time" name="preferred_from" |
| 279 |
id="preferred_from" |
| 280 |
value="<?php echo esc_attr($item['preferred_from']) ?>" |
| 281 |
/> |
| 282 |
<legend class="screen-reader-text"><span><?php __('Pickup Time - Preferred to', 'mail-boxes-etc') ?></span></legend> |
| 283 |
<input class="input-text regular-input" |
| 284 |
type="time" name="preferred_to" |
| 285 |
id="preferred_to" |
| 286 |
value="<?php echo esc_attr($item['preferred_to']) ?>" |
| 287 |
/> |
| 288 |
<p class="description"> |
| 289 |
<?php esc_html_e('Minimum and maximum pickup time that will be communicated to the courier(N.B. pickup time is approximate and may not be observed by the final courier)', 'mail-boxes-etc') ?> |
| 290 |
</p> |
| 291 |
</fieldset> |
| 292 |
</td> |
| 293 |
</tr> |
| 294 |
<!-- Alternative time--> |
| 295 |
<tr class="form-field"> |
| 296 |
<th scope="row"> |
| 297 |
<label for="alternative"> |
| 298 |
<span><?php esc_html_e('Pickup Time - Alternative', 'mail-boxes-etc') ?></span> |
| 299 |
</label> |
| 300 |
</th> |
| 301 |
|
| 302 |
<td class="forminp forminp-time"> |
| 303 |
<fieldset> |
| 304 |
<legend class="screen-reader-text"><span><?php __('Pickup Time - Alternative from', 'mail-boxes-etc') ?></span></legend> |
| 305 |
<input class="input-text regular-input" |
| 306 |
type="time" name="alternative_from" |
| 307 |
id="alternative_from" |
| 308 |
value="<?php echo esc_attr($item['alternative_from']) ?>" |
| 309 |
/> |
| 310 |
<legend class="screen-reader-text"><span><?php __('Pickup Time - Alternative to', 'mail-boxes-etc') ?></span></legend> |
| 311 |
<input class="input-text regular-input" |
| 312 |
type="time" name="alternative_to" |
| 313 |
id="alternative_to" |
| 314 |
value="<?php echo esc_attr($item['alternative_to']) ?>" |
| 315 |
/> |
| 316 |
<p class="description"> |
| 317 |
<?php esc_html_e('Alternative minimum and maximum pickup time that will be communicated to the courier (N.B. pickup time is approximate and may not be observed by the final courier)', 'mail-boxes-etc') ?> |
| 318 |
</p> |
| 319 |
</fieldset> |
| 320 |
</td> |
| 321 |
</tr> |
| 322 |
<!-- Note --> |
| 323 |
<tr class="form-field"> |
| 324 |
<th scope="row"> |
| 325 |
<label for="notes"><?php esc_html_e( 'Pickup notes', 'mail-boxes-etc' ) ?></label> |
| 326 |
</th> |
| 327 |
<td> |
| 328 |
<input id="notes" name="notes" type="text" style="width: 95%" |
| 329 |
value="<?php echo esc_attr( $item['notes'] ) ?>" |
| 330 |
required> |
| 331 |
</td> |
| 332 |
</tr> |
| 333 |
|
| 334 |
<tr class="form-field"> |
| 335 |
<th></th> |
| 336 |
<td> |
| 337 |
<div style="float:right;"> |
| 338 |
<?php /* |
| 339 |
if( Mbe_Shipping_Helper_Data::MBE_PICKUP_REQUEST_MANUAL === $helper->getPickupRequestMode() && |
| 340 |
Mbe_Shipping_Helper_Data::MBE_CREATION_MODE_MANUALLY === $helper->getShipmentsCreationMode() && |
| 341 |
(count($item['order_ids']) > 1 || !empty($item['pickup_batch_id'])) |
| 342 |
) { |
| 343 |
*/ ?> |
| 344 |
<input type="submit" value="<?php esc_html_e( 'Save', 'mail-boxes-etc' ) ?>" id="submit_save" |
| 345 |
class="button-primary" name="submit_save"> |
| 346 |
<?php /* } */?> |
| 347 |
<?php if($helper->getPickupRequestEnabled()) { ?> |
| 348 |
<input type="submit" value="<?php esc_html_e( 'Save and send', 'mail-boxes-etc' ) ?>" id="submit_savesend" |
| 349 |
class="button-primary" name="submit_savesend"> |
| 350 |
<?php } ?> |
| 351 |
</div> |
| 352 |
</td> |
| 353 |
|
| 354 |
</tr> |
| 355 |
</tbody> |
| 356 |
</table> |
| 357 |
|
| 358 |
|
| 359 |
<?php |
| 360 |
} |
| 361 |
|
| 362 |
} |