145 lines
3.5 KiB
PHP
145 lines
3.5 KiB
PHP
<?php
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
class Warehouses extends CI_Controller {
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
|
|
if (!$this->session->userdata('logged_in')) {
|
|
redirect('auth');
|
|
}
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$data = ["active_menu" => "warehouses"];
|
|
$this->load->view('partials/header', $data);
|
|
$this->load->view('warehouses/layout');
|
|
$this->load->view('partials/footer');
|
|
}
|
|
|
|
// =========================
|
|
// GET DATA (DATATABLE)
|
|
// =========================
|
|
public function get_data()
|
|
{
|
|
$data = $this->db->get('warehouses')->result();
|
|
|
|
$result = [];
|
|
$no = 1;
|
|
|
|
foreach ($data as $row) {
|
|
$result[] = [
|
|
$no++,
|
|
$row->nama,
|
|
'
|
|
<button class="btn btn-sm btn-secondary btn-editgudang" data-id="'.$row->id.'">Edit</button>
|
|
<button class="btn btn-sm btn-danger btn-delete" data-id="'.$row->id.'">Delete</button>
|
|
'
|
|
];
|
|
}
|
|
|
|
echo json_encode(["data"=>$result]);
|
|
}
|
|
|
|
// =========================
|
|
// DETAIL
|
|
// =========================
|
|
public function detail($id)
|
|
{
|
|
echo json_encode(
|
|
$this->db->get_where('warehouses',['id'=>$id])->row()
|
|
);
|
|
}
|
|
|
|
// =========================
|
|
// SAVE
|
|
// =========================
|
|
public function save()
|
|
{
|
|
$nama = $this->input->post('nama');
|
|
|
|
if(!$nama){
|
|
echo json_encode(['status'=>false,'message'=>'Nama wajib diisi']);
|
|
return;
|
|
}
|
|
|
|
$this->db->insert('warehouses', [
|
|
'nama' => $nama
|
|
]);
|
|
|
|
log_activity(
|
|
'kode_barang',
|
|
'create',
|
|
'Tambah gudang '.$nama,
|
|
'success'
|
|
);
|
|
|
|
echo json_encode(['status'=>true,'message'=>'Gudang berhasil ditambahkan']);
|
|
}
|
|
|
|
// =========================
|
|
// UPDATE
|
|
// =========================
|
|
public function update()
|
|
{
|
|
$id = $this->input->post('id');
|
|
$nama = $this->input->post('nama');
|
|
|
|
if(!$nama){
|
|
echo json_encode(['status'=>false,'message'=>'Nama wajib diisi']);
|
|
return;
|
|
}
|
|
|
|
$this->db->where('id',$id)->update('warehouses', [
|
|
'nama' => $nama
|
|
]);
|
|
|
|
log_activity(
|
|
'kode_barang',
|
|
'update',
|
|
'Update gudang '.$nama,
|
|
'success'
|
|
);
|
|
|
|
echo json_encode(['status'=>true,'message'=>'Gudang berhasil diupdate']);
|
|
}
|
|
|
|
// =========================
|
|
// DELETE
|
|
// =========================
|
|
public function delete($id)
|
|
{
|
|
// optional: cek relasi ke items
|
|
$cek = $this->db->get_where('items',['warehouse_id'=>$id])->num_rows();
|
|
|
|
if($cek > 0){
|
|
echo json_encode([
|
|
'status'=>false,
|
|
'message'=>'Gudang tidak bisa dihapus karena masih dipakai item'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$this->db->delete('warehouses',['id'=>$id]);
|
|
|
|
log_activity(
|
|
'kode_barang',
|
|
'delete',
|
|
'Delete gudang '.$nama,
|
|
'success'
|
|
);
|
|
|
|
echo json_encode(['status'=>true,'message'=>'Gudang berhasil dihapus']);
|
|
}
|
|
|
|
|
|
public function list()
|
|
{
|
|
echo json_encode(
|
|
$this->db->get('warehouses')->result()
|
|
);
|
|
}
|
|
} |