Handle assigment for order delivery flow

This commit is contained in:
Almira Krdzic
2018-11-02 10:30:25 +01:00
parent 4b0fc6c61d
commit cdbefc7ef0
25 changed files with 1131 additions and 1047 deletions

View File

@@ -1,7 +1,17 @@
<?php
/**
* Class Wiaas_Delivery_Process_Action
*/
class Wiaas_Delivery_Process_Action {
/**
* Check if form is delivery process action form
*
* @param $form
*
* @return bool
*/
public static function is_action_form($form) {
$delivery_settings = rgar($form, 'wiaas_delivery_process');
@@ -9,6 +19,13 @@ class Wiaas_Delivery_Process_Action {
return ! empty($delivery_settings) && $delivery_settings['delivery_form_type'] === 'action';
}
/**
* Retrieve action form for step
*
* @param Wiaas_Delivery_Process_Step $step
*
* @return mixed|null
*/
public static function get_process_step_action_form($step) {
if (empty($step->target_form_id)) {
@@ -16,11 +33,18 @@ class Wiaas_Delivery_Process_Action {
return null;
}
$action_form = GFAPI::get_form($step->target_form_id);
return $action_form;
return GFAPI::get_form($step->target_form_id);
}
/**
* Get action code for delivery process step form
*
* Action code is used for implementing specific logic for step form
*
* @param Wiaas_Delivery_Process_Step $step
*
* @return string
*/
public static function get_process_step_action_form_action_code($step) {
$action_form = self::get_process_step_action_form($step);
@@ -35,16 +59,35 @@ class Wiaas_Delivery_Process_Action {
return empty($delivery_settings) ? 'manual' : $delivery_settings['delivery_action_code'];
}
/**
* Check if delivery process step has customer acceptance action form
*
* @param Wiaas_Delivery_Process_Step $step
*
* @return bool
*/
public static function process_step_has_customer_acceptance_action($step) {
return self::get_process_step_action_form_action_code($step) === 'customer-acceptance';
}
/**
* Check if delivery process step has customer validate action form
*
* @param Wiaas_Delivery_Process_Step $step
*
* @return bool
*/
public static function process_step_has_customer_validate_questionnaires_action($step) {
return self::get_process_step_action_form_action_code($step) === 'validate-questionnaire';
}
/**
* @param Wiaas_Delivery_Process_Step $step
*
* @return array|WP_Error
*/
public static function get_process_step_action_entries(Wiaas_Delivery_Process_Step $step) {
$action_form = self::get_process_step_action_form($step);
@@ -67,6 +110,11 @@ class Wiaas_Delivery_Process_Action {
return GFAPI::get_entries( $action_form, $search_criteria, $sorting );
}
/**
* Retrieve forms that will be used as possible action forms
*
* @return array
*/
public static function get_action_forms() {
$forms = GFAPI::get_forms();
@@ -86,9 +134,14 @@ class Wiaas_Delivery_Process_Action {
return $action_forms;
}
public static function complete_action_step($action_id) {
/**
* Complete current workflow step for action form
*
* @param int $action_entry_id
*/
public static function complete_action_step($action_entry_id) {
$action_entry = GFAPI::get_entry($action_id);
$action_entry = GFAPI::get_entry($action_entry_id);
$action_form = GFAPI::get_form($action_entry['form_id']);
@@ -111,6 +164,11 @@ class Wiaas_Delivery_Process_Action {
gravity_flow()->process_workflow($action_form, $action_entry['id']);
}
/**
* Upload customer questionnaire document
*
* @param int $action_entry_id
*/
public static function upload_customer_questionnaire($action_entry_id) {
$action_entry = GFAPI::get_entry($action_entry_id);
@@ -125,56 +183,34 @@ class Wiaas_Delivery_Process_Action {
GFAPI::update_entry($action_entry);
}
public static function get_customer_acceptance_action_data($action_id) {
$action_entry = GFAPI::get_entry($action_id);
/**
* Check if customer uploaded acceptance document
*
* @param int $action_entry_id
*
* @return bool
*/
public static function is_customer_acceptance_uploaded($action_entry_id) {
$action_entry = GFAPI::get_entry($action_entry_id);
$action_form = GFAPI::get_form($action_entry['form_id']);
$acceptance_documents_field = GFCommon::get_fields_by_type($action_form, 'fileupload')[0];
$acceptance_field = GFCommon::get_fields_by_type($action_form, 'radio')[0];
$decline_reason_field = GFCommon::get_fields_by_type($action_form, 'textarea')[0];
$expiration_date_field = GFCommon::get_fields_by_type($action_form, 'date')[0];
$file_paths = json_decode($action_entry[$acceptance_documents_field->id]);
$documents = array();
foreach ($file_paths as $file_path) {
$info = pathinfo( $file_path );
$documents[] = array(
'name' => $info['basename'],
'url' => $acceptance_documents_field->get_download_url( $file_path, true )
);
}
return array(
'action_id' => $action_entry['id'],
'documents' => $documents,
'expiration' => $action_entry[$expiration_date_field->id],
'decline_reason' => $action_entry[$decline_reason_field->id],
'status' => ($action_entry[$acceptance_field->id] === 'accept') ? 1 : -1
);
}
public static function is_customer_acceptance_uploaded($action_id) {
$action_entry = GFAPI::get_entry($action_id);
$action_form = GFAPI::get_form($action_entry['form_id']);
$acceptance_documents_field = GFCommon::get_fields_by_type($action_form, 'fileupload')[0];
$acceptance_documents_field = GFCommon::get_fields_by_type($action_form, 'wiaas_order_document')[0];
return ! empty( $action_entry[$acceptance_documents_field->id]);
}
public static function update_customer_acceptance_status($action_id, $new_status, $reason) {
/**
* Update acceptance status for order
*
* @param int $action_entry_id
* @param $new_status
* @param $reason
*/
public static function update_customer_acceptance_status($action_entry_id, $new_status, $reason) {
$action_entry = GFAPI::get_entry($action_id);
$action_entry = GFAPI::get_entry($action_entry_id);
$action_form = GFAPI::get_form($action_entry['form_id']);
$acceptance_field = GFCommon::get_fields_by_type($action_form, 'radio')[0];
@@ -184,14 +220,28 @@ class Wiaas_Delivery_Process_Action {
$action_entry[$decline_reason_field->id] = $reason;
GFAPI::update_entry($action_entry);
$workflow = new Gravity_Flow_API($action_form['id']);
$current_step = $workflow->get_current_step($action_entry);
if ($current_step && $current_step->get_t)
Wiaas_Delivery_Process_Action::complete_action_step($action_entry['id']);
}
public static function upload_customer_acceptance_document($action_id) {
/**
* Upload customer acceptance document for order
*
* @param int $action_entry_id
*
* @return bool
*/
public static function upload_customer_acceptance_document($action_entry_id) {
$action_entry = GFAPI::get_entry($action_id);
$action_entry = GFAPI::get_entry($action_entry_id);
$action_form = GFAPI::get_form($action_entry['form_id']);
$acceptance_documents_field = GFCommon::get_fields_by_type($action_form, 'fileupload')[0];
$acceptance_documents_field = GFCommon::get_fields_by_type($action_form, 'wiaas_order_document')[0];
$old_value = $action_entry[$acceptance_documents_field->id];
@@ -220,9 +270,69 @@ class Wiaas_Delivery_Process_Action {
return ! is_wp_error($result);
}
public static function get_customer_validate_questionnaires_action_data($action_id) {
/**
* Collect customer acceptance action data
*
* @param int $action_entry_id
*
* @return array
*/
public static function get_customer_acceptance_action_data($action_entry_id) {
$action_entry = GFAPI::get_entry($action_id);
$action_entry = GFAPI::get_entry($action_entry_id);
$action_form = GFAPI::get_form($action_entry['form_id']);
$acceptance_documents_field = GFCommon::get_fields_by_type($action_form, 'wiaas_order_document')[0];
$acceptance_field = GFCommon::get_fields_by_type($action_form, 'radio')[0];
$decline_reason_field = GFCommon::get_fields_by_type($action_form, 'textarea')[0];
$expiration_date_field = GFCommon::get_fields_by_type($action_form, 'date')[0];
$file_paths = json_decode($action_entry[$acceptance_documents_field->id]);
error_log($action_entry[$acceptance_documents_field->id]);
$documents = array();
foreach ($file_paths as $file_path) {
$info = pathinfo( $file_path );
$documents[] = array(
'name' => $info['basename'],
'url' => $acceptance_documents_field->get_download_url( $file_path, true )
);
}
$status = 0;
if ($action_entry[$acceptance_field->id] === 'accept') {
$status = 1;
}
if ($action_entry[$acceptance_field->id] === 'decline') {
$status = -1;
}
return array(
'action_id' => $action_entry['id'],
'documents' => $documents,
'expiration' => $action_entry[$expiration_date_field->id],
'decline_reason' => $action_entry[$decline_reason_field->id],
'status' => $status
);
}
/**
* Get customer validate questionnaires action data
*
* @param int $action_entry_id
*
* @return array|null
*/
public static function get_customer_validate_questionnaires_action_data($action_entry_id) {
$action_entry = GFAPI::get_entry($action_entry_id);
$action_form = GFAPI::get_form($action_entry['form_id']);
@@ -240,9 +350,8 @@ class Wiaas_Delivery_Process_Action {
return null;
}
$bundle_item = $order->get_item($bundle_item_id);
$document_field = GFCommon::get_fields_by_type(GFAPI::get_form($action_entry['form_id']), 'wiaas_order_bundle_document')[0];
$document_field = GFCommon::get_fields_by_type($action_form, 'wiaas_order_bundle_document')[0];
$file_path = $action_entry[$document_field->id];
$info = pathinfo( $action_entry[$document_field->id] );
@@ -260,25 +369,29 @@ class Wiaas_Delivery_Process_Action {
if (is_array($discussion_items)) {
foreach ($discussion_items as $item) {
$formated_comments[] = $discussion_field->format_discussion_item( $item, 'text', $action_id );
$formated_comments[] = $discussion_field->format_discussion_item( $item, 'text', $action_entry_id );
}
}
$action_workflow_api = new Gravity_Flow_API($action_form['id']);
$action_step = $action_workflow_api->get_current_step($action_entry);
// if completed it means that document got validated
$status = 'validated';
if (! empty($action_step)) {
if ($action_step->get_type() === 'approval') {
$status = 'not-validated';
}
if ($action_step->get_type() === 'user_input') {
$is_assignee = $action_step->is_assignee($action_step->get_current_assignee_key());
if ($is_assignee) {
// if customer is assignee it means it is his turn to upload changed version of document
// which means it was rejected
$status = 'invalid';
} else {
// if customer is not assignee it means administrator is still reviewing document
$status = 'not-validated';
}
}