您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Transaction.php 8.8KB

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