akaunting/app/Utilities/Modules.php

93 lines
2.3 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Utilities;
2020-08-28 10:34:51 +03:00
use App\Events\Module\PaymentMethodShowing;
2017-09-14 22:21:00 +03:00
use Cache;
use Date;
class Modules
{
public static function getPaymentMethods($type = null)
2017-09-14 22:21:00 +03:00
{
2020-05-23 15:34:54 +03:00
$cache_admin = static::getPaymentMethodsCacheKey('admin');
$cache_customer = static::getPaymentMethodsCacheKey('customer');
$payment_methods = Cache::get($cache_admin);
2017-11-18 15:23:20 +03:00
2019-11-16 10:21:14 +03:00
$contact = true;
2018-10-15 16:10:49 +03:00
2020-08-28 10:34:51 +03:00
if ($user = user()) {
$contact = $user->contact;
2018-10-15 16:10:49 +03:00
}
2017-11-18 15:23:20 +03:00
2019-11-16 10:21:14 +03:00
if ($contact && ($type != 'all')) {
$payment_methods = Cache::get($cache_customer);
2017-11-18 15:23:20 +03:00
}
2017-09-14 22:21:00 +03:00
if (!empty($payment_methods)) {
return $payment_methods;
}
2019-11-16 10:21:14 +03:00
$list = [];
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
$modules = new \stdClass();
$modules->payment_methods = [];
2019-11-16 10:21:14 +03:00
// Fire the event to get the list of payment methods
2020-08-28 10:34:51 +03:00
event(new PaymentMethodShowing($modules));
2017-11-18 15:23:20 +03:00
2020-08-28 10:34:51 +03:00
foreach ((array) $modules->payment_methods as $method) {
2019-11-16 10:21:14 +03:00
if (!isset($method['name']) || !isset($method['code'])) {
continue;
2017-09-14 22:21:00 +03:00
}
2019-11-16 10:21:14 +03:00
if (($contact && empty($method['customer'])) && ($type != 'all')) {
continue;
2017-11-18 15:23:20 +03:00
}
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
$list[] = $method;
}
static::sortPaymentMethods($list);
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
foreach ($list as $method) {
$payment_methods[$method['code']] = $method['name'];
2017-09-14 22:21:00 +03:00
}
2019-11-16 10:21:14 +03:00
if ($contact) {
Cache::put($cache_customer, $payment_methods, Date::now()->addHour(6));
2017-11-18 15:23:20 +03:00
} else {
Cache::put($cache_admin, $payment_methods, Date::now()->addHour(6));
2017-11-18 15:23:20 +03:00
}
2017-09-14 22:21:00 +03:00
2017-11-20 19:01:34 +03:00
return ($payment_methods) ? $payment_methods : [];
2017-09-14 22:21:00 +03:00
}
2019-11-16 10:21:14 +03:00
2020-05-23 15:34:54 +03:00
public static function clearPaymentMethodsCache()
{
Cache::forget(static::getPaymentMethodsCacheKey('admin'));
Cache::forget(static::getPaymentMethodsCacheKey('customer'));
}
public static function getPaymentMethodsCacheKey($type)
{
return 'payment_methods.' . session('company_id') . '.' . $type;
}
2019-11-16 10:21:14 +03:00
protected static function sortPaymentMethods(&$list)
{
$sort_order = [];
foreach ($list as $key => $value) {
$sort_order[$key] = !empty($value['order']) ? $value['order'] : 0;
}
if (empty($sort_order)) {
return;
}
array_multisort($sort_order, SORT_ASC, $list);
}
2017-09-14 22:21:00 +03:00
}