MIF_E31220215/peramalanmovingaverage/app/Http/Controllers/AdminController.php

872 lines
30 KiB
PHP

<?php
namespace App\Http\Controllers;
use Barryvdh\DomPDF\Facade\Pdf as FacadePdf;
use Barryvdh\DomPDF\PDF;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Container\Attributes\DB;
use Illuminate\Support\Facades\DB as FacadesDB;
use Carbon\Carbon;
class AdminController extends Controller
{
public function dashboard()
{
// Total produk
$jumlahbarang = FacadesDB::table('produk')->count('idproduk');
// Total transaksi
$jumlahtransaksi = FacadesDB::table('penjualan')->count('idpenjualan');
// Total penjualan per bulan
$sales = FacadesDB::table('penjualan')
->selectRaw('MONTH(tanggalpenjualan) as bulan, SUM(grandtotal) as total')
->groupBy('bulan')
->pluck('total', 'bulan')
->toArray();
// Ambil 5 produk terlaris berdasarkan jumlah penjualan
$produkTerlaris = FacadesDB::table('penjualan')
->select('namabarang', FacadesDB::raw('SUM(jumlah) as total_terjual'))
->groupBy('namabarang')
->orderByDesc('total_terjual')
->limit(5)
->get();
return view('admin.dashboard', compact('jumlahbarang', 'jumlahtransaksi', 'sales', 'produkTerlaris'));
}
public function daftarbarang()
{
$data['daftarbarang'] = FacadesDB::table('produk')->get();
return view('admin.daftarbarang', $data);
}
public function tambahbarang()
{
return view('admin.tambahbarang');
}
public function tambahbarangsimpan(Request $request)
{
$request->validate([
'namaproduk' => 'required',
'stok' => 'required',
'hargajual' => 'required',
]);
FacadesDB::table('produk')->insert([
'namaproduk' => $request->input('namaproduk'),
'stok' => $request->input('stok'),
'hargajual' => $request->input('hargajual'),
'created_at' => date('Y-m-d H:i:s'),
]);
return redirect('daftarbarang')->with('success', 'Data Berhasil Ditambahkan');
}
public function barangedit($id)
{
$data['barang'] = FacadesDB::table('produk')->where('idproduk', $id)->first();
return view('admin.barangedit', $data);
}
public function barangeditsimpan(Request $request, $id)
{
$request->validate([
'namaproduk' => 'required',
'stok' => 'required',
'hargajual' => 'required',
]);
FacadesDB::table('produk')->where('idproduk', $id)->update([
'namaproduk' => $request->input('namaproduk'),
'stok' => $request->input('stok'),
'hargajual' => $request->input('hargajual'),
]);
return redirect('daftarbarang')->with('success', 'Data Berhasil Diubah');
}
public function baranghapus($id)
{
FacadesDB::table('produk')->where('idproduk', $id)->delete();
return redirect('daftarbarang')->with('success', 'Data Berhasil Dihapus');
}
// Barang masuk
public function barangmasuk()
{
$data['barangmasuk'] = FacadesDB::table('barangmasuk')
->select('notabeli', 'tanggalmasuk', 'grandtotal', 'idsupplier')
->groupBy('notabeli')
->orderBy('idbarangmasuk', 'desc')
->get();
$barangmasukdetail = [];
$datasupplier = [];
// Loop untuk setiap penjualan dan ambil detailnya
foreach ($data['barangmasuk'] as $value) {
$barangmasukdetail[$value->notabeli] = FacadesDB::table('barangmasuk')
->where('notabeli', $value->notabeli)
->get();
$supplier = FacadesDB::table('supplier')->where('idsupplier', $value->idsupplier)->first();
if ($supplier) {
$datasupplier[$value->notabeli] = $supplier->namasupplier;
}
}
// Tambahkan detail penjualan ke data yang akan dikirim ke view
$data['datasupplier'] = $datasupplier;
$data['barangmasukdetail'] = $barangmasukdetail;
return view('admin.barangmasuk', $data);
}
public function barangmasuktambah()
{
$data['barang'] = FacadesDB::table('produk')->get();
$data['supplier'] = FacadesDB::table('supplier')->get();
return view('admin.barangmasuktambah', $data);
}
public function barangmasuktambahsimpan(Request $request)
{
$notabeli = now()->format('Ymdhis');
$idsupplier = $request->input('idsupplier');
foreach ($request->namabarang as $key => $namabarang) {
FacadesDB::table('barangmasuk')->insert([
'idsupplier' => $idsupplier,
'notabeli' => $notabeli,
'namaproduk' => $namabarang,
'harga' => $request->harga[$key],
'jumlah' => $request->jumlah[$key],
'total' => $request->total[$key],
'tanggalmasuk' => $request->tanggalmasuk,
'grandtotal' => $request->grandtotalnon,
'created_at' => date('Y-m-d H:i:s'),
]);
// Update stock
FacadesDB::table('produk')->where('namaproduk', $namabarang)->increment('stok', $request->jumlah[$key]);
}
return redirect('barangmasuk')->with('success', 'Data Berhasil Ditambahkan');
}
public function barangmasukhapus($notabeli)
{
FacadesDB::table('barangmasuk')->where('notabeli', $notabeli)->delete();
return redirect('barangmasuk')->with('success', 'Data Berhasil Dihapus');
}
// Barang keluar
public function barangkeluar()
{
$data['barangkeluar'] = FacadesDB::table('barangkeluar')->Join('produk', 'barangkeluar.idproduk', '=', 'produk.idproduk')->get();
return view('admin.barangkeluar', $data);
}
public function barangkeluartambah()
{
$data['produk'] = FacadesDB::table('produk')->get();
return view('admin.barangkeluartambah', $data);
}
public function barangkeluartambahsimpan(Request $request)
{
$request->validate([
'idproduk' => 'required',
'jumlah' => 'required|numeric',
'keterangan' => 'required',
'tanggalkeluar' => 'required|date',
]);
FacadesDB::table('barangkeluar')->insert([
'idproduk' => $request->input('idproduk'),
'jumlah' => $request->input('jumlah'),
'keterangan' => $request->input('keterangan'),
'tanggalkeluar' => $request->input('tanggalkeluar'),
]);
$stokOld = FacadesDB::table('produk')->where('idproduk', $request->input('idproduk'))->first()->stok;
$newStok = $stokOld - $request->input('jumlah');
FacadesDB::table('produk')->where('idproduk', $request->input('idproduk'))->update([
'stok' => $newStok,
]);
return redirect('barangkeluar')->with('success', 'Data Berhasil Ditambahkan');
}
public function barangkeluarhapus($id)
{
FacadesDB::table('barangkeluar')->where('idbarangkeluar', $id)->delete();
return redirect('barangkeluar')->with('success', 'Data Berhasil Dihapus');
}
// Pengguna
public function penggunadaftar()
{
$data['pengguna'] = FacadesDB::table('users')->where('id', '!=', Auth::user()->id)->get();
return view('admin.penggunadaftar', $data);
}
public function penggunatambah()
{
return view('admin.penggunatambah');
}
public function penggunatambahsimpan(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required',
'password' => 'required',
'role' => 'required',
'username' => 'required',
]);
FacadesDB::table('users')->insert([
'name' => $request->input('name'),
'username' => $request->input('username'),
'email' => $request->input('email'),
'password' => bcrypt($request->input('password')),
'role' => $request->input('role'),
]);
return redirect('penggunadaftar')->with('success', 'Data Berhasil Ditambahkan');
}
public function penggunaedit($id)
{
$data['pengguna'] = FacadesDB::table('users')->where('id', $id)->first();
return view('admin.penggunaedit', $data);
}
public function penggunaeditsimpan(Request $request, $id)
{
$request->validate([
'name' => 'required',
'email' => 'required',
'role' => 'required',
'username' => 'required',
]);
$data = [
'name' => $request->input('name'),
'username' => $request->input('username'),
'email' => $request->input('email'),
'role' => $request->input('role'),
];
if ($request->input('password')) {
$data['password'] = bcrypt($request->input('password'));
}
FacadesDB::table('users')->where('id', $id)->update($data);
return redirect('penggunadaftar')->with('success', 'Data Berhasil Diubah');
}
public function penggunahapus($id)
{
FacadesDB::table('users')->where('id', $id)->delete();
return redirect('penggunadaftar')->with('success', 'Data Berhasil Dihapus');
}
// Profile
public function profile()
{
$data['profile'] = FacadesDB::table('users')->where('id', Auth::user()->id)->first();
return view('admin.profile', $data);
}
public function profileupdate(Request $request)
{
$request->validate([
'name' => 'required',
'username' => 'required',
'email' => 'required',
]);
$data = [
'name' => $request->input('name'),
'username' => $request->input('username'),
'email' => $request->input('email'),
];
if ($request->input('password')) {
$data['password'] = bcrypt($request->input('password'));
}
FacadesDB::table('users')->where('id', Auth::user()->id)->update($data);
return redirect('profile')->with('success', 'Data Berhasil Diubah');
}
public function penjualandaftar()
{
// Ambil data penjualan yang dikelompokkan berdasarkan 'notajual'
$data['penjualan'] = FacadesDB::table('penjualan')
->select('notajual', 'tanggalpenjualan', 'grandtotal')
->groupBy('notajual')
->orderBy('idpenjualan', 'desc')
->get();
// Buat array untuk menyimpan detail penjualan
$penjualanDetail = [];
// Loop untuk setiap penjualan dan ambil detailnya
foreach ($data['penjualan'] as $value) {
$penjualanDetail[$value->notajual] = FacadesDB::table('penjualan')
->where('notajual', $value->notajual)
->get();
}
// Tambahkan detail penjualan ke data yang akan dikirim ke view
$data['penjualandetail'] = $penjualanDetail;
// dd($data);
return view('admin.penjualandaftar', $data);
}
public function penjualantambah()
{
$data['barang'] = FacadesDB::table('produk')->get();
return view('admin.penjualantambah', $data);
}
public function penjualantambahsimpan(Request $request)
{
$notajual = now()->format('Ymdhis');
foreach ($request->namabarang as $key => $namabarang) {
$stok = FacadesDB::table('produk')->where('namaproduk', $namabarang)->select('stok')->first();
if ($stok->stok < $request->jumlah[$key]) {
return redirect('penjualantambah')->with('error', 'Jumlah pembelian melebihi stok');
}
FacadesDB::table('penjualan')->insert([
'notajual' => $notajual,
'namabarang' => $namabarang,
'harga' => $request->harga[$key],
'jumlah' => $request->jumlah[$key],
'total' => $request->total[$key],
'tanggalpenjualan' => $request->tanggalpenjualan,
'grandtotal' => $request->grandtotalnon,
'uangpembeli' => $request->uangpembeli,
'kembalian' => $request->kembalian,
'metodepembayaran' => $request->metodepembayaran,
'potongan' => $request->potongan[$key],
]);
// Update stock
FacadesDB::table('produk')->where('namaproduk', $namabarang)->decrement('stok', $request->jumlah[$key]);
}
// dd($request);
return redirect('notapenjualan/' . $notajual);
}
public function penjualanhapus($id)
{
FacadesDB::table('penjualan')->where('notajual', $id)->delete();
return redirect('penjualan')->with('success', 'Data Berhasil Dihapus');
}
public function cetaknotapenjualan($id)
{
// Ambil data penjualan yang dikelompokkan berdasarkan 'notajual'
$data['penjualan'] = FacadesDB::table('penjualan')
->select('*')
->groupBy('notajual')
->orderBy('idpenjualan', 'desc')
->where('notajual', $id)->first();
// Buat array untuk menyimpan detail penjualan
$penjualanDetail = [];
// Loop untuk setiap penjualan dan ambil detailnya
$penjualanDetail[$data['penjualan']->notajual] = FacadesDB::table('penjualan')
->where('notajual', $data['penjualan']->notajual)
->get();
// Tambahkan detail penjualan ke data yang akan dikirim ke view
$data['penjualandetail'] = $penjualanDetail;
return view('admin.cetaknotapenjualan', $data);
}
public function cetakfakturpenjualan($id)
{
// Ambil data penjualan yang dikelompokkan berdasarkan 'notajual'
$data['penjualan'] = FacadesDB::table('penjualan')
->select('*')
->groupBy('notajual')
->orderBy('idpenjualan', 'desc')
->where('notajual', $id)->first();
// Buat array untuk menyimpan detail penjualan
$penjualanDetail = [];
// Loop untuk setiap penjualan dan ambil detailnya
$penjualanDetail[$data['penjualan']->notajual] = FacadesDB::table('penjualan')
->where('notajual', $data['penjualan']->notajual)
->get();
// Tambahkan detail penjualan ke data yang akan dikirim ke view
$data['penjualandetail'] = $penjualanDetail;
// dd($data);
return view('admin.cetakfakturpenjualan', $data);
}
public function pelanggan()
{
// Get all pelanggan records
$data['pelanggan'] = FacadesDB::table('pelanggan')->get();
// Getting unique visit counts for each pelanggan based on distinct notajual
$visitCounts = FacadesDB::table('penjualan')
->select('idpelanggan', FacadesDB::raw('COUNT(DISTINCT notajual) as visit_count'))
->groupBy('idpelanggan')
->pluck('visit_count', 'idpelanggan');
// Preparing the visit array
$data['visit'] = [];
foreach ($data['pelanggan'] as $value) {
// Default to 0 if not found
$data['visit'][$value->idpelanggan] = $visitCounts->get($value->idpelanggan, 0);
}
return view('admin.pelanggandaftar', $data);
}
public function pelanggantambah()
{
return view('admin.pelanggantambah');
}
public function pelanggantambahsimpan(Request $request)
{
$request->validate([
'namapelanggan' => 'required',
'alamat' => 'required',
'nohp' => 'required|numeric',
]);
FacadesDB::table('pelanggan')->insert([
'namapelanggan' => $request->input('namapelanggan'),
'alamat' => $request->input('alamat'),
'nohp' => $request->input('nohp'),
]);
return redirect('pelanggandaftar')->with('success', 'Data Berhasil Ditambahkan');
}
public function pelangganedit($id)
{
$data['pelanggan'] = FacadesDB::table('pelanggan')->where('idpelanggan', $id)->first();
return view('admin.pelangganedit', $data);
}
public function pelanggandetail($id)
{
$data['penjualan'] = FacadesDB::table('penjualan')
->select('notajual', 'tanggalpenjualan', 'grandtotal')
->where('idpelanggan', $id)
->groupBy('notajual')
->orderBy('idpenjualan', 'desc')
->get();
// Buat array untuk menyimpan detail penjualan
$penjualanDetail = [];
// Loop untuk setiap penjualan dan ambil detailnya
foreach ($data['penjualan'] as $value) {
$penjualanDetail[$value->notajual] = FacadesDB::table('penjualan')
->where('notajual', $value->notajual)
->get();
}
// Tambahkan detail penjualan ke data yang akan dikirim ke view
$data['penjualandetail'] = $penjualanDetail;
$data['namapelanggan'] = FacadesDB::table('pelanggan')
->select('namapelanggan')
->where('idpelanggan', $id)
->first()
->namapelanggan;
return view('admin.pelanggandetail', $data);
}
public function pelangganeditsimpan(Request $request, $id)
{
$request->validate([
'namapelanggan' => 'required',
'alamat' => 'required',
'nohp' => 'required|numeric',
]);
FacadesDB::table('pelanggan')->where('idpelanggan', $id)->update([
'namapelanggan' => $request->input('namapelanggan'),
'alamat' => $request->input('alamat'),
'nohp' => $request->input('nohp'),
]);
return redirect('pelanggandaftar')->with('success', 'Data Berhasil Diubah');
}
public function pelangganhapus($id)
{
FacadesDB::table('pelanggan')->where('idpelanggan', $id)->delete();
return back()->with('success', 'Data Berhasil Dihapus');
}
public function supplier()
{
// Get all supplier records
$data['supplier'] = FacadesDB::table('supplier')->get();
return view('admin.supplierdaftar', $data);
}
public function suppliertambah()
{
return view('admin.suppliertambah');
}
public function suppliertambahsimpan(Request $request)
{
$request->validate([
'namasupplier' => 'required',
'alamatsupplier' => 'required',
'nohpsupplier' => 'required|numeric',
]);
FacadesDB::table('supplier')->insert([
'namasupplier' => $request->input('namasupplier'),
'alamatsupplier' => $request->input('alamatsupplier'),
'nohpsupplier' => $request->input('nohpsupplier'),
]);
return redirect('supplierdaftar')->with('success', 'Data Berhasil Ditambahkan');
}
public function supplieredit($id)
{
$data['supplier'] = FacadesDB::table('supplier')->where('idsupplier', $id)->first();
return view('admin.supplieredit', $data);
}
public function suppliereditsimpan(Request $request, $id)
{
$request->validate([
'namasupplier' => 'required',
'alamatsupplier' => 'required',
'nohpsupplier' => 'required|numeric',
]);
FacadesDB::table('supplier')->where('idsupplier', $id)->update([
'namasupplier' => $request->input('namasupplier'),
'alamatsupplier' => $request->input('alamatsupplier'),
'nohpsupplier' => $request->input('nohpsupplier'),
]);
return redirect('supplierdaftar')->with('success', 'Data Berhasil Diubah');
}
public function supplierhapus($id)
{
FacadesDB::table('supplier')->where('idsupplier', $id)->delete();
return back()->with('success', 'Data Berhasil Dihapus');
}
public function laporanpenjualan(Request $request)
{
$tanggalAwal = $request->input('tanggal_awal');
$tanggalAkhir = $request->input('tanggal_akhir');
$query = FacadesDB::table('penjualan');
if ($tanggalAwal && $tanggalAkhir) {
$query->whereBetween('tanggalpenjualan', [$tanggalAwal, $tanggalAkhir]);
}
$sales = $query->orderBy('tanggalpenjualan', 'desc')->get();
$totalPemasukan = $sales->sum('total');
// Check the action from the request
if ($request->input('action') === 'cetak') {
// Load the view for the PDF
$pdf = FacadePdf::loadView('admin.laporanpenjualancetak', [
'sales' => $sales,
'totalPemasukan' => $totalPemasukan,
'tanggalAwal' => $tanggalAwal,
'tanggalAkhir' => $tanggalAkhir,
]);
// Return the PDF to the browser
return $pdf->download('laporan_penjualan.pdf');
}
// Return the view with the sales data for the search action
return view('admin.laporanpenjualan', compact('sales', 'totalPemasukan', 'tanggalAwal', 'tanggalAkhir'));
}
public function laporanpembelian(Request $request)
{
$tanggalAwal = $request->input('tanggal_awal');
$tanggalAkhir = $request->input('tanggal_akhir');
$query = FacadesDB::table('barangmasuk');
if ($tanggalAwal && $tanggalAkhir) {
$query->whereBetween('tanggalmasuk', [$tanggalAwal, $tanggalAkhir]);
}
$sales = $query->orderBy('tanggalmasuk', 'desc')->get();
$totalPembelian = $sales->sum('total');
// Check the action from the request
if ($request->input('action') === 'cetak') {
// Load the view for the PDF
$pdf = FacadePdf::loadView('admin.laporanpembeliancetak', [
'sales' => $sales,
'totalPembelian' => $totalPembelian,
'tanggalAwal' => $tanggalAwal,
'tanggalAkhir' => $tanggalAkhir,
]);
// Return the PDF to the browser
return $pdf->download('laporan_pembelian.pdf');
}
// Return the view with the sales data for the search action
return view('admin.laporanpembelian', compact('sales', 'totalPembelian', 'tanggalAwal', 'tanggalAkhir'));
}
public function laporanlabarugi(Request $request)
{
$tanggalawal = $request->input('tanggal_awal', now()->toDateString());
$tanggalakhir = $request->input('tanggal_akhir', now()->toDateString());
// Query untuk menghitung total penjualan per produk
$penjualan = FacadesDB::table('penjualan')
->select('namabarang', FacadesDB::raw('SUM(total) as total_penjualan'))
->whereBetween('tanggalpenjualan', [$tanggalawal, $tanggalakhir])
->groupBy('namabarang')
->get();
// Query untuk menghitung total pembelian per produk
$pembelian = FacadesDB::table('barangmasuk')
->select('namaproduk', FacadesDB::raw('SUM(total) as total_pembelian'))
->whereBetween('tanggalmasuk', [$tanggalawal, $tanggalakhir])
->groupBy('namaproduk')
->get();
// // Konversi hasil query ke array untuk digunakan di view
// $data_penjualan = [];
// foreach ($penjualan as $pjl) {
// $data_penjualan[$pjl->namabarang] = ['total_penjualan' => $pjl->total_penjualan];
// }
// $data_pembelian = [];
// foreach ($pembelian as $pbl) {
// $data_pembelian[$pbl->namaproduk] = ['total_pembelian' => $pbl->total_pembelian];
// }
$data_penjualan = $penjualan->keyBy('namabarang')->toArray();
$data_pembelian = $pembelian->keyBy('namaproduk')->toArray();
// Hitung total pendapatan, beban pokok, dan laba kotor
$total_pendapatan = array_sum(array_column($data_penjualan, 'total_penjualan'));
$total_beban_pokok = array_sum(array_column($data_pembelian, 'total_pembelian'));
$laba_kotor = $total_pendapatan - $total_beban_pokok;
// dd(compact('data_penjualan', 'data_pembelian', 'total_pendapatan', 'total_beban_pokok', 'laba_kotor' ,'tanggalawal', 'tanggalakhir'));
// Check the action from the request
if ($request->input('action') === 'cetak') {
// Load the view for the PDF
$pdf = FacadePdf::loadView('admin.laporanlabarugicetak', [
'data_penjualan' => $data_penjualan,
'data_pembelian' => $data_pembelian,
'total_pendapatan' => $total_pendapatan,
'total_beban_pokok' => $total_beban_pokok,
'tanggalAwal' => $tanggalawal,
'tanggalAkhir' => $tanggalakhir,
]);
// Return the PDF to the browser
return $pdf->download('laporan_labarugi.pdf');
}
return view('admin.laporanlabarugi', compact('data_penjualan', 'data_pembelian', 'total_pendapatan', 'total_beban_pokok', 'laba_kotor', 'tanggalawal', 'tanggalakhir'));
}
public function peramalan(Request $request)
{
$tanggal_awal = $request->input('tanggal_awal', date('Y-m-01')); // Default awal bulan ini
$tanggal_akhir = $request->input('tanggal_akhir', date('Y-m-t')); // Default akhir bulan ini
$periode = $request->input('periode', 3); // Default 3 bulan
$penjualan = FacadesDB::table('penjualan')
->select(FacadesDB::raw('DATE_FORMAT(tanggalpenjualan, "%Y-%m") as bulan'), FacadesDB::raw('SUM(jumlah) as jumlahtotal'))
->whereBetween('tanggalpenjualan', [$tanggal_awal, $tanggal_akhir]) // Filter tanggal
->groupBy('bulan')
->orderBy('bulan', 'asc')
->get();
$data_penjualan = [];
foreach ($penjualan as $p) {
$data_penjualan[] = ['bulan' => $p->bulan, 'jumlahtotal' => $p->jumlahtotal];
}
usort($data_penjualan, function ($a, $b) {
return strcmp($a['bulan'], $b['bulan']);
});
$moving_averages = [];
$grafik = [];
if (count($data_penjualan) >= $periode) {
for ($i = $periode - 1; $i < count($data_penjualan); $i++) {
$sum = 0;
for ($j = 0; $j < $periode; $j++) {
$sum += $data_penjualan[$i - $j]['jumlahtotal'];
}
$rata2 = $sum / $periode;
$moving_averages[] = [
'tanggal' => $data_penjualan[$i]['bulan'],
'rata2' => round($rata2, 2)
];
$grafik[] = [
'bulan' => $data_penjualan[$i]['bulan'],
'jumlah' => $data_penjualan[$i]['jumlahtotal'],
'prediksi' => round($rata2, 2)
];
}
// Prediksi bulan berikutnya
$sum = 0;
for ($k = count($data_penjualan) - $periode; $k < count($data_penjualan); $k++) {
$sum += $data_penjualan[$k]['jumlahtotal'];
}
$prediksi = $sum / $periode;
$lastMonth = end($data_penjualan)['bulan'];
$nextMonth = date('Y-m', strtotime($lastMonth . ' +1 month'));
$moving_averages[] = [
'tanggal' => $nextMonth,
'rata2' => round($prediksi, 2)
];
$grafik[] = [
'bulan' => $nextMonth,
'jumlah' => null,
'prediksi' => round($prediksi, 2)
];
}
return view('admin.peramalan', compact('data_penjualan', 'moving_averages', 'periode', 'grafik', 'tanggal_awal', 'tanggal_akhir'));
}
public function peramalanhasil(Request $request)
{
$tanggal_awal = $request->input('tanggal_awal', date('Y-m-01')); // Default awal bulan ini
$tanggal_akhir = $request->input('tanggal_akhir', date('Y-m-t')); // Default akhir bulan ini
$periode = $request->input('periode', 3); // Default 3 bulan
$penjualan = FacadesDB::table('penjualan')
->select(FacadesDB::raw('DATE_FORMAT(tanggalpenjualan, "%Y-%m") as bulan'), FacadesDB::raw('SUM(jumlah) as jumlahtotal'))
->whereBetween('tanggalpenjualan', [$tanggal_awal, $tanggal_akhir]) // Filter tanggal
->groupBy('bulan')
->orderBy('bulan', 'asc')
->get();
$data_penjualan = [];
foreach ($penjualan as $p) {
$data_penjualan[] = ['bulan' => $p->bulan, 'jumlahtotal' => $p->jumlahtotal];
}
usort($data_penjualan, function ($a, $b) {
return strcmp($a['bulan'], $b['bulan']);
});
$moving_averages = [];
$grafik = [];
$errors = [];
$mape = 0;
if (count($data_penjualan) >= $periode) {
for ($i = $periode - 1; $i < count($data_penjualan); $i++) {
$sum = 0;
for ($j = 0; $j < $periode; $j++) {
$sum += $data_penjualan[$i - $j]['jumlahtotal'];
}
$rata2 = $sum / $periode;
$actual = $data_penjualan[$i]['jumlahtotal'];
// MAPE Calculation
$error = abs(($actual - $rata2) / $actual) * 100;
$errors[] = $error;
$moving_averages[] = [
'tanggal' => $data_penjualan[$i]['bulan'],
'rata2' => round($rata2, 2),
'error' => round($error, 2)
];
$grafik[] = [
'bulan' => $data_penjualan[$i]['bulan'],
'jumlah' => $actual,
'prediksi' => round($rata2, 2)
];
}
// Hitung MAPE rata-rata
if (count($errors) > 0) {
$mape = array_sum($errors) / count($errors);
}
// Prediksi bulan berikutnya
$sum = 0;
for ($k = count($data_penjualan) - $periode; $k < count($data_penjualan); $k++) {
$sum += $data_penjualan[$k]['jumlahtotal'];
}
$prediksi = $sum / $periode;
$lastMonth = end($data_penjualan)['bulan'];
$nextMonth = date('Y-m', strtotime($lastMonth . ' +1 month'));
$moving_averages[] = [
'tanggal' => $nextMonth,
'rata2' => round($prediksi, 2),
'error' => null // Tidak ada error untuk prediksi bulan depan
];
$grafik[] = [
'bulan' => $nextMonth,
'jumlah' => null,
'prediksi' => round($prediksi, 2)
];
}
return view('admin.peramalanhasil', compact('data_penjualan', 'moving_averages', 'periode', 'grafik', 'tanggal_awal', 'tanggal_akhir', 'mape'));
}
}