From c7bad23909761dcaedd5d0193bc0574d4de8bc1c Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 21 Feb 2025 13:28:47 +1100 Subject: [PATCH] Add locations to mini/first load --- app/Http/Controllers/BaseController.php | 8 ++++++ app/Models/Credit.php | 5 ++++ app/Models/Invoice.php | 5 ++++ app/Models/PurchaseOrder.php | 5 ++++ app/Models/Quote.php | 5 ++++ app/Models/RecurringInvoice.php | 5 ++++ app/Transformers/CreditTransformer.php | 13 +++++++++ app/Transformers/InvoiceTransformer.php | 13 +++++++++ app/Transformers/PurchaseOrderTransformer.php | 1 + app/Transformers/QuoteTransformer.php | 14 ++++++++++ .../RecurringInvoiceTransformer.php | 13 +++++++++ ...25_02_20_224129_entity_location_schema.php | 27 ++++++++++++++++--- 12 files changed, 110 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/BaseController.php b/app/Http/Controllers/BaseController.php index 289ad3d553..5ed88c0ba0 100644 --- a/app/Http/Controllers/BaseController.php +++ b/app/Http/Controllers/BaseController.php @@ -154,6 +154,7 @@ class BaseController extends Controller 'company.bank_transactions', 'company.bank_transaction_rules', 'company.task_schedulers', + 'company.locations', ]; /** @@ -178,6 +179,7 @@ class BaseController extends Controller 'company.bank_integrations', 'company.bank_transaction_rules', 'company.task_schedulers', + 'company.locations', ]; /** @@ -525,6 +527,9 @@ class BaseController extends Controller 'company.task_schedulers' => function ($query) { $query->whereNotNull('updated_at'); }, + 'company.locations' => function ($query) { + $query->whereNotNull('updated_at'); + }, ] ); @@ -630,6 +635,9 @@ class BaseController extends Controller $query->where('schedulers.user_id', $user->id); } }, + 'company.locations' => function ($query) use ($created_at) { + $query->where('created_at', '>=', $created_at); + }, ] ); diff --git a/app/Models/Credit.php b/app/Models/Credit.php index 0a0d05fdbc..046eff8986 100644 --- a/app/Models/Credit.php +++ b/app/Models/Credit.php @@ -252,6 +252,11 @@ class Credit extends BaseModel return $this->belongsTo(User::class, 'assigned_user_id', 'id')->withTrashed(); } + public function location(): \Illuminate\Database\Eloquent\Relations\BelongsTo + { + return $this->belongsTo(Location::class)->withTrashed(); + } + public function vendor(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Vendor::class); diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 73ca947925..3dfdc13e6d 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -319,6 +319,11 @@ class Invoice extends BaseModel return $this->belongsTo(User::class)->withTrashed(); } + public function location(): \Illuminate\Database\Eloquent\Relations\BelongsTo + { + return $this->belongsTo(Location::class)->withTrashed(); + } + public function recurring_invoice(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(RecurringInvoice::class, 'recurring_id', 'id')->withTrashed(); diff --git a/app/Models/PurchaseOrder.php b/app/Models/PurchaseOrder.php index e24165e8b5..ab14978fcf 100644 --- a/app/Models/PurchaseOrder.php +++ b/app/Models/PurchaseOrder.php @@ -307,6 +307,11 @@ class PurchaseOrder extends BaseModel return $this->belongsTo(User::class)->withTrashed(); } + public function location(): \Illuminate\Database\Eloquent\Relations\BelongsTo + { + return $this->belongsTo(Location::class)->withTrashed(); + } + public function client(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Client::class)->withTrashed(); diff --git a/app/Models/Quote.php b/app/Models/Quote.php index 5694ab9738..a5745bdc5c 100644 --- a/app/Models/Quote.php +++ b/app/Models/Quote.php @@ -268,6 +268,11 @@ class Quote extends BaseModel return $this->belongsTo(User::class)->withTrashed(); } + public function location(): \Illuminate\Database\Eloquent\Relations\BelongsTo + { + return $this->belongsTo(Location::class)->withTrashed(); + } + public function client(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Client::class)->withTrashed(); diff --git a/app/Models/RecurringInvoice.php b/app/Models/RecurringInvoice.php index f4211a7b2a..e5a89bf8f5 100644 --- a/app/Models/RecurringInvoice.php +++ b/app/Models/RecurringInvoice.php @@ -362,6 +362,11 @@ class RecurringInvoice extends BaseModel return $this->belongsTo(User::class)->withTrashed(); } + public function location(): \Illuminate\Database\Eloquent\Relations\BelongsTo + { + return $this->belongsTo(Location::class)->withTrashed(); + } + public function assigned_user(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(User::class, 'assigned_user_id', 'id')->withTrashed(); diff --git a/app/Transformers/CreditTransformer.php b/app/Transformers/CreditTransformer.php index a044c9d580..7eacec0fb0 100644 --- a/app/Transformers/CreditTransformer.php +++ b/app/Transformers/CreditTransformer.php @@ -32,8 +32,20 @@ class CreditTransformer extends EntityTransformer protected array $availableIncludes = [ 'activities', 'client', + 'location', ]; + public function includeLocation(Credit $credit) + { + $transformer = new LocationTransformer($this->serializer); + + if (!$credit->location) { + return null; + } + + return $this->includeItem($credit->location, $transformer, \App\Models\Location::class); + } + public function includeActivities(Credit $credit) { $transformer = new ActivityTransformer($this->serializer); @@ -134,6 +146,7 @@ class CreditTransformer extends EntityTransformer 'invoice_id' => $credit->invoice_id ? $this->encodePrimaryKey($credit->invoice_id) : '', 'tax_info' => $credit->tax_data ?: new \stdClass(), 'e_invoice' => $credit->e_invoice ?: new \stdClass(), + 'location_id' => $this->encodePrimaryKey($credit->location_id), ]; } diff --git a/app/Transformers/InvoiceTransformer.php b/app/Transformers/InvoiceTransformer.php index 4697dc280c..87c56c1f7c 100644 --- a/app/Transformers/InvoiceTransformer.php +++ b/app/Transformers/InvoiceTransformer.php @@ -34,8 +34,20 @@ class InvoiceTransformer extends EntityTransformer 'payments', 'client', 'activities', + 'location', ]; + public function includeLocation(Invoice $invoice) + { + $transformer = new LocationTransformer($this->serializer); + + if (!$invoice->location) { + return null; + } + + return $this->includeItem($invoice->location, $transformer, \App\Models\Location::class); + } + public function includeInvitations(Invoice $invoice) { $transformer = new InvoiceInvitationTransformer($this->serializer); @@ -160,6 +172,7 @@ class InvoiceTransformer extends EntityTransformer 'tax_info' => $invoice->tax_data ?: new \stdClass(), 'e_invoice' => $invoice->e_invoice ?: new \stdClass(), 'backup' => $invoice->backup ?: new \stdClass(), + 'location_id' => $this->encodePrimaryKey($invoice->location_id), ]; if (request()->has('reminder_schedule') && request()->query('reminder_schedule') == 'true') { diff --git a/app/Transformers/PurchaseOrderTransformer.php b/app/Transformers/PurchaseOrderTransformer.php index 8867a66297..4604af5b3d 100644 --- a/app/Transformers/PurchaseOrderTransformer.php +++ b/app/Transformers/PurchaseOrderTransformer.php @@ -151,6 +151,7 @@ class PurchaseOrderTransformer extends EntityTransformer 'currency_id' => $purchase_order->currency_id ? (string) $purchase_order->currency_id : '', 'tax_info' => $purchase_order->tax_data ?: new \stdClass(), 'e_invoice' => $purchase_order->e_invoice ?: new \stdClass(), + 'location_id' => $this->encodePrimaryKey($purchase_order->location_id), ]; } diff --git a/app/Transformers/QuoteTransformer.php b/app/Transformers/QuoteTransformer.php index ae23f921f9..f1d9317cdd 100644 --- a/app/Transformers/QuoteTransformer.php +++ b/app/Transformers/QuoteTransformer.php @@ -32,8 +32,21 @@ class QuoteTransformer extends EntityTransformer protected array $availableIncludes = [ 'activities', 'client', + 'location', ]; + + public function includeLocation(Quote $quote) + { + $transformer = new LocationTransformer($this->serializer); + + if (!$quote->location) { + return null; + } + + return $this->includeItem($quote->location, $transformer, \App\Models\Location::class); + } + public function includeActivities(Quote $quote) { $transformer = new ActivityTransformer($this->serializer); @@ -150,6 +163,7 @@ class QuoteTransformer extends EntityTransformer 'subscription_id' => $this->encodePrimaryKey($quote->subscription_id), 'tax_info' => $quote->tax_data ?: new \stdClass(), 'e_invoice' => $quote->e_invoice ?: new \stdClass(), + 'location_id' => $this->encodePrimaryKey($quote->location_id), ]; } diff --git a/app/Transformers/RecurringInvoiceTransformer.php b/app/Transformers/RecurringInvoiceTransformer.php index 4d653e5fce..1fc60cc09f 100644 --- a/app/Transformers/RecurringInvoiceTransformer.php +++ b/app/Transformers/RecurringInvoiceTransformer.php @@ -31,8 +31,20 @@ class RecurringInvoiceTransformer extends EntityTransformer protected array $availableIncludes = [ 'activities', 'client', + 'location', ]; + public function includeLocation(RecurringInvoice $invoice) + { + $transformer = new LocationTransformer($this->serializer); + + if (!$invoice->location) { + return null; + } + + return $this->includeItem($invoice->location, $transformer, \App\Models\Location::class); + } + public function includeHistory(RecurringInvoice $invoice) { $transformer = new InvoiceHistoryTransformer($this->serializer); @@ -134,6 +146,7 @@ class RecurringInvoiceTransformer extends EntityTransformer 'paid_to_date' => (float) $invoice->paid_to_date, 'subscription_id' => (string) $this->encodePrimaryKey($invoice->subscription_id), 'e_invoice' => $invoice->e_invoice ?: new \stdClass(), + 'location_id' => $this->encodePrimaryKey($invoice->location_id), ]; if (request()->has('show_dates') && request()->query('show_dates') == 'true') { diff --git a/database/migrations/2025_02_20_224129_entity_location_schema.php b/database/migrations/2025_02_20_224129_entity_location_schema.php index 4512d879a8..0a91d03b4e 100644 --- a/database/migrations/2025_02_20_224129_entity_location_schema.php +++ b/database/migrations/2025_02_20_224129_entity_location_schema.php @@ -40,11 +40,30 @@ return new class extends Migration $table->foreign('vendor_id')->references('id')->on('vendors')->onDelete('cascade')->onUpdate('cascade'); $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade'); }); - } - /** - * Reverse the migrations. - */ + Schema::table('invoices', function (Blueprint $table){ + + $table->foreignId('location_id')->nullable()->constrained()->cascadeOnDelete(); + }); + + Schema::table('recurring_invoices', function (Blueprint $table) { + $table->foreignId('location_id')->nullable()->constrained()->cascadeOnDelete(); + }); + + Schema::table('credits', function (Blueprint $table) { + $table->foreignId('location_id')->nullable()->constrained()->cascadeOnDelete(); + }); + + Schema::table('quotes', function (Blueprint $table) { + $table->foreignId('location_id')->nullable()->constrained()->cascadeOnDelete(); + }); + + Schema::table('purchase_orders', function (Blueprint $table) { + $table->foreignId('location_id')->nullable()->constrained()->cascadeOnDelete(); + }); + + } + public function down(): void { //