Delivery setup

This commit is contained in:
Almira Krdzic
2018-08-06 17:36:57 +02:00
parent ed0e44b964
commit 5afdde434b
27 changed files with 2809 additions and 236 deletions

View File

@@ -1,52 +1,156 @@
<?php
add_action( 'gravityflow_loaded', 'register_delivery_step', 1 );
/**
* Class Wiaas Delivery Process
*
* Adds suport for wiaas order delivery process with integration with Gravity Flow and Gravity Forms
*/
defined( 'ABSPATH' ) || exit;
function register_delivery_step() {
require_once( 'delivery-process/class-wiaas-delivery-process-step.php' );
Gravity_Flow_Steps::register( new Wiaas_Delivery_Process_Step() );
}
class Wiaas_Delivery_Process {
add_action( 'gravityflow_workflow_complete', 'action_gflow_after_workflow_complete', 5, 3 );
add_filter( 'gform_entry_meta', 'wiaas_get_entry_meta', 10, 2 );
private static $process_form_title_prefix = 'DELIVERY PROCESS:';
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' ),
),
);
public static function init() {
self::_register_delivery_process_step_type();
$entry_meta[ 'wiaas_delivery_process_id' ] = array(
'label' => 'Wiaas Delivery Process Id',
'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;
self::_init_hooks();
}
private static function _init_hooks() {
add_action('woocommerce_new_order', array( __CLASS__, 'create_delivery_process_for_order' ));
$api = new Gravity_Flow_API( $form['id'] );
$current_step = $api->get_current_step( $entry );
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 );
}
/**
* Registers our Delivery Process Step Type as available Gravity Flow Step Type
*/
private static function _register_delivery_process_step_type() {
require_once( 'delivery-process/class-wiaas-delivery-process-step.php' );
Gravity_Flow_Steps::register( new Wiaas_Delivery_Process_Step() );
}
/**
* Retrieves delivery process instance for order
*
* @param $order_id
*
* @return array|null
*/
public static function get_order_delivery_process($order_id) {
$process_entry_id = get_post_meta($order_id, 'wiaas_delivery_process_entry_id');
if (!isset($process_entry_id)) {
return null;
}
$process_instance = GFAPI::get_entry($process_entry_id);
$process_form = GFAPI::get_form($process_instance['form_id']);
$api = new Gravity_Flow_API($process_instance['form_id']);
$steps_info = $api->get_steps();
$delivery_process = array(
'id' => $process_form['id'],
'name' => $process_form['title'],
'steps' => array()
);
foreach ( $steps_info as $step_info ) {
$step = $api->get_step( $step_info->get_id(), $process_instance );
$info = $step->get_feed_meta();
$delivery_process['steps'][] = 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 $delivery_process;
}
/**
* Automatically create delivery process instance when order is created
* @param $order_id
*/
public static function create_delivery_process_for_order($order_id) {
$process_form = null;
$forms = GFFormsModel::search_forms( self::$process_form_title_prefix, true );
$process_form = $forms[0];
if(isset($process_form)) {
$order = wc_get_order( $order_id );
$new_process_entry = array(
'form_id' => $process_form->id,
'2' => $order->get_customer_id(),
'wiaas_delivery_order_id' => $order_id,
);
$process_entry_id = GFAPI::add_entry( $new_process_entry );
add_post_meta($order_id, 'wiaas_delivery_process_id', $process_form->id);
add_post_meta($order_id, 'wiaas_delivery_process_entry_id', $process_entry_id);
}
}
/**
* Extends Gravity Form entry metadata with 'wiaas_delivery_process_id'
*
* This way we can track for each delivery step its parent process
* @param $entry_meta
*
* @return mixed
*/
public static function extend_gravity_form_entry_meta($entry_meta) {
$entry_meta[ 'wiaas_delivery_process_id' ] = array(
'label' => 'Wiaas Delivery Process Id',
'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' ),
),
);
$entry_meta[ 'wiaas_delivery_order_id' ] = array(
'label' => 'Wiaas Delivery Process Order Id',
'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;
}
/**
* Process parent process when single step workflow has completed
* @param $entry_id
* @param $form
* @param $final_status
*/
public static function maybe_complete_parent_process_step($entry_id, $form) {
$entry = GFAPI::get_entry($entry_id);
$parent_entry_id = $entry['wiaas_delivery_process_id'];
if (empty($parent_entry_id)) {
return false;
}
if ( empty( $current_step ) ) {
$parent_entry_id = absint( $parent_entry_id );
$parent_entry = GFAPI::get_entry( $parent_entry_id );
@@ -55,105 +159,4 @@ function action_gflow_after_workflow_complete($entry_id, $form, $final_status) {
}
}
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 );
}
add_action( 'gravityflow_loaded', array('Wiaas_Delivery_Process', 'init') );