<?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;
class ViewManageController extends Controller
{
// Show View Dashboard
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 = $kd_transaction_nonppn->sortByDesc('created_at');
// dump($kd_transaction);
$kd_transaction_uniques = array();
foreach ($kd_transaction as $c) {
$kd_transaction_uniques[$c->kode_transaksi] = $c; // Get unique
}
$transactions = Transaction::where('status', 'lunas')->get();
$transactions_nonppn = TransactionNonPPN::where('status', 'lunas')->get();
$transactions = $transactions->concat($transactions_nonppn);
// dd($transactions);
$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 = Transaction::select(['kode_transaksi'])
->distinct()
->get();
$kode_transaksi_npp_dis = TransactionNonPPN::select(['kode_transaksi'])
->distinct()
->get();
$kode_transaksi_dis = $kode_transaksi_dis->concat($kode_transaksi_npp_dis);
// $test = $kode_transaksi_dis->merge($kode_transaksi_npp_dis);
// dd(array_unique($kode_transaksi_dis));
// dd($kode_transaksi_dis->items);
// dd($kode_transaksi_dis);
$kode_transaksi_uniques = array();
foreach ($kode_transaksi_dis as $c) {
$kode_transaksi_uniques[$c->kode_transaksi] = $c; // Get unique
}
// dd($kode_transaksi_uniques);
$kode_transaksi_dis_daily = Transaction::whereDate('created_at', Carbon::now())
->select(['kode_transaksi'])
->distinct()
->get();
$kode_transaksi_npp_dis_daily = TransactionNonPPN::whereDate('created_at', Carbon::now())
->select(['kode_transaksi'])
->distinct()
->get();
$kode_transaksi_dis_daily = $kode_transaksi_dis_daily->concat($kode_transaksi_npp_dis_daily);
$kode_transaksi_daily_uniques = array();
foreach ($kode_transaksi_dis_daily as $c) {
$kode_transaksi_daily_uniques[$c->kode_transaksi] = $c; // Get unique
}
// $test = $kode_transaksi_dis;
$all_incomes = 0;
$incomes_daily = 0;
$retur = 0;
foreach ($kode_transaksi_uniques as $kode) {
$transaksi = Transaction::where('kode_transaksi', $kode->kode_transaksi)->first();
if ($transaksi != null) {
$all_incomes += $transaksi->total;
// dump($kode->kode_transaksi);
} else {
$transaksi = TransactionNonPPN::where('kode_transaksi', $kode->kode_transaksi)->first();
$all_incomes += $transaksi->total;
// dump($kode->kode_transaksi);
}
// $total_tr = \App\Transaction::where('status','lunas')->sum('total_barang');
// $total_tr_nppn = \App\TransactionNonPPN::where('status','lunas')->sum('total_barang');
// $all_incomes = (int) $total_tr + $total_tr_nppn;
}
foreach ($kode_transaksi_daily_uniques as $kode) {
// print("masuk");
$transaksi_daily = Transaction::where('kode_transaksi', $kode->kode_transaksi)->first();
if ($transaksi_daily != null) {
$incomes_daily += $transaksi_daily->total;
} else {
$transaksi_daily = TransactionNonPPN::where('kode_transaksi', $kode->kode_transaksi)->first();
$incomes_daily += $transaksi_daily->total;
}
// $transaksi_npp_daily = TransactionNonPPN::where('kode_transaksi', $kode->kode_transaksi)->first();
// $incomes_daily += $transaksi_daily->total;
}
// dd($incomes_daily, $all_incomes);
$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;
// dd($incomes_daily);
$customers_daily = count($kode_transaksi_dis_daily);
$min_date = Transaction::where('status', 'lunas')->min('created_at');
$max_date = Transaction::where('status', 'lunas')->max('created_at');
$min_date_nppn = TransactionNonPPN::where('status', 'lunas')->min('created_at');
$max_date_nppn = TransactionNonPPN::where('status', 'lunas')->max('created_at');
$min_date = $min_date < $min_date_nppn ? $min_date : $min_date_nppn;
$max_date = $max_date > $max_date_nppn ? $max_date : $max_date_nppn;
// dd($min_date, $max_date);
$market = Market::first();
return view('dashboard', compact('kd_transaction_uniques', 'incomes', 'incomes_daily', 'customers_daily', 'all_incomes', 'min_date', 'max_date', 'market'));
} else {
return view('blank_dashboard');
}
}
// Filter Chart Dashboard
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);
$total = array();
foreach ($incomes as $no => $income) {
array_push($total, [
Transaction::whereDate('created_at', $income)->where('status', 'lunas')->sum('total'),
TransactionNonPPN::whereDate('created_at', $income)->where('status', 'lunas')->sum('total')
]);
}
// total array sortby last index
$total = array_map(function ($arr) {
return $arr[count($arr) - 1];
}, $total);
return response()->json([
'incomes' => $incomes,
'total' => $total
]);
} else {
$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);
$jumlah = array();
foreach ($customers as $no => $customer) {
array_push($jumlah, [
Transaction::whereDate('created_at', $customer)->where('status', 'lunas')->count(),
TransactionNonPPN::whereDate('created_at', $customer)->where('status', 'lunas')->count()
]);
}
//jumlah array sortby last index
$jumlah = array_map(function ($arr) {
return $arr[count($arr) - 1];
}, $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();
}
}