Files
old-new-wiaas/backend/app/plugins/wiaas/includes/pricing/class-wiaas-package-cl-pricing.php
Almira Krdzic f14d35b1aa Fix tests
2018-10-15 05:06:46 +02:00

97 lines
3.1 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 $organization_id
* @param int $package_id
* @param array $cl_extras
*/
public static function set_extras($organization_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($organization_id, $package_id);
// Persist package price extras
update_term_meta(
$organization_id,
'_wiaas_cm_extras_'.$package_id,
$cl_extras);
do_action('wiaas_package_prices_extras_set', $organization_id, $package_id, $cl_extras, $old_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;
}
}