/** * Finds the GoodsMovement model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return GoodsMovement the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = GoodsMovement::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Deletes an existing Sales model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { $model = $this->findModel($id); if ($model->status == Sales::STATUS_DRAFT) { $model->delete(); return $this->redirect(['index']); } $transaction = Yii::$app->db->beginTransaction(); try { // gl $gl = GlHeader::findOne(['reff_type' => GlHeader::REFF_SALES, 'reff_id' => $id]); // movement $movement = $gl != null ? GoodsMovement::findOne(['reff_type' => GoodsMovement::REFF_SALES, 'reff_id' => $id]) : null; // invoice from movement $invoice = $movement != null ? Invoice::findOne(['reff_type' => Invoice::REFF_GOODS_MOVEMENT, 'reff_id' => $movement->id]) : null; // payment invoive $payments = $invoice != null ? $invoice->payments : []; foreach ($payments as $payment) { if (!$payment->delete()) { throw new UserException('Cannot delete payment'); } } if (count($payments) > 0 && $invoice->delete() && $movement->delete() && $gl->reserve() && $model->delete()) { //do nothing $transaction->commit(); return $this->redirect(['index']); } else { throw new UserException('Something error'); } } catch (\Exception $exc) { $transaction->rollBack(); throw $exc; } }
public function revertAdjust() { $gm = GoodsMovement::findOne(['reff_type' => self::REFF_SELF, 'reff_id' => $this->id]); $gm->status = GoodsMovement::STATUS_CANCELED; return $gm->save(); }