Perubahan barcode dan inventory

This commit is contained in:
Wian Drs
2026-07-12 22:10:02 +07:00
parent 3ac6316da4
commit 3113d77242
21 changed files with 2989 additions and 1138 deletions
+168 -91
View File
@@ -367,7 +367,9 @@ class Invoices extends CI_Controller {
'warehouse_id' => null,
'keterangan' => $inv_items->keterangan,
'subtotal_asli' => $inv_items->subtotal_asli,
'is_cicilan' => $inv_items->is_cicilan
'is_cicilan' => $inv_items->is_cicilan,
'source_cicilan_id' => $c->id,
'total_hpp_barang' => $c->cicilan_hpp_barang
];
$this->db->insert('invoice_details', $insert_data);
@@ -476,13 +478,14 @@ class Invoices extends CI_Controller {
public function detail($id)
{
// GET customer_id
$row = $this->db->select('customer_id')
$row = $this->db->select('customer_id, no_invoice')
->where('id',$id)
->get('invoices')
->row();
$data['active_menu'] = "invoice_draft";
$data['invoice_id'] = $id;
$data['no_invoice'] = $row ? $row->no_invoice : null;
$data['customer_id'] = $row ? $row->customer_id : null;
$this->load->view('partials/header', $data);
@@ -554,6 +557,7 @@ class Invoices extends CI_Controller {
$harga = (float)$this->input->post('harga');
$account_id = $this->input->post('account_id');
$warehouse_id = $this->input->post('warehouse_id');
$barcode_id = $this->input->post('barcode_id');
$total_harga = $qty * $harga;
$total_hpp = 0;
@@ -561,7 +565,9 @@ class Invoices extends CI_Controller {
// jika di bayar cicilan
$is_cicilan = $this->input->post('is_cicilan') ?? null;
$tenor = (int)$this->input->post('tenor') ?? 0;
// ambil invoice Detail
$invoice = $this->db->get_where('invoices', ['id' => $invoice_id])->row();
// =========================
// VALIDASI & AMBIL ITEM
// =========================
@@ -576,12 +582,44 @@ class Invoices extends CI_Controller {
$nama_item = $item->nama_barang;
// cek stok
$stok = $this->get_stok($item_id);
if($stok < $qty){
echo json_encode(['status'=>false, 'message'=>'Stok tidak cukup. Sisa: '.$stok]);
// $stok = $this->get_stok($item_id);
// if($stok < $qty){
// echo json_encode(['status'=>false, 'message'=>'Stok tidak cukup. Sisa: '.$stok]);
// $this->db->trans_rollback();
// return;
// }
// START - Item Movement & item barcodes update status
$this->db->where('item_id', $item_id);
$this->db->order_by('created_at', 'DESC');
$this->db->limit(1);
$item_move = $this->db->get('item_movements')->row();
// get item_barcode
$itemBar = $this->db->get_where('item_barcodes', ['id'=>$barcode_id])->row();
if($itemBar->qty_sisa < $qty) {
echo json_encode(['status'=>false, 'message'=>'Stok tidak cukup. Sisa: '.$itemBar->qty_sisa]);
$this->db->trans_rollback();
return;
}
$total_sisa = $itemBar->qty_sisa - $qty;
$status_bar = ($total_sisa == 0) ? 'sold_out' : 'available';
$this->db->insert('item_movements',[
'item_id' => $item_id,
'barcode_id' => $barcode_id,
'qty' => $qty,
'from_type' => $item_move->to_type, //enum('supplier','warehouse','technician','customer','invoice')
'to_type' => 'invoice', //enum('supplier','warehouse','technician','customer','invoice')
'movement_type' => 'sold_out', //enum('purchase','transfer','installation','uninstallation','supplier_return','customer_return','adjustment','sold_out')
'notes' => 'Barang terjual ke INV : ' . $invoice->no_invoice,
'created_at' => date('Y-m-d H:i:s')
]);
$this->db->where('id', $barcode_id)->update('item_barcodes', ['status' => $status_bar, 'qty_sisa' => $total_sisa]);
// END - Item Movement & item barcodes update status
$total_hpp = $qty * $item->harga_beli;
}
@@ -600,6 +638,9 @@ class Invoices extends CI_Controller {
// harga per cicilan
$harga_cicilan = round($total_harga / $tenor);
//ngatur HPP Cicilan
$total_hpp = round($total_hpp / $tenor);
$harga_simpan = $harga_cicilan;
$subtotal_simpan = $harga_cicilan;
@@ -624,7 +665,8 @@ class Invoices extends CI_Controller {
'keterangan' => $this->input->post('keterangan'),
'subtotal_asli' => $harga_asli,
'is_cicilan' => $is_cicilan ? 1 : 0,
'total_hpp_barang' => $total_hpp
'total_hpp_barang' => $total_hpp,
'barcode_id' => $barcode_id
];
$this->db->insert('invoice_details', $insert_data);
@@ -639,8 +681,6 @@ class Invoices extends CI_Controller {
$this->update_total($invoice_id);
// ambil customer dari invoice
$invoice = $this->db->get_where('invoices', ['id' => $invoice_id])->row();
if ($is_cicilan && $tenor > 0) {
@@ -653,6 +693,7 @@ class Invoices extends CI_Controller {
$tenor,
$tanggal,
$account_id,
$total_hpp,
$item_id ?: null
);
}
@@ -675,105 +716,108 @@ class Invoices extends CI_Controller {
}
}
// =========================
// UPDATE ITEM - RESPONSE KONSISTEN
// =========================
public function update_item()
{
$this->db->trans_begin();
// // =========================
// // UPDATE ITEM - RESPONSE KONSISTEN
// // =========================
// public function update_item()
// {
// $this->db->trans_begin();
$id = $this->input->post('id');
$old = $this->db->get_where('invoice_details', ['id'=>$id])->row();
// $id = $this->input->post('id');
// $old = $this->db->get_where('invoice_details', ['id'=>$id])->row();
if(!$old){
echo json_encode(['status'=>false, 'message'=>'Item tidak ditemukan']);
return;
}
// if(!$old){
// echo json_encode(['status'=>false, 'message'=>'Item tidak ditemukan']);
// return;
// }
$cek = $this->db->get_where('invoice_installments', [
'invoice_detail_id' => $id
])->num_rows();
// $cek = $this->db->get_where('invoice_installments', [
// 'invoice_detail_id' => $id
// ])->num_rows();
if ($cek > 0) {
echo json_encode([
'status' => false,
'message' => 'Item cicilan tidak bisa diubah. Hapus dan buat ulang.'
]);
return;
}
// if ($cek > 0) {
// echo json_encode([
// 'status' => false,
// 'message' => 'Item cicilan tidak bisa diubah. Hapus dan buat ulang.'
// ]);
// return;
// }
// 🔥 REVERSE DATA LAMA
$this->delete_jurnal_item($id);
$this->delete_stok_item($id);
// // 🔥 REVERSE DATA LAMA
// $this->delete_jurnal_item($id);
// $this->delete_stok_item($id);
// DATA BARU
$item_id = $this->input->post('item_id');
$nama_item = $this->input->post('nama_item');
$tanggal = $this->input->post('tanggal');
// // DATA BARU
// $item_id = $this->input->post('item_id');
// $nama_item = $this->input->post('nama_item');
// $tanggal = $this->input->post('tanggal');
$total_hpp = 0;
// $total_hpp = 0;
if($item_id){
$item = $this->db->get_where('items', ['id'=>$item_id])->row();
if($item){
$nama_item = $item->nama_barang;
$stok = $this->get_stok($item_id);
$qty = (float)$this->input->post('qty');
$total_hpp = $this->input->post('qty') * $item->harga_beli;
if($stok < $qty){
echo json_encode(['status'=>false, 'message'=>'Stok tidak cukup']);
return;
}
}
}
// if($item_id){
// $item = $this->db->get_where('items', ['id'=>$item_id])->row();
// if($item){
// $nama_item = $item->nama_barang;
// $stok = $this->get_stok($item_id);
// $qty = (float)$this->input->post('qty');
// $total_hpp = $this->input->post('qty') * $item->harga_beli;
// if($stok < $qty){
// echo json_encode(['status'=>false, 'message'=>'Stok tidak cukup']);
// return;
// }
// }
// }
$qty = (float)$this->input->post('qty');
$harga = (float)$this->input->post('harga');
// $qty = (float)$this->input->post('qty');
// $harga = (float)$this->input->post('harga');
// UPDATE
$update_data = [
'tanggal' => $tanggal,
'account_id' => $this->input->post('account_id'),
'items_id' => $item_id ?: null,
'nama_item' => $nama_item,
'qty' => $qty,
'harga' => $harga,
'subtotal' => $qty * $harga,
'warehouse_id' => $this->input->post('warehouse_id'),
'keterangan' => $this->input->post('keterangan'),
'total_hpp_barang' => $total_hpp
];
// // UPDATE
// $update_data = [
// 'tanggal' => $tanggal,
// 'account_id' => $this->input->post('account_id'),
// 'items_id' => $item_id ?: null,
// 'nama_item' => $nama_item,
// 'qty' => $qty,
// 'harga' => $harga,
// 'subtotal' => $qty * $harga,
// 'warehouse_id' => $this->input->post('warehouse_id'),
// 'keterangan' => $this->input->post('keterangan'),
// 'total_hpp_barang' => $total_hpp
// ];
$this->db->where('id', $id)->update('invoice_details', $update_data);
// $this->db->where('id', $id)->update('invoice_details', $update_data);
// GENERATE ULANG
if($this->input->post('account_id') != 70) {
$this->generate_jurnal_item($id);
}
$this->update_total($old->invoice_id);
// // GENERATE ULANG
// if($this->input->post('account_id') != 70) {
// $this->generate_jurnal_item($id);
// }
// $this->update_total($old->invoice_id);
// ✅ RESPONSE KONSISTEN DENGAN JS
if ($this->db->trans_status() === FALSE){
$this->db->trans_rollback();
echo json_encode(['status'=>false, 'message'=>'Update gagal']);
} else {
$this->db->trans_commit();
// // ✅ RESPONSE KONSISTEN DENGAN JS
// if ($this->db->trans_status() === FALSE){
// $this->db->trans_rollback();
// echo json_encode(['status'=>false, 'message'=>'Update gagal']);
// } else {
// $this->db->trans_commit();
log_activity(
'invoice',
'update_item',
'Update item invoice detail ID ' . $id,
'success'
);
// log_activity(
// 'invoice',
// 'update_item',
// 'Update item invoice detail ID ' . $id,
// 'success'
// );
echo json_encode(['status'=>true, 'message'=>'Item berhasil diupdate']);
}
}
// echo json_encode(['status'=>true, 'message'=>'Item berhasil diupdate']);
// }
// }
// =========================
// DELETE ITEM - RESPONSE KONSISTEN
// =========================
public function delete_item($id)
public function delete_item($id, $no_invoice)
{
$this->db->trans_begin();
@@ -792,6 +836,36 @@ class Invoices extends CI_Controller {
$this->delete_stok_item($id);
$this->db->delete('invoice_details', ['id'=>$id]);
$this->update_total($d->invoice_id);
if ($d->items_id AND $d->is_cicilan != 1) {
// START - Item Movement & item barcodes update status
$this->db->where('item_id', $d->items_id);
$this->db->order_by('created_at', 'DESC');
$this->db->limit(1);
$item_move = $this->db->get('item_movements')->row();
// get item_barcode
$itemBar = $this->db->get_where('item_barcodes', ['id'=>$d->barcode_id])->row();
$total_sisa = $itemBar->qty_sisa + $d->qty;
$status_bar = ($total_sisa == 0) ? 'sold_out' : 'available';
$this->db->insert('item_movements',[
'item_id' => $d->items_id,
'barcode_id' => $d->barcode_id,
'qty' => $d->qty,
'from_type' => $item_move->to_type, //enum('supplier','warehouse','technician','customer','invoice')
'to_type' => 'warehouse', //enum('supplier','warehouse','technician','customer','invoice')
'movement_type' => 'purchase', //enum('purchase','transfer','installation','uninstallation','supplier_return','customer_return','adjustment','sold_out')
'notes' => 'Barang di kembalikan ke gudang dari : ' . $no_invoice,
'created_at' => date('Y-m-d H:i:s')
]);
// $this->db->where('id', $d->barcode_id)->update('item_barcodes', ['status' => 'available']);
$this->db->where('id', $d->barcode_id)->update('item_barcodes', ['status' => $status_bar, 'qty_sisa' => $total_sisa]);
// END - Item Movement & item barcodes update status
}
// ✅ RESPONSE KONSISTEN DENGAN JS
if ($this->db->trans_status() === FALSE){
@@ -1223,7 +1297,7 @@ class Invoices extends CI_Controller {
}
private function generate_installments($invoice_id, $detail_id, $customer_id, $total, $tenor, $start_date, $account_id, $item_id = null)
private function generate_installments($invoice_id, $detail_id, $customer_id, $total, $tenor, $start_date, $account_id, $total_hpp, $item_id = null)
{
$nominal = round($total / $tenor);
$sisa = $total - ($nominal * $tenor);
@@ -1250,6 +1324,7 @@ class Invoices extends CI_Controller {
'nominal' => $nilai,
'account_id' => $account_id,
'item_id' => $item_id ?: null,
'cicilan_hpp_barang' => $total_hpp,
'status' => 'unpaid',
'is_billed' => $is_billed
]);
@@ -1336,7 +1411,8 @@ class Invoices extends CI_Controller {
'keterangan' => $inv_items->keterangan,
'subtotal_asli' => $inv_items->subtotal_asli,
'is_cicilan' => $inv_items->is_cicilan,
'source_cicilan_id' => $cicilan->id
'source_cicilan_id' => $cicilan->id,
'total_hpp_barang' => $cicilan->cicilan_hpp_barang
]);
// =========================
@@ -1498,6 +1574,7 @@ public function bayar()
invoice_details.recognized_amount,
invoice_details.total_hpp_barang,
invoice_details.recognized_hpp,
invoice_details.is_cicilan,
accounts.priority
')
->from('invoice_details')