PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/application/controllers/Keranjang.php

https://gitlab.com/alfiantaufani/koperasi-tugas-kuliah
PHP | 230 lines | 204 code | 23 blank | 3 comment | 19 complexity | 9cf30e33b29a4509cacb3ef23a2798f3 MD5 | raw file
  1. <?php
  2. header('Access-Control-Allow-Origin: *');
  3. header("Access-Control-Allow-Methods: GET, OPTIONS");
  4. defined('BASEPATH') OR exit('No direct script access allowed');
  5. class Keranjang extends CI_Controller {
  6. public function __construct()
  7. {
  8. parent::__construct();
  9. $this->load->library('form_validation');
  10. }
  11. public function index()
  12. {
  13. $this->form_validation->set_rules('username', 'Username', 'required');
  14. $this->form_validation->set_rules('password', 'Password', 'required');
  15. if ($this->form_validation->run() == TRUE) {
  16. //get data dari FORM
  17. $username = $this->input->post("username", TRUE);
  18. $password = $this->input->post('password', TRUE);
  19. $checking = $this->M_login->check_login('tbl_admin', array('username' => $username), array('password' => md5($password)));
  20. if ($checking != FALSE) {
  21. foreach ($checking as $apps) {
  22. $data = array(
  23. 'id' => $apps->id,
  24. 'username' => $apps->username,
  25. 'nama' => $apps->nama,
  26. 'password' => $apps->password,
  27. 'level' => $apps->level
  28. );
  29. }
  30. $status = 'success';
  31. }else{
  32. $status = 'error';
  33. $data = 'user atau password salah.';
  34. }
  35. $this->output->set_content_type('application/json');
  36. $this->output->set_output(json_encode([
  37. 'status' => $status,
  38. 'data' => $data
  39. ]));
  40. }else{
  41. $this->output->set_content_type('application/json');
  42. $this->output->set_output(json_encode([
  43. 'status' => 'error',
  44. 'data' => 'form harus diisi.',
  45. ]));
  46. }
  47. }
  48. public function store()
  49. {
  50. $this->form_validation->set_rules('barcode', 'Barcode', 'required');
  51. if ($this->form_validation->run() == FALSE) {
  52. $this->output->set_content_type('application/json');
  53. $this->output->set_output(json_encode([
  54. 'status' => 'error',
  55. 'message' => 'form harus diisi.',
  56. ]));
  57. }else{
  58. $barcode = $this->input->post("barcode", TRUE);
  59. $cek_barang = $this->db->query("SELECT * FROM tbl_produk WHERE kode_produk='$barcode'");
  60. $barcode_sendiri = $this->db->query("SELECT * FROM tbl_produk WHERE barcode_sendiri='$barcode'");
  61. $cek_keranjang = $this->db->query("SELECT * FROM tbl_keranjang WHERE kode_produk='$barcode'");
  62. if ($cek_barang->num_rows() > 0){
  63. $data = $cek_barang->row();
  64. if ($cek_keranjang->num_rows() > 0){
  65. $this->output->set_content_type('application/json');
  66. $this->output->set_output(json_encode([
  67. 'status' => 'error',
  68. 'message' => 'Barang sudah ada di keranjang.',
  69. ]));
  70. }else{
  71. if ($data->stock > 0) {
  72. $insert_keranjang = array(
  73. 'id_produk' => $data->id_produk,
  74. 'kode_produk' => $data->kode_produk,
  75. 'nama_produk' => $data->nama_produk,
  76. 'qty' => 1,
  77. 'harga' => $data->harga_jual,
  78. );
  79. $create = $this->db->insert('tbl_keranjang', $insert_keranjang);
  80. if ($create) {
  81. require APPPATH . '/views/vendor/autoload.php';
  82. $pusher = new Pusher\Pusher(
  83. "fcc6b0d11fd751af380d",
  84. "3dc5db5486f39251742b",
  85. "1399648",
  86. array('cluster' => 'ap1')
  87. );
  88. $pusher->trigger('my-channel', 'my-event', array('message' => 'hello world'));
  89. // output
  90. $this->output->set_content_type('application/json');
  91. $this->output->set_output(json_encode([
  92. 'status' => 'success',
  93. ]));
  94. }else{
  95. $this->output->set_content_type('application/json');
  96. $this->output->set_output(json_encode([
  97. 'status' => 'error',
  98. 'message' => 'Gagal input keranjang.',
  99. ]));
  100. }
  101. } else {
  102. $this->output->set_content_type('application/json');
  103. $this->output->set_output(json_encode([
  104. 'status' => 'error',
  105. 'message' => 'Stock produk kosong.',
  106. ]));
  107. }
  108. }
  109. }elseif($barcode_sendiri->num_rows() > 0){
  110. $dari_barcode = $barcode_sendiri->row();
  111. if ($cek_keranjang->num_rows() > 0){
  112. $this->output->set_content_type('application/json');
  113. $this->output->set_output(json_encode([
  114. 'status' => 'error',
  115. 'message' => 'Barang sudah ada di keranjang.',
  116. ]));
  117. }else{
  118. if ($dari_barcode->stock > 0) {
  119. $insert_keranjang = array(
  120. 'id_produk' => $dari_barcode->id_produk,
  121. 'kode_produk' => $dari_barcode->barcode_sendiri,
  122. 'nama_produk' => $dari_barcode->nama_produk,
  123. 'qty' => 1,
  124. 'harga' => $dari_barcode->harga_jual,
  125. );
  126. $create = $this->db->insert('tbl_keranjang', $insert_keranjang);
  127. if ($create) {
  128. require APPPATH . '/views/vendor/autoload.php';
  129. $pusher = new Pusher\Pusher(
  130. "fcc6b0d11fd751af380d",
  131. "3dc5db5486f39251742b",
  132. "1399648",
  133. array('cluster' => 'ap1')
  134. );
  135. $pusher->trigger('my-channel', 'my-event', array('message' => 'hello world'));
  136. // output
  137. $this->output->set_content_type('application/json');
  138. $this->output->set_output(json_encode([
  139. 'status' => 'success',
  140. ]));
  141. }else{
  142. $this->output->set_content_type('application/json');
  143. $this->output->set_output(json_encode([
  144. 'status' => 'error',
  145. 'message' => 'Gagal input keranjang.',
  146. ]));
  147. }
  148. } else {
  149. $this->output->set_content_type('application/json');
  150. $this->output->set_output(json_encode([
  151. 'status' => 'error',
  152. 'message' => 'Stock produk kosong.',
  153. ]));
  154. }
  155. }
  156. }else{
  157. $this->output->set_content_type('application/json');
  158. $this->output->set_output(json_encode([
  159. 'status' => 'error',
  160. 'message' => 'Barcode produk tidak ada',
  161. ]));
  162. }
  163. }
  164. }
  165. public function show()
  166. {
  167. $draw = intval($this->input->get("draw"));
  168. $start = intval($this->input->get("start"));
  169. $length = intval($this->input->get("length"));
  170. $data = $this->db->get('tbl_keranjang');
  171. $result = array(
  172. "draw" => $draw,
  173. "recordsTotal" => $data->num_rows(),
  174. "recordsFiltered" => $data->num_rows(),
  175. "data" => $data->result()
  176. );
  177. echo json_encode($result);
  178. exit();
  179. }
  180. public function update()
  181. {
  182. if ($_POST['id'] != "") {
  183. $result = array();
  184. foreach ($_POST['id'] as $key => $val) {
  185. $result[] = array(
  186. 'id' => $_POST['id'][$key],
  187. 'qty' => $_POST['qty'][$key],
  188. );
  189. }
  190. $update = $this->db->update_batch('tbl_keranjang', $result, 'id');
  191. echo '<script>window.location.href = "'.base_url("kasir?cek=true").'";</script>';
  192. }else{
  193. echo '<script>window.location.href = "'.base_url("kasir").'";</script>';
  194. }
  195. }
  196. public function delete()
  197. {
  198. $delete = $this->db->delete('tbl_keranjang', array('id' => $this->input->post("id")));
  199. if ($delete) {
  200. $this->output->set_content_type('application/json');
  201. $this->output->set_output(json_encode('success'));
  202. } else {
  203. $this->output->set_content_type('application/json');
  204. $this->output->set_output(json_encode('error'));
  205. }
  206. }
  207. }