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';
}
}

View File

@@ -32,18 +32,17 @@ class Wiaas_Delivery_Process_Addon extends Gravity_Flow_Extension {
add_filter('gravityflow_assignee_choices', array ($this, 'add_orders_assignee_choices'));
add_filter('gravityflow_admin_actions_workflow_detail', array ($this, 'filter_process_send_to_step_options'), 10, 5);
add_filter('gravityflow_step_assignees', array ($this, 'maybe_assign_organization_to_step'), 10, 2);
}
public function init_ajax() {
parent::init_ajax();
// this AJAX action is here and not in /admin folder because of Gravity Forms extension logic
add_action( 'wp_ajax_wiaas_delivery_get_form', array( $this, 'ajax_get_form' ) );
}
/**
* Handle organization assignee for step (Map users from organization as assignees to step)
*
* @param $assignees
* @param Gravity_Flow_Step $step
*
* @return array
*/
public function maybe_assign_organization_to_step($assignees, Gravity_Flow_Step $step) {
$mapped_assignees = array();
@@ -78,46 +77,6 @@ class Wiaas_Delivery_Process_Addon extends Gravity_Flow_Extension {
return $mapped_assignees;
}
public function filter_process_send_to_step_options($admin_actions, $current_step, $steps, $form, $entry) {
$delivery_process_actions = array();
if ( $current_step ) {
$previous_step_id = null;
// get previous step id for current step
foreach ($steps as $index => $step) {
$next_step = gravity_flow()->get_next_step($step, $entry, $form);
if ($next_step && $next_step->get_id() === $current_step->get_id()) {
$previous_step_id = $step->get_id();
}
}
if (! empty($previous_step_id)) {
$delivery_process_actions[] = array(
'label' => esc_html__( 'Previous step', 'wiaas' ),
'value' => 'send_to_step|' . $previous_step_id
);
}
// get next step id for current step
$next_step = gravity_flow()->get_next_step($current_step, $entry, $form);
if ($next_step) {
$delivery_process_actions[] = array(
'label' => esc_html__( 'Next step', 'wiaas' ),
'value' => 'send_to_step|' . $next_step->get_id()
);
}
}
return $delivery_process_actions;
}
public function allow_only_administrator_to_be_assigned_to_step($args) {
@@ -133,34 +92,6 @@ class Wiaas_Delivery_Process_Addon extends Gravity_Flow_Extension {
return $choices;
}
public function ajax_get_form() {
$form_id = isset( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : 0;
$field_id = sanitize_text_field( rgget( 'field_id' ) );
$entry_id = absint( rgget( 'entry_id' ) );
$field_values = array( $field_id => $entry_id );
gravity_form_enqueue_scripts( $form_id, true );
$is_admin = isset( $_GET['is_admin'] );
if ( $is_admin ) {
wp_enqueue_style( 'common', site_url() . '/wp-admin/css/common.css', array(), $this->_version );
} else {
wp_enqueue_style( 'common', get_stylesheet_directory_uri() . '/style.css', array(), $this->_version );
}
wp_print_styles();
wp_print_scripts();
// Render an AJAX-enabled form.
// https://www.gravityhelp.com/documentation/article/embedding-a-form/#function-call
$html = gravity_form( $form_id, true, false, false, $field_values, true, 1, false );
printf( "<div id='wiaas-delivery-process-form' style='padding:10px;'>%s</div>", $html );
die();
}
/**
* Extends Gravity Form entry metadata with 'wiaas_delivery_process_id'
*

View File

@@ -0,0 +1,189 @@
<?php
/**
* Handle organization and order level assignment to delivery process step.
*
* Currently Gravity Flow enables assignment of roles to step, but these roles are not bounded to organization level
* so cannot be used in that form for our delivery processes.
*
* This will enable delivery process step to be assigned to order admin, order seller, order customer or
* single order supplier organization.
*
*
* Class Wiaas_Delivery_Process_Step_Assignee
*/
class Wiaas_Delivery_Process_Step_Assignee {
public static function init() {
add_filter('gravityflow_assignee_choices', array (__CLASS__, 'filter_delivery_process_step_assignee_choices'));
add_filter('gravityflow_step_assignees', array (__CLASS__, 'maybe_handle_order_organization_assignees'), 10, 2);
}
/**
* Allow only order specific organization roles and fields as choices for delivery process step assignee
*
* @param array $old_choices
*
* @return array
*/
public static function filter_delivery_process_step_assignee_choices($old_choices) {
$choices = array(
$choices[] = array(
'label' => __( 'Order', 'wiaas' ),
'choices' => array(
array(
'value' => 'role|administrator', // allow global administrator role here since it is only one
'label' => __('Administrator', 'wiaas')
)
),
)
);
// add administrator users
$args = array(
'number' => 1000,
'orderby' => 'display_name',
'role' => 'administrator'
);
$accounts = get_users( $args );
$account_choices = array();
foreach ( $accounts as $account ) {
$account_choices[] = array( 'value' => 'user_id|' . $account->ID, 'label' => $account->display_name );
}
$choices[] = array(
'label' => __( 'Users', 'gravityflow' ),
'choices' => $account_choices,
);
// append field choices
foreach ($old_choices as $old_choice) {
if ($old_choice['label'] === 'Fields') {
$choices[] = $old_choice;
break;
}
}
return $choices;
}
/**
* Handle order level assignees for delivery process step
*
* @param $assignees
*
* @param Gravity_Flow_Step $step
*
* @return array
*/
public static function maybe_handle_order_organization_assignees($assignees, Gravity_Flow_Step $step) {
$mapped_assignees = array();
$order_id = self::get_order_id_for_step($step);
$order = wc_get_order($order_id);
foreach ($assignees as $assignee) {
if (strpos($assignee->get_type(), 'wiaas_installation_') !== false) {
$item_id = $assignee->get_id();
$item = $order->get_item($item_id);
$product_id = $item['product_id'];
//TODO: Get this value from order since product can get changed or deleted later
$organization_id = Wiaas_Product_Supplier::get_supplier_organisation_id_from_product($product_id);
$user_ids = wiaas_get_organization_user_ids($organization_id);
if (empty($user_ids)) {
continue;
}
$user_id = $user_ids[0];
$mapped_assignees[] = $step->get_assignee( array(
'id' => $user_id,
'type' => 'user_id',
'editable_fields' => $assignee->get_editable_fields()
) );
continue;
}
if ($assignee->get_type() === 'wiaas_order_role') {
$order_role = $assignee->get_id();
if ($order_role === 'customer') {
$customer_user_id = $order->get_customer_id();
// for now assign only customer that create order
// check if all customer organization users should be able to see order step
$mapped_assignees[] = $step->get_assignee( array(
'id' => $customer_user_id,
'type' => 'user_id',
'editable_fields' => $assignee->get_editable_fields()
) );
continue;
}
}
$mapped_assignees[] = $assignee;
}
return $mapped_assignees;
}
/**
* Retrieve order id for delivery process step
*
* @param Gravity_Flow_Step $step
*
* @return bool|int Order id on success, false if step does not have associated order id
*
*/
public static function get_order_id_for_step(Gravity_Flow_Step $step) {
$entry = $step->get_entry();
// if order is present in entry metadata use that value
if (! empty($entry['wiaas_delivery_order_id'])) {
return absint($entry['wiaas_delivery_order_id']);
}
// try getting order id from order field
$form = GFAPI::get_form($entry['form_id']);
$order_fields = GFCommon::get_fields_by_type($form, array( 'wiaas_order'));
if (! empty($order_fields)) {
$order_field = $order_fields[0]; // there should only be one order field per entry
return absint($entry[$order_field->id]);
}
return false;
}
}
Wiaas_Delivery_Process_Step_Assignee::init();

View File

@@ -27,16 +27,7 @@ class Wiaas_Delivery_Process_Step extends Gravity_Flow_Step {
$form_choices[] = array( 'label' => esc_html__( 'Select a Form', 'wiaas' ), 'value' => '' );
foreach ( $forms as $form ) {
$delivery_settings = rgar($form, 'wiaas_delivery_process');
$code = $form['id'];
if (! empty($delivery_settings['delivery_action_code'])) {
$code = $delivery_settings['delivery_action_code'];
}
$form_choices[] = array( 'label' => $form['title'], 'value' => $code );
$form_choices[] = array( 'label' => $form['title'], 'value' => $form['id'] );
}
$settings = array(
@@ -215,26 +206,12 @@ class Wiaas_Delivery_Process_Step extends Gravity_Flow_Step {
/**
* Retrieves forms that are valid options for delivery step action
*
* @return array
*/
public function get_action_forms_choices() {
$forms = GFAPI::get_forms();
$action_forms = array();
foreach ( $forms as $form ) {
$delivery_settings = rgar($form, 'wiaas_delivery_process');
if ( ! empty($delivery_settings) && $delivery_settings['delivery_form_type'] === 'action'){
$action_forms[] = $form;
}
}
return $action_forms;
return Wiaas_Delivery_Process_Action::get_action_forms();
}

View File

@@ -13,6 +13,7 @@ class Wiaas_Field_Order_Bundle_Document extends GF_Field_FileUpload {
public function get_form_editor_field_settings() {
return array(
'wiaas_doc_type_filter',
'multiple_files_setting',
'conditional_logic_field_setting',
'error_message_setting',
'label_setting',

View File

@@ -1,112 +0,0 @@
<?php
if ( ! class_exists( 'GFForms' ) ) {
die();
}
class Wiaas_Field_Order_Bundle_Select extends GF_Field_Select {
public $type = 'wiaas_order_bundle';
public function get_form_editor_field_title() {
return esc_attr__( 'Bundle Select', 'wiaas' );
}
public function get_value_entry_list( $value, $entry, $field_id, $columns, $form ) {
return $this->get_selected_bundle_display_name($value);
}
public function get_value_merge_tag( $value, $input_id, $entry, $form, $modifier, $raw_value, $url_encode, $esc_html, $format, $nl2br ) {
return $this->get_selected_bundle_display_name($value);
}
public function get_value_entry_detail( $value, $currency = '', $use_text = false, $format = 'html', $media = 'screen' ) {
return $this->get_selected_bundle_display_name($value);
}
// public function get_field_input( $form, $value = '', $entry = null ) {
//
// $this->choices = array();
//
// $order_id = null;
//
// if (! empty($entry)) {
//
// $order_field = GFCommon::get_fields_by_type($form, array( 'wiaas_order' ) )[0];
//
// $order_id = ! empty($order_field) ? $entry[$order_field->id] : null;
//
// } else if( ! empty($value)) {
//
// list ($order_id, $item_id) = explode('|', $value);
// } else {
//
// $order_id = absint(rgget('order_id'));
// }
//
// if (! empty($order_id)) {
//
// $this->choices = $this->get_selected_bundle_display_name($order_id);
// }
//
// return parent::get_field_input( $form, $value, $entry );
// }
/**
* Adds bundles as choices
*/
public function post_convert_field() {
if ($this->is_form_editor()) {
$this->choices = array();
}
if ( ! $this->is_form_editor() && ! empty( rgget('order_id') )) {
$this->choices = $this->get_bundles_as_choices(absint(rgget('order_id')));
}
}
public function get_selected_bundle_display_name($value) {
list ($order_id, $item_id) = explode('|', $value);
if (! empty($order_id) && ! empty($item_id) && $order = wc_get_order($order_id)) {
$item = $order->get_item($item_id);
return $item->get_name();
}
return '';
}
public function get_bundles_as_choices($order_id) {
$choices = array();
if (! empty ($order_id) && $order = wc_get_order($order_id)) {
$standard_bundle_items = wiaas_get_order_standard_bundle_items($order);
foreach ($standard_bundle_items as $item) {
$choices[] = array(
'value' => $order_id . '|' . $item->get_id(),
'text' => $item->get_name()
);
}
}
return $choices;
}
}
GF_Fields::register( new Wiaas_Field_Order_Bundle_Select() );

View File

@@ -0,0 +1,50 @@
<?php
class Wiaas_Field_Order_Bundle extends GF_Field_Text {
public $type = 'wiaas_order_bundle';
public function get_form_editor_field_title() {
return esc_attr__( 'Bundle', 'wiaas' );
}
public function get_value_entry_list( $value, $entry, $field_id, $columns, $form ) {
return $this->get_bundle_display_name($value);
}
public function get_value_merge_tag( $value, $input_id, $entry, $form, $modifier, $raw_value, $url_encode, $esc_html, $format, $nl2br ) {
return $this->get_bundle_display_name($value);
}
public function get_value_entry_detail( $value, $currency = '', $use_text = false, $format = 'html', $media = 'screen' ) {
return $this->get_bundle_display_name($value);
}
public function get_bundle_item($value) {
list($order_id, $item_id) = rgexplode('|', $value, 2);
if (! empty($order_id) && ! empty($item_id) && $order = wc_get_order($order_id)) {
return $order->get_item($item_id);
}
return null;
}
public function get_bundle_display_name($value) {
$item = $this->get_bundle_item($value);
if (! empty($item)) {
return $item->get_name();
}
return '';
}
}
GF_Fields::register( new Wiaas_Field_Order_Bundle() );

View File

@@ -0,0 +1,61 @@
<?php
if ( ! class_exists( 'GFForms' ) ) {
die();
}
class Wiaas_Field_Order_Document extends GF_Field_FileUpload {
public $type ='wiaas_order_document';
public $wiaasDocTypeFilter = 'install_guide';
public function get_form_editor_field_settings() {
return array(
'wiaas_doc_type_filter',
'multiple_files_setting',
'conditional_logic_field_setting',
'error_message_setting',
'label_setting',
'label_placement_setting',
'admin_label_setting',
'rules_setting',
'file_extensions_setting',
'file_size_setting',
'visibility_setting',
'description_setting',
'css_class_setting',
);
}
public function get_input_type() {
return 'fileupload';
}
public function get_form_editor_field_title() {
return esc_attr__( 'Order Document', 'wiaas' );
}
public function get_download_url( $file, $force_download = false ) {
$upload_root = GFFormsModel::get_upload_url( $this->formId );
$upload_root = trailingslashit( $upload_root );
// handle download for order documents not uploaded by gravity forms
if ( strpos( $file, $upload_root ) === false ) {
return admin_url() . '?gf-wiaas-order-doc=' . urlencode($file);
}
return parent::get_download_url( $file, true );
}
public function sanitize_settings() {
parent::sanitize_settings();
$this->wiaasDocTypeFilter = sanitize_key($this->wiaasDocTypeFilter);
}
}
GF_Fields::register( new Wiaas_Field_Order_Document() );

View File

@@ -0,0 +1,110 @@
<?php
if ( ! class_exists( 'GFForms' ) ) {
die();
}
class Wiaas_Order_Installation_Select extends GF_Field_Select {
public $type = 'wiaas_order_installation_select';
public function get_input_type() {
return 'workflow_assignee_select';
}
public function get_field_input( $form, $value = '', $entry = null ) {
if ( empty($entry) ) {
return parent::get_field_input($form, $value, $entry);
}
// get bundle item field
$bundle_item_field = GFCommon::get_fields_by_type($form, array( 'wiaas_order_bundle') )[0];
if (! empty($bundle_item_field) &&
$bundle_item = $bundle_item_field->get_bundle_item($entry[$bundle_item_field->id])) {
$bundled_items = wc_pb_get_bundled_order_items($bundle_item);
$installation_items = array();
foreach ($bundled_items as $id => $bundled_item) {
$product = $bundled_item->get_product();
if ($product && Wiaas_Product_Category::is_installation($product)) {
$installation_items[] = $bundled_item;
}
}
$choices = array(
array( 'value' => '0', 'text' => 'Select installation ...')
);
$order_id = $bundle_item->get_order_id();
foreach ($installation_items as $installation_item) {
$choices[] = array(
'value' => 'wiaas_installation_' . $order_id . '|' . $installation_item->get_id(),
'text' => $installation_item->get_name()
);
}
$this->choices = $choices;
}
return parent::get_field_input($form, $value, $entry);
}
public function get_form_editor_field_title() {
return esc_attr__( 'Installation Select', 'wiaas' );
}
public function get_value_entry_list( $value, $entry, $field_id, $columns, $form ) {
return $this->get_selected_installation_display_name($value);
}
public function get_value_merge_tag( $value, $input_id, $entry, $form, $modifier, $raw_value, $url_encode, $esc_html, $format, $nl2br ) {
return $this->get_selected_installation_display_name($value);
}
public function get_value_entry_detail( $value, $currency = '', $use_text = false, $format = 'html', $media = 'screen' ) {
return $this->get_selected_installation_display_name($value);
}
public function get_selected_installation_display_name($value) {
$value = $value ? str_replace('wiaas_installation_', '', $value) : '';
list ($order_id, $item_id) = explode('|', $value);
if (! empty($order_id) && ! empty($item_id) && $order = wc_get_order($order_id)) {
$item = $order->get_item($item_id);
return $item->get_name();
}
return '';
}
public function post_convert_field() {
if ($this->is_form_editor()) {
$this->choices = array(
array( 'value' => '0', 'text' => 'Select installation ...')
);
}
}
}
GF_Fields::register( new Wiaas_Order_Installation_Select() );

View File

@@ -1,83 +0,0 @@
<?php
if ( ! class_exists( 'GFForms' ) ) {
die();
}
class Wiaas_Field_Order_Supplier_Select extends GF_Field_Select {
public $type = 'wiaas_order_supplier';
public function get_input_type() {
// set this input type so delivery workflow step can be assigned to supplier in this field
return 'workflow_assignee_select';
}
public function get_form_editor_field_title() {
return esc_attr__( 'Supplier Select', 'wiaas' );
}
public function get_value_entry_list( $value, $entry, $field_id, $columns, $form ) {
return $this->_get_selected_supplier_display_name($value);
}
public function get_value_merge_tag( $value, $input_id, $entry, $form, $modifier, $raw_value, $url_encode, $esc_html, $format, $nl2br ) {
return $this->_get_selected_supplier_display_name($value);
}
public function get_value_entry_detail( $value, $currency = '', $use_text = false, $format = 'html', $media = 'screen' ) {
return $this->_get_selected_supplier_display_name($value);
}
/**
* Adds bundles as choices
*/
public function post_convert_field() {
$this->choices = array();
if ( ! $this->is_form_editor() ) {
$this->choices = $this->_get_suppliers_as_choices();
}
}
private function _get_selected_supplier_display_name($value) {
list ($order_key, $supplier_id) = explode('|', $value);
if (! empty($supplier_id)) {
return wiaas_get_organization_name($supplier_id);
}
return '';
}
private function _get_suppliers_as_choices() {
$choices = array();
$order_id = absint(rgget('order_id'));
if (! empty ($order_id) && $order = wc_get_order($order_id)) {
$suppliers = wiaas_get_order_suppliers($order);
foreach ($suppliers as $supplier_id => $supplier_name) {
$choices[] = array(
'value' => 'wiaas_organization_order_' . $order_id . '|' . $supplier_id,
'text' => $supplier_name
);
}
}
return $choices;
}
}
GF_Fields::register( new Wiaas_Field_Order_Supplier_Select() );

View File

@@ -9,6 +9,21 @@ class Wiaas_Order_Fields {
public static function init() {
add_action( 'gform_field_standard_settings', array( __CLASS__, 'field_settings' ) );
add_filter('gform_fileupload_entry_value_file_path', array( __CLASS__, 'display_order_document_fields' ), 999, 2);
}
public static function display_order_document_fields($file_path, $field) {
if ($field->type === 'wiaas_order_bundle_document') {
$field = new Wiaas_Field_Order_Bundle_Document();
return $field->get_download_url($file_path);
}
return $file_path;
}
/**
@@ -30,11 +45,20 @@ class Wiaas_Order_Fields {
<option value="configuration"><?php esc_html_e( 'Configuration', 'wiaas' ); ?></option>
<option value="order_questionaire"><?php esc_html_e( 'Order Questionaire', 'wiaas' ); ?></option>
<option value="installation_protocol"><?php esc_html_e( 'Installation protocol', 'wiaas' ); ?></option>
<option value="install_guide"><?php esc_html_e( 'Installation Guide', 'wiaas' ); ?></option>
<option value="customer_acceptance"><?php esc_html_e( 'Customer acceptance', 'wiaas' ); ?></option>
</select>
</li>
<li class="wiaas_installation_organization_filter field_setting">
<input type="checkbox" id="wiaas-installation-organization-filter"
onclick="var value = jQuery(this).is(':checked'); SetFieldProperty('wiaasOnlyInstallationOrg', value);""/>
<label for="wiaas-installation-organization-filter" class="inline">
<?php esc_html_e( 'Only installation', 'wiaas' ); ?>
</label>
</li>
<?php
}
}
@@ -73,8 +97,6 @@ class Wiaas_Order_Fields {
$order = wc_get_order($order_id);
$bundle_item = $order->get_item($bundle_item_id);
$entry = array();
foreach ($form['fields'] as $field) {
@@ -87,8 +109,15 @@ class Wiaas_Order_Fields {
break;
case 'workflow_user':
$entry[(string) $field->id] = $order->get_customer_id(); // save customer if needed
break;
case 'wiaas_order_bundle':
$bundle_item = $order->get_item($bundle_item_id);
if ( empty($bundle_item) && $field->isRequired) {
// there is no data for required field so entry cannot be created
return false;
@@ -103,6 +132,8 @@ class Wiaas_Order_Fields {
case 'wiaas_order_bundle_document':
$bundle_item = $order->get_item($bundle_item_id);
if ( empty($bundle_item) && $field->isRequired) {
// there is no data for required field so entry cannot be created
return false;
@@ -110,31 +141,80 @@ class Wiaas_Order_Fields {
if (! empty($bundle_item)) {
$documents = wiaas_get_order_item_documents($bundle_item, $field->wiaasDocTypeFilter);
$documents = wiaas_get_standard_package_order_item_documents($order, $bundle_item_id);
if ( empty($documents) && $field->isRequired) {
$filtered_documents = array();
foreach ($documents as $document) {
if ($document['type'] === $field->wiaasDocTypeFilter) {
$filtered_documents[] = $document;
}
}
if ( empty($filtered_documents) && $field->isRequired ) {
// there is no data for required field so entry cannot be created
return false;
}
if (! empty($documents)) {
if (! empty ($filtered_documents) && ! $field->multipleFiles) {
$document = $documents[0];
$document = $filtered_documents[0];
$entry[$field->id] = $document['version'];
} else if (! empty ($filtered_documents) ) {
$versions = array();
foreach ($filtered_documents as $filtered_document) {
$versions[] = $filtered_document['version'];
}
$entry[$field->id] = json_encode($versions);
}
}
break;
case 'wiaas_order_supplier':
$suppliers = wiaas_get_order_suppliers($order);
case 'wiaas_order_installation_select':
$supplier_ids = array_keys($suppliers);
$bundle_item = $order->get_item($bundle_item_id);
$supplier_id = $supplier_ids[0];
if ( empty($bundle_item) && $field->isRequired) {
// there is no data for required field so entry cannot be created
return false;
}
$entry[(string) $field->id] = 'wiaas_organization_order_' . $order_id . '|' . $supplier_id;
$bundled_items = wc_pb_get_bundled_order_items($bundle_item, $order);
$installation_items = array();
foreach ($bundled_items as $id => $bundled_item) {
$product = $bundled_item->get_product();
if ($product && Wiaas_Product_Category::is_installation($product)) {
$installation_items[] = $bundled_item;
}
}
if (empty($installation_items) && $field->isRequired) {
// there is no data for required field so entry cannot be created
return false;
}
if (count($installation_items) === 1) {
$installation_item = $installation_items[0];
$entry[(string) $field->id] = 'wiaas_installation_' . $order->get_id() . '|' . $installation_item->get_id();
} else if (count($installation_items) > 1) {
// force admin to select installation
$entry[(string) $field->id] = '0';
}
}
}