public function actionHapusDetail($id) { $detail = PenjualanDetail::model()->findByPk($id); PenjualanDiskon::model()->deleteAll('penjualan_detail_id=' . $detail->id); if (!$detail->delete()) { throw new Exception('Gagal hapus detail penjualan'); } }
public function gantiCustomer($customer) { $transaction = $this->dbConnection->beginTransaction(); try { if (!$this->saveAttributes(array('profil_id' => $customer->id))) { throw new Exception('Gagal ubah customer', 500); } $alamat1 = !empty($customer->alamat1) ? $customer->alamat1 : ''; $alamat2 = !empty($customer->alamat2) ? '<br>' . $customer->alamat2 : ''; $alamat3 = !empty($customer->alamat3) ? '<br>' . $customer->alamat3 : ''; /* Ambil data detail */ $penjualanDetails = PenjualanDetail::model()->findAll('penjualan_id=:penjualanId', array('penjualanId' => $this->id)); /* Hapus dan re-insert */ $tabelPenjualanDiskon = PenjualanDiskon::model()->tableName(); $tabelPenjualanDetail = PenjualanDetail::model()->tableName(); Yii::app()->db->createCommand("\n DELETE {$tabelPenjualanDiskon}\n FROM {$tabelPenjualanDiskon}\n INNER JOIN {$tabelPenjualanDetail} ON {$tabelPenjualanDiskon}.penjualan_detail_id = {$tabelPenjualanDetail}.id\n WHERE {$tabelPenjualanDetail}.penjualan_id=:penjualanId\n ")->bindValues(array(':penjualanId' => $this->id))->execute(); PenjualanDetail::model()->deleteAll('penjualan_id=:penjualanId', array('penjualanId' => $this->id)); foreach ($penjualanDetails as $detail) { $barang = Barang::model()->findByPk($detail->barang_id); $this->tambahBarangProc($barang, $detail->qty); } $transaction->commit(); return array('sukses' => true, 'nama' => $customer->nama, 'nomor' => $customer->nomor, 'address' => $alamat1 . $alamat2 . $alamat3); } catch (Exception $ex) { $transaction->rollback(); return array('sukses' => false, 'error' => array('msg' => $ex->getMessage(), 'code' => $ex->getCode())); } }
/** * Update qty detail pembelian via ajax */ public function actionUpdateQty() { $return = array('sukses' => false, 'error' => array('code' => '500', 'msg' => 'Sempurnakan input!')); if (isset($_POST['pk'])) { $pk = $_POST['pk']; $qtyInput = $_POST['value']; $detail = PenjualanDetail::model()->findByPk($pk); if ($qtyInput > 0) { $selisih = $qtyInput - $detail->qty; $return = array('sukses' => false); $penjualan = $this->loadModel($detail->penjualan_id); $return = $penjualan->tambahBarang($detail->barang->barcode, $selisih); } else { /* qty=0 / hapus barang, hanya bisa jika ada otorisasi Admin */ if ($this->isOtorisasiAdmin($detail->penjualan_id)) { PenjualanDiskon::model()->deleteAll('penjualan_detail_id=' . $pk); $this->simpanHapusDetail($detail); $detail->delete(); $return = array('sukses' => true); } else { throw new Exception('Tidak ada otorisasi Admin', 500); } } } $this->renderJSON($return); }