ViewManageController.php

function viewDashboard
 avatar
user_8248690604
php_laravel_blade
2 years ago
4.0 kB
4
Indexable
Never
public function viewDashboard()
    {
        $id_account = Auth::id();
        $role = User::where('id', $id_account)->first();
        if ($role->role != 'kasir') {
            $kd_transaction = Transaction::select(['kode_transaksi', 'total', 'created_at'])
                ->latest()
                ->distinct()
                ->take(3)
                ->get();
            $kd_transaction_nonppn = TransactionNonPPN::select(['kode_transaksi', 'total', 'created_at'])
                ->latest()
                ->distinct()
                ->take(3)
                ->get();
            $kd_transaction = $kd_transaction->concat($kd_transaction_nonppn);
            $kd_transaction = $kd_transaction->sortByDesc('created_at');
            $kd_transaction_uniques = array();
            foreach ($kd_transaction as $c) {
                $kd_transaction_uniques[$c->kode_transaksi] = $c;
            }


            $transactions = Transaction::where('status', 'lunas')->get();
            $transactions_nonppn = TransactionNonPPN::where('status', 'lunas')->get();
            $transactions = $transactions->concat($transactions_nonppn);
            $array = array();
            foreach ($transactions as $no => $transaction) {
                array_push($array, $transactions[$no]->created_at->toDateString());
            }
            $dates = array_unique($array);
            rsort($dates);

            $arr_ammount = count($dates);
            $incomes_data = array();
            if ($arr_ammount > 7) {
                for ($i = 0; $i < 7; $i++) {
                    array_push($incomes_data, $dates[$i]);
                }
            } elseif ($arr_ammount > 0) {
                for ($i = 0; $i < $arr_ammount; $i++) {
                    array_push($incomes_data, $dates[$i]);
                }
            }
            $incomes = array_reverse($incomes_data);
            $kode_transaksi_dis_daily = Transaction::whereDate('created_at', Carbon::now())
                ->select(['kode_transaksi', 'id_customer', 'total'])
                ->distinct()
                ->get();
            $kode_transaksi_npp_dis_daily = TransactionNonPPN::whereDate('created_at', Carbon::now())
                ->select(['kode_transaksi', 'id_customer', 'total'])
                ->distinct()
                ->get();
            $kode_transaksi_dis_daily = $kode_transaksi_dis_daily->concat($kode_transaksi_npp_dis_daily);

            // =========================== Perubahan Baru ==========================//
            $kode_transaksi_daily_uniques = array();
            $costumer_daily_uniques = array();
            // ------------- baru - //
            $income_total_daily_uniques = array();
            $incomes_daily = 0;
            // ------------- baru - //
            foreach ($kode_transaksi_dis_daily as $c) {
                $kode_transaksi_daily_uniques[$c->kode_transaksi] = $c;
                $costumer_daily_uniques[$c->id_customer] = $c;
                // --------------------- baru - //
                $income_total_daily_uniques[$c->total] = $c;
                $incomes_daily += $c->total;
                // --------------------- baru - //
            }
            // =================================================================//

            $retur = 0;
            $retur = Transaction::where('status', 'retur')->whereDate('created_at', Carbon::now())->sum('total_barang');
            $retur_nppn = TransactionNonPPN::where('status', 'retur')->whereDate('created_at', Carbon::now())->sum('total_barang');
            $retur = $retur + $retur_nppn;
            $incomes_daily = $incomes_daily - $retur;

            $customers_daily = count($costumer_daily_uniques);

            $market = Market::first();
            return view('dashboard', compact('kd_transaction_uniques', 'incomes', 'incomes_daily', 'customers_daily', 'market'));
        } else {
            return view('blank_dashboard');
        }
    }