You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2023_09_03_100000_create_accounting_tables.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. use App\Enums\BankAccountType;
  3. use Illuminate\Database\Migrations\Migration;
  4. use Illuminate\Database\Schema\Blueprint;
  5. use Illuminate\Support\Facades\Schema;
  6. return new class extends Migration
  7. {
  8. /**
  9. * Run the migrations.
  10. */
  11. public function up(): void
  12. {
  13. Schema::create('institutions', function (Blueprint $table) {
  14. $table->id();
  15. $table->string('external_institution_id')->nullable(); // Plaid
  16. $table->string('name')->index();
  17. $table->string('logo')->nullable();
  18. $table->string('website')->nullable();
  19. $table->string('phone', 20)->nullable();
  20. $table->text('address')->nullable();
  21. $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
  22. $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
  23. $table->timestamps();
  24. });
  25. Schema::create('account_subtypes', function (Blueprint $table) {
  26. $table->id();
  27. $table->foreignId('company_id')->constrained()->cascadeOnDelete();
  28. $table->boolean('multi_currency')->default(false);
  29. $table->string('category');
  30. $table->string('type');
  31. $table->string('name');
  32. $table->text('description')->nullable();
  33. $table->timestamps();
  34. });
  35. Schema::create('accounts', function (Blueprint $table) {
  36. $table->id();
  37. $table->foreignId('company_id')->constrained()->cascadeOnDelete();
  38. $table->foreignId('subtype_id')->nullable()->constrained('account_subtypes')->nullOnDelete();
  39. $table->foreignId('parent_id')->nullable()->constrained('accounts')->nullOnDelete();
  40. $table->string('category')->nullable();
  41. $table->string('type')->nullable();
  42. $table->string('code')->nullable()->index();
  43. $table->string('name')->nullable()->index();
  44. $table->string('currency_code')->nullable();
  45. $table->bigInteger('starting_balance')->default(0);
  46. $table->bigInteger('debit_balance')->default(0);
  47. $table->bigInteger('credit_balance')->default(0);
  48. $table->bigInteger('net_movement')->default(0);
  49. $table->bigInteger('ending_balance')->default(0);
  50. $table->text('description')->nullable();
  51. $table->boolean('active')->default(true);
  52. $table->boolean('default')->default(false);
  53. $table->unsignedBigInteger('accountable_id')->nullable();
  54. $table->string('accountable_type')->nullable();
  55. $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
  56. $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
  57. $table->timestamps();
  58. $table->unique(['company_id', 'code']);
  59. });
  60. Schema::create('bank_accounts', function (Blueprint $table) {
  61. $table->id();
  62. $table->foreignId('company_id')->constrained()->cascadeOnDelete();
  63. $table->foreignId('institution_id')->nullable()->constrained('institutions')->nullOnDelete();
  64. $table->string('type')->default(BankAccountType::DEFAULT);
  65. $table->string('number', 20);
  66. $table->boolean('enabled')->default(true);
  67. $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
  68. $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
  69. $table->timestamps();
  70. });
  71. Schema::create('connected_bank_accounts', function (Blueprint $table) {
  72. $table->id();
  73. $table->foreignId('company_id')->constrained()->cascadeOnDelete();
  74. $table->foreignId('institution_id')->nullable()->constrained('institutions')->nullOnDelete();
  75. $table->foreignId('bank_account_id')->nullable()->constrained('bank_accounts')->nullOnDelete();
  76. $table->string('external_account_id')->nullable();
  77. $table->text('access_token')->nullable();
  78. $table->string('identifier')->unique()->nullable(); // Plaid
  79. $table->string('item_id')->nullable();
  80. $table->string('name');
  81. $table->string('mask');
  82. $table->string('type')->default(BankAccountType::DEFAULT);
  83. $table->string('subtype')->nullable();
  84. $table->boolean('import_transactions')->default(false);
  85. $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
  86. $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
  87. $table->timestamps();
  88. });
  89. }
  90. /**
  91. * Reverse the migrations.
  92. */
  93. public function down(): void
  94. {
  95. Schema::dropIfExists('institutions');
  96. Schema::dropIfExists('account_subtypes');
  97. Schema::dropIfExists('accounts');
  98. Schema::dropIfExists('bank_accounts');
  99. Schema::dropIfExists('connected_bank_accounts');
  100. }
  101. };