use only customer specific prices if set

This commit is contained in:
Almira Krdzic
2018-10-15 14:59:57 +02:00
parent f14d35b1aa
commit b7ac53d195
3 changed files with 212 additions and 31 deletions

View File

@@ -46,28 +46,29 @@ class Wiaas_Package_API {
$query['tax_query'] = array();
}
// Moved package status handling here
// 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_serch_terms = array();
$price_search_terms = array();
foreach ($pay_types as $pay_type) {
$price_serch_terms[] = '_' . $commercial_lead_id . '_' . $pay_type . '_default';
$price_serch_terms[] = '_' . $commercial_lead_id . '_' . $pay_type . '_customer_' . $customer_id;
$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_serch_terms,
'terms' => $price_search_terms,
'field' => 'slug'
);

View File

@@ -10,11 +10,11 @@ class Wiaas_Package_CL_Pricing {
/**
* Sets package extra prices
*
* @param int $organization_id
* @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($organization_id, $package_id, $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']);
@@ -23,28 +23,28 @@ class Wiaas_Package_CL_Pricing {
$cl_extras[$extra_type] = $cl_extra;
}
$old_extras = self::get_extras($organization_id, $package_id);
$old_extras = self::get_extras($owner_id, $package_id);
// Persist package price extras
update_term_meta(
$organization_id,
$owner_id,
'_wiaas_cm_extras_'.$package_id,
$cl_extras);
do_action('wiaas_package_prices_extras_set', $organization_id, $package_id, $cl_extras, $old_extras);
do_action('wiaas_package_prices_extras_set', $owner_id, $package_id, $cl_extras, $old_extras);
}
/**
* Retrieves package catalogue extra prices
*
* @param int $organization_id
* @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($organization_id, $package_id) {
public static function get_extras($owner_id, $package_id) {
$cl_extras = get_term_meta(
$organization_id,
$owner_id,
'_wiaas_cm_extras_'.$package_id,
true);
@@ -54,31 +54,30 @@ class Wiaas_Package_CL_Pricing {
/**
* Retrieve catalogue package extra prices for provided customer
*
* @param int $organization_id
* @param string $pay_type
* @param int $package_id
* @param int $customer_id
* 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($organization_id, $pay_type, $package_id, $customer_id) {
$cl_extras = self::get_extras($organization_id, $package_id);
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)) {
// 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 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']) {
@@ -93,4 +92,23 @@ class Wiaas_Package_CL_Pricing {
// 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

@@ -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']);
}
}
}