Merge pull request #4703 from turbo124/v5-develop

Refactor alternateSave() in BaseRepo
This commit is contained in:
David Bomba 2021-01-16 08:45:37 +11:00 committed by GitHub
commit 8c8509ca40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 258 additions and 53 deletions

View File

@ -52,7 +52,7 @@ class PostUpdate extends Command
nlog("finished migrating");
exec('vendor/bin/composer install --no-dev:');
exec('vendor/bin/composer install --no-dev');
nlog("finished running composer install ");

View File

@ -21,17 +21,18 @@
*/
function nlog($output, $context = []): void
{
$trace = debug_backtrace();
\Illuminate\Support\Facades\Log::channel('invoiceninja')->info(print_r($trace[1]['class'],1), []);
if (!config('ninja.expanded_logging'))
return;
if (config('ninja.expanded_logging')) {
if (gettype($output) == 'object') {
$output = print_r($output, 1);
}
$trace = debug_backtrace();
\Illuminate\Support\Facades\Log::channel('invoiceninja')->info(print_r($trace[1]['class'],1), []);
\Illuminate\Support\Facades\Log::channel('invoiceninja')->info($output, $context);
}
}
if (!function_exists('ray')) {

View File

@ -22,6 +22,7 @@ class DashboardController extends Controller
*/
public function index()
{
return $this->render('dashboard.index');
return redirect()->route('client.invoices.index');
//return $this->render('dashboard.index');
}
}

View File

@ -431,6 +431,62 @@ class RecurringInvoiceController extends BaseController
return response()->json([], 200);
}
/**
* @OA\Get(
* path="/api/v1/recurring_invoice/{invitation_key}/download",
* operationId="downloadInvoice",
* tags={"invoices"},
* summary="Download a specific invoice by invitation key",
* description="Downloads a specific invoice",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="invitation_key",
* in="path",
* description="The Recurring Invoice Invitation Key",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the recurring invoice pdf",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
* @param $invitation_key
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function downloadPdf($invitation_key)
{
$invitation = $this->recurring_invoice_repo->getInvitationByKey($invitation_key);
$contact = $invitation->contact;
$recurring_invoice = $invitation->recurring_invoice;
$file_path = $recurring_invoice->service()->getInvoicePdf($contact);
return response()->download($file_path, basename($file_path));
}
/**
* Perform bulk actions on the list view.
*

View File

@ -67,8 +67,9 @@ class PortalComposer
{
$data = [];
if($this->settings->enable_client_portal_dashboard == TRUE)
$data[] = [ 'title' => ctrans('texts.dashboard'), 'url' => 'client.dashboard', 'icon' => 'activity'];
//@todo wire this back in when we are happy with dashboard.
// if($this->settings->enable_client_portal_dashboard == TRUE)
// $data[] = [ 'title' => ctrans('texts.dashboard'), 'url' => 'client.dashboard', 'icon' => 'activity'];
$data[] = ['title' => ctrans('texts.invoices'), 'url' => 'client.invoices.index', 'icon' => 'file-text'];
$data[] = ['title' => ctrans('texts.recurring_invoices'), 'url' => 'client.recurring_invoices.index', 'icon' => 'file'];

View File

@ -19,6 +19,7 @@ use App\Models\Invoice;
use App\Models\InvoiceInvitation;
use App\Models\Quote;
use App\Models\QuoteInvitation;
use App\Models\RecurringInvoice;
use App\Models\RecurringInvoiceInvitation;
use App\Services\PdfMaker\Design as PdfDesignModel;
use App\Services\PdfMaker\Design as PdfMakerDesign;
@ -106,6 +107,9 @@ class CreateEntityPdf implements ShouldQueue
} elseif ($this->entity instanceof Credit) {
$path = $this->entity->client->credit_filepath();
$entity_design_id = 'credit_design_id';
} elseif ($this->entity instanceof RecurringInvoice) {
$path = $this->entity->client->recurring_invoice_filepath();
$entity_design_id = 'invoice_design_id';
}
$file_path = $path.$this->entity->number.'.pdf';

View File

@ -605,6 +605,11 @@ class Client extends BaseModel implements HasLocalePreference
return $this->company->company_key.'/'.$this->client_hash.'/credits/';
}
public function recurring_invoice_filepath()
{
return $this->company->company_key.'/'.$this->client_hash.'/recurring_invoices/';
}
public function company_filepath()
{
return $this->company->company_key.'/';

View File

@ -0,0 +1,31 @@
<?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://opensource.org/licenses/AAL
*/
namespace App\Models\Presenters;
use App\Utils\Number;
use App\Utils\Traits\MakesDates;
use Laracasts\Presenter\PresentableTrait;
/**
* Class InvoicePresenter.
*
* For convenience and to allow users to easiliy
* customise their invoices, we provide all possible
* invoice variables to be available from this presenter.
*
* Shortcuts to other presenters are here to facilitate
* a clean UI / UX
*/
class RecurringInvoicePresenter extends InvoicePresenter
{
}

View File

@ -13,12 +13,14 @@ namespace App\Models;
use App\Helpers\Invoice\InvoiceSum;
use App\Helpers\Invoice\InvoiceSumInclusive;
use App\Models\Presenters\RecurringInvoicePresenter;
use App\Services\Recurring\RecurringService;
use App\Utils\Traits\MakesDates;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\Recurring\HasRecurrence;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use Laracasts\Presenter\PresentableTrait;
/**
* Class for Recurring Invoices.
@ -30,6 +32,9 @@ class RecurringInvoice extends BaseModel
use Filterable;
use MakesDates;
use HasRecurrence;
use PresentableTrait;
protected $presenter = RecurringInvoicePresenter::class;
/**
* Invoice Statuses.

View File

@ -130,13 +130,12 @@ class BaseRepository
return count($entities);
}
/* Returns an invoice if defined as a key in the $resource array*/
public function getInvitation($invitation, $resource)
{
if (is_array($invitation) && ! array_key_exists('key', $invitation)) {
if (is_array($invitation) && ! array_key_exists('key', $invitation))
return false;
}
$invitation_class = sprintf('App\\Models\\%sInvitation', $resource);
$invitation = $invitation_class::whereRaw('BINARY `key`= ?', [$invitation['key']])->first();
@ -144,8 +143,24 @@ class BaseRepository
return $invitation;
}
/* Clean return of a key rather than butchering the model*/
private function resolveEntityKey($model)
{
switch ($model) {
case ($model instanceof RecurringInvoice):
return 'recurring_invoice_id';
case ($model instanceof Invoice):
return 'invoice_id';
case ($model instanceof Quote):
return 'quote_id';
case ($model instanceof Credit):
return 'credit_id';
}
}
/**
* Alternative save used for Invoices, Quotes & Credits.
* Alternative save used for Invoices, Recurring Invoices, Quotes & Credits.
*
* @param $data
* @param $model
* @return mixed
@ -153,20 +168,17 @@ class BaseRepository
*/
protected function alternativeSave($data, $model)
{
$class = new ReflectionClass($model);
if (array_key_exists('client_id', $data)) {
$client = Client::where('id', $data['client_id'])->withTrashed()->first();
} else {
$client = Client::where('id', $model->client_id)->withTrashed()->first();
}
if (array_key_exists('client_id', $data)) //forces the client_id if it doesn't exist
$model->client_id = $data['client_id'];
$client = Client::where('id', $model->client_id)->withTrashed()->first();
$state = [];
$resource = explode('\\', $class->name)[2]; /** This will extract 'Invoice' from App\Models\Invoice */
$lcfirst_resource_id = lcfirst($resource).'_id'; //doesn't work for recurring.
if($class->name == RecurringInvoice::class)
$lcfirst_resource_id = 'recurring_invoice_id';
$resource = class_basename($model); //ie Invoice
$lcfirst_resource_id = $this->resolveEntityKey($model); //ie invoice_id
$state['starting_amount'] = $model->amount;
@ -176,26 +188,24 @@ class BaseRepository
$data = array_merge($company_defaults, $data);
}
$tmp_data = $data;
$tmp_data = $data; //preserves the $data arrayss
/* We need to unset some variable as we sometimes unguard the model */
if (isset($tmp_data['invitations'])) {
if (isset($tmp_data['invitations']))
unset($tmp_data['invitations']);
}
if (isset($tmp_data['client_contacts'])) {
if (isset($tmp_data['client_contacts']))
unset($tmp_data['client_contacts']);
}
$model->fill($tmp_data);
$model->save();
if (array_key_exists('documents', $data)) {
/* Model now persisted, now lets do some child tasks */
if (array_key_exists('documents', $data))
$this->saveDocuments($data['documents'], $model);
}
$invitation_factory_class = sprintf('App\\Factory\\%sInvitationFactory', $resource);
/* Marks whether the client contact should receive emails based on the send_email property */
if (isset($data['client_contacts'])) {
foreach ($data['client_contacts'] as $contact) {
if ($contact['send_email'] == 1 && is_string($contact['id'])) {
@ -206,6 +216,7 @@ class BaseRepository
}
}
/* If invitations are present we need to filter existing invitations with the new ones */
if (isset($data['invitations'])) {
$invitations = collect($data['invitations']);
@ -214,23 +225,24 @@ class BaseRepository
$invitation_class = sprintf('App\\Models\\%sInvitation', $resource);
$invitation = $invitation_class::whereRaw('BINARY `key`= ?', [$invitation])->first();
if ($invitation) {
if ($invitation)
$invitation->delete();
}
});
foreach ($data['invitations'] as $invitation) {
//if no invitations are present - create one.
if (! $this->getInvitation($invitation, $resource)) {
if (isset($invitation['id'])) {
if (isset($invitation['id']))
unset($invitation['id']);
}
//make sure we are creating an invite for a contact who belongs to the client only!
$contact = ClientContact::find($invitation['client_contact_id']);
if ($contact && $model->client_id == $contact->client_id) {
$invitation_class = sprintf('App\\Models\\%sInvitation', $resource);
$new_invitation = $invitation_class::withTrashed()
@ -239,12 +251,17 @@ class BaseRepository
->first();
if ($new_invitation && $new_invitation->trashed()) {
$new_invitation->restore();
} else {
$invitation_factory_class = sprintf('App\\Factory\\%sInvitationFactory', $resource);
$new_invitation = $invitation_factory_class::create($model->company_id, $model->user_id);
$new_invitation->{$lcfirst_resource_id} = $model->id;
$new_invitation->client_contact_id = $contact->id;
$new_invitation->save();
}
}
}
@ -254,50 +271,61 @@ class BaseRepository
$model->load('invitations');
/* If no invitations have been created, this is our fail safe to maintain state*/
if ($model->invitations->count() == 0) {
if ($model->invitations->count() == 0)
$model->service()->createInvitations();
}
/* Recalculate invoice amounts */
$model = $model->calc()->getInvoice();
/* We use this to compare to our starting amount */
$state['finished_amount'] = $model->amount;
/* Apply entity number */
$model = $model->service()->applyNumber()->save();
if ($model->company->update_products !== false) {
/* Update product details if necessary */
if ($model->company->update_products !== false)
UpdateOrCreateProduct::dispatch($model->line_items, $model, $model->company);
}
if ($class->name == Invoice::class) {
/* Perform model specific tasks */
if ($model instanceof Invoice) {
if (($state['finished_amount'] != $state['starting_amount']) && ($model->status_id != Invoice::STATUS_DRAFT)) {
$model->ledger()->updateInvoiceBalance(($state['finished_amount'] - $state['starting_amount']));
$model->client->service()->updateBalance(($state['finished_amount'] - $state['starting_amount']))->save();
}
if (! $model->design_id) {
if (! $model->design_id)
$model->design_id = $this->decodePrimaryKey($client->getSetting('invoice_design_id'));
}
//links tasks and expenses back to the invoice.
$model->service()->linkEntities()->save();
}
if ($class->name == Credit::class) {
if ($model instanceof Credit) {
$model = $model->calc()->getCredit();
$model->ledger()->updateCreditBalance(($state['finished_amount'] - $state['starting_amount']));
if (! $model->design_id) {
if (! $model->design_id)
$model->design_id = $this->decodePrimaryKey($client->getSetting('credit_design_id'));
}
}
if ($class->name == Quote::class) {
if ($model instanceof Quote) {
$model = $model->calc()->getQuote();
}
if($class->name == RecurringInvoice::class) {
if ($model instanceof RecurringInvoice) {
$model = $model->calc()->getRecurringInvoice();
}
$model->save();

View File

@ -13,6 +13,7 @@ namespace App\Repositories;
use App\Helpers\Invoice\InvoiceSum;
use App\Models\RecurringInvoice;
use App\Models\RecurringInvoiceInvitation;
/**
* RecurringInvoiceRepository.
@ -38,4 +39,9 @@ class RecurringInvoiceRepository extends BaseRepository
return $invoice;
}
public function getInvitationByKey($key) :?RecurringInvoiceInvitation
{
return RecurringInvoiceInvitation::whereRaw('BINARY `key`= ?', [$key])->first();
}
}

View File

@ -0,0 +1,60 @@
<?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://opensource.org/licenses/AAL
*/
namespace App\Services\Recurring;
use App\Jobs\Entity\CreateEntityPdf;
use App\Models\ClientContact;
use App\Models\Invoice;
use App\Services\AbstractService;
use App\Utils\TempFile;
use Illuminate\Support\Facades\Storage;
class GetInvoicePdf extends AbstractService
{
public $entity;
public function __construct($entity, ClientContact $contact = null)
{
$this->entity = $entity;
$this->contact = $contact;
}
public function run()
{
if (! $this->contact) {
$this->contact = $this->entity->client->primary_contact()->first();
}
$invitation = $this->entity->invitations->where('client_contact_id', $this->contact->id)->first();
$path = $this->entity->client->recurring_invoice_filepath();
$file_path = $path.$this->entity->hashed_id.'.pdf';
$disk = config('filesystems.default');
$file = Storage::disk($disk)->exists($file_path);
if (! $file) {
$file_path = CreateEntityPdf::dispatchNow($invitation);
}
/* Copy from remote disk to local when using cloud file storage. */
if(config('filesystems.default') == 's3')
return TempFile::path(Storage::disk($disk)->url($file_path));
// return Storage::disk($disk)->url($file_path);
return Storage::disk($disk)->path($file_path);
}
}

View File

@ -12,6 +12,7 @@
namespace App\Services\Recurring;
use App\Models\RecurringInvoice;
use App\Services\Recurring\GetInvoicePdf;
use Illuminate\Support\Carbon;
class RecurringService
@ -77,6 +78,11 @@ class RecurringService
return $this;
}
public function getInvoicePdf($contact = null)
{
return (new GetInvoicePdf($this->recurring_entity, $contact))->run();
}
public function save()
{
$this->recurring_entity->save();

View File

@ -59,7 +59,7 @@ class TaskTransformer extends EntityTransformer
'project_id' => $this->encodePrimaryKey($task->project_id) ?: '',
'is_deleted' => (bool) $task->is_deleted,
'time_log' => $task->time_log ?: '',
'is_running' => (bool) $task->is_running,
'is_running' => (bool) $task->is_running, //@deprecate
'custom_value1' => $task->custom_value1 ?: '',
'custom_value2' => $task->custom_value2 ?: '',
'custom_value3' => $task->custom_value3 ?: '',

View File

@ -118,7 +118,7 @@ class HtmlEngine
$data['$quote.datetime'] = &$data['$entity.datetime'];
$data['$credit.datetime'] = &$data['$entity.datetime'];
if ($this->entity_string == 'invoice') {
if ($this->entity_string == 'invoice' || $this->entity_string == 'recurring_invoice') {
$data['$entity'] = ['value' => '', 'label' => ctrans('texts.invoice')];
$data['$number'] = ['value' => $this->entity->number ?: '&nbsp;', 'label' => ctrans('texts.invoice_number')];
$data['$entity.terms'] = ['value' => $this->entity->terms ?: '&nbsp;', 'label' => ctrans('texts.invoice_terms')];

View File

@ -3259,7 +3259,7 @@ return [
'enable_only_for_development' => 'Enable only for development',
'test_pdf' => 'Test PDF',
'status_cancelled' => 'Cancelled',
'cancelled' => 'Cancelled',
'checkout_authorize_label' => 'Checkout.com can be can saved as payment method for future use, once you complete your first transaction. Don\'t forget to check "Store credit card details" during payment process.',

View File

@ -80,6 +80,7 @@ Route::group(['middleware' => ['invite_db'], 'prefix' => 'client', 'as' => 'clie
/*Invitation catches*/
Route::get('recurring_invoice/{invitation_key}', 'ClientPortal\InvitationController@recurringRouter');
Route::get('{entity}/{invitation_key}', 'ClientPortal\InvitationController@router');
Route::get('recurring_invoice/{invitation_key}/download_pdf', 'RecurringInvoiceController@downloadPdf')->name('recurring_invoice.download_invitation_key');
Route::get('invoice/{invitation_key}/download_pdf', 'InvoiceController@downloadPdf')->name('invoice.download_invitation_key');
Route::get('quote/{invitation_key}/download_pdf', 'QuoteController@downloadPdf')->name('quote.download_invitation_key');
Route::get('credit/{invitation_key}/download_pdf', 'CreditController@downloadPdf')->name('credit.download_invitation_key');