Wiaas delivery process integration with flow

This commit is contained in:
Almira Krdzic
2018-08-01 11:46:43 +02:00
parent 2c57157703
commit 321fbc3f61
5 changed files with 362 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
<?php
/**
* Wiaas API
*
* Handles wiaas API endpoint requests
*/
defined( 'ABSPATH' ) || exit;
class Wiaas_API {
/**
* Include REST API classes
*/
private function rest_api_includes() {
}
}

View File

@@ -0,0 +1,149 @@
<?php
add_action( 'gravityflow_loaded', 'register_delivery_step', 1 );
function register_delivery_step() {
require_once( 'delivery-process/class-wiaas-delivery-process-step.php' );
Gravity_Flow_Steps::register( new Wiaas_Delivery_Process_Step() );
}
add_action( 'gravityflow_workflow_complete', 'action_gflow_after_workflow_complete', 5, 3 );
add_filter( 'gform_entry_meta', 'wiaas_get_entry_meta', 10, 2 );
function wiaas_get_entry_meta($entry_meta, $form_id) {
$entry_meta[ 'wiaas_delivery_order_id' ] = array(
'label' => 'Wiaas Delivery Process Step Order',
'is_numeric' => true,
'update_entry_meta_callback' => null,
'is_default_column' => false, // this column will be displayed by default on the entry list
'filter' => array(
'operators' => array( 'is' ),
),
);
return $entry_meta;
}
function action_gflow_after_workflow_complete($entry_id, $form, $final_status) {
$entry = GFAPI::get_entry($entry_id);
$parent_entry_id = $entry['wiaas_delivery_process_id'];
if (empty($parent_entry_id)) {
return;
}
$api = new Gravity_Flow_API( $form['id'] );
$current_step = $api->get_current_step( $entry );
if ( empty( $current_step ) ) {
$parent_entry_id = absint( $parent_entry_id );
$parent_entry = GFAPI::get_entry( $parent_entry_id );
$parent_api = new Gravity_Flow_API( $parent_entry['form_id'] );
$parent_api->process_workflow( $parent_entry_id );
}
}
add_action( 'rest_api_init', 'wiaas_action_rest_api_init' );
function wiaas_action_rest_api_init() {
register_rest_route( 'wiaas', 'next-delivery-steps', array(
'methods' => 'GET',
'callback' => 'get_next_actions_for_user',
'permission_callback' => null,
) );
register_rest_route( 'wiaas', 'order-delivery-process/(?P<id>[\d]+)', array(
'args' => array(
'id' => array(
'description' => __( 'Unique identifier for the resource.', 'wiaas' ),
'type' => 'integer',
),
),
array(
'methods' => 'GET',
'callback' => 'get_delivery_process_for_order',
'permission_callback' => null,
)
) );
}
function get_next_actions_for_user() {
$current_user = wp_get_current_user();
$field_filters = array();
$field_filters[] = array(
'key' => 'workflow_user_id_' . $current_user->ID,
'value' => 'pending',
);
$user_roles = gravity_flow()->get_user_roles();
foreach ( $user_roles as $user_role ) {
$field_filters[] = array(
'key' => 'workflow_role_' . $user_role,
'value' => 'pending',
);
}
$field_filters['mode'] = 'any';
$search_criteria = array();
$search_criteria['field_filters'] = $field_filters;
$search_criteria['status'] = 'active';
$form_ids = gravity_flow()->get_workflow_form_ids();
$total_count = 7;
$entries = GFAPI::get_entries( $form_ids, $search_criteria, null, null, $total_count );
$data = array();
foreach ($entries as $entry) {
$step = gravity_flow()->get_step( $entry['workflow_step'] );
$data[] = array(
'idOrder' => $entry['wiaas_delivery_order_id'],
'orderNumber' => $entry['wiaas_delivery_order_id'],
'status' => $entry['workflow_final_status'],
'stepAction' => $step->get_name(),
);
}
$response = new WP_REST_Response( $data );
return $response;
}
function get_delivery_process_for_order($data) {
$order_id = $data["id"];
$process = GFAPI::get_entry(161);
$api = new Gravity_Flow_API($process['form_id']);
$steps_info = $api->get_steps();
$response = array();
foreach ( $steps_info as $step_info ) {
$step = $api->get_step( $step_info->get_id(), $process );
$info = $step->get_feed_meta();
$response[] = array(
'step_id' => $step->get_id(),
'step_form_entry_id' => $step->get_target_form_entry_id() ?: null,
'short_desc' => $info['step_name'],
'full_desc' => $info['description'],
'action_code' => $step->get_delivery_action_type(),
'step_type' => $step->get_delivery_action_type() === 'manual' ? 'manual' : 'extraAction',
'status' => $step->get_status() ?: 'inactive',
'order_id' => $order_id,
'actual_date' => $step->get_target_actual_date(),
'comments' => $step->get_target_step_comments(),
);
}
return new WP_REST_Response( $response );
}

View File

@@ -0,0 +1,192 @@
<?php
class Wiaas_Delivery_Process_Step extends Gravity_Flow_Step {
public $_step_type = 'wiaas_delivery_step';
private static $delivery_action_types = array(
'DELIVERY ACTION TYPE: Customer acceptance' => 'customer-acceptance',
'DELIVERY ACTION TYPE: Validate Questionnaire' => 'validate-questionnaire',
'DELIVERY ACTION TYPE: Manual' => 'manual',
'DELIVERY ACTION TYPE: Schedule meeting' => 'schedule-meeting'
);
public function get_label() {
return esc_html__( 'Wiaas Delivery Step', 'wiaas' );
}
public function get_settings() {
$settings_api = $this->get_common_settings_api();
$forms = $this->get_forms();
$form_choices[] = array( 'label' => esc_html__( 'Select a Form', 'wiaas' ), 'value' => '' );
foreach ( $forms as $form ) {
$form_choices[] = array( 'label' => $form->title, 'value' => $form->id );
}
$settings = array(
'title' => esc_html__( 'Wiaas Delivery Step', 'wiaas' ),
'fields' => array(
$settings_api->get_setting_instructions(),
array (
'name' => 'is_visible_to_customer',
'label' => __( 'Visible to customer', 'wiaas' ),
'type' => 'checkbox',
'choices' => array(
array(
'label' => __( 'Show to customer', 'wiaas' ),
'name' => 'is_visible_to_customer',
'default_value' => true,
),
),
'tooltip' => __( 'Determines if this step will be shown to customer.', 'wiaas' ),
),
array(
'name' => 'target_form_id',
'label' => esc_html__( 'Form', 'wiaas' ),
'type' => 'select',
'onchange' => "jQuery(this).closest('form').submit();",
'choices' => $form_choices,
),
),
);
$settings['fields'][] = array(
'name' => 'store_new_entry_id',
'label' => esc_html__( 'Store New Entry ID', 'wiaas' ),
'type' => 'checkbox_and_container',
'checkbox' => array(
'label' => esc_html__( 'Store the ID of the new entry.', 'wiaas' ),
),
'settings' => array(
array(
'name' => 'new_entry_id_field',
'type' => 'field_select',
'args' => array(
'input_types' => array(
'hidden',
),
),
),
),
);
return $settings;
}
function process() {
$form = $this->get_form();
$entry = $this->get_entry();
$target_form = GFAPI::get_form( $this->target_form_id );
$new_entry = array(
'form_id' => $this->target_form_id,
);
$customer_id_value = null;
if ( is_array( $form['fields'] ) ) {
foreach ( $form['fields'] as $field ) {
if (GFCommon::get_label( $field ) === 'order-id') {
$new_entry['wiaas_delivery_order_id'] = $entry[$field->id];
}
if (GFCommon::get_label( $field ) === 'customer-id') {
$customer_id_value = $entry[$field->id];
}
}
}
if ( is_array( $target_form['fields'] ) ) {
foreach ( $target_form['fields'] as $field ) {
if (GFCommon::get_label( $field ) === 'customer-id') {
$new_entry[$field->id] = $customer_id_value;
}
}
}
$entry_id = GFAPI::add_entry( $new_entry );
if ( is_wp_error( $entry_id ) ) {
$this->log_debug( __METHOD__ .'(): failed to add entry' );
} else {
// store entry id
gform_update_meta($this->get_entry_id(), 'wiaas_delivery_step_' . $this->get_id() .'_entry_id', $entry_id);
}
$note = $this->get_name() . ': ' . esc_html__( 'started.', 'wiaas' );
$this->add_note( $note );
return false;
}
public function status_evaluation() {
$target_form_entry = $this->get_target_form_entry();
if(is_wp_error($target_form_entry)) {
return 'complete';
}
$api = new Gravity_Flow_API( $this->target_form_id );
$status = $api->get_status($target_form_entry);
if ( $status === 'complete' || $status === 'approved' ) {
return 'complete';
}
return 'pending';
}
public function get_entry_meta($entry_meta, $form_id) {
$entry_meta['wiaas_delivery_step_' . $this->get_id() .'_entry_id'] = null;
return $entry_meta;
}
public function get_forms() {
$forms = GFFormsModel::get_forms();
return $forms;
}
public function get_delivery_action_type() {
$target_form = GFAPI::get_form( $this->target_form_id );
return self::$delivery_action_types[$target_form['title']];
}
public function get_target_actual_date() {
$target_entry = $this->get_target_form_entry();
$target_form = GFAPI::get_form( $this->target_form_id );
if (!is_wp_error($target_entry) && is_array($target_form['fields'])) {
foreach ( $target_form['fields'] as $field ) {
if (GFCommon::get_label( $field ) === 'Actual Date') {
return $target_entry[$field->id];
}
}
}
return null;
}
public function get_target_step_comments() {
$notes = RGFormsModel::get_lead_notes( $this->get_target_form_entry_id() );
$comments = array();
foreach ( $notes as $key => $note ) {
if ( $note->note_type !== 'gravityflow' ) {
$comments[] = array(
'date' => $note->date_created,
'text' => $note->value,
'user' => $note->user_name
);
}
}
return $comments;
}
public function get_target_form_entry() {
return GFAPI::get_entry($this->get_target_form_entry_id());
}
public function get_target_form_entry_id() {
return gform_get_meta($this->get_entry_id(), 'wiaas_delivery_step_' . $this->get_id() .'_entry_id');
}
}

View File

@@ -21,3 +21,5 @@ define( 'WIAAS_DIR', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
if ( defined( 'WP_CLI' ) && WP_CLI ) {
include_once WIAAS_DIR . '/includes/class-wiaas-cli.php';
}
include_once WIAAS_DIR . '/includes/class-wiaas-delivery-process.php';