From b157ee39173754f2852e045544b5daf7081ad0d3 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 4 Mar 2020 10:51:50 +1100 Subject: [PATCH] Fixes for Credit PDF generation (#3417) * Fix for design GET route * Fixes for Credit PDF creation --- app/Filters/DesignFilters.php | 3 +- app/Http/Controllers/BaseController.php | 3 +- app/Models/Credit.php | 8 ++++- app/Models/Design.php | 1 + app/Models/Presenters/CreditPresenter.php | 21 ++++++++++++ app/Models/Presenters/EntityPresenter.php | 38 +++++++++++++++++++++- app/Models/Presenters/InvoicePresenter.php | 34 ------------------- app/Models/Presenters/QuotePresenter.php | 33 ------------------- app/Services/Credit/CreditService.php | 2 +- app/Services/Credit/MarkSent.php | 19 ++++++----- app/Utils/Traits/MakesInvoiceValues.php | 5 ++- tests/Integration/DesignTest.php | 33 +++++++++++++++++++ tests/MockAccountData.php | 7 ++++ 13 files changed, 125 insertions(+), 82 deletions(-) create mode 100644 app/Models/Presenters/CreditPresenter.php diff --git a/app/Filters/DesignFilters.php b/app/Filters/DesignFilters.php index 058979bfac83..e3e83f3adc3f 100644 --- a/app/Filters/DesignFilters.php +++ b/app/Filters/DesignFilters.php @@ -137,8 +137,7 @@ class DesignFilters extends QueryFilters */ public function entityFilter() { - //return $this->builder->whereCompanyId(auth()->user()->company()->id); - return $this->builder->company(); + return $this->builder->whereCompanyId(auth()->user()->company()->id)->orWhere('company_id',null); } } diff --git a/app/Http/Controllers/BaseController.php b/app/Http/Controllers/BaseController.php index a79649d8043a..d5fb7199cd8b 100644 --- a/app/Http/Controllers/BaseController.php +++ b/app/Http/Controllers/BaseController.php @@ -12,6 +12,7 @@ namespace App\Http\Controllers; use App\Models\Company; +use App\Models\Design; use App\Models\User; use App\Transformers\ArraySerializer; use App\Transformers\EntityTransformer; @@ -150,7 +151,7 @@ class BaseController extends Controller $query->with($includes); if (auth()->user()->cannot('view_'.$this->entity_type)) { - if ($this->entity_type == Company::class) { + if ($this->entity_type == Company::class || $this->entity_type == Design::class ) { //no user keys exist on the company table, so we need to skip } elseif ($this->entity_type == User::class) { $query->where('id', '=', auth()->user()->id); diff --git a/app/Models/Credit.php b/app/Models/Credit.php index c4ac741557c1..57eda39ac43d 100644 --- a/app/Models/Credit.php +++ b/app/Models/Credit.php @@ -18,9 +18,11 @@ use App\Models\Filterable; use App\Services\Credit\CreditService; use App\Utils\Traits\MakesDates; use App\Utils\Traits\MakesHash; +use App\Utils\Traits\MakesInvoiceValues; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Carbon; +use Laracasts\Presenter\PresentableTrait; class Credit extends BaseModel { @@ -28,7 +30,11 @@ class Credit extends BaseModel use Filterable; use MakesDates; use SoftDeletes; - + use PresentableTrait; + use MakesInvoiceValues; + + protected $presenter = 'App\Models\Presenters\CreditPresenter'; + protected $fillable = [ 'number', 'discount', diff --git a/app/Models/Design.php b/app/Models/Design.php index 23733ebd62ea..72ed2794be24 100644 --- a/app/Models/Design.php +++ b/app/Models/Design.php @@ -40,4 +40,5 @@ class Design extends BaseModel } + } \ No newline at end of file diff --git a/app/Models/Presenters/CreditPresenter.php b/app/Models/Presenters/CreditPresenter.php new file mode 100644 index 000000000000..9959069cee8f --- /dev/null +++ b/app/Models/Presenters/CreditPresenter.php @@ -0,0 +1,21 @@ +client->present()->name(); + } + + public function address() + { + return $this->client->present()->address(); + } + + public function shippingAddress() + { + return $this->client->present()->shipping_address(); + } + + public function companyLogo() + { + return $this->company->logo; + } + + public function clientLogo() + { + return $this->client->logo; + } + + public function companyName() + { + return $this->company->present()->name(); + } + + public function companyAddress() + { + return $this->company->present()->address(); + } + +} \ No newline at end of file diff --git a/app/Models/Presenters/InvoicePresenter.php b/app/Models/Presenters/InvoicePresenter.php index 6bcca5103fc2..48cfd5692def 100644 --- a/app/Models/Presenters/InvoicePresenter.php +++ b/app/Models/Presenters/InvoicePresenter.php @@ -45,38 +45,4 @@ class InvoicePresenter extends EntityPresenter } } - public function clientName() - { - return $this->client->present()->name(); - } - - public function address() - { - return $this->client->present()->address(); - } - - public function shippingAddress() - { - return $this->client->present()->shipping_address(); - } - - public function companyLogo() - { - return $this->company->logo; - } - - public function clientLogo() - { - return $this->client->logo; - } - - public function companyName() - { - return $this->company->present()->name(); - } - - public function companyAddress() - { - return $this->company->present()->address(); - } } diff --git a/app/Models/Presenters/QuotePresenter.php b/app/Models/Presenters/QuotePresenter.php index b0b13c7c4ffe..60690be2d0c3 100644 --- a/app/Models/Presenters/QuotePresenter.php +++ b/app/Models/Presenters/QuotePresenter.php @@ -45,38 +45,5 @@ class QuotePresenter extends EntityPresenter } } - public function clientName() - { - return $this->client->present()->name(); - } - public function address() - { - return $this->client->present()->address(); - } - - public function shippingAddress() - { - return $this->client->present()->shipping_address(); - } - - public function companyLogo() - { - return $this->company->logo; - } - - public function clientLogo() - { - return $this->client->logo; - } - - public function companyName() - { - return $this->company->present()->name(); - } - - public function companyAddress() - { - return $this->company->present()->address(); - } } diff --git a/app/Services/Credit/CreditService.php b/app/Services/Credit/CreditService.php index 5cc3c3f8cb7a..c53e8af726ac 100644 --- a/app/Services/Credit/CreditService.php +++ b/app/Services/Credit/CreditService.php @@ -47,7 +47,7 @@ class CreditService public function markSent() { - $this->credit = (new MarkSent($this->credit->client))->run($this->credit); + $this->credit = (new MarkSent($this->credit->client, $this->credit))->run(); return $this; } diff --git a/app/Services/Credit/MarkSent.php b/app/Services/Credit/MarkSent.php index da9900ab4f32..d6169b547505 100644 --- a/app/Services/Credit/MarkSent.php +++ b/app/Services/Credit/MarkSent.php @@ -9,26 +9,29 @@ class MarkSent { private $client; - public function __construct($client) + private $credit; + + public function __construct($client, $credit) { $this->client = $client; + $this->credit = $credit; } - public function run($credit) + public function run() { /* Return immediately if status is not draft */ - if ($credit->status_id != Credit::STATUS_DRAFT) { - return $credit; + if ($this->credit->status_id != Credit::STATUS_DRAFT) { + return $this->credit; } - $credit->markInvitationsSent(); + $this->credit->markInvitationsSent(); - event(new CreditWasMarkedSent($credit, $credit->company)); + event(new CreditWasMarkedSent($this->credit, $this->credit->company)); - $credit->service()->setStatus(Credit::STATUS_SENT)->applyNumber()->save(); + $this->credit->service()->setStatus(Credit::STATUS_SENT)->applyNumber()->save(); - return $credit; + return $this->credit; } } diff --git a/app/Utils/Traits/MakesInvoiceValues.php b/app/Utils/Traits/MakesInvoiceValues.php index 93fd1833cd57..f8c0ac354e37 100644 --- a/app/Utils/Traits/MakesInvoiceValues.php +++ b/app/Utils/Traits/MakesInvoiceValues.php @@ -344,6 +344,9 @@ trait MakesInvoiceValues $data['$credit_amount'] = &$data['$total']; $data['$credit_balance'] = &$data['$balance']; $data['$credit.amount'] = &$data['$total']; + $data['$credit_number'] = &$data['$number']; + $data['$credit_no'] = &$data['$number']; + $data['$credit.credit_no'] = &$data['$number']; // $data['$invoice_issued_to'] = ; // $data['$quote_issued_to'] = ; @@ -355,6 +358,7 @@ trait MakesInvoiceValues // $data['$quote_to'] = ; // $data['$details'] = ; $data['$invoice_no'] = $this->number ?: ' '; + $data['$invoice.invoice_no'] = &$data['$invoice_no']; $data['$client1'] = $this->client->custom_value1 ?: ' '; $data['$client2'] = $this->client->custom_value2 ?: ' '; @@ -432,7 +436,6 @@ trait MakesInvoiceValues $data['$statement_to'] = ; $data['$credit_note'] = ; $data['$credit_date'] = ; - $data['$credit_number'] = ; $data['$credit_issued_to'] = ; $data['$credit_to'] = ; $data['$your_credit'] = ; diff --git a/tests/Integration/DesignTest.php b/tests/Integration/DesignTest.php index 4519ab79843c..e5120fb780ae 100644 --- a/tests/Integration/DesignTest.php +++ b/tests/Integration/DesignTest.php @@ -4,6 +4,7 @@ namespace Tests\Integration; use App\Designs\Designer; use App\Designs\Modern; +use App\Jobs\Credit\CreateCreditPdf; use App\Jobs\Invoice\CreateInvoicePdf; use App\Jobs\Quote\CreateQuotePdf; use App\Utils\Traits\GeneratesCounter; @@ -71,12 +72,40 @@ class DesignTest extends TestCase $settings = $this->invoice->client->settings; $settings->quote_design_id = "6"; + $this->quote->client_id = $this->client->id; + $this->quote->setRelation('client', $this->client); + $this->quote->save(); + $this->client->settings = $settings; $this->client->save(); CreateQuotePdf::dispatchNow($this->quote, $this->quote->company, $this->quote->client->primary_contact()->first()); } + public function testCreditDesignExists() + { + + $modern = new Modern(); + + $designer = new Designer($modern, $this->company->settings->pdf_variables, 'credit'); + + $html = $designer->build($this->credit)->getHtml(); + + $this->assertNotNull($html); + + $settings = $this->invoice->client->settings; + $settings->quote_design_id = "6"; + + $this->credit->client_id = $this->client->id; + $this->credit->setRelation('client', $this->client); + $this->credit->save(); + + $this->client->settings = $settings; + $this->client->save(); + + CreateCreditPdf::dispatchNow($this->credit, $this->credit->company, $this->credit->client->primary_contact()->first()); + } + public function testAllDesigns() { @@ -85,6 +114,10 @@ class DesignTest extends TestCase $settings = $this->invoice->client->settings; $settings->quote_design_id = (string)$x; + + $this->quote->client_id = $this->client->id; + $this->quote->setRelation('client', $this->client); + $this->quote->save(); $this->client->settings = $settings; $this->client->save(); diff --git a/tests/MockAccountData.php b/tests/MockAccountData.php index 7fc257898fa5..d49fa405ac46 100644 --- a/tests/MockAccountData.php +++ b/tests/MockAccountData.php @@ -248,6 +248,13 @@ trait MockAccountData $this->credit->save(); + + $this->credit_calc = new InvoiceSum($this->credit); + $this->credit_calc->build(); + + $this->credit = $this->credit_calc->getCredit(); + $this->credit->service()->markSent(); + $contacts = $this->invoice->client->contacts; $contacts->each(function ($contact) {