Commercial lead prices #33

Merged
akrdzic merged 11 commits from commercial-lead-prices into master 2018-10-17 11:29:55 +02:00
34 changed files with 1748 additions and 287 deletions

View File

@@ -128,4 +128,9 @@
#your-profile label[for="role"], #your-profile label[for="url"],#your-profile #role, #your-profile #url{
display: none;
}
#adminmenu #toplevel_page_wiaas-cl-packages div.wp-menu-image::before {
font-family: WooCommerce!important;
content: '\e006';
}

View File

@@ -0,0 +1,7 @@
#posts-filter .tablenav .actions {
display: none;
}
#menu-posts-product .wp-submenu li:last-child {
display: none;
}

View File

@@ -0,0 +1,71 @@
jQuery(document).ready(function ($) {
$('#tabs').each(function() {
var disabled = $( this ).data('disabled') || '';
$( this ).tabs({
disabled: [ disabled ]
});
});
$('#wiaas_add_cl_customer_extras').click(function(e) {
e.preventDefault();
var customer_id = $('#wiaas_cl_customers').val();
if (!customer_id || customer_id === '0') {
return;
}
$.post(window.ajaxurl, {
action: 'wiaas_create_cl_customer_extras',
_ajax_nonce: $('#wiaas_create_cl_customer_extras_nonce').val(),
customer_id: customer_id,
package_id: $('#wiaas_cl_package_id').val()
}).done( function (result) {
$('#tabs-2').append(result);
$('#wiaas_cl_customer_' + customer_id).prop( 'disabled', true );
});
$('#wiaas_cl_customers').val('0');
});
$('#wiaas_package_extras').delegate('.wiaas_remove_cl_extras', 'click', function(e) {
e.preventDefault();
var customer_id = $( this ).data('customer_id');
$('#extras_customer_' + customer_id).remove();
$('#wiaas_cl_customer_' + customer_id).prop( 'disabled', false );
$('#wiaas_cl_customers').val('0');
});
$('#wiaas_package_extras').delegate('.wiaas-cl-extra-input', 'change', function(e) {
e.preventDefault();
var val = parseFloat($( this ).val());
var target = '#' + $( this).data('target');
if (isNaN(val)) {
$(target).val('Invalid!');
return;
}
var type = $( this).data('type');
if (type === 'fixed') {
$(target).text( $(target).data('base') + val);
}
if (type === 'recurrent' || type === 'services') {
$(target).data(type, val);
$(target).text( $(target).data('base') + $(target).data('recurrent') + $(target).data('services'));
}
});
});

View File

@@ -0,0 +1,260 @@
<?php
/**
* Class Wiaas_Admin_CM_Packages
*
*/
class Wiaas_Admin_CL_Packages {
/**
* Handles output of Commercial Lead packages page in admin panel.
*
* Shows list of packages available for sale and packages sold by current commercial lead.
* Enables adding package to commercial lead catalogue and setting package specific prices
* for current commercial lead.
*
*/
public static function init() {
add_action( 'admin_enqueue_scripts', array(__CLASS__, 'enqueue_scripts'), 100 );
add_action( 'admin_menu', array(__CLASS__, 'add_cl_packages_menu') );
add_action( 'admin_head', array( __CLASS__, 'menu_highlight' ) );
add_filter( 'manage_product_posts_columns', array( __CLASS__, 'define_list_table_products_columns' ), 999 );
add_filter( 'bulk_actions-edit-product', array( __CLASS__, 'define_list_table_products_bulk_actions' ), 999 );
add_filter( 'views_edit-product', array( __CLASS__, 'define_list_table_products_views' ), 999 );
add_filter( 'post_row_actions', array( __CLASS__, 'define_list_table_products_row_actions' ), 999, 2 );
add_filter( 'list_table_primary_column', array( __CLASS__, 'define_list_table_products_primary_column' ), 999, 2 );
add_action( 'manage_product_posts_custom_column', array( __CLASS__, 'render_list_table_products_columns' ), 10, 2 );
}
public static function enqueue_scripts() {
$plugin_url = untrailingslashit( plugins_url( '/', WIAAS_FILE ) );
wp_enqueue_style( 'wiaas_admin_menu', $plugin_url . '/assets/css/menu.css' );
}
public static function add_cl_packages_menu() {
add_submenu_page(
'edit.php?post_type=product',
__( 'Products', 'wiaas' ),
null,
'manage_wiaas_cl_products',
'wiaas-cl-package',
array(__CLASS__, 'output_package')
);
}
public static function menu_highlight() {
global $parent_file;
$screen = get_current_screen();
if ($screen->id === 'admin_page_wiaas-cl-product') {
error_log('set parent');
$parent_file = 'edit.php?post_type=product';
}
}
public static function output_package() {
wp_enqueue_script( 'jquery-ui-tabs' );
$package_id = absint( $_GET['cl_package_id'] );
$package = wc_get_product($package_id);
if (!$package) {
return;
}
if (! empty($_POST['wiaas_save_cl_extras_nonce']) && ! empty($_POST['cl_extras'])) {
if (wp_verify_nonce($_POST['wiaas_save_cl_extras_nonce'], 'wiaas_save_cl_extras')) {
self::_handle_cl_extras_update($package_id);
}
}
$bundled_items = $package->get_bundled_items();
$bundled_items_per_category = array();
foreach ($bundled_items as $bundled_item) {
$category = Wiaas_Product_Category::get_category($bundled_item->product);
if (isset($category)) {
if ($category ==='hardware' || $category === 'software') {
$category = 'products';
}
$bundled_items_per_category[$category] ?: array();
$bundled_items_per_category[$category][] = $bundled_item;
}
}
$configured_prices = Wiaas_Package_Pricing::get_package_prices($package);
$cl_id = wiaas_get_current_user_organization_id();
$cl_extras = Wiaas_Package_CL_Pricing::get_extras($cl_id, $package_id);
$has_default_cl_extras = ! empty($cl_extras);
// default values for catalogue package
if (! $has_default_cl_extras) {
$cl_extras = array();
foreach ($configured_prices as $type => $configured_price) {
$cl_extras[$type.'_default'] = array(
'visible' => true,
'fixed' => 0,
'services' => 0,
'recurrent' => 0
);
}
}
$package_cl_extra_types = array_keys($cl_extras);
$customer_ids_with_extras = array();
foreach ($package_cl_extra_types as $package_cl_extra_type) {
$customer_id_with_extras = explode('_customer_', $package_cl_extra_type)[1];
if (isset($customer_id_with_extras) && ($customer_id_with_extras = absint($customer_id_with_extras)) > 0) {
$customer_ids_with_extras[] = $customer_id_with_extras;
}
}
$customer_ids_with_extras = array_unique($customer_ids_with_extras);
require 'views/html-cl-package-details.php';
}
/**
* @section Customize display of list table products for commercial lead
*/
/**
* Define columns for commercial lead view
* @param array $columns
*
* @return array
*/
public static function define_list_table_products_columns($columns) {
$cl_columns = array();
$cl_columns['thumb'] = $columns['thumb'];
$cl_columns['wiaas_cl_name'] = $columns['name'];
$cl_columns['taxonomy-package_type'] = __('Type', 'wiaas');
$cl_columns['taxonomy-package_status'] = __('Status', 'wiaas');
$cl_columns['taxonomy-product_country'] = $columns['taxonomy-product_country'];
$cl_columns['date'] = $columns['date'];
return $cl_columns;
}
/**
* Hide list table products bulk actions for commercial lead view
*
* @return array
*/
public static function define_list_table_products_bulk_actions() {
return array();
}
/**
* Hide list table products views for commercial lead view
*
* @return array
*/
public static function define_list_table_products_views() {
return array();
}
/**
* Show only ID in table products views for commercial lead view
*
* @return array
*/
public static function define_list_table_products_row_actions($actions, $post) {
if ($post->post_type === 'product') {
return array( 'id' => sprintf( __( 'ID: %d', 'woocommerce' ), $post->ID ) );
}
return $actions;
}
/**
* Define custom primary column in list table products for commercial lead view
*
* @param $default
* @param $screen_id
*
* @return string
*/
public static function define_list_table_products_primary_column($default, $screen_id) {
if ( 'edit-product' === $screen_id ) {
return 'wiaas_cl_name';
}
return $default;
}
public static function render_list_table_products_columns($column, $post_id) {
if ($column === 'wiaas_cl_name') {
$package = wc_get_product($post_id);
$edit_link = admin_url( 'edit.php?post_type=product&page=wiaas-cl-package&cl_package_id=' . absint( $package->get_id() ) );
echo '<strong><a class="row-title" href="' . esc_url( $edit_link ) . '">' . esc_html( $package->get_title() ) . '</a></strong>';
}
}
// PRIVATE HANDLERS
private static function _handle_cl_extras_update($package_id) {
$cl_extras = wp_unslash($_POST['cl_extras']);
$pay_types = array_keys(Wiaas_Package_Pricing::get_available_pay_types());
$posted_cl_extras = array();
foreach ($cl_extras as $cl_extra) {
$customer_id = isset($cl_extra['customer']) ? absint($cl_extra['customer']) : 0;
$type = sanitize_key($cl_extra['type']);
$fixed_extra = isset($cl_extra['fixed']) ? floatval($cl_extra['fixed']) : 0;
$services_extra = isset($cl_extra['services']) ? floatval($cl_extra['services']) : 0;
$recurrent_extra = isset($cl_extra['recurrent']) ? floatval($cl_extra['recurrent']) : 0;
$extra_type = $customer_id > 0 ? $type.'_customer_'.$customer_id : $type.'_default';
if (in_array($type, $pay_types)) {
$posted_cl_extras[$extra_type] = array(
'type' => $type,
'visible' => $cl_extra['visible'] === 'on',
'fixed' => $fixed_extra,
'services' => $services_extra,
'recurrent' => $recurrent_extra,
);
}
}
Wiaas_Package_CL_Pricing::set_extras(
wiaas_get_current_user_organization_id(),
$package_id,
$posted_cl_extras
);
}
}
Wiaas_Admin_CL_Packages::init();

View File

@@ -0,0 +1,237 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
?>
<style>
#tabs-navigation {
height: 30px;
}
#tabs-navigation > li {
float: left;
line-height: 30px;
font-weight: bold;
}
#tabs-navigation > li.ui-state-active > a {
color: #000;
}
#tabs-navigation > li.ui-state-disabled > a {
color: #ccc;
cursor: default;
}
#tabs-navigation > li > a {
text-decoration: none;
padding: .5em;
}
.wiaas-configured-prices > thead {
background: #f2f2f2;
}
.wiaas-configured-prices > thead td {
vertical-align: middle;
position: relative;
}
.wiaas-configured-prices > thead i {
position: absolute;
vertical-align: top;
right: -8px;
top: -8px;
cursor: pointer;
}
.wiaas-configured-prices > tbody > tr > td {
vertical-align: middle;
}
.wiaas-configured-prices > tbody > tr > td:first-child {
max-width: 100px;
vertical-align: middle;
}
.wiaas-configured-prices > tbody > tr > td:nth-child(2) label {
float: left;;
width: 70px;
}
.wiaas-configured-prices input[type='text'] {
width: 100px;
}
#managed_36 > td:nth-child(2) > div:last-child {
position: relative;
padding-bottom: 20px;
}
#managed_36 > td:nth-child(2) > div:last-child > span {
position: absolute;
bottom: 0;
left: 70px;
}
</style>
<script>
</script>
<div class="wrap">
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-2">
<div id="postbox-container-1" class="postbox-container">
<div class="postbox">
<div class="inside">
<div class="panel-wrap">
<div class="panel">
<div>
<?php
foreach ($bundled_items_per_category as $category => $category_bundled_items) {
echo sprintf(
'<strong style="text-transform: uppercase;" class="text-uppercase">%s (%s)</strong>',
esc_attr( $category ),
esc_attr(count($category_bundled_items)) );
foreach ($category_bundled_items as $bundled_item) {
$title = $bundled_item->product->get_title();
echo sprintf( '<p>%s x %s</p>', esc_attr($bundled_item->get_quantity('max')), esc_attr( $title ) );
}
}
?>
</div>
</div>
</div>
</div>
</div>
</div>
<div id="postbox-container-2" class="postbox-container">
<div class="postbox">
<div class="inside">
<div class="panel-wrap">
<div class="panel">
<h1 style="padding: 0;"><?php echo $package->get_name(); ?></h1>
</div>
</div>
</div>
</div>
<?php
if (! empty($package->get_short_description())) {
?>
<div class="postbox">
<?php wp_editor( $package->get_short_description(), 'content', array(
'_content_editor_dfw' => $_content_editor_dfw,
'drag_drop_upload' => false,
'media_buttons' => false,
'quicktags' => false,
'tabfocus_elements' => 'content-html',
'tinymce' => array(
'resize' => false,
'wp_autoresize_on' => true,
'add_unload_trigger' => false,
'wp_keep_scroll_position' => ! $is_IE,
'readonly'=> true,
'toolbar' => false,
),
) ); ?>
</div>
<?php
}
?>
<div class="postbox">
<div class="inside">
<div class="panel-wrap">
<div class="panel">
<div class="wrap">
<div>
<i style="vertical-align: middle;" class="dashicons dashicons-info"></i>
<strong><?php esc_html_e('EPR', 'wiaas') ?></strong>
<span><?php esc_html_e('= Extra package recurrent commission', 'wiaas') ?></span>
</div>
<div>
<i style="vertical-align: middle;" class="dashicons dashicons-info"></i>
<strong><?php esc_html_e('ESR') ?></strong>
<span><?php esc_html_e('= Extra services and support recurrent commission', 'wiaas') ?></span>
</div>
<div>
<i style="vertical-align: middle;" class="dashicons dashicons-info"></i>
<strong><?php esc_html_e('Extra commission', 'wiaas') ?></strong>
<span><?php esc_html_e('= EPR + ESR', 'wiaas') ?></span>
</div>
</div>
<div id="tabs" data-disabled="<?php echo $has_default_cl_extras ? '' : '1' ; ?>">
<ul id="tabs-navigation">
<li><a href="#tabs-1"><?php esc_html_e('Default prices', 'wiaas') ?></a> | </li>
<li><a href="#tabs-2"><?php esc_html_e('Customer specific prices', 'wiaas') ?></a></li>
</ul>
<form id="wiaas_package_extras" action="" method="post">
<input type="hidden" name="page" value="wiaas-cl-package"/>
<input type="hidden" id="wiaas_cl_package_id" name="cl_package_id" value="<?php echo esc_attr($package->get_id()) ?>"/>
<div id="tabs-1">
<?php require 'html-cl-package-prices.php'; ?>
</div>
<div id="tabs-2">
<div class="form-group">
<select id="wiaas_cl_customers" class="form-control">
<option selected value="0" disabled> Select customer ...</option>
<?php
$customers = wiaas_get_customers();
foreach ($customers as $id => $name) {
// shik current organization if it has role of customer
if ($id === $cl_id) {
continue;
}
?>
<option
<?php disabled(in_array($id, $customer_ids_with_extras), true, true) ?>
id="wiaas_cl_customer_<?php esc_attr_e($id, 'wiaas') ?>"
value="<?php esc_attr_e($id, 'wiaas') ?>"
>
<?php esc_html_e($name, 'wiaas') ?>
</option>
<?php
}
?>
</select>
<?php wp_nonce_field( 'wiaas_create_cl_customer_extras', 'wiaas_create_cl_customer_extras_nonce' ); ?>
<button id="wiaas_add_cl_customer_extras" class="button">Add</button>
</div>
<?php
// Render customer specific prices if any
foreach ($customer_ids_with_extras as $customer_id) {
require 'html-cl-package-prices.php';
}
?>
</div>
<?php wp_nonce_field( 'wiaas_save_cl_extras', 'wiaas_save_cl_extras_nonce' ); ?>
<?php submit_button( __( 'Save', 'wiaas' ), 'primary', 'submit' ); ?>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<br class="clear" />
</div>
</div>

View File

@@ -0,0 +1,162 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
$id = isset($customer_id) ? 'extras_customer_'.$customer_id : 'extras_default';
?>
<table id="<?php esc_attr_e($id, 'wiaas') ?>" style="margin-top: 20px;" class="widefat wiaas-configured-prices">
<thead>
<tr>
<td>
<?php
if (isset($customer_id)) {
echo '<h4>'. esc_html(wiaas_get_organization_name($customer_id)). '</h4>';
}
?>
</td>
<td><?php esc_html_e('Minimal sell price', 'wiaas') ?></td>
<td><?php esc_html_e('Extra commision', 'wiaas') ?></td>
<td><?php esc_html_e('Default price', 'wiaas') ?></td>
<td><?php esc_html_e('Visible?', 'wiaas') ?>
<?php
if (isset($customer_id)) {
?>
<i
data-customer_id="<?php esc_attr_e($customer_id, 'wiaas') ?>"
class="dashicons dashicons-dismiss wiaas_remove_cl_extras"
></i>
<?php
}
?>
</td>
</tr>
</thead>
<tbody>
<?php
foreach ($configured_prices as $type => $configured_price) {
$extra_type = isset($customer_id) ? $type.'_customer_'.$customer_id : $type.'_default';
?>
<input
type="hidden"
name="cl_extras[<?php esc_attr_e($extra_type, 'wiaas')?>][type]"
value="<?php esc_attr_e($type, 'wiaas')?>"
>
<?php
if (isset($customer_id)) {
?>
<input
type="hidden"
name="cl_extras[<?php esc_attr_e($extra_type, 'wiaas')?>][customer]"
value="<?php esc_attr_e($customer_id, 'wiaas')?>"
>
<?php
}
?>
<tr id="<?php esc_attr_e($extra_type, 'wias')?>">
<td>
<strong><?php esc_html_e($configured_price['payment_type']) ?></strong>
</td>
<td class="form-group">
<div class="form-control">
<label>Fixed:</label>
<input type="text" value="<?php esc_attr_e($configured_price['minimal_fixed_price'], 'wiaas') ?>" readonly>
</div>
<div class="form-control">
<label>Monthly:</label>
<input type="text" value="<?php esc_attr_e($configured_price['minimal_recurrent_price'], 'wiaas') ?>" readonly>
<?php
if($configured_price['package_pay_period'] > 0) {
echo sprintf( '<span>(%s for services)</span>', esc_attr( $configured_price['minimal_services_price'] ) );
}
?>
</div>
</td>
<td class="form-group">
<div class="form-control">
<label>+</label>
<input
class="wiaas-cl-extra-input"
data-target="wiaas_cl_fixed_extra_<?php esc_attr_e($extra_type, 'wiaas') ?>"
data-type="fixed"
name="cl_extras[<?php esc_attr_e($extra_type, 'wiaas')?>][fixed]"
value="<?php esc_attr_e($cl_extras[$extra_type]['fixed'], 'wiaas') ?>"
type="text"
>
</div>
<?php
if($configured_price['package_pay_period'] > 0) {
?>
<div class="form-control">
<label>+</label>
<input
class="wiaas-cl-extra-input"
data-target="wiaas_cl_recurrent_extra_<?php esc_attr_e($extra_type, 'wiaas') ?>"
data-type="recurrent"
name="cl_extras[<?php esc_attr_e($extra_type, 'wiaas')?>][recurrent]"
value="<?php esc_attr_e($cl_extras[$extra_type]['recurrent'], 'wiaas') ?>"
type="text"
>
<label><?php esc_html_e('(EPR)', 'wiaas') ?></label>
</div>
<?php
}
?>
<div class="form-control">
<label>+</label>
<input
class="wiaas-cl-extra-input"
data-target="wiaas_cl_monthly_extra_<?php esc_attr_e($extra_type, 'wiaas') ?>"
data-type="services"
name="cl_extras[<?php esc_attr_e($extra_type, 'wiaas')?>][services]"
value="<?php esc_attr_e($cl_extras[$extra_type]['services'], 'wiaas') ?>"
type="text"
>
<label><?php esc_html_e('(ESR)', 'wiaas') ?></label>
</div>
</td>
<td>
<div> =
<strong
id="wiaas_cl_fixed_extra_<?php esc_attr_e($extra_type, 'wiaas') ?>"
data-base="<?php esc_attr_e($configured_price['minimal_fixed_price'], 'wiaas') ?>"
>
<?php esc_html_e($configured_price['minimal_fixed_price'] + $cl_extras[$extra_type]['fixed'], 'wiaas') ?>
</strong>
</div>
<div> =
<strong
id="wiaas_cl_monthly_extra_<?php esc_attr_e($extra_type, 'wiaas') ?>"
data-base="<?php esc_attr_e($configured_price['minimal_recurrent_price'], 'wiaas') ?>"
data-recurrent="<?php esc_attr_e($cl_extras[$extra_type]['recurrent'], 'wiaas') ?>"
data-services="<?php esc_attr_e($cl_extras[$extra_type]['services'], 'wiaas') ?>"
>
<?php
esc_html_e($configured_price['minimal_recurrent_price'] +
$cl_extras[$extra_type]['recurrent'] +
$cl_extras[$extra_type]['services'], 'wiaas')
?>
</div>
</td>
<td class="form-group">
<input
name="cl_extras[<?php esc_attr_e($extra_type, 'wiaas')?>][visible]"
<?php checked($cl_extras[$extra_type]['visible'], true, true) ?>
type="checkbox"
>
</td>
</tr>
<?php
}
?>
</tbody>
</table>

View File

@@ -0,0 +1,31 @@
<?php
add_action('wp_ajax_wiaas_create_cl_customer_extras', 'wiaas_ajax_create_cl_customer_extras');
function wiaas_ajax_create_cl_customer_extras() {
check_ajax_referer('wiaas_create_cl_customer_extras');
$customer_id = absint($_POST['customer_id']);
$package_id = absint($_POST['package_id']);
$package = wc_get_product($package_id);
$configured_prices = Wiaas_Package_Pricing::get_package_prices($package);
$cl_extras = array();
foreach ($configured_prices as $type => $configured_price) {
$cl_extras[$type.'_customer_'.$customer_id] = array(
'type' => $type,
'visible' => true,
'fixed' => 0,
'services' => 0,
'recurrent' => 0
);
}
require_once 'views/html-cl-package-prices.php';
die();
}

View File

@@ -0,0 +1,40 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class Wiaas_Admin_CL {
public static function init() {
add_action('init', array(__CLASS__, 'init_admin_cl'));
}
public static function init_admin_cl() {
$user = wp_get_current_user();
$role = $user->roles[0];
if ($role === 'commercial_lead') {
require_once dirname( __FILE__ ) . '/admin-cl/class-wiaas-admin-cl-packages.php';
require_once dirname( __FILE__ ) . '/admin-cl/wiaas-admin-cl-packages-ajax.php';
add_action( 'admin_enqueue_scripts', array(__CLASS__, 'enqueue_scripts'), 100 );
}
}
public static function enqueue_scripts() {
$plugin_url = untrailingslashit( plugins_url( '/', WIAAS_FILE ) );
wp_enqueue_script( 'wiaas_admin_cl_packages', $plugin_url . '/assets/js/wiaas-admin-cl-packages.js' );
wp_enqueue_style( 'wiaas_admin_cl', $plugin_url . '/assets/css/wiaas-admin-cl.css' );
}
}
Wiaas_Admin_CL::init();

View File

@@ -26,6 +26,13 @@ class Wiaas_Admin_Organization {
return array();
}
/**
* Render user organization roles as available user roles on user list
* @param $role_list
* @param $user
*
* @return mixed
*/
public static function get_role_list_for_user($role_list, $user) {
$organization_id = wiaas_get_user_organization_id($user->ID);
@@ -44,6 +51,13 @@ class Wiaas_Admin_Organization {
return $role_list;
}
/**
* Customize columns for users table list view
*
* @param array $defaults
*
* @return array
*/
public static function manage_users_table_columns( $defaults = array() ) {
$defaults['role'] = __('Roles', 'wiaas');

View File

@@ -7,16 +7,10 @@ if ( ! defined( 'ABSPATH' ) ) {
class Wiaas_Admin_Package {
public static function init() {
add_action( 'admin_enqueue_scripts', array(__CLASS__, 'enqueue_scripts'), 100 );
require_once dirname( __FILE__ ) . '/package/class-wiaas-admin-linked-packages.php';
require_once dirname( __FILE__ ) . '/package/class-wiaas-admin-package-types.php';
}
public static function enqueue_scripts() {
$plugin_url = untrailingslashit( plugins_url( '/', WIAAS_FILE ) );
wp_enqueue_style( 'wiaas_admin_styles', $plugin_url . '/assets/css/package.css' );
}
}
Wiaas_Admin_Package::init();

View File

@@ -6,13 +6,7 @@ if ( ! defined( 'ABSPATH' ) ) {
<?php
$value = $pricing_rule['minimal_services_price'];
if ($pricing_rule['principal_amount'] > 0 && $pay_type['package_pay_period'] > 0) {
$value += wiaas_PMT(
Wiaas_Pricing::INTEREST_RATE,
$pay_type['package_pay_period'],
$pricing_rule['principal_amount']);
}
$value = $pricing_rule['minimal_recurrent_price'];
?>
<tr>

View File

@@ -0,0 +1,79 @@
<?php
class Wiaas_Package_API {
private static $namespace = 'wiaas';
public static function init() {
add_filter('woocommerce_rest_product_object_query', array(__CLASS__, 'filter_packages'), 10, 2);
}
public static function register_routes() {
// TODO: Handle this when assigment of customer to commercial lead is done
register_rest_route( self::$namespace, '/commercial-leads', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array(__CLASS__, 'get_customer_commercial_leads'),
'permission_callback' => 'is_user_logged_in'
) );
}
// TODO: Handle this when assigment of customer to commercial lead is done
public static function get_customer_commercial_leads() {
$commercial_leads = array();
foreach (wiaas_get_commercial_leads() as $id => $name) {
$commercial_leads[] = array(
'id' => $id,
'name' => $name
);
}
return rest_ensure_response($commercial_leads);
}
/**
* Filter woocommerce REST API query so only valid wiaas packages are returned to the customer
*
* @param $args
* @param $request
*
* @return mixed
*/
public static function filter_packages($args, $request) {
if ( empty($query['tax_query']) ){
$query['tax_query'] = array();
}
// Retrieve only packages with available package status
$query['tax_query'][] = array(
'taxonomy' => 'package_status',
'field' => 'name',
'terms' => Wiaas_Package_Status::AVAILABLE
);
$commercial_lead_id = absint($request['cl_id']);
$customer_id = wiaas_get_current_user_organization_id();
$pay_types = array_keys(Wiaas_Package_Pricing::get_available_pay_types());
$price_search_terms = array();
foreach ($pay_types as $pay_type) {
$price_search_terms[] = '_' . $commercial_lead_id . '_' . $pay_type . '_default';
$price_search_terms[] = '_' . $commercial_lead_id . '_' . $pay_type . '_customer_' . $customer_id;
}
$args['tax_query'][] = array(
'taxonomy' => '_wiaas_shop_prices',
'terms' => $price_search_terms,
'field' => 'slug'
);
return $args;
}
}
Wiaas_Package_API::init();

View File

@@ -0,0 +1,51 @@
<?php
class Wiaas_Access_Management {
public static function init() {
add_action( 'save_post', array( __CLASS__, 'maybe_handle_product_access' ), 999, 2 );
}
/**
* Automatize access control for product and packages
*
* @param int $post_id
* @param WP_Post $post
*/
public static function maybe_handle_product_access($post_id, $post) {
// $post_id and $post are required
if ( empty( $post_id ) || empty( $post ) || $post->post_type !== 'product') {
return;
}
$product = wc_get_product($post_id);
$admin_access_group = Groups_Group::read_by_name('admin');
$access_group_ids = array();
if ($admin_access_group) {
$access_group_ids[] = $admin_access_group->group_id;
}
// allow commercial lead to see published bundle products
$cl_access_group = Groups_Group::read_by_name('commercial_lead');
if ($product->get_type() === 'bundle' &&
$product->get_status() === 'publish' &&
$cl_access_group) {
$access_group_ids[] = $cl_access_group->group_id;
}
Groups_Post_Access::update(
array(
'post_id' => $product->get_id(),
'groups_read' => $access_group_ids
) );
}
}
Wiaas_Access_Management::init();

View File

@@ -20,13 +20,17 @@ class Wiaas_Admin {
// Admin organization interface
require_once dirname(__FILE__) . '/admin/class-wiaas-admin-organization.php';
require_once dirname(__FILE__) . '/admin/class-wiaas-admin-cl.php';
add_action( 'admin_enqueue_scripts', array(__CLASS__, 'enqueue_scripts'), 100 );
}
public static function enqueue_scripts() {
$plugin_url = untrailingslashit( plugins_url( '/', WIAAS_FILE ) );
wp_enqueue_style( 'wiaas_admin_menu', $plugin_url . '/assets/css/menu.css' );
wp_enqueue_style( 'wiaas_admin_packages', $plugin_url . '/assets/css/package.css' );
}
}

View File

@@ -43,6 +43,8 @@ class Wiaas_API {
include_once dirname( __FILE__ ) . '/api/class-wiaas-order-projects-api.php';
include_once dirname( __FILE__ ) . '/api/class-wiaas-package-api.php';
// API functions
include_once dirname( __FILE__ ) . '/api/wiaas-api-functions.php';
@@ -55,7 +57,8 @@ class Wiaas_API {
'Wiaas_Document_API',
'Wiass_REST_User_API',
'Wiaas_REST_Customer_API',
'Wiaas_Order_Projects_API'
'Wiaas_Order_Projects_API',
'Wiaas_Package_API'
);
foreach ( $controllers as $controller ) {

View File

@@ -144,8 +144,12 @@ class Wiaas_Cart {
// Retrieve package country
$country = Wiaas_Countries::get_package_country($package);
// Retrieve package price
$package_prices = Wiaas_Pricing::get_standard_package_customer_prices($package);
// TODO: Change this so commercial lead is sent via request
$customer_id = wiaas_get_current_user_organization_id();
$commercial_lead_id = array_keys(wiaas_get_commercial_leads())[0];
// Retrieve package price
$package_prices = Wiaas_Pricing::get_standard_package_customer_prices($package, $customer_id, $commercial_lead_id);
$selected_price_index = array_search($price_id, array_column($package_prices, 'id'));
// Initialize additional cart item data for wiaas packages
@@ -644,6 +648,10 @@ class Wiaas_Cart {
$addon_items_keys = array();
$option_items_keys = array();
// TODO: Change this so commercial lead is sent via request
$customer_id = wiaas_get_current_user_organization_id();
$commercial_lead_id = array_keys(wiaas_get_commercial_leads())[0];
// Try adding package addons to cart
foreach ($addons_ids as $addon_id) {
//Check if addon package exists
@@ -652,8 +660,13 @@ class Wiaas_Cart {
throw new Exception( __( 'Sorry, additional package does not exist.', 'wiaas' ) );
}
// Retrieve addon package price
$package_prices = Wiaas_Pricing::get_addon_package_customer_price($addon_package, $parent_item['data']);
// Retrieve addon package price
$package_prices = Wiaas_Pricing::get_addon_package_customer_price(
$addon_package,
$parent_item['data'],
$customer_id,
$commercial_lead_id
);
$selected_price_index = array_search($price_id, array_column($package_prices, 'id'));
// Initialize additional cart item data for wiaas addon packages
@@ -684,8 +697,12 @@ class Wiaas_Cart {
throw new Exception( __( 'Sorry, option package does not exist.', 'wiaas' ) );
}
// Retrieve option package price
$package_prices = Wiaas_Pricing::get_option_package_customer_price($option_package, $parent_item['data']);
// Retrieve option package price
$package_prices = Wiaas_Pricing::get_option_package_customer_price(
$option_package,
$parent_item['data'],
$customer_id,
$commercial_lead_id);
$selected_price_index = array_search($price_id, array_column($package_prices, 'id'));
// Retrieve option package group name

View File

@@ -12,12 +12,15 @@ class Wiaas_DB_Update {
'20180813134511' => 'wiaas_db_update_enable_order_numbers',
'20180826153509' => 'wiaas_create_broker_access_group',
'20180911101010' => 'wiaas_db_setup_exclusive_taxonomies',
'20180912101010' => 'wiaas_db_setup_default_cl',
'20181003164100' => 'wiaas_db_setup_customer_capabilities',
'201810031644700' => 'wiaas_db_update_create_default_roles',
'201810101644700' => 'wiaas_db_import_aam_role_settings',
'201810111644700' => 'wiaas_db_update_add_organization_info_ui_fields',
'201810121644700' => 'wiaas_db_update_add_user_organization_ui_fields'
'201810121644700' => 'wiaas_db_update_add_user_organization_ui_fields',
'201810171645700' => 'wiaas_db_update_create_default_roles',
'201810171745700' => 'wiaas_db_import_aam_role_settings',
'201810173045700' => 'wiaas_db_update_update_commercial_lead_capabilities',
'201810173145700' => 'wiaas_db_update_update_supplier_capabilities',
'201810173245700' => 'wiaas_db_update_update_admin_capabilities',
'201810173345700' => 'wiaas_create_role_access_groups'
);
public static function execute() {

View File

@@ -17,33 +17,6 @@ class Wiaas_Package {
require_once dirname( __FILE__ ) . '/package/wiaas-package-functions.php';
add_filter('woocommerce_rest_prepare_product_object', array(__CLASS__, 'transform_rest_package'), 999, 3);
add_filter('woocommerce_rest_product_object_query', array( __CLASS__, 'edit_product_query'), 10, 2);
}
/**
* Change product query to fetch only available packages (available status)
* @param $query
* @param $instance
*/
public static function edit_product_query($query, $request){
if (isset($request['id'])){
return;
}
if (!isset($query['tax_query'])){
$query['tax_query'] = array();
}
$query['tax_query'][] =
array(
'taxonomy' => 'package_status',
'field' => 'name',
'terms' => Wiaas_Package_Status::AVAILABLE
);
return $query;
}
/**
@@ -120,6 +93,9 @@ class Wiaas_Package {
* @return array
*/
private static function _append_additional_packages($data, $package, $request) {
// TODO: Change this so commercial lead is sent via request
$customer_id = wiaas_get_current_user_organization_id();
$commercial_lead_id = array_keys(wiaas_get_commercial_leads())[0];
$data['additional_packages'] = array();
$addons = Wiaas_Package_Addon::get_package_addons($package);
@@ -128,7 +104,7 @@ class Wiaas_Package {
'id' => $addon->get_id(),
'name' => $addon->get_name(),
'description' => $addon->get_description(),
'prices' => Wiaas_Pricing::get_addon_package_customer_price($addon, $package),
'prices' => Wiaas_Pricing::get_addon_package_customer_price($addon, $package, $customer_id, $commercial_lead_id),
);
}
@@ -149,7 +125,7 @@ class Wiaas_Package {
'name' => $option_package->get_name(),
'description' => $option_package->get_description(),
'default' => $default_option_id === $option_package->get_id(),
'prices' => Wiaas_Pricing::get_option_package_customer_price($option_package, $package),
'prices' => Wiaas_Pricing::get_option_package_customer_price($option_package, $package, $customer_id, $commercial_lead_id),
);
}
}
@@ -166,7 +142,11 @@ class Wiaas_Package {
* @return array
*/
private static function _append_package_prices($data, $package, $request) {
$data['prices'] = Wiaas_Pricing::get_standard_package_customer_prices($package);
// TODO: Change this so commercial lead is sent via request
$customer_id = wiaas_get_current_user_organization_id();
$commercial_lead_id = array_keys(wiaas_get_commercial_leads())[0];
$data['prices'] = Wiaas_Pricing::get_standard_package_customer_prices($package, $customer_id, $commercial_lead_id);
return $data;
}

View File

@@ -1,7 +1,7 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
exit; // Exit if accessed directly
}
/**
@@ -9,229 +9,240 @@ if ( ! defined( 'ABSPATH' ) ) {
*/
class Wiaas_Pricing {
/**
* Wiaas interest rate
*/
const INTEREST_RATE = 0.58;
/**
* Wiaas interest rate
*/
const INTEREST_RATE = 0.58;
const COMMERCIAL_LEAD_NAME = 'Coor Service Management';
public static function init() {
require_once dirname( __FILE__ ) . '/pricing/class-wiaas-product-pricing.php';
require_once dirname( __FILE__ ) . '/pricing/class-wiaas-package-pricing.php';
require_once dirname( __FILE__ ) . '/pricing/class-wiaas-package-cl-pricing.php';
require_once dirname( __FILE__ ) . '/pricing/wiaas-pricing-functions.php';
}
public static function init() {
require_once dirname( __FILE__ ) . '/pricing/class-wiaas-product-pricing.php';
require_once dirname( __FILE__ ) . '/pricing/class-wiaas-package-pricing.php';
require_once dirname( __FILE__ ) . '/pricing/wiaas-pricing-functions.php';
}
/**
* Calculates total cost for product
* @param WC_Product_Simple $product
*
* @return float
*/
public static function get_product_total_cost($product) {
$product_price = Wiaas_Product_Pricing::get_product_price($product);
/**
* Calculates total cost for product
* @param $product
*
* @return float
*/
public static function get_product_total_cost($product) {
$product_price = Wiaas_Product_Pricing::get_product_price($product);
return $product_price['is_recurring'] ?
$product_price['price'] * $product_price['pay_period'] :
$product_price['price'];
}
return $product_price['is_recurring'] ?
$product_price['price'] * $product_price['pay_period'] :
$product_price['price'];
}
/**
* Calculates total cost for package
* @param WC_Product_Bundle $package
*
* @return float
*/
public static function get_package_total_cost($package) {
/**
* Calculates total cost for package
* @param $package
*
* @return float
*/
public static function get_package_total_cost($package) {
$total_cost_per_category = array();
$total_cost_per_category = array();
$bundled_items = $package->get_bundled_items();
foreach ($bundled_items as $bundled_item) {
$bundled_items = $package->get_bundled_items();
foreach ($bundled_items as $bundled_item) {
$product = $bundled_item->product;
$product_cat = Wiaas_Product_Category::get_category($product);
$product = $bundled_item->product;
$product_cat = Wiaas_Product_Category::get_category($product);
if (!isset($total_cost_per_category[$product_cat])) {
$total_cost_per_category[$product_cat] = 0;
}
if (!isset($total_cost_per_category[$product_cat])) {
$total_cost_per_category[$product_cat] = 0;
}
$total_item_cost = self::get_product_total_cost($product) * $bundled_item->get_quantity();
$total_item_cost = self::get_product_total_cost($product) * $bundled_item->get_quantity();
if (Wiaas_Product_Category::is_installation($product)) {
$total_cost_per_category[$product_cat] = $total_cost_per_category[$product_cat] < $total_item_cost ?
$total_item_cost :
$total_cost_per_category[$product_cat];
} else {
$total_cost_per_category[$product_cat] += $total_item_cost;
}
}
if (Wiaas_Product_Category::is_installation($product)) {
$total_cost_per_category[$product_cat] = $total_cost_per_category[$product_cat] < $total_item_cost ?
$total_item_cost :
$total_cost_per_category[$product_cat];
} else {
$total_cost_per_category[$product_cat] += $total_item_cost;
}
}
return array_sum(array_values($total_cost_per_category));
}
return array_sum(array_values($total_cost_per_category));
}
/**
* Calculates customer prices for wiaas standard package
* @param WC_Product_Bundle $package
* @param int $customer_id
* @param int $commercial_lead_id
*
* @return array
*/
public static function get_standard_package_customer_prices($package, $customer_id, $commercial_lead_id) {
/**
* Calculates customer price for wiaas standard package
* @param $package
*
* @return array
*/
public static function get_standard_package_customer_prices($package) {
return self::_get_package_customer_prices(
$package,
(100 - Wiaas_Package_Pricing::get_package_pricing_commission($package)) / 100, // commercial lead commission
self::get_package_total_cost($package), // total cost of package items
$customer_id,
$commercial_lead_id);
$is_same_company_as_cl = self::_is_customer_same_company_as_cl();
$package_prices = Wiaas_Package_Pricing::get_package_prices($package);
$cl_commision = (100 - Wiaas_Package_Pricing::get_package_pricing_commission($package)) / 100;
$total_cost = self::get_package_total_cost($package);
}
$customer_package_prices = array();
/**
* Calculates customer prices for wiaas addon package
*
* @param WC_Product_Bundle $addon_package
* @param WC_Product_Bundle $parent_package
* @param int $customer_id
* @param int $commercial_lead_id
*
* @return array
*/
public static function get_addon_package_customer_price($addon_package, $parent_package, $customer_id, $commercial_lead_id) {
foreach ($package_prices as $type => $package_price) {
$customer_package_prices[] = self::_get_package_customer_price(
$package_price,
$cl_commision,
$total_cost,
$is_same_company_as_cl);
}
$parent_total_cost = self::get_package_total_cost($parent_package);
$parent_cl_commision = (100 - Wiaas_Package_Pricing::get_package_pricing_commission($parent_package)) / 100;
return $customer_package_prices;
return self::_get_package_customer_prices(
$addon_package,
$parent_cl_commision, // commercial lead commission of parent package
$parent_total_cost, // total cost of parent package items
$customer_id,
$commercial_lead_id);
}
}
/**
* Calculates customer prices for wiaas option package
*
* @param WC_Product_Bundle $option_package
* @param WC_Product_Bundle $parent_package
* @param int $customer_id
* @param int $commercial_lead_id
*
* @return array
*/
public static function get_option_package_customer_price($option_package, $parent_package, $customer_id, $commercial_lead_id) {
/**
* Calculates customer price for wiaas addon package
* @param $addon_package
* @param $parent_package
*
* @return array
*/
public static function get_addon_package_customer_price($addon_package, $parent_package) {
$parent_total_cost = self::get_package_total_cost($parent_package);
$parent_cl_commision = (100 - Wiaas_Package_Pricing::get_package_pricing_commission($parent_package)) / 100;
$is_same_company_as_cl = self::_is_customer_same_company_as_cl();
$parent_total_cost = self::get_package_total_cost($parent_package);
$parent_cl_commision = (100 - Wiaas_Package_Pricing::get_package_pricing_commission($parent_package)) / 100;
$addon_package_prices = Wiaas_Package_Pricing::get_package_prices($addon_package);
return self::_get_package_customer_prices(
$option_package,
$parent_cl_commision, // commercial lead commission of parent package
$parent_total_cost, // total cost of parent package items
$customer_id,
$commercial_lead_id);
}
$customer_package_prices = array();
foreach ($addon_package_prices as $type => $addon_package_price) {
$customer_package_prices[] = self::_get_package_customer_price(
$addon_package_price,
$parent_cl_commision,
$parent_total_cost,
$is_same_company_as_cl);
}
return $customer_package_prices;
}
/**
* Calculates customer price for wiaas option package
* @param $option_package
* @param $parent_package
*
* @return array
*/
public static function get_option_package_customer_price($option_package, $parent_package) {
// PRIVATE SECTION
$is_same_company_as_cl = self::_is_customer_same_company_as_cl();
$parent_total_cost = self::get_package_total_cost($parent_package);
$parent_cl_commision = (100 - Wiaas_Package_Pricing::get_package_pricing_commission($parent_package)) / 100;
$option_package_prices = Wiaas_Package_Pricing::get_package_prices($option_package);
$customer_package_prices = array();
/**
* Calculates customer prices for all package payment types
*
* @param WC_Product_Bundle $package
* @param int $cl_commision
* @param int $total_cost
* @param int $customer_id
* @param int $commercial_lead_id
*
* @return array
*/
private static function _get_package_customer_prices($package, $cl_commision, $total_cost, $customer_id, $commercial_lead_id) {
$is_same_company_as_cl = $customer_id === $commercial_lead_id;
foreach ($option_package_prices as $type => $option_package_price) {
$customer_package_prices[] = self::_get_package_customer_price(
$option_package_price,
$parent_cl_commision,
$parent_total_cost,
$is_same_company_as_cl);
}
$package_prices = Wiaas_Package_Pricing::get_package_prices($package);
return $customer_package_prices;
}
$customer_package_prices = array();
// PRIVATE
foreach ($package_prices as $type => $package_price) {
$cl_package_extras = Wiaas_Package_CL_Pricing::get_extras_for_customer(
$commercial_lead_id,
$package_price['id'],
$package->get_id(),
$customer_id
);
/**
* Determines if customer and commercial lead are in the same company
* For now this is hardcoded and we have only one CL
*
* TODO: This will be handled in next PR for setting commercial lead prices
* @return bool
*/
private static function _is_customer_same_company_as_cl() {
return false;
}
// commercial lead did not set prices for this payment type so it must not be visible for customer
if (empty($cl_package_extras)) {
continue;
}
/**
* Calculates customer price for wiaas package
* @param $package_price
* @param $cl_commision
* @param $total_cost
* @param $is_same_company_as_cl
*
* @return array
*/
private static function _get_package_customer_price($package_price, $cl_commision, $total_cost, $is_same_company_as_cl) {
$customer_package_prices[] = self::_get_package_customer_price(
$package_price,
$total_cost,
$cl_package_extras,
$cl_commision,
$is_same_company_as_cl);
}
$package_total_margin = wiaas_get_price_margin(
$package_price['minimal_fixed_price'],
$package_price['principal_amount'],
$total_cost);
$cl_margin = $package_total_margin > 0 ? $package_total_margin * $cl_commision : 0;
return $customer_package_prices;
}
$cl_package_prices = array(
'fixed_extra' => 0,
'recurrent_extra' => 0,
'services_extra' => 0
);
/**
* Calculates customer price for single package payment type
*
* @param array $package_price
* @param int $total_cost
* @param array $cl_package_extras
* @param int $cl_commision
* @param bool $is_same_company_as_cl
*
* @return array
*/
private static function _get_package_customer_price($package_price, $total_cost, $cl_package_extras, $cl_commision, $is_same_company_as_cl) {
$package_total_margin = wiaas_get_price_margin(
$package_price['minimal_fixed_price'],
$package_price['principal_amount'],
$total_cost);
$cl_margin = $package_total_margin > 0 ? $package_total_margin * $cl_commision : 0;
$interest_rate = self::INTEREST_RATE;
$customer_package_price = array(
'id' => $package_price['id'],
'payment_type' => $package_price['payment_type'],
'max_contract_period' => $package_price['max_contract_period'],
'package_pay_period' => $package_price['package_pay_period'],
'period_unit' => $package_price['period_unit'],
'services_contract_period' => $package_price['services_contract_period'],
$customer_price = array(
'id' => $package_price['id'],
'payment_type' => $package_price['payment_type'],
'max_contract_period' => $package_price['max_contract_period'],
'package_pay_period' => $package_price['package_pay_period'],
'period_unit' => $package_price['period_unit'],
'services_contract_period' => $package_price['services_contract_period'],
'fixed_extra' => 0,
'recurrent_extra' => 0,
'services_extra' => 0
);
'fixed_extra' => 0,
'recurrent_extra' => 0,
'services_extra' => 0
);
if ($is_same_company_as_cl) {
if ($is_same_company_as_cl) {
if ($package_price['package_pay_period'] > 0) {
$customer_package_price['fixed_extra'] = $package_price['minimal_fixed_price'];
$customer_package_price['recurrent_extra'] = wiaas_get_recurrent_price_mortage(
$package_price['principal_amount'],
$package_price['package_pay_period'],
$cl_margin,
self::INTEREST_RATE);
} else {
$customer_package_price['fixed_extra'] = $package_price['minimal_fixed_price'] - $cl_margin;
$customer_package_price['recurrent_extra'] = 0;
}
$customer_package_price['services_extra'] = $package_price['minimal_services_price'];
if ($package_price['package_pay_period'] > 0) {
$customer_price['fixed_extra'] = $package_price['minimal_fixed_price'];
$customer_price['recurrent_extra'] = wiaas_get_recurrent_price_mortage(
$package_price['principal_amount'],
$package_price['package_pay_period'],
$cl_margin,
$interest_rate);
} else {
$customer_price['fixed_extra'] = $package_price['minimal_fixed_price'] - $cl_margin;
$customer_price['recurrent_extra'] = 0;
}
$customer_price['services_extra'] = $package_price['minimal_services_price'];
} else {
} else {
$customer_package_price['fixed_extra'] = $cl_package_extras['fixed'] + $package_price['minimal_fixed_price'];
$customer_package_price['recurrent_extra'] = $package_price['package_pay_period'] > 0 ?
$cl_package_extras['recurrent'] + wiaas_get_recurrent_price_mortage(
$package_price['principal_amount'],
$package_price['package_pay_period'],
0,
self::INTEREST_RATE)
: 0;
$customer_package_price['services_extra'] = $cl_package_extras['services'] + $package_price['minimal_services_price'];
}
$customer_price['fixed_extra'] = $cl_package_prices['fixed_extra'] + $package_price['minimal_fixed_price'];
$customer_price['recurrent_extra'] = $package_price['package_pay_period'] > 0 ?
$cl_package_prices['recurrent_extra'] + wiaas_get_recurrent_price_mortage(
$package_price['principal_amount'],
$package_price['package_pay_period'],
0,
$interest_rate)
: 0;
$customer_price['services_extra'] = $cl_package_prices['services_extra'] + $package_price['minimal_services_price'];
}
return $customer_price;
}
return $customer_package_price;
}
}
Wiaas_Pricing::init();

View File

@@ -4,7 +4,31 @@ class Wiaas_Product {
public static function init() {
require_once dirname( __FILE__ ) . '/product/class-wiaas-product-category.php';
require_once dirname( __FILE__ ) . '/product/class-wiaas-product-supplier.php';
require_once dirname( __FILE__ ) . '/product/class-wiaas-product-supplier.php';
add_filter('woocommerce_register_post_type_product', array(__CLASS__, 'define_product_capabilities'));
}
/**
* Define capabilities for editing products so we can easily control read/edit/create access for them
*
* @param $args
*
* @return mixed
*/
public static function define_product_capabilities($args) {
$args['capabilities'] = array(
'edit_post' => 'edit_product',
'read_post' => 'read_product',
'delete_post' => 'delete_product',
'edit_posts' => 'edit_products',
'edit_others_posts' => 'edit_others_products',
'publish_posts' => 'publish_products',
'read_private_posts' => 'read_private_products',
'create_posts' => 'create_products', // use `create_products` instead of default `edit_products`
);
return $args;
}
}

View File

@@ -0,0 +1,75 @@
<?php
class Wiaas_Shop {
public static function init() {
// registers special prices taxonomy that will enable package search based on
// set and visible price types (default and customer specific ones)
add_action('woocommerce_after_register_taxonomy', array(__CLASS__, 'register_prices_taxonomy'));
// update prices search terms for package after prices extras have been updated
add_action('wiaas_package_prices_extras_set', array(__CLASS__, 'update_package_prices_search_terms'), 10, 4);
}
/**
* Register special prices taxonomy to enable search of packages by prices inside every shop
*/
public static function register_prices_taxonomy() {
$args = array(
'hierarchical' => false,
'query_var' => true,
'rewrite' => false,
'public' => true,
'capabilities' => array(
'manage_terms' => 'manage_wiaas_package_price_terms',
'edit_terms' => 'edit_wiaas_package_price_terms',
'delete_terms' => 'delete_wiaas_package_price_terms',
'assign_terms' => 'assign_wiaas_package_price_terms',
),
);
register_taxonomy( '_wiaas_shop_prices', array( 'product' ), $args );
}
/**
* Relate pricing search terms to package so customer can retrieve packages with default or
* their own specific prices.
* (ex: Package which will be hidden fro Customer 1 because default price
* is hidden can be visible for Customer 2 because specific prices are set for that customer)
*
* @param int $owner_id
* @param int $package_id
* @param array $cl_extras {
* $extra_price_payment_type => {
* @type bool visible Indicates if payment type is visible to customer
* }
* }
* @param array $old_cl_extras {
* $extra_price_payment_type => {
* @type bool visible Indicates if payment type is visible to customer
* }
* }
*/
public static function update_package_prices_search_terms($owner_id, $package_id, $cl_extras, $old_cl_extras) {
// remove pricing terms for previous prices
if (! empty($old_cl_extras)) {
$old_visible_price_types = array_keys(wp_list_filter($old_cl_extras, array( 'visible' => true )));
$old_terms_names = preg_filter('/^/', '_' . $owner_id . '_', $old_visible_price_types);
wp_remove_object_terms($package_id, $old_terms_names, '_wiaas_shop_prices');
}
// get visible price types set by shop owner (commercial lead)
$visible_price_types = array_keys(wp_list_filter($cl_extras, array('visible' => true)));
$new_terms_names = preg_filter('/^/', '_' . $owner_id . '_', $visible_price_types);
// create term for every visible pricing type and link them to package so package can be queried
wp_set_object_terms($package_id, $new_terms_names, '_wiaas_shop_prices');
}
}
Wiaas_Shop::init();

View File

@@ -113,10 +113,6 @@ function wiaas_db_setup_exclusive_taxonomies() {
));
}
function wiaas_db_setup_default_cl() {
wp_insert_term(Wiaas_Pricing::COMMERCIAL_LEAD_NAME, Wiaas_User_Organization::TAXONOMY_NAME);
}
function wiaas_db_setup_customer_capabilities() {
$customer_role = get_role('customer');

View File

@@ -36,8 +36,7 @@ function wiaas_db_update_create_default_roles() {
'commercial_lead',
'Commercial Lead',
array(
'read',
'view_admin_dashboard'
'read' => true,
)
);
@@ -45,8 +44,7 @@ function wiaas_db_update_create_default_roles() {
'supplier',
'Supplier',
array(
'read',
'view_admin_dashboard'
'read' => true,
)
);
@@ -54,7 +52,7 @@ function wiaas_db_update_create_default_roles() {
'user',
'User',
array(
'read'
'read' => true
)
);
@@ -94,12 +92,11 @@ function wiaas_db_update_create_default_roles() {
}
foreach ( $capabilities as $cap_group ) {
foreach ( $cap_group as $cap ) {
wp_roles()->add_cap( 'administrator', $cap );
wp_roles()->add_cap( 'commercial_lead', $cap );
}
foreach ( $cap_group as $cap ) {
wp_roles()->add_cap( 'administrator', $cap );
wp_roles()->add_cap( 'commercial_lead', $cap );
}
}
}
@@ -136,4 +133,44 @@ function wiaas_db_import_aam_role_settings() {
'gravityforms-new-form' => '1',
)
);
}
function wiaas_db_update_update_commercial_lead_capabilities() {
// add commercial lead specific roles
wp_roles()->add_cap( 'commercial_lead', 'manage_wiaas_cl_products' );
wp_roles()->add_cap( 'commercial_lead', 'view_admin_dashboard' );
wp_roles()->add_cap( 'commercial_lead', 'read' );
wp_roles()->add_cap( 'commercial_lead', 'upload_files' );
wp_roles()->add_cap( 'commercial_lead', 'edit_products' );
wp_roles()->add_cap( 'commercial_lead', 'edit_others_products' );
}
function wiaas_db_update_update_supplier_capabilities() {
// add supplier specific roles
wp_roles()->add_cap( 'supplier', 'view_admin_dashboard' );
wp_roles()->add_cap( 'supplier', 'read' );
}
function wiaas_db_update_update_admin_capabilities() {
wp_roles()->add_cap( 'administrator', 'create_products' );
}
function wiaas_create_role_access_groups() {
Groups_Group::create(array(
'name' => 'admin',
));
Groups_Group::create(array(
'name' => 'commercial_lead',
));
Groups_Group::create(array(
'name' => 'supplier',
));
Groups_Group::create(array(
'name' => 'customer',
));
}

View File

@@ -0,0 +1,114 @@
<?php
/**
* Handles logic for setting and retrieving commercial lead extra prices
*
* Class Wiaas_Package_CL_Pricing
*/
class Wiaas_Package_CL_Pricing {
/**
* Sets package extra prices
*
* @param int $owner_id ID of the shop owner organization who set extra prices
* @param int $package_id
* @param array $cl_extras
*/
public static function set_extras($owner_id, $package_id, $cl_extras) {
foreach ($cl_extras as $extra_type => $cl_extra) {
unset($cl_extra['type']);
unset($cl_extra['customer']);
$cl_extras[$extra_type] = $cl_extra;
}
$old_extras = self::get_extras($owner_id, $package_id);
// Persist package price extras
update_term_meta(
$owner_id,
'_wiaas_cm_extras_'.$package_id,
$cl_extras);
do_action('wiaas_package_prices_extras_set', $owner_id, $package_id, $cl_extras, $old_extras);
}
/**
* Retrieves package catalogue extra prices
*
* @param int $owner_id ID of the shop owner organization who set extra prices
* @param int $package_id
*
* @return array|null
*/
public static function get_extras($owner_id, $package_id) {
$cl_extras = get_term_meta(
$owner_id,
'_wiaas_cm_extras_'.$package_id,
true);
return is_array($cl_extras) ? $cl_extras : null;
}
/**
* Retrieve catalogue package extra prices for provided customer
*
* If customer specific prices are set for the package, only those will be retrieved
* otherwise deafult prices exras will be retrieved
*
* @param int $owner_id ID of the shop owner organization who set extra prices
* @param string $pay_type Payment type for the pacakge
* @param int $package_id ID of the package
* @param int $customer_id ID of the customer
*
* @return array|null
*/
public static function get_extras_for_customer($owner_id, $pay_type, $package_id, $customer_id) {
$cl_extras = self::get_extras($owner_id, $package_id);
if (! empty($cl_extras)) {
// if customer specific prices are set for this package use only them
// else use default prices set for this package
if (self::_customer_specific_prices_exist($cl_extras, $customer_id)) {
$cl_pay_type = $pay_type . '_customer_' . $customer_id;
} else {
$cl_pay_type = $pay_type . '_default';
}
$cl_pay_type_extras = $cl_extras[$cl_pay_type];
// if commercial lead has default price for this package then use it
if (! empty($cl_pay_type_extras) && $cl_pay_type_extras['visible']) {
return array(
'fixed' => floatval($cl_pay_type_extras['fixed']),
'services' => floatval($cl_pay_type_extras['services']),
'recurrent' => floatval($cl_pay_type_extras['recurrent'])
);
}
}
// there are no prices set for this payment type
return null;
}
private static function _customer_specific_prices_exist($cl_extras, $customer_id) {
$pay_types = array_keys(Wiaas_Package_Pricing::get_available_pay_types());
$cl_customer_pay_types = array();
foreach ($pay_types as $pay_type) {
$cl_customer_pay_types[] = $pay_type.'_customer_'.$customer_id;
}
foreach ($cl_extras as $cl_pay_type => $data) {
if (in_array($cl_pay_type, $cl_customer_pay_types)) {
return true;
}
}
return false;
}
}

View File

@@ -167,6 +167,16 @@ class Wiaas_Package_Pricing {
'principal_amount' => floatval($pricing_rule['principal_amount']),
'minimal_services_price' => floatval($pricing_rule['minimal_services_price']),
);
$minimal_recurrent_price = $prices[$type]['minimal_services_price'];
if ($prices[$type]['principal_amount'] > 0 && $prices[$type]['package_pay_period'] > 0) {
$minimal_recurrent_price += wiaas_PMT(
Wiaas_Pricing::INTEREST_RATE,
$prices[$type]['package_pay_period'],
$prices[$type]['principal_amount']);
}
$prices[$type]['minimal_recurrent_price'] = round($minimal_recurrent_price);
}
return $prices;

View File

@@ -50,6 +50,7 @@ function wiaas_get_organizations_with_role($role) {
'meta_value' => $role,
'fields' => 'id=>name',
'meta_compare' => 'LIKE',
'hide_empty' => false
));
return is_array($terms) ? $terms : array();
@@ -61,7 +62,7 @@ function wiaas_get_organizations_with_role($role) {
* @return array
*/
function wiaas_get_commercial_leads() {
return wiaas_get_organizations_with_role('shop_manager');
return wiaas_get_organizations_with_role('commercial_lead');
}
/**

View File

@@ -0,0 +1,162 @@
<?php
class Wiaas_Package_CL_Pricing_Test extends Wiaas_Unit_Test_Case {
var $shop_owner_id, $customer_id, $package_id, $cl_extras;
function setUp() {
parent::setUp();
$this->shop_owner_id = wp_insert_term(
'Shop owner organization',
Wiaas_User_Organization::TAXONOMY_NAME)['term_id'];
$this->customer_id = wp_insert_term(
'Customer Organization',
Wiaas_User_Organization::TAXONOMY_NAME)['term_id'];
$package = $this->create_new_package();
$this->add_products_to_package($package, array( $this->create_new_product()));
$this->package_id = $package->get_id();
$this->cl_extras = array(
'purchase_default' => array(
'fixed' => 10,
'recurrent' => 10,
'services' => 10,
'visible' => true
),
'purchase_24_default' => array(
'fixed' => 10,
'recurrent' => 10,
'services' => 10,
'visible' => true
),
'managed_36_default' => array(
'fixed' => 10,
'recurrent' => 10,
'services' => 10,
'visible' => true
)
);
}
/**
* @covers Wiaas_Package_CL_Pricing::set_extras()
*/
function test_packages_extras_set() {
Wiaas_Package_CL_Pricing::set_extras(
$this->shop_owner_id,
$this->package_id,
$this->cl_extras);
// validate data saved
$cl_extras = get_term_meta(
$this->shop_owner_id,
'_wiaas_cm_extras_' . $this->package_id,
true);
$this->_validate_default_cl_extras($cl_extras);
}
/**
* @covers Wiaas_Package_CL_Pricing::get_extras()
*/
function test_package_extras_retrieved() {
update_term_meta(
$this->shop_owner_id,
'_wiaas_cm_extras_' . $this->package_id,
$this->cl_extras);
$cl_extras = Wiaas_Package_CL_Pricing::get_extras($this->shop_owner_id, $this->package_id);
$this->_validate_default_cl_extras($cl_extras);
}
/**
* @covers Wiaas_Package_CL_Pricing::get_extras_for_customer()
*/
function test_default_extras_retrieved_for_customer() {
update_term_meta(
$this->shop_owner_id,
'_wiaas_cm_extras_' . $this->package_id,
$this->cl_extras);
$pay_types = array_keys(Wiaas_Package_Pricing::get_available_pay_types());
foreach ($pay_types as $pay_type) {
$customer_cl_extra = Wiaas_Package_CL_Pricing::get_extras_for_customer(
$this->shop_owner_id,
$pay_type,
$this->package_id,
$this->customer_id);
$this->assertNotNull($customer_cl_extra);
$this->assertEquals(10, $customer_cl_extra['fixed']);
$this->assertEquals(10, $customer_cl_extra['recurrent']);
$this->assertEquals(10, $customer_cl_extra['services']);
}
}
/**
* @covers Wiaas_Package_CL_Pricing::get_extras_for_customer()
*/
function test_customer_specific_extras_retrieved_for_customer() {
// append customer specific extras for purchase payment type
$this->cl_extras['purchase_customer_' . $this->customer_id] = array(
'fixed' => 100,
'recurrent' => 100,
'services' => 100,
'visible' => true
);
// set prices extras
update_term_meta(
$this->shop_owner_id,
'_wiaas_cm_extras_' . $this->package_id,
$this->cl_extras);
// test that only extras are retrieved for purchase payment type
// and the rest are not retrieved
$pay_types = array_keys(Wiaas_Package_Pricing::get_available_pay_types());
foreach ($pay_types as $pay_type) {
$customer_cl_extra = Wiaas_Package_CL_Pricing::get_extras_for_customer(
$this->shop_owner_id,
$pay_type,
$this->package_id,
$this->customer_id);
if ($pay_type === 'purchase') {
$this->assertNotNull($customer_cl_extra);
$this->assertEquals(100, $customer_cl_extra['fixed']);
$this->assertEquals(100, $customer_cl_extra['recurrent']);
$this->assertEquals(100, $customer_cl_extra['services']);
} else {
$this->assertNull($customer_cl_extra);
}
}
}
// HELPERS
private function _validate_default_cl_extras($cl_extras) {
$this->assertNotEmpty($cl_extras);
$this->assertArrayHasKey('purchase_default', $cl_extras);
$this->assertArrayHasKey('purchase_24_default', $cl_extras);
$this->assertArrayHasKey('managed_36_default', $cl_extras);
foreach ($cl_extras as $cl_extra) {
$this->assertEquals(10, $cl_extra['fixed']);
$this->assertEquals(10, $cl_extra['recurrent']);
$this->assertEquals(10, $cl_extra['services']);
$this->assertTrue($cl_extra['visible']);
}
}
}

View File

@@ -34,6 +34,19 @@ class Wiaas_Pricing_Test extends Wiaas_Unit_Test_Case {
$cost_margin = 0;
Wiaas_Package_Pricing::set_package_prices($package, $pricing_rules, $commision, $cost_margin);
$customer_id = wp_create_term(
'Customer',
Wiaas_User_Organization::TAXONOMY_NAME
)['term_id'];
$commercial_lead_id = wp_create_term(
'Commercial Lead',
Wiaas_User_Organization::TAXONOMY_NAME
)['term_id'];
self::_set_package_default_extras($commercial_lead_id, $package->get_id());
$expected_prices = array(
'purchase' => array(
'fixed_extra' => 100,
@@ -52,7 +65,32 @@ class Wiaas_Pricing_Test extends Wiaas_Unit_Test_Case {
)
);
return array( $package, $expected_prices );
return array( $package, $expected_prices, $customer_id, $commercial_lead_id );
}
private function _set_package_default_extras($commercial_lead_id, $package_id) {
$cl_extras = array(
'purchase_default' => array(
'fixed' => 0,
'recurrent' => 0,
'services' => 0,
'visible' => true
),
'purchase_24_default' => array(
'fixed' => 0,
'recurrent' => 0,
'services' => 0,
'visible' => true
),
'managed_36_default' => array(
'fixed' => 0,
'recurrent' => 0,
'services' => 0,
'visible' => true
)
);
Wiaas_Package_CL_Pricing::set_extras($commercial_lead_id, $package_id, $cl_extras);
}
/**
@@ -172,9 +210,12 @@ class Wiaas_Pricing_Test extends Wiaas_Unit_Test_Case {
*/
function test_get_standard_package_customer_price() {
list( $package, $expected_prices ) = $this->_create_package_to_sell();
list( $package, $expected_prices, $customer_id, $commercial_lead_id ) = $this->_create_package_to_sell();
$customer_prices = Wiaas_Pricing::get_standard_package_customer_prices($package);
$customer_prices = Wiaas_Pricing::get_standard_package_customer_prices(
$package,
$customer_id,
$commercial_lead_id);
$this->assertCount(3, $customer_prices);
@@ -203,7 +244,7 @@ class Wiaas_Pricing_Test extends Wiaas_Unit_Test_Case {
* @covers Wiaas_Pricing::get_addon_package_customer_price()
*/
function test_get_addon_package_customer_price() {
list( $package ) = $this->_create_package_to_sell();
list( $package, $expected_prices, $customer_id, $commercial_lead_id ) = $this->_create_package_to_sell();
$addon_product = $this->create_new_product(20);
$this->add_product_category($addon_product, 'hardware');
@@ -233,6 +274,8 @@ class Wiaas_Pricing_Test extends Wiaas_Unit_Test_Case {
$cost_margin = 0;
Wiaas_Package_Pricing::set_package_prices($addon_package, $pricing_rules, $commision, $cost_margin);
self::_set_package_default_extras($commercial_lead_id, $addon_package->get_id());
Wiaas_Package_Addon::set_package_addons($package, array($addon_package->get_id()));
$expected_prices = array(
@@ -253,7 +296,11 @@ class Wiaas_Pricing_Test extends Wiaas_Unit_Test_Case {
)
);
$customer_prices = Wiaas_Pricing::get_addon_package_customer_price($addon_package, $package);
$customer_prices = Wiaas_Pricing::get_addon_package_customer_price(
$addon_package,
$package,
$customer_id,
$commercial_lead_id);
$this->assertCount(3, $customer_prices);
@@ -281,7 +328,7 @@ class Wiaas_Pricing_Test extends Wiaas_Unit_Test_Case {
* @covers Wiaas_Pricing::get_option_package_customer_price()
*/
function test_get_option_package_customer_price() {
list( $package ) = $this->_create_package_to_sell();
list( $package, $customer_id, $commercial_lead_id ) = $this->_create_package_to_sell();
$option_product = $this->create_new_product(20);
$this->add_product_category($option_product, 'hardware');
@@ -311,6 +358,8 @@ class Wiaas_Pricing_Test extends Wiaas_Unit_Test_Case {
$cost_margin = 0;
Wiaas_Package_Pricing::set_package_prices($option_package, $pricing_rules, $commision, $cost_margin);
self::_set_package_default_extras($commercial_lead_id, $option_package->get_id());
Wiaas_Package_Option_Groups::set_package_option_groups($package, array(
'id' => 'option',
'name' => 'Option',
@@ -336,7 +385,11 @@ class Wiaas_Pricing_Test extends Wiaas_Unit_Test_Case {
)
);
$customer_prices = Wiaas_Pricing::get_option_package_customer_price($option_package, $package);
$customer_prices = Wiaas_Pricing::get_option_package_customer_price(
$option_package,
$package,
$customer_id,
$commercial_lead_id);
$this->assertCount(3, $customer_prices);

View File

@@ -0,0 +1,17 @@
<?php
class Wiaas_Unit_Test_Factory {
//
public $product;
//
public $package;
//
public $organization;
function __construct() {
}
}

View File

@@ -28,6 +28,7 @@ if (is_admin()) {
include_once WIAAS_DIR . '/includes/class-wiaas-admin.php';
}
include_once WIAAS_DIR . '/includes/class-wiaas-access-management.php';
include_once WIAAS_DIR . '/includes/class-wiaas-delivery-process.php';
@@ -37,6 +38,8 @@ include_once WIAAS_DIR . '/includes/class-wiaas-product.php';
include_once WIAAS_DIR . '/includes/class-wiaas-package.php';
include_once WIAAS_DIR . '/includes/class-wiaas-shop.php';
include_once WIAAS_DIR . '/includes/class-wiaas-user.php';
include_once WIAAS_DIR . '/includes/class-wiaas-pricing.php';

View File

@@ -27,15 +27,9 @@ export const fetchShopPackages = (cl, search) => {
return dispatch => {
dispatch(requestShopPackages());
let searchParam = search ? '?search=' +search : ''
return client.fetch({
url: `${API_SERVER}/wp-json/wc/v2/products` + searchParam,
// TODO: Add comercialLead parameter after the support for it is added on backend
// method: 'post',
// data: {
// idCommercialLead: (cl && cl.value) || 0,
// search
// }
return client.fetch({
url: `${API_SERVER}/wp-json/wc/v2/products?cl_id=${cl.idCommercialLead}` + searchParam,
})
.then(response => {
if (response.data) {
@@ -68,23 +62,29 @@ const generateClOptions = (commercialLeads) => {
export const fetchShopCommercialLeads = () => {
return dispatch => {
dispatch(requestShopCommercialLeads());
// TODO: FetchcomercialLead after the support for it is added on backend
// return client.fetch({url: `${API_SERVER}/coMarket/api/getAllCommercialLeads`})
// .then(response => {
// if(response.data && response.data.commercialLeads){
const clOptions = generateClOptions([{ "idCommercialLead": 14, "commercialLeadName": "Coor Service Management", "mail": "rikard@co-ideation.com" }]);
dispatch(recieveShopCommercialLeads(clOptions));
if (clOptions.length) {
dispatch(selectCommercialLead(clOptions[0]));
dispatch(fetchShopPackages(clOptions[0]));
}
return client.fetch({url: `${API_SERVER}/wp-json/wiaas/commercial-leads` })
.then(response => {
if(response.data){
const clOptions = generateClOptions(response.data.map(cl => ({
idCommercialLead: cl.id,
commercialLeadName: cl.name
})));
dispatch(recieveShopCommercialLeads(clOptions));
if (clOptions.length) {
dispatch(selectCommercialLead(clOptions[0]));
dispatch(fetchShopPackages(clOptions[0]));
}
}
})
.catch(error => {
client.onError(error, dispatch);
});
}
// })
// .catch(error => {
// client.onError(error, dispatch);
// });
}
// }
export const selectCommercialLead = (cl) => ({
type: SELECT_SHOP_COMMERCIAL_LEAD,

View File

@@ -16,7 +16,9 @@ class CoMarketNavContainer extends Component {
handleSearchChange(event) {
this.setState({searchValue: event.target.value});
this.props.dispatch(fetchShopPackages(this.props.selectedCommercialLead, event.target.value));
if (this.props.selectedCommercialLead) {
this.props.dispatch(fetchShopPackages(this.props.selectedCommercialLead, event.target.value));
}
}
render() {

View File

@@ -8,7 +8,9 @@ import {fetchShopPackages} from '../../actions/coMarket/coMarketPackagesActions'
class CoMarketPackagesContainer extends Component {
componentDidMount() {
this.props.dispatch(fetchShopPackages(this.props.selectedCommercialLead));
if (this.props.selectedCommercialLead) {
this.props.dispatch(fetchShopPackages(this.props.selectedCommercialLead));
}
}
render() {

View File

@@ -43,7 +43,9 @@ class CoMarketCatalogSelect extends Component {
handleSearchChange(event) {
this.setState({searchValue: event.target.value});
this.props.dispatch(fetchShopPackages(this.props.selectedCommercialLead, event.target.value));
if (this.props.selectedCommercialLead) {
this.props.dispatch(fetchShopPackages(this.props.selectedCommercialLead, event.target.value));
}
}
render() {