<?php

namespace App\Models;

use CodeIgniter\Model;

class CartItemModel extends Model {
    protected $table = 'cart_items';
    protected $primaryKey = 'id_cart_item';
    protected $allowedFields = ['id_cart', 'id_product', 'qty', 'harga', 'diskon', 'subtotal'];


    /**
     * Menghitung total item di keranjang tertentu
     */
    public function getSubtotal($id_cart)
    {
        return $this->db->table('cart_items')
            ->join('products', 'products.id_product = cart_items.id_product')
            ->selectSum('qty * products.harga_jual', 'subtotal')
            ->where('id_cart', $id_cart)
            ->get()->getRow()->subtotal;
    }
}