Files
old-new-wiaas/backend/app/plugins/wiaas/includes/class-wiaas-delivery-process.php

339 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 {
public static function init() {
self::_register_delivery_process();
self::_init_hooks();
}
private static function _init_hooks() {
add_action( 'gravityflow_workflow_complete', array(__CLASS__, 'maybe_complete_parent_order'), 10, 2 );
}
/**
*
* Registers our Delivery Process Addons
*
*/
private static function _register_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' );
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' );
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_field = GFCommon::get_fields_by_type($form, 'wiaas_order')[0];
$order_id = empty($order_field) ? null : absint($entry[$order_field->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);
}
}
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
*
* @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 (empty($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 );
if (! $step->is_visible_to_customer) {
continue;
}
$action_code = Wiaas_Delivery_Process_Action::get_process_step_action_form_action_code($step);
$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;
}
}
add_action( 'gravityflow_loaded', array('Wiaas_Delivery_Process', 'init') );
add_filter( 'gform_notification', 'rw_notification_attachments', 10, 3 );
function rw_notification_attachments( $notification, $form, $entry ) {
$fileupload_fields = GFAPI::get_fields_by_type( $form, array( 'fileupload' ) , true);
if ( ! is_array( $fileupload_fields ) ) {
return $notification;
}
$notification['attachments'] = rgar( $notification, 'attachments', array() );
$upload_root = RGFormsModel::get_upload_root();
foreach( $fileupload_fields as $field ) {
$url = rgar( $entry, $field->id );
if ( empty( $url ) ) {
continue;
} elseif ( $field->multipleFiles ) {
$uploaded_files = json_decode( stripslashes( $url ), true );
foreach ( $uploaded_files as $uploaded_file ) {
$attachment = preg_replace( '|^(.*?)/gravity_forms/|', $upload_root, $uploaded_file );
$notification['attachments'][] = $attachment;
}
} else {
$attachment = preg_replace( '|^(.*?)/gravity_forms/|', $upload_root, $url );
$notification['attachments'][] = $attachment;
}
}
return $notification;
}