Skip to content

Commit

Permalink
Client Settings (#2668)
Browse files Browse the repository at this point in the history
* Clean up Client Show

* Working on Show Client menu action

* working on client view permissions

* Finishing up Client Statement View

* Workig on client settings

* add mix manifest

* css for client settings

* Client Settings

* Working on Client Settings

* Implement StartupCheck and static seeders

* Implement cached statics in view composers

* Working on client settings

* Payment Terms

* Working on Payment Terms View Composer

* Payment Terms builder

* Client Settings

* refactor companies table

* Refactor for company settings, move settings to json

* Set object cast on settings column of Company table

* Fixes for refactor of companies and clients table

* Test

* Client Settings Datamapper

* Client Settings

* Default client language

* Client Settings

* Working on client settings options

* Client Settings

* Settings Json serialization/deserialization handling
  • Loading branch information
turbo124 committed Feb 17, 2019
1 parent 1ad1973 commit eddb9ad
Show file tree
Hide file tree
Showing 45 changed files with 3,470 additions and 1,756 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ MAIL_ENCRYPTION=null
MULTI_DB_ENABLED=true
POSTMARK_API_TOKEN=

GOOGLE_MAPS_API_KEY
GOOGLE_MAPS_API_KEY=
COMPANY_SETTINGS='{"timezone_id":"15","currency_id":"1","language_id":"1","payment_terms":"7"}'
70 changes: 70 additions & 0 deletions app/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,75 @@
define('RANDOM_KEY_LENGTH', 32); //63340286662973277706162286946811886609896461828096 combinations


$cached_tables = [
'currencies' => 'App\Models\Currency',
// 'sizes' => 'App\Models\Size',
'industries' => 'App\Models\Industry',
// 'timezones' => 'App\Models\Timezone',
// 'dateFormats' => 'App\Models\DateFormat',
// 'datetimeFormats' => 'App\Models\DatetimeFormat',
'languages' => 'App\Models\Language',
'paymentTypes' => 'App\Models\PaymentType',
'countries' => 'App\Models\Country',
// 'invoiceDesigns' => 'App\Models\InvoiceDesign',
// 'invoiceStatus' => 'App\Models\InvoiceStatus',
// 'frequencies' => 'App\Models\Frequency',
// 'gateways' => 'App\Models\Gateway',
// 'gatewayTypes' => 'App\Models\GatewayType',
// 'fonts' => 'App\Models\Font',
// 'banks' => 'App\Models\Bank',
];

define('CACHED_TABLES', serialize($cached_tables));

define('CACHED_PAYMENT_TERMS', serialize(
[
[
'num_days' => 0,
'name' => '',
],
[
'num_days' => 7,
'name' => '',
],
[
'num_days' => 10,
'name' => '',
],
[
'num_days' => 14,
'name' => '',
],
[
'num_days' => 15,
'name' => '',
],
[
'num_days' => 30,
'name' => '',
],
[
'num_days' => 60,
'name' => '',
],
[
'num_days' => 90,
'name' => '',
]
]));

define('GATEWAY_TYPE_CREDIT_CARD', 1);
define('GATEWAY_TYPE_BANK_TRANSFER', 2);
define('GATEWAY_TYPE_PAYPAL', 3);
define('GATEWAY_TYPE_BITCOIN', 4);
define('GATEWAY_TYPE_DWOLLA', 5);
define('GATEWAY_TYPE_CUSTOM1', 6);
define('GATEWAY_TYPE_ALIPAY', 7);
define('GATEWAY_TYPE_SOFORT', 8);
define('GATEWAY_TYPE_SEPA', 9);
define('GATEWAY_TYPE_GOCARDLESS', 10);
define('GATEWAY_TYPE_APPLE_PAY', 11);
define('GATEWAY_TYPE_CUSTOM2', 12);
define('GATEWAY_TYPE_CUSTOM3', 13);
define('GATEWAY_TYPE_TOKEN', 'token');

28 changes: 28 additions & 0 deletions app/DataMapper/BaseSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\DataMapper;

/**
* ClientSettings
*/
class BaseSettings
{
/**
* Migrates properties of the datamapper classes when new properties are added
*
* @param \stdClass $object Datamapper settings object
* @return \stdClass $object Datamapper settings object updated
*/
public function migrate(\stdClass $object) : \stdClass
{
$properties = self::default();

foreach($properties as $property)
{
if(!property_exists($object, $property))
$object->{$property} = NULL;
}

return $object;
}
}
32 changes: 32 additions & 0 deletions app/DataMapper/ClientSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\DataMapper;

/**
* ClientSettings
*/
class ClientSettings extends BaseSettings
{
public $timezone_id;
public $language_id;
public $currency_id;

/**
* @return \stdClass
*
*/
public static function defaults() : \stdClass
{

return (object)[
'timezone_id' => NULL,
'language_id' => NULL,
'currency_id' => NULL,
'payment_terms' => NULL,
];

}


}

79 changes: 79 additions & 0 deletions app/DataMapper/CompanySettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace App\DataMapper;

/**
* CompanySettings
*/
class CompanySettings extends BaseSettings
{

public $timezone_id;
public $language_id;
public $currency_id;
public $payment_terms;
public $custom_label1;
public $custom_value1;
public $custom_label2;
public $custom_value2;
public $custom_label3;
public $custom_value3;
public $custom_label4;
public $custom_value5;
public $custom_client_label1;
public $custom_client_label2;
public $custom_client_label3;
public $custom_client_label4;
public $custom_client_contact_label1;
public $custom_client_contact_label2;
public $custom_client_contact_label3;
public $custom_client_contact_label4;
public $custom_invoice_label1;
public $custom_invoice_label2;
public $custom_invoice_label3;
public $custom_invoice_label4;
public $custom_product_label1;
public $custom_product_label2;
public $custom_product_label3;
public $custom_product_label4;
public $custom_task_label1;
public $custom_task_label2;
public $custom_task_label3;
public $custom_task_label4;
public $custom_expense_label1;
public $custom_expense_label2;
public $custom_expense_label3;
public $custom_expense_label4;
public $translations;

/**
* Cast object values and return entire class
* prevents missing properties from not being returned
* and always ensure an up to date class is returned
*
* @return \stdClass
*/
public function __construct($obj)
{
foreach($obj as $key => $value)
$this->{$key} = $value;
}

/**
* Provides class defaults on init
* @return object
*/
public static function defaults() : object
{
$config = json_decode(config('ninja.settings'));

return (object) [
'timezone_id' => $config->timezone_id,
'language_id' => $config->language_id,
'currency_id' => $config->currency_id,
'payment_terms' => $config->payment_terms,
'translations' => (object) [],
];
}
}

2 changes: 1 addition & 1 deletion app/DataMapper/DefaultSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Class DefaultSettings
* @package App\DataMapper
*/
class DefaultSettings
class DefaultSettings extends BaseSettings
{

/**
Expand Down
4 changes: 3 additions & 1 deletion app/Factory/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Factory;

use App\DataMapper\ClientSettings;
use App\Models\Client;

class ClientFactory
Expand All @@ -18,7 +19,8 @@ public static function create(int $company_id, int $user_id) :Client
$client->paid_to_date = 0;
$client->country_id = 4;
$client->is_deleted = 0;

$client->settings = ClientSettings::defaults();

$client_contact = ClientContactFactory::create($company_id, $user_id);
$client->contacts->add($client_contact);

Expand Down
5 changes: 3 additions & 2 deletions app/Filters/ClientFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function baseQuery(int $company_id, User $user) : Builder
->where('client_contacts.deleted_at', '=', null)
//->whereRaw('(clients.name != "" or contacts.first_name != "" or contacts.last_name != "" or contacts.email != "")') // filter out buy now invoices
->select(
DB::raw('COALESCE(clients.currency_id, companies.currency_id) currency_id'),
// DB::raw('COALESCE(clients.currency_id, companies.currency_id) currency_id'),
DB::raw('COALESCE(clients.country_id, companies.country_id) country_id'),
DB::raw("CONCAT(COALESCE(client_contacts.first_name, ''), ' ', COALESCE(client_contacts.last_name, '')) contact"),
'clients.id',
Expand All @@ -151,7 +151,8 @@ public function baseQuery(int $company_id, User $user) : Builder
'clients.deleted_at',
'clients.is_deleted',
'clients.user_id',
'clients.id_number'
'clients.id_number',
'clients.settings'
);

/**
Expand Down
10 changes: 7 additions & 3 deletions app/Http/Controllers/ClientController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
use App\Models\Client;
use App\Models\ClientContact;
use App\Models\Country;
use App\Models\Currency;
use App\Models\Size;
use App\Repositories\ClientRepository;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\MakesMenu;
use App\Utils\Traits\UserSessionAttributes;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;


/**
* Class ClientController
Expand Down Expand Up @@ -121,11 +125,11 @@ public function edit(EditClientRequest $request, Client $client)

$data = [
'client' => $client,
'settings' => $client,
'settings' => collect($client->settings),
'pills' => $this->makeEntityTabMenu(Client::class),
'hashed_id' => $this->encodePrimarykey($client->id),
'countries' => Country::all(),
'company' => auth()->user()->company()
'company' => auth()->user()->company(),
'sizes' => Size::all(),
];

return view('client.edit', $data);
Expand Down
6 changes: 5 additions & 1 deletion app/Http/Controllers/TranslationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ public function index()
});

header('Content-Type: text/javascript');
echo('i18n = ' . json_encode($strings) . ';');
echo('i18n = ' . $this->easyMinify(json_encode($strings)) . ';');
exit();
}

private function easyMinify($javascript){
return preg_replace(array("/\s+\n/", "/\n\s+/", "/ +/"), array("\n", "\n ", " "), $javascript);
}

/**
* Show the form for creating a new resource.
*
Expand Down
1 change: 1 addition & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Kernel extends HttpKernel
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\StartupCheck::class,
],

'api' => [
Expand Down
60 changes: 60 additions & 0 deletions app/Http/Middleware/StartupCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Session;
use Closure;


/**
* Class StartupCheck.
*/
class StartupCheck
{
/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure $next
*
* @return mixed
*/
public function handle(Request $request, Closure $next)
{

$cached_tables = unserialize(CACHED_TABLES);

if (Input::has('clear_cache')) {
Session::flash('message', 'Cache cleared');
}
foreach ($cached_tables as $name => $class) {
if (Input::has('clear_cache') || ! Cache::has($name)) {
// check that the table exists in case the migration is pending
if (! Schema::hasTable((new $class())->getTable())) {
continue;
}
if ($name == 'paymentTerms') {
$orderBy = 'num_days';
} elseif ($name == 'fonts') {
$orderBy = 'sort_order';
} elseif (in_array($name, ['currencies', 'industries', 'languages', 'countries', 'banks'])) {
$orderBy = 'name';
} else {
$orderBy = 'id';
}
$tableData = $class::orderBy($orderBy)->get();
if ($tableData->count()) {
Cache::forever($name, $tableData);
}
}
}

$response = $next($request);

return $response;
}
}

0 comments on commit eddb9ad

Please sign in to comment.