Files
old-new-wiaas/backend/app/plugins/wiaas/includes/pricing/class-wiaas-package-cl-pricing.php
2018-10-15 14:59:57 +02:00

115 lines
3.6 KiB
PHP

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