270 lines
10 KiB
PHP
270 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\MasterData;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Barang;
|
|
use App\Models\Kategori;
|
|
use App\Models\Penjualan;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Models\ListItemPembelian;
|
|
use App\Models\Pembelian;
|
|
|
|
class BarangController extends Controller
|
|
{
|
|
private function handleFileUpload(Request $request)
|
|
{
|
|
if ($request->hasFile('gambar')) {
|
|
$gambar = $request->file('gambar');
|
|
$fileName = $this->generateFileName($gambar);
|
|
$gambar->move(public_path('gambar'), $fileName);
|
|
return $fileName;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private function generateFileName($file)
|
|
{
|
|
return time() . '.' . $file->getClientOriginalExtension();
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
// $data = DB::table('list_item_penjualan')->selectRaw("barang_id,SUM(qty) as jumlah_jual")
|
|
// ->groupBy("barang_id")
|
|
// ->get();
|
|
// $pembelian = Pembelian::create([
|
|
// 'operator' => Auth::user()->id,
|
|
// 'no_transaksi' => "IDTRN1716529745",
|
|
// // 'total_harga'=> $request->total,
|
|
// // 'bayar'=> $request->bayar,
|
|
// // 'sisa'=> $request->sisa,
|
|
// 'supplier_id'=> 1
|
|
// ]);
|
|
// foreach($data as $value){
|
|
// $barang = Barang::where('id', $value->barang_id)->first();
|
|
|
|
// ListItemPembelian::create([
|
|
// 'pembelian_id' => $pembelian->id,
|
|
// 'barang_id' => $barang->id,
|
|
// 'qty_beli' => $value->jumlah_jual,
|
|
// 'harga_beli' => $barang->harga_beli,
|
|
// 'harga_jual' => $barang->harga_jual,
|
|
// 'nama_barang' => $barang->nama,
|
|
// 'ukuran_barang' => $barang->ukuran
|
|
// ]);
|
|
// }
|
|
|
|
// die();
|
|
$data = [
|
|
'title' => 'Data Barang',
|
|
'kategori' => Kategori::all(),
|
|
'barang' => Barang::with("kategori")
|
|
->selectRaw("barang.*,barang.id as barang_id,(IFNULL(jumlah_beli,0)-IFNULL(jumlah_jual,0)) as stok_terbaru")
|
|
->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()
|
|
];
|
|
|
|
if (!$request->ajax() && $request->isMethod('get')) {
|
|
|
|
$res_barang = [];
|
|
foreach($data['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
|
|
]);
|
|
|
|
|
|
}
|
|
$data['barang'] = $res_barang;
|
|
return view('page.dashboard.barang.index', compact('data'));
|
|
}
|
|
|
|
if ($request->ajax() && $request->isMethod('get')) {
|
|
$perPage = $request->input('per_page', 10);
|
|
$query = Barang::query()->with('kategori');
|
|
|
|
$searchTerm = $request->input('search');
|
|
|
|
if ($searchTerm) {
|
|
$query->where(function($q) use ($searchTerm) {
|
|
$q->where('kode', 'like', "%$searchTerm%")
|
|
->orWhere('nama', 'like', "%$searchTerm%")
|
|
->orWhere('ukuran', 'like', "%$searchTerm%")
|
|
->orWhere('harga_beli', 'like', "%$searchTerm%")
|
|
->orWhere('harga_jual', 'like', "%$searchTerm%")
|
|
->orWhere('stok', 'like', "%$searchTerm%")
|
|
->orWhere('kadaluarsa', 'like', "%$searchTerm%")
|
|
->orWhere('id_kategori', 'like', "%$searchTerm%")
|
|
->orWhere('created_at', 'like', "%$searchTerm%");
|
|
});
|
|
}
|
|
|
|
$totalMonths = Penjualan::select(
|
|
DB::raw('YEAR(created_at) as year'),
|
|
DB::raw('MONTH(created_at) as month'),
|
|
DB::raw('COUNT(*) as count')
|
|
)
|
|
->groupBy('year', 'month')
|
|
->get();
|
|
|
|
$totalItems = $totalMonths->sum('count');
|
|
$totalPerbulanNow = $totalItems / $totalMonths->count();
|
|
|
|
$totalPerbulanKemarin = Penjualan::whereMonth('created_at', '=', Carbon::now()->subMonth()->month)->count();
|
|
|
|
$data = $query->paginate($perPage);
|
|
|
|
if ($data->isEmpty()) {
|
|
return response()->json(['message' => 'Tidak ada data!'], 404);
|
|
}
|
|
|
|
return response()->json([
|
|
'message' => $data,
|
|
'totalPerbulanNow' => $totalPerbulanNow,
|
|
'totalPerbulanKemarin' => $totalPerbulanKemarin
|
|
], 200);
|
|
}
|
|
|
|
$gambar = $this->handleFileUpload($request);
|
|
|
|
$kode = $request->kode;
|
|
$checkKode = Barang::where('kode', $kode)->first();
|
|
if ($checkKode) {
|
|
return response()->json(['message' => 'Barang sudah ada!'], 200);
|
|
}
|
|
|
|
$create = Barang::create([
|
|
'kode' => $kode,
|
|
'nama' => $request->nama,
|
|
'ukuran' => $request->ukuran,
|
|
'harga_beli' => $request->harga_beli,
|
|
'harga_jual' => $request->harga_jual,
|
|
'stok' => $request->stok,
|
|
'kadaluarsa' => $request->kadaluarsa,
|
|
'demand' => $request->demand,
|
|
'holding_cost' => $request->holding_cost,
|
|
'biaya_pemesanan' => $request->biaya_pemesanan,
|
|
'lead_time_variance' => $request->lead_time_variance,
|
|
'lead_time' => $request->lead_time,
|
|
'id_kategori' => $request->id_kategori,
|
|
'gambar' => $gambar
|
|
]);
|
|
|
|
if ($create) {
|
|
return response()->json(['message' => 'Berhasil menambah data!'], 200);
|
|
} else {
|
|
return response()->json(['message' => 'Gagal menambah data!'], 404);
|
|
}
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
if ($request->ajax()) {
|
|
$data = Barang::with('kategori')->findOrFail($id);
|
|
|
|
if (!$data) {
|
|
return response()->json(['message' => 'Data tidak ditemukan!'], 404);
|
|
}
|
|
|
|
if ($request->isMethod('get')) {
|
|
return response()->json(['message' => $data], 200);
|
|
}
|
|
|
|
$data->update($request->only(['kode', 'id_kategori','nama', 'ukuran', 'harga_jual', 'harga_beli', 'stok', 'kadaluarsa', 'demand', 'biaya_pemesanan','holding_cost', 'lead_time_variance', 'lead_time', 'updated_at']));
|
|
|
|
$gambar = $this->handleFileUpload($request);
|
|
|
|
if ($gambar) {
|
|
$data->gambar = $gambar;
|
|
$data->save();
|
|
}
|
|
|
|
return response()->json(['message' => 'Berhasil update data!'], 200);
|
|
}
|
|
}
|
|
|
|
public function delete(Request $request, $id)
|
|
{
|
|
if ($request->ajax()) {
|
|
$data = Barang::findOrFail($id);
|
|
if ($data->delete()) {
|
|
return response()->json(['message' => 'Berhasil hapus data!'], 200);
|
|
} else {
|
|
return response()->json(['message' => 'Gagal hapus data!'], 404);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function findBarcode($kode)
|
|
{
|
|
return response()->json(Barang::where('kode', $kode)->first());
|
|
}
|
|
|
|
}
|