1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00

Merge pull request #5757 from turbo124/v5-develop

Fixes for importing Ninja clients/contacts
This commit is contained in:
David Bomba 2021-05-20 14:16:30 +10:00 committed by GitHub
commit 6614cc4248
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 124 additions and 4 deletions

View File

@ -14,8 +14,10 @@ namespace App\Http\Requests\Company;
use App\DataMapper\CompanySettings;
use App\Http\Requests\Request;
use App\Http\ValidationRules\Company\ValidCompanyQuantity;
use App\Http\ValidationRules\Company\ValidSubdomain;
use App\Http\ValidationRules\ValidSettingsRule;
use App\Models\Company;
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
class StoreCompanyRequest extends Request
@ -45,7 +47,13 @@ class StoreCompanyRequest extends Request
if (isset($input['portal_mode']) && ($input['portal_mode'] == 'domain' || $input['portal_mode'] == 'iframe')) {
$rules['portal_domain'] = 'sometimes|url';
} else {
$rules['subdomain'] = 'nullable|alpha_num';
if(Ninja::isHosted()){
$rules['subdomain'] = ['nullable|alpha_num', new ValidSubdomain($this->all())];
}
else
$rules['subdomain'] = 'nullable|alpha_num';
}
return $rules;

View File

@ -13,7 +13,9 @@ namespace App\Http\Requests\Company;
use App\DataMapper\CompanySettings;
use App\Http\Requests\Request;
use App\Http\ValidationRules\Company\ValidSubdomain;
use App\Http\ValidationRules\ValidSettingsRule;
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
class UpdateCompanyRequest extends Request
@ -46,7 +48,12 @@ class UpdateCompanyRequest extends Request
if (isset($input['portal_mode']) && ($input['portal_mode'] == 'domain' || $input['portal_mode'] == 'iframe')) {
$rules['portal_domain'] = 'sometimes|url';
} else {
$rules['subdomain'] = 'nullable|alpha_num';
if(Ninja::isHosted()){
$rules['subdomain'] = ['nullable|alpha_num', new ValidSubdomain($this->all())];
}
else
$rules['subdomain'] = 'nullable|alpha_num';
}
// if($this->company->account->isPaidHostedClient()) {

View File

@ -0,0 +1,50 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\ValidationRules\Company;
use App\Libraries\MultiDB;
use Illuminate\Contracts\Validation\Rule;
/**
* Class ValidCompanyQuantity.
*/
class ValidSubdomain implements Rule
{
/**
* @param string $attribute
* @param mixed $value
* @return bool
*/
private $input;
public function __construct($input)
{
$this->input = $input;
}
public function passes($attribute, $value)
{
if(empty($input['subdomain']))
return true;
return MultiDB::checkDomainAvailable($input['subdomain']);
}
/**
* @return string
*/
public function message()
{
return ctrans('texts.subdomain_taken');
}
}

View File

@ -17,6 +17,7 @@ use App\Exceptions\MigrationValidatorFailed;
use App\Exceptions\ProcessingMigrationArchiveFailed;
use App\Exceptions\ResourceDependencyMissing;
use App\Exceptions\ResourceNotAvailableForMigration;
use App\Factory\ClientContactFactory;
use App\Factory\ClientFactory;
use App\Factory\CompanyLedgerFactory;
use App\Factory\CreditFactory;
@ -1641,9 +1642,60 @@ class Import implements ShouldQueue
return $response->getBody();
}
private function buildNewUserPlan() :Client
{
$local_company = Company::find($this->company->id);
$owner = $local_company->owner();
$ninja_company = Company::on('db-ninja-01')->find(config('ninja.ninja_default_company_id'));
/* If we already have a record of this user - move along. */
if($client_contact = ClientContact::on('db-ninja-01')->where(['email' => $owner->email, 'company_id' => $ninja_company->id)->exists())
return $client_contact->client;
$ninja_client = ClientFactory::create($ninja_company->id, $ninja_company->owner()->id);
$ninja_client->setConnection('db-ninja-01');
$ninja_client->name = $owner->present()->name();
$ninja_client->address1 = $local_company->settings->address1;
$ninja_client->address2 = $local_company->settings->address2;
$ninja_client->city = $local_company->settings->city;
$ninja_client->postal_code = $local_company->settings->postal_code;
$ninja_client->state = $local_company->settings->state;
$ninja_client->country_id = $local_company->settings->country_id;
$ninja_client->save();
$ninja_client_contact = ClientContactFactory::create($ninja_company->id, $ninja_company->owner()->id);
$ninja_client_contact->setConnection('db-ninja-01');
$ninja_client_contact->first_name = $owner->first_name;
$ninja_client_contact->last_name = $owner->last_name;
$ninja_client_contact->client_id = $ninja_client->id;
$ninja_client_contact->email = $owner->email;
$ninja_client_contact->phone = $owner->phone;
$ninja_client_contact->save();
}
private function processNinjaTokens(array $data)
{
if(count($data) == 0)
$ninja_client = $this->buildNewUserPlan();
foreach($data as $token)
{
//get invoiceninja company_id
$ninja_company = Company::on('db-ninja-01')->where('id', config('ninja.ninja_default_company_id'))->first();
$token['company_id'] = $ninja_client->company_id;
$token['client_id'] = $ninja_client->id;
$token['user_id'] = $ninja_client->user_id;
$token['company_gateway_id'] = config('ninja.ninja_default_company_gateway_id')
//todo
$cgt = ClientGatewayToken::Create($token);
}
}
/* In V4 we use negative invoices (credits) and add then into the client balance. In V5, these sit off ledger and are applied later.

View File

@ -56,6 +56,7 @@ class MultiDB
return Company::whereSubdomain($subdomain)->get()->count() == 0;
}
//multi-db active
foreach (self::$dbs as $db) {
if (Company::on($db)->whereSubdomain($subdomain)->get()->count() >= 1) {
@ -63,7 +64,7 @@ class MultiDB
}
}
self::setDefaultDatabase();
//self::setDefaultDatabase();
return true;
}

View File

@ -150,6 +150,8 @@ return [
'ninja_stripe_key' => env('NINJA_STRIPE_KEY', null),
'ninja_stripe_publishable_key' => env('NINJA_PUBLISHABLE_KEY', null),
'ninja_stripe_client_id' => env('NINJA_STRIPE_CLIENT_ID', null),
'ninja_default_company_id' => env('NINJA_COMPANY_ID', null),
'ninja_default_company_gateway_id' => env('NINJA_COMPANY_GATEWAY_ID', null),
'pdf_generator' => env('PDF_GENERATOR', false),
'internal_queue_enabled' => env('INTERNAL_QUEUE_ENABLED', true),
];