mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Merge pull request #6600 from beganovich/v5-659
Statements in client portal
This commit is contained in:
commit
a51436f224
59
app/Http/Controllers/ClientPortal/StatementController.php
Normal file
59
app/Http/Controllers/ClientPortal/StatementController.php
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoice Ninja (https://invoiceninja.com).
|
||||||
|
*
|
||||||
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
||||||
|
*
|
||||||
|
* @license https://www.elastic.co/licensing/elastic-license
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\ClientPortal;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Http\Requests\ClientPortal\Statements\ShowStatementRequest;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\View\View;
|
||||||
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||||
|
|
||||||
|
class StatementController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Show the statement in the client portal.
|
||||||
|
*
|
||||||
|
* @return View
|
||||||
|
*/
|
||||||
|
public function index(): View
|
||||||
|
{
|
||||||
|
return render('statement.index');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show the raw stream of the PDF.
|
||||||
|
*
|
||||||
|
* @param ShowStatementRequest $request
|
||||||
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|JsonResponse|\Illuminate\Http\Response|StreamedResponse
|
||||||
|
*/
|
||||||
|
public function raw(ShowStatementRequest $request)
|
||||||
|
{
|
||||||
|
$pdf = $request->client()->service()->statement(
|
||||||
|
$request->only(['start_date', 'end_date', 'show_payments_table', 'show_aging_table'])
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($pdf && $request->query('download')) {
|
||||||
|
return response()->streamDownload(function () use ($pdf) {
|
||||||
|
echo $pdf;
|
||||||
|
}, 'statement.pdf', ['Content-Type' => 'application/pdf']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($pdf) {
|
||||||
|
return response($pdf, 200)->withHeaders([
|
||||||
|
'Content-Type' => 'application/pdf',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json(['message' => 'Something went wrong. Please check logs.']);
|
||||||
|
}
|
||||||
|
}
|
@ -110,8 +110,11 @@ class ClientStatementController extends BaseController
|
|||||||
|
|
||||||
public function statement(CreateStatementRequest $request)
|
public function statement(CreateStatementRequest $request)
|
||||||
{
|
{
|
||||||
$pdf = $this->createStatement($request);
|
$pdf = $request->client()->service()->statement([
|
||||||
|
'start_date' => $request->start_date,
|
||||||
|
'end_date' => $request->end_date,
|
||||||
|
]);
|
||||||
|
|
||||||
if ($pdf) {
|
if ($pdf) {
|
||||||
return response()->streamDownload(function () use ($pdf) {
|
return response()->streamDownload(function () use ($pdf) {
|
||||||
echo $pdf;
|
echo $pdf;
|
||||||
@ -120,88 +123,4 @@ class ClientStatementController extends BaseController
|
|||||||
|
|
||||||
return response()->json(['message' => 'Something went wrong. Please check logs.']);
|
return response()->json(['message' => 'Something went wrong. Please check logs.']);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function createStatement(CreateStatementRequest $request): ?string
|
|
||||||
{
|
|
||||||
$invitation = false;
|
|
||||||
|
|
||||||
if ($request->getInvoices()->count() >= 1) {
|
|
||||||
$this->entity = $request->getInvoices()->first();
|
|
||||||
$invitation = $this->entity->invitations->first();
|
|
||||||
}
|
|
||||||
else if ($request->getPayments()->count() >= 1) {
|
|
||||||
$this->entity = $request->getPayments()->first()->invoices->first()->invitations->first();
|
|
||||||
$invitation = $this->entity->invitations->first();
|
|
||||||
}
|
|
||||||
|
|
||||||
$entity_design_id = 1;
|
|
||||||
|
|
||||||
$entity_design_id = $this->entity->design_id
|
|
||||||
? $this->entity->design_id
|
|
||||||
: $this->decodePrimaryKey($this->entity->client->getSetting('invoice_design_id'));
|
|
||||||
|
|
||||||
|
|
||||||
$design = Design::find($entity_design_id);
|
|
||||||
|
|
||||||
if (!$design) {
|
|
||||||
$design = Design::find($entity_design_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
$html = new HtmlEngine($invitation);
|
|
||||||
|
|
||||||
$options = [
|
|
||||||
'start_date' => $request->start_date,
|
|
||||||
'end_date' => $request->end_date,
|
|
||||||
'show_payments_table' => $request->show_payments_table,
|
|
||||||
'show_aging_table' => $request->show_aging_table,
|
|
||||||
];
|
|
||||||
|
|
||||||
if ($design->is_custom) {
|
|
||||||
$options['custom_partials'] = \json_decode(\json_encode($design->design), true);
|
|
||||||
$template = new PdfMakerDesign(PdfDesignModel::CUSTOM, $options);
|
|
||||||
} else {
|
|
||||||
$template = new PdfMakerDesign(strtolower($design->name), $options);
|
|
||||||
}
|
|
||||||
|
|
||||||
$variables = $html->generateLabelsAndValues();
|
|
||||||
|
|
||||||
$state = [
|
|
||||||
'template' => $template->elements([
|
|
||||||
'client' => $this->entity->client,
|
|
||||||
'entity' => $this->entity,
|
|
||||||
'pdf_variables' => (array)$this->entity->company->settings->pdf_variables,
|
|
||||||
'$product' => $design->design->product,
|
|
||||||
'variables' => $variables,
|
|
||||||
'invoices' => $request->getInvoices(),
|
|
||||||
'payments' => $request->getPayments(),
|
|
||||||
'aging' => $request->getAging(),
|
|
||||||
], \App\Services\PdfMaker\Design::STATEMENT),
|
|
||||||
'variables' => $variables,
|
|
||||||
'options' => [],
|
|
||||||
'process_markdown' => $this->entity->client->company->markdown_enabled,
|
|
||||||
];
|
|
||||||
|
|
||||||
$maker = new PdfMakerService($state);
|
|
||||||
|
|
||||||
$maker
|
|
||||||
->design($template)
|
|
||||||
->build();
|
|
||||||
|
|
||||||
$pdf = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
|
|
||||||
$pdf = (new Phantom)->convertHtmlToPdf($maker->getCompiledHTML(true));
|
|
||||||
}
|
|
||||||
else if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
|
|
||||||
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
|
|
||||||
} else {
|
|
||||||
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
|
|
||||||
}
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
nlog(print_r($e->getMessage(), 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
return $pdf;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
49
app/Http/Livewire/Statement.php
Normal file
49
app/Http/Livewire/Statement.php
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoice Ninja (https://invoiceninja.com).
|
||||||
|
*
|
||||||
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
||||||
|
*
|
||||||
|
* @license https://www.elastic.co/licensing/elastic-license
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Http\Livewire;
|
||||||
|
|
||||||
|
use Illuminate\View\View;
|
||||||
|
use Livewire\Component;
|
||||||
|
|
||||||
|
class Statement extends Component
|
||||||
|
{
|
||||||
|
public string $url;
|
||||||
|
|
||||||
|
public array $options = [
|
||||||
|
'show_payments_table' => 0,
|
||||||
|
'show_aging_table' => 0,
|
||||||
|
];
|
||||||
|
|
||||||
|
public function mount(): void
|
||||||
|
{
|
||||||
|
$this->options['start_date'] = now()->startOfYear()->format('Y-m-d');
|
||||||
|
$this->options['end_date'] = now()->format('Y-m-d');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getCurrentUrl(): string
|
||||||
|
{
|
||||||
|
return route('client.statement.raw', $this->options);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function download()
|
||||||
|
{
|
||||||
|
return redirect()->route('client.statement.raw', \array_merge($this->options, ['download' => 1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render(): View
|
||||||
|
{
|
||||||
|
$this->url = route('client.statement.raw', $this->options);
|
||||||
|
|
||||||
|
return render('components.statement');
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\ClientPortal\Statements;
|
||||||
|
|
||||||
|
use App\Models\Client;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class ShowStatementRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function rules()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
//
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare the data for validation.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function prepareForValidation(): void
|
||||||
|
{
|
||||||
|
$this->merge([
|
||||||
|
'show_payments_table' => $this->has('show_payments_table') ? \boolval($this->show_payments_table) : false,
|
||||||
|
'show_aging_table' => $this->has('show_aging_table') ? \boolval($this->show_aging_table) : false,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function client(): Client
|
||||||
|
{
|
||||||
|
return auth('contact')->user()->client;
|
||||||
|
}
|
||||||
|
}
|
@ -4,11 +4,7 @@ namespace App\Http\Requests\Statements;
|
|||||||
|
|
||||||
use App\Http\Requests\Request;
|
use App\Http\Requests\Request;
|
||||||
use App\Models\Client;
|
use App\Models\Client;
|
||||||
use App\Models\Invoice;
|
|
||||||
use App\Models\Payment;
|
|
||||||
use App\Utils\Number;
|
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
use Carbon\Carbon;
|
|
||||||
|
|
||||||
class CreateStatementRequest extends Request
|
class CreateStatementRequest extends Request
|
||||||
{
|
{
|
||||||
@ -33,137 +29,23 @@ class CreateStatementRequest extends Request
|
|||||||
return [
|
return [
|
||||||
'start_date' => 'required|date_format:Y-m-d',
|
'start_date' => 'required|date_format:Y-m-d',
|
||||||
'end_date' => 'required|date_format:Y-m-d',
|
'end_date' => 'required|date_format:Y-m-d',
|
||||||
'client_id' => 'bail|required|exists:clients,id,company_id,'.auth()->user()->company()->id,
|
'client_id' => 'bail|required|exists:clients,id,company_id,' . auth()->user()->company()->id,
|
||||||
'show_payments_table' => 'boolean',
|
'show_payments_table' => 'boolean',
|
||||||
'show_aging_table' => 'boolean',
|
'show_aging_table' => 'boolean',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function prepareForValidation()
|
protected function prepareForValidation()
|
||||||
{
|
{
|
||||||
$input = $this->all();
|
$input = $this->all();
|
||||||
|
|
||||||
$input = $this->decodePrimaryKeys($input);
|
$input = $this->decodePrimaryKeys($input);
|
||||||
|
|
||||||
$this->replace($input);
|
$this->replace($input);
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* The collection of invoices for the statement.
|
public function client(): ?Client
|
||||||
*
|
|
||||||
* @return Invoice[]|\Illuminate\Database\Eloquent\Collection
|
|
||||||
*/
|
|
||||||
public function getInvoices()
|
|
||||||
{
|
{
|
||||||
$input = $this->all();
|
return Client::where('id', $this->client_id)->first();
|
||||||
|
|
||||||
// $input['start_date & $input['end_date are available.
|
|
||||||
$client = Client::where('id', $input['client_id'])->first();
|
|
||||||
|
|
||||||
$from = Carbon::parse($input['start_date']);
|
|
||||||
$to = Carbon::parse($input['end_date']);
|
|
||||||
|
|
||||||
return Invoice::where('company_id', auth()->user()->company()->id)
|
|
||||||
->where('client_id', $client->id)
|
|
||||||
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL, Invoice::STATUS_PAID])
|
|
||||||
->whereBetween('date',[$from, $to])
|
|
||||||
->get();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The collection of payments for the statement.
|
|
||||||
*
|
|
||||||
* @return Payment[]|\Illuminate\Database\Eloquent\Collection
|
|
||||||
*/
|
|
||||||
public function getPayments()
|
|
||||||
{
|
|
||||||
// $input['start_date & $input['end_date are available.
|
|
||||||
$input = $this->all();
|
|
||||||
|
|
||||||
$client = Client::where('id', $input['client_id'])->first();
|
|
||||||
|
|
||||||
$from = Carbon::parse($input['start_date']);
|
|
||||||
$to = Carbon::parse($input['end_date']);
|
|
||||||
|
|
||||||
return Payment::where('company_id', auth()->user()->company()->id)
|
|
||||||
->where('client_id', $client->id)
|
|
||||||
->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED])
|
|
||||||
->whereBetween('date',[$from, $to])
|
|
||||||
->get();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The array of aging data.
|
|
||||||
*/
|
|
||||||
public function getAging(): array
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'0-30' => $this->getAgingAmount('30'),
|
|
||||||
'30-60' => $this->getAgingAmount('60'),
|
|
||||||
'60-90' => $this->getAgingAmount('90'),
|
|
||||||
'90-120' => $this->getAgingAmount('120'),
|
|
||||||
'120+' => $this->getAgingAmount('120+'),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
private function getAgingAmount($range)
|
|
||||||
{
|
|
||||||
$input = $this->all();
|
|
||||||
|
|
||||||
$ranges = $this->calculateDateRanges($range);
|
|
||||||
|
|
||||||
$from = $ranges[0];
|
|
||||||
$to = $ranges[1];
|
|
||||||
|
|
||||||
$client = Client::where('id', $input['client_id'])->first();
|
|
||||||
|
|
||||||
$amount = Invoice::where('company_id', auth()->user()->company()->id)
|
|
||||||
->where('client_id', $client->id)
|
|
||||||
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
|
|
||||||
->where('balance', '>', 0)
|
|
||||||
->whereBetween('date',[$from, $to])
|
|
||||||
->sum('balance');
|
|
||||||
|
|
||||||
return Number::formatMoney($amount, $client);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function calculateDateRanges($range)
|
|
||||||
{
|
|
||||||
|
|
||||||
$ranges = [];
|
|
||||||
|
|
||||||
switch ($range) {
|
|
||||||
case '30':
|
|
||||||
$ranges[0] = now();
|
|
||||||
$ranges[1] = now()->subDays(30);
|
|
||||||
return $ranges;
|
|
||||||
break;
|
|
||||||
case '60':
|
|
||||||
$ranges[0] = now()->subDays(30);
|
|
||||||
$ranges[1] = now()->subDays(60);
|
|
||||||
return $ranges;
|
|
||||||
break;
|
|
||||||
case '90':
|
|
||||||
$ranges[0] = now()->subDays(60);
|
|
||||||
$ranges[1] = now()->subDays(90);
|
|
||||||
return $ranges;
|
|
||||||
break;
|
|
||||||
case '120':
|
|
||||||
$ranges[0] = now()->subDays(90);
|
|
||||||
$ranges[1] = now()->subDays(120);
|
|
||||||
return $ranges;
|
|
||||||
break;
|
|
||||||
case '120+':
|
|
||||||
$ranges[0] = now()->subDays(120);
|
|
||||||
$ranges[1] = now()->subYears(40);
|
|
||||||
return $ranges;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
$ranges[0] = now()->subDays(0);
|
|
||||||
$ranges[1] = now()->subDays(30);
|
|
||||||
return $ranges;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -119,6 +119,8 @@ class PortalComposer
|
|||||||
$data[] = ['title' => ctrans('texts.tasks'), 'url' => 'client.tasks.index', 'icon' => 'clock'];
|
$data[] = ['title' => ctrans('texts.tasks'), 'url' => 'client.tasks.index', 'icon' => 'clock'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$data[] = ['title' => ctrans('texts.statement'), 'url' => 'client.statement', 'icon' => 'activity'];
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -85,6 +85,16 @@ class ClientService
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate the client statement.
|
||||||
|
*
|
||||||
|
* @param array $options
|
||||||
|
*/
|
||||||
|
public function statement(array $options = [])
|
||||||
|
{
|
||||||
|
return (new Statement($this->client, $options))->run();
|
||||||
|
}
|
||||||
|
|
||||||
public function save() :Client
|
public function save() :Client
|
||||||
{
|
{
|
||||||
$this->client->save();
|
$this->client->save();
|
||||||
|
355
app/Services/Client/Statement.php
Normal file
355
app/Services/Client/Statement.php
Normal file
@ -0,0 +1,355 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoice Ninja (https://invoiceninja.com).
|
||||||
|
*
|
||||||
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
||||||
|
*
|
||||||
|
* @license https://www.elastic.co/licensing/elastic-license
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Services\Client;
|
||||||
|
|
||||||
|
use App\Factory\InvoiceFactory;
|
||||||
|
use App\Factory\InvoiceInvitationFactory;
|
||||||
|
use App\Factory\InvoiceItemFactory;
|
||||||
|
use App\Models\Client;
|
||||||
|
use App\Models\Design;
|
||||||
|
use App\Models\Invoice;
|
||||||
|
use App\Models\Payment;
|
||||||
|
use App\Models\Product;
|
||||||
|
use App\Services\PdfMaker\Design as PdfMakerDesign;
|
||||||
|
use App\Services\PdfMaker\PdfMaker;
|
||||||
|
use App\Utils\HostedPDF\NinjaPdf;
|
||||||
|
use App\Utils\HtmlEngine;
|
||||||
|
use App\Utils\Number;
|
||||||
|
use App\Utils\PhantomJS\Phantom;
|
||||||
|
use App\Utils\Traits\Pdf\PdfMaker as PdfMakerTrait;
|
||||||
|
use Illuminate\Database\Eloquent\Collection;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
class Statement
|
||||||
|
{
|
||||||
|
use PdfMakerTrait;
|
||||||
|
|
||||||
|
protected Client $client;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Invoice|Payment|null
|
||||||
|
*/
|
||||||
|
protected $entity;
|
||||||
|
|
||||||
|
protected array $options;
|
||||||
|
|
||||||
|
protected bool $rollback = false;
|
||||||
|
|
||||||
|
public function __construct(Client $client, array $options)
|
||||||
|
{
|
||||||
|
$this->client = $client;
|
||||||
|
|
||||||
|
$this->options = $options;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function run(): ?string
|
||||||
|
{
|
||||||
|
$this
|
||||||
|
->setupOptions()
|
||||||
|
->setupEntity();
|
||||||
|
|
||||||
|
$html = new HtmlEngine($this->getInvitation());
|
||||||
|
|
||||||
|
if ($this->getDesign()->is_custom) {
|
||||||
|
$this->options['custom_partials'] = \json_decode(\json_encode($this->getDesign()->design), true);
|
||||||
|
|
||||||
|
$template = new PdfMakerDesign(\App\Services\PdfMaker\Design::CUSTOM, $this->options);
|
||||||
|
} else {
|
||||||
|
$template = new PdfMakerDesign(strtolower($this->getDesign()->name), $this->options);
|
||||||
|
}
|
||||||
|
|
||||||
|
$variables = $html->generateLabelsAndValues();
|
||||||
|
|
||||||
|
$state = [
|
||||||
|
'template' => $template->elements([
|
||||||
|
'client' => $this->entity->client,
|
||||||
|
'entity' => $this->entity,
|
||||||
|
'pdf_variables' => (array)$this->entity->company->settings->pdf_variables,
|
||||||
|
'$product' => $this->getDesign()->design->product,
|
||||||
|
'variables' => $variables,
|
||||||
|
'invoices' => $this->getInvoices(),
|
||||||
|
'payments' => $this->getPayments(),
|
||||||
|
'aging' => $this->getAging(),
|
||||||
|
], \App\Services\PdfMaker\Design::STATEMENT),
|
||||||
|
'variables' => $variables,
|
||||||
|
'options' => [],
|
||||||
|
'process_markdown' => $this->entity->client->company->markdown_enabled,
|
||||||
|
];
|
||||||
|
|
||||||
|
$maker = new PdfMaker($state);
|
||||||
|
|
||||||
|
$maker
|
||||||
|
->design($template)
|
||||||
|
->build();
|
||||||
|
|
||||||
|
$pdf = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
|
||||||
|
$pdf = (new Phantom)->convertHtmlToPdf($maker->getCompiledHTML(true));
|
||||||
|
} elseif (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
|
||||||
|
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
|
||||||
|
} else {
|
||||||
|
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
|
||||||
|
}
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
nlog(print_r($e->getMessage(), 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->rollback) {
|
||||||
|
DB::rollBack();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $pdf;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup correct entity instance.
|
||||||
|
*
|
||||||
|
* @return Statement
|
||||||
|
*/
|
||||||
|
protected function setupEntity(): self
|
||||||
|
{
|
||||||
|
if (count($this->getInvoices()) >= 1) {
|
||||||
|
$this->entity = $this->getInvoices()->first();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (\is_null($this->entity)) {
|
||||||
|
DB::beginTransaction();
|
||||||
|
$this->rollback = true;
|
||||||
|
|
||||||
|
$invoice = InvoiceFactory::create($this->client->company->id, $this->client->user->id);
|
||||||
|
$invoice->client_id = $this->client->id;
|
||||||
|
$invoice->line_items = $this->buildLineItems();
|
||||||
|
$invoice->save();
|
||||||
|
|
||||||
|
$invitation = InvoiceInvitationFactory::create($invoice->company_id, $invoice->user_id);
|
||||||
|
$invitation->invoice_id = $invoice->id;
|
||||||
|
$invitation->client_contact_id = $this->client->contacts->first()->id;
|
||||||
|
$invitation->save();
|
||||||
|
|
||||||
|
$this->entity = $invoice;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function buildLineItems($count = 1)
|
||||||
|
{
|
||||||
|
$line_items = [];
|
||||||
|
|
||||||
|
for ($x = 0; $x < $count; $x++) {
|
||||||
|
$item = InvoiceItemFactory::create();
|
||||||
|
$item->quantity = 1;
|
||||||
|
//$item->cost = 10;
|
||||||
|
|
||||||
|
if (rand(0, 1)) {
|
||||||
|
$item->tax_name1 = 'GST';
|
||||||
|
$item->tax_rate1 = 10.00;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rand(0, 1)) {
|
||||||
|
$item->tax_name1 = 'VAT';
|
||||||
|
$item->tax_rate1 = 17.50;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rand(0, 1)) {
|
||||||
|
$item->tax_name1 = 'Sales Tax';
|
||||||
|
$item->tax_rate1 = 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
$product = Product::all()->random();
|
||||||
|
|
||||||
|
$item->cost = (float) $product->cost;
|
||||||
|
$item->product_key = $product->product_key;
|
||||||
|
$item->notes = $product->notes;
|
||||||
|
$item->custom_value1 = $product->custom_value1;
|
||||||
|
$item->custom_value2 = $product->custom_value2;
|
||||||
|
$item->custom_value3 = $product->custom_value3;
|
||||||
|
$item->custom_value4 = $product->custom_value4;
|
||||||
|
|
||||||
|
$line_items[] = $item;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $line_items;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup & prepare options.
|
||||||
|
*
|
||||||
|
* @return Statement
|
||||||
|
*/
|
||||||
|
protected function setupOptions(): self
|
||||||
|
{
|
||||||
|
if (! \array_key_exists('start_date', $this->options)) {
|
||||||
|
$this->options['start_date'] = now()->startOfYear()->format('Y-m-d');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! \array_key_exists('end_date', $this->options)) {
|
||||||
|
$this->options['end_date'] = now()->format('Y-m-d');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! \array_key_exists('show_payments_table', $this->options)) {
|
||||||
|
$this->options['show_payments_table'] = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! \array_key_exists('show_aging_table', $this->options)) {
|
||||||
|
$this->options['show_aging_table'] = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The collection of invoices for the statement.
|
||||||
|
*
|
||||||
|
* @return Invoice[]|\Illuminate\Database\Eloquent\Collection
|
||||||
|
*/
|
||||||
|
protected function getInvoices(): Collection
|
||||||
|
{
|
||||||
|
return Invoice::where('company_id', $this->client->company->id)
|
||||||
|
->where('client_id', $this->client->id)
|
||||||
|
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL, Invoice::STATUS_PAID])
|
||||||
|
->whereBetween('date', [$this->options['start_date'], $this->options['end_date']])
|
||||||
|
->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The collection of payments for the statement.
|
||||||
|
*
|
||||||
|
* @return Payment[]|\Illuminate\Database\Eloquent\Collection
|
||||||
|
*/
|
||||||
|
protected function getPayments(): Collection
|
||||||
|
{
|
||||||
|
return Payment::where('company_id', $this->client->company->id)
|
||||||
|
->where('client_id', $this->client->id)
|
||||||
|
->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED])
|
||||||
|
->whereBetween('date', [$this->options['start_date'], $this->options['end_date']])
|
||||||
|
->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get correct invitation ID.
|
||||||
|
*
|
||||||
|
* @return int|bool
|
||||||
|
*/
|
||||||
|
protected function getInvitation()
|
||||||
|
{
|
||||||
|
if ($this->entity instanceof Invoice || $this->entity instanceof Payment) {
|
||||||
|
return $this->entity->invitations->first();
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the array of aging data.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getAging(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'0-30' => $this->getAgingAmount('30'),
|
||||||
|
'30-60' => $this->getAgingAmount('60'),
|
||||||
|
'60-90' => $this->getAgingAmount('90'),
|
||||||
|
'90-120' => $this->getAgingAmount('120'),
|
||||||
|
'120+' => $this->getAgingAmount('120+'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate aging amount.
|
||||||
|
*
|
||||||
|
* @param mixed $range
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function getAgingAmount($range)
|
||||||
|
{
|
||||||
|
$ranges = $this->calculateDateRanges($range);
|
||||||
|
|
||||||
|
$from = $ranges[0];
|
||||||
|
$to = $ranges[1];
|
||||||
|
|
||||||
|
$client = Client::where('id', $this->client->id)->first();
|
||||||
|
|
||||||
|
$amount = Invoice::where('company_id', $this->client->company->id)
|
||||||
|
->where('client_id', $client->id)
|
||||||
|
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
|
||||||
|
->where('balance', '>', 0)
|
||||||
|
->whereBetween('date', [$from, $to])
|
||||||
|
->sum('balance');
|
||||||
|
|
||||||
|
return Number::formatMoney($amount, $client);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate date ranges for aging.
|
||||||
|
*
|
||||||
|
* @param mixed $range
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function calculateDateRanges($range)
|
||||||
|
{
|
||||||
|
$ranges = [];
|
||||||
|
|
||||||
|
switch ($range) {
|
||||||
|
case '30':
|
||||||
|
$ranges[0] = now();
|
||||||
|
$ranges[1] = now()->subDays(30);
|
||||||
|
return $ranges;
|
||||||
|
break;
|
||||||
|
case '60':
|
||||||
|
$ranges[0] = now()->subDays(30);
|
||||||
|
$ranges[1] = now()->subDays(60);
|
||||||
|
return $ranges;
|
||||||
|
break;
|
||||||
|
case '90':
|
||||||
|
$ranges[0] = now()->subDays(60);
|
||||||
|
$ranges[1] = now()->subDays(90);
|
||||||
|
return $ranges;
|
||||||
|
break;
|
||||||
|
case '120':
|
||||||
|
$ranges[0] = now()->subDays(90);
|
||||||
|
$ranges[1] = now()->subDays(120);
|
||||||
|
return $ranges;
|
||||||
|
break;
|
||||||
|
case '120+':
|
||||||
|
$ranges[0] = now()->subDays(120);
|
||||||
|
$ranges[1] = now()->subYears(40);
|
||||||
|
return $ranges;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$ranges[0] = now()->subDays(0);
|
||||||
|
$ranges[1] = now()->subDays(30);
|
||||||
|
return $ranges;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get correct design for statement.
|
||||||
|
*
|
||||||
|
* @return \App\Models\Design
|
||||||
|
*/
|
||||||
|
protected function getDesign(): Design
|
||||||
|
{
|
||||||
|
$id = 1;
|
||||||
|
|
||||||
|
if (!empty($this->client->getSetting('entity_design_id'))) {
|
||||||
|
$id = (int) $this->client->getSetting('entity_design_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
return Design::find($id);
|
||||||
|
}
|
||||||
|
}
|
@ -40,7 +40,10 @@ trait DesignHelpers
|
|||||||
|
|
||||||
if (isset($this->context['invoices'])) {
|
if (isset($this->context['invoices'])) {
|
||||||
$this->invoices = $this->context['invoices'];
|
$this->invoices = $this->context['invoices'];
|
||||||
$this->entity = $this->invoices->first();
|
|
||||||
|
if (\count($this->invoices) >= 1) {
|
||||||
|
$this->entity = $this->invoices->first();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($this->context['payments'])) {
|
if (isset($this->context['payments'])) {
|
||||||
|
@ -82,6 +82,12 @@ class PdfMaker
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return compiled HTML.
|
||||||
|
*
|
||||||
|
* @param bool $final deprecated
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
public function getCompiledHTML($final = false)
|
public function getCompiledHTML($final = false)
|
||||||
{
|
{
|
||||||
$html = $this->document->saveHTML();
|
$html = $this->document->saveHTML();
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
@endphp
|
@endphp
|
||||||
|
|
||||||
@push('head')
|
@push('head')
|
||||||
<meta name="pdf-url" content="{{ $entity->pdf_file_path(null, 'url', true) }}">
|
<meta name="pdf-url" content="{{ $url ?? $entity->pdf_file_path(null, 'url', true) }}">
|
||||||
<script src="{{ asset('js/vendor/pdf.js/pdf.min.js') }}"></script>
|
<script src="{{ asset('js/vendor/pdf.js/pdf.min.js') }}"></script>
|
||||||
@endpush
|
@endpush
|
||||||
|
|
||||||
@ -72,7 +72,7 @@
|
|||||||
class="origin-top-right absolute right-0 mt-2 w-56 rounded-md shadow-lg">
|
class="origin-top-right absolute right-0 mt-2 w-56 rounded-md shadow-lg">
|
||||||
<div class="rounded-md bg-white shadow-xs">
|
<div class="rounded-md bg-white shadow-xs">
|
||||||
<div class="py-1">
|
<div class="py-1">
|
||||||
<a target="_blank" href="?mode=fullscreen"
|
<a target="_blank" href="{{ $fullscreen_url ?? '?mode=fullscreen' }}"
|
||||||
class="block px-4 py-2 text-sm leading-5 text-gray-700 hover:bg-gray-100 hover:text-gray-900 focus:outline-none focus:bg-gray-100 focus:text-gray-900">{{ ctrans('texts.open_in_new_tab') }}</a>
|
class="block px-4 py-2 text-sm leading-5 text-gray-700 hover:bg-gray-100 hover:text-gray-900 focus:outline-none focus:bg-gray-100 focus:text-gray-900">{{ ctrans('texts.open_in_new_tab') }}</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -86,7 +86,7 @@
|
|||||||
<canvas id="pdf-placeholder" class="shadow rounded-lg bg-white mt-4 p-4"></canvas>
|
<canvas id="pdf-placeholder" class="shadow rounded-lg bg-white mt-4 p-4"></canvas>
|
||||||
</div>
|
</div>
|
||||||
@else
|
@else
|
||||||
<iframe src="{{ $entity->pdf_file_path(null, 'url', true) }}" class="h-screen w-full border-0 mt-4"></iframe>
|
<iframe src="{{ $url ?? $entity->pdf_file_path(null, 'url', true) }}" class="h-screen w-full border-0 mt-4"></iframe>
|
||||||
@endif
|
@endif
|
||||||
|
|
||||||
|
|
||||||
|
@ -0,0 +1,39 @@
|
|||||||
|
<div>
|
||||||
|
<div class="flex flex-col md:flex-row md:justify-between">
|
||||||
|
<div class="flex flex-col md:flex-row md:items-center">
|
||||||
|
{{-- <label for="status" class="flex items-center mr-4">
|
||||||
|
<span class="mr-2">{{ ctrans('texts.status') }}</span>
|
||||||
|
<select class="input">
|
||||||
|
<option value="all">{{ ctrans('texts.all') }}</option>
|
||||||
|
<option value="unpaid">{{ ctrans('texts.unpaid') }}</option>
|
||||||
|
<option value="paid">{{ ctrans('texts.paid') }}</option>
|
||||||
|
</select>
|
||||||
|
</label> <!-- End status dropdown --> --}}
|
||||||
|
|
||||||
|
<div class="flex">
|
||||||
|
<label for="from" class="block w-full flex items-center mr-4">
|
||||||
|
<span class="mr-2">{{ ctrans('texts.from') }}:</span>
|
||||||
|
<input wire:model="options.start_date" type="date" class="input w-full">
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<label for="to" class="block w-full flex items-center mr-4">
|
||||||
|
<span class="mr-2">{{ ctrans('texts.to') }}:</span>
|
||||||
|
<input wire:model="options.end_date" type="date" class="input w-full">
|
||||||
|
</label>
|
||||||
|
</div> <!-- End date range -->
|
||||||
|
|
||||||
|
<label for="show_payments" class="block flex items-center mr-4 mt-4 md:mt-0">
|
||||||
|
<input wire:model="options.show_payments_table" type="checkbox" class="form-checkbox" autocomplete="off">
|
||||||
|
<span class="ml-2">{{ ctrans('texts.show_payments') }}</span>
|
||||||
|
</label> <!-- End show payments checkbox -->
|
||||||
|
|
||||||
|
<label for="show_aging" class="block flex items-center">
|
||||||
|
<input wire:model="options.show_aging_table" type="checkbox" class="form-checkbox" autocomplete="off">
|
||||||
|
<span class="ml-2">{{ ctrans('texts.show_aging') }}</span>
|
||||||
|
</label> <!-- End show aging checkbox -->
|
||||||
|
</div>
|
||||||
|
<button wire:click="download" class="button button-primary bg-primary mt-4 md:mt-0">{{ ctrans('texts.download') }}</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
@include('portal.ninja2020.components.pdf-viewer', ['url' => $url])
|
||||||
|
</div>
|
@ -0,0 +1,7 @@
|
|||||||
|
@extends('portal.ninja2020.layout.app')
|
||||||
|
|
||||||
|
@section('meta_title', ctrans('texts.statement'))
|
||||||
|
|
||||||
|
@section('body')
|
||||||
|
@livewire('statement')
|
||||||
|
@endsection
|
@ -80,6 +80,9 @@ Route::group(['middleware' => ['auth:contact', 'locale', 'check_client_existence
|
|||||||
|
|
||||||
Route::resource('tasks', 'ClientPortal\TaskController')->only(['index']);
|
Route::resource('tasks', 'ClientPortal\TaskController')->only(['index']);
|
||||||
|
|
||||||
|
Route::get('statement', 'ClientPortal\StatementController@index')->name('statement');
|
||||||
|
Route::get('statement/raw', 'ClientPortal\StatementController@raw')->name('statement.raw');
|
||||||
|
|
||||||
Route::post('upload', 'ClientPortal\UploadController')->name('upload.store');
|
Route::post('upload', 'ClientPortal\UploadController')->name('upload.store');
|
||||||
Route::get('logout', 'Auth\ContactLoginController@logout')->name('logout');
|
Route::get('logout', 'Auth\ContactLoginController@logout')->name('logout');
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user