Untitled

mail@pastecode.io avatar
unknown
plain_text
21 days ago
4.0 kB
2
Indexable
Never
<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use App\Models\ModelPembelian;

class Pembelian extends BaseController
{
    protected $ModelPembelian;

    public function __construct()
    {
        $this->ModelPembelian = new ModelPembelian();
    }

    public function index()
    {
        $data = [
            'judul' => 'Pembelian',
            'produk' => $this->ModelPembelian->AllProduk(),
            'cart_items' => session()->get('cart_items') ?? [],
        ];
        return view('v_pembelian', $data);
    }

    public function getProdukDetails($kode_produk)
    {
        if ($this->request->isAJAX()) {
            $produk = $this->ModelPembelian->getProdukById($kode_produk);
            return $this->response->setJSON($produk);
        }
    }

    public function addToCart()
    {
        $kode_produk = $this->request->getPost('kode_produk');
        $qty = $this->request->getPost('qty');
        $produk = $this->ModelPembelian->getProdukById($kode_produk);

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

            // Cek apakah produk sudah ada di cart
            $found = false;
            foreach ($cart_items as &$item) {
                if ($item['kode_produk'] == $kode_produk) {
                    $item['qty'] += $qty;
                    $item['total_harga'] = $item['qty'] * $item['harga_jual'];
                    $found = true;
                    break;
                }
            }
            unset($item);

            if (!$found) {
                $cart_items[] = [
                    'kode_produk' => $kode_produk,
                    'nama_produk' => $produk['nama_produk'],
                    'nama_kategori' => $produk['nama_kategori'],
                    'nama_satuan' => $produk['nama_satuan'],
                    'harga_jual' => $produk['harga_jual'],
                    'qty' => $qty,
                    'total_harga' => $qty * $produk['harga_jual']
                ];
            }

            session()->set('cart_items', $cart_items);
            session()->setFlashdata('pesan', 'Produk berhasil ditambahkan ke keranjang');
        }

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

    public function removeItem($kode_produk)
    {
        $cart_items = session()->get('cart_items') ?? [];

        foreach ($cart_items as $key => $item) {
            if ($item['kode_produk'] == $kode_produk) {
                unset($cart_items[$key]);
                break;
            }
        }

        session()->set('cart_items', array_values($cart_items));
        session()->setFlashdata('pesan', 'Produk berhasil dihapus dari keranjang');

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

    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();

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

        foreach ($cart_items as $item) {
            $dataRinciBeli = [
                'id_beli' => $id_beli,
                'kode_produk' => $item['kode_produk'], // Perbaiki dari $item['id'] ke $item['kode_produk']
                '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