public function updateStatusDokumenAsal($status) { switch ($this->asal) { case HutangPiutang::DARI_PEMBELIAN: $model = Pembelian::model()->findByAttributes(array('hutang_piutang_id' => $this->id)); $model->status = $status == HutangPiutang::STATUS_LUNAS ? Pembelian::STATUS_LUNAS : Pembelian::STATUS_HUTANG; $model->update(array('status')); break; case HutangPiutang::DARI_RETUR_BELI: $model = ReturPembelian::model()->findByAttributes(array('hutang_piutang_id' => $this->id)); $model->status = $status == HutangPiutang::STATUS_LUNAS ? ReturPembelian::STATUS_LUNAS : ReturPembelian::STATUS_PIUTANG; $model->update(array('status')); break; case HutangPiutang::DARI_PENJUALAN: $model = Penjualan::model()->findByAttributes(array('hutang_piutang_id' => $this->id)); $model->status = $status == HutangPiutang::STATUS_LUNAS ? Penjualan::STATUS_LUNAS : Penjualan::STATUS_PIUTANG; $model->update(array('status')); break; case HutangPiutang::DARI_RETUR_JUAL: $model = ReturPenjualan::model()->findByAttributes(array('hutang_piutang_id' => $this->id)); $model->status = $status == HutangPiutang::STATUS_LUNAS ? ReturPenjualan::STATUS_LUNAS : ReturPenjualan::STATUS_HUTANG; $model->update(array('status')); break; } }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer $id the ID of the model to be loaded * @return ReturPembelian the loaded model * @throws CHttpException */ public function loadModel($id) { $model = ReturPembelian::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }
public function simpanReturPembelian() { $this->scenario = 'simpanReturBeli'; $transaction = $this->dbConnection->beginTransaction(); try { /* * Save sekaligus mengubah status dari draft jadi piutang */ if ($this->save()) { $details = ReturPembelianDetail::model()->findAll("retur_pembelian_id={$this->id}"); if (is_null($details)) { throw new Exception("Tidak ada detail"); } foreach ($details as $detail) { $inventoryTerpakai = InventoryBalance::model()->returBeli($detail); $count = 1; foreach ($inventoryTerpakai as $layer) { if ($count > 1) { $detailBaru = new ReturPembelianDetail(); $detailBaru->retur_pembelian_id = $this->id; $detailBaru->inventory_balance_id = $layer['id']; $detailBaru->qty = $layer['qtyTerpakai']; if (!$detailBaru->save()) { throw new Exception('Gagal simpan retur pembelian detail (layer baru)', 500); } } else { $detail->qty = $layer['qtyTerpakai']; if (!$detail->save()) { throw new Exception("Gagal simpan detail", 500); } } $count++; } } $jumlahReturBeli = $this->ambilTotal(); /* * Create (piutang) */ $hutang = new HutangPiutang(); $hutang->profil_id = $this->profil_id; $hutang->jumlah = $jumlahReturBeli; $hutang->tipe = HutangPiutang::TIPE_PIUTANG; $hutang->asal = HutangPiutang::DARI_RETUR_BELI; $hutang->nomor_dokumen_asal = $this->nomor; if (!$hutang->save()) { throw new Exception("Gagal simpan hutang"); } /* * Hutang Detail */ $hutangDetail = new HutangPiutangDetail(); $hutangDetail->hutang_piutang_id = $hutang->id; $hutangDetail->keterangan = 'Retur Beli: ' . $this->nomor; $hutangDetail->jumlah = $jumlahReturBeli; if (!$hutangDetail->save()) { throw new Exception("Gagal simpan hutang detail"); } /* * Simpan hutang_id ke retur pembelian */ if (!ReturPembelian::model()->updateByPk($this->id, array('hutang_piutang_id' => $hutang->id)) > 1) { throw new Exception("Gagal simpan hutang_id"); } $transaction->commit(); return ['sukses' => true]; } else { throw new Exception("Gagal Simpan Retur Pembelian"); } } catch (Exception $ex) { $transaction->rollback(); return ['sukses' => false, 'error' => ['msg' => $ex->getMessage(), 'code' => $ex->getCode()]]; } }