ViewManageController.php

Tambahan error message "tidak ada transaksi" dan juga perbaikan untuk range date untuk FILLTER total pemasukan
 avatar
user_8248690604
php_laravel_blade
2 years ago
12 kB
3
Indexable
Never
<?php

namespace App\Http\Controllers;

use Session;
use Auth;
use Carbon\Carbon;
use App\Market;
use App\User;
use App\Transaction;
use App\TransactionNonPPN;
use Illuminate\Http\Request;
use PhpParser\Node\Expr\AssignOp\Concat;

use function GuzzleHttp\Psr7\parse_response;

class ViewManageController extends Controller
{
    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);

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

            $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');
        }
    }
    public function getYear()
    {
        $today = Carbon::now()->format('Y');
        return $today;
    }
    //====================================== Perubahan Baru ==========================================//
    public function allIncomesDashboard($min_year = 0, $max_year = 0)
    {
        // ============================= Tambahan 'error' ============================= //
        $today = Carbon::now()->format('Y');
        if ($min_year == 0 || $min_year == null) {
            $min_year = $today;
        }
        if ($max_year == 0 || $max_year == null) {
            $max_year = $today;
        }

        $min_year = date($min_year . '-01-01');
        $max_year = date($max_year . '-12-31');

        $min_date = Transaction::where('status', 'lunas')->whereBetween('created_at', [$min_year, $max_year])->min('created_at');
        $max_date = Transaction::where('status', 'lunas')->whereBetween('created_at', [$min_year, $max_year])->max('created_at');
        $min_date_nppn = TransactionNonPPN::where('status', 'lunas')->whereBetween('created_at', [$min_year, $max_year])->min('created_at');
        $max_date_nppn = TransactionNonPPN::where('status', 'lunas')->whereBetween('created_at', [$min_year, $max_year])->max('created_at');

        $min = $min_date < $min_date_nppn ? $min_date : $min_date_nppn;
        $max = $max_date > $max_date_nppn ? $max_date : $max_date_nppn;
        if ($min != null && $max != null) {
            $min_date = date('d M, Y', strtotime($min));
            $max_date = date('d M, Y', strtotime($max));
        }


        $kode_transaksi_dis = Transaction::select(['kode_transaksi', 'total', 'created_at'])
            ->distinct()
            ->whereYear('created_at', '>=', $min_year)
            ->whereYear('created_at', '<=', $max_year)
            ->get();
        $kode_transaksi_npp_dis = TransactionNonPPN::select(['kode_transaksi', 'total', 'created_at'])
            ->distinct()
            ->whereYear('created_at', '>=', $min_year)
            ->whereYear('created_at', '<=', $max_year)
            ->get();
        $kode_transaksi_dis = $kode_transaksi_dis->concat($kode_transaksi_npp_dis);

        $kode_transaksi_uniques = array();
        foreach ($kode_transaksi_dis as $c) {
            $kode_transaksi_uniques[$c->kode_transaksi] = $c;
        }
        $all_incomes = 0;
        foreach ($kode_transaksi_uniques as $kode) {

            $transaksi = Transaction::where('kode_transaksi', $kode->kode_transaksi)->first();
            if ($transaksi != null) {
                $all_incomes += $transaksi->total;
            } else {
                $transaksi = TransactionNonPPN::where('kode_transaksi', $kode->kode_transaksi)->first();
                $all_incomes += $transaksi->total;
            }
        }
        $all_incomes = number_format($all_incomes, 2, ',', '.');
        $error = null;
        if ($min_date == null || $max_date == null) {
            $error = 'Tidak ada transaksi';
        }
        return response()->json([
            'all_incomes' => $all_incomes,
            'error' => $error,
            'min_date' => $min_date,
            'max_date' => $max_date
        ]);
        // ============================== Tambahan 'error' ============================= //
        //====================================== Perubahan Baru ==========================================//
    }

    public function filterChartDashboard($filter)
    {
        if ($filter == 'pemasukan') {
            $supplies = Transaction::where('status', 'lunas')->get();
            $supplies_nonppn = TransactionNonPPN::where('status', 'lunas')->get();
            $supplies = $supplies->concat($supplies_nonppn);
            $array = array();
            foreach ($supplies as $no => $supply) {
                array_push($array, $supplies[$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);
            $tmp_ppn = Transaction::select(['kode_transaksi', 'created_at', 'total'])
                ->distinct()
                ->get();
            $tmp_nppn = TransactionNonPPN::select(['kode_transaksi', 'created_at', 'total'])
                ->distinct()
                ->get();
            $tmp_concat = $tmp_ppn->concat($tmp_nppn);
            $tmp_uniques = array();
            foreach ($tmp_concat as $c) {
                $tmp_uniques[$c->kode_transaksi] = $c;
            }
            $total = array();
            $summ = 0;
            foreach ($incomes as $no => $income) {
                foreach ($tmp_uniques as $t) {
                    if ($t->created_at->toDateString() == $income) {
                        $summ += $t->total;
                    }
                }
                array_push($total, $summ);
                $summ = 0;
            }

            return response()->json([
                'incomes' => $incomes,
                'total' => $total
            ]);
        } else if ($filter == 'pelanggan') {
            $supplies = Transaction::where('status', 'lunas')->get();
            $supplies_nonppn = TransactionNonPPN::where('status', 'lunas')->get();
            $supplies = $supplies->concat($supplies_nonppn);
            $array = array();
            foreach ($supplies as $no => $supply) {
                array_push($array, $supplies[$no]->created_at->toDateString());
            }
            $dates = array_unique($array);
            rsort($dates);
            $arr_ammount = count($dates);
            $customer_data = array();
            if ($arr_ammount > 7) {
                for ($i = 0; $i < 7; $i++) {
                    array_push($customer_data, $dates[$i]);
                }
            } elseif ($arr_ammount > 0) {
                for ($i = 0; $i < $arr_ammount; $i++) {
                    array_push($customer_data, $dates[$i]);
                }
            }
            $customers = array_reverse($customer_data);
            $tmp_ppn = Transaction::select(['kode_transaksi', 'created_at', 'id_customer'])
                ->distinct()
                ->get();
            $tmp_nppn = TransactionNonPPN::select(['kode_transaksi', 'created_at', 'id_customer'])
                ->distinct()
                ->get();
            $tmp_concat = $tmp_ppn->concat($tmp_nppn);
            $tmp_uniques = array();
            foreach ($tmp_concat as $c) {
                $tmp_uniques[$c->kode_transaksi] = $c;
            }
            $jumlah = array();
            $summ = 0;
            foreach ($customer_data as $no => $costumer_dat) {
                $temp = array();
                foreach ($tmp_uniques as $t) {
                    if ($t->created_at->toDateString() == $costumer_dat) {
                        if (!in_array($t->id_customer, $temp)) {
                            $summ += 1;
                            array_push($temp, $t->id_customer);
                        }
                    }
                }
                array_push($jumlah, $summ);
                unset($temp);
                $summ = 0;
            }
            $jumlah = array_reverse($jumlah);

            return response()->json([
                'customers' => $customers,
                'jumlah' => $jumlah
            ]);
        }
    }

    // Update Market
    public function updateMarket(Request $req)
    {
        $market = Market::first();
        $market->nama_toko = $req->nama_toko;
        $market->no_telp = $req->no_telp;
        $market->alamat = $req->alamat;
        $market->save();

        Session::flash('update_success', 'Pengaturan berhasil diubah');

        return back();
    }
}