handle order actions
This commit is contained in:
@@ -2,15 +2,14 @@
|
||||
|
||||
class Wiaas_Delivery_Process_Action {
|
||||
|
||||
public static function init() {
|
||||
public static function is_action_form($form) {
|
||||
|
||||
$delivery_settings = rgar($form, 'wiaas_delivery_process');
|
||||
|
||||
return ! empty($delivery_settings) && $delivery_settings['delivery_form_type'] === 'action';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Wiaas_Delivery_Process_Step $step
|
||||
*/
|
||||
public static function get_step_action_form(Wiaas_Delivery_Process_Step $step) {
|
||||
public static function get_process_step_action_form($step) {
|
||||
|
||||
if (empty($step->target_form_id)) {
|
||||
|
||||
@@ -19,15 +18,36 @@ class Wiaas_Delivery_Process_Action {
|
||||
|
||||
$action_form = GFAPI::get_form($step->target_form_id);
|
||||
|
||||
if (! $action_form) {
|
||||
|
||||
return null;
|
||||
}
|
||||
return $action_form;
|
||||
}
|
||||
|
||||
public static function get_step_action_entries(Wiaas_Delivery_Process_Step $step) {
|
||||
public static function get_process_step_action_form_action_code($step) {
|
||||
|
||||
$action_form = self::get_step_action_form($step);
|
||||
$action_form = self::get_process_step_action_form($step);
|
||||
|
||||
if (empty($action_form)) {
|
||||
|
||||
return 'manual';
|
||||
}
|
||||
|
||||
$delivery_settings = rgar($action_form, 'wiaas_delivery_process');
|
||||
|
||||
return empty($delivery_settings) ? 'manual' : $delivery_settings['delivery_action_code'];
|
||||
}
|
||||
|
||||
public static function process_step_has_customer_acceptance_action($step) {
|
||||
|
||||
return self::get_process_step_action_form_action_code($step) === 'customer-acceptance';
|
||||
}
|
||||
|
||||
public static function process_step_has_customer_validate_questionnaires_action($step) {
|
||||
|
||||
return self::get_process_step_action_form_action_code($step) === 'validate-questionnaire';
|
||||
}
|
||||
|
||||
public static function get_process_step_action_entries(Wiaas_Delivery_Process_Step $step) {
|
||||
|
||||
$action_form = self::get_process_step_action_form($step);
|
||||
|
||||
if (!$action_form) {
|
||||
|
||||
@@ -35,7 +55,6 @@ class Wiaas_Delivery_Process_Action {
|
||||
}
|
||||
|
||||
$search_criteria = array(
|
||||
'status' => 'active',
|
||||
'field_filters' => array(
|
||||
array( 'key' => 'wiaas_delivery_process_id',
|
||||
'value' => $step->get_entry_id()
|
||||
@@ -67,18 +86,210 @@ class Wiaas_Delivery_Process_Action {
|
||||
return $action_forms;
|
||||
}
|
||||
|
||||
public static function is_action_form($form) {
|
||||
public static function complete_action_step($action_id) {
|
||||
|
||||
$delivery_settings = rgar($form, 'wiaas_delivery_process');
|
||||
$action_entry = GFAPI::get_entry($action_id);
|
||||
|
||||
return ! empty($delivery_settings) && $delivery_settings['delivery_form_type'] === 'action';
|
||||
$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 ) {
|
||||
|
||||
$new_status = $current_step->get_type() === 'approval' ? 'approved' : 'complete';
|
||||
|
||||
$assignees = $current_step->get_assignees();
|
||||
|
||||
foreach ($assignees as $assignee) {
|
||||
|
||||
$current_step->process_assignee_status($assignee, $new_status, $action_form);
|
||||
}
|
||||
}
|
||||
|
||||
gravity_flow()->process_workflow($action_form, $action_entry['id']);
|
||||
}
|
||||
|
||||
public static function get_customer_step_actions(Wiaas_Delivery_Process_Step $step) {
|
||||
public static function upload_customer_questionnaire($action_entry_id) {
|
||||
|
||||
$action_entry = GFAPI::get_entry($action_entry_id);
|
||||
|
||||
$document_field = GFCommon::get_fields_by_type(GFAPI::get_form($action_entry['form_id']), 'wiaas_order_bundle_document')[0];
|
||||
|
||||
|
||||
$new_file = $document_field->get_single_file_value($action_entry['form_id'], 'file');
|
||||
|
||||
$action_entry[$document_field->id] = $new_file;
|
||||
|
||||
GFAPI::update_entry($action_entry);
|
||||
}
|
||||
|
||||
public static function get_customer_acceptance_action_data($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_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];
|
||||
|
||||
return ! empty( $action_entry[$acceptance_documents_field->id]);
|
||||
}
|
||||
|
||||
public static function update_customer_acceptance_status($action_id, $new_status, $reason) {
|
||||
|
||||
$action_entry = GFAPI::get_entry($action_id);
|
||||
$action_form = GFAPI::get_form($action_entry['form_id']);
|
||||
|
||||
$acceptance_field = GFCommon::get_fields_by_type($action_form, 'radio')[0];
|
||||
$decline_reason_field = GFCommon::get_fields_by_type($action_form, 'textarea')[0];
|
||||
|
||||
$action_entry[$acceptance_field->id] = $new_status;
|
||||
$action_entry[$decline_reason_field->id] = $reason;
|
||||
|
||||
GFAPI::update_entry($action_entry);
|
||||
}
|
||||
|
||||
public static function upload_customer_acceptance_document($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];
|
||||
|
||||
$old_value = $action_entry[$acceptance_documents_field->id];
|
||||
|
||||
$value = $acceptance_documents_field->get_single_file_value($action_form['id'], 'file');
|
||||
|
||||
if ($acceptance_documents_field->multipleFiles ) {
|
||||
|
||||
$value = array( $value );
|
||||
|
||||
$old_value = json_decode( $old_value );
|
||||
|
||||
if (! empty($old_value)) {
|
||||
|
||||
$old_value = is_array( $old_value ) ? $old_value : array( $old_value );
|
||||
|
||||
$value = array_merge( $value, $old_value );
|
||||
}
|
||||
|
||||
$value = json_encode( $value );
|
||||
}
|
||||
|
||||
$action_entry[$acceptance_documents_field->id] = $value;
|
||||
|
||||
$result = GFAPI::update_entry($action_entry);
|
||||
|
||||
return ! is_wp_error($result);
|
||||
}
|
||||
|
||||
public static function get_customer_validate_questionnaires_action_data($action_id) {
|
||||
|
||||
$action_entry = GFAPI::get_entry($action_id);
|
||||
|
||||
$action_form = GFAPI::get_form($action_entry['form_id']);
|
||||
|
||||
$order_id = $action_entry['wiaas_delivery_order_id'];
|
||||
$order = wc_get_order($order_id);
|
||||
|
||||
|
||||
// we need to collect document, bundle id and current status
|
||||
$bundle_item_id = null; $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]);
|
||||
|
||||
if (empty($bundle_item_id)) {
|
||||
|
||||
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];
|
||||
$file_path = $action_entry[$document_field->id];
|
||||
|
||||
$info = pathinfo( $action_entry[$document_field->id] );
|
||||
|
||||
$document = array(
|
||||
'name' => $info['basename'],
|
||||
'url' => $document_field->get_download_url( $file_path, true )
|
||||
);
|
||||
|
||||
$discussion_field = GFCommon::get_fields_by_type(GFAPI::get_form($action_entry['form_id']), 'workflow_discussion')[0];
|
||||
$discussion_items = json_decode($action_entry[$discussion_field->id], ARRAY_A);
|
||||
|
||||
$formated_comments = array();
|
||||
|
||||
if (is_array($discussion_items)) {
|
||||
foreach ($discussion_items as $item) {
|
||||
|
||||
$formated_comments[] = $discussion_field->format_discussion_item( $item, 'text', $action_id );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$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,
|
||||
'comments' => $formated_comments
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Wiaas_Delivery_Process_Action::init();
|
||||
|
||||
Reference in New Issue
Block a user