<?php

namespace App\Models;

use CodeIgniter\Model;

class SaleDetailModel extends Model
{
    protected $table         = 'sale_detail';
    protected $primaryKey    = 'id';

    protected $allowedFields = [
        'sale_id',
        'item_id',
        'qty',
        'harga',
        'subtotal'
    ];

    /**
     * Ambil detail penjualan berdasarkan ID transaksi
     *
     * @param int $saleId
     * @return array
     */
    public function getDetailBySale($saleId)
    {
        return $this->select('sale_detail.*, items.nama AS nama_item')
                    ->join('items', 'items.id = sale_detail.item_id')
                    ->where('sale_id', $saleId)
                    ->findAll();
    }

    public function getBySaleId($saleId)
    {
        return $this->select('sale_detail.*, items.nama as nama_item')
                    ->join('items', 'items.id = sale_detail.item_id', 'left')
                    ->where('sale_id', $saleId)
                    ->findAll();
    }
}
