Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
- class Transaksi extends MY_Controller
- {
- public function __construct(){
- parent::__construct();
- if(!$this->session->userdata('username'))
- {
- redirect('login');
- }
- $this->load->model('Transaksi_Model');
- }
- public function index()
- {
- $user = $_SESSION['username'];
- $row = $this->db->query("SELECT id_user AS query FROM user WHERE username = '".$user."'")->row();
- if ($row) {
- $query = $row->query;
- }
- $this->load->model('Transaksi_Model');
- $this->load->helper('url');
- $this->halaman = 'Tambah Transaksi';
- $halaman = $this->halaman;
- $barang = $this->db->get('barang')->result_array();
- $main_view = 'transaksi/tambah';
- $this->load->view('template', compact('query', 'halaman', 'main_view', 'barang'));
- }
- public function getbarang($id)
- {
- $this->load->model('Transaksi_Model');
- $barang = $this->Transaksi_Model->get_by_id($id);
- if ($barang) {
- if ($barang->stok == '0') {
- $disabled = 'disabled';
- $info_stok = '<span class="help-block badge" id="reset"
- style="background-color: #d9534f;">
- stok habis</span>';
- }else{
- $disabled = '';
- $info_stok = '<span class="help-block badge" id="reset"
- style="background-color: #5cb85c;">stok : '
- .$barang->stok.'</span>';
- }
- echo '<div class="input-group">
- <label for="nama_barang">Nama Barang</label>
- <input type="text" class="form-control reset"
- name="nama_barang" id="nama_barang"
- value="'.$barang->nama.'"
- readonly="readonly"/>
- </div>
- <div class="input-group">
- <label for="harga_barang">Harga Barang</label>
- <input type="text" class="form-control reset"
- name="harga_barang" id="harga_barang"
- value="'.number_format( $barang->harga, 0 ,
- '' , '.' ).'"
- readonly="readonly"/>
- </div>
- <div class="input-group">
- <label for="qty">Quantity</label>
- <input type="number" class="form-control reset"
- name="qty" placeholder="Isi qty..." autocomplete="off"
- id="qty" onchange="subTotal(this.value)"
- onkeyup="subTotal(this.value)" min="0"
- max="'.$barang->stok.'" '.$disabled.'>
- </div>'.$info_stok.'';
- }else{
- echo '<div class="input-group">
- <label for="harga">Nama Barang</label>
- <input type="text" class="form-control reset"
- name="nama_barang"
- id="nama_barang" readonly="readonly"/>
- </div>
- <div class="input-group">
- <label for="harga">Harga Barang</label>
- <input type="text" class="form-control reset"
- name="harga_barang"
- id="harga_barang" readonly="readonly"/>
- </div>
- <div class="input-group">
- <label for="harga">Quantity</label>
- <input type="number" class="form-control reset"
- autocomplete="off" onchange="subTotal(this.value)"
- onkeyup="subTotal(this.value)" id="qty" min="0"
- name="qty" placeholder="Isi qty...">
- </div>';
- }
- }
- public function ajax_list_transaksi()
- {
- $data = array();
- $no = 1;
- foreach ($this->cart->contents() as $items){
- $row = array();
- $row[] = $no;
- $row[] = $items["id_user"];
- $row[] = $items["date"];
- $row[] = $items["time"];
- $row[] = $items["id"];
- $row[] = $items["name"];
- $row[] = 'Rp. ' . number_format( $items['price'],
- 0 , '' , '.' ) . ',-';
- $row[] = $items["qty"];
- $row[] = 'Rp. ' . number_format( $items['subtotal'],
- 0 , '' , '.' ) . ',-';
- //add html for action
- $row[] = '<a
- href="javascript:void()" style="color:rgb(255,128,128);
- text-decoration:none" onclick="deletebarang('
- ."'".$items["rowid"]."'".','."'".$items['subtotal'].
- "'".')"> <i class="fa fa-close"></i> Delete</a>';
- $data[] = $row;
- $no++;
- }
- $output = array(
- "data" => $data,
- );
- //output to json format
- echo json_encode($output);
- }
- public function addbarang()
- {
- $data = array(
- 'id_user' => $this->input->post('id_user'),
- 'date' => $this->input->post('datepicker'),
- 'time' => $this->input->post('waktu'),
- 'id' => $this->input->post('id_barang'),
- 'name' => $this->input->post('nama_barang'),
- 'price' => str_replace('.', '', $this->input->post(
- 'harga_barang')),
- 'qty' => $this->input->post('qty')
- );
- $insert = $this->cart->insert($data);
- echo json_encode(array("status" => TRUE));
- }
- public function deletebarang($rowid)
- {
- $this->cart->update(array(
- 'rowid'=>$rowid,
- 'qty'=>0,));
- echo json_encode(array("status" => TRUE));
- }
- public function stokawal()
- {
- foreach ($this->cart->contents() as $items){
- $row = $this->db->query("SELECT stok AS stokawal FROM barang WHERE kode = '".$items["id"]."'")->row();
- if ($row) {
- $stokawal= $row->stokawal;
- }
- $qty=$items["qty"];
- $stokakhir = $stokawal - $qty;
- echo $stokawal;
- echo $qty;
- echo $stokakhir;
- }
- }
- public function addPenjualan()
- {
- $this->load->model('Transaksi_Model');
- foreach ($this->cart->contents() as $items){
- $row = $this->db->query("SELECT stok AS stokawal FROM barang WHERE kode = '".$items["id"]."'")->row();
- if ($row) {
- $stokawal= $row->stokawal;
- }
- $qty = $items["qty"];
- $stokakhir = $stokawal - $qty;
- $this->db->set('stok', $stokakhir);
- $this->db->where('kode', $items["id"]);
- $this->db->update('barang', $data);
- $barang = array(
- 'id_user' => $items['id_user'],
- 'tanggal' => $items['date'],
- 'jam' => $items['time'],
- 'kodebarang' => $items["id"],
- 'jumlah' => $items["qty"],
- 'total' => $items["subtotal"]
- );
- $this->Transaksi_Model->addBarang($barang);
- }
- $this->Transaksi_Model->destroy();
- redirect('penjualan');
- }
- public function batalPenjualan()
- {
- $this->Transaksi_Model->destroy();
- redirect('home');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment