Bulk download PDF, Client Portal formatting (#3046)

* Update OpenAPI for TemplateController

* Add bulk invoice download functionality

* Working on Client portal

* Move selective queries to cache instead of DB

* Fix formatting in Payments table, implement cache for languages, resolve route model for clientcontacts and users
This commit is contained in:
David Bomba 2019-11-06 09:52:57 +11:00 committed by GitHub
parent 4694675b91
commit 3405b91c64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 319 additions and 142 deletions

View File

@ -20,11 +20,14 @@ use App\Repositories\BaseRepository;
use App\Utils\Number;
use App\Utils\Traits\MakesDates;
use App\Utils\Traits\MakesHash;
use Barracuda\ArchiveStream\Archive;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Yajra\DataTables\Facades\DataTables;
use Yajra\DataTables\Html\Builder;
use ZipStream\Option\Archive;
use ZipStream\ZipStream;
/**
* Class InvoiceController
@ -168,25 +171,32 @@ class InvoiceController extends Controller
{
$invoices = Invoice::whereIn('id', $ids)
->whereClientId(auth()->user()->client->id)
->get()
->filter(function ($invoice){
return $invoice->isPayable();
});
->get();
//generate pdf's of invoices locally
if(!$invoices || $invoices->count() == 0)
return;
//if only 1 pdf, output to buffer for download
//if multiple pdf's, output to zip stream using Barracuda lib
if($invoices->count() == 1)
return response()->download(public_path($invoices->first()->pdf_file_path()));
# enable output of HTTP headers
$options = new Archive();
$options->setSendHttpHeaders(true);
# create a new zipstream object
$zip = new ZipStream(date('Y-m-d') . '_' . str_replace(' ', '_', trans('texts.invoices')).".zip", $options);
foreach($invoices as $invoice){
$zip->addFileFromPath(basename($invoice->pdf_file_path()), public_path($invoice->pdf_file_path()));
}
# finish the zip stream
$zip->finish();
/*
$zip = Archive::instance_by_useragent(date('Y-m-d') . '_' . str_replace(' ', '_', trans('texts.invoices')));
$zip->add_file($name, $document->getRaw());
$zip->finish();
*/
}

View File

@ -46,7 +46,7 @@ class PaymentController extends Controller
public function index(PaymentFilters $filters, Builder $builder)
{
//$payments = Payment::filter($filters);
$payments = Payment::with('type')->get();
$payments = Payment::with('type','client')->get();
if (request()->ajax()) {
@ -58,6 +58,13 @@ class PaymentController extends Controller
->editColumn('status_id', function ($payment){
return Payment::badgeForStatus($payment->status_id);
})
->editColumn('payment_date', function ($payment){
//return $payment->payment_date;
return $payment->formatDate($payment->payment_date, $payment->client->date_format());
})
->editColumn('amount', function ($payment) {
return Number::formatMoney($payment->amount, $payment->client);
})
->rawColumns(['action', 'status_id','payment_type_id'])
->make(true);

View File

@ -16,6 +16,8 @@ use App\Http\Requests\ClientPortal\ShowRecurringInvoiceRequest;
use App\Models\RecurringInvoice;
use App\Notifications\ClientContactRequestCancellation;
use App\Notifications\ClientContactResetPassword;
use App\Utils\Number;
use App\Utils\Traits\MakesDates;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
@ -33,7 +35,7 @@ class RecurringInvoiceController extends Controller
{
use MakesHash;
use MakesDates;
/**
* Show the list of Invoices
*
@ -46,6 +48,7 @@ class RecurringInvoiceController extends Controller
$invoices = RecurringInvoice::whereClientId(auth()->user()->client->id)
->whereIn('status_id', [RecurringInvoice::STATUS_PENDING, RecurringInvoice::STATUS_ACTIVE, RecurringInvoice::STATUS_COMPLETED])
->orderBy('status_id', 'asc')
->with('client')
->get();
if (request()->ajax()) {
@ -58,6 +61,15 @@ class RecurringInvoiceController extends Controller
->editColumn('status_id', function ($invoice){
return RecurringInvoice::badgeForStatus($invoice->status);
})
->editColumn('start_date', function ($invoice){
return $this->formatDate($invoice->invoice_date, $invoice->client->date_format());
})
->editColumn('next_send_date', function ($invoice){
return $this->formatDate($invoice->next_send_date, $invoice->client->date_format());
})
->editColumn('amount', function ($invoice){
return Number::formatMoney($invoice->amount, $invoice->client);
})
->rawColumns(['action', 'status_id'])
->make(true);

View File

@ -77,7 +77,7 @@ class TemplateController extends BaseController
*
* @return \Illuminate\Http\Response
*
* @OA\Get(
* @OA\Post(
* path="/api/v1/templates/{entity}/{entity_id}",
* operationId="getShowTemplate",
* tags={"templates"},

View File

@ -50,7 +50,7 @@ class QueryLogging
Log::info($request->method() . ' - ' . $request->url() . ": $count queries - " . $time);
// if($count > 50)
// Log::info($queries);
// Log::info($queries);
}
}

View File

@ -0,0 +1,155 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Invoice;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\PaymentTerm;
use App\Repositories\InvoiceRepository;
use App\Utils\Traits\NumberFormatter;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Spatie\Browsershot\Browsershot;
use Symfony\Component\Debug\Exception\FatalThrowableError;
class CreateInvoicePdf implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter;
public $invoice;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Invoice $invoice)
{
$this->invoice = $invoice;
}
public function handle()
{
$this->invoice->load('client');
$path = 'public/' . $this->invoice->client->client_hash . '/invoices/';
$file_path = $path . $this->invoice->invoice_number . '.pdf';
//get invoice design
$html = $this->generateInvoiceHtml($this->invoice->design(), $this->invoice);
//todo - move this to the client creation stage so we don't keep hitting this unnecessarily
Storage::makeDirectory($path, 0755);
//create pdf
$pdf = $this->makePdf(null,null,$html);
$path = Storage::put($file_path, $pdf);
}
/**
* Returns a PDF stream
*
* @param string $header Header to be included in PDF
* @param string $footer Footer to be included in PDF
* @param string $html The HTML object to be converted into PDF
*
* @return string The PDF string
*/
private function makePdf($header, $footer, $html)
{
return Browsershot::html($html)
//->showBrowserHeaderAndFooter()
//->headerHtml($header)
//->footerHtml($footer)
->waitUntilNetworkIdle(false)->pdf();
//->margins(10,10,10,10)
//->savePdf('test.pdf');
}
/**
* Generate the HTML invoice parsing variables
* and generating the final invoice HTML
*
* @param string $design either the path to the design template, OR the full design template string
* @param Collection $invoice The invoice object
*
* @return string The invoice string in HTML format
*/
private function generateInvoiceHtml($design, $invoice) :string
{
$variables = array_merge($invoice->makeLabels(), $invoice->makeValues());
$design = str_replace(array_keys($variables), array_values($variables), $design);
$data['invoice'] = $invoice;
return $this->renderView($design, $data);
//return view($design, $data)->render();
}
/**
* Parses the blade file string and processes the template variables
*
* @param string $string The Blade file string
* @param array $data The array of template variables
* @return string The return HTML string
*
*/
private function renderView($string, $data) :string
{
if (!$data) {
$data = [];
}
$data['__env'] = app(\Illuminate\View\Factory::class);
$php = Blade::compileString($string);
$obLevel = ob_get_level();
ob_start();
extract($data, EXTR_SKIP);
try {
eval('?' . '>' . $php);
} catch (\Exception $e) {
while (ob_get_level() > $obLevel) {
ob_end_clean();
}
throw $e;
} catch (\Throwable $e) {
while (ob_get_level() > $obLevel) {
ob_end_clean();
}
throw new FatalThrowableError($e);
}
return ob_get_clean();
}
}

View File

@ -11,18 +11,13 @@
namespace App\Listeners\Invoice;
use App\Utils\Traits\MakesHash;
use App\Jobs\Invoice\CreateInvoicePdf as PdfCreator;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Spatie\Browsershot\Browsershot;
use Symfony\Component\Debug\Exception\FatalThrowableError;
class CreateInvoicePdf implements ShouldQueue
{
protected $activity_repo;
/**
* Create the event listener.
*
@ -40,109 +35,6 @@ class CreateInvoicePdf implements ShouldQueue
*/
public function handle($event)
{
$invoice = $event->invoice;
$invoice->load('client');
$path = 'public/' . $invoice->client->client_hash . '/invoices/';
$file_path = $path . $invoice->invoice_number . '.pdf';
//get invoice design
$html = $this->generateInvoiceHtml($invoice->design(), $invoice);
//todo - move this to the client creation stage so we don't keep hitting this unnecessarily
Storage::makeDirectory($path, 0755);
//create pdf
$pdf = $this->makePdf(null,null,$html);
$path = Storage::put($file_path, $pdf);
PdfCreator::dispatch($event->invoice);
}
/**
* Returns a PDF stream
*
* @param string $header Header to be included in PDF
* @param string $footer Footer to be included in PDF
* @param string $html The HTML object to be converted into PDF
*
* @return string The PDF string
*/
private function makePdf($header, $footer, $html)
{
return Browsershot::html($html)
//->showBrowserHeaderAndFooter()
//->headerHtml($header)
//->footerHtml($footer)
->waitUntilNetworkIdle(false)->pdf();
//->margins(10,10,10,10)
//->savePdf('test.pdf');
}
/**
* Generate the HTML invoice parsing variables
* and generating the final invoice HTML
*
* @param string $design either the path to the design template, OR the full design template string
* @param Collection $invoice The invoice object
*
* @return string The invoice string in HTML format
*/
private function generateInvoiceHtml($design, $invoice) :string
{
$variables = array_merge($invoice->makeLabels(), $invoice->makeValues());
$design = str_replace(array_keys($variables), array_values($variables), $design);
$data['invoice'] = $invoice;
return $this->renderView($design, $data);
//return view($design, $data)->render();
}
/**
* Parses the blade file string and processes the template variables
*
* @param string $string The Blade file string
* @param array $data The array of template variables
* @return string The return HTML string
*
*/
private function renderView($string, $data) :string
{
if (!$data) {
$data = [];
}
$data['__env'] = app(\Illuminate\View\Factory::class);
$php = Blade::compileString($string);
$obLevel = ob_get_level();
ob_start();
extract($data, EXTR_SKIP);
try {
eval('?' . '>' . $php);
} catch (\Exception $e) {
while (ob_get_level() > $obLevel) {
ob_end_clean();
}
throw $e;
} catch (\Throwable $e) {
while (ob_get_level() > $obLevel) {
ob_end_clean();
}
throw new FatalThrowableError($e);
}
return ob_get_clean();
}
}

View File

@ -31,6 +31,7 @@ use App\Utils\Traits\MakesDates;
use App\Utils\Traits\MakesHash;
use Hashids\Hashids;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\URL;
use Laracasts\Presenter\PresentableTrait;
@ -168,13 +169,26 @@ class Client extends BaseModel
}
public function date_format()
{
return DateFormat::find($this->getSetting('date_format_id'))->format;
{
$date_formats = Cache::get('date_formats');
return $date_formats->filter(function($item) {
return $item->id == $this->getSetting('date_format_id');
})->first()->format;
//return DateFormat::find($this->getSetting('date_format_id'))->format;
}
public function currency()
{
return Currency::find($this->getSetting('currency_id'));
$currencies = Cache::get('currencies');
return $currencies->filter(function($item) {
return $item->id == $this->getSetting('currency_id');
})->first();
//return Currency::find($this->getSetting('currency_id'));
//return $this->belongsTo(Currency::class);
}

View File

@ -138,8 +138,28 @@ class ClientContact extends Authenticatable implements HasLocalePreference
public function preferredLocale()
{
$lang = Language::find($this->client->getSetting('language_id'));
$languages = Cache::get('languages');
return $languages->filter(function($item) {
return $item->id == $this->client->getSetting('language_id');
})->first()->locale;
return $lang->locale;
//$lang = Language::find($this->client->getSetting('language_id'));
//return $lang->locale;
}
/**
* Retrieve the model for a bound value.
*
* @param mixed $value
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function resolveRouteBinding($value)
{
return $this
->withTrashed()
->where('id', $this->decodePrimaryKey($value))->firstOrFail();
}
}

View File

@ -14,6 +14,7 @@ namespace App\Models;
use App\Events\Invoice\InvoiceWasUpdated;
use App\Helpers\Invoice\InvoiceSum;
use App\Helpers\Invoice\InvoiceSumInclusive;
use App\Jobs\Invoice\CreateInvoicePdf;
use App\Models\Currency;
use App\Models\Filterable;
use App\Models\PaymentTerm;
@ -298,6 +299,17 @@ class Invoice extends BaseModel
return $public_path;
}
public function pdf_file_path()
{
$storage_path = 'storage/' . $this->client->client_hash . '/invoices/'. $this->invoice_number . '.pdf';
if(!Storage::exists($storage_path)) {
CreateInvoicePdf::dispatchNow($this);
}
return $storage_path;
}
/**
* @param bool $save
*/

View File

@ -12,6 +12,7 @@
namespace App\Models;
use App\Models\Filterable;
use App\Utils\Traits\MakesDates;
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
@ -25,7 +26,7 @@ class RecurringInvoice extends BaseModel
use MakesHash;
use SoftDeletes;
use Filterable;
use MakesDates;
/**
* Invoice Statuses
*/

View File

@ -361,6 +361,17 @@ class User extends Authenticatable implements MustVerifyEmail
return $this->email;
}
/**
* Retrieve the model for a bound value.
*
* @param mixed $value
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function resolveRouteBinding($value)
{
return $this
->withTrashed()
->where('id', $this->decodePrimaryKey($value))->firstOrFail();
}
}

View File

@ -46,7 +46,7 @@ class ClientContactRequestCancellation extends Notification implements ShouldQue
*/
public function via($notifiable)
{
return ['mail'];
return ['mail','slack'];
}
/**

View File

@ -33,6 +33,7 @@
"laravelcollective/html": "6.0.*",
"league/fractal": "^0.17.0",
"league/omnipay": "^3.0",
"maennchen/zipstream-php": "^1.2",
"nwidart/laravel-modules": "^6.0",
"omnipay/paypal": "^3.0",
"omnipay/stripe": "^3.0",

View File

@ -178,7 +178,10 @@ $(document).ready(function() {
});
$('#download_invoices').click(function() {
alert('download');
$('#hashed_ids').val(selected);
$('#action').val('download');
$('#payment_form').submit();
});

View File

@ -12,11 +12,11 @@
</div>
<div class="card-body">
<table class="table table-responsive-sm table-bordered">
<tr><td style="text-align: right;">{{ctrans('texts.start_date')}}</td><td>{!! $invoice->start_date !!}</td></tr>
<tr><td style="text-align: right;">{{ctrans('texts.next_send_date')}}</td><td>{!! $invoice->next_send_date !!}</td></tr>
<tr><td style="text-align: right;">{{ctrans('texts.start_date')}}</td><td>{!! $invoice->formatDate($invoice->start_date,$invoice->client->date_format()) !!}</td></tr>
<tr><td style="text-align: right;">{{ctrans('texts.next_send_date')}}</td><td>{!! $invoice->formatDate($invoice->next_send_date,$invoice->client->date_format()) !!}</td></tr>
<tr><td style="text-align: right;">{{ctrans('texts.frequency')}}</td><td>{!! App\Models\RecurringInvoice::frequencyForKey($invoice->frequency_id) !!}</td></tr>
<tr><td style="text-align: right;">{{ctrans('texts.cycles_remaining')}}</td><td>{!! $invoice->remaining_cycles !!}</td></tr>
<tr><td style="text-align: right;">{{ctrans('texts.amount')}}</td><td>{!! $invoice->amount !!}</td></tr>
<tr><td style="text-align: right;">{{ctrans('texts.amount')}}</td><td>{!! App\Utils\Number::formatMoney($invoice->amount, $invoice->client) !!}</td></tr>
</table>

View File

@ -0,0 +1,39 @@
<?php
namespace Tests\Integration;
use Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Cache;
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
*/
class CheckCacheTest extends TestCase
{
use MockAccountData;
use DatabaseTransactions;
public function setUp() :void
{
parent::setUp();
$this->makeTestData();
}
public function testWarmedUpCache()
{
$date_formats = Cache::get('date_formats');
$this->assertNotNull($date_formats);
}
public function testCacheCount()
{
$date_formats = Cache::get('date_formats');
$this->assertEquals(13, count($date_formats));
}
}