app/Http/Controllers/PembelianController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request, $id)
    {
        $request->validate([
            'supplier'     =>  'required',
            'barang'     =>  'required',
            'total_barang'     =>  'required|numeric|min:0',
            'total_bayar'     =>  'required|numeric',
        ]);
        $pembelian = Pembelian::where('id', $id)->first();
        $pembelian->supplier            = $request->supplier;
        $pembelian->barang              = $request->barang;
        $pembelian->total_barang        = $request->total_barang;
        $pembelian->total_bayar         = $request->total_bayar;
        $pembelian->hari                = $request->hari;
        $hari = Carbon::now(new \DateTimeZone('Asia/Jakarta'))->format('Y-m-d');
        $bulan = Carbon::now(new \DateTimeZone('Asia/Jakarta'))->format('Y-m');
        $labaRugi = LabaRugi::where('hari', $hari)->first();
        if (!$labaRugi) {
            $labaRugi = new LabaRugi();
            $labaRugi->total_pemasukan = 0;
            $labaRugi->created_at = Carbon::now(new \DateTimeZone('Asia/Jakarta'));
            $labaRugi->total_pengeluaran = $pembelian->total_bayar - $pembelian->total_bayar;
            $labaRugi->hasil = $pembelian->total_bayar - $pembelian->total_bayar;
            $labaRugi->hari = $hari;
            $labaRugi->bulan = $bulan;
            $labaRugi->save();
        }
        $pembelianDB = Pembelian::where('id', $id)->first();
        $labaRugi->total_pengeluaran = $labaRugi->total_pengeluaran - $pembelianDB->total_bayar + $pembelian->total_bayar;
        $labaRugi->hasil = $labaRugi->hasil - $pembelian->total_bayar + $pembelianDB->total_bayar ;
        $pembelian->update();
        $labaRugi->save();
        return redirect()->route('pembelian.index')->with('info', 'Data transaksi pembelian berhasil diupdate');
    }
public function update ( Request $ request , $ id ) { $ request -> validate ( [ 'supplier' => 'required' , 'barang' => 'required' , 'total_barang' => 'required|numeric|min:0' , 'total_bayar' => 'required|numeric' , ] ) ; $ pembelian = Pembelian :: where ( 'id' , $ id ) -> first ( ) ; $ pembelian -> supplier = $ request -> supplier ; $ pembelian -> barang = $ request -> barang ; $ pembelian -> total_barang = $ request -> total_barang ; $ pembelian -> total_bayar = $ request -> total_bayar ; $ pembelian -> hari = $ request -> hari ; $ hari = Carbon :: now ( new \ DateTimeZone ( 'Asia/Jakarta' ) ) -> format ( 'Y-m-d' ) ; $ bulan = Carbon :: now ( new \ DateTimeZone ( 'Asia/Jakarta' ) ) -> format ( 'Y-m' ) ; $ labaRugi = LabaRugi :: where ( 'hari' , $ hari ) -> first ( ) ; if ( ! $ labaRugi ) { $ labaRugi = new LabaRugi ( ) ; $ labaRugi -> total_pemasukan = 0 ; $ labaRugi -> created_at = Carbon :: now ( new \ DateTimeZone ( 'Asia/Jakarta' ) ) ; $ labaRugi -> total_pengeluaran = $ pembelian -> total_bayar - $ pembelian -> total_bayar ; $ labaRugi -> hasil = $ pembelian -> total_bayar - $ pembelian -> total_bayar ; $ labaRugi -> hari = $ hari ; $ labaRugi -> bulan = $ bulan ; $ labaRugi -> save ( ) ; } $ pembelianDB = Pembelian :: where ( 'id' , $ id ) -> first ( ) ; $ labaRugi -> total_pengeluaran = $ labaRugi -> total_pengeluaran - $ pembelianDB -> total_bayar + $ pembelian -> total_bayar ; $ labaRugi -> hasil = $ labaRugi -> hasil - $ pembelian -> total_bayar + $ pembelianDB -> total_bayar ; $ pembelian -> update ( ) ; $ labaRugi -> save ( ) ; return redirect ( ) -> route ( 'pembelian.index' ) -> with ( 'info' , 'Data transaksi pembelian berhasil diupdate' ) ; }
Update the specified resource in storage.
Update the specified resource in storage .
// masukkan ke database laba_rugi