diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-delivery-process.php b/backend/app/plugins/wiaas/includes/class-wiaas-delivery-process.php index c3a526c..c8a0a8a 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-delivery-process.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-delivery-process.php @@ -23,6 +23,8 @@ class Wiaas_Delivery_Process { add_filter( 'gform_entry_meta', array(__CLASS__, 'extend_gravity_form_entry_meta'), 10, 2 ); add_action( 'gravityflow_workflow_complete', array(__CLASS__, 'maybe_complete_parent_process_step'), 5, 3 ); + + add_action( 'gravityflow_workflow_complete', array(__CLASS__, 'maybe_complete_parent_order'), 10, 3 ); } /** @@ -34,6 +36,29 @@ class Wiaas_Delivery_Process { Gravity_Flow_Steps::register( new Wiaas_Delivery_Process_Step() ); } + /** + * Maybe complete parent order for completed delivery process + * @param $entry_id + * @param $form + */ + public static function maybe_complete_parent_order($entry_id, $form) { + $entry = GFAPI::get_entry($entry_id); + + $order_id = $entry['wiaas_delivery_order_id']; + + if (!isset($order_id)) { + return; + } + + $process_entry_id = get_post_meta($order_id, 'wiaas_delivery_process_entry_id', true); + + // order process entry completed, so complete order + if (absint($process_entry_id) === $entry_id) { + $order = wc_get_order($order_id); + $order->update_status('completed', 'Completed order delivery process.', true); + } + } + /** * Retrieves delivery process instance for order * diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-order.php b/backend/app/plugins/wiaas/includes/class-wiaas-order.php index 816a168..06387ed 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-order.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-order.php @@ -18,8 +18,19 @@ class Wiaas_Order { add_filter('woocommerce_rest_prepare_shop_order_object', array(__CLASS__, 'transform_rest_order'), 999, 3); add_filter('woocommerce_rest_orders_prepare_object_query', array( __CLASS__, 'wiaas_prepare_rest_orders_query'), 10, 2); + + add_filter('woocommerce_new_order_note_data', array( __CLASS__, 'update_new_order_comment_date'), 10, 3); } + public static function update_new_order_comment_date($comment_data, $order_data) { + $user = wp_get_current_user(); + + $comment_data['comment_author'] = $user->display_name; + $comment_data['comment_author_email'] = $user->user_email; + + return $comment_data; + } + /** * Assignees order to corresponding user organization when order is created. * @@ -94,6 +105,8 @@ class Wiaas_Order { $data = self::_append_commercial_lead_info($data, $order, $request); $data = self::_append_wiaas_order_details($data, $order, $request); + + $data = self::_append_order_comments($data, $order, $request); $response->set_data($data); @@ -229,6 +242,34 @@ class Wiaas_Order { return $data; } + + /** Append order comments if single order is requested + * @param $data + * @param $order + * @param $request + */ + private static function _append_order_comments($data, $order, $request) { + + if (isset($request['id'])) { + $current_user = wp_get_current_user(); + + $comments = $order->get_customer_order_notes(); + + $data['comments'] = array(); + + foreach ($comments as $comment) { + $data['comments'][] = array( + 'id' => $comment->comment_ID, + 'content' => $comment->comment_content, + 'username' => $comment->comment_author, + 'date' => $comment->comment_date, + 'is_owner' => $comment->comment_author === $current_user->display_name, + ); + } + } + + return $data; + } } Wiaas_Order::init(); diff --git a/frontend/src/actions/orders/processActions.js b/frontend/src/actions/orders/processActions.js index e45d409..87bef48 100644 --- a/frontend/src/actions/orders/processActions.js +++ b/frontend/src/actions/orders/processActions.js @@ -62,27 +62,16 @@ const sendComment = () => ({ type: SEND_ORDER_COMMENT }) -export const addComment = (idOrder, newComment, existingComments) => { +export const addComment = (idOrder, newComment) => { return dispatch => { dispatch(sendComment()); - //TODO: get real user name and store in comment object - const newCommentObject = { - comment : newComment, - date : moment().format("Do MMM, YY"), - username: localStorage.getItem('username') || '-', - } - const concatenatedComments = (existingComments) ? existingComments.concat(newCommentObject) : [newCommentObject]; return htmlClient.fetch({ - url: `${API_SERVER}/wp-json/wc/v2/orders/${idOrder}`, - method: 'put', + url: `${API_SERVER}/wp-json/wc/v2/orders/${idOrder}/notes`, + method: 'post', data: { - meta_data: [ - { - key:'comments', - value: JSON.stringify(concatenatedComments), - } - ] + note : newComment || 'Test comment', + customer_note: true, } }) .then(response => { diff --git a/frontend/src/containers/orders/components/OrderComments.jsx b/frontend/src/containers/orders/components/OrderComments.jsx index 9717e42..51a7588 100644 --- a/frontend/src/containers/orders/components/OrderComments.jsx +++ b/frontend/src/containers/orders/components/OrderComments.jsx @@ -18,19 +18,19 @@ class OrderComments extends Component { } addNewComment(){ - this.props.dispatch(addComment(this.props.orderInfo.id, this.state.newComment, this.props.orderComments)); + this.props.dispatch(addComment(this.props.orderInfo.id, this.state.newComment)); } onEditorChange(newComment){ this.setState({newComment}); } - getOffset(username){ - return (username === localStorage.getItem('username')) ? 6 : 0; + getOffset(isOwner){ + return isOwner ? 6 : 0; } - getClassByOwner(username){ - return (username === localStorage.getItem('username')) ? 'mine' : ''; + getClassByOwner(isOwner){ + return isOwner ? 'mine' : ''; } render() { @@ -46,9 +46,9 @@ class OrderComments extends Component { orderComments && orderComments.map((orderComment, index) => - -
-
{orderComment.username} - {orderComment.date}
+ +
+
{orderComment.username} - {orderComment.dateCreated}
diff --git a/frontend/src/containers/orders/style/ProcessContainer.scss b/frontend/src/containers/orders/style/ProcessContainer.scss index 225f339..3e4d82e 100644 --- a/frontend/src/containers/orders/style/ProcessContainer.scss +++ b/frontend/src/containers/orders/style/ProcessContainer.scss @@ -26,6 +26,7 @@ $link-line-height: 1.5rem; .completed { border-left: $border-width $production-status-color solid; + background: $production-status-color; } .end-of-life { @@ -131,7 +132,7 @@ $link-line-height: 1.5rem; background: $canceled-status-color; } -.production { +.completed { background: $production-status-color; } diff --git a/frontend/src/helpers/OrderHelper.js b/frontend/src/helpers/OrderHelper.js index 202f4a6..e9f50ff 100644 --- a/frontend/src/helpers/OrderHelper.js +++ b/frontend/src/helpers/OrderHelper.js @@ -9,13 +9,6 @@ function formatAddress(addressObject) { return `${addressObject.address_1}, ${addressObject.city}, ${addressObject.country}, ${addressObject.postcode}`; } -function extractComments(metaDataArray){ - const commentsObject = metaDataArray.find(metaDataElement => { - return metaDataElement.key === "comments"; - }); - return commentsObject ? JSON.parse(commentsObject.value) : []; -} - export const fromWCOrder = (WCOrder) => { let processInfo = Object.assign({},WCOrder['delivery-process']); if (WCOrder['delivery-process']){ @@ -59,7 +52,13 @@ export const fromWCOrder = (WCOrder) => { }; }), process: processInfo, - comments: extractComments(WCOrder.meta_data), + comments: WCOrder.comments ? WCOrder.comments.map(comment => ({ + id: comment.id, + comment: comment.content, + username: comment.username, + dateCreated: formatDate(comment.date), + isOwner: comment['is_owner'] + })) : [], deliveryAddress: formatAddress(WCOrder.shipping), customer: WCOrder.customer, commercialLead: WCOrder['commercial_lead']