2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
namespace App\Libraries;
|
|
|
|
|
|
|
|
use App;
|
2015-03-24 08:25:31 +01:00
|
|
|
use Auth;
|
2015-03-26 06:13:31 +01:00
|
|
|
use Cache;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Carbon;
|
|
|
|
use DateTime;
|
2015-03-27 07:06:14 +01:00
|
|
|
use DateTimeZone;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Exception;
|
2015-03-30 21:45:10 +02:00
|
|
|
use Input;
|
|
|
|
use Log;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Request;
|
|
|
|
use Schema;
|
|
|
|
use Session;
|
2015-03-31 11:38:24 +02:00
|
|
|
use stdClass;
|
2017-01-30 20:40:43 +01:00
|
|
|
use View;
|
2016-05-12 04:55:37 +02:00
|
|
|
use WePay;
|
2015-03-31 11:38:24 +02:00
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
class Utils
|
|
|
|
{
|
2016-07-04 16:20:22 +02:00
|
|
|
private static $weekdayNames = [
|
2017-01-30 20:40:43 +01:00
|
|
|
'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',
|
2016-07-04 16:20:22 +02:00
|
|
|
];
|
|
|
|
|
2016-10-31 10:11:30 +01:00
|
|
|
public static $months = [
|
|
|
|
'january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december',
|
|
|
|
];
|
2016-07-21 14:35:23 +02:00
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
public static function isRegistered()
|
|
|
|
{
|
|
|
|
return Auth::check() && Auth::user()->registered;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function isConfirmed()
|
|
|
|
{
|
|
|
|
return Auth::check() && Auth::user()->confirmed;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function isDatabaseSetup()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (Schema::hasTable('accounts')) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-12-08 11:10:20 +01:00
|
|
|
} catch (Exception $e) {
|
2015-03-16 22:45:25 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-29 20:19:18 +02:00
|
|
|
public static function isDownForMaintenance()
|
|
|
|
{
|
|
|
|
return file_exists(storage_path() . '/framework/down');
|
|
|
|
}
|
|
|
|
|
2016-01-18 09:30:42 +01:00
|
|
|
public static function isCron()
|
|
|
|
{
|
|
|
|
return php_sapi_name() == 'cli';
|
|
|
|
}
|
|
|
|
|
2016-05-04 08:53:43 +02:00
|
|
|
public static function isTravis()
|
|
|
|
{
|
|
|
|
return env('TRAVIS') == 'true';
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
public static function isNinja()
|
|
|
|
{
|
|
|
|
return self::isNinjaProd() || self::isNinjaDev();
|
|
|
|
}
|
|
|
|
|
2016-12-28 15:39:29 +01:00
|
|
|
public static function isSelfHost()
|
|
|
|
{
|
|
|
|
return ! static::isNinjaProd();
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
public static function isNinjaProd()
|
|
|
|
{
|
2017-01-30 20:40:43 +01:00
|
|
|
if (self::isReseller()) {
|
2016-01-28 13:04:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-18 22:05:30 +01:00
|
|
|
return env('NINJA_PROD') == 'true';
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function isNinjaDev()
|
|
|
|
{
|
2016-02-18 22:05:30 +01:00
|
|
|
return env('NINJA_DEV') == 'true';
|
2015-06-16 21:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-11-24 20:45:38 +01:00
|
|
|
public static function requireHTTPS()
|
|
|
|
{
|
2016-07-21 14:35:23 +02:00
|
|
|
if (Request::root() === 'http://ninja.dev' || Request::root() === 'http://ninja.dev:8000') {
|
2016-02-11 16:12:27 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
return self::isNinjaProd() || (isset($_ENV['REQUIRE_HTTPS']) && $_ENV['REQUIRE_HTTPS'] == 'true');
|
2015-11-24 20:45:38 +01:00
|
|
|
}
|
|
|
|
|
2016-01-28 13:04:55 +01:00
|
|
|
public static function isReseller()
|
|
|
|
{
|
2017-01-30 20:40:43 +01:00
|
|
|
return self::getResllerType() ? true : false;
|
2016-01-28 13:04:55 +01:00
|
|
|
}
|
|
|
|
|
2017-02-16 04:26:55 +01:00
|
|
|
public static function clientViewCSS()
|
|
|
|
{
|
|
|
|
$account = false;
|
|
|
|
|
|
|
|
if (Auth::check()) {
|
|
|
|
$account = Auth::user()->account;
|
|
|
|
} elseif ($contactKey = session('contact_key')) {
|
|
|
|
if ($contact = \App\Models\Contact::whereContactKey($contactKey)->first()) {
|
|
|
|
$account = $contact->account;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !$account && ! self::isNinja()) {
|
|
|
|
// For self-hosted accounts, pick the first account
|
|
|
|
$account = \App\Models\Account::first();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $account ? $account->clientViewCSS() : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getAccountFontsUrl($protocol = '')
|
|
|
|
{
|
|
|
|
$account = false;
|
|
|
|
|
|
|
|
if (Auth::check()) {
|
|
|
|
$account = Auth::user()->account;
|
|
|
|
} elseif ($contactKey = session('contact_key')) {
|
|
|
|
if ($contact = \App\Models\Contact::whereContactKey($contactKey)->first()) {
|
|
|
|
$account = $contact->account;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !$account && ! self::isNinja()) {
|
|
|
|
// For self-hosted accounts, pick the first account
|
|
|
|
$account = \App\Models\Account::first();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $account ? $account->getFontsUrl($protocol) : false;
|
|
|
|
}
|
|
|
|
|
2016-10-30 17:09:22 +01:00
|
|
|
public static function isWhiteLabel()
|
|
|
|
{
|
2017-01-09 15:25:23 +01:00
|
|
|
$account = false;
|
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
if (self::isNinja()) {
|
2017-01-09 15:25:23 +01:00
|
|
|
if (Auth::check()) {
|
|
|
|
$account = Auth::user()->account;
|
|
|
|
} elseif ($contactKey = session('contact_key')) {
|
|
|
|
if ($contact = \App\Models\Contact::whereContactKey($contactKey)->first()) {
|
|
|
|
$account = $contact->account;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$account = \App\Models\Account::first();
|
2016-10-30 17:09:22 +01:00
|
|
|
}
|
2016-10-31 10:11:30 +01:00
|
|
|
|
2017-01-09 15:25:23 +01:00
|
|
|
return $account ? $account->hasFeature(FEATURE_WHITE_LABEL) : false;
|
2016-10-30 17:09:22 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
public static function getResllerType()
|
2016-01-28 13:04:55 +01:00
|
|
|
{
|
|
|
|
return isset($_ENV['RESELLER_TYPE']) ? $_ENV['RESELLER_TYPE'] : false;
|
|
|
|
}
|
|
|
|
|
2017-01-22 21:36:43 +01:00
|
|
|
public static function getTermsLink()
|
|
|
|
{
|
|
|
|
return static::isNinja() ? NINJA_WEB_URL.'/terms' : NINJA_WEB_URL.'/self-hosting-the-invoice-ninja-platform';
|
|
|
|
}
|
|
|
|
|
2015-11-01 19:21:11 +01:00
|
|
|
public static function isOAuthEnabled()
|
|
|
|
{
|
|
|
|
$providers = [
|
|
|
|
SOCIAL_GOOGLE,
|
|
|
|
SOCIAL_FACEBOOK,
|
|
|
|
SOCIAL_GITHUB,
|
2017-01-30 20:40:43 +01:00
|
|
|
SOCIAL_LINKEDIN,
|
2015-11-01 19:21:11 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($providers as $provider) {
|
|
|
|
$key = strtoupper($provider) . '_CLIENT_ID';
|
|
|
|
if (isset($_ENV[$key]) && $_ENV[$key]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-16 21:35:35 +02:00
|
|
|
public static function allowNewAccounts()
|
|
|
|
{
|
2017-01-30 20:40:43 +01:00
|
|
|
return self::isNinja() || Auth::check();
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function isPro()
|
|
|
|
{
|
|
|
|
return Auth::check() && Auth::user()->isPro();
|
|
|
|
}
|
|
|
|
|
2016-04-19 04:35:18 +02:00
|
|
|
public static function hasFeature($feature)
|
|
|
|
{
|
|
|
|
return Auth::check() && Auth::user()->hasFeature($feature);
|
|
|
|
}
|
|
|
|
|
2016-03-16 00:08:00 +01:00
|
|
|
public static function isAdmin()
|
|
|
|
{
|
|
|
|
return Auth::check() && Auth::user()->is_admin;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function hasPermission($permission, $requireAll = false)
|
|
|
|
{
|
|
|
|
return Auth::check() && Auth::user()->hasPermission($permission, $requireAll);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function hasAllPermissions($permission)
|
|
|
|
{
|
2016-05-08 20:29:49 +02:00
|
|
|
return Auth::check() && Auth::user()->hasPermission($permission);
|
2016-03-16 00:08:00 +01:00
|
|
|
}
|
|
|
|
|
2016-02-11 16:12:27 +01:00
|
|
|
public static function isTrial()
|
|
|
|
{
|
|
|
|
return Auth::check() && Auth::user()->isTrial();
|
|
|
|
}
|
|
|
|
|
2016-10-31 11:59:48 +01:00
|
|
|
public static function isPaidPro()
|
|
|
|
{
|
|
|
|
return static::isPro() && ! static::isTrial();
|
|
|
|
}
|
|
|
|
|
2015-08-03 21:08:07 +02:00
|
|
|
public static function isEnglish()
|
|
|
|
{
|
|
|
|
return App::getLocale() == 'en';
|
|
|
|
}
|
2016-05-15 22:16:08 +02:00
|
|
|
|
2016-03-03 21:24:27 +01:00
|
|
|
public static function getLocaleRegion()
|
|
|
|
{
|
2016-05-15 22:16:08 +02:00
|
|
|
$parts = explode('_', App::getLocale());
|
|
|
|
|
2016-03-03 21:24:27 +01:00
|
|
|
return count($parts) ? $parts[0] : 'en';
|
|
|
|
}
|
2015-08-03 21:08:07 +02:00
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
public static function getUserType()
|
|
|
|
{
|
2017-01-30 20:40:43 +01:00
|
|
|
if (self::isNinja()) {
|
2015-03-16 22:45:25 +01:00
|
|
|
return USER_TYPE_CLOUD_HOST;
|
2016-07-21 14:35:23 +02:00
|
|
|
} else {
|
|
|
|
return USER_TYPE_SELF_HOST;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getNewsFeedResponse($userType = false)
|
|
|
|
{
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! $userType) {
|
|
|
|
$userType = self::getUserType();
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$response = new stdClass();
|
|
|
|
$response->message = isset($_ENV["{$userType}_MESSAGE"]) ? $_ENV["{$userType}_MESSAGE"] : '';
|
|
|
|
$response->id = isset($_ENV["{$userType}_ID"]) ? $_ENV["{$userType}_ID"] : '';
|
2016-09-13 10:48:27 +02:00
|
|
|
$response->version = NINJA_VERSION;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getProLabel($feature)
|
|
|
|
{
|
|
|
|
if (Auth::check()
|
2017-01-30 20:40:43 +01:00
|
|
|
&& ! Auth::user()->isPro()
|
2015-03-16 22:45:25 +01:00
|
|
|
&& $feature == ACCOUNT_ADVANCED_SETTINGS) {
|
|
|
|
return ' <sup class="pro-label">PRO</sup>';
|
2016-07-21 14:35:23 +02:00
|
|
|
} else {
|
|
|
|
return '';
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
public static function getPlanPrice($plan)
|
2016-07-11 19:08:43 +02:00
|
|
|
{
|
|
|
|
$term = $plan['term'];
|
|
|
|
$numUsers = $plan['num_users'];
|
|
|
|
$plan = $plan['plan'];
|
|
|
|
|
|
|
|
if ($plan == PLAN_FREE) {
|
|
|
|
$price = 0;
|
|
|
|
} elseif ($plan == PLAN_PRO) {
|
|
|
|
$price = PLAN_PRICE_PRO_MONTHLY;
|
|
|
|
} elseif ($plan == PLAN_ENTERPRISE) {
|
|
|
|
if ($numUsers <= 2) {
|
|
|
|
$price = PLAN_PRICE_ENTERPRISE_MONTHLY_2;
|
|
|
|
} elseif ($numUsers <= 5) {
|
|
|
|
$price = PLAN_PRICE_ENTERPRISE_MONTHLY_5;
|
|
|
|
} elseif ($numUsers <= 10) {
|
|
|
|
$price = PLAN_PRICE_ENTERPRISE_MONTHLY_10;
|
2017-01-01 19:43:46 +01:00
|
|
|
} elseif ($numUsers <= 20) {
|
|
|
|
$price = PLAN_PRICE_ENTERPRISE_MONTHLY_20;
|
2016-07-11 19:08:43 +02:00
|
|
|
} else {
|
|
|
|
static::fatalError('Invalid number of users: ' . $numUsers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($term == PLAN_TERM_YEARLY) {
|
|
|
|
$price = $price * 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $price;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getMinNumUsers($max)
|
|
|
|
{
|
|
|
|
if ($max <= 2) {
|
|
|
|
return 1;
|
|
|
|
} elseif ($max <= 5) {
|
|
|
|
return 3;
|
2017-01-01 19:43:46 +01:00
|
|
|
} elseif ($max <= 10) {
|
2016-07-11 19:08:43 +02:00
|
|
|
return 6;
|
2017-01-01 19:43:46 +01:00
|
|
|
} else {
|
|
|
|
return 11;
|
2016-07-11 19:08:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
public static function basePath()
|
|
|
|
{
|
|
|
|
return substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], '/') + 1);
|
|
|
|
}
|
|
|
|
|
2016-12-19 12:22:03 +01:00
|
|
|
public static function trans($input, $module = false)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
|
|
|
$data = [];
|
|
|
|
|
|
|
|
foreach ($input as $field) {
|
2016-07-03 18:11:58 +02:00
|
|
|
if ($field == 'checkbox') {
|
2015-03-16 22:45:25 +01:00
|
|
|
$data[] = $field;
|
2015-11-06 00:14:00 +01:00
|
|
|
} elseif ($field) {
|
2016-12-19 12:22:03 +01:00
|
|
|
if ($module) {
|
|
|
|
$data[] = mtrans($module, $field);
|
|
|
|
} else {
|
|
|
|
$data[] = trans("texts.$field");
|
|
|
|
}
|
2015-11-06 00:14:00 +01:00
|
|
|
} else {
|
|
|
|
$data[] = '';
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function fatalError($message = false, $exception = false)
|
|
|
|
{
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! $message) {
|
2016-07-03 18:11:58 +02:00
|
|
|
$message = 'An error occurred, please try again later.';
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static::logError($message.' '.$exception);
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'showBreadcrumbs' => false,
|
|
|
|
'hideHeader' => true,
|
|
|
|
];
|
|
|
|
|
|
|
|
return View::make('error', $data)->with('error', $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getErrorString($exception)
|
|
|
|
{
|
2015-04-27 14:28:40 +02:00
|
|
|
$class = get_class($exception);
|
|
|
|
$code = method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : $exception->getCode();
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2015-04-27 14:28:40 +02:00
|
|
|
return "***{$class}*** [{$code}] : {$exception->getFile()} [Line {$exception->getLine()}] => {$exception->getMessage()}";
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-03-22 16:14:40 +01:00
|
|
|
public static function logError($error, $context = 'PHP', $info = false)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-12-08 11:10:20 +01:00
|
|
|
if ($error instanceof Exception) {
|
|
|
|
$error = self::getErrorString($error);
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$count = Session::get('error_count', 0);
|
|
|
|
Session::put('error_count', ++$count);
|
2016-01-31 20:36:50 +01:00
|
|
|
if ($count > 200) {
|
2015-03-16 22:45:25 +01:00
|
|
|
return 'logged';
|
|
|
|
}
|
|
|
|
|
2017-06-14 20:18:55 +02:00
|
|
|
$data = static::prepareErrorData($context);
|
|
|
|
|
|
|
|
if ($info) {
|
|
|
|
Log::info($error."\n", $data);
|
|
|
|
} else {
|
|
|
|
Log::error($error."\n", $data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function prepareErrorData($context)
|
|
|
|
{
|
|
|
|
return [
|
2015-03-16 22:45:25 +01:00
|
|
|
'context' => $context,
|
|
|
|
'user_id' => Auth::check() ? Auth::user()->id : 0,
|
2015-11-16 20:07:23 +01:00
|
|
|
'account_id' => Auth::check() ? Auth::user()->account_id : 0,
|
2015-03-16 22:45:25 +01:00
|
|
|
'user_name' => Auth::check() ? Auth::user()->getDisplayName() : '',
|
2016-07-21 14:35:23 +02:00
|
|
|
'method' => Request::method(),
|
|
|
|
'url' => Input::get('url', Request::url()),
|
2017-03-24 15:38:42 +01:00
|
|
|
'previous' => url()->previous(),
|
2015-03-16 22:45:25 +01:00
|
|
|
'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '',
|
2016-07-21 14:35:23 +02:00
|
|
|
'ip' => Request::getClientIp(),
|
2015-03-16 22:45:25 +01:00
|
|
|
'count' => Session::get('error_count', 0),
|
2017-05-04 19:08:27 +02:00
|
|
|
'is_console' => App::runningInConsole() ? 'yes' : 'no',
|
2017-06-06 09:56:29 +02:00
|
|
|
'is_api' => session('token_id') ? 'yes' : 'no',
|
2017-06-13 11:04:46 +02:00
|
|
|
'db_server' => config('database.default'),
|
2015-03-16 22:45:25 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function parseFloat($value)
|
|
|
|
{
|
|
|
|
$value = preg_replace('/[^0-9\.\-]/', '', $value);
|
|
|
|
|
|
|
|
return floatval($value);
|
|
|
|
}
|
|
|
|
|
2015-11-29 11:41:32 +01:00
|
|
|
public static function parseInt($value)
|
|
|
|
{
|
|
|
|
$value = preg_replace('/[^0-9]/', '', $value);
|
|
|
|
|
|
|
|
return intval($value);
|
|
|
|
}
|
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
public static function getFromCache($id, $type)
|
|
|
|
{
|
2016-03-07 16:22:20 +01:00
|
|
|
$cache = Cache::get($type);
|
2016-05-15 22:16:08 +02:00
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
if (! $cache) {
|
2016-03-07 16:22:20 +01:00
|
|
|
static::logError("Cache for {$type} is not set");
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2016-03-07 16:22:20 +01:00
|
|
|
return null;
|
|
|
|
}
|
2016-05-15 22:16:08 +02:00
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
$data = $cache->filter(function ($item) use ($id) {
|
2015-12-07 14:34:55 +01:00
|
|
|
return $item->id == $id;
|
2016-07-21 14:35:23 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return $data->first();
|
2015-12-07 14:34:55 +01:00
|
|
|
}
|
|
|
|
|
2016-10-20 17:14:54 +02:00
|
|
|
public static function formatMoney($value, $currencyId = false, $countryId = false, $decorator = false)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2016-08-31 11:21:54 +02:00
|
|
|
$value = floatval($value);
|
2015-12-07 14:34:55 +01:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! $currencyId) {
|
2015-06-10 10:34:20 +02:00
|
|
|
$currencyId = Session::get(SESSION_CURRENCY, DEFAULT_CURRENCY);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! $decorator) {
|
2016-10-20 17:14:54 +02:00
|
|
|
$decorator = Session::get(SESSION_CURRENCY_DECORATOR, CURRENCY_DECORATOR_SYMBOL);
|
|
|
|
}
|
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! $countryId && Auth::check()) {
|
2015-12-07 14:34:55 +01:00
|
|
|
$countryId = Auth::user()->account->country_id;
|
2015-05-05 11:48:23 +02:00
|
|
|
}
|
|
|
|
|
2015-12-07 14:34:55 +01:00
|
|
|
$currency = self::getFromCache($currencyId, 'currencies');
|
|
|
|
$thousand = $currency->thousand_separator;
|
|
|
|
$decimal = $currency->decimal_separator;
|
2016-05-05 21:49:09 +02:00
|
|
|
$precision = $currency->precision;
|
2016-01-18 15:35:05 +01:00
|
|
|
$code = $currency->code;
|
2016-05-16 22:37:16 +02:00
|
|
|
$swapSymbol = $currency->swap_currency_symbol;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-12-07 14:34:55 +01:00
|
|
|
if ($countryId && $currencyId == CURRENCY_EURO) {
|
|
|
|
$country = self::getFromCache($countryId, 'countries');
|
|
|
|
$swapSymbol = $country->swap_currency_symbol;
|
|
|
|
if ($country->thousand_separator) {
|
|
|
|
$thousand = $country->thousand_separator;
|
|
|
|
}
|
|
|
|
if ($country->decimal_separator) {
|
|
|
|
$decimal = $country->decimal_separator;
|
|
|
|
}
|
2015-06-19 01:30:41 +02:00
|
|
|
}
|
|
|
|
|
2016-05-05 21:49:09 +02:00
|
|
|
$value = number_format($value, $precision, $decimal, $thousand);
|
2015-12-07 14:34:55 +01:00
|
|
|
$symbol = $currency->symbol;
|
|
|
|
|
2016-10-20 16:17:47 +02:00
|
|
|
if ($decorator == CURRENCY_DECORATOR_NONE) {
|
|
|
|
return $value;
|
2016-10-20 16:44:31 +02:00
|
|
|
} elseif ($decorator == CURRENCY_DECORATOR_CODE || ! $symbol) {
|
2016-01-18 15:35:05 +01:00
|
|
|
return "{$value} {$code}";
|
2015-12-07 14:34:55 +01:00
|
|
|
} elseif ($swapSymbol) {
|
|
|
|
return "{$value} " . trim($symbol);
|
|
|
|
} else {
|
|
|
|
return "{$symbol}{$value}";
|
2015-11-18 22:37:11 +01:00
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function pluralize($string, $count)
|
|
|
|
{
|
|
|
|
$field = $count == 1 ? $string : $string.'s';
|
|
|
|
$string = trans("texts.$field", ['count' => $count]);
|
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
2016-07-06 20:35:16 +02:00
|
|
|
public static function pluralizeEntityType($type)
|
|
|
|
{
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! self::isNinjaProd()) {
|
2016-12-08 16:09:45 +01:00
|
|
|
if ($module = \Module::find($type)) {
|
|
|
|
return $module->get('plural', $type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-06 20:35:16 +02:00
|
|
|
if ($type === ENTITY_EXPENSE_CATEGORY) {
|
|
|
|
return 'expense_categories';
|
|
|
|
} else {
|
|
|
|
return $type . 's';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 00:07:31 +01:00
|
|
|
public static function maskAccountNumber($value)
|
|
|
|
{
|
|
|
|
$length = strlen($value);
|
|
|
|
if ($length < 4) {
|
|
|
|
str_repeat('*', 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
$lastDigits = substr($value, -4);
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2016-01-20 00:07:31 +01:00
|
|
|
return str_repeat('*', $length - 4) . $lastDigits;
|
|
|
|
}
|
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
// http://wephp.co/detect-credit-card-type-php/
|
2016-01-20 00:07:31 +01:00
|
|
|
public static function getCardType($number)
|
|
|
|
{
|
|
|
|
$number = preg_replace('/[^\d]/', '', $number);
|
|
|
|
|
|
|
|
if (preg_match('/^3[47][0-9]{13}$/', $number)) {
|
|
|
|
return 'American Express';
|
|
|
|
} elseif (preg_match('/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/', $number)) {
|
|
|
|
return 'Diners Club';
|
|
|
|
} elseif (preg_match('/^6(?:011|5[0-9][0-9])[0-9]{12}$/', $number)) {
|
|
|
|
return 'Discover';
|
|
|
|
} elseif (preg_match('/^(?:2131|1800|35\d{3})\d{11}$/', $number)) {
|
|
|
|
return 'JCB';
|
|
|
|
} elseif (preg_match('/^5[1-5][0-9]{14}$/', $number)) {
|
|
|
|
return 'MasterCard';
|
|
|
|
} elseif (preg_match('/^4[0-9]{12}(?:[0-9]{3})?$/', $number)) {
|
|
|
|
return 'Visa';
|
2016-07-21 14:35:23 +02:00
|
|
|
} else {
|
|
|
|
return 'Unknown';
|
2016-01-20 00:07:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
public static function toArray($data)
|
|
|
|
{
|
|
|
|
return json_decode(json_encode((array) $data), true);
|
|
|
|
}
|
|
|
|
|
2015-12-02 14:26:06 +01:00
|
|
|
public static function toSpaceCase($string)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-12-02 14:26:06 +01:00
|
|
|
return preg_replace('/([a-z])([A-Z])/s', '$1 $2', $string);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function toSnakeCase($string)
|
|
|
|
{
|
|
|
|
return preg_replace('/([a-z])([A-Z])/s', '$1_$2', $string);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function toCamelCase($string)
|
|
|
|
{
|
2016-05-15 22:16:08 +02:00
|
|
|
return lcfirst(static::toClassCase($string));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function toClassCase($string)
|
|
|
|
{
|
|
|
|
return str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function timestampToDateTimeString($timestamp)
|
|
|
|
{
|
|
|
|
$timezone = Session::get(SESSION_TIMEZONE, DEFAULT_TIMEZONE);
|
|
|
|
$format = Session::get(SESSION_DATETIME_FORMAT, DEFAULT_DATETIME_FORMAT);
|
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
return self::timestampToString($timestamp, $timezone, $format);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function timestampToDateString($timestamp)
|
|
|
|
{
|
|
|
|
$timezone = Session::get(SESSION_TIMEZONE, DEFAULT_TIMEZONE);
|
|
|
|
$format = Session::get(SESSION_DATE_FORMAT, DEFAULT_DATE_FORMAT);
|
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
return self::timestampToString($timestamp, $timezone, $format);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function dateToString($date)
|
|
|
|
{
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! $date) {
|
2015-10-15 08:19:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-19 14:01:19 +01:00
|
|
|
|
2016-04-17 00:34:39 +02:00
|
|
|
if ($date instanceof DateTime) {
|
|
|
|
$dateTime = $date;
|
|
|
|
} else {
|
|
|
|
$dateTime = new DateTime($date);
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$timestamp = $dateTime->getTimestamp();
|
|
|
|
$format = Session::get(SESSION_DATE_FORMAT, DEFAULT_DATE_FORMAT);
|
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
return self::timestampToString($timestamp, false, $format);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
public static function timestampToString($timestamp, $timezone, $format)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! $timestamp) {
|
2015-03-16 22:45:25 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$date = Carbon::createFromTimeStamp($timestamp);
|
|
|
|
if ($timezone) {
|
|
|
|
$date->tz = $timezone;
|
|
|
|
}
|
|
|
|
if ($date->year < 1900) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $date->format($format);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function toSqlDate($date, $formatResult = true)
|
|
|
|
{
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! $date) {
|
2015-03-16 22:45:25 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-30 21:45:10 +02:00
|
|
|
$format = Session::get(SESSION_DATE_FORMAT, DEFAULT_DATE_FORMAT);
|
2015-08-07 08:14:29 +02:00
|
|
|
$dateTime = DateTime::createFromFormat($format, $date);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! $dateTime) {
|
2016-01-07 23:56:06 +01:00
|
|
|
return $date;
|
2017-01-30 17:05:31 +01:00
|
|
|
} else {
|
2016-01-07 23:56:06 +01:00
|
|
|
return $formatResult ? $dateTime->format('Y-m-d') : $dateTime;
|
2017-01-30 17:05:31 +01:00
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function fromSqlDate($date, $formatResult = true)
|
|
|
|
{
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! $date || $date == '0000-00-00') {
|
2015-03-16 22:45:25 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2015-03-30 21:45:10 +02:00
|
|
|
$format = Session::get(SESSION_DATE_FORMAT, DEFAULT_DATE_FORMAT);
|
2015-05-27 18:52:10 +02:00
|
|
|
$dateTime = DateTime::createFromFormat('Y-m-d', $date);
|
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! $dateTime) {
|
2016-01-20 23:09:10 +01:00
|
|
|
return $date;
|
2017-01-30 17:05:31 +01:00
|
|
|
} else {
|
2016-01-20 23:09:10 +01:00
|
|
|
return $formatResult ? $dateTime->format($format) : $dateTime;
|
2017-01-30 17:05:31 +01:00
|
|
|
}
|
2015-05-27 18:52:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function fromSqlDateTime($date, $formatResult = true)
|
|
|
|
{
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! $date || $date == '0000-00-00 00:00:00') {
|
2015-05-27 18:52:10 +02:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$timezone = Session::get(SESSION_TIMEZONE, DEFAULT_TIMEZONE);
|
|
|
|
$format = Session::get(SESSION_DATETIME_FORMAT, DEFAULT_DATETIME_FORMAT);
|
2015-06-19 01:30:41 +02:00
|
|
|
|
2015-05-27 18:52:10 +02:00
|
|
|
$dateTime = DateTime::createFromFormat('Y-m-d H:i:s', $date);
|
|
|
|
$dateTime->setTimeZone(new DateTimeZone($timezone));
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
return $formatResult ? $dateTime->format($format) : $dateTime;
|
|
|
|
}
|
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
public static function formatTime($t)
|
2015-10-05 19:48:16 +02:00
|
|
|
{
|
|
|
|
// http://stackoverflow.com/a/3172665
|
|
|
|
$f = ':';
|
2017-01-30 20:40:43 +01:00
|
|
|
|
|
|
|
return sprintf('%02d%s%02d%s%02d', floor($t / 3600), $f, ($t / 60) % 60, $f, $t % 60);
|
2015-10-05 19:48:16 +02:00
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
public static function today($formatResult = true)
|
|
|
|
{
|
2015-03-30 21:45:10 +02:00
|
|
|
$timezone = Session::get(SESSION_TIMEZONE, DEFAULT_TIMEZONE);
|
|
|
|
$format = Session::get(SESSION_DATE_FORMAT, DEFAULT_DATE_FORMAT);
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$date = date_create(null, new DateTimeZone($timezone));
|
|
|
|
|
|
|
|
if ($formatResult) {
|
|
|
|
return $date->format($format);
|
|
|
|
} else {
|
|
|
|
return $date;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-10 10:24:27 +02:00
|
|
|
public static function processVariables($str, $client = false)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! $str) {
|
2015-03-16 22:45:25 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$variables = ['MONTH', 'QUARTER', 'YEAR'];
|
2017-01-30 20:40:43 +01:00
|
|
|
for ($i = 0; $i < count($variables); $i++) {
|
2015-03-16 22:45:25 +01:00
|
|
|
$variable = $variables[$i];
|
|
|
|
$regExp = '/:'.$variable.'[+-]?[\d]*/';
|
|
|
|
preg_match_all($regExp, $str, $matches);
|
|
|
|
$matches = $matches[0];
|
|
|
|
if (count($matches) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-30 17:05:31 +01:00
|
|
|
usort($matches, function ($a, $b) {
|
2015-05-27 18:52:10 +02:00
|
|
|
return strlen($b) - strlen($a);
|
|
|
|
});
|
2015-03-16 22:45:25 +01:00
|
|
|
foreach ($matches as $match) {
|
|
|
|
$offset = 0;
|
|
|
|
$addArray = explode('+', $match);
|
|
|
|
$minArray = explode('-', $match);
|
|
|
|
if (count($addArray) > 1) {
|
|
|
|
$offset = intval($addArray[1]);
|
|
|
|
} elseif (count($minArray) > 1) {
|
|
|
|
$offset = intval($minArray[1]) * -1;
|
|
|
|
}
|
|
|
|
|
2017-07-10 10:24:27 +02:00
|
|
|
$locale = $client && $client->language_id ? $client->language->locale : null;
|
|
|
|
$val = self::getDatePart($variable, $offset, $locale);
|
2015-03-16 22:45:25 +01:00
|
|
|
$str = str_replace($match, $val, $str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
2017-07-10 10:24:27 +02:00
|
|
|
private static function getDatePart($part, $offset, $locale)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
|
|
|
$offset = intval($offset);
|
|
|
|
if ($part == 'MONTH') {
|
2017-07-10 10:24:27 +02:00
|
|
|
return self::getMonth($offset, $locale);
|
2015-03-16 22:45:25 +01:00
|
|
|
} elseif ($part == 'QUARTER') {
|
2017-01-30 20:40:43 +01:00
|
|
|
return self::getQuarter($offset);
|
2015-03-16 22:45:25 +01:00
|
|
|
} elseif ($part == 'YEAR') {
|
2017-01-30 20:40:43 +01:00
|
|
|
return self::getYear($offset);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
}
|
2016-10-31 11:59:48 +01:00
|
|
|
|
2016-10-31 10:11:30 +01:00
|
|
|
public static function getMonthOptions()
|
|
|
|
{
|
|
|
|
$months = [];
|
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
for ($i = 1; $i <= count(static::$months); $i++) {
|
|
|
|
$month = static::$months[$i - 1];
|
2016-10-31 10:11:30 +01:00
|
|
|
$number = $i < 10 ? '0' . $i : $i;
|
|
|
|
$months["2000-{$number}-01"] = trans("texts.{$month}");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $months;
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2017-07-10 10:24:27 +02:00
|
|
|
private static function getMonth($offset, $locale)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2016-10-31 10:11:30 +01:00
|
|
|
$months = static::$months;
|
2015-03-16 22:45:25 +01:00
|
|
|
$month = intval(date('n')) - 1;
|
|
|
|
|
|
|
|
$month += $offset;
|
|
|
|
$month = $month % 12;
|
|
|
|
|
|
|
|
if ($month < 0) {
|
|
|
|
$month += 12;
|
|
|
|
}
|
|
|
|
|
2017-07-10 10:24:27 +02:00
|
|
|
return trans('texts.' . $months[$month], [], null, $locale);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static function getQuarter($offset)
|
|
|
|
{
|
|
|
|
$month = intval(date('n')) - 1;
|
|
|
|
$quarter = floor(($month + 3) / 3);
|
|
|
|
$quarter += $offset;
|
|
|
|
$quarter = $quarter % 4;
|
|
|
|
if ($quarter == 0) {
|
|
|
|
$quarter = 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'Q'.$quarter;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function getYear($offset)
|
|
|
|
{
|
|
|
|
$year = intval(date('Y'));
|
|
|
|
|
|
|
|
return $year + $offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getEntityName($entityType)
|
|
|
|
{
|
2017-01-30 20:40:43 +01:00
|
|
|
return ucwords(self::toCamelCase($entityType));
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function getClientDisplayName($model)
|
|
|
|
{
|
|
|
|
if ($model->client_name) {
|
|
|
|
return $model->client_name;
|
|
|
|
} elseif ($model->first_name || $model->last_name) {
|
|
|
|
return $model->first_name.' '.$model->last_name;
|
|
|
|
} else {
|
2016-12-04 10:47:58 +01:00
|
|
|
return $model->email ?: '';
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 15:23:58 +01:00
|
|
|
public static function getVendorDisplayName($model)
|
|
|
|
{
|
2017-01-30 17:05:31 +01:00
|
|
|
if (is_null($model)) {
|
2016-01-19 14:01:19 +01:00
|
|
|
return '';
|
2017-01-30 17:05:31 +01:00
|
|
|
}
|
2016-01-19 14:01:19 +01:00
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
if ($model->vendor_name) {
|
2016-01-19 14:01:19 +01:00
|
|
|
return $model->vendor_name;
|
2017-01-30 17:05:31 +01:00
|
|
|
}
|
2016-01-19 14:01:19 +01:00
|
|
|
|
|
|
|
return 'No vendor name';
|
2016-01-06 15:23:58 +01:00
|
|
|
}
|
2016-01-19 14:01:19 +01:00
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
public static function getPersonDisplayName($firstName, $lastName, $email)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-10-28 20:22:07 +01:00
|
|
|
if ($firstName || $lastName) {
|
|
|
|
return $firstName.' '.$lastName;
|
|
|
|
} elseif ($email) {
|
|
|
|
return $email;
|
|
|
|
} else {
|
|
|
|
return trans('texts.guest');
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function generateLicense()
|
|
|
|
{
|
|
|
|
$parts = [];
|
2017-01-30 20:40:43 +01:00
|
|
|
for ($i = 0; $i < 5; $i++) {
|
2015-03-16 22:45:25 +01:00
|
|
|
$parts[] = strtoupper(str_random(4));
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode('-', $parts);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function lookupEventId($eventName)
|
|
|
|
{
|
|
|
|
if ($eventName == 'create_client') {
|
|
|
|
return EVENT_CREATE_CLIENT;
|
|
|
|
} elseif ($eventName == 'create_invoice') {
|
|
|
|
return EVENT_CREATE_INVOICE;
|
|
|
|
} elseif ($eventName == 'create_quote') {
|
|
|
|
return EVENT_CREATE_QUOTE;
|
|
|
|
} elseif ($eventName == 'create_payment') {
|
|
|
|
return EVENT_CREATE_PAYMENT;
|
2016-01-06 15:23:58 +01:00
|
|
|
} elseif ($eventName == 'create_vendor') {
|
|
|
|
return EVENT_CREATE_VENDOR;
|
2016-07-21 14:35:23 +02:00
|
|
|
} else {
|
|
|
|
return false;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function notifyZapier($subscription, $data)
|
|
|
|
{
|
|
|
|
$curl = curl_init();
|
2016-02-16 16:30:09 +01:00
|
|
|
$jsonEncodedData = json_encode($data);
|
2015-10-02 10:32:13 +02:00
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$opts = [
|
2015-07-02 22:21:29 +02:00
|
|
|
CURLOPT_URL => $subscription->target_url,
|
|
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
|
|
CURLOPT_CUSTOMREQUEST => 'POST',
|
|
|
|
CURLOPT_POST => 1,
|
|
|
|
CURLOPT_POSTFIELDS => $jsonEncodedData,
|
2017-01-30 20:40:43 +01:00
|
|
|
CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'Content-Length: '.strlen($jsonEncodedData)],
|
2015-03-16 22:45:25 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
curl_setopt_array($curl, $opts);
|
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
$result = curl_exec($curl);
|
2015-03-16 22:45:25 +01:00
|
|
|
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
|
|
|
|
|
|
|
curl_close($curl);
|
|
|
|
|
|
|
|
if ($status == 410) {
|
|
|
|
$subscription->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getApiHeaders($count = 0)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'Content-Type' => 'application/json',
|
2016-07-21 14:35:23 +02:00
|
|
|
//'Access-Control-Allow-Origin' => '*',
|
|
|
|
//'Access-Control-Allow-Methods' => 'GET',
|
|
|
|
//'Access-Control-Allow-Headers' => 'Origin, Content-Type, Accept, Authorization, X-Requested-With',
|
|
|
|
//'Access-Control-Allow-Credentials' => 'true',
|
2015-03-16 22:45:25 +01:00
|
|
|
'X-Total-Count' => $count,
|
2015-11-19 12:37:30 +01:00
|
|
|
'X-Ninja-Version' => NINJA_VERSION,
|
2016-07-21 14:35:23 +02:00
|
|
|
//'X-Rate-Limit-Limit' - The number of allowed requests in the current period
|
|
|
|
//'X-Rate-Limit-Remaining' - The number of remaining requests in the current period
|
|
|
|
//'X-Rate-Limit-Reset' - The number of seconds left in the current period,
|
2015-03-16 22:45:25 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-10-13 09:11:44 +02:00
|
|
|
public static function isEmpty($value)
|
|
|
|
{
|
2017-01-30 20:40:43 +01:00
|
|
|
return ! $value || $value == '0' || $value == '0.00' || $value == '0,00';
|
2015-10-13 09:11:44 +02:00
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
public static function startsWith($haystack, $needle)
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
return $needle === '' || strpos($haystack, $needle) === 0;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function endsWith($haystack, $needle)
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
return $needle === '' || substr($haystack, -strlen($needle)) === $needle;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function getEntityRowClass($model)
|
|
|
|
{
|
2015-11-05 23:37:04 +01:00
|
|
|
$str = '';
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-11-05 23:37:04 +01:00
|
|
|
if (property_exists($model, 'is_deleted')) {
|
2017-07-19 16:34:09 +02:00
|
|
|
$str = $model->is_deleted ? 'DISABLED ' : '';
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-11-05 23:37:04 +01:00
|
|
|
if ($model->is_deleted) {
|
|
|
|
$str .= 'ENTITY_DELETED ';
|
|
|
|
}
|
|
|
|
}
|
2016-01-19 14:01:19 +01:00
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
if ($model->deleted_at && $model->deleted_at != '0000-00-00') {
|
|
|
|
$str .= 'ENTITY_ARCHIVED ';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $str;
|
|
|
|
}
|
2015-04-30 19:54:19 +02:00
|
|
|
|
2016-02-23 22:32:39 +01:00
|
|
|
public static function exportData($output, $data, $headers = false)
|
2015-04-30 19:54:19 +02:00
|
|
|
{
|
2016-02-23 22:32:39 +01:00
|
|
|
if ($headers) {
|
|
|
|
fputcsv($output, $headers);
|
|
|
|
} elseif (count($data) > 0) {
|
2015-04-30 19:54:19 +02:00
|
|
|
fputcsv($output, array_keys($data[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($data as $record) {
|
|
|
|
fputcsv($output, $record);
|
|
|
|
}
|
|
|
|
|
|
|
|
fwrite($output, "\n");
|
|
|
|
}
|
2016-01-19 14:01:19 +01:00
|
|
|
|
2015-10-13 09:11:44 +02:00
|
|
|
public static function getFirst($values)
|
|
|
|
{
|
2015-08-20 10:02:03 +02:00
|
|
|
if (is_array($values)) {
|
|
|
|
return count($values) ? $values[0] : false;
|
2016-07-21 14:35:23 +02:00
|
|
|
} else {
|
|
|
|
return $values;
|
2015-08-20 10:02:03 +02:00
|
|
|
}
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
2015-09-03 09:32:39 +02:00
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
// nouns in German and French should be uppercase
|
|
|
|
// TODO remove this
|
|
|
|
public static function transFlowText($key)
|
|
|
|
{
|
|
|
|
$str = trans("texts.$key");
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! in_array(App::getLocale(), ['de', 'fr'])) {
|
2016-07-21 14:35:23 +02:00
|
|
|
$str = strtolower($str);
|
|
|
|
}
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
return $str;
|
2015-09-03 09:32:39 +02:00
|
|
|
}
|
2015-09-10 19:50:09 +02:00
|
|
|
|
2017-04-18 14:44:08 +02:00
|
|
|
public static function getSubdomain($url)
|
2015-10-13 09:11:44 +02:00
|
|
|
{
|
2017-04-18 14:44:08 +02:00
|
|
|
$parts = parse_url($url);
|
2015-09-10 19:50:09 +02:00
|
|
|
$subdomain = '';
|
2017-04-18 14:44:08 +02:00
|
|
|
|
2015-09-10 19:50:09 +02:00
|
|
|
if (isset($parts['host'])) {
|
|
|
|
$host = explode('.', $parts['host']);
|
|
|
|
if (count($host) > 2) {
|
|
|
|
$subdomain = $host[0];
|
|
|
|
}
|
|
|
|
}
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2015-09-10 19:50:09 +02:00
|
|
|
return $subdomain;
|
|
|
|
}
|
|
|
|
|
2017-04-18 14:44:08 +02:00
|
|
|
public static function getSubdomainPlaceholder()
|
|
|
|
{
|
|
|
|
return static::getSubdomain(SITE_URL);
|
|
|
|
}
|
|
|
|
|
2015-10-13 09:11:44 +02:00
|
|
|
public static function getDomainPlaceholder()
|
|
|
|
{
|
2015-09-10 19:50:09 +02:00
|
|
|
$parts = parse_url(SITE_URL);
|
|
|
|
$domain = '';
|
|
|
|
if (isset($parts['host'])) {
|
|
|
|
$host = explode('.', $parts['host']);
|
|
|
|
if (count($host) > 2) {
|
|
|
|
array_shift($host);
|
|
|
|
$domain .= implode('.', $host);
|
|
|
|
} else {
|
|
|
|
$domain .= $parts['host'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isset($parts['path'])) {
|
|
|
|
$domain .= $parts['path'];
|
|
|
|
}
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2015-09-10 19:50:09 +02:00
|
|
|
return $domain;
|
|
|
|
}
|
2015-09-17 21:01:06 +02:00
|
|
|
|
2015-10-13 09:11:44 +02:00
|
|
|
public static function replaceSubdomain($domain, $subdomain)
|
|
|
|
{
|
2015-09-17 21:01:06 +02:00
|
|
|
$parsedUrl = parse_url($domain);
|
|
|
|
$host = explode('.', $parsedUrl['host']);
|
|
|
|
if (count($host) > 0) {
|
|
|
|
$oldSubdomain = $host[0];
|
|
|
|
$domain = str_replace("://{$oldSubdomain}.", "://{$subdomain}.", $domain);
|
|
|
|
}
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2015-09-17 21:01:06 +02:00
|
|
|
return $domain;
|
|
|
|
}
|
2015-10-11 16:41:09 +02:00
|
|
|
|
2015-10-13 09:11:44 +02:00
|
|
|
public static function splitName($name)
|
|
|
|
{
|
2015-10-11 16:41:09 +02:00
|
|
|
$name = trim($name);
|
|
|
|
$lastName = (strpos($name, ' ') === false) ? '' : preg_replace('#.*\s([\w-]*)$#', '$1', $name);
|
2015-10-13 09:11:44 +02:00
|
|
|
$firstName = trim(preg_replace('#'.$lastName.'#', '', $name));
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
return [$firstName, $lastName];
|
2015-10-11 16:41:09 +02:00
|
|
|
}
|
|
|
|
|
2015-10-13 09:11:44 +02:00
|
|
|
public static function decodePDF($string)
|
|
|
|
{
|
|
|
|
$string = str_replace('data:application/pdf;base64,', '', $string);
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2015-12-13 21:12:54 +01:00
|
|
|
return base64_decode($string);
|
2015-10-13 09:11:44 +02:00
|
|
|
}
|
2015-10-13 17:44:01 +02:00
|
|
|
|
|
|
|
public static function cityStateZip($city, $state, $postalCode, $swap)
|
|
|
|
{
|
|
|
|
$str = $city;
|
|
|
|
|
|
|
|
if ($state) {
|
|
|
|
if ($str) {
|
|
|
|
$str .= ', ';
|
|
|
|
}
|
|
|
|
$str .= $state;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($swap) {
|
|
|
|
return $postalCode . ' ' . $str;
|
2016-07-21 14:35:23 +02:00
|
|
|
} else {
|
|
|
|
return $str . ' ' . $postalCode;
|
2015-10-13 17:44:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-19 20:15:02 +02:00
|
|
|
public static function formatWebsite($link)
|
2015-10-13 17:44:01 +02:00
|
|
|
{
|
2017-07-19 20:15:02 +02:00
|
|
|
if (! $link) {
|
2015-10-13 17:44:01 +02:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2017-07-19 20:15:02 +02:00
|
|
|
$title = $link;
|
|
|
|
if (substr($link, 0, 4) != 'http') {
|
|
|
|
$link = 'http://' . $link;
|
2015-10-13 17:44:01 +02:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
return link_to($link, $title, ['target' => '_blank']);
|
2015-10-13 17:44:01 +02:00
|
|
|
}
|
|
|
|
|
2015-12-07 14:34:55 +01:00
|
|
|
public static function wrapAdjustment($adjustment, $currencyId, $countryId)
|
2015-10-13 17:44:01 +02:00
|
|
|
{
|
|
|
|
$class = $adjustment <= 0 ? 'success' : 'default';
|
2017-01-30 20:40:43 +01:00
|
|
|
$adjustment = self::formatMoney($adjustment, $currencyId, $countryId);
|
|
|
|
|
2015-10-13 17:44:01 +02:00
|
|
|
return "<h4><div class=\"label label-{$class}\">$adjustment</div></h4>";
|
|
|
|
}
|
2015-10-28 20:22:07 +01:00
|
|
|
|
|
|
|
public static function copyContext($entity1, $entity2)
|
|
|
|
{
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! $entity2) {
|
2015-10-28 20:22:07 +01:00
|
|
|
return $entity1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields = [
|
|
|
|
'contact_id',
|
|
|
|
'payment_id',
|
|
|
|
'invoice_id',
|
|
|
|
'credit_id',
|
2017-01-30 20:40:43 +01:00
|
|
|
'invitation_id',
|
2015-10-28 20:22:07 +01:00
|
|
|
];
|
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
$fields1 = $entity1->getAttributes();
|
2015-10-28 20:22:07 +01:00
|
|
|
$fields2 = $entity2->getAttributes();
|
|
|
|
|
|
|
|
foreach ($fields as $field) {
|
|
|
|
if (isset($fields2[$field]) && $fields2[$field]) {
|
|
|
|
$entity1->$field = $entity2->$field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $entity1;
|
|
|
|
}
|
2015-11-01 19:21:11 +01:00
|
|
|
|
2015-12-14 22:05:17 +01:00
|
|
|
public static function addHttp($url)
|
|
|
|
{
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! preg_match('~^(?:f|ht)tps?://~i', $url)) {
|
2016-07-03 18:11:58 +02:00
|
|
|
$url = 'http://' . $url;
|
2015-12-14 22:05:17 +01:00
|
|
|
}
|
2016-01-19 14:01:19 +01:00
|
|
|
|
2015-12-14 22:05:17 +01:00
|
|
|
return $url;
|
|
|
|
}
|
2016-05-12 04:55:37 +02:00
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
public static function setupWePay($accountGateway = null)
|
2016-05-12 04:55:37 +02:00
|
|
|
{
|
|
|
|
if (WePay::getEnvironment() == 'none') {
|
2016-05-13 15:30:22 +02:00
|
|
|
if (WEPAY_ENVIRONMENT == WEPAY_STAGE) {
|
2016-05-12 04:55:37 +02:00
|
|
|
WePay::useStaging(WEPAY_CLIENT_ID, WEPAY_CLIENT_SECRET);
|
|
|
|
} else {
|
|
|
|
WePay::useProduction(WEPAY_CLIENT_ID, WEPAY_CLIENT_SECRET);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($accountGateway) {
|
|
|
|
return new WePay($accountGateway->getConfig()->accessToken);
|
2016-07-21 14:35:23 +02:00
|
|
|
} else {
|
|
|
|
return new WePay(null);
|
2016-05-12 04:55:37 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-04 16:20:22 +02:00
|
|
|
|
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* Gets an array of weekday names (in English).
|
2016-07-04 16:20:22 +02:00
|
|
|
*
|
|
|
|
* @see getTranslatedWeekdayNames()
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Support\Collection
|
|
|
|
*/
|
|
|
|
public static function getWeekdayNames()
|
|
|
|
{
|
|
|
|
return collect(static::$weekdayNames);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* Gets an array of translated weekday names.
|
2016-07-04 16:20:22 +02:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Support\Collection
|
|
|
|
*/
|
|
|
|
public static function getTranslatedWeekdayNames()
|
|
|
|
{
|
|
|
|
return collect(static::$weekdayNames)->transform(function ($day) {
|
2017-01-30 17:05:31 +01:00
|
|
|
return trans('texts.'.strtolower($day));
|
2016-07-04 16:20:22 +02:00
|
|
|
});
|
|
|
|
}
|
2016-09-21 15:18:26 +02:00
|
|
|
|
2016-12-11 23:25:22 +01:00
|
|
|
public static function getReadableUrl($path)
|
|
|
|
{
|
|
|
|
$url = static::getDocsUrl($path);
|
|
|
|
|
|
|
|
$parts = explode('/', $url);
|
|
|
|
$part = $parts[count($parts) - 1];
|
|
|
|
$part = str_replace('#', '> ', $part);
|
|
|
|
$part = str_replace(['.html', '-', '_'], ' ', $part);
|
|
|
|
|
|
|
|
if ($part) {
|
|
|
|
return trans('texts.user_guide') . ': ' . ucwords($part);
|
|
|
|
} else {
|
|
|
|
return trans('texts.user_guide');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 15:18:26 +02:00
|
|
|
public static function getDocsUrl($path)
|
|
|
|
{
|
|
|
|
$page = '';
|
|
|
|
$parts = explode('/', $path);
|
|
|
|
$first = count($parts) ? $parts[0] : false;
|
|
|
|
$second = count($parts) > 1 ? $parts[1] : false;
|
|
|
|
|
|
|
|
$entityTypes = [
|
|
|
|
'clients',
|
|
|
|
'invoices',
|
|
|
|
'payments',
|
|
|
|
'recurring_invoices',
|
|
|
|
'credits',
|
|
|
|
'quotes',
|
|
|
|
'tasks',
|
|
|
|
'expenses',
|
|
|
|
'vendors',
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($path == 'dashboard') {
|
|
|
|
$page = '/introduction.html#dashboard';
|
|
|
|
} elseif (in_array($path, $entityTypes)) {
|
|
|
|
$page = "/{$path}.html#list-" . str_replace('_', '-', $path);
|
|
|
|
} elseif (in_array($first, $entityTypes)) {
|
|
|
|
$action = ($first == 'payments' || $first == 'credits') ? 'enter' : 'create';
|
|
|
|
$page = "/{$first}.html#{$action}-" . substr(str_replace('_', '-', $first), 0, -1);
|
|
|
|
} elseif ($first == 'expense_categories') {
|
|
|
|
$page = '/expenses.html#expense-categories';
|
|
|
|
} elseif ($first == 'settings') {
|
|
|
|
if ($second == 'bank_accounts') {
|
|
|
|
$page = ''; // TODO write docs
|
2016-09-21 20:26:25 +02:00
|
|
|
} elseif (in_array($second, \App\Models\Account::$basicSettings)) {
|
2016-09-21 15:18:26 +02:00
|
|
|
if ($second == 'products') {
|
|
|
|
$second = 'product_library';
|
|
|
|
} elseif ($second == 'notifications') {
|
|
|
|
$second = 'email_notifications';
|
|
|
|
}
|
|
|
|
$page = '/settings.html#' . str_replace('_', '-', $second);
|
2016-09-21 20:26:25 +02:00
|
|
|
} elseif (in_array($second, \App\Models\Account::$advancedSettings)) {
|
2016-09-21 15:18:26 +02:00
|
|
|
$page = "/{$second}.html";
|
|
|
|
} elseif ($second == 'customize_design') {
|
|
|
|
$page = '/invoice_design.html#customize';
|
|
|
|
}
|
|
|
|
} elseif ($first == 'tax_rates') {
|
|
|
|
$page = '/settings.html#tax-rates';
|
|
|
|
} elseif ($first == 'products') {
|
|
|
|
$page = '/settings.html#product-library';
|
|
|
|
} elseif ($first == 'users') {
|
|
|
|
$page = '/user_management.html#create-user';
|
|
|
|
}
|
|
|
|
|
|
|
|
return url(NINJA_DOCS_URL . $page);
|
|
|
|
}
|
2016-10-27 16:26:42 +02:00
|
|
|
|
|
|
|
public static function calculateTaxes($amount, $taxRate1, $taxRate2)
|
|
|
|
{
|
|
|
|
$tax1 = round($amount * $taxRate1 / 100, 2);
|
|
|
|
$tax2 = round($amount * $taxRate2 / 100, 2);
|
|
|
|
|
|
|
|
return round($amount + $tax1 + $tax2, 2);
|
|
|
|
}
|
2017-03-08 07:46:24 +01:00
|
|
|
|
|
|
|
public static function truncateString($string, $length)
|
|
|
|
{
|
2017-03-08 10:31:34 +01:00
|
|
|
return strlen($string) > $length ? rtrim(substr($string, 0, $length)) . '...' : $string;
|
2017-03-08 07:46:24 +01:00
|
|
|
}
|
2017-04-27 20:57:47 +02:00
|
|
|
|
|
|
|
// http://stackoverflow.com/a/14238078/497368
|
|
|
|
public static function isInterlaced($filename)
|
|
|
|
{
|
|
|
|
$handle = fopen($filename, 'r');
|
|
|
|
$contents = fread($handle, 32);
|
|
|
|
fclose($handle);
|
|
|
|
return( ord($contents[28]) != 0 );
|
2017-04-28 11:23:56 +02:00
|
|
|
}
|
2017-08-01 06:44:12 +02:00
|
|
|
|
|
|
|
//Source: https://stackoverflow.com/questions/3302857/algorithm-to-get-the-excel-like-column-name-of-a-number
|
|
|
|
public static function num2alpha($n)
|
|
|
|
{
|
|
|
|
for($r = ""; $n >= 0; $n = intval($n / 26) - 1)
|
|
|
|
$r = chr($n%26 + 0x41) . $r;
|
|
|
|
return $r;
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|