Files
2026-06-13 16:02:07 +10:00

255 lines
6.8 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 ManagePembelianINV extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('ModelPembelian', 'Pembelian');
$this->load->model('ModelNeraca', 'Neraca');
$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->getDataPembelianHarga();
$data = [
'sidebar' => "menu pembelian",
'navChild' => "",
'pembelian' => $datPembelian
];
$this->load->view('proses/PembelianINV', $data);
}
public function tambahHarga($id) {
$harga = $this->Pembelian->getPembelianHarga($id);
$title = $this->Pembelian->getSJPembelian($id);
$nominal_hda='0';
foreach ($harga as $key) {
$hutang = $key['HARGA_DB']*$key['QTY_DB'];
$nominal_hda = $nominal_hda+$hutang;
}
$data = [
'sidebar' => "menu pembelian",
'navChild' => "",
'harga' => $harga,
'id_pembelian' => $id,
'nominal_hda' => $nominal_hda,
'sj_pembelian' => $title[0]['SJ_PEMBELIAN'],
'inv_pembelian' => $title[0]['INV_PEMBELIAN']
];
$this->load->view('proses/PembelianHarga', $data);
}
public function saveharga() {
$method = $this->input->post('method');
$id_db = $this->input->post('id_db');
$harga_db = $this->input->post('harga_db');
$update = [
'HARGA_DB' => $harga_db
];
$action = $this->Pembelian->update('daftar_beli', $update, 'ID_DB', $id_db);
if ($action) {
$this->session->set_flashdata('flash', 'ditambah');
redirect($_SERVER['HTTP_REFERER']);
} else {
$this->session->set_flashdata('flashgagal', 'ditambah');
redirect($_SERVER['HTTP_REFERER']);
}
}
public function editharga() {
$method = $this->input->post('method');
$id_db = $this->input->post('id_db');
$harga_db = $this->input->post('harga_dbed');
$update = [
'HARGA_DB' => $harga_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 invoice() {
$method = $this->input->post('method');
$id_pembelian = $this->input->post('id_pembelian');
$inv_pembelian = $this->input->post('inv_pembelian');
$tgl_invbeli = $this->input->post('tgl_invbeli');
$tempo_invbeli = $this->input->post('tempo_invbeli');
$ppn_pembelian = $this->input->post('radio_ppn');
$harga = $this->Pembelian->getPembelianHarga($id_pembelian);
foreach ($harga as $key) {
if ($key['HARGA_DB']==NULL) {
$this->session->set_flashdata('flashharga', 'diproses');
redirect($_SERVER['HTTP_REFERER']);
}
}
$inv_pembelian = strtoupper($inv_pembelian);
if ($tempo_invbeli=="") {
$tempo_invbeli = '0';
$tempo_invbeli = $this->Pembelian->addTempo($tgl_invbeli,$tempo_invbeli);
} else {
$tempo_invbeli = $this->Pembelian->addTempo($tgl_invbeli,$tempo_invbeli);
}
$update = [
'INV_PEMBELIAN' => $inv_pembelian,
'TGL_INVBELI' => $tgl_invbeli,
'TEMPO_INVBELI' => $tempo_invbeli,
'PPN_PEMBELIAN' => $ppn_pembelian
];
$action = $this->Pembelian->update('pembelian', $update, 'ID_PEMBELIAN', $id_pembelian);
if ($action) {
$this->session->set_flashdata('flash', 'diproses');
redirect('ManagePembelianINV');
} else {
$this->session->set_flashdata('flashgagal', 'diproses');
redirect('ManagePembelianINV');
}
}
public function kodeHutangDagang() {
$kk = $this->Pembelian->getKodeHutangDagang();
if (is_array($kk) || is_object($kk)) {
foreach ($kk as $row) {
if ($row['KODE'] == null) {
return "HDA/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 = "HDA/000000";
$data = substr($data, 0, $panjang) . $str;
return $data;
}
}
}
}
public function konfirmasi() {
$method = $this->input->post('method');
$id_pembelian = $this->input->post('id_pembelian');
$kode_hda = $this->kodeHutangDagang();
$daftarHarga = $this->Pembelian->getDataDaftarHarga($id_pembelian);
foreach ($daftarHarga as $key) {
$avg_dh = $key['AVG_DH'];
$global_kuantiti = $key['GLOBAL_KUANTITI'];
$harga_db = $key['HARGA_DB'];
$qty_db = $key['QTY_DB'];
$data1 = $global_kuantiti-$qty_db;
$data2 = $data1*$avg_dh;
$data3 = $harga_db*$qty_db;
$data4 = $data2+$data3;
$data5 = $data4/$global_kuantiti;
$tgl_dh = date('Y-m-d H:i:s');
$update = [
'AVG_DH' => $data5,
'TGL_DH' => $tgl_dh
];
$action = $this->Pembelian->update('daftar_harga', $update, 'ID_DH', $key['ID_DH']);
}
$hda = $this->Pembelian->getPembelianHarga($id_pembelian);
$tempo_invbeli = $hda[0]['TEMPO_INVBELI'];
$nominal_hda='0';
foreach ($hda as $key) {
$hutang = $key['HARGA_DB']*$key['QTY_DB'];
$nominal_hda = $nominal_hda+$hutang;
}
$setdata = $this->Neraca->getSetdata_ID();
$id_setdata = $setdata[0]['SETDATA_ID'];
$neraca = $this->Neraca->getDataNilaiNeraca($id_setdata,15);
$nilai_neraca = $neraca[0]['NILAI_NERACA'];
$id_neraca = $neraca[0]['ID_NERACA'];
$nilai = $nilai_neraca+$nominal_hda;
$update = [
'NILAI_NERACA' => $nilai
];
$action = $this->Neraca->update('neraca', $update, 'ID_NERACA', $id_neraca);
$neraca = $this->Neraca->getDataNilaiNeraca($id_setdata,27);
$nilai_neraca = $neraca[0]['NILAI_NERACA'];
$id_neraca = $neraca[0]['ID_NERACA'];
$nilai = $nilai_neraca+$nominal_hda;
$update = [
'NILAI_NERACA' => $nilai
];
$action = $this->Neraca->update('neraca', $update, 'ID_NERACA', $id_neraca);
$input = [
'PEMBELIAN_ID' => $id_pembelian,
'NOMINAL_HDA' => $nominal_hda,
'KODE_HDA' => $kode_hda,
'TEMPO_HDA' => $tempo_invbeli,
];
$action = $this->Pembelian->create('hutang_dagang', $input);
$update = [
'STATUS_PEMBELIAN' => '1'
];
$action = $this->Pembelian->update('pembelian', $update, 'ID_PEMBELIAN', $id_pembelian);
if ($action) {
$this->session->set_flashdata('flash', 'diproses');
redirect('ManagePembelianINV');
} else {
$this->session->set_flashdata('flashgagal', 'diproses');
redirect('ManagePembelianINV');
}
}
}