handle order actions
This commit is contained in:
@@ -26,43 +26,45 @@ class Wiass_REST_Delivery_Process_API {
|
||||
*/
|
||||
private static $namespace = 'wiaas';
|
||||
|
||||
private static $rest_base = 'delivery';
|
||||
|
||||
|
||||
public static function register_routes() {
|
||||
register_rest_route( self::$namespace, 'next-delivery-steps', array(
|
||||
register_rest_route( self::$namespace, '/' . self::$rest_base . '/next-actions', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array(__CLASS__, 'get_next_actions_for_user'),
|
||||
'permission_callback' => 'is_user_logged_in'
|
||||
) );
|
||||
|
||||
register_rest_route( self::$namespace, 'customer-acceptance/(?P<entry_id>\d+)', array(
|
||||
register_rest_route( self::$namespace, '/' . self::$rest_base . '/(?P<order_id>\d+)/customer-acceptance', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array(__CLASS__, 'get_customer_acceptance'),
|
||||
'permission_callback' => 'is_user_logged_in'
|
||||
) );
|
||||
|
||||
register_rest_route( self::$namespace, 'customer-acceptance/(?P<entry_id>\d+)', array(
|
||||
register_rest_route( self::$namespace, '/' . self::$rest_base . '/(?P<order_id>\d+)/customer-acceptance', array(
|
||||
'methods' => 'POST',
|
||||
'callback' => array(__CLASS__, 'submit_customer_acceptance'),
|
||||
'permission_callback' => 'is_user_logged_in'
|
||||
) );
|
||||
|
||||
register_rest_route( self::$namespace, 'customer-acceptance/(?P<entry_id>\d+)/upload-file' , array(
|
||||
register_rest_route( self::$namespace, '/' . self::$rest_base . '/(?P<order_id>\d+)/customer-acceptance/upload' , array(
|
||||
'methods' => 'POST',
|
||||
'callback' => array(__CLASS__, 'upload_file'),
|
||||
'callback' => array(__CLASS__, 'upload_customer_acceptance'),
|
||||
'permission_callback' => 'is_user_logged_in'
|
||||
) );
|
||||
|
||||
|
||||
register_rest_route( self::$namespace, 'customer-questionnaires/(?P<order_id>\d+)', array(
|
||||
register_rest_route( self::$namespace, '/' . self::$rest_base . '/(?P<order_id>\d+)/customer-questionnaires', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array(__CLASS__, 'get_customer_questionnaires'),
|
||||
//'permission_callback' => 'is_user_logged_in'
|
||||
'permission_callback' => 'is_user_logged_in'
|
||||
) );
|
||||
|
||||
register_rest_route( self::$namespace, 'customer-questionnaires/(?P<order_id>\d+)/upload/(?P<action_id>\d+)', array(
|
||||
register_rest_route( self::$namespace, '/' . self::$rest_base . '/(?P<order_id>\d+)/customer-questionnaires/upload/(?P<action_entry_id>\d+)', array(
|
||||
'methods' => 'POST',
|
||||
'callback' => array(__CLASS__, 'upload_customer_questionnaire'),
|
||||
//'permission_callback' => 'is_user_logged_in'
|
||||
'permission_callback' => 'is_user_logged_in'
|
||||
) );
|
||||
}
|
||||
|
||||
@@ -114,132 +116,53 @@ class Wiass_REST_Delivery_Process_API {
|
||||
|
||||
$order_id = absint($request['order_id']);
|
||||
|
||||
|
||||
$data = Wiaas_Delivery_Process::get_current_delivery_step_info($order_id);
|
||||
$data = Wiaas_Delivery_Process::get_customer_questionnaires_data($order_id);
|
||||
|
||||
return rest_ensure_response($data);
|
||||
}
|
||||
|
||||
public function upload_customer_questionnaire(WP_REST_Request $request) {
|
||||
|
||||
$action_id = absint($request['action_id']);
|
||||
$order_id = absint($request['order_id']);
|
||||
|
||||
try {
|
||||
$action_entry_id = absint($request['action_entry_id']);
|
||||
|
||||
$result = Wiaas_Document_Upload::upload_document_version();
|
||||
|
||||
$entry = GFAPI::get_entry($action_id);
|
||||
|
||||
$document_field = GFCommon::get_fields_by_type(GFAPI::get_form($entry['form_id']), 'wiaas_order_bundle_document')[0];
|
||||
|
||||
$entry[$document_field->id] = $result;
|
||||
|
||||
GFAPI::update_entry($entry);
|
||||
|
||||
Wiaas_Delivery_Process::complete_action_step($action_id);
|
||||
|
||||
return rest_ensure_response($result);
|
||||
|
||||
} catch( Exception $e) {
|
||||
}
|
||||
Wiaas_Delivery_Process::upload_customer_questionnaire($order_id, $action_entry_id);
|
||||
|
||||
return wiaas_api_notice('INSTALLATION_ACCEPTED', 'success');
|
||||
}
|
||||
|
||||
public static function get_customer_acceptance(WP_REST_Request $request){
|
||||
$entry = GFAPI::get_entry($request['entry_id']);
|
||||
if (is_wp_error($entry)){
|
||||
return wiaas_api_generate_error('Customer acceptance entry not found', 404);
|
||||
}
|
||||
|
||||
$acceptance_documents = array();
|
||||
$uploaded_files = json_decode($entry[self::UPLOADED_FILES_FIELD_ID]);
|
||||
$order_id = absint($request['order_id']);
|
||||
|
||||
foreach($uploaded_files as $file_url){
|
||||
//example of decoded url :
|
||||
//http://localhost/wp/index.php?gf-download=2018/08/rokovi-1535378841.docx&form-id=1&field-id=12&hash=1be6c30f0eeff93563b352d15fe459d5ded12ee06c2c8f36fed66b42dedf2534
|
||||
|
||||
$decoded_url = urldecode($file_url);
|
||||
$url_parts = explode('?', $decoded_url);
|
||||
$file_name_base_parts = explode('&', $url_parts[1]);
|
||||
$file_name_parts = explode('/', $file_name_base_parts[0]);
|
||||
$file_name_with_extension_parts = explode('.', $file_name_parts[2]);
|
||||
$data = Wiaas_Delivery_Process::get_customer_acceptance_data($order_id);
|
||||
|
||||
$acceptance_documents_entry = array(
|
||||
'name' => $file_name_with_extension_parts[0],
|
||||
'extension' => $file_name_with_extension_parts[1],
|
||||
'url' => $file_url
|
||||
);
|
||||
|
||||
array_push($acceptance_documents, $acceptance_documents_entry);
|
||||
}
|
||||
|
||||
$acceptance_status = 0;
|
||||
if ($entry[self::ACCEPTANCE_STATUS_FIELD_ID]){
|
||||
$acceptance_status = ($entry[self::ACCEPTANCE_STATUS_FIELD_ID] === 'accept') ? 1 : -1;
|
||||
}
|
||||
|
||||
return rest_ensure_response(array(
|
||||
'documents' => $acceptance_documents,
|
||||
'expiration' => $entry[self::EXPIRATION_DATE_FIELD_ID],
|
||||
'status' => $acceptance_status,
|
||||
'decline_reason' => $entry[self::DECLINE_REASON_FIELD_ID]
|
||||
));
|
||||
return rest_ensure_response($data);
|
||||
}
|
||||
|
||||
public static function submit_customer_acceptance(WP_REST_Request $request){
|
||||
$entry = GFAPI::get_entry($request['entry_id']);
|
||||
if (is_wp_error($entry)){
|
||||
return wiaas_api_generate_error('Customer acceptance entry not found', 404);
|
||||
}
|
||||
public static function submit_customer_acceptance(WP_REST_Request $request) {
|
||||
|
||||
$status = $request['actionType'];
|
||||
$reason = $request['declineReason'];
|
||||
$status = $request['action_type'];
|
||||
$reason = $request['decline_reason'];
|
||||
|
||||
if (!in_array($status, self::ACCEPTABLE_STATUS)){
|
||||
return wiaas_api_notice('ACCEPTANCE_STATUS_MISSING', 'error');
|
||||
}
|
||||
|
||||
$installation_declined = ($status === self::DECLINE_STATUS_LABEL);
|
||||
|
||||
$uploaded_files = json_decode($entry[self::UPLOADED_FILES_FIELD_ID]);
|
||||
|
||||
if ($installation_declined && $reason === ''){
|
||||
return wiaas_api_notice('DECLINE_REASON_EMPTY', 'error');
|
||||
}
|
||||
|
||||
if (!$installation_declined && (count($uploaded_files)===0)){
|
||||
|
||||
$order_id = $request['order_id'];
|
||||
|
||||
if (! $installation_declined && ! Wiaas_Delivery_Process::is_customer_acceptance_uploaded($order_id)) {
|
||||
return wiaas_api_notice('ACCEPTANCE_NOT_UPLOADED', 'error');
|
||||
}
|
||||
|
||||
$entry[self::DECLINE_REASON_FIELD_ID] = $reason;
|
||||
$entry[self::ACCEPTANCE_STATUS_FIELD_ID] = $status;
|
||||
|
||||
if (!GFAPI::update_entry( $entry )){
|
||||
return wiaas_api_notice('INTERNAL_SERVER_ERROR', 'error');
|
||||
}
|
||||
|
||||
//Check if step is already completed, to not submit again
|
||||
$gf_api = new Gravity_Flow_API($entry['form_id']);
|
||||
$current_step = $gf_api->get_current_step($entry);
|
||||
if ($current_step->get_name() !== self::USER_INPUT_STEP_NAME){
|
||||
return wiaas_api_notice('ACCEPTANCE_STATUS_UPDATED', 'success');
|
||||
}
|
||||
|
||||
if ( $current_step ) {
|
||||
$current_step->purge_assignees();
|
||||
$current_step->update_step_status( 'complete' );
|
||||
}
|
||||
$entry_id = $entry['id'];
|
||||
$new_step_id = $current_step->get_id() + 1;
|
||||
$new_step = $gf_api->get_step( $new_step_id, $entry );
|
||||
$feedback = sprintf( esc_html__( 'Sent to step: %s', 'gravityflow' ), $new_step->get_name() );
|
||||
$gf_api->add_timeline_note( $entry_id, $feedback );
|
||||
$gf_api->log_activity( 'workflow', 'sent_to_step', $gf_api->form_id, $entry_id, $step_id );
|
||||
gform_update_meta( $entry_id, 'workflow_final_status', 'pending' );
|
||||
$new_step->start();
|
||||
$gf_api->process_workflow( $entry_id );
|
||||
|
||||
Wiaas_Delivery_Process::update_customer_acceptance_status($order_id, $status, $reason);
|
||||
|
||||
if ($installation_declined){
|
||||
return wiaas_api_notice('INSTALLATION_DECLINED', 'success');
|
||||
@@ -247,75 +170,14 @@ class Wiass_REST_Delivery_Process_API {
|
||||
return wiaas_api_notice('INSTALLATION_ACCEPTED', 'success');
|
||||
}
|
||||
|
||||
public static function upload_file(WP_REST_Request $request){
|
||||
$files = $request->get_file_params();
|
||||
if (!$files[self::FILE_KEY_NAME]){
|
||||
return wiaas_api_notice('NO_FILES_UPLOADED', 'error');
|
||||
}
|
||||
public static function upload_customer_acceptance(WP_REST_Request $request){
|
||||
|
||||
$entry = GFAPI::get_entry($request['entry_id']);
|
||||
if (is_wp_error($entry)){
|
||||
return wiaas_api_generate_error('Customer acceptance entry not found', 404);
|
||||
}
|
||||
$order_id = $request['order_id'];
|
||||
|
||||
$form = GFAPI::get_form($entry['form_id']);
|
||||
$form_upload_path = GFFormsModel::get_upload_path( $form['id'] );
|
||||
$success = Wiaas_Delivery_Process::upload_customer_acceptance_document($order_id);
|
||||
|
||||
$target_path = $form_upload_path . '/' . date('Y') . '/' . date('m') . '/';
|
||||
wp_mkdir_p( $target_path );
|
||||
GFCommon::recursive_add_index_file( $target_path );
|
||||
|
||||
$upload_file_field = GFAPI::get_field($form['id'], self::UPLOADED_FILES_FIELD_ID);
|
||||
$file_name = sanitize_file_name($files[self::FILE_KEY_NAME]['name']);
|
||||
$file_path_details = pathinfo($file_name);
|
||||
|
||||
if ( GFCommon::file_name_has_disallowed_extension( $file_name ) ) {
|
||||
return wiaas_api_notice('INVALID_FILE_ACCEPTANCE', 'error');
|
||||
}
|
||||
$allowed_extensions = ! empty( $upload_file_field->allowedExtensions ) ? GFCommon::clean_extensions( explode( ',', strtolower( $upload_file_field->allowedExtensions ) ) ) : array();
|
||||
if ( ! empty( $allowed_extensions ) ) {
|
||||
if ( ! GFCommon::match_file_extension( $file_name, $allowed_extensions ) ) {
|
||||
return wiaas_api_notice('INVALID_FILE_ACCEPTANCE', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
$new_file_name = $file_path_details['filename'] . '-' . time() . '.' . $file_path_details['extension'];
|
||||
|
||||
// Bypasses security checks when running unit tests.
|
||||
if ( defined( 'WP_TEST_IN_PROGRESS' ) && WP_TEST_IN_PROGRESS ) {
|
||||
return wiaas_api_notice('FILE_UPLOADED', 'success');
|
||||
}
|
||||
|
||||
if ( move_uploaded_file($files[self::FILE_KEY_NAME]['tmp_name'], $target_path . $new_file_name ) ) {
|
||||
GFFormsModel::set_permissions( $target_path . $new_file_name );
|
||||
} else {
|
||||
return wiaas_api_notice('INTERNAL_SERVER_ERROR', 'error');
|
||||
}
|
||||
|
||||
//Extract path relative to the root
|
||||
//Last 6 strings (excluding last empty) are path relative to the root
|
||||
$path_parts = explode('/', $target_path);
|
||||
|
||||
$relative_path = '';
|
||||
$i = count($path_parts) - self::PATH_PARTS_TO_EXTRACT;
|
||||
while($i < count($path_parts)-1){
|
||||
$relative_path = $relative_path . $path_parts[$i] . '/';
|
||||
$i++;
|
||||
}
|
||||
|
||||
$file_url = self::BASE_NAME . $relative_path . $new_file_name;
|
||||
$url_for_download = $upload_file_field->get_download_url($file_url);
|
||||
|
||||
$uploaded_files = json_decode($entry[self::UPLOADED_FILES_FIELD_ID]);
|
||||
|
||||
if ($uploaded_files === NULL){
|
||||
$uploaded_files = [];
|
||||
}
|
||||
array_push($uploaded_files, $url_for_download);
|
||||
|
||||
$entry[self::UPLOADED_FILES_FIELD_ID] = json_encode($uploaded_files);
|
||||
|
||||
if (GFAPI::update_entry( $entry )) {
|
||||
if ($success) {
|
||||
return wiaas_api_notice('FILE_UPLOADED','success');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user