diff --git a/app/Jobs/Payment/EmailPayment.php b/app/Jobs/Payment/EmailPayment.php index 4ef30426d2..ff01b4e9e8 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; @@ -72,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(); diff --git a/app/Services/Invoice/MarkSent.php b/app/Services/Invoice/MarkSent.php index 557fb37ec5..54ed2a1410 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/tests/Feature/PaymentTest.php b/tests/Feature/PaymentTest.php index 04b6ef1f36..f1e5c17c59 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;