diff --git a/application/controllers/Asset.php b/application/controllers/Asset.php index f58107a..532ad40 100644 --- a/application/controllers/Asset.php +++ b/application/controllers/Asset.php @@ -202,10 +202,22 @@ class Asset extends CI_Controller { $this->db->trans_start(); + // stock log keluar per item + $this->db->insert('stock_logs',[ + 'item_id' => $item_id, + 'warehouse_id' => '1', // default gudang + 'qty' => $qty, + 'tipe' => 'keluar', + 'keterangan' => 'Pemindahan barang ke asset tanggal ' . $tanggal . ' - (' . $keterangan . ')' + ]); + $stock_log_id = $this->db->insert_id(); + $this->update_item_stok($item_id); + // INSERT ASSET $this->db->insert('assets',[ 'item_id' => $item_id, 'barcode_id' => $barcode_id, + 'stock_log_id' => $stock_log_id, 'kode_asset' => 'AST-'.time(), 'nama_asset' => $nama_asset, 'harga_per_unit' => $harga, @@ -240,6 +252,8 @@ class Asset extends CI_Controller { return; } + // END - Item Movement & item barcodes update status + $total_sisa = $itemBar->qty_sisa - $qty; $status_bar = ($total_sisa == 0) ? 'installed' : 'available'; @@ -284,11 +298,6 @@ class Asset extends CI_Controller { 'kredit' => $nilai_perolehan ]); - // UPDATE STOK ITEM (kurangi sesuai qty) - $this->db->where('id', $item_id) - ->set('stok', 'stok-'.$qty, FALSE) - ->update('items'); - // MUTATION DARI GUDANG $this->db->insert('asset_mutations',[ 'asset_id' => $asset_id, @@ -315,7 +324,12 @@ class Asset extends CI_Controller { $lokasi_asset_id = (int)$this->input->post('lokasi_asset'); $keterangan = $this->input->post('keterangan'); - $this->db->trans_start(); + $asset = $this->db->get_where('assets',['id'=>$id])->row(); + + if(!$asset){ + echo json_encode(['status'=>false,'message'=>'Asset tidak ditemukan']); + return; + } $data = [ 'nama_asset' => $nama_asset, @@ -323,17 +337,18 @@ class Asset extends CI_Controller { 'keterangan' => $keterangan ]; + if($asset->barcode_id && $asset->item_id){ + $data = [ + 'lokasi_asset_id' => $lokasi_asset_id, + 'keterangan' => $keterangan + ]; + } + + $this->db->trans_start(); + $this->db->where('id',$id); $this->db->update('assets',$data); - // MUTATION EDIT - $this->db->insert('asset_mutations',[ - 'asset_id' => $id, - 'tipe' => 'penambahan', - 'nilai' => 0, - 'keterangan' => 'Edit asset: '.$nama_asset - ]); - $this->db->trans_complete(); if ($this->db->trans_status()) { @@ -351,9 +366,27 @@ class Asset extends CI_Controller { $keterangan = $this->input->post('keterangan'); $asset = $this->db->get_where('assets',['id'=>$id])->row(); + $itemBar = $this->db->get_where('item_barcodes',['id'=>$asset->barcode_id])->row(); + + if($alasan == 'dijual' || $alasan == 'rusak' ){ + echo json_encode(['status'=>false,'message'=>'Alasan ' .strtoupper($alasan).' tidak bisa digunakan, Karena fitur belum tersedia. Silahkan pilih alasan lain.']); + return; + + } $this->db->trans_start(); + if ($asset->barcode_id && $asset->item_id) { + // Update item_barcodes status to 'available' if it was linked to this asset + $qty_sisa = $asset->qty + $itemBar->qty_sisa; + + $this->db->where('id', $asset->barcode_id) + ->update('item_barcodes', ['status' => 'available', 'qty_sisa' => $qty_sisa]); + $this->db->delete('stock_logs', ['id'=>$asset->stock_log_id]); + $this->update_item_stok($asset->item_id); + + } + // Hapus journal $journals = $this->db->get_where('journals', ['ref_type'=>'assets', 'ref_id'=>$id])->result(); foreach($journals as $j){ @@ -455,4 +488,18 @@ class Asset extends CI_Controller { ->result(); echo json_encode($data); } + + private function update_item_stok($item_id) + { + $stok = $this->db->query(" + SELECT COALESCE(SUM(qty * IF(tipe='masuk',1,-1)),0) as stok + FROM stock_logs + WHERE item_id = ? + ", [$item_id])->row()->stok; + + $this->db->where('id', $item_id) + ->update('items', [ + 'stok' => $stok + ]); + } } \ No newline at end of file diff --git a/application/controllers/Generateinvoice.php b/application/controllers/Generateinvoice.php index 1773d6c..20d8f72 100644 --- a/application/controllers/Generateinvoice.php +++ b/application/controllers/Generateinvoice.php @@ -66,7 +66,7 @@ class Generateinvoice extends CI_Controller { $detail = $this->db ->where('invoice_id', $invoice_id) ->order_by('tanggal', 'ASC') - ->order_by('id', 'DESC') + ->order_by('id', 'ASC') ->get('invoice_details') ->result_array(); diff --git a/application/controllers/Invoices.php b/application/controllers/Invoices.php index 2f3e81a..c356043 100644 --- a/application/controllers/Invoices.php +++ b/application/controllers/Invoices.php @@ -536,7 +536,7 @@ class Invoices extends CI_Controller { $this->db->join('item_barcodes', 'item_barcodes.id = invoice_details.barcode_id', 'left'); $this->db->where('invoice_details.invoice_id', $id); $this->db->order_by('tanggal','ASC'); - $this->db->order_by('id','DESC'); + $this->db->order_by('id','ASC'); $data = $this->db->get()->result(); diff --git a/application/views/asset/layout.php b/application/views/asset/layout.php index 98e29c7..ed29cbb 100644 --- a/application/views/asset/layout.php +++ b/application/views/asset/layout.php @@ -214,9 +214,9 @@ @@ -559,6 +559,8 @@ $('#btnConfirmDelete').click(function(){ $('#modalDelete').modal('hide'); table.ajax.reload(null,false); Swal.fire('Berhasil','Asset berhasil dihapus','success'); + }else{ + Swal.fire('Error',res.message || 'Gagal menghapus asset','error'); } },'json').always(()=>{ btn.prop('disabled', false).html('Hapus');