476 lines
18 KiB
PHP
476 lines
18 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Barang;
|
|
use App\Models\StokBarang;
|
|
use App\Models\Kategori;
|
|
use App\Models\ListItemPenjualan;
|
|
use App\Models\Notifikasi;
|
|
use App\Models\Penjualan;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Response;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$totalHargaJual = 0;
|
|
$totalModal = 0;
|
|
$pendapatanPerBulan = [];
|
|
$lastYear = now()->subYear()->format('Y');
|
|
$safetyStockMonthly = [];
|
|
$currentStockPerBarang = [];
|
|
// $barangKeluarThisMonthPerSize = [];
|
|
// $ukuran_barang = Barang::groupBy("ukuran")->get();
|
|
// // dd($ukuran_barang);
|
|
// foreach($ukuran_barang as $val){
|
|
// $barangKeluarThisMonthPerSize[$val->ukuran] = 0;
|
|
// }
|
|
$barangKeluarThisMonthPerSize = [
|
|
'NBS' => 0,
|
|
'S' => 0,
|
|
'M' => 0,
|
|
'L' => 0,
|
|
'XL' => 0,
|
|
'XXL' => 0,
|
|
];
|
|
$barang = Barang::
|
|
selectRaw("barang.*,barang.id as barang_id,(IFNULL(jumlah_beli,0)-IFNULL(jumlah_jual,0)) as stok_terbaru")
|
|
->join("kategori","id_kategori","kategori.id")
|
|
->leftJoin(DB::raw('(SELECT barang_id,SUM(qty_beli) as jumlah_beli from list_item_pembelian group by barang_id) as
|
|
data_beli'),
|
|
function($join)
|
|
{
|
|
$join->on('barang.id', '=', 'data_beli.barang_id');
|
|
}
|
|
)
|
|
->leftJoin(DB::raw('(SELECT barang_id,SUM(qty) as jumlah_jual from list_item_penjualan group by barang_id) as
|
|
data_jual'),
|
|
function($join)
|
|
{
|
|
$join->on('barang.id', '=', 'data_jual.barang_id');
|
|
}
|
|
)
|
|
->groupBy("barang.id")
|
|
->get();
|
|
// dd($barang);
|
|
$res_barang = [];
|
|
$safetyStockMonthly = [];
|
|
$currentStockPerBarang = [];
|
|
foreach($barang as $item){
|
|
$penjualans = Penjualan::join("list_item_penjualan","penjualan.id","penjualan_id")
|
|
->selectRaw("SUM(qty) as demand")
|
|
->where("barang_id",$item->id)
|
|
->whereYear("list_item_penjualan.created_at",date("Y")-1)
|
|
->first();
|
|
$max = Penjualan::join("list_item_penjualan","penjualan.id","penjualan_id")
|
|
->selectRaw("SUM(qty) as qty_harian")
|
|
->where("barang_id",$item->id)
|
|
->whereYear("list_item_penjualan.created_at",date("Y")-1)
|
|
->groupBy(DB::raw("DATE_FORMAT(list_item_penjualan.created_at, '%Y-%m-%d')"))
|
|
->get();
|
|
$harian = [];
|
|
foreach($max as $a){
|
|
array_push($harian,$a->qty_harian);
|
|
}
|
|
if(count($harian) > 0){
|
|
$max_sales = max($harian);
|
|
|
|
}else{
|
|
$max_sales = 1;
|
|
}
|
|
$demand =$penjualans->demand;
|
|
$item->demand = $demand;
|
|
$average = $demand/365;
|
|
$item->average = round($average);
|
|
$orderingCost = $item->biaya_pemesanan;
|
|
$orderingCost = $orderingCost==0 || $orderingCost==NULL ?1:$orderingCost;
|
|
$eoq_annual = sqrt((2 * $demand * $orderingCost ) / $item->holding_cost);
|
|
$eoq_annual = $eoq_annual==0?1:$eoq_annual;
|
|
$pemesanan = $demand/$eoq_annual;
|
|
$safety_stock = ($max_sales * $item->lead_time_variance) - ($average * $item->lead_time);
|
|
$rop = ($average* $item->lead_time) + $safety_stock ;
|
|
$item->max_qty = $max_sales;
|
|
$item->pemesanan = round($pemesanan);
|
|
$item->eoq_annual = round($eoq_annual);
|
|
$item->safety_stock = round($safety_stock);
|
|
$item->rop = $rop;
|
|
array_push($res_barang,$item);
|
|
$currentStockPerBarang[$item->nama] = $item->stok_terbaru;
|
|
$safetyStockMonthly[$item->nama] = round($safety_stock);
|
|
|
|
$update_barang = Barang::find($item->id);
|
|
$update_barang->update([
|
|
'demand' => $demand,
|
|
'eoq_annual' => $eoq_annual,
|
|
'safety_stok' => $safety_stock,
|
|
'pemesanan' => $pemesanan,
|
|
'rop' => $rop,
|
|
'max_sales' => $max_sales
|
|
]);
|
|
|
|
|
|
|
|
}
|
|
// dd($res_barang);
|
|
//Bulan Ini
|
|
$totalHargaJual = 0;
|
|
$totalModal = 0;
|
|
$totalBarangTerjual = 0;
|
|
$penjualan_bulan_ini = Penjualan::with('listItem.barang')
|
|
->whereMonth('created_at', date("m"))
|
|
->whereYear('created_at', date("Y"))
|
|
->orderBy("id","DESC")
|
|
->get();
|
|
$barangKeluarThisMonthPerBarang = [];
|
|
// dd($penjualan_bulan_ini);
|
|
foreach ($penjualan_bulan_ini as $penjualan) {
|
|
foreach ($penjualan->listItem as $listItem) {
|
|
$totalHargaJual += (int) $listItem->barang->harga_jual * $listItem->qty;
|
|
$totalModal += (int) $listItem->barang->harga_beli * $listItem->qty;
|
|
$totalBarangTerjual += $listItem->qty;
|
|
// echo $listItem->nama_barang."<br>";
|
|
if (array_key_exists($listItem->barang->nama,$barangKeluarThisMonthPerBarang)) {
|
|
$barangKeluarThisMonthPerBarang[$listItem->barang->nama] += $listItem->qty;
|
|
}else{
|
|
$barangKeluarThisMonthPerBarang[$listItem->barang->nama] = $listItem->qty;
|
|
}
|
|
if(array_key_exists($listItem->barang->ukuran,$barangKeluarThisMonthPerSize)){
|
|
|
|
$barangKeluarThisMonthPerSize[$listItem->barang->ukuran] += $listItem->qty;
|
|
}
|
|
}
|
|
}
|
|
// dd($barangKeluarThisMonthPerBarang);
|
|
|
|
$totalPendapatan = $totalHargaJual - $totalModal;
|
|
|
|
$data = [
|
|
'title' => 'Dashboard',
|
|
'barang' => Barang::count(),
|
|
'totalPenjualan' => $this->formatIDR($totalHargaJual),
|
|
'totalPendapatan' => $this->formatIDR($totalPendapatan),
|
|
'SSperBarang' => $safetyStockMonthly,
|
|
'currentStockPerBarang' => $currentStockPerBarang,
|
|
'barangKeluarThisMonthPerBarang' =>$barangKeluarThisMonthPerBarang,
|
|
'barangKeluarThisMonthPerSize' => $barangKeluarThisMonthPerSize
|
|
];
|
|
|
|
return view('page.dashboard.index', compact('data'));
|
|
}
|
|
|
|
|
|
|
|
// public function index()
|
|
// {
|
|
// $penjualans = Penjualan::with('listItem')->get();
|
|
|
|
// $totalHargaJual = 0;
|
|
// $totalModal = 0;
|
|
// $pendapatanPerBulan = [];
|
|
|
|
// $lastYear = now()->subYear()->format('Y');
|
|
// // dd($lastYear);
|
|
// $safetyStockMonthly = [];
|
|
// $currentStockPerBarang = [];
|
|
// $barangKeluarThisMonthPerSize = [
|
|
// 'NBS' => 0,
|
|
// 'S' => 0,
|
|
// 'M' => 0,
|
|
// 'L' => 0,
|
|
// 'XL' => 0,
|
|
// 'XXL' => 0,
|
|
// ];
|
|
|
|
// $barang = Barang::
|
|
// selectRaw("barang.*,barang.id as barang_id,barang.nama as nama_barang,kategori.*,(IFNULL(jumlah_beli,0)-IFNULL(jumlah_jual,0)) as stok_terbaru")
|
|
// ->join("kategori","id_kategori","kategori.id")
|
|
// ->leftJoin(DB::raw('(SELECT barang_id,SUM(qty_beli) as jumlah_beli from list_item_pembelian group by barang_id) as
|
|
// data_beli'),
|
|
// function($join)
|
|
// {
|
|
// $join->on('barang.id', '=', 'data_beli.barang_id');
|
|
// }
|
|
// )
|
|
// ->leftJoin(DB::raw('(SELECT barang_id,SUM(qty) as jumlah_jual from list_item_penjualan group by barang_id) as
|
|
// data_jual'),
|
|
// function($join)
|
|
// {
|
|
// $join->on('barang.id', '=', 'data_jual.barang_id');
|
|
// }
|
|
// )
|
|
// ->groupBy("barang.id")
|
|
// ->get();
|
|
// // $monthyear_now = date("Ym");
|
|
// // $barang_new = Barang::with('kategori', 'listpenjualan')
|
|
// // ->where
|
|
// // ->get();
|
|
|
|
// foreach ($penjualans as $penjualan) {
|
|
// foreach ($penjualan->listItem as $listItem) {
|
|
// $totalHargaJual += (int) $listItem->barang->harga_jual * $listItem->qty;
|
|
// $totalModal += (int) $listItem->barang->harga_beli * $listItem->qty;
|
|
|
|
// $month = $penjualan->created_at->format('Y-m');
|
|
// if (!isset($pendapatanPerBulan[$month])) {
|
|
// $pendapatanPerBulan[$month] = [
|
|
// 'totalHargaJual' => 0,
|
|
// 'totalModal' => 0,
|
|
// ];
|
|
// }
|
|
// $pendapatanPerBulan[$month]['totalHargaJual'] += (int) $listItem->barang->harga_jual * $listItem->qty;
|
|
// $pendapatanPerBulan[$month]['totalModal'] += (int) $listItem->barang->harga_beli * $listItem->qty;
|
|
// }
|
|
// }
|
|
|
|
// $thisMonth = Carbon::now()->format('Y-m');
|
|
// if (!array_key_exists($thisMonth,$pendapatanPerBulan)){
|
|
// $pendapatanPerBulan[$thisMonth]['totalHargaJual'] = 0;
|
|
// $pendapatanPerBulan[$thisMonth]['totalModal'] = 0;
|
|
// }
|
|
// $totalPendapatan = $pendapatanPerBulan[$thisMonth]['totalHargaJual'] - $pendapatanPerBulan[$thisMonth]['totalModal'];
|
|
|
|
// $hitungPenjualanPerhariSelamaSeminggu = [];
|
|
// $startOfWeek = now()->startOfWeek();
|
|
// $endOfWeek = now()->endOfWeek();
|
|
|
|
// for ($date = $startOfWeek; $date <= $endOfWeek; $date->addDay()) {
|
|
// $totalPenjualanHarian = Penjualan::whereDate('created_at', $date)->count();
|
|
// $hitungPenjualanPerhariSelamaSeminggu[$date->format('Y-m-d')] = $totalPenjualanHarian;
|
|
// }
|
|
|
|
// $barangKeluarPerBulan = [];
|
|
// $listItems = ListItemPenjualan::with('barang')->get();
|
|
|
|
// foreach ($listItems as $listItem) {
|
|
// $month = $listItem->created_at->format('Y-m');
|
|
// if (!isset($barangKeluarPerBulan[$month])) {
|
|
// $barangKeluarPerBulan[$month] = 0;
|
|
// }
|
|
// $barangKeluarPerBulan[$month] += $listItem->qty;
|
|
// }
|
|
|
|
// foreach($barang as $index => $item) {
|
|
// $lastYearTotal = 0;
|
|
// $lastYearCount = 0;
|
|
// $maxLastYearQty = 0;
|
|
// $salesPerDay = [];
|
|
// $highestSalesPerDay = [];
|
|
|
|
// $barangKeluarThisMonthPerBarang = [];
|
|
|
|
// $listItems = ListItemPenjualan::with('barang')
|
|
// ->whereYear('created_at', now()->year)
|
|
// ->whereMonth('created_at', now()->month)
|
|
// ->get();
|
|
|
|
// foreach ($listItems as $listItem) {
|
|
// $namaBarang = $listItem->barang->nama;
|
|
|
|
// if (!isset($barangKeluarThisMonthPerBarang[$namaBarang])) {
|
|
// $barangKeluarThisMonthPerBarang[$namaBarang] = 0;
|
|
// }
|
|
|
|
// $barangKeluarThisMonthPerBarang[$namaBarang] += $listItem->qty;
|
|
// }
|
|
|
|
// if (!empty($item->listpenjualan)) {
|
|
// foreach ($item->listpenjualan as $penjualan) {
|
|
// if (substr($penjualan->penjualan->created_at, 0, 4) === $lastYear) {
|
|
// $lastYearTotal += $penjualan->qty;
|
|
// $lastYearCount++;
|
|
|
|
// if ($penjualan->qty > $maxLastYearQty) {
|
|
// $maxLastYearQty = $penjualan->qty;
|
|
// }
|
|
|
|
// $saleDate = substr($penjualan->penjualan->created_at, 0, 10);
|
|
// $qty = $penjualan->qty;
|
|
|
|
// if (!isset($salesPerDay[$saleDate])) {
|
|
// $salesPerDay[$saleDate] = $qty;
|
|
// } else {
|
|
// $salesPerDay[$saleDate] += $qty;
|
|
// }
|
|
|
|
// if (!isset($highestSalesPerDay[$saleDate])) {
|
|
// $highestSalesPerDay[$saleDate] = $qty;
|
|
// } else {
|
|
// $highestSalesPerDay[$saleDate] += $qty;
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// if ($lastYearTotal > 0) {
|
|
// $averageQtyPerDay = array_sum($highestSalesPerDay) / count($highestSalesPerDay);
|
|
|
|
// $maxSalesPerDay = max($highestSalesPerDay);
|
|
|
|
// $orderingCost = $item->biaya_pemesanan;
|
|
// $orderingCost = $orderingCost==0?1:$orderingCost;
|
|
// $EOQAnnual = sqrt((2 * $item->demand * $orderingCost ) / $item->holding_cost);
|
|
// $Pemesanan = $item->demand / $EOQAnnual ;
|
|
// $rata = $item->demand / 365 ;
|
|
// $safetyStock = ($maxSalesPerDay * $item->lead_time_variance) - ($rata * $item->lead_time);
|
|
// $Dm = $item->demand / $EOQAnnual;
|
|
// $safetyStockMonthly[$item->id] = round($safetyStock);
|
|
// $ROP = ($rata* $item->lead_time) + $safetyStock ;
|
|
// } else {
|
|
// $EOQAnnual = 0;
|
|
// $EOQMonthly = 0;
|
|
// $orderingCost = 0;
|
|
// $ROP = 0;
|
|
// $safetyStockMonthly[$item->id] = 0;
|
|
// }
|
|
|
|
// $update_barang = Barang::findOrFail($item->id);
|
|
// $update_barang->update(['rop'=>$ROP]);
|
|
|
|
// $stok_barang = StokBarang::where("id",$item->id)->first();
|
|
// // $currentStock = $item->stok;
|
|
// $currentStock = $stok_barang->stok_terbaru;
|
|
// $currentStockPerBarang[$item->nama] = $currentStock;
|
|
|
|
// if (!empty($item->listpenjualan)) {
|
|
// foreach ($item->listpenjualan as $penjualan) {
|
|
// $sekarang = "2024-01-01";
|
|
// $sekarang = date("Y-m-d");
|
|
|
|
// $bulan_jual = date("m",strtotime($penjualan->created_at));
|
|
// $tahun_jual = date("Y",strtotime($penjualan->created_at));
|
|
// $bulan_sekarang = date("m",strtotime($sekarang));
|
|
// $tahun_sekarang = date("Y",strtotime($sekarang));
|
|
// if(($bulan_jual==$bulan_sekarang) && ($tahun_jual==$tahun_sekarang)){
|
|
// $size = $penjualan->barang->ukuran;
|
|
|
|
// if (isset($barangKeluarThisMonthPerSize[$size])) {
|
|
// $barangKeluarThisMonthPerSize[$size] += $penjualan->qty;
|
|
// }
|
|
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// $data = [
|
|
// 'title' => 'Dashboard',
|
|
// 'barang' => Barang::count(),
|
|
// 'totalPenjualan' => $this->formatIDR($pendapatanPerBulan[$thisMonth]['totalHargaJual']),
|
|
// 'totalPendapatan' => $this->formatIDR($totalPendapatan),
|
|
// 'penjualanHarian' => $hitungPenjualanPerhariSelamaSeminggu,
|
|
// 'SSperBarang' => $safetyStockMonthly,
|
|
// 'currentStockPerBarang' => $currentStockPerBarang,
|
|
// 'barangKeluarThisMonthPerBarang' =>$barangKeluarThisMonthPerBarang,
|
|
// 'barangKeluarThisMonthPerSize' => $barangKeluarThisMonthPerSize
|
|
// ];
|
|
|
|
// return view('page.dashboard.index', compact('data'));
|
|
// }
|
|
|
|
public function getNotification()
|
|
{
|
|
$notifications = StokBarang::whereColumn('rop',">","stok_terbaru")->get();
|
|
|
|
return response()->json(['notifications' => $notifications]);
|
|
}
|
|
|
|
public function cetak()
|
|
{
|
|
$data = StokBarang::whereColumn('rop',">","stok_terbaru")->get();
|
|
|
|
return view('page.dashboard.cetak', compact('data'));
|
|
}
|
|
|
|
public function updateNotification(Request $request, $id)
|
|
{
|
|
try {
|
|
$notification = Notifikasi::findOrFail($id);
|
|
|
|
$notification->update(['read' => $request->read]);
|
|
|
|
return response()->json(['message' => 'Notification updated successfully'], 200);
|
|
} catch (\Exception $e) {
|
|
return response()->json(['error' => 'Failed to update notification', 'message' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
private function formatIDR($amount)
|
|
{
|
|
return 'IDR ' . number_format($amount, 0, ',', '.');
|
|
}
|
|
|
|
public function profile(Request $request)
|
|
{
|
|
$user = User::find(Auth::user()->id);
|
|
$data = [
|
|
'title' => 'Profile',
|
|
'content' => $user,
|
|
];
|
|
|
|
if (!$request->ajax() && $request->isMethod('GET')) {
|
|
return view('page.dashboard.profile', compact('data'));
|
|
}
|
|
|
|
if (!$data['content']) {
|
|
return response()->json(['message' => 'Data not found!', 'code' => 404]);
|
|
}
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
'name' => 'required|string|max:255',
|
|
'email' => 'required|string|email|max:255|unique:users,email,'.$user->id,
|
|
'img' => 'image|mimes:jpeg,png,jpg,gif|max:2048',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json(['message' => $validator->errors()->first(), 'code' => 422]);
|
|
}
|
|
|
|
if ($request->hasFile('image')) {
|
|
$image = $request->file('image');
|
|
$imageName = time().'.'.$image->getClientOriginalExtension();
|
|
$image->storeAs('public/foto_user', $imageName);
|
|
$user->img = $imageName;
|
|
}
|
|
|
|
$user->name = $request->input('name');
|
|
$user->email = $request->input('email');
|
|
$user->save();
|
|
|
|
return response()->json(['message' => 'Data updated successfully', 'code' => 200]);
|
|
}
|
|
|
|
|
|
public function changePassword(Request $request)
|
|
{
|
|
$data = User::find(Auth::user()->id);
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
'old_password' => 'required',
|
|
'password' => 'required|string|min:8|confirmed',
|
|
'password_confirmation' => 'required|string|min:8',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return Response::json(['message' => $validator->errors()->first(), 'code' => 422]);
|
|
}
|
|
|
|
if (!Hash::check($request->old_password, $data->password)) {
|
|
return Response::json(['message' => 'The old password is incorrect.', 'code' => 422]);
|
|
}
|
|
|
|
$data->update(['password' => bcrypt($request->password)]);
|
|
|
|
return Response::json(['message' => 'data updated successfully', 'code' => 200]);
|
|
}
|
|
|
|
|
|
}
|