Blame view

app/Http/Controllers/Banking/Transactions.php 3.24 KB
b7c7a5f6   Alexey Boroda   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  <?php
  
  namespace App\Http\Controllers\Banking;
  
  use App\Http\Controllers\Controller;
  use App\Models\Banking\Account;
  use App\Models\Banking\Transaction;
  use App\Models\Expense\BillPayment;
  use App\Models\Expense\Payment;
  use App\Models\Income\InvoicePayment;
  use App\Models\Income\Revenue;
  use App\Models\Setting\Category;
  
  class Transactions extends Controller
  {
  
      public $transactions = [];
  
      /**
       * Display a listing of the resource.
       *
       * @return Response
       */
      public function index()
      {
          $request = request();
          
          $accounts = collect(Account::enabled()->pluck('name', 'id'))
              ->prepend(trans('general.all_type', ['type' => trans_choice('general.accounts', 2)]), '');
  
          $types = collect(['expense' => 'Expense', 'income' => 'Income'])
              ->prepend(trans('general.all_type', ['type' => trans_choice('general.types', 2)]), '');
              
          $categories = collect(Category::enabled()->type('income')->pluck('name', 'id'))
              ->prepend(trans('general.all_type', ['type' => trans_choice('general.categories', 2)]), '');
  
          $type = $request->get('type');
  
          if ($type != 'income') {
              $this->addTransactions(Payment::collect(['paid_at'=> 'desc']), trans_choice('general.expenses', 1));
              $this->addTransactions(BillPayment::collect(['paid_at'=> 'desc']), trans_choice('general.expenses', 1), trans_choice('general.bills', 1));
          }
  
          if ($type != 'expense') {
              $this->addTransactions(Revenue::collect(['paid_at'=> 'desc']), trans_choice('general.incomes', 1));
              $this->addTransactions(InvoicePayment::collect(['paid_at'=> 'desc']), trans_choice('general.incomes', 1), trans_choice('general.invoices', 1));
          }
  
          $transactions = $this->getTransactions($request);
  
          return view('banking.transactions.index', compact('transactions', 'accounts', 'types', 'categories'));
      }
  
      /**
       * Add items to transactions array.
       *
       * @param $items
       * @param $type
       * @param $category
       */
      protected function addTransactions($items, $type, $category = null)
      {
          foreach ($items as $item) {
              $data = [
                  'paid_at'           => $item->paid_at,
                  'account_name'      => $item->account->name,
                  'type'              => $type,
                  'description'       => $item->description,
                  'amount'            => $item->amount,
                  'currency_code'     => $item->currency_code,
              ];
  
              if (!is_null($category)) {
                  $data['category_name'] = $category;
              } else {
                  $data['category_name'] = $item->category->name;
              }
  
              $this->transactions[] = (object) $data;
          }
      }
  
      protected function getTransactions($request)
      {
          // Sort items
          if (isset($request['sort'])) {
              if ($request['order'] == 'asc') {
                  $f = 'sortBy';
              } else {
                  $f = 'sortByDesc';
              }
  
              $transactions = collect($this->transactions)->$f($request['sort']);
          } else {
              $transactions = collect($this->transactions)->sortByDesc('paid_at');
          }
  
          return $transactions;
      }
  }