first commit

This commit is contained in:
denisdulici
2017-09-14 22:21:00 +03:00
commit 515bdaf5cd
598 changed files with 48030 additions and 0 deletions

61
app/Traits/Currencies.php Normal file
View File

@@ -0,0 +1,61 @@
<?php
namespace App\Traits;
use ClickNow\Money\Money;
use ClickNow\Money\Currency;
trait Currencies
{
public function convert($amount, $code, $rate, $format = false)
{
$default = new Currency(setting('general.default_currency', 'USD'));
if ($format) {
$money = Money::$code($amount, true)->convert($default, $rate)->format();
} else {
$money = Money::$code($amount)->convert($default, $rate)->getAmount();
}
return $money;
}
public function reverseConvert($amount, $code, $rate, $format = false)
{
$default = setting('general.default_currency', 'USD');
$code = new Currency($code);
if ($format) {
$money = Money::$default($amount, true)->convert($code, $rate)->format();
} else {
$money = Money::$default($amount)->convert($code, $rate)->getAmount();
}
return $money;
}
public function dynamicConvert($default, $amount, $code, $rate, $format = false)
{
$code = new Currency($code);
if ($format) {
$money = Money::$default($amount, true)->convert($code, $rate)->format();
} else {
$money = Money::$default($amount)->convert($code, $rate)->getAmount();
}
return $money;
}
public function getConvertedAmount($format = false)
{
return $this->convert($this->amount, $this->currency_code, $this->currency_rate, $format);
}
public function getDynamicConvertedAmount($format = false)
{
return $this->dynamicConvert($this->default_currency_code, $this->amount, $this->currency_code, $this->currency_rate, $format);
}
}

78
app/Traits/DateTime.php Normal file
View File

@@ -0,0 +1,78 @@
<?php
namespace App\Traits;
use Date;
trait DateTime
{
/*
* Get the date format based on company settings.
* getDateFormat method is used by Eloquent
*
* @return string
*/
public function getCompanyDateFormat()
{
$chars = ['dash' => '-', 'slash' => '/', 'dot' => '.', 'comma' => ',', 'space' => ' '];
$date_format = setting('general.date_format', 'd F Y');
$date_separator = $chars[setting('general.date_separator', 'space')];
return str_replace(' ', $date_separator, $date_format);
}
public static function getMonthsOfYear($field)
{
$year = request('year');
// Get current year if not set
if (empty($year)) {
$year = Date::now()->year;
}
$start = Date::parse($year . '-01-01')->format('Y-m-d');
$end = Date::parse($year . '-12-31')->format('Y-m-d');
return static::whereBetween($field, [$start, $end])->get();
}
public function getTimezones()
{
// The list of available timezone groups to use.
$use_zones = array('Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific');
// Get the list of time zones from the server.
$zones = \DateTimeZone::listIdentifiers();
// Build the group lists.
foreach ($zones as $zone) {
// Time zones not in a group we will ignore.
if (strpos($zone, '/') === false) {
continue;
}
// Get the group/locale from the timezone.
list ($group, $locale) = explode('/', $zone, 2);
// Only use known groups.
if (in_array($group, $use_zones)) {
// Initialize the group if necessary.
if (!isset($groups[$group])) {
$groups[$group] = array();
}
// Only add options where a locale exists.
if (!empty($locale)) {
$groups[$group][$zone] = str_replace('_', ' ', $locale);
}
}
}
// Sort the group lists.
ksort($groups);
return $groups;
}
}

298
app/Traits/Modules.php Normal file
View File

@@ -0,0 +1,298 @@
<?php
namespace App\Traits;
use Artisan;
use File;
use Zipper;
use Module;
use App\Utilities\Info;
use GuzzleHttp\Client;
trait Modules
{
public function getModules()
{
$response = $this->getRemote('modules/items');
if ($response->getStatusCode() == 200) {
return json_decode($response->getBody())->data;
}
return array();
}
public function getModule($alias)
{
$response = $this->getRemote('modules/items/' . $alias);
if ($response->getStatusCode() == 200) {
return json_decode($response->getBody())->data;
}
return array();
}
public function getCategories()
{
$response = $this->getRemote('modules/categories');
if ($response->getStatusCode() == 200) {
return json_decode($response->getBody())->data;
}
return array();
}
public function getModulesByCategory($alias)
{
$response = $this->getRemote('modules/categories/' . $alias);
if ($response->getStatusCode() == 200) {
return json_decode($response->getBody())->data;
}
return array();
}
public function getPaidModules()
{
$response = $this->getRemote('modules/paid');
if ($response->getStatusCode() == 200) {
return json_decode($response->getBody())->data;
}
return array();
}
public function getNewModules()
{
$response = $this->getRemote('modules/new');
if ($response->getStatusCode() == 200) {
return json_decode($response->getBody())->data;
}
return array();
}
public function getFreeModules()
{
$response = $this->getRemote('modules/free');
if ($response->getStatusCode() == 200) {
return json_decode($response->getBody())->data;
}
return array();
}
public function getCoreVersion()
{
$data['query'] = Info::all();
$response = $this->getRemote('core/version', 'GET', $data);
if ($response->getStatusCode() == 200) {
return $response->json();
}
return array();
}
public function downloadModule($path)
{
$response = $this->getRemote($path);
if ($response->getStatusCode() == 200) {
$file = $response->getBody()->getContents();
$path = 'temp-' . md5(mt_rand());
$temp_path = storage_path('app/temp') . '/' . $path;
$file_path = $temp_path . '/upload.zip';
// Create tmp directory
if (!File::isDirectory($temp_path)) {
File::makeDirectory($temp_path);
}
// Add content to the Zip file
$uploaded = is_int(file_put_contents($file_path, $file)) ? true : false;
if (!$uploaded) {
return false;
}
$data = array(
'path' => $path
);
return array(
'success' => true,
'errors' => false,
'data' => $data,
);
}
return array(
'success' => false,
'errors' => true,
'data' => null,
);
}
public function unzipModule($path)
{
$temp_path = storage_path('app/temp') . '/' . $path;
$file = $temp_path . '/upload.zip';
// Unzip the file
try {
Zipper::make($file)->extractTo($temp_path);
} catch (\RuntimeException $e) {
return array(
'success' => false,
'errors' => true,
'data' => null,
);
}
// Remove Zip
Zipper::delete();
$data = array(
'path' => $path
);
return array(
'success' => true,
'errors' => false,
'data' => $data,
);
}
public function installModule($path)
{
$temp_path = storage_path('app/temp') . '/' . $path;
$modules_path = base_path() . '/modules';
// Create modules directory
if (!File::isDirectory($modules_path)) {
File::makeDirectory($modules_path);
}
$module = json_decode(file_get_contents($temp_path . '/module.json'));
$module_path = $modules_path . '/' . $module->name;
// Create module directory
if (!File::isDirectory($module_path)) {
File::makeDirectory($module_path);
}
// Move all files/folders from temp path then delete it
File::copyDirectory($temp_path, $module_path);
File::deleteDirectory($temp_path);
// Update database
Artisan::call('migrate', ['--force' => true]);
$data = array(
'path' => $path
);
return array(
'success' => true,
'installed' => true,
'errors' => false,
'data' => $data,
);
}
public function uninstallModule($alias)
{
$module = Module::findByAlias($alias);
$data = array(
'name' => $module->get('name'),
'category' => $module->get('category'),
'version' => $module->get('version'),
);
$module->delete();
Artisan::call('cache:clear');
return array(
'success' => true,
'errors' => false,
'data' => $data
);
}
public function enabledModule($alias)
{
$module = Module::findByAlias($alias);
$data = array(
'name' => $module->get('name'),
'category' => $module->get('category'),
'version' => $module->get('version'),
);
$module->enable();
Artisan::call('cache:clear');
return array(
'success' => true,
'errors' => false,
'data' => $data
);
}
public function disabledModule($alias)
{
$module = Module::findByAlias($alias);
$data = array(
'name' => $module->get('name'),
'category' => $module->get('category'),
'version' => $module->get('version'),
);
$module->disable();
Artisan::call('cache:clear');
return array(
'success' => true,
'errors' => false,
'data' => $data
);
}
protected function getRemote($path, $method = 'GET', $data = array())
{
$base = 'https://akaunting.com/api/';
$client = new Client(['verify' => false, 'base_uri' => $base]);
$headers['headers'] = array(
'Authorization' => 'Bearer ' . setting('general.api_token'),
'Accept' => 'application/json',
);
$data = array_merge($data, $headers);
$result = $client->request($method, $path, $data);
return $result;
}
}

27
app/Traits/SiteApi.php Normal file
View File

@@ -0,0 +1,27 @@
<?php
namespace App\Traits;
use GuzzleHttp\Client;
trait SiteApi
{
protected static function getRemote($url, $data = array())
{
$base = 'https://akaunting.com/api/';
$client = new Client(['verify' => false, 'base_uri' => $base]);
$headers['headers'] = array(
'Authorization' => 'Bearer ' . setting('general.api_token'),
'Accept' => 'application/json',
);
$data = array_merge($data, $headers);
$result = $client->get($url, $data);
return $result;
}
}

26
app/Traits/Uploads.php Normal file
View File

@@ -0,0 +1,26 @@
<?php
namespace App\Traits;
trait Uploads
{
public function getUploadedFilePath($file, $folder = 'settings', $company_id = null)
{
$path = '';
if (!$file || !$file->isValid()) {
return $path;
}
if (!$company_id) {
$company_id = session('company_id');
}
$file_name = $file->getClientOriginalName();
$path = 'storage/app/' . $file->storeAs('uploads/' . $company_id . '/' . $folder, $file_name);
return $path;
}
}