app/Http/Controllers/PembelianController.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([
            'supplier'     =>  'required',
            'barang'     =>  'required',
            'total_barang'     =>  'required|numeric|min:0',
            'total_bayar'     =>  'required|numeric',
        ]);
        $hari = Carbon::now(new \DateTimeZone('Asia/Jakarta'))->format('Y-m-d');
        $bulan = Carbon::now(new \DateTimeZone('Asia/Jakarta'))->format('Y-m');
        $pembelian = new Pembelian();
        $supplier = Supplier::where('nama', $request->supplier)->first();
        if (!$supplier) {
            $supplier = new Supplier();
            $supplier->nama = $request->supplier;
            $supplier->save();
        }
        $pembelian->supplier            = $request->supplier;
        $pembelian->barang              = $request->barang;
        $pembelian->total_barang        = $request->total_barang;
        $pembelian->total_bayar         = $request->total_bayar;
        $pembelian->hari                = $hari;
        $pembelian->bulan               = $bulan;
        $pembelian->save();
        $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();
        }
        $labaRugi->total_pengeluaran = $labaRugi->total_pengeluaran + $pembelian->total_bayar;
        $labaRugi->hasil = $labaRugi->hasil - $pembelian->total_bayar;
        $labaRugi->save();
        return redirect()->route('pembelian.index')->with('success', 'Data transaksi pembelian berhasil ditambahkan');
    }
public function store ( Request $ request ) { $ request -> validate ( [ 'supplier' => 'required' , 'barang' => 'required' , 'total_barang' => 'required|numeric|min:0' , 'total_bayar' => 'required|numeric' , ] ) ; $ hari = Carbon :: now ( new \ DateTimeZone ( 'Asia/Jakarta' ) ) -> format ( 'Y-m-d' ) ; $ bulan = Carbon :: now ( new \ DateTimeZone ( 'Asia/Jakarta' ) ) -> format ( 'Y-m' ) ; $ pembelian = new Pembelian ( ) ; $ supplier = Supplier :: where ( 'nama' , $ request -> supplier ) -> first ( ) ; if ( ! $ supplier ) { $ supplier = new Supplier ( ) ; $ supplier -> nama = $ request -> supplier ; $ supplier -> save ( ) ; } $ pembelian -> supplier = $ request -> supplier ; $ pembelian -> barang = $ request -> barang ; $ pembelian -> total_barang = $ request -> total_barang ; $ pembelian -> total_bayar = $ request -> total_bayar ; $ pembelian -> hari = $ hari ; $ pembelian -> bulan = $ bulan ; $ pembelian -> save ( ) ; $ 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 ( ) ; } $ labaRugi -> total_pengeluaran = $ labaRugi -> total_pengeluaran + $ pembelian -> total_bayar ; $ labaRugi -> hasil = $ labaRugi -> hasil - $ pembelian -> total_bayar ; $ labaRugi -> save ( ) ; return redirect ( ) -> route ( 'pembelian.index' ) -> with ( 'success' , 'Data transaksi pembelian berhasil ditambahkan' ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
// masukkan ke database pembelian // jika supplier tidak ditemukan // dd($supplier); // masukkan ke database laba_rugi