diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index 97b442b5b207..4189e0d8ca60 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -447,8 +447,8 @@ class CheckData extends Command payments.id = paymentables.payment_id WHERE paymentable_type = 'App\\Models\\Credit' AND paymentables.deleted_at is NULL - AND payments.client_id = 85; - ") ); + AND payments.client_id = ?; + "), [$client->id] ); return $results; } diff --git a/app/Console/Commands/ImportMigrations.php b/app/Console/Commands/ImportMigrations.php index a589634203ab..c09058cda827 100644 --- a/app/Console/Commands/ImportMigrations.php +++ b/app/Console/Commands/ImportMigrations.php @@ -127,7 +127,7 @@ class ImportMigrations extends Command $user = User::factory()->create([ 'account_id' => $account->id, 'email' => Str::random(10) . "@example.com", - 'confirmation_code' => $this->createDbHash(config('database.default')), + 'confirmation_code' => $this->createDbHash($company->db), ]); CompanyToken::unguard(); diff --git a/app/Http/Controllers/Auth/ContactRegisterController.php b/app/Http/Controllers/Auth/ContactRegisterController.php index 51bc35739ffe..6ef9b17ee108 100644 --- a/app/Http/Controllers/Auth/ContactRegisterController.php +++ b/app/Http/Controllers/Auth/ContactRegisterController.php @@ -65,7 +65,9 @@ class ContactRegisterController extends Controller $client_contact->client_id = $client->id; $client_contact->is_primary = true; - $client_contact->password = Hash::make($data['password']); + + if(array_key_exists('password', $data)) + $client_contact->password = Hash::make($data['password']); $client_contact->save(); diff --git a/app/Http/Controllers/BaseController.php b/app/Http/Controllers/BaseController.php index 0b63b60801a6..3c17614da0a8 100644 --- a/app/Http/Controllers/BaseController.php +++ b/app/Http/Controllers/BaseController.php @@ -257,8 +257,8 @@ class BaseController extends Controller 'company.groups' => function ($query) use ($updated_at, $user) { $query->where('updated_at', '>=', $updated_at)->with('documents'); - if(!$user->isAdmin()) - $query->where('group_settings.user_id', $user->id); + // if(!$user->isAdmin()) + // $query->where('group_settings.user_id', $user->id); }, 'company.invoices'=> function ($query) use ($updated_at, $user) { $query->where('updated_at', '>=', $updated_at)->with('invitations', 'documents'); @@ -499,8 +499,8 @@ class BaseController extends Controller 'company.groups' => function ($query) use ($created_at, $user) { $query->where('created_at', '>=', $created_at)->with('documents'); - if(!$user->isAdmin()) - $query->where('group_settings.user_id', $user->id); + // if(!$user->isAdmin()) + // $query->where('group_settings.user_id', $user->id); }, 'company.invoices'=> function ($query) use ($created_at, $user) { $query->where('created_at', '>=', $created_at)->with('invitations', 'documents'); diff --git a/app/Jobs/Company/CompanyImport.php b/app/Jobs/Company/CompanyImport.php index 0001deac7888..33c11acaa65b 100644 --- a/app/Jobs/Company/CompanyImport.php +++ b/app/Jobs/Company/CompanyImport.php @@ -1024,7 +1024,7 @@ class CompanyImport implements ShouldQueue foreach((object)$this->getObject("users") as $user) { - if(User::where('email', $user->email)->where('account_id', '!=', $this->account->id)->exists()) + if(User::withTrashed()->where('email', $user->email)->where('account_id', '!=', $this->account->id)->exists()) throw new ImportCompanyFailed("{$user->email} is already in the system attached to a different account"); $user_array = (array)$user; @@ -1037,7 +1037,7 @@ class CompanyImport implements ShouldQueue $new_user = User::withTrashed()->firstOrNew( ['email' => $user->email], $user_array, - )->restore(); + ); $new_user->account_id = $this->account->id; $new_user->save(['timestamps' => false]); @@ -1067,7 +1067,7 @@ class CompanyImport implements ShouldQueue $new_cu = CompanyUser::withTrashed()->firstOrNew( ['user_id' => $user_id, 'company_id' => $this->company->id], $cu_array, - )->restore(); + ); $new_cu->account_id = $this->account->id; $new_cu->save(['timestamps' => false]); @@ -1078,8 +1078,6 @@ class CompanyImport implements ShouldQueue } - - private function transformDocumentId($id, $type) { switch ($type) { diff --git a/app/Jobs/RecurringInvoice/SendRecurring.php b/app/Jobs/RecurringInvoice/SendRecurring.php index 464849ef70d8..4cb5ce6395d4 100644 --- a/app/Jobs/RecurringInvoice/SendRecurring.php +++ b/app/Jobs/RecurringInvoice/SendRecurring.php @@ -171,7 +171,7 @@ class SendRecurring implements ShouldQueue $this->recurring_invoice->invitations->each(function ($recurring_invitation) use($invoice){ $ii = InvoiceInvitationFactory::create($invoice->company_id, $invoice->user_id); - $ii->key = $this->createDbHash(config('database.default')); + $ii->key = $this->createDbHash($invoice->company->db); $ii->invoice_id = $invoice->id; $ii->client_contact_id = $recurring_invitation->client_contact_id; $ii->save(); diff --git a/app/Jobs/User/CreateUser.php b/app/Jobs/User/CreateUser.php index ceb9a08215ec..495f3fecd78b 100644 --- a/app/Jobs/User/CreateUser.php +++ b/app/Jobs/User/CreateUser.php @@ -60,7 +60,7 @@ class CreateUser $user->account_id = $this->account->id; $user->password = $this->request['password'] ? bcrypt($this->request['password']) : ''; $user->accepted_terms_version = config('ninja.terms_version'); - $user->confirmation_code = $this->createDbHash(config('database.default')); + $user->confirmation_code = $this->createDbHash($this->company->db); $user->fill($this->request); $user->email = $this->request['email']; //todo need to remove this in production $user->last_login = now(); diff --git a/app/Listeners/Quote/CreateQuoteInvitation.php b/app/Listeners/Quote/CreateQuoteInvitation.php index fa71ba6440a2..9c5f08a25430 100644 --- a/app/Listeners/Quote/CreateQuoteInvitation.php +++ b/app/Listeners/Quote/CreateQuoteInvitation.php @@ -43,7 +43,7 @@ class CreateQuoteInvitation implements ShouldQueue if (! $invitation && $contact->send_credit) { $ii = QuoteInvitationFactory::create($quote->company_id, $quote->user_id); - $ii->key = $this->createDbHash(config('database.default')); + $ii->key = $this->createDbHash($quote->company->db); $ii->quote_id = $quote->id; $ii->client_contact_id = $contact->id; $ii->save(); diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 9b8d2ca04a12..cfdd0151acfd 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -233,7 +233,6 @@ class BaseDriver extends AbstractPaymentDriver } - $payment = PaymentFactory::create($this->client->company->id, $this->client->user->id); $payment->client_id = $this->client->id; $payment->company_gateway_id = $this->company_gateway->id; @@ -386,6 +385,9 @@ class BaseDriver extends AbstractPaymentDriver } else $error = $e->getMessage(); + if(!$this->payment_hash) + throw new PaymentFailed($error, $e->getCode()); + $amount = array_sum(array_column($this->payment_hash->invoices(), 'amount')) + $this->payment_hash->fee_total; $this->sendFailureMail($error); diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index cf37b1d3f141..2eada6f2bd11 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -277,7 +277,7 @@ class BaseRepository $new_invitation = $invitation_factory_class::create($model->company_id, $model->user_id); $new_invitation->{$lcfirst_resource_id} = $model->id; $new_invitation->client_contact_id = $contact->id; - $new_invitation->key = $this->createDbHash(config('database.default')); + $new_invitation->key = $this->createDbHash($model->company->db); $new_invitation->save(); } diff --git a/app/Repositories/UserRepository.php b/app/Repositories/UserRepository.php index 85d9105b8b64..6e91d344cca7 100644 --- a/app/Repositories/UserRepository.php +++ b/app/Repositories/UserRepository.php @@ -70,7 +70,7 @@ class UserRepository extends BaseRepository } if (!$user->confirmation_code) { - $user->confirmation_code = $this->createDbHash(config('database.default')); + $user->confirmation_code = $this->createDbHash($company->db); } $user->account_id = $account->id; diff --git a/app/Services/Credit/CreateInvitations.php b/app/Services/Credit/CreateInvitations.php index 498ba051c99d..a4cca1d37b75 100644 --- a/app/Services/Credit/CreateInvitations.php +++ b/app/Services/Credit/CreateInvitations.php @@ -49,7 +49,7 @@ class CreateInvitations extends AbstractService if (! $invitation) { $ii = CreditInvitationFactory::create($this->credit->company_id, $this->credit->user_id); - $ii->key = $this->createDbHash(config('database.default')); + $ii->key = $this->createDbHash($this->credit->company->db); $ii->credit_id = $this->credit->id; $ii->client_contact_id = $contact->id; $ii->save(); diff --git a/app/Services/Invoice/CreateInvitations.php b/app/Services/Invoice/CreateInvitations.php index 2f659f0d272d..7d7c6d7dad0a 100644 --- a/app/Services/Invoice/CreateInvitations.php +++ b/app/Services/Invoice/CreateInvitations.php @@ -51,7 +51,7 @@ class CreateInvitations extends AbstractService if (! $invitation && $contact->send_email) { $ii = InvoiceInvitationFactory::create($this->invoice->company_id, $this->invoice->user_id); - $ii->key = $this->createDbHash(config('database.default')); + $ii->key = $this->createDbHash($this->invoice->company->db); $ii->invoice_id = $this->invoice->id; $ii->client_contact_id = $contact->id; $ii->save(); @@ -65,7 +65,7 @@ class CreateInvitations extends AbstractService $contact = $this->createBlankContact(); $ii = InvoiceInvitationFactory::create($this->invoice->company_id, $this->invoice->user_id); - $ii->key = $this->createDbHash(config('database.default')); + $ii->key = $this->createDbHash($this->invoice->company->db); $ii->invoice_id = $this->invoice->id; $ii->client_contact_id = $contact->id; $ii->save(); diff --git a/app/Services/Invoice/HandleReversal.php b/app/Services/Invoice/HandleReversal.php index 81ef0090fb31..7854d9ea2c1d 100644 --- a/app/Services/Invoice/HandleReversal.php +++ b/app/Services/Invoice/HandleReversal.php @@ -56,8 +56,15 @@ class HandleReversal extends AbstractService $paymentables->each(function ($paymentable) use ($total_paid) { + //new concept - when reversing, we unwind the payments + $payment = Payment::find($paymentable->payment_id); + $reversable_amount = $paymentable->amount - $paymentable->refunded; $total_paid -= $reversable_amount; + + $payment->applied -= $reversable_amount; + $payment->save(); + $paymentable->amount = $paymentable->refunded; $paymentable->save(); @@ -67,45 +74,45 @@ class HandleReversal extends AbstractService $notes = 'Credit for reversal of '.$this->invoice->number; $credit = false; - if ($total_paid > 0) { + // if ($total_paid > 0) { - $credit = CreditFactory::create($this->invoice->company_id, $this->invoice->user_id); - $credit->client_id = $this->invoice->client_id; - $credit->invoice_id = $this->invoice->id; - $credit->date = now(); + // $credit = CreditFactory::create($this->invoice->company_id, $this->invoice->user_id); + // $credit->client_id = $this->invoice->client_id; + // $credit->invoice_id = $this->invoice->id; + // $credit->date = now(); - $item = InvoiceItemFactory::create(); - $item->quantity = 1; - $item->cost = (float) $total_paid; - $item->notes = $notes; + // $item = InvoiceItemFactory::create(); + // $item->quantity = 1; + // $item->cost = (float) $total_paid; + // $item->notes = $notes; - $line_items[] = $item; - $credit->line_items = $line_items; - $credit->save(); + // $line_items[] = $item; + // $credit->line_items = $line_items; + // $credit->save(); - $credit_calc = new InvoiceSum($credit); - $credit_calc->build(); + // $credit_calc = new InvoiceSum($credit); + // $credit_calc->build(); - $credit = $credit_calc->purgeTaxes()->getCredit(); - $credit->service()->markSent()->save(); - } + // $credit = $credit_calc->purgeTaxes()->getCredit(); + // $credit->service()->markSent()->save(); + // } /*If there is a payment linked, then the credit needs to be linked back to that payment in case of refund*/ if ($paymentables->count() > 0 && $credit) { - $payment = $paymentables->first()->payment; - $payment->credits()->save($credit); + // $payment = $paymentables->first()->payment; + // $payment->credits()->save($credit); - $paymentable_credit = $payment->credits() - ->wherePaymentableType(Credit::class) - ->wherePaymentableId($credit->id) - ->first(); + // $paymentable_credit = $payment->credits() + // ->wherePaymentableType(Credit::class) + // ->wherePaymentableId($credit->id) + // ->first(); - //harvest the credit record and add in the amount for the credit. - $paymentable_credit->pivot->amount = $total_paid; - $paymentable_credit->pivot->save(); + // //harvest the credit record and add in the amount for the credit. + // $paymentable_credit->pivot->amount = $total_paid; + // $paymentable_credit->pivot->save(); - $paymentable_credit->paid_to_date += $total_paid; - $paymentable_credit->save(); + // $paymentable_credit->paid_to_date += $total_paid; + // $paymentable_credit->save(); } /* Set invoice balance to 0 */ @@ -124,7 +131,7 @@ class HandleReversal extends AbstractService $this->invoice->client->service() ->updateBalance($balance_remaining * -1) - ->updatePaidToDate($total_paid * -1) + // ->updatePaidToDate($total_paid * -1) ->save(); event(new InvoiceWasReversed($this->invoice, $this->invoice->company, Ninja::eventVars())); diff --git a/app/Services/Payment/PaymentService.php b/app/Services/Payment/PaymentService.php index 39df23ef77ff..2412a4838907 100644 --- a/app/Services/Payment/PaymentService.php +++ b/app/Services/Payment/PaymentService.php @@ -112,7 +112,7 @@ class PaymentService /* Iterate through the invoices and apply credits to them */ collect($payment_hash->invoices())->each(function ($payable_invoice) use ($payment_hash) { - $invoice = Invoice::find($this->decodePrimaryKey($payable_invoice->invoice_id)); + $invoice = Invoice::withTrashed()->find($this->decodePrimaryKey($payable_invoice->invoice_id)); $amount = $payable_invoice->amount; diff --git a/app/Services/Quote/ConvertQuote.php b/app/Services/Quote/ConvertQuote.php index 12915ce54f24..77137f5c24a9 100644 --- a/app/Services/Quote/ConvertQuote.php +++ b/app/Services/Quote/ConvertQuote.php @@ -77,7 +77,7 @@ class ConvertQuote foreach($quote->invitations as $quote_invitation){ $ii = InvoiceInvitationFactory::create($invoice->company_id, $invoice->user_id); - $ii->key = $this->createDbHash(config('database.default')); + $ii->key = $this->createDbHash($invoice->company->db); $ii->client_contact_id = $quote_invitation->client_contact_id; $invites[] = $ii; diff --git a/app/Services/Quote/CreateInvitations.php b/app/Services/Quote/CreateInvitations.php index e5da296d758c..2ef9fbb320c4 100644 --- a/app/Services/Quote/CreateInvitations.php +++ b/app/Services/Quote/CreateInvitations.php @@ -50,7 +50,7 @@ class CreateInvitations if (! $invitation && $contact->send_email) { $ii = QuoteInvitationFactory::create($this->quote->company_id, $this->quote->user_id); - $ii->key = $this->createDbHash(config('database.default')); + $ii->key = $this->createDbHash($this->quote->company->db); $ii->quote_id = $this->quote->id; $ii->client_contact_id = $contact->id; $ii->save(); diff --git a/app/Services/Recurring/CreateRecurringInvitations.php b/app/Services/Recurring/CreateRecurringInvitations.php index ae2692214a6c..657bfa38c581 100644 --- a/app/Services/Recurring/CreateRecurringInvitations.php +++ b/app/Services/Recurring/CreateRecurringInvitations.php @@ -51,7 +51,7 @@ class CreateRecurringInvitations extends AbstractService if (! $invitation && $contact->send_email) { $ii = $this->invitation_factory::create($this->entity->company_id, $this->entity->user_id); - $ii->key = $this->createDbHash(config('database.default')); + $ii->key = $this->createDbHash($this->entity->company->db); $ii->{$this->entity_id_name} = $this->entity->id; $ii->client_contact_id = $contact->id; $ii->save(); diff --git a/routes/api.php b/routes/api.php index 78fa89b232cd..0e2f9d4760cf 100644 --- a/routes/api.php +++ b/routes/api.php @@ -216,7 +216,7 @@ Route::match(['get', 'post'], 'payment_notification_webhook/{company_key}/{compa ->middleware(['guest']) ->name('payment_notification_webhook'); -Route::post('api/v1/postmark_webhook', 'PostMarkController@webhook')->middleware(['throttle:5000,1']); +Route::post('api/v1/postmark_webhook', 'PostMarkController@webhook')->middleware(['throttle:10000,1']); Route::get('token_hash_router', 'OneTimeTokenController@router'); Route::get('webcron', 'WebCronController@index'); Route::post('api/v1/get_migration_account', 'HostedMigrationController@getAccount')->middleware('guest');