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

180 lines
5.2 KiB
PHP
Raw Normal View History

2018-08-06 17:36:57 +02:00
<?php
class Wiass_REST_Delivery_Process_API {
const BASE_NAME = WP_HOME . '/';
2018-08-23 07:19:01 +02:00
const FILE_KEY_NAME = 'file';
const PATH_PARTS_TO_EXTRACT = 7;
const ACCEPTANCE_STATUS_FIELD_ID = 8;
const EXPIRATION_DATE_FIELD_ID = 9;
const DECLINE_REASON_FIELD_ID = 10;
const UPLOADED_FILES_FIELD_ID = 12;
const USER_INPUT_STEP_NAME = 'Upload acceptance file';
const ACCEPT_STATUS_LABEL = 'accept';
const DECLINE_STATUS_LABEL = 'decline';
const ACCEPTABLE_STATUS = [self::ACCEPT_STATUS_LABEL, self::DECLINE_STATUS_LABEL];
2018-08-06 17:36:57 +02:00
/**
* Endpoint namespace.
*
* @var string
*/
private static $namespace = 'wiaas';
2018-10-31 10:23:59 +01:00
private static $rest_base = 'delivery';
2018-08-06 17:36:57 +02:00
public static function register_routes() {
2018-10-31 10:23:59 +01:00
register_rest_route( self::$namespace, '/' . self::$rest_base . '/next-actions', array(
2018-08-06 17:36:57 +02:00
'methods' => 'GET',
'callback' => array(__CLASS__, 'get_next_actions_for_user'),
'permission_callback' => 'is_user_logged_in'
2018-08-06 17:36:57 +02:00
) );
2018-10-31 10:23:59 +01:00
register_rest_route( self::$namespace, '/' . self::$rest_base . '/(?P<order_id>\d+)/customer-acceptance', array(
'methods' => 'GET',
'callback' => array(__CLASS__, 'get_customer_acceptance'),
2018-08-27 17:54:05 +02:00
'permission_callback' => 'is_user_logged_in'
) );
2018-10-31 10:23:59 +01:00
register_rest_route( self::$namespace, '/' . self::$rest_base . '/(?P<order_id>\d+)/customer-acceptance', array(
'methods' => 'POST',
2018-08-18 22:11:23 +02:00
'callback' => array(__CLASS__, 'submit_customer_acceptance'),
2018-08-27 17:54:05 +02:00
'permission_callback' => 'is_user_logged_in'
) );
2018-10-31 10:23:59 +01:00
register_rest_route( self::$namespace, '/' . self::$rest_base . '/(?P<order_id>\d+)/customer-acceptance/upload' , array(
'methods' => 'POST',
2018-10-31 10:23:59 +01:00
'callback' => array(__CLASS__, 'upload_customer_acceptance'),
2018-08-27 17:54:05 +02:00
'permission_callback' => 'is_user_logged_in'
) );
2018-10-30 17:20:56 +01:00
2018-10-31 10:23:59 +01:00
register_rest_route( self::$namespace, '/' . self::$rest_base . '/(?P<order_id>\d+)/customer-questionnaires', array(
2018-10-30 17:20:56 +01:00
'methods' => 'GET',
'callback' => array(__CLASS__, 'get_customer_questionnaires'),
2018-10-31 10:23:59 +01:00
'permission_callback' => 'is_user_logged_in'
2018-10-30 17:20:56 +01:00
) );
2018-10-31 10:23:59 +01:00
register_rest_route( self::$namespace, '/' . self::$rest_base . '/(?P<order_id>\d+)/customer-questionnaires/upload/(?P<action_entry_id>\d+)', array(
2018-10-30 17:20:56 +01:00
'methods' => 'POST',
'callback' => array(__CLASS__, 'upload_customer_questionnaire'),
2018-10-31 10:23:59 +01:00
'permission_callback' => 'is_user_logged_in'
2018-10-30 17:20:56 +01:00
) );
2018-08-06 17:36:57 +02:00
}
public static function get_next_actions_for_user() {
$current_user = wp_get_current_user();
$field_filters = array();
$field_filters[] = array(
'key' => 'workflow_user_id_' . $current_user->ID,
'value' => 'pending',
);
$field_filters['mode'] = 'any';
$search_criteria = array();
$search_criteria['field_filters'] = $field_filters;
$search_criteria['status'] = 'active';
$form_ids = gravity_flow()->get_workflow_form_ids();
$entries = GFAPI::get_entries(
$form_ids,
$search_criteria,
null,
null);
2018-08-06 17:36:57 +02:00
$data = array();
foreach ($entries as $entry) {
$step = gravity_flow()->get_step( $entry['workflow_step'] );
$data[] = array(
'order_id' => $entry['wiaas_delivery_order_id'],
'order_number' => $entry['wiaas_delivery_order_id'],
2018-08-06 17:36:57 +02:00
'status' => $entry['workflow_final_status'],
'step_action' => $step->get_name(),
2018-08-06 17:36:57 +02:00
);
}
2018-09-24 21:51:55 +02:00
return rest_ensure_response($data);
2018-08-06 17:36:57 +02:00
}
2018-10-30 17:20:56 +01:00
public function get_customer_questionnaires(WP_REST_Request $request) {
$order_id = absint($request['order_id']);
2018-10-31 10:23:59 +01:00
$data = Wiaas_Delivery_Process::get_customer_questionnaires_data($order_id);
2018-10-30 17:20:56 +01:00
return rest_ensure_response($data);
}
public function upload_customer_questionnaire(WP_REST_Request $request) {
2018-10-31 10:23:59 +01:00
$order_id = absint($request['order_id']);
2018-10-30 17:20:56 +01:00
2018-10-31 10:23:59 +01:00
$action_entry_id = absint($request['action_entry_id']);
2018-10-30 17:20:56 +01:00
2018-10-31 10:23:59 +01:00
Wiaas_Delivery_Process::upload_customer_questionnaire($order_id, $action_entry_id);
2018-10-30 17:20:56 +01:00
return wiaas_api_notice('INSTALLATION_ACCEPTED', 'success');
}
2018-08-23 07:19:01 +02:00
public static function get_customer_acceptance(WP_REST_Request $request){
2018-10-31 10:23:59 +01:00
$order_id = absint($request['order_id']);
2018-10-31 10:23:59 +01:00
$data = Wiaas_Delivery_Process::get_customer_acceptance_data($order_id);
2018-10-31 10:23:59 +01:00
return rest_ensure_response($data);
}
2018-10-31 10:23:59 +01:00
public static function submit_customer_acceptance(WP_REST_Request $request) {
2018-10-31 10:23:59 +01:00
$status = $request['action_type'];
$reason = $request['decline_reason'];
2018-08-23 07:19:01 +02:00
if (!in_array($status, self::ACCEPTABLE_STATUS)){
2018-09-24 21:51:55 +02:00
return wiaas_api_notice('ACCEPTANCE_STATUS_MISSING', 'error');
}
2018-08-27 17:54:05 +02:00
$installation_declined = ($status === self::DECLINE_STATUS_LABEL);
if ($installation_declined && $reason === ''){
2018-09-24 21:51:55 +02:00
return wiaas_api_notice('DECLINE_REASON_EMPTY', 'error');
}
2018-10-31 10:23:59 +01:00
$order_id = $request['order_id'];
2018-10-31 10:23:59 +01:00
if (! $installation_declined && ! Wiaas_Delivery_Process::is_customer_acceptance_uploaded($order_id)) {
return wiaas_api_notice('ACCEPTANCE_NOT_UPLOADED', 'error');
}
2018-10-31 10:23:59 +01:00
Wiaas_Delivery_Process::update_customer_acceptance_status($order_id, $status, $reason);
2018-08-27 17:54:05 +02:00
if ($installation_declined){
2018-09-24 21:51:55 +02:00
return wiaas_api_notice('INSTALLATION_DECLINED', 'success');
2018-08-27 17:54:05 +02:00
}
2018-09-24 21:51:55 +02:00
return wiaas_api_notice('INSTALLATION_ACCEPTED', 'success');
}
2018-10-31 10:23:59 +01:00
public static function upload_customer_acceptance(WP_REST_Request $request){
2018-10-31 10:23:59 +01:00
$order_id = $request['order_id'];
2018-10-31 10:23:59 +01:00
$success = Wiaas_Delivery_Process::upload_customer_acceptance_document($order_id);
2018-10-31 10:23:59 +01:00
if ($success) {
2018-09-24 21:51:55 +02:00
return wiaas_api_notice('FILE_UPLOADED','success');
}
2018-09-24 21:51:55 +02:00
return wiaas_api_notice('NOT_UPLOADED', 'error');
}
2018-08-06 17:36:57 +02:00
}