handle order actions

This commit is contained in:
Almira Krdzic
2018-10-31 10:23:59 +01:00
parent 5aca4e8572
commit 308c836460
17 changed files with 834 additions and 649 deletions

View File

@@ -10,8 +10,6 @@ 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();
@@ -21,7 +19,7 @@ class Wiaas_Delivery_Process {
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 );
// add_action( 'gravityflow_workflow_complete', array(__CLASS__, 'maybe_complete_parent_order'), 10, 3 );
}
/**
@@ -31,6 +29,7 @@ class Wiaas_Delivery_Process {
require_once( 'delivery-process/class-wiaas-delivery-process-step.php' );
require_once( 'delivery-process/class-wiaas-delivery-process-addon.php' );
require_once( 'delivery-process/class-wiaas-delivery-process-action.php' );
// order fields
require_once( 'delivery-process/class-wiaas-order-fields.php' );
@@ -71,6 +70,174 @@ class Wiaas_Delivery_Process {
}
}
public static function get_available_process_list_for_country($country_code) {
$forms = GFAPI::get_forms();
$available_process_forms = array();
foreach ($forms as $form) {
$delivery_settings = rgar($form, 'wiaas_delivery_process');
if ($delivery_settings['delivery_form_type'] === 'process' &&
$delivery_settings['delivery_country'] === $country_code) {
$available_process_forms[] = $form;
}
}
return $available_process_forms;
}
public static function get_customer_acceptance_data($order_id) {
$delivery_process_entry = self::get_order_delivery_process_entry($order_id);
$data = array();
if (! empty($delivery_process_entry)) {
$workflow = new Gravity_Flow_API($delivery_process_entry['form_id']);
$step = $workflow->get_current_step($delivery_process_entry);
if ($step && Wiaas_Delivery_Process_Action::process_step_has_customer_acceptance_action($step)) {
$action_entries = Wiaas_Delivery_Process_Action::get_process_step_action_entries($step);
$action_entry = $action_entries[0];
$data[] = Wiaas_Delivery_Process_Action::get_customer_acceptance_action_data($action_entry['id']);
}
}
return empty( $data ) ? $data : $data[0];
}
public static function get_customer_questionnaires_data($order_id) {
$data = array();
$delivery_process_entry = self::get_order_delivery_process_entry($order_id);
if (! empty($delivery_process_entry)) {
$workflow = new Gravity_Flow_API($delivery_process_entry['form_id']);
$step = $workflow->get_current_step($delivery_process_entry);
if ($step && Wiaas_Delivery_Process_Action::process_step_has_customer_validate_questionnaires_action($step)) {
$action_entries = Wiaas_Delivery_Process_Action::get_process_step_action_entries($step);
foreach ($action_entries as $action_entry) {
$action_data = Wiaas_Delivery_Process_Action::get_customer_validate_questionnaires_action_data($action_entry['id']);
if (! empty($action_data)) {
$data[] = $action_data;
}
}
}
}
return $data;
}
public static function update_customer_acceptance_status($order_id,$status, $reason) {
$delivery_process_entry = self::get_order_delivery_process_entry($order_id);
if (! empty($delivery_process_entry) ) {
$workflow = new Gravity_Flow_API($delivery_process_entry['form_id']);
$step = $workflow->get_current_step($delivery_process_entry);
$action_entries = Wiaas_Delivery_Process_Action::get_process_step_action_entries($step);
$action_entry = $action_entries[0];
Wiaas_Delivery_Process_Action::update_customer_acceptance_status($action_entry['id'], $status, $reason);
if ($status === 'accept') {
Wiaas_Delivery_Process_Action::complete_action_step($action_entry['id']);
}
}
}
public static function is_customer_acceptance_uploaded($order_id) {
$delivery_process_entry = self::get_order_delivery_process_entry($order_id);
if (! empty($delivery_process_entry) ) {
$workflow = new Gravity_Flow_API($delivery_process_entry['form_id']);
$step = $workflow->get_current_step($delivery_process_entry);
$action_entries = Wiaas_Delivery_Process_Action::get_process_step_action_entries($step);
$action_entry = $action_entries[0];
return Wiaas_Delivery_Process_Action::is_customer_acceptance_uploaded($action_entry['id']);
}
return false;
}
public static function upload_customer_questionnaire($order_id, $action_entry_id) {
Wiaas_Delivery_Process_Action::upload_customer_questionnaire($action_entry_id);
Wiaas_Delivery_Process_Action::complete_action_step($action_entry_id);
}
public static function upload_customer_acceptance_document($order_id) {
$delivery_process_entry = self::get_order_delivery_process_entry($order_id);
if (! empty($delivery_process_entry) ) {
$workflow = new Gravity_Flow_API($delivery_process_entry['form_id']);
$step = $workflow->get_current_step($delivery_process_entry);
$action_entries = Wiaas_Delivery_Process_Action::get_process_step_action_entries($step);
$action_entry = $action_entries[0];
$success = Wiaas_Delivery_Process_Action::upload_customer_acceptance_document($action_entry['id']);
if ($success) {
Wiaas_Delivery_Process_Action::complete_action_step($action_entry['id']);
}
return $success;
}
return false;
}
public static function get_order_delivery_process_entry($order_id) {
$order = wc_get_order($order_id);
$process_entry_id = $order->get_meta('wiaas_delivery_process_entry_id', true);
if (!isset($process_entry_id)) {
return array();
}
return GFAPI::get_entry($process_entry_id);
}
/**
* Retrieves delivery process instance for order
*
@@ -82,9 +249,7 @@ class Wiaas_Delivery_Process {
$process_entry_id = get_post_meta($order_id, 'wiaas_delivery_process_entry_id');
$process_entry_id = 159;
if (!isset($process_entry_id)) {
if (empty($process_entry_id)) {
return null;
}
@@ -107,6 +272,11 @@ class Wiaas_Delivery_Process {
foreach ( $steps_info as $step_info ) {
$step = $api->get_step( $step_info->get_id(), $process_instance );
if (! $step->is_visible_to_customer) {
continue;
}
$action_code = 'manual';
$action_form = GFAPI::get_form($step->target_form_id);
$has_action_form = $action_form !== false;
@@ -141,144 +311,6 @@ class Wiaas_Delivery_Process {
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
@@ -319,24 +351,3 @@ class Wiaas_Delivery_Process {
}
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 );