Blame view

app/Models/Banking/Transaction.php 3.82 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
  <?php
  
  namespace App\Models\Banking;
  
  use App\Models\Expense\Bill;
  use App\Models\Expense\Payment;
  use App\Models\Income\Invoice;
  use App\Models\Income\Revenue;
  use Illuminate\Database\Eloquent\Model;
  
  class Transaction extends Model
  {
      public static function getUserTransactions($user_id, $type)
      {
          $transactions = array();
  
          switch ($type) {
              case 'payments':
                  $bills = Bill::where('vendor_id', $user_id)->get();
  
                  foreach ($bills as $bill) {
                      $bill_payments = $bill->payments;
  
                      if ($bill_payments) {
                          foreach ($bill_payments as $bill_payment) {
                              $transactions[] = (object) [
                                  'date'          => $bill_payment->paid_at,
                                  'account'       => $bill_payment->account->name,
                                  'type'          => trans('invoices.status.partial'),
                                  'category'      => trans_choice('general.invoices', 1),
                                  'description'   => $bill_payment->description,
                                  'amount'        => $bill_payment->amount,
                                  'currency_code' => $bill_payment->currency_code,
                              ];
                          }
                      }
                  }
  
                  $payments = Payment::where('vendor_id', $user_id)->get();
  
                  foreach ($payments as $payment) {
                      $transactions[] = (object) [
                          'date'          => $payment->paid_at,
                          'account'       => $payment->account->name,
                          'type'          => 'Expense',
                          'category'      => $payment->category->name,
                          'description'   => $payment->description,
                          'amount'        => $payment->amount,
                          'currency_code' => $payment->currency_code,
                      ];
                  }
                  break;
              case 'revenues':
                  $invoices = Invoice::where('customer_id', $user_id)->get();
  
                  foreach ($invoices as $invoice) {
                      $invoice_payments = $invoice->payments;
  
                      if ($invoice_payments) {
                          foreach ($invoice_payments as $invoice_payment) {
                              $transactions[] = (object) [
                                  'date'          => $invoice_payment->paid_at,
                                  'account'       => $invoice_payment->account->name,
                                  'type'          => trans('invoices.status.partial'),
                                  'category'      => trans_choice('general.invoices', 1),
                                  'description'   => $invoice_payment->description,
                                  'amount'        => $invoice_payment->amount,
                                  'currency_code' => $invoice_payment->currency_code,
                              ];
                          }
                      }
                  }
  
                  $revenues = Revenue::where('customer_id', $user_id)->get();
  
                  foreach ($revenues as $revenue) {
                      $transactions[] = (object) [
                          'date'          => $revenue->paid_at,
                          'account'       => $revenue->account->name,
                          'type'          => trans_choice('general.payments', 1),
                          'category'      => $revenue->category->name,
                          'description'   => $revenue->description,
                          'amount'        => $revenue->amount,
                          'currency_code' => $revenue->currency_code,
                      ];
                  }
                  break;
          }
  
          return $transactions;
      }
  }