diff --git a/app/Http/Controllers/PaymentController.php b/app/Http/Controllers/PaymentController.php index e6bc57408d6d..ea9838358515 100644 --- a/app/Http/Controllers/PaymentController.php +++ b/app/Http/Controllers/PaymentController.php @@ -615,7 +615,7 @@ class PaymentController extends BaseController } break; case 'email_receipt': - $this->payment->service()->sendEmail(); + $payment->service()->sendEmail(); if (! $bulk) { return $this->itemResponse($payment); diff --git a/app/Http/Requests/TaskScheduler/ShowSchedulerRequest.php b/app/Http/Requests/TaskScheduler/ShowSchedulerRequest.php index a0b4777df8f2..fddd848e154f 100644 --- a/app/Http/Requests/TaskScheduler/ShowSchedulerRequest.php +++ b/app/Http/Requests/TaskScheduler/ShowSchedulerRequest.php @@ -22,6 +22,6 @@ class ShowSchedulerRequest extends Request */ public function authorize() : bool { - return auth()->user()->can('view', $this->scheduler); + return auth()->user()->can('view', $this->task_scheduler); } } diff --git a/app/Http/Requests/TaskScheduler/UpdateSchedulerRequest.php b/app/Http/Requests/TaskScheduler/UpdateSchedulerRequest.php index 0c019e25b0d7..4c5038813644 100644 --- a/app/Http/Requests/TaskScheduler/UpdateSchedulerRequest.php +++ b/app/Http/Requests/TaskScheduler/UpdateSchedulerRequest.php @@ -23,7 +23,7 @@ class UpdateSchedulerRequest extends Request */ public function authorize(): bool { - return auth()->user()->isAdmin(); + return auth()->user()->isAdmin() && $this->task_scheduler->company_id == auth()->user()->company()->id; } public function rules(): array diff --git a/app/Jobs/Payment/EmailPayment.php b/app/Jobs/Payment/EmailPayment.php index fab34abbc7b7..ff01b4e9e898 100644 --- a/app/Jobs/Payment/EmailPayment.php +++ b/app/Jobs/Payment/EmailPayment.php @@ -49,7 +49,7 @@ class EmailPayment implements ShouldQueue * @param $contact * @param $company */ - public function __construct(Payment $payment, Company $company, ClientContact $contact) + public function __construct(Payment $payment, Company $company, ?ClientContact $contact) { $this->payment = $payment; $this->contact = $contact; @@ -60,7 +60,6 @@ class EmailPayment implements ShouldQueue /** * Execute the job. * - * * @return void */ public function handle() @@ -73,6 +72,10 @@ class EmailPayment implements ShouldQueue MultiDB::setDb($this->company->db); $this->payment->load('invoices'); + + if(!$this->contact) + $this->contact = $this->payment->client->contacts()->first(); + $this->contact->load('client'); $email_builder = (new PaymentEmailEngine($this->payment, $this->contact))->build(); @@ -90,7 +93,7 @@ class EmailPayment implements ShouldQueue $nmo->company = $this->company; $nmo->entity = $this->payment; - NinjaMailerJob::dispatch($nmo); + (new NinjaMailerJob($nmo))->handle(); event(new PaymentWasEmailed($this->payment, $this->payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); } diff --git a/app/Jobs/Payment/EmailRefundPayment.php b/app/Jobs/Payment/EmailRefundPayment.php index a3ef64cdbb41..ef79ac773b0d 100644 --- a/app/Jobs/Payment/EmailRefundPayment.php +++ b/app/Jobs/Payment/EmailRefundPayment.php @@ -99,7 +99,7 @@ class EmailRefundPayment implements ShouldQueue $nmo->company = $this->company; $nmo->entity = $this->payment; - NinjaMailerJob::dispatch($nmo); + (new NinjaMailerJob($nmo))->handle(); event(new PaymentWasEmailed($this->payment, $this->payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); } diff --git a/app/Mail/Engine/PaymentEmailEngine.php b/app/Mail/Engine/PaymentEmailEngine.php index eb2f10735d4d..353dfbfb810d 100644 --- a/app/Mail/Engine/PaymentEmailEngine.php +++ b/app/Mail/Engine/PaymentEmailEngine.php @@ -389,6 +389,7 @@ class PaymentEmailEngine extends BaseEmailEngine private function buildViewButton(string $link, string $text): string { return ' +
+
'; - + return ' diff --git a/app/Services/Invoice/ApplyPaymentAmount.php b/app/Services/Invoice/ApplyPaymentAmount.php index 690d54f86146..59f7269d9a57 100644 --- a/app/Services/Invoice/ApplyPaymentAmount.php +++ b/app/Services/Invoice/ApplyPaymentAmount.php @@ -114,7 +114,7 @@ class ApplyPaymentAmount extends AbstractService $exchange_rate = new CurrencyApi(); $payment->exchange_rate = $exchange_rate->exchangeRate($client_currency, $company_currency, Carbon::parse($payment->date)); - //$payment->exchange_currency_id = $client_currency; // 23/06/2021 + $payment->exchange_currency_id = $company_currency; $payment->saveQuietly(); diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index 34919ad37ad8..6c4fe4882003 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -103,7 +103,6 @@ class InvoiceService * @param Payment $payment The Payment * @param float $payment_amount The Payment amount * @return InvoiceService Parent class object - * @deprecated 24-11-2022 - cannot find any references to this method anywhere */ public function applyPayment(Payment $payment, float $payment_amount) { diff --git a/app/Services/Invoice/MarkSent.php b/app/Services/Invoice/MarkSent.php index 557fb37ec55e..54ed2a141045 100644 --- a/app/Services/Invoice/MarkSent.php +++ b/app/Services/Invoice/MarkSent.php @@ -20,16 +20,8 @@ use App\Utils\Ninja; class MarkSent extends AbstractService { - public $client; - - public $invoice; - - public function __construct(Client $client, Invoice $invoice) - { - $this->client = $client; - - $this->invoice = $invoice; - } + public function __construct(public Client $client, public Invoice $invoice) + {} public function run($fire_webhook = false) { diff --git a/app/Services/Ledger/LedgerService.php b/app/Services/Ledger/LedgerService.php index 9d9935fc5e02..d03945ad21db 100644 --- a/app/Services/Ledger/LedgerService.php +++ b/app/Services/Ledger/LedgerService.php @@ -35,7 +35,7 @@ class LedgerService $this->entity->company_ledger()->save($company_ledger); - ClientLedgerBalanceUpdate::dispatch($this->entity->company, $this->entity->client)->delay(now()->addSeconds(rand(30, 300))); + ClientLedgerBalanceUpdate::dispatch($this->entity->company, $this->entity->client)->delay(rand(3, 13)); return $this; } @@ -51,7 +51,7 @@ class LedgerService $this->entity->company_ledger()->save($company_ledger); - ClientLedgerBalanceUpdate::dispatch($this->entity->company, $this->entity->client)->delay(now()->addSeconds(rand(30, 300))); + ClientLedgerBalanceUpdate::dispatch($this->entity->company, $this->entity->client)->delay(rand(3, 13)); return $this; } @@ -67,7 +67,7 @@ class LedgerService $this->entity->company_ledger()->save($company_ledger); - ClientLedgerBalanceUpdate::dispatch($this->entity->company, $this->entity->client)->delay(now()->addSeconds(rand(30, 300))); + ClientLedgerBalanceUpdate::dispatch($this->entity->company, $this->entity->client)->delay(rand(3, 13)); return $this; } diff --git a/app/Services/Payment/SendEmail.php b/app/Services/Payment/SendEmail.php index 770b54de7248..d967fd9ed34d 100644 --- a/app/Services/Payment/SendEmail.php +++ b/app/Services/Payment/SendEmail.php @@ -11,20 +11,15 @@ namespace App\Services\Payment; +use App\Models\Payment; +use App\Models\ClientContact; use App\Jobs\Payment\EmailPayment; class SendEmail { - public $payment; - public $contact; - - public function __construct($payment, $contact) - { - $this->payment = $payment; - - $this->contact = $contact; - } + public function __construct(public Payment $payment, public ?ClientContact $contact) + {} /** * Builds the correct template to send. @@ -34,18 +29,16 @@ class SendEmail { $this->payment->load('company', 'client.contacts', 'invoices'); - $contact = $this->payment->client->contacts()->first(); + if(!$this->contact) + $this->contact = $this->payment->client->contacts()->first(); - // if ($contact?->email) - // EmailPayment::dispatch($this->payment, $this->payment->company, $contact)->delay(now()->addSeconds(2)); - - - $this->payment->invoices->sortByDesc('id')->first(function ($invoice) { - $invoice->invitations->each(function ($invitation) { - if (!$invitation->contact->trashed() && $invitation->contact->email) { - EmailPayment::dispatch($this->payment, $this->payment->company, $invitation->contact)->delay(now()->addSeconds(2)); - } - }); - }); + // $this->payment->invoices->sortByDesc('id')->first(function ($invoice) { + // $invoice->invitations->each(function ($invitation) { + // if (!$invitation->contact->trashed() && $invitation->contact->email) { + EmailPayment::dispatch($this->payment, $this->payment->company, $this->contact); + // } + // }); + // }); + } } diff --git a/app/Utils/HtmlEngine.php b/app/Utils/HtmlEngine.php index a3497072f2fd..5f1c3281d4a0 100644 --- a/app/Utils/HtmlEngine.php +++ b/app/Utils/HtmlEngine.php @@ -985,6 +985,7 @@ html { private function buildViewButton(string $link, string $text): string { return ' +
+
'; diff --git a/app/Utils/Traits/Uploadable.php b/app/Utils/Traits/Uploadable.php index 69be5021f470..2e3987a329fe 100644 --- a/app/Utils/Traits/Uploadable.php +++ b/app/Utils/Traits/Uploadable.php @@ -22,9 +22,7 @@ trait Uploadable { public function removeLogo($company) { - //if (Storage::disk(config('filesystems.default'))->exists($company->settings->company_logo)) { - (new UnlinkFile(config('filesystems.default'), $company->settings->company_logo))->handle(); - //} + (new UnlinkFile(config('filesystems.default'), $company?->settings?->company_logo))->handle(); } public function uploadLogo($file, $company, $entity) diff --git a/app/Utils/VendorHtmlEngine.php b/app/Utils/VendorHtmlEngine.php index 95112fc6bda1..cddb2af04463 100644 --- a/app/Utils/VendorHtmlEngine.php +++ b/app/Utils/VendorHtmlEngine.php @@ -808,6 +808,7 @@ html { private function buildViewButton(string $link, string $text): string { return ' +
+
'; - return '
diff --git a/tests/Feature/PaymentTest.php b/tests/Feature/PaymentTest.php index 04b6ef1f3667..f1e5c17c5972 100644 --- a/tests/Feature/PaymentTest.php +++ b/tests/Feature/PaymentTest.php @@ -230,8 +230,13 @@ class PaymentTest extends TestCase public function testStorePaymentWithClientId() { - $client = ClientFactory::create($this->company->id, $this->user->id); - $client->save(); + $client = Client::factory()->create(['company_id' =>$this->company->id, 'user_id' => $this->user->id]); + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $client->id, + 'company_id' => $this->company->id, + 'is_primary' => 1, + ]); $invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id $invoice->client_id = $client->id; @@ -289,8 +294,14 @@ class PaymentTest extends TestCase public function testStorePaymentWithNoInvoiecs() { - $client = ClientFactory::create($this->company->id, $this->user->id); - $client->save(); + $client = Client::factory()->create(['company_id' =>$this->company->id, 'user_id' => $this->user->id]); + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $client->id, + 'company_id' => $this->company->id, + 'is_primary' => 1, + ]); + $invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id $invoice->client_id = $client->id; @@ -336,16 +347,15 @@ class PaymentTest extends TestCase { $invoice = null; - $client = ClientFactory::create($this->company->id, $this->user->id); - $client->save(); - + $client = Client::factory()->create(['company_id' =>$this->company->id, 'user_id' => $this->user->id]); ClientContact::factory()->create([ 'user_id' => $this->user->id, 'client_id' => $client->id, - 'company_id' =>$this->company->id, - 'is_primary' => true, + 'company_id' => $this->company->id, + 'is_primary' => 1, ]); + $invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id $invoice->client_id = $client->id; @@ -626,8 +636,14 @@ class PaymentTest extends TestCase { $invoice = null; - $client = ClientFactory::create($this->company->id, $this->user->id); - $client->save(); + $client = Client::factory()->create(['company_id' =>$this->company->id, 'user_id' => $this->user->id]); + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $client->id, + 'company_id' => $this->company->id, + 'is_primary' => 1, + ]); + $invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id $invoice->client_id = $client->id; @@ -686,8 +702,14 @@ class PaymentTest extends TestCase { $invoice = null; - $client = ClientFactory::create($this->company->id, $this->user->id); - $client->save(); + $client = Client::factory()->create(['company_id' =>$this->company->id, 'user_id' => $this->user->id]); + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $client->id, + 'company_id' => $this->company->id, + 'is_primary' => 1, + ]); + $invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id $invoice->client_id = $client->id; @@ -739,8 +761,13 @@ class PaymentTest extends TestCase { $invoice = null; - $client = ClientFactory::create($this->company->id, $this->user->id); - $client->save(); + $client = Client::factory()->create(['company_id' =>$this->company->id, 'user_id' => $this->user->id]); + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $client->id, + 'company_id' => $this->company->id, + 'is_primary' => 1, + ]); $invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id $invoice->client_id = $client->id; @@ -799,8 +826,14 @@ class PaymentTest extends TestCase { $invoice = null; - $client = ClientFactory::create($this->company->id, $this->user->id); - $client->save(); + $client = Client::factory()->create(['company_id' =>$this->company->id, 'user_id' => $this->user->id]); + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $client->id, + 'company_id' => $this->company->id, + 'is_primary' => 1, + ]); + $invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id $invoice->client_id = $client->id; @@ -898,8 +931,13 @@ class PaymentTest extends TestCase { $invoice = null; - $client = ClientFactory::create($this->company->id, $this->user->id); - $client->save(); + $client = Client::factory()->create(['company_id' =>$this->company->id, 'user_id' => $this->user->id]); + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $client->id, + 'company_id' => $this->company->id, + 'is_primary' => 1, + ]); $invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id $invoice->client_id = $client->id; @@ -944,59 +982,18 @@ class PaymentTest extends TestCase $this->assertEquals($payment->amount, 20); $this->assertEquals($payment->applied, 10); - // $invoice = null; - // $invoice = InvoiceFactory::create($this->company->id, $this->user->id);//stub the company and user_id - // $invoice->client_id = $client->id; - - // $invoice->line_items = $this->buildLineItems(); - // $invoice->uses_inclusive_taxes = false; - - // $invoice->save(); - - // $invoice_calc = new InvoiceSum($invoice); - // $invoice_calc->build(); - - // $invoice = $invoice_calc->getInvoice(); - // $invoice->save(); - // $invoice->service()->markSent()->createInvitations()->save(); - - // $data = [ - // 'amount' => 20.0, - // 'client_id' => $this->encodePrimaryKey($client->id), - // 'invoices' => [ - // [ - // 'invoice_id' => $this->encodePrimaryKey($invoice->id), - // 'amount' => 10, - // ] - // ], - // 'date' => '2019/12/12', - // ]; - - // $response = false; - - // try { - // $response = $this->withHeaders([ - // 'X-API-SECRET' => config('ninja.api_secret'), - // 'X-API-TOKEN' => $this->token, - // ])->put('/api/v1/payments/'.$this->encodePrimaryKey($payment->id), $data); - // } catch (ValidationException $e) { - // $message = json_decode($e->validator->getMessageBag(), 1); - // \Log::error(print_r($e->validator->getMessageBag(), 1)); - - // $this->assertTrue(array_key_exists('invoices', $message)); - // } - - // $response->assertStatus(200); - - // $arr = $response->json(); - - // $this->assertEquals(20, $arr['data']['applied']); } public function testStorePaymentWithNoAmountField() { - $client = ClientFactory::create($this->company->id, $this->user->id); - $client->save(); + $client = Client::factory()->create(['company_id' =>$this->company->id, 'user_id' => $this->user->id]); + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $client->id, + 'company_id' => $this->company->id, + 'is_primary' => 1, + ]); + $invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id $invoice->client_id = $client->id; @@ -1055,8 +1052,13 @@ class PaymentTest extends TestCase public function testStorePaymentWithZeroAmountField() { - $client = ClientFactory::create($this->company->id, $this->user->id); - $client->save(); + $client = Client::factory()->create(['company_id' =>$this->company->id, 'user_id' => $this->user->id]); + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $client->id, + 'company_id' => $this->company->id, + 'is_primary' => 1, + ]); $invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id $invoice->client_id = $client->id; @@ -1109,11 +1111,22 @@ class PaymentTest extends TestCase public function testPaymentForInvoicesFromDifferentClients() { - $client1 = ClientFactory::create($this->company->id, $this->user->id); - $client1->save(); + $client1 = Client::factory()->create(['company_id' =>$this->company->id, 'user_id' => $this->user->id]); + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $client1->id, + 'company_id' => $this->company->id, + 'is_primary' => 1, + ]); - $client2 = ClientFactory::create($this->company->id, $this->user->id); - $client2->save(); + + $client2 = Client::factory()->create(['company_id' =>$this->company->id, 'user_id' => $this->user->id]); + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $client2->id, + 'company_id' => $this->company->id, + 'is_primary' => 1, + ]); $invoice1 = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id $invoice1->client_id = $client1->id; @@ -1175,8 +1188,13 @@ class PaymentTest extends TestCase public function testPaymentWithSameInvoiceMultipleTimes() { - $client1 = ClientFactory::create($this->company->id, $this->user->id); - $client1->save(); + $client1 = Client::factory()->create(['company_id' =>$this->company->id, 'user_id' => $this->user->id]); + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $client1->id, + 'company_id' => $this->company->id, + 'is_primary' => 1, + ]); $invoice1 = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id $invoice1->client_id = $client1->id; @@ -1227,8 +1245,13 @@ class PaymentTest extends TestCase public function testStorePaymentWithCredits() { - $client = ClientFactory::create($this->company->id, $this->user->id); - $client->save(); + $client = Client::factory()->create(['company_id' =>$this->company->id, 'user_id' => $this->user->id]); + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $client->id, + 'company_id' => $this->company->id, + 'is_primary' => 1, + ]); $invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id $invoice->client_id = $client->id; @@ -1310,7 +1333,14 @@ class PaymentTest extends TestCase $settings = ClientSettings::defaults(); $settings->currency_id = '2'; - $client = ClientFactory::create($this->company->id, $this->user->id); + $client = Client::factory()->create(['company_id' =>$this->company->id, 'user_id' => $this->user->id]); + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $client->id, + 'company_id' => $this->company->id, + 'is_primary' => 1, + ]); + $client->settings = $settings; $client->save(); @@ -1374,8 +1404,14 @@ class PaymentTest extends TestCase { $invoice = null; - $client = ClientFactory::create($this->company->id, $this->user->id); - $client->save(); + $client = Client::factory()->create(['company_id' =>$this->company->id, 'user_id' => $this->user->id]); + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $client->id, + 'company_id' => $this->company->id, + 'is_primary' => 1, + ]); + $invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id $invoice->client_id = $client->id; @@ -1453,8 +1489,14 @@ class PaymentTest extends TestCase { $invoice = null; - $client = ClientFactory::create($this->company->id, $this->user->id); - $client->save(); + $client = Client::factory()->create(['company_id' =>$this->company->id, 'user_id' => $this->user->id]); + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $client->id, + 'company_id' => $this->company->id, + 'is_primary' => 1, + ]); + $invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id $invoice->client_id = $client->id; diff --git a/tests/Feature/Scheduler/SchedulerTest.php b/tests/Feature/Scheduler/SchedulerTest.php index 055206f4c027..28101cebefcb 100644 --- a/tests/Feature/Scheduler/SchedulerTest.php +++ b/tests/Feature/Scheduler/SchedulerTest.php @@ -54,6 +54,22 @@ class SchedulerTest extends TestCase ); } + + + public function testSchedulerGet2() + { + + $scheduler = SchedulerFactory::create($this->company->id, $this->user->id); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/task_schedulers/'.$this->encodePrimaryKey($scheduler->id)); + + $response->assertStatus(200); + } + + public function testCustomDateRanges() { $data = [ @@ -619,21 +635,6 @@ class SchedulerTest extends TestCase } - // public function testSchedulerPut() - // { - // $data = [ - // 'description' => $this->faker->firstName(), - // ]; - - // $response = $this->withHeaders([ - // 'X-API-SECRET' => config('ninja.api_secret'), - // 'X-API-TOKEN' => $this->token, - // ])->put('/api/v1/task_schedulers/'.$this->encodePrimaryKey($this->task->id), $data); - - // $response->assertStatus(200); - // } - - // public function testSchedulerCantBeCreatedWithWrongData() // {