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

375 lines
13 KiB
PHP

<?php
defined('BASEPATH') or exit('No direct script access allowed');
class ManagePelanggan extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('ModelDashboard', 'Dashboard');
$this->load->model('AllModels', 'Amod');
$this->load->library(['datatables', 'form_validation']);
$this->form_validation->set_error_delimiters('', '');
$this->Dashboard->ceklogin();
}
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() {
$id_karyawan = $this->session->userdata('id_karyawan');
$username = $this->session->userdata('username');
$datShm = $this->Dashboard->getData_SetHakMaster_byKARYAWAN($id_karyawan);
$datKelompok = $this->Dashboard->getData_Kelompok();
$datKota = $this->Dashboard->getData_Kota();
$datPelLatest = $this->Dashboard->getData_Pelanggan_orderDESC_limit10();
$tgl_now = date('d/m/Y');
$datSpe = $this->Dashboard->getData_SetPelanggan_byKARYAWAN($id_karyawan);
if ($datSpe==FALSE) {
$id_kota = 2;
$id_kelompok = 0;
$nama_spe = NULL;
$type_spe = 9;
} else {
$id_kota = $datSpe[0]['KOTA_ID'];
$id_kelompok = $datSpe[0]['KELOMPOK_ID'];
$nama_spe = $datSpe[0]['NAMA_SPE'];
$type_spe = $datSpe[0]['TYPE_SPE'];
}
$datPelanggan = $this->Dashboard->getData_Pelanggan_byNAMA_byTYPE_byKOTA_byKELOMPOK($nama_spe, $type_spe, $id_kota, $id_kelompok);
$no = 0;
foreach ($datPelanggan as $key) {
$datSo = $this->Dashboard->getData_SalesOrder_byPELANGGAN($key['ID_PELANGGAN']);
$datHsn = $this->Dashboard->getData_HistoriSn_byPELANGGAN($key['ID_PELANGGAN']);
$datPda = $this->Dashboard->getData_PiutangDagang_byPELANGGAN($key['ID_PELANGGAN']);
$datDpj = $this->Dashboard->getData_DpPenjualan_byPELANGGAN($key['ID_PELANGGAN']);
$datService = $this->Dashboard->getData_Service_byPELANGGAN($key['ID_PELANGGAN']);
if ($datSo==TRUE OR $datHsn==TRUE OR $datPda==TRUE OR $datDpj==TRUE OR $datService==TRUE) {
$lock = 1;
} else {
$lock = 0;
}
$datPelanggan[$no++] += ['LOCK' => $lock];
}
if ($datShm[0]['PELANGGAN_SHM']==0) {
$akses = "Tidak Berhak";
$datPelanggan = NULL;
$datPelLatest = NULL;
} else if ($datShm[0]['PELANGGAN_SHM']==1) {
$akses = "Hanya Baca";
} else if ($datShm[0]['PELANGGAN_SHM']==2) {
$akses = "Akses Penuh";
}
$data = [
'sidebar' => "dashboard",
'navChild' => "",
'pelanggan' => $datPelanggan,
'pel_latest' => $datPelLatest,
'kelompok' => $datKelompok,
'kota' => $datKota,
'id_kelompok' => $id_kelompok,
'id_kota' => $id_kota,
'nama_spe' => $nama_spe,
'type_spe' => $type_spe,
'tgl_now' => $tgl_now,
'hak' => $datShm[0]['PELANGGAN_SHM'],
'akses' => $akses,
'username' => $username,
];
$this->load->view('master/Pelanggan', $data);
}
public function simpanset_AA1() {
$method = $this->input->post('method');
$id_kelompok = $this->input->post('id_kelompok');
$id_kota = $this->input->post('id_kota');
$nama_spe = $this->input->post('nama_spe');
$type_spe = $this->input->post('type_spe');
$id_karyawan = $this->session->userdata('id_karyawan');
$tgl = date('Y-m-d H:i:s');
$nama_spe = strtoupper($nama_spe);
$datSpe = $this->Dashboard->getData_SetPelanggan_byKARYAWAN($id_karyawan);
if ($datSpe==FALSE) {
$input = [
'KELOMPOK_ID' => $id_kelompok,
'KOTA_ID' => $id_kota,
'NAMA_SPE' => $nama_spe,
'TYPE_SPE' => $type_spe,
'KARYAWAN_ID' => $id_karyawan,
'LAST_SPE' => $tgl,
];
$action = $this->Dashboard->create('set_pelanggan', $input);
$set = 1;
} else {
$update = [
'KELOMPOK_ID' => $id_kelompok,
'KOTA_ID' => $id_kota,
'NAMA_SPE' => $nama_spe,
'TYPE_SPE' => $type_spe,
'LAST_SPE' => $tgl,
];
$action = $this->Dashboard->update('set_pelanggan', $update, 'ID_SPE', $datSpe[0]['ID_SPE']);
$set = 2;
}
if ($set==1) {
$this->session->set_flashdata('filter_success', 'ditambah');
redirect($_SERVER['HTTP_REFERER']);
} else if ($set==2) {
$this->session->set_flashdata('filter_success', 'diperbaruhi');
redirect($_SERVER['HTTP_REFERER']);
} else {
$this->session->set_flashdata('filter_failed', 'ditambah');
redirect($_SERVER['HTTP_REFERER']);
}
}
public function simpan() {
$method = $this->input->post('method');
$id_pelanggan = $this->input->post('id');
$id_kelompok = $this->input->post('id_kelompok');
$id_kota = $this->input->post('id_kota');
$nama_pelanggan = $this->input->post('nama_pelanggan');
$alias_pelanggan = $this->input->post('alias_pelanggan');
$alamat_pelanggan = $this->input->post('alamat_pelanggan');
$kirim_pelanggan = $this->input->post('kirim_pelanggan');
$telp_pelanggan = $this->input->post('telp_pelanggan');
$pic_pelanggan = $this->input->post('pic_pelanggan');
$npwp_pelanggan = $this->input->post('npwp_pelanggan');
$email_pelanggan = $this->input->post('email_pelanggan');
$type_pelanggan = $this->input->post('type_pelanggan');
$top_pelanggan = $this->input->post('top_pelanggan');
$id_karyawan = $this->session->userdata('id_karyawan');
$datPelanggan = $this->Dashboard->getData_Pelanggan_byNAMA($nama_pelanggan);
if ($datPelanggan==TRUE) {
$this->session->set_flashdata('data_copy', 'error');
redirect('ManagePelanggan');
}
if ($top_pelanggan<0) {
$this->session->set_flashdata('not_valid', 'error');
redirect('ManagePelanggan');
}
if ($id_karyawan==11) {
$top_pelanggan = 0;
}
if ($type_pelanggan=="2") {
$limit_pelanggan=25000000;
} else if ($type_pelanggan=="1") {
$limit_pelanggan=100000000;
} else if ($type_pelanggan=="0") {
$limit_pelanggan=300000000;
}
$email_pelanggan = preg_replace('/"/', '', $email_pelanggan);
$input = [
'NAMA_PELANGGAN' => $nama_pelanggan,
'ALIAS_PELANGGAN' => $alias_pelanggan,
'ALAMAT_PELANGGAN' => $alamat_pelanggan,
'KIRIM_PELANGGAN' => $kirim_pelanggan,
'TELP_PELANGGAN' => $telp_pelanggan,
'PIC_PELANGGAN' => $pic_pelanggan,
'NPWP_PELANGGAN' => $npwp_pelanggan,
'EMAIL_PELANGGAN' => $email_pelanggan,
'TYPE_PELANGGAN' => $type_pelanggan,
'KELOMPOK_ID' => $id_kelompok,
'KOTA_ID' => $id_kota,
'LIMIT_PELANGGAN' => $limit_pelanggan,
'TOP_PELANGGAN' => $top_pelanggan,
'JANGKA_PELANGGAN' => '60'
];
$action = $this->Dashboard->create('pelanggan', $input);
if ($action) {
$this->session->set_flashdata('success', 'ditambah');
redirect('ManagePelanggan');
} else {
$this->session->set_flashdata('failed', 'ditambah');
redirect('ManagePelanggan');
}
}
public function ubah() {
$method = $this->input->post('method');
$id_pelanggan = $this->input->post('id_pelangganed');
$type_pelanggan = $this->input->post('type_pelangganed');
$id_kota = $this->input->post('id_kotaed');
$nama_pelanggan = $this->input->post('nama_pelangganed');
$alias_pelanggan = $this->input->post('alias_pelangganed');
$alamat_pelanggan = $this->input->post('alamat_pelangganed');
$kirim_pelanggan = $this->input->post('kirim_pelangganed');
$telp_pelanggan = $this->input->post('telp_pelangganed');
$pic_pelanggan = $this->input->post('pic_pelangganed');
$npwp_pelanggan = $this->input->post('npwp_pelangganed');
$email_pelanggan = $this->input->post('email_pelangganed');
$id_kelompok = $this->input->post('id_kelompoked');
$limit_pelanggan = $this->input->post('limit_pelangganed');
$top_pelanggan = $this->input->post('top_pelangganed');
$jangka_pelanggan = $this->input->post('jangka_pelangganed');
$username = $this->session->userdata('username');
$id_karyawan = $this->session->userdata('id_karyawan');
$datPelanggan1 = $this->Dashboard->getData_Pelanggan_byID($id_pelanggan);
if ($datPelanggan1[0]['NAMA_PELANGGAN']!=$nama_pelanggan ) {
$datPelanggan2 = $this->Dashboard->getData_Pelanggan_byNAMA($nama_pelanggan);
if ($datPelanggan2==TRUE) {
$this->session->set_flashdata('data_copy', 'error');
redirect('ManagePelanggan');
}
}
if ($username=="admin" OR $username=="sugeng" OR $username=="iwan" OR $username=="helda" OR $username=="venny") {
if ($top_pelanggan<0 OR $jangka_pelanggan<0 OR $limit_pelanggan<=0) {
$this->session->set_flashdata('not_valid', 'error');
redirect($_SERVER['HTTP_REFERER']);
}
} else {
if ($id_karyawan==11) {
$top_pelanggan = 0;
}
if ($top_pelanggan<0) {
$this->session->set_flashdata('not_valid', 'error');
redirect($_SERVER['HTTP_REFERER']);
}
}
if ($id_kelompok=="" OR $id_kelompok==NULL) {
$id_kelompok = 0;
}
$email_pelanggan = preg_replace('/"/', '', $email_pelanggan);
if ($username=="admin" OR $username=="sugeng" OR $username=="helda" OR $username=="venny") {
$update = [
'NAMA_PELANGGAN' => $nama_pelanggan,
'ALIAS_PELANGGAN' => $alias_pelanggan,
'ALAMAT_PELANGGAN' => $alamat_pelanggan,
'KIRIM_PELANGGAN' => $kirim_pelanggan,
'TELP_PELANGGAN' => $telp_pelanggan,
'PIC_PELANGGAN' => $pic_pelanggan,
'NPWP_PELANGGAN' => $npwp_pelanggan,
'EMAIL_PELANGGAN' => $email_pelanggan,
'TYPE_PELANGGAN' => $type_pelanggan,
'KELOMPOK_ID' => $id_kelompok,
'KOTA_ID' => $id_kota,
'LIMIT_PELANGGAN' => $limit_pelanggan,
'TOP_PELANGGAN' => $top_pelanggan,
'JANGKA_PELANGGAN' => $jangka_pelanggan,
];
} else if ($username=="iwan" OR $username=="gita") {
$update = [
'NAMA_PELANGGAN' => $nama_pelanggan,
'ALIAS_PELANGGAN' => $alias_pelanggan,
'ALAMAT_PELANGGAN' => $alamat_pelanggan,
'KIRIM_PELANGGAN' => $kirim_pelanggan,
'TELP_PELANGGAN' => $telp_pelanggan,
'PIC_PELANGGAN' => $pic_pelanggan,
'NPWP_PELANGGAN' => $npwp_pelanggan,
'EMAIL_PELANGGAN' => $email_pelanggan,
'TYPE_PELANGGAN' => $type_pelanggan,
'LIMIT_PELANGGAN' => $limit_pelanggan,
'KELOMPOK_ID' => $id_kelompok,
'KOTA_ID' => $id_kota,
'TOP_PELANGGAN' => $top_pelanggan
];
} else {
$update = [
'NAMA_PELANGGAN' => $nama_pelanggan,
'ALIAS_PELANGGAN' => $alias_pelanggan,
'ALAMAT_PELANGGAN' => $alamat_pelanggan,
'KIRIM_PELANGGAN' => $kirim_pelanggan,
'TELP_PELANGGAN' => $telp_pelanggan,
'PIC_PELANGGAN' => $pic_pelanggan,
'NPWP_PELANGGAN' => $npwp_pelanggan,
'EMAIL_PELANGGAN' => $email_pelanggan,
'TYPE_PELANGGAN' => $type_pelanggan,
'KELOMPOK_ID' => $id_kelompok,
'KOTA_ID' => $id_kota,
];
}
$action = $this->Dashboard->update('pelanggan', $update, 'ID_PELANGGAN', $id_pelanggan);
if ($action) {
$this->session->set_flashdata('success', 'diubah');
redirect('ManagePelanggan');
} else {
$this->session->set_flashdata('failed', 'diubah');
redirect('ManagePelanggan');
}
}
public function hapus() {
$id = $this->input->post('id_pelanggandel');
$datSo = $this->Dashboard->getData_SalesOrder_byPELANGGAN($id);
$datHsn = $this->Dashboard->getData_HistoriSn_byPELANGGAN($id);
$datPda = $this->Dashboard->getData_PiutangDagang_byPELANGGAN($id);
$datDpj = $this->Dashboard->getData_DaftarAkuntansi_byPELANGGAN($id);
$datService = $this->Dashboard->getData_Service_byPELANGGAN($id);
$datProyek = $this->Dashboard->getData_Proyek_byPELANGGAN($id);
if ($datSo==TRUE OR $datHsn==TRUE OR $datPda==TRUE OR $datDpj==TRUE OR $datService==TRUE OR $datProyek==TRUE) {
$this->session->set_flashdata('data_used', 'error');
redirect('ManagePelanggan');
}
$action = $this->Dashboard->delete('pelanggan', $id, 'ID_PELANGGAN');
if ($action) {
$this->session->set_flashdata('success', 'dihapus');
redirect('ManagePelanggan');
} else {
$this->session->set_flashdata('failed', 'dihapus');
redirect('ManagePelanggan');
}
}
public function simpan_excel_pelanggan() {
$id_karyawan = $this->session->userdata('id_karyawan');
$pilih_tabel = array('No', 'Tipe', 'Nama' ,'Alias', 'Alamat', 'Alamat Kirim', 'Kota', 'PIC', 'Kelompok', 'Telepon', 'NPWP', 'Email');
$tgl = date('d/m/Y');
$datSpe = $this->Dashboard->getData_SetPelanggan_byKARYAWAN($id_karyawan);
if ($datSpe==FALSE) {
$id_kota = 2;
$id_kelompok = 0;
$nama_spe = NULL;
$type_spe = 9;
} else {
$id_kota = $datSpe[0]['KOTA_ID'];
$id_kelompok = $datSpe[0]['KELOMPOK_ID'];
$nama_spe = $datSpe[0]['NAMA_SPE'];
$type_spe = $datSpe[0]['TYPE_SPE'];
}
$dataExcel = $this->Dashboard->getData_Pelanggan_byNAMA_byTYPE_byKOTA_byKELOMPOK_forExcel($nama_spe, $type_spe, $id_kota, $id_kelompok);
$data = [
'title' => '',
'jumlah_kolom' => count($pilih_tabel),
'nama_tabel' => $pilih_tabel,
'laporan' => $dataExcel,
'tgl' => $tgl,
];
$this->load->view('cetak/excel_pelanggan', $data);
}
}