MIF_E31212274/application/controllers/Api.php

1054 lines
30 KiB
PHP

<?php
defined('BASEPATH') or exit('No direct script access allowed');
use chriskacerguis\RestServer\RestController;
class Api extends RestController
{
public function __construct()
{
parent::__construct();
$this->load->model('keranjang_model');
$this->load->model('produk_model');
$this->load->model('menu_model');
$this->load->model('pengguna_model');
$this->load->model('ulasan_model');
$this->load->model('pembeli_model');
$this->load->model('penjualan_model');
$this->load->model('Chat_model');
}
public function chat_admin_get() {
$id_admin = $this->get('id');
$chats = $this->Chat_model->get_chat_by_admin($id_admin);
if ($chats)
{
$this->response([
'status' => true,
'data' => $chats,
], 200);
}
else
{
$this->response([
'status' => false,
'message' => 'Tidak ada chat'
], 200);
}
}
public function chat_all_pembeli_get() {
$id_pembeli = $this->get('id_pembeli');
$chats = $this->Chat_model->get_all_chat_by_pembeli($id_pembeli);
if ($chats)
{
$this->response([
'status' => true,
'data' => $chats,
], 200);
}
else
{
$this->response([
'status' => false,
'message' => 'Tidak ada chat'
], 200);
}
}
public function chat_pembeli_get() {
$id_pembeli = $this->get('id_pembeli');
$chats = $this->Chat_model->get_chat_by_pembeli($id_pembeli);
if ($chats)
{
$this->response([
'status' => true,
'data' => $chats,
], 200);
}
else
{
$this->response([
'status' => false,
'message' => 'Tidak ada chat'
], 200);
}
}
public function pesanan_get() {
$pesanan = $this->penjualan_model->get_new_penjualan();
if ($pesanan)
{
$this->response([
'status' => true,
'data' => $chats,
], 200);
}
else
{
$this->response([
'status' => false,
'message' => 'Tidak ada chat'
], 200);
}
}
public function sendChat_post() {
$data = array(
"id_admin" => $this->post("id_admin"),
"id_pembeli" => $this->post("id_pembeli"),
"pesan" => $this->post("pesan"),
"sender" => $this->post("sender"),
"timestamp" => date('Y-m-d H:i:s')
);
$result = $this->Chat_model->add_chat($data);
$this->response([
'status' => true,
'message' => 'Berhasil mengirimkan chat',
], 200);
}
public function kategori_get() {
$kategori = $this->produk_model->get_unique_kategori();
if (!empty($kategori)) {
$this->response($kategori, 200);
} else {
$this->response([
'status' => FALSE,
'message' => 'Data kategori tidak ditemukan'
], 404);
}
}
public function login_post()
{
$username = $this->post('username');
$password = $this->post('password');
$query = $this->pengguna_model->get_by_username($username);
if ($query->num_rows() > 0)
{
$result = $query->row_array();
if (password_verify($password, $result['password']))
{
$pembeli = [];
if ($result['role'] == 'Pembeli')
{
$pembeli = $this->pembeli_model->get_by_id_pengguna($result['id_pengguna'])->row_array();
}
$this->response([
'status' => true,
'data' => $result,
'pembeli' => $pembeli,
'message' => 'Login berhasil'
], 200);
}
else
{
$this->response([
'status' => false,
'message' => 'Username atau password salah'
], 200);
}
}
else
{
$this->response([
'status' => false,
'message' => 'Pengguna tidak terdaftar'
], 200);
}
}
public function kirimemail_post()
{
$email = $this->post('email');
$config = [
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_user' => 'ridhoriyadi335@gmail.com',
'smtp_pass' => 'kmehfkecuzkknlhh',
'smtp_port' => 465,
'mailtype' => 'html',
'charset' => 'utf-8',
'newline' => "\r\n"
];
$token = base64_encode(random_bytes(3));
$this->email->initialize($config);
$this->email->from('ridhoriyadi335@gmail.com', 'Token Reset Password');
$this->email->to($email);
$user_token = [
'email' => $email,
'token' => $token,
'date_created' => time()
];
$this->db->insert('user_token', $user_token);
$this->email->subject('Reset Password');
$this->email->message('Masukan token ini pada aplikasi : ' . $token);
if ($this->email->send())
{
return true;
}
else
{
echo $this->email->print_debugger();
die;
}
}
public function resetpassword_post()
{
$params = array(
'password' => password_hash($this->post('password'), PASSWORD_DEFAULT),
);
$this->pengguna_model->update_byemail($this->post('email'), $params);
$this->response([
'status' => true,
'message' => 'Detail akun berhasil diubah'
], 200);
}
public function cektoken_post()
{
$result = $this->pengguna_model->cek_token($this->post('email'), $this->post('token'));
if ($result->num_rows() < 1)
{
$this->response([
'status' => false,
'message' => 'Token Salah'
], 200);
}
else
{
$this->response([
'status' => true,
'message' => 'Token Sesuai'
], 200);
}
}
public function pembeli_post()
{
$result = $this->pengguna_model->get_by_username($this->post('username'));
if ($result->num_rows() > 0)
{
$this->response([
'status' => false,
'message' => 'Username sudah digunakan'
], 200);
}
else
{
$params = array(
'nama_lengkap' => $this->post('nama_pembeli'),
'nama_belakang' => $this->post('nama_belakang'),
'username' => $this->post('username'),
'email' => $this->post('email'),
'password' => password_hash($this->post('password'), PASSWORD_DEFAULT),
'role' => 'Pembeli',
);
$id_pengguna = $this->pengguna_model->add_pengguna($params);
$params = array(
'nama_pembeli' => $this->post('nama_pembeli'),
'alamat' => $this->post('alamat'),
'no_hp' => $this->post('no_hp'),
'email' => $this->post('email'),
'id_pengguna' => $id_pengguna,
);
$this->pembeli_model->add_pembeli($params);
$this->response([
'status' => true,
'message' => 'Registrasi berhasil'
], 200);
}
}
public function produk_get()
{
$id = $this->get('id');
if ($id === null)
{
$produk = $this->produk_model->get_all_produk_api()->result_array();
if ($produk)
{
$this->response([
'status' => true,
'data' => $produk,
], 200);
}
else
{
$this->response([
'status' => false,
'message' => 'Tidak ada produk'
], 200);
}
}
else
{
$produk = $this->produk_model->get_produk($id)->row_array();
if ($produk)
{
$this->response([
'status' => true,
'data' => $produk,
], 200);
}
else
{
$this->response([
'status' => false,
'message' => 'Tidak ada produk'
], 200);
}
}
}
public function add_produk_post()
{
$nama_file = rand() . "_" . time() . ".jpg";
$nama_produk = $this->post('nama_produk');
$harga = $this->post('harga');
$kategori = $this->post('kategori');
$deskripsi = $this->post('deskripsi');
$foto = $this->post('foto');
$params = array(
'nama_produk' => $nama_produk,
'kategori' => $kategori,
'harga' => $harga,
'deskripsi' => $deskripsi,
'foto' => $nama_file,
);
if ($foto !== null) {
if (file_put_contents('assets/images/produk/' . $nama_file, base64_decode($foto))) {
$this->produk_model->add_produk($params);
$this->response([
'status' => true,
'message' => 'Produk berhasil ditambahkan',
], 200);
} else {
$this->response([
'status' => false,
'message' => 'Gagal menyimpan foto produk',
], 200);
}
} else {
$this->response([
'status' => false,
'message' => 'Foto produk tidak diterima',
], 200);
}
}
public function menu_post()
{
$id_prod = $this->post('id_produk');
$date = date('Y-m-d');
$params = array(
'id_produk' => $id_prod,
'date_created' => $date,
);
$this->menu_model->add_menu($params);
$this->response([
'status' => true,
'message' => 'Berhasil ditambahkan',
], 200);
}
public function menu_get()
{
$date = date("Y-m-d");
$produk = $this->menu_model->get_all_menu($date)->result_array();
if ($produk)
{
$this->response([
'status' => true,
'data' => $produk,
], 200);
}
else
{
$this->response([
'status' => false,
'message' => 'Tidak ada menu'
], 200);
}
}
public function delete_menu_post()
{
$this->menu_model->delete_menu($this->post('id_menu'));
$this->response([
'status' => true,
'message' => 'Menu berhasil dihapus',
], 200);
}
public function edit_post()
{
$id_produk = $this->post('id_produk');
$nama_produk = $this->post('nama_produk');
$harga = $this->post('harga');
$deskripsi = $this->post('deskripsi');
$foto = $this->post('foto');
if (empty($foto))
{
$params = array(
'nama_produk' => $nama_produk,
'harga' => $harga,
'deskripsi' => $deskripsi,
);
$this->produk_model->update_produk($id_produk, $params);
$this->response([
'status' => true,
'message' => 'Produk berhasil diubah',
], 200);
}
else
{
$nama_file = rand() . "_" . time() . ".jpg";
$params = array(
'nama_produk' => $nama_produk,
'harga' => $harga,
'deskripsi' => $deskripsi,
'foto' => $nama_file,
);
if (file_put_contents('assets/images/produk/' . $nama_file, base64_decode($foto)))
{
$produk = $this->produk_model->get_produk($id_produk)->row_array();
if (!empty($produk['foto']))
{
unlink('assets/images/produk/' . $produk['foto']);
}
$this->produk_model->update_produk($id_produk, $params);
$this->response([
'status' => true,
'message' => 'Produk berhasil diubah',
], 200);
}
else
{
$this->response([
'status' => false,
'message' => 'Produk gagal diubah',
], 200);
}
}
}
public function delete_post()
{
$produk = $this->produk_model->get_produk($this->post('id_produk'))->row_array();
if (!empty($produk['foto']))
{
unlink('assets/images/produk/' . $produk['foto']);
}
$this->produk_model->delete_produk($this->post('id_produk'));
$this->response([
'status' => true,
'message' => 'Produk berhasil dihapus',
], 200);
}
public function ulasan_get()
{
$rate = $this->ulasan_model->get_all()->result_array();
if ($rate)
{
$this->response([
'status' => true,
'data' => $rate,
], 200);
}
else
{
$this->response([
'status' => false,
'message' => 'Tidak ada Ulasan'
], 200);
}
}
public function produk_rate_get()
{
$avg = $this->ulasan_model->get_avg($this->get('id'))->result_array();
$sum = array_sum(array_column($avg, 'avg'));
$result = $sum / count($avg);
$result = number_format($result, 1, '.', '');
$rate_1 = $this->ulasan_model->get_rate($this->get('id'), 1);
$rate_2 = $this->ulasan_model->get_rate($this->get('id'), 2);
$rate_3 = $this->ulasan_model->get_rate($this->get('id'), 3);
$rate_4 = $this->ulasan_model->get_rate($this->get('id'), 4);
$rate_5 = $this->ulasan_model->get_rate($this->get('id'), 5);
$params = array(
'avg' => $result,
'rate_1' => $rate_1,
'rate_2' => $rate_2,
'rate_3' => $rate_3,
'rate_4' => $rate_4,
'rate_5' => $rate_5,
);
if ($avg)
{
$this->response([
'status' => true,
'data' => $params,
], 200);
}
else
{
$this->response([
'status' => false,
'message' => 'Tidak ada Rate'
], 200);
}
}
public function produk_ulasan_get()
{
$ulasan = $this->ulasan_model->get_ulasan($this->get('id'))->result_array();
if ($ulasan)
{
$this->response([
'status' => true,
'ulasan' => $ulasan,
], 200);
}
else
{
$this->response([
'status' => false,
'message' => 'Tidak ada Ulasan'
], 200);
}
}
public function ulasan_post()
{
$id_produk = $this->post('id_produk');
$id_pembeli = $this->post('id_pembeli');
$rating = $this->post('rating');
$keterangan = $this->post('keterangan');
$date = date('Y-m-d');
$params = array(
'id_produk' => $id_produk,
'id_pembeli' => $id_pembeli,
'rating' => $rating,
'keterangan' => $keterangan,
'date_created' => $date,
);
$this->ulasan_model->add_ulasan($params);
$this->response([
'status' => true,
'message' => 'Berhasil menambahkan',
], 200);
}
public function search_get()
{
$kata_kunci = $this->get('kata_kunci');
$result = $this->produk_model->search_produk($kata_kunci)->result_array();
if ($result)
{
$this->response([
'status' => true,
'data' => $result
], 200);
}
else
{
$this->response([
'status' => false,
'message' => 'Produk tidak ditemukan'
], 200);
}
}
public function keranjang_post()
{
if ($this->post('date') == null) {
$params = array(
'id_pembeli' => $this->post('id_pembeli'),
'id_produk' => $this->post('id_produk'),
'jumlah' => $this->post('jumlah'),
'date' => date("Y-m-d", strtotime("tomorrow")) . ' ' . $this->post('time'),
'harga' => $this->post('harga'),
'subtotal' => $this->post('subtotal'),
);
} else {
$params = array(
'id_pembeli' => $this->post('id_pembeli'),
'id_produk' => $this->post('id_produk'),
'jumlah' => $this->post('jumlah'),
'date' => $this->post('date'),
'harga' => $this->post('harga'),
'subtotal' => $this->post('subtotal'),
);
}
$this->keranjang_model->add_keranjang($params);
$this->response([
'status' => true,
'message' => 'Produk berhasil ditambahkan ke keranjang'
], 200);
}
public function data_pembeli_get()
{
$id = $this->get('id');
$pembeli = $this->pembeli_model->get_pembeli($id)->row_array();
if ($pembeli)
{
$this->response([
'status' => true,
'data' => $pembeli,
], 200);
}
else
{
$this->response([
'status' => false,
'message' => 'Tidak ada data pembeli'
], 200);
}
}
public function keranjang_get()
{
$id_pembeli = $this->get('id');
$keranjang = $this->keranjang_model->get_keranjang($id_pembeli)->result_array();
if ($keranjang)
{
$this->response([
'status' => true,
'data' => $keranjang,
], 200);
}
else
{
$this->response([
'status' => false,
'message' => 'Keranjang belanja kosong'
], 200);
}
}
public function akun_post()
{
$result = $this->pengguna_model->cek_unik_username_lama($this->post('username'), $this->post('username_lama'));
if ($result->num_rows() > 0)
{
$this->response([
'status' => false,
'message' => 'Username sudah digunakan'
], 200);
}
else
{
$params = array(
'nama_pembeli' => $this->post('nama_pembeli'),
'alamat' => $this->post('alamat'),
'no_hp' => $this->post('no_hp'),
'email' => $this->post('email'),
);
$this->pembeli_model->update_pembeli($this->post('id_pembeli'), $params);
$pembeli = $this->pembeli_model->get_pembeli($this->post('id_pembeli'))->row_array();
if (empty($this->post('password')))
{
$params = array(
'nama_lengkap' => $this->post('nama_pembeli'),
'username' => $this->post('username'),
'email' => $this->post('email'),
);
$this->pengguna_model->update_pengguna($pembeli['id_pengguna'], $params);
}
else
{
$params = array(
'nama_lengkap' => $this->post('nama_pembeli'),
'username' => $this->post('username'),
'email' => $this->post('email'),
'password' => password_hash($this->post('password'), PASSWORD_DEFAULT),
);
$this->pengguna_model->update_pengguna($pembeli['id_pengguna'], $params);
}
$this->response([
'status' => true,
'message' => 'Detail akun berhasil diubah'
], 200);
}
}
public function pengguna_get()
{
$id = $this->get('id');
$pengguna = $this->pengguna_model->get_pengguna($id)->row_array();
if ($pengguna)
{
$this->response([
'status' => true,
'data' => $pengguna,
], 200);
}
else
{
$this->response([
'status' => false,
'message' => 'Tidak ada data pengguna'
], 200);
}
}
public function pengguna_data_post()
{
$result = $this->pengguna_model->cek_unik_username_lama($this->post('username'), $this->post('username_lama'));
if ($result->num_rows() > 0)
{
$this->response([
'status' => false,
'message' => 'Username sudah digunakan'
], 200);
}
else
{
if (empty($this->post('password')))
{
$params = array(
'nama_lengkap' => $this->post('nama_lengkap'),
'username' => $this->post('username'),
);
$this->pengguna_model->update_pengguna($this->post('id_pengguna'), $params);
}
else
{
$params = array(
'nama_lengkap' => $this->post('nama_lengkap'),
'username' => $this->post('username'),
'password' => password_hash($this->post('password'), PASSWORD_DEFAULT),
);
$this->pengguna_model->update_pengguna($this->post('id_pengguna'), $params);
}
$this->response([
'status' => true,
'message' => 'Detail akun berhasil diubah'
], 200);
}
}
public function data_keranjang_get()
{
$id_pembeli = $this->get('id');
$keranjang = $this->keranjang_model->get_keranjang($id_pembeli)->result_array();
if ($keranjang)
{
$this->response([
'status' => true,
'data' => $keranjang,
], 200);
}
else
{
$this->response([
'status' => false,
'message' => 'Keranjang belanja kosong'
], 200);
}
}
public function hapuskeranjang_get()
{
$this->keranjang_model->delete_keranjang($this->get('id_pembeli'));
$this->response([
'status' => true,
'message' => 'Keranjang belanja sudah dihapus',
], 200);
}
public function hapus_get()
{
$this->keranjang_model->delete_keranjang_item($this->get('id_keranjang'));
$this->response([
'status' => true,
'message' => 'Produk berhasil dihapus dari keranjang',
], 200);
}
public function penjualan_add_post()
{
$no_invoice = $this->post('no_invoice');
$tanggal = date('Y-m-d');
$result = $this->post('ongkir');
$status = $this->post('status_pembayaran');
$total = $this->post('total');
$keterangan = $this->post('keterangan');
if ($status == 1) {
$status_pesanan = "menunggu konfirmasi";
if (empty($keterangan))
{
$params = array(
'no_invoice' => $no_invoice,
'tanggal' => $tanggal,
'tanggal_pembayaran' => $tanggal,
'id_pembeli' => $this->post('id_pembeli'),
'total_ongkir' => $result,
'total' => $total,
'status_pembayaran' => $status,
'status_pesanan' => $status_pesanan
);
}
else
{
$params = array(
'no_invoice' => $no_invoice,
'tanggal' => $tanggal,
'tanggal_pembayaran' => $tanggal,
'id_pembeli' => $this->post('id_pembeli'),
'total_ongkir' => $result,
'total' => $total,
'keterangan' => $keterangan,
'status_pembayaran' => $status,
'status_pesanan' => $status_pesanan
);
}
} else {
$status_pesanan = "menunggu pembayaran";
if (empty($keterangan))
{
$params = array(
'no_invoice' => $no_invoice,
'tanggal' => $tanggal,
'id_pembeli' => $this->post('id_pembeli'),
'total_ongkir' => $result,
'total' => $total,
'status_pembayaran' => $status,
'status_pesanan' => $status_pesanan
);
}
else
{
$params = array(
'no_invoice' => $no_invoice,
'tanggal' => $tanggal,
'id_pembeli' => $this->post('id_pembeli'),
'total_ongkir' => $result,
'total' => $total,
'keterangan' => $keterangan,
'status_pembayaran' => $status,
'status_pesanan' => $status_pesanan
);
}
}
$id_penjualan = $this->penjualan_model->add_penjualan($params);
$keranjang = $this->keranjang_model->get_keranjang($this->post('id_pembeli'))->result();
foreach ($keranjang as $row)
{
$params2 = array(
'id_penjualan' => $id_penjualan,
'id_produk' => $row->id_produk,
'harga' => $row->harga,
'date_pemesanan' => $row->date,
'jumlah' => $row->jumlah,
'subtotal' => $row->subtotal,
);
$this->penjualan_model->add_penjualan_produk($params2);
}
$this->keranjang_model->delete_keranjang($this->post('id_pembeli'));
$params3 = array(
'alamat' => $this->post('alamat'),
'no_hp' => $this->post('no_hp'),
);
$this->pembeli_model->update_pembeli($this->post('id_pembeli'), $params3);
$this->response([
'status' => true,
'message' => 'Pesanan berhasil disimpan',
'data' => $id_penjualan
], 200);
}
public function penjualan_get()
{
$id_pembeli = $this->get('id');
$penjualan = $this->penjualan_model->get_penjualan($id_pembeli)->result_array();
if ($penjualan)
{
$this->response([
'status' => true,
'data' => $penjualan,
], 200);
}
else
{
$this->response([
'status' => false,
'message' => 'Tidak ada pesanan'
], 200);
}
}
public function allpenjualan_get()
{
$penjualan = $this->penjualan_model->get_all_penjualan()->result_array();
if ($penjualan)
{
$this->response([
'status' => true,
'data' => $penjualan,
], 200);
}
else
{
$this->response([
'status' => false,
'message' => 'Tidak ada pesanan'
], 200);
}
}
public function penjualandetail_get()
{
$id_penjualan = $this->get('id');
$penjualan_detail = $this->penjualan_model->get_penjualan_detail($id_penjualan)->result_array();
if ($penjualan_detail)
{
$this->response([
'status' => true,
'data' => $penjualan_detail,
], 200);
}
else
{
$this->response([
'status' => false,
'message' => 'Tidak ada detail penjualan'
], 200);
}
}
public function konfirmasipembayaran_post()
{
$nama_file = rand() . "_" . time() . ".jpg";
$id_penjualan = $this->post('id');
$foto = $this->post('foto');
$params = array(
'tanggal_pembayaran' => date('Y-m-d'),
'status_pembayaran' => 1,
'status_pesanan' => "menunggu konfirmasi",
'bukti_pembayaran' => $nama_file,
);
if (file_put_contents('assets/images/pembayaran/' . $nama_file, base64_decode($foto)))
{
$this->penjualan_model->update_penjualan($id_penjualan, $params);
$this->response([
'status' => true,
'message' => 'Berhasil mengunggah bukti pembayaran',
], 200);
}
else
{
$this->response([
'status' => false,
'message' => 'Bukti pembayaran gagal diunggah',
], 200);
}
}
public function status_post()
{
$id_penjualan = $this->post('id_penjualan');
$status = $this->post('status');
$params = array(
'status_pesanan' => $status,
);
$this->penjualan_model->update_penjualan($id_penjualan, $params);
$this->response([
'status' => true,
'message' => 'Status pesanan berhasil diubah',
], 200);
}
public function add_ulasan_post()
{
$params = array(
'id_pembeli' => $this->post('id_pembeli'),
'id_produk' => $this->post('id_produk'),
'rating' => $this->post('rating'),
'keterangan' => $this->post('keterangan'),
'date_created' => date('Y-m-d H:i:s'),
);
$this->ulasan_model->add_ulasan($params);
$this->response([
'status' => true,
'message' => 'Ulasan berhasil disimpan',
], 200);
}
}
?>