tripsdoc

Transaksi.php

Dec 12th, 2016
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.99 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class Transaksi extends MY_Controller
  5. {
  6.     public function __construct(){
  7.         parent::__construct();
  8.         if(!$this->session->userdata('username'))
  9.         {
  10.             redirect('login');
  11.         }
  12.         $this->load->model('Transaksi_Model');
  13.     }
  14.     public function index()
  15.     {
  16.         $user = $_SESSION['username'];
  17.         $row = $this->db->query("SELECT id_user AS query FROM user WHERE username = '".$user."'")->row();
  18.         if ($row) {
  19.             $query = $row->query;
  20.         }
  21.         $this->load->model('Transaksi_Model');
  22.         $this->load->helper('url');
  23.        
  24.         $this->halaman = 'Tambah Transaksi';
  25.         $halaman        = $this->halaman;
  26.        
  27.         $barang = $this->db->get('barang')->result_array();
  28.         $main_view = 'transaksi/tambah';
  29.         $this->load->view('template', compact('query', 'halaman', 'main_view', 'barang'));
  30.     }
  31.     public function getbarang($id)
  32.     {
  33.  
  34.         $this->load->model('Transaksi_Model');
  35.  
  36.         $barang = $this->Transaksi_Model->get_by_id($id);
  37.  
  38.         if ($barang) {
  39.  
  40.             if ($barang->stok == '0') {
  41.                 $disabled = 'disabled';
  42.                 $info_stok = '<span class="help-block badge" id="reset"
  43.                               style="background-color: #d9534f;">
  44.                               stok habis</span>';
  45.             }else{
  46.                 $disabled = '';
  47.                 $info_stok = '<span class="help-block badge" id="reset"
  48.                               style="background-color: #5cb85c;">stok : '
  49.                               .$barang->stok.'</span>';
  50.             }
  51.  
  52.             echo '<div class="input-group">
  53.                     <label for="nama_barang">Nama Barang</label>
  54.                     <input type="text" class="form-control reset"
  55.                         name="nama_barang" id="nama_barang"
  56.                         value="'.$barang->nama.'"
  57.                         readonly="readonly"/>
  58.                   </div>
  59.                   <div class="input-group">
  60.                     <label for="harga_barang">Harga Barang</label>
  61.                     <input type="text" class="form-control reset"
  62.                         name="harga_barang" id="harga_barang"
  63.                         value="'.number_format( $barang->harga, 0 ,
  64.                          '' , '.' ).'"
  65.                         readonly="readonly"/>
  66.                   </div>
  67.                   <div class="input-group">
  68.                     <label for="qty">Quantity</label>
  69.                     <input type="number" class="form-control reset"
  70.                             name="qty" placeholder="Isi qty..." autocomplete="off"  
  71.                             id="qty" onchange="subTotal(this.value)"
  72.                             onkeyup="subTotal(this.value)"  min="0"
  73.                             max="'.$barang->stok.'" '.$disabled.'>
  74.                   </div>'.$info_stok.'';
  75.         }else{
  76.  
  77.             echo '<div class="input-group">
  78.                     <label for="harga">Nama Barang</label>
  79.                     <input type="text" class="form-control reset"
  80.                     name="nama_barang"
  81.                     id="nama_barang" readonly="readonly"/>
  82.                   </div>
  83.                   <div class="input-group">
  84.                     <label for="harga">Harga Barang</label>
  85.                     <input type="text" class="form-control reset"
  86.                     name="harga_barang"
  87.                     id="harga_barang" readonly="readonly"/>
  88.                   </div>
  89.                   <div class="input-group">
  90.                     <label for="harga">Quantity</label>
  91.                     <input type="number" class="form-control reset"
  92.                         autocomplete="off" onchange="subTotal(this.value)"
  93.                         onkeyup="subTotal(this.value)" id="qty" min="0"
  94.                         name="qty" placeholder="Isi qty...">
  95.                   </div>';
  96.         }
  97.  
  98.     }
  99.     public function ajax_list_transaksi()
  100.     {
  101.         $data = array();
  102.  
  103.         $no = 1;
  104.        
  105.         foreach ($this->cart->contents() as $items){
  106.            
  107.             $row = array();
  108.             $row[] = $no;
  109.             $row[] = $items["id_user"];
  110.             $row[] = $items["date"];
  111.             $row[] = $items["time"];
  112.             $row[] = $items["id"];
  113.             $row[] = $items["name"];
  114.             $row[] = 'Rp. ' . number_format( $items['price'],
  115.                     0 , '' , '.' ) . ',-';
  116.             $row[] = $items["qty"];
  117.             $row[] = 'Rp. ' . number_format( $items['subtotal'],
  118.                     0 , '' , '.' ) . ',-';
  119.  
  120.             //add html for action
  121.             $row[] = '<a
  122.                 href="javascript:void()" style="color:rgb(255,128,128);
  123.                 text-decoration:none" onclick="deletebarang('
  124.                     ."'".$items["rowid"]."'".','."'".$items['subtotal'].
  125.                     "'".')"> <i class="fa fa-close"></i> Delete</a>';
  126.        
  127.             $data[] = $row;
  128.             $no++;
  129.         }
  130.  
  131.         $output = array(
  132.                         "data" => $data,
  133.                 );
  134.         //output to json format
  135.         echo json_encode($output);
  136.     }
  137.     public function addbarang()
  138.     {
  139.  
  140.         $data = array(
  141.                 'id_user' => $this->input->post('id_user'),
  142.                 'date' => $this->input->post('datepicker'),
  143.                 'time' => $this->input->post('waktu'),
  144.                 'id' => $this->input->post('id_barang'),
  145.                 'name' => $this->input->post('nama_barang'),
  146.                 'price' => str_replace('.', '', $this->input->post(
  147.                     'harga_barang')),
  148.                 'qty' => $this->input->post('qty')
  149.             );
  150.         $insert = $this->cart->insert($data);
  151.         echo json_encode(array("status" => TRUE));
  152.     }
  153.     public function deletebarang($rowid)
  154.     {
  155.  
  156.         $this->cart->update(array(
  157.                 'rowid'=>$rowid,
  158.                 'qty'=>0,));
  159.         echo json_encode(array("status" => TRUE));
  160.     }
  161.     public function stokawal()
  162.     {
  163.         foreach ($this->cart->contents() as $items){
  164.         $row = $this->db->query("SELECT stok AS stokawal FROM barang WHERE kode = '".$items["id"]."'")->row();
  165.         if ($row) {
  166.             $stokawal= $row->stokawal;
  167.         }
  168.         $qty=$items["qty"];
  169.         $stokakhir = $stokawal - $qty;
  170.         echo $stokawal;
  171.         echo $qty;
  172.         echo $stokakhir;
  173.         }
  174.     }
  175.     public function addPenjualan()
  176.     {
  177.         $this->load->model('Transaksi_Model');
  178.         foreach ($this->cart->contents() as $items){
  179.             $row = $this->db->query("SELECT stok AS stokawal FROM barang WHERE kode = '".$items["id"]."'")->row();
  180.             if ($row) {
  181.                 $stokawal= $row->stokawal;
  182.             }
  183.             $qty = $items["qty"];
  184.             $stokakhir = $stokawal - $qty;
  185.            
  186.             $this->db->set('stok', $stokakhir);
  187.             $this->db->where('kode', $items["id"]);
  188.             $this->db->update('barang', $data);
  189.            
  190.             $barang = array(
  191.                 'id_user' => $items['id_user'],
  192.                 'tanggal' => $items['date'],
  193.                 'jam' => $items['time'],
  194.                 'kodebarang' => $items["id"],
  195.                 'jumlah' => $items["qty"],
  196.                 'total' => $items["subtotal"]
  197.             );
  198.             $this->Transaksi_Model->addBarang($barang);
  199.            
  200.         }
  201.         $this->Transaksi_Model->destroy();
  202.         redirect('penjualan');
  203.     }
  204.     public function batalPenjualan()
  205.     {
  206.         $this->Transaksi_Model->destroy();
  207.         redirect('home');
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment