diff --git a/app/Http/Controllers/ClientPortalController.php b/app/Http/Controllers/ClientPortalController.php index 773b898e6f..316508d6a2 100644 --- a/app/Http/Controllers/ClientPortalController.php +++ b/app/Http/Controllers/ClientPortalController.php @@ -17,6 +17,7 @@ use App\Ninja\Repositories\InvoiceRepository; use App\Ninja\Repositories\PaymentRepository; use App\Ninja\Repositories\TaskRepository; use App\Services\PaymentService; +use App\Jobs\Client\GenerateStatementData; use Auth; use Barracuda\ArchiveStream\ZipArchive; use Cache; @@ -1025,7 +1026,6 @@ class ClientPortalController extends BaseController $statusId = request()->status_id; $startDate = request()->start_date; $endDate = request()->end_date; - $account = auth()->user()->account; if (! $startDate) { $startDate = Utils::today(false)->modify('-6 month')->format('Y-m-d'); @@ -1033,7 +1033,7 @@ class ClientPortalController extends BaseController } if (request()->json) { - return dispatch(new GenerateStatementData($client, request()->all())); + return dispatch(new GenerateStatementData($client, request()->all(), $contact)); } $data = [ diff --git a/app/Jobs/Client/GenerateStatementData.php b/app/Jobs/Client/GenerateStatementData.php index bb2080dc97..79ceb7bedc 100644 --- a/app/Jobs/Client/GenerateStatementData.php +++ b/app/Jobs/Client/GenerateStatementData.php @@ -9,10 +9,11 @@ use App\Models\Eloquent; class GenerateStatementData { - public function __construct($client, $options) + public function __construct($client, $options, $contact = false) { $this->client = $client; $this->options = $options; + $this->contact = $contact; } /** @@ -23,9 +24,11 @@ class GenerateStatementData public function handle() { $client = $this->client; + $client->load('contacts'); $account = $client->account; - - $invoice = $account->createInvoice(ENTITY_INVOICE); + + $invoice = new Invoice(); + $invoice->account = $account; $invoice->client = $client; $invoice->date_format = $account->date_format ? $account->date_format->format_moment : 'MMM D, YYYY'; @@ -42,8 +45,7 @@ class GenerateStatementData { $statusId = intval($this->options['status_id']); - $invoices = Invoice::scope() - ->with(['client']) + $invoices = Invoice::with(['client']) ->invoices() ->whereClientId($this->client->id) ->whereIsPublic(true) @@ -61,6 +63,12 @@ class GenerateStatementData ->where('invoice_date', '<=', $this->options['end_date']); } + if ($this->contact) { + $invoices->whereHas('invitations', function ($query) { + $query->where('contact_id', $this->contact->id); + }); + } + $invoices = $invoices->get(); $data = collect(); @@ -87,8 +95,7 @@ class GenerateStatementData private function getPayments() { - $payments = Payment::scope() - ->with('invoice', 'payment_type') + $payments = Payment::with('invoice', 'payment_type') ->withArchived() ->whereClientId($this->client->id) ->where('payment_date', '>=', $this->options['start_date']) diff --git a/app/Models/EntityModel.php b/app/Models/EntityModel.php index cc08b33ef7..7a37b2c195 100644 --- a/app/Models/EntityModel.php +++ b/app/Models/EntityModel.php @@ -112,7 +112,7 @@ class EntityModel extends Eloquent return $className::scope($publicId)->withTrashed()->value('id'); } else { return $className::scope($publicId)->value('id'); - } + } } /** diff --git a/resources/views/clients/statement.blade.php b/resources/views/clients/statement.blade.php index 797c5389b0..e96f6bde20 100644 --- a/resources/views/clients/statement.blade.php +++ b/resources/views/clients/statement.blade.php @@ -7,14 +7,14 @@ @include('money_script') - @foreach (Auth::user()->account->getFontFolders() as $font) + @foreach ($account->getFontFolders() as $font) @endforeach