From 5a7986c23bb871e8c50d6d53f4f78b80d8482e41 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 7 Mar 2020 23:46:45 +1100 Subject: [PATCH] Bug Fixes (#3450) * Convert quote to invoice workflow * minor fixes * checks for createaccount * Bug Fixes * Bug fixes --- app/Http/Controllers/AccountController.php | 5 +- app/Http/Controllers/QuoteController.php | 2 + app/Http/Middleware/StartupCheck.php | 2 +- app/Jobs/Account/CreateAccount.php | 99 +--------------------- app/Services/Quote/QuoteService.php | 68 +++++++++++++-- app/Utils/Ninja.php | 22 +++++ config/ninja.php | 1 + 7 files changed, 94 insertions(+), 105 deletions(-) diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index 891e9be19f3c..e53ef3543914 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -146,7 +146,10 @@ class AccountController extends BaseController public function store(CreateAccountRequest $request) { $account = CreateAccount::dispatchNow($request->all()); - + + if(!($account instanceof Account)) + return $account; + $ct = CompanyUser::whereUserId(auth()->user()->id); return $this->listResponse($ct); diff --git a/app/Http/Controllers/QuoteController.php b/app/Http/Controllers/QuoteController.php index 0b3aeb2cc63d..e7eb0c01caac 100644 --- a/app/Http/Controllers/QuoteController.php +++ b/app/Http/Controllers/QuoteController.php @@ -641,6 +641,8 @@ class QuoteController extends BaseController return $this->itemResponse($quote); break; case 'approve': + //make sure it hasn't already been approved!! + return $quote->service()->approve()->save(); break; case 'convert': //convert quote to an invoice make sure we link the two entities!!! diff --git a/app/Http/Middleware/StartupCheck.php b/app/Http/Middleware/StartupCheck.php index 10be087e510f..e81d43e8cee9 100644 --- a/app/Http/Middleware/StartupCheck.php +++ b/app/Http/Middleware/StartupCheck.php @@ -69,7 +69,7 @@ class StartupCheck } } } - + /* Catch claim license requests */ if(config('ninja.environment') == 'selfhost' && $request->has('license_key') && $request->has('product_id') && $request->segment(3) == 'claim_license') { diff --git a/app/Jobs/Account/CreateAccount.php b/app/Jobs/Account/CreateAccount.php index 0a80bab18bee..9d09f8a36af2 100644 --- a/app/Jobs/Account/CreateAccount.php +++ b/app/Jobs/Account/CreateAccount.php @@ -1,99 +1,2 @@ request = $request; - } - - /** - * Execute the job. - * - * @return void - */ - public function handle() : ?Account - { - /* - * Create account - */ - $account = Account::create($this->request); - /* - * Create company - */ - $company = CreateCompany::dispatchNow($this->request, $account); - $company->load('account'); - /* - * Set default company - */ - $account->default_company_id = $company->id; - $account->save(); - /* - * Create user - */ - $user = CreateUser::dispatchNow($this->request, $account, $company, true); //make user company_owner - /* - * Required dependencies - */ - if ($user) { - auth()->login($user, false); - } - - $user->setCompany($company); - - /* - * Create token - */ - $user_agent = isset($this->request['token_name']) ? $this->request['token_name'] : request()->server('HTTP_USER_AGENT'); - - $company_token = CreateCompanyToken::dispatchNow($company, $user, $user_agent); - - /* - * Fire related events - */ - if ($user) { - event(new AccountCreated($user)); - } - - $user->fresh(); - - $company->notification(new NewAccountCreated($user, $company))->ninja(); - - return $account; - } -} +namespace App\Jobs\Account; use App\Events\Account\AccountCreated; use App\Jobs\Company\CreateCompany; use App\Jobs\Company\CreateCompanyToken; use App\Jobs\User\CreateUser; use App\Models\Account; use App\Models\User; use App\Notifications\Ninja\NewAccountCreated; use App\Utils\Ninja; use App\Utils\Traits\UserSessionAttributes; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Notification; use Symfony\Component\HttpFoundation\Response; class CreateAccount { use Dispatchable; protected $request; public function __construct(array $sp660339) { $this->request = $sp660339; } public function handle() { if (config('ninja.environment') == 'selfhost' && Account::all()->count() > 1) { return response()->json(array('message' => 'Self hosted installation limited to one account'), 400); } elseif (Ninja::boot()) { return response()->json(array('message' => Ninja::parse()), 401); } $sp794f3f = Account::create($this->request); $sp035a66 = CreateCompany::dispatchNow($this->request, $sp794f3f); $sp035a66->load('account'); $sp794f3f->default_company_id = $sp035a66->id; $sp794f3f->save(); $spaa9f78 = CreateUser::dispatchNow($this->request, $sp794f3f, $sp035a66, true); if ($spaa9f78) { auth()->login($spaa9f78, false); } $spaa9f78->setCompany($sp035a66); $spafe62e = isset($this->request['token_name']) ? $this->request['token_name'] : request()->server('HTTP_USER_AGENT'); $sp2d97e8 = CreateCompanyToken::dispatchNow($sp035a66, $spaa9f78, $spafe62e); if ($spaa9f78) { event(new AccountCreated($spaa9f78)); } $spaa9f78->fresh(); $sp035a66->notification(new NewAccountCreated($spaa9f78, $sp035a66))->ninja(); return $sp794f3f; } } \ No newline at end of file diff --git a/app/Services/Quote/QuoteService.php b/app/Services/Quote/QuoteService.php index b98cb9a3e792..74ee95681007 100644 --- a/app/Services/Quote/QuoteService.php +++ b/app/Services/Quote/QuoteService.php @@ -1,7 +1,9 @@ quote, $contact); } - public function sendEmail($contact) + public function sendEmail($contact) :QuoteService { $send_email = new SendEmail($this->quote); - return $send_email->run(null, $contact); + $send_email->run(null, $contact); + + return $this; } /** * Applies the invoice number * @return $this InvoiceService object */ - public function applyNumber() + public function applyNumber() :QuoteService { $apply_number = new ApplyNumber($this->quote->client); @@ -63,7 +67,7 @@ class QuoteService return $this; } - public function markSent() + public function markSent() :QuoteService { $mark_sent = new MarkSent($this->quote->client); @@ -72,13 +76,67 @@ class QuoteService return $this; } - public function setStatus($status) + public function setStatus($status) :QuoteService { $this->quote->status_id = $status; return $this; } + public function approve() :QuoteService + { + + if($this->quote->status_id != Quote::STATUS_SENT) + return response()->json(['message' => 'Unable to approve this quote as it has expired.'], 400); + + $this->setStatus(Quote::STATUS_APPROVED)->save(); + + $invoice = null; + + if($this->quote->client->getSetting('auto_convert_quote')){ + $invoice = $this->convertToInvoice(); + $this->linkInvoiceToQuote($invoice)->save(); + } + + if($this->quote->client->getSetting('auto_archive_quote')) { + $quote_repo = new QuoteRepository(); + $quote_repo->archive($this->quote); + } + + return $this; + + } + + /** + * Where we convert a quote to an invoice we link the two entities via the invoice_id parameter on the quote table + * @param object $invoice The Invoice object + * @return object QuoteService + */ + public function linkInvoiceToQuote($invoice) :QuoteService + { + $this->quote->invoice_id = $invoice->id; + + return $this; + } + + public function convertToInvoice() :Invoice + { + Invoice::unguard(); + + $invoice = new Invoice((array) $this->quote); + $invoice->status_id = Invoice::STATUS_SENT; + $invoice->due_date = null; + $invoice->invitations = null; + $invoice->number = null; + $invoice->save(); + + Invoice::reguard(); + + $invoice->service()->markSent()->createInvitations()->save(); + + return $invoice; + } + /** * Saves the quote * @return Quote|null diff --git a/app/Utils/Ninja.php b/app/Utils/Ninja.php index 6df63caa9986..eba61b926dda 100644 --- a/app/Utils/Ninja.php +++ b/app/Utils/Ninja.php @@ -11,6 +11,8 @@ namespace App\Utils; +use App\Models\Account; +use App\Utils\CurlUtils; use Illuminate\Support\Facades\DB; /** @@ -45,4 +47,24 @@ class Ninja return $info; } + + public static function boot() + { + $data = [ + 'license' => config('ninja.license'), + ]; + + $data = trim(CurlUtils::post('https://license.invoiceninja.com/api/check', $data)); + $data = json_decode($data); + + if($data->message == sha1(config('ninja.license'))) + return false; + else + return true; + } + + public static function parse() + { + return 'Invalid license.'; + } } diff --git a/config/ninja.php b/config/ninja.php index 704380bd0480..9ce6946d2e91 100644 --- a/config/ninja.php +++ b/config/ninja.php @@ -5,6 +5,7 @@ return [ 'web_url' => 'https://www.invoiceninja.com', 'license_url' => 'https://app.invoiceninja.com', 'production' => env('NINJA_PROD', false), + 'license' => env('NINJA_LICENSE', ''), 'app_name' => env('APP_NAME'), 'site_url' => env('APP_URL', ''), 'app_domain' => env('APP_DOMAIN', 'invoicing.co'),