588 lines
18 KiB
Markdown
588 lines
18 KiB
Markdown
# Salinan: Data Barang & Print Barcode
|
||
|
||
Sumber yang dibaca:
|
||
|
||
- `application/views/items/layout.php` — halaman **Data Barang** (`active_menu = items`)
|
||
- `application/views/items/draft_items.php` — halaman **Data Barang Dalam Perjalanan** (draft)
|
||
- `application/controllers/Items.php` — endpoint `get_data()` dan `print_barcode()`
|
||
|
||
Dokumen ini menyalin kode dan **ketentuan PDF print barcode** apa adanya. Tidak ada perubahan aturan.
|
||
|
||
---
|
||
|
||
## 1. Ringkasan Alur
|
||
|
||
1. Halaman Data Barang memuat DataTables dari `items/get_data`.
|
||
2. Tombol **Print Barcode** memakai `data-id` = `items.id`.
|
||
3. Klik tombol membuka dialog Swal:
|
||
- Jumlah Kolom: 1 / 2 / 3 (default **3**).
|
||
- Jumlah Cetak per Barcode: 1 / 2 label (default **1**).
|
||
4. Browser membuka:
|
||
`items/print_barcode/{item_id}?column={column}&duplicat={duplicat}`
|
||
5. Controller `Items::print_barcode()` membuat PDF FPDF berisi QR Code + teks barcode + serial number, lalu mengirimnya inline dengan nama `Barcode-{item_id}.pdf`.
|
||
|
||
---
|
||
|
||
## 2. Ketentuan PDF Print Barcode (PERSIS)
|
||
|
||
### 2.1 Sumber data
|
||
|
||
| Ketentuan | Nilai persis |
|
||
|---|---|
|
||
| Tabel | `item_barcodes` |
|
||
| Filter | `item_id = {item_id}` |
|
||
| Filter status | `status != 'sold_out'` |
|
||
| Urutan | `id ASC` |
|
||
| Jika kosong | `show_error('Barcode tidak ditemukan.')` |
|
||
| Duplikasi | Setiap baris diulang `duplicat` kali secara berurutan |
|
||
|
||
### 2.2 Parameter halaman
|
||
|
||
| Parameter | Nilai persis |
|
||
|---|---|
|
||
| Library | FPDF (`application/third_party/fpdf/fpdf.php`) |
|
||
| QR library | PHP QR Code (`application/libraries/phpqrcode/qrlib.php`) |
|
||
| Orientasi | `P` (portrait) |
|
||
| Unit | `mm` |
|
||
| Lebar label | `33.3` mm |
|
||
| Tinggi label | `23.2` mm |
|
||
| Jarak antar label X (`spaceX`) | `0` |
|
||
| Jarak antar label Y (`spaceY`) | `0` |
|
||
| Baris per halaman | `6` |
|
||
| Margin kiri | `0` |
|
||
| Margin atas | `0` |
|
||
| Margin kanan | `0` |
|
||
| Auto page break | `false` |
|
||
| Lebar kertas | `33.3 × column` mm |
|
||
| Tinggi kertas | `23.2 × 6 = 139.2` mm |
|
||
| Label per halaman | `column × 6` |
|
||
| Posisi X label | `col × 33.3` |
|
||
| Posisi Y label | `line × 23.2` |
|
||
| Pindah halaman | Setiap indeks ke-`labelPerPage` (18 untuk 3 kolom) |
|
||
|
||
### 2.3 Pilihan dari UI
|
||
|
||
| Parameter | Nilai yang diterima | Default / fallback |
|
||
|---|---|---|
|
||
| `column` | `1`, `2`, `3` | `3` jika kosong / tidak valid |
|
||
| `duplicat` | integer `>= 1` | `1` jika `< 1` |
|
||
|
||
Ukuran kertas hasil:
|
||
|
||
| Column | Lebar kertas | Tinggi kertas | Label per halaman |
|
||
|---|---|---|---|
|
||
| 1 | 33.3 mm | 139.2 mm | 6 |
|
||
| 2 | 66.6 mm | 139.2 mm | 12 |
|
||
| 3 | 99.9 mm | 139.2 mm | 18 |
|
||
|
||
### 2.4 Tata letak tiap label
|
||
|
||
Koordinat dihitung dari pojok kiri-atas label (`x`, `y`):
|
||
|
||
| Elemen | Posisi | Ukuran / setting |
|
||
|---|---|---|
|
||
| QR Code | `x + 16`, `y + 4` | `15 × 15` mm |
|
||
| Teks barcode | `x + 2`, `y + 4` | `MultiCell(14, 3, barcode, 0, 'L')` |
|
||
| Font teks barcode | — | `Arial`, `B`, `7` pt |
|
||
| Teks serial number | `x + 2`, `y + 11` | `MultiCell(16, 3, 'SN: ' . serial, 0, 'L')` |
|
||
| Font serial number | — | `Arial`, `B`, `4.5` pt |
|
||
| Serial kosong | — | Ditampilkan `-` |
|
||
|
||
Ilustrasi satu label (33.3 × 23.2 mm):
|
||
|
||
```text
|
||
+-----------------------------------+
|
||
| [teks barcode] [ QR 15x15 ] |
|
||
| x+2, y+4 x+16, y+4 |
|
||
| |
|
||
| SN: xxxxxxxx |
|
||
| x+2, y+11 |
|
||
+-----------------------------------+
|
||
```
|
||
|
||
### 2.5 QR Code
|
||
|
||
| Ketentuan | Nilai persis |
|
||
|---|---|
|
||
| Isi QR | `item_barcodes.barcode` |
|
||
| Lokasi cache | `FCPATH . 'uploads/temp_qr/'` |
|
||
| Nama file | `{barcode}.png` |
|
||
| Dibuat jika belum ada | Ya (`file_exists`) |
|
||
| Error correction | `QR_ECLEVEL_L` |
|
||
| Ukuran modul | `4` |
|
||
| Margin QR | `1` |
|
||
| Ukuran tempel di PDF | `15 × 15` mm |
|
||
|
||
Catatan: file QR hanya dibuat sekali berdasarkan nama barcode. Jika isi barcode sama, file lama dipakai ulang.
|
||
|
||
### 2.6 Output
|
||
|
||
| Ketentuan | Nilai persis |
|
||
|---|---|
|
||
| Mode output FPDF | `I` (inline di browser) |
|
||
| Nama file | `Barcode-{item_id}.pdf` |
|
||
| Halaman pertama | Selalu `AddPage()` sebelum render |
|
||
| Page break otomatis | Dimatikan |
|
||
|
||
---
|
||
|
||
## 3. Salinan Kode Sumber
|
||
|
||
### 3.1 `application/controllers/Items.php` — `print_barcode()`
|
||
|
||
```php
|
||
public function print_barcode($item_id)
|
||
{
|
||
require_once APPPATH.'third_party/fpdf/fpdf.php';
|
||
require_once APPPATH.'libraries/phpqrcode/qrlib.php';
|
||
|
||
$barcodes = $this->db
|
||
->where('item_id',$item_id)
|
||
->where('status !=', 'sold_out')
|
||
->order_by('id','ASC')
|
||
->get('item_barcodes')
|
||
->result();
|
||
|
||
if(empty($barcodes)){
|
||
show_error('Barcode tidak ditemukan.');
|
||
}
|
||
|
||
$column=(int)$this->input->get('column');
|
||
if(!in_array($column,[1,2,3])){
|
||
$column=3;
|
||
}
|
||
|
||
$duplicate=(int)$this->input->get('duplicat');
|
||
if($duplicate<1){
|
||
$duplicate=1;
|
||
}
|
||
|
||
$labelWidth=33.3;
|
||
$labelHeight=23.2;
|
||
|
||
$spaceX=0;
|
||
$spaceY=0;
|
||
|
||
$rowPerPage=6;
|
||
|
||
$paperWidth=$labelWidth*$column;
|
||
$paperHeight=$labelHeight*$rowPerPage;
|
||
|
||
$marginLeft=0;
|
||
$marginTop=0;
|
||
|
||
$labelPerPage=$column*$rowPerPage;
|
||
|
||
$pdf=new FPDF('P','mm',array($paperWidth,$paperHeight));
|
||
$pdf->SetMargins(0,0,0);
|
||
$pdf->SetAutoPageBreak(false);
|
||
$pdf->AddPage();
|
||
|
||
$tempDir=FCPATH.'uploads/temp_qr/';
|
||
if(!is_dir($tempDir)){
|
||
mkdir($tempDir,0777,true);
|
||
}
|
||
|
||
$printData=[];
|
||
foreach($barcodes as $row){
|
||
for($i=0;$i<$duplicate;$i++){
|
||
$printData[]=$row;
|
||
}
|
||
}
|
||
|
||
foreach($printData as $i=>$row){
|
||
|
||
if($i>0 && $i%$labelPerPage==0){
|
||
$pdf->AddPage();
|
||
}
|
||
|
||
$pageIndex=$i%$labelPerPage;
|
||
|
||
$col=$pageIndex%$column;
|
||
$line=floor($pageIndex/$column);
|
||
|
||
// $x=$marginLeft+(($labelWidth+$spaceX)*$col);
|
||
// $y=$marginTop+(($labelHeight+$spaceY)*$line);
|
||
|
||
$x=$col*$labelWidth;
|
||
$y=$line*$labelHeight;
|
||
|
||
$qrFile=$tempDir.$row->barcode.'.png';
|
||
|
||
if(!file_exists($qrFile)){
|
||
QRcode::png(
|
||
$row->barcode,
|
||
$qrFile,
|
||
QR_ECLEVEL_L,
|
||
4,
|
||
1
|
||
);
|
||
}
|
||
|
||
$qrSize=15;
|
||
|
||
$pdf->Image(
|
||
$qrFile,
|
||
$x+16,
|
||
$y+4,
|
||
$qrSize,
|
||
$qrSize
|
||
);
|
||
|
||
$textWidth=14;
|
||
|
||
$pdf->SetFont('Arial','B',7);
|
||
|
||
$pdf->SetXY($x+2,$y+4);
|
||
|
||
$pdf->MultiCell(
|
||
$textWidth,
|
||
3,
|
||
$row->barcode,
|
||
0,
|
||
'L'
|
||
);
|
||
|
||
$serial=empty($row->serial_number)?'-':$row->serial_number;
|
||
|
||
$pdf->SetFont('Arial','B',4.5);
|
||
|
||
$pdf->SetXY($x+2,$y+11);
|
||
|
||
$pdf->MultiCell(
|
||
$textWidth+2,
|
||
3,
|
||
'SN: ' . $serial,
|
||
0,
|
||
'L'
|
||
);
|
||
}
|
||
|
||
$pdf->Output(
|
||
'I',
|
||
'Barcode-'.$item_id.'.pdf'
|
||
);
|
||
}
|
||
```
|
||
|
||
### 3.2 `application/views/items/layout.php` — pemicu Print Barcode
|
||
|
||
Kode di `application/views/items/draft_items.php` identik.
|
||
|
||
```js
|
||
// ================= PRINT BARCODE =================
|
||
$(document).on('click', '.btn-print', function () {
|
||
|
||
let id = $(this).data('id');
|
||
|
||
Swal.fire({
|
||
title: 'Cetak Barcode',
|
||
html: `
|
||
<div class="text-left">
|
||
|
||
<div class="form-group">
|
||
<label for="print_column" class="font-weight-bold">
|
||
Jumlah Kolom
|
||
</label>
|
||
<select id="print_column" class="form-control">
|
||
<option value="1">1 Kolom</option>
|
||
<option value="2">2 Kolom</option>
|
||
<option value="3" selected>3 Kolom (Rekomendasi)</option>
|
||
</select>
|
||
</div>
|
||
|
||
<div class="form-group mb-0">
|
||
<label for="duplicat" class="font-weight-bold">
|
||
Jumlah Cetak per Barcode
|
||
</label>
|
||
<select id="duplicat" class="form-control">
|
||
<option value="1" selected>1 Label</option>
|
||
<option value="2">2 Label</option>
|
||
</select>
|
||
</div>
|
||
|
||
</div>
|
||
`,
|
||
icon: 'question',
|
||
showCancelButton: true,
|
||
confirmButtonText: 'Cetak',
|
||
cancelButtonText: 'Batal',
|
||
confirmButtonColor: '#f0ad4e',
|
||
preConfirm: () => {
|
||
|
||
let column = $('#print_column').val();
|
||
let duplicat = $('#duplicat').val();
|
||
|
||
return {
|
||
column: column,
|
||
duplicat: duplicat
|
||
};
|
||
|
||
}
|
||
|
||
}).then((result) => {
|
||
|
||
if (!result.isConfirmed) {
|
||
return;
|
||
}
|
||
|
||
window.open(
|
||
"<?= base_url('items/print_barcode/'); ?>" +
|
||
id +
|
||
"?column=" + result.value.column +
|
||
"&duplicat=" + result.value.duplicat,
|
||
"_blank"
|
||
);
|
||
|
||
});
|
||
|
||
});
|
||
```
|
||
|
||
### 3.3 `application/controllers/Items.php` — `get_data()` (Data Barang)
|
||
|
||
```php
|
||
public function get_data()
|
||
{
|
||
$status = $this->input->get('s') ?? 'all';
|
||
|
||
$allowed = ['draft','active','all'];
|
||
if (!in_array($status, $allowed)) {
|
||
$status = 'all';
|
||
}
|
||
|
||
$this->db->select('
|
||
items.id,
|
||
items.nama_barang,
|
||
items.kode_detail,
|
||
items.harga_beli,
|
||
items.harga_jual,
|
||
items.tanggal_pembelian,
|
||
items.created_at,
|
||
items.stok,
|
||
items.status,
|
||
items.kode_id,
|
||
kode.need_serial_number,
|
||
(
|
||
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->join('kode_barang kode', 'items.kode_id = kode.id', 'left');
|
||
|
||
if ($status !== 'all') {
|
||
$this->db->where('items.status', $status);
|
||
}
|
||
|
||
// urutan custom
|
||
$this->db->order_by('(items.stok = 0)', 'ASC', false);
|
||
$this->db->order_by('items.tanggal_pembelian', 'ASC');
|
||
|
||
$this->db->from('items');
|
||
|
||
$data = $this->db->get()->result();
|
||
|
||
$result = [];
|
||
$no = 1;
|
||
|
||
foreach($data as $row){
|
||
|
||
$btnDetail = '<button class="btn btn-info btn-sm btn-detail m-1" data-id="'.htmlspecialchars($row->id, ENT_QUOTES, 'UTF-8').'">Detail</button>';
|
||
$btnEdit = '<button class="btn btn-secondary btn-sm btn-editItem m-1" data-id="'.htmlspecialchars($row->id, ENT_QUOTES, 'UTF-8').'">Edit</button>';
|
||
$btnDelete = '<button class="btn btn-danger btn-sm btn-delete m-1" data-id="'.htmlspecialchars($row->id, ENT_QUOTES, 'UTF-8').'">Delete</button>';
|
||
$btnInsertSn = '<a href="'.base_url('items/item_barcodes/'.$row->id).'" class="btn btn-secondary btn-sm m-1">Barcode</a>';
|
||
$btnPrintBarcode = '<button class="btn btn-secondary btn-sm btn-print m-1" data-id="'.htmlspecialchars($row->id, ENT_QUOTES, 'UTF-8').'"><i class="bi bi-printer"></i> Print Barcode</button>';
|
||
$btnPost = '<button class="btn btn-success btn-sm btn-updatePosting m-1" data-id="'.htmlspecialchars($row->id, ENT_QUOTES, 'UTF-8').'">Posting</button>';
|
||
|
||
if ($row->need_serial_number == 'true') {
|
||
|
||
$list = $this->db
|
||
->where('item_id', $row->id)
|
||
->order_by('id', 'ASC')
|
||
->get('item_barcodes')
|
||
->result();
|
||
|
||
$total = count($list);
|
||
|
||
$done = 0;
|
||
foreach ($list as $barcode) {
|
||
if (!empty($barcode->serial_number)) {
|
||
$done++;
|
||
}
|
||
}
|
||
|
||
$total_sn = $total;
|
||
$done_sn = $done;
|
||
|
||
if ($row->status == 'draft') {
|
||
if ($total_sn == $done_sn) {
|
||
$btnSN = $btnInsertSn . $btnPrintBarcode;
|
||
$btnPostview = $btnPost;
|
||
} else {
|
||
$btnSN = $btnInsertSn ;
|
||
$btnPostview = '';
|
||
}
|
||
} else {
|
||
$btnSN = $btnPrintBarcode;
|
||
}
|
||
|
||
} else {
|
||
|
||
$btnSN = $btnPrintBarcode;
|
||
$btnPostview = $btnPost;
|
||
}
|
||
|
||
if($row->status == 'draft'){
|
||
$action = $btnDetail . $btnPostview . $btnDelete . $btnSN;
|
||
|
||
} else {
|
||
|
||
if($this->session->userdata('role') == 'Admin') {
|
||
$btnAdjust = '<button class="btn btn-warning btn-sm btn-adjust m-1" data-id="'.htmlspecialchars($row->id, ENT_QUOTES, 'UTF-8').'">Adjust</button>';
|
||
} else {
|
||
$btnAdjust = '';
|
||
}
|
||
$action = $btnDetail . $btnEdit . $btnAdjust . $btnSN;
|
||
|
||
}
|
||
|
||
$result[] = [
|
||
$no++,
|
||
$row->tanggal_pembelian,
|
||
$row->kode_detail,
|
||
$row->nama_barang,
|
||
$row->gudang ?? '-',
|
||
$row->stok,
|
||
number_format($row->harga_beli ?? 0, 2),
|
||
number_format($row->harga_jual ?? 0, 2),
|
||
$row->created_at,
|
||
$action
|
||
];
|
||
}
|
||
|
||
echo json_encode(["data"=>$result]);
|
||
}
|
||
```
|
||
|
||
### 3.4 `application/views/items/layout.php` — tabel Data Barang
|
||
|
||
```html
|
||
<div class="container mt-4">
|
||
<div class="card modern-card shadow-sm border-0 rounded-4">
|
||
<div class="card-body p-4">
|
||
|
||
<div class="d-flex justify-content-between mb-3">
|
||
<h5>Data Barang</h5>
|
||
<div class="text-end">
|
||
<button class="btn btn-add bg-danger btn-out mr-2">Pengeluaran</button>
|
||
<!-- <button class="btn btn-warning btn-add btn-add-item mr-2">Tambah Barang</button> -->
|
||
</div>
|
||
</div>
|
||
|
||
<div class="table-responsive">
|
||
<table id="tableItems" class="table modern-table w-100">
|
||
<thead>
|
||
<tr>
|
||
<th>No</th>
|
||
<th>Tanggal Beli</th>
|
||
<th>Kode Barang</th>
|
||
<th>Nama</th>
|
||
<th style="max-width: 150px;">Gudang</th>
|
||
<th>Stok</th>
|
||
<th>Harga Beli</th>
|
||
<th>Harga Jual</th>
|
||
<th>Create at</th>
|
||
<th>Aksi</th>
|
||
</tr>
|
||
</thead>
|
||
</table>
|
||
</div>
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
```
|
||
|
||
Konfigurasi DataTables halaman aktif:
|
||
|
||
```js
|
||
let table = $('#tableItems').DataTable({
|
||
ajax: {
|
||
url: "<?= base_url('items/get_data'); ?>",
|
||
data: function (d) {
|
||
d.s = `active`;
|
||
}
|
||
}
|
||
});
|
||
```
|
||
|
||
Halaman draft memakai endpoint yang sama dengan nilai `d.s` = `draft`.
|
||
|
||
### 3.5 Aturan tombol Print Barcode
|
||
|
||
| Kondisi item | Tombol yang tampil |
|
||
|---|---|
|
||
| `need_serial_number = true`, status `draft`, SN belum lengkap | Hanya tombol **Barcode** (input SN) |
|
||
| `need_serial_number = true`, status `draft`, SN lengkap | **Barcode** + **Print Barcode** + **Posting** |
|
||
| `need_serial_number = true`, status `active` | **Print Barcode** |
|
||
| `need_serial_number = false` | **Print Barcode** + **Posting** |
|
||
|
||
Tombol Print selalu membawa `data-id` = `items.id`.
|
||
|
||
---
|
||
|
||
## 4. Data yang Dipakai PDF
|
||
|
||
Kolom `item_barcodes` yang dibaca oleh `print_barcode()`:
|
||
|
||
| Kolom | Fungsi di PDF |
|
||
|---|---|
|
||
| `id` | Urutan `ASC` |
|
||
| `item_id` | Filter item |
|
||
| `barcode` | Isi QR + teks barcode + nama file QR |
|
||
| `serial_number` | Teks `SN: ...` (kosong menjadi `-`) |
|
||
| `status` | Dikecualikan jika `sold_out` |
|
||
|
||
Kolom `items` yang dipakai halaman Data Barang:
|
||
|
||
`id`, `nama_barang`, `kode_detail`, `harga_beli`, `harga_jual`, `tanggal_pembelian`, `created_at`, `stok`, `status`, `kode_id`.
|
||
|
||
Kolom `kode_barang`:
|
||
|
||
`need_serial_number`, `kode_barang` (dipakai saat pembuatan barcode).
|
||
|
||
---
|
||
|
||
## 5. Checklist Implementasi Ulang (Harus Sama)
|
||
|
||
- [ ] Filter barcode: `item_id` dan `status != 'sold_out'`.
|
||
- [ ] Urutan `id ASC`.
|
||
- [ ] `column` hanya 1/2/3, fallback 3.
|
||
- [ ] `duplicat` minimal 1, fallback 1.
|
||
- [ ] Label 33.3 × 23.2 mm, tanpa gap, margin 0.
|
||
- [ ] 6 baris per halaman.
|
||
- [ ] Kertas = `33.3 × column` mm dan `139.2` mm.
|
||
- [ ] Auto page break dimatikan.
|
||
- [ ] QR 15 × 15 mm di `x+16`, `y+4`.
|
||
- [ ] Teks barcode Arial Bold 7 pt di `x+2`, `y+4`, lebar 14 mm, line height 3.
|
||
- [ ] Teks SN Arial Bold 4.5 pt di `x+2`, `y+11`, lebar 16 mm, line height 3.
|
||
- [ ] Serial kosong ditampilkan `-`.
|
||
- [ ] QR memakai `QR_ECLEVEL_L`, size 4, margin 1.
|
||
- [ ] Nama file `Barcode-{item_id}.pdf`, output inline (`I`).
|
||
- [ ] Duplikasi label berurutan per barcode.
|
||
- [ ] Jika tidak ada barcode: `Barcode tidak ditemukan.`
|
||
|
||
---
|
||
|
||
## 6. Catatan Perilaku
|
||
|
||
1. `duplicat` tidak dibatasi maksimum di controller; UI hanya menyediakan 1 atau 2.
|
||
2. Semua barcode yang bukan `sold_out` dicetak, termasuk yang `qty_sisa = 0` atau status lain.
|
||
3. Label duplikat dicetak berdampingan karena loop duplikasi dilakukan per baris sebelum render.
|
||
4. Cache QR tidak memakai hash isi; nama file hanya `{barcode}.png`.
|
||
5. PDF selalu memakai halaman pertama, walaupun data kosong sudah ditolak lebih awal.
|
||
6. `print_barcode()` tidak memeriksa status `items`; hanya status `item_barcodes`.
|