Files
shopee-apps/application/delete/controllers/ManagePembelianSJ.php
T
2026-06-13 16:02:07 +10:00

465 lines
13 KiB
PHP

<?php
use Restserver\Libraries\REST_Controller;
defined('BASEPATH') or exit('No direct script access allowed');
require APPPATH . 'libraries/REST_Controller.php';
require APPPATH . 'libraries/Format.php';
class ManagePembelianSJ extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('ModelBarang', 'Barang');
$this->load->model('ModelCorp', 'Corp');
$this->load->model('ModelGudang', 'Gudang');
$this->load->model('ModelNeraca', 'Neraca');
$this->load->model('ModelSupplier', 'Supplier');
$this->load->model('ModelPembelian', 'Pembelian');
$this->load->library(['datatables', 'form_validation']);
$this->form_validation->set_error_delimiters('', '');
}
public function output_json($data, $encode = true)
{
if ($encode) $data = json_encode($data);
$this->output->set_content_type('application/json')->set_output($data);
}
public function index() {
$datPembelian = $this->Pembelian->getDataPembelian();
$datCorp = $this->Corp->getDataCorp();
$datGudang = $this->Gudang->getDataGudangPembelian();
$datSupplier = $this->Supplier->getDataSupplier();
$tgl = date('m/d/Y');
$data = [
'sidebar' => "menu pembelian",
'navChild' => "",
'pembelian' => $datPembelian,
'corp' => $datCorp,
'gudang' => $datGudang,
'supplier' => $datSupplier,
'tgl' => $tgl
];
$this->load->view('proses/PembelianSJ', $data);
}
public function kodePembelian() {
$kk = $this->Pembelian->getKodePembelian();
if (is_array($kk) || is_object($kk)) {
foreach ($kk as $row) {
if ($row['KODE'] == null) {
return "PB/000001";
} else {
$str = (string) $row['KODE'];
$panjang = 6;
$data = substr($str, 3, $panjang);
$ko = (int) $data + 1;
$str = (string) $ko;
$panjang = 9 - strlen($str);
$data = "PB/000000";
$data = substr($data, 0, $panjang) . $str;
return $data;
}
}
}
}
public function save() {
$method = $this->input->post('method');
$id_pembelian = $this->input->post('id');
$id_supplier = $this->input->post('id_supplier');
$id_corp = $this->input->post('id_corp');
$sj_pembelian = $this->input->post('sj_pembelian');
$tgl_sjbeli = $this->input->post('tgl_sjbeli');
$kode_pembelian = $this->kodePembelian();
$sj_pembelian = strtoupper($sj_pembelian);
$input = [
'SUPPLIER_ID' => $id_supplier,
'CORP_ID' => $id_corp,
'TGL_SJBELI' => $tgl_sjbeli,
'SJ_PEMBELIAN' => $sj_pembelian,
'KODE_PEMBELIAN'=> $kode_pembelian
];
$action = $this->Pembelian->create('pembelian', $input);
if ($action) {
$this->session->set_flashdata('flash', 'ditambah');
redirect('ManagePembelianSJ');
} else {
$this->session->set_flashdata('flashgagal', 'ditambah');
redirect('ManagePembelianSJ');
}
}
public function edit() {
$method = $this->input->post('method');
$id_pembelian = $this->input->post('id_pembelian');
$id_supplier = $this->input->post('id_suppliered');
$id_corp = $this->input->post('id_corped');
$sj_pembelian = $this->input->post('sj_pembelianed');
$tgl_sjbeli = $this->input->post('tgl_sjbelied');
$update = [
'SUPPLIER_ID' => $id_supplier,
'CORP_ID' => $id_corp,
'TGL_SJBELI' => $tgl_sjbeli,
'SJ_PEMBELIAN' => $sj_pembelian
];
$action = $this->Pembelian->update('pembelian', $update,'ID_PEMBELIAN',$id_pembelian);
if ($action) {
$this->session->set_flashdata('flash', 'diubah');
redirect('ManagePembelianSJ');
} else {
$this->session->set_flashdata('flashgagal', 'diubah');
redirect('ManagePembelianSJ');
}
}
public function hapus() {
$id = $this->input->post('id');
$action = $this->Pembelian->delDB($id);
$action = $this->Pembelian->delete('pembelian',$id,'ID_PEMBELIAN');
$this->session->set_flashdata('flash', 'dihapus');
redirect('ManagePembelianSJ');
}
public function tambahStock($id) {
$stock = $this->Pembelian->getDataTambahStock($id);
$title = $this->Pembelian->getSJPembelian($id);
$barang = $this->Barang->getBarangDB();
$gudang = $this->Gudang->getDataGudangPembelian();
$data = [
'sidebar' => "menu pembelian",
'navChild' => "",
'stock' => $stock,
'id_pembelian' => $id,
'sj_pembelian' => $title[0]['SJ_PEMBELIAN'],
'id_db' => $title[0]['ID_DB'],
'barang' => $barang,
'gudang' => $gudang
];
$this->load->view('proses/PembelianST', $data);
}
public function savedb() {
$method = $this->input->post('method');
$id_pembelian = $this->input->post('id_pembelian');
$id = $this->input->post('id_db');
$id_barang = $this->input->post('id_barang');
$qty_db = $this->input->post('qty_db');
$input = [
'BARANG_ID' => $id_barang,
'PEMBELIAN_ID' => $id_pembelian,
'QTY_DB' => $qty_db
];
$action = $this->Pembelian->create('daftar_beli', $input);
if ($action) {
$this->session->set_flashdata('flash', 'dilakukan');
redirect($_SERVER['HTTP_REFERER']);
} else {
$this->session->set_flashdata('flashgagal', 'dilakukan');
redirect($_SERVER['HTTP_REFERER']);
}
}
public function editdb() {
$method = $this->input->post('method');
$id_db = $this->input->post('id_db');
$id_barang = $this->input->post('id_barang');
$qty_db = $this->input->post('qty_dbed');
$datadb = $this->Pembelian->getDataDaftarBeli($id_db);
$qty = $datadb[0]['QTY_DB'];
$barang = $datadb[0]['BARANG_ID'];
$jenis = $this->Pembelian->getDataJenis($barang);
$type_jenis = $jenis[0]['TYPE_JENIS'];
if ($type_jenis=="1") {
$action = $this->Pembelian->delSn($id_db);
}
$update = [
'BARANG_ID' => $id_barang,
'QTY_DB' => $qty_db
];
$action = $this->Pembelian->update('daftar_beli', $update, 'ID_DB', $id_db);
if ($action) {
$this->session->set_flashdata('flash', 'diubah');
redirect($_SERVER['HTTP_REFERER']);
} else {
$this->session->set_flashdata('flashgagal', 'diubah');
redirect($_SERVER['HTTP_REFERER']);
}
}
public function hapusdb() {
$id = $this->input->post('id');
$datadb = $this->Pembelian->getDataDaftarBeli($id);
$id_barang = $datadb[0]['BARANG_ID'];
$jenis = $this->Pembelian->getDataJenis($id_barang);
$type_jenis = $jenis[0]['TYPE_JENIS'];
if ($type_jenis=="1") {
$action = $this->Pembelian->delSn($id);
}
$action = $this->Pembelian->delete('daftar_beli',$id,'ID_DB');
$this->session->set_flashdata('flash', 'dihapus');
redirect($_SERVER['HTTP_REFERER']);
}
public function tambahSn($id) {
$stock = $this->Pembelian->getDataTambahSn($id);
$max = $this->Pembelian->getQtyReal($id);
$data = [
'sidebar' => "menu pembelian",
'navChild' => "",
'stock' => $stock,
'id_db' => $id,
'qty' => $stock[0]['QTY_DB'],
'title' => $stock[0]['NAMA_BARANG'],
'max' => $max,
];
$this->load->view('proses/PembelianSN', $data);
}
public function datasn() {
$id = $this->input->post('id');
$data = $this->Pembelian->getDataSn($id);
$this->output_json(['value' => 1, 'total' => sizeof($data), 'data' => $data]);
}
public function jumlahdatasn() {
$id = $this->input->post('id');
$data = $this->Pembelian->getDataSn($id);
$this->output_json(['value' => 1, 'jumlah_data' => sizeof($data)]);
}
public function savesn() {
$id = $this->input->post('id_sn');
$id_db = $this->input->post('id_db');
$nama_sn = $this->input->post('nama_sn');
$nama_sn = strtoupper($nama_sn);
$dataDb = $this->Pembelian->getDataTambahSn($id_db);
$qtyDb = $dataDb[0]['QTY_DB'];
$id_barang = $dataDb[0]['BID'];
$qtyReal = $this->Pembelian->getQtyReal($id_db);
if ($id != null) {
$data = [
'NAMA_SN' => $nama_sn,
'DB_ID' => $id_db,
'BARANG_ID' => $id_barang
];
$action = $this->Pembelian->update('sn', $data, 'ID_SN', $id);
if ($action) {
$this->output_json(['value' => 1, 'id' => $id, 'nama_sn' => $nama_sn]);
$this->session->set_flashdata('flash', 'diedit');
} else {
$this->output_json(['value' => 0, 'id' => $id, 'nama_sn' => $nama_sn]);
}
} else {
//Fungsi Tambah SN
$data = [
'NAMA_SN' => $nama_sn,
'DB_ID' => $id_db,
'GUDANG_ID' => '1',
'BARANG_ID' => $id_barang
];
$action = $this->Pembelian->create('sn', $data);
//Fungsi update Qty Real Tabel Barang
$qtyReal = $this->Pembelian->getQtyReal($id_db);
if ($qtyReal==$qtyDb){
$this->output_json(['value' => 2, 'id' => $id, 'nama_sn' => $nama_sn]);
$this->session->set_flashdata('flashkunci', 'dilakukan');
} else if ($action) {
$this->output_json(['value' => 1, 'id' => $id, 'nama_sn' => $nama_sn]);
} else {
$this->output_json(['value' => 0, 'id' => $id, 'nama_sn' => $nama_sn]);
}
}
}
public function hapussn() {
$id = $this->input->post('id_sn');
$id_db = $this->Pembelian->getDataSnDb($id);
$id_db = $id_db[0]['DB_ID'];
$qtyReal = $this->Pembelian->getQtyReal($id_db);
$qtyStock = $this->Pembelian->getDataTambahSn($id_db);
$qtyStock = $qtyStock[0]['QTY_DB'];
$action = $this->Pembelian->delete('sn', $id, 'ID_SN');
if ($qtyReal==$qtyStock){
$this->output_json(['value' => 2, 'message' => 'Berhasil', 'dt' => $action]);
$this->session->set_flashdata('flashkurang', 'dilakukan');
} else {
if ($action) {
$this->output_json(['value' => 1, 'message' => 'Berhasil', 'dt' => $action]);
} else {
$this->output_json(['value' => 0, 'message' => "gagal", 'dt' => $action]);
}
}
}
public function kodeStockKuantiti() {
$kk = $this->Pembelian->getKodeSTQ();
if (is_array($kk) || is_object($kk)) {
foreach ($kk as $row) {
if ($row['KODE'] == null) {
return "STQ/000001";
} else {
$str = (string) $row['KODE'];
$panjang = 6;
$data = substr($str, 4, $panjang);
$ko = (int) $data + 1;
$str = (string) $ko;
$panjang = 10 - strlen($str);
$data = "STQ/000000";
$data = substr($data, 0, $panjang) . $str;
return $data;
}
}
}
}
public function kodeStockSn() {
$kk = $this->Pembelian->getKodeSTSN();
if (is_array($kk) || is_object($kk)) {
foreach ($kk as $row) {
if ($row['KODE'] == null) {
return "STSN/000001";
} else {
$str = (string) $row['KODE'];
$panjang = 6;
$data = substr($str, 5, $panjang);
$ko = (int) $data + 1;
$str = (string) $ko;
$panjang = 11 - strlen($str);
$data = "STSN/000000";
$data = substr($data, 0, $panjang) . $str;
return $data;
}
}
}
}
public function konfirmasi() {
$method = $this->input->post('method');
$id_pembelian = $this->input->post('id_pembelian');
$pembelian = $this->Pembelian->getPembelianHarga($id_pembelian);
foreach ($pembelian as $key) {
$type_jenis = $key['TYPE_JENIS'];
if ($type_jenis=="1") {
$qtyReal = $this->Pembelian->getQtyReal($key['ID_DB']);
$qtyDb = $key['QTY_DB'];
if ($qtyReal<$qtyDb) {
$this->session->set_flashdata('flashsn', 'diproses');
redirect($_SERVER['HTTP_REFERER']);
}
}
}
foreach ($pembelian as $key) {
$type_jenis = $key['TYPE_JENIS'];
if ($type_jenis=="1") {
$sn = $this->Pembelian->getDataSn($key['ID_DB']);
foreach ($sn as $dat) {
$kode_stsn = $this->kodeStockSn();
$input = [
'KODE_STSN' => $kode_stsn,
'KODE_BUKTI' => $key['KODE_PEMBELIAN'],
'TGL_STSN' => $key['TGL_SJBELI'],
'BARANG_ID' => $key['BARANG_ID'],
'SUPPLIER_ID' => $key['SUPPLIER_ID'],
'SN_ID' => $dat['ID_SN']
];
$action = $this->Pembelian->create('stock_sn', $input);
}
}
}
foreach ($pembelian as $key) {
$kuantiti = $this->Pembelian->getDataKuantiti($key['ID_DB']);
$id_kuantiti = $kuantiti[0]['ID_KUANTITI'];
$global_kuantiti = $kuantiti[0]['GLOBAL_KUANTITI'];
$pcm_kuantiti = $kuantiti[0]['PCM_KUANTITI'];
$tgl_dh = date('Y-m-d H:i:s');
$kuantiti1 = $pcm_kuantiti+$key['QTY_DB'];
$kuantiti2 = $global_kuantiti+$key['QTY_DB'];
$update = [
'PCM_KUANTITI' => $kuantiti1,
'GLOBAL_KUANTITI' => $kuantiti2,
'TGL_KUANTITI' => $tgl_dh
];
$action = $this->Pembelian->update('kuantiti', $update, 'ID_KUANTITI', $id_kuantiti);
$kode_stq = $this->kodeStockKuantiti();
$input = [
'KODE_STQ' => $kode_stq,
'KODE_BUKTI' => $key['KODE_PEMBELIAN'],
'TGL_STQ' => $key['TGL_SJBELI'],
'BARANG_ID' => $key['BARANG_ID'],
'QTY_BEFORE' => $global_kuantiti,
'QTY_IN' => $key['QTY_DB'],
'QTY_OUT' => '0',
'QTY_AFTER' => $kuantiti2
];
$action = $this->Pembelian->create('stock_kuantiti', $input);
}
$update = [
'STATUS_PEMBELIAN' => '0'
];
$action = $this->Pembelian->update('pembelian', $update, 'ID_PEMBELIAN', $id_pembelian);
if ($action) {
$this->session->set_flashdata('flash', 'diproses');
redirect('ManagePembelianSJ');
} else {
$this->session->set_flashdata('flashgagal', 'diproses');
redirect('ManagePembelianSJ');
}
}
}