Blame view

app/Http/Controllers/Customers/Dashboard.php 3.7 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  <?php
  
  namespace App\Http\Controllers\Customers;
  
  use App\Http\Controllers\Controller;
  use App\Models\Income\Invoice;
  use Charts;
  use Date;
  
  class Dashboard extends Controller
  {
      /**
       * Display a listing of the resource.
       *
       * @return Response
       */
      public function index()
      {
          $customer = auth()->user()->customer;
  
          $invoices = Invoice::with('status')->accrued()->where('customer_id', $customer->id)->get();
  
          $start = Date::parse(request('start', Date::today()->startOfYear()->format('Y-m-d')));
          $end = Date::parse(request('end', Date::today()->endOfYear()->format('Y-m-d')));
  
          $start_month = $start->month;
          $end_month = $end->month;
  
          // Monthly
          $labels = [];
  
          $s = clone $start;
  
          for ($j = $end_month; $j >= $start_month; $j--) {
              $labels[$end_month - $j] = $s->format('M Y');
  
              $s->addMonth();
          }
  
          $unpaid = $paid = $overdue = $partial_paid = [];
  
          foreach ($invoices as $invoice) {
              switch ($invoice->invoice_status_code) {
                  case 'paid':
                      $paid[] = $invoice;
                      break;
                  case 'partial':
                      $partial_paid[] = $invoice;
                      break;
                  case 'sent':
                  default:
                      if (Date::today()->format('Y-m-d') > $invoice->due_at->format('Y-m-d')) {
                          $overdue[] = $invoice;
                      } else {
                          $unpaid[] = $invoice;
                      }
              }
          }
  
          $total = count($unpaid) + count($paid) + count($partial_paid) + count($overdue);
  
          $progress = [
              'unpaid' => count($unpaid),
              'paid' => count($paid),
              'overdue' => count($overdue),
              'partially_paid' => count($partial_paid),
              'total' => $total,
          ];
  
          $unpaid = $this->calculateTotals($unpaid, $start, $end, 'unpaid');
          $paid = $this->calculateTotals($paid, $start, $end, 'paid');
          $partial_paid = $this->calculateTotals($partial_paid, $start, $end, 'partial');
          $overdue = $this->calculateTotals($overdue, $start, $end, 'overdue');
  
          $chart = Charts::multi('line', 'chartjs')
              ->dimensions(0, 300)
              ->colors(['#dd4b39', '#6da252', '#f39c12', '#00c0ef'])
              ->dataset(trans('general.unpaid'), $unpaid)
              ->dataset(trans('general.paid'), $paid)
              ->dataset(trans('general.overdue'), $overdue)
              ->dataset(trans('general.partially_paid'), $partial_paid)
              ->labels($labels)
              ->credits(false)
              ->view('vendor.consoletvs.charts.chartjs.multi.line');
  
          return view('customers.dashboard.index', compact('customer', 'invoices', 'progress', 'chart'));
      }
  
      private function calculateTotals($items, $start, $end, $type)
      {
          $totals = [];
  
          $date_format = 'Y-m';
  
          $n = 1;
          $start_date = $start->format($date_format);
          $end_date = $end->format($date_format);
          $next_date = $start_date;
  
          $s = clone $start;
  
          while ($next_date <= $end_date) {
              $totals[$next_date] = 0;
  
              $next_date = $s->addMonths($n)->format($date_format);
          }
  
          $this->setTotals($totals, $items, $date_format, $type);
  
          return $totals;
      }
  
      private function setTotals(&$totals, $items, $date_format, $type)
      {
          foreach ($items as $item) {
              if ($type == 'partial') {
                  $item->amount = $item->payments()->paid();
              }
  
              $i = Date::parse($item->paid_at)->format($date_format);
  
              $totals[$i] += $item->getConvertedAmount();
          }
      }
  }