Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Transaction.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace App\Models\Accounting;
  3. use App\Concerns\Blamable;
  4. use App\Concerns\CompanyOwned;
  5. use App\Enums\Accounting\AccountCategory;
  6. use App\Enums\Accounting\AccountType;
  7. use App\Enums\Accounting\PaymentMethod;
  8. use App\Enums\Accounting\TransactionType;
  9. use App\Filament\Company\Resources\Accounting\TransactionResource\Pages\ViewTransaction;
  10. use App\Filament\Company\Resources\Purchases\BillResource\Pages\ViewBill;
  11. use App\Filament\Company\Resources\Sales\InvoiceResource\Pages\ViewInvoice;
  12. use App\Models\Banking\BankAccount;
  13. use App\Models\Common\Client;
  14. use App\Models\Common\Contact;
  15. use App\Models\Common\Vendor;
  16. use App\Observers\TransactionObserver;
  17. use Database\Factories\Accounting\TransactionFactory;
  18. use Illuminate\Database\Eloquent\Attributes\ObservedBy;
  19. use Illuminate\Database\Eloquent\Builder;
  20. use Illuminate\Database\Eloquent\Factories\Factory;
  21. use Illuminate\Database\Eloquent\Factories\HasFactory;
  22. use Illuminate\Database\Eloquent\Model;
  23. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  24. use Illuminate\Database\Eloquent\Relations\HasMany;
  25. use Illuminate\Database\Eloquent\Relations\MorphTo;
  26. use Illuminate\Support\Collection;
  27. #[ObservedBy(TransactionObserver::class)]
  28. class Transaction extends Model
  29. {
  30. use Blamable;
  31. use CompanyOwned;
  32. use HasFactory;
  33. protected $fillable = [
  34. 'company_id',
  35. 'account_id', // Account from Chart of Accounts (Income/Expense accounts)
  36. 'bank_account_id', // Cash/Bank Account
  37. 'plaid_transaction_id',
  38. 'contact_id',
  39. 'type', // 'deposit', 'withdrawal', 'journal'
  40. 'payment_channel',
  41. 'payment_method',
  42. 'is_payment',
  43. 'description',
  44. 'notes',
  45. 'reference',
  46. 'amount',
  47. 'pending',
  48. 'reviewed',
  49. 'posted_at',
  50. 'created_by',
  51. 'updated_by',
  52. 'meta',
  53. ];
  54. protected $casts = [
  55. 'type' => TransactionType::class,
  56. 'payment_method' => PaymentMethod::class,
  57. 'pending' => 'boolean',
  58. 'reviewed' => 'boolean',
  59. 'posted_at' => 'date',
  60. 'meta' => 'array',
  61. ];
  62. public function account(): BelongsTo
  63. {
  64. return $this->belongsTo(Account::class, 'account_id');
  65. }
  66. public function bankAccount(): BelongsTo
  67. {
  68. return $this->belongsTo(BankAccount::class, 'bank_account_id');
  69. }
  70. public function contact(): BelongsTo
  71. {
  72. return $this->belongsTo(Contact::class, 'contact_id');
  73. }
  74. public function journalEntries(): HasMany
  75. {
  76. return $this->hasMany(JournalEntry::class, 'transaction_id');
  77. }
  78. public function transactionable(): MorphTo
  79. {
  80. return $this->morphTo();
  81. }
  82. public function payeeable(): MorphTo
  83. {
  84. return $this->morphTo();
  85. }
  86. public function isUncategorized(): bool
  87. {
  88. return $this->journalEntries->contains(fn (JournalEntry $entry) => $entry->account->isUncategorized());
  89. }
  90. public function isPayment(): bool
  91. {
  92. return $this->is_payment;
  93. }
  94. public function updateAmountIfBalanced(): void
  95. {
  96. if ($this->journalEntries->areBalanced() && $this->journalEntries->sumDebits()->formatSimple() !== $this->getAttributeValue('amount')) {
  97. $this->setAttribute('amount', $this->journalEntries->sumDebits()->getAmount());
  98. $this->save();
  99. }
  100. }
  101. public static function getBankAccountOptionsFlat(?int $excludedAccountId = null, ?int $currentBankAccountId = null, bool $excludeArchived = true): array
  102. {
  103. return BankAccount::query()
  104. ->whereHas('account', function (Builder $query) use ($excludeArchived) {
  105. if ($excludeArchived) {
  106. $query->where('archived', false);
  107. }
  108. })
  109. ->with(['account' => function ($query) use ($excludeArchived) {
  110. if ($excludeArchived) {
  111. $query->where('archived', false);
  112. }
  113. }])
  114. ->when($excludedAccountId, fn (Builder $query) => $query->where('account_id', '!=', $excludedAccountId))
  115. ->when($currentBankAccountId, fn (Builder $query) => $query->orWhere('id', $currentBankAccountId))
  116. ->get()
  117. ->pluck('account.name', 'id')
  118. ->toArray();
  119. }
  120. public static function getBankAccountOptions(?int $excludedAccountId = null, ?int $currentBankAccountId = null, bool $excludeArchived = true): array
  121. {
  122. return BankAccount::query()
  123. ->whereHas('account', function (Builder $query) use ($excludeArchived) {
  124. if ($excludeArchived) {
  125. $query->where('archived', false);
  126. }
  127. })
  128. ->with(['account' => function ($query) use ($excludeArchived) {
  129. if ($excludeArchived) {
  130. $query->where('archived', false);
  131. }
  132. }, 'account.subtype' => function ($query) {
  133. $query->select(['id', 'name']);
  134. }])
  135. ->when($excludedAccountId, fn (Builder $query) => $query->where('account_id', '!=', $excludedAccountId))
  136. ->when($currentBankAccountId, fn (Builder $query) => $query->orWhere('id', $currentBankAccountId))
  137. ->get()
  138. ->groupBy('account.subtype.name')
  139. ->map(fn (Collection $bankAccounts, string $subtype) => $bankAccounts->pluck('account.name', 'id'))
  140. ->toArray();
  141. }
  142. public static function getBankAccountAccountOptions(?int $excludedBankAccountId = null, ?int $currentAccountId = null): array
  143. {
  144. return Account::query()
  145. ->whereHas('bankAccount', function (Builder $query) use ($excludedBankAccountId) {
  146. // Exclude the specific bank account if provided
  147. if ($excludedBankAccountId) {
  148. $query->whereNot('id', $excludedBankAccountId);
  149. }
  150. })
  151. ->where(function (Builder $query) use ($currentAccountId) {
  152. $query->where('archived', false)
  153. ->orWhere('id', $currentAccountId);
  154. })
  155. ->get()
  156. ->groupBy(fn (Account $account) => $account->category->getPluralLabel())
  157. ->map(fn (Collection $accounts, string $category) => $accounts->pluck('name', 'id'))
  158. ->toArray();
  159. }
  160. public static function getChartAccountOptions(): array
  161. {
  162. return Account::query()
  163. ->select(['id', 'name', 'category'])
  164. ->get()
  165. ->groupBy(fn (Account $account) => $account->category->getPluralLabel())
  166. ->map(fn (Collection $accounts, string $category) => $accounts->pluck('name', 'id'))
  167. ->toArray();
  168. }
  169. public static function getTransactionAccountOptions(
  170. TransactionType $type,
  171. ?int $currentAccountId = null
  172. ): array {
  173. $associatedAccountTypes = match ($type) {
  174. TransactionType::Deposit => [
  175. AccountType::OperatingRevenue, // Sales, service income
  176. AccountType::NonOperatingRevenue, // Interest, dividends received
  177. AccountType::CurrentLiability, // Loans received
  178. AccountType::NonCurrentLiability, // Long-term financing
  179. AccountType::Equity, // Owner contributions
  180. AccountType::ContraExpense, // Refunds of expenses
  181. AccountType::UncategorizedRevenue,
  182. ],
  183. TransactionType::Withdrawal => [
  184. AccountType::OperatingExpense, // Regular business expenses
  185. AccountType::NonOperatingExpense, // Interest paid, etc.
  186. AccountType::CurrentLiability, // Loan payments
  187. AccountType::NonCurrentLiability, // Long-term debt payments
  188. AccountType::Equity, // Owner withdrawals
  189. AccountType::ContraRevenue, // Customer refunds, discounts
  190. AccountType::UncategorizedExpense,
  191. ],
  192. default => null,
  193. };
  194. return Account::query()
  195. ->doesntHave('adjustment')
  196. ->doesntHave('bankAccount')
  197. ->when($associatedAccountTypes, fn (Builder $query) => $query->whereIn('type', $associatedAccountTypes))
  198. ->where(function (Builder $query) use ($currentAccountId) {
  199. $query->where('archived', false)
  200. ->orWhere('id', $currentAccountId);
  201. })
  202. ->get()
  203. ->groupBy(fn (Account $account) => $account->category->getPluralLabel())
  204. ->map(fn (Collection $accounts, string $category) => $accounts->pluck('name', 'id'))
  205. ->toArray();
  206. }
  207. public static function getJournalAccountOptions(
  208. ?int $currentAccountId = null
  209. ): array {
  210. return Account::query()
  211. ->where(function (Builder $query) use ($currentAccountId) {
  212. $query->where('archived', false)
  213. ->orWhere('id', $currentAccountId);
  214. })
  215. ->get()
  216. ->groupBy(fn (Account $account) => $account->category->getPluralLabel())
  217. ->map(fn (Collection $accounts, string $category) => $accounts->pluck('name', 'id'))
  218. ->toArray();
  219. }
  220. public static function getUncategorizedAccountByType(TransactionType $type): ?Account
  221. {
  222. [$category, $accountName] = match ($type) {
  223. TransactionType::Deposit => [AccountCategory::Revenue, 'Uncategorized Income'],
  224. TransactionType::Withdrawal => [AccountCategory::Expense, 'Uncategorized Expense'],
  225. default => [null, null],
  226. };
  227. return Account::where('category', $category)
  228. ->where('name', $accountName)
  229. ->first();
  230. }
  231. public static function getPayeeOptions(): array
  232. {
  233. $clients = Client::query()
  234. ->orderBy('name')
  235. ->pluck('name', 'id')
  236. ->toArray();
  237. $vendors = Vendor::query()
  238. ->orderBy('name')
  239. ->pluck('name', 'id')
  240. ->mapWithKeys(fn ($name, $id) => [-$id => $name])
  241. ->toArray();
  242. return [
  243. 'Clients' => $clients,
  244. 'Vendors' => $vendors,
  245. ];
  246. }
  247. public function getReportTableUrl(): string
  248. {
  249. if ($this->transactionable_type && ! $this->is_payment) {
  250. return match ($this->transactionable_type) {
  251. Bill::class => ViewBill::getUrl(['record' => $this->transactionable_id]),
  252. default => ViewInvoice::getUrl(['record' => $this->transactionable_id]),
  253. };
  254. }
  255. return ViewTransaction::getUrl(['record' => $this->id]);
  256. }
  257. protected static function newFactory(): Factory
  258. {
  259. return TransactionFactory::new();
  260. }
  261. }