From 3dcef847099e96790aefe76b572bedf736d52ea5 Mon Sep 17 00:00:00 2001 From: Almira Krdzic Date: Mon, 5 Nov 2018 08:48:25 +0100 Subject: [PATCH] Persist workflow entry fields to order --- .../wiaas/includes/class-wiaas-order.php | 10 + .../class-wiaas-delivery-process-addon.php | 68 ------- ...-wiaas-field-order-installation-select.php | 16 +- .../class-wiaas-order-fields.php | 184 ++++++++++++++++++ .../orders/components/OrderDocuments.jsx | 18 +- .../orders/components/OrderDocumentsGroup.jsx | 32 ++- .../components/process/CustomerAcceptance.jsx | 2 +- .../process/ValidateQuestionnaireItem.jsx | 4 +- frontend/src/helpers/OrderHelper.js | 5 +- 9 files changed, 253 insertions(+), 86 deletions(-) diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-order.php b/backend/app/plugins/wiaas/includes/class-wiaas-order.php index 868eee1..a5556ad 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-order.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-order.php @@ -598,11 +598,21 @@ class Wiaas_Order { if (wiaas_is_order_item__standard_package($order_item)) { $documents = wiaas_get_standard_package_order_item_documents($order, $product_line['id']); + + if (! empty($order_item['wiaas_delivery_documents'])) { + + $documents = array_merge($documents, $order_item['wiaas_delivery_documents']); + } + $data['line_items'][$index] ['documents'] = $documents; } } + $order_delivery_documents = $order->get_meta('wiaas_delivery_documents', true); + $order_delivery_documents = empty($order_delivery_documents) ? array() : $order_delivery_documents; + $data['documents'] = $order_delivery_documents; + return $data; } } diff --git a/backend/app/plugins/wiaas/includes/delivery-process/class-wiaas-delivery-process-addon.php b/backend/app/plugins/wiaas/includes/delivery-process/class-wiaas-delivery-process-addon.php index 34f8e6d..55f2f07 100644 --- a/backend/app/plugins/wiaas/includes/delivery-process/class-wiaas-delivery-process-addon.php +++ b/backend/app/plugins/wiaas/includes/delivery-process/class-wiaas-delivery-process-addon.php @@ -24,74 +24,6 @@ class Wiaas_Delivery_Process_Addon extends Gravity_Flow_Extension { return self::$_instance; } - public function init() { - - parent::init(); - - add_filter('gravityflow_get_users_args', array ($this, 'allow_only_administrator_to_be_assigned_to_step')); - - add_filter('gravityflow_assignee_choices', array ($this, 'add_orders_assignee_choices')); - - add_filter('gravityflow_step_assignees', array ($this, 'maybe_assign_organization_to_step'), 10, 2); - } - - /** - * Handle organization assignee for step (Map users from organization as assignees to step) - * - * @param $assignees - * @param Gravity_Flow_Step $step - * - * @return array - */ - public function maybe_assign_organization_to_step($assignees, Gravity_Flow_Step $step) { - - $mapped_assignees = array(); - - foreach ($assignees as $assignee) { - - if (strpos($assignee->get_type(), 'wiaas_organization_order_') !== false) { - - $organization_id = $assignee->get_id(); - - $user_ids = wiaas_get_organization_user_ids($organization_id); - - if (empty($user_ids)) { - - continue; - } - - $user_id = $user_ids[0]; - - $mapped_assignees[] = $step->get_assignee( array( - 'id' => $user_id, - 'type' => 'user_id', - 'editable_fields' => $assignee->get_editable_fields() - ) ); - - continue; - } - - $mapped_assignees[] = $assignee; - } - - return $mapped_assignees; - } - - - public function allow_only_administrator_to_be_assigned_to_step($args) { - - $args['role'] = 'administrator'; - - //$args['exclude'] = array( 1 ); // exclude super admin user - - return $args; - } - - public static function add_orders_assignee_choices($choices) { - - return $choices; - } - /** * Extends Gravity Form entry metadata with 'wiaas_delivery_process_id' * diff --git a/backend/app/plugins/wiaas/includes/delivery-process/class-wiaas-field-order-installation-select.php b/backend/app/plugins/wiaas/includes/delivery-process/class-wiaas-field-order-installation-select.php index f19967a..da189cf 100644 --- a/backend/app/plugins/wiaas/includes/delivery-process/class-wiaas-field-order-installation-select.php +++ b/backend/app/plugins/wiaas/includes/delivery-process/class-wiaas-field-order-installation-select.php @@ -91,8 +91,7 @@ class Wiaas_Order_Installation_Select extends GF_Field_Select { return $this->get_selected_installation_display_name($value); } - - public function get_selected_installation_display_name($value) { + public function get_selected_installation($value) { $value = $value ? str_replace('wiaas_installation_', '', $value) : ''; @@ -100,12 +99,17 @@ class Wiaas_Order_Installation_Select extends GF_Field_Select { if (! empty($order_id) && ! empty($item_id) && $order = wc_get_order($order_id)) { - $item = $order->get_item($item_id); - - return $item->get_name(); + return $order->get_item($item_id); } - return ''; + return null; + } + + public function get_selected_installation_display_name($value) { + + $item = $this->get_selected_installation($value); + + return ! empty($item) ? $item->get_name() : ''; } public function post_convert_field() { diff --git a/backend/app/plugins/wiaas/includes/delivery-process/class-wiaas-order-fields.php b/backend/app/plugins/wiaas/includes/delivery-process/class-wiaas-order-fields.php index 35168fe..e59a77e 100644 --- a/backend/app/plugins/wiaas/includes/delivery-process/class-wiaas-order-fields.php +++ b/backend/app/plugins/wiaas/includes/delivery-process/class-wiaas-order-fields.php @@ -11,8 +11,192 @@ class Wiaas_Order_Fields { add_action( 'gform_field_standard_settings', array( __CLASS__, 'field_settings' ) ); add_filter('gform_fileupload_entry_value_file_path', array( __CLASS__, 'display_order_document_fields' ), 999, 2); + + add_action('gform_post_update_entry', array(__CLASS__, 'maybe_map_entry_to_order'), 10, 2); + + add_action('gform_after_update_entry', array(__CLASS__, 'maybe_apply_user_input_changes'), 10, 3); } + /** + * Workflow user input are handled separately in gravity flow. We get them here + * and then try to apply changes to order + * + * @param $form + * @param $entry_id + * @param $original_entry + */ + public static function maybe_apply_user_input_changes($form, $entry_id, $original_entry) { + + $entry = GFAPI::get_entry($entry_id); + + self::maybe_map_entry_to_order($entry, $original_entry); + } + + + /** + * Persist workflow entry change to order + * + * @param $entry + * @param $old_entry + */ + public static function maybe_map_entry_to_order($entry, $old_entry) { + + $form = GFAPI::get_form($old_entry['form_id']); + + $order_field = GFCommon::get_fields_by_type($form, 'wiaas_order')[0]; + + if (empty($order_field)) { + + return; + } + + $order_id = $entry[$order_field->id]; + + // get order process entry + $order = wc_get_order($order_id); + $process_entry = Wiaas_Delivery_Process::get_order_delivery_process_entry($order_id); + + if ( !$order || empty($process_entry) ) { + + return; + } + + // apply entry changes to order properties + foreach ($form['fields'] as $field) { + + $old_value = $old_entry[$field->id]; + $new_value = $entry[$field->id]; + + // check if field changed + if ($old_value === $new_value || empty($new_value)) { + + continue; + } + + // save changes to order + switch ($field->type) { + + case 'wiaas_order_bundle_document': + /** + * Persist delivery flow documents for bundle + */ + + // get corresponding bundle field + $bundle_field = GFCommon::get_fields_by_type( $form, 'wiaas_order_bundle' )[0]; + + if (empty($bundle_field)) { + + continue; + } + + $bundle_item = $bundle_field->get_bundle_item( $entry[$bundle_field->id] ); + + if (empty($bundle_item)) { + + continue; + } + + $bundle_documents = $bundle_item['wiaas_delivery_documents']; + if (empty($bundle_documents)) { + $bundle_documents = array(); + } + + $new_documents = $field->multipleFiles ? json_decode( $new_value ) : array( $new_value ); + if (! empty($old_value) ) { + + $old_documents = $field->multipleFiles ? json_decode( $old_value ) : array( $old_value ); + $added_documents = array_diff($new_documents, $old_documents); + } else { + + $added_documents = $new_documents; + } + + + foreach ($added_documents as $added_document) { + $info = pathinfo( $added_document ); + + $bundle_documents[] = array( + 'name' => $info['basename'], + 'extension' => $info['extension'], + 'url' => $field->get_download_url( $added_document, true ), + 'type' => $field->wiaasDocTypeFilter + ); + } + + $bundle_item->update_meta_data('wiaas_delivery_documents', $bundle_documents); + $bundle_item->save_meta_data(); + + break; + + case 'wiaas_order_document': + /** + * Persist delivery flow documents for order + */ + $new_documents = $field->multipleFiles ? json_decode( $new_value ) : array( $new_value ); + + if (! empty($old_value) ) { + + $old_documents = $field->multipleFiles ? json_decode( $old_value ) : array( $old_value ); + $added_documents = array_diff($new_documents, $old_documents); + } else { + + $added_documents = $new_documents; + } + + $order_documents = $order->get_meta('wiaas_delivery_documents', true); + if (empty($order_documents)) { + $order_documents = array(); + } + + foreach ($added_documents as $added_document) { + $info = pathinfo( $added_document ); + + $order_documents[] = array( + 'name' => $info['basename'], + 'extension' => $info['extension'], + 'url' => $field->get_download_url( $added_document, true ), + 'type' => $field->wiaasDocTypeFilter + ); + } + + $order->update_meta_data('wiaas_delivery_documents', $order_documents); + $order->save_meta_data(); + + break; + + case 'wiaas_order_installation_select': + /** + * Persist installation for bundle + */ + + $selected_installation = $field->get_selected_installation($new_value); + + if (empty($selected_installation)) { + // no installation selected + continue; + } + + // get corresponding bundle field + $bundle_field = GFCommon::get_fields_by_type( $form, 'wiaas_order_bundle' )[0]; + + if (empty($bundle_field)) { + + continue; + } + + $bundle_item = $bundle_field->get_bundle_item( $entry[$bundle_field->id] ); + + if (empty($bundle_item)) { + + continue; + } + + $bundle_item->update_meta_data('wiaas_installation', $selected_installation->get_id()); + $bundle_item->save_meta_data(); + } + } + } + /** * Adds the Order Fields group to the form editor. * diff --git a/frontend/src/containers/orders/components/OrderDocuments.jsx b/frontend/src/containers/orders/components/OrderDocuments.jsx index eb7e8a1..b7139ec 100644 --- a/frontend/src/containers/orders/components/OrderDocuments.jsx +++ b/frontend/src/containers/orders/components/OrderDocuments.jsx @@ -2,6 +2,7 @@ import React, {Component} from 'react'; import {connect} from 'react-redux'; import OrderDocumentsGroup from './OrderDocumentsGroup.jsx'; import {orderTexts} from '../../../constants/ordersConstants'; +import WiaasBox from "../../../mainComponents/box/WiaasBox"; class OrderDocuments extends Component { render() { @@ -18,7 +19,22 @@ class OrderDocuments extends Component { />)) } { - orderInfo.orderDocuments && + orderInfo.documents &&
+ + { + orderInfo.documents.map((document, index) => ( + + + +
+ {document.name} +
+
+
+ )) + } +
+
} ); diff --git a/frontend/src/containers/orders/components/OrderDocumentsGroup.jsx b/frontend/src/containers/orders/components/OrderDocumentsGroup.jsx index b638813..1876b37 100644 --- a/frontend/src/containers/orders/components/OrderDocumentsGroup.jsx +++ b/frontend/src/containers/orders/components/OrderDocumentsGroup.jsx @@ -27,13 +27,31 @@ class OrderDocumentsGroup extends Component { documentsGroup.documents.length > 0 && { - documentsGroup.documents.map(document => -
{this.downloadDocument(orderId, documentsGroup.orderItemId, document)}} className="document-link-big"> - -
- {document.name} -
-
) + documentsGroup.documents.map((document, index) => { + if (document.url) { + + return ( + +
+ +
+ {document.name} +
+
+
+ ); + } + + return ( + +
{this.downloadDocument(orderId, documentsGroup.orderItemId, document)}} className="document-link-big"> + +
+ {document.name} +
+
+ ); + }) }
} diff --git a/frontend/src/containers/orders/components/process/CustomerAcceptance.jsx b/frontend/src/containers/orders/components/process/CustomerAcceptance.jsx index 3c459a3..0d2c570 100644 --- a/frontend/src/containers/orders/components/process/CustomerAcceptance.jsx +++ b/frontend/src/containers/orders/components/process/CustomerAcceptance.jsx @@ -148,7 +148,7 @@ class CustomerAcceptance extends Component { { customerAcceptance.documents.map((document, index) =>
- {document.name} + {document.name} {document.validation}
diff --git a/frontend/src/containers/orders/components/process/ValidateQuestionnaireItem.jsx b/frontend/src/containers/orders/components/process/ValidateQuestionnaireItem.jsx index 6ec53ee..1b2dec7 100644 --- a/frontend/src/containers/orders/components/process/ValidateQuestionnaireItem.jsx +++ b/frontend/src/containers/orders/components/process/ValidateQuestionnaireItem.jsx @@ -48,7 +48,7 @@ class ValidateQuestionnaireItem extends Component {
- {document.name} + {document.name}
@@ -79,7 +79,7 @@ class ValidateQuestionnaireItem extends Component { - {document.name} + {document.name}
diff --git a/frontend/src/helpers/OrderHelper.js b/frontend/src/helpers/OrderHelper.js index 409405d..2ccd838 100644 --- a/frontend/src/helpers/OrderHelper.js +++ b/frontend/src/helpers/OrderHelper.js @@ -43,7 +43,10 @@ export const fromWCOrder = (WCOrder) => { email: WCOrder.billing.email, address: formatAddress(WCOrder.billing) }, - documents: WCOrder.documents || [], + documents: WCOrder.documents ? WCOrder.documents.map(document => { + document.icon = getDocumentIcon(document.extension); + return document; + }) : [], packages: WCOrder['line_items'].map(packageLine => { return { id: packageLine['product_id'],