Untitled

mail@pastecode.io avatar
unknown
plain_text
19 days ago
1.1 kB
1
Indexable
Never
public function savePembelian()
    {
        //simpan ke tbl_beli
        $data = [
            'tgl_beli' => date('Y-m-d'),
            'jam' => date('H:i:s'),
            'id_user' => session()->get('id_user'),
        ];
        // $data = new ModelPembelian();
        $this->ModelPembelian->InsertBeli($data);

        $cart_items = session()->get('cart_items') ?? [];
        $id_beli = $data->getInsertID();

        foreach ($cart_items as $item) {
            $data = [
                'id_beli' => $id_beli,
                'kode_produk' => $item['id'],
                'qty' => $item['qty'],
            ];
            $this->ModelPembelian->InsertRinciBeli($data);

            $this->ModelPembelian->updateStok($item['kode_produk'], $item['qty']);
        }

        // Kosongkan cart setelah pembelian berhasil disimpan
        session()->remove('cart_items');
        session()->setFlashdata('pesan', 'Pembelian berhasil disimpan dan stok diperbarui');

        return redirect()->to(base_url('Pembelian'));
    }
Leave a Comment