<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Generateitems extends CI_Controller {


public function __construct()
{
    parent::__construct();
    $this->load->model('DynamicModel', 'dm');
}


public function generatepdf()
{
    if(ob_get_length()){
        ob_end_clean();
    }

    $this->db->select('
        items.id,
        items.nama_barang,
        items.kode_detail,
        items.harga_beli,
        items.harga_jual,
        items.created_at,
        items.tanggal_pembelian,
        items.stok,
        (
            SELECT GROUP_CONCAT(DISTINCT w.nama SEPARATOR ", ")
            FROM stock_logs sl
            LEFT JOIN warehouses w ON sl.warehouse_id = w.id
            WHERE sl.item_id = items.id
        ) as gudang
    ');

    $this->db->where('items.status','active');
    $this->db->where('items.stok >', 0); // <-- stok 0 agar tidak muncul

    $this->db->order_by('(items.stok = 0)','ASC',false);
    $this->db->order_by('items.tanggal_pembelian','ASC');
    $this->db->order_by('items.kode_detail','ASC');

    $data = $this->db->get('items')->result();


    $company = $this->db->get('pengaturan')->row_array();


    require_once(APPPATH . 'third_party/fpdf/fpdf.php');
    require_once(APPPATH.  'libraries/PDF_Items.php');

    $pdf = new PDF();

$pdf->setHeaderData(
    $company['nama_perusahaan'],
    $company['alamat_perusahaan'],
    $company['telepon_perusahaan'],
    FCPATH.'uploads/img/logo-ljn.png'
);

$pdf->AddPage('P', 'A4');
$pdf->ItemsTable($data);
$pdf->TotalBarang(count($data));
// $pdf->Pembuat($company['nama_bendahara']);
$pdf->Output('I','Data_Barang_'.date('YmdHis').'.pdf');
}
}