Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.1 kB
3
Indexable
Never
public function savePembelian()
    {
        // Simpan ke tbl_beli
        $dataBeli = [
            'tgl_beli' => date('Y-m-d'),
            'jam' => date('H:i:s'),
            'id_user' => session()->get('id_user'),
        ];

        $this->ModelPembelian->InsertBeli($dataBeli);
        $id_beli = $this->ModelPembelian->getInsertID(); // Perbaiki ini untuk mendapatkan ID terakhir yang diinsert

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

        foreach ($cart_items as $item) {
            $dataRinciBeli = [
                'id_beli' => $id_beli,
                'kode_produk' => $item['id'],
                'qty' => $item['qty'],
            ];
            $this->ModelPembelian->InsertRinciBeli($dataRinciBeli);
            $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