Files
old-new-wiaas/backend/app/plugins/wiaas/includes/class-wiaas-delivery-process.php
2018-10-30 17:20:56 +01:00

343 lines
9.4 KiB
PHP

<?php
/**
* Class Wiaas Delivery Process
*
* Adds suport for wiaas order delivery process with integration with Gravity Flow and Gravity Forms
*/
defined( 'ABSPATH' ) || exit;
class Wiaas_Delivery_Process {
private static $process_form_title_prefix = 'DELIVERY PROCESS:';
public static function init() {
self::_register_delivery_process_step_type();
self::_init_hooks();
}
private static function _init_hooks() {
add_action('woocommerce_new_order', array( __CLASS__, 'create_delivery_process_for_order' ));
add_action( 'gravityflow_workflow_complete', array(__CLASS__, 'maybe_complete_parent_order'), 10, 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' );
require_once( 'delivery-process/class-wiaas-delivery-process-addon.php' );
// order fields
require_once( 'delivery-process/class-wiaas-order-fields.php' );
require_once( 'delivery-process/class-wiaas-field-order-number.php' );
require_once( 'delivery-process/class-wiaas-field-order-bundle-select.php' );
require_once( 'delivery-process/class-wiaas-field-order-supplier-select.php' );
require_once( 'delivery-process/class-wiaas-field-order-bundle-document.php' );
require_once( 'delivery-process/class-wiaas-date-list-field.php' );
Gravity_Flow_Steps::register( new Wiaas_Delivery_Process_Step() );
GFAddOn::register( 'Wiaas_Delivery_Process_Addon' );
}
/**
* 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
*
* @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');
$process_entry_id = 159;
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()
);
$current_step = $api->get_current_step($process_instance);
foreach ( $steps_info as $step_info ) {
$step = $api->get_step( $step_info->get_id(), $process_instance );
$action_code = 'manual';
$action_form = GFAPI::get_form($step->target_form_id);
$has_action_form = $action_form !== false;
if ($has_action_form) {
$delivery_settings = rgar($action_form, 'wiaas_delivery_process');
$action_code = empty($delivery_settings['delivery_action_code']) ? 'manual' : $delivery_settings['delivery_action_code'];
}
$info = $step->get_feed_meta();
$status = $step->get_status();
if ($current_step && $current_step->get_id() === $step->get_id()) {
$status = 'pending';
}
$delivery_process['steps'][] = array(
'step_id' => $step->get_id(),
'process_id' => $process_entry_id,
'short_desc' => $info['step_name'],
'full_desc' => $info['description'],
'action_code' => $action_code,
'status' => $status,
'order_id' => $order_id,
);
}
return $delivery_process;
}
public static function get_current_delivery_step_info($order_id) {
$process_entry_id = 159;
$process_instance = GFAPI::get_entry($process_entry_id);
$api = new Gravity_Flow_API($process_instance['form_id']);
$current_step = $api->get_current_step($process_instance);
if (!$current_step) {
return null;
}
$current_step_info = array(
'step_id' => $current_step->get_id(),
'process_id' => $process_entry_id,
'short_desc' => $current_step->get_name(),
'action_code' => 'manual',
'status' => $current_step->get_status(),
);
$action_form = GFAPI::get_form($current_step->target_form_id);
$has_action_form = $action_form !== false;
$delivery_settings = $action_form ? rgar($action_form, 'wiaas_delivery_process') : array();
$customer_allowed_actions = array( 'customer-acceptance', 'validate-questionnaire' );
if (! $has_action_form && ! in_array($delivery_settings['delivery_action_code'], $customer_allowed_actions)) {
return $current_step_info;
}
$current_step_info['action_code'] = $delivery_settings['delivery_action_code'];
$current_step_info['actions'] = array();
$action_entry_ids = gform_get_meta(
$current_step->get_entry_id(), 'wiaas_delivery_step_' . $current_step->get_id() . '_action_entry_ids'
);
if (empty($action_entry_ids)) {
return $current_step_info;
}
$order = wc_get_order($order_id);
foreach ($action_entry_ids as $action_entry_id) {
$action_entry = GFAPI::get_entry($action_entry_id);
if (is_wp_error($action_entry)) {
continue;
}
$action_workflow_api = new Gravity_Flow_API($action_form['id']);
$action_workflow_api->get_status($action_entry);
$action_data = self::_collect_validate_questionnaire_action_info($action_form, $action_entry, $order);
if (! empty($action_data)) {
$current_step_info['actions'][] = $action_data;
}
}
return $current_step_info;
}
/**
* Complete action step
*
* @param int $action_id
*/
public static function complete_action_step($action_id) {
$action_entry = GFAPI::get_entry($action_id);
$action_form = GFAPI::get_form($action_entry['form_id']);
$workflow = new Gravity_Flow_API($action_form['id']);
$current_step = $workflow->get_current_step($action_entry);
if ( $current_step ) {
$assignees = $current_step->get_assignees();
foreach ($assignees as $assignee) {
$current_step->process_assignee_status($assignee, 'complete', $action_form);
}
}
gravity_flow()->process_workflow($action_form, $action_entry['id']);
}
private static function _collect_validate_questionnaire_action_info($action_form, $action_entry, $order) {
// we need to collect document, bundle id and current status
$bundle_item_id = null; $document_info = array(); $status = null;
$bundle_field = GFCommon::get_fields_by_type($action_form, 'wiaas_order_bundle')[0];
$bundle_item_id = absint(explode('|', $action_entry[$bundle_field->id])[1]);
$bundle_item = $order->get_item($bundle_item_id);
$documents = wiaas_get_order_item_documents($bundle_item, 'order_questionaire');
$document = $documents[0];
$action_workflow_api = new Gravity_Flow_API($action_form['id']);
$action_step = $action_workflow_api->get_current_step($action_entry);
$status = 'validated';
if (! empty($action_step)) {
if ($action_step->get_type() === 'approval') {
$status = 'not-validated';
}
if ($action_step->get_type() === 'user_input') {
$status = 'invalid';
}
}
return array(
'item_id' => $bundle_item_id,
'order_id' =>$order->get_id(),
'action_id' => $action_entry['id'],
'document' => $document,
'status' => $status
);
}
/**
* 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 = GFAPI::get_forms();
foreach ( $forms as $form ) {
$delivery_settings = rgar($form, 'wiaas_delivery_process');
if ( ! empty($delivery_settings) && $delivery_settings['delivery_form_type'] === 'process'){
$process_form = $form;
break;
}
}
if (empty($process_form)) {
return;
}
$order = wc_get_order( $order_id );
$order_field = GFCommon::get_fields_by_type($form, 'wiaas_order')[0];
$order_field_id = $order_field->id;
$new_process_entry = array(
'form_id' => $process_form->id,
"$order_field_id" => $order_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);
}
}
add_action( 'gravityflow_loaded', array('Wiaas_Delivery_Process', 'init') );
function wiaas_gform_upload_path() {
$pathdata = wp_upload_dir();
if ( empty( $pathdata['subdir'] ) ) {
$pathdata['path'] = $pathdata['path'] . wiaas_documents_base_dir();
$pathdata['url'] = $pathdata['url'] . wiaas_documents_base_dir();
$pathdata['subdir'] = wiaas_documents_base_dir();
} else {
$new_subdir = wiaas_documents_base_dir() . $pathdata['subdir'];
$pathdata['path'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['path'] );
$pathdata['url'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['url'] );
$pathdata['subdir'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['subdir'] );
}
return $pathdata;
}
add_filter( 'gform_upload_path', 'wiaas_gform_upload_path', 10, 2 );