/*
* Be aware:
* if id > 0 ... it can test if the payment method exists (order)
* if id = 0 ... it lists all possible payment methods meeting the criteria (checkout)
* Always test for the id before using this function
*/
public function getPossiblePaymentMethods($amountNetto, $amountBrutto, $country, $region, $shipping, $id = 0, $selected = 0)
{
$app = Factory::getApplication();
$paramsC = PhocacartUtils::getComponentParameters();
$payment_amount_rule = $paramsC->get('payment_amount_rule', 0);
$user = PhocacartUser::getUser();
$userLevels = implode(',', $user->getAuthorisedViewLevels());
$userGroups = implode(',', PhocacartGroup::getGroupsById($user->id, 1, 1));
$db = Factory::getDBO();
$wheres = array();
// ACCESS
$wheres[] = " p.published = 1";
$wheres[] = " p.access IN (" . $userLevels . ")";
$wheres[] = " (ga.group_id IN (" . $userGroups . ") OR ga.group_id IS NULL)";
if (!empty($this->type) && is_array($this->type)) {
$wheres[] = " p.type IN (" . implode(',', $this->type) . ')';
}
if ((int) $id > 0) {
$wheres[] = 'p.id = ' . (int) $id;
$limit = ' LIMIT 1';
//$group = '';
} else {
$limit = '';
}
$columns = 'p.id, p.tax_id, p.cost, p.cost_additional, p.calculation_type, p.title, p.image, p.access, p.description, p.method,' . ' p.active_amount, p.active_zone, p.active_country, p.active_region, p.active_shipping,' . ' p.lowest_amount, p.highest_amount, p.default, p.params,' . ' t.id as taxid, t.title as taxtitle, t.tax_rate as taxrate, t.calculation_type as taxcalculationtype,' . ' GROUP_CONCAT(DISTINCT r.region_id) AS region,' . ' GROUP_CONCAT(DISTINCT c.country_id) AS country,' . ' GROUP_CONCAT(DISTINCT z.zone_id) AS zone,' . ' GROUP_CONCAT(DISTINCT s.shipping_id) AS shipping';
$groupsFull = 'p.id, p.tax_id, p.cost, p.cost_additional, p.calculation_type, p.title, p.image, p.access, p.description, p.method,' . ' p.active_amount, p.active_zone, p.active_country, p.active_region, p.active_shipping,' . ' p.lowest_amount, p.highest_amount, p.default, p.params,' . ' t.id, t.title, t.tax_rate, t.calculation_type';
$groupsFast = 'p.id';
$groups = PhocacartUtilsSettings::isFullGroupBy() ? $groupsFull : $groupsFast;
$where = count($wheres) ? ' WHERE ' . implode(' AND ', $wheres) : '';
/*$query = ' SELECT p.id, p.title, p.image'
.' FROM #__phocacart_payment_methods AS p'
.' WHERE p.published = 1'
.' ORDER BY p.ordering';
$db->setQuery($query);*/
$query = ' SELECT ' . $columns . ' FROM #__phocacart_payment_methods AS p' . ' LEFT JOIN #__phocacart_payment_method_regions AS r ON r.payment_id = p.id' . ' LEFT JOIN #__phocacart_payment_method_countries AS c ON c.payment_id = p.id' . ' LEFT JOIN #__phocacart_payment_method_zones AS z ON z.payment_id = p.id' . ' LEFT JOIN #__phocacart_payment_method_shipping AS s ON s.payment_id = p.id' . ' LEFT JOIN #__phocacart_taxes AS t ON t.id = p.tax_id' . ' LEFT JOIN #__phocacart_item_groups AS ga ON p.id = ga.item_id AND ga.type = 8' . $where . ' GROUP BY ' . $groups . ' ORDER BY p.ordering' . $limit;
PhocacartUtils::setConcatCharCount();
$db->setQuery($query);
$payments = $db->loadObjectList();
if (!empty($payments) && !isset($payments[0]->id) || isset($payments[0]->id) && (int) $payments[0]->id < 1) {
return false;
}
$i = 0;
if (!empty($payments)) {
foreach ($payments as $k => $v) {
$v->active = 0;
$v->selected = 0;
$a = 0;
$z = 0;
$c = 0;
$r = 0;
$s = 0;
// Amount Rule
if ($v->active_amount == 1) {
if ($payment_amount_rule == 0 || $payment_amount_rule == 2) {
// No tax, brutto
if ($amountBrutto >= $v->lowest_amount && $amountBrutto <= $v->highest_amount) {
$a = 1;
}
} else {
if ($payment_amount_rule == 2) {
// Netto
if ($amountNetto >= $v->lowest_amount && $amountNetto <= $v->highest_amount) {
$a = 1;
}
}
}
} else {
$a = 1;
}
// Zone Rule
if ($v->active_zone == 1) {
if (isset($v->zone) && $v->zone != '') {
$zones = explode(',', $v->zone);
if (PhocacartZone::isCountryOrRegionIncluded($zones, (int) $country, (int) $region)) {
$z = 1;
}
}
} else {
$z = 1;
}
// Country Rule
if ($v->active_country == 1) {
if (isset($v->country) && $v->country != '') {
$countries = explode(',', $v->country);
if (in_array((int) $country, $countries)) {
$c = 1;
}
}
} else {
$c = 1;
}
// Region Rule
if ($v->active_region == 1) {
if (isset($v->region) && $v->region != '') {
$regions = explode(',', $v->region);
if (in_array((int) $region, $regions)) {
$r = 1;
}
}
} else {
$r = 1;
}
// Shipping Rule
if ($v->active_shipping == 1) {
if (isset($v->shipping) && $v->shipping != '') {
$shippings = explode(',', $v->shipping);
if (in_array((int) $shipping, $shippings)) {
$s = 1;
}
}
} else {
$s = 1;
}
// No rule was set for shipping, it will be displayed at all events
if ($v->active_amount == 0 && $v->active_country == 0 && $v->active_region == 0 && $v->active_shipping == 0) {
$v->active = 1;
}
// if some of the rules is not valid, all the payment is NOT valid
if ($a == 0 || $z == 0 || $c == 0 || $r == 0 || $s == 0) {
$v->active = 0;
} else {
$v->active = 1;
}
if ($v->active == 0) {
if (isset($payments[$i])) {
unset($payments[$i]);
}
} else {
// Payment is active but payment method plugin can deactivate it
$pluginPayment = PluginHelper::importPlugin('pcp');
if ($pluginPayment) {
PluginHelper::importPlugin('pcp', htmlspecialchars(strip_tags($v->method)));
$eventData = array();
$active = true;
$eventData['pluginname'] = htmlspecialchars(strip_tags($v->method));
Factory::getApplication()->triggerEvent('onPCPbeforeShowPossiblePaymentMethod', array(&$active, $v, $eventData));
if ($active == false) {
if (isset($payments[$i])) {
unset($payments[$i]);
}
}
}
}
// Try to set default for frontend form
// If user selected some payment, such will be set as default
// If not then the default will be set
if ((int) $selected > 0) {
if ((int) $v->id == (int) $selected) {
$v->selected = 1;
}
} else {
$v->selected = $v->default;
}
$i++;
}
}
return $payments;
}