Explorar o código

feat(receivables): adiciona tabelas de Contas a Receber dos franqueados

- franchisee_account_receives: titulo (valor total, vencimento, status, asaas_id)
- franchisee_account_receive_details: 3 linhas por titulo
  (Royalties / FNM / Taxa Manutencao)
- asaas_id permanece NULL ate a integracao com Asaas

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
ebagabee hai 3 semanas
pai
achega
5cc68040d8

+ 42 - 0
app/Http/Resources/FranchiseeAccountReceiveResource.php

@@ -0,0 +1,42 @@
+<?php
+
+namespace App\Http\Resources;
+
+use Carbon\Carbon;
+use Illuminate\Http\Request;
+use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
+use Illuminate\Http\Resources\Json\JsonResource;
+
+class FranchiseeAccountReceiveResource extends JsonResource
+{
+    public function toArray(Request $request): array
+    {
+        return [
+            'id'                 => $this->id,
+            'unit_id'            => $this->unit_id,
+            'tbr_calculation_id' => $this->tbr_calculation_id,
+            'order'              => $this->order,
+            'history'            => $this->history,
+            'value'              => $this->value,
+            'paid_value'         => $this->paid_value,
+            'due_date'           => $this->due_date?->format('Y-m-d'),
+            'discount'           => $this->discount,
+            'fees'               => $this->fees,
+            'obs'                => $this->obs,
+            'asaas_id'           => $this->asaas_id,
+            'status'             => $this->status,
+            'details'            => $this->whenLoaded('details', fn () => $this->details->map(fn ($d) => [
+                'id'      => $d->id,
+                'value'   => $d->value,
+                'history' => $d->history,
+            ])),
+            'created_at'         => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
+            'updated_at'         => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
+        ];
+    }
+
+    public static function collection($resource): AnonymousResourceCollection
+    {
+        return parent::collection($resource);
+    }
+}

+ 43 - 0
app/Models/FranchiseeAccountReceive.php

@@ -0,0 +1,43 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
+use Illuminate\Database\Eloquent\Relations\HasMany;
+use Illuminate\Database\Eloquent\SoftDeletes;
+
+class FranchiseeAccountReceive extends Model
+{
+    use HasFactory, SoftDeletes;
+
+    protected $table = 'franchisee_account_receives';
+
+    protected $guarded = ['id'];
+
+    protected $casts = [
+        'value'      => 'decimal:2',
+        'paid_value' => 'decimal:2',
+        'discount'   => 'decimal:2',
+        'fees'       => 'decimal:2',
+        'due_date'   => 'date',
+        'created_at' => 'datetime',
+        'updated_at' => 'datetime',
+    ];
+
+    public function unit(): BelongsTo
+    {
+        return $this->belongsTo(Unit::class, 'unit_id');
+    }
+
+    public function tbrCalculation(): BelongsTo
+    {
+        return $this->belongsTo(TbrCalculation::class, 'tbr_calculation_id');
+    }
+
+    public function details(): HasMany
+    {
+        return $this->hasMany(FranchiseeAccountReceiveDetail::class, 'franchisee_account_receive_id');
+    }
+}

+ 27 - 0
app/Models/FranchiseeAccountReceiveDetail.php

@@ -0,0 +1,27 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
+
+class FranchiseeAccountReceiveDetail extends Model
+{
+    use HasFactory;
+
+    protected $table = 'franchisee_account_receive_details';
+
+    protected $guarded = ['id'];
+
+    protected $casts = [
+        'value'      => 'decimal:2',
+        'created_at' => 'datetime',
+        'updated_at' => 'datetime',
+    ];
+
+    public function accountReceive(): BelongsTo
+    {
+        return $this->belongsTo(FranchiseeAccountReceive::class, 'franchisee_account_receive_id');
+    }
+}

+ 38 - 0
database/migrations/2026_05_21_100200_create_franchisee_account_receives_table.php

@@ -0,0 +1,38 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    public function up(): void
+    {
+        Schema::create('franchisee_account_receives', function (Blueprint $table) {
+            $table->id();
+            $table->foreignId('unit_id')->constrained('units');
+            $table->foreignId('tbr_calculation_id')->constrained('tbr_calculations');
+            $table->unsignedInteger('order')->nullable();
+            $table->string('history', 255);
+            $table->decimal('value', 10, 2);
+            $table->decimal('paid_value', 10, 2)->default(0);
+            $table->date('due_date');
+            $table->decimal('discount', 10, 2)->default(0);
+            $table->decimal('fees', 10, 2)->default(0);
+            $table->string('obs', 255)->nullable();
+            $table->string('asaas_id', 100)->nullable();
+            $table->string('status', 30)->default('pending');
+            $table->timestamps();
+            $table->softDeletes();
+
+            $table->index('unit_id');
+            $table->index('tbr_calculation_id');
+            $table->index('due_date');
+        });
+    }
+
+    public function down(): void
+    {
+        Schema::dropIfExists('franchisee_account_receives');
+    }
+};

+ 28 - 0
database/migrations/2026_05_21_100300_create_franchisee_account_receive_details_table.php

@@ -0,0 +1,28 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    public function up(): void
+    {
+        Schema::create('franchisee_account_receive_details', function (Blueprint $table) {
+            $table->id();
+            $table->foreignId('franchisee_account_receive_id')
+                ->constrained('franchisee_account_receives')
+                ->cascadeOnDelete();
+            $table->decimal('value', 10, 2);
+            $table->string('history', 255);
+            $table->timestamps();
+
+            $table->index('franchisee_account_receive_id', 'far_details_far_id_index');
+        });
+    }
+
+    public function down(): void
+    {
+        Schema::dropIfExists('franchisee_account_receive_details');
+    }
+};