Update Controller Items karena ada bug

This commit is contained in:
Prod-Panel
2026-07-12 16:55:48 +00:00
parent 81e35f56a0
commit c7ec369234
222 changed files with 140 additions and 107 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ application/config/database.php
application/controllers/Dbcompare.php
.htaccess
uploads/temp_qr/*.png
uploads/temp_qr/*
application/cache/*
!application/cache/index.html
+130 -97
View File
@@ -540,6 +540,7 @@ class Items extends CI_Controller {
$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') {
@@ -564,8 +565,10 @@ class Items extends CI_Controller {
if ($row->status == 'draft') {
if ($total_sn == $done_sn) {
$btnSN = $btnInsertSn . $btnPrintBarcode;
$btnPostview = $btnPost;
} else {
$btnSN = $btnInsertSn ;
$btnPostview = '';
}
} else {
$btnSN = $btnPrintBarcode;
@@ -574,11 +577,11 @@ class Items extends CI_Controller {
} else {
$btnSN = $btnPrintBarcode;
$btnPostview = $btnPost;
}
if($row->status == 'draft'){
$btnPost = '<button class="btn btn-success btn-sm btn-updatePosting m-1" data-id="'.htmlspecialchars($row->id, ENT_QUOTES, 'UTF-8').'">Posting</button>';
$action = $btnDetail . $btnPost . $btnDelete . $btnSN;
$action = $btnDetail . $btnPostview . $btnDelete . $btnSN;
} else {
@@ -1424,114 +1427,144 @@ class Items extends CI_Controller {
echo json_encode($data);
}
public function update_barcode_item()
{
$items = $this->db
->select("
i.id,
i.nama_barang,
i.stok,
i.kode_detail,
kb.tracking_type
")
->from('items i')
->join('kode_barang kb', 'kb.id=i.kode_id')
->where('i.status', 'active')
->get()
->result();
public function update_barcode_item()
{
$items = $this->db
->select("
i.id,
i.nama_barang,
i.stok,
i.kode_detail,
kb.tracking_type,
kb.kode_barang
")
->from('items i')
->join('kode_barang kb', 'kb.id=i.kode_id')
->where('i.status', 'active')
->get()
->result();
$this->db->trans_begin();
$this->db->trans_begin();
foreach ($items as $item) {
foreach ($items as $item) {
// ===========================================
// Hitung jumlah barcode yang seharusnya
// ===========================================
// ===========================================
// Qty per barcode
// ===========================================
if ($item->tracking_type == 'UNIT') {
$target = (int)$item->stok;
} else {
$target = (int)floor($item->stok / 1000);
if ($item->tracking_type == 'UNIT') {
$qty_per_barcode = 1;
} else {
$qty_per_barcode = ($item->kode_barang == 'FO1') ? 1000 : 2000;
}
// ===========================================
// Jumlah barcode yang seharusnya
// ===========================================
$target = (int) ceil($item->stok / $qty_per_barcode);
if ($target <= 0) {
continue;
}
// ===========================================
// Jumlah barcode yang sudah ada
// ===========================================
$barcode_exist = $this->db
->where('item_id', $item->id)
->count_all_results('item_barcodes');
if ($barcode_exist >= $target) {
continue;
}
// ===========================================
// Total qty barcode yang sudah ada
// ===========================================
$qty_existing = $this->db
->select_sum('qty_awal')
->where('item_id', $item->id)
->get('item_barcodes')
->row();
$qty_existing = (float) ($qty_existing->qty_awal ?? 0);
// Sisa stok yang belum memiliki barcode
$qty_remaining = $item->stok - $qty_existing;
if ($qty_remaining <= 0) {
continue;
}
$need = $target - $barcode_exist;
for ($i = 1; $i <= $need; $i++) {
// Barcode terakhir akan berisi sisa stok
$qty = min($qty_per_barcode, $qty_remaining);
$barcode = strtoupper($item->kode_detail) . '-' . strtoupper(bin2hex(random_bytes(2)));
// =============================
// Insert Barcode
// =============================
$this->db->insert('item_barcodes', [
'item_id' => $item->id,
'barcode' => $barcode,
'warehouse_id' => null,
'qty_awal' => $qty,
'qty_sisa' => $qty,
'status' => 'available'
]);
$barcode_id = $this->db->insert_id();
// =============================
// Insert Movement
// =============================
$this->db->insert('item_movements', [
'item_id' => $item->id,
'barcode_id' => $barcode_id,
'qty' => $qty,
'from_type' => 'supplier',
'from_id' => null,
'to_type' => 'warehouse',
'to_id' => null,
'movement_type' => 'purchase',
'reference_type' => 'system_generate',
'reference_id' => null,
'notes' => 'Generate barcode otomatis'
]);
$qty_remaining -= $qty;
if ($qty_remaining <= 0) {
break;
}
}
}
if ($target <= 0) {
continue;
}
if ($this->db->trans_status() === FALSE) {
// ===========================================
// Barcode yang sudah ada
// ===========================================
$this->db->trans_rollback();
$barcode_exist = $this->db
->where('item_id', $item->id)
->count_all_results('item_barcodes');
if ($barcode_exist >= $target) {
continue;
}
$need = $target - $barcode_exist;
for ($i = 1; $i <= $need; $i++) {
$barcode = strtoupper($item->kode_detail) . '-' . strtoupper(bin2hex(random_bytes(2)));
$qty = ($item->tracking_type == 'UNIT')
? 1
: 1000;
// =============================
// Insert Barcode
// =============================
$this->db->insert('item_barcodes', [
'item_id' => $item->id,
'barcode' => $barcode,
'warehouse_id' => null,
'qty_awal' => $qty,
'qty_sisa' => $qty,
'status' => 'available'
]);
$barcode_id = $this->db->insert_id();
// =============================
// Insert Movement
// =============================
$this->db->insert('item_movements', [
'item_id' => $item->id,
'barcode_id' => $barcode_id,
'qty' => $qty,
'from_type' => 'supplier',
'from_id' => null,
'to_type' => 'warehouse',
'to_id' => null,
'movement_type' => 'purchase',
'reference_type' => 'system_generate',
'reference_id' => null,
'notes' => 'Generate barcode otomatis'
echo json_encode([
'status' => false,
'message' => 'Gagal generate barcode.'
]);
return;
}
}
if ($this->db->trans_status() == false) {
$this->db->trans_rollback();
$this->db->trans_commit();
echo json_encode([
'status' => false,
'message' => 'Gagal generate barcode.'
'status' => true,
'message' => 'Barcode berhasil diperbarui.'
]);
return;
}
$this->db->trans_commit();
echo json_encode([
'status' => true,
'message' => 'Barcode berhasil diperbarui.'
]);
}
}
Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 237 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 241 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 241 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 242 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 248 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 239 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 237 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 242 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 242 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 239 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 242 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 247 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 245 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 240 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 247 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 242 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 242 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 240 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 238 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 231 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 241 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 236 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 235 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 237 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 238 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 241 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 231 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 241 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 245 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 242 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 242 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 242 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 241 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 242 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 240 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 236 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 242 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 246 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 245 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 245 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 246 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 250 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 242 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 247 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 241 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 238 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 240 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 247 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 245 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 240 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 245 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 248 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 245 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 248 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 249 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 248 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 247 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 239 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 242 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 247 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 246 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 246 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 245 B

Some files were not shown because too many files have changed in this diff Show More