array( 'accepted', 'rejected', 'cancelled_diner' ), 'accepted' => array( 'preparing', 'ready', 'cancelled_restaurant' ), 'preparing' => array( 'ready', 'cancelled_restaurant' ), 'ready' => array( 'collected', 'not_collected', 'cancelled_restaurant' ), ); const SOFRAH_BOARD_NEEDS_REASON = array( 'rejected', 'cancelled_restaurant' ); /** * Status names as staff and diners see them. * * @param string $status Status key. * @return string */ function sofrah_board_status_label( $status ) { $labels = array( 'awaiting' => __( 'Waiting for the restaurant', 'sofrah-order-board' ), 'accepted' => __( 'Accepted', 'sofrah-order-board' ), 'preparing' => __( 'Being prepared', 'sofrah-order-board' ), 'ready' => __( 'Ready to collect', 'sofrah-order-board' ), 'collected' => __( 'Collected', 'sofrah-order-board' ), 'not_collected' => __( 'Not collected', 'sofrah-order-board' ), 'rejected' => __( 'Not accepted', 'sofrah-order-board' ), 'cancelled_diner' => __( 'Cancelled by you', 'sofrah-order-board' ), 'cancelled_restaurant' => __( 'Cancelled by the restaurant', 'sofrah-order-board' ), ); return $labels[ $status ] ?? $status; } /** * Button text on the order board for moving an order to a status. * * @param string $status Target status. * @return string */ function sofrah_board_action_label( $status ) { $labels = array( 'accepted' => __( 'Accept', 'sofrah-order-board' ), 'rejected' => __( 'Reject', 'sofrah-order-board' ), 'preparing' => __( 'Preparing', 'sofrah-order-board' ), 'ready' => __( 'Ready', 'sofrah-order-board' ), 'collected' => __( 'Collected', 'sofrah-order-board' ), 'not_collected' => __( 'Not collected', 'sofrah-order-board' ), 'cancelled_restaurant' => __( 'Cancel order', 'sofrah-order-board' ), ); return $labels[ $status ] ?? $status; } /** * Whether a payment method means pay at pickup. * * @param string $method Payment method ID. * @return bool */ function sofrah_board_is_pickup_method( $method ) { return in_array( (string) $method, (array) sofrah_board_setting( 'methods' ), true ); } /** * Whether an order is handled on the order board. * * @param mixed $order Order. * @return bool */ function sofrah_board_is_board_order( $order ) { return $order instanceof WC_Order && ! ( $order instanceof WC_Order_Refund ) && '' !== (string) $order->get_meta( '_sofrah_board_status' ); } /** * The order's status on the board. * * @param WC_Order $order Order. * @return string */ function sofrah_board_get_status( WC_Order $order ) { return (string) $order->get_meta( '_sofrah_board_status' ); } /** * Whether a board order is still open. * * @param mixed $order Order. * @return bool */ function sofrah_board_is_open( $order ) { return sofrah_board_is_board_order( $order ) && in_array( sofrah_board_get_status( $order ), SOFRAH_BOARD_OPEN, true ); } /** * The WooCommerce status that matches a board status. * * @param string $status Board status. * @return string */ function sofrah_board_expected_wc_status( $status ) { if ( in_array( $status, SOFRAH_BOARD_OPEN, true ) ) { return 'on-hold'; } return 'collected' === $status ? 'completed' : 'cancelled'; } // Offline "pay at pickup" orders wait as on-hold, not processing: nothing is paid yet. add_filter( 'woocommerce_cod_process_payment_order_status', function ( $status, $order = null ) { return ( $order instanceof WC_Order && sofrah_board_is_pickup_method( $order->get_payment_method() ) ) ? 'on-hold' : $status; }, 10, 2 ); // Placement: the moment a pickup order leaves checkout it becomes a board order, waiting for the restaurant. add_action( 'woocommerce_before_order_object_save', function ( $order ) { if ( ! ( $order instanceof WC_Order ) || $order instanceof WC_Order_Refund || sofrah_board_is_board_order( $order ) ) { return; } $changes = $order->get_changes(); $from = $order->get_data()['status']; if ( isset( $changes['status'] ) && 'on-hold' === $changes['status'] && in_array( $from, array( 'pending', 'checkout-draft', 'auto-draft', '' ), true ) && sofrah_board_is_pickup_method( $order->get_payment_method() ) ) { $order->update_meta_data( '_sofrah_board_status', 'awaiting' ); $order->update_meta_data( '_sofrah_board_placed_at', time() ); $order->update_meta_data( '_sofrah_board_history', array( array( 'at' => time(), 'by' => 'diner', 'from' => '', 'to' => 'awaiting', 'reason' => '' ) ) ); do_action( 'sofrah_board_order_placed', $order ); } }, 5 ); /** * The only way a board order's status changes. One change at a time per order; the first action wins. * * @param int $order_id Order ID. * @param string $to Target status. * @param string $by Who acted: a user login, or "diner". * @param string $reason Reason, required for rejection and cancellation by the restaurant. * @param int $minutes Minutes until pickup, for acceptance. * @return true|WP_Error */ function sofrah_board_transition( $order_id, $to, $by, $reason = '', $minutes = 0 ) { global $wpdb; $lock = 'sofrah_board_order_' . (int) $order_id; if ( '1' !== (string) $wpdb->get_var( $wpdb->prepare( 'SELECT GET_LOCK(%s, 5)', $lock ) ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- a database lock, one change at a time per order. return new WP_Error( 'sofrah_board_busy', __( 'Someone else is changing this order. Try again.', 'sofrah-order-board' ), array( 'status' => 409 ) ); } try { return sofrah_board_transition_locked( $order_id, $to, $by, $reason, $minutes ); } finally { $wpdb->query( $wpdb->prepare( 'SELECT RELEASE_LOCK(%s)', $lock ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- releases the lock above. } } /** * Drops this request's cached copy of an order, so the next read comes from the database. * * @param int $order_id Order ID. */ function sofrah_board_forget_cached_order( $order_id ) { $cache = '\Automattic\WooCommerce\Caches\OrderCache'; if ( function_exists( 'wc_get_container' ) && class_exists( $cache ) ) { try { wc_get_container()->get( $cache )->remove( $order_id ); } catch ( Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch -- no cache to clear. } } clean_post_cache( $order_id ); wp_cache_delete( $order_id, 'orders_meta' ); } /** * The board status as stored in the database right now, whichever order storage the site uses. * * @param int $order_id Order ID. * @return string */ function sofrah_board_stored_status( $order_id ) { global $wpdb; $hpos = class_exists( '\Automattic\WooCommerce\Utilities\OrderUtil' ) && \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled(); if ( $hpos ) { return (string) $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM %i WHERE order_id = %d AND meta_key = '_sofrah_board_status' ORDER BY id DESC LIMIT 1", $wpdb->prefix . 'wc_orders_meta', $order_id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- must bypass every cache. } return (string) $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM %i WHERE post_id = %d AND meta_key = '_sofrah_board_status' ORDER BY meta_id DESC LIMIT 1", $wpdb->postmeta, $order_id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- must bypass every cache. } /** * Applies a change while the order's lock is held. * * @param int $order_id Order ID. * @param string $to Target status. * @param string $by Who acted. * @param string $reason Reason. * @param int $minutes Minutes until pickup. * @return true|WP_Error */ function sofrah_board_transition_locked( $order_id, $to, $by, $reason, $minutes ) { sofrah_board_forget_cached_order( $order_id ); // This request may have loaded the order before another request changed it. $order = wc_get_order( $order_id ); if ( ! sofrah_board_is_board_order( $order ) ) { return new WP_Error( 'sofrah_board_not_found', __( 'This order is not on the order board.', 'sofrah-order-board' ), array( 'status' => 404 ) ); } $from = sofrah_board_get_status( $order ); $fresh = sofrah_board_stored_status( $order_id ); if ( $fresh !== $from ) { $from = $fresh; // Never act on a stale copy: the check below then refuses with the real current status. } if ( ! in_array( $to, SOFRAH_BOARD_ALLOWED[ $from ] ?? array(), true ) ) { /* translators: 1: current status, 2: requested status */ return new WP_Error( 'sofrah_board_refused', sprintf( __( 'This order is already "%1$s", so it cannot be moved to "%2$s".', 'sofrah-order-board' ), sofrah_board_status_label( $from ), sofrah_board_status_label( $to ) ), array( 'status' => 409, 'current' => $from ) ); } $reason = trim( wp_strip_all_tags( (string) $reason ) ); if ( in_array( $to, SOFRAH_BOARD_NEEDS_REASON, true ) && '' === $reason ) { return new WP_Error( 'sofrah_board_reason', __( 'Give a reason. The diner will see it.', 'sofrah-order-board' ), array( 'status' => 400 ) ); } if ( 'accepted' === $to ) { $minutes = $minutes > 0 ? min( 480, (int) $minutes ) : (int) sofrah_board_setting( 'lead_minutes' ); $order->update_meta_data( '_sofrah_board_pickup_at', time() + 60 * $minutes ); } if ( $reason ) { $order->update_meta_data( '_sofrah_board_reason', $reason ); } $history = (array) $order->get_meta( '_sofrah_board_history' ); $history[] = array( 'at' => time(), 'by' => $by, 'from' => $from, 'to' => $to, 'reason' => $reason ); $order->update_meta_data( '_sofrah_board_history', $history ); $order->update_meta_data( '_sofrah_board_status', $to ); $GLOBALS['sofrah_board_in_transition'] = true; try { $wc = sofrah_board_expected_wc_status( $to ); if ( $wc !== $order->get_status() ) { /* translators: 1: board status, 2: reason or empty */ $order->set_status( $wc, sprintf( __( 'Order board: %1$s. %2$s', 'sofrah-order-board' ), sofrah_board_status_label( $to ), $reason ) ); } $order->save(); } finally { $GLOBALS['sofrah_board_in_transition'] = false; } do_action( 'sofrah_board_transitioned', $order->get_id(), $to, $from ); return true; } /** * Records payment taken at pickup. Information only: it never changes the order's status. * * @param int $order_id Order ID. * @param string $by User login. * @return true|WP_Error */ function sofrah_board_mark_paid( $order_id, $by ) { $order = wc_get_order( $order_id ); if ( ! sofrah_board_is_board_order( $order ) ) { return new WP_Error( 'sofrah_board_not_found', __( 'This order is not on the order board.', 'sofrah-order-board' ), array( 'status' => 404 ) ); } if ( $order->get_meta( '_sofrah_board_paid_at' ) ) { return true; } $order->update_meta_data( '_sofrah_board_paid_at', time() ); $order->update_meta_data( '_sofrah_board_paid_by', $by ); $GLOBALS['sofrah_board_in_transition'] = true; try { $order->set_date_paid( time() ); // WooCommerce's paid date follows the board, never the other way round. $order->save(); } finally { $GLOBALS['sofrah_board_in_transition'] = false; } return true; } // Not collected: the food was made, so nothing goes back into stock. add_filter( 'woocommerce_can_restore_order_stock', function ( $can, $order ) { return ( sofrah_board_is_board_order( $order ) && 'not_collected' === sofrah_board_get_status( $order ) ) ? false : $can; }, 10, 2 );