219 lines
7.6 KiB
PHP
219 lines
7.6 KiB
PHP
<?php
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
class Kodebarang extends MY_Controller {
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
|
|
if (!$this->session->userdata('logged_in')) {
|
|
redirect('auth');
|
|
}
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$data = ["active_menu" => "kode_barang"];
|
|
$this->load->view('partials/header', $data);
|
|
$this->load->view('kode_barang/layout');
|
|
$this->load->view('partials/footer');
|
|
}
|
|
|
|
// =========================
|
|
// GET DATA (DATATABLE)
|
|
// =========================
|
|
public function get_data()
|
|
{
|
|
$data = $this->db->where('company_id',(int)$this->session->userdata('company_id'))->order_by('id', 'DESC')->get('kode_barang')->result();
|
|
|
|
$result = [];
|
|
$no = 1;
|
|
|
|
foreach ($data as $row) {
|
|
$result[] = [
|
|
$no++,
|
|
$row->kode_barang,
|
|
$row->nama,
|
|
(int)$row->limit_stock,
|
|
strtoupper($row->tracking_type ?? 'UNIT'),
|
|
strtolower($row->need_serial_number ?? 'false') === 'true' ? 'Ya' : 'Tidak',
|
|
(int)($row->allocate_purchase_cost ?? 0) === 1 ? 'Ya' : 'Tidak',
|
|
$row->unit ?? 'PCS',
|
|
(int)$row->is_active===1?'<span class="status-chip status-approved">Aktif</span>':'<span class="status-chip status-rejected">Nonaktif</span>',
|
|
($this->session->userdata('role')==='Admin'||check_permission('items','can_update'))?'<button class="btn btn-sm btn-outline-primary btn-editkode" data-id="'.$row->id.'"><i class="bi bi-pencil"></i> Ubah</button>':'-'
|
|
];
|
|
}
|
|
|
|
echo json_encode(["data"=>$result]);
|
|
}
|
|
|
|
// =========================
|
|
// DETAIL
|
|
// =========================
|
|
public function detail($id)
|
|
{
|
|
echo json_encode(
|
|
$this->db->get_where('kode_barang',['id'=>$id,'company_id'=>(int)$this->session->userdata('company_id')])->row()
|
|
);
|
|
}
|
|
|
|
// =========================
|
|
// SAVE
|
|
// =========================
|
|
public function save()
|
|
{
|
|
$nama = trim($this->input->post('nama'));
|
|
$kode = trim($this->input->post('kode'));
|
|
$limit_stock = (int)$this->input->post('limit_stock');
|
|
$tracking_type = strtoupper(trim($this->input->post('tracking_type')));
|
|
$need_serial_number = strtolower(trim($this->input->post('need_serial_number')));
|
|
$allocate_purchase_cost = $this->input->post('allocate_purchase_cost') ? 1 : 0;
|
|
$unit = trim($this->input->post('unit'));
|
|
$is_active = $this->input->post('is_active') ? 1 : 0;
|
|
|
|
if(!$nama || !$kode){
|
|
echo json_encode(['status'=>false,'message'=>'Kode dan nama wajib diisi']);
|
|
return;
|
|
}
|
|
|
|
if(!in_array($tracking_type, ['UNIT','QTY'])){
|
|
echo json_encode(['status'=>false,'message'=>'Tracking type tidak valid']);
|
|
return;
|
|
}
|
|
|
|
if(!in_array($need_serial_number, ['true','false'])){
|
|
echo json_encode(['status'=>false,'message'=>'Need serial number tidak valid']);
|
|
return;
|
|
}
|
|
|
|
if(!in_array($unit, ['PCS','Meter','Lembar'])){
|
|
echo json_encode(['status'=>false,'message'=>'Unit tidak valid']);
|
|
return;
|
|
}
|
|
|
|
$exists = $this->db->get_where('kode_barang', ['kode_barang' => $kode,'company_id'=>(int)$this->session->userdata('company_id')])->num_rows();
|
|
if($exists > 0){
|
|
echo json_encode(['status'=>false,'message'=>'Kode barang sudah digunakan']);
|
|
return;
|
|
}
|
|
|
|
$this->db->insert('kode_barang', [
|
|
'kode_barang' => $kode,
|
|
'company_id' => (int)$this->session->userdata('company_id'),
|
|
'nama' => $nama,
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
'limit_stock' => $limit_stock,
|
|
'tracking_type' => $tracking_type,
|
|
'need_serial_number' => $need_serial_number,
|
|
'allocate_purchase_cost' => $allocate_purchase_cost,
|
|
'unit' => $unit,
|
|
'is_active' => $is_active
|
|
]);
|
|
|
|
log_activity(
|
|
'kode_barang',
|
|
'create',
|
|
'Menambahkan kode barang: ' . $kode . ' - ' . $nama,
|
|
'success'
|
|
);
|
|
|
|
echo json_encode(['status'=>true,'message'=>'Kode barang berhasil ditambahkan']);
|
|
}
|
|
|
|
// =========================
|
|
// UPDATE
|
|
// =========================
|
|
public function update()
|
|
{
|
|
$id = (int)$this->input->post('id');
|
|
$kode = trim($this->input->post('kode'));
|
|
$nama = trim($this->input->post('nama'));
|
|
$limit_stock = (int)$this->input->post('limit_stock');
|
|
$tracking_type = strtoupper(trim($this->input->post('tracking_type')));
|
|
$need_serial_number = strtolower(trim($this->input->post('need_serial_number')));
|
|
$allocate_purchase_cost = $this->input->post('allocate_purchase_cost') ? 1 : 0;
|
|
$unit = trim($this->input->post('unit'));
|
|
$is_active = $this->input->post('is_active') ? 1 : 0;
|
|
|
|
if(!$id || !$nama || !$kode){
|
|
echo json_encode(['status'=>false,'message'=>'ID, kode dan nama wajib diisi']);
|
|
return;
|
|
}
|
|
|
|
if(!in_array($tracking_type, ['UNIT','QTY'])){
|
|
echo json_encode(['status'=>false,'message'=>'Tracking type tidak valid']);
|
|
return;
|
|
}
|
|
|
|
if(!in_array($need_serial_number, ['true','false'])){
|
|
echo json_encode(['status'=>false,'message'=>'Need serial number tidak valid']);
|
|
return;
|
|
}
|
|
|
|
if(!in_array($unit, ['PCS','Meter','Lembar'])){
|
|
echo json_encode(['status'=>false,'message'=>'Unit tidak valid']);
|
|
return;
|
|
}
|
|
|
|
$exists = $this->db
|
|
->where('kode_barang', $kode)
|
|
->where('company_id',(int)$this->session->userdata('company_id'))
|
|
->where('id !=', $id)
|
|
->get('kode_barang')
|
|
->num_rows();
|
|
|
|
if($exists > 0){
|
|
echo json_encode(['status'=>false,'message'=>'Kode barang sudah digunakan']);
|
|
return;
|
|
}
|
|
|
|
$this->db->where(array('id'=>$id,'company_id'=>(int)$this->session->userdata('company_id')))->update('kode_barang', [
|
|
'kode_barang' => $kode,
|
|
'nama' => $nama,
|
|
'limit_stock' => $limit_stock,
|
|
'tracking_type' => $tracking_type,
|
|
'need_serial_number' => $need_serial_number,
|
|
'allocate_purchase_cost' => $allocate_purchase_cost,
|
|
'unit' => $unit,
|
|
'is_active' => $is_active
|
|
]);
|
|
|
|
log_activity(
|
|
'kode_barang',
|
|
'update',
|
|
'Update kode barang ID: ' . $id . ' (' . $kode . ' - ' . $nama . ')',
|
|
'success'
|
|
);
|
|
|
|
echo json_encode(['status'=>true,'message'=>'Kode barang berhasil diupdate']);
|
|
}
|
|
|
|
// =========================
|
|
// DELETE
|
|
// =========================
|
|
public function delete($id)
|
|
{
|
|
if (strtoupper($this->input->method()) !== 'POST') {
|
|
$this->output->set_status_header(405);
|
|
} else {
|
|
$this->output->set_status_header(409);
|
|
}
|
|
|
|
return $this->output
|
|
->set_content_type('application/json')
|
|
->set_output(json_encode(array(
|
|
'status' => false,
|
|
'message' => 'Jenis/Kode Barang tidak dapat dihapus. Nonaktifkan melalui tombol Ubah.'
|
|
)));
|
|
}
|
|
|
|
|
|
public function list()
|
|
{
|
|
echo json_encode(
|
|
$this->db->where(array('company_id'=>(int)$this->session->userdata('company_id'),'is_active'=>1))->get('kode_barang')->result()
|
|
);
|
|
}
|
|
}
|