/** * {inheritdoc} */ public static function add($data) { if ($return = static::db()->insert(static::$table, $data)) { $prod_primary = Product::primary(); foreach (unserialize($data['produk']) as $id => $qty) { $product = Product::show([$prod_primary => $id])->fetchOne(); if (!$return) { break; } $return = Product::edit(['stok' => $product->stok - $qty], [$prod_primary => $product->id_produk]); } return $return; } return false; }
public function cart() { $items = []; if ($_tmp = session('cart-items')) { $items = unserialize($_tmp); } if ($do = get('do')) { $id = get('id'); if ($do != 'clear' and !$id) { redirect('shop'); } session('cart-items', serialize(Order::cart($id, $do, $items))); return redirect('cart'); } $data = ['items' => $items, 'heading' => 'Keranjang Belanja: ' . ($items ? count($items) . ' Produk' : 'Masih kosong :(')]; $cartItems = array_keys($items); if (count($cartItems) > 0) { $data['data'] = Product::show(Product::primary() . ' in (' . implode(',', $cartItems) . ')'); } else { $data['data'] = false; } return $this->render('cart', $data); }
<table class="data"> <thead> <tr> <th>Gambar</th> <th>Nama</th> <th>Qty</th> <th>Harga Satuan (Rp.)</th> <th>Subtotal (Rp.)</th> </tr> </thead> <tbody id="tbl-produk"> <?php if ($data) { $products = unserialize($data->produk); $prodKey = Product::primary(); $ordererProducts = Product::show($prodKey . ' in (' . implode(',', array_keys($products)) . ')'); if ($ordererProducts->count() > 0) { foreach ($ordererProducts->fetch(false) as $product) { $prodId = $product->{$prodKey}; $harga = $product->diskon ?: $product->harga; $diskon = $product->diskon ? '<br>(diskon dari: <del>' . format_number($product->harga) . '</del>)' : ''; $subtotal = $products[$prodId] * $harga; ?> <tr> <td><span class="thumb" style="background-image: url(<?php echo site_url('asset/uploads/' . $product->gambar); ?> );"></span></td> <td><?php echo '<strong>' . $product->nama . '</strong><br>' . $product->keterangan;
public function orders($do = '', $id = '') { $this->data['heading'] = 'Administrasi: Pembelian'; switch ($do) { case 'form': if (post('submit')) { $userKey = User::primary(); $customerKey = Customer::primary(); $productKey = Product::primary(); $order = [$userKey => 1, 'status' => post('status') ?: 0]; if ($tanggal = post('tanggal')) { $order['tanggal'] = formatTanggal($tanggal, 'Y-m-d'); } else { $order['tanggal'] = date('Y-m-d'); } try { $upload = new Upload('pembayaran'); $order['pembayaran'] = $upload->doUpload(); } catch (Exception $e) { setAlert('error', $e->getMessage()); } if ($id_pelanggan = post($customerKey)) { $order[$customerKey] = $id_pelanggan; } else { $pengguna = ['username' => post('username'), 'email' => post('email'), 'level' => 0, 'aktif' => 1]; $pelanggan = ['nama_lengkap' => post('nama_lengkap'), 'alamat' => post('alamat'), 'kota' => post('kota'), 'telp' => post('telp')]; if ($password = post('password') and $password == post('passconf')) { $pengguna['password'] = $password; } if ($id_pengguna = User::add($pengguna)) { $pelanggan[$userKey] = $id_pengguna; } if ($id_pengguna and $id_pelanggan = Customer::add($pelanggan)) { $order[$customerKey] = $id_pelanggan; } } if ($produks = post($productKey)) { $produk_qty = post('produk_qty'); $produk_arr = []; foreach ($produks as $i => $produk_id) { $produk_arr[$produk_id] = $produk_qty[$i]; } $order['produk'] = serialize($produk_arr); } if ($ongkir = post('ongkir') and $kurir = post('kurir')) { $order['ongkir'] = $ongkir; $order['kurir'] = $kurir; } if ($belanja = post('belanja') and $total = post('total')) { $order['belanja'] = $belanja; $order['total'] = $total; } if ($order['status'] === 0) { $order['potongan'] = post('potongan') ?: 0; $order['bayar'] = post('bayar') ?: 0; $order['kembali'] = post('kembali') ?: 0; if ($order['kembali'] < 0) { $order['kembali'] = 0; } if ($order['bayar'] > 0) { $order['status'] = 1; } } if ($resi = post('resi')) { $order['resi'] = $resi; } if (Order::save($order, $id)) { if ($id) { setAlert('success', 'Berhasil memperbarui data order <b>' . $order['nama'] . '</b>'); } else { setAlert('success', 'Berhasil menambahkan order <b>' . $order['nama'] . '</b>'); } return redirect('admin-shop/orders'); } setAlert('error', 'Terjadi kesalahan dalam penyimpanan order'); return redirect($this->uri->path()); } $order_data = $id ? Order::show($id)->fetchOne() : []; if (!User::is('admin') and ($order_data and $order_data->{$customerKey} != User::current($customerKey))) { return redirect('admin-shop/orders'); } $this->data['data'] = $order_data; return $this->render('order-form', $this->data); break; case 'delete': if (Order::del([Order::primary() => $id])) { setAlert('success', 'Order berhasil terhapus'); } else { setAlert('error', 'Terjadi kesalahan dalam penghapusan order'); } return redirect('admin-shop/orders'); break; default: $filter = !User::is('admin') ? [Customer::primary() => User::current('id_pelanggan')] : []; $this->data['data'] = Order::show($filter, get('sort')); return $this->render('order-table', $this->data); break; } }
<table class="data"> <thead> <tr> <th>Gambar</th> <th>Nama</th> <th>Qty</th> <th>Harga Satuan (Rp.)</th> <th>Subtotal (Rp.)</th> </tr> </thead> <tbody id="tbl-produk"> <?php if ($data) { $products = unserialize($data->produk); $prodId = Product::primary(); $ordererProducts = Product::show($prodId . ' in (' . implode(',', array_keys($products)) . ')'); } if ($data and $ordererProducts->count() > 0) { foreach ($ordererProducts->fetch(false) as $product) { $harga = $product->diskon ?: $product->harga; $diskon = $product->diskon ? '<br>(diskon dari: <del>' . formatAngka($product->harga) . '</del>)' : ''; $subtotal = $products[$product->{$prodId}] * $harga; ?> <tr> <td><span class="thumb" style="background-image: url(<?php echo siteUrl('asset/uploads/' . $product->gambar); ?> );"></span></td> <td><?php echo '<strong>' . $product->nama . '</strong><br>' . $product->keterangan;
public function products($do = '', $id = '') { $this->data['heading'] = 'Administrasi: Produk'; switch ($do) { case 'form': if (post('submit')) { $data = [User::primary() => User::current('id'), 'tgl_input' => date('Y-m-d'), 'id_kategori' => post('kategori'), 'nama' => post('nama'), 'gambar' => post('gambar'), 'tgl_masuk' => formatTanggal(post('tgl_masuk'), 'Y-m-d'), 'stok' => post('stok'), 'harga' => post('harga'), 'berat' => post('berat'), 'diskon' => post('diskon') ?: 0, 'keterangan' => post('keterangan', false)]; try { $upload = new Upload('gambar'); $data['gambar'] = $upload->doUpload(); } catch (Exception $e) { setAlert('error', $e->getMessage()); } if (Product::save($data, $id)) { if ($id) { setAlert('success', 'Berhasil memperbarui data produk <b>' . $data['nama'] . '</b>'); } else { setAlert('success', 'Berhasil menambahkan produk <b>' . $data['nama'] . '</b>'); } redirect('admin-shop/products'); } else { setAlert('error', 'Terjadi kesalahan dalam penyimpanan produk <b>' . $data['nama'] . '</b>'); redirect($this->uri->path()); } } else { if ($id) { $this->data['data'] = Product::show([Product::primary() => $id])->fetchOne(); } return $this->render('product-form', $this->data); } break; case 'delete': if (Product::del([Product::primary() => $id])) { setAlert('success', 'Produk berhasil terhapus'); } else { setAlert('error', 'Terjadi kesalahan dalam penghapusan produk'); } redirect('admin-shop/products'); break; default: $this->data['data'] = Product::show(); return $this->render('product-table', $this->data); break; } }