Handle commercial lead prices
This commit is contained in:
@@ -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';
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'WP_List_Table' ) ) {
|
||||
include_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
|
||||
}
|
||||
|
||||
class Wiaas_Admin_CL_Packages_List extends WP_List_Table {
|
||||
|
||||
public function __construct() {
|
||||
|
||||
parent::__construct( array(
|
||||
'singular' => 'Package',
|
||||
'plural' => 'Packages',
|
||||
'ajax' => false,
|
||||
) );
|
||||
|
||||
return true;
|
||||
}
|
||||
public function column_default( $item, $column_name ) {
|
||||
switch ( $column_name ) {
|
||||
case 'thumb':
|
||||
return '<a href="' . admin_url( 'admin.php?page=wiaas-cl-product&id=' . absint( $item->get_id() ) ) . '">' .
|
||||
$item->get_image( 'thumbnail' ) .
|
||||
'</a>';
|
||||
case 'name':
|
||||
return '<strong><a href="' . admin_url( 'admin.php?page=wiaas-cl-product&id=' . absint( $item->get_id() ) ) . '">' .
|
||||
esc_attr( $item->get_name() ) .
|
||||
'</a></strong>' .
|
||||
'<div><span style="color: #999;">ID: '. $item->get_id() . '</span></div>';
|
||||
case 'country':
|
||||
$country = Wiaas_Countries::get_package_country($item);
|
||||
if (! empty($country)) {
|
||||
return '<span>'. $country['name'] . '</span>';
|
||||
}
|
||||
return '';
|
||||
case 'type':
|
||||
$type = Wiaas_Package_Type::get_package_type($item->get_id());
|
||||
return '<span>'. $type . '</span>';
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function prepare_items() {
|
||||
$columns = $this->get_columns();
|
||||
$sortable = $this->get_sortable_columns();
|
||||
$this->_column_headers = array( $columns, $sortable );
|
||||
|
||||
$items_per_page = 10;
|
||||
|
||||
$current_page = $this->get_pagenum();
|
||||
|
||||
$query_args = array(
|
||||
'status' => 'publish',
|
||||
'type' => 'bundle',
|
||||
'tag' => array(),
|
||||
'limit' => $items_per_page,
|
||||
'page' => $current_page,
|
||||
'paginate' => true
|
||||
);
|
||||
|
||||
if (!empty($_REQUEST['s'])) {
|
||||
$query_args['s'] = sanitize_text_field($_REQUEST['s']);
|
||||
}
|
||||
|
||||
$results = wc_get_products($query_args);
|
||||
|
||||
$this->items = $results->products;
|
||||
|
||||
$this->set_pagination_args( array(
|
||||
'total_items' => $results->total,
|
||||
'per_page' => $items_per_page,
|
||||
) );
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
public function get_views() {
|
||||
|
||||
return array();
|
||||
}
|
||||
|
||||
public function get_columns() {
|
||||
$columns = array(
|
||||
'thumb' => __( '', 'wiaas' ),
|
||||
'name' => __( 'Name', 'wiaas' ),
|
||||
'country' => __( 'Sell in', 'wiaas' ),
|
||||
'type' => __( 'Type', 'wiaas' ),
|
||||
);
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
public function get_primary_column_name() {
|
||||
return 'product_name';
|
||||
}
|
||||
|
||||
public function get_sortable_columns() {
|
||||
$sort = array(
|
||||
'product_name' => array( 'product_name', true ),
|
||||
);
|
||||
|
||||
return $sort;
|
||||
}
|
||||
|
||||
public function no_items() {
|
||||
_e( 'No packages found.', 'wiaas' );
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function wiaas_handle_custom_query_var($query, $query_vars) {
|
||||
if ( ! empty( $query_vars['catalogue'] ) ) {
|
||||
|
||||
$query['meta_query'][] = array(
|
||||
'key' => '_wiaas_catalogue_'.$query_vars['catalogue'],
|
||||
'value' => 'yes',
|
||||
);
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
add_filter('woocommerce_product_data_store_cpt_get_products_query', 'wiaas_handle_custom_query_var', 10, 2);
|
||||
@@ -0,0 +1,170 @@
|
||||
<?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') );
|
||||
}
|
||||
|
||||
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_menu_page(
|
||||
__( 'Products', 'wiaas' ),
|
||||
__( 'Products', 'wiaas' ),
|
||||
'wiaas_manage_cl_products',
|
||||
'wiaas-cl-packages',
|
||||
array(__CLASS__, 'output_list'),
|
||||
null,
|
||||
'55.5' );
|
||||
|
||||
add_submenu_page(
|
||||
null,
|
||||
__( 'Products', 'wiaas' ),
|
||||
null,
|
||||
'wiaas_manage_cl_products',
|
||||
'wiaas-cl-product',
|
||||
array(__CLASS__, 'output_package')
|
||||
);
|
||||
}
|
||||
|
||||
public static function output_list() {
|
||||
|
||||
$packages_list = new Wiaas_Admin_CL_Packages_List();
|
||||
|
||||
$packages_list->prepare_items();
|
||||
|
||||
require 'views/html-admin-cl-packages-page.php';
|
||||
}
|
||||
|
||||
public static function output_package() {
|
||||
wp_enqueue_script( 'jquery-ui-tabs' );
|
||||
|
||||
$package_id = absint( $_GET['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';
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// 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();
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
<style>
|
||||
table.wp-list-table .column-thumb {
|
||||
width: 52px;
|
||||
text-align: center;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
table.wp-list-table td.column-thumb img {
|
||||
margin: 0;
|
||||
width: auto;
|
||||
height: auto;
|
||||
max-width: 40px;
|
||||
max-height: 40px;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
</style>
|
||||
|
||||
<div class="wrap">
|
||||
<h2><?php esc_html_e( 'Products', 'wiaas' ); ?>
|
||||
<?php
|
||||
if ( ! empty( $_REQUEST['s'] ) ) {
|
||||
echo '<span class="subtitle">' . esc_html__( 'Search results for', 'wiaas' ) . ' "' . sanitize_text_field( $_REQUEST['s'] ) . '"</span>';
|
||||
}
|
||||
?>
|
||||
</h2>
|
||||
|
||||
<ul class="subsubsub"><?php $packages_list->views(); ?></ul>
|
||||
|
||||
<form id="wiaas_products" action="" method="get">
|
||||
<input type="hidden" name="page" value="wiaas-cl-packages" />
|
||||
<?php $packages_list->search_box( __( 'Search Products', 'wiaas' ), 'wiaas_packages_search_id' ); ?>
|
||||
<?php $packages_list->display(); ?>
|
||||
</form>
|
||||
|
||||
</div>
|
||||
@@ -0,0 +1,296 @@
|
||||
<?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>
|
||||
jQuery(document).ready(function ($) {
|
||||
$( "#tabs" ).tabs({
|
||||
disabled: <?php echo $has_default_cl_extras ? '[ ]' : '[ 1 ]' ; ?>
|
||||
});
|
||||
|
||||
$('#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: '<?php echo wp_create_nonce('wiaas_create_cl_customer_extras') ?>',
|
||||
customer_id: customer_id,
|
||||
package_id: <?php echo esc_attr($package->get_id()) ?>
|
||||
}).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'));
|
||||
}
|
||||
});
|
||||
});
|
||||
</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') ?></strong>
|
||||
<span><?php esc_html_e('= Extra package recurrent commission') ?></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') ?></span>
|
||||
</div>
|
||||
<div>
|
||||
<i style="vertical-align: middle;" class="dashicons dashicons-info"></i>
|
||||
<strong><?php esc_html_e('Extra commission') ?></strong>
|
||||
<span><?php esc_html_e('= EPR + ESR') ?></span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="tabs">
|
||||
<ul id="tabs-navigation">
|
||||
<li><a href="#tabs-1">Default prices</a> | </li>
|
||||
<li><a href="#tabs-2">Customer specific prices</a></li>
|
||||
</ul>
|
||||
|
||||
<form id="wiaas_package_extras" action="" method="post">
|
||||
<input type="hidden" name="page" value="wiaas-cl-product"/>
|
||||
<input type="hidden" name="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) {
|
||||
?>
|
||||
<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>
|
||||
<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>
|
||||
@@ -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>Minimal sell price</td>
|
||||
<td>Extra commision</td>
|
||||
<td>Default price</td>
|
||||
<td>Visible?
|
||||
<?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>(EPR)</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>(ESR)</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>
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
class Wiaas_Admin_CL {
|
||||
|
||||
public static function init() {
|
||||
require_once dirname( __FILE__ ) . '/admin-cl/class-wiaas-admin-cl-packages.php';
|
||||
|
||||
require_once dirname( __FILE__ ) . '/admin-cl/class-wiaas-admin-cl-packages-list.php';
|
||||
|
||||
require_once dirname( __FILE__ ) . '/admin-cl/wiaas-admin-cl-packages-ajax.php';
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Admin_CL::init();
|
||||
@@ -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();
|
||||
@@ -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>
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
class Wiaas_Package_API {
|
||||
|
||||
private static $namespace = 'wiaas';
|
||||
|
||||
public static function init() {
|
||||
|
||||
add_filter('woocommerce_rest_product_object_query', array(__CLASS__, 'filter_by_commercial_lead'), 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'
|
||||
) );
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public static function filter_by_commercial_lead($args, $request) {
|
||||
|
||||
$catalogue_id = absint($request['cl_id']);
|
||||
|
||||
$args['meta_query'] ?: array();
|
||||
|
||||
$args['meta_query'][] = array(
|
||||
'key' => '_wiaas_catalogue_'.$catalogue_id,
|
||||
'value' => 'yes',
|
||||
);
|
||||
|
||||
return $args;
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Package_API::init();
|
||||
@@ -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' );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 ) {
|
||||
|
||||
@@ -138,8 +138,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
|
||||
@@ -638,6 +642,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
|
||||
@@ -646,8 +654,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
|
||||
@@ -678,8 +691,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
|
||||
|
||||
@@ -92,6 +92,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);
|
||||
@@ -100,7 +103,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),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -121,7 +124,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),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -138,7 +141,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;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
@@ -37,7 +37,8 @@ function wiaas_db_update_create_default_roles() {
|
||||
'Commercial Lead',
|
||||
array(
|
||||
'read',
|
||||
'view_admin_dashboard'
|
||||
'view_admin_dashboard',
|
||||
'wiaas_manage_cl_products'
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
<?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 $organization_id
|
||||
* @param int $package_id
|
||||
* @param array $cl_extras
|
||||
*/
|
||||
public static function set_extras($organization_id, $package_id, $cl_extras) {
|
||||
//self::_assign_catalogue_attribute_options($organization_id, $package_id, $cl_extras);
|
||||
|
||||
$has_extras = false;
|
||||
|
||||
foreach ($cl_extras as $extra_type => $cl_extra) {
|
||||
unset($cl_extra['type']);
|
||||
unset($cl_extra['customer']);
|
||||
|
||||
$has_extras = $has_extras || $cl_extra['visible'];
|
||||
|
||||
$cl_extras[$extra_type] = $cl_extra;
|
||||
}
|
||||
|
||||
$package = wc_get_product($package_id);
|
||||
|
||||
$package->add_meta_data('_wiaas_catalogue_'.$organization_id, $has_extras ? 'yes' : 'no', true);
|
||||
$package->save_meta_data();
|
||||
|
||||
// Persist package catalogue extras
|
||||
update_term_meta(
|
||||
$organization_id,
|
||||
'_wiaas_cm_extras_'.$package_id,
|
||||
$cl_extras);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves package catalogue extra prices
|
||||
*
|
||||
* @param int $organization_id
|
||||
* @param int $package_id
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public static function get_extras($organization_id, $package_id) {
|
||||
$cl_extras = get_term_meta(
|
||||
$organization_id,
|
||||
'_wiaas_cm_extras_'.$package_id,
|
||||
true);
|
||||
|
||||
return is_array($cl_extras) ? $cl_extras : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve catalogue package extra prices for provided customer
|
||||
*
|
||||
* @param int $organization_id
|
||||
* @param string $pay_type
|
||||
* @param int $package_id
|
||||
* @param int $customer_id
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public static function get_extras_for_customer($organization_id, $pay_type, $package_id, $customer_id) {
|
||||
$cl_extras = self::get_extras($organization_id, $package_id);
|
||||
|
||||
if (! empty($cl_extras)) {
|
||||
// retrieve default and customer specific extras
|
||||
$cl_pay_type_customer_extras = $cl_extras[$pay_type.'_customer_'.$customer_id];
|
||||
$cl_pay_type_extras = $cl_extras[$pay_type.'_default'];
|
||||
|
||||
// if commercial lead has special prices for this customer then use them
|
||||
if (! empty($cl_pay_type_customer_extras) && $cl_pay_type_customer_extras['visible']) {
|
||||
|
||||
return array(
|
||||
'fixed' => floatval($cl_pay_type_customer_extras['fixed']),
|
||||
'services' => floatval($cl_pay_type_customer_extras['services']),
|
||||
'recurrent' => floatval($cl_pay_type_customer_extras['recurrent'])
|
||||
);
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
@@ -140,6 +140,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;
|
||||
|
||||
@@ -61,7 +61,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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user