app/Http/Controllers/BarangController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $request)
    {
        $request->validate([
            'nama' => 'required|string|max:255',
            'harga_beli' => 'required|integer|min:0',
            'harga_jual' => 'required|integer|min:0',
            'merek' => 'required|string|max:255',
            'stok' => 'required|integer',
            'diskon' => 'required|integer|min:0',
            'kategori_id' => 'required|not_in:0'
        ]);
        $barang = Barang::where('nama', $request->nama)->first();
        if (!$barang) {
            $barang = new Barang();
            $barang->nama           = $request->nama;
            $barang->kode           = mt_rand(100000, 999999);
            $barang->harga_beli     = $request->harga_beli;
            $barang->harga_jual     = $request->harga_jual;
            $barang->kategori_id    = $request->kategori_id;
            $barang->merek          = $request->merek;
            $barang->stok           = $request->stok;
            $barang->diskon         = $request->diskon ?: 0;
            $barang->save();
            $stokBarang = new LaporanStok();
            $stokBarang->barang_id = $barang->id;
            $stokBarang->barang_masuk = $barang->stok;
            $stokBarang->sisa = $barang->stok;
            $stokBarang->hari =  Carbon::now(new \DateTimeZone('Asia/Jakarta'))->format('Y-m-d');
            $stokBarang->bulan =  Carbon::now(new \DateTimeZone('Asia/Jakarta'))->format('Y-m');
            $stokBarang->save();
            return redirect()->route('barang.index')->with('success', 'Data barang berhasil ditambahkan');
        }
        $barang = Barang::where('nama', $request->nama)->first();
        $barang->stok = $barang->stok + $request->stok;
        $barang->save();
        $hari = Carbon::now(new \DateTimeZone('Asia/Jakarta'))->format('Y-m-d');
        $bulan = Carbon::now(new \DateTimeZone('Asia/Jakarta'))->format('Y-m');
        $stokBarang = LaporanStok::where('hari', $hari)->where('bulan', $bulan)->where('barang_id', $barang->id)->first();
        if (!$stokBarang) {
            $stokBarang = new LaporanStok();
            $stokBarang->barang_id = $barang->id;
            $stokBarang->barang_masuk = $barang->stok - $barang->stok;
            $stokBarang->sisa = $barang->stok;
            $stokBarang->hari =  $hari;
            $stokBarang->bulan =  $bulan;
            $stokBarang->save();
        }
        $stokBarang->barang_id = $barang->id;
        $stokBarang->barang_masuk =  $request->stok + $stokBarang->barang_masuk;
        $stokBarang->sisa = $barang->stok;
        $stokBarang->hari =  $hari;
        $stokBarang->bulan =  $bulan;
        $stokBarang->save();
        return redirect()->route('barang.index')->with('success', 'Data barang berhasil ditambahkan');
    }
public function store ( Request $ request ) { $ request -> validate ( [ 'nama' => 'required|string|max:255' , 'harga_beli' => 'required|integer|min:0' , 'harga_jual' => 'required|integer|min:0' , 'merek' => 'required|string|max:255' , 'stok' => 'required|integer' , 'diskon' => 'required|integer|min:0' , 'kategori_id' => 'required|not_in:0' ] ) ; $ barang = Barang :: where ( 'nama' , $ request -> nama ) -> first ( ) ; if ( ! $ barang ) { $ barang = new Barang ( ) ; $ barang -> nama = $ request -> nama ; $ barang -> kode = mt_rand ( 100000 , 999999 ) ; $ barang -> harga_beli = $ request -> harga_beli ; $ barang -> harga_jual = $ request -> harga_jual ; $ barang -> kategori_id = $ request -> kategori_id ; $ barang -> merek = $ request -> merek ; $ barang -> stok = $ request -> stok ; $ barang -> diskon = $ request -> diskon ? : 0 ; $ barang -> save ( ) ; $ stokBarang = new LaporanStok ( ) ; $ stokBarang -> barang_id = $ barang -> id ; $ stokBarang -> barang_masuk = $ barang -> stok ; $ stokBarang -> sisa = $ barang -> stok ; $ stokBarang -> hari = Carbon :: now ( new \ DateTimeZone ( 'Asia/Jakarta' ) ) -> format ( 'Y-m-d' ) ; $ stokBarang -> bulan = Carbon :: now ( new \ DateTimeZone ( 'Asia/Jakarta' ) ) -> format ( 'Y-m' ) ; $ stokBarang -> save ( ) ; return redirect ( ) -> route ( 'barang.index' ) -> with ( 'success' , 'Data barang berhasil ditambahkan' ) ; } $ barang = Barang :: where ( 'nama' , $ request -> nama ) -> first ( ) ; $ barang -> stok = $ barang -> stok + $ request -> stok ; $ barang -> save ( ) ; $ hari = Carbon :: now ( new \ DateTimeZone ( 'Asia/Jakarta' ) ) -> format ( 'Y-m-d' ) ; $ bulan = Carbon :: now ( new \ DateTimeZone ( 'Asia/Jakarta' ) ) -> format ( 'Y-m' ) ; $ stokBarang = LaporanStok :: where ( 'hari' , $ hari ) -> where ( 'bulan' , $ bulan ) -> where ( 'barang_id' , $ barang -> id ) -> first ( ) ; if ( ! $ stokBarang ) { $ stokBarang = new LaporanStok ( ) ; $ stokBarang -> barang_id = $ barang -> id ; $ stokBarang -> barang_masuk = $ barang -> stok - $ barang -> stok ; $ stokBarang -> sisa = $ barang -> stok ; $ stokBarang -> hari = $ hari ; $ stokBarang -> bulan = $ bulan ; $ stokBarang -> save ( ) ; } $ stokBarang -> barang_id = $ barang -> id ; $ stokBarang -> barang_masuk = $ request -> stok + $ stokBarang -> barang_masuk ; $ stokBarang -> sisa = $ barang -> stok ; $ stokBarang -> hari = $ hari ; $ stokBarang -> bulan = $ bulan ; $ stokBarang -> save ( ) ; return redirect ( ) -> route ( 'barang.index' ) -> with ( 'success' , 'Data barang berhasil ditambahkan' ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
// masukkan ke database stok // masukkan ke database stok